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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! §8.7 — In-loop deblocking adapter for the encoder's local recon.
//!
//! The decoder applies §8.7 deblocking as a single picture-level pass
//! after every macroblock has been reconstructed. The encoder's local
//! recon must mirror the decoder's output, so it has to run the exact
//! same filter — otherwise its "I_Have_The_Decoded_Picture" buffer
//! diverges from what downstream decoders see, which would corrupt
//! reference pictures for any future inter-coded frames *and* produce
//! ffmpeg-interop test failures.
//!
//! This module is a thin adapter:
//!
//! 1. Encoder collects per-MB facts into [`MbDeblockInfo`] entries as
//!    each MB is encoded (intra/non-intra, QP_Y, per-4x4 nonzero mask).
//! 2. After all MBs are written, [`deblock_recon`] wraps the encoder's
//!    plain `Vec<u8>` recon planes in a [`Picture`] (i32 samples) and
//!    the per-MB facts in a [`MbGrid`], then calls
//!    [`crate::reconstruct::deblock_picture_full`] verbatim.
//! 3. The filtered samples are copied back into the encoder's recon
//!    buffers so subsequent operations (and the encoder's exported
//!    `EncodedIdr.recon_*`) see the post-deblock picture.
//!
//! Spec references throughout cite ITU-T Rec. H.264 (08/2024). Clean-room.

use crate::mb_grid::{MbGrid, MbInfo};
use crate::picture::Picture;
use crate::pps::Pps;
use crate::reconstruct::deblock_picture_full;

/// Per-MB facts the deblock walker reads from [`MbInfo`]. The encoder
/// fills one of these per MB as it encodes; [`build_mb_grid`] turns
/// the array into a fully-populated [`MbGrid`].
///
/// Round 14 scope (intra-only, single slice):
/// * `is_intra` is always true for our IDR pictures, but kept here so
///   later rounds (P/B slices) can reuse this adapter.
/// * `mv_l0`/`mv_l1`/`ref_idx_*`/`ref_poc_*` are unused for intra-only
///   content — `different_ref_or_mv_luma` short-circuits on
///   `is_intra && q_is_intra`.
///
/// Round 20 adds the L1 mirror (`mv_l1`, `ref_idx_l1`, `ref_poc_l1`)
/// so that B-slice MBs feed the deblock walker's bipred edge-strength
/// derivation correctly. Intra/P MBs leave the L1 fields at their
/// defaults (ref_idx = -1, mv = 0, poc = i32::MIN), which the walker
/// reads as "no L1 contribution" via the `predFlagL1 = 0` branch in
/// §8.7.2.1.
#[derive(Debug, Clone, Copy)]
pub struct MbDeblockInfo {
    pub is_intra: bool,
    pub qp_y: i32,
    /// §8.7.2.1 — per-4x4-luma-block nonzero-coefficient bitmap, indexed
    /// by §6.4.3 Figure 6-10 Z-scan (bit `n` ↔ 4x4 block at Z-scan
    /// position `n`). For Intra_16x16 this is set to 0xFFFF when
    /// `cbp_luma == 15` (all 4x4 blocks transmitted with non-zero AC) and
    /// 0 when `cbp_luma == 0` (DC-only). For I_NxN it's the per-block
    /// non-zero flag of the 8x8-quadrant-transmitted blocks.
    pub luma_nonzero_4x4: u16,
    /// §8.7.2.1 — per-4x4-chroma-block nonzero bitmap. For 4:2:0 only
    /// bits 0..=3 (Cb) and 8..=11 (Cr) are populated.
    pub chroma_nonzero_4x4: u16,
    /// Round-16: §8.4.1 per-4x4-block L0 MV (1/4-pel units). Used by
    /// the deblock walker's `different_ref_or_mv_luma` to derive
    /// boundary strength on inter/inter edges. For intra MBs this is
    /// unused. For our P_L0_16x16 / P_Skip path the same MV is
    /// replicated to all 16 4x4 blocks.
    pub mv_l0: [(i16, i16); 16],
    /// Round-16: §7.4.5.1 per-8x8-partition L0 ref_idx. `-1` for intra
    /// or L0-unused (B_L1_16x16).
    pub ref_idx_l0: [i8; 4],
    /// Round-16: §8.7.2.1 NOTE 1 per-8x8-partition L0 referenced POC.
    /// `i32::MIN` for intra / L0 unused. For our single-IDR-ref encoder
    /// every P MB references POC 0.
    pub ref_poc_l0: [i32; 4],
    /// Round-20: §8.4.1 per-4x4-block L1 MV (1/4-pel units). Same role
    /// as `mv_l0` but for the second reference list (only meaningful
    /// for B-slice MBs whose pred uses L1).
    pub mv_l1: [(i16, i16); 16],
    /// Round-20: §7.4.5.1 per-8x8-partition L1 ref_idx. `-1` for intra
    /// or L1-unused (B_L0_16x16, every P_L0_* MB).
    pub ref_idx_l1: [i8; 4],
    /// Round-20: §8.7.2.1 NOTE 1 per-8x8-partition L1 referenced POC.
    /// `i32::MIN` for intra / L1 unused.
    pub ref_poc_l1: [i32; 4],
    /// Round-382: §7.4.5 / §8.7 — the MB uses the High-profile 8x8
    /// transform. The §8.7 walker skips the 4x4-internal luma edges
    /// (offsets 4 and 12) for such MBs, so the encoder-side deblock
    /// mirror must carry the flag through to the shared
    /// `deblock_picture_full` grid.
    pub transform_size_8x8_flag: bool,
    /// Round-391 — the MB was coded I_NxN with the 4x4 transform
    /// (Intra_4x4). Not consumed by the §8.7 walker; carried so the
    /// per-picture encoder entry points can report selection counters
    /// (`EncodedIdr::i4x4_mb_count`) that tests pin RDO behaviour on.
    pub is_intra_4x4: bool,
}

