oxideav-h264 0.1.9

Pure-Rust H.264 / AVC (ITU-T H.264 | ISO/IEC 14496-10) codec for oxideav — under spec-driven rewrite, no decode/encode functionality yet
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! §8.6 — Decoding process for P macroblocks in SP slices or SI
//! macroblocks.
//!
//! SP slices carry Inter-predicted macroblocks whose reconstruction is
//! defined **in the transform domain**: the prediction samples are
//! forward-transformed (eq. 8-415), combined with the transmitted
//! residual coefficients, re-quantised with the slice's QSY / QSC
//! (eq. 7-33 `QSY = 26 + pic_init_qs_minus26 + slice_qs_delta`), and
//! only then run through the ordinary §8.5.12 scaling + inverse
//! transform. This makes the reconstructed samples a function of the
//! *quantised* transform coefficients alone — which is what allows a
//! switching picture (sp_for_switch_flag == 1, §8.6.2) or an SI picture
//! to reproduce a bit-identical reconstruction from a *different*
//! prediction (different reference pictures, or intra prediction).
//!
//! This module holds the pure coefficient-domain math of §8.6:
//!
//! * eq. 8-415 forward 4x4 core transform of prediction samples,
//! * eq. 8-417 `Aij` and eqs. 8-418/8-419 `LevelScale2(m, i, j)`,
//! * §8.6.1 (non-switching: eqs. 8-416, 8-420, 8-424, 8-425 and the
//!   chroma DC chain 8-427..8-431),
//! * §8.6.2 (switching / SI: eqs. 8-432, 8-433, 8-435, 8-437 and the
//!   chroma DC chain 8-439..8-441).
//!
//! The outputs of these helpers are 4x4 coefficient arrays `c` ready
//! for the §8.5.12 "scaling and transformation process for residual
//! 4x4 blocks" invoked with qP = QSY (luma) / QSC (chroma) per
//! eqs. 8-331 / 8-333 (`sMbFlag == 1`), followed by `uij = Clip1(rij)`
//! (eqs. 8-421 / 8-426 / 8-434 / 8-438 — note: the prediction is NOT
//! added again; it already entered through the transform domain).

/// §8.5.9 eq. 8-313 norm-adjust column classes shared by eq. 8-417
/// `Aij` and eq. 8-418 `LevelScale2`: class 0 for
/// (i, j) ∈ {(0,0), (0,2), (2,0), (2,2)}, class 1 for
/// {(1,1), (1,3), (3,1), (3,3)}, class 2 otherwise.
#[inline]
fn coeff_class(i: usize, j: usize) -> usize {
    match (i % 2, j % 2) {
        (0, 0) => 0,
        (1, 1) => 1,
        _ => 2,
    }
}

/// Eq. 8-417 — `Aij`.
#[inline]
fn a_ij(i: usize, j: usize) -> i64 {
    const A: [i64; 3] = [16, 25, 20];
    A[coeff_class(i, j)]
}

/// Eq. 8-419 — the `w` matrix backing `LevelScale2(m, i, j)`
/// (eq. 8-418). Row index = m (qP % 6), column index = coefficient
/// class.
const LEVEL_SCALE2_W: [[i64; 3]; 6] = [
    [13107, 5243, 8066],
    [11916, 4660, 7490],
    [10082, 4194, 6554],
    [9362, 3647, 5825],
    [8192, 3355, 5243],
    [7282, 2893, 4559],
];

/// Eq. 8-418 — `LevelScale2(m, i, j)`.
#[inline]
fn level_scale2(m: usize, i: usize, j: usize) -> i64 {
    LEVEL_SCALE2_W[m][coeff_class(i, j)]
}

