using Febe.Core.Codecs.H264; namespace Febe.Tests.Codecs.H264; /// The constant tables of the decoder: shape checks and a few values taken from the text of the standard. [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()); CollectionAssert.AreEquivalent(Enumerable.Range(0, 16).ToArray(), H264Tables.CbpIntraNoChroma.Select(b => (int)b).ToArray()); CollectionAssert.AreEquivalent(Enumerable.Range(0, 16).ToArray(), H264Tables.CbpInterNoChroma.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]); } }