Decodificatore H.264 per MVC e per le qualità MGS di SVC (#58)

MVC (Annex H): SPS di sottoinsieme, prefissi NAL 14 e slice NAL 20, un
decodificatore per vista con il suo stato, predizione fra viste (riferimenti
della stessa unità di accesso in coda alle liste, modifiche 4 e 5). Escono
tutte le viste, ciascuna con il suo ViewId; VideoFrameDecoder chiede solo la
vista base, che è il video 2D, per le miniature. I flussi MVC della serie non
hanno ricostruzione ufficiale: le prove controllano che ogni vista si
decodifichi pulita e che la vista base resti identica a quella di H.264 puro.

SVC (Annex G): lo strato base sempre; sopra, gli strati di qualità della
stessa risoluzione in CAVLC, decodificati uno sull'altro. Un macroblocco
intra con base_mode_flag somma il residuo accumulato alla ricostruzione dello
strato in cui era intra e per i vicini conta come I_BL. SVCBMT-13 coincide
ora con lo strato L2 ufficiale in tutti i 20 fotogrammi. Scalabilità
spaziale, CGS e CABAC negli strati di miglioramento non sono ancora scritti:
il decodificatore dà lo strato base esatto e dice perché, e SVCBST-19 e
SVCBCT-1 restano inconclusivi con l'elenco di ciò che manca.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 18:15:19 +01:00
1 parent da84c79f7e
commit 898e9d60e7
13 files changed
+1754 -59

No files matched your search

+91 -12
View File
@@ -28,6 +28,10 @@ public enum H264AccessPoint
/// (checked only on conformance streams split into partitions), 4:4:4, separate colour planes, lossless coding and bit
/// depths above 10. Not handled (an <see cref="H264Exception"/>): CABAC in 4:4:4 with the colour planes coded together
/// (its contexts 460–1023 are not transcribed yet), SP and SI slices outside 4:2:0 at 8 bits.
/// Multiview streams (MVC, Annex H): every view, with inter-view prediction (see H264Decoder.Mvc.cs), each frame tagged
/// with its <see cref="H264Frame.ViewId"/>; <see cref="DecodeAllViews"/> false gives the base view only. Scalable streams
/// (SVC, Annex G): the base layer always, and above it the quality refinements of the base resolution (MGS, CAVLC); the
/// other enhancement layers are left out and <see cref="EnhancementLayersSkipped"/> says why (see H264Decoder.Svc.cs).
/// </summary>
public sealed partial class H264Decoder
{
@@ -145,14 +149,21 @@ public sealed partial class H264Decoder
Decode(NalUnits.Unescape(sample[unit], ref rbsp));
}
FinishPicture();
FinishAccessUnit();
}
/// <summary>Decodes one access unit in Annex B form and finishes its picture (as <see cref="DecodeSample"/>).</summary>
public void DecodeAccessUnitAnnexB(ReadOnlySpan<byte> accessUnit)
{
DecodeUnitsAnnexB(accessUnit);
FinishAccessUnit();
}
/// <summary>Finishes the picture of every view of the access unit.</summary>
private void FinishAccessUnit()
{
FinishPicture();
FinishViews();
}
/// <summary>
@@ -214,6 +225,8 @@ public sealed partial class H264Decoder
openField = null;
HandOut(field, activeSps!);
}
FlushViews();
}
/// <summary>
@@ -257,7 +270,44 @@ public sealed partial class H264Decoder
{
case 1:
case 5:
if (viewDecoders.Count > 0)
{
// The base view opens a new access unit: the view components of the last one are complete.
FinishViews();
}
DecodeSlice(nal);
break;
case 14:
if (nal.HeaderLength == 4 && nal.IsSvc)
{
ReadSvcPrefix(nal);
}
else
{
ReadPrefix(nal);
}
break;
case 15:
{
var subset = Sps.ParseSubset(nal.Reader());
subsetSequences[subset.Id] = subset;
multiview |= subset.Mvc is not null;
scalable |= subset.Svc is not null;
break;
}
case 20:
if (nal.HeaderLength == 4 && nal.IsSvc)
{
DecodeSvcSlice(nal);
}
else
{
DecodeViewSlice(nal);
}
break;
case 2:
StorePartitionA(nal);
@@ -275,7 +325,7 @@ public sealed partial class H264Decoder
case 8:
{
var pps = Pps.Parse(nal.Reader(), sequences);
var pps = Pps.Parse(nal.Reader(), sequences, subsetSequences);
pictures[pps.Id] = pps;
break;
}
@@ -389,6 +439,11 @@ public sealed partial class H264Decoder
/// <summary>Whether a picture other than the one being decoded can be predicted from.</summary>
private bool HasReference()
{
if (root is not null && HasInterViewReference())
{
return true;
}
foreach (var p in dpb)
{
if (!ReferenceEquals(p, cur))
@@ -470,6 +525,10 @@ public sealed partial class H264Decoder
}
var picture = cur!;
NoteViewComponent(picture);
picDqId = 0;
picSvcStopped = false;
curStructure = structure;
picHeader = header;
picNalType = nal.Type;
@@ -487,7 +546,7 @@ public sealed partial class H264Decoder
}
// A picture nobody refers to and nobody wants: its header has set the state above, its slices are not decoded.
picSkipped = nal.RefIdc == 0 && Wanted is { } wanted && !wanted(picture.DecodeIndex);
picSkipped = nal.RefIdc == 0 && !NeededByOtherViews && Wanted is { } wanted && !wanted(picture.DecodeIndex);
if (picSkipped)
{
picture.Skipped = true;
@@ -780,7 +839,7 @@ public sealed partial class H264Decoder
{
if (!picture.Skipped && (Wanted?.Invoke(picture.DecodeIndex) ?? true))
{
FrameDecoded?.Invoke(Crop(picture, sps));
FrameDecoded?.Invoke(Crop(picture, sps, viewId));
}
decodeCounter++;
@@ -795,7 +854,7 @@ public sealed partial class H264Decoder
/// 4:2:0 size at the middle value, the rows of a field without its pair the middle value too, as the reference
/// decoder writes them; colour planes coded separately come out as 4:4:4 with the luma bit depth.
/// </summary>
private static H264Frame Crop(Picture p, Sps sps)
private static H264Frame Crop(Picture p, Sps sps, int viewId)
{
var bps = p.BytesPerSample;
var format = sps.ChromaFormatIdc;
@@ -856,7 +915,7 @@ public sealed partial class H264Decoder
}
}
return new H264Frame(width, height, y, cb, cr, p.Poc, p.Period, p.DecodeIndex, sps.MatrixCoefficients, sps.FullRange, format, bitDepthY, bitDepthC);
return new H264Frame(width, height, y, cb, cr, p.Poc, p.Period, p.DecodeIndex, sps.MatrixCoefficients, sps.FullRange, format, bitDepthY, bitDepthC, viewId);
}
/// <summary>A <paramref name="width"/> × <paramref name="height"/> rectangle of a plane <paramref name="stride"/> samples wide.</summary>
@@ -1297,8 +1356,14 @@ public sealed partial class H264Decoder
SwapIfSame(init0, init1);
}
refList[0] = Modify(init0, h.NumRefIdxActiveL0, h.Modifications[0], h, sps);
refList[1] = h.IsB ? Modify(init1, h.NumRefIdxActiveL1, h.Modifications[1], h, sps) : [];
AppendInterView(init0, 0);
refList[0] = Modify(init0, 0, h.NumRefIdxActiveL0, h.Modifications[0], h, sps);
if (h.IsB)
{
AppendInterView(init1, 1);
}
refList[1] = h.IsB ? Modify(init1, 1, h.NumRefIdxActiveL1, h.Modifications[1], h, sps) : [];
}
/// <summary>Lists of a field picture (§8.2.4.2.2, §8.2.4.2.4 and §8.2.4.2.5): frames ordered, then their fields alternated.</summary>
@@ -1342,8 +1407,14 @@ public sealed partial class H264Decoder
SwapIfSame(init0, init1);
}
refList[0] = Modify(init0, h.NumRefIdxActiveL0, h.Modifications[0], h, sps);
refList[1] = h.IsB ? Modify(init1, h.NumRefIdxActiveL1, h.Modifications[1], h, sps) : [];
AppendInterView(init0, 0);
refList[0] = Modify(init0, 0, h.NumRefIdxActiveL0, h.Modifications[0], h, sps);
if (h.IsB)
{
AppendInterView(init1, 1);
}
refList[1] = h.IsB ? Modify(init1, 1, h.NumRefIdxActiveL1, h.Modifications[1], h, sps) : [];
}
/// <summary>PicOrderCnt of a frame entry for the field lists: the smaller of its fields marked short-term.</summary>
@@ -1404,8 +1475,11 @@ public sealed partial class H264Decoder
}
}
/// <summary>The modification of a list (§8.2.4.3), for frames or fields.</summary>
private RefEntry[] Modify(List<RefEntry> initial, int count, List<(int Idc, int Value)> modifications, SliceHeader h, Sps sps)
/// <summary>
/// The modification of list <paramref name="listIndex"/> (§8.2.4.3), for frames or fields; in a non-base view also
/// with the inter-view references (§H.8.2.2.3).
/// </summary>
private RefEntry[] Modify(List<RefEntry> initial, int listIndex, int count, List<(int Idc, int Value)> modifications, SliceHeader h, Sps sps)
{
var list = new RefEntry[count + 1];
for (var i = 0; i < count && i < initial.Count; i++)
@@ -1418,6 +1492,7 @@ public sealed partial class H264Decoder
var maxPicNum = field ? 2 * sps.MaxFrameNum : sps.MaxFrameNum;
var currPicNum = field ? (2 * h.FrameNum) + 1 : h.FrameNum;
var pred = currPicNum;
var viewPred = -1;
var refIdx = 0;
foreach (var (idc, value) in modifications)
{
@@ -1447,6 +1522,10 @@ public sealed partial class H264Decoder
found = new RefEntry(p, field ? q - 1 : -1);
}
}
else if (idc is 4 or 5)
{
found = ModifiedInterView(listIndex, idc, value, ref viewPred);
}
else if (FindLongTerm(value, field, parity, out var p, out var q) && !(ReferenceEquals(p, cur) && q == FieldBit(curStructure)))
{
found = new RefEntry(p, field ? q - 1 : -1);