/// §8.5.9 eq. 8-313 — `LevelScale4x4(m, i, j)` with a caller-supplied
/// raster weightScale list (flat 16s outside High-profile scaling
/// matrices; SP/SI live in the Extended profile where the SPS/PPS
/// carry no scaling matrices).
#[inline]
fn level_scale4x4(weight_scale: &[i32; 16], m: usize, i: usize, j: usize) -> i64 {
    const NORM_ADJUST: [[i64; 3]; 6] = [
        [10, 16, 13],
        [11, 18, 14],
        [13, 20, 16],
        [14, 23, 18],
        [16, 25, 20],
        [18, 29, 23],
    ];
    weight_scale[i * 4 + j] as i64 * NORM_ADJUST[m][coeff_class(i, j)]
}

/// Eq. 8-415 — forward 4x4 core transform of prediction samples:
/// `cp = Cf · p · CfT` with `Cf` rows
/// `[1 1 1 1; 2 1 −1 −2; 1 −1 −1 1; 1 −2 2 −1]`. Input and output are
/// row-major (`p[i*4+j] = p_ij`).
pub fn forward_core_4x4(p: &[i32; 16]) -> [i32; 16] {
    // Rows: t = Cf · p.
    let mut t = [0i32; 16];
    for j in 0..4 {
        let (a, b, c, d) = (p[j], p[4 + j], p[8 + j], p[12 + j]);
        t[j] = a + b + c + d;
        t[4 + j] = 2 * a + b - c - 2 * d;
        t[8 + j] = a - b - c + d;
        t[12 + j] = a - 2 * b + 2 * c - d;
    }
    // Columns: cp = t · CfT.
    let mut cp = [0i32; 16];
    for i in 0..4 {
        let (a, b, c, d) = (t[i * 4], t[i * 4 + 1], t[i * 4 + 2], t[i * 4 + 3]);
        cp[i * 4] = a + b + c + d;
        cp[i * 4 + 1] = 2 * a + b - c - 2 * d;
        cp[i * 4 + 2] = a - b - c + d;
        cp[i * 4 + 3] = a - 2 * b + 2 * c - d;
    }
    cp
}

/// 2x2 Hadamard sandwich `H · x · H` with `H = [1 1; 1 −1]`
/// (eqs. 8-427 / 8-430). Row-major 2x2 (`x[i*2+j] = x_ij`).
#[inline]
pub fn hadamard_2x2(x: &[i64; 4]) -> [i64; 4] {
    let (a, b, c, d) = (x[0], x[1], x[2], x[3]);
    [a + b + c + d, a - b + c - d, a + b - c - d, a - b - c + d]
}

/// Eq. 8-416 dequant term for one coefficient:
/// `( ( cr · LevelScale4x4(qP % 6, i, j) · Aij ) << ( qP / 6 ) ) >> 10`.
#[inline]
fn dequant_8_416(cr: i32, qp: i32, weight_scale: &[i32; 16], i: usize, j: usize) -> i64 {
    let m = (qp % 6) as usize;
    ((cr as i64 * level_scale4x4(weight_scale, m, i, j) * a_ij(i, j)) << (qp / 6)) >> 10
}

/// Eqs. 8-420 / 8-432 (luma):
/// `c = Sign(cs) · ( ( Abs(cs) · LevelScale2(QS % 6, i, j)
///                    + ( 1 << ( 14 + QS / 6 ) ) ) >> ( 15 + QS / 6 ) )`
/// — the sign multiplies the SHIFTED magnitude.
#[inline]
fn quant_qs_luma(cs: i64, qs: i32, i: usize, j: usize) -> i32 {
    let m = (qs % 6) as usize;
    let shift = 15 + qs / 6;
    let mag = (cs.abs() * level_scale2(m, i, j) + (1i64 << (14 + qs / 6))) >> shift;
    (cs.signum() * mag) as i32
}

/// Eqs. 8-425 / 8-435 (chroma AC):
/// `c = ( Sign(cs) · ( Abs(cs) · LevelScale2(QS % 6, i, j)
///        + ( 1 << ( 14 + QS / 6 ) ) ) ) >> ( 15 + QS / 6 )`
/// — unlike the luma eqs. 8-420 / 8-432, the sign multiplies the
/// PRE-shift magnitude and the arithmetic right shift then floors
/// (negatives round differently than the luma form). The asymmetry
/// repeats identically across both the §8.6.1 and §8.6.2 chroma
/// equations, so it is transcribed literally.
#[inline]
fn quant_qs_chroma(cs: i64, qs: i32, i: usize, j: usize) -> i32 {
    let m = (qs % 6) as usize;
    let shift = 15 + qs / 6;
    ((cs.signum() * (cs.abs() * level_scale2(m, i, j) + (1i64 << (14 + qs / 6)))) >> shift) as i32
}

