Files
Febe/src/Febe.Core/Codecs/H264/SwitchingTransform.cs
T
Alby96andClaude Opus 5.5 8d03ca23de Decodificatore H.264 per gli strumenti di Baseline ed Extended: FMO, ASO, slice ridondanti, partizionamento dei dati, slice SP e SI (#58)
Il decodificatore rifiutava i flussi con gruppi di slice e con slice SP o SI, e
non leggeva le partizioni A, B e C: ora li gestisce tutti, perché sono parte di
Baseline ed Extended e un file della libreria può usarli.

- Gruppi di slice dal PPS, tipi da 0 a 6, con il cambio per immagine di
  slice_group_change_cycle; il macroblocco successivo segue il gruppo (§8.2.2).
- Slice in ordine qualunque: la disponibilità dei vicini era già per slice, il
  deblocking a fine immagine.
- Slice SP e SI con QS (§8.6): predizione trasformata e riquantizzata in QS, anche
  per P_Skip; sp_for_switch_flag e macroblocchi SI; nel deblocking contano come
  intra.
- Partizionamento: A con intestazione e dati di categoria 2, B con il residuo
  intra (e gli I_PCM), C con quello inter; la regola del nN a zero con la
  predizione intra vincolata (§9.2.1). Una partizione che manca lascia a zero il
  suo residuo e, da lì in poi, quello della partizione che ne dipende.

I cinque flussi FMO, ASO e SP della serie ITU-T (FM2_SVA_C, FM1_FT_E, FM1_BT_B,
sp1_bt_a, sp2_bt_b) passano fra i verificati e coincidono fotogramma per
fotogramma. Partizionamento, slice ridondanti e ASO si provano riscrivendo flussi
verificati senza toccare i bit dei macroblocchi (StreamRewriter) e chiedendo di
nuovo il riferimento ufficiale; le SI con un flusso sintetico calcolato a mano.
Anche i flussi nuovi e quelli partizionati sono provati rovinati.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 16:42:11 +01:00

162 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace Febe.Core.Codecs.H264;
/// <summary>
/// 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.
/// </summary>
internal static class SwitchingTransform
{
/// <summary>Quantisation factors (the inverse of normAdjust4x4) by QS % 6 and class of position (0 even-even, 1 odd-odd, 2 others).</summary>
private static readonly int[,] Quant =
{
{ 13107, 5243, 8066 }, { 11916, 4660, 7490 }, { 10082, 4194, 6554 }, { 9362, 3647, 5825 }, { 8192, 3355, 5243 }, { 7282, 2893, 4559 },
};
/// <summary>A<sub>ij</sub> of §8.6.1 by class of position.</summary>
private static readonly int[] Norm = [16, 25, 20];
/// <summary>The class of each raster position of a 4×4 block (0 even row and column, 1 both odd, 2 the others).</summary>
private static readonly byte[] Class = [0, 2, 0, 2, 2, 1, 2, 1, 0, 2, 0, 2, 2, 1, 2, 1];
/// <summary>
/// A luma 4×4 block: the prediction at <paramref name="o"/> is replaced by the reconstruction from it and the levels
/// (raster order, not scaled). <paramref name="levelInQs"/> 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).
/// </summary>
internal static void Luma4x4(byte[] plane, int o, int stride, ReadOnlySpan<int> levels, int qp, int qs, bool levelInQs)
{
Span<int> 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);
}
/// <summary>
/// One chroma component (four 4×4 blocks): <paramref name="ac"/> holds the AC levels of the four blocks (block × 16
/// + raster position; position 0 is ignored), <paramref name="dc"/> the four DC levels in the order they are parsed.
/// </summary>
internal static void Chroma(byte[] plane, int o, int stride, ReadOnlySpan<int> ac, ReadOnlySpan<int> dc, int qpc, int qsc, bool levelInQs)
{
Span<int> 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<int> 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);
}
}
/// <summary>
/// The level in QS of one coefficient from the transformed prediction <paramref name="predicted"/> 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.
/// </summary>
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);
/// <summary>The forward 4×4 core transform of the samples at <paramref name="o"/> (§8.6.1, c<sup>p</sup>), raster order.</summary>
private static void Forward(byte[] plane, int o, int stride, Span<int> 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);
}
}
/// <summary>The inverse 4×4 transform of scaled coefficients (§8.5.12), stored at <paramref name="o"/> in place of the prediction.</summary>
private static void Store4x4(Span<int> 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);
}