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:
1 parent
479c95f899
commit
b23632fc96
22 files changed
+1273
-282
No files matched your search
@@ -27,8 +27,12 @@ public static class IsoBmffReader
|
||||
/// <summary>Reads a seekable stream from its start.</summary>
|
||||
/// <param name="stream">The file.</param>
|
||||
/// <param name="now">Current time: later creation times are ignored.</param>
|
||||
/// <param name="samples">False to stop at the metadata (no sample tables, no fragments).</param>
|
||||
public static MediaContainer Read(Stream stream, DateTimeOffset now, bool samples = true)
|
||||
/// <param name="samples">
|
||||
/// False to stop at the metadata: no sample tables, and fragments only when they alone tell the duration (then only
|
||||
/// their durations are added up).
|
||||
/// </param>
|
||||
/// <param name="ct">Checked between the top-level boxes (each fragment of a fragmented file).</param>
|
||||
public static MediaContainer Read(Stream stream, DateTimeOffset now, bool samples = true, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(stream);
|
||||
var length = stream.Length;
|
||||
@@ -38,6 +42,7 @@ public static class IsoBmffReader
|
||||
var pending = new List<(long Start, byte[] Body)>();
|
||||
for (var i = 0; i < MaxTopLevelBoxes && position + 8 <= length; i++)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
stream.Position = position;
|
||||
stream.ReadExactly(header[..8]);
|
||||
long size = BinaryPrimitives.ReadUInt32BigEndian(header);
|
||||
@@ -137,7 +142,17 @@ public static class IsoBmffReader
|
||||
movie.ParseMvhd(body, now);
|
||||
break;
|
||||
case "trak":
|
||||
movie.ParseTrak(moov, start, count);
|
||||
try
|
||||
{
|
||||
movie.ParseTrak(moov, start, count);
|
||||
}
|
||||
catch (Exception ex) when (ex is ArgumentOutOfRangeException or IndexOutOfRangeException or OverflowException
|
||||
or ContainerException)
|
||||
{
|
||||
// A damaged track header, edit list or description (often of the audio track): that track is
|
||||
// dropped, the others are still read.
|
||||
}
|
||||
|
||||
break;
|
||||
case "mvex":
|
||||
movie.Fragmented = true;
|
||||
@@ -406,6 +421,11 @@ public static class IsoBmffReader
|
||||
var stz2 = false;
|
||||
foreach (var (type, s, c) in Children(data, start, count))
|
||||
{
|
||||
if (!samples && type != "stsd")
|
||||
{
|
||||
continue; // metadata only: the tables are not even copied
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case "stsd":
|
||||
@@ -528,6 +548,11 @@ public static class IsoBmffReader
|
||||
{
|
||||
case 0x03: // ES_Descriptor
|
||||
{
|
||||
if (p + 3 > body.Length)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var flags = body[p + 2];
|
||||
p += 3;
|
||||
if ((flags & 0x80) != 0)
|
||||
@@ -537,6 +562,11 @@ public static class IsoBmffReader
|
||||
|
||||
if ((flags & 0x40) != 0)
|
||||
{
|
||||
if (p >= body.Length)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
p += 1 + body[p];
|
||||
}
|
||||
|
||||
@@ -789,12 +819,22 @@ public static class IsoBmffReader
|
||||
var bounded = perSample > 0
|
||||
? (long)n * perSample <= body.Length - p
|
||||
: (long)n * Math.Max(1u, defSize) <= fileLength;
|
||||
if (n < 0 || !bounded || track.Samples.Count + n > MaxSamples)
|
||||
if (n < 0 || !bounded || (samples && track.Samples.Count + n > MaxSamples))
|
||||
{
|
||||
throw new ContainerException("Troppi campioni.");
|
||||
}
|
||||
|
||||
var dts = decodeTime ?? Math.Max(0, track.FragmentTime);
|
||||
if (!samples)
|
||||
{
|
||||
// Metadata only: the fragments are read for the duration, so only durations add up.
|
||||
var run = RunDuration(body[p..], flags, n, defDuration);
|
||||
dts = run > (long.MaxValue / 2) - dts ? long.MaxValue / 2 : dts + run;
|
||||
track.FragmentTime = dts;
|
||||
decodeTime = dts;
|
||||
break;
|
||||
}
|
||||
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
var duration = defDuration;
|
||||
@@ -840,6 +880,24 @@ public static class IsoBmffReader
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The total duration of the <paramref name="count"/> samples of a trun, from their fields after the header.</summary>
|
||||
private static long RunDuration(ReadOnlySpan<byte> fields, uint flags, int count, uint defaultDuration)
|
||||
{
|
||||
if ((flags & 0x100) == 0)
|
||||
{
|
||||
return (long)count * defaultDuration;
|
||||
}
|
||||
|
||||
var stride = 4 * (int)(((flags >> 8) & 1) + ((flags >> 9) & 1) + ((flags >> 10) & 1) + ((flags >> 11) & 1));
|
||||
long total = 0;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
total += BinaryPrimitives.ReadUInt32BigEndian(fields[(i * stride)..]); // the duration comes first
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
internal MediaContainer Build()
|
||||
{
|
||||
var video = tracks.Find(t => t.Handler == "vide");
|
||||
|
||||
Reference in new issue
Block a user