Skip to main content

threers_codec/h264/
inter.rs

1//! H.264 with P-frames: an IDR picture every so often, and between them
2//! pictures predicted from the one before.
3//!
4//! [`CompressedEncoder`](super::CompressedEncoder) codes every picture on its
5//! own, which keeps encoding parallel per frame but spends the same bits on a
6//! static background in every frame. Here the pictures between keyframes are
7//! P slices: each 16×16 macroblock is either *skipped* (copied from the
8//! previous picture along the predicted motion vector) or predicted from it
9//! along a searched motion vector, with only the residual coded.
10//!
11//! * Keyframes are [`super::compress::compressed_slice_rbsp`]
12//!   IDR pictures — CABAC, deblocked — under PPS 0.
13//! * P pictures are `P_L0_16x16` / `P_Skip` macroblocks, one reference
14//!   picture, the deblocking filter off: CABAC under PPS 0 (the default), or
15//!   CAVLC under PPS 1.
16//! * Motion vectors are in quarter luma samples ([`super::interp`]'s 6-tap
17//!   interpolation); the search is whole samples (a diamond),
18//!   then half and quarter samples by SATD, on several threads, one band of
19//!   macroblock rows each.
20//! * A picture that does not match the one before it (a cut) is coded as a
21//!   keyframe instead.
22//!
23//! The encoder's own reconstruction is what a decoder reproduces, sample for
24//! sample: `tests/h264_inter.rs` checks it against threers' decoder and ffmpeg.
25
26use crate::bitstream::{emulation_prevention, rbsp_trailing_bits, BitWriter};
27use crate::h264::cabac::{self as cabac_syntax, Cabac};
28use crate::h264::cavlc::{self, BlockKind, ZIGZAG4};
29use crate::h264::compress::{compressed_slice_rbsp, Reconstruction};
30use crate::h264::interp::HalfPlanes;
31use crate::h264::nal::{nal_unit_base, NalUnitType};
32use crate::h264::params::{write_sps_with_refs, EntropyMode, H264Config, MB_SIZE};
33use crate::h264::pps::write_pps_with_id;
34use crate::h264::transform::{
35    chroma_dc_forward, chroma_dc_inverse, dequant4, forward4, inverse4, quant4,
36};
37use crate::hevc::encoder::{pad_plane, Yuv420Frame};
38
39impl Reference {
40    fn new(rec: &Reconstruction, mvs: Vec<[i16; 2]>) -> Reference {
41        let threads = if cfg!(target_family = "wasm") {
42            1
43        } else {
44            std::thread::available_parallelism()
45                .map_or(1, |n| n.get())
46                .min(16)
47        };
48        let halves = HalfPlanes::new(
49            &rec.y,
50            rec.coded_width as usize,
51            rec.coded_height as usize,
52            threads,
53        );
54        Reference {
55            y: rec.y.clone(),
56            u: rec.u.clone(),
57            v: rec.v.clone(),
58            halves,
59            mvs,
60        }
61    }
62}
63
64/// Table 9-4, inter column: code number → `coded_block_pattern`.
65const CBP_INTER_FROM_CODE: [u8; 48] = [
66    0, 16, 1, 2, 4, 8, 32, 3, 5, 10, 12, 15, 47, 7, 11, 13, 14, 6, 9, 31, 35, 37, 42, 44, 33, 34,
67    36, 40, 39, 43, 45, 46, 17, 18, 20, 24, 19, 21, 26, 28, 23, 27, 29, 30, 22, 25, 38, 41,
68];
69
70const fn cbp_inter_code() -> [u32; 48] {
71    let mut out = [0u32; 48];
72    let mut i = 0;
73    while i < 48 {
74        out[CBP_INTER_FROM_CODE[i] as usize] = i as u32;
75        i += 1;
76    }
77    out
78}
79const CBP_INTER_CODE: [u32; 48] = cbp_inter_code();
80
81/// The 4×4 luma blocks in bitstream order (Table 6-3), in 4-sample units.
82const BLK_POS: [(usize, usize); 16] = [
83    (0, 0),
84    (1, 0),
85    (0, 1),
86    (1, 1),
87    (2, 0),
88    (3, 0),
89    (2, 1),
90    (3, 1),
91    (0, 2),
92    (1, 2),
93    (0, 3),
94    (1, 3),
95    (2, 2),
96    (3, 2),
97    (2, 3),
98    (3, 3),
99];
100
101/// Chroma QP from luma QP (Table 8-15), `chroma_qp_index_offset = 0`.
102fn chroma_qp(qp: i32) -> i32 {
103    const T: [i32; 22] = [
104        29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39,
105    ];
106    let q = qp.clamp(0, 51);
107    if q < 30 {
108        q
109    } else {
110        T[(q - 30) as usize]
111    }
112}
113
114/// The SSD Lagrangian the intra encoder uses, and its square root for SAD.
115fn lambda(qp: i32) -> f64 {
116    0.85 * (f64::from(qp - 12) / 3.0).exp2()
117}
118
119/// Bits in a `se(v)` code.
120fn se_bits(v: i32) -> u32 {
121    let code = if v > 0 {
122        2 * v as u32 - 1
123    } else {
124        2 * (-v) as u32
125    };
126    2 * (32 - (code + 1).leading_zeros() - 1) + 1
127}
128
129/// One encoded picture.
130#[derive(Debug, Clone)]
131pub struct EncodedPicture {
132    /// The access unit as an MP4 sample: 4-byte length-prefixed NAL units.
133    pub sample: Vec<u8>,
134    /// An IDR picture: decoding can start here.
135    pub keyframe: bool,
136}
137
138/// A reference picture, at coded (macroblock-aligned) size.
139struct Reference {
140    y: Vec<u8>,
141    u: Vec<u8>,
142    v: Vec<u8>,
143    /// The luma's half-sample planes, for quarter-sample prediction.
144    halves: HalfPlanes,
145    /// Each macroblock's motion vector (quarter samples), for the next
146    /// picture's search to start from.
147    mvs: Vec<[i16; 2]>,
148}
149
150/// H.264 with P-frames. See the [module docs](self).
151pub struct InterEncoder {
152    cfg: H264Config,
153    qp: i32,
154    sps: Vec<u8>,
155    pps_intra: Vec<u8>,
156    pps_inter: Vec<u8>,
157    keyframe_interval: u32,
158    entropy: EntropyMode,
159    scene_cut: f64,
160    search_range: i32,
161    threads: usize,
162    reference: Option<Reference>,
163    /// Pictures since the last keyframe (the keyframe is 0).
164    since_key: u32,
165    force_key: bool,
166    last: Option<Reconstruction>,
167    /// The last P picture's mean absolute difference after motion search.
168    last_mad: Option<f64>,
169}
170
171impl InterEncoder {
172    /// `width × height` at quantizer `qp` (lower is better and bigger; 18–40
173    /// is the useful range).
174    pub fn new(width: u32, height: u32, qp: i32) -> Self {
175        let mut cfg = H264Config::new(width, height);
176        cfg.qp = qp.clamp(0, 51);
177        cfg.entropy = EntropyMode::Cabac;
178        cfg.deblock = true;
179        let sps = nal_unit_base(NalUnitType::Sps, &write_sps_with_refs(&cfg, 1));
180        let pps_intra = nal_unit_base(NalUnitType::Pps, &write_pps_with_id(0, true));
181        let pps_inter = nal_unit_base(NalUnitType::Pps, &write_pps_with_id(1, false));
182        let threads = if cfg!(target_family = "wasm") {
183            1
184        } else {
185            std::thread::available_parallelism()
186                .map_or(1, |n| n.get())
187                .min(16)
188        };
189        InterEncoder {
190            qp: cfg.qp,
191            cfg,
192            sps,
193            pps_intra,
194            pps_inter,
195            keyframe_interval: 50,
196            entropy: EntropyMode::Cabac,
197            scene_cut: 28.0,
198            search_range: 48,
199            threads,
200            reference: None,
201            since_key: 0,
202            force_key: false,
203            last: None,
204            last_mad: None,
205        }
206    }
207
208    /// A keyframe at least every `n` pictures (default 50: two seconds at
209    /// 25 fps). Seeking lands on keyframes, so shorter is faster to seek and
210    /// bigger. `1` is all keyframes.
211    pub fn keyframe_interval(mut self, n: u32) -> Self {
212        self.keyframe_interval = n.max(1);
213        self
214    }
215
216    /// The P slices' entropy coder: CABAC (the default; Main profile, as the
217    /// keyframes already are) or CAVLC, about 10–15 % larger.
218    pub fn entropy(mut self, mode: EntropyMode) -> Self {
219        self.entropy = mode;
220        self
221    }
222
223    /// Mean absolute difference per luma sample, after motion search, above
224    /// which a picture is taken to be a cut and coded as a keyframe
225    /// (default 28). A jump to three times the previous picture's (and over
226    /// 12) is a cut too. `f64::INFINITY` turns the absolute test off.
227    pub fn scene_cut_threshold(mut self, mad: f64) -> Self {
228        self.scene_cut = mad;
229        self
230    }
231
232    /// Motion search range in luma samples, each way (default 48).
233    pub fn search_range(mut self, samples: i32) -> Self {
234        self.search_range = samples.clamp(1, 512);
235        self
236    }
237
238    /// Threads for motion search (default: the machine's cores, up to 16).
239    pub fn threads(mut self, n: usize) -> Self {
240        self.threads = n.max(1);
241        self
242    }
243
244    /// Make the next picture a keyframe.
245    pub fn force_keyframe(&mut self) {
246        self.force_key = true;
247    }
248
249    /// The configuration the parameter sets describe.
250    pub fn config(&self) -> &H264Config {
251        &self.cfg
252    }
253
254    /// The sequence parameter set NAL unit: header included, no start code.
255    pub fn sps(&self) -> &[u8] {
256        &self.sps
257    }
258
259    /// Both picture parameter sets: the intra one (id 0), the inter one (id 1).
260    pub fn pps(&self) -> [&[u8]; 2] {
261        [&self.pps_intra, &self.pps_inter]
262    }
263
264    /// The `avcC` decoder configuration, with the SPS and both PPSs.
265    pub fn avcc(&self) -> Vec<u8> {
266        let mut out = vec![1, self.sps[1], self.sps[2], self.sps[3], 0xff, 0xe1];
267        out.extend_from_slice(&(self.sps.len() as u16).to_be_bytes());
268        out.extend_from_slice(&self.sps);
269        out.push(2);
270        for pps in [&self.pps_intra, &self.pps_inter] {
271            out.extend_from_slice(&(pps.len() as u16).to_be_bytes());
272            out.extend_from_slice(pps);
273        }
274        out
275    }
276
277    /// The picture a decoder reconstructs from the last [`encode`](Self::encode),
278    /// at coded size.
279    pub fn reconstruction(&self) -> Option<&Reconstruction> {
280        self.last.as_ref()
281    }
282
283    /// Encode the next picture, in display order.
284    pub fn encode(&mut self, frame: &Yuv420Frame) -> EncodedPicture {
285        assert_eq!(
286            (frame.width, frame.height),
287            (self.cfg.width, self.cfg.height),
288            "frame size differs from encoder size"
289        );
290        let (cw, ch) = (self.cfg.coded_width, self.cfg.coded_height);
291        let y = pad_plane(&frame.y, frame.width, frame.height, cw, ch);
292        let u = pad_plane(&frame.u, frame.width / 2, frame.height / 2, cw / 2, ch / 2);
293        let v = pad_plane(&frame.v, frame.width / 2, frame.height / 2, cw / 2, ch / 2);
294
295        let due = self.force_key
296            || self.reference.is_none()
297            || self.since_key + 1 >= self.keyframe_interval;
298        if !due {
299            let reference = self.reference.as_ref().unwrap();
300            let search = motion_search(
301                &y,
302                reference,
303                cw,
304                ch,
305                self.qp,
306                self.search_range,
307                self.threads,
308            );
309            let mad = search.iter().map(|m| m.sad as f64).sum::<f64>() / (cw * ch) as f64;
310            // A cut: worse than the threshold, or a sudden jump from how
311            // well the last pictures matched.
312            let jump = self
313                .last_mad
314                .is_some_and(|prev| mad > 12.0 && mad > 3.0 * prev.max(2.0));
315            if mad <= self.scene_cut && !jump {
316                self.last_mad = Some(mad);
317                let frame_num = (self.since_key + 1) % 16;
318                let (rbsp, rec, mvs) = p_slice(
319                    &self.cfg,
320                    self.entropy,
321                    self.qp,
322                    frame_num,
323                    &y,
324                    &u,
325                    &v,
326                    reference,
327                    &search,
328                );
329                self.since_key += 1;
330                let nal = nal(1, 2, &rbsp);
331                self.reference = Some(Reference::new(&rec, mvs));
332                self.last = Some(rec);
333                return EncodedPicture {
334                    sample: length_prefixed(&[&nal]),
335                    keyframe: false,
336                };
337            }
338        }
339        self.force_key = false;
340        self.since_key = 0;
341        self.last_mad = None;
342        let (rbsp, rec) = compressed_slice_rbsp(&self.cfg, &y, &u, &v, self.qp);
343        let mbs = (cw / MB_SIZE * (ch / MB_SIZE)) as usize;
344        self.reference = Some(Reference::new(&rec, vec![[0, 0]; mbs]));
345        self.last = Some(rec);
346        let nal = nal_unit_base(NalUnitType::Idr, &rbsp);
347        EncodedPicture {
348            sample: length_prefixed(&[&nal]),
349            keyframe: true,
350        }
351    }
352}
353
354fn nal(kind: u8, ref_idc: u8, rbsp: &[u8]) -> Vec<u8> {
355    let mut out = vec![(ref_idc << 5) | kind];
356    out.extend_from_slice(&emulation_prevention(rbsp));
357    out
358}
359
360fn length_prefixed(nals: &[&[u8]]) -> Vec<u8> {
361    let mut out = Vec::new();
362    for n in nals {
363        out.extend_from_slice(&(n.len() as u32).to_be_bytes());
364        out.extend_from_slice(n);
365    }
366    out
367}
368
369// ── Motion search ─────────────────────────────────────────────────────────────
370
371/// One macroblock's search result: the best whole-sample vector (quarter
372/// units) and its luma SAD.
373#[derive(Clone, Copy, Debug)]
374struct Searched {
375    mv: [i16; 2],
376    sad: u32,
377}
378
379/// 16×16 SAD of `orig` at `(x, y)` against `refp` displaced by whole samples
380/// `(dx, dy)`, edges clamped as a decoder does.
381fn sad16(
382    orig: &[u8],
383    refp: &[u8],
384    w: u32,
385    h: u32,
386    x: u32,
387    y: u32,
388    dx: i32,
389    dy: i32,
390    limit: u32,
391) -> u32 {
392    let (rx, ry) = (x as i32 + dx, y as i32 + dy);
393    let inside = rx >= 0 && ry >= 0 && rx + 16 <= w as i32 && ry + 16 <= h as i32;
394    let mut sad = 0u32;
395    for j in 0..16 {
396        let o = &orig[((y + j) * w + x) as usize..][..16];
397        if inside {
398            let r = &refp[((ry as u32 + j) * w + rx as u32) as usize..][..16];
399            sad += o
400                .iter()
401                .zip(r)
402                .map(|(a, b)| a.abs_diff(*b) as u32)
403                .sum::<u32>();
404        } else {
405            let yy = (ry + j as i32).clamp(0, h as i32 - 1) as u32;
406            for (i, a) in o.iter().enumerate() {
407                let xx = (rx + i as i32).clamp(0, w as i32 - 1) as u32;
408                sad += a.abs_diff(refp[(yy * w + xx) as usize]) as u32;
409            }
410        }
411        if sad >= limit {
412            return sad;
413        }
414    }
415    sad
416}
417
418/// Sum of absolute 4×4 Hadamard-transformed differences over a 16×16 block.
419fn satd16(orig: &[i32; 256], pred: &[i32; 256]) -> u32 {
420    let mut total = 0u32;
421    for by in 0..4 {
422        for bx in 0..4 {
423            let mut d = [0i32; 16];
424            for j in 0..4 {
425                for i in 0..4 {
426                    let k = (by * 4 + j) * 16 + bx * 4 + i;
427                    d[j * 4 + i] = orig[k] - pred[k];
428                }
429            }
430            let mut t = [0i32; 16];
431            for j in 0..4 {
432                let (a, b, c, e) = (d[j * 4], d[j * 4 + 1], d[j * 4 + 2], d[j * 4 + 3]);
433                let (s0, s1, d0, d1) = (a + e, b + c, a - e, b - c);
434                t[j * 4] = s0 + s1;
435                t[j * 4 + 1] = d0 + d1;
436                t[j * 4 + 2] = s0 - s1;
437                t[j * 4 + 3] = d0 - d1;
438            }
439            for i in 0..4 {
440                let (a, b, c, e) = (t[i], t[4 + i], t[8 + i], t[12 + i]);
441                let (s0, s1, d0, d1) = (a + e, b + c, a - e, b - c);
442                total +=
443                    ((s0 + s1).abs() + (d0 + d1).abs() + (s0 - s1).abs() + (d0 - d1).abs()) as u32;
444            }
445        }
446    }
447    total / 2
448}
449
450/// Search every macroblock against the reference, bands of rows in parallel.
451/// The cost adds a rate term for the vector's distance from the same
452/// macroblock's vector in the previous picture (the spatial predictor is not
453/// known until the picture is coded in order).
454fn motion_search(
455    y: &[u8],
456    r: &Reference,
457    cw: u32,
458    ch: u32,
459    qp: i32,
460    range: i32,
461    threads: usize,
462) -> Vec<Searched> {
463    let (mbw, mbh) = ((cw / 16) as usize, (ch / 16) as usize);
464    let lambda_sad = lambda(qp).sqrt();
465    let search_mb = |mbx: usize, mby: usize| -> Searched {
466        let (x, y0) = ((mbx * 16) as u32, (mby * 16) as u32);
467        let at = |m: [i32; 2], limit: u32| sad16(y, &r.y, cw, ch, x, y0, m[0], m[1], limit);
468        let prev = r.mvs[mby * mbw + mbx];
469        let pred = [prev[0] as i32 / 4, prev[1] as i32 / 4];
470        let cost = |m: [i32; 2], sad: u32| {
471            sad as f64
472                + lambda_sad
473                    * (se_bits((m[0] - pred[0]) * 4) + se_bits((m[1] - pred[1]) * 4)) as f64
474        };
475        let mut cands = vec![[0, 0], pred];
476        for (dx, dy) in [(-1i32, 0i32), (0, -1), (1, 0), (0, 1)] {
477            let (nx, ny) = (mbx as i32 + dx, mby as i32 + dy);
478            if nx >= 0 && ny >= 0 && (nx as usize) < mbw && (ny as usize) < mbh {
479                let m = r.mvs[ny as usize * mbw + nx as usize];
480                cands.push([m[0] as i32 / 4, m[1] as i32 / 4]);
481            }
482        }
483        let (mut best, mut best_sad) = ([0, 0], u32::MAX);
484        let mut best_cost = f64::INFINITY;
485        for c in cands {
486            let s = at(c, u32::MAX);
487            let k = cost(c, s);
488            if k < best_cost {
489                (best, best_sad, best_cost) = (c, s, k);
490            }
491        }
492        // Diamond search, coarse to fine.
493        for step in [8, 4, 2, 1] {
494            for _ in 0..12 {
495                let mut moved = false;
496                for (dx, dy) in [(step, 0), (-step, 0), (0, step), (0, -step)] {
497                    let m = [best[0] + dx, best[1] + dy];
498                    if m[0].abs() > range || m[1].abs() > range {
499                        continue;
500                    }
501                    let s = at(m, best_sad.saturating_add(256));
502                    let k = cost(m, s);
503                    if k < best_cost {
504                        (best, best_sad, best_cost) = (m, s, k);
505                        moved = true;
506                    }
507                }
508                if !moved {
509                    break;
510                }
511            }
512        }
513        // Half then quarter samples around the best whole-sample vector,
514        // judged by SATD — which tracks the residual's coded size better
515        // than SAD where interpolation smooths the prediction.
516        let orig: [i32; 256] = gather(y, cw, x, y0, 16);
517        let pred_q = [pred[0] * 4, pred[1] * 4];
518        let qcost = |m: [i32; 2], satd: u32| {
519            satd as f64
520                + lambda_sad * (se_bits(m[0] - pred_q[0]) + se_bits(m[1] - pred_q[1])) as f64
521        };
522        let mut q = [best[0] * 4, best[1] * 4];
523        let mut q_satd = satd16(
524            &orig,
525            &predict_luma(r, cw, ch, x, y0, [q[0] as i16, q[1] as i16]),
526        );
527        let mut q_cost = qcost(q, q_satd);
528        for step in [2, 1] {
529            let centre = q;
530            for (dx, dy) in [
531                (-1, -1),
532                (0, -1),
533                (1, -1),
534                (-1, 0),
535                (1, 0),
536                (-1, 1),
537                (0, 1),
538                (1, 1),
539            ] {
540                let m = [centre[0] + dx * step, centre[1] + dy * step];
541                if m[0].abs() > range * 4 + 3 || m[1].abs() > range * 4 + 3 {
542                    continue;
543                }
544                let s = satd16(
545                    &orig,
546                    &predict_luma(r, cw, ch, x, y0, [m[0] as i16, m[1] as i16]),
547                );
548                let k = qcost(m, s);
549                if k < q_cost {
550                    (q, q_satd, q_cost) = (m, s, k);
551                }
552            }
553        }
554        let _ = q_satd;
555        Searched {
556            mv: [q[0] as i16, q[1] as i16],
557            sad: best_sad,
558        }
559    };
560    let mut out = vec![Searched { mv: [0, 0], sad: 0 }; mbw * mbh];
561    let rows_each = mbh.div_ceil(threads.max(1));
562    if threads <= 1 || mbh < 2 {
563        for (i, o) in out.iter_mut().enumerate() {
564            *o = search_mb(i % mbw, i / mbw);
565        }
566        return out;
567    }
568    std::thread::scope(|scope| {
569        for (band, chunk) in out.chunks_mut(rows_each * mbw).enumerate() {
570            let search_mb = &search_mb;
571            scope.spawn(move || {
572                for (i, o) in chunk.iter_mut().enumerate() {
573                    let idx = band * rows_each * mbw + i;
574                    *o = search_mb(idx % mbw, idx / mbw);
575                }
576            });
577        }
578    });
579    out
580}
581
582// ── Prediction ────────────────────────────────────────────────────────────────
583
584/// Luma prediction at a quarter-sample vector (§8.4.2.2.1): whole-sample
585/// vectors copy, fractional ones are interpolated ([`super::interp`]).
586fn predict_luma(r: &Reference, w: u32, h: u32, x: u32, y: u32, mv: [i16; 2]) -> [i32; 256] {
587    if mv[0] & 3 != 0 || mv[1] & 3 != 0 {
588        let mut out = [0i32; 256];
589        let (fx, fy) = ((mv[0] & 3) as usize, (mv[1] & 3) as usize);
590        r.halves.block(
591            x as isize + (mv[0] as isize >> 2),
592            y as isize + (mv[1] as isize >> 2),
593            fx,
594            fy,
595            16,
596            16,
597            &mut out,
598        );
599        return out;
600    }
601    let refp = &r.y;
602    let (dx, dy) = (mv[0] as i32 >> 2, mv[1] as i32 >> 2);
603    let mut out = [0i32; 256];
604    let (rx, ry) = (x as i32 + dx, y as i32 + dy);
605    if rx >= 0 && ry >= 0 && rx + 16 <= w as i32 && ry + 16 <= h as i32 {
606        for j in 0..16 {
607            let row = &refp[((ry as u32 + j) * w + rx as u32) as usize..][..16];
608            for (o, &p) in out[(j * 16) as usize..][..16].iter_mut().zip(row) {
609                *o = p as i32;
610            }
611        }
612        return out;
613    }
614    for j in 0..16 {
615        let yy = (y as i32 + j + dy).clamp(0, h as i32 - 1) as u32;
616        for i in 0..16 {
617            let xx = (x as i32 + i + dx).clamp(0, w as i32 - 1) as u32;
618            out[(j * 16 + i) as usize] = refp[(yy * w + xx) as usize] as i32;
619        }
620    }
621    out
622}
623
624/// Chroma prediction (§8.4.2.2.2): the luma vector is in eighth chroma
625/// samples, interpolated bilinearly.
626fn predict_chroma(refp: &[u8], w: u32, h: u32, cx: u32, cy: u32, mv: [i16; 2]) -> [i32; 64] {
627    let (mx, my) = (mv[0] as i32, mv[1] as i32);
628    let (fx, fy) = (mx & 7, my & 7);
629    let (ix, iy) = (cx as i32 + (mx >> 3), cy as i32 + (my >> 3));
630    let mut out = [0i32; 64];
631    if ix >= 0 && iy >= 0 && ix + 9 <= w as i32 && iy + 9 <= h as i32 {
632        let (wa, wb, wc, wd) = ((8 - fx) * (8 - fy), fx * (8 - fy), (8 - fx) * fy, fx * fy);
633        for j in 0..8 {
634            let r0 = &refp[((iy + j) as u32 * w + ix as u32) as usize..][..9];
635            let r1 = &refp[((iy + j + 1) as u32 * w + ix as u32) as usize..][..9];
636            for i in 0..8 {
637                out[(j * 8 + i) as usize] = (wa * r0[i as usize] as i32
638                    + wb * r0[i as usize + 1] as i32
639                    + wc * r1[i as usize] as i32
640                    + wd * r1[i as usize + 1] as i32
641                    + 32)
642                    >> 6;
643            }
644        }
645        return out;
646    }
647    let at = |x: i32, y: i32| {
648        refp[(y.clamp(0, h as i32 - 1) as u32 * w + x.clamp(0, w as i32 - 1) as u32) as usize]
649            as i32
650    };
651    for j in 0..8 {
652        for i in 0..8 {
653            let (x, y) = (ix + i, iy + j);
654            let (a, b, c, d) = (at(x, y), at(x + 1, y), at(x, y + 1), at(x + 1, y + 1));
655            out[(j * 8 + i) as usize] = ((8 - fx) * (8 - fy) * a
656                + fx * (8 - fy) * b
657                + (8 - fx) * fy * c
658                + fx * fy * d
659                + 32)
660                >> 6;
661        }
662    }
663    out
664}
665
666// ── The P slice ───────────────────────────────────────────────────────────────
667
668/// What a coded (not skipped) macroblock carries.
669struct Coded {
670    mv: [i16; 2],
671    cbp: u32,
672    /// Zig-zag luma levels per block, in bitstream order.
673    luma: [[i32; 16]; 16],
674    chroma_dc: [[i32; 4]; 2],
675    chroma_ac: [[[i32; 15]; 4]; 2],
676    rec_y: [i32; 256],
677    rec_c: [[i32; 64]; 2],
678}
679
680/// Per-picture coding state.
681struct State {
682    cw: u32,
683    mbw: usize,
684    mbh: usize,
685    nnz_y: Vec<usize>,
686    nnz_c: [Vec<usize>; 2],
687    /// Motion per macroblock once coded: `None` before it is.
688    mv: Vec<Option<[i16; 2]>>,
689    // What CABAC's contexts ask of neighbours, per macroblock and block.
690    skip: Vec<bool>,
691    cbp: Vec<u32>,
692    /// |mvd| per component.
693    mvd: Vec<[u32; 2]>,
694    /// coded_block_flag per 4×4 luma block (picture grid), false where the
695    /// block was not coded.
696    cbf_y: Vec<bool>,
697    cbf_c: [Vec<bool>; 2],
698    cbf_dc: Vec<[bool; 2]>,
699}
700
701impl State {
702    fn nb(&self, mbx: isize, mby: isize) -> Option<[i16; 2]> {
703        if mbx < 0 || mby < 0 || mbx as usize >= self.mbw || mby as usize >= self.mbh {
704            return None;
705        }
706        self.mv[mby as usize * self.mbw + mbx as usize]
707    }
708
709    /// The 16×16 partition's motion vector predictor (§8.4.1.3), every
710    /// available neighbour being an inter macroblock on reference 0.
711    fn mvp(&self, mbx: usize, mby: usize) -> [i16; 2] {
712        let (x, y) = (mbx as isize, mby as isize);
713        let a = self.nb(x - 1, y);
714        let b = self.nb(x, y - 1);
715        let c = self.nb(x + 1, y - 1).or_else(|| self.nb(x - 1, y - 1));
716        if let (Some(a), None, None) = (a, b, c) {
717            return a;
718        }
719        let avail = [a, b, c];
720        if avail.iter().filter(|m| m.is_some()).count() == 1 {
721            return avail.into_iter().flatten().next().unwrap();
722        }
723        let (a, b, c) = (
724            a.unwrap_or([0, 0]),
725            b.unwrap_or([0, 0]),
726            c.unwrap_or([0, 0]),
727        );
728        let med = |p: i16, q: i16, s: i16| p.max(q).min(p.min(q).max(s));
729        [med(a[0], b[0], c[0]), med(a[1], b[1], c[1])]
730    }
731
732    /// `P_Skip`'s vector (§8.4.1.1).
733    fn skip_mv(&self, mbx: usize, mby: usize) -> [i16; 2] {
734        let (x, y) = (mbx as isize, mby as isize);
735        match (self.nb(x - 1, y), self.nb(x, y - 1)) {
736            (None, _) | (_, None) => [0, 0],
737            (Some([0, 0]), _) | (_, Some([0, 0])) => [0, 0],
738            _ => self.mvp(mbx, mby),
739        }
740    }
741
742    /// A neighbouring macroblock that has been coded (or skipped).
743    fn coded_nb(&self, mbx: isize, mby: isize) -> Option<usize> {
744        if mbx < 0 || mby < 0 || mbx as usize >= self.mbw || mby as usize >= self.mbh {
745            return None;
746        }
747        let i = mby as usize * self.mbw + mbx as usize;
748        self.mv[i].is_some().then_some(i)
749    }
750
751    /// `mb_skip_flag`'s ctxIdxInc: neighbours that exist and are not skipped.
752    fn skip_inc(&self, mbx: usize, mby: usize) -> usize {
753        let (x, y) = (mbx as isize, mby as isize);
754        [self.coded_nb(x - 1, y), self.coded_nb(x, y - 1)]
755            .iter()
756            .filter(|n| n.is_some_and(|i| !self.skip[i]))
757            .count()
758    }
759
760    fn commit_skip(&mut self, mbx: usize, mby: usize) {
761        let i = mby * self.mbw + mbx;
762        (self.skip[i], self.cbp[i], self.mvd[i], self.cbf_dc[i]) = (true, 0, [0, 0], [false; 2]);
763        let gw = (self.cw / 4) as usize;
764        for (bx, by) in BLK_POS {
765            self.cbf_y[(mby * 4 + by) * gw + mbx * 4 + bx] = false;
766        }
767        let gwc = (self.cw / 8) as usize;
768        for p in 0..2 {
769            for b in 0..4usize {
770                self.cbf_c[p][(mby * 2 + b / 2) * gwc + mbx * 2 + b % 2] = false;
771            }
772        }
773    }
774
775    fn commit_coded(&mut self, c: &Coded, mvp: [i16; 2], mbx: usize, mby: usize) {
776        let i = mby * self.mbw + mbx;
777        self.skip[i] = false;
778        self.cbp[i] = c.cbp;
779        self.mvd[i] = [
780            (c.mv[0] - mvp[0]).unsigned_abs() as u32,
781            (c.mv[1] - mvp[1]).unsigned_abs() as u32,
782        ];
783        let chroma = c.cbp >> 4;
784        self.cbf_dc[i] = [0, 1].map(|p| chroma > 0 && c.chroma_dc[p].iter().any(|&l| l != 0));
785        let gw = (self.cw / 4) as usize;
786        for (b, (bx, by)) in BLK_POS.iter().enumerate() {
787            self.cbf_y[(mby * 4 + by) * gw + mbx * 4 + bx] =
788                c.cbp & (1 << (b / 4)) != 0 && c.luma[b].iter().any(|&l| l != 0);
789        }
790        let gwc = (self.cw / 8) as usize;
791        for p in 0..2 {
792            for b in 0..4usize {
793                self.cbf_c[p][(mby * 2 + b / 2) * gwc + mbx * 2 + b % 2] =
794                    chroma == 2 && c.chroma_ac[p][b].iter().any(|&l| l != 0);
795            }
796        }
797    }
798
799    /// This macroblock's 16 luma and 2 × 4 chroma coefficient counts.
800    fn save_counts(&self, mbx: usize, mby: usize) -> ([usize; 16], [[usize; 4]; 2]) {
801        let (gw, gwc) = ((self.cw / 4) as usize, (self.cw / 8) as usize);
802        let mut y = [0; 16];
803        for (k, (bx, by)) in BLK_POS.iter().enumerate() {
804            y[k] = self.nnz_y[(mby * 4 + by) * gw + mbx * 4 + bx];
805        }
806        let mut c = [[0; 4]; 2];
807        for (p, cp) in c.iter_mut().enumerate() {
808            for (b, v) in cp.iter_mut().enumerate() {
809                *v = self.nnz_c[p][(mby * 2 + b / 2) * gwc + mbx * 2 + b % 2];
810            }
811        }
812        (y, c)
813    }
814
815    fn restore_counts(&mut self, mbx: usize, mby: usize, saved: &([usize; 16], [[usize; 4]; 2])) {
816        let (gw, gwc) = ((self.cw / 4) as usize, (self.cw / 8) as usize);
817        for (k, (bx, by)) in BLK_POS.iter().enumerate() {
818            self.nnz_y[(mby * 4 + by) * gw + mbx * 4 + bx] = saved.0[k];
819        }
820        for p in 0..2 {
821            for b in 0..4 {
822                self.nnz_c[p][(mby * 2 + b / 2) * gwc + mbx * 2 + b % 2] = saved.1[p][b];
823            }
824        }
825    }
826
827    fn luma_nc(&self, px: u32, py: u32) -> i32 {
828        let gw = (self.cw / 4) as usize;
829        let at = |x: u32, y: u32| self.nnz_y[(y / 4) as usize * gw + (x / 4) as usize];
830        let left = (px >= 4).then(|| at(px - 4, py));
831        let above = (py >= 4).then(|| at(px, py - 4));
832        cavlc::derive_nc(left, above)
833    }
834
835    fn chroma_nc(&self, plane: usize, cx: u32, cy: u32) -> i32 {
836        let gw = (self.cw / 8) as usize;
837        let at = |x: u32, y: u32| self.nnz_c[plane][(y / 4) as usize * gw + (x / 4) as usize];
838        let left = (cx >= 4).then(|| at(cx - 4, cy));
839        let above = (cy >= 4).then(|| at(cx, cy - 4));
840        cavlc::derive_nc(left, above)
841    }
842}
843
844/// Transform, quantize and reconstruct a macroblock's residual against a
845/// prediction. Inter rounding (a sixth of a step), and 8×8 luma blocks
846/// holding a single ±1 are dropped: they cost more bits than they save.
847fn code_residual(
848    orig_y: &[i32; 256],
849    pred_y: &[i32; 256],
850    orig_c: &[[i32; 64]; 2],
851    pred_c: &[[i32; 64]; 2],
852    qp: i32,
853    mv: [i16; 2],
854) -> Coded {
855    let qpc = chroma_qp(qp);
856    let mut c = Coded {
857        mv,
858        cbp: 0,
859        luma: [[0; 16]; 16],
860        chroma_dc: [[0; 4]; 2],
861        chroma_ac: [[[0; 15]; 4]; 2],
862        rec_y: *pred_y,
863        rec_c: *pred_c,
864    };
865    let mut q4 = [[0i32; 16]; 16];
866    for (b, &(bx, by)) in BLK_POS.iter().enumerate() {
867        let mut r = [0i32; 16];
868        for j in 0..4 {
869            for i in 0..4 {
870                let k = (by * 4 + j) * 16 + bx * 4 + i;
871                r[j * 4 + i] = orig_y[k] - pred_y[k];
872            }
873        }
874        q4[b] = quant4(&forward4(&r), qp, false);
875    }
876    for b8 in 0..4 {
877        let blocks = &q4[b8 * 4..b8 * 4 + 4];
878        let nonzero: Vec<i32> = blocks
879            .iter()
880            .flatten()
881            .copied()
882            .filter(|&l| l != 0)
883            .collect();
884        if nonzero.is_empty() || (nonzero.len() == 1 && nonzero[0].abs() == 1) {
885            continue;
886        }
887        c.cbp |= 1 << b8;
888        for b in b8 * 4..b8 * 4 + 4 {
889            for s in 0..16 {
890                c.luma[b][s] = q4[b][ZIGZAG4[s]];
891            }
892            let res = inverse4(&dequant4(&q4[b], qp));
893            let (bx, by) = BLK_POS[b];
894            for j in 0..4 {
895                for i in 0..4 {
896                    let k = (by * 4 + j) * 16 + bx * 4 + i;
897                    c.rec_y[k] = (pred_y[k] + res[j * 4 + i]).clamp(0, 255);
898                }
899            }
900        }
901    }
902
903    // Chroma: DC through the 2×2 Hadamard, AC per 4×4 — the intra encoder's
904    // scheme with inter rounding.
905    let mut chroma_cbp = 0;
906    let mut qac = [[[0i32; 16]; 4]; 2];
907    let mut dc_levels = [[0i32; 4]; 2];
908    for p in 0..2 {
909        let mut dc = [0i32; 4];
910        for b in 0..4 {
911            let (sx, sy) = ((b % 2) * 4, (b / 2) * 4);
912            let mut r = [0i32; 16];
913            for j in 0..4 {
914                for i in 0..4 {
915                    let k = (sy + j) * 8 + sx + i;
916                    r[j * 4 + i] = orig_c[p][k] - pred_c[p][k];
917                }
918            }
919            let co = forward4(&r);
920            dc[b] = co[0];
921            qac[p][b] = quant4(&co, qpc, false);
922            qac[p][b][0] = 0;
923        }
924        let hdc = chroma_dc_forward(&dc);
925        let m = (qpc % 6) as usize;
926        let qbits = 15 + qpc / 6 + 1;
927        let f = (1i64 << qbits) / 6;
928        const MF0: [i64; 6] = [13107, 11916, 10082, 9362, 8192, 7282];
929        for b in 0..4 {
930            let l = ((hdc[b].abs() as i64) * MF0[m] + f) >> qbits;
931            dc_levels[p][b] = if hdc[b] < 0 { -(l as i32) } else { l as i32 };
932        }
933        crate::h264::transform::fit_chroma_dc_levels(&mut dc_levels[p], qpc);
934        if qac[p].iter().any(|blk| blk.iter().any(|&l| l != 0)) {
935            chroma_cbp = 2;
936        } else if dc_levels[p].iter().any(|&l| l != 0) {
937            chroma_cbp = chroma_cbp.max(1);
938        }
939    }
940    for p in 0..2 {
941        if chroma_cbp == 0 {
942            break;
943        }
944        c.chroma_dc[p] = dc_levels[p];
945        let dc_rec = chroma_dc_inverse(&dc_levels[p], qpc);
946        for b in 0..4 {
947            let ac = if chroma_cbp == 2 { qac[p][b] } else { [0; 16] };
948            for s in 1..16 {
949                c.chroma_ac[p][b][s - 1] = ac[ZIGZAG4[s]];
950            }
951            let mut deq = dequant4(&ac, qpc);
952            deq[0] = dc_rec[b];
953            let res = inverse4(&deq);
954            let (sx, sy) = ((b % 2) * 4, (b / 2) * 4);
955            for j in 0..4 {
956                for i in 0..4 {
957                    let k = (sy + j) * 8 + sx + i;
958                    c.rec_c[p][k] = (pred_c[p][k] + res[j * 4 + i]).clamp(0, 255);
959                }
960            }
961        }
962    }
963    c.cbp |= chroma_cbp << 4;
964    c
965}
966
967/// Write a coded macroblock's `macroblock_layer` (after its `mb_skip_run`),
968/// updating the coefficient counts the next blocks' contexts read. Returns
969/// the bits written.
970fn write_coded(
971    w: &mut BitWriter,
972    st: &mut State,
973    c: &Coded,
974    mvp: [i16; 2],
975    mbx: usize,
976    mby: usize,
977) -> usize {
978    let start = w.bit_len();
979    w.write_ue(0); // mb_type P_L0_16x16
980    w.write_se((c.mv[0] - mvp[0]) as i32);
981    w.write_se((c.mv[1] - mvp[1]) as i32);
982    w.write_ue(CBP_INTER_CODE[c.cbp as usize]);
983    if c.cbp != 0 {
984        w.write_se(0); // mb_qp_delta
985    }
986    let gw = (st.cw / 4) as usize;
987    let (x0, y0) = ((mbx * 16) as u32, (mby * 16) as u32);
988    for (b, &(bx, by)) in BLK_POS.iter().enumerate() {
989        let (px, py) = (x0 + bx as u32 * 4, y0 + by as u32 * 4);
990        let nnz = if c.cbp & (1 << (b / 4)) != 0 {
991            let nc = st.luma_nc(px, py);
992            cavlc::encode_block(w, &c.luma[b], BlockKind::Luma4x4, nc)
993        } else {
994            0
995        };
996        st.nnz_y[(py / 4) as usize * gw + (px / 4) as usize] = nnz;
997    }
998    let chroma = c.cbp >> 4;
999    if chroma > 0 {
1000        for p in 0..2 {
1001            cavlc::encode_block(w, &c.chroma_dc[p], BlockKind::ChromaDc, -1);
1002        }
1003    }
1004    let gwc = (st.cw / 8) as usize;
1005    let (cx0, cy0) = ((mbx * 8) as u32, (mby * 8) as u32);
1006    for p in 0..2 {
1007        for b in 0..4 {
1008            let (cx, cy) = (cx0 + (b as u32 % 2) * 4, cy0 + (b as u32 / 2) * 4);
1009            let nnz = if chroma == 2 {
1010                let nc = st.chroma_nc(p, cx, cy);
1011                cavlc::encode_block(w, &c.chroma_ac[p][b], BlockKind::ChromaAc, nc)
1012            } else {
1013                0
1014            };
1015            st.nnz_c[p][(cy / 4) as usize * gwc + (cx / 4) as usize] = nnz;
1016        }
1017    }
1018    w.bit_len() - start
1019}
1020
1021/// A coded macroblock's `macroblock_layer` in CABAC (after its
1022/// `mb_skip_flag`). Reads `st` for the neighbours; the macroblock's own
1023/// blocks are tracked locally, so a trial write leaves `st` as it was.
1024fn write_coded_cabac(
1025    c: &mut Cabac,
1026    st: &State,
1027    coded: &Coded,
1028    mvp: [i16; 2],
1029    mbx: usize,
1030    mby: usize,
1031) {
1032    let (x, y) = (mbx as isize, mby as isize);
1033    let left = st.coded_nb(x - 1, y);
1034    let above = st.coded_nb(x, y - 1);
1035    c.mb_type_p_16x16();
1036    for (comp, (&mv, &pred)) in coded.mv.iter().zip(&mvp).enumerate() {
1037        let abs = |n: Option<usize>| n.map_or(0, |i| st.mvd[i][comp]);
1038        c.mvd(comp, (mv - pred) as i32, abs(left) + abs(above));
1039    }
1040
1041    // coded_block_pattern (§9.3.3.1.1.4): a luma 8×8's context counts
1042    // neighbouring 8×8s that exist and are empty (skipped ones included).
1043    let cbp_l = coded.cbp & 15;
1044    let chroma = coded.cbp >> 4;
1045    let luma_inc: [usize; 4] = std::array::from_fn(|b8| {
1046        let empty = |n: Option<usize>, bit: usize| n.is_some_and(|i| self_empty(st, i, bit));
1047        let a = if b8 % 2 == 1 {
1048            cbp_l & (1 << (b8 - 1)) == 0
1049        } else {
1050            empty(left, b8 + 1)
1051        };
1052        let b = if b8 >= 2 {
1053            cbp_l & (1 << (b8 - 2)) == 0
1054        } else {
1055            empty(above, b8 + 2)
1056        };
1057        usize::from(a) + 2 * usize::from(b)
1058    });
1059    let chroma_has = |n: Option<usize>, two: bool| {
1060        n.is_some_and(|i| {
1061            !st.skip[i]
1062                && if two {
1063                    st.cbp[i] >> 4 == 2
1064                } else {
1065                    st.cbp[i] >> 4 != 0
1066                }
1067        })
1068    };
1069    let cin0 = usize::from(chroma_has(left, false)) + 2 * usize::from(chroma_has(above, false));
1070    let cin1 = usize::from(chroma_has(left, true)) + 2 * usize::from(chroma_has(above, true));
1071    c.coded_block_pattern(cbp_l, chroma, luma_inc, cin0, cin1);
1072    if coded.cbp != 0 {
1073        c.mb_qp_delta(0, false);
1074    }
1075
1076    // Luma: coded_block_flag's context from the left and above 4×4 blocks,
1077    // inside this macroblock or in its neighbours.
1078    let gw = (st.cw / 4) as usize;
1079    let mut local = [false; 16];
1080    let pos_blk = |bx: usize, by: usize| BLK_POS.iter().position(|&p| p == (bx, by)).unwrap();
1081    for b in 0..16 {
1082        if cbp_l & (1 << (b / 4)) == 0 {
1083            continue;
1084        }
1085        let (bx, by) = BLK_POS[b];
1086        let (gx, gy) = (mbx * 4 + bx, mby * 4 + by);
1087        let nb = |inside: bool, lb: usize, gx: isize, gy: isize| {
1088            if inside {
1089                local[lb]
1090            } else if gx < 0 || gy < 0 {
1091                false
1092            } else {
1093                st.cbf_y[gy as usize * gw + gx as usize]
1094            }
1095        };
1096        let a = nb(
1097            bx > 0,
1098            if bx > 0 { pos_blk(bx - 1, by) } else { 0 },
1099            gx as isize - 1,
1100            gy as isize,
1101        );
1102        let bb = nb(
1103            by > 0,
1104            if by > 0 { pos_blk(bx, by - 1) } else { 0 },
1105            gx as isize,
1106            gy as isize - 1,
1107        );
1108        let cbf = coded.luma[b].iter().any(|&l| l != 0);
1109        c.coded_block_flag(
1110            cabac_syntax::CAT_LUMA_4X4,
1111            usize::from(a) + 2 * usize::from(bb),
1112            cbf,
1113        );
1114        if cbf {
1115            c.residual_block(&coded.luma[b], cabac_syntax::CAT_LUMA_4X4);
1116        }
1117        local[b] = cbf;
1118    }
1119    if chroma > 0 {
1120        for p in 0..2 {
1121            let has = |n: Option<usize>| n.is_some_and(|i| st.cbf_dc[i][p]);
1122            let cbf = coded.chroma_dc[p].iter().any(|&l| l != 0);
1123            c.coded_block_flag(
1124                cabac_syntax::CAT_CHROMA_DC,
1125                usize::from(has(left)) + 2 * usize::from(has(above)),
1126                cbf,
1127            );
1128            if cbf {
1129                c.residual_block(&coded.chroma_dc[p], cabac_syntax::CAT_CHROMA_DC);
1130            }
1131        }
1132    }
1133    if chroma == 2 {
1134        let gwc = (st.cw / 8) as usize;
1135        for p in 0..2 {
1136            let mut local = [false; 4];
1137            for b in 0..4usize {
1138                let (bx, by) = (b % 2, b / 2);
1139                let (gx, gy) = ((mbx * 2 + bx) as isize, (mby * 2 + by) as isize);
1140                let grid = |gx: isize, gy: isize| {
1141                    gx >= 0 && gy >= 0 && st.cbf_c[p][gy as usize * gwc + gx as usize]
1142                };
1143                let a = if bx > 0 {
1144                    local[b - 1]
1145                } else {
1146                    grid(gx - 1, gy)
1147                };
1148                let bb = if by > 0 {
1149                    local[b - 2]
1150                } else {
1151                    grid(gx, gy - 1)
1152                };
1153                let cbf = coded.chroma_ac[p][b].iter().any(|&l| l != 0);
1154                c.coded_block_flag(
1155                    cabac_syntax::CAT_CHROMA_AC,
1156                    usize::from(a) + 2 * usize::from(bb),
1157                    cbf,
1158                );
1159                if cbf {
1160                    c.residual_block(&coded.chroma_ac[p][b], cabac_syntax::CAT_CHROMA_AC);
1161                }
1162                local[b] = cbf;
1163            }
1164        }
1165    }
1166}
1167
1168/// Whether neighbour macroblock `i`'s 8×8 `bit` is empty for
1169/// `coded_block_pattern`'s context: skipped, or coded without it.
1170fn self_empty(st: &State, i: usize, bit: usize) -> bool {
1171    st.skip[i] || st.cbp[i] & (1 << bit) == 0
1172}
1173
1174fn ssd<const N: usize>(a: &[i32; N], b: &[i32; N]) -> u64 {
1175    a.iter()
1176        .zip(b)
1177        .map(|(x, y)| ((x - y) * (x - y)) as u64)
1178        .sum()
1179}
1180
1181fn gather<const N: usize>(plane: &[u8], w: u32, x: u32, y: u32, size: u32) -> [i32; N] {
1182    let mut out = [0i32; N];
1183    for j in 0..size {
1184        for i in 0..size {
1185            out[(j * size + i) as usize] = plane[((y + j) * w + x + i) as usize] as i32;
1186        }
1187    }
1188    out
1189}
1190
1191fn store<const N: usize>(plane: &mut [u8], w: u32, x: u32, y: u32, size: u32, block: &[i32; N]) {
1192    for j in 0..size {
1193        for i in 0..size {
1194            plane[((y + j) * w + x + i) as usize] = block[(j * size + i) as usize] as u8;
1195        }
1196    }
1197}
1198
1199/// Encode one P slice covering the picture. Returns the RBSP, the
1200/// reconstruction, and the motion vectors per macroblock.
1201#[allow(clippy::too_many_arguments)]
1202fn p_slice(
1203    cfg: &H264Config,
1204    entropy: EntropyMode,
1205    qp: i32,
1206    frame_num: u32,
1207    y: &[u8],
1208    u: &[u8],
1209    v: &[u8],
1210    r: &Reference,
1211    search: &[Searched],
1212) -> (Vec<u8>, Reconstruction, Vec<[i16; 2]>) {
1213    let (cw, ch) = (cfg.coded_width, cfg.coded_height);
1214    let (cwc, chc) = (cw / 2, ch / 2);
1215    let (mbw, mbh) = ((cw / 16) as usize, (ch / 16) as usize);
1216    let lambda_ssd = lambda(qp);
1217    let lambda_sad = lambda_ssd.sqrt();
1218
1219    let cabac = entropy == EntropyMode::Cabac;
1220    let mut w = BitWriter::new();
1221    w.write_ue(0); // first_mb_in_slice
1222    w.write_ue(5); // slice_type: P, every slice of the picture
1223    w.write_ue(if cabac { 0 } else { 1 }); // pic_parameter_set_id: CABAC 0, CAVLC 1
1224    w.write_bits(frame_num, 4); // frame_num (log2_max_frame_num = 4)
1225    w.flag(false); // num_ref_idx_active_override_flag
1226    w.flag(false); // ref_pic_list_modification_flag_l0
1227    w.flag(false); // adaptive_ref_pic_marking_mode_flag: sliding window
1228    if cabac {
1229        w.write_ue(0); // cabac_init_idc
1230    }
1231    w.write_se(qp - 26); // slice_qp_delta
1232    w.write_ue(1); // disable_deblocking_filter_idc: off
1233                   // CABAC data starts byte-aligned, padded with ones (§7.3.4).
1234    let mut header = Vec::new();
1235    let mut cab: Option<Box<Cabac>> = None;
1236    if cabac {
1237        while !w.is_byte_aligned() {
1238            w.write_bit(1);
1239        }
1240        header = std::mem::take(&mut w).finish();
1241        cab = Some(Box::new(Cabac::new_p(qp)));
1242    }
1243
1244    let mut st = State {
1245        cw,
1246        mbw,
1247        mbh,
1248        nnz_y: vec![0; (cw / 4 * (ch / 4)) as usize],
1249        nnz_c: [
1250            vec![0; (cwc / 4 * (chc / 4)) as usize],
1251            vec![0; (cwc / 4 * (chc / 4)) as usize],
1252        ],
1253        mv: vec![None; mbw * mbh],
1254        skip: vec![false; mbw * mbh],
1255        cbp: vec![0; mbw * mbh],
1256        mvd: vec![[0; 2]; mbw * mbh],
1257        cbf_y: vec![false; (cw / 4 * (ch / 4)) as usize],
1258        cbf_c: [
1259            vec![false; (cwc / 4 * (chc / 4)) as usize],
1260            vec![false; (cwc / 4 * (chc / 4)) as usize],
1261        ],
1262        cbf_dc: vec![[false; 2]; mbw * mbh],
1263    };
1264    let last_mb = mbw * mbh - 1;
1265    let (mut ry, mut ru, mut rv) = (
1266        vec![0u8; (cw * ch) as usize],
1267        vec![0u8; (cwc * chc) as usize],
1268        vec![0u8; (cwc * chc) as usize],
1269    );
1270    let mut skip_run = 0u32;
1271
1272    for mby in 0..mbh {
1273        for mbx in 0..mbw {
1274            let i = mby * mbw + mbx;
1275            let (x, yy) = ((mbx * 16) as u32, (mby * 16) as u32);
1276            let (cx, cy) = ((mbx * 8) as u32, (mby * 8) as u32);
1277            let orig_y: [i32; 256] = gather(y, cw, x, yy, 16);
1278            let orig_c: [[i32; 64]; 2] = [gather(u, cwc, cx, cy, 8), gather(v, cwc, cx, cy, 8)];
1279            let mvp = st.mvp(mbx, mby);
1280            let skip_mv = st.skip_mv(mbx, mby);
1281
1282            // The vector: the search's, the predictor, or the skip vector —
1283            // whichever is cheapest counting what its difference costs.
1284            let mut best = (search[i].mv, f64::INFINITY);
1285            for cand in [search[i].mv, mvp, skip_mv] {
1286                let p = predict_luma(r, cw, ch, x, yy, cand);
1287                let sad: u32 = orig_y
1288                    .iter()
1289                    .zip(&p)
1290                    .map(|(a, b)| (a - b).unsigned_abs())
1291                    .sum();
1292                let bits = se_bits((cand[0] - mvp[0]) as i32) + se_bits((cand[1] - mvp[1]) as i32);
1293                let cost = sad as f64 + lambda_sad * bits as f64;
1294                if cost < best.1 {
1295                    best = (cand, cost);
1296                }
1297            }
1298            let mv = best.0;
1299
1300            // Skip: the prediction at the skip vector, nothing coded.
1301            let skip_y = predict_luma(r, cw, ch, x, yy, skip_mv);
1302            let skip_c = [
1303                predict_chroma(&r.u, cwc, chc, cx, cy, skip_mv),
1304                predict_chroma(&r.v, cwc, chc, cx, cy, skip_mv),
1305            ];
1306            let skip_cost = (ssd(&orig_y, &skip_y)
1307                + ssd(&orig_c[0], &skip_c[0])
1308                + ssd(&orig_c[1], &skip_c[1])) as f64
1309                + lambda_ssd;
1310
1311            let pred_y = if mv == skip_mv {
1312                skip_y
1313            } else {
1314                predict_luma(r, cw, ch, x, yy, mv)
1315            };
1316            let pred_c = if mv == skip_mv {
1317                skip_c
1318            } else {
1319                [
1320                    predict_chroma(&r.u, cwc, chc, cx, cy, mv),
1321                    predict_chroma(&r.v, cwc, chc, cx, cy, mv),
1322                ]
1323            };
1324            let coded = code_residual(&orig_y, &pred_y, &orig_c, &pred_c, qp, mv);
1325
1326            let take_skip = if coded.cbp == 0 && mv == skip_mv {
1327                true
1328            } else {
1329                // Exact bits of the coded choice, from a trial write — which
1330                // updates this macroblock's coefficient counts, so they are
1331                // put back after.
1332                let (bits, skip_bits) = match &cab {
1333                    Some(c) => {
1334                        let mut t = c.probe();
1335                        t.mb_skip_flag(false, st.skip_inc(mbx, mby));
1336                        write_coded_cabac(&mut t, &st, &coded, mvp, mbx, mby);
1337                        let mut s = c.probe();
1338                        s.mb_skip_flag(true, st.skip_inc(mbx, mby));
1339                        (t.bit_len(), s.bit_len())
1340                    }
1341                    None => {
1342                        let saved = st.save_counts(mbx, mby);
1343                        let mut trial = BitWriter::new();
1344                        // Coding it also ends the skip run, which costs ue(run).
1345                        let run_bits = 2 * (32 - (skip_run + 1).leading_zeros() - 1) as usize + 1;
1346                        let bits =
1347                            run_bits + write_coded(&mut trial, &mut st, &coded, mvp, mbx, mby);
1348                        st.restore_counts(mbx, mby, &saved);
1349                        (bits, 1)
1350                    }
1351                };
1352                let coded_ssd = ssd(&orig_y, &coded.rec_y)
1353                    + ssd(&orig_c[0], &coded.rec_c[0])
1354                    + ssd(&orig_c[1], &coded.rec_c[1]);
1355                skip_cost - lambda_ssd + lambda_ssd * skip_bits as f64
1356                    <= coded_ssd as f64 + lambda_ssd * bits as f64
1357            };
1358
1359            if take_skip {
1360                if let Some(c) = cab.as_mut() {
1361                    c.mb_skip_flag(true, st.skip_inc(mbx, mby));
1362                } else {
1363                    skip_run += 1;
1364                }
1365                st.commit_skip(mbx, mby);
1366                st.mv[i] = Some(skip_mv);
1367                store(&mut ry, cw, x, yy, 16, &skip_y);
1368                store(&mut ru, cwc, cx, cy, 8, &skip_c[0]);
1369                store(&mut rv, cwc, cx, cy, 8, &skip_c[1]);
1370                // Its coefficient counts are zero, as they started.
1371                let gw = (cw / 4) as usize;
1372                for (bx, by) in BLK_POS {
1373                    st.nnz_y[(yy as usize / 4 + by) * gw + x as usize / 4 + bx] = 0;
1374                }
1375                let gwc = (cwc / 4) as usize;
1376                for p in 0..2 {
1377                    for b in 0..4usize {
1378                        st.nnz_c[p][(cy as usize / 4 + b / 2) * gwc + cx as usize / 4 + b % 2] = 0;
1379                    }
1380                }
1381            } else {
1382                if let Some(c) = cab.as_mut() {
1383                    c.mb_skip_flag(false, st.skip_inc(mbx, mby));
1384                    write_coded_cabac(c, &st, &coded, mvp, mbx, mby);
1385                    st.commit_coded(&coded, mvp, mbx, mby);
1386                } else {
1387                    w.write_ue(skip_run); // mb_skip_run
1388                    skip_run = 0;
1389                    write_coded(&mut w, &mut st, &coded, mvp, mbx, mby);
1390                }
1391                st.mv[i] = Some(coded.mv);
1392                store(&mut ry, cw, x, yy, 16, &coded.rec_y);
1393                store(&mut ru, cwc, cx, cy, 8, &coded.rec_c[0]);
1394                store(&mut rv, cwc, cx, cy, 8, &coded.rec_c[1]);
1395            }
1396            if let Some(c) = cab.as_mut() {
1397                c.terminate(u32::from(i == last_mb)); // end_of_slice_flag
1398            }
1399        }
1400    }
1401    let mvs = st.mv.iter().map(|m| m.unwrap_or([0, 0])).collect();
1402    let rec = Reconstruction {
1403        y: ry,
1404        u: ru,
1405        v: rv,
1406        coded_width: cw,
1407        coded_height: ch,
1408    };
1409    if let Some(c) = cab {
1410        // The coder's flush ends with the rbsp_stop_one_bit (§9.3.4.3.5).
1411        header.extend_from_slice(&c.finish());
1412        return (header, rec, mvs);
1413    }
1414    if skip_run > 0 {
1415        w.write_ue(skip_run);
1416    }
1417    rbsp_trailing_bits(&mut w);
1418    (w.finish(), rec, mvs)
1419}
1420
1421#[cfg(test)]
1422mod tests {
1423    use super::*;
1424
1425    #[test]
1426    fn inter_cbp_table_is_a_permutation() {
1427        let mut seen = [false; 48];
1428        for &c in CBP_INTER_FROM_CODE.iter() {
1429            assert!(!seen[c as usize]);
1430            seen[c as usize] = true;
1431        }
1432        for cbp in 0..48 {
1433            assert_eq!(
1434                CBP_INTER_FROM_CODE[CBP_INTER_CODE[cbp] as usize] as usize,
1435                cbp
1436            );
1437        }
1438    }
1439
1440    #[test]
1441    fn se_bits_match_the_writer() {
1442        for v in -300..300 {
1443            let mut w = BitWriter::new();
1444            w.write_se(v);
1445            assert_eq!(se_bits(v) as usize, w.bit_len(), "{v}");
1446        }
1447    }
1448}