Refactor ATSC PLP Parser to use Span<byte> instead of byte[]
All checks were successful
🚀 Pack skyscraper8 / make-zip (push) Successful in 56s

This commit is contained in:
feyris-tan 2026-05-28 22:28:19 +02:00
parent ef903cba96
commit 1843d914ed
2 changed files with 40 additions and 14 deletions

View File

@ -13,10 +13,9 @@ namespace skyscraper8.Atsc.A322
{ {
internal class AtscPlpBasebandParser internal class AtscPlpBasebandParser
{ {
internal void PushBbframe(byte[] payload) internal void PushBbframe(Span<byte> payload)
{ {
MemoryStream ms = new MemoryStream(payload); byte a = payload[0];
byte a = ms.ReadUInt8();
bool mode = ((a >> 7) & 0x01) != 0; bool mode = ((a >> 7) & 0x01) != 0;
int pointer = (a & 0x7f); int pointer = (a & 0x7f);
int addToPointer; int addToPointer;
@ -29,7 +28,7 @@ namespace skyscraper8.Atsc.A322
else else
{ {
addToPointer = 2; addToPointer = 2;
byte b = ms.ReadUInt8(); byte b = payload[1];
pointer |= (((b >> 2) & 0x3F) << 7); pointer |= (((b >> 2) & 0x3F) << 7);
OfiDescription ofi = (OfiDescription)(b & 0x3); OfiDescription ofi = (OfiDescription)(b & 0x3);
switch (ofi) switch (ofi)
@ -37,29 +36,45 @@ namespace skyscraper8.Atsc.A322
case OfiDescription.NoExtensionMode: case OfiDescription.NoExtensionMode:
break; break;
case OfiDescription.LongExtensionMode: case OfiDescription.LongExtensionMode:
byte lc = ms.ReadUInt8(); byte lc = payload[2];
addToPointer++; addToPointer++;
int extType = (lc >> 5) & 0x7; int extType = (lc >> 5) & 0x7;
int extLen = (lc) & 0x1F; int extLen = (lc) & 0x1F;
byte ld = ms.ReadUInt8(); byte ld = payload[3];
addToPointer++; addToPointer++;
extLen |= (((ld) & 0xFF) << 5); extLen |= (((ld) & 0xFF) << 5);
// see A322-2026-04-Physical-Layer-Protocol.pdf, Table 5.2 // see A322-2026-04-Physical-Layer-Protocol.pdf, Table 5.2
//0 = counter //0 = counter
//7 = padding //7 = padding
byte[] extensionFiled = ms.ReadBytes(extLen); Span<byte> extensionField = payload.Slice(4, extLen);
addToPointer += extLen; addToPointer += extLen;
if (extType != 7) HandleExtension(true, extType, extensionField);
{ break;
throw new NotImplementedException(String.Format("ATSC 3.0 BBFRAME Padding Type {0} not yet implemented.", extType)); case OfiDescription.ShortExtensionMode:
} byte lc2 = payload[2];
addToPointer++;
int extType2 = (lc2 >> 5) & 0x07;
int extLen2 = (lc2) & 0x1f;
Span<byte> extensionField2 = payload.Slice(4, extLen2);
addToPointer += extLen2;
HandleExtension(false, extType2, extensionField2);
break; break;
default: default:
throw new NotImplementedException(String.Format("{0} is not yet implemented in the ATSC 3.0 BBFrame Parser.", ofi)); throw new NotImplementedException(String.Format("{0} is not yet implemented in the ATSC 3.0 BBFrame Parser.", ofi));
} }
} }
while (ms.GetAvailableBytes() > 0) Span<byte> bbHeaderStripped = payload.Slice(addToPointer);
if (pointer + 4 > bbHeaderStripped.Length)
{
Console.WriteLine("BBFRame, offset: {0}, less than four bytes left.", pointer);
}
else
{
Console.WriteLine("BBFRame, offset: {0}, first bytes: 0x{1:X2}{2:X2}{3:X2}{4:X2}", pointer, bbHeaderStripped[pointer], bbHeaderStripped[pointer + 1], bbHeaderStripped[pointer + 2], bbHeaderStripped[pointer + 3]);
}
/*while (ms.GetAvailableBytes() > 0)
{ {
switch(stateMachineState) switch(stateMachineState)
{ {
@ -169,7 +184,17 @@ namespace skyscraper8.Atsc.A322
default: default:
throw new NotImplementedException(String.Format("ATSC PLP Baseband Parser State machine state #{0}", stateMachineState)); throw new NotImplementedException(String.Format("ATSC PLP Baseband Parser State machine state #{0}", stateMachineState));
} }
}*/
}
private void HandleExtension(bool isLongExtension, int extType, Span<byte> extensionField)
{
if (extType == 7)
{
//Padding
return;
} }
throw new NotImplementedException();
} }
private void DeliverPacket(AlpPacket? currentAlpPacket, byte[]? neededBuffer) private void DeliverPacket(AlpPacket? currentAlpPacket, byte[]? neededBuffer)

View File

@ -94,7 +94,8 @@ namespace skyscraper8.Atsc.A324
} }
else if (plp <= 63) else if (plp <= 63)
{ {
HandlePlp(plp, rtpPacket.RtpPayload); Span<byte> rtpPayloadSpan = new Span<byte>(rtpPacket.RtpPayload);
HandlePlp(plp, rtpPayloadSpan);
return; return;
} }
} }
@ -149,7 +150,7 @@ namespace skyscraper8.Atsc.A324
} }
private AtscPlpBasebandParser[] plps; private AtscPlpBasebandParser[] plps;
private void HandlePlp(ushort plp, byte[] rtpPayload) private void HandlePlp(ushort plp, Span<byte> rtpPayload)
{ {
if (plps == null) if (plps == null)
{ {