phasm-core 0.2.4

Pure-Rust steganography engine — hide encrypted messages in JPEG photos
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Copyright (c) 2026 Christoph Gaffga
// SPDX-License-Identifier: GPL-3.0-only
// https://github.com/cgaffga/phasmcore

//! H.264 8×8 integer transform — forward + inverse + quant/dequant.
//! Implemented from H.264 spec (Rec. ITU-T H.264 (03/2010)) § 8.5.13
//! ("Scaling and transformation process for residual 8x8 blocks") and
//! § 8.5.13.2 ("Transformation process for residual 8x8 blocks").
//!
//! Forward path uses direct matrix multiplication with the published
//! 8×8 integer-DCT basis (spec Eq. 7-1 conceptual DCT-II at 8×8; the
//! integer basis is standard across H.264 literature and widely
//! published, e.g. Malvar et al., "Low-complexity transform and
//! quantization in H.264/AVC", IEEE TCSVT 13(7), 2003). The overall
//! gain of 64 per dimension (4096 total for DC) is absorbed by the
//! 8×8 quantizer's MF table.
//!
//! Inverse path implements the spec's three-stage integer lift of
//! § 8.5.13.2 equations (8-362) … (8-385), applied to rows first
//! then to columns per the spec's prose ordering. The final
//! `(v + 32) >> 6` per-pixel rounding specified by § 8.5.13 step 6
//! happens in the reconstruction pipeline (caller of
//! `inverse_dct_8x8`), not here.
//!
//! Design note: the forward is implemented as a direct matrix
//! multiply rather than a butterfly factorisation. Matrix-multiply
//! form has unambiguous provenance (each output is a linear
//! combination of inputs, spelled out), at the cost of ~2× ops vs a
//! custom factorisation. Revisit if the 8×8 forward becomes a hot
//! path.

/// H.264 8×8 forward transform basis matrix — 8 row vectors of the
/// integer DCT. Row 0 is the DC row (all 8s). Odd-indexed rows capture
/// the high-frequency basis with integer coefficients 12/10/6/3 and
/// 10/6/3/12 patterns.
///
/// Basis source: spec § 8.5.12 + Malvar 2003 (publicly documented,
/// not GPL-contaminated).
#[rustfmt::skip]
const H8: [[i32; 8]; 8] = [
    [ 8,   8,   8,   8,   8,   8,   8,   8],
    [12,  10,   6,   3,  -3,  -6, -10, -12],
    [ 8,   4,  -4,  -8,  -8,  -4,   4,   8],
    [10,  -3, -12,  -6,   6,  12,   3, -10],
    [ 8,  -8,  -8,   8,   8,  -8,  -8,   8],
    [ 6, -12,   3,  10, -10,  -3,  12,  -6],
    [ 4,  -8,   8,  -4,  -4,   8,  -8,   4],
    [ 3,  -6,  10, -12,  12, -10,   6,  -3],
];

/// Forward 8×8 integer DCT: `Y = H8 · X · H8ᵀ`.
///
/// Inputs are typically residuals in the range roughly `-255..=255`; the
/// returned coefficient matrix has the 8×8 integer-DCT scaling built
/// in and will be renormalized by the 8×8 quantizer (Phase 100-B). The
/// DC coefficient `Y[0][0]` equals `64 · sum(X[i][j])`, matching the
/// spec's integer-DCT convention.
pub fn forward_dct_8x8(input: &[[i32; 8]; 8]) -> [[i32; 8]; 8] {
    // Stage 1: row-wise multiply — temp = H8 · input.
    let mut temp = [[0i32; 8]; 8];
    for i in 0..8 {
        for j in 0..8 {
            let mut acc = 0i32;
            for k in 0..8 {
                acc += H8[i][k] * input[k][j];
            }
            temp[i][j] = acc;
        }
    }
    // Stage 2: column-wise multiply — output = temp · H8ᵀ.
    let mut output = [[0i32; 8]; 8];
    for i in 0..8 {
        for j in 0..8 {
            let mut acc = 0i32;
            for k in 0..8 {
                acc += temp[i][k] * H8[j][k];
            }
            output[i][j] = acc;
        }
    }
    output
}

