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

@@ -40,7 +40,7 @@ del riferimento.
## I flussi verificati
`H264ConformanceTests.FramesMatchTheReference`: a immagini intere, 4:2:0 a 8 bit, sulla sequenza «Foreman».
`H264ConformanceTests.FramesMatchTheReference`, tutti 4:2:0 a 8 bit. A immagini intere, sulla sequenza «Foreman»:
| Flusso | Profilo, formato | Che cosa prova |
|---|---|---|
@@ -57,17 +57,8 @@ del riferimento.
| `FRExt3_Panasonic_E` | High, CIF, 11 | CABAC, trasformata 8×8 e 4×4 adattiva, matrici di quantizzazione, slice B con diretta temporale |
| `HCAFR1_HHI_C` | High, CIF, 10 | CABAC con inizializzazione adattiva (`cabac_init_idc` 0, 1, 2), trasformata 8×8, matrici predefinite, slice B |
`H264ConformanceTests.SvcBaseLayerMatchesTheReference`: lo strato base dei tre flussi SVC qui sotto, con
`riferimento-base.md5`.
## I flussi delle parti ancora da fare
`H264ConformanceTests.FeaturesStillToDo`: il decodificatore oggi rifiuta queste parti con un'`H264Exception` (o, per SVC
e MVC, dà solo lo strato o la vista base), e la prova è **inconclusiva** con il messaggio «da fare: …», così la serie
resta verde e il vuoto si vede. Quando una di queste coincide con il riferimento la prova **fallisce** apposta, per
ricordare di spostarla fra i flussi verificati.
**Immagini interlacciate** (campi, PAFF, MBAFF), 4:2:0 a 8 bit:
Immagini interlacciate (campi, PAFF, MBAFF). Ogni fotogramma esce con i due campi intercalati riga per riga; un campo
rimasto senza il suo compagno esce da solo, con le righe dell'altro campo grigie (128).
| Flusso | Profilo, formato | Che cosa prova |
|---|---|---|
@@ -89,6 +80,19 @@ ricordare di spostarla fra i flussi verificati.
| `MR6_BT_B` | Extended, 192×128, 60 | campi, CAVLC, POC di tipo 1, MMCO, riordino, numero di slice variabile |
| `MR9_BT_B` | Main, 192×128, 58 | MBAFF e PAFF, CABAC, slice B, POC di tipo 1, MMCO, riordino |
Nessuno di questi flussi usa la predizione pesata, i salti di `frame_num` o gli I_PCM: quelle parti del decodificatore
non hanno qui un riferimento ufficiale.
`H264ConformanceTests.SvcBaseLayerMatchesTheReference`: lo strato base dei tre flussi SVC qui sotto, con
`riferimento-base.md5`.
## I flussi delle parti ancora da fare
`H264ConformanceTests.FeaturesStillToDo`: il decodificatore oggi rifiuta queste parti con un'`H264Exception` (o, per SVC
e MVC, dà solo lo strato o la vista base), e la prova è **inconclusiva** con il messaggio «da fare: …», così la serie
resta verde e il vuoto si vede. Quando una di queste coincide con il riferimento la prova **fallisce** apposta, per
ricordare di spostarla fra i flussi verificati.
**Strumenti di Baseline ed Extended**, QCIF 4:2:0 a 8 bit:
| Flusso | Profilo, formato | Che cosa prova |
@@ -6,11 +6,12 @@ namespace Febe.Tests.Codecs.H264;
/// <summary>
/// The decoder against streams of the ITU-T H.264.1 conformance suite: every output frame, in output order, must have the
/// MD5 of the corresponding frame of the official reconstruction (see Conformita/LEGGIMI.md). None of the verified streams
/// uses weighted prediction, memory management operations, long-term references, gaps in frame_num, POC type 1, I_PCM or
/// CABAC pictures of several slices: those parts of the decoder have no reference MD5 here (some are reached by
/// <see cref="H264SyntheticStreamTests"/>, without an official reference). The streams of the parts the decoder does not
/// handle yet (interlaced pictures, FMO and ASO, SP slices, 4:2:2, more than 8 bits, 4:0:0, SVC, MVC) are in
/// MD5 of the corresponding frame of the official reconstruction (see Conformita/LEGGIMI.md). The verified streams include
/// interlaced video — field pictures, PAFF, MBAFF, both together — with memory management operations, long-term
/// references and POC type 1 on fields (MR6_BT_B, MR9_BT_B). None of them uses weighted prediction, gaps in frame_num or
/// I_PCM: those parts of the decoder have no reference MD5 here (some are reached by <see cref="H264SyntheticStreamTests"/>,
/// without an official reference). The streams of the parts the decoder does not handle yet (FMO and ASO, SP slices,
/// 4:2:2, more than 8 bits, 4:0:0, SVC, MVC) are in
/// <see cref="FeaturesStillToDo"/>: they are inconclusive until the feature is written, and fail as soon as one of them
/// matches its reference without having been moved among the verified ones.
/// </summary>
@@ -30,6 +31,23 @@ public sealed class H264ConformanceTests
[DataRow("FRExt1_Panasonic_D")]
[DataRow("FRExt3_Panasonic_E")]
[DataRow("HCAFR1_HHI_C")]
[DataRow("CVFI1_SVA_C")]
[DataRow("FI1_Sony_E")]
[DataRow("CAFI1_SVA_C")]
[DataRow("Sharp_MP_PAFF_1r2")]
[DataRow("HCAFF1_HHI_B")]
[DataRow("CVMAQP3_Sony_D")]
[DataRow("FREXT02_JVC_C")]
[DataRow("CAMACI3_Sony_C")]
[DataRow("CAMASL3_Sony_B")]
[DataRow("HCAMFF1_HHI_B")]
[DataRow("FREXT01_JVC_D")]
[DataRow("CAPAMA3_Sand_F")]
[DataRow("cama1_vtc_c")]
[DataRow("FRExt2_Panasonic_C")]
[DataRow("FRExt4_Panasonic_B")]
[DataRow("MR6_BT_B")]
[DataRow("MR9_BT_B")]
public void FramesMatchTheReference(string name) => AssertMatches(name, "riferimento.md5");
[TestMethod]
@@ -44,23 +62,6 @@ public sealed class H264ConformanceTests
}
[TestMethod]
[DataRow("CVFI1_SVA_C", "immagini a campi (CAVLC)")]
[DataRow("FI1_Sony_E", "immagini a campi, solo slice I (CAVLC)")]
[DataRow("CAFI1_SVA_C", "immagini a campi (CABAC)")]
[DataRow("Sharp_MP_PAFF_1r2", "PAFF, fotogrammi e coppie di campi (CAVLC, POC di tipo 1)")]
[DataRow("HCAFF1_HHI_B", "PAFF (CABAC, High)")]
[DataRow("CVMAQP3_Sony_D", "MBAFF (CAVLC)")]
[DataRow("FREXT02_JVC_C", "MBAFF (CAVLC, High)")]
[DataRow("CAMACI3_Sony_C", "MBAFF con predizione intra vincolata (CABAC)")]
[DataRow("CAMASL3_Sony_B", "MBAFF con quattro slice per immagine (CABAC)")]
[DataRow("HCAMFF1_HHI_B", "MBAFF (CABAC, High)")]
[DataRow("FREXT01_JVC_D", "MBAFF (CABAC, High)")]
[DataRow("CAPAMA3_Sand_F", "PAFF e MBAFF insieme (CABAC)")]
[DataRow("cama1_vtc_c", "campi con il campo inferiore per primo e MBAFF (CABAC)")]
[DataRow("FRExt2_Panasonic_C", "campi e MBAFF con MMCO e matrici di quantizzazione (CAVLC, High)")]
[DataRow("FRExt4_Panasonic_B", "campi e MBAFF con matrici di quantizzazione (CABAC, High)")]
[DataRow("MR6_BT_B", "campi con MMCO e riordino dei riferimenti (Extended, POC di tipo 1)")]
[DataRow("MR9_BT_B", "campi e MBAFF con MMCO e riordino dei riferimenti (CABAC, POC di tipo 1)")]
[DataRow("FM2_SVA_C", "FMO (gruppi di slice di tipo 0) e ASO")]
[DataRow("FM1_FT_E", "FMO (gruppi di slice di tipo 1, 2, 3 e 4)")]
[DataRow("FM1_BT_B", "FMO (gruppi di slice di tipo 0-6) e ASO")]
@@ -123,6 +124,12 @@ public sealed class H264ConformanceTests
[DataRow("FRExt1_Panasonic_D")]
[DataRow("FRExt3_Panasonic_E")]
[DataRow("HCAFR1_HHI_C")]
[DataRow("CAFI1_SVA_C")]
[DataRow("Sharp_MP_PAFF_1r2")]
[DataRow("CAPAMA3_Sand_F")]
[DataRow("cama1_vtc_c")]
[DataRow("HCAMFF1_HHI_B")]
[DataRow("MR9_BT_B")]
public void PicturesLeftOutDoNotChangeTheOthers(string name)
{
// Pictures nobody refers to and nobody wants are not reconstructed (only their headers count): every frame
@@ -22,10 +22,16 @@ public sealed class H264RobustnessTests
[DataRow("FRExt1_Panasonic_D", 2650)]
[DataRow("FRExt3_Panasonic_E", 2651)]
[DataRow("HCAFR1_HHI_C", 2652)]
[DataRow("FI1_Sony_E", 2653)]
[DataRow("CVMAQP3_Sony_D", 2654)]
[DataRow("CAMACI3_Sony_C", 2655)]
[DataRow("CAMASL3_Sony_B", 2656)]
[DataRow("MR6_BT_B", 2657)]
[DataRow("MR9_BT_B", 2658)]
[DataRow("CAPAMA3_Sand_F", 2659)]
public void DamagedStreamsGiveFramesOrAnH264Exception(string name, int seed)
{
var folder = Path.Combine(AppContext.BaseDirectory, "Media", "H264", "Conformita", name);
var data = File.ReadAllBytes(Directory.EnumerateFiles(folder).Single(f => Path.GetExtension(f) is ".264" or ".jsv" or ".avc"));
var data = File.ReadAllBytes(H264ConformanceTests.StreamFile(H264ConformanceTests.Folder(name)));
var random = new Random(seed);
for (var trial = 0; trial < 60; trial++)
{
@@ -48,6 +48,30 @@ public sealed class H264TablesTests
{
CollectionAssert.AreEquivalent(Enumerable.Range(0, 16).ToArray(), H264Tables.ZigZag4x4.Select(b => (int)b).ToArray());
CollectionAssert.AreEquivalent(Enumerable.Range(0, 64).ToArray(), H264Tables.ZigZag8x8.Select(b => (int)b).ToArray());
CollectionAssert.AreEquivalent(Enumerable.Range(0, 16).ToArray(), H264Tables.FieldScan4x4.Select(b => (int)b).ToArray());
CollectionAssert.AreEquivalent(Enumerable.Range(0, 64).ToArray(), H264Tables.FieldScan8x8.Select(b => (int)b).ToArray());
}
[TestMethod]
public void FieldScansGoDownBeforeAcross()
{
// Field scans (Tables 8-13 and 8-14) favour vertical frequencies: the first coefficients after DC are the ones
// below it.
CollectionAssert.AreEqual(new byte[] { 0, 4, 1, 8, 12, 5 }, H264Tables.FieldScan4x4[..6]);
CollectionAssert.AreEqual(new byte[] { 0, 8, 16, 1, 9, 24, 32, 17 }, H264Tables.FieldScan8x8[..8]);
Assert.AreEqual(63, H264Tables.FieldScan8x8[63]);
}
[TestMethod]
public void FieldSignificanceContextsOf8x8BlocksMatchTheStandard()
{
// Table 9-43, ctxIdxInc of significant_coeff_flag for field coded 8×8 blocks: 63 values from 0 to 14.
var table = H264Tables.SignificantContext8x8Field;
Assert.AreEqual(63, table.Length);
Assert.AreEqual(0, table[0]);
Assert.AreEqual(14, table.Max());
Assert.AreEqual(14, table[62]);
Assert.AreEqual(H264Tables.SignificantContext8x8.Length, table.Length);
}
[TestMethod]