Skip to main content

threers_codec/decode/hevc/
pic.rs

1//! Per-picture state: CTB and tile layout, and the 4×4-granular metadata
2//! that prediction, deblocking and SAO read.
3
4use std::sync::Arc;
5
6use super::params::{Pps, Sps};
7use crate::decode::picture::Plane;
8
9/// Where CTBs sit in tile scan, and the z-scan order of every 4×4 block
10/// (§6.5.1, §6.5.2). Depends on the SPS and PPS only.
11pub struct Layout {
12    pub ctb_w: usize,
13    pub ctb_h: usize,
14    pub log2_ctb: u32,
15    pub rs2ts: Vec<u32>,
16    pub ts2rs: Vec<u32>,
17    /// Tile index per CTB, by raster address.
18    pub tile: Vec<u16>,
19    /// Column boundaries of tiles, in CTBs.
20    pub col_bd: Vec<u32>,
21    /// Z-scan order per 4×4 block, raster over the picture in 4×4 units.
22    pub z: Vec<u32>,
23    pub w4: usize,
24    pub h4: usize,
25}
26
27impl Layout {
28    pub fn new(sps: &Sps, pps: &Pps) -> Layout {
29        let ctb_w = sps.width_ctbs() as usize;
30        let ctb_h = sps.height_ctbs() as usize;
31        let (col_bd, row_bd) = if pps.tiles {
32            (
33                pps.tile_cols.boundaries(ctb_w as u32),
34                pps.tile_rows.boundaries(ctb_h as u32),
35            )
36        } else {
37            (vec![0, ctb_w as u32], vec![0, ctb_h as u32])
38        };
39        let n = ctb_w * ctb_h;
40        let mut rs2ts = vec![0u32; n];
41        let mut tile = vec![0u16; n];
42        for rs in 0..n {
43            let tbx = (rs % ctb_w) as u32;
44            let tby = (rs / ctb_w) as u32;
45            let tx = (0..col_bd.len() - 1)
46                .rfind(|&i| tbx >= col_bd[i])
47                .unwrap_or(0);
48            let ty = (0..row_bd.len() - 1)
49                .rfind(|&j| tby >= row_bd[j])
50                .unwrap_or(0);
51            let mut v = 0u32;
52            for i in 0..tx {
53                v += (row_bd[ty + 1] - row_bd[ty]) * (col_bd[i + 1] - col_bd[i]);
54            }
55            for j in 0..ty {
56                v += ctb_w as u32 * (row_bd[j + 1] - row_bd[j]);
57            }
58            v += (tby - row_bd[ty]) * (col_bd[tx + 1] - col_bd[tx]) + tbx - col_bd[tx];
59            rs2ts[rs] = v;
60            tile[rs] = (ty * (col_bd.len() - 1) + tx) as u16;
61        }
62        let mut ts2rs = vec![0u32; n];
63        for (rs, &ts) in rs2ts.iter().enumerate() {
64            ts2rs[ts as usize] = rs as u32;
65        }
66        let w4 = (sps.width as usize).div_ceil(4);
67        let h4 = (sps.height as usize).div_ceil(4);
68        let shift = sps.log2_ctb - 2;
69        let mut z = vec![0u32; w4 * h4];
70        for y in 0..h4 {
71            for x in 0..w4 {
72                let rs = (y >> shift) * ctb_w + (x >> shift);
73                let mut v = rs2ts[rs] << (2 * shift);
74                for i in 0..shift {
75                    let m = 1usize << i;
76                    if x & m != 0 {
77                        v += (m * m) as u32;
78                    }
79                    if y & m != 0 {
80                        v += (2 * m * m) as u32;
81                    }
82                }
83                z[y * w4 + x] = v;
84            }
85        }
86        Layout {
87            ctb_w,
88            ctb_h,
89            log2_ctb: sps.log2_ctb,
90            rs2ts,
91            ts2rs,
92            tile,
93            col_bd,
94            z,
95            w4,
96            h4,
97        }
98    }
99
100    /// Whether the CTB at tile-scan address `ts` starts a tile.
101    pub fn tile_start(&self, ts: usize) -> bool {
102        ts == 0 || self.tile[self.ts2rs[ts] as usize] != self.tile[self.ts2rs[ts - 1] as usize]
103    }
104
105    /// The tile column start (in CTBs) for CTB column `x`.
106    pub fn tile_col_start(&self, x: u32) -> u32 {
107        *self.col_bd.iter().rfind(|&&b| b <= x).unwrap_or(&0)
108    }
109}
110
111/// Motion of one 4×4 block. `ref_idx < 0` means the list is unused; both
112/// unused means intra (or not yet decoded).
113#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
114pub struct MvField {
115    pub mv: [[i16; 2]; 2],
116    pub ref_idx: [i8; 2],
117}
118
119impl MvField {
120    pub const INTRA: MvField = MvField {
121        mv: [[0; 2]; 2],
122        ref_idx: [-1, -1],
123    };
124
125    #[inline]
126    pub fn pred(&self, l: usize) -> bool {
127        self.ref_idx[l] >= 0
128    }
129}
130
131/// Per-slice facts later stages need.
132#[derive(Clone, Debug)]
133pub struct SliceInfo {
134    pub addr_rs: u32,
135    pub deblocking_disabled: bool,
136    pub beta_offset: i32,
137    pub tc_offset: i32,
138    pub lf_across_slices: bool,
139    pub cb_qp_offset: i32,
140    pub cr_qp_offset: i32,
141    /// `(POC, long-term)` of each reference, per list — a colocated
142    /// picture's motion is read through these.
143    pub refs: [Vec<(i32, bool)>; 2],
144}
145
146/// SAO parameters of one CTB, one component.
147#[derive(Clone, Copy, Debug, Default)]
148pub struct Sao {
149    /// 0 off, 1 band, 2 edge.
150    pub kind: u8,
151    pub band_or_class: u8,
152    pub offsets: [i16; 4],
153}
154
155/// Flags per 4×4 block.
156pub mod flag {
157    pub const INTRA: u8 = 1;
158    pub const SKIP: u8 = 2;
159    /// PCM with `pcm_loop_filter_disabled`, or `cu_transquant_bypass`: loop
160    /// filters leave these samples alone.
161    pub const NO_FILTER: u8 = 4;
162    pub const PCM: u8 = 8;
163    /// Transform block with non-zero luma coefficients.
164    pub const CBF: u8 = 16;
165    /// Left / top edge of this block is a transform-block edge.
166    pub const EDGE_V: u8 = 32;
167    pub const EDGE_H: u8 = 64;
168}
169
170pub struct PicState {
171    pub planes: [Plane; 3],
172    pub width: usize,
173    pub height: usize,
174    pub layout: Arc<Layout>,
175    pub poc: i32,
176    pub flags: Vec<u8>,
177    /// Extra edge flags: bit 0 vertical PU edge, bit 1 horizontal PU edge.
178    pub pu_edge: Vec<u8>,
179    pub depth: Vec<u8>,
180    pub ipm: Vec<u8>,
181    pub qp: Vec<i8>,
182    pub mv: Vec<MvField>,
183    /// Slice index (into `slices`) per CTB, `u16::MAX` until decoded.
184    pub ctb_slice: Vec<u16>,
185    pub slices: Vec<SliceInfo>,
186    pub sao: Vec<[Sao; 3]>,
187    pub decoded_ctbs: usize,
188}
189
190impl PicState {
191    pub fn new(sps: &Sps, layout: Arc<Layout>) -> PicState {
192        let (w, h) = (sps.width as usize, sps.height as usize);
193        let n4 = layout.w4 * layout.h4;
194        let nctb = layout.ctb_w * layout.ctb_h;
195        let (hs, vs) = sps.chroma_shift();
196        let (cw, ch) = ((w + hs) >> hs, (h + vs) >> vs);
197        PicState {
198            planes: [
199                Plane::new(w, h, 1 << (sps.bit_depth_y - 1)),
200                Plane::new(cw, ch, 1 << (sps.bit_depth_c - 1)),
201                Plane::new(cw, ch, 1 << (sps.bit_depth_c - 1)),
202            ],
203            width: w,
204            height: h,
205            layout,
206            poc: 0,
207            flags: vec![0; n4],
208            pu_edge: vec![0; n4],
209            depth: vec![0; n4],
210            ipm: vec![1; n4],
211            qp: vec![0; n4],
212            mv: vec![MvField::INTRA; n4],
213            ctb_slice: vec![u16::MAX; nctb],
214            slices: Vec::new(),
215            sao: vec![[Sao::default(); 3]; nctb],
216            decoded_ctbs: 0,
217        }
218    }
219
220    /// A picture an accelerator reconstructs: the sizes, none of the
221    /// per-sample or per-block state the native path fills in.
222    pub fn sized(sps: &Sps, layout: Arc<Layout>) -> PicState {
223        let (w, h) = (sps.width as usize, sps.height as usize);
224        let (hs, vs) = sps.chroma_shift();
225        let (cw, ch) = ((w + hs) >> hs, (h + vs) >> vs);
226        let empty = |width, height| Plane {
227            data: Vec::new(),
228            width,
229            height,
230        };
231        PicState {
232            planes: [empty(w, h), empty(cw, ch), empty(cw, ch)],
233            width: w,
234            height: h,
235            layout,
236            poc: 0,
237            flags: Vec::new(),
238            pu_edge: Vec::new(),
239            depth: Vec::new(),
240            ipm: Vec::new(),
241            qp: Vec::new(),
242            mv: Vec::new(),
243            ctb_slice: Vec::new(),
244            slices: Vec::new(),
245            sao: Vec::new(),
246            decoded_ctbs: 0,
247        }
248    }
249
250    #[inline]
251    pub fn idx4(&self, x: usize, y: usize) -> usize {
252        (y >> 2) * self.layout.w4 + (x >> 2)
253    }
254
255    #[inline]
256    pub fn ctb_rs(&self, x: usize, y: usize) -> usize {
257        let s = self.layout.log2_ctb;
258        (y >> s) * self.layout.ctb_w + (x >> s)
259    }
260}