Decodificatore H.264 per le immagini interlacciate: campi, PAFF e MBAFF (#58)

I video interlacciati (telecamere, registrazioni televisive) davano un'H264Exception e restavano
senza miniatura. Ora il decodificatore li gestisce tutti, scritto in casa sulla specifica:

- campi e coppie di campi (PAFF) e macroblocchi adattivi (MBAFF) su una sola griglia di
  fotogramma, con origine e passo per macroblocco, senza copie in piani separati;
- POC, liste dei riferimenti, riordino e marcatura (MMCO, lungo termine) per campi;
- vicini MBAFF della tabella 6-4, vettori di moto e riferimenti scalati fra campo e
  fotogramma, diretta spaziale e temporale con il colocato di campo, pesi impliciti per parità;
- contesti CABAC e CAVLC di campo, scansioni di campo 4x4 e 8x8;
- deblocking per campi e per coppie MBAFF;
- uscita con i due campi intercalati; un campo rimasto solo esce con l'altro a 128.

I 17 flussi interlacciati della serie ITU-T H.264.1 passano da «da fare» ai flussi verificati e
coincidono fotogramma per fotogramma con la ricostruzione ufficiale; i 12 a immagini intere e lo
strato base SVC coincidono ancora e non sono più lenti. Prove di robustezza e di immagini saltate
anche sui flussi interlacciati; LEGGIMI.md e una riga in decisioni.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 16:05:41 +01:00
1 parent 25a6d9ce1d
commit 9c626f67ec
15 files changed
+1909 -613

No files matched your search

@@ -10,7 +10,7 @@ public sealed partial class H264Decoder
private int ReadMbSkip()
{
var inc = (availA && !IsSkip(mb - 1) ? 1 : 0) + (availB && !IsSkip(mb - widthInMbs) ? 1 : 0);
var inc = (mbA >= 0 && !IsSkip(mbA) ? 1 : 0) + (mbB >= 0 && !IsSkip(mbB) ? 1 : 0);
return cabac.Decision((sh.IsB ? 24 : 11) + inc);
}
@@ -25,8 +25,8 @@ public sealed partial class H264Decoder
{
case SliceType.I:
{
var inc = (availA && mbKind[mb - 1] is not (MbKind.I4x4 or MbKind.I8x8) ? 1 : 0)
+ (availB && mbKind[mb - widthInMbs] is not (MbKind.I4x4 or MbKind.I8x8) ? 1 : 0);
var inc = (mbA >= 0 && mbKind[mbA] is not (MbKind.I4x4 or MbKind.I8x8) ? 1 : 0)
+ (mbB >= 0 && mbKind[mbB] is not (MbKind.I4x4 or MbKind.I8x8) ? 1 : 0);
if (cabac.Decision(3 + inc) == 0)
{
return 0;
@@ -49,8 +49,8 @@ public sealed partial class H264Decoder
return 5 + (cabac.Decision(17) == 0 ? 0 : IntraMbTypeSuffix(17, intraSlice: false));
default:
{
var inc = (availA && mbKind[mb - 1] is not (MbKind.BSkip or MbKind.BDirect) ? 1 : 0)
+ (availB && mbKind[mb - widthInMbs] is not (MbKind.BSkip or MbKind.BDirect) ? 1 : 0);
var inc = (mbA >= 0 && mbKind[mbA] is not (MbKind.BSkip or MbKind.BDirect) ? 1 : 0)
+ (mbB >= 0 && mbKind[mbB] is not (MbKind.BSkip or MbKind.BDirect) ? 1 : 0);
if (cabac.Decision(27 + inc) == 0)
{
return 0;
@@ -202,7 +202,9 @@ public sealed partial class H264Decoder
return 0;
}
return cur!.RefIdx[(((list * mbCount) + n) * 4) + b8] > 0 ? 1 : 0;
// A field neighbour of a frame macroblock counts in fields: index 1 is still the first frame (§9.3.3.1.1.6).
var zero = mbaff && !mbField && cur!.MbField[n] ? 1 : 0;
return cur!.RefIdx[(((list * mbCount) + n) * 4) + b8] > zero ? 1 : 0;
}
private int ReadMvd(int list, int comp, int px, int py)
@@ -256,7 +258,14 @@ public sealed partial class H264Decoder
return 0;
}
return mvdAbs[(((((list * mbCount) + n) * 16) + blk) * 2) + comp];
int value = mvdAbs[(((((list * mbCount) + n) * 16) + blk) * 2) + comp];
if (comp == 1 && mbaff && cur!.MbField[n] != mbField)
{
// Vertical differences in the units of the current macroblock (§9.3.3.1.1.7).
value = mbField ? value / 2 : value * 2;
}
return value;
}
private bool ReadTransform8x8()
@@ -266,7 +275,7 @@ public sealed partial class H264Decoder
return rd.ReadFlag();
}
var inc = (availA && mbT8[mb - 1] ? 1 : 0) + (availB && mbT8[mb - widthInMbs] ? 1 : 0);
var inc = (mbA >= 0 && mbT8[mbA] ? 1 : 0) + (mbB >= 0 && mbT8[mbB] ? 1 : 0);
return cabac.Decision(399 + inc) != 0;
}
@@ -295,7 +304,7 @@ public sealed partial class H264Decoder
return rd.ReadUe(3);
}
var inc = ChromaModeCond(availA, mb - 1) + ChromaModeCond(availB, mb - widthInMbs);
var inc = ChromaModeCond(mbA) + ChromaModeCond(mbB);
if (cabac.Decision(64 + inc) == 0)
{
return 0;
@@ -309,8 +318,8 @@ public sealed partial class H264Decoder
return cabac.Decision(67) == 0 ? 2 : 3;
}
private int ChromaModeCond(bool available, int n) =>
available && IsIntra(n) && mbKind[n] != MbKind.IPcm && chromaModes[n] != 0 ? 1 : 0;
private int ChromaModeCond(int n) =>
n >= 0 && IsIntra(n) && mbKind[n] != MbKind.IPcm && chromaModes[n] != 0 ? 1 : 0;
private int ReadCbp(bool intra)
{
@@ -331,7 +340,8 @@ public sealed partial class H264Decoder
}
else
{
condA = availA && ((mbCbp[mb - 1] >> (b8 + 1)) & 1) == 0 ? 1 : 0;
var n = Locate(-1, by * 8, 16, out var yM);
condA = n >= 0 && ((mbCbp[n] >> ((((yM & 15) >> 3) * 2) + 1)) & 1) == 0 ? 1 : 0;
}
if (by == 1)
@@ -340,14 +350,15 @@ public sealed partial class H264Decoder
}
else
{
condB = availB && ((mbCbp[mb - widthInMbs] >> (b8 + 2)) & 1) == 0 ? 1 : 0;
var n = Locate(bx * 8, -1, 16, out _);
condB = n >= 0 && ((mbCbp[n] >> (b8 + 2)) & 1) == 0 ? 1 : 0;
}
cbp |= cabac.Decision(73 + condA + (2 * condB)) << b8;
}
var a = availA ? mbCbp[mb - 1] >> 4 : 0;
var b = availB ? mbCbp[mb - widthInMbs] >> 4 : 0;
var a = mbA >= 0 ? mbCbp[mbA] >> 4 : 0;
var b = mbB >= 0 ? mbCbp[mbB] >> 4 : 0;
var inc = (a != 0 ? 1 : 0) + (b != 0 ? 2 : 0);
if (cabac.Decision(77 + inc) != 0)
{
@@ -556,9 +567,9 @@ public sealed partial class H264Decoder
{
nA = nz[(mb * 24) + raster - 1];
}
else if (availA)
else if (Locate(-1, by * 4, 16, out var yM) is var a and >= 0)
{
nA = nz[((mb - 1) * 24) + raster + 3];
nA = nz[(a * 24) + (((yM & 15) >> 2) * 4) + 3];
}
else
{
@@ -569,9 +580,9 @@ public sealed partial class H264Decoder
{
nB = nz[(mb * 24) + raster - 4];
}
else if (availB)
else if (Locate(bx * 4, -1, 16, out _) is var b and >= 0)
{
nB = nz[((mb - widthInMbs) * 24) + raster + 12];
nB = nz[(b * 24) + 12 + bx];
}
else
{
@@ -591,9 +602,9 @@ public sealed partial class H264Decoder
{
nA = nz[(mb * 24) + baseIndex + b - 1];
}
else if (availA)
else if (Locate(-1, by * 4, 8, out var yM) is var a and >= 0)
{
nA = nz[((mb - 1) * 24) + baseIndex + b + 1];
nA = nz[(a * 24) + baseIndex + (((yM & 7) >> 2) * 2) + 1];
}
else
{
@@ -604,9 +615,9 @@ public sealed partial class H264Decoder
{
nB = nz[(mb * 24) + baseIndex + b - 2];
}
else if (availB)
else if (Locate(bx * 4, -1, 8, out _) is var n and >= 0)
{
nB = nz[((mb - widthInMbs) * 24) + baseIndex + b + 2];
nB = nz[(n * 24) + baseIndex + 2 + bx];
}
else
{
@@ -636,24 +647,26 @@ public sealed partial class H264Decoder
}
}
// Blocks of field macroblocks have their own significance contexts (Table 9-34).
int sigBase, lastBase, absBase;
if (cat == 5)
{
(sigBase, lastBase, absBase) = (402, 417, 426);
(sigBase, lastBase, absBase) = mbField ? (436, 451, 426) : (402, 417, 426);
}
else
{
sigBase = 105 + SigCatOffset[cat];
lastBase = 166 + SigCatOffset[cat];
sigBase = (mbField ? 277 : 105) + SigCatOffset[cat];
lastBase = (mbField ? 338 : 166) + SigCatOffset[cat];
absBase = 227 + AbsCatOffset[cat];
}
var sig8x8 = mbField ? H264Tables.SignificantContext8x8Field : H264Tables.SignificantContext8x8;
Span<bool> significant = stackalloc bool[64];
significant.Clear();
var last = -1;
for (var i = 0; i < maxNum - 1; i++)
{
var ctx = cat == 5 ? H264Tables.SignificantContext8x8[i] : cat == 3 ? Math.Min(i, 2) : i;
var ctx = cat == 5 ? sig8x8[i] : cat == 3 ? Math.Min(i, 2) : i;
if (cabac.Decision(sigBase + ctx) == 0)
{
continue;
@@ -743,12 +756,12 @@ public sealed partial class H264Decoder
switch (cat)
{
case 0:
if (!(left ? availA : availB))
n = left ? mbA : mbB;
if (n < 0)
{
return currentIntra ? 1 : 0;
}
n = left ? mb - 1 : mb - widthInMbs;
if (mbKind[n] == MbKind.IPcm)
{
return 1;
@@ -756,12 +769,12 @@ public sealed partial class H264Decoder
return mbKind[n] == MbKind.I16x16 ? cbfDc[n] & 1 : 0;
case 3:
if (!(left ? availA : availB))
n = left ? mbA : mbB;
if (n < 0)
{
return currentIntra ? 1 : 0;
}
n = left ? mb - 1 : mb - widthInMbs;
if (mbKind[n] == MbKind.IPcm)
{
return 1;
@@ -778,13 +791,13 @@ public sealed partial class H264Decoder
}
else
{
if (!(left ? availA : availB))
n = left ? Locate(-1, by * 4, 8, out var yM) : Locate(bx * 4, -1, 8, out yM);
if (n < 0)
{
return currentIntra ? 1 : 0;
}
n = left ? mb - 1 : mb - widthInMbs;
index = left ? blk + 1 : blk + 2;
index = left ? ((((yM & 7) >> 2) * 2) + 1) : 2 + bx;
}
if (mbKind[n] == MbKind.IPcm)
@@ -805,13 +818,13 @@ public sealed partial class H264Decoder
}
else
{
if (!(left ? availA : availB))
n = left ? Locate(-1, by * 4, 16, out var yM) : Locate(bx * 4, -1, 16, out yM);
if (n < 0)
{
return currentIntra ? 1 : 0;
}
n = left ? mb - 1 : mb - widthInMbs;
index = left ? blk + 3 : blk + 12;
index = left ? ((((yM & 15) >> 2) * 4) + 3) : 12 + bx;
}
if (mbKind[n] == MbKind.IPcm)