impl Default for MbDeblockInfo {
    fn default() -> Self {
        Self {
            is_intra: false,
            qp_y: 0,
            luma_nonzero_4x4: 0,
            chroma_nonzero_4x4: 0,
            mv_l0: [(0, 0); 16],
            ref_idx_l0: [-1; 4],
            ref_poc_l0: [i32::MIN; 4],
            mv_l1: [(0, 0); 16],
            ref_idx_l1: [-1; 4],
            ref_poc_l1: [i32::MIN; 4],
            transform_size_8x8_flag: false,
            is_intra_4x4: false,
        }
    }
}

/// Wrap the encoder's recon planes + per-MB facts into a [`Picture`] +
/// [`MbGrid`], invoke [`deblock_picture_full`], then copy the filtered
/// samples back. After this returns, `recon_y/u/v` reflect the §8.7
/// post-filter picture — what every downstream decoder will reproduce.
///
/// `width`, `height` are luma dimensions in samples (already aligned to
/// 16 by the caller). `chroma_width`/`chroma_height` follow §6.2 (4:2:0
/// — half W and half H).
///
/// `mb_infos` is row-major over (mb_y, mb_x), length `width_mbs *
/// height_mbs`.
///
/// `chroma_qp_index_offset` is the §7.4.2.2 PPS field; the deblock
/// chroma QP derivation needs it.
#[allow(clippy::too_many_arguments)]
pub fn deblock_recon(
    width: u32,
    height: u32,
    chroma_width: u32,
    chroma_height: u32,
    recon_y: &mut [u8],
    recon_u: &mut [u8],
    recon_v: &mut [u8],
    mb_infos: &[MbDeblockInfo],
    chroma_qp_index_offset: i32,
    width_mbs: u32,
    height_mbs: u32,
) {
    deblock_recon_with_chroma_array_type(
        width,
        height,
        chroma_width,
        chroma_height,
        recon_y,
        recon_u,
        recon_v,
        mb_infos,
        chroma_qp_index_offset,
        width_mbs,
        height_mbs,
        /* chroma_array_type */ 1,
    )
}