/// 1-D inverse 8-point transform per spec § 8.5.13.2 integer
/// butterfly. Input `f[]` holds one row (or column) of dequantized
/// coefficients; output is the partially-reconstructed values along
/// that axis prior to the second pass.
///
/// Three-stage lift directly transcribed from spec equations
/// (8-362) … (8-385):
///   Stage 1 — compute e[i] from f[] per equations (8-362) … (8-369).
///   Stage 2 — compute g[i] from e[] per equations (8-370) … (8-377).
///   Stage 3 — compute h[i] from g[] per equations (8-378) … (8-385).
///
/// The `>> 1` / `>> 2` integer shifts inside stages 1 and 2 make this
/// a non-linear map, so the two-pass 2D inverse requires the passes
/// to be applied in the order the spec specifies (row, then column).
#[inline]
fn inverse_1d_8(f: &[i32; 8]) -> [i32; 8] {
    // Stage 1 — equations (8-362) … (8-369).
    let e0 =  f[0] + f[4];
    let e1 = -f[3] + f[5] - f[7] - (f[7] >> 1);
    let e2 =  f[0] - f[4];
    let e3 =  f[1] + f[7] - f[3] - (f[3] >> 1);
    let e4 = (f[2] >> 1) - f[6];
    let e5 = -f[1] + f[7] + f[5] + (f[5] >> 1);
    let e6 =  f[2] + (f[6] >> 1);
    let e7 =  f[3] + f[5] + f[1] + (f[1] >> 1);

    // Stage 2 — equations (8-370) … (8-377).
    let g0 =  e0 + e6;
    let g1 =  e1 + (e7 >> 2);
    let g2 =  e2 + e4;
    let g3 =  e3 + (e5 >> 2);
    let g4 =  e2 - e4;
    let g5 = (e3 >> 2) - e5;
    let g6 =  e0 - e6;
    let g7 =  e7 - (e1 >> 2);

    // Stage 3 — equations (8-378) … (8-385).
    [
        g0 + g7,
        g2 + g5,
        g4 + g3,
        g6 + g1,
        g6 - g1,
        g4 - g3,
        g2 - g5,
        g0 - g7,
    ]
}

/// Inverse 8×8 integer DCT. Applies the 1-D inverse to rows first,
/// then to columns, per spec § 8.5.13.2 ("First, each (horizontal)
/// row of scaled transform coefficients is transformed … Then, each
/// (vertical) column of the resulting matrix is transformed using
/// the same one-dimensional inverse transform"). Because the 1-D
/// butterfly is non-linear under integer `>> 1` / `>> 2` shifts,
/// the row-then-column order is not interchangeable with
/// column-then-row; mirroring the spec's order produces the
/// canonical reconstruction values.
///
/// Output is pre-rounding — the reconstruction pipeline adds
/// `(v + 32) >> 6` before integer clipping to `[0, 255]`.
pub fn inverse_dct_8x8(input: &[[i32; 8]; 8]) -> [[i32; 8]; 8] {
    // Row pass.
    let mut temp = [[0i32; 8]; 8];
    for i in 0..8 {
        temp[i] = inverse_1d_8(&input[i]);
    }
    // Column pass.
    let mut output = [[0i32; 8]; 8];
    for j in 0..8 {
        let col = [
            temp[0][j], temp[1][j], temp[2][j], temp[3][j],
            temp[4][j], temp[5][j], temp[6][j], temp[7][j],
        ];
        let out = inverse_1d_8(&col);
        for i in 0..8 {
            output[i][j] = out[i];
        }
    }
    output
}

// ─── Phase 100-B: 8×8 quantization / dequantization ─────────────────────────

/// H.264 8×8 `normAdjust8x8` (spec Table 8-16, rows m = 0..5).
/// Indexed by `[qp % 6][class]` where `class` is the 6-position
/// equivalence class over an 8×8 block (see `class_of_8x8_pos`).
/// Tabular data is defined verbatim by the spec — not copyrightable.
const NORM_ADJUST_8X8: [[i32; 6]; 6] = [
    [20, 18, 32, 19, 25, 24],
    [22, 19, 35, 21, 28, 26],
    [26, 23, 42, 24, 33, 31],
    [28, 25, 45, 26, 35, 33],
    [32, 28, 51, 30, 40, 38],
    [36, 32, 58, 34, 46, 43],
];

