namespace Febe.Core.Codecs.H265; /// /// Constant tables of ITU-T H.265, transcribed from the text of the Recommendation: scan orders (§6.5.3–6.5.5), default /// scaling lists (table 7-6), intra prediction angles (tables 8-4 and 8-5), the 4:2:2 chroma intra modes (table 8-3), the chroma QP mapping (table 8-10), the /// thresholds of the deblocking filter (table 8-12) and the transform matrices (§8.6.4.2). /// internal static class H265Tables { /// intraPredAngle by intra prediction mode (table 8-4); modes 0 (planar) and 1 (DC) have none. internal static readonly sbyte[] IntraPredAngle = [ 0, 0, 32, 26, 21, 17, 13, 9, 5, 2, 0, -2, -5, -9, -13, -17, -21, -26, -32, -26, -21, -17, -13, -9, -5, -2, 0, 2, 5, 9, 13, 17, 21, 26, 32, ]; /// invAngle of the modes 11 to 25 (table 8-5), indexed by mode − 11. internal static readonly short[] InvAngle = [-4096, -1638, -910, -630, -482, -390, -315, -256, -315, -390, -482, -630, -910, -1638, -4096]; /// QpC as a function of qPi for ChromaArrayType 1 (table 8-10), for qPi from 30 to 43. internal static readonly byte[] ChromaQp30To43 = [29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37]; /// /// The 4:2:2 intra prediction mode of chroma (table 8-3) from modeIdc, the mode table 8-2 gives: for chroma samples /// twice as tall as they are wide, the mode whose angle is closest to the luma one with the horizontal displacement /// halved (horizontal modes: the vertical one doubled; past 45° a mode of the other direction). /// internal static readonly byte[] ChromaMode422 = [ 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20, 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31, ]; /// QpC of chroma from qPi (8-258 to 8-261, table 8-10): the table for 4:2:0, Min(qPi, 51) otherwise. internal static int ChromaQp(int qPi, int chromaArrayType) => chromaArrayType != 1 ? Math.Min(qPi, 51) : qPi < 30 ? qPi : qPi > 43 ? qPi - 6 : ChromaQp30To43[qPi - 30]; /// β′ for Q from 0 to 51 (table 8-12). internal static readonly byte[] Beta = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, ]; /// tC′ for Q from 0 to 53 (table 8-12). internal static readonly byte[] Tc = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 24, ]; /// levelScale[qP % 6] of the scaling process (§8.6.3). internal static readonly int[] LevelScale = [40, 45, 51, 57, 64, 72]; /// Default values of ScalingList[1..3][0..2] (intra), in up-right diagonal order (table 7-6). internal static readonly byte[] DefaultScalingIntra = [ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 16, 17, 16, 17, 18, 17, 18, 18, 17, 18, 21, 19, 20, 21, 20, 19, 21, 24, 22, 22, 24, 24, 22, 22, 24, 25, 25, 27, 30, 27, 25, 25, 29, 31, 35, 35, 31, 29, 36, 41, 44, 41, 36, 47, 54, 54, 47, 65, 70, 65, 88, 88, 115, ]; /// Default values of ScalingList[1..3][3..5] (inter), in up-right diagonal order (table 7-6). internal static readonly byte[] DefaultScalingInter = [ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 20, 20, 20, 20, 20, 20, 20, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 28, 28, 28, 28, 28, 28, 33, 33, 33, 33, 33, 41, 41, 41, 41, 54, 54, 54, 71, 71, 91, ]; /// /// Scan orders of blocks from 1×1 to 32×32 (index log2 size, 0 to 5) for scanIdx 0 (up-right diagonal, §6.5.3), 1 /// (horizontal, §6.5.4) and 2 (vertical, §6.5.5): each entry is x | (y << 8). /// internal static readonly ushort[][][] ScanOrder = BuildScans(); /// For the 4×4 scans: position in scan order of each raster position (y * 4 + x), by scanIdx. internal static readonly byte[][] ScanPosition4x4 = BuildInverse4x4(); /// /// Magnitudes of the coefficients of the 32-point DCT of §8.6.4.2 by angle index a (cos(aπ/64) scaled), for a from 0 to /// 32: the odd rows of the 32-point matrix are 90, 90, 88, 85, …, 4, the odd rows of the 16-point one 90, 87, 80, …, 9, /// those of the 8-point one 89, 75, 50, 18 and of the 4-point one 83, 36. /// internal static readonly byte[] DctCosines = [ 64, 90, 90, 90, 89, 88, 87, 85, 83, 82, 80, 78, 75, 73, 70, 67, 64, 61, 57, 54, 50, 46, 43, 38, 36, 31, 25, 22, 18, 13, 9, 4, 0, ]; /// /// The 32×32 inverse DCT matrix of §8.6.4.2, as transMatrix[k][n] = DctMatrix[k * 32 + n] with k the frequency and n /// the sample: the 4-, 8- and 16-point matrices are its rows 32/N·k and first N columns. /// internal static readonly short[] DctMatrix = BuildDct(); /// The 4×4 DST matrix of §8.6.4.2 (trType 1), transMatrix[k][n] = DstMatrix[k * 4 + n]. internal static readonly short[] DstMatrix = [29, 55, 74, 84, 74, 74, 0, -74, 84, -29, -74, 55, 55, -84, 74, -29]; private static short[] BuildDct() { var m = new short[32 * 32]; for (var k = 0; k < 32; k++) { for (var n = 0; n < 32; n++) { if (k == 0) { m[n] = 64; continue; } // The entry is cos((2n + 1)kπ/64) on the scale of the table: fold the angle into the first quadrant. var a = ((2 * n) + 1) * k % 128; int value = a switch { <= 32 => DctCosines[a], <= 64 => -DctCosines[64 - a], <= 96 => -DctCosines[a - 64], _ => DctCosines[128 - a], }; m[(k * 32) + n] = (short)value; } } return m; } private static ushort[][][] BuildScans() { var scans = new ushort[6][][]; for (var log2 = 0; log2 < 6; log2++) { var size = 1 << log2; var diagonal = new ushort[size * size]; int i = 0, x = 0, y = 0; var stop = false; while (!stop) { while (y >= 0) { if (x < size && y < size) { diagonal[i++] = (ushort)(x | (y << 8)); } y--; x++; } y = x; x = 0; if (i >= size * size) { stop = true; } } var horizontal = new ushort[size * size]; var vertical = new ushort[size * size]; i = 0; for (y = 0; y < size; y++) { for (x = 0; x < size; x++) { horizontal[i] = (ushort)(x | (y << 8)); vertical[i] = (ushort)(y | (x << 8)); i++; } } scans[log2] = [diagonal, horizontal, vertical]; } return scans; } private static byte[][] BuildInverse4x4() { var inverse = new byte[3][]; for (var s = 0; s < 3; s++) { inverse[s] = new byte[16]; for (var n = 0; n < 16; n++) { var p = ScanOrder[2][s][n]; inverse[s][((p >> 8) * 4) + (p & 0xFF)] = (byte)n; } } return inverse; } }