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

+218 -3
View File
@@ -75,7 +75,36 @@ internal sealed class Sps
internal int HeightInMbs => (2 - (FrameMbsOnly ? 1 : 0)) * HeightInMapUnits;
internal static Sps Parse(RbspReader r)
/// <summary>The multiview extension of a subset SPS (profiles 118, 128 and 134), null otherwise.</summary>
internal MvcExtension? Mvc;
/// <summary>The scalable extension of a subset SPS (profiles 83 and 86), null otherwise.</summary>
internal SvcExtension? Svc;
/// <summary>
/// A subset sequence parameter set (NAL unit 15, §7.3.2.1.3): seq_parameter_set_data() with its whole VUI, then the
/// extension of its profile — scalable (§G.7.3.2.1.4) or multiview (§H.7.3.2.1.4); what follows the parts the decoder
/// uses is not read.
/// </summary>
internal static Sps ParseSubset(RbspReader r)
{
var s = Parse(r, wholeVui: true);
if (s.ProfileIdc is 83 or 86)
{
s.Svc = SvcExtension.Parse(r, s.ChromaArrayType);
}
else if (s.ProfileIdc is 118 or 128 or 134)
{
r.Skip(1); // bit_equal_to_one
s.Mvc = MvcExtension.Parse(r);
}
return s;
}
internal static Sps Parse(RbspReader r) => Parse(r, wholeVui: false);
private static Sps Parse(RbspReader r, bool wholeVui)
{
var s = new Sps { ProfileIdc = (int)r.Read(8) };
r.Skip(8); // constraint flags and reserved bits
@@ -160,11 +189,72 @@ internal sealed class Sps
if (r.ReadFlag())
{
ParseVuiColour(r, s);
if (wholeVui)
{
SkipVuiRest(r);
}
}
return s;
}
/// <summary>The VUI after the colour description (§E.1.1), read only to reach what follows it in a subset SPS.</summary>
private static void SkipVuiRest(RbspReader r)
{
if (r.ReadFlag())
{
_ = r.ReadUe(); // chroma_sample_loc_type_top_field
_ = r.ReadUe(); // chroma_sample_loc_type_bottom_field
}
if (r.ReadFlag())
{
r.Skip(65); // num_units_in_tick, time_scale, fixed_frame_rate_flag
}
var nalHrd = r.ReadFlag();
if (nalHrd)
{
SkipHrd(r);
}
var vclHrd = r.ReadFlag();
if (vclHrd)
{
SkipHrd(r);
}
if (nalHrd || vclHrd)
{
r.Skip(1); // low_delay_hrd_flag
}
r.Skip(1); // pic_struct_present_flag
if (r.ReadFlag())
{
r.Skip(1); // motion_vectors_over_pic_boundaries_flag
for (var i = 0; i < 6; i++)
{
_ = r.ReadUe();
}
}
}
/// <summary>hrd_parameters() (§E.1.2), skipped.</summary>
private static void SkipHrd(RbspReader r)
{
var count = r.ReadUe(31) + 1;
r.Skip(8); // bit_rate_scale, cpb_size_scale
for (var i = 0; i < count; i++)
{
_ = r.ReadUe();
_ = r.ReadUe();
r.Skip(1);
}
r.Skip(20);
}
/// <summary>The start of the VUI (§E.1.1), up to the colour description; the rest is not needed.</summary>
private static void ParseVuiColour(RbspReader r, Sps s)
{
@@ -195,6 +285,123 @@ internal sealed class Sps
}
}
/// <summary>
/// seq_parameter_set_mvc_extension() (§H.7.3.2.1.4) up to the dependencies between views: the views in view order, and
/// for each the views its anchor and non-anchor view components may refer to, per list.
/// </summary>
internal sealed class MvcExtension
{
/// <summary>view_id of each view, by view order index (VOIdx).</summary>
internal int[] ViewIds = [];
/// <summary>anchor_ref_lX[VOIdx][j] by list X: the view_id of each inter-view reference of an anchor view component.</summary>
internal int[][][] AnchorRefs = [[], []];
/// <summary>non_anchor_ref_lX[VOIdx][j] by list X.</summary>
internal int[][][] NonAnchorRefs = [[], []];
/// <summary>The view order index of a view, −1 when the sequence does not have it.</summary>
internal int OrderOf(int viewId) => Array.IndexOf(ViewIds, viewId);
internal static MvcExtension Parse(RbspReader r)
{
var views = r.ReadUe(1023) + 1;
var m = new MvcExtension { ViewIds = new int[views] };
for (var i = 0; i < views; i++)
{
m.ViewIds[i] = r.ReadUe(1023);
}
for (var list = 0; list < 2; list++)
{
m.AnchorRefs[list] = new int[views][];
m.NonAnchorRefs[list] = new int[views][];
m.AnchorRefs[list][0] = [];
m.NonAnchorRefs[list][0] = [];
}
for (var pass = 0; pass < 2; pass++)
{
var refs = pass == 0 ? m.AnchorRefs : m.NonAnchorRefs;
for (var i = 1; i < views; i++)
{
for (var list = 0; list < 2; list++)
{
var count = r.ReadUe(15);
var ids = new int[count];
for (var j = 0; j < count; j++)
{
ids[j] = r.ReadUe(1023);
}
refs[list][i] = ids;
}
}
}
return m;
}
}
/// <summary>seq_parameter_set_svc_extension() (§G.7.3.2.1.4).</summary>
internal sealed class SvcExtension
{
internal bool InterLayerDeblockingFilterControlPresent;
internal int ExtendedSpatialScalabilityIdc;
internal bool ChromaPhaseXPlus1;
internal int ChromaPhaseYPlus1 = 1;
internal bool SeqRefLayerChromaPhaseXPlus1;
internal int SeqRefLayerChromaPhaseYPlus1 = 1;
internal int SeqScaledRefLayerLeft;
internal int SeqScaledRefLayerTop;
internal int SeqScaledRefLayerRight;
internal int SeqScaledRefLayerBottom;
internal bool SeqTcoeffLevelPrediction;
internal bool AdaptiveTcoeffLevelPrediction;
internal bool SliceHeaderRestriction;
internal static SvcExtension Parse(RbspReader r, int chromaArrayType)
{
var e = new SvcExtension
{
InterLayerDeblockingFilterControlPresent = r.ReadFlag(),
ExtendedSpatialScalabilityIdc = (int)r.Read(2),
};
if (chromaArrayType is 1 or 2)
{
e.ChromaPhaseXPlus1 = r.ReadFlag();
}
if (chromaArrayType == 1)
{
e.ChromaPhaseYPlus1 = (int)r.Read(2);
}
if (e.ExtendedSpatialScalabilityIdc == 1)
{
if (chromaArrayType > 0)
{
e.SeqRefLayerChromaPhaseXPlus1 = r.ReadFlag();
e.SeqRefLayerChromaPhaseYPlus1 = (int)r.Read(2);
}
e.SeqScaledRefLayerLeft = r.ReadSe();
e.SeqScaledRefLayerTop = r.ReadSe();
e.SeqScaledRefLayerRight = r.ReadSe();
e.SeqScaledRefLayerBottom = r.ReadSe();
}
e.SeqTcoeffLevelPrediction = r.ReadFlag();
if (e.SeqTcoeffLevelPrediction)
{
e.AdaptiveTcoeffLevelPrediction = r.ReadFlag();
}
e.SliceHeaderRestriction = r.ReadFlag();
return e;
}
}
/// <summary>A picture parameter set (§7.3.2.2).</summary>
internal sealed class Pps
{
@@ -242,7 +449,10 @@ internal sealed class Pps
/// <summary>The scaling lists of the picture, null when the PPS has none (those of the SPS apply).</summary>
internal byte[][]? ScalingLists;
internal static Pps Parse(RbspReader r, IReadOnlyDictionary<int, Sps> sequences)
/// <param name="r">The RBSP after the NAL unit header.</param>
/// <param name="sequences">The SPSs read so far.</param>
/// <param name="subsetSequences">The subset SPSs read so far: a PPS of the enhancement layers or views may refer to one.</param>
internal static Pps Parse(RbspReader r, IReadOnlyDictionary<int, Sps> sequences, IReadOnlyDictionary<int, Sps>? subsetSequences = null)
{
var p = new Pps { Id = r.ReadUe(255), SpsId = r.ReadUe(31) };
p.Cabac = r.ReadFlag();
@@ -274,7 +484,12 @@ internal sealed class Pps
p.Transform8x8Mode = r.ReadFlag();
if (r.ReadFlag())
{
var chroma444 = sequences.TryGetValue(p.SpsId, out var sps) && sps.ChromaFormatIdc == 3;
if (!sequences.TryGetValue(p.SpsId, out var sps))
{
subsetSequences?.TryGetValue(p.SpsId, out sps);
}
var chroma444 = sps is { ChromaFormatIdc: 3 };
var count = 6 + ((chroma444 ? 6 : 2) * (p.Transform8x8Mode ? 1 : 0));
p.ScalingLists = ScalingMatrix.ParseLists(r, count, sps is { ScalingMatrixPresent: true } ? sps.ScalingLists : null);
}