Un flusso HEVC di pochi byte poteva far allocare gigabyte: ora un SPS il cui buffer supera il MaxDpbSize del livello 6.2 è rifiutato, il buffer non cresce oltre quanto l'SPS dichiara e una sola immagine grigia, riusata, sta per ogni riferimento mancante (il POC viaggia nelle liste). Nei file HEIF ogni immagine deve avere la dimensione del suo ispe, controllata dal decodificatore prima di allocare; i riquadri di una griglia devono coprire l'uscita senza traboccare, i loro byte non superano il file né MaxTotalBytes, e ciò che va storto nei riquadri decodificati in parallelo esce come HeifException. Inoltre: SPS e PPS ripetuti identici dentro un'immagine sono accettati (7.4.2.4.4) e uno cambiato vale dall'immagine dopo; le estensioni SCC sono rifiutate subito; l'intervallo di luma_offset segue l'alta precisione; YCgCo convertito, le altre matrici approssimate e documentate. «Oggetto» e «riquadro» al posto di «elemento» e «tessera» per HEIF (glossario). Righe nuove in decisioni.md: limiti, righe superate, consegne al binario dei documenti, piano per NSIS, Node e tini. Prove nuove su flussi HEVC scritti bit per bit con un codificatore CABAC di prova, su file HEIF costruiti apposta e sulla conversione YCgCo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
230 lines
9.5 KiB
C#
230 lines
9.5 KiB
C#
using System.Numerics;
|
||
|
||
namespace Febe.Core.Codecs;
|
||
|
||
/// <summary>
|
||
/// The sample planes of a decoded picture, whatever its codec: luma <see cref="Width"/> × <see cref="Height"/>, chroma
|
||
/// subsampled by <c>1 << ChromaShiftX</c> and <c>1 << ChromaShiftY</c> (4:2:0: 1 and 1; 4:2:2: 1 and 0; 4:4:4:
|
||
/// 0 and 0), no chroma at all for a monochrome picture. Samples are 8-bit bytes (H.264) or 16-bit values of any bit
|
||
/// depth up to 16 (HEVC).
|
||
/// </summary>
|
||
internal readonly struct YuvPlanes<T>(
|
||
T[] y, T[] cb, T[] cr, int width, int height, int chromaShiftX, int chromaShiftY, bool monochrome, int bitDepthY, int bitDepthC)
|
||
where T : unmanaged, IBinaryInteger<T>
|
||
{
|
||
internal T[] Y { get; } = y;
|
||
|
||
internal T[] Cb { get; } = cb;
|
||
|
||
internal T[] Cr { get; } = cr;
|
||
|
||
internal int Width { get; } = width;
|
||
|
||
internal int Height { get; } = height;
|
||
|
||
internal int ChromaShiftX { get; } = chromaShiftX;
|
||
|
||
internal int ChromaShiftY { get; } = chromaShiftY;
|
||
|
||
internal bool Monochrome { get; } = monochrome;
|
||
|
||
internal int BitDepthY { get; } = bitDepthY;
|
||
|
||
internal int BitDepthC { get; } = bitDepthC;
|
||
|
||
/// <summary>Width of a chroma row (rounded up, as for odd H.264 widths).</summary>
|
||
internal int ChromaWidth => (Width + (1 << ChromaShiftX) - 1) >> ChromaShiftX;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Conversion of Y′CbCr samples to 8-bit RGB (ITU-T H.273 for the meaning of matrix_coeffs and video_full_range_flag):
|
||
/// BT.709 (1), FCC (4), BT.601 (5, 6), SMPTE 240M (7) or BT.2020 non-constant luminance (9) coefficients, the identity
|
||
/// matrix (0: G, B and R coded as Y, Cb and Cr), YCgCo (8: Y, Cg and Co in place of Y, Cb and Cr, the inverse of the
|
||
/// equations of H.273 for MatrixCoefficients 8), limited or full range, any bit depth; chroma taken at the nearest
|
||
/// sample. Integer arithmetic with 16 fractional bits, so every caller gets the same pixels.
|
||
/// <para>
|
||
/// Approximated: BT.2020 constant luminance (10) is converted with the non-constant luminance coefficients of 9, without
|
||
/// its transfer to linear light; the chromaticity-derived matrices (12, 13), which need the colour primaries,
|
||
/// SMPTE ST 2085 (11) and ICtCp (14), as every unspecified or unknown value, take BT.709 from 720 lines up and BT.601
|
||
/// below. None of them comes from the phones and services Febe meets.
|
||
/// </para>
|
||
/// </summary>
|
||
internal static class YuvToRgb
|
||
{
|
||
private const int Shift = 16;
|
||
private const double One = 1 << Shift;
|
||
private const int Half = 1 << (Shift - 1);
|
||
|
||
/// <summary>The integer coefficients for one matrix, range and bit depth.</summary>
|
||
internal readonly struct Coefficients
|
||
{
|
||
internal bool Identity { get; init; }
|
||
|
||
/// <summary>YCgCo (matrix 8): G = Y + Cg, R = Y − Cg + Co, B = Y − Cg − Co, Cg and Co scaled by <see cref="ChromaScale"/>.</summary>
|
||
internal bool YCgCo { get; init; }
|
||
|
||
internal int ChromaScale { get; init; }
|
||
|
||
internal int LumaScale { get; init; }
|
||
|
||
internal int LumaOffset { get; init; }
|
||
|
||
internal int ChromaMiddle { get; init; }
|
||
|
||
internal int CrToR { get; init; }
|
||
|
||
internal int CbToB { get; init; }
|
||
|
||
internal int CbToG { get; init; }
|
||
|
||
internal int CrToG { get; init; }
|
||
|
||
/// <summary>Right shift that brings an identity-matrix sample to 8 bits, luma and chroma.</summary>
|
||
internal int IdentityShiftY { get; init; }
|
||
|
||
internal int IdentityShiftC { get; init; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// The coefficients for <paramref name="matrix"/> (matrix_coeffs; unspecified, unknown or approximated as the class
|
||
/// says: BT.709 from 720 lines up, BT.601 below), <paramref name="fullRange"/> and the bit depths of the samples.
|
||
/// </summary>
|
||
internal static Coefficients For(int matrix, bool fullRange, int bitDepthY, int bitDepthC, int height)
|
||
{
|
||
if (matrix == 0)
|
||
{
|
||
return new Coefficients { Identity = true, IdentityShiftY = Math.Max(0, bitDepthY - 8), IdentityShiftC = Math.Max(0, bitDepthC - 8) };
|
||
}
|
||
|
||
if (matrix == 8)
|
||
{
|
||
// Cg and Co are quantised as Cb and Cr are, with the same ranges (H.273, MatrixCoefficients 8).
|
||
return new Coefficients
|
||
{
|
||
YCgCo = true,
|
||
LumaScale = (int)Math.Round((fullRange ? 1.0 : 255.0 / 219.0) / (1 << Math.Max(0, bitDepthY - 8)) * One),
|
||
LumaOffset = fullRange ? 0 : 16 << Math.Max(0, bitDepthY - 8),
|
||
ChromaMiddle = 1 << (bitDepthC - 1),
|
||
ChromaScale = (int)Math.Round((fullRange ? 1.0 : 255.0 / 224.0) / (1 << Math.Max(0, bitDepthC - 8)) * One),
|
||
};
|
||
}
|
||
|
||
var (kr, kb) = matrix switch
|
||
{
|
||
1 => (0.2126, 0.0722),
|
||
4 => (0.30, 0.11),
|
||
5 or 6 => (0.299, 0.114),
|
||
7 => (0.212, 0.087),
|
||
9 or 10 => (0.2627, 0.0593),
|
||
_ => height >= 720 ? (0.2126, 0.0722) : (0.299, 0.114),
|
||
};
|
||
var kg = 1 - kr - kb;
|
||
var yScale = (fullRange ? 1.0 : 255.0 / 219.0) / (1 << Math.Max(0, bitDepthY - 8));
|
||
var cScale = (fullRange ? 1.0 : 255.0 / 224.0) / (1 << Math.Max(0, bitDepthC - 8));
|
||
return new Coefficients
|
||
{
|
||
LumaScale = (int)Math.Round(yScale * One),
|
||
LumaOffset = fullRange ? 0 : 16 << Math.Max(0, bitDepthY - 8),
|
||
ChromaMiddle = 1 << (bitDepthC - 1),
|
||
CrToR = (int)Math.Round(2 * (1 - kr) * cScale * One),
|
||
CbToB = (int)Math.Round(2 * (1 - kb) * cScale * One),
|
||
CbToG = (int)Math.Round(2 * kb * (1 - kb) / kg * cScale * One),
|
||
CrToG = (int)Math.Round(2 * kr * (1 - kr) / kg * cScale * One),
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Converts the picture reduced by <paramref name="factor"/> (each output pixel the mean of factor × factor converted
|
||
/// pixels) into <paramref name="target"/>: <paramref name="width"/> × <paramref name="height"/> pixels of
|
||
/// <paramref name="pixelSize"/> bytes (3: R, G, B; 4: R, G, B and an alpha of 255), rows <paramref name="stride"/>
|
||
/// bytes apart. The output must fit in the picture: width × factor ≤ its width, height × factor ≤ its height.
|
||
/// </summary>
|
||
internal static void Convert<T>(in YuvPlanes<T> p, in Coefficients k, int factor, Span<byte> target, int stride, int pixelSize, int width, int height)
|
||
where T : unmanaged, IBinaryInteger<T>
|
||
{
|
||
var area = factor * factor;
|
||
var cw = p.ChromaWidth;
|
||
var sx = p.ChromaShiftX;
|
||
var sy = p.ChromaShiftY;
|
||
ReadOnlySpan<T> yPlane = p.Y, cbPlane = p.Cb, crPlane = p.Cr;
|
||
var mono = p.Monochrome;
|
||
for (var oy = 0; oy < height; oy++)
|
||
{
|
||
var row = target.Slice(oy * stride, width * pixelSize);
|
||
for (var ox = 0; ox < width; ox++)
|
||
{
|
||
int r = 0, g = 0, b = 0;
|
||
for (var dy = 0; dy < factor; dy++)
|
||
{
|
||
var y = (oy * factor) + dy;
|
||
var lumaRow = y * p.Width;
|
||
var chromaRow = (y >> sy) * cw;
|
||
for (var dx = 0; dx < factor; dx++)
|
||
{
|
||
var x = (ox * factor) + dx;
|
||
var luma = int.CreateTruncating(yPlane[lumaRow + x]);
|
||
int u, v;
|
||
if (mono)
|
||
{
|
||
u = v = k.ChromaMiddle;
|
||
}
|
||
else
|
||
{
|
||
var c = chromaRow + (x >> sx);
|
||
u = int.CreateTruncating(cbPlane[c]);
|
||
v = int.CreateTruncating(crPlane[c]);
|
||
}
|
||
|
||
if (k.Identity)
|
||
{
|
||
g += Clip(luma >> k.IdentityShiftY);
|
||
b += Clip(mono ? luma >> k.IdentityShiftY : u >> k.IdentityShiftC);
|
||
r += Clip(mono ? luma >> k.IdentityShiftY : v >> k.IdentityShiftC);
|
||
continue;
|
||
}
|
||
|
||
var l = (luma - k.LumaOffset) * k.LumaScale;
|
||
u -= k.ChromaMiddle;
|
||
v -= k.ChromaMiddle;
|
||
if (k.YCgCo)
|
||
{
|
||
var cg = u * k.ChromaScale;
|
||
var co = v * k.ChromaScale;
|
||
var t = l - cg;
|
||
r += Clip((t + co + Half) >> Shift);
|
||
g += Clip((l + cg + Half) >> Shift);
|
||
b += Clip((t - co + Half) >> Shift);
|
||
continue;
|
||
}
|
||
|
||
r += Clip((l + (k.CrToR * v) + Half) >> Shift);
|
||
g += Clip((l - (k.CbToG * u) - (k.CrToG * v) + Half) >> Shift);
|
||
b += Clip((l + (k.CbToB * u) + Half) >> Shift);
|
||
}
|
||
}
|
||
|
||
var o = ox * pixelSize;
|
||
if (area == 1)
|
||
{
|
||
row[o] = (byte)r;
|
||
row[o + 1] = (byte)g;
|
||
row[o + 2] = (byte)b;
|
||
}
|
||
else
|
||
{
|
||
row[o] = (byte)(r / area);
|
||
row[o + 1] = (byte)(g / area);
|
||
row[o + 2] = (byte)(b / area);
|
||
}
|
||
|
||
if (pixelSize == 4)
|
||
{
|
||
row[o + 3] = 255;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private static int Clip(int v) => v < 0 ? 0 : v > 255 ? 255 : v;
|
||
}
|