Skip to main content

threers_codec/decode/vp9/
mod.rs

1//! VP9 decoder: a port of ffmpeg 7.1's native `vp9` decoder (`vp9.c`,
2//! `vp9block.c`, `vp9recon.c`, `vp9mvs.c`, `vp9prob.c`, `vp9lpf.c`,
3//! `vp9dsp_template.c`), single-threaded path, every profile (8/10/12-bit;
4//! 4:2:0, 4:2:2, 4:4:0, 4:4:4, RGB), reference scaling, show-existing and
5//! intra-only frames, and superframes.
6//!
7//! VP9 decoding is integer and normative, and ffmpeg's decoder matches
8//! libvpx; porting ffmpeg's code keeps its libvpx-compatibility quirks
9//! (each marked where it appears). Planes are `u16` for every bit depth and
10//! allocated superblock-aligned; the frame is cropped on output. The whole
11//! frame is decoded before the loop filter runs superblock row by row, which
12//! is what ffmpeg's per-row filtering plus its `intra_pred_data` backup of
13//! unfiltered rows amounts to.
14
15pub mod block;
16pub mod data;
17pub mod dsp;
18pub mod lpf;
19pub mod mvs;
20pub mod probs;
21pub mod rac;
22pub mod recon;
23
24use std::sync::Arc;
25
26use self::probs::{Counts, ProbContext, SavedCtx};
27use self::rac::Rac;
28use crate::decode::bits::BitReader;
29use crate::decode::picture::Plane;
30pub use crate::decode::vp9_packet::{packet_shows, split_superframe};
31
32// IntraPredMode (ffmpeg order).
33/// Intra prediction mode: Vertical.
34pub const VERT_PRED: usize = 0;
35/// Intra prediction mode: Horizontal.
36pub const HOR_PRED: usize = 1;
37/// Intra prediction mode: DC.
38pub const DC_PRED: usize = 2;
39/// Intra prediction mode: Diagonal down-left.
40pub const DIAG_DOWN_LEFT_PRED: usize = 3;
41/// Intra prediction mode: Diagonal down-right.
42pub const DIAG_DOWN_RIGHT_PRED: usize = 4;
43/// Intra prediction mode: Vertical-right.
44pub const VERT_RIGHT_PRED: usize = 5;
45/// Intra prediction mode: Horizontal-down.
46pub const HOR_DOWN_PRED: usize = 6;
47/// Intra prediction mode: Vertical-left.
48pub const VERT_LEFT_PRED: usize = 7;
49/// Intra prediction mode: Horizontal-up.
50pub const HOR_UP_PRED: usize = 8;
51/// Intra prediction mode: True motion (VP8's TM).
52pub const TM_VP8_PRED: usize = 9;
53/// Intra prediction mode: DC from the left edge only.
54pub const LEFT_DC_PRED: usize = 10;
55/// Intra prediction mode: DC from the top edge only.
56pub const TOP_DC_PRED: usize = 11;
57/// Intra prediction mode: DC of 128 (no edges).
58pub const DC_128_PRED: usize = 12;
59/// Intra prediction mode: DC of 127 (no top edge).
60pub const DC_127_PRED: usize = 13;
61/// Intra prediction mode: DC of 129 (no left edge).
62pub const DC_129_PRED: usize = 14;
63// InterPredMode.
64/// Inter prediction mode: Nearest MV.
65pub const NEARESTMV: u8 = 10;
66/// Inter prediction mode: Near MV.
67pub const NEARMV: u8 = 11;
68/// Inter prediction mode: Zero MV.
69pub const ZEROMV: u8 = 12;
70/// Inter prediction mode: New MV.
71pub const NEWMV: u8 = 13;
72// TxfmMode.
73/// Transform mode: 4×4 only.
74pub const TX_4X4: u8 = 0;
75/// Transform mode: Up to 8×8.
76pub const TX_8X8: u8 = 1;
77/// Transform mode: Up to 16×16.
78pub const TX_16X16: u8 = 2;
79/// Transform mode: Up to 32×32.
80pub const TX_32X32: u8 = 3;
81/// Transform mode: Chosen per block.
82pub const TX_SWITCHABLE: u8 = 4;
83// TxfmType.
84/// Transform type: DCT both ways.
85pub const DCT_DCT: usize = 0;
86// BlockSize.
87/// Block size 64×64.
88pub const BS_64X64: u8 = 0;
89/// Block size 8×8.
90pub const BS_8X8: u8 = 9;
91/// Block size 8×4.
92pub const BS_8X4: u8 = 10;
93/// Block size 4×8.
94pub const BS_4X8: u8 = 11;
95/// Block size 4×4.
96pub const BS_4X4: u8 = 12;
97// BlockLevel.
98/// Partition level 64×64.
99pub const BL_64X64: u8 = 0;
100/// Partition level 8×8.
101pub const BL_8X8: u8 = 3;
102// BlockPartition.
103/// Partition: None.
104pub const PARTITION_NONE: u8 = 0;
105/// Partition: Horizontal.
106pub const PARTITION_H: u8 = 1;
107/// Partition: Vertical.
108pub const PARTITION_V: u8 = 2;
109/// Partition: Split into four.
110pub const PARTITION_SPLIT: u8 = 3;
111
112/// `FilterMode` (block filter values 0-3; 4 = switchable in the header).
113#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
114pub enum FilterMode {
115    /// 8-tap smooth.
116    Smooth = 0,
117    /// 8-tap regular.
118    #[default]
119    Regular = 1,
120    /// 8-tap sharp.
121    Sharp = 2,
122    /// Bilinear.
123    Bilinear = 3,
124    /// Chosen per block.
125    Switchable = 4,
126}
127
128impl FilterMode {
129    fn from_bits(v: u32) -> Self {
130        [
131            FilterMode::Smooth,
132            FilterMode::Regular,
133            FilterMode::Sharp,
134            FilterMode::Bilinear,
135        ][v as usize & 3]
136    }
137}
138
139/// `CompPredMode`.
140#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
141pub enum CompPred {
142    /// Single reference only.
143    #[default]
144    Single,
145    /// Compound prediction only.
146    Comp,
147    /// Chosen per block.
148    Switchable,
149}
150
151/// A motion vector in eighth samples.
152#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
153pub struct Mv {
154    /// Horizontal.
155    pub x: i16,
156    /// Vertical.
157    pub y: i16,
158}
159
160impl Mv {
161    #[inline]
162    fn packed(self) -> u32 {
163        (self.x as u16 as u32) | ((self.y as u16 as u32) << 16)
164    }
165}
166
167/// A block's motion as later frames read it (two lists).
168#[derive(Clone, Copy, Debug, Default)]
169pub struct MvRefPair {
170    /// Vector per list.
171    pub mv: [Mv; 2],
172    /// Reference per list, `-1` for none.
173    pub r#ref: [i8; 2],
174}
175
176/// One segment's features and what they resolve to.
177#[derive(Clone, Copy, Debug, Default)]
178pub struct SegFeat {
179    /// Alternate quantizer enabled.
180    pub q_enabled: bool,
181    /// Alternate loop filter level enabled.
182    pub lf_enabled: bool,
183    /// Fixed reference enabled.
184    pub ref_enabled: bool,
185    /// Skip enabled.
186    pub skip_enabled: bool,
187    /// The fixed reference.
188    pub ref_val: u8,
189    /// The quantizer value (delta or absolute).
190    pub q_val: i16,
191    /// The loop filter value (delta or absolute).
192    pub lf_val: i8,
193    /// Dequantizers: `[luma, chroma][dc, ac]`.
194    pub qmul: [[i16; 2]; 2],
195    /// Loop filter level per reference `[ref][mode]` (intra, last, golden, altref).
196    pub lflvl: [[u8; 2]; 4],
197}
198
199/// `segmentation_params()`.
200#[derive(Clone, Copy, Debug, Default)]
201pub struct Segmentation {
202    /// `segmentation_enabled`.
203    pub enabled: bool,
204    /// `segmentation_temporal_update`.
205    pub temporal: bool,
206    /// `segmentation_abs_or_delta_update`.
207    pub absolute_vals: bool,
208    /// `segmentation_update_map`.
209    pub update_map: bool,
210    /// `segmentation_tree_probs`.
211    pub prob: [u8; 7],
212    /// `segmentation_pred_prob`.
213    pub pred_prob: [u8; 3],
214    /// Per segment.
215    pub feat: [SegFeat; 8],
216}
217
218/// `VP9BitstreamHeader`. It persists across frames: fields a frame does not
219/// code keep the previous frame's values, as in ffmpeg.
220#[derive(Clone, Debug, Default)]
221pub struct Header {
222    /// `profile`.
223    pub profile: u8,
224    /// Bits per sample.
225    pub bpp: u8,
226    /// `frame_type == KEY_FRAME`.
227    pub keyframe: bool,
228    /// `!show_frame`.
229    pub invisible: bool,
230    /// `error_resilient_mode`.
231    pub errorres: bool,
232    /// `intra_only`.
233    pub intraonly: bool,
234    /// `reset_frame_context`.
235    pub resetctx: u8,
236    /// `refresh_frame_flags`.
237    pub refreshrefmask: u8,
238    /// `allow_high_precision_mv`.
239    pub highprecisionmvs: bool,
240    /// `interpolation_filter`.
241    pub filtermode: FilterMode,
242    /// Compound prediction is possible (sign biases differ).
243    pub allowcompinter: bool,
244    /// `refresh_frame_context`.
245    pub refreshctx: bool,
246    /// `frame_parallel_decoding_mode`.
247    pub parallelmode: bool,
248    /// `frame_context_idx`.
249    pub framectxid: u8,
250    /// The previous frame's MVs are usable.
251    pub use_last_frame_mvs: bool,
252    /// `ref_frame_idx` for last, golden, altref.
253    pub refidx: [u8; 3],
254    /// `ref_frame_sign_bias` for the same.
255    pub signbias: [bool; 3],
256    /// The fixed reference of compound prediction.
257    pub fixcompref: u8,
258    /// Its variable references.
259    pub varcompref: [u8; 2],
260    /// `loop_filter_level`.
261    pub filter_level: u8,
262    /// `loop_filter_sharpness`.
263    pub sharpness: i8,
264    /// `loop_filter_delta_enabled`.
265    pub lf_delta_enabled: bool,
266    /// `loop_filter_delta_update`.
267    pub lf_delta_updated: bool,
268    /// `loop_filter_mode_deltas`.
269    pub lf_delta_mode: [i8; 2],
270    /// `loop_filter_ref_deltas`.
271    pub lf_delta_ref: [i8; 4],
272    /// `base_q_idx`.
273    pub yac_qi: u8,
274    /// `delta_q_y_dc`.
275    pub ydc_qdelta: i8,
276    /// `delta_q_uv_dc`.
277    pub uvdc_qdelta: i8,
278    /// `delta_q_uv_ac`.
279    pub uvac_qdelta: i8,
280    /// Lossless: every quantizer 0.
281    pub lossless: bool,
282    /// `segmentation_params()`.
283    pub segmentation: Segmentation,
284    /// `tx_mode`.
285    pub txfmmode: u8,
286    /// `reference_select`.
287    pub comppredmode: CompPred,
288    /// `tile_cols_log2`.
289    pub log2_tile_cols: u32,
290    /// `tile_rows_log2`.
291    pub log2_tile_rows: u32,
292    /// Tile columns.
293    pub tile_cols: u32,
294    /// Tile rows.
295    pub tile_rows: u32,
296}
297
298/// A decoded frame (a reference, or the output).
299pub struct Frame {
300    /// Superblock-aligned planes; the visible picture is `width × height`.
301    pub planes: [Plane; 3],
302    /// Visible width.
303    pub width: u32,
304    /// Visible height.
305    pub height: u32,
306    /// Horizontal chroma subsampling (log2).
307    pub ss_h: u8,
308    /// Vertical chroma subsampling (log2).
309    pub ss_v: u8,
310    /// Bits per sample.
311    pub bpp: u8,
312    /// VP9 `color_space` (0 unknown, 1 BT.601, 2 BT.709, 3 SMPTE-170,
313    /// 4 SMPTE-240, 5 BT.2020, 6 reserved, 7 sRGB) and full range.
314    pub color_space: u8,
315    /// Full-range samples.
316    pub full_range: bool,
317    /// Segment id per 8×8 block, row stride `8 * sb_cols`.
318    pub segmap: Vec<u8>,
319    /// Motion per 8×8 block, as later frames read it.
320    pub mv: Vec<MvRefPair>,
321    /// Width in superblocks.
322    pub sb_cols: usize,
323}
324
325impl Frame {
326    /// As a [`DecodedFrame`](crate::decode::DecodedFrame): planes
327    /// cropped, VP9's `color_space` as the matrix number ffmpeg reports
328    /// (`AVColorSpace`; 0 for RGB, whose planes are G, B, R). 4:4:0 has no
329    /// [`ChromaFormat`](crate::decode::ChromaFormat) and is refused.
330    pub fn to_decoded(&self) -> Result<crate::decode::DecodedFrame, String> {
331        use crate::decode::{ChromaFormat, ColorInfo, DecodedFrame};
332        let chroma = match (self.ss_h, self.ss_v) {
333            (1, 1) => ChromaFormat::Yuv420,
334            (1, 0) => ChromaFormat::Yuv422,
335            (0, 0) => ChromaFormat::Yuv444,
336            _ => ChromaFormat::Yuv440,
337        };
338        const SPC: [u32; 8] = [2, 5, 1, 6, 7, 9, 2, 0];
339        let (y, _, _) = self.cropped(0);
340        let (u, _, _) = self.cropped(1);
341        let (v, _, _) = self.cropped(2);
342        Ok(DecodedFrame {
343            width: self.width,
344            height: self.height,
345            bit_depth: self.bpp,
346            chroma,
347            y,
348            u,
349            v,
350            color: ColorInfo::from_vui(
351                Some(SPC[self.color_space as usize & 7]),
352                self.full_range,
353                self.height,
354            ),
355        })
356    }
357
358    /// The visible luma/chroma planes, tightly packed.
359    pub fn cropped(&self, p: usize) -> (Vec<u16>, usize, usize) {
360        let (w, h) = if p == 0 {
361            (self.width as usize, self.height as usize)
362        } else {
363            (
364                (self.width as usize + self.ss_h as usize) >> self.ss_h,
365                (self.height as usize + self.ss_v as usize) >> self.ss_v,
366            )
367        };
368        let pl = &self.planes[p];
369        let mut out = Vec::with_capacity(w * h);
370        for y in 0..h {
371            out.extend_from_slice(&pl.data[y * pl.width..y * pl.width + w]);
372        }
373        (out, w, h)
374    }
375}
376
377/// A hardware back end for [`Vp9Decoder`]: the decoder parses the
378/// uncompressed header and keeps the eight reference slots; the accelerator
379/// parses the compressed header, adapts probabilities and reconstructs.
380pub trait Vp9Accel {
381    /// Decode one frame.
382    fn frame(&mut self, f: &AccelFrame<'_>) -> Result<(), String>;
383    /// The samples of a frame it decoded.
384    fn read(&mut self, frame: &Frame) -> Result<crate::decode::DecodedFrame, String>;
385}
386
387/// Everything a [`Vp9Accel`] is told about one frame.
388pub struct AccelFrame<'a> {
389    /// The frame header.
390    pub h: &'a Header,
391    /// The whole frame: uncompressed header, compressed header, tiles.
392    pub data: &'a [u8],
393    /// Bytes of the uncompressed header.
394    pub uncompressed_size: usize,
395    /// Bytes of the compressed header.
396    pub compressed_size: usize,
397    /// The frame being decoded (sizes and format; no samples).
398    pub cur: &'a Arc<Frame>,
399    /// The reference slots as the frame finds them.
400    pub refs: &'a [Option<Arc<Frame>>; 8],
401}
402
403/// The decoder.
404pub struct Vp9Decoder {
405    h: Header,
406    refs: [Option<Arc<Frame>>; 8],
407    /// The previous decoded frame (ffmpeg `CUR_FRAME` before allocation),
408    /// and the frames whose segmentation map / motion the next frame reads.
409    last: Option<Arc<Frame>>,
410    segmap_ref: Option<Arc<Frame>>,
411    prob_ctx: [SavedCtx; 4],
412    last_keyframe: bool,
413    lim_lut: [u8; 64],
414    mblim_lut: [u8; 64],
415    ss_h: u8,
416    ss_v: u8,
417    bpp_index: usize,
418    color_space: u8,
419    full_range: bool,
420    mvscale: [[u16; 2]; 3],
421    mvstep: [[u8; 2]; 3],
422    accel: Option<Box<dyn Vp9Accel>>,
423    /// Header sizes of the frame being decoded, for the accelerator.
424    header_sizes: (usize, usize),
425}
426
427impl Default for Vp9Decoder {
428    fn default() -> Self {
429        Self::new()
430    }
431}
432
433/// `inv_recenter_nonneg`.
434fn inv_recenter_nonneg(v: i32, m: i32) -> i32 {
435    if v > 2 * m {
436        v
437    } else if v & 1 != 0 {
438        m - ((v + 1) >> 1)
439    } else {
440        m + (v >> 1)
441    }
442}
443
444const INV_MAP_TABLE: [u8; 255] = [
445    7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176, 189, 202, 215, 228, 241, 254, 1,
446    2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28,
447    29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
448    55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 80,
449    81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100, 101, 102, 103, 104,
450    105, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 125,
451    126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
452    146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 164, 165, 166,
453    167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
454    187, 188, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206, 207,
455    208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
456    229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248,
457    249, 250, 251, 252, 253, 253,
458];
459
460/// `update_prob`: a differential forward probability update.
461fn update_prob(c: &mut Rac, p: u8) -> u8 {
462    let mut d = if !c.get() {
463        c.get_uint(4) as usize
464    } else if !c.get() {
465        c.get_uint(4) as usize + 16
466    } else if !c.get() {
467        c.get_uint(5) as usize + 32
468    } else {
469        let mut d = c.get_uint(7) as usize;
470        if d >= 65 {
471            d = (d << 1) - 65 + c.get() as usize;
472        }
473        d + 64
474    };
475    d = d.min(254);
476    let p = p as i32;
477    let v = if p <= 128 {
478        1 + inv_recenter_nonneg(INV_MAP_TABLE[d] as i32, p - 1)
479    } else {
480        255 - inv_recenter_nonneg(INV_MAP_TABLE[d] as i32, 255 - p)
481    };
482    v as u8
483}
484
485fn sbits_inv(r: &mut BitReader, n: u32) -> i32 {
486    let v = r.u(n) as i32;
487    if r.flag() {
488        -v
489    } else {
490        v
491    }
492}
493
494/// `av_clip_uintp2(v, n)`.
495fn clip_u(v: i32, n: u32) -> i32 {
496    v.clamp(0, (1 << n) - 1)
497}
498
499impl Vp9Decoder {
500    /// A decoder waiting for a keyframe.
501    pub fn new() -> Self {
502        Vp9Decoder {
503            h: Header {
504                sharpness: -1,
505                ..Default::default()
506            },
507            refs: Default::default(),
508            last: None,
509            segmap_ref: None,
510            prob_ctx: [
511                SavedCtx::default_ctx(),
512                SavedCtx::default_ctx(),
513                SavedCtx::default_ctx(),
514                SavedCtx::default_ctx(),
515            ],
516            last_keyframe: false,
517            lim_lut: [0; 64],
518            mblim_lut: [0; 64],
519            ss_h: 1,
520            ss_v: 1,
521            bpp_index: 0,
522            color_space: 0,
523            full_range: false,
524            mvscale: [[0; 2]; 3],
525            mvstep: [[0; 2]; 3],
526            accel: None,
527            header_sizes: (0, 0),
528        }
529    }
530
531    /// Decode one packet (a frame or superframe); returns the frames to show.
532    /// Hand reconstruction to `accel` (see [`Vp9Accel`]).
533    pub fn set_accel(&mut self, accel: Box<dyn Vp9Accel>) {
534        self.accel = Some(accel);
535    }
536
537    /// The samples of `frame`, from the accelerator that decoded it.
538    pub fn accel_read(&mut self, frame: &Frame) -> Result<crate::decode::DecodedFrame, String> {
539        self.accel.as_mut().ok_or("no accelerator")?.read(frame)
540    }
541
542    /// Decode one packet (a frame or a superframe) and return the frames it shows.
543    pub fn decode(&mut self, packet: &[u8]) -> Result<Vec<Arc<Frame>>, String> {
544        let mut out = Vec::new();
545        for f in split_superframe(packet) {
546            if let Some(frame) = self.decode_frame(f)? {
547                out.push(frame);
548            }
549        }
550        Ok(out)
551    }
552
553    fn read_colorspace_details(&mut self, r: &mut BitReader) -> Result<(), String> {
554        let bits = if self.h.profile <= 1 {
555            0
556        } else {
557            1 + r.flag() as usize
558        };
559        self.bpp_index = bits;
560        self.h.bpp = 8 + bits as u8 * 2;
561        self.color_space = r.u(3) as u8;
562        if self.color_space == 7 {
563            // RGB (profile 1/3 only).
564            self.ss_h = 0;
565            self.ss_v = 0;
566            self.full_range = true;
567            if self.h.profile & 1 != 0 {
568                if r.flag() {
569                    return Err("VP9: reserved bit set in RGB".into());
570                }
571            } else {
572                return Err(format!(
573                    "VP9: RGB not supported in profile {}",
574                    self.h.profile
575                ));
576            }
577        } else {
578            self.full_range = r.flag();
579            if self.h.profile & 1 != 0 {
580                self.ss_h = r.flag() as u8;
581                self.ss_v = r.flag() as u8;
582                if self.ss_h == 1 && self.ss_v == 1 {
583                    return Err(format!(
584                        "VP9: 4:2:0 not supported in profile {}",
585                        self.h.profile
586                    ));
587                } else if r.flag() {
588                    return Err("VP9: color details reserved bit set".into());
589                }
590            } else {
591                self.ss_h = 1;
592                self.ss_v = 1;
593            }
594        }
595        Ok(())
596    }
597
598    /// `decode_frame_header`: `Ok(None)` after the compressed header (then
599    /// `(offset of tile data)`), `Ok(Some(ref))` for show-existing.
600    fn decode_frame_header(
601        &mut self,
602        data: &[u8],
603        probs: &mut ProbContext,
604        coef: &mut CoefProbs,
605        w_out: &mut (u32, u32),
606    ) -> Result<Result<usize, u8>, String> {
607        let mut r = BitReader::new(data);
608        if r.u(2) != 2 {
609            return Err("VP9: invalid frame marker".into());
610        }
611        let mut profile = r.u(1) as u8;
612        profile |= (r.u(1) as u8) << 1;
613        if profile == 3 {
614            profile += r.u(1) as u8;
615        }
616        if profile > 3 {
617            return Err(format!("VP9: profile {profile} is not supported"));
618        }
619        self.h.profile = profile;
620        if r.flag() {
621            return Ok(Err(r.u(3) as u8));
622        }
623        self.last_keyframe = self.h.keyframe;
624        self.h.keyframe = !r.flag();
625        let last_invisible = self.h.invisible;
626        self.h.invisible = !r.flag();
627        self.h.errorres = r.flag();
628        self.h.use_last_frame_mvs = !self.h.errorres && !last_invisible;
629        let (w, h);
630        if self.h.keyframe {
631            if r.u(24) != 0x49_8342 {
632                return Err("VP9: invalid sync code".into());
633            }
634            self.read_colorspace_details(&mut r)?;
635            self.h.refreshrefmask = 0xff;
636            w = r.u(16) + 1;
637            h = r.u(16) + 1;
638            if r.flag() {
639                r.skip(32);
640            }
641        } else {
642            self.h.intraonly = if self.h.invisible { r.flag() } else { false };
643            self.h.resetctx = if self.h.errorres { 0 } else { r.u(2) as u8 };
644            if self.h.intraonly {
645                if r.u(24) != 0x49_8342 {
646                    return Err("VP9: invalid sync code".into());
647                }
648                if profile >= 1 {
649                    self.read_colorspace_details(&mut r)?;
650                } else {
651                    self.ss_h = 1;
652                    self.ss_v = 1;
653                    self.h.bpp = 8;
654                    self.bpp_index = 0;
655                    self.color_space = 1;
656                    self.full_range = false;
657                }
658                self.h.refreshrefmask = r.u(8) as u8;
659                w = r.u(16) + 1;
660                h = r.u(16) + 1;
661                if r.flag() {
662                    r.skip(32);
663                }
664            } else {
665                self.h.refreshrefmask = r.u(8) as u8;
666                for i in 0..3 {
667                    self.h.refidx[i] = r.u(3) as u8;
668                    self.h.signbias[i] = r.flag() && !self.h.errorres;
669                }
670                if self
671                    .h
672                    .refidx
673                    .iter()
674                    .any(|&i| self.refs[i as usize].is_none())
675                {
676                    return Err("VP9: not all references are available".into());
677                }
678                let rs = |i: usize| {
679                    let f = self.refs[self.h.refidx[i] as usize].as_ref().unwrap();
680                    (f.width, f.height)
681                };
682                if r.flag() {
683                    (w, h) = rs(0);
684                } else if r.flag() {
685                    (w, h) = rs(1);
686                } else if r.flag() {
687                    (w, h) = rs(2);
688                } else {
689                    w = r.u(16) + 1;
690                    h = r.u(16) + 1;
691                }
692                self.h.use_last_frame_mvs &= self
693                    .last
694                    .as_ref()
695                    .is_some_and(|f| f.width == w && f.height == h);
696                if r.flag() {
697                    r.skip(32);
698                }
699                self.h.highprecisionmvs = r.flag();
700                self.h.filtermode = if r.flag() {
701                    FilterMode::Switchable
702                } else {
703                    FilterMode::from_bits(r.u(2))
704                };
705                let sb = self.h.signbias;
706                self.h.allowcompinter = sb[0] != sb[1] || sb[0] != sb[2];
707                if self.h.allowcompinter {
708                    if sb[0] == sb[1] {
709                        self.h.fixcompref = 2;
710                        self.h.varcompref = [0, 1];
711                    } else if sb[0] == sb[2] {
712                        self.h.fixcompref = 1;
713                        self.h.varcompref = [0, 2];
714                    } else {
715                        self.h.fixcompref = 0;
716                        self.h.varcompref = [1, 2];
717                    }
718                }
719            }
720        }
721        self.h.refreshctx = if self.h.errorres { false } else { r.flag() };
722        self.h.parallelmode = if self.h.errorres { true } else { r.flag() };
723        let c = r.u(2) as usize;
724        self.h.framectxid = c as u8;
725        if self.h.keyframe || self.h.intraonly {
726            // BUG: libvpx ignores this field in keyframes.
727            self.h.framectxid = 0;
728        }
729        if self.h.keyframe || self.h.errorres || self.h.intraonly {
730            self.h.lf_delta_ref = [1, 0, -1, -1];
731            self.h.lf_delta_mode = [0, 0];
732            self.h.segmentation.feat = [SegFeat::default(); 8];
733        }
734        self.h.filter_level = r.u(6) as u8;
735        let sharp = r.u(3) as i8;
736        if self.h.sharpness != sharp {
737            for i in 1..=63 {
738                let mut limit = i as i32;
739                if sharp > 0 {
740                    limit >>= (sharp as i32 + 3) >> 2;
741                    limit = limit.min(9 - sharp as i32);
742                }
743                limit = limit.max(1);
744                self.lim_lut[i] = limit as u8;
745                self.mblim_lut[i] = (2 * (i as i32 + 2) + limit) as u8;
746            }
747        }
748        self.h.sharpness = sharp;
749        self.h.lf_delta_enabled = r.flag();
750        if self.h.lf_delta_enabled {
751            self.h.lf_delta_updated = r.flag();
752            if self.h.lf_delta_updated {
753                for i in 0..4 {
754                    if r.flag() {
755                        self.h.lf_delta_ref[i] = sbits_inv(&mut r, 6) as i8;
756                    }
757                }
758                for i in 0..2 {
759                    if r.flag() {
760                        self.h.lf_delta_mode[i] = sbits_inv(&mut r, 6) as i8;
761                    }
762                }
763            }
764        }
765        self.h.yac_qi = r.u(8) as u8;
766        self.h.ydc_qdelta = if r.flag() {
767            sbits_inv(&mut r, 4) as i8
768        } else {
769            0
770        };
771        self.h.uvdc_qdelta = if r.flag() {
772            sbits_inv(&mut r, 4) as i8
773        } else {
774            0
775        };
776        self.h.uvac_qdelta = if r.flag() {
777            sbits_inv(&mut r, 4) as i8
778        } else {
779            0
780        };
781        self.h.lossless = self.h.yac_qi == 0
782            && self.h.ydc_qdelta == 0
783            && self.h.uvdc_qdelta == 0
784            && self.h.uvac_qdelta == 0;
785        let seg = &mut self.h.segmentation;
786        seg.enabled = r.flag();
787        if seg.enabled {
788            seg.update_map = r.flag();
789            if seg.update_map {
790                for i in 0..7 {
791                    seg.prob[i] = if r.flag() { r.u(8) as u8 } else { 255 };
792                }
793                seg.temporal = r.flag();
794                if seg.temporal {
795                    for i in 0..3 {
796                        seg.pred_prob[i] = if r.flag() { r.u(8) as u8 } else { 255 };
797                    }
798                }
799            }
800            if r.flag() {
801                seg.absolute_vals = r.flag();
802                for i in 0..8 {
803                    let f = &mut seg.feat[i];
804                    f.q_enabled = r.flag();
805                    if f.q_enabled {
806                        f.q_val = sbits_inv(&mut r, 8) as i16;
807                    }
808                    f.lf_enabled = r.flag();
809                    if f.lf_enabled {
810                        f.lf_val = sbits_inv(&mut r, 6) as i8;
811                    }
812                    f.ref_enabled = r.flag();
813                    if f.ref_enabled {
814                        f.ref_val = r.u(2) as u8;
815                    }
816                    f.skip_enabled = r.flag();
817                }
818            }
819        } else {
820            seg.temporal = false;
821            seg.update_map = false;
822        }
823        // qmul / lflvl per segment.
824        let nseg = if self.h.segmentation.enabled { 8 } else { 1 };
825        for i in 0..nseg {
826            let h = &mut self.h;
827            let f = h.segmentation.feat[i];
828            let mut qyac = if h.segmentation.enabled && f.q_enabled {
829                if h.segmentation.absolute_vals {
830                    clip_u(f.q_val as i32, 8)
831                } else {
832                    clip_u(h.yac_qi as i32 + f.q_val as i32, 8)
833                }
834            } else {
835                h.yac_qi as i32
836            };
837            let qydc = clip_u(qyac + h.ydc_qdelta as i32, 8) as usize;
838            let quvdc = clip_u(qyac + h.uvdc_qdelta as i32, 8) as usize;
839            let quvac = clip_u(qyac + h.uvac_qdelta as i32, 8) as usize;
840            qyac = clip_u(qyac, 8);
841            let b = self.bpp_index;
842            let fm = &mut h.segmentation.feat[i];
843            fm.qmul[0][0] = data::DC_QLOOKUP[b][qydc];
844            fm.qmul[0][1] = data::AC_QLOOKUP[b][qyac as usize];
845            fm.qmul[1][0] = data::DC_QLOOKUP[b][quvdc];
846            fm.qmul[1][1] = data::AC_QLOOKUP[b][quvac];
847            let sh = (h.filter_level >= 32) as i32;
848            let lflvl = if h.segmentation.enabled && f.lf_enabled {
849                if h.segmentation.absolute_vals {
850                    clip_u(f.lf_val as i32, 6)
851                } else {
852                    clip_u(h.filter_level as i32 + f.lf_val as i32, 6)
853                }
854            } else {
855                h.filter_level as i32
856            };
857            let fm = &mut h.segmentation.feat[i];
858            if h.lf_delta_enabled {
859                let v = clip_u(lflvl + (h.lf_delta_ref[0] as i32) * (1 << sh), 6) as u8;
860                fm.lflvl[0] = [v, v];
861                for j in 1..4 {
862                    fm.lflvl[j][0] = clip_u(
863                        lflvl
864                            + ((h.lf_delta_ref[j] as i32 + h.lf_delta_mode[0] as i32) * (1 << sh)),
865                        6,
866                    ) as u8;
867                    fm.lflvl[j][1] = clip_u(
868                        lflvl
869                            + ((h.lf_delta_ref[j] as i32 + h.lf_delta_mode[1] as i32) * (1 << sh)),
870                        6,
871                    ) as u8;
872                }
873            } else {
874                fm.lflvl = [[lflvl as u8; 2]; 4];
875            }
876        }
877        // Tiling.
878        let sb_cols = (w + 63) >> 6;
879        self.h.log2_tile_cols = 0;
880        while sb_cols > (64 << self.h.log2_tile_cols) {
881            self.h.log2_tile_cols += 1;
882        }
883        let mut max = 0;
884        while (sb_cols >> max) >= 4 {
885            max += 1;
886        }
887        let max = (max - 1).max(0) as u32;
888        while max > self.h.log2_tile_cols {
889            if r.flag() {
890                self.h.log2_tile_cols += 1;
891            } else {
892                break;
893            }
894        }
895        // decode012
896        self.h.log2_tile_rows = if !r.flag() { 0 } else { 1 + r.flag() as u32 };
897        self.h.tile_rows = 1 << self.h.log2_tile_rows;
898        self.h.tile_cols = 1 << self.h.log2_tile_cols;
899        // Reference scaling.
900        if !self.h.keyframe && !self.h.intraonly {
901            let mut valid = 0;
902            for i in 0..3 {
903                let rf = self.refs[self.h.refidx[i] as usize].as_ref().unwrap();
904                let (refw, refh) = (rf.width, rf.height);
905                if rf.bpp != self.h.bpp || rf.ss_h != self.ss_h || rf.ss_v != self.ss_v {
906                    return Err("VP9: reference pixel format differs from the frame's".into());
907                } else if refw == w && refh == h {
908                    self.mvscale[i] = [0, 0];
909                } else {
910                    if w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh {
911                        self.mvscale[i] = [0xFFFF, 0xFFFF];
912                        continue;
913                    }
914                    self.mvscale[i] = [((refw << 14) / w) as u16, ((refh << 14) / h) as u16];
915                    self.mvstep[i] = [
916                        ((16 * self.mvscale[i][0] as u32) >> 14) as u8,
917                        ((16 * self.mvscale[i][1] as u32) >> 14) as u8,
918                    ];
919                }
920                valid += 1;
921            }
922            if valid == 0 {
923                return Err("VP9: no valid reference frame".into());
924            }
925        }
926        if self.h.keyframe || self.h.errorres || (self.h.intraonly && self.h.resetctx == 3) {
927            for p in self.prob_ctx.iter_mut() {
928                *p = SavedCtx::default_ctx();
929            }
930        } else if self.h.intraonly && self.h.resetctx == 2 {
931            self.prob_ctx[c] = SavedCtx::default_ctx();
932        }
933        let size2 = r.u(16) as usize;
934        r.byte_align();
935        let off = r.pos() / 8;
936        if size2 > data.len().saturating_sub(off) {
937            return Err("VP9: invalid compressed header size".into());
938        }
939        super::picture::check_picture_size(w, h)?;
940        *w_out = (w, h);
941        if self.accel.is_some() {
942            // The compressed header is the accelerator's to parse.
943            self.header_sizes = (off, size2);
944            return Ok(Ok(off + size2));
945        }
946        let compressed = off
947            .checked_add(size2)
948            .and_then(|end| data.get(off..end))
949            .ok_or("VP9: compressed header overruns the frame")?;
950        let mut rc = Rac::new(compressed)?;
951        if rc.get_prob(128) {
952            return Err("VP9: marker bit set".into());
953        }
954        let ctx = self.prob_ctx[c].clone();
955        *probs = ctx.p;
956        // Transform mode.
957        if self.h.lossless {
958            self.h.txfmmode = TX_4X4;
959        } else {
960            self.h.txfmmode = rc.get_uint(2) as u8;
961            if self.h.txfmmode == 3 {
962                self.h.txfmmode += rc.get() as u8;
963            }
964            if self.h.txfmmode == TX_SWITCHABLE {
965                for i in 0..2 {
966                    if rc.get_prob(252) {
967                        probs.tx8p[i] = update_prob(&mut rc, probs.tx8p[i]);
968                    }
969                }
970                for i in 0..2 {
971                    for j in 0..2 {
972                        if rc.get_prob(252) {
973                            probs.tx16p[i][j] = update_prob(&mut rc, probs.tx16p[i][j]);
974                        }
975                    }
976                }
977                for i in 0..2 {
978                    for j in 0..3 {
979                        if rc.get_prob(252) {
980                            probs.tx32p[i][j] = update_prob(&mut rc, probs.tx32p[i][j]);
981                        }
982                    }
983                }
984            }
985        }
986        // Coefficients.
987        for i in 0..4 {
988            let rf = &ctx.coef[i];
989            if rc.get() {
990                for j in 0..2 {
991                    for k in 0..2 {
992                        for l in 0..6 {
993                            for m in 0..6 {
994                                if m >= 3 && l == 0 {
995                                    break;
996                                }
997                                let p = &mut coef[i][j][k][l][m];
998                                let rr = &rf[j][k][l][m];
999                                for n in 0..3 {
1000                                    p[n] = if rc.get_prob(252) {
1001                                        update_prob(&mut rc, rr[n])
1002                                    } else {
1003                                        rr[n]
1004                                    };
1005                                }
1006                                let p2 = p[2] as usize;
1007                                p[3..11].copy_from_slice(&data::MODEL_PARETO8[p2]);
1008                            }
1009                        }
1010                    }
1011                }
1012            } else {
1013                for j in 0..2 {
1014                    for k in 0..2 {
1015                        for l in 0..6 {
1016                            for m in 0..6 {
1017                                // (ffmpeg: `m > 3`, so the DC band copies a
1018                                // fourth, unused context.)
1019                                if m > 3 && l == 0 {
1020                                    break;
1021                                }
1022                                let p = &mut coef[i][j][k][l][m];
1023                                p[..3].copy_from_slice(&rf[j][k][l][m]);
1024                                let p2 = p[2] as usize;
1025                                p[3..11].copy_from_slice(&data::MODEL_PARETO8[p2]);
1026                            }
1027                        }
1028                    }
1029                }
1030            }
1031            if self.h.txfmmode as usize == i {
1032                break;
1033            }
1034        }
1035        // Modes.
1036        for i in 0..3 {
1037            if rc.get_prob(252) {
1038                probs.skip[i] = update_prob(&mut rc, probs.skip[i]);
1039            }
1040        }
1041        if !self.h.keyframe && !self.h.intraonly {
1042            for i in 0..7 {
1043                for j in 0..3 {
1044                    if rc.get_prob(252) {
1045                        probs.mv_mode[i][j] = update_prob(&mut rc, probs.mv_mode[i][j]);
1046                    }
1047                }
1048            }
1049            if self.h.filtermode == FilterMode::Switchable {
1050                for i in 0..4 {
1051                    for j in 0..2 {
1052                        if rc.get_prob(252) {
1053                            probs.filter[i][j] = update_prob(&mut rc, probs.filter[i][j]);
1054                        }
1055                    }
1056                }
1057            }
1058            for i in 0..4 {
1059                if rc.get_prob(252) {
1060                    probs.intra[i] = update_prob(&mut rc, probs.intra[i]);
1061                }
1062            }
1063            if self.h.allowcompinter {
1064                let mut m = rc.get() as u8;
1065                if m != 0 {
1066                    m += rc.get() as u8;
1067                }
1068                self.h.comppredmode =
1069                    [CompPred::Single, CompPred::Comp, CompPred::Switchable][m as usize];
1070                if self.h.comppredmode == CompPred::Switchable {
1071                    for i in 0..5 {
1072                        if rc.get_prob(252) {
1073                            probs.comp[i] = update_prob(&mut rc, probs.comp[i]);
1074                        }
1075                    }
1076                }
1077            } else {
1078                self.h.comppredmode = CompPred::Single;
1079            }
1080            if self.h.comppredmode != CompPred::Comp {
1081                for i in 0..5 {
1082                    for j in 0..2 {
1083                        if rc.get_prob(252) {
1084                            probs.single_ref[i][j] = update_prob(&mut rc, probs.single_ref[i][j]);
1085                        }
1086                    }
1087                }
1088            }
1089            if self.h.comppredmode != CompPred::Single {
1090                for i in 0..5 {
1091                    if rc.get_prob(252) {
1092                        probs.comp_ref[i] = update_prob(&mut rc, probs.comp_ref[i]);
1093                    }
1094                }
1095            }
1096            for i in 0..4 {
1097                for j in 0..9 {
1098                    if rc.get_prob(252) {
1099                        probs.y_mode[i][j] = update_prob(&mut rc, probs.y_mode[i][j]);
1100                    }
1101                }
1102            }
1103            for i in 0..4 {
1104                for j in 0..4 {
1105                    for k in 0..3 {
1106                        if rc.get_prob(252) {
1107                            probs.partition[3 - i][j][k] =
1108                                update_prob(&mut rc, probs.partition[3 - i][j][k]);
1109                        }
1110                    }
1111                }
1112            }
1113            // mv fields don't use the update_prob subexp model.
1114            let mvp = |rc: &mut Rac, p: &mut u8| {
1115                if rc.get_prob(252) {
1116                    *p = ((rc.get_uint(7) << 1) | 1) as u8;
1117                }
1118            };
1119            for i in 0..3 {
1120                mvp(&mut rc, &mut probs.mv_joint[i]);
1121            }
1122            for i in 0..2 {
1123                let m = &mut probs.mv_comp[i];
1124                mvp(&mut rc, &mut m.sign);
1125                for j in 0..10 {
1126                    mvp(&mut rc, &mut m.classes[j]);
1127                }
1128                mvp(&mut rc, &mut m.class0);
1129                for j in 0..10 {
1130                    mvp(&mut rc, &mut m.bits[j]);
1131                }
1132            }
1133            for i in 0..2 {
1134                let m = &mut probs.mv_comp[i];
1135                for j in 0..2 {
1136                    for k in 0..3 {
1137                        mvp(&mut rc, &mut m.class0_fp[j][k]);
1138                    }
1139                }
1140                for j in 0..3 {
1141                    mvp(&mut rc, &mut m.fp[j]);
1142                }
1143            }
1144            if self.h.highprecisionmvs {
1145                for i in 0..2 {
1146                    mvp(&mut rc, &mut probs.mv_comp[i].class0_hp);
1147                    mvp(&mut rc, &mut probs.mv_comp[i].hp);
1148                }
1149            }
1150        }
1151        Ok(Ok(off + size2))
1152    }
1153
1154    fn decode_frame(&mut self, data: &[u8]) -> Result<Option<Arc<Frame>>, String> {
1155        let retain_segmap_ref = self.segmap_ref.is_some()
1156            && (!self.h.segmentation.enabled || !self.h.segmentation.update_map);
1157        let mut probs = data::DEFAULT_PROBS;
1158        let mut coef: Box<CoefProbs> = Box::new([[[[[[0u8; 11]; 6]; 6]; 2]; 2]; 4]);
1159        let mut wh = (0, 0);
1160        let off = match self.decode_frame_header(data, &mut probs, &mut coef, &mut wh)? {
1161            Err(r) => {
1162                let f = self.refs[r as usize]
1163                    .clone()
1164                    .ok_or_else(|| format!("VP9: requested reference {r} not available"))?;
1165                return Ok(Some(f));
1166            }
1167            Ok(off) => off,
1168        };
1169        let (w, h) = wh;
1170        let h_ = self.h.clone();
1171        // Reference frames for segmentation-map prediction and last-frame MVs.
1172        let src = if !h_.keyframe && !h_.intraonly && !h_.errorres {
1173            self.last.clone()
1174        } else {
1175            None
1176        };
1177        if !retain_segmap_ref || h_.keyframe || h_.intraonly {
1178            self.segmap_ref = src.clone();
1179        }
1180        let mvpair = src;
1181        let sb_cols = ((w + 63) >> 6) as usize;
1182        let sb_rows = ((h + 63) >> 6) as usize;
1183        if mvpair
1184            .as_ref()
1185            .is_none_or(|f| f.width != w || f.height != h)
1186        {
1187            self.segmap_ref = None;
1188        }
1189        let (ss_h, ss_v) = (self.ss_h, self.ss_v);
1190        let lw = sb_cols * 64;
1191        let lh = sb_rows * 64;
1192        if let Some(accel) = self.accel.as_mut() {
1193            let sized = |w: usize, h: usize| Plane {
1194                data: Vec::new(),
1195                width: w,
1196                height: h,
1197            };
1198            let cur = Arc::new(Frame {
1199                planes: [
1200                    sized(lw, lh),
1201                    sized(lw >> ss_h, lh >> ss_v),
1202                    sized(lw >> ss_h, lh >> ss_v),
1203                ],
1204                width: w,
1205                height: h,
1206                ss_h,
1207                ss_v,
1208                bpp: h_.bpp,
1209                color_space: self.color_space,
1210                full_range: self.full_range,
1211                segmap: Vec::new(),
1212                mv: Vec::new(),
1213                sb_cols,
1214            });
1215            let (uncompressed_size, compressed_size) = self.header_sizes;
1216            accel.frame(&AccelFrame {
1217                h: &h_,
1218                data,
1219                uncompressed_size,
1220                compressed_size,
1221                cur: &cur,
1222                refs: &self.refs,
1223            })?;
1224            for i in 0..8 {
1225                if h_.refreshrefmask & (1 << i) != 0 {
1226                    self.refs[i] = Some(cur.clone());
1227                }
1228            }
1229            self.last = Some(cur.clone());
1230            return Ok(if h_.invisible { None } else { Some(cur) });
1231        }
1232        let fill = 0u16;
1233        let mut cur = Frame {
1234            planes: [
1235                Plane::new(lw, lh, fill),
1236                Plane::new(lw >> ss_h, lh >> ss_v, fill),
1237                Plane::new(lw >> ss_h, lh >> ss_v, fill),
1238            ],
1239            width: w,
1240            height: h,
1241            ss_h,
1242            ss_v,
1243            bpp: h_.bpp,
1244            color_space: self.color_space,
1245            full_range: self.full_range,
1246            segmap: vec![0; 64 * sb_cols * sb_rows],
1247            mv: vec![MvRefPair::default(); 64 * sb_cols * sb_rows],
1248            sb_cols,
1249        };
1250        let refs3: [Option<Arc<Frame>>; 3] = if h_.keyframe || h_.intraonly {
1251            [None, None, None]
1252        } else {
1253            [0, 1, 2].map(|i| self.refs[h_.refidx[i] as usize].clone())
1254        };
1255        let mut counts = Counts::new();
1256        if h_.refreshctx && h_.parallelmode {
1257            let ctx = &mut self.prob_ctx[h_.framectxid as usize];
1258            for i in 0..4 {
1259                for j in 0..2 {
1260                    for k in 0..2 {
1261                        for l in 0..6 {
1262                            for m in 0..6 {
1263                                ctx.coef[i][j][k][l][m].copy_from_slice(&coef[i][j][k][l][m][..3]);
1264                            }
1265                        }
1266                    }
1267                }
1268                if h_.txfmmode as usize == i {
1269                    break;
1270                }
1271            }
1272            ctx.p = probs;
1273        }
1274        {
1275            let mut fd = block::FrameDec::new(
1276                &h_,
1277                &probs,
1278                &coef,
1279                &mut counts,
1280                &mut cur,
1281                &refs3,
1282                self.segmap_ref.as_deref(),
1283                mvpair.as_deref(),
1284                self.mvscale,
1285                self.mvstep,
1286            );
1287            fd.decode_tiles(&data[off..])?;
1288            if h_.filter_level != 0 {
1289                lpf::loop_filter_frame(&mut fd, &self.lim_lut, &self.mblim_lut);
1290            }
1291        }
1292        if h_.refreshctx && !h_.parallelmode {
1293            let fc = h_.framectxid as usize;
1294            probs::adapt_probs(
1295                &mut self.prob_ctx[fc],
1296                &probs,
1297                &counts,
1298                &h_,
1299                self.last_keyframe,
1300            );
1301        }
1302        let cur = Arc::new(cur);
1303        for i in 0..8 {
1304            if h_.refreshrefmask & (1 << i) != 0 {
1305                self.refs[i] = Some(cur.clone());
1306            }
1307        }
1308        self.last = Some(cur.clone());
1309        Ok(if h_.invisible { None } else { Some(cur) })
1310    }
1311}
1312
1313/// Coefficient probabilities: `[tx][plane][inter][band][ctx]`, 3 coded and
1314/// 8 Pareto-model entries.
1315pub type CoefProbs = [[[[[[u8; 11]; 6]; 6]; 2]; 2]; 4];