Contenitori e decodificatore H.264 provati sui file rovinati; corretti trex, data_offset e ordine dei blocchi MP4 (#58)
Revisione del lavoro in corso sui lettori in casa che sostituiscono ffmpeg e ffprobe. La corsa completa di tests/Febe.Tests ora è verde. Difetti corretti: - ISO BMFF frammentato: i valori di trex erano letti con i campi scambiati (la durata predefinita prendeva la dimensione, i flag andavano persi) e il data_offset di un secondo trun si sommava alla fine del primo invece che alla base del moof. - Mp4Writer: i blocchi di una traccia con tempi uguali (audio senza tempi, come da AVI) potevano uscire fuori ordine, perché l'ordinamento non è stabile; ora restano nell'ordine dei campioni. - Durata di un MP4 frammentato nella sola lettura dei metadati: da mehd o, se manca, dai frammenti (prima restava ignota). - File danneggiati: un stsz o un trun di pochi byte poteva chiedere centinaia di MB, un elemento Matroska più grande del file veniva allocato, un salto oltre i 2 GB faceva fallire MemoryStream, IndexOutOfRange e conversioni di date e durate uscivano come eccezioni non previste. Ora ogni tabella si controlla contro il file prima di allocare, al più 2.000.000 di campioni per traccia e 64 MB per campione, e una traccia MP4 con tabelle rovinate resta senza campioni senza togliere le altre. - H.264: un SPS oltre il MaxFS del livello 6.2 o con un ritaglio che non lascia immagine è rifiutato prima di allocare; mai più riferimenti di Max(max_num_ref_frames, 1); gli errori di deblocco e ritaglio escono come H264Exception. - VideoFrameDecoder: i fotogrammi si associano al campione decodificato, non al conteggio del decodificatore (un campione senza immagine non sposta più quelli dopo); un campione oltre la fine di un file troncato non toglie i fotogrammi già decodificati; il fotogramma per la miniatura è ridotto a 960 pixel sul lato lungo. Prove nuove: file troncati e rovinati con semi fissi per MP4, MP4 frammentato, Matroska e AVI (lettori, sonda e decodificatore dei fotogrammi, con limite di memoria allocata), flussi di conformità rovinati per il decodificatore H.264, SPS troppo grande e ritaglio non valido, MP4 frammentato con valori predefiniti e due trun, ordine dei blocchi con tempi uguali. Le prove nuove dei difetti falliscono sul codice di prima. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6ffcb1bb18
commit
479c95f899
14 files changed
+787
-113
No files matched your search
@@ -72,10 +72,11 @@ public sealed class VideoFrameDecoder
|
||||
|
||||
/// <summary>
|
||||
/// A frame of the video as a BMP image, taken one second in (or the first frame of shorter videos), rotated as the
|
||||
/// video is shown; null when the file cannot be read or decoded.
|
||||
/// video is shown, reduced to at most twice the long side of a thumbnail (a 4K frame would be a 24 MB bitmap);
|
||||
/// null when the file cannot be read or decoded.
|
||||
/// </summary>
|
||||
public Task<byte[]?> ExtractImageAsync(string videoPath, CancellationToken ct = default) =>
|
||||
ExtractImageAsync(videoPath, TimeSpan.FromSeconds(1), 0, ct);
|
||||
ExtractImageAsync(videoPath, TimeSpan.FromSeconds(1), 2 * ThumbnailStore.LongSide, ct);
|
||||
|
||||
/// <summary>
|
||||
/// A frame of the video at <paramref name="at"/> (the first frame when the video is shorter), as a BMP image whose
|
||||
@@ -288,6 +289,9 @@ public sealed class VideoFrameDecoder
|
||||
private readonly HashSet<int> wanted = [];
|
||||
private byte[] buffer = new byte[64 * 1024];
|
||||
|
||||
/// <summary>The sample being decoded: the picture it finishes is that sample's frame.</summary>
|
||||
private int current;
|
||||
|
||||
internal Dictionary<int, H264Frame> Decode(int[] targets, CancellationToken ct)
|
||||
{
|
||||
foreach (var t in targets)
|
||||
@@ -304,23 +308,24 @@ public sealed class VideoFrameDecoder
|
||||
var key = KeyBefore(keys, target);
|
||||
if (decoder is null || next > target || key >= next)
|
||||
{
|
||||
decoder = new H264Decoder();
|
||||
var runStart = key;
|
||||
decoder.Wanted = index => wanted.Contains(runStart + (int)index);
|
||||
decoder.FrameDecoded += frame => found.TryAdd(runStart + (int)frame.DecodeIndex, frame);
|
||||
if (!video.AnnexB)
|
||||
{
|
||||
lengthSize = decoder.ConfigureAvcC(video.AvcConfiguration);
|
||||
}
|
||||
|
||||
// Frames are matched to samples by the sample being decoded, not by the decoder's own count: a
|
||||
// sample without a picture (only parameter sets, or damaged) must not shift the frames after it.
|
||||
decoder = new H264Decoder { Wanted = _ => wanted.Contains(current) };
|
||||
decoder.FrameDecoded += frame => found.TryAdd(current, frame);
|
||||
next = key;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (next == key && !video.AnnexB)
|
||||
{
|
||||
lengthSize = decoder.ConfigureAvcC(video.AvcConfiguration);
|
||||
}
|
||||
|
||||
for (; next <= target; next++)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
current = next;
|
||||
var sample = ContainerReader.ReadSample(stream, video.Samples[next], ref buffer);
|
||||
if (video.AnnexB)
|
||||
{
|
||||
@@ -332,9 +337,12 @@ public sealed class VideoFrameDecoder
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is H264Exception or IndexOutOfRangeException or ArgumentException or InvalidOperationException)
|
||||
catch (Exception ex) when (ex is H264Exception or ContainerException or EndOfStreamException or IndexOutOfRangeException
|
||||
or ArgumentException or InvalidOperationException)
|
||||
{
|
||||
decoder = null; // this run is damaged: the next target starts again from its own key frame
|
||||
// This run is damaged (or its samples lie past the end of a truncated file): the next target starts
|
||||
// again from its own key frame, and what was decoded so far stands.
|
||||
decoder = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user