Files
Febe/tests/Febe.Tests/Media/H264/H264ConformanceTests.cs
T
Alby96andClaude Opus 5.5 25a6d9ce1d Flussi di conformità H.264 per le parti ancora da fare: campi, MBAFF, FMO, SP, 4:2:2, 10 bit, 4:0:0, SVC, MVC (#58)
Trentasette flussi ufficiali della serie ITU-T H.264.1 (8,3 MB), scaricati il 18 novembre con il
permesso di Alberto e controllati con il CRC-32 dell'indice o dello zip, entrano in
tests/Febe.Tests/Media/H264/Conformita con il loro readme e un riferimento.md5 calcolato dalle
ricostruzioni ufficiali, che restano fuori dal repository. Così chi scrive campi, PAFF, MBAFF,
FMO e ASO, slice SP, 4:2:2 a 8 e 10 bit, monocromatico, SVC o MVC trova già il riferimento.

La nuova prova FeaturesStillToDo è inconclusiva con «da fare: …» finché la parte manca e
fallisce apposta quando un flusso coincide senza essere passato fra i verificati. Lo strato base
dei tre flussi SVC è H.264 puro e si verifica già: coincide fotogramma per fotogramma. Per MVC la
serie non ha ricostruzioni; il file lo dice e si controlla solo che la vista base si decodifichi.

Il riepilogo delle prove della CI conta a parte le inconclusive «da fare:», che altrimenti
sarebbero 37 saltate oltre le zero ammesse. LEGGIMI.md dice che cosa copre ogni flusso e che cosa
resta fuori e perché (partizionamento dei dati, slice ridondanti e SI: nessun flusso della serie
li usa; High 10 in 4:2:0, 4:2:2 Intra, 4:4:4: senza ricostruzione e grandi).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 15:04:42 +01:00

227 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Febe.Core.Codecs.H264;
namespace Febe.Tests.Codecs.H264;
/// <summary>
/// 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 the verified 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
/// <see cref="H264SyntheticStreamTests"/>, without an official reference). The streams of the parts the decoder does not
/// handle yet (interlaced pictures, FMO and ASO, SP slices, 4:2:2, more than 8 bits, 4:0:0, SVC, MVC) are in
/// <see cref="FeaturesStillToDo"/>: they are inconclusive until the feature is written, and fail as soon as one of them
/// matches its reference without having been moved among the verified ones.
/// </summary>
[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) => AssertMatches(name, "riferimento.md5");
[TestMethod]
[DataRow("SVCBMT-13")]
[DataRow("SVCBST-19")]
[DataRow("SVCBCT-1")]
public void SvcBaseLayerMatchesTheReference(string name)
{
// The base layer of an SVC stream is plain H.264: a decoder that skips NAL units 14, 15 and 20 (prefix, subset
// SPS, slice extension) must give exactly the official reconstruction of layer L0.
AssertMatches(name, "riferimento-base.md5");
}
[TestMethod]
[DataRow("CVFI1_SVA_C", "immagini a campi (CAVLC)")]
[DataRow("FI1_Sony_E", "immagini a campi, solo slice I (CAVLC)")]
[DataRow("CAFI1_SVA_C", "immagini a campi (CABAC)")]
[DataRow("Sharp_MP_PAFF_1r2", "PAFF, fotogrammi e coppie di campi (CAVLC, POC di tipo 1)")]
[DataRow("HCAFF1_HHI_B", "PAFF (CABAC, High)")]
[DataRow("CVMAQP3_Sony_D", "MBAFF (CAVLC)")]
[DataRow("FREXT02_JVC_C", "MBAFF (CAVLC, High)")]
[DataRow("CAMACI3_Sony_C", "MBAFF con predizione intra vincolata (CABAC)")]
[DataRow("CAMASL3_Sony_B", "MBAFF con quattro slice per immagine (CABAC)")]
[DataRow("HCAMFF1_HHI_B", "MBAFF (CABAC, High)")]
[DataRow("FREXT01_JVC_D", "MBAFF (CABAC, High)")]
[DataRow("CAPAMA3_Sand_F", "PAFF e MBAFF insieme (CABAC)")]
[DataRow("cama1_vtc_c", "campi con il campo inferiore per primo e MBAFF (CABAC)")]
[DataRow("FRExt2_Panasonic_C", "campi e MBAFF con MMCO e matrici di quantizzazione (CAVLC, High)")]
[DataRow("FRExt4_Panasonic_B", "campi e MBAFF con matrici di quantizzazione (CABAC, High)")]
[DataRow("MR6_BT_B", "campi con MMCO e riordino dei riferimenti (Extended, POC di tipo 1)")]
[DataRow("MR9_BT_B", "campi e MBAFF con MMCO e riordino dei riferimenti (CABAC, POC di tipo 1)")]
[DataRow("FM2_SVA_C", "FMO (gruppi di slice di tipo 0) e ASO")]
[DataRow("FM1_FT_E", "FMO (gruppi di slice di tipo 1, 2, 3 e 4)")]
[DataRow("FM1_BT_B", "FMO (gruppi di slice di tipo 0-6) e ASO")]
[DataRow("sp1_bt_a", "slice SP (Extended)")]
[DataRow("sp2_bt_b", "slice SP (Extended)")]
[DataRow("Hi422FR10_SONY_A", "4:2:2 a 8 bit, solo slice I (CABAC)")]
[DataRow("Hi422FR11_SONY_A", "4:2:2 a 8 bit con slice P (CABAC)")]
[DataRow("Hi422FR12_SONY_A", "4:2:2 a 8 bit con slice B (CABAC)")]
[DataRow("Hi422FR13_SONY_A", "4:2:2 a 10 bit, solo slice I (CABAC)")]
[DataRow("Hi422FR14_SONY_A", "4:2:2 a 10 bit con slice P (CABAC)")]
[DataRow("Hi422FR15_SONY_A", "4:2:2 a 10 bit con slice B (CABAC)")]
[DataRow("FREXT2_TANDBERG_A", "4:2:2 a 8 bit con slice P (CAVLC)")]
[DataRow("FREXT3_TANDBERG_A", "4:2:2 a 8 bit con slice B (CAVLC)")]
[DataRow("HPCAMOLQ_BRCM_B", "monocromatico 4:0:0 (CABAC, High)")]
[DataRow("HPCVMOLQ_BRCM_B", "monocromatico 4:0:0 (CAVLC, High)")]
[DataRow("SVCBMT-13", "SVC, qualità MGS e scalabilità temporale (strato L2)")]
[DataRow("SVCBST-19", "SVC, scalabilità spaziale e temporale (strato L1, 640×480)")]
[DataRow("SVCBCT-1", "SVC, qualità CGS e scalabilità temporale (strato L1)")]
[DataRow("MVCDS-4", "MVC, due viste")]
[DataRow("MVCNV-3", "MVC, tre viste")]
public void FeaturesStillToDo(string name, string feature)
{
var folder = Folder(name);
var expected = ReadReference(Path.Combine(folder, "riferimento.md5"), out var declared);
Assert.AreEqual(declared, expected.Count, $"{name}: riferimento.md5 non ha tutti i fotogrammi dichiarati");
List<string> actual;
try
{
actual = Decode(StreamFile(folder));
}
catch (H264Exception ex)
{
Assert.Inconclusive($"da fare: {feature} ({ex.Message})");
return;
}
if (expected.Count == 0)
{
Assert.Inconclusive($"da fare: {feature}; manca la ricostruzione ufficiale (oggi escono {actual.Count} fotogrammi della vista base)");
}
var same = 0;
for (var i = 0; i < Math.Min(actual.Count, expected.Count); i++)
{
same += actual[i] == expected[i] ? 1 : 0;
}
if (same == expected.Count && actual.Count == expected.Count)
{
Assert.Fail($"{name} ora coincide con il riferimento: spostalo fra i flussi di {nameof(FramesMatchTheReference)}.");
}
Assert.Inconclusive($"da fare: {feature}; oggi {same} fotogrammi su {expected.Count} coincidono ({actual.Count} decodificati)");
}
[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 data = File.ReadAllBytes(StreamFile(Folder(name)));
var all = new List<H264Frame>();
var full = new H264Decoder();
full.FrameDecoded += all.Add;
full.DecodeAnnexB(data);
var some = new List<H264Frame>();
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}");
}
}
/// <summary>The folder of a conformance stream.</summary>
internal static string Folder(string name) => Path.Combine(AppContext.BaseDirectory, "Media", "H264", "Conformita", name);
/// <summary>The Annex B stream of a folder, whatever extension its authors gave it.</summary>
internal static string StreamFile(string folder) =>
Directory.EnumerateFiles(folder).Single(f => Path.GetExtension(f).ToLowerInvariant() is ".264" or ".jsv" or ".avc" or ".h264" or ".jvt");
private static void AssertMatches(string name, string reference)
{
var folder = Folder(name);
var expected = ReadReference(Path.Combine(folder, reference), out var declared);
Assert.AreEqual(declared, expected.Count, $"{name}: {reference} non ha tutti i fotogrammi dichiarati");
var frames = new List<H264Frame>();
var decoder = new H264Decoder();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(File.ReadAllBytes(StreamFile(folder)));
var ordered = OutputOrder(frames);
var actual = ordered.Select(Md5).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<string> Decode(string stream)
{
var frames = new List<H264Frame>();
var decoder = new H264Decoder();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(File.ReadAllBytes(stream));
return OutputOrder(frames).Select(Md5).ToList();
}
private static List<H264Frame> OutputOrder(List<H264Frame> frames) =>
frames.OrderBy(f => f.Period).ThenBy(f => f.Poc).ThenBy(f => f.DecodeIndex).ToList();
private static string Md5(H264Frame frame) => Convert.ToHexStringLower(MD5.HashData(frame.ToI420()));
/// <summary>
/// The frame MD5s of a riferimento.md5, in output order, and the number of frames its first line declares (0 when
/// the file only says that there is no official reconstruction).
/// </summary>
private static List<string> ReadReference(string path, out int declared)
{
var frames = new List<string>();
declared = 0;
foreach (var line in File.ReadAllLines(path))
{
if (frames.Count == 0 && declared == 0 && line.StartsWith('#'))
{
var count = Regex.Match(line, @" (\d+) fotogrammi,");
if (count.Success)
{
declared = int.Parse(count.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture);
}
}
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;
}
}