using Febe.Core.Codecs.H264;
using static Febe.Tests.Codecs.H264.SyntheticStreams;
namespace Febe.Tests.Codecs.H264;
///
/// The formats of the High profiles on tiny streams written by , 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.
///
[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 official reconstructions
// of HPCAMOLQ_BRCM_B and HPCVMOLQ_BRCM_B hold 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([.. 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(() => Decode([.. HighSps(h), .. HighPps(h), .. HighIdr(h, b => b.Bits(0, 8))]));
}
private static List Decode(byte[] stream)
{
var decoder = new H264Decoder();
var frames = new List();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(stream);
decoder.Flush();
return frames;
}
}