Decodificatore HEVC scritto in casa, prima parte: le immagini intra coincidono coi flussi di conformità (#58)
Per leggere i video HEVC senza ffmpeg serve un decodificatore nostro, scritto dalla norma ITU-T H.265. Questa prima parte copre tutto ciò che serve alle immagini intra: unità NAL Annex B e hvcC, VPS/SPS/PPS (liste di scala, VUI, indicatori RExt), intestazione di slice, POC, RPS, DPB con uscita e bumping (C.5.2), CABAC con tutte le tabelle d'inizio, albero di codifica, unità di predizione, residui con sign data hiding e transform skip, predizione intra, DCT/DST e dequantizzazione, PCM, deblocking e SAO, tile, WPP e segmenti dipendenti. I 12 flussi di sole slice I dei profili Main, Main 10 e Main Still Picture coincidono col YUV ufficiale. Nei 136 flussi Main e Main 10 ogni immagine di sole slice I coincide con l'MD5 SEI del codificatore, dove il flusso lo porta. Le slice P e B si rifiutano finché manca la predizione inter (tappa successiva). Le prove ne leggono comunque la sintassi fino alla fine di ogni slice, con le unità di predizione grigie. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6274c4ee03
commit
f77b5e4db6
17 files changed
+5708
-9
No files matched your search
@@ -0,0 +1,175 @@
|
||||
namespace Febe.Core.Codecs.H265;
|
||||
|
||||
/// <summary>
|
||||
/// 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 chroma QP mapping (table 8-10), the
|
||||
/// thresholds of the deblocking filter (table 8-12) and the transform matrices (§8.6.4.2).
|
||||
/// </summary>
|
||||
internal static class H265Tables
|
||||
{
|
||||
/// <summary>intraPredAngle by intra prediction mode (table 8-4); modes 0 (planar) and 1 (DC) have none.</summary>
|
||||
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,
|
||||
];
|
||||
|
||||
/// <summary>invAngle of the modes 11 to 25 (table 8-5), indexed by mode − 11.</summary>
|
||||
internal static readonly short[] InvAngle = [-4096, -1638, -910, -630, -482, -390, -315, -256, -315, -390, -482, -630, -910, -1638, -4096];
|
||||
|
||||
/// <summary>QpC as a function of qPi for ChromaArrayType 1 (table 8-10), for qPi from 30 to 43.</summary>
|
||||
internal static readonly byte[] ChromaQp30To43 = [29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37];
|
||||
|
||||
/// <summary>β′ for Q from 0 to 51 (table 8-12).</summary>
|
||||
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,
|
||||
];
|
||||
|
||||
/// <summary>tC′ for Q from 0 to 53 (table 8-12).</summary>
|
||||
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,
|
||||
];
|
||||
|
||||
/// <summary>levelScale[qP % 6] of the scaling process (§8.6.3).</summary>
|
||||
internal static readonly int[] LevelScale = [40, 45, 51, 57, 64, 72];
|
||||
|
||||
/// <summary>Default values of ScalingList[1..3][0..2] (intra), in up-right diagonal order (table 7-6).</summary>
|
||||
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,
|
||||
];
|
||||
|
||||
/// <summary>Default values of ScalingList[1..3][3..5] (inter), in up-right diagonal order (table 7-6).</summary>
|
||||
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,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
internal static readonly ushort[][][] ScanOrder = BuildScans();
|
||||
|
||||
/// <summary>For the 4×4 scans: position in scan order of each raster position (y * 4 + x), by scanIdx.</summary>
|
||||
internal static readonly byte[][] ScanPosition4x4 = BuildInverse4x4();
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
internal static readonly short[] DctMatrix = BuildDct();
|
||||
|
||||
/// <summary>The 4×4 DST matrix of §8.6.4.2 (trType 1), transMatrix[k][n] = DstMatrix[k * 4 + n].</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user