Files
Febe/src/Febe.Core/Codecs/H265/Picture.cs
T
Alby96andClaude Opus 5.5 3cabd87316 Decodificatore HEVC, terza parte: anche le estensioni di formato, tutti i 146 flussi di conformità coincidono (#58)
I 10 flussi RExt sono nella serie di conformità, quindi il decodificatore li
decodifica invece di lasciarli inconcludenti: 4:0:0, 4:2:2 (due blocchi di
croma per unità di trasformata, modi intra della tabella 8-3, QP del croma
senza la tabella del 4:2:0), 4:4:4, fino a 12 bit, predizione fra componenti,
RDPCM implicito ed esplicito, rotazione e contesto unico del transform skip,
smoothing intra spento, adattamento persistente di Rice salvato con i contesti
di WPP e dei segmenti dipendenti, chroma_qp_offset_list; deblocking del croma
sulla sua griglia e SAO con CTB di croma non quadrati. Precisione estesa e
allineamento del CABAC restano rifiutati: nessun flusso li usa.

Le prove RExt non sono più inconcludenti, così la CI non trova prove HEVC
saltate. Una prova nuova controlla che tile, WPP, segmenti dipendenti, liste
di scala, lungo termine, PCM e strumenti RExt siano accesi in almeno un flusso,
perché gli hash valgano qualcosa per ciascuno. Il ritaglio dei fotogrammi in
uscita usa array non inizializzati. Misurati 160-600 fotogrammi al secondo a
416x240 e 26-27 a 1080p su questa macchina; nei cicli delle CTU non si alloca.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 17:06:04 +01:00

190 lines
7.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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],
];
Motion = new MvField[((Width + 3) >> 2) * ((Height + 3) >> 2)];
}
/// <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;
/// <summary>The motion of every 4×4 luma block (§8.5.3), kept for the pictures that take this one as collocated (TMVP).</summary>
internal MvField[] Motion { get; }
/// <summary>Index in <see cref="Slices"/> of the slice of each CTB (raster order), −1 where none was decoded.</summary>
internal int[] CtbSlice = [];
/// <summary>The independent slice segment headers of the picture, with their reference picture lists.</summary>
internal List<SliceHeader> Slices { get; } = [];
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 = GC.AllocateUninitializedArray<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>
/// The motion of a prediction block: a motion vector in quarter luma samples and a reference index for each list, the
/// index −1 when the list is not used (both −1 for an intra block). An unused list always has a zero vector, so that
/// equal motion means equal fields.
/// </summary>
internal struct MvField
{
internal static readonly MvField Intra = new() { Ref0 = -1, Ref1 = -1 };
internal short X0;
internal short Y0;
internal short X1;
internal short Y1;
internal sbyte Ref0;
internal sbyte Ref1;
internal readonly bool SameAs(in MvField other) =>
X0 == other.X0 && Y0 == other.Y0 && X1 == other.X1 && Y1 == other.Y1 && Ref0 == other.Ref0 && Ref1 == other.Ref1;
}
/// <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;
}
}