/// Position-class lookup for an 8×8 coefficient (spec § 8.5.12,
/// Table 8-16 footnote defines a 6-class equivalence). Indexed by
/// `((row & 3) << 2) | (col & 3)`; the 8×8 block's 64 positions
/// reduce to 16 distinct mod-4 patterns (each pattern shared by
/// the 4 tiles of the 8×8), which in turn map to 6 distinct classes
/// via this scan.
const CLASS_SCAN_8X8: [u8; 16] = [
    0, 3, 4, 3, 3, 1, 5, 1, 4, 5, 2, 5, 3, 1, 5, 1,
];

/// Position class 0..=5 for the 8×8 position `(row, col)`.
#[inline]
const fn class_of_8x8_pos(row: usize, col: usize) -> usize {
    CLASS_SCAN_8X8[((row & 3) << 2) | (col & 3)] as usize
}

/// H.264 8×8 forward quant multiplication factor `MF[qp % 6][class]`.
///
/// Derived from `NORM_ADJUST_8X8` via `MF = round(4096 / norm)`. This
/// pairing gives flat-block round-trip identity on DC-only input when
/// combined with `qbits = 16 + qp/6`, the spec dequant formula, and
/// the `(v + 32) >> 6` final pixel rounding.
const MF_8X8: [[i32; 6]; 6] = [
    // qp%6 = 0  — NORM {20, 18, 32, 19, 25, 24}
    [205, 228, 128, 216, 164, 171],
    // qp%6 = 1  — NORM {22, 19, 35, 21, 28, 26}
    [186, 216, 117, 195, 146, 158],
    // qp%6 = 2  — NORM {26, 23, 42, 24, 33, 31}
    [158, 178,  98, 171, 124, 132],
    // qp%6 = 3  — NORM {28, 25, 45, 26, 35, 33}
    [146, 164,  91, 158, 117, 124],
    // qp%6 = 4  — NORM {32, 28, 51, 30, 40, 38}
    [128, 146,  80, 137, 102, 108],
    // qp%6 = 5  — NORM {36, 32, 58, 34, 46, 43}
    [114, 128,  71, 120,  89,  95],
];

/// Slice-type dead-zone selector (mirrors `quantization::QuantSlice` so
/// the 8×8 path can use the same offsets without cross-imports).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Slice8x8 {
    /// I-slice: wider dead-zone `f = 2^qbits / 3`.
    Intra,
    /// P/B-slice: narrower dead-zone `f = 2^qbits / 6`.
    Inter,
}

/// Forward 8×8 dead-zone quantizer.
///
/// `coeffs` is the output of `forward_dct_8x8` (signed `i32`, typically
/// in the range ±(4096·255) for 8-bit residuals).
///
/// Returns 64 `i16` levels in row-major order. Levels round-trip with
/// `dequant_8x8_block` + `inverse_dct_8x8` + the `(v + 32) >> 6` final
/// pixel rounding to the input residual within the quantization error
/// budget.
pub fn quant_8x8_block(coeffs: &[[i32; 8]; 8], qp: u8, slice: Slice8x8) -> [[i16; 8]; 8] {
    let qp = qp.min(51) as i32;
    let m = (qp % 6) as usize;
    let qbits: i32 = 16 + qp / 6;
    let f_divisor: i32 = match slice {
        Slice8x8::Intra => 3,
        Slice8x8::Inter => 6,
    };
    let f: i64 = (1i64 << qbits) / (f_divisor as i64);

    let mut out = [[0i16; 8]; 8];
    for i in 0..8 {
        for j in 0..8 {
            let c = coeffs[i][j];
            let abs_c = c.unsigned_abs() as i64;
            let mf = MF_8X8[m][class_of_8x8_pos(i, j)] as i64;
            let level_unsigned = ((abs_c * mf + f) >> qbits) as i32;
            let signed = if c < 0 {
                -level_unsigned
            } else {
                level_unsigned
            };
            // Clamp to i16 range. Real residuals rarely come close to
            // the limit, but quant of saturated inputs could.
            out[i][j] = signed.clamp(i16::MIN as i32, i16::MAX as i32) as i16;
        }
    }
    out
}

