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:
Alby96andClaude Opus 5.5 committed 2026-11-18 17:31:12 +01:00
1 parent 8d03ca23de
commit da84c79f7e
23 files changed
+2016 -607

No files matched your search

+218 -110
View File
@@ -1,10 +1,16 @@
using static Febe.Core.Codecs.H264.Samples;
namespace Febe.Core.Codecs.H264;
public sealed partial class H264Decoder
{
/// <summary>The luma-like plane the deblocking filter is working on: Y, or one colour plane coded separately.</summary>
private byte[] deblockLuma = [];
/// <summary>
/// Deblocking filter (§8.7) of the picture just decoded, macroblock by macroblock in address order: a frame, one
/// field (its rows only, as a picture of its own), or an MBAFF frame (pairs, top then bottom macroblock).
/// field (its rows only, as a picture of its own), or an MBAFF frame (pairs, top then bottom macroblock). Colour planes
/// coded separately are filtered one by one, each as a monochrome picture with its own macroblock data.
/// </summary>
private void Deblock(Picture picture)
{
@@ -19,25 +25,59 @@ public sealed partial class H264Decoder
return;
}
if (curStructure != 0)
if (!separatePlanes)
{
DeblockRows(picture, 2, curStructure - 1);
DeblockPlane(picture, picture.Y);
return;
}
else if (activeSps!.MbAdaptiveFrameField)
for (var k = 0; k < 3; k++)
{
DeblockPairs(picture);
SelectColourPlane(k);
picture.SelectPlane(k);
DeblockPlane(picture, picture.Plane(k));
}
SelectColourPlane(0);
picture.SelectPlane(0);
}
private void DeblockPlane(Picture picture, byte[] luma)
{
deblockLuma = luma;
if (wide)
{
DeblockPicture<ushort>(picture);
}
else
{
DeblockRows(picture, 1, 0);
DeblockPicture<byte>(picture);
}
}
private void DeblockPicture<T>(Picture picture)
where T : unmanaged
{
if (curStructure != 0)
{
DeblockRows<T>(picture, 2, curStructure - 1);
}
else if (activeSps!.MbAdaptiveFrameField)
{
DeblockPairs<T>(picture);
}
else
{
DeblockRows<T>(picture, 1, 0);
}
}
/// <summary>A frame (<paramref name="rowStep"/> 1) or the field of <paramref name="parity"/> (<paramref name="rowStep"/> 2).</summary>
private void DeblockRows(Picture picture, int rowStep, int parity)
private void DeblockRows<T>(Picture picture, int rowStep, int parity)
where T : unmanaged
{
var w = widthInMbs;
int width = picture.Width, cwidth = width / 2;
int width = picture.Width, cwidth = picture.ChromaWidth;
int stride = width * rowStep, cstride = cwidth * rowStep;
Span<byte> bs = stackalloc byte[4];
var rows = heightInMbs / rowStep;
@@ -58,21 +98,21 @@ public sealed partial class H264Decoder
continue;
}
var lumaOrigin = ((((row * 16 * rowStep) + parity) * width)) + (mx * 16);
var chromaOrigin = ((((row * 8 * rowStep) + parity) * cwidth)) + (mx * 8);
var lumaOrigin = (((row * 16 * rowStep) + parity) * width) + (mx * 16);
var chromaOrigin = (((row * mbHeightC * rowStep) + parity) * cwidth) + (mx * mbWidthC);
if (mx > 0 && mbSlice[m - 1] >= 0 && (disable != 2 || mbSlice[m - 1] == s))
{
MbEdge(picture, m - 1, m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
MbEdge<T>(picture, m - 1, m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
}
InnerEdges(picture, m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
InnerEdges<T>(picture, m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
var up = m - (rowStep * w);
if (row > 0 && mbSlice[up] >= 0 && (disable != 2 || mbSlice[up] == s))
{
MbEdge(picture, up, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
MbEdge<T>(picture, up, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
}
InnerEdges(picture, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
InnerEdges<T>(picture, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
}
}
}
@@ -81,10 +121,11 @@ public sealed partial class H264Decoder
/// An MBAFF frame. Between a frame and a field pair the left edge is filtered row by row, each row against the
/// macroblock that holds it; the top edge of a frame macroblock under a field pair twice, once per field (§8.7.1).
/// </summary>
private void DeblockPairs(Picture picture)
private void DeblockPairs<T>(Picture picture)
where T : unmanaged
{
var w = widthInMbs;
int width = picture.Width, cwidth = width / 2;
int width = picture.Width, cwidth = picture.ChromaWidth;
Span<byte> bs = stackalloc byte[4];
Span<byte> rowBs = stackalloc byte[16];
Span<int> rowMb = stackalloc int[16];
@@ -113,14 +154,14 @@ public sealed partial class H264Decoder
if (isField)
{
lumaOrigin = (((pairY * 32) + bottom) * width) + (mx * 16);
chromaOrigin = (((pairY * 16) + bottom) * cwidth) + (mx * 8);
chromaOrigin = (((pairY * 2 * mbHeightC) + bottom) * cwidth) + (mx * mbWidthC);
stride = 2 * width;
cstride = 2 * cwidth;
}
else
{
lumaOrigin = ((((2 * pairY) + bottom) * 16) * width) + (mx * 16);
chromaOrigin = ((((2 * pairY) + bottom) * 8) * cwidth) + (mx * 8);
chromaOrigin = ((((2 * pairY) + bottom) * mbHeightC) * cwidth) + (mx * mbWidthC);
stride = width;
cstride = cwidth;
}
@@ -132,20 +173,20 @@ public sealed partial class H264Decoder
var leftField = field[leftTop];
if (leftField == isField)
{
MbEdge(picture, leftTop + (bottom * w), m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
MbEdge<T>(picture, leftTop + (bottom * w), m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
}
else
{
MixedLeftEdge(picture, m, leftTop, isField, bottom != 0, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, rowBs, rowMb);
MixedLeftEdge<T>(picture, m, leftTop, isField, bottom != 0, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, rowBs, rowMb);
}
}
InnerEdges(picture, m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
InnerEdges<T>(picture, m, true, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
// Top edge.
if (!isField && bottom == 1)
{
MbEdge(picture, m - w, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
MbEdge<T>(picture, m - w, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
}
else if (pairY > 0)
{
@@ -157,18 +198,18 @@ public sealed partial class H264Decoder
// A frame macroblock under a field pair: each field of its top rows against that field.
for (var j = 0; j < 2; j++)
{
MbEdge(picture, aboveTop + (j * w), m, false, lumaOrigin + (j * width), 2 * width, chromaOrigin + (j * cwidth), 2 * cwidth, alphaOffset, betaOffset, bs);
MbEdge<T>(picture, aboveTop + (j * w), m, false, lumaOrigin + (j * width), 2 * width, chromaOrigin + (j * cwidth), 2 * cwidth, alphaOffset, betaOffset, bs);
}
}
else
{
var pm = isField && bottom == 0 && field[aboveTop] ? aboveTop : aboveTop + w;
MbEdge(picture, pm, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
MbEdge<T>(picture, pm, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
}
}
}
InnerEdges(picture, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
InnerEdges<T>(picture, m, false, lumaOrigin, stride, chromaOrigin, cstride, alphaOffset, betaOffset, bs);
}
}
}
@@ -176,11 +217,13 @@ public sealed partial class H264Decoder
/// <summary>
/// The left edge of an MBAFF macroblock whose left pair is of the other kind (frame and field): bS and qP row by
/// row, from the macroblock holding each row of the left pair.
/// row, from the macroblock holding each row of the left pair. A chroma row takes the bS of a luma row: in 4:2:0 the
/// mapping of §8.7.2.1, in 4:2:2 and 4:4:4 (as many chroma rows as luma rows) the row itself.
/// </summary>
private void MixedLeftEdge(
private void MixedLeftEdge<T>(
Picture picture, int m, int leftTop, bool isField, bool bottom, int lumaOrigin, int stride, int chromaOrigin, int cstride,
int alphaOffset, int betaOffset, Span<byte> rowBs, Span<int> rowMb)
where T : unmanaged
{
var w = widthInMbs;
var leftField = picture.MbField[leftTop];
@@ -191,6 +234,7 @@ public sealed partial class H264Decoder
rowBs[r] = (byte)BoundaryStrength(picture, pm, (((yM & 15) >> 2) * 4) + 3, m, (r >> 2) * 4, true, true);
}
var luma = Plane<T>(deblockLuma);
for (var r = 0; r < 16; r++)
{
if (rowBs[r] == 0)
@@ -198,18 +242,22 @@ public sealed partial class H264Decoder
continue;
}
var qpav = (mbQp[rowMb[r]] + mbQp[m] + 1) >> 1;
var indexA = Math.Clamp(qpav + alphaOffset, 0, 51);
int alpha = H264Tables.Alpha[indexA], beta = H264Tables.Beta[Math.Clamp(qpav + betaOffset, 0, 51)];
if (alpha != 0 && beta != 0)
if (Thresholds(mbQp[rowMb[r]], mbQp[m], alphaOffset, betaOffset, bitDepthY, out var alpha, out var beta, out var indexA))
{
FilterLuma(picture.Y, lumaOrigin + (r * stride), 1, rowBs[r], alpha, beta, indexA);
FilterLuma(luma, lumaOrigin + (r * stride), 1, rowBs[r], alpha, beta, indexA, bitDepthY - 8, maxY);
}
}
for (var k = 0; k < 8; k++)
if (chromaArrayType == 0)
{
var lumaRow = isField && !leftField ? 2 * k : (4 * (k >> 1)) + (k & 1);
return;
}
var cb = Plane<T>(picture.Cb);
var cr = Plane<T>(picture.Cr);
for (var k = 0; k < mbHeightC; k++)
{
var lumaRow = mbHeightC == 16 ? k : isField && !leftField ? 2 * k : (4 * (k >> 1)) + (k & 1);
var strength = rowBs[lumaRow];
if (strength == 0)
{
@@ -219,20 +267,28 @@ public sealed partial class H264Decoder
var pm = rowMb[lumaRow];
for (var c = 0; c < 2; c++)
{
var qpav = (mbQpC[(pm * 2) + c] + mbQpC[(m * 2) + c] + 1) >> 1;
var indexA = Math.Clamp(qpav + alphaOffset, 0, 51);
int alpha = H264Tables.Alpha[indexA], beta = H264Tables.Beta[Math.Clamp(qpav + betaOffset, 0, 51)];
if (alpha != 0 && beta != 0)
if (!Thresholds(mbQpC[(pm * 2) + c], mbQpC[(m * 2) + c], alphaOffset, betaOffset, bitDepthC, out var alpha, out var beta, out var indexA))
{
FilterChroma(c == 0 ? picture.Cb : picture.Cr, chromaOrigin + (k * cstride), 1, strength, alpha, beta, indexA);
continue;
}
var plane = c == 0 ? cb : cr;
if (chromaArrayType == 3)
{
FilterLuma(plane, chromaOrigin + (k * cstride), 1, strength, alpha, beta, indexA, bitDepthC - 8, maxC);
}
else
{
FilterChroma(plane, chromaOrigin + (k * cstride), 1, strength, alpha, beta, indexA, bitDepthC - 8, maxC);
}
}
}
}
/// <summary>A macroblock edge between <paramref name="pm"/> (left or above) and <paramref name="qm"/>, luma and chroma.</summary>
private void MbEdge(
private void MbEdge<T>(
Picture picture, int pm, int qm, bool vertical, int lumaOrigin, int stride, int chromaOrigin, int cstride, int alphaOffset, int betaOffset, Span<byte> bs)
where T : unmanaged
{
for (var i = 0; i < 4; i++)
{
@@ -246,24 +302,44 @@ public sealed partial class H264Decoder
}
int along = vertical ? stride : 1, across = vertical ? 1 : stride;
LumaEdge(picture.Y, lumaOrigin, along, across, bs, mbQp[pm], mbQp[qm], alphaOffset, betaOffset);
Edge(Plane<T>(deblockLuma), lumaOrigin, along, across, bs, 16, mbQp[pm], mbQp[qm], alphaOffset, betaOffset, bitDepthY, chromaStyle: false);
if (chromaArrayType == 0)
{
return;
}
int calong = vertical ? cstride : 1, cacross = vertical ? 1 : cstride;
var count = vertical ? mbHeightC : mbWidthC;
for (var c = 0; c < 2; c++)
{
ChromaEdge(c == 0 ? picture.Cb : picture.Cr, chromaOrigin, calong, cacross, bs, mbQpC[(pm * 2) + c], mbQpC[(qm * 2) + c], alphaOffset, betaOffset);
Edge(Plane<T>(c == 0 ? picture.Cb : picture.Cr), chromaOrigin, calong, cacross, bs, count, mbQpC[(pm * 2) + c], mbQpC[(qm * 2) + c], alphaOffset, betaOffset, bitDepthC, chromaStyle: chromaArrayType != 3);
}
}
/// <summary>The internal edges of macroblock <paramref name="m"/> in one direction (luma 4, 8, 12; chroma 4).</summary>
private void InnerEdges(
/// <summary>
/// The internal edges of macroblock <paramref name="m"/> in one direction: luma 4, 8, 12 (only 8 with the 8×8
/// transform); chroma at the same luma positions where they fall on a 4×4 chroma block edge — 4 for 4:2:0, the
/// rows 4, 8 and 12 of 4:2:2 (whatever the transform of luma), all the luma edges in 4:4:4.
/// </summary>
private void InnerEdges<T>(
Picture picture, int m, bool vertical, int lumaOrigin, int stride, int chromaOrigin, int cstride, int alphaOffset, int betaOffset, Span<byte> bs)
where T : unmanaged
{
var t8 = mbT8[m];
int along = vertical ? stride : 1, across = vertical ? 1 : stride;
int calong = vertical ? cstride : 1, cacross = vertical ? 1 : cstride;
var chromaSize = vertical ? mbWidthC : mbHeightC;
var count = vertical ? mbHeightC : mbWidthC;
for (var edge = 1; edge < 4; edge++)
{
if (t8 && (edge & 1) != 0)
var lumaEdge = !t8 || (edge & 1) == 0;
var chromaEdge = chromaArrayType switch
{
0 => false,
3 => lumaEdge,
_ => chromaSize == 16 || edge == 2,
};
if (!lumaEdge && !chromaEdge)
{
continue;
}
@@ -279,55 +355,67 @@ public sealed partial class H264Decoder
continue;
}
var offset = edge * 4 * (vertical ? 1 : stride);
LumaEdge(picture.Y, lumaOrigin + offset, along, across, bs, mbQp[m], mbQp[m], alphaOffset, betaOffset);
if (edge == 2)
if (lumaEdge)
{
var coffset = 4 * (vertical ? 1 : cstride);
var offset = edge * 4 * (vertical ? 1 : stride);
Edge(Plane<T>(deblockLuma), lumaOrigin + offset, along, across, bs, 16, mbQp[m], mbQp[m], alphaOffset, betaOffset, bitDepthY, chromaStyle: false);
}
if (chromaEdge)
{
var position = edge * chromaSize / 4;
var coffset = position * (vertical ? 1 : cstride);
for (var c = 0; c < 2; c++)
{
ChromaEdge(c == 0 ? picture.Cb : picture.Cr, chromaOrigin + coffset, calong, cacross, bs, mbQpC[(m * 2) + c], mbQpC[(m * 2) + c], alphaOffset, betaOffset);
Edge(Plane<T>(c == 0 ? picture.Cb : picture.Cr), chromaOrigin + coffset, calong, cacross, bs, count, mbQpC[(m * 2) + c], mbQpC[(m * 2) + c], alphaOffset, betaOffset, bitDepthC, chromaStyle: chromaArrayType != 3);
}
}
}
}
private static void LumaEdge(byte[] plane, int origin, int along, int across, ReadOnlySpan<byte> bs, int qpP, int qpQ, int alphaOffset, int betaOffset)
/// <summary>
/// α, β and indexA of an edge (§8.7.2.2) from the qP of its two sides, scaled to the bit depth (equations 8-466,
/// 8-467); false when the edge is not filtered.
/// </summary>
private static bool Thresholds(int qpP, int qpQ, int alphaOffset, int betaOffset, int bitDepth, out int alpha, out int beta, out int indexA)
{
var qpav = (qpP + qpQ + 1) >> 1;
var indexA = Math.Clamp(qpav + alphaOffset, 0, 51);
int alpha = H264Tables.Alpha[indexA], beta = H264Tables.Beta[Math.Clamp(qpav + betaOffset, 0, 51)];
if (alpha == 0 || beta == 0)
{
return;
}
for (var k = 0; k < 16; k++)
{
var strength = bs[k >> 2];
if (strength != 0)
{
FilterLuma(plane, origin + (k * along), across, strength, alpha, beta, indexA);
}
}
indexA = Math.Clamp(qpav + alphaOffset, 0, 51);
alpha = H264Tables.Alpha[indexA] << (bitDepth - 8);
beta = H264Tables.Beta[Math.Clamp(qpav + betaOffset, 0, 51)] << (bitDepth - 8);
return alpha != 0 && beta != 0;
}
private static void ChromaEdge(byte[] plane, int origin, int along, int across, ReadOnlySpan<byte> bs, int qpP, int qpQ, int alphaOffset, int betaOffset)
/// <summary>
/// The <paramref name="count"/> samples along one edge (16 in luma; 8 or 16 in chroma), each with the bS of its 4×4
/// luma block; the filter of chroma (chromaStyleFilteringFlag) or that of luma, which 4:4:4 uses for Cb and Cr.
/// </summary>
private static void Edge<T>(Span<T> plane, int origin, int along, int across, ReadOnlySpan<byte> bs, int count, int qpP, int qpQ, int alphaOffset, int betaOffset, int bitDepth, bool chromaStyle)
where T : unmanaged
{
var qpav = (qpP + qpQ + 1) >> 1;
var indexA = Math.Clamp(qpav + alphaOffset, 0, 51);
int alpha = H264Tables.Alpha[indexA], beta = H264Tables.Beta[Math.Clamp(qpav + betaOffset, 0, 51)];
if (alpha == 0 || beta == 0)
if (!Thresholds(qpP, qpQ, alphaOffset, betaOffset, bitDepth, out var alpha, out var beta, out var indexA))
{
return;
}
for (var k = 0; k < 8; k++)
var shift = count == 16 ? 2 : 1;
var bdShift = bitDepth - 8;
var max = (1 << bitDepth) - 1;
for (var k = 0; k < count; k++)
{
var strength = bs[k >> 1];
if (strength != 0)
var strength = bs[k >> shift];
if (strength == 0)
{
FilterChroma(plane, origin + (k * along), across, strength, alpha, beta, indexA);
continue;
}
if (chromaStyle)
{
FilterChroma(plane, origin + (k * along), across, strength, alpha, beta, indexA, bdShift, max);
}
else
{
FilterLuma(plane, origin + (k * along), across, strength, alpha, beta, indexA, bdShift, max);
}
}
}
@@ -417,51 +505,71 @@ public sealed partial class H264Decoder
private static bool MvFar(short[] mv, int a, int b, int verticalLimit) =>
Math.Abs(mv[a] - mv[b]) >= 4 || Math.Abs(mv[a + 1] - mv[b + 1]) >= verticalLimit;
/// <summary>
/// The transform block holding the 4×4 luma block <paramref name="blk"/> has coefficients; in 4:4:4 (not coded as
/// separate planes) those of the Cb and Cr blocks at the same place count too.
/// </summary>
private bool HasCoefficients(int m, int blk)
{
var baseIndex = m * 24;
if (!mbT8[m])
var planes = chromaArrayType == 3 ? 3 : 1;
for (var plane = 0; plane < planes; plane++)
{
return nz[baseIndex + blk] != 0;
var baseIndex = (m * NzPerMb) + (plane * 16);
if (!mbT8[m])
{
if (nz[baseIndex + blk] != 0)
{
return true;
}
continue;
}
// The 8×8 transform block containing the 4×4 block.
var r0 = ((blk >> 3) * 8) + (((blk & 3) >> 1) * 2);
if ((nz[baseIndex + r0] | nz[baseIndex + r0 + 1] | nz[baseIndex + r0 + 4] | nz[baseIndex + r0 + 5]) != 0)
{
return true;
}
}
// The 8×8 transform block containing the 4×4 block.
var r0 = ((blk >> 3) * 8) + (((blk & 3) >> 1) * 2);
return (nz[baseIndex + r0] | nz[baseIndex + r0 + 1] | nz[baseIndex + r0 + 4] | nz[baseIndex + r0 + 5]) != 0;
return false;
}
private static void FilterLuma(byte[] s, int o, int step, int bS, int alpha, int beta, int indexA)
/// <summary>The filter of luma edges (§8.7.2.3, §8.7.2.4), also that of Cb and Cr in 4:4:4; tC0 scaled by <paramref name="bdShift"/>.</summary>
private static void FilterLuma<T>(Span<T> s, int o, int step, int bS, int alpha, int beta, int indexA, int bdShift, int max)
where T : unmanaged
{
int p0 = s[o - step], q0 = s[o];
int p0 = Get(s[o - step]), q0 = Get(s[o]);
if (Math.Abs(p0 - q0) >= alpha)
{
return;
}
int p1 = s[o - (2 * step)], q1 = s[o + step];
int p1 = Get(s[o - (2 * step)]), q1 = Get(s[o + step]);
if (Math.Abs(p1 - p0) >= beta || Math.Abs(q1 - q0) >= beta)
{
return;
}
int p2 = s[o - (3 * step)], q2 = s[o + (2 * step)];
int p2 = Get(s[o - (3 * step)]), q2 = Get(s[o + (2 * step)]);
var ap = Math.Abs(p2 - p0) < beta;
var aq = Math.Abs(q2 - q0) < beta;
if (bS < 4)
{
int tc0 = H264Tables.Tc0[(indexA * 3) + bS - 1];
var tc0 = H264Tables.Tc0[(indexA * 3) + bS - 1] << bdShift;
var tc = tc0 + (ap ? 1 : 0) + (aq ? 1 : 0);
var delta = Math.Clamp((((q0 - p0) << 2) + (p1 - q1) + 4) >> 3, -tc, tc);
s[o - step] = Clip(p0 + delta);
s[o] = Clip(q0 - delta);
s[o - step] = Make<T>(Clip(p0 + delta, max));
s[o] = Make<T>(Clip(q0 - delta, max));
if (ap)
{
s[o - (2 * step)] = (byte)(p1 + Math.Clamp((p2 + ((p0 + q0 + 1) >> 1) - (p1 << 1)) >> 1, -tc0, tc0));
s[o - (2 * step)] = Make<T>(p1 + Math.Clamp((p2 + ((p0 + q0 + 1) >> 1) - (p1 << 1)) >> 1, -tc0, tc0));
}
if (aq)
{
s[o + step] = (byte)(q1 + Math.Clamp((q2 + ((p0 + q0 + 1) >> 1) - (q1 << 1)) >> 1, -tc0, tc0));
s[o + step] = Make<T>(q1 + Math.Clamp((q2 + ((p0 + q0 + 1) >> 1) - (q1 << 1)) >> 1, -tc0, tc0));
}
return;
@@ -470,38 +578,40 @@ public sealed partial class H264Decoder
var strong = Math.Abs(p0 - q0) < ((alpha >> 2) + 2);
if (ap && strong)
{
int p3 = s[o - (4 * step)];
s[o - step] = (byte)((p2 + (2 * p1) + (2 * p0) + (2 * q0) + q1 + 4) >> 3);
s[o - (2 * step)] = (byte)((p2 + p1 + p0 + q0 + 2) >> 2);
s[o - (3 * step)] = (byte)(((2 * p3) + (3 * p2) + p1 + p0 + q0 + 4) >> 3);
var p3 = Get(s[o - (4 * step)]);
s[o - step] = Make<T>((p2 + (2 * p1) + (2 * p0) + (2 * q0) + q1 + 4) >> 3);
s[o - (2 * step)] = Make<T>((p2 + p1 + p0 + q0 + 2) >> 2);
s[o - (3 * step)] = Make<T>(((2 * p3) + (3 * p2) + p1 + p0 + q0 + 4) >> 3);
}
else
{
s[o - step] = (byte)(((2 * p1) + p0 + q1 + 2) >> 2);
s[o - step] = Make<T>(((2 * p1) + p0 + q1 + 2) >> 2);
}
if (aq && strong)
{
int q3 = s[o + (3 * step)];
s[o] = (byte)((p1 + (2 * p0) + (2 * q0) + (2 * q1) + q2 + 4) >> 3);
s[o + step] = (byte)((p0 + q0 + q1 + q2 + 2) >> 2);
s[o + (2 * step)] = (byte)(((2 * q3) + (3 * q2) + q1 + q0 + p0 + 4) >> 3);
var q3 = Get(s[o + (3 * step)]);
s[o] = Make<T>((p1 + (2 * p0) + (2 * q0) + (2 * q1) + q2 + 4) >> 3);
s[o + step] = Make<T>((p0 + q0 + q1 + q2 + 2) >> 2);
s[o + (2 * step)] = Make<T>(((2 * q3) + (3 * q2) + q1 + q0 + p0 + 4) >> 3);
}
else
{
s[o] = (byte)(((2 * q1) + q0 + p1 + 2) >> 2);
s[o] = Make<T>(((2 * q1) + q0 + p1 + 2) >> 2);
}
}
private static void FilterChroma(byte[] s, int o, int step, int bS, int alpha, int beta, int indexA)
/// <summary>The filter of the chroma edges of 4:2:0 and 4:2:2 (chromaStyleFilteringFlag).</summary>
private static void FilterChroma<T>(Span<T> s, int o, int step, int bS, int alpha, int beta, int indexA, int bdShift, int max)
where T : unmanaged
{
int p0 = s[o - step], q0 = s[o];
int p0 = Get(s[o - step]), q0 = Get(s[o]);
if (Math.Abs(p0 - q0) >= alpha)
{
return;
}
int p1 = s[o - (2 * step)], q1 = s[o + step];
int p1 = Get(s[o - (2 * step)]), q1 = Get(s[o + step]);
if (Math.Abs(p1 - p0) >= beta || Math.Abs(q1 - q0) >= beta)
{
return;
@@ -509,16 +619,14 @@ public sealed partial class H264Decoder
if (bS < 4)
{
var tc = H264Tables.Tc0[(indexA * 3) + bS - 1] + 1;
var tc = (H264Tables.Tc0[(indexA * 3) + bS - 1] << bdShift) + 1;
var delta = Math.Clamp((((q0 - p0) << 2) + (p1 - q1) + 4) >> 3, -tc, tc);
s[o - step] = Clip(p0 + delta);
s[o] = Clip(q0 - delta);
s[o - step] = Make<T>(Clip(p0 + delta, max));
s[o] = Make<T>(Clip(q0 - delta, max));
return;
}
s[o - step] = (byte)(((2 * p1) + p0 + q1 + 2) >> 2);
s[o] = (byte)(((2 * q1) + q0 + p1 + 2) >> 2);
s[o - step] = Make<T>(((2 * p1) + p0 + q1 + 2) >> 2);
s[o] = Make<T>(((2 * q1) + q0 + p1 + 2) >> 2);
}
private static byte Clip(int v) => (byte)(v < 0 ? 0 : v > 255 ? 255 : v);
}