Decodificatore HEVC, terza parte: anche le estensioni di formato, tutti i 146 flussi di conformità coincidono (#58)
I 10 flussi RExt sono nella serie di conformità, quindi il decodificatore li decodifica invece di lasciarli inconcludenti: 4:0:0, 4:2:2 (due blocchi di croma per unità di trasformata, modi intra della tabella 8-3, QP del croma senza la tabella del 4:2:0), 4:4:4, fino a 12 bit, predizione fra componenti, RDPCM implicito ed esplicito, rotazione e contesto unico del transform skip, smoothing intra spento, adattamento persistente di Rice salvato con i contesti di WPP e dei segmenti dipendenti, chroma_qp_offset_list; deblocking del croma sulla sua griglia e SAO con CTB di croma non quadrati. Precisione estesa e allineamento del CABAC restano rifiutati: nessun flusso li usa. Le prove RExt non sono più inconcludenti, così la CI non trova prove HEVC saltate. Una prova nuova controlla che tile, WPP, segmenti dipendenti, liste di scala, lungo termine, PCM e strumenti RExt siano accesi in almeno un flusso, perché gli hash valgano qualcosa per ciascuno. Il ritaglio dei fotogrammi in uscita usa array non inizializzati. Misurati 160-600 fotogrammi al secondo a 416x240 e 26-27 a 1080p su questa macchina; nei cicli delle CTU non si alloca. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d5e1282261
commit
3cabd87316
14 files changed
+535
-188
No files matched your search
@@ -191,7 +191,7 @@ public sealed partial class H265Decoder
|
||||
|
||||
private static bool Far(int ax, int ay, int bx, int by) => Math.Abs(ax - bx) >= 4 || Math.Abs(ay - by) >= 4;
|
||||
|
||||
/// <summary>Filters every vertical (or horizontal) edge of the picture: luma on the 8×8 grid, chroma on the 16×16 one.</summary>
|
||||
/// <summary>Filters every vertical (or horizontal) edge of the picture: luma on the 8×8 grid, chroma on the 8×8 grid of chroma samples.</summary>
|
||||
private void Deblock(Picture picture, bool vertical)
|
||||
{
|
||||
var edges = vertical ? edgeV : edgeH;
|
||||
@@ -201,6 +201,14 @@ public sealed partial class H265Decoder
|
||||
var scaleY = 1 << (sps.BitDepthY - 8);
|
||||
var log2Ctb = sps.Log2CtbSize;
|
||||
var checkFlags = pictureHasUnfilteredBlocks;
|
||||
|
||||
// Chroma edges (§8.7.2.5.5) lie on the 8×8 grid of chroma samples and are filtered in segments of 4 chroma
|
||||
// samples, each taking bS and QP from the luma segment at its start: a vertical edge every 8 << sx luma columns,
|
||||
// a segment every 4 << sy luma rows (and the other way round for horizontal edges).
|
||||
var chroma = sps.ChromaArrayType != 0;
|
||||
int sx = sps.ChromaShiftX, sy = sps.ChromaShiftY;
|
||||
int edgeMask = vertical ? (8 << sx) - 1 : (8 << sy) - 1;
|
||||
int segmentMask = vertical ? (4 << sy) - 1 : (4 << sx) - 1;
|
||||
for (var gy = 0; gy < h4; gy++)
|
||||
{
|
||||
for (var gx = 0; gx < w4; gx++)
|
||||
@@ -236,11 +244,10 @@ public sealed partial class H265Decoder
|
||||
FilterLuma(luma, origin, vertical ? 1 : stride, vertical ? stride : 1, beta, tc, noP, noQ, maxY);
|
||||
}
|
||||
|
||||
// Chroma (§8.7.2.5.5): bS 2 only, edges on the chroma 8×8 grid; the segment of 4 chroma samples takes the
|
||||
// values of the first luma segment of its 8 luma samples.
|
||||
if (bS == 2 && (vertical ? (x & 15) == 0 && (y & 7) == 0 : (y & 15) == 0 && (x & 7) == 0))
|
||||
// Chroma: bS 2 only.
|
||||
if (bS == 2 && chroma && (vertical ? (x & edgeMask) == 0 && (y & segmentMask) == 0 : (y & edgeMask) == 0 && (x & segmentMask) == 0))
|
||||
{
|
||||
FilterChromaEdge(picture, x >> 1, y >> 1, vertical, qpL, header.TcOffsetDiv2, noP, noQ);
|
||||
FilterChromaEdge(picture, x >> sx, y >> sy, vertical, qpL, header.TcOffsetDiv2, noP, noQ);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -254,8 +261,7 @@ public sealed partial class H265Decoder
|
||||
var lines = Math.Min(4, vertical ? picture.ChromaHeight - yc : picture.ChromaWidth - xc);
|
||||
for (var c = 1; c <= 2; c++)
|
||||
{
|
||||
var qPi = qpL + (c == 1 ? pps.CbQpOffset : pps.CrQpOffset);
|
||||
var qpC = qPi < 30 ? qPi : qPi > 43 ? qPi - 6 : H265Tables.ChromaQp30To43[qPi - 30];
|
||||
var qpC = H265Tables.ChromaQp(qpL + (c == 1 ? pps.CbQpOffset : pps.CrQpOffset), sps.ChromaArrayType);
|
||||
var tc = H265Tables.Tc[Math.Clamp(qpC + 2 + (tcOffsetDiv2 * 2), 0, 53)] * scale;
|
||||
if (tc == 0)
|
||||
{
|
||||
@@ -372,13 +378,14 @@ public sealed partial class H265Decoder
|
||||
var log2Ctb = sps.Log2CtbSize;
|
||||
var heightInCtbs = sps.HeightInCtbs;
|
||||
Span<bool> neighbours = stackalloc bool[9];
|
||||
for (var c = 0; c < 3; c++)
|
||||
var components = sps.ChromaArrayType == 0 ? 1 : 3;
|
||||
for (var c = 0; c < components; c++)
|
||||
{
|
||||
var plane = picture.Planes[c];
|
||||
var width = c == 0 ? picture.Width : picture.ChromaWidth;
|
||||
var height = c == 0 ? picture.Height : picture.ChromaHeight;
|
||||
var shift = c == 0 ? 0 : 1;
|
||||
var ctbSize = (1 << log2Ctb) >> shift;
|
||||
int sx = c == 0 ? 0 : sps.ChromaShiftX, sy = c == 0 ? 0 : sps.ChromaShiftY;
|
||||
int ctbWidth = (1 << log2Ctb) >> sx, ctbHeight = (1 << log2Ctb) >> sy;
|
||||
var bitDepth = c == 0 ? sps.BitDepthY : sps.BitDepthC;
|
||||
var max = (1 << bitDepth) - 1;
|
||||
if (saoSource.Length < plane.Length)
|
||||
@@ -403,12 +410,12 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
|
||||
int cx = rs % widthInCtbs, cy = rs / widthInCtbs;
|
||||
int x0 = cx * ctbSize, y0 = cy * ctbSize;
|
||||
int x1 = Math.Min(x0 + ctbSize, width), y1 = Math.Min(y0 + ctbSize, height);
|
||||
int x0 = cx * ctbWidth, y0 = cy * ctbHeight;
|
||||
int x1 = Math.Min(x0 + ctbWidth, width), y1 = Math.Min(y0 + ctbHeight, height);
|
||||
var offsets = saoOffset.AsSpan(at * 4, 4);
|
||||
if (type == 1)
|
||||
{
|
||||
SaoBand(plane, width, x0, y0, x1, y1, saoClass[at], offsets, bitDepth, max, shift);
|
||||
SaoBand(plane, width, x0, y0, x1, y1, saoClass[at], offsets, bitDepth, max, sx, sy);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -420,7 +427,7 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
}
|
||||
|
||||
SaoEdge(plane, width, x0, y0, x1, y1, saoClass[at], offsets, max, shift, neighbours);
|
||||
SaoEdge(plane, width, x0, y0, x1, y1, saoClass[at], offsets, max, sx, sy, neighbours);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -459,7 +466,7 @@ public sealed partial class H265Decoder
|
||||
return pps.LoopFilterAcrossTilesEnabled || pps.TileIdRs[nb] == pps.TileIdRs[rs];
|
||||
}
|
||||
|
||||
private void SaoBand(ushort[] plane, int stride, int x0, int y0, int x1, int y1, int position, ReadOnlySpan<short> offsets, int bitDepth, int max, int shift)
|
||||
private void SaoBand(ushort[] plane, int stride, int x0, int y0, int x1, int y1, int position, ReadOnlySpan<short> offsets, int bitDepth, int max, int sx, int sy)
|
||||
{
|
||||
Span<int> table = stackalloc int[32];
|
||||
for (var k = 0; k < 4; k++)
|
||||
@@ -474,7 +481,7 @@ public sealed partial class H265Decoder
|
||||
var row = y * stride;
|
||||
for (var x = x0; x < x1; x++)
|
||||
{
|
||||
if (check && (blockFlags[Grid(x << shift, y << shift)] & FlagNoFilter) != 0)
|
||||
if (check && (blockFlags[Grid(x << sx, y << sy)] & FlagNoFilter) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -489,7 +496,7 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
}
|
||||
|
||||
private void SaoEdge(ushort[] plane, int stride, int x0, int y0, int x1, int y1, int eoClass, ReadOnlySpan<short> offsets, int max, int shift, ReadOnlySpan<bool> neighbours)
|
||||
private void SaoEdge(ushort[] plane, int stride, int x0, int y0, int x1, int y1, int eoClass, ReadOnlySpan<short> offsets, int max, int sx, int sy, ReadOnlySpan<bool> neighbours)
|
||||
{
|
||||
// hPos and vPos of table 8-14 (the second neighbour is the opposite of the first).
|
||||
int dx = eoClass switch { 0 => -1, 1 => 0, 2 => -1, _ => 1 };
|
||||
@@ -518,7 +525,7 @@ public sealed partial class H265Decoder
|
||||
continue;
|
||||
}
|
||||
|
||||
if (check && (blockFlags[Grid(x << shift, y << shift)] & FlagNoFilter) != 0)
|
||||
if (check && (blockFlags[Grid(x << sx, y << sy)] & FlagNoFilter) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -544,26 +544,34 @@ public sealed partial class H265Decoder
|
||||
{
|
||||
var weighted = slice.Type == SliceType.P ? pps.WeightedPred : pps.WeightedBipred;
|
||||
var picture = cur!;
|
||||
for (var c = 0; c < 3; c++)
|
||||
var components = sps.ChromaArrayType == 0 ? 1 : 3;
|
||||
for (var c = 0; c < components; c++)
|
||||
{
|
||||
int x0 = xPb, y0 = yPb, w = width, h = height;
|
||||
int mx0 = motion.X0, my0 = motion.Y0, mx1 = motion.X1, my1 = motion.Y1;
|
||||
if (c > 0)
|
||||
{
|
||||
x0 >>= 1;
|
||||
y0 >>= 1;
|
||||
w >>= 1;
|
||||
h >>= 1;
|
||||
// Chroma vectors in eighths of a chroma sample (8-228, 8-229): mvCLX = mvLX * 2 / SubWidthC (SubHeightC).
|
||||
int sx = sps.ChromaShiftX, sy = sps.ChromaShiftY;
|
||||
x0 >>= sx;
|
||||
y0 >>= sy;
|
||||
w >>= sx;
|
||||
h >>= sy;
|
||||
mx0 <<= 1 - sx;
|
||||
mx1 <<= 1 - sx;
|
||||
my0 <<= 1 - sy;
|
||||
my1 <<= 1 - sy;
|
||||
}
|
||||
|
||||
var bitDepth = c == 0 ? sps.BitDepthY : sps.BitDepthC;
|
||||
if (motion.Ref0 >= 0)
|
||||
{
|
||||
Interpolate(slice.RefPicList[0][motion.Ref0], c, x0, y0, w, h, motion.X0, motion.Y0, bitDepth, predL0);
|
||||
Interpolate(slice.RefPicList[0][motion.Ref0], c, x0, y0, w, h, mx0, my0, bitDepth, predL0);
|
||||
}
|
||||
|
||||
if (motion.Ref1 >= 0)
|
||||
{
|
||||
Interpolate(slice.RefPicList[1][motion.Ref1], c, x0, y0, w, h, motion.X1, motion.Y1, bitDepth, predL1);
|
||||
Interpolate(slice.RefPicList[1][motion.Ref1], c, x0, y0, w, h, mx1, my1, bitDepth, predL1);
|
||||
}
|
||||
|
||||
var plane = picture.Planes[c];
|
||||
|
||||
@@ -22,19 +22,20 @@ public sealed partial class H265Decoder
|
||||
var bitDepth = cIdx == 0 ? sps.BitDepthY : sps.BitDepthC;
|
||||
var n = 1 << log2;
|
||||
var n2 = 2 * n;
|
||||
var shift = cIdx == 0 ? 0 : 1; // 4:2:0
|
||||
var unit = 4 >> shift;
|
||||
int xL = x0 << shift, yL = y0 << shift;
|
||||
// Neighbours are looked up in luma locations; availability is known by 4×4 luma blocks.
|
||||
int sx = cIdx == 0 ? 0 : sps.ChromaShiftX, sy = cIdx == 0 ? 0 : sps.ChromaShiftY;
|
||||
int unitV = 4 >> sy, unitH = 4 >> sx;
|
||||
int xL = x0 << sx, yL = y0 << sy;
|
||||
var line = refLine.AsSpan(0, (4 * n) + 1);
|
||||
var available = refAvailable.AsSpan(0, (4 * n) + 1);
|
||||
var constrained = pps.ConstrainedIntraPred;
|
||||
var any = false;
|
||||
|
||||
// Left and below-left, top to bottom.
|
||||
for (var k = 0; k < n2; k += unit)
|
||||
for (var k = 0; k < n2; k += unitV)
|
||||
{
|
||||
var ok = IntraNeighbour(xL, yL, (x0 - 1) << shift, (y0 + k) << shift, constrained);
|
||||
for (var j = 0; j < unit; j++)
|
||||
var ok = IntraNeighbour(xL, yL, (x0 - 1) << sx, (y0 + k) << sy, constrained);
|
||||
for (var j = 0; j < unitV; j++)
|
||||
{
|
||||
var index = n2 - 1 - (k + j);
|
||||
available[index] = ok;
|
||||
@@ -47,7 +48,7 @@ public sealed partial class H265Decoder
|
||||
any |= ok;
|
||||
}
|
||||
|
||||
var corner = IntraNeighbour(xL, yL, (x0 - 1) << shift, (y0 - 1) << shift, constrained);
|
||||
var corner = IntraNeighbour(xL, yL, (x0 - 1) << sx, (y0 - 1) << sy, constrained);
|
||||
available[n2] = corner;
|
||||
if (corner)
|
||||
{
|
||||
@@ -56,19 +57,19 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
|
||||
// Above and above-right, left to right.
|
||||
for (var k = 0; k < n2; k += unit)
|
||||
for (var k = 0; k < n2; k += unitH)
|
||||
{
|
||||
var ok = IntraNeighbour(xL, yL, (x0 + k) << shift, (y0 - 1) << shift, constrained);
|
||||
var ok = IntraNeighbour(xL, yL, (x0 + k) << sx, (y0 - 1) << sy, constrained);
|
||||
if (ok)
|
||||
{
|
||||
plane.AsSpan(((y0 - 1) * stride) + x0 + k, unit).CopyTo(refRow);
|
||||
for (var j = 0; j < unit; j++)
|
||||
plane.AsSpan(((y0 - 1) * stride) + x0 + k, unitH).CopyTo(refRow);
|
||||
for (var j = 0; j < unitH; j++)
|
||||
{
|
||||
line[n2 + 1 + k + j] = refRow[j];
|
||||
}
|
||||
}
|
||||
|
||||
for (var j = 0; j < unit; j++)
|
||||
for (var j = 0; j < unitH; j++)
|
||||
{
|
||||
available[n2 + 1 + k + j] = ok;
|
||||
}
|
||||
@@ -104,9 +105,9 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
}
|
||||
|
||||
// Filtering (§8.4.4.2.3), luma only in 4:2:0.
|
||||
// Filtering (§8.4.4.2.3): luma, and chroma in 4:4:4, unless intra_smoothing_disabled_flag.
|
||||
Span<int> p = line;
|
||||
if (cIdx == 0 && mode != 1 && n != 4)
|
||||
if ((cIdx == 0 || sps.ChromaArrayType == 3) && !sps.IntraSmoothingDisabled && mode != 1 && n != 4)
|
||||
{
|
||||
var minDistVerHor = Math.Min(Math.Abs(mode - 26), Math.Abs(mode - 10));
|
||||
var threshold = n == 8 ? 7 : n == 16 ? 1 : 0;
|
||||
@@ -115,7 +116,7 @@ public sealed partial class H265Decoder
|
||||
var filtered = refFiltered.AsSpan(0, line.Length);
|
||||
var last = 4 * n;
|
||||
var limit = 1 << (bitDepth - 5);
|
||||
if (sps.StrongIntraSmoothing && n == 32
|
||||
if (cIdx == 0 && sps.StrongIntraSmoothing && n == 32
|
||||
&& Math.Abs(line[n2] + line[last] - (2 * line[n2 + n])) < limit
|
||||
&& Math.Abs(line[n2] + line[0] - (2 * line[n2 - n])) < limit)
|
||||
{
|
||||
@@ -234,7 +235,8 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
}
|
||||
|
||||
if (cIdx == 0 && n < 32)
|
||||
// disableIntraBoundaryFilter (§8.4.4.2.6): implicit RDPCM of a coding unit without transform and quantisation.
|
||||
if (cIdx == 0 && n < 32 && !(sps.ImplicitRdpcmEnabled && cuTransquantBypass))
|
||||
{
|
||||
if (mode == 26)
|
||||
{
|
||||
|
||||
@@ -6,6 +6,12 @@ public sealed partial class H265Decoder
|
||||
/// <summary>ctxIdxMap of sig_coeff_flag in 4×4 blocks (9-55), by raster position.</summary>
|
||||
private static readonly byte[] s_sigCtxMap4x4 = [0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8];
|
||||
|
||||
/// <summary>No residual DPCM; horizontal; vertical (§8.6.8, the direction of explicit_rdpcm_dir_flag + 1).</summary>
|
||||
private const int RdpcmNone = 0;
|
||||
|
||||
private const int RdpcmHorizontal = 1;
|
||||
private const int RdpcmVertical = 2;
|
||||
|
||||
/// <summary>TransCoeffLevel of the block being decoded, raster order (y << log2) + x; zero outside it.</summary>
|
||||
private readonly int[] coefficients = new int[32 * 32];
|
||||
|
||||
@@ -16,25 +22,42 @@ public sealed partial class H265Decoder
|
||||
private readonly int[] transformTemp = new int[32 * 32];
|
||||
private readonly byte[] codedSubBlocks = new byte[64];
|
||||
|
||||
/// <summary>The luma residual of the transform unit, for the cross-component prediction of its chroma (§8.6.6).</summary>
|
||||
private readonly int[] lumaResidual = new int[32 * 32];
|
||||
|
||||
/// <summary>Decodes the residual of one transform block and adds it to the prediction in the picture.</summary>
|
||||
/// <param name="cIdx">Colour component.</param>
|
||||
/// <param name="x0">Left of the block, in samples of the component.</param>
|
||||
/// <param name="y0">Top of the block, in samples of the component.</param>
|
||||
/// <param name="log2">Log2 of the block size.</param>
|
||||
/// <param name="predModeIntra">The intra prediction mode of the block, −1 for inter.</param>
|
||||
private void Residual(int cIdx, int x0, int y0, int log2, int predModeIntra)
|
||||
/// <param name="resScale">ResScaleVal of the cross-component prediction of a chroma block, 0 for none.</param>
|
||||
private void Residual(int cIdx, int x0, int y0, int log2, int predModeIntra, int resScale)
|
||||
{
|
||||
var count = ParseResidual(cIdx, log2, predModeIntra, out var transformSkip, out var maxX, out var maxY);
|
||||
var count = ParseResidual(cIdx, log2, predModeIntra, out var transformSkip, out var maxX, out var maxY, out var rdpcm);
|
||||
var size = 1 << log2;
|
||||
var bitDepth = cIdx == 0 ? sps.BitDepthY : sps.BitDepthC;
|
||||
var total = size * size;
|
||||
|
||||
// transform_skip_rotation_enabled_flag: the residual of 4×4 intra blocks without a transform is rotated by 180°.
|
||||
var rotate = sps.TransformSkipRotationEnabled && log2 == 2 && cuIntra && (transformSkip || cuTransquantBypass);
|
||||
if (cuTransquantBypass)
|
||||
{
|
||||
Array.Copy(coefficients, residual, total);
|
||||
if (rotate)
|
||||
{
|
||||
for (var i = 0; i < total; i++)
|
||||
{
|
||||
residual[i] = coefficients[total - 1 - i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(coefficients, residual, total);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Dequantise(cIdx, log2, count, bitDepth);
|
||||
Dequantise(cIdx, log2, count, bitDepth, transformSkip);
|
||||
if (transformSkip)
|
||||
{
|
||||
// §8.6.4.2: r = d << tsShift, then the same rounding as after a transform.
|
||||
@@ -43,7 +66,7 @@ public sealed partial class H265Decoder
|
||||
var add = 1 << (bdShift - 1);
|
||||
for (var i = 0; i < total; i++)
|
||||
{
|
||||
residual[i] = ((coefficients[i] << tsShift) + add) >> bdShift;
|
||||
residual[i] = ((coefficients[rotate ? total - 1 - i : i] << tsShift) + add) >> bdShift;
|
||||
}
|
||||
}
|
||||
else if (count == 1 && nonzero[0] == 0 && !(cuIntra && cIdx == 0 && log2 == 2))
|
||||
@@ -60,28 +83,99 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
}
|
||||
|
||||
if (rdpcm != RdpcmNone)
|
||||
{
|
||||
AccumulateResidual(log2, rdpcm == RdpcmVertical);
|
||||
}
|
||||
|
||||
// Clear the coefficients for the next block.
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
coefficients[nonzero[i]] = 0;
|
||||
}
|
||||
|
||||
if (cIdx == 0)
|
||||
{
|
||||
if (pps.CrossComponentPredictionEnabled)
|
||||
{
|
||||
Array.Copy(residual, lumaResidual, total);
|
||||
}
|
||||
}
|
||||
else if (resScale != 0)
|
||||
{
|
||||
AddCrossComponent(total, resScale);
|
||||
}
|
||||
|
||||
AddResidual(cIdx, x0, y0, log2, bitDepth);
|
||||
}
|
||||
|
||||
/// <summary>A chroma block without coefficients whose residual is only the cross-component prediction (§8.6.6).</summary>
|
||||
private void CrossComponentResidual(int cIdx, int x0, int y0, int log2, int resScale)
|
||||
{
|
||||
var total = 1 << (2 * log2);
|
||||
residual.AsSpan(0, total).Clear();
|
||||
AddCrossComponent(total, resScale);
|
||||
AddResidual(cIdx, x0, y0, log2, sps.BitDepthC);
|
||||
}
|
||||
|
||||
/// <summary>The residual modification process for transform blocks using cross-component prediction (8-xx of §8.6.6).</summary>
|
||||
private void AddCrossComponent(int total, int resScale)
|
||||
{
|
||||
int bitDepthY = sps.BitDepthY, bitDepthC = sps.BitDepthC;
|
||||
for (var i = 0; i < total; i++)
|
||||
{
|
||||
residual[i] += (resScale * ((lumaResidual[i] << bitDepthC) >> bitDepthY)) >> 3;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The directional residual modification (§8.6.8): each residual sample accumulates the previous ones.</summary>
|
||||
private void AccumulateResidual(int log2, bool vertical)
|
||||
{
|
||||
var size = 1 << log2;
|
||||
if (vertical)
|
||||
{
|
||||
for (var y = 1; y < size; y++)
|
||||
{
|
||||
var row = y << log2;
|
||||
for (var x = 0; x < size; x++)
|
||||
{
|
||||
residual[row + x] += residual[row - size + x];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var y = 0; y < size; y++)
|
||||
{
|
||||
var row = y << log2;
|
||||
for (var x = 1; x < size; x++)
|
||||
{
|
||||
residual[row + x] += residual[row + x - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The picture construction (§8.6.7): prediction plus residual, clipped to the bit depth.</summary>
|
||||
private void AddResidual(int cIdx, int x0, int y0, int log2, int bitDepth)
|
||||
{
|
||||
var size = 1 << log2;
|
||||
var plane = cur!.Planes[cIdx];
|
||||
var stride = cur.Stride(cIdx);
|
||||
var max = (1 << bitDepth) - 1;
|
||||
for (var y = 0; y < size; y++)
|
||||
{
|
||||
var row = ((y0 + y) * stride) + x0;
|
||||
var r = y << log2;
|
||||
for (var x = 0; x < size; x++)
|
||||
var row = plane.AsSpan(((y0 + y) * stride) + x0, size);
|
||||
var r = residual.AsSpan(y << log2, size);
|
||||
for (var x = 0; x < row.Length; x++)
|
||||
{
|
||||
plane[row + x] = (ushort)Math.Clamp(plane[row + x] + residual[r + x], 0, max);
|
||||
row[x] = (ushort)Math.Clamp(row[x] + r[x], 0, max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The scaling process for transform coefficients (§8.6.3), on the nonzero coefficients.</summary>
|
||||
private void Dequantise(int cIdx, int log2, int count, int bitDepth)
|
||||
private void Dequantise(int cIdx, int log2, int count, int bitDepth, bool transformSkip)
|
||||
{
|
||||
int qp;
|
||||
if (cIdx == 0)
|
||||
@@ -92,8 +186,7 @@ public sealed partial class H265Decoder
|
||||
{
|
||||
var offset = cIdx == 1 ? pps.CbQpOffset + slice.CbQpOffset + cuQpOffsetCb : pps.CrQpOffset + slice.CrQpOffset + cuQpOffsetCr;
|
||||
var qPi = Math.Clamp(qpY + offset, -sps.QpBdOffsetC, 57);
|
||||
var qPc = qPi < 30 ? qPi : qPi > 43 ? qPi - 6 : H265Tables.ChromaQp30To43[qPi - 30];
|
||||
qp = qPc + sps.QpBdOffsetC;
|
||||
qp = H265Tables.ChromaQp(qPi, sps.ChromaArrayType) + sps.QpBdOffsetC;
|
||||
}
|
||||
|
||||
var bdShift = bitDepth + log2 - 5;
|
||||
@@ -101,7 +194,9 @@ public sealed partial class H265Decoder
|
||||
var scale = (long)H265Tables.LevelScale[qp % 6] << (qp / 6);
|
||||
var list = pps.ScalingList ?? sps.ScalingList;
|
||||
byte[]? factors = null;
|
||||
if (sps.ScalingListEnabled && list is not null)
|
||||
|
||||
// m is 16 without scaling lists, and for transform skip blocks larger than 4×4 (8-xx).
|
||||
if (sps.ScalingListEnabled && list is not null && !(transformSkip && log2 > 2))
|
||||
{
|
||||
factors = list.Factors[((log2 - 2) * 6) + (cuIntra ? 0 : 3) + cIdx];
|
||||
}
|
||||
@@ -117,9 +212,9 @@ public sealed partial class H265Decoder
|
||||
|
||||
/// <summary>
|
||||
/// residual_coding(): fills <see cref="coefficients"/> and <see cref="nonzero"/>; returns the number of nonzero
|
||||
/// coefficients and the largest column and row that hold one.
|
||||
/// coefficients and the largest column and row that hold one, and the residual DPCM of the block (§8.6.8).
|
||||
/// </summary>
|
||||
private int ParseResidual(int cIdx, int log2, int predModeIntra, out bool transformSkip, out int maxX, out int maxY)
|
||||
private int ParseResidual(int cIdx, int log2, int predModeIntra, out bool transformSkip, out int maxX, out int maxY, out int rdpcm)
|
||||
{
|
||||
transformSkip = false;
|
||||
if (pps.TransformSkipEnabled && !cuTransquantBypass && log2 <= pps.Log2MaxTransformSkipSize)
|
||||
@@ -127,6 +222,28 @@ public sealed partial class H265Decoder
|
||||
transformSkip = cabac.Flag(CabacTables.TransformSkipFlag + (cIdx == 0 ? 0 : 1));
|
||||
}
|
||||
|
||||
// Residual DPCM of blocks without a transform: explicit_rdpcm_flag in inter coding units, implicit in intra ones
|
||||
// predicted horizontally (mode 10) or vertically (mode 26).
|
||||
var untransformed = transformSkip || cuTransquantBypass;
|
||||
rdpcm = RdpcmNone;
|
||||
var explicitRdpcm = false;
|
||||
if (untransformed)
|
||||
{
|
||||
if (!cuIntra && sps.ExplicitRdpcmEnabled)
|
||||
{
|
||||
var c = cIdx == 0 ? 0 : 1;
|
||||
if (cabac.Flag(CabacTables.ExplicitRdpcmFlag + c))
|
||||
{
|
||||
explicitRdpcm = true;
|
||||
rdpcm = cabac.Flag(CabacTables.ExplicitRdpcmDirFlag + c) ? RdpcmVertical : RdpcmHorizontal;
|
||||
}
|
||||
}
|
||||
else if (cuIntra && sps.ImplicitRdpcmEnabled && predModeIntra is 10 or 26)
|
||||
{
|
||||
rdpcm = predModeIntra == 26 ? RdpcmVertical : RdpcmHorizontal;
|
||||
}
|
||||
}
|
||||
|
||||
// last_sig_coeff_x_prefix, last_sig_coeff_y_prefix and their suffixes (§9.3.4.2.3).
|
||||
int ctxOffset, ctxShift;
|
||||
if (cIdx == 0)
|
||||
@@ -169,7 +286,7 @@ public sealed partial class H265Decoder
|
||||
|
||||
// scanIdx (§7.4.9.11): mode-dependent scans for intra 4×4 blocks and 8×8 luma blocks.
|
||||
var scanIdx = 0;
|
||||
if (predModeIntra >= 0 && (log2 == 2 || (log2 == 3 && cIdx == 0)))
|
||||
if (predModeIntra >= 0 && (log2 == 2 || (log2 == 3 && (cIdx == 0 || sps.ChromaArrayType == 3))))
|
||||
{
|
||||
if (predModeIntra is >= 6 and <= 14)
|
||||
{
|
||||
@@ -210,7 +327,17 @@ public sealed partial class H265Decoder
|
||||
var lastScanPos = H265Tables.ScanPosition4x4[scanIdx][((lastY & 3) << 2) + (lastX & 3)];
|
||||
codedSubBlocks.AsSpan(0, sbWidth * sbWidth).Clear();
|
||||
|
||||
var signHidingEnabled = pps.SignDataHidingEnabled && !cuTransquantBypass;
|
||||
// No sign data hiding without a transform where the residual is accumulated (§7.3.8.11).
|
||||
var signHidingEnabled = pps.SignDataHidingEnabled && !cuTransquantBypass
|
||||
&& !(cuIntra && sps.ImplicitRdpcmEnabled && transformSkip && predModeIntra is 10 or 26) && !explicitRdpcm;
|
||||
|
||||
// transform_skip_context_enabled_flag: one context for every sig_coeff_flag of a block without a transform.
|
||||
var singleSigCtx = sps.TransformSkipContextEnabled && untransformed ? (cIdx == 0 ? 42 : 16) : -1;
|
||||
|
||||
// The persistent Rice adaptation (§9.3.3.11): StatCoeff by component and transform, no bound on cRiceParam.
|
||||
var persistentRice = sps.PersistentRiceAdaptationEnabled;
|
||||
var sbType = (cIdx == 0 ? 0 : 2) + (untransformed ? 1 : 0);
|
||||
var riceLimit = persistentRice ? 16 : 4;
|
||||
var greater1Ctx = 1;
|
||||
var firstSubBlock = true;
|
||||
var count = 0;
|
||||
@@ -278,7 +405,11 @@ public sealed partial class H265Decoder
|
||||
if (n > 0 || !inferSbDcSigCoeff)
|
||||
{
|
||||
int sigCtx;
|
||||
if (log2 == 2)
|
||||
if (singleSigCtx >= 0)
|
||||
{
|
||||
sigCtx = singleSigCtx;
|
||||
}
|
||||
else if (log2 == 2)
|
||||
{
|
||||
sigCtx = s_sigCtxMap4x4[(yP << 2) + xP];
|
||||
}
|
||||
@@ -379,8 +510,10 @@ public sealed partial class H265Decoder
|
||||
signs = 0;
|
||||
}
|
||||
|
||||
// coeff_abs_level_remaining (§9.3.3.11) with the Rice parameter of §9.3.3.11 (v1: starts at 0, at most 4).
|
||||
var rice = 0;
|
||||
// coeff_abs_level_remaining (§9.3.3.11): the Rice parameter starts at 0 (at StatCoeff / 4 with the
|
||||
// persistent adaptation, which the first value of the sub-block updates) and grows with the levels.
|
||||
var rice = persistentRice ? statCoeff[sbType] >> 2 : 0;
|
||||
var firstRemaining = persistentRice;
|
||||
var sumAbs = 0;
|
||||
for (var k = 0; k < sigCount; k++)
|
||||
{
|
||||
@@ -390,9 +523,24 @@ public sealed partial class H265Decoder
|
||||
{
|
||||
var remaining = CoeffAbsLevelRemaining(rice);
|
||||
absolute = baseLevel + remaining;
|
||||
if (firstRemaining)
|
||||
{
|
||||
ref var stat = ref statCoeff[sbType];
|
||||
if (remaining >= 3 << (stat >> 2))
|
||||
{
|
||||
stat++;
|
||||
}
|
||||
else if (2 * remaining < 1 << (stat >> 2) && stat > 0)
|
||||
{
|
||||
stat--;
|
||||
}
|
||||
|
||||
firstRemaining = false;
|
||||
}
|
||||
|
||||
if (absolute > 3 * (1 << rice))
|
||||
{
|
||||
rice = Math.Min(rice + 1, 4);
|
||||
rice = Math.Min(rice + 1, riceLimit);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,12 @@ public sealed partial class H265Decoder
|
||||
private readonly CabacEngine cabac = new();
|
||||
private readonly byte[] wppContexts = new byte[CabacTables.ContextCount];
|
||||
private readonly byte[] dsContexts = new byte[CabacTables.ContextCount];
|
||||
|
||||
/// <summary>StatCoeff of the persistent Rice adaptation (§9.3.3.11), kept and synchronised with the contexts.</summary>
|
||||
private readonly int[] statCoeff = new int[4];
|
||||
|
||||
private readonly int[] wppStatCoeff = new int[4];
|
||||
private readonly int[] dsStatCoeff = new int[4];
|
||||
private List<SliceHeader> slices = [];
|
||||
|
||||
private Picture? cur;
|
||||
@@ -90,7 +96,11 @@ public sealed partial class H265Decoder
|
||||
private bool cuIntra;
|
||||
private bool cuTransquantBypass;
|
||||
private int cuPartMode;
|
||||
private int cuIntraModeC;
|
||||
|
||||
/// <summary>IntraPredModeC of each prediction block (one unless 4:4:4 with NxN), and whether intra_chroma_pred_mode was 4.</summary>
|
||||
private readonly int[] cuChromaModes = new int[4];
|
||||
|
||||
private readonly bool[] cuChromaDm = new bool[4];
|
||||
private int cuMaxTrafoDepth;
|
||||
private bool cuIntraSplit;
|
||||
|
||||
@@ -152,6 +162,8 @@ public sealed partial class H265Decoder
|
||||
{
|
||||
sliceIdx = slices.Count - 1;
|
||||
resetQpPrev = true;
|
||||
cuQpOffsetCb = 0;
|
||||
cuQpOffsetCr = 0;
|
||||
}
|
||||
|
||||
data = nal.Rbsp;
|
||||
@@ -180,7 +192,7 @@ public sealed partial class H265Decoder
|
||||
// Initialisation of the context variables (§9.3.2.2 to §9.3.2.4).
|
||||
if (tileStart)
|
||||
{
|
||||
cabac.InitContexts(initType, h.SliceQpY);
|
||||
InitEntropy(initType, h.SliceQpY);
|
||||
}
|
||||
else if (wppRowStart)
|
||||
{
|
||||
@@ -188,20 +200,20 @@ public sealed partial class H265Decoder
|
||||
var available = ctbY > 0 && ctbX + 1 < widthInCtbs && ctbSlice[topRight] == sliceIdx && pps.TileIdRs[topRight] == tile;
|
||||
if (available)
|
||||
{
|
||||
wppContexts.CopyTo(cabac.Contexts, 0);
|
||||
LoadEntropy(wppContexts, wppStatCoeff);
|
||||
}
|
||||
else
|
||||
{
|
||||
cabac.InitContexts(initType, h.SliceQpY);
|
||||
InitEntropy(initType, h.SliceQpY);
|
||||
}
|
||||
}
|
||||
else if (h.Dependent)
|
||||
{
|
||||
dsContexts.CopyTo(cabac.Contexts, 0);
|
||||
LoadEntropy(dsContexts, dsStatCoeff);
|
||||
}
|
||||
else
|
||||
{
|
||||
cabac.InitContexts(initType, h.SliceQpY);
|
||||
InitEntropy(initType, h.SliceQpY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +226,7 @@ public sealed partial class H265Decoder
|
||||
CodingTreeUnit();
|
||||
if (pps.EntropyCodingSyncEnabled && ctbX - colStart <= 1)
|
||||
{
|
||||
cabac.Contexts.CopyTo(wppContexts, 0);
|
||||
SaveEntropy(wppContexts, wppStatCoeff);
|
||||
}
|
||||
|
||||
var end = cabac.Terminate() == 1;
|
||||
@@ -223,7 +235,7 @@ public sealed partial class H265Decoder
|
||||
{
|
||||
if (pps.DependentSliceSegmentsEnabled)
|
||||
{
|
||||
cabac.Contexts.CopyTo(dsContexts, 0);
|
||||
SaveEntropy(dsContexts, dsStatCoeff);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -255,6 +267,27 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The initialisation of §9.3.2.2: the context variables, and StatCoeff to 0.</summary>
|
||||
private void InitEntropy(int initType, int sliceQp)
|
||||
{
|
||||
cabac.InitContexts(initType, sliceQp);
|
||||
Array.Clear(statCoeff);
|
||||
}
|
||||
|
||||
/// <summary>The synchronisation process of §9.3.2.4: contexts and StatCoeff from a stored state.</summary>
|
||||
private void LoadEntropy(byte[] contexts, int[] stat)
|
||||
{
|
||||
contexts.CopyTo(cabac.Contexts, 0);
|
||||
stat.CopyTo(statCoeff, 0);
|
||||
}
|
||||
|
||||
/// <summary>The storage process of §9.3.2.3.</summary>
|
||||
private void SaveEntropy(byte[] contexts, int[] stat)
|
||||
{
|
||||
cabac.Contexts.CopyTo(contexts, 0);
|
||||
statCoeff.CopyTo(stat, 0);
|
||||
}
|
||||
|
||||
private void CodingTreeUnit()
|
||||
{
|
||||
var log2 = sps.Log2CtbSize;
|
||||
@@ -518,7 +551,7 @@ public sealed partial class H265Decoder
|
||||
if (rqtRootCbf)
|
||||
{
|
||||
cuMaxTrafoDepth = cuIntra ? sps.MaxTransformHierarchyDepthIntra + (cuIntraSplit ? 1 : 0) : sps.MaxTransformHierarchyDepthInter;
|
||||
TransformTree(x0, y0, x0, y0, log2, 0, 0, false, false);
|
||||
TransformTree(x0, y0, x0, y0, log2, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -593,12 +626,12 @@ public sealed partial class H265Decoder
|
||||
var pbSize = cuIntraSplit ? 1 << (log2 - 1) : 1 << log2;
|
||||
Span<bool> prev = stackalloc bool[4];
|
||||
Span<int> list = stackalloc int[3];
|
||||
Span<int> lumaModes = stackalloc int[4];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
prev[i] = cabac.Flag(CabacTables.PrevIntraLumaPredFlag);
|
||||
}
|
||||
|
||||
var firstMode = 0;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var xPb = x0 + ((i & 1) * pbSize);
|
||||
@@ -648,23 +681,33 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
|
||||
FillGrid(intraModeMap, xPb, yPb, pbSize, pbSize, (byte)mode);
|
||||
if (i == 0)
|
||||
{
|
||||
firstMode = mode;
|
||||
}
|
||||
lumaModes[i] = mode;
|
||||
}
|
||||
|
||||
// intra_chroma_pred_mode (table 8-2 for ChromaArrayType 1): 4 is the luma mode, 0 to 3 are planar, vertical,
|
||||
// horizontal and DC, replaced by mode 34 when they equal the luma mode.
|
||||
var chroma = !cabac.Flag(CabacTables.IntraChromaPredMode) ? 4 : cabac.BypassBits(2);
|
||||
cuIntraModeC = chroma switch
|
||||
if (sps.ChromaArrayType == 0)
|
||||
{
|
||||
0 => firstMode == 0 ? 34 : 0,
|
||||
1 => firstMode == 26 ? 34 : 26,
|
||||
2 => firstMode == 10 ? 34 : 10,
|
||||
3 => firstMode == 1 ? 34 : 1,
|
||||
_ => firstMode,
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
// intra_chroma_pred_mode (§8.4.3, table 8-2): 4 is the luma mode, 0 to 3 are planar, vertical, horizontal and DC,
|
||||
// replaced by mode 34 when they equal the luma mode. One for each prediction block in 4:4:4, one for the coding
|
||||
// unit otherwise (with the mode of its first block); in 4:2:2 the mode is then mapped by table 8-3.
|
||||
var chromaCount = sps.ChromaArrayType == 3 ? count : 1;
|
||||
for (var i = 0; i < chromaCount; i++)
|
||||
{
|
||||
var luma = lumaModes[i];
|
||||
var chroma = !cabac.Flag(CabacTables.IntraChromaPredMode) ? 4 : cabac.BypassBits(2);
|
||||
var mode = chroma switch
|
||||
{
|
||||
0 => luma == 0 ? 34 : 0,
|
||||
1 => luma == 26 ? 34 : 26,
|
||||
2 => luma == 10 ? 34 : 10,
|
||||
3 => luma == 1 ? 34 : 1,
|
||||
_ => luma,
|
||||
};
|
||||
cuChromaModes[i] = sps.ChromaArrayType == 2 ? H265Tables.ChromaMode422[mode] : mode;
|
||||
cuChromaDm[i] = chroma == 4;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>candIntraPredModeX of §8.4.2.</summary>
|
||||
@@ -705,18 +748,19 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
}
|
||||
|
||||
var chromaSize = size >> 1;
|
||||
shift = sps.BitDepthC - sps.PcmBitDepthC;
|
||||
bits = sps.PcmBitDepthC;
|
||||
for (var c = 1; c <= 2; c++)
|
||||
int sx = sps.ChromaShiftX, sy = sps.ChromaShiftY;
|
||||
int chromaWidth = size >> sx, chromaHeight = size >> sy, xc = x0 >> sx, yc = y0 >> sy;
|
||||
for (var c = 1; c <= 2 && sps.ChromaArrayType != 0; c++)
|
||||
{
|
||||
plane = cur.Planes[c];
|
||||
stride = cur.ChromaWidth;
|
||||
for (var y = 0; y < chromaSize; y++)
|
||||
for (var y = 0; y < chromaHeight; y++)
|
||||
{
|
||||
for (var x = 0; x < chromaSize; x++)
|
||||
for (var x = 0; x < chromaWidth; x++)
|
||||
{
|
||||
plane[(((y0 >> 1) + y) * stride) + (x0 >> 1) + x] = (ushort)(reader.Read(bits) << shift);
|
||||
plane[((yc + y) * stride) + xc + x] = (ushort)(reader.Read(bits) << shift);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -936,7 +980,11 @@ public sealed partial class H265Decoder
|
||||
return (value << count) | cabac.BypassBits(count);
|
||||
}
|
||||
|
||||
private void TransformTree(int x0, int y0, int xBase, int yBase, int log2, int depth, int blkIdx, bool parentCbfCb, bool parentCbfCr)
|
||||
/// <summary>
|
||||
/// transform_tree() (§7.3.8.8). <paramref name="parentCbf"/> holds the chroma coded block flags of the parent: bit 0
|
||||
/// cbf_cb, bit 1 the cbf_cb of the second (lower) 4:2:2 block, bits 2 and 3 the same for cbf_cr.
|
||||
/// </summary>
|
||||
private void TransformTree(int x0, int y0, int xBase, int yBase, int log2, int depth, int blkIdx, int parentCbf)
|
||||
{
|
||||
bool split;
|
||||
var interSplit = sps.MaxTransformHierarchyDepthInter == 0 && !cuIntra && cuPartMode != Part2Nx2N && depth == 0;
|
||||
@@ -949,42 +997,54 @@ public sealed partial class H265Decoder
|
||||
split = log2 > sps.Log2MaxTbSize || (cuIntraSplit && depth == 0) || interSplit;
|
||||
}
|
||||
|
||||
bool cbfCb, cbfCr;
|
||||
if (log2 > 2)
|
||||
var chromaType = sps.ChromaArrayType;
|
||||
var cbf = 0;
|
||||
if ((log2 > 2 && chromaType != 0) || chromaType == 3)
|
||||
{
|
||||
cbfCb = (depth == 0 || parentCbfCb) && cabac.Flag(CabacTables.CbfChroma + depth);
|
||||
cbfCr = (depth == 0 || parentCbfCr) && cabac.Flag(CabacTables.CbfChroma + depth);
|
||||
// In 4:2:2 a block that is not split further (or splits into 4×4 luma blocks) has two chroma blocks per
|
||||
// component, one above the other, each with its flag.
|
||||
var second = chromaType == 2 && (!split || log2 == 3);
|
||||
if (depth == 0 || (parentCbf & 1) != 0)
|
||||
{
|
||||
cbf |= cabac.Flag(CabacTables.CbfChroma + depth) ? 1 : 0;
|
||||
cbf |= second && cabac.Flag(CabacTables.CbfChroma + depth) ? 2 : 0;
|
||||
}
|
||||
|
||||
if (depth == 0 || (parentCbf & 4) != 0)
|
||||
{
|
||||
cbf |= cabac.Flag(CabacTables.CbfChroma + depth) ? 4 : 0;
|
||||
cbf |= second && cabac.Flag(CabacTables.CbfChroma + depth) ? 8 : 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (chromaType != 0)
|
||||
{
|
||||
// 4×4 luma blocks: the chroma of the four is coded once, with the parent's flags (§7.4.9.8).
|
||||
cbfCb = parentCbfCb;
|
||||
cbfCr = parentCbfCr;
|
||||
// 4×4 luma blocks in 4:2:0 and 4:2:2: the chroma of the four is coded once, with the parent's flags (§7.4.9.8).
|
||||
cbf = parentCbf;
|
||||
}
|
||||
|
||||
if (split)
|
||||
{
|
||||
var half = 1 << (log2 - 1);
|
||||
TransformTree(x0, y0, x0, y0, log2 - 1, depth + 1, 0, cbfCb, cbfCr);
|
||||
TransformTree(x0 + half, y0, x0, y0, log2 - 1, depth + 1, 1, cbfCb, cbfCr);
|
||||
TransformTree(x0, y0 + half, x0, y0, log2 - 1, depth + 1, 2, cbfCb, cbfCr);
|
||||
TransformTree(x0 + half, y0 + half, x0, y0, log2 - 1, depth + 1, 3, cbfCb, cbfCr);
|
||||
TransformTree(x0, y0, x0, y0, log2 - 1, depth + 1, 0, cbf);
|
||||
TransformTree(x0 + half, y0, x0, y0, log2 - 1, depth + 1, 1, cbf);
|
||||
TransformTree(x0, y0 + half, x0, y0, log2 - 1, depth + 1, 2, cbf);
|
||||
TransformTree(x0 + half, y0 + half, x0, y0, log2 - 1, depth + 1, 3, cbf);
|
||||
return;
|
||||
}
|
||||
|
||||
var cbfLuma = true;
|
||||
if (cuIntra || depth != 0 || cbfCb || cbfCr)
|
||||
if (cuIntra || depth != 0 || cbf != 0)
|
||||
{
|
||||
cbfLuma = cabac.Flag(CabacTables.CbfLuma + (depth == 0 ? 1 : 0));
|
||||
}
|
||||
|
||||
TransformUnit(x0, y0, xBase, yBase, log2, blkIdx, cbfLuma, cbfCb, cbfCr);
|
||||
TransformUnit(x0, y0, xBase, yBase, log2, blkIdx, cbfLuma, cbf);
|
||||
}
|
||||
|
||||
private void TransformUnit(int x0, int y0, int xBase, int yBase, int log2, int blkIdx, bool cbfLuma, bool cbfCb, bool cbfCr)
|
||||
private void TransformUnit(int x0, int y0, int xBase, int yBase, int log2, int blkIdx, bool cbfLuma, int cbf)
|
||||
{
|
||||
var size = 1 << log2;
|
||||
var cbfChroma = cbfCb || cbfCr;
|
||||
var cbfChroma = cbf != 0;
|
||||
if (cbfLuma || cbfChroma)
|
||||
{
|
||||
if (pps.CuQpDeltaEnabled && !isCuQpDeltaCoded)
|
||||
@@ -1033,7 +1093,6 @@ public sealed partial class H265Decoder
|
||||
}
|
||||
|
||||
var lumaMode = cuIntra ? intraModeMap[Grid(x0, y0)] : -1;
|
||||
var chromaMode = cuIntra ? cuIntraModeC : -1;
|
||||
if (cuIntra)
|
||||
{
|
||||
PredictIntra(0, x0, y0, log2, lumaMode);
|
||||
@@ -1041,7 +1100,7 @@ public sealed partial class H265Decoder
|
||||
|
||||
if (cbfLuma)
|
||||
{
|
||||
Residual(0, x0, y0, log2, lumaMode);
|
||||
Residual(0, x0, y0, log2, lumaMode, 0);
|
||||
FillGrid(blockFlags, x0, y0, size, size, FlagCoded, true);
|
||||
}
|
||||
|
||||
@@ -1055,54 +1114,80 @@ public sealed partial class H265Decoder
|
||||
MarkHorizontal(x0, y0, size, 2);
|
||||
}
|
||||
|
||||
if (log2 > 2)
|
||||
var chromaType = sps.ChromaArrayType;
|
||||
if (chromaType == 0)
|
||||
{
|
||||
int xc = x0 >> 1, yc = y0 >> 1;
|
||||
if (cuIntra)
|
||||
return;
|
||||
}
|
||||
|
||||
int sx = sps.ChromaShiftX, sy = sps.ChromaShiftY;
|
||||
if (log2 > 2 || chromaType == 3)
|
||||
{
|
||||
// In 4:4:4 an NxN coding unit has a chroma mode for each of its four blocks.
|
||||
var part = 0;
|
||||
if (cuIntraSplit && chromaType == 3)
|
||||
{
|
||||
PredictIntra(1, xc, yc, log2 - 1, cuIntraModeC);
|
||||
var half = 1 << (cuLog2 - 1);
|
||||
part = (y0 >= cuY + half ? 2 : 0) + (x0 >= cuX + half ? 1 : 0);
|
||||
}
|
||||
|
||||
if (cbfCb)
|
||||
{
|
||||
Residual(1, xc, yc, log2 - 1, chromaMode);
|
||||
}
|
||||
|
||||
if (cuIntra)
|
||||
{
|
||||
PredictIntra(2, xc, yc, log2 - 1, cuIntraModeC);
|
||||
}
|
||||
|
||||
if (cbfCr)
|
||||
{
|
||||
Residual(2, xc, yc, log2 - 1, chromaMode);
|
||||
}
|
||||
ChromaBlocks(x0 >> sx, y0 >> sy, chromaType == 3 ? log2 : log2 - 1, cbf, part, cbfLuma);
|
||||
}
|
||||
else if (blkIdx == 3)
|
||||
{
|
||||
int xc = xBase >> 1, yc = yBase >> 1;
|
||||
if (cuIntra)
|
||||
{
|
||||
PredictIntra(1, xc, yc, 2, cuIntraModeC);
|
||||
}
|
||||
ChromaBlocks(xBase >> sx, yBase >> sy, 2, cbf, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (cbfCb)
|
||||
/// <summary>
|
||||
/// The chroma blocks of a transform unit (two per component in 4:2:2, one above the other): prediction, then the
|
||||
/// residual, with cross_comp_pred() (§7.3.8.12) when the luma block has coefficients.
|
||||
/// </summary>
|
||||
private void ChromaBlocks(int xc, int yc, int log2, int cbf, int part, bool lumaCoded)
|
||||
{
|
||||
var mode = cuIntra ? cuChromaModes[part] : -1;
|
||||
var crossComponent = lumaCoded && pps.CrossComponentPredictionEnabled && (!cuIntra || cuChromaDm[part]);
|
||||
var blocks = sps.ChromaArrayType == 2 ? 2 : 1;
|
||||
for (var c = 1; c <= 2; c++)
|
||||
{
|
||||
var resScale = crossComponent ? CrossComponentPrediction(c - 1) : 0;
|
||||
for (var t = 0; t < blocks; t++)
|
||||
{
|
||||
Residual(1, xc, yc, 2, chromaMode);
|
||||
}
|
||||
var y = yc + (t << log2);
|
||||
if (cuIntra)
|
||||
{
|
||||
PredictIntra(c, xc, y, log2, mode);
|
||||
}
|
||||
|
||||
if (cuIntra)
|
||||
{
|
||||
PredictIntra(2, xc, yc, 2, cuIntraModeC);
|
||||
}
|
||||
|
||||
if (cbfCr)
|
||||
{
|
||||
Residual(2, xc, yc, 2, chromaMode);
|
||||
if ((cbf & (1 << (((c - 1) * 2) + t))) != 0)
|
||||
{
|
||||
Residual(c, xc, y, log2, mode, resScale);
|
||||
}
|
||||
else if (resScale != 0)
|
||||
{
|
||||
CrossComponentResidual(c, xc, y, log2, resScale);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>log2_res_scale_abs_plus1 and res_scale_sign_flag of one chroma component, as ResScaleVal (7.4.9.12).</summary>
|
||||
private int CrossComponentPrediction(int c)
|
||||
{
|
||||
var value = 0;
|
||||
while (value < 4 && cabac.Flag(CabacTables.Log2ResScaleAbsPlus1 + (4 * c) + value))
|
||||
{
|
||||
value++;
|
||||
}
|
||||
|
||||
if (value == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return cabac.Flag(CabacTables.ResScaleSignFlag + c) ? -(1 << (value - 1)) : 1 << (value - 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// availableN of the z-scan order (§6.4.1), for luma locations: inside the picture, already decoded, in the same
|
||||
/// slice and the same tile.
|
||||
|
||||
@@ -4,11 +4,13 @@ namespace Febe.Core.Codecs.H265;
|
||||
|
||||
/// <summary>
|
||||
/// An HEVC (ITU-T H.265 | ISO/IEC 23008-2) decoder written from the Recommendation, for the Main, Main 10 and Main Still
|
||||
/// Picture profiles: 4:2:0 at 8 to 10 bits (up to 12 accepted), tiles, wavefronts, dependent slice segments, PCM, transform
|
||||
/// skip, sign data hiding, scaling lists, inter prediction (merge, AMVP, temporal motion vector prediction, weighted
|
||||
/// prediction), deblocking and SAO. Not handled (an
|
||||
/// <see cref="H265Exception"/>): the range extensions (4:0:0, 4:2:2, 4:4:4 and their coding tools), separate colour planes
|
||||
/// and the layers above 0 of scalable and multiview streams, which are ignored.
|
||||
/// Picture profiles and the format range extensions up to 12 bits: 4:0:0, 4:2:0, 4:2:2 and 4:4:4, tiles, wavefronts,
|
||||
/// dependent slice segments, PCM, transform skip, sign data hiding, scaling lists, inter prediction (merge, AMVP, temporal
|
||||
/// motion vector prediction, weighted prediction), deblocking and SAO, and the range extension tools (cross-component
|
||||
/// prediction, implicit and explicit RDPCM, transform skip rotation and contexts, persistent Rice adaptation, chroma QP
|
||||
/// offset lists, intra smoothing disabled, high precision offsets). Not handled (an <see cref="H265Exception"/>): extended
|
||||
/// precision processing, the CABAC bypass alignment, separate colour planes, bit depths above 12; the layers above 0 of
|
||||
/// scalable and multiview streams are ignored.
|
||||
/// </summary>
|
||||
public sealed partial class H265Decoder
|
||||
{
|
||||
@@ -248,14 +250,14 @@ public sealed partial class H265Decoder
|
||||
return;
|
||||
}
|
||||
|
||||
if (s.ChromaArrayType != 1 || s.BitDepthY > 12 || s.BitDepthC > 12)
|
||||
if (s.SeparateColourPlane || s.BitDepthY > 12 || s.BitDepthC > 12)
|
||||
{
|
||||
throw new H265Exception("Solo video HEVC 4:2:0 fino a 12 bit.");
|
||||
throw new H265Exception("Solo video HEVC fino a 12 bit, senza piani di colore separati.");
|
||||
}
|
||||
|
||||
if (s.UsesRangeExtensionTools || p.UsesRangeExtensionTools)
|
||||
if (s.UsesUnsupportedTools)
|
||||
{
|
||||
throw new H265Exception("Gli strumenti delle estensioni di formato HEVC non sono gestiti.");
|
||||
throw new H265Exception("La precisione estesa e l'allineamento CABAC delle estensioni di formato HEVC non sono gestiti.");
|
||||
}
|
||||
|
||||
p.Activate(s);
|
||||
|
||||
@@ -2,7 +2,7 @@ namespace Febe.Core.Codecs.H265;
|
||||
|
||||
/// <summary>
|
||||
/// Constant tables of ITU-T H.265, transcribed from the text of the Recommendation: scan orders (§6.5.3–6.5.5), default
|
||||
/// scaling lists (table 7-6), intra prediction angles (tables 8-4 and 8-5), the chroma QP mapping (table 8-10), the
|
||||
/// scaling lists (table 7-6), intra prediction angles (tables 8-4 and 8-5), the 4:2:2 chroma intra modes (table 8-3), the chroma QP mapping (table 8-10), the
|
||||
/// thresholds of the deblocking filter (table 8-12) and the transform matrices (§8.6.4.2).
|
||||
/// </summary>
|
||||
internal static class H265Tables
|
||||
@@ -20,6 +20,21 @@ internal static class H265Tables
|
||||
/// <summary>QpC as a function of qPi for ChromaArrayType 1 (table 8-10), for qPi from 30 to 43.</summary>
|
||||
internal static readonly byte[] ChromaQp30To43 = [29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37];
|
||||
|
||||
/// <summary>
|
||||
/// The 4:2:2 intra prediction mode of chroma (table 8-3) from modeIdc, the mode table 8-2 gives: for chroma samples
|
||||
/// twice as tall as they are wide, the mode whose angle is closest to the luma one with the horizontal displacement
|
||||
/// halved (horizontal modes: the vertical one doubled; past 45° a mode of the other direction).
|
||||
/// </summary>
|
||||
internal static readonly byte[] ChromaMode422 =
|
||||
[
|
||||
0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
|
||||
21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31,
|
||||
];
|
||||
|
||||
/// <summary>QpC of chroma from qPi (8-258 to 8-261, table 8-10): the table for 4:2:0, Min(qPi, 51) otherwise.</summary>
|
||||
internal static int ChromaQp(int qPi, int chromaArrayType) =>
|
||||
chromaArrayType != 1 ? Math.Min(qPi, 51) : qPi < 30 ? qPi : qPi > 43 ? qPi - 6 : ChromaQp30To43[qPi - 30];
|
||||
|
||||
/// <summary>β′ for Q from 0 to 51 (table 8-12).</summary>
|
||||
internal static readonly byte[] Beta =
|
||||
[
|
||||
|
||||
@@ -336,7 +336,7 @@ internal sealed class Sps
|
||||
internal bool TemporalMvpEnabled;
|
||||
internal bool StrongIntraSmoothing;
|
||||
|
||||
// Range extension (§7.3.2.2.2): parsed; streams that use these tools are refused for now.
|
||||
// Range extension (§7.3.2.2.2): every tool is decoded except extended precision and the bypass alignment.
|
||||
internal bool TransformSkipRotationEnabled;
|
||||
internal bool TransformSkipContextEnabled;
|
||||
internal bool ImplicitRdpcmEnabled;
|
||||
@@ -372,10 +372,16 @@ internal sealed class Sps
|
||||
|
||||
internal int HighestTid => MaxSubLayersMinus1;
|
||||
|
||||
internal bool UsesRangeExtensionTools =>
|
||||
TransformSkipRotationEnabled || TransformSkipContextEnabled || ImplicitRdpcmEnabled || ExplicitRdpcmEnabled
|
||||
|| ExtendedPrecisionProcessing || IntraSmoothingDisabled || HighPrecisionOffsetsEnabled
|
||||
|| PersistentRiceAdaptationEnabled || CabacBypassAlignmentEnabled;
|
||||
/// <summary>
|
||||
/// The range extension tools the decoder does not handle: extended_precision_processing_flag and
|
||||
/// cabac_bypass_alignment_enabled_flag, which only the 16-bit and High Throughput 4:4:4 profiles use.
|
||||
/// </summary>
|
||||
internal bool UsesUnsupportedTools => ExtendedPrecisionProcessing || CabacBypassAlignmentEnabled;
|
||||
|
||||
/// <summary>log2 of SubWidthC and SubHeightC (table 6-1).</summary>
|
||||
internal int ChromaShiftX => SubWidthC - 1;
|
||||
|
||||
internal int ChromaShiftY => SubHeightC - 1;
|
||||
|
||||
internal static Sps Parse(BitReader r)
|
||||
{
|
||||
@@ -753,9 +759,6 @@ internal sealed class Pps
|
||||
|
||||
internal int[] RowBd = [];
|
||||
|
||||
internal bool UsesRangeExtensionTools => CrossComponentPredictionEnabled || ChromaQpOffsetListEnabled
|
||||
|| Log2SaoOffsetScaleLuma != 0 || Log2SaoOffsetScaleChroma != 0 || Log2MaxTransformSkipSize != 2;
|
||||
|
||||
internal static Pps Parse(BitReader r)
|
||||
{
|
||||
var p = new Pps
|
||||
|
||||
@@ -98,7 +98,7 @@ internal sealed class Picture
|
||||
|
||||
private static ushort[] Crop(ushort[] plane, int stride, int x0, int y0, int width, int height)
|
||||
{
|
||||
var result = new ushort[width * height];
|
||||
var result = GC.AllocateUninitializedArray<ushort>(width * height);
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
plane.AsSpan(((y0 + y) * stride) + x0, width).CopyTo(result.AsSpan(y * width, width));
|
||||
|
||||
Reference in new issue
Block a user