Files
Febe/tests/Febe.Tests/Media/H264/H264TablesTests.cs
T
Alby96andClaude Opus 5.5 9c626f67ec 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>
2026-11-18 16:05:41 +01:00

93 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Febe.Core.Codecs.H264;
namespace Febe.Tests.Codecs.H264;
/// <summary>The constant tables of the decoder: shape checks and a few values taken from the text of the standard.</summary>
[TestClass]
public sealed class H264TablesTests
{
[TestMethod]
public void VariableLengthCodesArePrefixFree()
{
foreach (var (name, codes) in CavlcTables.AllCodeTables())
{
var present = codes.Where(c => c is not null).Select(c => c!).ToList();
for (var i = 0; i < present.Count; i++)
{
for (var j = 0; j < present.Count; j++)
{
if (i != j)
{
Assert.IsFalse(present[j].StartsWith(present[i], StringComparison.Ordinal), $"{name}: {present[i]} è prefisso di {present[j]}");
}
}
}
}
}
[TestMethod]
public void DeblockingTablesCoverEveryIndex()
{
Assert.AreEqual(52, H264Tables.Alpha.Length);
Assert.AreEqual(52, H264Tables.Beta.Length);
Assert.AreEqual(52 * 3, H264Tables.Tc0.Length);
Assert.AreEqual(25, H264Tables.Tc0[(51 * 3) + 2]);
Assert.AreEqual(52, H264Tables.ChromaQp.Length);
Assert.AreEqual(39, H264Tables.ChromaQp[51]);
}
[TestMethod]
public void CodedBlockPatternTablesArePermutations()
{
CollectionAssert.AreEquivalent(Enumerable.Range(0, 48).ToArray(), H264Tables.CbpIntra.Select(b => (int)b).ToArray());
CollectionAssert.AreEquivalent(Enumerable.Range(0, 48).ToArray(), H264Tables.CbpInter.Select(b => (int)b).ToArray());
}
[TestMethod]
public void ScansArePermutations()
{
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]
[DataRow(-1, 402, -17, 120)]
[DataRow(0, 11, 23, 33)]
[DataRow(1, 417, 17, -10)] // once taken from the field table by mistake
[DataRow(1, 426, -5, 71)]
[DataRow(1, 178, 102, -94)]
[DataRow(2, 435, -10, 79)]
[DataRow(2, 165, -22, 127)]
public void ContextInitialisationMatchesTheStandard(int idc, int ctx, int m, int n)
{
var table = idc < 0 ? CabacTables.InitI : CabacTables.InitPB[idc];
Assert.AreEqual(CabacTables.ContextCount * 2, table.Length);
Assert.AreEqual(m, table[ctx * 2]);
Assert.AreEqual(n, table[(ctx * 2) + 1]);
}
}