Lettori in casa di WMV/ASF, FLV e MPEG: data, durata, dimensioni e codec anche per questi video (#58)

Finora MediaSniffer riconosceva WMV, FLV e MPEG ma la sonda dei metadati non ne sapeva nulla:
data, durata e dimensioni venivano solo dal nome e dalla data di modifica. Tre lettori nuovi,
scritti dalle specifiche pubbliche senza componenti esterni, stanno dietro ContainerReader e
quindi dietro ContainerVideoMetadataProbe e VideoFrameDecoder.

- AsfReader: proprietà del file (data FILETIME, durata meno il preroll, niente durata per le
  trasmissioni), proprietà dei flussi anche dentro l'estensione dell'intestazione, titolo della
  descrizione del contenuto, WM/EncodingTime quando manca la data. Nessun campione.
- FlvReader: onMetaData in AMF0 (durata, dimensioni, creationdate come testo o come data),
  dimensioni dall'SPS della sequenza AVC o dall'intestazione H.263 di Sorenson, durata
  dall'ultimo tag senza metadati, codec degli FLV «enhanced» dal FourCC; i tag H.264 sono
  campioni, così anche un FLV ha la miniatura.
- MpegPsReader: pacchetti MPEG-1 e MPEG-2, intestazione di sistema, mappa del flusso, PES; codec
  e dimensioni dall'intestazione di sequenza (con l'estensione di MPEG-2) o dall'SPS H.264, audio
  MPEG, ADTS, AC-3 e LPCM; durata dal primo all'ultimo PTS attraverso il giro dei 33 bit,
  leggendo solo i primi 2 MB e l'ultimo MB.
- H264StreamInfo: dimensioni ritagliate di un SPS, da un avcC o da un flusso Annex B.
- MediaSniffer: un WMA o un FLV di solo audio non è più un video, quando l'intestazione intera
  nei primi byte lo dimostra.

Prove con file costruiti dalle prove stesse (LegacyTestVideos, semi fissi): metadati, MD5
ufficiali dei fotogrammi da FLV, miniatura dell'FLV uguale a quella dell'MP4, file lunghi e giro
dell'orologio, file troncati e rovinati, AMF annidato senza fine e oggetti ASF più grandi del
file. LEGGIMI dell'importazione e una riga in decisioni.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 18:50:44 +01:00
1 parent 898e9d60e7
commit 6c8b228333
18 files changed
+2983 -13

No files matched your search

