edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Partition coding and intra-frame mode info (spec 6.3.4/6.3.5, |
| 2 | //! libvpx `vp9_decodeframe.c decode_partition` + `vp9_decodemv.c |
| 3 | //! read_intra_frame_mode_info`). |
| 4 | |
| 5 | use crate::bool::BoolDecoder; |
| 6 | use crate::header::FrameContext; |
| 7 | use crate::tables::*; |
| 8 | |
| 9 | /// One block's parsed prediction record (`MODE_INFO`). |
| 10 | #[derive(Clone, Default)] |
| 11 | pub(crate) struct MiInfo { |
| 12 | pub sb_type: usize, |
| 13 | pub segment: u8, |
| 14 | pub skip: bool, |
| 15 | pub tx_size: usize, |
| 16 | pub mode: u8, |
| 17 | pub uv_mode: u8, |
| 18 | /// Per-4x4 sub-block y modes, valid when `sb_type < 3` (BLOCK_8X8). |
| 19 | pub bmi: [u8; 4], |
| 20 | /// `is_inter_block(mi)`: `ref_frame[0] > INTRA_FRAME`. |
| 21 | pub is_inter: bool, |
| 22 | /// `mi->ref_frame[0]`/`[1]`: `INTRA_FRAME` (0), `LAST`/`GOLDEN`/`ALTREF` |
| 23 | /// (1..3), `NO_REF_FRAME` (-1). |
| 24 | pub ref_frame: [i8; 2], |
| 25 | /// `mi->mv[..]`: the refreshed MV (predictor + codeword diff). |
| 26 | pub mv: [(i32, i32); 2], |
| 27 | /// The raw MV codeword diff of each reference frame. |
| 28 | pub mv_diff: [(i32, i32); 2], |
| 29 | /// `mi->bmi[j].as_mv`. |
| 30 | pub bmi_mv: [[(i32, i32); 2]; 4], |
| 31 | /// `mi->seg_id_predicted`. |
| 32 | pub seg_id_predicted: bool, |
| 33 | /// `mi->interp_filter`. |
| 34 | pub interp_filter: u8, |
| 35 | } |
| 36 | |
| 37 | /// The mode-info grid and the partition contexts of the frame. |
| 38 | pub(crate) struct MiState { |
| 39 | pub mi_cols: usize, |
| 40 | pub mi_rows: usize, |
| 41 | /// `mi_row * mi_cols + mi_col`; every cell a block covers holds a |
| 42 | /// clone of its record (libvpx shares the pointer). Frame-scoped: a |
| 43 | /// tile row k > 0 reads the rows written by tile row k - 1. |
| 44 | pub grid: Vec<Option<MiInfo>>, |
| 45 | /// `above_seg_context`, one byte per 8x8 column (frame-scoped, like |
| 46 | /// libvpx's `cm->above_seg_context`, memset once per frame). |
| 47 | pub above_seg: Vec<u8>, |
| 48 | /// `left_seg_context`, one byte per 8x8 row (reset per SB row). |
| 49 | pub left_seg: [u8; 32], |
| 50 | } |
| 51 | |
| 52 | impl MiState { |
| 53 | pub(crate) fn new(mi_cols: usize, mi_rows: usize) -> Self { |
| 54 | MiState { |
| 55 | mi_cols, |
| 56 | mi_rows, |
| 57 | grid: vec![None; mi_cols * mi_rows], |
| 58 | // `above_seg_context` is `mi_cols_aligned_to_sb` long in libvpx; |
| 59 | // `update_partition_ctx` fills `[col .. col + bw)` which can |
| 60 | // overrun `mi_cols` by up to a full superblock (a 64x64 block at |
| 61 | // the last mi col). |
| 62 | above_seg: vec![0; mi_cols.div_ceil(8) * 8], |
| 63 | left_seg: [0; 32], |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// The grid cell at an absolute (row, col), `None` when not decoded yet. |
| 68 | pub(crate) fn at(&self, row: usize, col: usize) -> Option<&MiInfo> { |
| 69 | self.grid.get(row * self.mi_cols + col).and_then(|c| c.as_ref()) |
| 70 | } |
| 71 | |
| 72 | /// `xd->above_mi` (`set_mi_row_col`, vp9_onyxc_int.h:430): available iff |
| 73 | /// the block is not on the FRAME's first mi row. libvpx gates on |
| 74 | /// `mi_row != 0`, not on the tile row start — for a tile-row stream the |
| 75 | /// row above tile row k > 0 was decoded by tile row k - 1 into the same |
| 76 | /// frame-scoped mi grid. |
| 77 | pub(crate) fn above_mi(&self, row: usize, col: usize) -> Option<&MiInfo> { |
| 78 | if row > 0 { |
| 79 | self.grid[(row - 1) * self.mi_cols + col].as_ref() |
| 80 | } else { |
| 81 | None |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | pub(crate) fn left_mi(&self, row: usize, col: usize, tile_col_start: usize) -> Option<&MiInfo> { |
| 86 | if col > tile_col_start { |
| 87 | self.grid[row * self.mi_cols + col - 1].as_ref() |
| 88 | } else { |
| 89 | None |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// `dec_partition_plane_context`. |
| 94 | pub(crate) fn partition_ctx(&self, row: usize, col: usize, bsl: u32) -> usize { |
| 95 | let above = (self.above_seg[col] >> bsl) & 1; |
| 96 | let left = (self.left_seg[row & 31] >> bsl) & 1; |
| 97 | (left * 2 + above) as usize + (bsl as usize) * 4 |
| 98 | } |
| 99 | |
| 100 | /// `dec_update_partition_context`; `partition_context_lookup[subsize]`. |
| 101 | pub(crate) fn update_partition_ctx( |
| 102 | &mut self, |
| 103 | row: usize, |
| 104 | col: usize, |
| 105 | subsize: usize, |
| 106 | bw: usize, |
| 107 | ) { |
| 108 | // (above, left) per BLOCK_SIZE, common_data.c:230. |
| 109 | const LOOKUP: [(u8, u8); 13] = [ |
| 110 | (15, 15), |
| 111 | (15, 14), |
| 112 | (14, 15), |
| 113 | (14, 14), |
| 114 | (14, 12), |
| 115 | (12, 14), |
| 116 | (12, 12), |
| 117 | (12, 8), |
| 118 | (8, 12), |
| 119 | (8, 8), |
| 120 | (8, 0), |
| 121 | (0, 8), |
| 122 | (0, 0), |
| 123 | ]; |
| 124 | let (a, l) = LOOKUP[subsize]; |
| 125 | self.above_seg[col..col + bw].fill(a); |
| 126 | self.left_seg[row & 31..(row & 31) + bw].fill(l); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// `read_intra_mode_kf` + `read_intra_frame_mode_info` (decodemv.c). |
| 131 | pub(crate) fn read_intra_frame_mode_info( |
| 132 | r: &mut BoolDecoder, |
| 133 | st: &mut MiState, |
| 134 | seg: &ec_vp9_syntax::SegmentationParams, |
| 135 | row: usize, |
| 136 | col: usize, |
| 137 | sb_type: usize, |
| 138 | tile_col_start: usize, |
| 139 | tx_mode: u8, |
| 140 | fc: &FrameContext, |
| 141 | ) -> crate::Result<MiInfo> { |
| 142 | let above_mi = st.above_mi(row, col).cloned(); |
| 143 | let left_mi = st.left_mi(row, col, tile_col_start).cloned(); |
| 144 | |
| 145 | // read_intra_segment_id: keyframes have no temporal prediction; a |
| 146 | // map that is not updated keeps the previous id, which for a |
| 147 | // keyframe's fresh map is 0 (setup_past_independence cleared it). |
| 148 | let segment = if seg.enabled && seg.update_map { |
| 149 | r.read_tree(&SEGMENT_TREE, &seg.tree_probs) |
| 150 | } else { |
| 151 | 0 |
| 152 | }; |
| 153 | |
| 154 | // read_skip |
| 155 | let skip_ctx = above_mi.as_ref().map_or(0, |m| u32::from(m.skip)) |
| 156 | + left_mi.as_ref().map_or(0, |u| u32::from(u.skip)); |
| 157 | let skip_ctx = skip_ctx as usize; |
| 158 | let seg_skip = seg.enabled |
| 159 | && seg |
| 160 | .feature_enabled |
| 161 | .get(segment as usize) |
| 162 | .is_some_and(|f| f[3]); |
| 163 | let skip = if seg_skip { |
| 164 | true |
| 165 | } else { |
| 166 | r.read_bool(fc.skip[skip_ctx]) |
| 167 | }; |
| 168 | |
| 169 | // read_tx_size |
| 170 | let max_tx = MAX_TXSIZE_LOOKUP[sb_type] as usize; |
| 171 | let tx_size = if tx_mode == TX_MODE_SELECT && sb_type >= 3 { |
| 172 | // get_tx_size_context (vp9_pred_common.h:156): default each side to |
| 173 | // max_tx, then substitute the other side when one is missing, in |
| 174 | // this exact order — the `!has_above` fallback is load-bearing. |
| 175 | let ctx = { |
| 176 | let a = above_mi |
| 177 | .as_ref() |
| 178 | .map_or(max_tx, |m| if m.skip { max_tx } else { m.tx_size }); |
| 179 | let mut l = left_mi |
| 180 | .as_ref() |
| 181 | .map_or(max_tx, |m| if m.skip { max_tx } else { m.tx_size }); |
| 182 | if left_mi.is_none() { |
| 183 | l = a; |
| 184 | } |
| 185 | let mut a = a; |
| 186 | if above_mi.is_none() { |
| 187 | a = l; |
| 188 | } |
| 189 | usize::from(a + l > max_tx) |
| 190 | }; |
| 191 | // get_tx_probs (vp9_pred_common.h:173): the FRAME-updated tables |
| 192 | // (`cm->fc->tx_probs`), not the const defaults. |
| 193 | let probs: &[u8] = match max_tx { |
| 194 | 1 => &fc.tx_p8x8[ctx][..], |
| 195 | 2 => &fc.tx_p16x16[ctx][..], |
| 196 | _ => &fc.tx_p32x32[ctx][..], |
| 197 | }; |
| 198 | let mut tx = r.read_bool(probs[0]) as usize; |
| 199 | if tx != TX_4X4 && max_tx >= TX_16X16 { |
| 200 | tx += r.read_bool(probs[1]) as usize; |
| 201 | if tx != TX_8X8 && max_tx >= TX_32X32 { |
| 202 | tx += r.read_bool(probs[2]) as usize; |
| 203 | } |
| 204 | } |
| 205 | tx |
| 206 | } else { |
| 207 | max_tx.min(TX_MODE_TO_BIGGEST_TX_SIZE[tx_mode as usize]) |
| 208 | }; |
| 209 | |
| 210 | // y mode(s), per sub-block for sub-8x8 blocks |
| 211 | let above_pair: Option<(usize, [u8; 4], u8)> = |
| 212 | above_mi.as_ref().map(|m| (m.sb_type, m.bmi, m.mode)); |
| 213 | let left_pair: Option<(usize, [u8; 4], u8)> = |
| 214 | left_mi.as_ref().map(|m| (m.sb_type, m.bmi, m.mode)); |
| 215 | let mut mi = MiInfo { |
| 216 | sb_type, |
| 217 | segment, |
| 218 | skip, |
| 219 | tx_size, |
| 220 | mode: DC_PRED, |
| 221 | uv_mode: DC_PRED, |
| 222 | bmi: [DC_PRED; 4], |
| 223 | is_inter: false, |
| 224 | ref_frame: [0, -1], |
| 225 | mv: [(0, 0); 2], |
| 226 | mv_diff: [(0, 0); 2], |
| 227 | bmi_mv: [[(0, 0); 2]; 4], |
| 228 | seg_id_predicted: false, |
| 229 | // read_intra_block_mode_info sets this so a later |
| 230 | // get_pred_context_switchable_interp never sees a stale filter. |
| 231 | interp_filter: 3, |
| 232 | }; |
| 233 | match sb_type { |
| 234 | 0 => { |
| 235 | // BLOCK_4X4 |
| 236 | for i in 0..4 { |
| 237 | let a = above_block_mode(&mi.bmi, above_pair, i); |
| 238 | let l = left_block_mode(&mi.bmi, left_pair, i); |
| 239 | mi.bmi[i] = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a, l)); |
| 240 | } |
| 241 | mi.mode = mi.bmi[3]; |
| 242 | } |
| 243 | 1 => { |
| 244 | // BLOCK_4X8; decodemv.c commits bmi[0]/bmi[2] BEFORE the |
| 245 | // second mode's probs are derived from the current bmi. |
| 246 | let a0 = above_block_mode(&mi.bmi, above_pair, 0); |
| 247 | let l0 = left_block_mode(&mi.bmi, left_pair, 0); |
| 248 | let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0)); |
| 249 | mi.bmi[0] = m0; |
| 250 | mi.bmi[2] = m0; |
| 251 | let a1 = above_block_mode(&mi.bmi, above_pair, 1); |
| 252 | let l1 = left_block_mode(&mi.bmi, left_pair, 1); |
| 253 | let m1 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a1, l1)); |
| 254 | mi.bmi[1] = m1; |
| 255 | mi.bmi[3] = m1; |
| 256 | mi.mode = m1; |
| 257 | } |
| 258 | 2 => { |
| 259 | // BLOCK_8X4; same commit-before-read ordering. |
| 260 | let a0 = above_block_mode(&mi.bmi, above_pair, 0); |
| 261 | let l0 = left_block_mode(&mi.bmi, left_pair, 0); |
| 262 | let m0 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a0, l0)); |
| 263 | mi.bmi[0] = m0; |
| 264 | mi.bmi[1] = m0; |
| 265 | let a2 = above_block_mode(&mi.bmi, above_pair, 2); |
| 266 | let l2 = left_block_mode(&mi.bmi, left_pair, 2); |
| 267 | let m2 = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a2, l2)); |
| 268 | mi.bmi[2] = m2; |
| 269 | mi.bmi[3] = m2; |
| 270 | mi.mode = m2; |
| 271 | } |
| 272 | _ => { |
| 273 | let a = above_block_mode(&mi.bmi, above_pair, 0); |
| 274 | let l = left_block_mode(&mi.bmi, left_pair, 0); |
| 275 | mi.mode = r.read_tree(&INTRA_MODE_TREE, &kf_y_mode_probs(a, l)); |
| 276 | } |
| 277 | } |
| 278 | // uv mode: `vp9_kf_uv_mode_prob` indexed by y mode, decoded with the |
| 279 | // SAME intra_mode_tree (decodemv.c:232 calls read_intra_mode). |
| 280 | mi.uv_mode = r.read_tree(&INTRA_MODE_TREE, &kf_uv_probs(mi.mode)); |
| 281 | if crate::trace_enabled() { |
| 282 | eprintln!( |
| 283 | "MODE row={} col={} bsize={} skip={} tx={} y={} uv={}", |
| 284 | row, col, sb_type, mi.skip as u8, mi.tx_size, mi.mode, mi.uv_mode |
| 285 | ); |
| 286 | } |
| 287 | Ok(mi) |
| 288 | } |