HEVC nella catena dei media e foto HEIC/HEIF decodificate in casa (#58)
Le miniature e i fotogrammi dei video HEVC (i video dei telefoni) e le foto HEIC non avevano un decodificatore: ora passano dal decodificatore HEVC scritto in casa, senza componenti esterni. - Contenitori: hvcC dai sample entry hvc1/hev1 dei file ISO e dalla CodecPrivate di Matroska V_MPEGH/ISO/HEVC (VideoTrack.HevcConfiguration); HEVC in AVI come Annex B. Lo scrittore MP4 sa scrivere anche hvc1. - H265Decoder: fotogrammi in ordine di decodifica (FrameDecoded con Wanted), unità di accesso Annex B, punto d'accesso di un campione (IDR/BLA esatti, CRA vicino). - VideoFrameDecoder sceglie H.264 o HEVC dal codec della traccia; il fotogramma da cui parte la corsa vale per un campione senza immagine (RASL di una CRA). Una sola conversione Y'CbCr->RGB (Codecs/YuvToRgb) per H.264, HEVC 8-12 bit 4:0:0/4:2:0/4:2:2/4:4:4 e HEIF. - Febe.Core.Codecs.Heif (ISO/IEC 23008-12): meta, iinf, iloc (file e idat), iref, iprp/ipco/ipma; elemento hvc1 o griglia di tessere decodificate in parallelo, colr nclx/ICC, piano alfa, clap, irot/imir come orientamento EXIF, elemento Exif. Uscita HeifImage, con la stessa forma di RgbaImage del ramo funzione/codec-immagini (da unire). - ImageDecoding manda gli HEIF al decodificatore di casa: miniature, impronte, dimensioni e anteprime valgono anche per gli HEIC; ExifDateReader legge la data dall'elemento Exif nei primi 512 KB. - Prove su file costruiti dai flussi H.265.1 (MP4, frammentato, Matroska, AVI con e senza indice, Main 10, griglie, rotazioni, clap, alfa, nclx, Exif, file rovinati con semi fissi). Documenti: docker.md, LEGGIMI dell'importazione, una riga in decisioni.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3cabd87316
commit
ac85d1baed
21 files changed
+2695
-148
No files matched your search
@@ -0,0 +1,193 @@
|
||||
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, 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.
|
||||
/// </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; }
|
||||
|
||||
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 or unknown: 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) };
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user