Prima di scrivere il decodificatore HEVC serve un riferimento ufficiale per ogni strumento: 136 flussi Main, Main 10 e Main Still Picture (il profilo delle foto HEIC) e 10 RExt, circa 29 MB, con il readme degli autori e riferimento.md5 (MD5 di tutto il YUV ufficiale; per i 15 flussi che lo hanno nello zip anche di ogni fotogramma). I YUV restano fuori; i flussi grandi già coperti da altri sono elencati con il motivo in LEGGIMI.md. Le prove di decodifica sono inconcludenti finché il decodificatore non c'è, e quelle RExt finché Alberto non decide se servono. Già da ora si controllano i riferimenti e gli hash SEI delle immagini, letti con un lettore scritto dalla norma (D.3.19), che permetteranno di verificare immagine per immagine anche i flussi senza YUV. Le 146 prove saltate non passano la regola della CI che non ammette prove saltate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
7.9 KiB
C#
160 lines
7.9 KiB
C#
using System.Security.Cryptography;
|
||
|
||
namespace Febe.Tests.Codecs.H265;
|
||
|
||
/// <summary>
|
||
/// The streams of the ITU-T H.265.1 conformance set in Media/H265/Conformita (see LEGGIMI.md there). There is no HEVC
|
||
/// decoder yet: every stream is inconclusive until it exists, and the range extension streams (4:0:0, 4:2:2, 4:4:4,
|
||
/// 12 bits, RExt tools) stay inconclusive until Alberto says whether «HEVC» covers them. The reference data and the
|
||
/// reader of the decoded picture hashes are checked now.
|
||
/// </summary>
|
||
[TestClass]
|
||
public sealed class H265ConformanceTests
|
||
{
|
||
/// <summary>Streams the decoder is expected to reproduce exactly; empty while the decoder is not written.</summary>
|
||
private static readonly HashSet<string> s_supported = [];
|
||
|
||
public static IEnumerable<object[]> Streams => H265ConformanceStream.Names().Select(n => new object[] { n });
|
||
|
||
[TestMethod]
|
||
[DynamicData(nameof(Streams))]
|
||
public void FramesMatchTheReference(string name)
|
||
{
|
||
var stream = H265ConformanceStream.Load(name);
|
||
if (stream.IsRangeExtension)
|
||
{
|
||
Assert.Inconclusive($"{name}: estensione di formato (RExt, {stream.Chroma} a {stream.LumaBitDepth} bit), da decodificare solo se Alberto la vuole.");
|
||
}
|
||
|
||
if (!s_supported.Contains(name))
|
||
{
|
||
Assert.Inconclusive($"{name}: il decodificatore HEVC non è ancora scritto.");
|
||
}
|
||
|
||
// The decoder goes here: frames in output order, in the layout of the official YUV file.
|
||
Assert.Fail($"{name}: manca la decodifica.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void TheSetIsTheOneTheReadmeDescribes()
|
||
{
|
||
var names = H265ConformanceStream.Names().ToList();
|
||
Assert.HasCount(146, names);
|
||
Assert.AreEqual(10, names.Count(n => H265ConformanceStream.Load(n).IsRangeExtension));
|
||
var readme = File.ReadAllText(Path.Combine(H265ConformanceStream.Root, "LEGGIMI.md"));
|
||
var missing = names.Where(n => !readme.Contains($"`{n}`", StringComparison.Ordinal)).ToList();
|
||
Assert.IsEmpty(missing, $"LEGGIMI.md non descrive: {string.Join(", ", missing)}");
|
||
Assert.AreEqual(15, names.Count(n => H265ConformanceStream.Load(n).FrameMd5.Count > 0), "flussi con il YUV ufficiale");
|
||
}
|
||
|
||
[TestMethod]
|
||
[DynamicData(nameof(Streams))]
|
||
public void TheReferenceIsComplete(string name)
|
||
{
|
||
var stream = H265ConformanceStream.Load(name);
|
||
Assert.IsGreaterThan(0, stream.FrameCount);
|
||
Assert.AreEqual(32, stream.TotalMd5.Length, $"{name}: manca la riga «tutto»");
|
||
Assert.IsTrue(stream.FrameMd5.Count == 0 || stream.FrameMd5.Count == stream.FrameCount, $"{name}: righe per fotogramma");
|
||
Assert.IsTrue(stream.LumaBitDepth is >= 8 and <= 12 && stream.ChromaBitDepth is >= 8 and <= 12, $"{name}: profondità");
|
||
if (stream.Size is var (width, height))
|
||
{
|
||
Assert.AreEqual(stream.FrameCount * stream.FrameBytes(width, height), stream.ByteCount, $"{name}: byte del YUV");
|
||
}
|
||
else
|
||
{
|
||
// A stream that changes size: at least the bytes are a sum of frames of the sizes it lists.
|
||
Assert.IsGreaterThan(1, stream.Sizes.Count);
|
||
Assert.IsTrue(stream.Sizes.Any(s => stream.ByteCount >= stream.FrameBytes(s.Width, s.Height)), $"{name}: byte del YUV");
|
||
}
|
||
|
||
if (!stream.IsRangeExtension)
|
||
{
|
||
Assert.AreEqual("4:2:0", stream.Chroma, $"{name}: i profili Main, Main 10 e Main Still Picture sono 4:2:0");
|
||
Assert.IsLessThanOrEqualTo(10, Math.Max(stream.LumaBitDepth, stream.ChromaBitDepth), name);
|
||
}
|
||
|
||
var data = File.ReadAllBytes(stream.BitstreamPath);
|
||
CollectionAssert.AreEqual(new byte[] { 0, 0, 1 }, data.AsSpan(data[2] == 1 ? 0 : 1, 3).ToArray(), $"{name}: non comincia con un codice d'inizio (Annex B)");
|
||
}
|
||
|
||
[TestMethod]
|
||
[DynamicData(nameof(Streams))]
|
||
public void EveryDecodedPictureHasOneHashOrNoneHas(string name)
|
||
{
|
||
// Counted independently from the bitstream: either no picture carries a decoded picture hash, or every coded
|
||
// picture carries exactly one, with one value per colour component. NUT_A is the exception: its two RASL
|
||
// pictures after a BLA, which a decoder skips, have none.
|
||
var stream = H265ConformanceStream.Load(name);
|
||
var data = File.ReadAllBytes(stream.BitstreamPath);
|
||
var hashes = H265PictureHashes.Read(data);
|
||
if (hashes.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var pictures = CountPictures(data);
|
||
var expected = name == "NUT_A_ericsson_5" ? pictures - 2 : pictures;
|
||
Assert.HasCount(expected, hashes, $"{name}: {pictures} immagini");
|
||
Assert.AreEqual(hashes.Count, hashes.Select(h => h.Picture).Distinct().Count(), name);
|
||
var planes = stream.Chroma == "4:0:0" ? 1 : 3;
|
||
Assert.IsTrue(hashes.All(h => h.Planes.Length == planes), $"{name}: componenti per hash");
|
||
var type = name is "HRD_A_Fujitsu_3" or "PICSIZE_C_Bossen_1" or "PICSIZE_D_Bossen_1" ? H265HashType.Checksum : H265HashType.Md5;
|
||
Assert.IsTrue(hashes.All(h => h.Type == type), $"{name}: tipo di hash");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void PictureHashesFollowTheDefinition()
|
||
{
|
||
// 3×2 samples in a stride of 4; the fourth column must be left out.
|
||
ushort[] samples = [1, 2, 3, 999, 4, 5, 6, 999];
|
||
CollectionAssert.AreEqual(MD5.HashData(new byte[] { 1, 2, 3, 4, 5, 6 }), H265PictureHashes.Compute(H265HashType.Md5, samples, 3, 2, 4, 8));
|
||
ushort[] wide = [0x3FF, 0x100];
|
||
CollectionAssert.AreEqual(MD5.HashData(new byte[] { 0xFF, 0x03, 0x00, 0x01 }), H265PictureHashes.Compute(H265HashType.Md5, wide, 2, 1, 2, 10));
|
||
|
||
// Checksum (D.3.19): every sample xored with (x & 0xFF) ^ (y & 0xFF) ^ (x >> 8) ^ (y >> 8), high byte too above 8 bits.
|
||
var expected = (1 ^ 0) + (2 ^ 1) + (3 ^ 2) + (4 ^ 1) + (5 ^ 0) + (6 ^ 3);
|
||
CollectionAssert.AreEqual(new byte[] { 0, 0, 0, (byte)expected }, H265PictureHashes.Compute(H265HashType.Checksum, samples, 3, 2, 4, 8));
|
||
var expectedWide = (0xFF ^ 0) + (0x03 ^ 0) + (0x00 ^ 1) + (0x01 ^ 1);
|
||
CollectionAssert.AreEqual(new byte[] { 0, 0, (byte)(expectedWide >> 8), (byte)expectedWide }, H265PictureHashes.Compute(H265HashType.Checksum, wide, 2, 1, 2, 10));
|
||
|
||
// CRC (D.3.19): bit by bit from 0xFFFF with the polynomial 0x1021 over the bytes and two zero bytes, which is
|
||
// CRC-16/AUG-CCITT: 0x1D0F over no bytes, 0xE5CC over "123456789" (the check value of that CRC).
|
||
CollectionAssert.AreEqual(new byte[] { 0x1D, 0x0F }, H265PictureHashes.Compute(H265HashType.Crc, ReadOnlySpan<ushort>.Empty, 0, 0, 0, 8));
|
||
ushort[] digits = [.. "123456789".Select(c => (ushort)c)];
|
||
CollectionAssert.AreEqual(new byte[] { 0xE5, 0xCC }, H265PictureHashes.Compute(H265HashType.Crc, digits, 9, 1, 9, 8));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void AStreamOfOnePictureHasItsHash()
|
||
{
|
||
// IPRED_B (Main Still Picture, one 1920×1080 picture): one MD5 per component, decoding index 0.
|
||
var data = File.ReadAllBytes(H265ConformanceStream.Load("IPRED_B_Nokia_3").BitstreamPath);
|
||
var hash = H265PictureHashes.Read(data).Single();
|
||
Assert.AreEqual(0, hash.Picture);
|
||
Assert.AreEqual(H265HashType.Md5, hash.Type);
|
||
Assert.IsTrue(hash.Planes.All(p => p.Length == 16));
|
||
}
|
||
|
||
/// <summary>Coded pictures of layer 0: VCL NAL units whose slice segment header starts with first_slice_segment_in_pic_flag = 1.</summary>
|
||
private static int CountPictures(byte[] data)
|
||
{
|
||
var count = 0;
|
||
for (var i = 0; i + 5 < data.Length; i++)
|
||
{
|
||
if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1)
|
||
{
|
||
var type = (data[i + 3] >> 1) & 63;
|
||
var layer = ((data[i + 3] & 1) << 5) | (data[i + 4] >> 3);
|
||
if (type < 32 && layer == 0 && (data[i + 5] & 0x80) != 0)
|
||
{
|
||
count++;
|
||
}
|
||
|
||
i += 2;
|
||
}
|
||
}
|
||
|
||
return count;
|
||
}
|
||
}
|