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]
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;
}
}