using Febe.Core.Codecs.H264;
namespace Febe.Tests.Codecs.H264;
///
/// The recovery point SEI (H.264 §D.1.8, §D.2.8) that lets an exact frame start at an I picture (D12 of tappa E3): only
/// with recovery_frame_cnt 0 and exact_match_flag 1. The access units are written by the test.
///
[TestClass]
public sealed class H264RecoveryPointTests
{
[TestMethod]
public void OnlyAnExactMatchIsAnExactStart()
{
// payload: ue(recovery_frame_cnt) = 0 «1», exact_match_flag, broken_link_flag 0, changing_slice_group_idc 00,
// then the stop bit of the payload: 1 1 0 00 1 00 (exact) and 1 0 0 00 1 00 (not exact).
Assert.IsTrue(H264Decoder.HasExactRecoveryPoint(AccessUnit(0xC4), 4), "exact_match_flag 1");
Assert.IsFalse(H264Decoder.HasExactRecoveryPoint(AccessUnit(0x84), 4), "exact_match_flag 0: pictures may differ from the IDR's");
// recovery_frame_cnt 1 («010»), exact: the pictures are exact only one frame later.
Assert.IsFalse(H264Decoder.HasExactRecoveryPoint(AccessUnit(0x51), 4));
// Annex B carries the same.
Assert.IsTrue(H264Decoder.HasExactRecoveryPoint([0, 0, 0, 1, .. Sei(0xC4), 0, 0, 0, 1, .. Slice], 0));
Assert.IsFalse(H264Decoder.HasExactRecoveryPoint([0, 0, 0, 1, .. Sei(0x84), 0, 0, 0, 1, .. Slice], 0));
}
/// A non-IDR slice NAL header and a byte of data: where the SEIs of an access unit end.
private static readonly byte[] Slice = [0x41, 0x9A];
/// An SEI NAL unit with one recovery point message of one byte, and its RBSP trailing bits.
private static byte[] Sei(byte payload) => [0x06, 0x06, 0x01, payload, 0x80];
private static byte[] AccessUnit(byte payload)
{
var sei = Sei(payload);
return [0, 0, 0, (byte)sei.Length, .. sei, 0, 0, 0, (byte)Slice.Length, .. Slice];
}
}