@@ -21,6 +21,10 @@ public sealed class MalformedInputTests
[DataRow("frammentato.mp4")]
[DataRow("mkv")]
[DataRow("avi")]
[DataRow("wmv")]
[DataRow("flv")]
[DataRow("mpg")]
[DataRow("h264.mpg")]
public void TruncatedFilesNeverBreakTheReaders(string kind)
{
var file = Build(kind);
@@ -36,6 +40,10 @@ public sealed class MalformedInputTests
[DataRow("frammentato.mp4", 5802)]
[DataRow("mkv", 5803)]
[DataRow("avi", 5804)]
[DataRow("wmv", 5805)]
[DataRow("flv", 5806)]
[DataRow("mpg", 5807)]
[DataRow("h264.mpg", 5808)]
public void GarbledFilesNeverBreakTheReaders(string kind, int seed)
{
var file = Build(kind);
@@ -51,6 +59,9 @@ public sealed class MalformedInputTests
[DataRow("frammentato.mp4", 5812)]
[DataRow("mkv", 5813)]
[DataRow("avi", 5814)]
[DataRow("wmv", 5815)]
[DataRow("flv", 5816)]
[DataRow("mpg", 5817)]
public async Task GarbledFilesNeverBreakTheProbeOrTheFrameDecoder(string kind, int seed)
{
var file = Build(kind);
@@ -67,8 +78,16 @@ public sealed class MalformedInputTests
_ = stillness;
}
// Most damage hits frame data, not headers: many variants still give a frame.
Assert.IsGreaterThan(0, decoded, "nessuna variante ha dato un fotogramma");
// Most damage hits frame data, not headers: many variants still give a frame. WMV and MPEG program streams have
// no samples Febe decodes, so no frame ever.
if (kind is "wmv" or "mpg")
{
Assert.AreEqual(0, decoded);
}
else
{
Assert.IsGreaterThan(0, decoded, "nessuna variante ha dato un fotogramma");
}
}
[TestMethod]
@@ -189,6 +208,54 @@ public sealed class MalformedInputTests
Assert.IsLessThan(4L * 1024 * 1024, allocated, $"{allocated} byte allocati");
}
[TestMethod]
public void DeepOrEndlessFlvMetadataKeepsWhatCameBefore()
{
// onMetaData: a duration, then objects nested a hundred thousand deep (a stack overflow if followed blindly).
var deep = new List<byte> { 2, 0, 10 };
deep.AddRange("onMetaData"u8.ToArray());
deep.Add(3);
deep.AddRange([0, 8, .. "duration"u8, 0, .. Be64(5.0)]);
for (var i = 0; i < 100_000; i++)
{
deep.AddRange([0, 1, (byte)'a', 3]);
}
var container = ReadMeasured(LegacyTestVideos.FlvWithScript([.. deep]), out var allocated);
Assert.AreEqual(TimeSpan.FromSeconds(5), container!.Duration);
Assert.IsLessThan(4L * 1024 * 1024, allocated, $"{allocated} byte allocati");
// A strict array that claims four billion values, then a width: the array ends the reading, the duration stays.
List<byte> endless = [2, 0, 10, .. "onMetaData"u8, 8, 0, 0, 0, 2, 0, 8, .. "duration"u8, 0, .. Be64(7.0),
0, 4, .. "list"u8, 10, 0xFF, 0xFF, 0xFF, 0xFF, 0, .. Be64(1), 0, 5, .. "width"u8, 0, .. Be64(640)];
container = ReadMeasured(LegacyTestVideos.FlvWithScript([.. endless]), out _);
Assert.AreEqual(TimeSpan.FromSeconds(7), container!.Duration);
}
[TestMethod]
public void AnAsfObjectLargerThanItsFileAllocatesNothing()
{
var file = LegacyTestVideos.Asf(new AsfOptions { Title = "Prova" });
// The file properties object (the first after the 30 bytes of the header) claims a gigabyte.
BinaryPrimitives.WriteUInt64LittleEndian(file.AsSpan(30 + 16), 1L << 30);
var container = ReadMeasured(file, out var allocated);
Assert.IsLessThan(4L * 1024 * 1024, allocated, $"{allocated} byte allocati");
Assert.IsNull(container!.Duration, "nulla dopo l'oggetto rovinato");
// A header object smaller than its own fields.
file = LegacyTestVideos.Asf(new AsfOptions());
BinaryPrimitives.WriteUInt64LittleEndian(file.AsSpan(16), 12);
Assert.ThrowsExactly<ContainerException>(() => ReadMeasured(file, out _));
}
private static byte[] Be64(double value)
{
var bytes = new byte[8];
BinaryPrimitives.WriteDoubleBigEndian(bytes, value);
return bytes;
}
private static void AssertReadersSurvive(byte[] data, string what)
{
foreach (var samples in new[] { false, true })
@@ -252,6 +319,16 @@ public sealed class MalformedInputTests
"frammentato.mp4" => TestVideos.FragmentedMp4(es, perFragment: 6, defaults: true),
"mkv" => TestVideos.Matroska(es, new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero)),
"avi" => TestVideos.Avi(es, "Thu Mar 04 05:06:07 2021"),
"wmv" => LegacyTestVideos.Asf(new AsfOptions
{
Created = new DateTimeOffset(2009, 6, 7, 8, 9, 10, TimeSpan.Zero),
EncodingTime = new DateTimeOffset(2009, 6, 7, 8, 9, 10, TimeSpan.Zero),
Title = "Prova",
VideoInExtension = true,
}),
"flv" => LegacyTestVideos.Flv(es, creationDate: "Thu Mar 04 05:06:07 2021", audio: true),
"mpg" => LegacyTestVideos.MpegPs(new PsOptions { Frames = 30, PictureBytes = 300, StreamMap = true }),
"h264.mpg" => LegacyTestVideos.MpegPs(new PsOptions { Video = "h264", Stream = es, Audio = "ac3" }),
_ => TestVideos.Mp4(es, new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero), rotation: 90, audio: true),
};
}