using Febe.Core.Codecs.H264; namespace Febe.Tests.Codecs.H264; /// /// Tools of the Baseline and Extended profiles that no stream of the ITU-T series uses — slice data partitioning, /// redundant slices — and slices in another order than the one of the series, on conformance streams rewritten by /// : the macroblocks keep their bits, so every frame must still be the official one. /// [TestClass] public sealed class H264ExtendedToolsTests { [TestMethod] [DataRow("BA3_SVA_C")] // Extended, B slices [DataRow("sp1_bt_a")] // SP slices [DataRow("FM1_FT_E")] // slice groups of types 1–4 [DataRow("MR6_BT_B")] // Extended, field pictures [DataRow("CVMAQP3_Sony_D")] // MBAFF [DataRow("SVA_BA1_B")] // non-IDR I slices: residual in partition B only public void DataPartitionsGiveTheOfficialFrames(string name) { var stream = StreamRewriter.Partition(Original(name)); var types = NalTypes(stream); Assert.IsFalse(types.Contains(1), "restano slice non partizionate"); Assert.IsTrue(types.Contains(2), "manca la partizione A"); Assert.IsTrue(types.Contains(3) || types.Contains(4), "mancano le partizioni B e C"); H264ConformanceTests.AssertStreamMatches(name, stream); } [TestMethod] [DataRow(false, true)] [DataRow(true, false)] [DataRow(false, false)] public void MissingPartitionsLeaveTheirResidualOut(bool keepB, bool keepC) { // Without partition B or C a slice still decodes, without the residual of its intra or inter macroblocks: every // picture comes out, and the IDR picture (never partitioned) is still the official one. var name = "BA3_SVA_C"; var complete = H264ConformanceTests.FrameMd5s(StreamRewriter.Partition(Original(name))); var partial = H264ConformanceTests.FrameMd5s(StreamRewriter.Partition(Original(name), keepB, keepC)); var expected = H264ConformanceTests.ReferenceFrames(name); Assert.HasCount(expected.Count, partial); Assert.AreEqual(expected[0], partial[0], "l'immagine IDR"); Assert.IsTrue(partial.Where((md5, i) => md5 != complete[i]).Any(), "togliere una partizione cambia qualcosa"); } [TestMethod] [DataRow("BA3_SVA_C", 4101)] // CAVLC [DataRow("CABA3_SVA_B", 4102)] // CABAC [DataRow("MR6_BT_B", 4103)] // fields, POC type 1 [DataRow("CAPAMA3_Sand_F", 4104)] // CABAC, MBAFF and fields [DataRow("FM1_FT_E", 4105)] // slice groups, several PPS [DataRow("sp1_bt_a", 4106)] // SP slices public void RedundantSlicesAreSkipped(string name, int seed) { var stream = StreamRewriter.AddRedundantSlices(Original(name), seed); Assert.IsGreaterThan(Original(name).Length, stream.Length); H264ConformanceTests.AssertStreamMatches(name, stream); } [TestMethod] public void RedundantSlicesAreSkippedAlsoWhenPartitioned() { // The redundant slices become partitions A (header and random bits) with slice_id and redundant_pic_cnt. var name = "BA3_SVA_C"; H264ConformanceTests.AssertStreamMatches(name, StreamRewriter.Partition(StreamRewriter.AddRedundantSlices(Original(name), 4107))); } [TestMethod] [DataRow("BASQP1_Sony_C")] // twenty slices per picture [DataRow("FM1_FT_E")] // slice groups [DataRow("CAMASL3_Sony_B")] // CABAC, MBAFF, four slices per picture public void SlicesInReverseOrderGiveTheOfficialFrames(string name) { var original = Original(name); var stream = StreamRewriter.ReverseSlices(original); CollectionAssert.AreNotEqual(original, stream, "il flusso ha più slice per immagine"); H264ConformanceTests.AssertStreamMatches(name, stream); } [TestMethod] [DataRow("FM1_FT_E")] // several slices per picture: the stray partitions have a slice_id of their own [DataRow("BASQP1_Sony_C")] // twenty slices per picture [DataRow("BA3_SVA_C")] // one slice per picture: the stray partitions come after both B and C of the slice before public void PartitionsBAndCWithoutTheirPartitionAAreIgnored(string name) { // A lost partition A leaves its B and C right after the partitions of the slice before, which is still waiting // to be decoded: they must change nothing, as if the whole slice had been lost, and above all not the residual // of the waiting slice. var partitioned = StreamRewriter.Partition(Original(name)); var strays = StreamRewriter.DropPartitionsA(partitioned, alsoBAndC: false, out var dropped); var lost = StreamRewriter.DropPartitionsA(partitioned, alsoBAndC: true, out var droppedWhole); Assert.AreEqual(dropped, droppedWhole); Assert.IsGreaterThanOrEqualTo(5, dropped, "slice senza partizione A"); Assert.IsGreaterThan(StreamRewriter.Units(lost).Count, StreamRewriter.Units(strays).Count, "restano le partizioni B e C"); CollectionAssert.AreEqual(H264ConformanceTests.FrameMd5s(lost), H264ConformanceTests.FrameMd5s(strays)); } [TestMethod] [DataRow("FM1_FT_E", 4201)] [DataRow("sp2_bt_b", 4202)] [DataRow("BA3_SVA_C", 4203)] public void DamagedPartitionedStreamsGiveFramesOrAnH264Exception(string name, int seed) { H264RobustnessTests.AssertDamagedCopiesDecode(name, StreamRewriter.Partition(Original(name)), seed, trials: 30); } private static byte[] Original(string name) => File.ReadAllBytes(H264ConformanceTests.StreamFile(H264ConformanceTests.Folder(name))); private static HashSet NalTypes(byte[] stream) => StreamRewriter.Units(stream).Select(u => u[0] & 0x1F).ToHashSet(); }