/// Eqs. 8-429 / 8-439 (chroma DC — one extra bit in the rounding term
/// and the shift; sign inside the shifted product like the chroma AC
/// quantiser, see [`quant_qs_chroma`]):
/// `dc = ( Sign(x) · ( Abs(x) · LevelScale2(QS % 6, 0, 0)
///          + ( 1 << ( 15 + QS / 6 ) ) ) ) >> ( 16 + QS / 6 )`.
#[inline]
pub(crate) fn quant_qs_dc(x: i64, qs: i32) -> i64 {
    let m = (qs % 6) as usize;
    let shift = 16 + qs / 6;
    (x.signum() * (x.abs() * level_scale2(m, 0, 0) + (1i64 << (15 + qs / 6)))) >> shift
}

/// §8.6.1.1 — luma coefficients for one 4x4 block of a P macroblock in
/// an SP slice with `sp_for_switch_flag == 0`.
///
/// * `pred` — the §8.4 Inter prediction samples `p` (row-major 4x4,
///   eq. 8-414).
/// * `cr` — the parsed prediction residual transform coefficients after
///   the §8.5.6 inverse scan (row-major 4x4).
/// * `qp_y` — the current macroblock's QPY (eq. 8-416 dequant of `cr`).
/// * `qs_y` — the slice QSY (eq. 8-420 re-quantisation).
///
/// Returns the 4x4 array `c` to feed §8.5.12 with qP = QSY
/// (eq. 8-331).
pub fn sp_luma_non_switching(
    pred: &[i32; 16],
    cr: &[i32; 16],
    qp_y: i32,
    qs_y: i32,
    weight_scale: &[i32; 16],
) -> [i32; 16] {
    let cp = forward_core_4x4(pred);
    let mut c = [0i32; 16];
    for i in 0..4 {
        for j in 0..4 {
            let idx = i * 4 + j;
            // Eq. 8-416.
            let cs = cp[idx] as i64 + dequant_8_416(cr[idx], qp_y, weight_scale, i, j);
            // Eq. 8-420.
            c[idx] = quant_qs_luma(cs, qs_y, i, j);
        }
    }
    c
}

/// §8.6.2.1 — luma coefficients for one 4x4 block of a P macroblock in
/// an SP slice with `sp_for_switch_flag == 1`, or of an SI macroblock
/// (the prediction is then the §8.3 Intra_4x4 prediction).
///
/// Eq. 8-432 quantises the transformed prediction with QSY; eq. 8-433
/// adds the parsed residual coefficients `cr` (inverse-scanned,
/// row-major). Returns `c` for §8.5.12 with qP = QSY.
pub fn sp_luma_switching(pred: &[i32; 16], cr: &[i32; 16], qs_y: i32) -> [i32; 16] {
    let cp = forward_core_4x4(pred);
    let mut c = [0i32; 16];
    for i in 0..4 {
        for j in 0..4 {
            let idx = i * 4 + j;
            // Eq. 8-432.
            let cs = quant_qs_luma(cp[idx] as i64, qs_y, i, j);
            // Eq. 8-433.
            c[idx] = cr[idx] + cs;
        }
    }
    c
}

