HEVC anche nei flussi di programma MPEG: codec e dimensioni dall'SPS
Il lettore MPEG arrivato con il ramo H.264 prendeva le dimensioni solo dall'SPS dell'H.264; un flusso con il tipo 0x24 nella mappa restava HEVC senza dimensioni. H265StreamInfo legge l'SPS di un flusso Annex B (dopo la finestra di conformità), come H264StreamInfo. Senza mappa un HEVC non si riconosce; i campioni non si elencano, quindi niente miniatura, come per l'H.264. Due righe in decisioni.md: questa e le scelte dell'unione (HEIC nella facciata dei codec, fotogrammi H.264 a 16 bit nel convertitore comune). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7e5eccd1ec
commit
281b2e5bc3
6 files changed
+115
-9
No files matched your search
@@ -2,6 +2,7 @@ using System.Buffers;
|
||||
using System.Buffers.Binary;
|
||||
using System.Globalization;
|
||||
using Febe.Core.Codecs.H264;
|
||||
using Febe.Core.Codecs.H265;
|
||||
|
||||
namespace Febe.Core.Codecs.Containers;
|
||||
|
||||
@@ -524,6 +525,13 @@ public static class MpegPsReader
|
||||
return;
|
||||
}
|
||||
|
||||
// HEVC only with its stream type (0x24) in the stream map: without one, the H.264 look above has the stream.
|
||||
if (type == 0x24 && H265StreamInfo.TryPictureSizeFromAnnexB(data, whole, out width, out height))
|
||||
{
|
||||
(Codec, Width, Height, VideoDone) = (VideoCodec.Hevc, width, height, true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (final)
|
||||
{
|
||||
Codec = type switch
|
||||
@@ -620,7 +628,7 @@ public static class MpegPsReader
|
||||
: Codec.ToString(),
|
||||
Width = Width,
|
||||
Height = Height,
|
||||
AnnexB = Codec == VideoCodec.H264,
|
||||
AnnexB = Codec is VideoCodec.H264 or VideoCodec.Hevc,
|
||||
Timescale = ClockRate,
|
||||
Duration = ticks ?? 0,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
namespace Febe.Core.Codecs.H265;
|
||||
|
||||
/// <summary>
|
||||
/// What the sequence parameter set of an HEVC stream says without decoding it: the size of the pictures shown, after
|
||||
/// the conformance window (§7.4.3.2.1). Used by the container readers whose headers do not carry the size (MPEG program
|
||||
/// streams), as <c>H264StreamInfo</c> is for H.264.
|
||||
/// </summary>
|
||||
public static class H265StreamInfo
|
||||
{
|
||||
/// <summary>nal_unit_type of a sequence parameter set (table 7-1).</summary>
|
||||
private const int SpsType = 33;
|
||||
|
||||
/// <summary>The picture size of an SPS NAL unit (two-byte header 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] & 0x80) != 0 || ((nal[0] >> 1) & 63) != SpsType)
|
||||
{
|
||||
return false; // forbidden_zero_bit set, or not an SPS
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
byte[]? buffer = null;
|
||||
var sps = Sps.Parse(H265NalUnit.Unescape(nal, ref buffer).Reader());
|
||||
var w = sps.Width - sps.ConfLeft - sps.ConfRight;
|
||||
var h = sps.Height - sps.ConfTop - sps.ConfBottom;
|
||||
if (w <= 0 || h <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
(width, height) = (w, h);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is H265Exception or IndexOutOfRangeException or ArgumentException or OverflowException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 (TryPictureSize(nal, out width, out height))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
found = next < 0 ? -1 : end;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -164,10 +164,10 @@ virgolette quando serve, apostrofo davanti a un testo che Excel prenderebbe per
|
||||
trasmissione), dei flussi (codec, dimensioni, audio, anche dentro l'estensione dell'intestazione), il titolo e
|
||||
`WM/EncodingTime` quando manca la data; di un FLV `onMetaData` (durata, dimensioni, `creationdate`), le dimensioni
|
||||
dall'SPS dell'H.264 o dall'intestazione dell'H.263 di Sorenson, e senza durata nei metadati quella fino alla fine dell'ultimo fotogramma (l'ultimo tag video più l'intervallo dal precedente; l'audio solo se non c'è video); di un MPEG
|
||||
(MPEG-1 e MPEG-2, anche VOB) codec e dimensioni dall'intestazione di sequenza o dall'SPS dell'H.264 e la durata dal
|
||||
(MPEG-1 e MPEG-2, anche VOB) codec e dimensioni dall'intestazione di sequenza o dall'SPS dell'H.264 o dell'HEVC (tipo 0x24 nella mappa) e la durata dal
|
||||
primo all'ultimo tempo di presentazione, leggendo solo i primi 2 MB e l'ultimo MB. Un MPEG non ha data. La
|
||||
miniatura viene solo dagli FLV H.264: WMV (VC-1) e MPEG-2 non si decodificano, e i campioni di un MPEG, spezzati nei
|
||||
pacchetti PES, non si elencano, nemmeno se H.264.
|
||||
pacchetti PES, non si elencano, nemmeno se H.264 o HEVC.
|
||||
- HEIC/HEIF: dimensioni, impronta e miniatura vengono dal decodificatore scritto in casa (`Febe.Core.Codecs.Heif`, sul
|
||||
decodificatore HEVC): immagine singola o griglia di riquadri, `clap`, `irot`/`imir`, trasparenza. La data di scatto
|
||||
si legge dall'oggetto `Exif` del box `meta` solo se sta nei primi 512 KB del file. Restano senza miniatura gli HEIF
|
||||
|
||||
Reference in new issue
Block a user