/// Round-27 — variant of [`deblock_recon`] that takes an explicit
/// `chroma_array_type` (1 = 4:2:0, 2 = 4:2:2). The §8.7 walker reads
/// the chroma layout from `Picture::chroma_array_type`, so picking the
/// right value here is sufficient for the chroma deblock pass to use
/// the 8x16 chroma MB geometry.
#[allow(clippy::too_many_arguments)]
pub fn deblock_recon_with_chroma_array_type(
    width: u32,
    height: u32,
    chroma_width: u32,
    chroma_height: u32,
    recon_y: &mut [u8],
    recon_u: &mut [u8],
    recon_v: &mut [u8],
    mb_infos: &[MbDeblockInfo],
    chroma_qp_index_offset: i32,
    width_mbs: u32,
    height_mbs: u32,
    chroma_array_type: u32,
) {
    deblock_recon_inner(
        width,
        height,
        chroma_width,
        chroma_height,
        recon_y,
        recon_u,
        recon_v,
        mb_infos,
        chroma_qp_index_offset,
        width_mbs,
        height_mbs,
        chroma_array_type,
        /* field_pic */ false,
        None,
    )
}

/// Round-416 — PAFF variant: deblock one coded FIELD picture
/// (`field_pic_flag == 1`). The planes are the field's own half-height
/// samples; the §8.7 pass runs with `field_pic = true` so the walker
/// applies the field-picture bS rules of §8.7.2.1 (a horizontal intra
/// macroblock edge takes bS = 3 — the bS = 4 first bullet requires
/// "both in frame macroblocks" or a vertical edge — and the NOTE 3
/// |Δmv| threshold is 2 in quarter luma FIELD sample units).
#[allow(clippy::too_many_arguments)]
pub fn deblock_recon_field(
    width: u32,
    height: u32,
    chroma_width: u32,
    chroma_height: u32,
    recon_y: &mut [u8],
    recon_u: &mut [u8],
    recon_v: &mut [u8],
    mb_infos: &[MbDeblockInfo],
    chroma_qp_index_offset: i32,
    width_mbs: u32,
    height_mbs: u32,
    chroma_array_type: u32,
) {
    deblock_recon_inner(
        width,
        height,
        chroma_width,
        chroma_height,
        recon_y,
        recon_u,
        recon_v,
        mb_infos,
        chroma_qp_index_offset,
        width_mbs,
        height_mbs,
        chroma_array_type,
        /* field_pic */ true,
        None,
    )
}

/// Round-453 — §8.7 deblock of an **MBAFF frame** (§7.4.2.1.1
/// `mb_adaptive_frame_field_flag = 1`, `field_pic_flag = 0`).
/// `mb_infos` and `mb_field_flags` are indexed by the pair-interleaved
/// macroblock address (§6.4.1: `2 * pairIdx + isBottom`), exactly as
/// the decoder's grid is. The walker applies the MBAFF rules of
/// §8.7.2.1 (mixedModeEdgeFlag, the field-MB top-edge double filter,
/// the NOTE 3 field |Δmv| threshold).
#[allow(clippy::too_many_arguments)]
pub fn deblock_recon_mbaff(
    width: u32,
    height: u32,
    chroma_width: u32,
    chroma_height: u32,
    recon_y: &mut [u8],
    recon_u: &mut [u8],
    recon_v: &mut [u8],
    mb_infos: &[MbDeblockInfo],
    mb_field_flags: &[bool],
    chroma_qp_index_offset: i32,
    width_mbs: u32,
    height_mbs: u32,
    chroma_array_type: u32,
) {
    debug_assert_eq!(mb_infos.len(), mb_field_flags.len());
    deblock_recon_inner(
        width,
        height,
        chroma_width,
        chroma_height,
        recon_y,
        recon_u,
        recon_v,
        mb_infos,
        chroma_qp_index_offset,
        width_mbs,
        height_mbs,
        chroma_array_type,
        /* field_pic */ false,
        Some(mb_field_flags),
    )
}

