using Febe.Core.Codecs.H264;
using static Febe.Tests.Codecs.H264.SyntheticStreams;
namespace Febe.Tests.Codecs.H264;
///
/// The decoder on tiny streams written by : the parts of the standard no conformance stream in
/// the repository exercises.
///
[TestClass]
public sealed class H264SyntheticStreamTests
{
[TestMethod]
[DataRow(16)]
[DataRow(2)]
public void AGapInFrameNumKeepsTheWrapInThePictureOrder(int maxNumRefFrames)
{
// POC type 2, MaxFrameNum 16: references 0..14, then frame_num 3. Frames 15, 0, 1 and 2 are missing (§8.2.5.2):
// the wrap at 0 is among them, so the frame after the gap has FrameNumOffset 16 and POC 2 × (16 + 3) (§8.2.1.3).
// With two reference frames, 15 and 0 are counted without becoming references.
var s = new SequenceSettings(MaxNumRefFrames: maxNumRefFrames, GapsAllowed: true);
var stream = new List([.. Sps(s), .. Pps(), .. Idr(s, 100)]);
for (var n = 1; n <= 14; n++)
{
stream.AddRange(SkippedP(s, n));
}
stream.AddRange(SkippedP(s, 3));
var frames = Decode([.. stream]);
Assert.HasCount(16, frames);
for (var n = 0; n <= 14; n++)
{
Assert.AreEqual(2 * n, frames[n].Poc, $"fotogramma {n}");
}
Assert.AreEqual(2 * (16 + 3), frames[15].Poc, "il fotogramma dopo il salto");
}
[TestMethod]
public void ARedundantPictureIsSkipped()
{
// The primary IDR picture (luma 100) and a redundant copy (redundant_pic_cnt 1) whose slice also starts at
// macroblock 0: one frame, the primary one.
var s = new SequenceSettings();
byte[] stream = [.. Sps(s), .. Pps(redundantPicCntPresent: true), .. Idr(s, 100, redundantPicCnt: 0), .. Idr(s, 200, redundantPicCnt: 1)];
var frames = Decode(stream);
Assert.HasCount(1, frames);
Assert.IsTrue(frames[0].Y.All(v => v == 100), "il fotogramma è quello primario");
}
[TestMethod]
[DataRow(0, 127, 49, 128)]
[DataRow(12, 130, 52, 126)]
public void AnSiPictureIsRebuiltInQs(int qsDelta, int first, int third, int chroma)
{
// Three macroblocks: SI, I_PCM (luma 50), SI; the SI ones without residual, every block predicted in DC mode. Each
// 4×4 block of an SI macroblock is its prediction transformed, quantised in QS and decoded in QS (§8.6.2), so a
// flat block of 128 comes back as 127 with QS 26 (DC 2048 → level 39 → 8112 → 127) and as 130 with QS 38; next to
// the I_PCM samples (50) the blocks give 49 and 52. Chroma: the DC of the four blocks through the 2×2 transform
// (8192 → level 79 with QSc 26, 28 with QSc 35), back to 128 and 126. Worked out by hand from the equations.
var s = new SequenceSettings(WidthInMbs: 3);
var frames = Decode([.. Sps(s), .. Pps(), .. SiIdr(s, qsDelta, pcm: [1], pcmLuma: 50)]);
Assert.HasCount(1, frames);
var f = frames[0];
for (var y = 0; y < 16; y++)
{
for (var x = 0; x < 48; x++)
{
var expected = x < 16 ? first : x < 32 ? 50 : third;
Assert.AreEqual(expected, f.Y[(y * 48) + x], $"luma ({x}, {y})");
}
}
for (var i = 0; i < f.Cb.Length; i++)
{
var x = i % 24;
var expected = x is >= 8 and < 16 ? 128 : chroma;
Assert.AreEqual(expected, f.Cb[i], $"Cb {i}");
Assert.AreEqual(expected, f.Cr[i], $"Cr {i}");
}
}
[TestMethod]
public void APSliceWithNothingToPredictFromIsRefused()
{
// Decoding that starts at a P picture (an AVI without index, read from a sample that is not a key frame): the
// decoder refuses it instead of predicting the picture from itself.
var s = new SequenceSettings();
var decoder = new H264Decoder();
var frames = new List();
decoder.FrameDecoded += frames.Add;
Assert.ThrowsExactly(() => decoder.DecodeAnnexB([.. Sps(s), .. Pps(), .. SkippedP(s, 1)]));
Assert.IsEmpty(frames);
}
[TestMethod]
public void AnUnwantedPictureNobodyRefersToIsNotDecoded()
{
// The second picture is not a reference and its slice data is broken (a skip run past the picture): decoding it
// fails, so a decoder that skips it as unwanted shows it was never reconstructed.
var s = new SequenceSettings(PocType: 0);
byte[] broken = [.. SkippedP(s with { WidthInMbs = 2 }, 1, reference: false)]; // a run of 2 in a 1-macroblock picture
byte[] stream = [.. Sps(s), .. Pps(), .. Idr(s, 100), .. broken, .. SkippedP(s, 1, pocLsb: 4)];
Assert.ThrowsExactly(() => Decode(stream));
var decoder = new H264Decoder { Wanted = index => index != 1 };
var frames = new List();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(stream);
Assert.AreEqual(3, decoder.FramesDecoded);
CollectionAssert.AreEqual(new long[] { 0, 2 }, frames.Select(f => f.DecodeIndex).ToArray());
Assert.AreEqual(4, frames[1].Poc, "il fotogramma dopo quello saltato ha il suo ordine");
Assert.IsTrue(frames[1].Y.All(v => v == 100));
}
[TestMethod]
public void TheStartOfDecodingIsRecognised()
{
var s = new SequenceSettings();
Assert.AreEqual(H264AccessPoint.Idr, H264Decoder.AccessPoint([.. Sps(s), .. Pps(), .. Idr(s, 100)], 0));
Assert.AreEqual(H264AccessPoint.None, H264Decoder.AccessPoint([.. Sps(s), .. Pps(), .. SkippedP(s, 1)], 0));
Assert.AreEqual(H264AccessPoint.None, H264Decoder.AccessPoint([], 0));
// Length-prefixed (MP4): the same IDR picture without its start code.
var idr = Idr(s, 100)[4..];
Assert.AreEqual(H264AccessPoint.Idr, H264Decoder.AccessPoint([0, 0, (byte)(idr.Length >> 8), (byte)idr.Length, .. idr], 4));
}
private static List Decode(byte[] stream)
{
var decoder = new H264Decoder();
var frames = new List();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(stream);
return frames;
}
}