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

@@ -0,0 +1,92 @@
using System.Buffers.Binary;
namespace Febe.Core.Codecs.H264;
/// <summary>
/// What the sequence parameter set of an H.264 stream says without decoding it: the size of the pictures shown, after
/// the cropping of §7.4.2.1.1. Used by the container readers whose headers do not carry the size (FLV without
/// metadata, MPEG program streams).
/// </summary>
public static class H264StreamInfo
{
/// <summary>The picture size of an SPS NAL unit (header byte included); false when it is not an SPS or is damaged.</summary>
public static bool TryPictureSize(ReadOnlySpan<byte> nal, out int width, out int height)
{
width = height = 0;
if (nal.Length < 4 || (nal[0] & 0x9F) != 7 || nal[1] is not (44 or 66 or 77 or 83 or 86 or 88 or 100 or 110 or 118 or 122
or 128 or 134 or 135 or 138 or 139 or 144 or 244))
{
return false; // forbidden_zero_bit set, not nal_unit_type 7, or a profile_idc no profile of Annex A has
}
try
{
var sps = Sps.Parse(NalUnits.Parse(nal).Reader());
var w = (sps.WidthInMbs * 16) - (sps.CropUnitX * (sps.CropLeft + sps.CropRight));
var h = (sps.HeightInMbs * 16) - (sps.CropUnitY * (sps.CropTop + sps.CropBottom));
if (w <= 0 || h <= 0)
{
return false;
}
(width, height) = (w, h);
return true;
}
catch (Exception ex) when (ex is H264Exception or IndexOutOfRangeException or ArgumentException or OverflowException)
{
return false;
}
}
/// <summary>
/// The picture size of the first SPS of an AVCDecoderConfigurationRecord (ISO/IEC 14496-15 §5.3.3.1), as FLV and MP4
/// carry it.
/// </summary>
public static bool TryPictureSizeFromConfiguration(ReadOnlySpan<byte> avcC, out int width, out int height)
{
width = height = 0;
if (avcC.Length < 8 || avcC[0] != 1 || (avcC[5] & 0x1F) == 0)
{
return false;
}
var length = BinaryPrimitives.ReadUInt16BigEndian(avcC[6..]);
return 8 + length <= avcC.Length && TryPictureSize(avcC.Slice(8, length), out width, out height);
}
/// <summary>
/// The picture size of the first whole SPS of an Annex B byte stream; an SPS that the end of
/// <paramref name="stream"/> cuts counts only when <paramref name="complete"/> says nothing follows.
/// </summary>
public static bool TryPictureSizeFromAnnexB(ReadOnlySpan<byte> stream, bool complete, out int width, out int height)
{
width = height = 0;
ReadOnlySpan<byte> startCode = [0, 0, 1];
var found = stream.IndexOf(startCode);
while (found >= 0)
{
var start = found + 3;
var next = stream[start..].IndexOf(startCode);
if (next < 0 && !complete)
{
return false; // the last unit may be cut short
}
var end = next < 0 ? stream.Length : start + next;
var nal = stream[start..end];
while (nal.Length > 0 && nal[^1] == 0)
{
nal = nal[..^1]; // the zero byte of a four-byte start code belongs to the next unit
}
if (nal.Length > 0 && (nal[0] & 0x1F) == 7 && TryPictureSize(nal, out width, out height))
{
return true;
}
found = next < 0 ? -1 : end;
}
return false;
}
}