Fotogrammi e contenitori più robusti dopo la revisione: punto di partenza vero, limiti di tempo e di riferimenti, file rovinati tollerati (#58)

Corregge i difetti confermati dalla revisione del decodificatore H.264 e dei lettori
di contenitori.

- Fotogrammi: un AVI senza idx1 (o OpenDML oltre il primo GB) segna ogni campione
  come chiave; ora si parte dall'immagine IDR più vicina (entro 300 campioni),
  altrimenti da un'immagine di slice I o dal primo campione. Oltre 300 campioni vale
  il fotogramma del punto di partenza; ogni chiamata ha al più 30 secondi, come
  l'estrattore con ffmpeg che sostituisce.
- H.264: una slice P/B senza riferimenti è un errore invece di una predizione da sé
  stessa; SPS con riferimenti oltre MaxDpbMbs del livello 6.2 rifiutato (1,4 GB da
  pochi byte); FrameNumOffset dei fotogrammi mancanti di un salto di frame_num;
  immagini ridondanti saltate e inizio d'immagine secondo §7.4.1.2.4; immagini non di
  riferimento non chieste non ricostruite; CABAC con finestra di 64 bit; unità NAL in
  un solo buffer. Il sommario non dice più verificato ciò che nessun MD5 verifica.
- Matroska: un blocco rovinato o troncato ferma la lettura e i blocchi letti restano;
  scala dei tempi esatta per ogni TimecodeScale.
- MP4: una traccia con tkhd/elst/mdhd/hdlr/esds rovinati si scarta senza perdere le
  altre; esds con limiti controllati; nei soli metadati niente copie delle tabelle né
  campioni dei frammenti.
- AVI: durata impossibile ignota senza perdere la data.
- Sonda dei metadati davvero asincrona, annullabile fra i frammenti.
- Prove nuove: flussi H.264 sintetici scritti bit per bit (salto di frame_num, immagine
  ridondante, P senza riferimenti, immagini saltate, SPS enormi con PPS e slice),
  AVI senza indice, blocchi Matroska rovinati, audio MP4 rovinato, durata AVI,
  TimecodeScale di 3 ms, trun sotto il limite ma oltre il file, limite di run e di
  tempo, i due MP4 x264 del sito di prova, ffmpeg/ffprobe fuori dal Dockerfile.
  Provate contro il codice senza le correzioni: falliscono tutte.
- decisioni.md: quattro righe del 18 novembre.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 14:13:45 +01:00
1 parent 479c95f899
commit b23632fc96
22 files changed
+1273 -282

No files matched your search

@@ -0,0 +1,106 @@
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]
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;
}
}