HEVC e HEIF: limiti di memoria contro i file danneggiati e correzioni della revisione (#58)

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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 18:23:05 +01:00
1 parent ac85d1baed
commit 7b10f4ad4c
16 files changed
+1389 -131

No files matched your search

+41 -5
View File
@@ -38,9 +38,16 @@ internal readonly struct YuvPlanes<T>(
/// <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, BT.601, FCC, SMPTE 240M or BT.2020 (non-constant luminance) coefficients, the identity matrix (G, B and R
/// coded as Y, Cb and Cr), 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.
/// 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
{
@@ -53,6 +60,11 @@ internal static class YuvToRgb
{
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; }
@@ -74,8 +86,8 @@ internal static class YuvToRgb
}
/// <summary>
/// The coefficients for <paramref name="matrix"/> (matrix_coeffs; unspecified or unknown: BT.709 from 720 lines up,
/// BT.601 below), <paramref name="fullRange"/> and the bit depths of the samples.
/// 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)
{
@@ -84,6 +96,19 @@ internal static class YuvToRgb
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),
@@ -161,6 +186,17 @@ internal static class YuvToRgb
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);