/// §8.6.1.2 — chroma coefficients for one component of a P macroblock
/// in a non-switching SP slice (4:2:0: four 4x4 blocks).
///
/// * `pred` — the 8x8 Inter prediction samples of this chroma
///   component (row-major).
/// * `dc_levels` — parsed `ChromaDCLevel[iCbCr][k]`, k = 0..3.
/// * `ac` — per-4x4-block inverse-scanned residual coefficients
///   (row-major; entry 0 of each block is ignored — the DC comes from
///   the 2x2 chain).
///
/// Returns four 4x4 arrays `c(chroma4x4BlkIdx)` (with `c00` filled per
/// eq. 8-431) ready for §8.5.12 with qP = QSC. §8.5.12.1 leaves chroma
/// `d00 = c00` untouched (eq. 8-335), so the eq. 8-431 pre-scaled DC
/// flows through exactly like the ordinary §8.5.11 chroma DC output.
pub fn sp_chroma_non_switching(
    pred: &[i32; 64],
    dc_levels: &[i32; 4],
    ac: &[[i32; 16]; 4],
    qp_c: i32,
    qs_c: i32,
    weight_scale: &[i32; 16],
) -> [[i32; 16]; 4] {
    // Steps 2..3 — per-block forward transform of the prediction.
    let cp = chroma_pred_transform(pred);

    let mut out = [[0i32; 16]; 4];
    for blk in 0..4 {
        for i in 0..4 {
            for j in 0..4 {
                if i == 0 && j == 0 {
                    continue; // DC handled below.
                }
                let idx = i * 4 + j;
                // Eq. 8-424.
                let cs =
                    cp[blk][idx] as i64 + dequant_8_416(ac[blk][idx], qp_c, weight_scale, i, j);
                // Eq. 8-425.
                out[blk][idx] = quant_qs_chroma(cs, qs_c, i, j);
            }
        }
    }

    // Eq. 8-427 — 2x2 transform of the prediction DC coefficients.
    // Matrix element (i, j) is c00p(i * 2 + j).
    let dcp = hadamard_2x2(&[
        cp[0][0] as i64,
        cp[1][0] as i64,
        cp[2][0] as i64,
        cp[3][0] as i64,
    ]);
    let m = (qp_c % 6) as usize;
    let ls_dc_qp = level_scale4x4(weight_scale, m, 0, 0);
    let mut dcr = [0i64; 4];
    for pos in 0..4 {
        // Eq. 8-428 — ChromaDCLevel[ j * 2 + i ] with the (i, j) of the
        // dc arrays read as the same positional index pair eq. 8-304
        // rasterises ChromaDCLevel with (the §8.5.11 2x2 chain is
        // transpose-invariant, so the level pickup and the eq. 8-431
        // block distribution must simply agree with the §8.5.4 raster
        // convention — level k belongs to dc position k and to
        // chroma4x4BlkIdx k). The shift is >> 9, one less than the
        // AC's >> 10, to account for the un-normalised 2x2 Hadamard.
        let level = dc_levels[pos] as i64;
        let dcs = dcp[pos] + (((level * ls_dc_qp * a_ij(0, 0)) << (qp_c / 6)) >> 9);
        // Eq. 8-429.
        dcr[pos] = quant_qs_dc(dcs, qs_c);
    }
    // Eq. 8-430.
    let f = hadamard_2x2(&dcr);
    // Eq. 8-431 — scale and distribute to the per-block DC slots
    // (raster: f position k → chroma4x4BlkIdx k, as in §8.5.4).
    let ls_dc_qs = level_scale4x4(weight_scale, (qs_c % 6) as usize, 0, 0);
    for (blk, fv) in f.iter().enumerate() {
        out[blk][0] = (((fv * ls_dc_qs) << (qs_c / 6)) >> 5) as i32;
    }
    out
}

