Skip to main content

threers_codec/decode/h264/
slice.rs

1//! Slice data decoding: the macroblock loop, neighbour access, motion vector
2//! derivation (§8.4.1), and reconstruction.
3//!
4//! Parsing lives in `parse_cavlc` / `parse_cabac` as further `impl SliceDec`
5//! blocks; both fill [`MbSyn`] and the current [`MbInfo`] and hand over to
6//! [`SliceDec::decode_mb`].
7
8use super::cavlc::CavlcTables;
9use super::inter;
10use super::intra;
11use super::params::{Pps, SliceHeader, SliceType, Sps};
12use super::tables::{chroma_qp, norm_adjust4, norm_adjust8, BLK_XY, RASTER_TO_BLK};
13use super::types::*;
14use crate::decode::bits::BitReader;
15use crate::decode::cabac::{Cabac, Ctx};
16use crate::decode::picture::Plane;
17
18pub type Result<T> = std::result::Result<T, String>;
19
20/// Deblocking parameters of one slice.
21#[derive(Clone, Copy, Debug)]
22pub struct SliceDeblock {
23    pub disable: u32,
24    pub alpha: i32,
25    pub beta: i32,
26}
27
28/// The picture being decoded.
29pub struct CurPic {
30    pub planes: [Plane; 3],
31    pub mbs: Vec<MbInfo>,
32    pub w_mbs: usize,
33    pub h_mbs: usize,
34    pub poc: i32,
35    /// `TopFieldOrderCnt` / `BottomFieldOrderCnt` (MBAFF field macroblocks).
36    pub field_poc: [i32; 2],
37    pub slices: Vec<SliceDeblock>,
38    /// Macroblocks decoded so far, to tell a complete picture from one with
39    /// slices missing.
40    pub decoded: usize,
41}
42
43/// Neighbour motion as §8.4.1.3.2 sees it: `None` when the partition is not
44/// available, otherwise its reference index (`-1` when it does not use the
45/// list) and vector.
46type NbMotion = Option<(i8, [i16; 2])>;
47
48pub struct SliceDec<'a> {
49    pub sps: &'a Sps,
50    pub pps: &'a Pps,
51    pub hdr: &'a SliceHeader,
52    pub slice_num: u32,
53    pub pic: &'a mut CurPic,
54    pub dpb: &'a [RefPic],
55    pub refs: &'a [Vec<RefEntry>; 2],
56    /// MBAFF: the field lists, `[parity][list]` (see `Decoder::field_lists`).
57    pub frefs: &'a [[Vec<RefEntry>; 2]; 2],
58    pub tables: &'a CavlcTables,
59    pub mb_x: usize,
60    /// Macroblock row in the picture (MBAFF: `2 * pair row + bottom`).
61    pub mb_y: usize,
62    /// Raster index of the macroblock in `pic.mbs`.
63    pub addr: usize,
64    /// `MbaffFrameFlag`.
65    pub mbaff: bool,
66    /// The current macroblock is a field macroblock of an MBAFF frame.
67    pub mb_field: bool,
68    /// Macroblock row in the current macroblock's own sample view: `mb_y`,
69    /// or the pair row for an MBAFF field macroblock.
70    pub vmb_y: usize,
71    /// How that view's rows map onto picture rows.
72    pub geo: Geo,
73    /// `QPY` of the previous macroblock in decoding order.
74    pub qp: i32,
75    /// Whether the previous macroblock had a non-zero `mb_qp_delta`.
76    pub prev_qp_nz: bool,
77    pub ctx: Vec<Ctx>,
78    /// `LevelScale4x4[list][qP % 6][raster]`.
79    ls4: Box<[[[i32; 16]; 6]; 6]>,
80    /// `LevelScale8x8[intra/inter][qP % 6][raster]`.
81    ls8: Box<[[[i32; 64]; 6]; 2]>,
82    pub bd_y: u32,
83    pub bd_c: u32,
84    pub chroma: bool,
85    /// 4:2:2: chroma macroblocks are 8×16.
86    pub c422: bool,
87    pub syn: MbSyn,
88    /// Edge-clamped reference window, reused by every block.
89    mc_win: Vec<u16>,
90    /// Prediction scratch for `mc_block`, one block per list; every sample a
91    /// block reads is written first, so it is never cleared.
92    mc_pred: Option<Box<[[i32; 256]; 2]>>,
93    luma_scratch: Box<inter::LumaScratch>,
94}
95
96impl<'a> SliceDec<'a> {
97    #[allow(clippy::too_many_arguments)]
98    pub fn new(
99        sps: &'a Sps,
100        pps: &'a Pps,
101        hdr: &'a SliceHeader,
102        slice_num: u32,
103        pic: &'a mut CurPic,
104        dpb: &'a [RefPic],
105        refs: &'a [Vec<RefEntry>; 2],
106        frefs: &'a [[Vec<RefEntry>; 2]; 2],
107        tables: &'a CavlcTables,
108    ) -> Self {
109        let lists = pps
110            .scaling
111            .as_ref()
112            .or(sps.scaling.as_ref())
113            .cloned()
114            .unwrap_or_else(super::params::ScalingLists::flat);
115        let mut ls4 = Box::new([[[0i32; 16]; 6]; 6]);
116        let mut ls8 = Box::new([[[0i32; 64]; 6]; 2]);
117        for l in 0..6 {
118            for m in 0..6 {
119                for k in 0..16 {
120                    ls4[l][m][k] = lists.m4[l][k] as i32 * norm_adjust4(m, k / 4, k % 4);
121                }
122            }
123        }
124        for l in 0..2 {
125            for m in 0..6 {
126                for k in 0..64 {
127                    ls8[l][m][k] = lists.m8[l][k] as i32 * norm_adjust8(m, k / 8, k % 8);
128                }
129            }
130        }
131        SliceDec {
132            sps,
133            pps,
134            hdr,
135            slice_num,
136            pic,
137            dpb,
138            refs,
139            frefs,
140            tables,
141            mb_x: 0,
142            mb_y: 0,
143            addr: 0,
144            mbaff: hdr.mbaff,
145            mb_field: false,
146            vmb_y: 0,
147            geo: Geo { step: 1, par: 0 },
148            qp: hdr.qp,
149            prev_qp_nz: false,
150            ctx: Vec::new(),
151            ls4,
152            ls8,
153            bd_y: sps.bit_depth_luma,
154            bd_c: sps.bit_depth_chroma,
155            chroma: sps.chroma_format_idc != 0,
156            c422: sps.chroma_format_idc == 2,
157            syn: MbSyn::new(),
158            mc_win: Vec::new(),
159            mc_pred: None,
160            luma_scratch: Box::default(),
161        }
162    }
163
164    /// Make macroblock `addr` (`CurrMbAddr`, in decoding order) current.
165    /// An MBAFF frame decodes pair by pair, top macroblock first.
166    fn set_addr(&mut self, addr: usize) {
167        let w = self.pic.w_mbs;
168        if self.mbaff {
169            let pair = addr / 2;
170            self.mb_x = pair % w;
171            self.mb_y = 2 * (pair / w) + addr % 2;
172        } else {
173            self.mb_x = addr % w;
174            self.mb_y = addr / w;
175        }
176        self.addr = self.mb_y * w + self.mb_x;
177        self.set_field(self.mb_field);
178    }
179
180    /// Set the current macroblock's `mb_field_decoding_flag` (MBAFF).
181    pub fn set_field(&mut self, field: bool) {
182        self.mb_field = self.mbaff && field;
183        if self.mb_field {
184            self.vmb_y = self.mb_y / 2;
185            self.geo = Geo {
186                step: 2,
187                par: self.mb_y % 2,
188            };
189        } else {
190            self.vmb_y = self.mb_y;
191            self.geo = Geo { step: 1, par: 0 };
192        }
193        self.pic.mbs[self.addr].field = self.mb_field;
194    }
195
196    fn begin_mb(&mut self, addr: usize) {
197        self.set_addr(addr);
198        self.pic.mbs[self.addr] = MbInfo {
199            slice: self.slice_num,
200            field: self.mb_field,
201            ..Default::default()
202        };
203        self.syn.reset();
204    }
205
206    /// Decode the slice's macroblocks.
207    pub fn decode(&mut self, rbsp: &[u8]) -> Result<()> {
208        let total = self.pic.w_mbs * self.pic.h_mbs;
209        let mut addr = self.hdr.first_mb as usize * if self.mbaff { 2 } else { 1 };
210        if addr >= total {
211            return Err("first_mb_in_slice past the picture".into());
212        }
213        let mut r = BitReader::new(rbsp);
214        r.set_pos(self.hdr.data_bit_offset);
215        let first = addr;
216        if self.pps.cabac {
217            r.byte_align();
218            self.init_contexts();
219            let mut c = Cabac::new(rbsp, r.pos() / 8);
220            // MBAFF: the bottom macroblock's skip flag, read early when the
221            // top one is skipped.
222            let mut next_skip = false;
223            let mut prev_skipped = false;
224            loop {
225                if addr >= total {
226                    return Err("slice runs past the end of the picture".into());
227                }
228                self.begin_pair_mb(addr, first);
229                let top = addr.is_multiple_of(2);
230                let skip = if self.hdr.slice_type == SliceType::I {
231                    false
232                } else if self.mbaff && !top && prev_skipped {
233                    next_skip
234                } else {
235                    self.cabac_skip_flag(&mut c)
236                };
237                if self.mbaff && top {
238                    if skip {
239                        // A skipped top macroblock takes its pair's field flag
240                        // from the bottom one, when that is coded.
241                        self.pic.mbs[self.addr].kind = self.skip_kind();
242                        let me = addr;
243                        self.set_addr(addr + 1);
244                        next_skip = self.cabac_skip_flag(&mut c);
245                        self.set_addr(me);
246                        if !next_skip {
247                            let f = self.cabac_field_flag(&mut c);
248                            self.set_field(f);
249                        }
250                    } else {
251                        let f = self.cabac_field_flag(&mut c);
252                        self.set_field(f);
253                    }
254                }
255                prev_skipped = skip;
256                if skip {
257                    self.decode_skip()?;
258                } else {
259                    self.parse_mb_cabac(&mut c)?;
260                    self.decode_mb()?;
261                }
262                self.pic.decoded += 1;
263                addr += 1;
264                if !(self.mbaff && top) && c.terminate() == 1 {
265                    break;
266                }
267                if c.overrun() {
268                    return Err("CABAC slice data overrun".into());
269                }
270            }
271        } else {
272            let mut run_left = 0usize;
273            let mut have_run = false;
274            loop {
275                if self.hdr.slice_type != SliceType::I && !have_run {
276                    run_left = r.ue() as usize;
277                    if run_left > total {
278                        return Err("mb_skip_run past the picture".into());
279                    }
280                    have_run = true;
281                }
282                if addr >= total {
283                    if run_left > 0 {
284                        return Err("skip run past the end of the picture".into());
285                    }
286                    return Err("slice runs past the end of the picture".into());
287                }
288                self.begin_pair_mb(addr, first);
289                let top = addr.is_multiple_of(2);
290                if run_left > 0 {
291                    run_left -= 1;
292                    if self.mbaff && top && run_left == 0 {
293                        // The bottom macroblock is coded: the pair's flag
294                        // comes before the skipped top one.
295                        let f = r.flag();
296                        self.set_field(f);
297                    }
298                    self.decode_skip()?;
299                    self.pic.decoded += 1;
300                    addr += 1;
301                    if run_left == 0 && !r.more_rbsp_data() {
302                        break;
303                    }
304                    continue;
305                }
306                have_run = false;
307                if self.mbaff && top {
308                    let f = r.flag();
309                    self.set_field(f);
310                }
311                self.parse_mb_cavlc(&mut r)?;
312                self.decode_mb()?;
313                self.pic.decoded += 1;
314                addr += 1;
315                if r.overrun() {
316                    return Err("CAVLC slice data overrun".into());
317                }
318                if !r.more_rbsp_data() {
319                    break;
320                }
321            }
322        }
323        Ok(())
324    }
325
326    /// Reference list `l` as the current macroblock indexes it: an MBAFF
327    /// field macroblock's is the field list of its parity.
328    #[inline]
329    pub fn rl(&self, l: usize) -> &'a [RefEntry] {
330        if self.mb_field {
331            &self.frefs[self.mb_y % 2][l]
332        } else {
333            &self.refs[l]
334        }
335    }
336
337    /// `num_ref_idx_lX_active` for the current macroblock (doubled for an
338    /// MBAFF field macroblock).
339    #[inline]
340    pub fn num_ref(&self, l: usize) -> u32 {
341        self.hdr.num_ref_idx[l] << self.mb_field as u32
342    }
343
344    /// Field scans and CABAC field contexts: a field picture or an MBAFF
345    /// field macroblock.
346    #[inline]
347    pub fn field_coded(&self) -> bool {
348        self.hdr.field_pic || self.mb_field
349    }
350
351    fn skip_kind(&self) -> MbKind {
352        if self.hdr.slice_type == SliceType::P {
353            MbKind::PSkip
354        } else {
355            MbKind::BSkip
356        }
357    }
358
359    /// [`Self::begin_mb`], with MBAFF's inferred field flag: a pair keeps
360    /// the previous pair's flag, except that a new row of pairs takes the
361    /// pair above's (ffmpeg `predict_field_decoding_flag`); the slice's first
362    /// pair starts as frame.
363    fn begin_pair_mb(&mut self, addr: usize, first: usize) {
364        if self.mbaff && addr.is_multiple_of(2) {
365            let w = self.pic.w_mbs;
366            if addr == first {
367                self.mb_field = false;
368            } else if (addr / 2).is_multiple_of(w) {
369                // The bottom macroblock of the pair above.
370                let above = (2 * (addr / 2 / w) - 1) * w;
371                let m = &self.pic.mbs[above];
372                self.mb_field = m.slice == self.slice_num && m.field;
373            }
374        }
375        self.begin_mb(addr);
376    }
377
378    // ------------------------------------------------------------------
379    // Neighbours
380    // ------------------------------------------------------------------
381
382    /// Address of the macroblock at offset `(dx, dy)`, if it is available:
383    /// inside the picture, already decoded, and in this slice. In an MBAFF
384    /// frame, the macroblock covering luma location `(-1, 0)`, `(0, -1)`,
385    /// `(16, -1)` or `(-1, -1)` (§6.4.11.1).
386    #[inline]
387    pub fn mb_nb(&self, dx: isize, dy: isize) -> Option<usize> {
388        if self.mbaff {
389            let xn = [-1, 0, 16][(dx + 1) as usize];
390            return self.loc_nb(xn, dy.min(0), 16, 16).map(|(a, _, _)| a);
391        }
392        let x = self.mb_x as isize + dx;
393        let y = self.mb_y as isize + dy;
394        if x < 0 || y < 0 || x >= self.pic.w_mbs as isize || y >= self.pic.h_mbs as isize {
395            return None;
396        }
397        let a = y as usize * self.pic.w_mbs + x as usize;
398        if a == self.addr || self.pic.mbs[a].slice != self.slice_num {
399            return None;
400        }
401        Some(a)
402    }
403
404    /// MBAFF neighbouring location (§6.4.12.2): the available macroblock
405    /// covering location `(xn, yn)` of the current macroblock's view, in a
406    /// component whose macroblocks are `maxw × maxh`, and the location within
407    /// it. A location maps onto the picture row it shows (every other row in
408    /// a field macroblock), and from there into whichever macroblock of the
409    /// pair holds that row: this is Table 6-4, and ffmpeg's
410    /// `left_block_options` / `top_xy` / `topleft_xy` adjustments.
411    pub fn loc_nb(
412        &self,
413        xn: isize,
414        yn: isize,
415        maxw: isize,
416        maxh: isize,
417    ) -> Option<(usize, usize, usize)> {
418        let w = self.pic.w_mbs as isize;
419        let (px, pr, bot) = (
420            self.mb_x as isize,
421            (self.mb_y / 2) as isize,
422            (self.mb_y % 2) as isize,
423        );
424        let y = if self.mb_field {
425            2 * yn + bot
426        } else {
427            yn + maxh * bot
428        };
429        let (dxp, xw) = (xn.div_euclid(maxw), xn.rem_euclid(maxw));
430        let (dyp, yp) = (y.div_euclid(2 * maxh), y.rem_euclid(2 * maxh));
431        let (tx, tr) = (px + dxp, pr + dyp);
432        if tx < 0 || tr < 0 || tx >= w || (dyp == 0 && dxp > 0) {
433            return None;
434        }
435        let field = if dxp == 0 && dyp == 0 {
436            self.mb_field
437        } else {
438            self.pic.mbs[(2 * tr * w + tx) as usize].field
439        };
440        let (mbi, yw) = if field {
441            (yp & 1, yp >> 1)
442        } else {
443            (yp / maxh, yp % maxh)
444        };
445        let a = ((2 * tr + mbi) * w + tx) as usize;
446        if a != self.addr && self.pic.mbs[a].slice != self.slice_num {
447            return None;
448        }
449        Some((a, xw as usize, yw as usize))
450    }
451
452    /// The 4×4 block at `(x, y)` (4-sample units relative to the current
453    /// macroblock, `x` in `-1..=4`, `y` in `-1..=3`): `(mb address, raster
454    /// index)`. The current macroblock is returned too; callers that care
455    /// whether its block is decoded yet check that themselves.
456    #[inline]
457    pub fn blk_nb(&self, x: isize, y: isize) -> Option<(usize, usize)> {
458        self.blk_nb_row(x, y, false)
459    }
460
461    /// [`Self::blk_nb`], locating the block by its last sample row instead
462    /// of its first: neighbour D of a partition is luma location
463    /// `(x - 1, y - 1)`, the bottom row of the block above-left. Only an
464    /// MBAFF left pair of the other kind tells the two apart.
465    pub fn blk_nb_row(&self, x: isize, y: isize, last_row: bool) -> Option<(usize, usize)> {
466        if (0..4).contains(&x) && (0..4).contains(&y) {
467            return Some((self.addr, (y * 4 + x) as usize));
468        }
469        if self.mbaff {
470            let xn = if x < 0 { -1 } else { 4 * x };
471            let yn = if y < 0 {
472                -1
473            } else {
474                4 * y + if last_row { 3 } else { 0 }
475            };
476            return self
477                .loc_nb(xn, yn, 16, 16)
478                .map(|(a, xw, yw)| (a, (yw / 4) * 4 + xw / 4));
479        }
480        let (dx, bx) = if x < 0 {
481            (-1, x + 4)
482        } else if x >= 4 {
483            (1, x - 4)
484        } else {
485            (0, x)
486        };
487        let (dy, by) = if y < 0 { (-1, y + 4) } else { (0, y) };
488        let raster = (by * 4 + bx) as usize;
489        if dx == 1 && dy == 0 {
490            return None;
491        }
492        self.mb_nb(dx, dy).map(|a| (a, raster))
493    }
494
495    /// The chroma 4×4 block at `(x, y)` (`x` in `-1..2`, `y` in `-1..rows`):
496    /// `(mb address, index into nnz_c)`.
497    pub fn cblk_nb(&self, x: isize, y: isize) -> Option<(usize, usize)> {
498        let rows = if self.c422 { 4 } else { 2 };
499        if x >= 0 && y >= 0 {
500            return Some((self.addr, (y * 2 + x) as usize));
501        }
502        if self.mbaff {
503            let xn = if x < 0 { -1 } else { 4 * x };
504            let yn = if y < 0 { -1 } else { 4 * y };
505            return self
506                .loc_nb(xn, yn, 8, 4 * rows)
507                .map(|(a, xw, yw)| (a, (yw / 4) * 2 + xw / 4));
508        }
509        let (dx, bx) = if x < 0 { (-1, x + 2) } else { (0, x) };
510        let (dy, by) = if y < 0 { (-1, y + rows) } else { (0, y) };
511        let a = self.mb_nb(dx, dy)?;
512        Some((a, (by * 2 + bx) as usize))
513    }
514
515    /// Whether intra prediction may use the left samples of rows
516    /// `y0..y0 + n` of the current macroblock's view (luma, or chroma). In an
517    /// MBAFF frame those rows can come from both macroblocks of the left
518    /// pair, which constrained intra prediction judges separately (ffmpeg
519    /// `left_samples_available`).
520    fn left_avail(&self, y0: usize, n: usize, chroma: bool) -> bool {
521        if !self.mbaff {
522            return self.intra_nb(-1, 0);
523        }
524        let (maxw, maxh) = if chroma {
525            (8, if self.c422 { 16 } else { 8 })
526        } else {
527            (16, 16)
528        };
529        (y0..y0 + n).all(|y| {
530            self.loc_nb(-1, y as isize, maxw, maxh)
531                .is_some_and(|(a, _, _)| {
532                    !self.pps.constrained_intra_pred || self.pic.mbs[a].kind.is_intra()
533                })
534        })
535    }
536
537    /// Whether the neighbouring macroblock may be used for intra prediction.
538    #[inline]
539    fn intra_nb(&self, dx: isize, dy: isize) -> bool {
540        match self.mb_nb(dx, dy) {
541            Some(a) => !self.pps.constrained_intra_pred || self.pic.mbs[a].kind.is_intra(),
542            None => false,
543        }
544    }
545
546    // ------------------------------------------------------------------
547    // QP and scaling
548    // ------------------------------------------------------------------
549
550    fn update_qp(&mut self, delta: i32) -> Result<()> {
551        let off = 6 * (self.bd_y as i32 - 8);
552        if delta < -(26 + off / 2) || delta > 25 + off / 2 {
553            return Err(format!("mb_qp_delta {delta} out of range"));
554        }
555        self.qp = ((self.qp + delta + 52 + 2 * off) % (52 + off)) - off;
556        Ok(())
557    }
558
559    /// `QP'C` for component `c` (0 Cb, 1 Cr) at luma `QPY` `qpy`.
560    fn qpc_prime(&self, qpy: i32, c: usize) -> i32 {
561        let off_c = 6 * (self.bd_c as i32 - 8);
562        let qpi = (qpy + self.pps.chroma_qp_offset[c]).clamp(-off_c, 51);
563        chroma_qp(qpi) + off_c
564    }
565
566    /// `QPC` without the bit-depth offset — what deblocking averages.
567    fn qpc(&self, qpy: i32, c: usize) -> i32 {
568        self.qpc_prime(qpy, c) - 6 * (self.bd_c as i32 - 8)
569    }
570
571    // ------------------------------------------------------------------
572    // Macroblock reconstruction
573    // ------------------------------------------------------------------
574
575    pub fn decode_skip(&mut self) -> Result<()> {
576        let a = self.addr;
577        self.pic.mbs[a].qp = self.qp as i8;
578        self.pic.mbs[a].qpc = [self.qpc(self.qp, 0) as i8, self.qpc(self.qp, 1) as i8];
579        self.prev_qp_nz = false;
580        if self.hdr.slice_type == SliceType::P {
581            self.pic.mbs[a].kind = MbKind::PSkip;
582            self.p_skip_motion();
583        } else {
584            self.pic.mbs[a].kind = MbKind::BSkip;
585            self.pic.mbs[a].b_direct16 = true;
586            self.pic.mbs[a].direct8 = 0xF;
587            self.direct_motion(0xF);
588        }
589        self.motion_compensate();
590        Ok(())
591    }
592
593    pub fn decode_mb(&mut self) -> Result<()> {
594        let a = self.addr;
595        let kind = self.syn.kind;
596        if kind == MbKind::IPcm {
597            return self.decode_pcm();
598        }
599        let has_residual = self.syn.cbp != 0 || kind == MbKind::I16x16;
600        if has_residual {
601            self.update_qp(self.syn.qp_delta)?;
602        }
603        self.prev_qp_nz = has_residual && self.syn.qp_delta != 0;
604        {
605            let qp = self.qp;
606            let qpc = [self.qpc(qp, 0) as i8, self.qpc(qp, 1) as i8];
607            let m = &mut self.pic.mbs[a];
608            m.kind = kind;
609            m.qp = qp as i8;
610            m.qpc = qpc;
611            m.cbp = self.syn.cbp;
612            m.t8x8 = self.syn.t8x8;
613            m.chroma_mode = self.syn.chroma_mode;
614        }
615        self.set_nz_mask();
616        if kind.is_intra() {
617            self.intra_luma()?;
618        } else {
619            self.inter_motion()?;
620            self.motion_compensate();
621            self.inter_luma_residual();
622        }
623        if self.chroma && kind.is_intra() {
624            self.intra_chroma_pred();
625        }
626        if self.chroma {
627            self.chroma_residual(kind.is_intra());
628        }
629        Ok(())
630    }
631
632    fn set_nz_mask(&mut self) {
633        let m = &mut self.pic.mbs[self.addr];
634        let mut mask = 0u16;
635        for k in 0..16 {
636            if m.nnz[k] != 0 {
637                mask |= 1 << k;
638            }
639        }
640        if m.t8x8 {
641            for b8 in 0..4 {
642                let bits: u16 = 0x33 << ((b8 % 2) * 2 + (b8 / 2) * 8);
643                if mask & bits != 0 {
644                    mask |= bits;
645                }
646            }
647        }
648        m.nz_mask = mask;
649    }
650
651    fn decode_pcm(&mut self) -> Result<()> {
652        let g = self.geo;
653        let (x0, y0) = (self.mb_x * 16, self.vmb_y * 16);
654        for y in 0..16 {
655            for x in 0..16 {
656                self.pic.planes[0].set(x0 + x, g.row(y0 + y), self.syn.pcm[y * 16 + x]);
657            }
658        }
659        if self.chroma {
660            let (ch, cy0) = if self.c422 { (16, y0) } else { (8, y0 / 2) };
661            for c in 0..2 {
662                for y in 0..ch {
663                    for x in 0..8 {
664                        let v = self.syn.pcm[256 + c * 8 * ch + y * 8 + x];
665                        self.pic.planes[1 + c].set(x0 / 2 + x, g.row(cy0 + y), v);
666                    }
667                }
668            }
669        }
670        let qpc = [self.qpc(0, 0) as i8, self.qpc(0, 1) as i8];
671        let m = &mut self.pic.mbs[self.addr];
672        m.kind = MbKind::IPcm;
673        m.qp = 0;
674        m.qpc = qpc;
675        m.cbp = 0x2F;
676        m.nnz = [16; 16];
677        m.nnz_c = [[16; 8]; 2];
678        m.cbf_dc = 7;
679        m.nz_mask = 0xFFFF;
680        self.prev_qp_nz = false;
681        Ok(())
682    }
683
684    /// Resolve `Intra4x4PredMode` / `Intra8x8PredMode` for the block whose
685    /// top-left 4×4 is `(bx, by)` (§8.3.1.1).
686    fn intra_mode(&self, bx: usize, by: usize, prev: bool, rem: u8) -> u8 {
687        let cur = &self.pic.mbs[self.addr];
688        let nb = |x: isize, y: isize| -> Option<u8> {
689            let (a, k) = self.blk_nb(x, y)?;
690            let m = if a == self.addr {
691                cur
692            } else {
693                &self.pic.mbs[a]
694            };
695            if a != self.addr && self.pps.constrained_intra_pred && !m.kind.is_intra() {
696                return None;
697            }
698            Some(if a == self.addr || m.kind.is_nxn() {
699                m.ipred[k]
700            } else {
701                2
702            })
703        };
704        let a = nb(bx as isize - 1, by as isize);
705        let b = nb(bx as isize, by as isize - 1);
706        let pred = match (a, b) {
707            (Some(a), Some(b)) => a.min(b),
708            _ => 2,
709        };
710        if prev {
711            pred
712        } else if rem < pred {
713            rem
714        } else {
715            rem + 1
716        }
717    }
718
719    /// Gather intra neighbours of the `n × n` luma block at `(x0, y0)` in the
720    /// picture, whose top-left 4×4 is `(bx, by)` in the macroblock.
721    /// Returns `(top, left, at, al, atl)`.
722    fn gather_nxn(
723        &self,
724        bx: usize,
725        by: usize,
726        n: usize,
727    ) -> ([i32; 17], [i32; 9], bool, bool, bool) {
728        let g = self.geo;
729        let n4 = n / 4;
730        let p = &self.pic.planes[0];
731        let x0 = self.mb_x * 16 + bx * 4;
732        let y0 = self.vmb_y * 16 + by * 4;
733        let al = bx > 0 || self.left_avail(by * 4, n, false);
734        let at = by > 0 || self.intra_nb(0, -1);
735        let atl = match (bx > 0, by > 0) {
736            (true, true) => true,
737            (false, true) => self.left_avail(by * 4 - 1, 1, false),
738            (true, false) => self.intra_nb(0, -1),
739            (false, false) => self.intra_nb(-1, -1),
740        };
741        let atr = if by == 0 {
742            if bx + n4 < 4 {
743                self.intra_nb(0, -1)
744            } else {
745                self.intra_nb(1, -1)
746            }
747        } else if bx + n4 >= 4 {
748            false
749        } else if n == 4 {
750            RASTER_TO_BLK[(by - 1) * 4 + bx + 1] < RASTER_TO_BLK[by * 4 + bx]
751        } else {
752            // 8×8: only the bottom-left block's top-right (block 1) is decoded.
753            bx == 0 && by == 2
754        };
755        let mut top = [0i32; 17];
756        let mut left = [0i32; 9];
757        if atl {
758            top[0] = p.at(x0 - 1, g.row(y0 - 1)) as i32;
759            left[0] = top[0];
760        }
761        if at {
762            for i in 0..n {
763                top[1 + i] = p.at(x0 + i, g.row(y0 - 1)) as i32;
764            }
765            for i in n..2 * n {
766                top[1 + i] = if atr {
767                    p.at(x0 + i, g.row(y0 - 1)) as i32
768                } else {
769                    top[n]
770                };
771            }
772        }
773        if al {
774            for i in 0..n {
775                left[1 + i] = p.at(x0 - 1, g.row(y0 + i)) as i32;
776            }
777        }
778        (top, left, at, al, atl)
779    }
780
781    fn intra_luma(&mut self) -> Result<()> {
782        let g = self.geo;
783        let kind = self.syn.kind;
784        let bd = self.bd_y;
785        let qp = self.qp + 6 * (bd as i32 - 8);
786        match kind {
787            MbKind::I4x4 => {
788                for blk in 0..16 {
789                    let (bx, by) = BLK_XY[blk];
790                    let mode = self.intra_mode(bx, by, self.syn.prev_flag[blk], self.syn.rem[blk]);
791                    self.pic.mbs[self.addr].ipred[by * 4 + bx] = mode;
792                    let (top, left, at, al, _) = self.gather_nxn(bx, by, 4);
793                    let mut out = [0i32; 16];
794                    intra::pred_nxn(&mut out, 4, mode, &top, &left, at, al, bd);
795                    let (x0, y0) = (self.mb_x * 16 + bx * 4, self.vmb_y * 16 + by * 4);
796                    let coded = self.syn.coded & (1 << blk) != 0;
797                    let coef = self.syn.coef[blk];
798                    self.put_4x4(x0, y0, &out, coded.then_some(&coef), qp, 0, None);
799                }
800            }
801            MbKind::I8x8 => {
802                for b8 in 0..4 {
803                    let (bx, by) = ((b8 % 2) * 2, (b8 / 2) * 2);
804                    let mode = self.intra_mode(bx, by, self.syn.prev_flag[b8], self.syn.rem[b8]);
805                    for k in [0, 1, 4, 5] {
806                        self.pic.mbs[self.addr].ipred[by * 4 + bx + k] = mode;
807                    }
808                    let (mut top, mut left, at, al, atl) = self.gather_nxn(bx, by, 8);
809                    intra::filter_8x8_refs(&mut top, &mut left, at, al, atl);
810                    let mut out = [0i32; 64];
811                    intra::pred_nxn(&mut out, 8, mode, &top, &left, at, al, bd);
812                    let (x0, y0) = (self.mb_x * 16 + bx * 4, self.vmb_y * 16 + by * 4);
813                    let coded = self.syn.coded8 & (1 << b8) != 0;
814                    let coef = self.syn.coef8[b8];
815                    self.put_8x8(x0, y0, &out, coded.then_some(&coef), qp, 0);
816                }
817            }
818            _ => {
819                // Intra 16×16.
820                let al = self.left_avail(0, 16, false);
821                let at = self.intra_nb(0, -1);
822                let atl = self.intra_nb(-1, -1);
823                let mode = self.syn.i16_mode;
824                if (mode == 0 && !at) || (mode == 1 && !al) || (mode == 3 && !(at && al && atl)) {
825                    return Err("Intra 16x16 mode uses unavailable neighbours".into());
826                }
827                let (x0, y0) = (self.mb_x * 16, self.vmb_y * 16);
828                let p = &self.pic.planes[0];
829                let mut top = [0i32; 17];
830                let mut left = [0i32; 17];
831                if atl {
832                    top[0] = p.at(x0 - 1, g.row(y0 - 1)) as i32;
833                    left[0] = top[0];
834                }
835                if at {
836                    for i in 0..16 {
837                        top[1 + i] = p.at(x0 + i, g.row(y0 - 1)) as i32;
838                    }
839                }
840                if al {
841                    for i in 0..16 {
842                        left[1 + i] = p.at(x0 - 1, g.row(y0 + i)) as i32;
843                    }
844                }
845                let mut out = [0i32; 256];
846                intra::pred_16x16(&mut out, mode, &top, &left, at, al, bd);
847                // Luma DC: inverse Hadamard then scale.
848                let dc = self.luma_dc(qp);
849                for blk in 0..16 {
850                    let (bx, by) = BLK_XY[blk];
851                    let mut pb = [0i32; 16];
852                    for y in 0..4 {
853                        for x in 0..4 {
854                            pb[y * 4 + x] = out[(by * 4 + y) * 16 + bx * 4 + x];
855                        }
856                    }
857                    let coef = self.syn.coef[blk];
858                    let d = dc[by * 4 + bx];
859                    let coded = self.syn.coded & (1 << blk) != 0 || d != 0;
860                    self.put_4x4(
861                        x0 + bx * 4,
862                        y0 + by * 4,
863                        &pb,
864                        coded.then_some(&coef),
865                        qp,
866                        0,
867                        Some(d),
868                    );
869                }
870            }
871        }
872        Ok(())
873    }
874
875    /// Inverse Hadamard and scaling of the Intra 16×16 DC levels (§8.5.10).
876    fn luma_dc(&self, qp: i32) -> [i32; 16] {
877        let c = &self.syn.dc_luma;
878        let mut t = [0i32; 16];
879        for i in 0..4 {
880            let (a, b, cc, d) = (c[i * 4], c[i * 4 + 1], c[i * 4 + 2], c[i * 4 + 3]);
881            t[i * 4] = a + b + cc + d;
882            t[i * 4 + 1] = a + b - cc - d;
883            t[i * 4 + 2] = a - b - cc + d;
884            t[i * 4 + 3] = a - b + cc - d;
885        }
886        let mut f = [0i32; 16];
887        for j in 0..4 {
888            let (a, b, cc, d) = (t[j], t[4 + j], t[8 + j], t[12 + j]);
889            f[j] = a + b + cc + d;
890            f[4 + j] = a + b - cc - d;
891            f[8 + j] = a - b - cc + d;
892            f[12 + j] = a - b + cc - d;
893        }
894        let ls = self.ls4[0][(qp % 6) as usize][0];
895        let mut out = [0i32; 16];
896        for k in 0..16 {
897            out[k] = if qp >= 36 {
898                (f[k] * ls) << (qp / 6 - 6)
899            } else {
900                (f[k] * ls + (1 << (5 - qp / 6))) >> (6 - qp / 6)
901            };
902        }
903        out
904    }
905
906    /// Add a 4×4 residual (levels `coef`, raster) over prediction `pred` and
907    /// store at `(x0, y0)` of `plane`. `dc` replaces coefficient 0 already
908    /// scaled (Intra 16×16 and chroma).
909    #[allow(clippy::too_many_arguments)]
910    fn put_4x4(
911        &mut self,
912        x0: usize,
913        y0: usize,
914        pred: &[i32; 16],
915        coef: Option<&[i32; 16]>,
916        qp: i32,
917        plane: usize,
918        dc: Option<i32>,
919    ) {
920        let g = self.geo;
921        let bd = if plane == 0 { self.bd_y } else { self.bd_c };
922        let max = (1i32 << bd) - 1;
923        let p = &mut self.pic.planes[plane];
924        let Some(coef) = coef else {
925            for y in 0..4 {
926                for x in 0..4 {
927                    p.set(x0 + x, g.row(y0 + y), pred[y * 4 + x] as u16);
928                }
929            }
930            return;
931        };
932        let intra = self.syn.kind.is_intra();
933        let list = plane + if intra { 0 } else { 3 };
934        let ls = &self.ls4[list][(qp % 6) as usize];
935        let mut d = [0i32; 16];
936        for k in 0..16 {
937            if coef[k] != 0 {
938                d[k] = if qp >= 24 {
939                    (coef[k] * ls[k]) << (qp / 6 - 4)
940                } else {
941                    (coef[k] * ls[k] + (1 << (3 - qp / 6))) >> (4 - qp / 6)
942                };
943            }
944        }
945        if let Some(dc) = dc {
946            d[0] = dc;
947        }
948        let r = idct4(&d);
949        for y in 0..4 {
950            for x in 0..4 {
951                let v = (pred[y * 4 + x] + r[y * 4 + x]).clamp(0, max);
952                p.set(x0 + x, g.row(y0 + y), v as u16);
953            }
954        }
955    }
956
957    fn put_8x8(
958        &mut self,
959        x0: usize,
960        y0: usize,
961        pred: &[i32; 64],
962        coef: Option<&[i32; 64]>,
963        qp: i32,
964        plane: usize,
965    ) {
966        let g = self.geo;
967        let max = (1i32 << self.bd_y) - 1;
968        let p = &mut self.pic.planes[plane];
969        let Some(coef) = coef else {
970            for y in 0..8 {
971                for x in 0..8 {
972                    p.set(x0 + x, g.row(y0 + y), pred[y * 8 + x] as u16);
973                }
974            }
975            return;
976        };
977        let intra = self.syn.kind.is_intra();
978        let ls = &self.ls8[if intra { 0 } else { 1 }][(qp % 6) as usize];
979        let mut d = [0i32; 64];
980        for k in 0..64 {
981            if coef[k] != 0 {
982                d[k] = if qp >= 36 {
983                    (coef[k] * ls[k]) << (qp / 6 - 6)
984                } else {
985                    (coef[k] * ls[k] + (1 << (5 - qp / 6))) >> (6 - qp / 6)
986                };
987            }
988        }
989        let r = idct8(&d);
990        for y in 0..8 {
991            for x in 0..8 {
992                let v = (pred[y * 8 + x] + r[y * 8 + x]).clamp(0, max);
993                p.set(x0 + x, g.row(y0 + y), v as u16);
994            }
995        }
996    }
997
998    fn inter_luma_residual(&mut self) {
999        let g = self.geo;
1000        let qp = self.qp + 6 * (self.bd_y as i32 - 8);
1001        let (mx, my) = (self.mb_x * 16, self.vmb_y * 16);
1002        if self.syn.t8x8 {
1003            for b8 in 0..4 {
1004                if self.syn.coded8 & (1 << b8) == 0 {
1005                    continue;
1006                }
1007                let (x0, y0) = (mx + (b8 % 2) * 8, my + (b8 / 2) * 8);
1008                let mut pred = [0i32; 64];
1009                for y in 0..8 {
1010                    for x in 0..8 {
1011                        pred[y * 8 + x] = self.pic.planes[0].at(x0 + x, g.row(y0 + y)) as i32;
1012                    }
1013                }
1014                let coef = self.syn.coef8[b8];
1015                self.put_8x8(x0, y0, &pred, Some(&coef), qp, 0);
1016            }
1017        } else {
1018            for blk in 0..16 {
1019                if self.syn.coded & (1 << blk) == 0 {
1020                    continue;
1021                }
1022                let (bx, by) = BLK_XY[blk];
1023                let (x0, y0) = (mx + bx * 4, my + by * 4);
1024                let mut pred = [0i32; 16];
1025                for y in 0..4 {
1026                    for x in 0..4 {
1027                        pred[y * 4 + x] = self.pic.planes[0].at(x0 + x, g.row(y0 + y)) as i32;
1028                    }
1029                }
1030                let coef = self.syn.coef[blk];
1031                self.put_4x4(x0, y0, &pred, Some(&coef), qp, 0, None);
1032            }
1033        }
1034    }
1035
1036    fn intra_chroma_pred(&mut self) {
1037        let g = self.geo;
1038        let ch = if self.c422 { 16 } else { 8 };
1039        // Left availability per 4-row chroma block (MBAFF pairs of the other
1040        // kind; ffmpeg's ALZHEIMER_DC modes are this for 4:2:0).
1041        let mut al_rows = [false; 4];
1042        for (r, a) in al_rows.iter_mut().enumerate().take(ch / 4) {
1043            *a = self.left_avail(r * 4, 4, true);
1044        }
1045        let at = self.intra_nb(0, -1);
1046        let atl = self.intra_nb(-1, -1);
1047        let (x0, y0) = (self.mb_x * 8, self.vmb_y * ch);
1048        for c in 1..3 {
1049            let p = &self.pic.planes[c];
1050            let mut top = [0i32; 9];
1051            let mut left = [0i32; 17];
1052            if atl {
1053                top[0] = p.at(x0 - 1, g.row(y0 - 1)) as i32;
1054                left[0] = top[0];
1055            }
1056            if at {
1057                for i in 0..8 {
1058                    top[1 + i] = p.at(x0 + i, g.row(y0 - 1)) as i32;
1059                }
1060            }
1061            for i in 0..ch {
1062                if al_rows[i / 4] {
1063                    left[1 + i] = p.at(x0 - 1, g.row(y0 + i)) as i32;
1064                }
1065            }
1066            let mut out = [0i32; 128];
1067            intra::pred_chroma(
1068                &mut out,
1069                ch,
1070                self.syn.chroma_mode,
1071                &top,
1072                &left,
1073                at,
1074                &al_rows,
1075                self.bd_c,
1076            );
1077            let p = &mut self.pic.planes[c];
1078            for y in 0..ch {
1079                for x in 0..8 {
1080                    p.set(x0 + x, g.row(y0 + y), out[y * 8 + x] as u16);
1081                }
1082            }
1083        }
1084    }
1085
1086    fn chroma_residual(&mut self, intra: bool) {
1087        let g = self.geo;
1088        let cbp_c = self.syn.cbp >> 4;
1089        if cbp_c == 0 {
1090            return;
1091        }
1092        let ch = if self.c422 { 16 } else { 8 };
1093        let (x0, y0) = (self.mb_x * 8, self.vmb_y * ch);
1094        for c in 0..2 {
1095            let qp = self.qpc_prime(self.qp, c);
1096            let list = 1 + c + if intra { 0 } else { 3 };
1097            let cc = self.syn.dc_c[c];
1098            let mut dcs = [0i32; 8];
1099            if self.c422 {
1100                // §8.5.11.1-2 (4:2:2): c is 4×2 from the scan
1101                // c = [[c0, c2], [c1, c5], [c3, c6], [c4, c7]];
1102                // f = A4 · c · A2ᵀ, then QP'c + 3 dequantisation.
1103                let m = [
1104                    [cc[0], cc[2]],
1105                    [cc[1], cc[5]],
1106                    [cc[3], cc[6]],
1107                    [cc[4], cc[7]],
1108                ];
1109                let t: Vec<[i32; 2]> = m.iter().map(|r| [r[0] + r[1], r[0] - r[1]]).collect();
1110                let qdc = qp + 3;
1111                let ls = self.ls4[list][(qdc % 6) as usize][0];
1112                for x in 0..2 {
1113                    let (z0, z1) = (t[0][x] + t[2][x], t[0][x] - t[2][x]);
1114                    let (z2, z3) = (t[1][x] - t[3][x], t[1][x] + t[3][x]);
1115                    let f = [z0 + z3, z1 + z2, z1 - z2, z0 - z3];
1116                    for (y, fv) in f.into_iter().enumerate() {
1117                        dcs[y * 2 + x] = if qdc >= 36 {
1118                            (fv * ls) << (qdc / 6 - 6)
1119                        } else {
1120                            (fv * ls + (1 << (5 - qdc / 6))) >> (6 - qdc / 6)
1121                        };
1122                    }
1123                }
1124            } else {
1125                let ls = self.ls4[list][(qp % 6) as usize][0];
1126                let f = [
1127                    cc[0] + cc[1] + cc[2] + cc[3],
1128                    cc[0] - cc[1] + cc[2] - cc[3],
1129                    cc[0] + cc[1] - cc[2] - cc[3],
1130                    cc[0] - cc[1] - cc[2] + cc[3],
1131                ];
1132                for blk in 0..4 {
1133                    dcs[blk] = ((f[blk] * ls) << (qp / 6)) >> 5;
1134                }
1135            }
1136            for blk in 0..if self.c422 { 8 } else { 4 } {
1137                let dc = dcs[blk];
1138                let coded = self.syn.coded_c[c] & (1 << blk) != 0;
1139                if dc == 0 && !coded {
1140                    continue;
1141                }
1142                let (bx, by) = (x0 + (blk % 2) * 4, y0 + (blk / 2) * 4);
1143                let mut pred = [0i32; 16];
1144                for y in 0..4 {
1145                    for x in 0..4 {
1146                        pred[y * 4 + x] = self.pic.planes[1 + c].at(bx + x, g.row(by + y)) as i32;
1147                    }
1148                }
1149                let coef = self.syn.ac_c[c][blk];
1150                self.put_4x4(bx, by, &pred, Some(&coef), qp, 1 + c, Some(dc));
1151            }
1152        }
1153    }
1154
1155    // ------------------------------------------------------------------
1156    // Motion
1157    // ------------------------------------------------------------------
1158
1159    /// Neighbour partition motion for list `l` at 4×4 position `(x, y)`
1160    /// relative to the macroblock. `done` marks the current macroblock's 4×4
1161    /// blocks whose motion is already known.
1162    fn nb_motion(&self, l: usize, x: isize, y: isize, done: u16) -> NbMotion {
1163        self.nb_motion_row(l, x, y, done, false)
1164    }
1165
1166    /// [`Self::nb_motion`] for neighbour D (see [`Self::blk_nb_row`]).
1167    fn nb_motion_d(&self, l: usize, x: isize, y: isize, done: u16) -> NbMotion {
1168        self.nb_motion_row(l, x, y, done, true)
1169    }
1170
1171    fn nb_motion_row(&self, l: usize, x: isize, y: isize, done: u16, last_row: bool) -> NbMotion {
1172        let (a, k) = self.blk_nb_row(x, y, last_row)?;
1173        let m = &self.pic.mbs[a];
1174        if a == self.addr {
1175            if done & (1 << k) == 0 {
1176                return None;
1177            }
1178        } else if m.kind.is_intra() {
1179            return Some((-1, [0, 0]));
1180        }
1181        let r = m.ref_idx[l][(k / 8) * 2 + (k % 4) / 2];
1182        if r < 0 {
1183            return Some((-1, [0, 0]));
1184        }
1185        let mv = m.mv[l][k];
1186        // MBAFF: a neighbour of the other kind counts in this one's units
1187        // (§8.4.1.3.1; ffmpeg MAP_F2F).
1188        if a != self.addr && self.mbaff && m.field != self.mb_field {
1189            return Some(if self.mb_field {
1190                (r * 2, [mv[0], mv[1] / 2])
1191            } else {
1192                (r >> 1, [mv[0], mv[1] * 2])
1193            });
1194        }
1195        Some((r, mv))
1196    }
1197
1198    /// `mvpLX` (§8.4.1.3) for the partition at `(x, y)` of size `w × h` (4×4
1199    /// units). `dir` selects the 16×8 / 8×16 directional rule: 1/2 for the
1200    /// upper/lower 16×8, 3/4 for the left/right 8×16.
1201    #[allow(clippy::too_many_arguments)]
1202    fn mv_pred(
1203        &self,
1204        l: usize,
1205        r: i8,
1206        x: usize,
1207        y: usize,
1208        w: usize,
1209        dir: u8,
1210        done: u16,
1211    ) -> [i16; 2] {
1212        let (xi, yi) = (x as isize, y as isize);
1213        let a = self.nb_motion(l, xi - 1, yi, done);
1214        let b = self.nb_motion(l, xi, yi - 1, done);
1215        let c = self
1216            .nb_motion(l, xi + w as isize, yi - 1, done)
1217            .or_else(|| self.nb_motion_d(l, xi - 1, yi - 1, done));
1218        let un = (-1i8, [0i16, 0]);
1219        match dir {
1220            1 => {
1221                if let Some(b) = b.filter(|b| b.0 == r) {
1222                    return b.1;
1223                }
1224            }
1225            2 | 3 => {
1226                if let Some(a) = a.filter(|a| a.0 == r) {
1227                    return a.1;
1228                }
1229            }
1230            4 => {
1231                if let Some(c) = c.filter(|c| c.0 == r) {
1232                    return c.1;
1233                }
1234            }
1235            _ => {}
1236        }
1237        let (a, b, c) = match (a, b, c) {
1238            (Some(a), None, None) => (a, a, a),
1239            _ => (a.unwrap_or(un), b.unwrap_or(un), c.unwrap_or(un)),
1240        };
1241        let matches = (a.0 == r) as u8 + (b.0 == r) as u8 + (c.0 == r) as u8;
1242        if matches == 1 {
1243            if a.0 == r {
1244                return a.1;
1245            }
1246            if b.0 == r {
1247                return b.1;
1248            }
1249            return c.1;
1250        }
1251        let med = |p: i16, q: i16, s: i16| p.max(q).min(p.min(q).max(s));
1252        [med(a.1[0], b.1[0], c.1[0]), med(a.1[1], b.1[1], c.1[1])]
1253    }
1254
1255    /// Store motion for list `l` over the 4×4 blocks of a partition.
1256    #[allow(clippy::too_many_arguments)]
1257    fn set_motion(
1258        &mut self,
1259        l: usize,
1260        x: usize,
1261        y: usize,
1262        w: usize,
1263        h: usize,
1264        r: i8,
1265        mv: [i16; 2],
1266    ) {
1267        let id = if r >= 0 {
1268            self.rl(l)
1269                .get(r as usize)
1270                .map(|e| e.id as i32)
1271                .unwrap_or(-1)
1272        } else {
1273            -1
1274        };
1275        let m = &mut self.pic.mbs[self.addr];
1276        for yy in y..y + h {
1277            for xx in x..x + w {
1278                m.mv[l][yy * 4 + xx] = if r >= 0 { mv } else { [0, 0] };
1279                let k8 = (yy / 2) * 2 + xx / 2;
1280                m.ref_idx[l][k8] = r;
1281                m.ref_id[l][k8] = id;
1282            }
1283        }
1284    }
1285
1286    fn p_skip_motion(&mut self) {
1287        let a = self.nb_motion(0, -1, 0, 0);
1288        let b = self.nb_motion(0, 0, -1, 0);
1289        let zero = match (a, b) {
1290            (Some(a), Some(b)) => (a.0 == 0 && a.1 == [0, 0]) || (b.0 == 0 && b.1 == [0, 0]),
1291            _ => true,
1292        };
1293        let mv = if zero {
1294            [0, 0]
1295        } else {
1296            self.mv_pred(0, 0, 0, 0, 4, 0, 0)
1297        };
1298        self.set_motion(0, 0, 0, 4, 4, 0, mv);
1299        self.set_motion(1, 0, 0, 4, 4, -1, [0, 0]);
1300    }
1301
1302    fn check_ref(&self, l: usize, r: i8) -> Result<()> {
1303        if r < 0 || r as usize >= self.rl(l).len() {
1304            return Err(format!("ref_idx_l{l} {r} past the list"));
1305        }
1306        Ok(())
1307    }
1308
1309    fn inter_motion(&mut self) -> Result<()> {
1310        let is_b = self.syn.kind == MbKind::B;
1311        let t = self.syn.mb_type;
1312        if is_b && t == 0 {
1313            // B_Direct_16x16.
1314            let m = &mut self.pic.mbs[self.addr];
1315            m.b_direct16 = true;
1316            m.direct8 = 0xF;
1317            self.direct_motion(0xF);
1318            return Ok(());
1319        }
1320        let (shape, preds) = if is_b { b_mb_layout(t) } else { p_mb_layout(t) };
1321        let mut done = 0u16;
1322        if shape != Shape::S8x8 {
1323            for (i, &(x, y, w, h)) in shape.parts().iter().enumerate() {
1324                let dir = match (shape, i) {
1325                    (Shape::S16x8, 0) => 1,
1326                    (Shape::S16x8, _) => 2,
1327                    (Shape::S8x16, 0) => 3,
1328                    (Shape::S8x16, _) => 4,
1329                    _ => 0,
1330                };
1331                for l in 0..2 {
1332                    if preds[i] & (1 << l) != 0 {
1333                        let r = self.syn.ref_idx[l][i];
1334                        self.check_ref(l, r)?;
1335                        let p = self.mv_pred(l, r, x, y, w, dir, done);
1336                        let d = self.syn.mvd[l][y * 4 + x];
1337                        let mv = [(p[0] as i32 + d[0]) as i16, (p[1] as i32 + d[1]) as i16];
1338                        self.set_motion(l, x, y, w, h, r, mv);
1339                    } else {
1340                        self.set_motion(l, x, y, w, h, -1, [0, 0]);
1341                    }
1342                }
1343                for yy in y..y + h {
1344                    for xx in x..x + w {
1345                        done |= 1 << (yy * 4 + xx);
1346                    }
1347                }
1348            }
1349            return Ok(());
1350        }
1351        // 8×8 sub-macroblocks, in order.
1352        let mut direct = 0u8;
1353        for s in 0..4 {
1354            if is_b && self.syn.sub[s] == 0 {
1355                direct |= 1 << s;
1356            }
1357        }
1358        self.pic.mbs[self.addr].direct8 = direct;
1359        for s in 0..4 {
1360            let (sx, sy) = ((s % 2) * 2, (s / 2) * 2);
1361            if direct & (1 << s) != 0 {
1362                self.direct_motion(1 << s);
1363            } else {
1364                let (pred, w, h) = if is_b {
1365                    b_sub_layout(self.syn.sub[s])
1366                } else {
1367                    p_sub_layout(self.syn.sub[s])
1368                };
1369                for l in 0..2 {
1370                    if pred & (1 << l) == 0 {
1371                        self.set_motion(l, sx, sy, 2, 2, -1, [0, 0]);
1372                    }
1373                }
1374                let mut sub_done = done;
1375                for py in (0..2).step_by(h) {
1376                    for px in (0..2).step_by(w) {
1377                        let (x, y) = (sx + px, sy + py);
1378                        for l in 0..2 {
1379                            if pred & (1 << l) == 0 {
1380                                continue;
1381                            }
1382                            let r = self.syn.ref_idx[l][s];
1383                            self.check_ref(l, r)?;
1384                            let p = self.mv_pred(l, r, x, y, w, 0, sub_done);
1385                            let d = self.syn.mvd[l][y * 4 + x];
1386                            let mv = [(p[0] as i32 + d[0]) as i16, (p[1] as i32 + d[1]) as i16];
1387                            self.set_motion(l, x, y, w, h, r, mv);
1388                        }
1389                        for yy in y..y + h {
1390                            for xx in x..x + w {
1391                                sub_done |= 1 << (yy * 4 + xx);
1392                            }
1393                        }
1394                    }
1395                }
1396            }
1397            for yy in sy..sy + 2 {
1398                for xx in sx..sx + 2 {
1399                    done |= 1 << (yy * 4 + xx);
1400                }
1401            }
1402        }
1403        Ok(())
1404    }
1405
1406    /// The colocated 4×4 block (§8.4.1.2.1) for raster block `k` of this
1407    /// macroblock: `(mvCol, refIdxCol, id of the picture it references,
1408    /// vertical scaling)`, or `None` when the colocated macroblock is intra.
1409    fn colocated(&self, k: usize) -> Option<([i16; 2], i8, i32, ColScale)> {
1410        let col = self.rl(1).first()?;
1411        let st = &self.dpb[col.dpb];
1412        let col_fields = st.field_mbs[0].is_some() || st.field_mbs[1].is_some();
1413        let w = self.pic.w_mbs;
1414        let (mx, my) = (self.mb_x, self.mb_y);
1415        let (x8, y8) = ((k % 4) / 2, (k / 4) / 2);
1416        // The corner block of the 8×8 the block is in.
1417        let corner = if self.sps.direct_8x8_inference {
1418            (y8 * 3) * 4 + x8 * 3
1419        } else {
1420            k
1421        };
1422        // A frame macroblock over a field co-located picture or pair takes
1423        // the field closer in POC (bottom on a tie).
1424        let closer = || {
1425            let cur = self.pic.poc as i64;
1426            ((st.field_poc[0] as i64 - cur).abs() >= (st.field_poc[1] as i64 - cur).abs()) as usize
1427        };
1428        // (motion of the co-located MB, its 4×4 block, vertical scaling),
1429        // after ffmpeg h264_direct.c: a field macroblock of a frame pair
1430        // reads the pair's upper MB for its top 8×8 row and the lower one
1431        // (4×4 row 2) for the bottom; a frame macroblock of a field one
1432        // reads rows 2·(mb_y & 1) + y8 of the chosen field's macroblock.
1433        // (direct_8x8_inference is mandatory whenever the two mix.)
1434        let (m, k, scale): (&MbInfo, usize, ColScale) = if self.hdr.field_pic {
1435            if col_fields {
1436                (
1437                    st.field_mbs[(col.structure == 2) as usize]
1438                        .as_ref()?
1439                        .get(self.addr)?,
1440                    corner,
1441                    ColScale::Same,
1442                )
1443            } else {
1444                // Field from a frame store: the MB at this field's rows; an
1445                // MBAFF field pair there is a field of the same parity.
1446                let par = self.hdr.bottom_field as usize;
1447                let m0 = st.mbs.get((2 * my + par) * w + mx)?;
1448                if m0.field {
1449                    (m0, corner, ColScale::Same)
1450                } else {
1451                    (
1452                        st.mbs.get((2 * my + y8) * w + mx)?,
1453                        (2 * y8) * 4 + x8 * 3,
1454                        ColScale::FrameToField,
1455                    )
1456                }
1457            }
1458        } else if self.mb_field {
1459            // MBAFF field macroblock.
1460            let par = my % 2;
1461            if col_fields {
1462                (
1463                    st.field_mbs[par].as_ref()?.get((my / 2) * w + mx)?,
1464                    corner,
1465                    ColScale::Same,
1466                )
1467            } else {
1468                let m0 = st.mbs.get(self.addr)?;
1469                if m0.field {
1470                    (m0, corner, ColScale::Same)
1471                } else {
1472                    (
1473                        st.mbs.get(((my & !1) + y8) * w + mx)?,
1474                        (2 * y8) * 4 + x8 * 3,
1475                        ColScale::FrameToField,
1476                    )
1477                }
1478            }
1479        } else {
1480            let row = 2 * (my & 1) + y8;
1481            if col_fields {
1482                (
1483                    st.field_mbs[closer()].as_ref()?.get((my / 2) * w + mx)?,
1484                    row * 4 + x8 * 3,
1485                    ColScale::FieldToFrame,
1486                )
1487            } else {
1488                let m0 = st.mbs.get(self.addr)?;
1489                if m0.field {
1490                    (
1491                        st.mbs.get(((my & !1) + closer()) * w + mx)?,
1492                        row * 4 + x8 * 3,
1493                        ColScale::FieldToFrame,
1494                    )
1495                } else {
1496                    (m0, corner, ColScale::Same)
1497                }
1498            }
1499        };
1500        if m.kind.is_intra() || m.kind == MbKind::None {
1501            return None;
1502        }
1503        let k8 = (k / 8) * 2 + (k % 4) / 2;
1504        if m.ref_idx[0][k8] >= 0 {
1505            Some((m.mv[0][k], m.ref_idx[0][k8], m.ref_id[0][k8], scale))
1506        } else {
1507            Some((m.mv[1][k], m.ref_idx[1][k8], m.ref_id[1][k8], scale))
1508        }
1509    }
1510
1511    /// Direct prediction (§8.4.1.2) for the 8×8 blocks in `mask`.
1512    fn direct_motion(&mut self, mask: u8) {
1513        if self.hdr.direct_spatial {
1514            self.direct_spatial(mask);
1515        } else {
1516            self.direct_temporal(mask);
1517        }
1518    }
1519
1520    fn direct_spatial(&mut self, mask: u8) {
1521        // Reference indices and predictors from the whole macroblock's
1522        // neighbours.
1523        let mut refs = [-1i8; 2];
1524        let mut mvp = [[0i16; 2]; 2];
1525        for l in 0..2 {
1526            let a = self.nb_motion(l, -1, 0, 0);
1527            let b = self.nb_motion(l, 0, -1, 0);
1528            let c = self
1529                .nb_motion(l, 4, -1, 0)
1530                .or_else(|| self.nb_motion(l, -1, -1, 0));
1531            let min_pos = |x: i8, y: i8| if x >= 0 && y >= 0 { x.min(y) } else { x.max(y) };
1532            let ra = a.map_or(-1, |m| m.0);
1533            let rb = b.map_or(-1, |m| m.0);
1534            let rc = c.map_or(-1, |m| m.0);
1535            refs[l] = min_pos(ra, min_pos(rb, rc));
1536        }
1537        let zero_pred = refs[0] < 0 && refs[1] < 0;
1538        if zero_pred {
1539            refs = [0, 0];
1540        } else {
1541            for l in 0..2 {
1542                if refs[l] >= 0 {
1543                    mvp[l] = self.mv_pred(l, refs[l], 0, 0, 4, 0, 0);
1544                }
1545            }
1546        }
1547        let col_short = self.rl(1).first().is_some_and(|e| !e.long_term);
1548        for s in 0..4 {
1549            if mask & (1 << s) == 0 {
1550                continue;
1551            }
1552            let (sx, sy) = ((s % 2) * 2, (s / 2) * 2);
1553            for by in sy..sy + 2 {
1554                for bx in sx..sx + 2 {
1555                    let k = by * 4 + bx;
1556                    let col_zero = col_short
1557                        && !zero_pred
1558                        && match self.colocated(k) {
1559                            Some((mv, r, _, _)) => {
1560                                r == 0 && (-1..=1).contains(&mv[0]) && (-1..=1).contains(&mv[1])
1561                            }
1562                            None => false,
1563                        };
1564                    for l in 0..2 {
1565                        let r = refs[l];
1566                        let mv = if r < 0 || zero_pred || (r == 0 && col_zero) {
1567                            [0, 0]
1568                        } else {
1569                            mvp[l]
1570                        };
1571                        self.set_motion(l, bx, by, 1, 1, r, mv);
1572                    }
1573                }
1574            }
1575        }
1576    }
1577
1578    fn direct_temporal(&mut self, mask: u8) {
1579        let cur_poc = self.cur_poc();
1580        for s in 0..4 {
1581            if mask & (1 << s) == 0 {
1582                continue;
1583            }
1584            let (sx, sy) = ((s % 2) * 2, (s / 2) * 2);
1585            for by in sy..sy + 2 {
1586                for bx in sx..sx + 2 {
1587                    let k = by * 4 + bx;
1588                    let (mv_col, ref0) = match self.colocated(k) {
1589                        None => ([0i16, 0], 0usize),
1590                        Some((mv, _, id, scale)) => {
1591                            // Map the co-located reference to this picture's
1592                            // structure: a field takes the same-parity field
1593                            // of a frame reference, a frame the whole frame.
1594                            let id = if !self.field_coded() {
1595                                id | 3
1596                            } else if id & 3 == 3 {
1597                                let par = if self.mb_field {
1598                                    self.mb_y % 2
1599                                } else {
1600                                    self.hdr.bottom_field as usize
1601                                };
1602                                (id & !3) + 1 + par as i32
1603                            } else {
1604                                id
1605                            };
1606                            let r = self
1607                                .rl(0)
1608                                .iter()
1609                                .position(|e| e.id as i32 == id)
1610                                .unwrap_or(0);
1611                            let my = match scale {
1612                                ColScale::Same => mv[1],
1613                                ColScale::FrameToField => mv[1] / 2,
1614                                ColScale::FieldToFrame => mv[1] * 2,
1615                            };
1616                            ([mv[0], my], r)
1617                        }
1618                    };
1619                    let (mv0, mv1) = match (self.rl(0).get(ref0), self.rl(1).first()) {
1620                        (Some(p0), Some(p1)) => {
1621                            let td = (p1.poc - p0.poc).clamp(-128, 127);
1622                            if p0.long_term || td == 0 {
1623                                (mv_col, [0, 0])
1624                            } else {
1625                                let tb = (cur_poc - p0.poc).clamp(-128, 127);
1626                                let tx = (16384 + (td / 2).abs()) / td;
1627                                let dsf = ((tb * tx + 32) >> 6).clamp(-1024, 1023);
1628                                let m0 = [
1629                                    ((dsf * mv_col[0] as i32 + 128) >> 8) as i16,
1630                                    ((dsf * mv_col[1] as i32 + 128) >> 8) as i16,
1631                                ];
1632                                (m0, [m0[0] - mv_col[0], m0[1] - mv_col[1]])
1633                            }
1634                        }
1635                        _ => ([0, 0], [0, 0]),
1636                    };
1637                    self.set_motion(0, bx, by, 1, 1, ref0 as i8, mv0);
1638                    self.set_motion(1, bx, by, 1, 1, 0, mv1);
1639                }
1640            }
1641        }
1642    }
1643
1644    // ------------------------------------------------------------------
1645    // Motion compensation
1646    // ------------------------------------------------------------------
1647
1648    /// Predict the whole macroblock from the motion stored in its
1649    /// [`MbInfo`], in the largest blocks whose motion is uniform.
1650    fn motion_compensate(&mut self) {
1651        let m = self.pic.mbs[self.addr];
1652        let same = |a: usize, b: usize| {
1653            (0..2).all(|l| {
1654                m.mv[l][a] == m.mv[l][b]
1655                    && m.ref_idx[l][(a / 8) * 2 + (a % 4) / 2]
1656                        == m.ref_idx[l][(b / 8) * 2 + (b % 4) / 2]
1657            })
1658        };
1659        if (1..16).all(|k| same(0, k)) {
1660            self.mc_block(&m, 0, 0, 4, 4);
1661            return;
1662        }
1663        for s in 0..4 {
1664            let (sx, sy) = ((s % 2) * 2, (s / 2) * 2);
1665            let k0 = sy * 4 + sx;
1666            if [1, 4, 5].iter().all(|&d| same(k0, k0 + d)) {
1667                self.mc_block(&m, sx, sy, 2, 2);
1668            } else {
1669                for d in [0, 1, 4, 5] {
1670                    let k = k0 + d;
1671                    self.mc_block(&m, k % 4, k / 4, 1, 1);
1672                }
1673            }
1674        }
1675    }
1676
1677    /// Inter prediction of the `w × h` (4×4 units) block at `(bx, by)`.
1678    fn mc_block(&mut self, m: &MbInfo, bx: usize, by: usize, w: usize, h: usize) {
1679        let k = by * 4 + bx;
1680        let k8 = (by / 2) * 2 + bx / 2;
1681        let r = [m.ref_idx[0][k8], m.ref_idx[1][k8]];
1682        let (pw, ph) = (w * 4, h * 4);
1683        let (x0, y0) = (self.mb_x * 16 + bx * 4, self.vmb_y * 16 + by * 4);
1684        let mut pred = self
1685            .mc_pred
1686            .take()
1687            .unwrap_or_else(|| Box::new([[0; 256]; 2]));
1688        let mut used = [false; 2];
1689        for l in 0..2 {
1690            if r[l] < 0 {
1691                continue;
1692            }
1693            let Some(e) = self.rl(l).get(r[l] as usize) else {
1694                continue;
1695            };
1696            used[l] = true;
1697            let planes = e.planes.clone();
1698            let refp = &planes[0];
1699            let mv = m.mv[l][k];
1700            inter::luma(
1701                refp,
1702                x0 as isize + (mv[0] >> 2) as isize,
1703                y0 as isize + (mv[1] >> 2) as isize,
1704                (mv[0] & 3) as usize,
1705                (mv[1] & 3) as usize,
1706                pw,
1707                ph,
1708                &mut pred[l],
1709                self.bd_y,
1710                &mut self.mc_win,
1711                &mut self.luma_scratch,
1712            );
1713        }
1714        self.weight_and_store(0, x0, y0, pw, ph, &pred, used, r);
1715        if !self.chroma {
1716            self.mc_pred = Some(pred);
1717            return;
1718        }
1719        let c422 = self.c422;
1720        let (cw, ch) = (pw / 2, if c422 { ph } else { ph / 2 });
1721        let (cx, cy) = (x0 / 2, if c422 { y0 } else { y0 / 2 });
1722        for c in 1..3 {
1723            let cpred = &mut pred;
1724            for l in 0..2 {
1725                if !used[l] {
1726                    continue;
1727                }
1728                let e = &self.rl(l)[r[l] as usize];
1729                let planes = e.planes.clone();
1730                let refp = &planes[c];
1731                let mut mv = m.mv[l][k];
1732                // Field prediction from the other parity: the chroma
1733                // vector shifts by a quarter of a chroma sample (ffmpeg
1734                // mc_dir_part: my += 2 * (cur parity - ref parity)).
1735                if (self.hdr.field_pic || self.mb_field) && e.structure != 3 {
1736                    let cur_par = if self.mb_field {
1737                        (self.mb_y % 2) as i16
1738                    } else {
1739                        self.hdr.bottom_field as i16
1740                    };
1741                    let ref_par = (e.structure == 2) as i16;
1742                    mv[1] += 2 * (cur_par - ref_par);
1743                }
1744                inter::chroma(
1745                    refp,
1746                    cx as isize + (mv[0] >> 3) as isize,
1747                    // 4:2:2: vertical chroma is luma-sized, quarter-sample
1748                    // vectors become eighths (§8.4.2.2.2).
1749                    cy as isize + (mv[1] >> if c422 { 2 } else { 3 }) as isize,
1750                    (mv[0] & 7) as i32,
1751                    if c422 {
1752                        ((mv[1] & 3) << 1) as i32
1753                    } else {
1754                        (mv[1] & 7) as i32
1755                    },
1756                    cw,
1757                    ch,
1758                    &mut cpred[l],
1759                    &mut self.mc_win,
1760                );
1761            }
1762            self.weight_and_store(c, cx, cy, cw, ch, cpred, used, r);
1763        }
1764        self.mc_pred = Some(pred);
1765    }
1766
1767    /// Weighted sample prediction (§8.4.2.3) and store.
1768    #[allow(clippy::too_many_arguments)]
1769    fn weight_and_store(
1770        &mut self,
1771        c: usize,
1772        x0: usize,
1773        y0: usize,
1774        w: usize,
1775        h: usize,
1776        pred: &[[i32; 256]; 2],
1777        used: [bool; 2],
1778        r: [i8; 2],
1779    ) {
1780        let bd = if c == 0 { self.bd_y } else { self.bd_c };
1781        let max = (1i32 << bd) - 1;
1782        let n = w * h;
1783        let explicit = self.hdr.weights.as_ref();
1784        let implicit = self.hdr.slice_type == SliceType::B && self.pps.weighted_bipred_idc == 2;
1785        let (p0, p1) = (&pred[0][..n], &pred[1][..n]);
1786        match (used[0], used[1]) {
1787            (true, true) => {
1788                let wts = if let Some(wt) = explicit {
1789                    let (w0, o0) = wt.weights[0][self.wp_idx(r[0])][c];
1790                    let (w1, o1) = wt.weights[1][self.wp_idx(r[1])][c];
1791                    let log_wd = if c == 0 {
1792                        wt.luma_log2_denom
1793                    } else {
1794                        wt.chroma_log2_denom
1795                    } as i32;
1796                    let sh = bd as i32 - 8;
1797                    Some((w0, w1, (o0 << sh), (o1 << sh), log_wd))
1798                } else if implicit {
1799                    let (w0, w1) = self.implicit_weights(r[0] as usize, r[1] as usize);
1800                    Some((w0, w1, 0, 0, 5))
1801                } else {
1802                    None
1803                };
1804                match wts {
1805                    Some((w0, w1, o0, o1, lw)) => {
1806                        let o = (o0 + o1 + 1) >> 1;
1807                        self.store2(c, x0, y0, w, h, p0, p1, |a, b| {
1808                            (((a * w0 + b * w1 + (1 << lw)) >> (lw + 1)) + o).clamp(0, max)
1809                        });
1810                    }
1811                    None => self.store2(c, x0, y0, w, h, p0, p1, |a, b| (a + b + 1) >> 1),
1812                }
1813            }
1814            (true, false) | (false, true) => {
1815                let l = if used[0] { 0 } else { 1 };
1816                let pl = &pred[l][..n];
1817                match explicit {
1818                    Some(wt) => {
1819                        let (wl, ol) = wt.weights[l][self.wp_idx(r[l])][c];
1820                        let lw = if c == 0 {
1821                            wt.luma_log2_denom
1822                        } else {
1823                            wt.chroma_log2_denom
1824                        } as i32;
1825                        let o = ol << (bd as i32 - 8);
1826                        if lw >= 1 {
1827                            self.store1(c, x0, y0, w, h, pl, |a| {
1828                                (((a * wl + (1 << (lw - 1))) >> lw) + o).clamp(0, max)
1829                            });
1830                        } else {
1831                            self.store1(c, x0, y0, w, h, pl, |a| (a * wl + o).clamp(0, max));
1832                        }
1833                    }
1834                    None => self.store1(c, x0, y0, w, h, pl, |a| a),
1835                }
1836            }
1837            _ => {
1838                // No usable reference (broken stream): leave mid-grey.
1839                let grey = [1 << (bd - 1); 256];
1840                self.store1(c, x0, y0, w, h, &grey[..n], |a| a);
1841            }
1842        }
1843    }
1844
1845    /// Write `f(src)` for each sample of `src` (`w × h`, row-major) to the
1846    /// block at `(x0, y0)` of plane `c`, a row at a time (`geo` maps a
1847    /// field's rows into the frame).
1848    #[inline(always)]
1849    fn store1(
1850        &mut self,
1851        c: usize,
1852        x0: usize,
1853        y0: usize,
1854        w: usize,
1855        h: usize,
1856        src: &[i32],
1857        f: impl Fn(i32) -> i32,
1858    ) {
1859        let g = self.geo;
1860        let p = &mut self.pic.planes[c];
1861        let stride = p.width;
1862        for (y, s) in src.chunks_exact(w).take(h).enumerate() {
1863            for (d, &a) in p.data[g.row(y0 + y) * stride + x0..][..w].iter_mut().zip(s) {
1864                *d = f(a) as u16;
1865            }
1866        }
1867    }
1868
1869    /// [`store1`](Self::store1) over two predictions.
1870    #[inline(always)]
1871    #[allow(clippy::too_many_arguments)]
1872    fn store2(
1873        &mut self,
1874        c: usize,
1875        x0: usize,
1876        y0: usize,
1877        w: usize,
1878        h: usize,
1879        s0: &[i32],
1880        s1: &[i32],
1881        f: impl Fn(i32, i32) -> i32,
1882    ) {
1883        let g = self.geo;
1884        let p = &mut self.pic.planes[c];
1885        let stride = p.width;
1886        for (y, (a, b)) in s0
1887            .chunks_exact(w)
1888            .zip(s1.chunks_exact(w))
1889            .take(h)
1890            .enumerate()
1891        {
1892            for ((d, &a), &b) in p.data[g.row(y0 + y) * stride + x0..][..w]
1893                .iter_mut()
1894                .zip(a)
1895                .zip(b)
1896            {
1897                *d = f(a, b) as u16;
1898            }
1899        }
1900    }
1901
1902    /// The current macroblock's POC: its field's in an MBAFF field
1903    /// macroblock.
1904    fn cur_poc(&self) -> i32 {
1905        if self.mb_field {
1906            self.pic.field_poc[self.mb_y % 2]
1907        } else {
1908            self.pic.poc
1909        }
1910    }
1911
1912    /// Explicit weight table index of reference `r` (§8.4.2.3:
1913    /// `refIdxL0WP`, halved in an MBAFF field macroblock).
1914    #[inline]
1915    fn wp_idx(&self, r: i8) -> usize {
1916        (r >> self.mb_field as i8) as usize
1917    }
1918
1919    /// Implicit bi-prediction weights `(w0, w1)` (§8.4.2.3.1).
1920    fn implicit_weights(&self, r0: usize, r1: usize) -> (i32, i32) {
1921        let (Some(p0), Some(p1)) = (self.rl(0).get(r0), self.rl(1).get(r1)) else {
1922            return (32, 32);
1923        };
1924        let td = (p1.poc - p0.poc).clamp(-128, 127);
1925        if td == 0 || p0.long_term || p1.long_term {
1926            return (32, 32);
1927        }
1928        let tb = (self.cur_poc() - p0.poc).clamp(-128, 127);
1929        let tx = (16384 + (td / 2).abs()) / td;
1930        let dsf = ((tb * tx + 32) >> 6).clamp(-1024, 1023);
1931        if !(-64..=128).contains(&(dsf >> 2)) {
1932            return (32, 32);
1933        }
1934        (64 - (dsf >> 2), dsf >> 2)
1935    }
1936
1937    // ------------------------------------------------------------------
1938    // CABAC context initialisation
1939    // ------------------------------------------------------------------
1940
1941    fn init_contexts(&mut self) {
1942        let table = if self.hdr.slice_type == SliceType::I {
1943            &super::cabac_init::INIT_I
1944        } else {
1945            super::cabac_init::INIT_PB[self.hdr.cabac_init_idc as usize]
1946        };
1947        self.ctx = table
1948            .iter()
1949            .map(|&[m, n]| Ctx::h264(m, n, self.hdr.qp))
1950            .collect();
1951    }
1952}
1953
1954/// 4×4 inverse transform (§8.5.12.2), rows then columns, `(x + 32) >> 6`.
1955pub fn idct4(d: &[i32; 16]) -> [i32; 16] {
1956    let mut t = [0i32; 16];
1957    for i in 0..4 {
1958        let r = &d[i * 4..i * 4 + 4];
1959        let e0 = r[0] + r[2];
1960        let e1 = r[0] - r[2];
1961        let e2 = (r[1] >> 1) - r[3];
1962        let e3 = r[1] + (r[3] >> 1);
1963        t[i * 4] = e0 + e3;
1964        t[i * 4 + 1] = e1 + e2;
1965        t[i * 4 + 2] = e1 - e2;
1966        t[i * 4 + 3] = e0 - e3;
1967    }
1968    let mut out = [0i32; 16];
1969    for j in 0..4 {
1970        let (f0, f1, f2, f3) = (t[j], t[4 + j], t[8 + j], t[12 + j]);
1971        let g0 = f0 + f2;
1972        let g1 = f0 - f2;
1973        let g2 = (f1 >> 1) - f3;
1974        let g3 = f1 + (f3 >> 1);
1975        out[j] = (g0 + g3 + 32) >> 6;
1976        out[4 + j] = (g1 + g2 + 32) >> 6;
1977        out[8 + j] = (g1 - g2 + 32) >> 6;
1978        out[12 + j] = (g0 - g3 + 32) >> 6;
1979    }
1980    out
1981}
1982
1983fn idct8_1d(d: [i32; 8]) -> [i32; 8] {
1984    let a0 = d[0] + d[4];
1985    let a4 = d[0] - d[4];
1986    let a2 = (d[2] >> 1) - d[6];
1987    let a6 = d[2] + (d[6] >> 1);
1988    let b0 = a0 + a6;
1989    let b2 = a4 + a2;
1990    let b4 = a4 - a2;
1991    let b6 = a0 - a6;
1992    let a1 = -d[3] + d[5] - d[7] - (d[7] >> 1);
1993    let a3 = d[1] + d[7] - d[3] - (d[3] >> 1);
1994    let a5 = -d[1] + d[7] + d[5] + (d[5] >> 1);
1995    let a7 = d[3] + d[5] + d[1] + (d[1] >> 1);
1996    let b1 = a1 + (a7 >> 2);
1997    let b7 = a7 - (a1 >> 2);
1998    let b3 = a3 + (a5 >> 2);
1999    let b5 = (a3 >> 2) - a5;
2000    [
2001        b0 + b7,
2002        b2 + b5,
2003        b4 + b3,
2004        b6 + b1,
2005        b6 - b1,
2006        b4 - b3,
2007        b2 - b5,
2008        b0 - b7,
2009    ]
2010}
2011
2012/// 8×8 inverse transform (§8.5.13.2).
2013pub fn idct8(d: &[i32; 64]) -> [i32; 64] {
2014    let mut t = [0i32; 64];
2015    for i in 0..8 {
2016        let row = idct8_1d(d[i * 8..i * 8 + 8].try_into().unwrap());
2017        t[i * 8..i * 8 + 8].copy_from_slice(&row);
2018    }
2019    let mut out = [0i32; 64];
2020    for j in 0..8 {
2021        let col = idct8_1d(std::array::from_fn(|i| t[i * 8 + j]));
2022        for i in 0..8 {
2023            out[i * 8 + j] = (col[i] + 32) >> 6;
2024        }
2025    }
2026    out
2027}
2028
2029/// How the rows of a macroblock's sample view map onto picture rows: every
2030/// row, or (MBAFF field macroblock) every other row starting at `par`.
2031#[derive(Clone, Copy, Debug)]
2032pub struct Geo {
2033    pub step: usize,
2034    pub par: usize,
2035}
2036
2037impl Geo {
2038    #[inline(always)]
2039    pub fn row(self, y: usize) -> usize {
2040        y * self.step + self.par
2041    }
2042}
2043
2044/// Vertical motion scaling between the co-located picture's structure and
2045/// the current one (§8.4.1.2.3 `vertMvScale`).
2046#[derive(Clone, Copy, PartialEq, Eq)]
2047enum ColScale {
2048    Same,
2049    FrameToField,
2050    FieldToFrame,
2051}