skyscraper8/skyscraper8/GS/GSE-BFBS/BfbsGseReader.cs
feyris-tan 124bf9eb49
Some checks failed
🚀 Pack skyscraper8 / make-zip (push) Failing after 1m31s
bfbs crc32
2025-11-07 16:09:27 +01:00

119 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Mpeg2;
using skyscraper8.GSE;
using skyscraper8.GSE.GSE;
using skyscraper8.Skyscraper.IO;
using skyscraper8.Skyscraper.Scraper;
namespace skyscraper8.GS.GSE_BFBS
{
internal class BfbsGseReader : IMisHandler
{
private readonly byte _misId;
private readonly ISubTsHandler _subTsHandler;
private GseLabel lastLabel;
private int frameNo;
public BfbsGseReader(byte misId, ISubTsHandler subTsHandler)
{
_misId = misId;
_subTsHandler = subTsHandler;
}
public void PushFrame(BBHeader bbHeader, ReadOnlySpan<byte> readOnlySpan)
{
frameNo++;
bool validCrc = DvbCrc32.ValidateCrc(readOnlySpan);
if (!validCrc)
return;
if (readOnlySpan[0] == 0 && readOnlySpan[1] == 0 && readOnlySpan[2] == 0 && readOnlySpan[3] == 0)
return;
StreamlikeSpan span = new StreamlikeSpan(readOnlySpan);
while (span.GetAvailableBytes() > 4)
{
byte byteA = span.ReadUInt8();
bool startIndicator = (byteA & 0x80) != 0;
bool endIndicator = (byteA & 0x40) != 0;
int labelTypeIndicator = (byteA & 0x30) >> 4;
if (!startIndicator && !endIndicator && labelTypeIndicator == 0)
{
//padding bits, packet has ended.
return;
}
else
{
int gseLength = (byteA & 0x0f) << 8;
gseLength += span.ReadUInt8();
byte? fragId = null;
if (!startIndicator || !endIndicator)
{
fragId = span.ReadUInt8();
gseLength -= 1;
}
ushort? totalLength = null;
if ((startIndicator) && !endIndicator)
{
totalLength = span.ReadUInt16BE();
gseLength -= 2;
}
GseLabel label = null;
ushort? protocolType = null;
if (startIndicator)
{
protocolType = span.ReadUInt16BE();
gseLength -= 2;
if (labelTypeIndicator == 0)
{
label = new _6byteLabel(span.ReadBytes(6));
gseLength -= 6;
}
else if (labelTypeIndicator == 1)
{
label = new _3byteLabel(span.ReadBytes(3));
gseLength -= 3;
}
else if (labelTypeIndicator == 2)
{
label = BroadcastLabel.GetInstance();
}
else if (labelTypeIndicator == 3)
{
label = lastLabel;
}
}
if (!startIndicator && endIndicator)
gseLength -= 4;
ReadOnlySpan<byte> gseDataBytes = span.ReadBytes(gseLength);
uint? crc32 = null;
if (!startIndicator && endIndicator)
{
crc32 = span.ReadUInt32BE();
}
HandleGseFrame(startIndicator, endIndicator, fragId, totalLength, label, protocolType, gseDataBytes, crc32);
}
}
}
private void HandleGseFrame(bool startIndicator, bool endIndicator, byte? fragId, ushort? totalLength, GseLabel label, ushort? protocolType, ReadOnlySpan<byte> gseDataBytes, uint? crc32)
{
//throw new NotImplementedException();
}
}
}