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
@@ -66,14 +66,28 @@ public sealed class H264RobustnessTests
|
||||
[TestMethod]
|
||||
public void APictureLargerThanEveryLevelIsRefusedBeforeItIsAllocated()
|
||||
{
|
||||
// 1024 × 1024 macroblocks (16384 × 16384): what ue(v) allows, a hundred times what level 6.2 does.
|
||||
// 1024 × 1024 macroblocks (16384 × 16384): what ue(v) allows, a hundred times what level 6.2 does. A PPS and the
|
||||
// start of an IDR slice follow, so a decoder without the bound would really allocate its pictures.
|
||||
var huge = new SyntheticStreams.SequenceSettings(WidthInMbs: 1024, HeightInMbs: 1024);
|
||||
byte[] stream = [.. SyntheticStreams.Sps(huge), .. SyntheticStreams.Pps(), .. SyntheticStreams.Idr(huge, 100, macroblocks: 1)];
|
||||
var before = GC.GetAllocatedBytesForCurrentThread();
|
||||
var error = Assert.ThrowsExactly<H264Exception>(() => new H264Decoder().DecodeAnnexB(Sps(widthInMbs: 1024, heightInMbs: 1024)));
|
||||
var error = Assert.ThrowsExactly<H264Exception>(() => new H264Decoder().DecodeAnnexB(stream));
|
||||
StringAssert.Contains(error.Message, "livello");
|
||||
Assert.IsLessThan(1024L * 1024, GC.GetAllocatedBytesForCurrentThread() - before);
|
||||
|
||||
// The largest frame of level 6.2 is still accepted.
|
||||
new H264Decoder().DecodeAnnexB(Sps(widthInMbs: 512, heightInMbs: 272));
|
||||
new H264Decoder().DecodeAnnexB(SyntheticStreams.Sps(new(WidthInMbs: 512, HeightInMbs: 272)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MoreReferenceFramesThanTheLargestBufferAreRefused()
|
||||
{
|
||||
// 8192 × 4352 with sixteen reference frames would be 1.4 GB of pictures; level 6.2 holds five of that size.
|
||||
var error = Assert.ThrowsExactly<H264Exception>(() =>
|
||||
new H264Decoder().DecodeAnnexB(SyntheticStreams.Sps(new(WidthInMbs: 512, HeightInMbs: 272, MaxNumRefFrames: 16))));
|
||||
StringAssert.Contains(error.Message, "riferimento");
|
||||
new H264Decoder().DecodeAnnexB(SyntheticStreams.Sps(new(WidthInMbs: 512, HeightInMbs: 272, MaxNumRefFrames: 5)));
|
||||
new H264Decoder().DecodeAnnexB(SyntheticStreams.Sps(new(WidthInMbs: 120, HeightInMbs: 68, MaxNumRefFrames: 16)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -85,83 +99,7 @@ public sealed class H264RobustnessTests
|
||||
Assert.ThrowsExactly<H264Exception>(() => new H264Decoder().DecodeAnnexB(Sps(11, 9, crop: (0, 0, 8192, 0))));
|
||||
}
|
||||
|
||||
/// <summary>A Baseline SPS NAL unit in Annex B form, written bit by bit (§7.3.2.1.1).</summary>
|
||||
private static byte[] Sps(int widthInMbs, int heightInMbs, (int Left, int Right, int Top, int Bottom)? crop = null)
|
||||
{
|
||||
var bits = new BitWriter();
|
||||
bits.Bits(66, 8); // profile_idc: Baseline
|
||||
bits.Bits(0, 8); // constraint flags
|
||||
bits.Bits(30, 8); // level_idc
|
||||
bits.Ue(0); // seq_parameter_set_id
|
||||
bits.Ue(0); // log2_max_frame_num_minus4
|
||||
bits.Ue(2); // pic_order_cnt_type
|
||||
bits.Ue(1); // max_num_ref_frames
|
||||
bits.Bits(0, 1); // gaps_in_frame_num_value_allowed_flag
|
||||
bits.Ue((uint)widthInMbs - 1);
|
||||
bits.Ue((uint)heightInMbs - 1);
|
||||
bits.Bits(1, 1); // frame_mbs_only_flag
|
||||
bits.Bits(1, 1); // direct_8x8_inference_flag
|
||||
bits.Bits(crop is null ? 0u : 1u, 1);
|
||||
if (crop is { } c)
|
||||
{
|
||||
bits.Ue((uint)c.Left);
|
||||
bits.Ue((uint)c.Right);
|
||||
bits.Ue((uint)c.Top);
|
||||
bits.Ue((uint)c.Bottom);
|
||||
}
|
||||
|
||||
bits.Bits(0, 1); // vui_parameters_present_flag
|
||||
bits.Bits(1, 1); // rbsp_stop_one_bit
|
||||
return [0, 0, 0, 1, 0x67, .. bits.ToArray()];
|
||||
}
|
||||
|
||||
private sealed class BitWriter
|
||||
{
|
||||
private readonly List<bool> bits = [];
|
||||
|
||||
public void Bits(uint value, int count)
|
||||
{
|
||||
for (var i = count - 1; i >= 0; i--)
|
||||
{
|
||||
bits.Add(((value >> i) & 1) != 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void Ue(uint value)
|
||||
{
|
||||
var code = value + 1;
|
||||
var length = 32 - System.Numerics.BitOperations.LeadingZeroCount(code);
|
||||
Bits(0, length - 1);
|
||||
Bits(code, length);
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
var bytes = new byte[(bits.Count + 7) / 8];
|
||||
for (var i = 0; i < bits.Count; i++)
|
||||
{
|
||||
if (bits[i])
|
||||
{
|
||||
bytes[i / 8] |= (byte)(0x80 >> (i % 8));
|
||||
}
|
||||
}
|
||||
|
||||
// Emulation prevention (§7.4.1): never two zero bytes followed by a byte up to 3.
|
||||
var output = new List<byte>();
|
||||
var zeros = 0;
|
||||
foreach (var b in bytes)
|
||||
{
|
||||
if (zeros >= 2 && b <= 3)
|
||||
{
|
||||
output.Add(3);
|
||||
zeros = 0;
|
||||
}
|
||||
|
||||
output.Add(b);
|
||||
zeros = b == 0 ? zeros + 1 : 0;
|
||||
}
|
||||
|
||||
return [.. output];
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>A Baseline SPS of the given size and crop.</summary>
|
||||
private static byte[] Sps(int widthInMbs, int heightInMbs, (int, int, int, int) crop) =>
|
||||
SyntheticStreams.Sps(new(WidthInMbs: widthInMbs, HeightInMbs: heightInMbs, Crop: crop));
|
||||
}
|
||||
Reference in new issue
Block a user