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

@@ -126,6 +126,17 @@ internal sealed class ElementaryStream
return new ElementaryStream(Name + "-fermo", units, [.. Enumerable.Range(0, times)], [.. Enumerable.Repeat(true, times)], AvcC, Width, Height);
}
/// <summary>
/// The same stream with its SPS and PPS repeated in every access unit, as some cameras and recorders write them: a
/// decoder started at any of its P or B frames finds its parameter sets.
/// </summary>
public ElementaryStream WithParameterSetsEverywhere()
{
var sets = Units[0].Where(n => (n[0] & 0x1F) is 7 or 8).ToList();
var units = Units.Select(u => u.Any(n => (n[0] & 0x1F) == 7) ? u : [.. sets, .. u]).ToList();
return new ElementaryStream(Name + "-parametri", units, Rank, Key, AvcC, Width, Height);
}
/// <summary>An access unit with 4-byte length prefixes (MP4, Matroska).</summary>
public byte[] LengthPrefixed(int index)
{
@@ -328,8 +339,11 @@ internal static class TestVideos
return output.ToArray();
}
/// <summary>A Matroska file with one video track of SimpleBlocks (times in milliseconds).</summary>
public static byte[] Matroska(ElementaryStream es, DateTimeOffset? created = null, bool webm = false)
/// <summary>
/// A Matroska file with one video track of SimpleBlocks, in a single cluster; block times are in ticks of
/// <paramref name="timecodeScale"/> nanoseconds (milliseconds by default).
/// </summary>
public static byte[] Matroska(ElementaryStream es, DateTimeOffset? created = null, bool webm = false, uint timecodeScale = 1_000_000)
{
var shift = 0;
for (var i = 0; i < es.Count; i++)
@@ -340,8 +354,9 @@ internal static class TestVideos
var header = Element(0x1A45DFA3,
Element(0x4286, [1]), Element(0x42F7, [1]), Element(0x42F2, [4]), Element(0x42F3, [8]),
Element(0x4282, Encoding.ASCII.GetBytes(webm ? "webm" : "matroska")), Element(0x4287, [4]), Element(0x4285, [2]));
var durationMs = es.Count * 1000.0 / 30;
var info = new List<byte[]> { Element(0x2AD7B1, U32(1_000_000)), Element(0x4489, F64(durationMs)), Element(0x4D80, "febe-prove"u8.ToArray()) };
var tickMs = timecodeScale / 1e6;
var durationTicks = es.Count * 1000.0 / 30 / tickMs;
var info = new List<byte[]> { Element(0x2AD7B1, U32(timecodeScale)), Element(0x4489, F64(durationTicks)), Element(0x4D80, "febe-prove"u8.ToArray()) };
if (created is { } c)
{
var ns = (c - new DateTimeOffset(2001, 1, 1, 0, 0, 0, TimeSpan.Zero)).Ticks * 100;
@@ -354,7 +369,7 @@ internal static class TestVideos
var blocks = new List<byte[]> { Element(0xE7, [0]) };
for (var i = 0; i < es.Count; i++)
{
var pts = (int)Math.Round((es.Rank[i] + shift) * 1000.0 / 30);
var pts = (int)Math.Round((es.Rank[i] + shift) * 1000.0 / 30 / tickMs);
var payload = es.LengthPrefixed(i);
blocks.Add(Element(0xA3, [0x81, (byte)(pts >> 8), (byte)pts, es.Key[i] ? (byte)0x80 : (byte)0, .. payload]));
}
@@ -363,8 +378,12 @@ internal static class TestVideos
return [.. header, .. segment];
}
/// <summary>An AVI file with one H.264 stream in Annex B form, an idx1 index and, when given, an IDIT date.</summary>
public static byte[] Avi(ElementaryStream es, string? idit = null, bool index = true)
/// <summary>
/// An AVI file with one H.264 stream in Annex B form, an idx1 index (unless <paramref name="index"/> is false, as in a
/// recording cut short) and, when given, an IDIT date. The stream header says <paramref name="rate"/> /
/// <paramref name="scale"/> frames per second and <paramref name="length"/> frames (default: the stream's).
/// </summary>
public static byte[] Avi(ElementaryStream es, string? idit = null, bool index = true, uint scale = 1, uint rate = 30, uint? length = null)
{
var avih = new List<byte>();
foreach (var v in new uint[] { 33333, 0, 0, 0x10, (uint)es.Count, 0, 1, 0, (uint)es.Width, (uint)es.Height, 0, 0, 0, 0 })
@@ -374,7 +393,7 @@ internal static class TestVideos
var strh = new List<byte>();
strh.AddRange("vidsH264"u8.ToArray());
foreach (var v in new uint[] { 0, 0, 0, 1, 30, 0, (uint)es.Count, 0, 0xFFFFFFFF, 0 })
foreach (var v in new uint[] { 0, 0, 0, scale, rate, 0, length ?? (uint)es.Count, 0, 0xFFFFFFFF, 0 })
{
strh.AddRange(Le32(v));
}