Files
Febe/tests/Febe.Tests/Media/H264/H264SyntheticStreamTests.cs
Alby96andClaude Opus 5.5 8d03ca23de Decodificatore H.264 per gli strumenti di Baseline ed Extended: FMO, ASO, slice ridondanti, partizionamento dei dati, slice SP e SI (#58)
Il decodificatore rifiutava i flussi con gruppi di slice e con slice SP o SI, e
non leggeva le partizioni A, B e C: ora li gestisce tutti, perché sono parte di
Baseline ed Extended e un file della libreria può usarli.

- Gruppi di slice dal PPS, tipi da 0 a 6, con il cambio per immagine di
  slice_group_change_cycle; il macroblocco successivo segue il gruppo (§8.2.2).
- Slice in ordine qualunque: la disponibilità dei vicini era già per slice, il
  deblocking a fine immagine.
- Slice SP e SI con QS (§8.6): predizione trasformata e riquantizzata in QS, anche
  per P_Skip; sp_for_switch_flag e macroblocchi SI; nel deblocking contano come
  intra.
- Partizionamento: A con intestazione e dati di categoria 2, B con il residuo
  intra (e gli I_PCM), C con quello inter; la regola del nN a zero con la
  predizione intra vincolata (§9.2.1). Una partizione che manca lascia a zero il
  suo residuo e, da lì in poi, quello della partizione che ne dipende.

I cinque flussi FMO, ASO e SP della serie ITU-T (FM2_SVA_C, FM1_FT_E, FM1_BT_B,
sp1_bt_a, sp2_bt_b) passano fra i verificati e coincidono fotogramma per
fotogramma. Partizionamento, slice ridondanti e ASO si provano riscrivendo flussi
verificati senza toccare i bit dei macroblocchi (StreamRewriter) e chiedendo di
nuovo il riferimento ufficiale; le SI con un flusso sintetico calcolato a mano.
Anche i flussi nuovi e quelli partizionati sono provati rovinati.

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

139 lines
6.2 KiB
C#
Raw Permalink 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 decoder on tiny streams written by <see cref="SyntheticStreams"/>: the parts of the standard no conformance stream in
/// the repository exercises.
/// </summary>
[TestClass]
public sealed class H264SyntheticStreamTests
{
[TestMethod]
[DataRow(16)]
[DataRow(2)]
public void AGapInFrameNumKeepsTheWrapInThePictureOrder(int maxNumRefFrames)
{
// POC type 2, MaxFrameNum 16: references 0..14, then frame_num 3. Frames 15, 0, 1 and 2 are missing (§8.2.5.2):
// the wrap at 0 is among them, so the frame after the gap has FrameNumOffset 16 and POC 2 × (16 + 3) (§8.2.1.3).
// With two reference frames, 15 and 0 are counted without becoming references.
var s = new SequenceSettings(MaxNumRefFrames: maxNumRefFrames, GapsAllowed: true);
var stream = new List<byte>([.. Sps(s), .. Pps(), .. Idr(s, 100)]);
for (var n = 1; n <= 14; n++)
{
stream.AddRange(SkippedP(s, n));
}
stream.AddRange(SkippedP(s, 3));
var frames = Decode([.. stream]);
Assert.HasCount(16, frames);
for (var n = 0; n <= 14; n++)
{
Assert.AreEqual(2 * n, frames[n].Poc, $"fotogramma {n}");
}
Assert.AreEqual(2 * (16 + 3), frames[15].Poc, "il fotogramma dopo il salto");
}
[TestMethod]
public void ARedundantPictureIsSkipped()
{
// The primary IDR picture (luma 100) and a redundant copy (redundant_pic_cnt 1) whose slice also starts at
// macroblock 0: one frame, the primary one.
var s = new SequenceSettings();
byte[] stream = [.. Sps(s), .. Pps(redundantPicCntPresent: true), .. Idr(s, 100, redundantPicCnt: 0), .. Idr(s, 200, redundantPicCnt: 1)];
var frames = Decode(stream);
Assert.HasCount(1, frames);
Assert.IsTrue(frames[0].Y.All(v => v == 100), "il fotogramma è quello primario");
}
[TestMethod]
[DataRow(0, 127, 49, 128)]
[DataRow(12, 130, 52, 126)]
public void AnSiPictureIsRebuiltInQs(int qsDelta, int first, int third, int chroma)
{
// Three macroblocks: SI, I_PCM (luma 50), SI; the SI ones without residual, every block predicted in DC mode. Each
// 4×4 block of an SI macroblock is its prediction transformed, quantised in QS and decoded in QS (§8.6.2), so a
// flat block of 128 comes back as 127 with QS 26 (DC 2048 → level 39 → 8112 → 127) and as 130 with QS 38; next to
// the I_PCM samples (50) the blocks give 49 and 52. Chroma: the DC of the four blocks through the 2×2 transform
// (8192 → level 79 with QSc 26, 28 with QSc 35), back to 128 and 126. Worked out by hand from the equations.
var s = new SequenceSettings(WidthInMbs: 3);
var frames = Decode([.. Sps(s), .. Pps(), .. SiIdr(s, qsDelta, pcm: [1], pcmLuma: 50)]);
Assert.HasCount(1, frames);
var f = frames[0];
for (var y = 0; y < 16; y++)
{
for (var x = 0; x < 48; x++)
{
var expected = x < 16 ? first : x < 32 ? 50 : third;
Assert.AreEqual(expected, f.Y[(y * 48) + x], $"luma ({x}, {y})");
}
}
for (var i = 0; i < f.Cb.Length; i++)
{
var x = i % 24;
var expected = x is >= 8 and < 16 ? 128 : chroma;
Assert.AreEqual(expected, f.Cb[i], $"Cb {i}");
Assert.AreEqual(expected, f.Cr[i], $"Cr {i}");
}
}
[TestMethod]
public void APSliceWithNothingToPredictFromIsRefused()
{
// Decoding that starts at a P picture (an AVI without index, read from a sample that is not a key frame): the
// decoder refuses it instead of predicting the picture from itself.
var s = new SequenceSettings();
var decoder = new H264Decoder();
var frames = new List<H264Frame>();
decoder.FrameDecoded += frames.Add;
Assert.ThrowsExactly<H264Exception>(() => decoder.DecodeAnnexB([.. Sps(s), .. Pps(), .. SkippedP(s, 1)]));
Assert.IsEmpty(frames);
}
[TestMethod]
public void AnUnwantedPictureNobodyRefersToIsNotDecoded()
{
// The second picture is not a reference and its slice data is broken (a skip run past the picture): decoding it
// fails, so a decoder that skips it as unwanted shows it was never reconstructed.
var s = new SequenceSettings(PocType: 0);
byte[] broken = [.. SkippedP(s with { WidthInMbs = 2 }, 1, reference: false)]; // a run of 2 in a 1-macroblock picture
byte[] stream = [.. Sps(s), .. Pps(), .. Idr(s, 100), .. broken, .. SkippedP(s, 1, pocLsb: 4)];
Assert.ThrowsExactly<H264Exception>(() => Decode(stream));
var decoder = new H264Decoder { Wanted = index => index != 1 };
var frames = new List<H264Frame>();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(stream);
Assert.AreEqual(3, decoder.FramesDecoded);
CollectionAssert.AreEqual(new long[] { 0, 2 }, frames.Select(f => f.DecodeIndex).ToArray());
Assert.AreEqual(4, frames[1].Poc, "il fotogramma dopo quello saltato ha il suo ordine");
Assert.IsTrue(frames[1].Y.All(v => v == 100));
}
[TestMethod]
public void TheStartOfDecodingIsRecognised()
{
var s = new SequenceSettings();
Assert.AreEqual(H264AccessPoint.Idr, H264Decoder.AccessPoint([.. Sps(s), .. Pps(), .. Idr(s, 100)], 0));
Assert.AreEqual(H264AccessPoint.None, H264Decoder.AccessPoint([.. Sps(s), .. Pps(), .. SkippedP(s, 1)], 0));
Assert.AreEqual(H264AccessPoint.None, H264Decoder.AccessPoint([], 0));
// Length-prefixed (MP4): the same IDR picture without its start code.
var idr = Idr(s, 100)[4..];
Assert.AreEqual(H264AccessPoint.Idr, H264Decoder.AccessPoint([0, 0, (byte)(idr.Length >> 8), (byte)idr.Length, .. idr], 4));
}
private static List<H264Frame> Decode(byte[] stream)
{
var decoder = new H264Decoder();
var frames = new List<H264Frame>();
decoder.FrameDecoded += frames.Add;
decoder.DecodeAnnexB(stream);
return frames;
}
}