Decodificatore H.264 per i formati dei profili High: 4:2:2, 4:4:4, piani separati, 4:0:0, da 8 a 14 bit, senza perdita (#58)
Il decodificatore rifiutava tutto ciò che non era 4:2:0 a 8 bit, così i video 4:2:2, monocromatici o a 10 bit restavano senza miniatura. Ora i campioni stanno in piani di uno o due byte e i nuclei di predizione, trasformata e filtro sono generici su byte e ushort, specializzati dal JIT: a 8 bit i tempi restano quelli di prima (misurati sui flussi di conformità, 100,4%). - 4:2:2: DC della crominanza 2x4 con qP + 3, tabelle CAVLC di nC -2, contesti CABAC per NumC8x8, predizione intra 8x16, vettore verticale a piena risoluzione, bordi della crominanza alle righe 4, 8 e 12. - 4:4:4: Cb e Cr codificati come la luminanza in CAVLC, con le loro matrici e il filtro della luminanza; separate_colour_plane_flag come tre immagini monocromatiche con i propri dati di macroblocco e di moto. - 4:0:0 senza sintassi della crominanza; in uscita la crominanza 4:2:0 al valore medio, come il file ufficiale. - Profondità da 8 a 14 bit: QP da -QpBdOffset, soglie del deblocking e scarti dei pesi scalati; uscita a due byte little-endian. - Senza perdita: qpprime_y_zero_transform_bypass_flag a QP'Y 0, con la DPCM della predizione intra orizzontale e verticale. - VideoFrameDecoder converte in RGB alla profondità e al campionamento del fotogramma. I dieci flussi 4:2:2 (8 e 10 bit) e 4:0:0 della serie ITU-T coincidono con la ricostruzione ufficiale e passano fra i verificati. 4:4:4, piani separati, senza perdita e 11-14 bit si provano con flussi sintetici (H264FormatTests). Il CABAC in 4:4:4 con i piani insieme si rifiuta: mancano i contesti 460-1023. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d03ca23de
commit
da84c79f7e
23 files changed
+2016
-607
No files matched your search
@@ -469,25 +469,35 @@ public sealed class VideoFrameDecoder
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A coarse grid of mean luma and chroma values, to compare frames without their compression noise.</summary>
|
||||
/// <summary>
|
||||
/// A coarse grid of mean luma and chroma values, to compare frames without their compression noise; values on the
|
||||
/// 8-bit scale whatever the bit depth, the chroma at any sampling (its own grid over its own plane).
|
||||
/// </summary>
|
||||
private static double[] Grid(H264Frame frame)
|
||||
{
|
||||
var grid = new double[GridSize * GridSize * 2];
|
||||
var counts = new int[GridSize * GridSize];
|
||||
var wide = frame.BytesPerSample == 2;
|
||||
ReadOnlySpan<ushort> y16 = wide ? System.Runtime.InteropServices.MemoryMarshal.Cast<byte, ushort>(frame.Y) : default;
|
||||
var lumaShift = frame.BitDepthLuma - 8;
|
||||
for (var y = 0; y < frame.Height; y++)
|
||||
{
|
||||
var gy = y * GridSize / frame.Height;
|
||||
var row = frame.Y.AsSpan(y * frame.Width, frame.Width);
|
||||
for (var x = 0; x < row.Length; x++)
|
||||
var start = y * frame.Width;
|
||||
for (var x = 0; x < frame.Width; x++)
|
||||
{
|
||||
var cell = (gy * GridSize) + (x * GridSize / frame.Width);
|
||||
grid[cell] += row[x];
|
||||
grid[cell] += wide ? y16[start + x] >> lumaShift : frame.Y[start + x];
|
||||
counts[cell]++;
|
||||
}
|
||||
}
|
||||
|
||||
var cw = (frame.Width + 1) / 2;
|
||||
var ch = (frame.Height + 1) / 2;
|
||||
var cw = frame.ChromaWidth;
|
||||
var ch = frame.ChromaHeight;
|
||||
ReadOnlySpan<ushort> cb16 = wide ? System.Runtime.InteropServices.MemoryMarshal.Cast<byte, ushort>(frame.Cb) : default;
|
||||
ReadOnlySpan<ushort> cr16 = wide ? System.Runtime.InteropServices.MemoryMarshal.Cast<byte, ushort>(frame.Cr) : default;
|
||||
var chromaShift = frame.BitDepthChroma - 8;
|
||||
var middle = 1 << (frame.BitDepthChroma - 1);
|
||||
var chromaCounts = new int[GridSize * GridSize];
|
||||
for (var y = 0; y < ch; y++)
|
||||
{
|
||||
@@ -496,7 +506,19 @@ public sealed class VideoFrameDecoder
|
||||
{
|
||||
var cell = (gy * GridSize) + (x * GridSize / cw);
|
||||
var i = (y * cw) + x;
|
||||
grid[(GridSize * GridSize) + cell] += (Math.Abs(frame.Cb[i] - 128) + Math.Abs(frame.Cr[i] - 128)) / 2.0;
|
||||
int u, v;
|
||||
if (wide)
|
||||
{
|
||||
u = Math.Abs(cb16[i] - middle) >> chromaShift;
|
||||
v = Math.Abs(cr16[i] - middle) >> chromaShift;
|
||||
}
|
||||
else
|
||||
{
|
||||
u = Math.Abs(frame.Cb[i] - 128);
|
||||
v = Math.Abs(frame.Cr[i] - 128);
|
||||
}
|
||||
|
||||
grid[(GridSize * GridSize) + cell] += (u + v) / 2.0;
|
||||
chromaCounts[cell]++;
|
||||
}
|
||||
}
|
||||
@@ -512,8 +534,10 @@ public sealed class VideoFrameDecoder
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The frame in RGB: BT.709 or BT.601 coefficients as the stream says (unspecified: BT.709 from 720 lines up), limited
|
||||
/// or full range, reduced by an integer factor to fit <paramref name="maxLongSide"/>, then rotated clockwise.
|
||||
/// The frame in RGB: BT.709, BT.601 or BT.2020 coefficients as the stream says (unspecified: BT.709 from 720 lines
|
||||
/// up), limited or full range at the bit depth of the frame (limited: black at 16 · 2^(n − 8), luma span 219 · 2^(n − 8),
|
||||
/// chroma 224 · 2^(n − 8) around 2^(n − 1)), any chroma sampling (4:2:0, 4:2:2, 4:4:4; monochrome comes with grey
|
||||
/// chroma), reduced by an integer factor to fit <paramref name="maxLongSide"/>, then rotated clockwise.
|
||||
/// </summary>
|
||||
internal static VideoImage ToImage(H264Frame frame, int rotation, int maxLongSide)
|
||||
{
|
||||
@@ -527,9 +551,11 @@ public sealed class VideoFrameDecoder
|
||||
_ => frame.Height >= 720 ? (0.2126, 0.0722) : (0.299, 0.114),
|
||||
};
|
||||
var kg = 1 - kr - kb;
|
||||
var yScale = frame.FullRange ? 1.0 : 255.0 / 219.0;
|
||||
var cScale = frame.FullRange ? 1.0 : 255.0 / 224.0;
|
||||
var yOffset = frame.FullRange ? 0 : 16;
|
||||
int lumaBits = frame.BitDepthLuma - 8, chromaBits = frame.BitDepthChroma - 8;
|
||||
var yScale = frame.FullRange ? 255.0 / ((1 << frame.BitDepthLuma) - 1) : 255.0 / (219 << lumaBits);
|
||||
var cScale = frame.FullRange ? 255.0 / ((1 << frame.BitDepthChroma) - 1) : 255.0 / (224 << chromaBits);
|
||||
var yOffset = frame.FullRange ? 0 : 16 << lumaBits;
|
||||
var middle = 1 << (frame.BitDepthChroma - 1);
|
||||
const int Shift = 16;
|
||||
const double One = 1 << Shift;
|
||||
var cy = (int)Math.Round(yScale * One);
|
||||
@@ -549,7 +575,13 @@ public sealed class VideoFrameDecoder
|
||||
var width = Math.Max(1, frame.Width / factor);
|
||||
var height = Math.Max(1, frame.Height / factor);
|
||||
var rgb = new byte[width * height * 3];
|
||||
var cw = (frame.Width + 1) / 2;
|
||||
var cw = frame.ChromaWidth;
|
||||
var shiftX = frame.ChromaFormat == 3 ? 0 : 1;
|
||||
var shiftY = frame.ChromaFormat is 2 or 3 ? 0 : 1;
|
||||
var wide = frame.BytesPerSample == 2;
|
||||
ReadOnlySpan<ushort> y16 = wide ? System.Runtime.InteropServices.MemoryMarshal.Cast<byte, ushort>(frame.Y) : default;
|
||||
ReadOnlySpan<ushort> cb16 = wide ? System.Runtime.InteropServices.MemoryMarshal.Cast<byte, ushort>(frame.Cb) : default;
|
||||
ReadOnlySpan<ushort> cr16 = wide ? System.Runtime.InteropServices.MemoryMarshal.Cast<byte, ushort>(frame.Cr) : default;
|
||||
var area = factor * factor;
|
||||
for (var oy = 0; oy < height; oy++)
|
||||
{
|
||||
@@ -560,13 +592,25 @@ public sealed class VideoFrameDecoder
|
||||
{
|
||||
var y = (oy * factor) + dy;
|
||||
var lumaRow = y * frame.Width;
|
||||
var chromaRow = (y >> 1) * cw;
|
||||
var chromaRow = (y >> shiftY) * cw;
|
||||
for (var dx = 0; dx < factor; dx++)
|
||||
{
|
||||
var x = (ox * factor) + dx;
|
||||
var l = (frame.Y[lumaRow + x] - yOffset) * cy;
|
||||
var u = frame.Cb[chromaRow + (x >> 1)] - 128;
|
||||
var v = frame.Cr[chromaRow + (x >> 1)] - 128;
|
||||
var ci = chromaRow + (x >> shiftX);
|
||||
int l, u, v;
|
||||
if (wide)
|
||||
{
|
||||
l = (y16[lumaRow + x] - yOffset) * cy;
|
||||
u = cb16[ci] - middle;
|
||||
v = cr16[ci] - middle;
|
||||
}
|
||||
else
|
||||
{
|
||||
l = (frame.Y[lumaRow + x] - yOffset) * cy;
|
||||
u = frame.Cb[ci] - middle;
|
||||
v = frame.Cr[ci] - middle;
|
||||
}
|
||||
|
||||
r += Clip((l + (crR * v) + Half) >> Shift);
|
||||
g += Clip((l - (cbG * u) - (crG * v) + Half) >> Shift);
|
||||
b += Clip((l + (cbB * u) + Half) >> Shift);
|
||||
|
||||
Reference in new issue
Block a user