854bdeda20
Adds the H.264 §8.5.11.1 chroma DC Hadamard transform. In 4:2:0
chroma, the four DC coefficients (one from each chroma 4x4 AC block
within an MB) go through a 2x2 Hadamard before quant-scaling and
before being added back to each block's [0,0] coefficient prior to
the 4x4 AC IDCT.
This PR ships the pure Hadamard transform:
f[0,0] = c[0,0] + c[0,1] + c[1,0] + c[1,1]
f[0,1] = c[0,0] - c[0,1] + c[1,0] - c[1,1]
f[1,0] = c[0,0] + c[0,1] - c[1,0] - c[1,1]
f[1,1] = c[0,0] - c[0,1] - c[1,0] + c[1,1]
implemented as the 2-stage row+col butterfly (1:1 with the NEON
SIMD shape upstream). Operates in-place on int16[4].
What this does NOT do (deferred to caller-side composition):
- QP-dependent scaling per §8.5.11.2. The scale depends on
QP_C (with chroma_qp_offset adjustment), so the formula has
branches (>=6 vs <6) and looks up LevelScale4x4 table values.
The libavcodec intercept patch composes Hadamard + scale +
shift itself since the scale shape varies by codec-level
context (slice header chroma_qp_offset, PPS chroma_qp_offset,
second_chroma_qp_offset for the chroma_qp_index_offset).
- Inverse transform (decode-time used for the FORWARD direction
is the same Hadamard up to scaling, but conceptually the spec
distinguishes them in §8.5.11; we expose only the matrix).
Test design (tests/test_chroma_dc_hadamard.c):
7 cases, all spec-derived hand-computations:
- all-uniform 5 → [20, 0, 0, 0]
- col gradient [0,10,0,10] → [20, -20, 0, 0]
- row gradient [0,0,10,10] → [20, 0, -20, 0]
- anti-diagonal [10,0,0,10] → [20, 0, 0, 20]
- asymmetric [1,2,3,4] → [10, -2, -4, 0]
- sign-alternating [-5,5,-5,5] → [0, -20, 0, 0]
- double-Hadamard invariant: H·H = 4·I, so applying twice
gives [4*c[0], 4*c[1], 4*c[2], 4*c[3]] for any input.
The double-Hadamard test is the strongest correctness gate: any
single sign error in the butterfly would break the H·H = 4·I
algebraic property, surfacing immediately. All 7 PASS first try.
Verified on hertz:
$ ./build/test_chroma_dc_hadamard
all-uniform 5 PASS
col gradient [0,10,0,10] PASS
row gradient [0,0,10,10] PASS
anti-diagonal [10,0,0,10] PASS
asymmetric [1,2,3,4] PASS
sign-alternating [-5,5,-5,5] PASS
double-Hadamard = 4*orig PASS
ALL chroma DC Hadamard tests PASS
With this primitive the H.264 8-bit 4:2:0 pixel-math primitive
matrix is complete in fourier:
- IDCT 4x4 (luma + chroma) ✓
- IDCT 8x8 (luma, High profile) ✓
- Chroma DC Hadamard 2x2 ✓ (this PR)
- Deblock (8 variants) ✓
- Intra prediction (26 modes) ✓
- MC qpel (30 dispatches) ✓
What remains for the libavcodec intercept patch: CABAC/CAVLC entropy
decode, SPS/PPS parsing, slice header parsing, MB type / QP / CBP /
intra mode prediction. All of that lives at the intercept layer
(it's spec-derived from the bitstream syntax, not pixel-math); the
intercept patch will call into these fourier primitives once the
metadata is decoded.
119 lines
3.7 KiB
C
119 lines
3.7 KiB
C
/*
|
|
* Tests the H.264 chroma DC 2x2 Hadamard primitive against
|
|
* spec-derived expected outputs.
|
|
*
|
|
* f[0,0] = c[0,0] + c[0,1] + c[1,0] + c[1,1] "sum"
|
|
* f[0,1] = c[0,0] - c[0,1] + c[1,0] - c[1,1] "col-diff"
|
|
* f[1,0] = c[0,0] + c[0,1] - c[1,0] - c[1,1] "row-diff"
|
|
* f[1,1] = c[0,0] - c[0,1] - c[1,0] + c[1,1] "anti-diag"
|
|
*/
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
extern void daedalus_h264_chroma_dc_hadamard_2x2_ref(int16_t c[4]);
|
|
|
|
static int check(const char *name, int16_t in[4], int16_t expect[4])
|
|
{
|
|
int16_t c[4]; memcpy(c, in, sizeof(c));
|
|
daedalus_h264_chroma_dc_hadamard_2x2_ref(c);
|
|
int fail = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
if (c[i] != expect[i]) {
|
|
fprintf(stderr, "%s: c[%d] = %d, expected %d\n",
|
|
name, i, c[i], expect[i]);
|
|
fail = 1;
|
|
}
|
|
}
|
|
if (!fail) printf(" %-32s PASS\n", name);
|
|
else printf(" %-32s FAIL\n", name);
|
|
return fail;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
int fail = 0;
|
|
|
|
/* Test 1: All-same input.
|
|
* c = [5, 5, 5, 5]
|
|
* f[0,0] = 20, f[0,1] = 0, f[1,0] = 0, f[1,1] = 0
|
|
*/
|
|
{ int16_t in[4] = { 5, 5, 5, 5 };
|
|
int16_t ex[4] = { 20, 0, 0, 0 };
|
|
fail |= check("all-uniform 5", in, ex); }
|
|
|
|
/* Test 2: Single-axis variation (col 1 = 0, col 2 = 10).
|
|
* c = [0, 10, 0, 10]
|
|
* f[0,0] = 0+10+0+10 = 20
|
|
* f[0,1] = 0-10+0-10 = -20
|
|
* f[1,0] = 0+10-0-10 = 0
|
|
* f[1,1] = 0-10-0+10 = 0
|
|
*/
|
|
{ int16_t in[4] = { 0, 10, 0, 10 };
|
|
int16_t ex[4] = { 20, -20, 0, 0 };
|
|
fail |= check("col gradient [0,10,0,10]", in, ex); }
|
|
|
|
/* Test 3: Row gradient.
|
|
* c = [0, 0, 10, 10]
|
|
* f[0,0] = 20, f[0,1] = 0, f[1,0] = 0-20 = -20, f[1,1] = 0
|
|
*/
|
|
{ int16_t in[4] = { 0, 0, 10, 10 };
|
|
int16_t ex[4] = { 20, 0, -20, 0 };
|
|
fail |= check("row gradient [0,0,10,10]", in, ex); }
|
|
|
|
/* Test 4: Anti-diagonal pattern.
|
|
* c = [10, 0, 0, 10]
|
|
* f[0,0] = 20
|
|
* f[0,1] = 10-0+0-10 = 0
|
|
* f[1,0] = 10+0-0-10 = 0
|
|
* f[1,1] = 10-0-0+10 = 20
|
|
*/
|
|
{ int16_t in[4] = { 10, 0, 0, 10 };
|
|
int16_t ex[4] = { 20, 0, 0, 20 };
|
|
fail |= check("anti-diagonal [10,0,0,10]", in, ex); }
|
|
|
|
/* Test 5: Asymmetric — all bands non-zero.
|
|
* c = [1, 2, 3, 4]
|
|
* f[0,0] = 10
|
|
* f[0,1] = 1-2+3-4 = -2
|
|
* f[1,0] = 1+2-3-4 = -4
|
|
* f[1,1] = 1-2-3+4 = 0
|
|
*/
|
|
{ int16_t in[4] = { 1, 2, 3, 4 };
|
|
int16_t ex[4] = { 10, -2, -4, 0 };
|
|
fail |= check("asymmetric [1,2,3,4]", in, ex); }
|
|
|
|
/* Test 6: Negative inputs (Hadamard is linear, so signs preserve).
|
|
* c = [-5, 5, -5, 5]
|
|
* f[0,0] = -5+5-5+5 = 0
|
|
* f[0,1] = -5-5-5-5 = -20
|
|
* f[1,0] = -5+5+5-5 = 0
|
|
* f[1,1] = -5-5+5+5 = 0
|
|
*/
|
|
{ int16_t in[4] = { -5, 5, -5, 5 };
|
|
int16_t ex[4] = { 0, -20, 0, 0 };
|
|
fail |= check("sign-alternating [-5,5,-5,5]", in, ex); }
|
|
|
|
/* Test 7: Inverse-property check. H * H = 4*I for the unscaled
|
|
* 2x2 Hadamard. So applying twice multiplies each by 4.
|
|
* c = [1, 2, 3, 4]
|
|
* First Hadamard: [10, -2, -4, 0]
|
|
* Second Hadamard: [4, 8, 12, 16]
|
|
*/
|
|
{ int16_t in[4] = { 1, 2, 3, 4 };
|
|
int16_t ex[4] = { 4, 8, 12, 16 };
|
|
int16_t c[4]; memcpy(c, in, sizeof(c));
|
|
daedalus_h264_chroma_dc_hadamard_2x2_ref(c);
|
|
daedalus_h264_chroma_dc_hadamard_2x2_ref(c);
|
|
int local_fail = 0;
|
|
for (int i = 0; i < 4; i++) if (c[i] != ex[i]) local_fail = 1;
|
|
printf(" %-32s %s\n", "double-Hadamard = 4*orig",
|
|
local_fail ? "FAIL" : "PASS");
|
|
fail |= local_fail;
|
|
}
|
|
|
|
if (fail == 0) printf("\nALL chroma DC Hadamard tests PASS\n");
|
|
else fprintf(stderr, "\n%d test(s) FAILED\n", fail);
|
|
return fail ? 1 : 0;
|
|
}
|