Files
Febe/tests/Febe.Tests/Media/H264/H264FormatTests.cs
T
Alby96andClaude Opus 5.5 cdc3cc11b9 Correzioni dopo la revisione del decodificatore H.264, dei lettori FLV e della CI (#58)
- CI: le due prove MVC di FeaturesStillToDo finivano con «da verificare: …», che
  riepilogo-prove.sh contava come saltate: ogni verifica del ramo sarebbe fallita.
  Ora cominciano con «da fare: », e lo script esenta solo le inconclusive di
  FeaturesStillToDo il cui motivo comincia con «da fare: » subito dopo la cornice
  di MSTest (prima bastava la parola in qualunque punto di qualunque prova).
  PipelineTests prova lo script su .trx con le cornici inglese e italiana;
  processo.md, Sviluppo.md e verifica.yml descrivono la categoria.
- Partizionamento dei dati: una partizione B o C sperduta (partizione A persa)
  sovrascriveva il buffer delle partizioni della slice in attesa. Ora slice_id e
  redundant_pic_cnt si leggono prima di copiare, e contano l'ordine A, B, C.
- MVC: le viste tengono insieme al massimo 2 × MaxDpbMbs di immagini, e il
  decodificatore di una vista nuova si tiene solo dopo il controllo dell'SPS di
  sottoinsieme; Wanted vale per ogni vista.
- FLV: senza durata nei metadati la durata arriva alla fine dell'ultimo
  fotogramma, come per MPEG; «solo audio» solo con una prova vera nella testa
  (un onMetaData grande non fa più scartare un video).
- Flussi di conformità: tolti i 48 readme e schede di terzi (decisione di
  Alberto: nel repository solo flussi e MD5).
- Prove nuove: partizioni A perse, limite delle viste e Wanted in MVC, unità di
  estensione troncate, flussi rovinati 4:2:2, 10 bit, 4:0:0, SVC e MVC.
- Commenti sulla provenienza che citano i file di ricostruzione ufficiali e
  §8.7 invece del decodificatore di riferimento; commento di
  ImportEnvironment.VideoProbe aggiornato; righe nuove in decisioni.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 19:30:45 +01:00

180 lines
7.2 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 Febe.Core.Codecs.H264;
using static Febe.Tests.Codecs.H264.SyntheticStreams;
namespace Febe.Tests.Codecs.H264;
/// <summary>
/// The formats of the High profiles on tiny streams written by <see cref="SyntheticStreams"/>, where the conformance
/// streams in the repository have no reference: every chroma format at every bit depth, the colour planes of 4:4:4
/// coded separately, lossless macroblocks with their DPCM, the CABAC of 4:4:4 that is refused.
/// </summary>
[TestClass]
public sealed class H264FormatTests
{
[TestMethod]
[DataRow(0, 8, 8)]
[DataRow(0, 12, 9)]
[DataRow(1, 10, 10)]
[DataRow(1, 8, 10)]
[DataRow(2, 12, 9)]
[DataRow(3, 14, 14)]
[DataRow(3, 9, 8)]
public void PcmSamplesKeepTheirFormatAndBitDepth(int chromaFormat, int bitDepthLuma, int bitDepthChroma)
{
// Two I_PCM macroblocks side by side: every sample is written as it is, at its bit depth.
var h = new HighSettings(chromaFormat, bitDepthLuma, bitDepthChroma, WidthInMbs: 2);
int luma = (1 << bitDepthLuma) - 3, cb = (1 << (bitDepthChroma - 1)) - 7, cr = (1 << bitDepthChroma) - 2;
var frames = Decode([.. HighSps(h), .. HighPps(h), .. HighIdr(h, b =>
{
Pcm(b, h, luma, cb, cr);
Pcm(b, h, luma, cb, cr);
})]);
Assert.HasCount(1, frames);
var f = frames[0];
Assert.AreEqual(32, f.Width);
Assert.AreEqual(16, f.Height);
Assert.AreEqual(chromaFormat, f.ChromaFormat);
Assert.AreEqual(bitDepthLuma, f.BitDepthLuma);
// Monochrome comes out with 4:2:0 chroma at the middle value, at the luma bit depth (as the official reconstructions
// of HPCAMOLQ_BRCM_B and HPCVMOLQ_BRCM_B hold it).
var depthC = chromaFormat == 0 ? bitDepthLuma : bitDepthChroma;
Assert.AreEqual(depthC, f.BitDepthChroma);
var bytes = Math.Max(bitDepthLuma, depthC) > 8 ? 2 : 1;
Assert.HasCount(32 * 16 * bytes, f.Y);
var (cw, ch) = chromaFormat switch
{
3 => (32, 16),
2 => (16, 16),
_ => (16, 8),
};
Assert.AreEqual(cw, f.ChromaWidth);
Assert.AreEqual(ch, f.ChromaHeight);
Assert.HasCount(cw * ch * bytes, f.Cb);
Assert.HasCount(cw * ch * bytes, f.Cr);
for (var i = 0; i < 32 * 16; i++)
{
Assert.AreEqual(luma, f.LumaAt(i), $"luma {i}");
}
int expectedCb = chromaFormat == 0 ? 1 << (depthC - 1) : cb, expectedCr = chromaFormat == 0 ? 1 << (depthC - 1) : cr;
for (var i = 0; i < cw * ch; i++)
{
Assert.AreEqual(expectedCb, f.ChromaAt(f.Cb, i), $"Cb {i}");
Assert.AreEqual(expectedCr, f.ChromaAt(f.Cr, i), $"Cr {i}");
}
}
[TestMethod]
[DataRow(8)]
[DataRow(10)]
public void SeparateColourPlanesMakeOneFrame(int bitDepth)
{
// separate_colour_plane_flag: three slices of the same IDR picture, one per colour_plane_id, each a monochrome
// picture of I_PCM macroblocks at the luma bit depth.
var h = new HighSettings(3, bitDepth, bitDepth, SeparatePlanes: true, WidthInMbs: 2);
int[] values = [11 << (bitDepth - 8), 22 << (bitDepth - 8), 33 << (bitDepth - 8)];
var stream = new List<byte>([.. HighSps(h), .. HighPps(h)]);
for (var plane = 0; plane < 3; plane++)
{
var v = values[plane];
stream.AddRange(HighIdr(h, b =>
{
Pcm(b, h, v, 0, 0);
Pcm(b, h, v, 0, 0);
}, plane));
}
var frames = Decode([.. stream]);
Assert.HasCount(1, frames);
var f = frames[0];
Assert.AreEqual(3, f.ChromaFormat);
Assert.AreEqual(bitDepth, f.BitDepthChroma);
Assert.AreEqual(32, f.ChromaWidth);
for (var i = 0; i < 32 * 16; i++)
{
Assert.AreEqual(values[0], f.LumaAt(i), $"Y {i}");
Assert.AreEqual(values[1], f.ChromaAt(f.Cb, i), $"Cb {i}");
Assert.AreEqual(values[2], f.ChromaAt(f.Cr, i), $"Cr {i}");
}
}
[TestMethod]
[DataRow(0, 8, 1)]
[DataRow(0, 8, 0)]
[DataRow(0, 8, 2)]
[DataRow(0, 10, 1)]
[DataRow(1, 8, 0)]
[DataRow(2, 10, 1)]
[DataRow(3, 8, 1)]
[DataRow(3, 8, 0)]
[DataRow(3, 12, 2)]
public void ALosslessMacroblockAccumulatesItsResidual(int chromaFormat, int bitDepth, int mode)
{
// An I_PCM macroblock, then an Intra_16x16 one predicted from it at QP′Y 0 with qpprime_y_zero_transform_bypass_flag:
// lossless (§8.5.15). Its only residual is a DC level on the first 4×4 block of each luma-like plane, taken as
// it is (no transform, no scaling) and accumulated along the prediction: horizontal (mode 1) adds it to the whole
// first row, vertical (mode 0, the I_PCM macroblock above) to the whole first column, DC (mode 2) only to the
// first sample.
var vertical = mode == 0;
var h = new HighSettings(chromaFormat, bitDepth, bitDepth, Lossless: true, WidthInMbs: vertical ? 1 : 2, HeightInMbs: vertical ? 2 : 1);
var shift = bitDepth - 8;
int[] pcm = [100 << shift, 60 << shift, 180 << shift];
int[] dc = [1, 2, -1];
var frames = Decode([.. HighSps(h), .. HighPps(h), .. HighIdr(h, b =>
{
Pcm(b, h, pcm[0], pcm[1], pcm[2]);
Intra16x16Dc(b, h, mode, dc);
})]);
Assert.HasCount(1, frames);
var f = frames[0];
var planes = chromaFormat == 3 ? 3 : 1;
for (var p = 0; p < planes; p++)
{
var plane = p == 0 ? f.Y : p == 1 ? f.Cb : f.Cr;
for (var y = 0; y < 16; y++)
{
for (var x = 0; x < 16; x++)
{
var touched = mode == 1 ? y == 0 : mode == 0 ? x == 0 : x == 0 && y == 0;
var expected = pcm[p] + (touched ? dc[p] : 0);
var index = vertical ? ((16 + y) * 16) + x : (y * 32) + 16 + x;
var actual = p == 0 ? f.LumaAt(index) : f.ChromaAt(plane, index);
Assert.AreEqual(expected, actual, $"piano {p}, ({x}, {y})");
}
}
}
if (chromaFormat is 1 or 2)
{
// Chroma: DC prediction from the I_PCM chroma, no residual.
for (var i = 0; i < f.ChromaWidth * f.ChromaHeight; i++)
{
Assert.AreEqual(pcm[1], f.ChromaAt(f.Cb, i), $"Cb {i}");
Assert.AreEqual(pcm[2], f.ChromaAt(f.Cr, i), $"Cr {i}");
}
}
}
[TestMethod]
public void CabacIn444IsRefusedUntilItsContextsAreWritten()
{
// The residual of Cb and Cr of 4:4:4 with CABAC needs the contexts 460–1023: not transcribed yet, so the slice is
// refused rather than decoded wrong.
var h = new HighSettings(3, Cabac: true);
Assert.ThrowsExactly<H264Exception>(() => Decode([.. HighSps(h), .. HighPps(h), .. HighIdr(h, b => b.Bits(0, 8))]));
}
private static List<H264Frame> Decode(byte[] stream)
{
var decoder = new H264Decoder();
var frames = new List<H264Frame>();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(stream);
decoder.Flush();
return frames;
}
}