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
@@ -85,14 +85,91 @@ public sealed class MalformedInputTests
|
||||
Assert.IsLessThan(4L * 1024 * 1024, allocated, $"{allocated} byte allocati");
|
||||
Assert.IsEmpty(container!.Video!.Samples, "una tabella impossibile non dà campioni");
|
||||
|
||||
// A trun of two billion samples without per-sample fields.
|
||||
// A trun of almost two million samples without per-sample fields: under the sample limit, but far more than the
|
||||
// file can hold (the trex default size is 0, so each would take at least a byte). That fragment gives nothing,
|
||||
// the others give their samples.
|
||||
var fragmented = TestVideos.FragmentedMp4(es, perFragment: 5);
|
||||
var trun = IndexOf(fragmented, "trun"u8);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(fragmented.AsSpan(trun + 4), 0x000001); // only data_offset
|
||||
BinaryPrimitives.WriteUInt32BigEndian(fragmented.AsSpan(trun + 8), int.MaxValue);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(fragmented.AsSpan(trun + 8), 1_999_999);
|
||||
container = ReadMeasured(fragmented, out allocated);
|
||||
Assert.IsLessThan(4L * 1024 * 1024, allocated, $"{allocated} byte allocati");
|
||||
Assert.IsNotNull(container);
|
||||
Assert.HasCount(es.Count - 5, container!.Video!.Samples, "nessun campione inventato");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task AMatroskaFileWithADamagedOrTruncatedBlockKeepsTheBlocksBefore()
|
||||
{
|
||||
var es = ElementaryStream.Load("CI_MW_D");
|
||||
var file = TestVideos.Matroska(es);
|
||||
using var temp = new TempFolder("matroska-rovinato");
|
||||
var intact = await VideoFrameDecoder.Instance.DecodeFrameAsync(TestVideos.Write(temp, "intero.mkv", file), TimeSpan.FromSeconds(1));
|
||||
Assert.IsNotNull(intact);
|
||||
|
||||
// The blocks close the file: each is the ID, an eight-byte size, track, time, flags and the sample.
|
||||
int StartOf(int block) => file.Length - Enumerable.Range(block, es.Count - block).Sum(i => 1 + 8 + 4 + es.LengthPrefixed(i).Length);
|
||||
|
||||
// Cut inside the fixed part of the last block.
|
||||
var truncated = file[..(StartOf(es.Count - 1) + 1 + 8 + 2)];
|
||||
Assert.HasCount(es.Count - 1, ReadMeasured(truncated, out _)!.Video!.Samples);
|
||||
|
||||
// Block 50 says it holds nothing but its own header: the blocks before it stand, the frame at one second too.
|
||||
var damaged = (byte[])file.Clone();
|
||||
var block = StartOf(50);
|
||||
Assert.AreEqual(0xA3, damaged[block]);
|
||||
damaged.AsSpan(block + 2, 7).Clear();
|
||||
damaged[block + 8] = 4;
|
||||
Assert.HasCount(50, ReadMeasured(damaged, out _)!.Video!.Samples);
|
||||
var image = await VideoFrameDecoder.Instance.DecodeFrameAsync(TestVideos.Write(temp, "rovinato.mkv", damaged), TimeSpan.FromSeconds(1));
|
||||
CollectionAssert.AreEqual(intact.Rgb, image!.Rgb);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ADamagedAudioTrackDoesNotCostTheVideo()
|
||||
{
|
||||
var es = ElementaryStream.Load("CABA3_SVA_B");
|
||||
|
||||
// The descriptors of the audio esds run to its very end: the ES_Descriptor starts at its last two bytes.
|
||||
var file = TestVideos.Mp4(es, audio: true);
|
||||
var esds = IndexOf(file, "esds"u8);
|
||||
var length = (int)BinaryPrimitives.ReadUInt32BigEndian(file.AsSpan(esds - 4)) - 8;
|
||||
var body = file.AsSpan(esds + 4, length);
|
||||
body[4..].Clear();
|
||||
body[4] = 0x7F; // an unknown descriptor that covers the body...
|
||||
body[5] = (byte)(length - 8);
|
||||
body[length - 2] = 0x03; // ...up to an ES_Descriptor with no room for its fields
|
||||
var container = ReadMeasured(file, out _)!;
|
||||
Assert.HasCount(es.Count, container.Video!.Samples);
|
||||
Assert.IsNotNull(container.Audio);
|
||||
Assert.IsNull(container.Audio.AacConfiguration);
|
||||
|
||||
// The audio track header claims version 1, whose fields lie past the end of the box: that track is dropped.
|
||||
file = TestVideos.Mp4(es, audio: true);
|
||||
var audioTkhd = file.AsSpan(IndexOf(file, "tkhd"u8) + 4).IndexOf("tkhd"u8) + IndexOf(file, "tkhd"u8) + 4;
|
||||
file[audioTkhd + 4] = 1;
|
||||
container = ReadMeasured(file, out _)!;
|
||||
Assert.HasCount(es.Count, container.Video!.Samples);
|
||||
Assert.IsNull(container.Audio);
|
||||
using var temp = new TempFolder("audio-rovinato");
|
||||
var metadata = ContainerVideoMetadataProbe.Probe(TestVideos.Write(temp, "video.mp4", file), Now);
|
||||
Assert.AreEqual((es.Width, es.Height), (metadata!.Width, metadata.Height));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AnAviWithAnImpossibleDurationKeepsItsDate()
|
||||
{
|
||||
// One frame per million seconds for a million frames: 10¹² seconds, beyond what a TimeSpan holds.
|
||||
var es = ElementaryStream.Load("BA1_Sony_D");
|
||||
var file = TestVideos.Avi(es, "Thu Mar 04 05:06:07 2021", scale: 1_000_000, rate: 1, length: 1_000_000);
|
||||
var container = ReadMeasured(file, out _)!;
|
||||
Assert.IsNull(container.Duration);
|
||||
Assert.AreEqual(2021, container.CreatedAt!.Value.Year);
|
||||
|
||||
using var temp = new TempFolder("avi-durata");
|
||||
var metadata = ContainerVideoMetadataProbe.Probe(TestVideos.Write(temp, "video.avi", file), Now);
|
||||
Assert.IsNotNull(metadata, "la data scritta nel file non va persa");
|
||||
Assert.AreEqual(2021, metadata.CreatedAt!.Value.Year);
|
||||
Assert.IsNull(metadata.Duration);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
||||
Reference in new issue
Block a user