/// §8.6.2.2 — chroma coefficients for one component of a P macroblock
/// in a switching SP slice, or of an SI macroblock (4:2:0).
///
/// Same inputs as [`sp_chroma_non_switching`]. Returns four 4x4 `c`
/// arrays for §8.5.12 with qP = QSC; per eqs. 8-440/8-441 the DC slot
/// carries `f` verbatim (no eq. 8-431 scaling), which §8.5.12.1
/// preserves (eq. 8-335).
pub fn sp_chroma_switching(
    pred: &[i32; 64],
    dc_levels: &[i32; 4],
    ac: &[[i32; 16]; 4],
    qs_c: i32,
) -> [[i32; 16]; 4] {
    let cp = chroma_pred_transform(pred);

    let mut out = [[0i32; 16]; 4];
    for blk in 0..4 {
        for i in 0..4 {
            for j in 0..4 {
                if i == 0 && j == 0 {
                    continue;
                }
                let idx = i * 4 + j;
                // Eq. 8-435.
                let cs = quant_qs_chroma(cp[blk][idx] as i64, qs_c, i, j);
                // Eq. 8-437.
                out[blk][idx] = ac[blk][idx] + cs;
            }
        }
    }

    // Eq. 8-427 on the prediction DCs.
    let dcp = hadamard_2x2(&[
        cp[0][0] as i64,
        cp[1][0] as i64,
        cp[2][0] as i64,
        cp[3][0] as i64,
    ]);
    let mut dcr = [0i64; 4];
    for pos in 0..4 {
        // Eq. 8-439.
        let dcs = quant_qs_dc(dcp[pos], qs_c);
        // Eq. 8-440 — level pickup in the §8.5.4 raster convention
        // (see the eq. 8-428 note in `sp_chroma_non_switching`).
        dcr[pos] = dcs + dc_levels[pos] as i64;
    }
    // Eq. 8-430.
    let f = hadamard_2x2(&dcr);
    // Eq. 8-441 — copied verbatim into the DC slots (raster).
    for (blk, fv) in f.iter().enumerate() {
        out[blk][0] = *fv as i32;
    }
    out
}

/// Steps shared by both chroma paths: split the 8x8 component
/// prediction into the four 4x4 blocks (§6.4.7 raster order) and apply
/// eq. 8-415 to each.
pub(crate) fn chroma_pred_transform(pred: &[i32; 64]) -> [[i32; 16]; 4] {
    let mut cp = [[0i32; 16]; 4];
    for (blk, dst) in cp.iter_mut().enumerate() {
        let (bx, by) = ((blk % 2) * 4, (blk / 2) * 4);
        let mut p = [0i32; 16];
        for y in 0..4 {
            for x in 0..4 {
                p[y * 4 + x] = pred[(by + y) * 8 + bx + x];
            }
        }
        *dst = forward_core_4x4(&p);
    }
    cp
}

// ---------------------------------------------------------------------------
// Encoder-side §8.6 helpers (non-normative): forward quantisers that
// invert the eq. 8-416 / 8-428 dequant terms so an SP encoder can pick
// residual levels approximating a target in the transform domain.
// ---------------------------------------------------------------------------

/// Encoder choice (non-normative): the residual level whose eq. 8-416
/// dequant best approximates `target` at the given qP.
pub fn encoder_quant_8_416(
    target: i64,
    qp: i32,
    weight_scale: &[i32; 16],
    i: usize,
    j: usize,
) -> i32 {
    let m = (qp % 6) as usize;
    let den = (level_scale4x4(weight_scale, m, i, j) * a_ij(i, j)) << (qp / 6);
    // round(target * 2^10 / den) with round-half-away-from-zero.
    let num = target << 10;
    let r = if num >= 0 {
        (num + den / 2) / den
    } else {
        (num - den / 2) / den
    };
    r as i32
}

/// Encoder choice (non-normative): the chroma DC level whose eq. 8-428
/// dequant best approximates `target` at the given qP.
pub fn encoder_quant_8_428(target: i64, qp_c: i32, weight_scale: &[i32; 16]) -> i32 {
    let m = (qp_c % 6) as usize;
    let den = (level_scale4x4(weight_scale, m, 0, 0) * a_ij(0, 0)) << (qp_c / 6);
    let num = target << 9;
    let r = if num >= 0 {
        (num + den / 2) / den
    } else {
        (num - den / 2) / den
    };
    r as i32
}