#[allow(clippy::too_many_arguments)]
fn deblock_recon_inner(
    width: u32,
    height: u32,
    chroma_width: u32,
    chroma_height: u32,
    recon_y: &mut [u8],
    recon_u: &mut [u8],
    recon_v: &mut [u8],
    mb_infos: &[MbDeblockInfo],
    chroma_qp_index_offset: i32,
    width_mbs: u32,
    height_mbs: u32,
    chroma_array_type: u32,
    field_pic: bool,
    mbaff_field_flags: Option<&[bool]>,
) {
    debug_assert_eq!(recon_y.len(), (width as usize) * (height as usize));
    debug_assert_eq!(
        recon_u.len(),
        (chroma_width as usize) * (chroma_height as usize)
    );
    debug_assert_eq!(
        recon_v.len(),
        (chroma_width as usize) * (chroma_height as usize)
    );
    debug_assert_eq!(mb_infos.len(), (width_mbs as usize) * (height_mbs as usize));

    // ------- Build a Picture from the encoder's recon planes. -------
    // Picture stores samples as i32; promote from u8. The
    // `chroma_array_type` argument selects 4:2:0 (1) or 4:2:2 (2)
    // chroma layouts for the deblock walker.
    let mut pic = Picture::new(width, height, chroma_array_type, 8, 8);
    for (dst, src) in pic.luma.iter_mut().zip(recon_y.iter()) {
        *dst = *src as i32;
    }
    for (dst, src) in pic.cb.iter_mut().zip(recon_u.iter()) {
        *dst = *src as i32;
    }
    for (dst, src) in pic.cr.iter_mut().zip(recon_v.iter()) {
        *dst = *src as i32;
    }

    // ------- Build the MbGrid from per-MB facts. -------
    let mut grid = MbGrid::new(width_mbs, height_mbs);
    grid.mbaff_frame_flag = mbaff_field_flags.is_some();
    for (i, info) in mb_infos.iter().enumerate() {
        let mb = &mut grid.info[i];
        // Round-453 — §7.4.4 per-MB field flag (MBAFF frames only).
        mb.mb_field_decoding_flag = mbaff_field_flags.is_some_and(|f| f[i]);
        // §6.4.4 — every MB the encoder produced is "available" for
        // neighbour lookups by the deblock walker.
        mb.available = true;
        mb.is_intra = info.is_intra;
        mb.is_intra_nxn = false; // not used by deblock
        mb.is_i_pcm = false;
        mb.qp_y = info.qp_y;
        mb.luma_nonzero_4x4 = info.luma_nonzero_4x4;
        mb.chroma_nonzero_4x4 = info.chroma_nonzero_4x4;
        mb.transform_size_8x8_flag = info.transform_size_8x8_flag;
        // §6.4.8 third bullet — single slice, slice_id 0 throughout, so
        // no "different slice" neighbour rejections fire.
        mb.slice_id = 0;
        // Round-16: per-4x4 mv / per-8x8 ref_idx + ref_poc for inter MBs.
        // For intra MBs the encoder leaves ref_idx = -1 and the deblock
        // walker's `different_ref_or_mv_luma` is gated on !is_intra.
        mb.mv_l0 = info.mv_l0;
        mb.ref_idx_l0 = info.ref_idx_l0;
        mb.ref_poc_l0 = info.ref_poc_l0;
        // Round-20: same for L1 (B-slice MBs).
        mb.mv_l1 = info.mv_l1;
        mb.ref_idx_l1 = info.ref_idx_l1;
        mb.ref_poc_l1 = info.ref_poc_l1;
    }
    // For unset entries (fully zeroed regions of the grid in pictures
    // that don't fill a power-of-2 — shouldn't happen given the encoder
    // requires 16-pixel-aligned dimensions), MbInfo::default leaves
    // available=false so the deblock walker skips them anyway.
    let _ = MbInfo::default; // marker: see comment above

    // ------- Build a minimal PPS for the deblock pass. -------
    // Only `chroma_qp_index_offset` and (when present)
    // `extension.second_chroma_qp_index_offset` are consulted.
    let pps = Pps {
        pic_parameter_set_id: 0,
        seq_parameter_set_id: 0,
        entropy_coding_mode_flag: false,
        bottom_field_pic_order_in_frame_present_flag: false,
        num_slice_groups_minus1: 0,
        slice_group_map: None,
        num_ref_idx_l0_default_active_minus1: 0,
        num_ref_idx_l1_default_active_minus1: 0,
        weighted_pred_flag: false,
        weighted_bipred_idc: 0,
        pic_init_qp_minus26: 0,
        pic_init_qs_minus26: 0,
        chroma_qp_index_offset,
        deblocking_filter_control_present_flag: true,
        constrained_intra_pred_flag: false,
        redundant_pic_cnt_present_flag: false,
        extension: None,
    };

    // ------- Run the §8.7 picture-level pass. -------
    // alpha/beta offsets default to 0 (slice header writes
    // slice_alpha_c0_offset_div2 = slice_beta_offset_div2 = 0). The
    // walker scales the picture-grid 4x4 edges and bumps bS to 4 on
    // intra MB boundaries automatically — no encoder-side preselection
    // needed.
    deblock_picture_full(
        &mut pic,
        &grid,
        /* alpha_off */ 0,
        /* beta_off */ 0,
        /* bit_depth_y */ 8,
        /* bit_depth_c */ 8,
        &pps,
        /* mbaff_frame_flag */ mbaff_field_flags.is_some(),
        field_pic,
        mbaff_field_flags.unwrap_or(&[]),
    );

    // ------- Copy filtered samples back to the encoder's u8 buffers. -------
    for (dst, src) in recon_y.iter_mut().zip(pic.luma.iter()) {
        *dst = (*src).clamp(0, 255) as u8;
    }
    for (dst, src) in recon_u.iter_mut().zip(pic.cb.iter()) {
        *dst = (*src).clamp(0, 255) as u8;
    }
    for (dst, src) in recon_v.iter_mut().zip(pic.cr.iter()) {
        *dst = (*src).clamp(0, 255) as u8;
    }
}

