using System.Security.Cryptography; using Febe.Core.Codecs.H264; namespace Febe.Tests.Codecs.H264; /// /// The decoder against streams of the ITU-T H.264.1 conformance suite: every output frame, in output order, must have the /// MD5 of the corresponding frame of the official reconstruction (see Conformita/LEGGIMI.md). None of these streams uses /// weighted prediction, memory management operations, long-term references, gaps in frame_num, POC type 1, I_PCM or /// CABAC pictures of several slices: those parts of the decoder have no reference MD5 here (some are reached by /// , without an official reference). /// [TestClass] public sealed class H264ConformanceTests { [TestMethod] [DataRow("BA1_Sony_D")] [DataRow("SVA_BA1_B")] [DataRow("BA3_SVA_C")] [DataRow("BASQP1_Sony_C")] [DataRow("CI_MW_D")] [DataRow("CABA1_SVA_B")] [DataRow("CABA2_SVA_B")] [DataRow("CABA3_SVA_B")] [DataRow("CANL1_SVA_B")] [DataRow("FRExt1_Panasonic_D")] [DataRow("FRExt3_Panasonic_E")] [DataRow("HCAFR1_HHI_C")] public void FramesMatchTheReference(string name) { var folder = Path.Combine(AppContext.BaseDirectory, "Media", "H264", "Conformita", name); var stream = Directory.EnumerateFiles(folder) .Single(f => Path.GetExtension(f) is ".264" or ".jsv" or ".avc"); var expected = ReadReference(Path.Combine(folder, "riferimento.md5")); var frames = new List(); var decoder = new H264Decoder(); decoder.FrameDecoded += frames.Add; decoder.DecodeAnnexB(File.ReadAllBytes(stream)); var ordered = frames.OrderBy(f => f.Period).ThenBy(f => f.Poc).ThenBy(f => f.DecodeIndex).ToList(); var actual = ordered.Select(f => Convert.ToHexStringLower(MD5.HashData(f.ToI420()))).ToList(); for (var i = 0; i < Math.Min(actual.Count, expected.Count); i++) { if (actual[i] != expected[i]) { var f = ordered[i]; Assert.Fail($"{name}: il fotogramma {i} (decodifica {f.DecodeIndex}, POC {f.Poc}, {f.Width}×{f.Height}) è diverso dal riferimento."); } } Assert.AreEqual(expected.Count, actual.Count, $"{name}: numero di fotogrammi"); } [TestMethod] [DataRow("CABA3_SVA_B")] [DataRow("CANL1_SVA_B")] [DataRow("FRExt1_Panasonic_D")] [DataRow("FRExt3_Panasonic_E")] [DataRow("HCAFR1_HHI_C")] public void PicturesLeftOutDoNotChangeTheOthers(string name) { // Pictures nobody refers to and nobody wants are not reconstructed (only their headers count): every frame // handed out must still be the one of the full decoding, in the same place of the output order. var folder = Path.Combine(AppContext.BaseDirectory, "Media", "H264", "Conformita", name); var data = File.ReadAllBytes(Directory.EnumerateFiles(folder).Single(f => Path.GetExtension(f) is ".264" or ".jsv" or ".avc")); 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.AreEqual(full.FramesDecoded, partial.FramesDecoded); CollectionAssert.AreEqual(all.Where(f => f.DecodeIndex % 3 == 2).Select(f => f.DecodeIndex).ToArray(), some.Select(f => f.DecodeIndex).ToArray()); foreach (var frame in some) { var expected = all.Single(f => f.DecodeIndex == frame.DecodeIndex); Assert.AreEqual((expected.Poc, expected.Period), (frame.Poc, frame.Period), $"{name}: ordine del fotogramma {frame.DecodeIndex}"); CollectionAssert.AreEqual(expected.ToI420(), frame.ToI420(), $"{name}: fotogramma {frame.DecodeIndex}"); } } private static List ReadReference(string path) { var frames = new List(); foreach (var line in File.ReadAllLines(path)) { var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 2 && int.TryParse(parts[0], out var index)) { Assert.AreEqual(frames.Count, index, "riferimento.md5 fuori ordine"); frames.Add(parts[1]); } } return frames; } }