/// Eq. 8-431 as a per-element integer scale: with `QSC >= 6` the
/// operation `((f · LevelScale4x4(QSC % 6, 0, 0)) << (QSC / 6)) >> 5`
/// is exactly `f · k` with
/// `k = LevelScale4x4 · 2^(QSC / 6) / 32` (an integer because
/// LevelScale4x4(m, 0, 0) is a multiple of 16 for flat weightScale and
/// `QSC / 6 >= 1`). A switching-picture encoder uses this to reproduce
/// a non-switching picture's chroma DC reconstruction exactly
/// (`dcr_switch = k · dcr_primary`). Returns `None` when the scale is
/// not an exact integer (QSC < 6 with an odd norm-adjust product).
pub fn chroma_dc_switch_scale(qs_c: i32, weight_scale: &[i32; 16]) -> Option<i64> {
    let ls = level_scale4x4(weight_scale, (qs_c % 6) as usize, 0, 0);
    let num = ls << (qs_c / 6);
    if num % 32 == 0 {
        Some(num / 32)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const FLAT: [i32; 16] = [16; 16];

    /// Eq. 8-415 on a constant block: only the DC survives, equal to
    /// 16 · v (sum over the 4x4 after both 1-D passes).
    #[test]
    fn forward_core_dc_of_constant_block() {
        let p = [7i32; 16];
        let cp = forward_core_4x4(&p);
        assert_eq!(cp[0], 16 * 7);
        assert!(cp[1..].iter().all(|&v| v == 0));
    }

    /// The eq. 8-415 core matches the §8.5.12.2 inverse: pushing a
    /// quantised version of T(p) through the §8.5.12 chain at the same
    /// qP reproduces p to within the quantisation error bound.
    #[test]
    fn sp_switching_luma_roundtrip_close_to_prediction() {
        // A gradient prediction block.
        let mut pred = [0i32; 16];
        for (k, v) in pred.iter_mut().enumerate() {
            *v = 60 + (k as i32) * 3;
        }
        let qs_y = 24;
        let c = sp_luma_switching(&pred, &[0i32; 16], qs_y);
        let r = crate::transform::inverse_transform_4x4(&c, qs_y, &FLAT, 8).unwrap();
        for k in 0..16 {
            let err = (r[k] - pred[k]).abs();
            assert!(
                err <= 6,
                "blk[{k}]: recon {} vs pred {} (err {err})",
                r[k],
                pred[k]
            );
        }
    }

    /// §8.6.1.1 with an all-zero residual still re-quantises the
    /// prediction (an SP P_Skip is NOT a sample-domain copy).
    #[test]
    fn sp_non_switching_zero_residual_requantises() {
        let mut pred = [0i32; 16];
        for (k, v) in pred.iter_mut().enumerate() {
            *v = 100 + ((k as i32 * 37) % 23);
        }
        let qp_y = 28;
        let qs_y = 28;
        let c = sp_luma_non_switching(&pred, &[0i32; 16], qp_y, qs_y, &FLAT);
        let r = crate::transform::inverse_transform_4x4(&c, qs_y, &FLAT, 8).unwrap();
        // The output is the QS-quantised prediction, not the original.
        let max_err = (0..16).map(|k| (r[k] - pred[k]).abs()).max().unwrap();
        assert!(max_err > 0, "QS=28 must not be lossless on this block");
        assert!(max_err <= 12, "but must stay within quantiser range");
    }

    /// The defining SP property at coefficient level: a switching block
    /// with `cr = c_target − cs` reproduces the target coefficients
    /// exactly, whatever the prediction.
    #[test]
    fn switching_identity_luma_exact() {
        let mut pred_a = [0i32; 16];
        let mut pred_b = [0i32; 16];
        for k in 0..16 {
            pred_a[k] = 90 + ((k as i32 * 17) % 31);
            pred_b[k] = 15 + ((k as i32 * 29) % 61);
        }
        let qp_y = 27;
        let qs_y = 25;
        // Primary SP block (stream A): some residual.
        let mut cr_a = [0i32; 16];
        cr_a[0] = 3;
        cr_a[5] = -2;
        let c_target = sp_luma_non_switching(&pred_a, &cr_a, qp_y, qs_y, &FLAT);

        // Switching block predicted from unrelated pred_b: choose
        // cr = c_target − Q_QS(T(pred_b)).
        let cs_b = sp_luma_switching(&pred_b, &[0i32; 16], qs_y);
        let mut cr_sw = [0i32; 16];
        for k in 0..16 {
            cr_sw[k] = c_target[k] - cs_b[k];
        }
        let c_sw = sp_luma_switching(&pred_b, &cr_sw, qs_y);
        assert_eq!(c_sw, c_target);
    }

    /// Chroma DC switching identity: with QSC >= 6 the eq. 8-431 scale
    /// is an exact integer `k`, and `dcr_switch = k · dcr_primary`
    /// reproduces the primary's scaled DC verbatim through the
    /// eq. 8-430/8-441 path.
    #[test]
    fn switching_identity_chroma_dc_exact() {
        let mut pred_a = [0i32; 64];
        let mut pred_b = [0i32; 64];
        for k in 0..64 {
            pred_a[k] = 120 + ((k as i32 * 13) % 17);
            pred_b[k] = 40 + ((k as i32 * 7) % 29);
        }
        let (qp_c, qs_c) = (28, 26);
        let dc_a = [5, -3, 2, 0];
        let ac_zero = [[0i32; 16]; 4];
        let c_a = sp_chroma_non_switching(&pred_a, &dc_a, &ac_zero, qp_c, qs_c, &FLAT);

        let k = chroma_dc_switch_scale(qs_c, &FLAT).expect("QSC >= 6 scale is integral");

        // Recover the primary's dcr from its c00 values: c00 = k · f,
        // f = H2(dcr) ⇒ H2(f) = 4 · dcr (H2 is 2·orthogonal). The
        // distribution is raster: c00(blk) = k · f[blk].
        let kf = [
            c_a[0][0] as i64,
            c_a[1][0] as i64,
            c_a[2][0] as i64,
            c_a[3][0] as i64,
        ];
        let h_kf = hadamard_2x2(&kf); // = 4k · dcr
        assert!(h_kf.iter().all(|v| v % 4 == 0));
        let target_dcr = [h_kf[0] / 4, h_kf[1] / 4, h_kf[2] / 4, h_kf[3] / 4];
        assert!(target_dcr.iter().all(|v| v % k == 0 || k == 1));

        // Switching side: dc level = target_dcr − dcs(pred_b).
        let cp_b = chroma_pred_transform(&pred_b);
        let dcp_b = hadamard_2x2(&[
            cp_b[0][0] as i64,
            cp_b[1][0] as i64,
            cp_b[2][0] as i64,
            cp_b[3][0] as i64,
        ]);
        let mut dc_levels_sw = [0i32; 4];
        for pos in 0..4 {
            let dcs = quant_qs_dc(dcp_b[pos], qs_c);
            dc_levels_sw[pos] = (target_dcr[pos] - dcs) as i32;
        }
        let c_sw = sp_chroma_switching(&pred_b, &dc_levels_sw, &ac_zero, qs_c);
        // The switching DC slots must equal the primary's scaled DCs.
        for blk in 0..4 {
            assert_eq!(c_sw[blk][0], c_a[blk][0], "blk {blk} DC");
        }
    }

    /// Encoder forward quant inverts eq. 8-416 within one step.
    #[test]
    fn encoder_quant_8_416_inverts_dequant() {
        for qp in [10, 22, 30, 40] {
            for target in [-2000i64, -37, 0, 5, 419, 3000] {
                for (i, j) in [(0, 0), (1, 1), (2, 3)] {
                    let level = encoder_quant_8_416(target, qp, &FLAT, i, j);
                    let back = dequant_8_416(level, qp, &FLAT, i, j);
                    let den =
                        (level_scale4x4(&FLAT, (qp % 6) as usize, i, j) * a_ij(i, j)) << (qp / 6);
                    let step = den >> 10;
                    assert!(
                        (back - target).abs() <= step.max(1),
                        "qp {qp} target {target} ({i},{j}): back {back} step {step}"
                    );
                }
            }
        }
    }
}