using Febe.Core.Codecs.H264; using static Febe.Tests.Codecs.H264.SyntheticStreams; namespace Febe.Tests.Codecs.H264; /// /// The decoders of the non-base views of an MVC stream (Annex H): what a damaged subset SPS may make them allocate, and /// the filter, which holds for every view. /// [TestClass] public sealed class H264MultiviewLimitsTests { [TestMethod] public void TheViewsOfAStreamShareOneBoundForTheirBuffers() { // A subset SPS may list up to 1,024 views, and each view has a decoder and frame stores of its own: together they // stay within H264Decoder.MaxAllViewsMbs, here sixteen reference frames of 1920×1088 per view. var s = new SequenceSettings(WidthInMbs: 120, HeightInMbs: 68, MaxNumRefFrames: 16); var decoder = new H264Decoder(); var frames = new List(); decoder.FrameDecoded += frames.Add; decoder.DecodeUnit(MvcSubsetSps(s, views: 64)[4..]); decoder.DecodeUnit(Pps()[4..]); // Views the subset SPS does not list are refused, and leave nothing behind that would count against the bound. for (var id = 100; id < 140; id++) { var absent = Assert.ThrowsExactly(() => decoder.DecodeUnit(ViewIdr(s, id)[4..])); StringAssert.Contains(absent.Message, "Vista assente"); } var perView = (s.MaxNumRefFrames + 1L) * s.WidthInMbs * s.HeightInMbs; var allowed = (int)(H264Decoder.MaxAllViewsMbs / perView) - 1; // besides the base view Assert.AreEqual(9, allowed); for (var id = 1; id <= allowed; id++) { decoder.DecodeUnit(ViewIdr(s, id)[4..]); } var refused = Assert.ThrowsExactly(() => decoder.DecodeUnit(ViewIdr(s, allowed + 1)[4..])); StringAssert.Contains(refused.Message, "Troppe viste"); // The views kept still decode: one frame each, grey (Intra_16x16 DC with nothing around gives 128). decoder.Flush(); CollectionAssert.AreEqual(Enumerable.Range(1, allowed).ToArray(), frames.Select(f => f.ViewId).ToArray()); foreach (var frame in frames) { Assert.IsTrue(frame.Y.All(v => v == 128), $"vista {frame.ViewId}"); } } [TestMethod] public void ExtensionUnitsCutShortGiveNothingOrAnH264Exception() { // NAL units 14, 15 and 20 whose header is read byte by byte (svc_extension_flag, view_id, dependency_id), from one // byte to a few, SVC and MVC, after an MVC subset SPS and a PPS or without them. var s = new SequenceSettings(WidthInMbs: 2, HeightInMbs: 2); foreach (var withParameters in new[] { false, true }) { foreach (var type in new[] { 14, 15, 20 }) { for (var length = 1; length <= 8; length++) { foreach (var second in new byte[] { 0x00, 0x40, 0x80, 0xC0, 0xFF }) { var decoder = new H264Decoder(); decoder.FrameDecoded += _ => { }; var unit = new byte[length]; unit[0] = (byte)(0x60 | type); for (var i = 1; i < length; i++) { unit[i] = i == 1 ? second : (byte)(0x35 * i); } try { if (withParameters) { decoder.DecodeUnit(MvcSubsetSps(s, views: 2)[4..]); decoder.DecodeUnit(Pps()[4..]); } decoder.DecodeUnit(unit); decoder.Flush(); } catch (H264Exception) { } } } } } } [TestMethod] [DataRow("MVCDS-4")] [DataRow("MVCNV-3")] public void WantedHoldsForEveryView(string name) { // The filter of the root decoder applies to the decoders of the other views too, each with its own decoding // index; the frames handed out are those of the full decoding, and the view components the views after them // refer to are still reconstructed. var data = File.ReadAllBytes(H264ConformanceTests.StreamFile(H264ConformanceTests.Folder(name))); var all = new List(); var full = new H264Decoder(); full.FrameDecoded += all.Add; full.DecodeAnnexB(data); var some = new List(); var partial = new H264Decoder { Wanted = index => index % 3 == 2 }; partial.FrameDecoded += some.Add; partial.DecodeAnnexB(data); Assert.IsGreaterThan(1, all.Select(f => f.ViewId).Distinct().Count(), "più viste"); CollectionAssert.AreEqual( all.Where(f => f.DecodeIndex % 3 == 2).Select(f => (f.ViewId, f.DecodeIndex)).ToArray(), some.Select(f => (f.ViewId, f.DecodeIndex)).ToArray()); foreach (var frame in some) { var expected = all.Single(f => f.ViewId == frame.ViewId && f.DecodeIndex == frame.DecodeIndex); CollectionAssert.AreEqual(expected.ToPlanar(), frame.ToPlanar(), $"{name}: vista {frame.ViewId}, fotogramma {frame.DecodeIndex}"); } } }