Il decodificatore rifiutava tutto ciò che non era 4:2:0 a 8 bit, così i video 4:2:2, monocromatici o a 10 bit restavano senza miniatura. Ora i campioni stanno in piani di uno o due byte e i nuclei di predizione, trasformata e filtro sono generici su byte e ushort, specializzati dal JIT: a 8 bit i tempi restano quelli di prima (misurati sui flussi di conformità, 100,4%). - 4:2:2: DC della crominanza 2x4 con qP + 3, tabelle CAVLC di nC -2, contesti CABAC per NumC8x8, predizione intra 8x16, vettore verticale a piena risoluzione, bordi della crominanza alle righe 4, 8 e 12. - 4:4:4: Cb e Cr codificati come la luminanza in CAVLC, con le loro matrici e il filtro della luminanza; separate_colour_plane_flag come tre immagini monocromatiche con i propri dati di macroblocco e di moto. - 4:0:0 senza sintassi della crominanza; in uscita la crominanza 4:2:0 al valore medio, come il file ufficiale. - Profondità da 8 a 14 bit: QP da -QpBdOffset, soglie del deblocking e scarti dei pesi scalati; uscita a due byte little-endian. - Senza perdita: qpprime_y_zero_transform_bypass_flag a QP'Y 0, con la DPCM della predizione intra orizzontale e verticale. - VideoFrameDecoder converte in RGB alla profondità e al campionamento del fotogramma. I dieci flussi 4:2:2 (8 e 10 bit) e 4:0:0 della serie ITU-T coincidono con la ricostruzione ufficiale e passano fra i verificati. 4:4:4, piani separati, senza perdita e 11-14 bit si provano con flussi sintetici (H264FormatTests). Il CABAC in 4:4:4 con i piani insieme si rifiuta: mancano i contesti 460-1023. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
7.1 KiB
C#
179 lines
7.1 KiB
C#
using Febe.Core.Codecs.H264;
|
||
using static Febe.Tests.Codecs.H264.SyntheticStreams;
|
||
|
||
namespace Febe.Tests.Codecs.H264;
|
||
|
||
/// <summary>
|
||
/// The formats of the High profiles on tiny streams written by <see cref="SyntheticStreams"/>, where the conformance
|
||
/// streams in the repository have no reference: every chroma format at every bit depth, the colour planes of 4:4:4
|
||
/// coded separately, lossless macroblocks with their DPCM, the CABAC of 4:4:4 that is refused.
|
||
/// </summary>
|
||
[TestClass]
|
||
public sealed class H264FormatTests
|
||
{
|
||
[TestMethod]
|
||
[DataRow(0, 8, 8)]
|
||
[DataRow(0, 12, 9)]
|
||
[DataRow(1, 10, 10)]
|
||
[DataRow(1, 8, 10)]
|
||
[DataRow(2, 12, 9)]
|
||
[DataRow(3, 14, 14)]
|
||
[DataRow(3, 9, 8)]
|
||
public void PcmSamplesKeepTheirFormatAndBitDepth(int chromaFormat, int bitDepthLuma, int bitDepthChroma)
|
||
{
|
||
// Two I_PCM macroblocks side by side: every sample is written as it is, at its bit depth.
|
||
var h = new HighSettings(chromaFormat, bitDepthLuma, bitDepthChroma, WidthInMbs: 2);
|
||
int luma = (1 << bitDepthLuma) - 3, cb = (1 << (bitDepthChroma - 1)) - 7, cr = (1 << bitDepthChroma) - 2;
|
||
var frames = Decode([.. HighSps(h), .. HighPps(h), .. HighIdr(h, b =>
|
||
{
|
||
Pcm(b, h, luma, cb, cr);
|
||
Pcm(b, h, luma, cb, cr);
|
||
})]);
|
||
|
||
Assert.HasCount(1, frames);
|
||
var f = frames[0];
|
||
Assert.AreEqual(32, f.Width);
|
||
Assert.AreEqual(16, f.Height);
|
||
Assert.AreEqual(chromaFormat, f.ChromaFormat);
|
||
Assert.AreEqual(bitDepthLuma, f.BitDepthLuma);
|
||
|
||
// Monochrome comes out with 4:2:0 chroma at the middle value, at the luma bit depth (as the reference decoder writes it).
|
||
var depthC = chromaFormat == 0 ? bitDepthLuma : bitDepthChroma;
|
||
Assert.AreEqual(depthC, f.BitDepthChroma);
|
||
var bytes = Math.Max(bitDepthLuma, depthC) > 8 ? 2 : 1;
|
||
Assert.HasCount(32 * 16 * bytes, f.Y);
|
||
var (cw, ch) = chromaFormat switch
|
||
{
|
||
3 => (32, 16),
|
||
2 => (16, 16),
|
||
_ => (16, 8),
|
||
};
|
||
Assert.AreEqual(cw, f.ChromaWidth);
|
||
Assert.AreEqual(ch, f.ChromaHeight);
|
||
Assert.HasCount(cw * ch * bytes, f.Cb);
|
||
Assert.HasCount(cw * ch * bytes, f.Cr);
|
||
for (var i = 0; i < 32 * 16; i++)
|
||
{
|
||
Assert.AreEqual(luma, f.LumaAt(i), $"luma {i}");
|
||
}
|
||
|
||
int expectedCb = chromaFormat == 0 ? 1 << (depthC - 1) : cb, expectedCr = chromaFormat == 0 ? 1 << (depthC - 1) : cr;
|
||
for (var i = 0; i < cw * ch; i++)
|
||
{
|
||
Assert.AreEqual(expectedCb, f.ChromaAt(f.Cb, i), $"Cb {i}");
|
||
Assert.AreEqual(expectedCr, f.ChromaAt(f.Cr, i), $"Cr {i}");
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
[DataRow(8)]
|
||
[DataRow(10)]
|
||
public void SeparateColourPlanesMakeOneFrame(int bitDepth)
|
||
{
|
||
// separate_colour_plane_flag: three slices of the same IDR picture, one per colour_plane_id, each a monochrome
|
||
// picture of I_PCM macroblocks at the luma bit depth.
|
||
var h = new HighSettings(3, bitDepth, bitDepth, SeparatePlanes: true, WidthInMbs: 2);
|
||
int[] values = [11 << (bitDepth - 8), 22 << (bitDepth - 8), 33 << (bitDepth - 8)];
|
||
var stream = new List<byte>([.. HighSps(h), .. HighPps(h)]);
|
||
for (var plane = 0; plane < 3; plane++)
|
||
{
|
||
var v = values[plane];
|
||
stream.AddRange(HighIdr(h, b =>
|
||
{
|
||
Pcm(b, h, v, 0, 0);
|
||
Pcm(b, h, v, 0, 0);
|
||
}, plane));
|
||
}
|
||
|
||
var frames = Decode([.. stream]);
|
||
Assert.HasCount(1, frames);
|
||
var f = frames[0];
|
||
Assert.AreEqual(3, f.ChromaFormat);
|
||
Assert.AreEqual(bitDepth, f.BitDepthChroma);
|
||
Assert.AreEqual(32, f.ChromaWidth);
|
||
for (var i = 0; i < 32 * 16; i++)
|
||
{
|
||
Assert.AreEqual(values[0], f.LumaAt(i), $"Y {i}");
|
||
Assert.AreEqual(values[1], f.ChromaAt(f.Cb, i), $"Cb {i}");
|
||
Assert.AreEqual(values[2], f.ChromaAt(f.Cr, i), $"Cr {i}");
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
[DataRow(0, 8, 1)]
|
||
[DataRow(0, 8, 0)]
|
||
[DataRow(0, 8, 2)]
|
||
[DataRow(0, 10, 1)]
|
||
[DataRow(1, 8, 0)]
|
||
[DataRow(2, 10, 1)]
|
||
[DataRow(3, 8, 1)]
|
||
[DataRow(3, 8, 0)]
|
||
[DataRow(3, 12, 2)]
|
||
public void ALosslessMacroblockAccumulatesItsResidual(int chromaFormat, int bitDepth, int mode)
|
||
{
|
||
// An I_PCM macroblock, then an Intra_16x16 one predicted from it at QP′Y 0 with qpprime_y_zero_transform_bypass_flag:
|
||
// lossless (§8.5.15). Its only residual is a DC level on the first 4×4 block of each luma-like plane, taken as
|
||
// it is (no transform, no scaling) and accumulated along the prediction: horizontal (mode 1) adds it to the whole
|
||
// first row, vertical (mode 0, the I_PCM macroblock above) to the whole first column, DC (mode 2) only to the
|
||
// first sample.
|
||
var vertical = mode == 0;
|
||
var h = new HighSettings(chromaFormat, bitDepth, bitDepth, Lossless: true, WidthInMbs: vertical ? 1 : 2, HeightInMbs: vertical ? 2 : 1);
|
||
var shift = bitDepth - 8;
|
||
int[] pcm = [100 << shift, 60 << shift, 180 << shift];
|
||
int[] dc = [1, 2, -1];
|
||
var frames = Decode([.. HighSps(h), .. HighPps(h), .. HighIdr(h, b =>
|
||
{
|
||
Pcm(b, h, pcm[0], pcm[1], pcm[2]);
|
||
Intra16x16Dc(b, h, mode, dc);
|
||
})]);
|
||
|
||
Assert.HasCount(1, frames);
|
||
var f = frames[0];
|
||
var planes = chromaFormat == 3 ? 3 : 1;
|
||
for (var p = 0; p < planes; p++)
|
||
{
|
||
var plane = p == 0 ? f.Y : p == 1 ? f.Cb : f.Cr;
|
||
for (var y = 0; y < 16; y++)
|
||
{
|
||
for (var x = 0; x < 16; x++)
|
||
{
|
||
var touched = mode == 1 ? y == 0 : mode == 0 ? x == 0 : x == 0 && y == 0;
|
||
var expected = pcm[p] + (touched ? dc[p] : 0);
|
||
var index = vertical ? ((16 + y) * 16) + x : (y * 32) + 16 + x;
|
||
var actual = p == 0 ? f.LumaAt(index) : f.ChromaAt(plane, index);
|
||
Assert.AreEqual(expected, actual, $"piano {p}, ({x}, {y})");
|
||
}
|
||
}
|
||
}
|
||
|
||
if (chromaFormat is 1 or 2)
|
||
{
|
||
// Chroma: DC prediction from the I_PCM chroma, no residual.
|
||
for (var i = 0; i < f.ChromaWidth * f.ChromaHeight; i++)
|
||
{
|
||
Assert.AreEqual(pcm[1], f.ChromaAt(f.Cb, i), $"Cb {i}");
|
||
Assert.AreEqual(pcm[2], f.ChromaAt(f.Cr, i), $"Cr {i}");
|
||
}
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public void CabacIn444IsRefusedUntilItsContextsAreWritten()
|
||
{
|
||
// The residual of Cb and Cr of 4:4:4 with CABAC needs the contexts 460–1023: not transcribed yet, so the slice is
|
||
// refused rather than decoded wrong.
|
||
var h = new HighSettings(3, Cabac: true);
|
||
Assert.ThrowsExactly<H264Exception>(() => Decode([.. HighSps(h), .. HighPps(h), .. HighIdr(h, b => b.Bits(0, 8))]));
|
||
}
|
||
|
||
private static List<H264Frame> Decode(byte[] stream)
|
||
{
|
||
var decoder = new H264Decoder();
|
||
var frames = new List<H264Frame>();
|
||
decoder.FrameDecoded += frames.Add;
|
||
decoder.DecodeAnnexB(stream);
|
||
decoder.Flush();
|
||
return frames;
|
||
}
|
||
}
|