vp9kf: drop crates/ec-vp9/src/decode.rsdecode.rs
Browse files at this commitfcfe94757df1d567bfd483b48706a1652d0e0c08Parent: 03cbf641 changed file +0 -620
crates/ec-vp9/src/decode.rsdecode.rsdeleted+0 -620
| @@ -1,620 +0,0 @@ | |||
| 1 | -//! Frame decoding: superframe split, uncompressed header (via | ||
| 2 | -//! [`ec_vp9_syntax::Vp9Parser`]), compressed header, tile parsing, | ||
| 3 | -//! partition recursion, intra prediction, token decoding, | ||
| 4 | -//! reconstruction and the loop filter (spec 6, 7, 8; libvpx | ||
| 5 | -//! `vp9_decodeframe.c`). | ||
| 6 | -//! | ||
| 7 | -//! Lane scope: profile-0 8-bit 4:2:0 keyframes. Inter frames and other | ||
| 8 | -//! profiles/subsamplings are refused by name; `show_existing_frame` | ||
| 9 | -//! returns the buffered reference picture instead of pretending to | ||
| 10 | -//! decode. | ||
| 11 | - | ||
| 12 | -use crate::header::{read_compressed_header, FrameContext}; | ||
| 13 | -use crate::intra::build_intra_predictors; | ||
| 14 | -use crate::loopfilter::LfGrids; | ||
| 15 | -use crate::modes::{read_intra_frame_mode_info, MiInfo, MiState}; | ||
| 16 | -use crate::tables::*; | ||
| 17 | -use crate::tokens::{decode_coefs, scan_for, PlaneContexts}; | ||
| 18 | -use crate::transform::inverse_transform_add; | ||
| 19 | -use crate::{Error, Result}; | ||
| 20 | -use ec_vp9_syntax::{superframe, FrameHeader, FrameType, Vp9Parser}; | ||
| 21 | - | ||
| 22 | -const SB_MI: usize = 8; // a 64x64 superblock in mi (8x8) units | ||
| 23 | - | ||
| 24 | -/// A decoded, displayable frame: cropped contiguous planes. | ||
| 25 | -#[derive(Clone, Debug)] | ||
| 26 | -pub struct Picture { | ||
| 27 | - /// Luma plane, `stride`-spaced rows of `width` pixels. | ||
| 28 | - pub y: Vec<u8>, | ||
| 29 | - /// U (Cb) plane, half resolution. | ||
| 30 | - pub u: Vec<u8>, | ||
| 31 | - /// V (Cr) plane, half resolution. | ||
| 32 | - pub v: Vec<u8>, | ||
| 33 | - /// Frame width in pixels. | ||
| 34 | - pub width: u16, | ||
| 35 | - /// Frame height in pixels. | ||
| 36 | - pub height: u16, | ||
| 37 | - /// Luma row pitch of the returned planes. | ||
| 38 | - pub stride: usize, | ||
| 39 | - /// Chroma row pitch of the returned planes. | ||
| 40 | - pub uv_stride: usize, | ||
| 41 | -} | ||
| 42 | - | ||
| 43 | -struct Planes { | ||
| 44 | - y: Vec<u8>, | ||
| 45 | - u: Vec<u8>, | ||
| 46 | - v: Vec<u8>, | ||
| 47 | - ys: usize, | ||
| 48 | - uvs: usize, | ||
| 49 | - aw: usize, | ||
| 50 | - ah: usize, | ||
| 51 | -} | ||
| 52 | - | ||
| 53 | -impl Planes { | ||
| 54 | - fn plane(&mut self, plane: usize) -> (&mut [u8], usize) { | ||
| 55 | - match plane { | ||
| 56 | - 0 => (&mut self.y, self.ys), | ||
| 57 | - 1 => (&mut self.u, self.uvs), | ||
| 58 | - _ => (&mut self.v, self.uvs), | ||
| 59 | - } | ||
| 60 | - } | ||
| 61 | - fn dims(&self, plane: usize) -> (usize, usize) { | ||
| 62 | - if plane == 0 { | ||
| 63 | - (self.aw, self.ah) | ||
| 64 | - } else { | ||
| 65 | - (self.aw / 2, self.ah / 2) | ||
| 66 | - } | ||
| 67 | - } | ||
| 68 | -} | ||
| 69 | - | ||
| 70 | -/// The VP9 decoder. Feed complete frames — one full VP9 frame payload | ||
| 71 | -/// or one superframe chunk, exactly what a container demuxer hands over | ||
| 72 | -/// for the `VP90` codec. | ||
| 73 | -pub struct Decoder { | ||
| 74 | - parser: Vp9Parser, | ||
| 75 | - ctx: FrameContext, | ||
| 76 | - refs: [Option<Picture>; 8], | ||
| 77 | - /// Frame scratch, live only inside `decode_keyframe`. | ||
| 78 | - planes: Option<Planes>, | ||
| 79 | -} | ||
| 80 | - | ||
| 81 | -impl Decoder { | ||
| 82 | - /// Create an empty decoder; the first key frame sets the size. | ||
| 83 | - pub fn new() -> Self { | ||
| 84 | - Decoder { | ||
| 85 | - parser: Vp9Parser::new(), | ||
| 86 | - ctx: FrameContext::new(), | ||
| 87 | - refs: [const { None }; 8], | ||
| 88 | - planes: None, | ||
| 89 | - } | ||
| 90 | - } | ||
| 91 | - | ||
| 92 | - /// Decode one frame (or one superframe chunk); returns the picture | ||
| 93 | - /// when a frame is for display. Hidden frames still update state. | ||
| 94 | - pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> { | ||
| 95 | - let mut out = None; | ||
| 96 | - for sub in superframe::split(frame)? { | ||
| 97 | - if let Some(pic) = self.decode_one(sub)? { | ||
| 98 | - out = Some(pic); | ||
| 99 | - } | ||
| 100 | - } | ||
| 101 | - Ok(out) | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - fn decode_one(&mut self, frame: &[u8]) -> Result<Option<Picture>> { | ||
| 105 | - let hdr = self.parser.parse_frame(frame)?; | ||
| 106 | - if hdr.show_existing_frame { | ||
| 107 | - let slot = hdr.frame_to_show_map_idx as usize; | ||
| 108 | - return match self.refs.get(slot).and_then(|p| p.as_ref()) { | ||
| 109 | - Some(p) => Ok(Some(p.clone())), | ||
| 110 | - None => Err(corrupt("show_existing_frame names an empty slot")), | ||
| 111 | - }; | ||
| 112 | - } | ||
| 113 | - if hdr.profile != 0 { | ||
| 114 | - return Err(Error::unsupported( | ||
| 115 | - format!("vp9 profile {}", hdr.profile), | ||
| 116 | - "this lane decodes profile 0 only", | ||
| 117 | - )); | ||
| 118 | - } | ||
| 119 | - if hdr.subsampling_x != 1 || hdr.subsampling_y != 1 { | ||
| 120 | - return Err(Error::unsupported( | ||
| 121 | - "vp9 subsampling", | ||
| 122 | - format!( | ||
| 123 | - "this lane decodes 4:2:0 only (subsampling {}/{} at profile {})", | ||
| 124 | - hdr.subsampling_x, hdr.subsampling_y, hdr.profile | ||
| 125 | - ), | ||
| 126 | - )); | ||
| 127 | - } | ||
| 128 | - if hdr.frame_type != FrameType::Key { | ||
| 129 | - return Err(Error::unsupported( | ||
| 130 | - "vp9 inter", | ||
| 131 | - "this lane decodes keyframes only", | ||
| 132 | - )); | ||
| 133 | - } | ||
| 134 | - // A keyframe resets the frame context (spec 7.2) before the | ||
| 135 | - // compressed header codes this frame's updates on top. | ||
| 136 | - self.ctx = FrameContext::new(); | ||
| 137 | - let hdr_start = hdr.uncompressed_header_size as usize; | ||
| 138 | - let tx_mode = read_compressed_header( | ||
| 139 | - &frame[hdr_start..hdr_start + hdr.header_size_in_bytes as usize], | ||
| 140 | - &mut self.ctx, | ||
| 141 | - )?; | ||
| 142 | - | ||
| 143 | - let pic = self.decode_keyframe(&hdr, frame, tx_mode)?; | ||
| 144 | - // Reference refresh (spec 8.10): a keyframe updates all slots. | ||
| 145 | - for slot in self.refs.iter_mut() { | ||
| 146 | - *slot = Some(pic.clone()); | ||
| 147 | - } | ||
| 148 | - Ok(if hdr.show_frame { Some(pic) } else { None }) | ||
| 149 | - } | ||
| 150 | - | ||
| 151 | - fn decode_keyframe(&mut self, hdr: &FrameHeader, frame: &[u8], tx_mode: u8) -> Result<Picture> { | ||
| 152 | - let width = hdr.width as usize; | ||
| 153 | - let height = hdr.height as usize; | ||
| 154 | - let aw = width.div_ceil(8) * 8; | ||
| 155 | - let ah = height.div_ceil(8) * 8; | ||
| 156 | - self.planes = Some(Planes { | ||
| 157 | - y: vec![128u8; aw * ah], | ||
| 158 | - u: vec![128u8; aw / 2 * ah / 2], | ||
| 159 | - v: vec![128u8; aw / 2 * ah / 2], | ||
| 160 | - ys: aw, | ||
| 161 | - uvs: aw / 2, | ||
| 162 | - aw, | ||
| 163 | - ah, | ||
| 164 | - }); | ||
| 165 | - let tail_start = | ||
| 166 | - hdr.uncompressed_header_size as usize + hdr.header_size_in_bytes as usize; | ||
| 167 | - let tail: Vec<u8> = frame[tail_start..].to_vec(); | ||
| 168 | - let res = self.decode_keyframe_inner(hdr, tx_mode, &tail); | ||
| 169 | - self.planes = None; | ||
| 170 | - res | ||
| 171 | - } | ||
| 172 | - | ||
| 173 | - fn decode_keyframe_inner(&mut self, hdr: &FrameHeader, tx_mode: u8, tail: &[u8]) -> Result<Picture> { | ||
| 174 | - let aw = self.planes.as_ref().expect("frame scratch").aw; | ||
| 175 | - let ah = self.planes.as_ref().expect("frame scratch").ah; | ||
| 176 | - let width = hdr.width as usize; | ||
| 177 | - let height = hdr.height as usize; | ||
| 178 | - let mi_cols = hdr.mi_cols() as usize; | ||
| 179 | - let mi_rows = hdr.mi_rows() as usize; | ||
| 180 | - let sb64_cols = mi_cols.div_ceil(SB_MI); | ||
| 181 | - let sb64_rows = mi_rows.div_ceil(SB_MI); | ||
| 182 | - let tile_cols = 1usize << hdr.tile_info.cols_log2; | ||
| 183 | - let tile_rows = 1usize << hdr.tile_info.rows_log2; | ||
| 184 | - | ||
| 185 | - // Tile data: every tile except the overall last carries a | ||
| 186 | - // 32-bit LE size prefix (spec 6.4). | ||
| 187 | - let mut data: &[u8] = tail; | ||
| 188 | - let mut tiles: Vec<&[u8]> = Vec::with_capacity(tile_cols * tile_rows); | ||
| 189 | - for tr in 0..tile_rows { | ||
| 190 | - for tc in 0..tile_cols { | ||
| 191 | - let last = tr == tile_rows - 1 && tc == tile_cols - 1; | ||
| 192 | - if last { | ||
| 193 | - tiles.push(data); | ||
| 194 | - } else { | ||
| 195 | - ensure(data.len() >= 4, "truncated tile size")?; | ||
| 196 | - let sz = u32::from_le_bytes(data[..4].try_into().unwrap()) as usize; | ||
| 197 | - data = &data[4..]; | ||
| 198 | - ensure(data.len() >= sz, "truncated tile data")?; | ||
| 199 | - tiles.push(&data[..sz]); | ||
| 200 | - data = &data[sz..]; | ||
| 201 | - } | ||
| 202 | - } | ||
| 203 | - } | ||
| 204 | - | ||
| 205 | - // Keyframe loop filter levels: intra ref, no mode class, per | ||
| 206 | - // segment (spec 8.8.1). | ||
| 207 | - let mut level_of_seg = [[0u8; 2]; 8]; | ||
| 208 | - for (seg, slot) in level_of_seg.iter_mut().enumerate() { | ||
| 209 | - *slot = hdr.loop_filter_levels(seg)[0]; | ||
| 210 | - } | ||
| 211 | - | ||
| 212 | - let mut grids = LfGrids { | ||
| 213 | - tx4: vec![0; mi_cols * 2 * mi_rows * 2], | ||
| 214 | - level8: vec![0; mi_cols * mi_rows], | ||
| 215 | - blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows], | ||
| 216 | - otx: vec![0; mi_cols * mi_rows], | ||
| 217 | - mi_cols, | ||
| 218 | - mi_rows, | ||
| 219 | - }; | ||
| 220 | - let mut ti = 0usize; | ||
| 221 | - for tr in 0..tile_rows { | ||
| 222 | - let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; | ||
| 223 | - let row_hi = ((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2).min(mi_rows); | ||
| 224 | - for tc in 0..tile_cols { | ||
| 225 | - let col_lo = ((sb64_cols * tc) >> hdr.tile_info.cols_log2) * SB_MI; | ||
| 226 | - let col_hi = ((sb64_cols * (tc + 1)) >> hdr.tile_info.cols_log2).min(mi_cols); | ||
| 227 | - let mut r = crate::bool::BoolDecoder::new(tiles[ti])?; | ||
| 228 | - ti += 1; | ||
| 229 | - let mut ectx = [ | ||
| 230 | - PlaneContexts::new(mi_cols * 2), | ||
| 231 | - PlaneContexts::new(mi_cols), | ||
| 232 | - PlaneContexts::new(mi_cols), | ||
| 233 | - ]; | ||
| 234 | - let mut mi = MiState::new(mi_cols, mi_rows); | ||
| 235 | - ectx.iter_mut().for_each(|p| p.above.fill(0)); | ||
| 236 | - mi.above_seg.fill(0); | ||
| 237 | - for sb_row in row_lo..row_hi { | ||
| 238 | - if sb_row % SB_MI == 0 { | ||
| 239 | - ectx.iter_mut().for_each(|p| p.left = [0; 32]); | ||
| 240 | - mi.left_seg = [0; 32]; | ||
| 241 | - } | ||
| 242 | - for sb_col in col_lo..col_hi { | ||
| 243 | - self.decode_partition( | ||
| 244 | - &mut r, | ||
| 245 | - &mut mi, | ||
| 246 | - &mut ectx, | ||
| 247 | - &mut grids, | ||
| 248 | - &level_of_seg, | ||
| 249 | - hdr, | ||
| 250 | - sb_row, | ||
| 251 | - sb_col, | ||
| 252 | - 4, | ||
| 253 | - row_lo, | ||
| 254 | - col_lo, | ||
| 255 | - tx_mode, | ||
| 256 | - )?; | ||
| 257 | - } | ||
| 258 | - } | ||
| 259 | - ensure(r.overreads() == 0, "tile bool decoder desync")?; | ||
| 260 | - } | ||
| 261 | - } | ||
| 262 | - | ||
| 263 | - // Loop filter post-pass (level 0 disables it for the frame). | ||
| 264 | - if hdr.loop_filter.level > 0 { | ||
| 265 | - let p = self.planes.as_mut().expect("frame scratch"); | ||
| 266 | - grids.filter_frame( | ||
| 267 | - &mut p.y, p.ys, &mut p.u, &mut p.v, p.uvs, aw, ah, aw / 2, ah / 2, | ||
| 268 | - hdr.loop_filter.sharpness, | ||
| 269 | - ); | ||
| 270 | - } | ||
| 271 | - | ||
| 272 | - let crop = |src: &[u8], stride: usize, w: usize, h: usize| -> Vec<u8> { | ||
| 273 | - let mut out = Vec::with_capacity(w * h); | ||
| 274 | - for row in 0..h { | ||
| 275 | - out.extend_from_slice(&src[row * stride..row * stride + w]); | ||
| 276 | - } | ||
| 277 | - out | ||
| 278 | - }; | ||
| 279 | - let planes = self.planes.as_ref().expect("frame scratch"); | ||
| 280 | - Ok(Picture { | ||
| 281 | - y: crop(&planes.y, planes.ys, width, height), | ||
| 282 | - u: crop(&planes.u, planes.uvs, width / 2, height / 2), | ||
| 283 | - v: crop(&planes.v, planes.uvs, width / 2, height / 2), | ||
| 284 | - width: width as u16, | ||
| 285 | - height: height as u16, | ||
| 286 | - stride: aw, | ||
| 287 | - uv_stride: aw / 2, | ||
| 288 | - }) | ||
| 289 | - } | ||
| 290 | - | ||
| 291 | - /// `decode_partition` (decodeframe.c:1173). | ||
| 292 | - #[allow(clippy::too_many_arguments)] | ||
| 293 | - fn decode_partition( | ||
| 294 | - &mut self, | ||
| 295 | - r: &mut crate::bool::BoolDecoder, | ||
| 296 | - mi: &mut MiState, | ||
| 297 | - ectx: &mut [PlaneContexts; 3], | ||
| 298 | - grids: &mut LfGrids, | ||
| 299 | - level_of_seg: &[[u8; 2]; 8], | ||
| 300 | - hdr: &FrameHeader, | ||
| 301 | - row: usize, | ||
| 302 | - col: usize, | ||
| 303 | - n4: usize, | ||
| 304 | - tile_row_start: usize, | ||
| 305 | - tile_col_start: usize, | ||
| 306 | - tx_mode: u8, | ||
| 307 | - ) -> Result<()> { | ||
| 308 | - if row >= mi.mi_rows || col >= mi.mi_cols { | ||
| 309 | - return Ok(()); | ||
| 310 | - } | ||
| 311 | - let n8 = n4 - 1; | ||
| 312 | - let hbs = 1usize << (n8 - 1); | ||
| 313 | - let has_rows = row + hbs < mi.mi_rows; | ||
| 314 | - let has_cols = col + hbs < mi.mi_cols; | ||
| 315 | - let partition = { | ||
| 316 | - let ctx = mi.partition_ctx(row, col, n8 as u32); | ||
| 317 | - let probs = &self.ctx.partition[ctx]; | ||
| 318 | - if has_rows && has_cols { | ||
| 319 | - r.read_tree(&PARTITION_TREE, probs) | ||
| 320 | - } else if !has_rows && has_cols { | ||
| 321 | - if r.read_bool(probs[1]) { 3 } else { 1 } | ||
| 322 | - } else if has_rows && !has_cols { | ||
| 323 | - if r.read_bool(probs[2]) { 3 } else { 2 } | ||
| 324 | - } else { | ||
| 325 | - 3 | ||
| 326 | - } | ||
| 327 | - }; | ||
| 328 | - let bsize = bsize_of_n4(n4); | ||
| 329 | - let subsize = SUBSIZE_LOOKUP[partition as usize * 13 + bsize] as usize; | ||
| 330 | - if hbs == 0 { | ||
| 331 | - self.decode_block(r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, 1, 1, | ||
| 332 | - tile_row_start, tile_col_start, tx_mode)?; | ||
| 333 | - } else { | ||
| 334 | - match partition { | ||
| 335 | - 0 => self.decode_block(r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, | ||
| 336 | - n4, n4, tile_row_start, tile_col_start, tx_mode)?, | ||
| 337 | - 1 => { | ||
| 338 | - self.decode_block(r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, | ||
| 339 | - n4, n8, tile_row_start, tile_col_start, tx_mode)?; | ||
| 340 | - if has_rows { | ||
| 341 | - self.decode_block(r, mi, ectx, grids, level_of_seg, hdr, row + hbs, col, | ||
| 342 | - subsize, n4, n8, tile_row_start, tile_col_start, tx_mode)?; | ||
| 343 | - } | ||
| 344 | - } | ||
| 345 | - 2 => { | ||
| 346 | - self.decode_block(r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, | ||
| 347 | - n8, n4, tile_row_start, tile_col_start, tx_mode)?; | ||
| 348 | - if has_cols { | ||
| 349 | - self.decode_block(r, mi, ectx, grids, level_of_seg, hdr, row, col + hbs, | ||
| 350 | - subsize, n8, n4, tile_row_start, tile_col_start, tx_mode)?; | ||
| 351 | - } | ||
| 352 | - } | ||
| 353 | - _ => { | ||
| 354 | - for (rr, cc) in [ | ||
| 355 | - (row, col), | ||
| 356 | - (row, col + hbs), | ||
| 357 | - (row + hbs, col), | ||
| 358 | - (row + hbs, col + hbs), | ||
| 359 | - ] { | ||
| 360 | - self.decode_partition(r, mi, ectx, grids, level_of_seg, hdr, rr, cc, n8, | ||
| 361 | - tile_row_start, tile_col_start, tx_mode)?; | ||
| 362 | - } | ||
| 363 | - } | ||
| 364 | - } | ||
| 365 | - } | ||
| 366 | - if bsize >= 3 && (bsize == 3 || partition != 3) { | ||
| 367 | - mi.update_partition_ctx(row, col, subsize, 1 << n8); | ||
| 368 | - } | ||
| 369 | - Ok(()) | ||
| 370 | - } | ||
| 371 | - | ||
| 372 | - /// `decode_block` intra path (decodeframe.c:913). | ||
| 373 | - #[allow(clippy::too_many_arguments)] | ||
| 374 | - fn decode_block( | ||
| 375 | - &mut self, | ||
| 376 | - r: &mut crate::bool::BoolDecoder, | ||
| 377 | - mi: &mut MiState, | ||
| 378 | - ectx: &mut [PlaneContexts; 3], | ||
| 379 | - grids: &mut LfGrids, | ||
| 380 | - level_of_seg: &[[u8; 2]; 8], | ||
| 381 | - hdr: &FrameHeader, | ||
| 382 | - row: usize, | ||
| 383 | - col: usize, | ||
| 384 | - sb_type: usize, | ||
| 385 | - bwl: usize, | ||
| 386 | - bhl: usize, | ||
| 387 | - tile_row_start: usize, | ||
| 388 | - tile_col_start: usize, | ||
| 389 | - tx_mode: u8, | ||
| 390 | - ) -> Result<()> { | ||
| 391 | - let bw = 1 << (bwl - 1); | ||
| 392 | - let bh = 1 << (bhl - 1); | ||
| 393 | - let x_mis = bw.min(mi.mi_cols - col); | ||
| 394 | - let y_mis = bh.min(mi.mi_rows - row); | ||
| 395 | - | ||
| 396 | - let info = read_intra_frame_mode_info( | ||
| 397 | - r, mi, &hdr.segmentation, &self.ctx.partition, row, col, sb_type, x_mis, y_mis, | ||
| 398 | - tile_row_start, tile_col_start, tx_mode, &self.ctx.skip, | ||
| 399 | - )?; | ||
| 400 | - | ||
| 401 | - let n4w = [bw * 2, bw, bw]; | ||
| 402 | - let n4h = [bh * 2, bh, bh]; | ||
| 403 | - let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; | ||
| 404 | - let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; | ||
| 405 | - | ||
| 406 | - let level = level_of_seg[info.segment as usize][0]; | ||
| 407 | - for dy in 0..y_mis { | ||
| 408 | - for dx in 0..x_mis { | ||
| 409 | - let cell = (row + dy) * mi.mi_cols + col + dx; | ||
| 410 | - grids.level8[cell] = level; | ||
| 411 | - grids.blk[cell] = (sb_type as u8, row as u32, col as u32); | ||
| 412 | - grids.otx[cell] = if sb_type < 3 { TX_4X4 as u8 } else { info.tx_size as u8 }; | ||
| 413 | - } | ||
| 414 | - } | ||
| 415 | - // Luma tx4 grid (block-uniform in VP9). | ||
| 416 | - { | ||
| 417 | - let maxw = (n4w[0] as i32 + (mb_to_right.min(0) >> 5)).max(0) as usize; | ||
| 418 | - let maxh = (n4h[0] as i32 + (mb_to_bottom.min(0) >> 5)).max(0) as usize; | ||
| 419 | - for rr in 0..maxh.min(n4h[0]) { | ||
| 420 | - for cc in 0..maxw.min(n4w[0]) { | ||
| 421 | - grids.tx4[(row * 2 + rr) * mi.mi_cols * 2 + col * 2 + cc] = | ||
| 422 | - info.tx_size as u8; | ||
| 423 | - } | ||
| 424 | - } | ||
| 425 | - } | ||
| 426 | - | ||
| 427 | - if info.skip { | ||
| 428 | - for plane in 0..3usize { | ||
| 429 | - let s = usize::from(plane != 0); | ||
| 430 | - let offx = (col << 1) >> s; | ||
| 431 | - let offy = (row << 1) >> s; | ||
| 432 | - for i in 0..n4w[plane] { | ||
| 433 | - ectx[plane].above[offx + i] = 0; | ||
| 434 | - } | ||
| 435 | - let lrows = 32usize >> s; | ||
| 436 | - for i in 0..n4h[plane].min(lrows) { | ||
| 437 | - ectx[plane].left[(offy + i) % lrows] = 0; | ||
| 438 | - } | ||
| 439 | - } | ||
| 440 | - } | ||
| 441 | - | ||
| 442 | - for plane in 0..3usize { | ||
| 443 | - let s = usize::from(plane != 0); | ||
| 444 | - let tx_size = if plane == 0 { | ||
| 445 | - info.tx_size | ||
| 446 | - } else { | ||
| 447 | - get_uv_tx_size(info.tx_size, sb_type) | ||
| 448 | - }; | ||
| 449 | - let step = 1usize << tx_size; | ||
| 450 | - let max_blocks_wide = | ||
| 451 | - (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize; | ||
| 452 | - let max_blocks_high = | ||
| 453 | - (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; | ||
| 454 | - for brow in (0..max_blocks_high).step_by(step) { | ||
| 455 | - for bcol in (0..max_blocks_wide).step_by(step) { | ||
| 456 | - self.predict_and_reconstruct(r, ectx, hdr, &info, plane, row, col, brow, bcol, | ||
| 457 | - tx_size, sb_type, bw, bh, mb_to_right, mb_to_bottom, tile_row_start, | ||
| 458 | - tile_col_start)?; | ||
| 459 | - } | ||
| 460 | - } | ||
| 461 | - } | ||
| 462 | - | ||
| 463 | - for dy in 0..y_mis { | ||
| 464 | - for dx in 0..x_mis { | ||
| 465 | - mi.grid[(row + dy) * mi.mi_cols + col + dx] = Some(info.clone()); | ||
| 466 | - } | ||
| 467 | - } | ||
| 468 | - Ok(()) | ||
| 469 | - } | ||
| 470 | - | ||
| 471 | - /// `predict_and_reconstruct_intra_block` (decodeframe.c:309): | ||
| 472 | - /// predict, then tokens, then residual add; plus | ||
| 473 | - /// `vp9_decode_block_tokens` context bookkeeping (the v1.15 edge | ||
| 474 | - /// shifts included). | ||
| 475 | - #[allow(clippy::too_many_arguments)] | ||
| 476 | - fn predict_and_reconstruct( | ||
| 477 | - &mut self, | ||
| 478 | - r: &mut crate::bool::BoolDecoder, | ||
| 479 | - ectx: &mut [PlaneContexts; 3], | ||
| 480 | - hdr: &FrameHeader, | ||
| 481 | - info: &MiInfo, | ||
| 482 | - plane: usize, | ||
| 483 | - mi_row: usize, | ||
| 484 | - mi_col: usize, | ||
| 485 | - tx_row: usize, | ||
| 486 | - tx_col: usize, | ||
| 487 | - tx_size: usize, | ||
| 488 | - sb_type: usize, | ||
| 489 | - bw: usize, | ||
| 490 | - bh: usize, | ||
| 491 | - mb_to_right: i32, | ||
| 492 | - mb_to_bottom: i32, | ||
| 493 | - tile_row_start: usize, | ||
| 494 | - tile_col_start: usize, | ||
| 495 | - ) -> Result<()> { | ||
| 496 | - let s = usize::from(plane != 0); | ||
| 497 | - let mode = if plane == 0 { | ||
| 498 | - if sb_type < 3 { | ||
| 499 | - info.bmi[(tx_row << 1) + tx_col] | ||
| 500 | - } else { | ||
| 501 | - info.mode | ||
| 502 | - } | ||
| 503 | - } else { | ||
| 504 | - info.uv_mode | ||
| 505 | - }; | ||
| 506 | - let planes = self.planes.as_mut().expect("frame scratch"); | ||
| 507 | - let (pw, ph) = planes.dims(plane); | ||
| 508 | - let stride = pw; | ||
| 509 | - let bs = 4 << tx_size; | ||
| 510 | - let x0 = ((mi_col * 8) >> s) + tx_col * 4; | ||
| 511 | - let y0 = ((mi_row * 8) >> s) + tx_row * 4; | ||
| 512 | - let up = tx_row != 0 || mi_row > tile_row_start; | ||
| 513 | - let lft = tx_col != 0 || mi_col > tile_col_start; | ||
| 514 | - | ||
| 515 | - // 1. Intra prediction (reads reconstructed neighbours). | ||
| 516 | - { | ||
| 517 | - let (data, _) = planes.plane(plane); | ||
| 518 | - build_intra_predictors(data, stride, pw, ph, x0, y0, mode, bs, up, lft, x0 + bs < pw); | ||
| 519 | - } | ||
| 520 | - | ||
| 521 | - if info.skip { | ||
| 522 | - return Ok(()); | ||
| 523 | - } | ||
| 524 | - | ||
| 525 | - // 2. Tokens. | ||
| 526 | - let lossless = hdr.quantization.lossless(); | ||
| 527 | - let tx_type = if plane != 0 || lossless { | ||
| 528 | - DCT_DCT | ||
| 529 | - } else { | ||
| 530 | - MODE_TO_TXTYPE_LOOKUP[mode as usize] as usize | ||
| 531 | - }; | ||
| 532 | - let (scan, nb) = scan_for(tx_size, tx_type); | ||
| 533 | - let q = hdr.segment_dequant(info.segment as usize); | ||
| 534 | - let dequant: [i16; 2] = if plane == 0 { | ||
| 535 | - [q.luma_dc, q.luma_ac] | ||
| 536 | - } else { | ||
| 537 | - [q.chroma_dc, q.chroma_ac] | ||
| 538 | - }; | ||
| 539 | - let ptype = usize::from(plane != 0); | ||
| 540 | - let ax = ((mi_col << 1) >> s) + tx_col; | ||
| 541 | - let lrows = 32usize >> s; | ||
| 542 | - let ay = ((mi_row << 1) >> s) % lrows; | ||
| 543 | - let ctx_in = | ||
| 544 | - usize::from(ectx[plane].above[ax] != 0) + usize::from(ectx[plane].left[ay] != 0); | ||
| 545 | - let coef = | ||
| 546 | - decode_coefs(r, ptype, tx_size, &dequant, ctx_in, scan, nb, &self.ctx.coef[tx_size]); | ||
| 547 | - | ||
| 548 | - // 3. Context write-back (vp9_decode_block_tokens, v1.15 shifts). | ||
| 549 | - let n4w = bw * 2 >> s; | ||
| 550 | - let n4h = bh * 2 >> s; | ||
| 551 | - let maxw = (n4w as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize; | ||
| 552 | - let maxh = (n4h as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; | ||
| 553 | - let nblocks = 1usize << tx_size; | ||
| 554 | - let eob_pos = usize::from(coef.eob > 0); | ||
| 555 | - let shift_a = if maxw > 0 && nblocks + tx_col > maxw { | ||
| 556 | - (nblocks - (maxw - tx_col)) * 8 | ||
| 557 | - } else { | ||
| 558 | - 0 | ||
| 559 | - }; | ||
| 560 | - let shift_l = if maxh > 0 && nblocks + tx_row > maxh { | ||
| 561 | - (nblocks - (maxh - tx_row)) * 8 | ||
| 562 | - } else { | ||
| 563 | - 0 | ||
| 564 | - }; | ||
| 565 | - match tx_size { | ||
| 566 | - TX_4X4 => { | ||
| 567 | - ectx[plane].above[ax] = eob_pos as u8; | ||
| 568 | - ectx[plane].left[ay] = eob_pos as u8; | ||
| 569 | - } | ||
| 570 | - TX_8X8 => { | ||
| 571 | - let va = ((eob_pos as u16) * 0x0101) >> shift_a; | ||
| 572 | - ectx[plane].above[ax] = va as u8; | ||
| 573 | - ectx[plane].above[ax + 1] = (va >> 8) as u8; | ||
| 574 | - let vl = ((eob_pos as u16) * 0x0101) >> shift_l; | ||
| 575 | - ectx[plane].left[ay] = vl as u8; | ||
| 576 | - ectx[plane].left[ay + 1] = (vl >> 8) as u8; | ||
| 577 | - } | ||
| 578 | - TX_16X16 => { | ||
| 579 | - for k in 0..4 { | ||
| 580 | - let va = ((eob_pos as u32) * 0x01010101) >> shift_a; | ||
| 581 | - ectx[plane].above[ax + k] = (va >> (8 * k)) as u8; | ||
| 582 | - let vl = ((eob_pos as u32) * 0x01010101) >> shift_l; | ||
| 583 | - ectx[plane].left[ay + k] = (vl >> (8 * k)) as u8; | ||
| 584 | - } | ||
| 585 | - } | ||
| 586 | - _ => { | ||
| 587 | - for k in 0..8 { | ||
| 588 | - let va = ((eob_pos as u64) * 0x0101010101010101) >> shift_a; | ||
| 589 | - ectx[plane].above[ax + k] = (va >> (8 * k)) as u8; | ||
| 590 | - let vl = ((eob_pos as u64) * 0x0101010101010101) >> shift_l; | ||
| 591 | - ectx[plane].left[ay + k] = (vl >> (8 * k)) as u8; | ||
| 592 | - } | ||
| 593 | - } | ||
| 594 | - } | ||
| 595 | - | ||
| 596 | - // 4. Residual add. | ||
| 597 | - if coef.eob > 0 { | ||
| 598 | - let dst_off = y0 * stride + x0; | ||
| 599 | - let (data, _) = planes.plane(plane); | ||
| 600 | - inverse_transform_add(tx_size, tx_type, &coef.coeffs, data, dst_off, stride, lossless); | ||
| 601 | - } | ||
| 602 | - Ok(()) | ||
| 603 | - } | ||
| 604 | -} | ||
| 605 | - | ||
| 606 | -impl Default for Decoder { | ||
| 607 | - fn default() -> Self { | ||
| 608 | - Self::new() | ||
| 609 | - } | ||
| 610 | -} | ||
| 611 | - | ||
| 612 | -/// `BLOCK_SIZE` from `log2(4x4s per side)`. | ||
| 613 | -fn bsize_of_n4(n4: usize) -> usize { | ||
| 614 | - match n4 { | ||
| 615 | - 4 => 12, // BLOCK_64X64 | ||
| 616 | - 3 => 9, // BLOCK_32X32 | ||
| 617 | - 2 => 3, // BLOCK_8X8 | ||
| 618 | - _ => 0, // BLOCK_4X4 | ||
| 619 | - } | ||
| 620 | -} | ||