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).
///
[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");
}
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;
}
}