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
@@ -34,7 +34,8 @@ public static class MatroskaReader
|
||||
/// <param name="stream">The file.</param>
|
||||
/// <param name="now">Current time: later creation times are ignored.</param>
|
||||
/// <param name="samples">False to stop before the first cluster.</param>
|
||||
public static MediaContainer Read(Stream stream, DateTimeOffset now, bool samples = true)
|
||||
/// <param name="ct">Checked between the top-level elements.</param>
|
||||
public static MediaContainer Read(Stream stream, DateTimeOffset now, bool samples = true, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(stream);
|
||||
var reader = new Reader(stream);
|
||||
@@ -82,45 +83,55 @@ public static class MatroskaReader
|
||||
var state = new State();
|
||||
while (stream.Position < segmentEnd)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
var elementStart = stream.Position;
|
||||
if (segmentEnd - elementStart < 2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
(id, size) = reader.Header();
|
||||
var bodyEnd = size < 0 ? segmentEnd : stream.Position + size;
|
||||
if (bodyEnd > segmentEnd)
|
||||
try
|
||||
{
|
||||
bodyEnd = segmentEnd; // truncated file
|
||||
}
|
||||
(id, size) = reader.Header();
|
||||
var bodyEnd = size < 0 ? segmentEnd : stream.Position + size;
|
||||
if (bodyEnd > segmentEnd)
|
||||
{
|
||||
bodyEnd = segmentEnd; // truncated file
|
||||
}
|
||||
|
||||
switch (id)
|
||||
switch (id)
|
||||
{
|
||||
case InfoId:
|
||||
ParseInfo(reader, bodyEnd, state, now);
|
||||
break;
|
||||
case TracksId:
|
||||
ParseTracks(reader, bodyEnd, state);
|
||||
break;
|
||||
case ClusterId:
|
||||
if (!samples)
|
||||
{
|
||||
return state.Build();
|
||||
}
|
||||
|
||||
ParseCluster(reader, bodyEnd, size < 0, state);
|
||||
continue; // the position is already past the cluster
|
||||
default:
|
||||
if (size < 0)
|
||||
{
|
||||
throw new ContainerException("Elemento Matroska di lunghezza ignota.");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
stream.Position = bodyEnd;
|
||||
}
|
||||
catch (Exception ex) when ((ex is ContainerException or EndOfStreamException) && state.HasSamples)
|
||||
{
|
||||
case InfoId:
|
||||
ParseInfo(reader, bodyEnd, state, now);
|
||||
break;
|
||||
case TracksId:
|
||||
ParseTracks(reader, bodyEnd, state);
|
||||
break;
|
||||
case ClusterId:
|
||||
if (!samples)
|
||||
{
|
||||
return state.Build();
|
||||
}
|
||||
|
||||
ParseCluster(reader, bodyEnd, size < 0, state);
|
||||
continue; // the position is already past the cluster
|
||||
default:
|
||||
if (size < 0)
|
||||
{
|
||||
throw new ContainerException("Elemento Matroska di lunghezza ignota.");
|
||||
}
|
||||
|
||||
break;
|
||||
// A damaged or truncated block after the first ones, or more samples than the limit: what was read
|
||||
// stands, as in the ISO reader.
|
||||
break;
|
||||
}
|
||||
|
||||
stream.Position = bodyEnd;
|
||||
}
|
||||
|
||||
return state.Build();
|
||||
@@ -377,7 +388,7 @@ public static class MatroskaReader
|
||||
throw new ContainerException("Troppi campioni.");
|
||||
}
|
||||
|
||||
var time = clusterTime + relative;
|
||||
var time = state.ToUnits(clusterTime + relative);
|
||||
var lacing = (flags >> 1) & 3;
|
||||
var dataStart = stream.Position;
|
||||
if (lacing == 0)
|
||||
@@ -456,7 +467,7 @@ public static class MatroskaReader
|
||||
}
|
||||
|
||||
var offset = dataBegin;
|
||||
var frameDuration = target.DefaultDuration > 0 && state.TimecodeScale > 0 ? target.DefaultDuration / state.TimecodeScale : 0;
|
||||
var frameDuration = target.DefaultDuration > 0 ? target.DefaultDuration / state.UnitNanoseconds : 0;
|
||||
for (var i = 0; i < frames; i++)
|
||||
{
|
||||
if (sizes[i] < 0 || sizes[i] > int.MaxValue || offset + sizes[i] > end)
|
||||
@@ -498,9 +509,53 @@ public static class MatroskaReader
|
||||
internal TrackEntry? Video;
|
||||
internal TrackEntry? Audio;
|
||||
|
||||
/// <summary>Nanoseconds of one unit of the sample times; fixed by the first block, 0 before.</summary>
|
||||
private long unit;
|
||||
|
||||
internal bool HasSamples => Video?.Samples.Count > 0 || Audio?.Samples.Count > 0;
|
||||
|
||||
/// <summary>
|
||||
/// Nanoseconds of one unit of the sample times: the greatest common divisor of TimecodeScale and a second, so
|
||||
/// that the timescale of the tracks (a second in units) is exact for any TimecodeScale (3 ms gives units of
|
||||
/// 1 ms, 2 s units of 1 s) and the block times are whole units.
|
||||
/// </summary>
|
||||
internal long UnitNanoseconds
|
||||
{
|
||||
get
|
||||
{
|
||||
if (unit == 0)
|
||||
{
|
||||
long a = Scale, b = 1_000_000_000;
|
||||
while (b != 0)
|
||||
{
|
||||
(a, b) = (b, a % b);
|
||||
}
|
||||
|
||||
unit = a;
|
||||
}
|
||||
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>TimecodeScale (nanoseconds of a tick of the block times); the default for a damaged value.</summary>
|
||||
private long Scale => TimecodeScale > 0 ? TimecodeScale : 1_000_000;
|
||||
|
||||
/// <summary>Units of <see cref="UnitNanoseconds"/> in a tick (at least 1, should Info come after the first block).</summary>
|
||||
private long Factor => Math.Max(1, Scale / UnitNanoseconds);
|
||||
|
||||
/// <summary>A time in TimecodeScale ticks as a time in units of <see cref="UnitNanoseconds"/>.</summary>
|
||||
internal long ToUnits(long ticks)
|
||||
{
|
||||
var limit = long.MaxValue / 4 / Factor;
|
||||
return Math.Clamp(ticks, -limit, limit) * Factor; // a damaged cluster time stays a (wrong) number
|
||||
}
|
||||
|
||||
internal MediaContainer Build()
|
||||
{
|
||||
var timescale = Math.Max(1, 1_000_000_000L / Math.Max(1, TimecodeScale));
|
||||
var timescale = 1_000_000_000L / UnitNanoseconds;
|
||||
var trackDuration = DurationTicks * Factor;
|
||||
var trackUnits = trackDuration is > 0 and < long.MaxValue / 2 ? (long)trackDuration : 0;
|
||||
VideoTrack? video = null;
|
||||
if (Video is { } v)
|
||||
{
|
||||
@@ -541,7 +596,7 @@ public static class MatroskaReader
|
||||
Rotation = roll is 90 or 180 or 270 ? roll : 0,
|
||||
AvcConfiguration = v.CodecId == "V_MPEG4/ISO/AVC" ? v.CodecPrivate : null,
|
||||
Timescale = timescale,
|
||||
Duration = DurationTicks < long.MaxValue / 2 ? (long)DurationTicks : 0,
|
||||
Duration = trackUnits,
|
||||
Samples = samples,
|
||||
};
|
||||
}
|
||||
@@ -565,7 +620,7 @@ public static class MatroskaReader
|
||||
Channels = a.Channels,
|
||||
AacConfiguration = a.CodecId == "A_AAC" ? a.CodecPrivate : null,
|
||||
Timescale = timescale,
|
||||
Duration = DurationTicks < long.MaxValue / 2 ? (long)DurationTicks : 0,
|
||||
Duration = trackUnits,
|
||||
Samples = a.Samples,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user