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>
105 lines
4.7 KiB
C#
105 lines
4.7 KiB
C#
using Febe.Core.Codecs.H264;
|
||
|
||
namespace Febe.Tests.Codecs.H264;
|
||
|
||
/// <summary>
|
||
/// Damaged H.264 streams: the decoder reads bytes of any file the library holds, so whatever they are it must end with
|
||
/// frames or an <see cref="H264Exception"/>, and a damaged parameter set must never make it allocate huge pictures.
|
||
/// </summary>
|
||
[TestClass]
|
||
public sealed class H264RobustnessTests
|
||
{
|
||
[TestMethod]
|
||
[DataRow("BA1_Sony_D", 2641)]
|
||
[DataRow("SVA_BA1_B", 2642)]
|
||
[DataRow("BA3_SVA_C", 2643)]
|
||
[DataRow("BASQP1_Sony_C", 2644)]
|
||
[DataRow("CI_MW_D", 2645)]
|
||
[DataRow("CABA1_SVA_B", 2646)]
|
||
[DataRow("CABA2_SVA_B", 2647)]
|
||
[DataRow("CABA3_SVA_B", 2648)]
|
||
[DataRow("CANL1_SVA_B", 2649)]
|
||
[DataRow("FRExt1_Panasonic_D", 2650)]
|
||
[DataRow("FRExt3_Panasonic_E", 2651)]
|
||
[DataRow("HCAFR1_HHI_C", 2652)]
|
||
public void DamagedStreamsGiveFramesOrAnH264Exception(string name, int seed)
|
||
{
|
||
var folder = Path.Combine(AppContext.BaseDirectory, "Media", "H264", "Conformita", name);
|
||
var data = File.ReadAllBytes(Directory.EnumerateFiles(folder).Single(f => Path.GetExtension(f) is ".264" or ".jsv" or ".avc"));
|
||
var random = new Random(seed);
|
||
for (var trial = 0; trial < 60; trial++)
|
||
{
|
||
var copy = (byte[])data.Clone();
|
||
var changes = 1 + random.Next(20);
|
||
for (var k = 0; k < changes; k++)
|
||
{
|
||
var p = random.Next(copy.Length);
|
||
copy[p] = random.Next(3) switch
|
||
{
|
||
0 => (byte)random.Next(256),
|
||
1 => (byte)(copy[p] ^ (1 << random.Next(8))),
|
||
_ => 0xFF,
|
||
};
|
||
}
|
||
|
||
if (random.Next(4) == 0)
|
||
{
|
||
copy = copy[..random.Next(copy.Length)];
|
||
}
|
||
|
||
try
|
||
{
|
||
var decoder = new H264Decoder();
|
||
decoder.FrameDecoded += _ => { };
|
||
decoder.DecodeAnnexB(copy);
|
||
}
|
||
catch (H264Exception)
|
||
{
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Assert.Fail($"{name}, variante {trial} del seme {seed}: {ex.GetType().Name} {ex.Message}\n{ex.StackTrace}");
|
||
}
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public void APictureLargerThanEveryLevelIsRefusedBeforeItIsAllocated()
|
||
{
|
||
// 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(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(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]
|
||
public void ACropThatLeavesNoPictureIsRefused()
|
||
{
|
||
// 176 × 144 (11 × 9 macroblocks): crop units are two samples in 4:2:0 frames.
|
||
new H264Decoder().DecodeAnnexB(Sps(11, 9, crop: (4, 4, 2, 2)));
|
||
Assert.ThrowsExactly<H264Exception>(() => new H264Decoder().DecodeAnnexB(Sps(11, 9, crop: (44, 44, 0, 0))));
|
||
Assert.ThrowsExactly<H264Exception>(() => new H264Decoder().DecodeAnnexB(Sps(11, 9, crop: (0, 0, 8192, 0))));
|
||
}
|
||
|
||
/// <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));
|
||
} |