/// Round-456 — §8.7 picture-level deblocking on **>8-bit** progressive
/// reconstruction planes (samples as `i32`, bit depths per plane):
/// the same `MbGrid` construction as [`deblock_recon`], run through the
/// decoder's bit-depth-aware `deblock_picture_full` (Table 8-16/8-17
/// α / β / tC0 scaled by `1 << (BitDepth − 8)`, §8.7.2.2). Chroma
/// arrays follow `chroma_array_type` (1 / 2 / 3).
#[allow(clippy::too_many_arguments)]
pub fn deblock_recon_deep(
    width: u32,
    height: u32,
    recon_y: &mut [i32],
    recon_u: &mut [i32],
    recon_v: &mut [i32],
    mb_infos: &[MbDeblockInfo],
    chroma_qp_index_offset: i32,
    chroma_array_type: u32,
    bit_depth_luma: u32,
    bit_depth_chroma: u32,
) {
    let width_mbs = width / 16;
    let height_mbs = height / 16;
    debug_assert_eq!(mb_infos.len(), (width_mbs as usize) * (height_mbs as usize));
    let mut pic = Picture::new(
        width,
        height,
        chroma_array_type,
        bit_depth_luma,
        bit_depth_chroma,
    );
    debug_assert_eq!(pic.luma.len(), recon_y.len());
    debug_assert_eq!(pic.cb.len(), recon_u.len());
    pic.luma.copy_from_slice(recon_y);
    pic.cb.copy_from_slice(recon_u);
    pic.cr.copy_from_slice(recon_v);

    let mut grid = MbGrid::new(width_mbs, height_mbs);
    for (i, info) in mb_infos.iter().enumerate() {
        let mb = &mut grid.info[i];
        mb.available = true;
        mb.is_intra = info.is_intra;
        mb.is_intra_nxn = false;
        mb.is_i_pcm = false;
        mb.qp_y = info.qp_y;
        mb.luma_nonzero_4x4 = info.luma_nonzero_4x4;
        mb.chroma_nonzero_4x4 = info.chroma_nonzero_4x4;
        mb.transform_size_8x8_flag = info.transform_size_8x8_flag;
        mb.slice_id = 0;
        mb.mv_l0 = info.mv_l0;
        mb.ref_idx_l0 = info.ref_idx_l0;
        mb.ref_poc_l0 = info.ref_poc_l0;
        mb.mv_l1 = info.mv_l1;
        mb.ref_idx_l1 = info.ref_idx_l1;
        mb.ref_poc_l1 = info.ref_poc_l1;
    }
    let pps = Pps {
        pic_parameter_set_id: 0,
        seq_parameter_set_id: 0,
        entropy_coding_mode_flag: false,
        bottom_field_pic_order_in_frame_present_flag: false,
        num_slice_groups_minus1: 0,
        slice_group_map: None,
        num_ref_idx_l0_default_active_minus1: 0,
        num_ref_idx_l1_default_active_minus1: 0,
        weighted_pred_flag: false,
        weighted_bipred_idc: 0,
        pic_init_qp_minus26: 0,
        pic_init_qs_minus26: 0,
        chroma_qp_index_offset,
        deblocking_filter_control_present_flag: true,
        constrained_intra_pred_flag: false,
        redundant_pic_cnt_present_flag: false,
        extension: None,
    };
    deblock_picture_full(
        &mut pic,
        &grid,
        0,
        0,
        bit_depth_luma,
        bit_depth_chroma,
        &pps,
        false,
        false,
        &[],
    );
    recon_y.copy_from_slice(&pic.luma);
    recon_u.copy_from_slice(&pic.cb);
    recon_v.copy_from_slice(&pic.cr);
}

