namespace Febe.Core.Codecs.H265;
///
/// 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).
///
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)];
}
/// The SPS of the picture: a picture of the pool is reused for another SPS of the same size.
internal Sps Sps { get; set; }
internal int Width { get; }
internal int Height { get; }
internal int ChromaWidth { get; }
internal int ChromaHeight { get; }
/// Y, Cb and Cr; the chroma arrays are empty for 4:0:0.
internal ushort[][] Planes { get; }
internal int Poc;
internal bool ShortTerm;
internal bool LongTerm;
/// Marked "needed for output" (C.5.2).
internal bool NeededForOutput;
internal bool OutputFlag;
internal int LatencyCount;
/// Position of the coded picture in decoding order, counting the pictures that are skipped too.
internal long DecodeIndex;
/// A picture generated for a missing reference (§8.3.3): never output, its samples are not meaningful.
internal bool Generated;
/// Every slice of the picture is an I slice: it does not depend on other pictures.
internal bool IntraOnly;
/// The motion of every 4×4 luma block (§8.5.3), kept for the pictures that take this one as collocated (TMVP).
internal MvField[] Motion { get; }
/// Index in of the slice of each CTB (raster order), −1 where none was decoded.
internal int[] CtbSlice = [];
/// The independent slice segment headers of the picture, with their reference picture lists.
internal List 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);
/// The picture cropped to the conformance window.
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(width * height);
for (var y = 0; y < height; y++)
{
plane.AsSpan(((y0 + y) * stride) + x0, width).CopyTo(result.AsSpan(y * width, width));
}
return result;
}
}
///
/// 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.
///
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;
}
/// A decoded HEVC frame, cropped to the conformance window, with one 16-bit value per sample.
/// Luma width after cropping.
/// Luma height after cropping.
/// ChromaArrayType: 0 (only luma), 1 (4:2:0), 2 (4:2:2), 3 (4:4:4).
/// Bits per luma sample.
/// Bits per chroma sample.
/// Luma, × .
/// Blue-difference chroma (empty for 4:0:0).
/// Red-difference chroma, same size as .
/// Picture order count.
/// Position of the coded picture in decoding order.
/// matrix_coeffs of the VUI (1 = BT.709, 5 and 6 = BT.601, 2 = unspecified).
/// video_full_range_flag of the VUI.
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)
{
///
/// 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.
///
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)[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;
}
}