Decodificatore HEVC scritto in casa, prima parte: le immagini intra coincidono coi flussi di conformità (#58)
Per leggere i video HEVC senza ffmpeg serve un decodificatore nostro, scritto dalla norma ITU-T H.265. Questa prima parte copre tutto ciò che serve alle immagini intra: unità NAL Annex B e hvcC, VPS/SPS/PPS (liste di scala, VUI, indicatori RExt), intestazione di slice, POC, RPS, DPB con uscita e bumping (C.5.2), CABAC con tutte le tabelle d'inizio, albero di codifica, unità di predizione, residui con sign data hiding e transform skip, predizione intra, DCT/DST e dequantizzazione, PCM, deblocking e SAO, tile, WPP e segmenti dipendenti. I 12 flussi di sole slice I dei profili Main, Main 10 e Main Still Picture coincidono col YUV ufficiale. Nei 136 flussi Main e Main 10 ogni immagine di sole slice I coincide con l'MD5 SEI del codificatore, dove il flusso lo porta. Le slice P e B si rifiutano finché manca la predizione inter (tappa successiva). Le prove ne leggono comunque la sintassi fino alla fine di ogni slice, con le unità di predizione grigie. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6274c4ee03
commit
f77b5e4db6
17 files changed
+5708
-9
No files matched your search
@@ -0,0 +1,159 @@
|
||||
namespace Febe.Core.Codecs.H265;
|
||||
|
||||
/// <summary>
|
||||
/// A decoded HEVC picture in the decoded picture buffer: its sample arrays at full size (pic_width_in_luma_samples ×
|
||||
/// pic_height_in_luma_samples, before the conformance window), one 16-bit value per sample whatever the bit depth, and
|
||||
/// its state for reference and output (§8.3.2, C.5.2).
|
||||
/// </summary>
|
||||
internal sealed class Picture
|
||||
{
|
||||
internal Picture(Sps sps)
|
||||
{
|
||||
Sps = sps;
|
||||
Width = sps.Width;
|
||||
Height = sps.Height;
|
||||
ChromaWidth = sps.ChromaArrayType == 0 ? 0 : Width / sps.SubWidthC;
|
||||
ChromaHeight = sps.ChromaArrayType == 0 ? 0 : Height / sps.SubHeightC;
|
||||
Planes =
|
||||
[
|
||||
new ushort[Width * Height],
|
||||
new ushort[ChromaWidth * ChromaHeight],
|
||||
new ushort[ChromaWidth * ChromaHeight],
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>The SPS of the picture: a picture of the pool is reused for another SPS of the same size.</summary>
|
||||
internal Sps Sps { get; set; }
|
||||
|
||||
internal int Width { get; }
|
||||
|
||||
internal int Height { get; }
|
||||
|
||||
internal int ChromaWidth { get; }
|
||||
|
||||
internal int ChromaHeight { get; }
|
||||
|
||||
/// <summary>Y, Cb and Cr; the chroma arrays are empty for 4:0:0.</summary>
|
||||
internal ushort[][] Planes { get; }
|
||||
|
||||
internal int Poc;
|
||||
|
||||
internal bool ShortTerm;
|
||||
|
||||
internal bool LongTerm;
|
||||
|
||||
/// <summary>Marked "needed for output" (C.5.2).</summary>
|
||||
internal bool NeededForOutput;
|
||||
|
||||
internal bool OutputFlag;
|
||||
|
||||
internal int LatencyCount;
|
||||
|
||||
/// <summary>Position of the coded picture in decoding order, counting the pictures that are skipped too.</summary>
|
||||
internal long DecodeIndex;
|
||||
|
||||
/// <summary>A picture generated for a missing reference (§8.3.3): never output, its samples are not meaningful.</summary>
|
||||
internal bool Generated;
|
||||
|
||||
/// <summary>Every slice of the picture is an I slice: it does not depend on other pictures.</summary>
|
||||
internal bool IntraOnly;
|
||||
|
||||
internal bool IsReference => ShortTerm || LongTerm;
|
||||
|
||||
internal int Stride(int cIdx) => cIdx == 0 ? Width : ChromaWidth;
|
||||
|
||||
internal int BitDepth(int cIdx) => cIdx == 0 ? Sps.BitDepthY : Sps.BitDepthC;
|
||||
|
||||
internal bool Fits(Sps sps) => Width == sps.Width && Height == sps.Height && ChromaWidth == (sps.ChromaArrayType == 0 ? 0 : sps.Width / sps.SubWidthC)
|
||||
&& ChromaHeight == (sps.ChromaArrayType == 0 ? 0 : sps.Height / sps.SubHeightC);
|
||||
|
||||
/// <summary>The picture cropped to the conformance window.</summary>
|
||||
internal H265Frame ToFrame()
|
||||
{
|
||||
var s = Sps;
|
||||
var width = Width - s.ConfLeft - s.ConfRight;
|
||||
var height = Height - s.ConfTop - s.ConfBottom;
|
||||
var y = Crop(Planes[0], Width, s.ConfLeft, s.ConfTop, width, height);
|
||||
ushort[] cb = [], cr = [];
|
||||
if (s.ChromaArrayType != 0)
|
||||
{
|
||||
int cx = s.ConfLeft / s.SubWidthC, cy = s.ConfTop / s.SubHeightC;
|
||||
int cw = width / s.SubWidthC, ch = height / s.SubHeightC;
|
||||
cb = Crop(Planes[1], ChromaWidth, cx, cy, cw, ch);
|
||||
cr = Crop(Planes[2], ChromaWidth, cx, cy, cw, ch);
|
||||
}
|
||||
|
||||
return new H265Frame(width, height, s.ChromaArrayType, s.BitDepthY, s.BitDepthC, y, cb, cr, Poc, DecodeIndex, s.MatrixCoefficients, s.FullRange);
|
||||
}
|
||||
|
||||
private static ushort[] Crop(ushort[] plane, int stride, int x0, int y0, int width, int height)
|
||||
{
|
||||
var result = new ushort[width * height];
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
plane.AsSpan(((y0 + y) * stride) + x0, width).CopyTo(result.AsSpan(y * width, width));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A decoded HEVC frame, cropped to the conformance window, with one 16-bit value per sample.</summary>
|
||||
/// <param name="Width">Luma width after cropping.</param>
|
||||
/// <param name="Height">Luma height after cropping.</param>
|
||||
/// <param name="ChromaFormat">ChromaArrayType: 0 (only luma), 1 (4:2:0), 2 (4:2:2), 3 (4:4:4).</param>
|
||||
/// <param name="BitDepthLuma">Bits per luma sample.</param>
|
||||
/// <param name="BitDepthChroma">Bits per chroma sample.</param>
|
||||
/// <param name="Y">Luma, <paramref name="Width"/> × <paramref name="Height"/>.</param>
|
||||
/// <param name="Cb">Blue-difference chroma (empty for 4:0:0).</param>
|
||||
/// <param name="Cr">Red-difference chroma, same size as <paramref name="Cb"/>.</param>
|
||||
/// <param name="Poc">Picture order count.</param>
|
||||
/// <param name="DecodeIndex">Position of the coded picture in decoding order.</param>
|
||||
/// <param name="MatrixCoefficients">matrix_coeffs of the VUI (1 = BT.709, 5 and 6 = BT.601, 2 = unspecified).</param>
|
||||
/// <param name="FullRange">video_full_range_flag of the VUI.</param>
|
||||
public sealed record H265Frame(
|
||||
int Width,
|
||||
int Height,
|
||||
int ChromaFormat,
|
||||
int BitDepthLuma,
|
||||
int BitDepthChroma,
|
||||
ushort[] Y,
|
||||
ushort[] Cb,
|
||||
ushort[] Cr,
|
||||
int Poc,
|
||||
long DecodeIndex,
|
||||
int MatrixCoefficients,
|
||||
bool FullRange)
|
||||
{
|
||||
/// <summary>
|
||||
/// The frame as reference decoders write it: Y, Cb and Cr one after the other, one byte per sample up to 8 bits,
|
||||
/// otherwise two bytes (the low one first) for every plane.
|
||||
/// </summary>
|
||||
public byte[] ToYuvFile()
|
||||
{
|
||||
var wide = Math.Max(BitDepthLuma, BitDepthChroma) > 8;
|
||||
var count = Y.Length + Cb.Length + Cr.Length;
|
||||
var buffer = new byte[wide ? count * 2 : count];
|
||||
var position = 0;
|
||||
foreach (var plane in (ReadOnlySpan<ushort[]>)[Y, Cb, Cr])
|
||||
{
|
||||
if (wide)
|
||||
{
|
||||
foreach (var sample in plane)
|
||||
{
|
||||
buffer[position++] = (byte)sample;
|
||||
buffer[position++] = (byte)(sample >> 8);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var sample in plane)
|
||||
{
|
||||
buffer[position++] = (byte)sample;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user