/// Convenience helper: derive a `luma_nonzero_4x4` mask from a per-block
/// "has any non-zero AC coefficient" array (raster-Z order, length 16).
///
/// For Intra_16x16 with `cbp_luma == 15`, callers should pass the per-
/// block flag derived from the actual scan-order coefficients (not
/// blanket 0xFFFF) — even with `cbp_luma == 15` an individual 4x4 may
/// quantise to all-zero AC. The deblock walker checks each block
/// independently per §8.7.2.1.
#[inline]
pub fn luma_nz_mask_from_blocks(blk_has_nz: &[bool; 16]) -> u16 {
    let mut m: u16 = 0;
    for (i, &nz) in blk_has_nz.iter().enumerate() {
        if nz {
            m |= 1u16 << i;
        }
    }
    m
}

/// Convenience helper: chroma nonzero mask from per-4x4-block AC nz
/// arrays (Cb then Cr). Layout matches §8.7.2.1: low 4 bits = Cb,
/// next 4 bits at offset 8 = Cr.
#[inline]
pub fn chroma_nz_mask_from_blocks(cb_has_nz: &[bool; 4], cr_has_nz: &[bool; 4]) -> u16 {
    let mut m: u16 = 0;
    for (i, &nz) in cb_has_nz.iter().enumerate() {
        if nz {
            m |= 1u16 << i;
        }
    }
    for (i, &nz) in cr_has_nz.iter().enumerate() {
        if nz {
            m |= 1u16 << (8 + i);
        }
    }
    m
}

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

    /// Two flat-128 macroblocks in a row → no AC anywhere → bS=4 on the
    /// internal MB edge for intra/intra (always-bS=4 for MB-edge intra,
    /// even with no coefficients) → the strong filter activates only
    /// when `|p0-q0| < α` etc. Since both sides are flat 128, the
    /// filter copies samples unchanged.
    #[test]
    fn flat_picture_unchanged() {
        let mut y = vec![128u8; 32 * 16];
        let mut u = vec![128u8; 16 * 8];
        let mut v = vec![128u8; 16 * 8];
        let mb_infos = vec![
            MbDeblockInfo {
                is_intra: true,
                qp_y: 26,
                luma_nonzero_4x4: 0,
                chroma_nonzero_4x4: 0,
                ..Default::default()
            },
            MbDeblockInfo {
                is_intra: true,
                qp_y: 26,
                luma_nonzero_4x4: 0,
                chroma_nonzero_4x4: 0,
                ..Default::default()
            },
        ];
        deblock_recon(32, 16, 16, 8, &mut y, &mut u, &mut v, &mb_infos, 0, 2, 1);
        assert!(y.iter().all(|&v| v == 128));
        assert!(u.iter().all(|&v| v == 128));
        assert!(v.iter().all(|&v| v == 128));
    }

    /// Deliberate block-boundary discontinuity at x=16 (left MB = 124,
    /// right MB = 132, well within the bS=4 strong-filter activation
    /// band at QP=26 → α=38, β=10 per Table 8-16). Confirm the deblock
    /// pass smooths the boundary.
    #[test]
    fn block_boundary_discontinuity_smoothed() {
        let mut y = vec![124u8; 32 * 16];
        let u = vec![128u8; 16 * 8];
        let v = vec![128u8; 16 * 8];
        for j in 0..16 {
            for i in 16..32 {
                y[j * 32 + i] = 132;
            }
        }
        let mut yy = y.clone();
        let mut uu = u.clone();
        let mut vv = v.clone();
        let mb_infos = vec![
            MbDeblockInfo {
                is_intra: true,
                qp_y: 26,
                luma_nonzero_4x4: 0,
                chroma_nonzero_4x4: 0,
                ..Default::default()
            },
            MbDeblockInfo {
                is_intra: true,
                qp_y: 26,
                luma_nonzero_4x4: 0,
                chroma_nonzero_4x4: 0,
                ..Default::default()
            },
        ];
        deblock_recon(32, 16, 16, 8, &mut yy, &mut uu, &mut vv, &mb_infos, 0, 2, 1);
        // Edge samples should now be intermediate (not 124 / 132).
        // Sample at (15, 0) = p0 of vertical edge x=16.
        let p0 = yy[15];
        let q0 = yy[16];
        assert!(
            p0 != 124 || q0 != 132,
            "deblock did not modify the block-boundary discontinuity",
        );
        // Smoothing should pull p0 up toward q0 and q0 down toward p0.
        assert!(p0 > 124, "p0={p0} did not move up");
        assert!(q0 < 132, "q0={q0} did not move down");
    }

    #[test]
    fn luma_nz_mask_packs_correctly() {
        let mut nz = [false; 16];
        nz[0] = true;
        nz[5] = true;
        nz[15] = true;
        assert_eq!(
            luma_nz_mask_from_blocks(&nz),
            (1u16 << 0) | (1u16 << 5) | (1u16 << 15),
        );
    }

    #[test]
    fn chroma_nz_mask_packs_cb_then_cr() {
        let cb = [true, false, false, true];
        let cr = [false, true, false, false];
        assert_eq!(
            chroma_nz_mask_from_blocks(&cb, &cr),
            (1u16 << 0) | (1u16 << 3) | (1u16 << 9),
        );
    }
}