namespace Febe.Core.Codecs.H264;
///
/// The reconstruction of the P macroblocks of SP slices and of the SI macroblocks (§8.6): the prediction is transformed
/// and quantised with QS together with the received levels, so that a switching picture rebuilds exactly the same
/// samples whichever stream it was reached from. The block is then decoded with QS as usual, and the result replaces the
/// prediction instead of being added to it.
///
internal static class SwitchingTransform
{
/// Quantisation factors (the inverse of normAdjust4x4) by QS % 6 and class of position (0 even-even, 1 odd-odd, 2 others).
private static readonly int[,] Quant =
{
{ 13107, 5243, 8066 }, { 11916, 4660, 7490 }, { 10082, 4194, 6554 }, { 9362, 3647, 5825 }, { 8192, 3355, 5243 }, { 7282, 2893, 4559 },
};
/// Aij of §8.6.1 by class of position.
private static readonly int[] Norm = [16, 25, 20];
/// The class of each raster position of a 4×4 block (0 even row and column, 1 both odd, 2 the others).
private static readonly byte[] Class = [0, 2, 0, 2, 2, 1, 2, 1, 0, 2, 0, 2, 2, 1, 2, 1];
///
/// A luma 4×4 block: the prediction at is replaced by the reconstruction from it and the levels
/// (raster order, not scaled). is set for SI macroblocks and for SP slices with
/// sp_for_switch_flag: the levels are then added to the quantised prediction (§8.6.2) instead of the scaled ones to the
/// transformed prediction (§8.6.1).
///
internal static void Luma4x4(byte[] plane, int o, int stride, ReadOnlySpan levels, int qp, int qs, bool levelInQs)
{
Span c = stackalloc int[16];
Forward(plane, o, stride, c);
int qpMod = qp % 6, qpDiv = qp / 6, qsMod = qs % 6, qsDiv = qs / 6;
for (var pos = 0; pos < 16; pos++)
{
var cls = Class[pos];
var level = Requantise(c[pos], levels[pos], cls, qpMod, qpDiv, qsMod, qsDiv, levelInQs, dc: false);
c[pos] = (level * H264Tables.NormAdjust4x4[qsMod, cls]) << qsDiv;
}
Store4x4(c, plane, o, stride);
}
///
/// One chroma component (four 4×4 blocks): holds the AC levels of the four blocks (block × 16
/// + raster position; position 0 is ignored), the four DC levels in the order they are parsed.
///
internal static void Chroma(byte[] plane, int o, int stride, ReadOnlySpan ac, ReadOnlySpan dc, int qpc, int qsc, bool levelInQs)
{
Span c = stackalloc int[64];
for (var b = 0; b < 4; b++)
{
Forward(plane, BlockOffset(o, stride, b), stride, c.Slice(b * 16, 16));
}
int qpMod = qpc % 6, qpDiv = qpc / 6, qsMod = qsc % 6, qsDiv = qsc / 6;
// The DC terms of the prediction through the 2×2 transform, combined with the levels, back through it (§8.6.1).
int p0 = c[0], p1 = c[16], p2 = c[32], p3 = c[48];
Span q = [p0 + p1 + p2 + p3, p0 - p1 + p2 - p3, p0 + p1 - p2 - p3, p0 - p1 - p2 + p3];
for (var k = 0; k < 4; k++)
{
q[k] = Requantise(q[k], dc[k], 0, qpMod, qpDiv, qsMod, qsDiv, levelInQs, dc: true);
}
int f0 = q[0] + q[1] + q[2] + q[3], f1 = q[0] - q[1] + q[2] - q[3], f2 = q[0] + q[1] - q[2] - q[3], f3 = q[0] - q[1] - q[2] + q[3];
var scaleDc = 16 * H264Tables.NormAdjust4x4[qsMod, 0];
c[0] = ((f0 * scaleDc) << qsDiv) >> 5;
c[16] = ((f1 * scaleDc) << qsDiv) >> 5;
c[32] = ((f2 * scaleDc) << qsDiv) >> 5;
c[48] = ((f3 * scaleDc) << qsDiv) >> 5;
for (var b = 0; b < 4; b++)
{
var block = c.Slice(b * 16, 16);
for (var pos = 1; pos < 16; pos++)
{
var cls = Class[pos];
var level = Requantise(block[pos], ac[(b * 16) + pos], cls, qpMod, qpDiv, qsMod, qsDiv, levelInQs, dc: false);
block[pos] = (level * H264Tables.NormAdjust4x4[qsMod, cls]) << qsDiv;
}
Store4x4(block, plane, BlockOffset(o, stride, b), stride);
}
}
///
/// The level in QS of one coefficient from the transformed prediction and the received
/// level: equations 8-401 to 8-404 of §8.6.1 (with QP), or 8-410 to 8-411 of §8.6.2 (in QS). The DC terms of
/// chroma carry one bit more of scaling.
///
private static int Requantise(int predicted, int level, int cls, int qpMod, int qpDiv, int qsMod, int qsDiv, bool levelInQs, bool dc)
{
var extra = dc ? 1 : 0;
var shift = 15 + qsDiv + extra;
var round = 1L << (shift - 1);
var quant = Quant[qsMod, cls];
if (levelInQs)
{
var magnitude = (int)(((Math.Abs((long)predicted) * quant) + round) >> shift);
return level + (predicted < 0 ? -magnitude : magnitude);
}
var sum = predicted + ((((long)level * H264Tables.NormAdjust4x4[qpMod, cls] * Norm[cls]) << qpDiv) >> (6 - extra));
var result = (int)Math.Min(((Math.Abs(sum) * quant) + round) >> shift, int.MaxValue / 64);
return sum < 0 ? -result : result;
}
private static int BlockOffset(int o, int stride, int b) => o + ((b >> 1) * 4 * stride) + ((b & 1) * 4);
/// The forward 4×4 core transform of the samples at (§8.6.1, cp), raster order.
private static void Forward(byte[] plane, int o, int stride, Span c)
{
for (var i = 0; i < 4; i++)
{
var row = o + (i * stride);
int x0 = plane[row], x1 = plane[row + 1], x2 = plane[row + 2], x3 = plane[row + 3];
int a = x0 + x3, b = x1 + x2, d = x1 - x2, e = x0 - x3;
c[i * 4] = a + b;
c[(i * 4) + 1] = (2 * e) + d;
c[(i * 4) + 2] = a - b;
c[(i * 4) + 3] = e - (2 * d);
}
for (var j = 0; j < 4; j++)
{
int x0 = c[j], x1 = c[4 + j], x2 = c[8 + j], x3 = c[12 + j];
int a = x0 + x3, b = x1 + x2, d = x1 - x2, e = x0 - x3;
c[j] = a + b;
c[4 + j] = (2 * e) + d;
c[8 + j] = a - b;
c[12 + j] = e - (2 * d);
}
}
/// The inverse 4×4 transform of scaled coefficients (§8.5.12), stored at in place of the prediction.
private static void Store4x4(Span d, byte[] plane, int o, int stride)
{
for (var i = 0; i < 4; i++)
{
var r = d.Slice(i * 4, 4);
int e0 = r[0] + r[2], e1 = r[0] - r[2], e2 = (r[1] >> 1) - r[3], e3 = r[1] + (r[3] >> 1);
r[0] = e0 + e3;
r[1] = e1 + e2;
r[2] = e1 - e2;
r[3] = e0 - e3;
}
for (var j = 0; j < 4; j++)
{
int f0 = d[j], f1 = d[4 + j], f2 = d[8 + j], f3 = d[12 + j];
int g0 = f0 + f2, g1 = f0 - f2, g2 = (f1 >> 1) - f3, g3 = f1 + (f3 >> 1);
plane[o + j] = Clip((g0 + g3 + 32) >> 6);
plane[o + stride + j] = Clip((g1 + g2 + 32) >> 6);
plane[o + (2 * stride) + j] = Clip((g1 - g2 + 32) >> 6);
plane[o + (3 * stride) + j] = Clip((g0 - g3 + 32) >> 6);
}
}
private static byte Clip(int v) => (byte)(v < 0 ? 0 : v > 255 ? 255 : v);
}