/// Inverse 8×8 scaling (dequantization) per spec § 8.5.13.1.
///
/// Spec form (equation 8-361):
///   `d[i][j] = (c[i][j] · LevelScale8x8(qP%6, i, j) + 2^(5 − qP/6))
///              >> (6 − qP/6)`     when `qP/6 < 6`
///   `d[i][j] =  c[i][j] · LevelScale8x8(qP%6, i, j) << (qP/6 − 6)`
///                                 when `qP/6 ≥ 6`
///
/// with `LevelScale8x8(m, i, j) = 16 · normAdjust8x8[m][class(i, j)]`
/// under flat scaling (no custom scaling matrix). The implementation
/// below is this formula transcribed directly.
pub fn dequant_8x8_block(levels: &[[i16; 8]; 8], qp: u8) -> [[i32; 8]; 8] {
    let qp = qp.min(51) as i32;
    let m = (qp % 6) as usize;
    let qp_div_6 = qp / 6;
    let mut out = [[0i32; 8]; 8];
    for i in 0..8 {
        for j in 0..8 {
            let level = levels[i][j] as i32;
            let level_scale = 16 * NORM_ADJUST_8X8[m][class_of_8x8_pos(i, j)];
            let scaled = if qp_div_6 >= 6 {
                level * level_scale * (1 << (qp_div_6 - 6))
            } else {
                let round = 1 << (5 - qp_div_6);
                let shift = 6 - qp_div_6;
                (level * level_scale + round) >> shift
            };
            out[i][j] = scaled;
        }
    }
    out
}

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

    #[test]
    fn mf_matches_derived_values() {
        // MF = round(4096 / normAdjust). Lock the derivation so any
        // future edit to NORM_ADJUST_8X8 triggers this test.
        for m in 0..6 {
            for c in 0..6 {
                let derived = (4096 + NORM_ADJUST_8X8[m][c] / 2) / NORM_ADJUST_8X8[m][c];
                assert_eq!(
                    MF_8X8[m][c], derived,
                    "MF_8X8[{m}][{c}] mismatch: table={}, derived={}",
                    MF_8X8[m][c], derived
                );
            }
        }
    }

    #[test]
    fn class_assignment_matches_spec_scan() {
        // DC is class 0.
        assert_eq!(class_of_8x8_pos(0, 0), 0);
        // (2, 2) is class 2.
        assert_eq!(class_of_8x8_pos(2, 2), 2);
        // Positions identical mod 4 share class.
        for i in 0..8 {
            for j in 0..8 {
                assert_eq!(class_of_8x8_pos(i, j), class_of_8x8_pos(i + 4 * (i < 4) as usize, j));
                assert_eq!(class_of_8x8_pos(i, j), class_of_8x8_pos(i, j + 4 * (j < 4) as usize));
            }
        }
    }

    #[test]
    fn quant_zero_coefs_gives_zero_levels() {
        let coeffs = [[0i32; 8]; 8];
        let levels = quant_8x8_block(&coeffs, 24, Slice8x8::Intra);
        for row in &levels {
            for &v in row {
                assert_eq!(v, 0);
            }
        }
    }

    #[test]
    fn dequant_zero_levels_gives_zero_coefs() {
        let levels = [[0i16; 8]; 8];
        for qp in [0, 1, 12, 24, 36, 51] {
            let coefs = dequant_8x8_block(&levels, qp);
            for row in &coefs {
                for &v in row {
                    assert_eq!(v, 0, "qp {qp}: expected 0, got {v}");
                }
            }
        }
    }

    #[test]
    fn roundtrip_flat_recovers_input_at_qp0() {
        // QP=0 is the lightest quant; round-trip of a flat input must
        // recover the input value within ±1 on every pixel.
        for v in [-64, -8, 0, 8, 16, 64, 128] {
            let input = [[v; 8]; 8];
            let fwd = forward_dct_8x8(&input);
            let levels = quant_8x8_block(&fwd, 0, Slice8x8::Intra);
            let deq = dequant_8x8_block(&levels, 0);
            let inv = inverse_dct_8x8(&deq);
            // Apply spec final rounding `(v + 32) >> 6` to each pixel.
            for row in &inv {
                for &scaled in row {
                    let pixel = (scaled + 32) >> 6;
                    let err = (pixel - v).abs();
                    assert!(
                        err <= 1,
                        "flat v={v} qp=0 recon={pixel} err={err}",
                    );
                }
            }
        }
    }

    #[test]
    fn roundtrip_flat_tolerates_noise_at_high_qp() {
        // Higher QP = more quant noise; even flat content won't recover
        // exactly, but the reconstruction should stay within a small
        // envelope controlled by the effective step size.
        for qp in [12, 18, 24, 30, 36, 42] {
            for v in [16, 64, 128] {
                let input = [[v; 8]; 8];
                let fwd = forward_dct_8x8(&input);
                let levels = quant_8x8_block(&fwd, qp, Slice8x8::Intra);
                let deq = dequant_8x8_block(&levels, qp);
                let inv = inverse_dct_8x8(&deq);
                // Expected noise envelope: dead-zone intra = 2^(qp/6).
                // Real quant error is usually well inside this.
                let envelope = 1 << (qp / 6);
                for row in &inv {
                    for &scaled in row {
                        let pixel = (scaled + 32) >> 6;
                        let err = (pixel - v).abs();
                        assert!(
                            err <= envelope,
                            "qp={qp} v={v} recon={pixel} err={err} > envelope {envelope}",
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn intra_vs_inter_dead_zone_differs() {
        // For a small coefficient, intra should quantize to a larger
        // level than inter at the same QP (intra's 1/3 offset < inter's
        // 1/6 means intra is LESS likely to zero small coefs, i.e.,
        // keeps more residual detail).
        //
        // Plant a single near-DC coefficient of magnitude 4096·3 = 12288
        // (= a flat-3 block's DC) and quantize at a QP where both modes
        // should round to either 0 or 1.
        let mut coefs = [[0i32; 8]; 8];
        coefs[0][0] = 12288;
        let lv_i = quant_8x8_block(&coefs, 18, Slice8x8::Intra)[0][0];
        let lv_p = quant_8x8_block(&coefs, 18, Slice8x8::Inter)[0][0];
        // Intra's level should be ≥ inter's (wider dead-zone → inter
        // more aggressive rounding toward zero).
        assert!(
            lv_i >= lv_p,
            "intra should preserve more than inter at same QP: intra={lv_i} inter={lv_p}",
        );
    }
}

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

    #[test]
    fn forward_zero_input_zero_output() {
        let zero = [[0i32; 8]; 8];
        let y = forward_dct_8x8(&zero);
        for r in &y {
            for &v in r {
                assert_eq!(v, 0);
            }
        }
    }

    #[test]
    fn forward_flat_input_dc_only() {
        // Flat block of value 1 → every element sums to 1·H8·H8ᵀ·1.
        // DC coefficient = 8 · 8 · 64 = 64·8·8 = 4096 (row-DC 8 · sum
        // (8) · col-DC 8).
        let flat = [[1i32; 8]; 8];
        let y = forward_dct_8x8(&flat);
        assert_eq!(y[0][0], 4096, "DC coefficient for flat-1 input");
        // All non-DC outputs zero because all basis rows 1..=7 are
        // odd/alternating → sum across a constant row = 0.
        for (i, row) in y.iter().enumerate() {
            for (j, &v) in row.iter().enumerate() {
                if i == 0 && j == 0 {
                    continue;
                }
                assert_eq!(v, 0, "expected 0 at ({i},{j}), got {v}");
            }
        }
    }

    #[test]
    fn forward_flat_input_dc_scales_with_amplitude() {
        // Flat = v → DC = 4096 · v.
        for v in [-16, -4, 0, 4, 16, 64, 128] {
            let input = [[v; 8]; 8];
            let y = forward_dct_8x8(&input);
            assert_eq!(y[0][0], 4096 * v);
        }
    }

    #[test]
    fn forward_column_stripe_row0_energy_only() {
        // Column stripe constant-down, alternating across cols. The
        // forward transform's first stage (temp = H8·X) with columns-
        // constant input nullifies every basis row except row 0
        // (rows 1..7 of H8 are sign-symmetric about the column axis
        // and each have sum 0 → temp[i][j] = 0 for i > 0 regardless
        // of j). So all output energy sits in row 0 of Y.
        let mut x = [[0i32; 8]; 8];
        for i in 0..8 {
            for j in 0..8 {
                x[i][j] = if j & 1 == 0 { 2 } else { 0 };
            }
        }
        let y = forward_dct_8x8(&x);
        for i in 1..8 {
            for j in 0..8 {
                assert_eq!(y[i][j], 0, "row {i} col {j} should be zero, got {}", y[i][j]);
            }
        }
        // Row 0 has non-zero energy.
        let row0_abs: i32 = y[0].iter().map(|v| v.abs()).sum();
        assert!(row0_abs > 0);
    }

    #[test]
    fn inverse_zero_input_zero_output() {
        let zero = [[0i32; 8]; 8];
        let x = inverse_dct_8x8(&zero);
        for r in &x {
            for &v in r {
                assert_eq!(v, 0);
            }
        }
    }

    #[test]
    fn inverse_dc_only_gives_flat() {
        // Only the DC coef non-zero. Spec butterfly produces a flat
        // output block whose value depends on the inverse-transform
        // scaling. With DC=64 the output should be all equal (flat),
        // and specifically equal to 64 per axis pass = 64·64 = 4096
        // if there were no lifts; but the lifts don't affect the DC
        // path (all lift-involved positions are 0 in a DC-only input).
        let mut f = [[0i32; 8]; 8];
        f[0][0] = 64;
        let x = inverse_dct_8x8(&f);
        let v0 = x[0][0];
        for row in &x {
            for &v in row {
                assert_eq!(v, v0, "DC-only input must produce a flat block");
            }
        }
        assert!(v0 != 0, "DC-only non-zero input must produce non-zero output");
    }

    #[test]
    fn roundtrip_flat_scales_by_4096() {
        // Forward+inverse on a flat input must produce a flat output.
        // Scale factor: forward DC gain = 64 per axis → 4096 over 2D.
        // Inverse DC path has unity gain on flat coefs (no lifts
        // fire on the zero non-DC positions). So overall scale = 4096.
        //
        // NB: this is the DC-ONLY identity. Non-DC basis functions
        // have different scale factors and DO NOT compose to a
        // constant through fwd+inv without the per-position
        // normalization in LevelScale8x8 (Phase 100-B).
        for v in [-32, -1, 0, 1, 3, 16, 64] {
            let input = [[v; 8]; 8];
            let fwd = forward_dct_8x8(&input);
            let back = inverse_dct_8x8(&fwd);
            for row in &back {
                for &got in row {
                    assert_eq!(got, 4096 * v, "flat {v} should scale to {} per pixel", 4096 * v);
                }
            }
        }
    }

    #[test]
    fn roundtrip_non_flat_not_identity() {
        // The matrix-form forward and spec-butterfly inverse are not
        // perfectly paired — the 64 basis functions have per-position
        // scale factors that require LevelScale8x8 (Phase 100-B) to
        // cancel out. This test documents that reality: a non-flat
        // input does NOT round-trip to a constant multiple of itself.
        //
        // When Phase 100-B lands its quantize/dequantize pipeline, a
        // paired test there will verify quantize→dequantize→inverse
        // reconstructs the input within expected quantization noise.
        let mut input = [[0i32; 8]; 8];
        input[0][0] = 10;   // DC component
        input[3][5] = 20;   // high-frequency component
        let fwd = forward_dct_8x8(&input);
        let back = inverse_dct_8x8(&fwd);
        // Sanity: output exists and differs from the input shape.
        // (A stronger claim would be "the impulse leaks everywhere",
        // which it does — but the exact leakage pattern is
        // LevelScale8x8-dependent.)
        let total: i32 = back.iter().flatten().map(|v| v.abs()).sum();
        assert!(total > 0);
    }

    #[test]
    fn per_basis_scale_factors() {
        // Documents the scale factor each of the 64 basis positions
        // incurs through forward+inverse. These factors go into
        // LevelScale8x8 construction in Phase 100-B. Test locks them
        // down so Phase 100-B quant calibration has a canonical
        // reference.
        //
        // For position (i, j), we plant a unit coefficient there and
        // inverse-transform to get the basis-function recon; then
        // forward-transform that recon to get the measured scale at
        // (i, j) = fwd(inv(e_{i,j}))[i][j].
        //
        // Expected structure: positions (0, 0) = 4096 (DC path),
        // other even/even positions similar, lift-affected positions
        // smaller.
        let mut seen_dc = false;
        for i in 0..8 {
            for j in 0..8 {
                let mut e = [[0i32; 8]; 8];
                e[i][j] = 1;
                let x = inverse_dct_8x8(&e);
                let y = forward_dct_8x8(&x);
                // The scale factor is y[i][j] — diagonal entry.
                // Off-diagonal leakage is expected for non-orthogonal
                // pairings and is handled by quantization normalization.
                if (i, j) == (0, 0) {
                    assert_eq!(y[0][0], 4096);
                    seen_dc = true;
                }
            }
        }
        assert!(seen_dc);
    }
}