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
@@ -240,8 +240,12 @@ internal static class TestVideos
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>A fragmented MP4 (empty sample tables in moov, then moof+mdat pairs of <paramref name="perFragment"/> frames).</summary>
|
||||
public static byte[] FragmentedMp4(ElementaryStream es, int perFragment = 10)
|
||||
/// <summary>
|
||||
/// A fragmented MP4 (empty sample tables in moov, then moof+mdat pairs of <paramref name="perFragment"/> frames).
|
||||
/// With <paramref name="defaults"/> the sample durations come from <c>trex</c>, each fragment has two runs that
|
||||
/// both give their <c>data_offset</c> from the start of the moof, and an <c>mehd</c> box tells the duration.
|
||||
/// </summary>
|
||||
public static byte[] FragmentedMp4(ElementaryStream es, int perFragment = 10, bool defaults = false)
|
||||
{
|
||||
var shift = 0;
|
||||
for (var i = 0; i < es.Count; i++)
|
||||
@@ -268,33 +272,52 @@ internal static class TestVideos
|
||||
var mvhd = FullBox("mvhd", 0, [.. new byte[8], .. U32(1000), .. U32(0), .. U32(0x10000), 1, 0, .. new byte[10],
|
||||
.. U32(0x10000), .. U32(0), .. U32(0), .. U32(0), .. U32(0x10000), .. U32(0), .. U32(0), .. U32(0), .. U32(0x40000000),
|
||||
.. new byte[24], .. U32(2)]);
|
||||
var trex = FullBox("trex", 0, [.. U32(1), .. U32(1), .. U32(FrameTicks), .. U32(0), .. U32(0)]);
|
||||
output.Write(Box("moov", mvhd, Box("trak", tkhd, mdia), Box("mvex", trex)));
|
||||
// trex: track_ID, default_sample_description_index, default duration, size and flags (non-sync).
|
||||
var trex = FullBox("trex", 0, [.. U32(1), .. U32(1), .. U32(FrameTicks), .. U32(0), .. U32(0x01010000)]);
|
||||
var mvex = defaults
|
||||
? Box("mvex", FullBox("mehd", 0, U32((uint)(es.Count * 1000 / 30))), trex) // movie timescale 1000
|
||||
: Box("mvex", trex);
|
||||
output.Write(Box("moov", mvhd, Box("trak", tkhd, mdia), mvex));
|
||||
|
||||
for (int first = 0, sequence = 1; first < es.Count; first += perFragment, sequence++)
|
||||
{
|
||||
var count = Math.Min(perFragment, es.Count - first);
|
||||
var payloads = Enumerable.Range(first, count).Select(es.LengthPrefixed).ToList();
|
||||
byte[] Moof(int dataOffset)
|
||||
byte[] Run(int from, int length, int dataOffset)
|
||||
{
|
||||
var run = new List<byte>();
|
||||
run.AddRange(U32((uint)count));
|
||||
run.AddRange(U32((uint)length));
|
||||
run.AddRange(U32((uint)dataOffset));
|
||||
for (var k = 0; k < count; k++)
|
||||
for (var k = from; k < from + length; k++)
|
||||
{
|
||||
var i = first + k;
|
||||
run.AddRange(U32(FrameTicks));
|
||||
if (!defaults)
|
||||
{
|
||||
run.AddRange(U32(FrameTicks));
|
||||
}
|
||||
|
||||
run.AddRange(U32((uint)payloads[k].Length));
|
||||
run.AddRange(U32(es.Key[i] ? 0x02000000u : 0x01010000u));
|
||||
run.AddRange(U32((uint)((es.Rank[i] - i + shift) * FrameTicks)));
|
||||
}
|
||||
|
||||
// Without the duration field (0x100) every sample takes the trex default.
|
||||
return FullBox("trun", defaults ? 0x000E01u : 0x000F01u, [.. run]);
|
||||
}
|
||||
|
||||
var split = defaults ? (count + 1) / 2 : count;
|
||||
var secondData = payloads.Take(split).Sum(p => p.Length);
|
||||
byte[] Moof(int dataOffset)
|
||||
{
|
||||
var runs = new List<byte[]> { Run(0, split, dataOffset) };
|
||||
if (split < count)
|
||||
{
|
||||
runs.Add(Run(split, count - split, dataOffset + secondData));
|
||||
}
|
||||
|
||||
return Box("moof",
|
||||
FullBox("mfhd", 0, U32((uint)sequence)),
|
||||
Box("traf",
|
||||
FullBox("tfhd", 0x020000, U32(1)),
|
||||
FullBox("tfdt", 0, U32((uint)(first * FrameTicks))),
|
||||
FullBox("trun", 0x000F01, [.. run])));
|
||||
Box("traf", [FullBox("tfhd", 0x020000, U32(1)), FullBox("tfdt", 0, U32((uint)(first * FrameTicks))), .. runs]));
|
||||
}
|
||||
|
||||
var size = Moof(0).Length;
|
||||
|
||||
Reference in new issue
Block a user