using Febe.Core.Codecs.H264; using Febe.Core.Library; using Febe.Tests.Support; namespace Febe.Tests.Codecs.Containers; /// The frame decoder that replaces ffmpeg for thumbnails, and the recognition of videos of a still picture. [TestClass] public sealed class VideoFrameDecoderTests { [TestMethod] [DataRow("mp4")] [DataRow("mkv")] [DataRow("avi")] [DataRow("frammentato.mp4")] public async Task TheFrameAtOneSecondIsTheOneTheStreamShowsThere(string kind) { var es = ElementaryStream.Load("CABA3_SVA_B"); using var temp = new TempFolder("fotogramma"); var data = kind switch { "mkv" => TestVideos.Matroska(es, new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero)), "avi" => TestVideos.Avi(es), "frammentato.mp4" => TestVideos.FragmentedMp4(es), _ => TestVideos.Mp4(es, new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero)), // a dated file, as from a phone }; var path = TestVideos.Write(temp, "video." + kind, data); var image = await VideoFrameDecoder.Instance.DecodeFrameAsync(path, TimeSpan.FromSeconds(1)); Assert.IsNotNull(image); // The same frame straight from the decoder: the one shown at frame 30 (30 frames per second). AVI has no // presentation times: there frames are shown in decoding order. var position = Math.Min(30, es.Count - 1); var expected = kind == "avi" ? DecodedFrame(es, position) : ShownFrame(es, position); var reference = VideoFrameDecoder.ToImage(expected, 0, 0); Assert.AreEqual((reference.Width, reference.Height), (image.Width, image.Height)); CollectionAssert.AreEqual(reference.Rgb, image.Rgb); } [TestMethod] public async Task ShortVideosGiveTheirFirstFrameAndTheBmpIsWellFormed() { var es = ElementaryStream.Load("BA1_Sony_D"); using var temp = new TempFolder("fotogramma-breve"); var path = TestVideos.Write(temp, "video.mp4", TestVideos.Mp4(es)); var bmp = await VideoFrameDecoder.Instance.ExtractImageAsync(path, TimeSpan.FromSeconds(3600), 0); Assert.IsNotNull(bmp); Assert.AreEqual((byte)'B', bmp[0]); Assert.AreEqual((byte)'M', bmp[1]); Assert.AreEqual(bmp.Length, BitConverter.ToInt32(bmp, 2)); Assert.AreEqual(es.Width, BitConverter.ToInt32(bmp, 18)); Assert.AreEqual(es.Height, BitConverter.ToInt32(bmp, 22)); Assert.AreEqual(24, BitConverter.ToInt16(bmp, 28)); var first = VideoFrameDecoder.ToImage(ShownFrame(es, 0), 0, 0); CollectionAssert.AreEqual(first.ToBmp(), bmp); } [TestMethod] public async Task RotationAndSizeLimitAreApplied() { var es = ElementaryStream.Load("BA1_Sony_D"); using var temp = new TempFolder("fotogramma-ruotato"); var path = TestVideos.Write(temp, "video.mp4", TestVideos.Mp4(es, rotation: 90)); var image = await VideoFrameDecoder.Instance.DecodeFrameAsync(path, TimeSpan.Zero); Assert.AreEqual((es.Height, es.Width), (image!.Width, image.Height)); var upright = VideoFrameDecoder.ToImage(ShownFrame(es, 0), 0, 0); // Rotated clockwise: the top-left pixel of the frame ends at the top-right corner. CollectionAssert.AreEqual(upright.Rgb[..3], image.Rgb[((image.Width - 1) * 3)..(image.Width * 3)]); var small = await VideoFrameDecoder.Instance.DecodeFrameAsync(path, TimeSpan.Zero, maxLongSide: es.Width / 2); Assert.AreEqual((es.Height / 2, es.Width / 2), (small!.Width, small.Height)); } [TestMethod] public void ColoursFollowTheMatrixAndTheRange() { static H264Frame Flat(byte y, byte cb, byte cr, int matrix, bool full) => new(2, 2, [y, y, y, y], [cb], [cr], 0, 0, 0, matrix, full); CollectionAssert.AreEqual(new byte[] { 0, 0, 0 }, VideoFrameDecoder.ToImage(Flat(16, 128, 128, 1, false), 0, 0).Rgb[..3]); CollectionAssert.AreEqual(new byte[] { 255, 255, 255 }, VideoFrameDecoder.ToImage(Flat(235, 128, 128, 1, false), 0, 0).Rgb[..3]); CollectionAssert.AreEqual(new byte[] { 128, 128, 128 }, VideoFrameDecoder.ToImage(Flat(128, 128, 128, 6, true), 0, 0).Rgb[..3]); // Pure red of BT.601 (limited range): Y 81, Cb 90, Cr 240. var red = VideoFrameDecoder.ToImage(Flat(81, 90, 240, 6, false), 0, 0).Rgb; Assert.IsTrue(red[0] >= 250 && red[1] <= 3 && red[2] <= 3, $"{red[0]} {red[1]} {red[2]}"); // The same values read as BT.709 are no longer pure red. var other = VideoFrameDecoder.ToImage(Flat(81, 90, 240, 1, false), 0, 0).Rgb; Assert.IsTrue(other[1] > 20, $"{other[0]} {other[1]} {other[2]}"); } [TestMethod] public async Task AVideoOfAStillPictureIsRecognised() { var es = ElementaryStream.Load("BA1_Sony_D"); using var temp = new TempFolder("fermo"); var still = TestVideos.Write(temp, "fermo.mp4", TestVideos.Mp4(es.FirstPictureRepeated(90))); var result = await VideoFrameDecoder.Instance.AnalyseStillnessAsync(still); Assert.IsNotNull(result); Assert.IsTrue(result.IsStill); Assert.IsTrue(result.SampledFrames >= 2); Assert.AreEqual(0, result.Difference); } [TestMethod] [DataRow("CABA3_SVA_B")] [DataRow("FRExt1_Panasonic_D")] public async Task AVideoThatMovesIsNotStill(string name) { var es = ElementaryStream.Load(name); using var temp = new TempFolder("movimento"); var path = TestVideos.Write(temp, "video.mp4", TestVideos.Mp4(es)); var result = await VideoFrameDecoder.Instance.AnalyseStillnessAsync(path); Assert.IsNotNull(result); Assert.IsFalse(result.IsStill, $"differenza {result.Difference}"); } [TestMethod] public async Task UnreadableFilesGiveNothing() { using var temp = new TempFolder("illeggibile"); var junk = TestVideos.Write(temp, "rotto.mp4", [0, 0, 0, 16, .. "ftypisom"u8, 0, 0, 0, 0, 0, 0, 0, 9, .. "mdat"u8, 1]); Assert.IsNull(await VideoFrameDecoder.Instance.ExtractImageAsync(junk)); Assert.IsNull(await VideoFrameDecoder.Instance.AnalyseStillnessAsync(junk)); Assert.IsNull(await VideoFrameDecoder.Instance.ExtractImageAsync(temp.Combine("non-esiste.mkv"))); } /// The frame decoded at position , straight from the stream. private static H264Frame DecodedFrame(ElementaryStream es, int index) => ShownFrame(es, es.Rank[index]); /// The frame shown at position , decoded straight from the stream. private static H264Frame ShownFrame(ElementaryStream es, int rank) { var decoder = new H264Decoder(); var frames = new List(); decoder.FrameDecoded += frames.Add; decoder.DecodeAnnexB(ElementaryStream.AnnexB(es.Name)); var index = Array.IndexOf(es.Rank, rank); return frames.Single(f => f.DecodeIndex == index); } }