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
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user