use super::INTRABC_128RECT_CHUNK_SHAPE; use super::ChromaUnits; use super::{Error, Result}; type RectParts = (bool, usize, i32, usize, i32, Option<(i32, i32)>, Option, Option, Option, Option<(i32, i32)>); #[cfg(test)] use super::inter_shapes::INTER_BLOCK_SHAPES; use super::block::{PlaneBuf, UV_CFL_PRED, cfl_ac_q3_at, read_cfl_alphas, read_plane}; use super::coeffs::{ Neighbours, PaletteUv, PaletteY, decode_color_index_map_wh, fi_tx_row, filter_intra_size_class_rect, palette_bsize_ctx_wh, palette_onscreen, palette_onscreen_uv, publish_txfm_bands_if_in_inter, read_angle_delta, read_chroma_coeffs_rect, read_coeffs, read_palette_colors_uv, read_palette_colors_y, tx_size_context_txfm_rect, txfm_partition_update_rect, }; use super::inter_ctx::{ covering_leaf_tx_type, mu_chunk_order, mv_to_q4, read_inter_chroma_lossless, read_inter_plane, read_intrabc_dv, }; use super::inter_shapes::{ embed_grid_stride, inter_txbset_for, read_block_tx_size_rect, read_inter_plane_rect, rect_inter_chroma_set, rect_inter_luma_set, txbset_for, }; use super::leaf8::decode_block_128rect; use super::rect4::{decode_block_rect4, decode_block_rect64, decode_rect4_16_strip}; use super::{ Cdfs, D67_PRED, DC_PRED, Grid, INTRA_MODE_CTX, MI, Pred, Reach, SCAN_4X8, SCAN_4X16, SCAN_8X4, SCAN_8X16, SCAN_8X32, SCAN_16X4, SCAN_16X8, SCAN_16X32, SCAN_32X8, SCAN_32X16, SUB, SymbolDecoder, TX32, TxType, TxbSet, V_PRED, ZERO_RESIDUAL, allow_intrabc_frame, block_iqmatrix, block_q_idx, cfl_allowed_px, cfl_src_rect, dc_sign_ctx, default_intra_tx_type, default_scan, dequant_and_inverse_typed_wh, intra_segment_id, is_smooth_mode, lossless, maybe_read_cdef_idx, maybe_read_delta_lf, maybe_read_delta_q, mc, mono, mu_units, palette_window, plane_q_delta, push_intra_rect, push_mc_rect, record_intrabc_mi_rect, seg_id_pre_skip, ss_x, ss_y, tu_reach, tu_reach_rect, unsupported, }; pub(super) fn read_intra_mode_rect( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, above_mode: usize, left_mode: usize, cfl: bool, has_chroma: bool, bw: usize, bh: usize, enable_filter_intra: bool, skip_ctx: usize, allow_screen_content_tools: bool, palette: Option<(usize, &[u16])>, palette_uv_cache: &[u16], mi_r: usize, mi_c: usize, fctx: &crate::decode::FrameCtx, ) -> Result { let has_chroma = has_chroma && !mono(fctx); macro_rules! istep { ($name:literal, $val:expr) => {}; } let cfl = if cfl && bw.max(bh) > 32 { false } else { cfl }; let inter = fctx.intra_in_inter_mode.get(); let skip = if let Some((_, skip)) = inter { skip } else { let (seg_w_mi, seg_h_mi) = (bw / 4, bh / 4); if seg_id_pre_skip(fctx) { intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx); } let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; istep!("skip", skip as i32); if !seg_id_pre_skip(fctx) { intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); } maybe_read_cdef_idx(dec, mi_r, mi_c, (bw / MI, bh / MI), skip, fctx); istep!("cdef", 0); maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx); maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx); istep!("dq", 0); if allow_intrabc_frame(fctx) { let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; istep!("intrabc", use_intrabc as i32); if use_intrabc { let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (bw / MI, bh / MI), fctx); return Ok(( skip, DC_PRED, 0, DC_PRED, 0, None, None, None, None, Some(dv), )); } } skip }; let mode = if let Some((size_group, _)) = inter { dec.symbol(&mut cdfs.y_mode[size_group]) } else { let above_ctx = INTRA_MODE_CTX[above_mode]; let left_ctx = INTRA_MODE_CTX[left_mode]; dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]) }; istep!("mode", mode as i32); let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) } else { 0 }; istep!("angle_y", angle_delta_y); let cfl = cfl && cfl_allowed_px(bw, bh, fctx); let uv_mode = if !has_chroma { DC_PRED } else if cfl { dec.symbol(&mut cdfs.uv_mode_cfl[mode]) } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) }; istep!("uv_mode", uv_mode as i32); let alpha = if has_chroma && cfl && uv_mode == UV_CFL_PRED { Some(read_cfl_alphas(dec, cdfs)) } else { None }; let angle_delta_uv = if has_chroma && (V_PRED..=D67_PRED).contains(&uv_mode) { read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) } else { 0 }; istep!("angle_uv", angle_delta_uv); let mut palette_y_pending: Option<(usize, [u16; 8])> = None; let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; if allow_screen_content_tools && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) { let (mode_ctx, cache) = palette.unwrap_or((0, &[])); let use_palette_y = mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; if use_palette_y { let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); let colors = read_palette_colors_y(dec, n, cache, fctx); palette_y_pending = Some((n, colors)); } let palette_uv_mode_ctx = usize::from(use_palette_y); if has_chroma && uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache, fctx); palette_uv_pending = Some((n, u_colors, v_colors)); } } let mut filter_intra = None; if mode == DC_PRED && enable_filter_intra && palette_y_pending.is_none() && let Some(class) = filter_intra_size_class_rect(bw, bh) { let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; istep!("use_filter_intra", use_filter_intra as i32); if use_filter_intra { let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); istep!("filter_intra_mode", fi_mode as i32); filter_intra = Some(fi_mode); } } let palette_y = palette_y_pending.map(|(n, colors)| { let map = decode_color_index_map_wh( dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false, ); PaletteY { size: n, colors, map, } }); let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); let on = palette_onscreen_uv( (bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx)), ); let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); PaletteUv { size: n, u_colors, v_colors, map, } }); Ok(( skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, None, )) } pub(super) fn record_strip_palette( n: &mut Neighbours, mi: (usize, usize), bw: usize, bh: usize, y_size: usize, y_colors: [u16; 8], uv_size: usize, uv_colors: [u16; 8], ) { let (r, c) = mi; { for cell in 0..(bw / MI).max(1) { if let Some(e) = n.above_palette_size.get_mut(c + cell) { *e = y_size as u8; n.above_palette_uv_size[c + cell] = uv_size as u8; if y_size > 0 { n.above_palette_colors[c + cell] = y_colors; } if uv_size > 0 { n.above_palette_uv_colors[c + cell] = uv_colors; } } } } { for cell in 0..(bh / MI).max(1) { if n.left_palette_size.get(r + cell).is_none() { break; } n.left_palette_size[r + cell] = y_size as u8; n.left_palette_uv_size[r + cell] = uv_size as u8; if y_size > 0 { n.left_palette_colors[r + cell] = y_colors; } if uv_size > 0 { n.left_palette_uv_colors[r + cell] = uv_colors; } } } } pub(super) fn cfl_ac_q3_rect( y: &PlaneBuf<'_>, px: usize, py: usize, bw: usize, bh: usize, ) -> Vec { cfl_ac_q3_at(px, py, bw, bh, |lx, ly| { i32::from(y.data[ly * y.width + lx]) }) } pub(super) struct RectStripModes { pub(super) skip: bool, pub(super) mode: usize, pub(super) angle_delta_y: i32, pub(super) uv_predict_mode: usize, pub(super) angle_delta_uv: i32, pub(super) alpha: Option<(i32, i32)>, pub(super) filter_intra: Option, pub(super) smooth_neighbor: bool, pub(super) smooth_neighbor_uv: bool, pub(super) palette_y: Option>, pub(super) palette_uv: Option<(Vec, Vec)>, } pub(super) fn depth_to_tx_wh( bw: usize, bh: usize, depth: usize, fctx: &crate::decode::FrameCtx, ) -> (usize, usize) { if lossless(fctx) { return (4, 4); } let (mut w, mut h) = (bw.min(64), bh.min(64)); for _ in 0..depth { match w.cmp(&h) { std::cmp::Ordering::Greater => w /= 2, std::cmp::Ordering::Less => h /= 2, std::cmp::Ordering::Equal => { w /= 2; h /= 2; } } } (w, h) } fn chroma422_pair_plane( ss_x: bool, ss_y: bool, mi_col: usize, bw: usize, bh: usize, ) -> Option<(usize, usize, usize, usize)> { if ss_x && !ss_y && (bw >> 1) == 2 { Some((4, bh, ((mi_col & !1) * MI) >> 1, mi_col & !1)) } else { None } } pub(super) fn decode_rect_split( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, at_mi: (usize, usize), bw: usize, bh: usize, tx_w: usize, tx_h: usize, m: &RectStripModes, y: &mut PlaneBuf<'static>, u: &mut PlaneBuf<'static>, v: &mut PlaneBuf<'static>, reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, ) -> Result<()> { let (mi_r, mi_c) = at_mi; let (px, py) = (mi_c * MI, mi_r * MI); let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let chroma_422_oob = ss_x(fctx) == 1 && ss_y(fctx) == 0 && matches!( (bw, bh), (8, 16) | (16, 32) | (32, 64) | (64, 128) | (8, 32) | (16, 64) ); let chroma422_pair = chroma422_pair_plane(ss_x(fctx) == 1, ss_y(fctx) == 0, mi_c, bw, bh); let chroma_422_pair_wide = chroma422_pair.is_some(); let (chroma_block_w, chroma_block_h, chroma_x) = chroma422_pair.map_or((chroma_w, chroma_h, cpx), |(w, h, x, _)| (w, h, x)); let chroma: Option<(TxbSet, &[u16])> = match (chroma_w, chroma_h) { (16, 8) => Some((TxbSet::ChromaRect16x8, &SCAN_16X8[..])), (8, 16) => Some((TxbSet::ChromaRect16x8, &SCAN_8X16[..])), (32, 16) => Some((TxbSet::ChromaRect32x16, &SCAN_32X16[..])), (16, 32) => Some((TxbSet::ChromaRect32x16, &SCAN_16X32[..])), (32, 8) => Some((TxbSet::Chroma16, &SCAN_32X8[..])), (8, 32) => Some((TxbSet::Chroma16, &SCAN_8X32[..])), (16, 4) => Some((TxbSet::Chroma8, &SCAN_16X4[..])), (4, 16) => Some((TxbSet::Chroma8, &SCAN_4X16[..])), (8, 4) => Some((TxbSet::ChromaRect8x4, &SCAN_8X4[..])), (4, 8) => Some((TxbSet::ChromaRect8x4, &SCAN_4X8[..])), _ => None, }; let chroma_tiled = chroma_422_pair_wide || chroma_422_oob || matches!( (chroma_w, chroma_h), (64, 32) | (32, 64) | (64, 16) | (16, 64) | (32, 32) | (16, 16) | (8, 8) ); if !m.skip && chroma.is_none() && !chroma_tiled { return Err(unsupported( "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", )); } let luma_rect: Option<(TxbSet, &[u16])> = match (tx_w, tx_h) { (w, h) if w == h => None, (16, 8) | (8, 16) => Some(( if reduced_tx_set { TxbSet::LumaRect16x8 } else { TxbSet::LumaRect16x8Set1 }, if tx_w == 16 { &SCAN_16X8[..] } else { &SCAN_8X16[..] }, )), (32, 16) => Some((TxbSet::LumaRect32x16, &SCAN_32X16[..])), (16, 32) => Some((TxbSet::LumaRect32x16, &SCAN_16X32[..])), (64, 32) | (32, 64) => None, _ => { return Err(unsupported(format!( "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma \ coefficient tables for that shape here)" ))); } }; let luma_64corner = tx_w != tx_h; let luma_set = txbset_for(tx_w.min(tx_h), reduced_tx_set); let luma_scan = default_scan(tx_w.min(tx_h)); let zero_tu = vec![0i32; tx_w * tx_h]; let block_reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); for tu_row in 0..bh / tx_h { for tu_col in 0..bw / tx_w { let tu_mi = (mi_r + tu_row * (tx_h / MI), mi_c + tu_col * (tx_w / MI)); let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); let (col_off, row_off) = (tu_col * tx_w, tu_row * tx_h); if tu_px >= y.true_width || tu_py >= y.true_height { continue; } let tu_reach = Reach::of_tu(bw, bh, col_off, row_off, tx_w, tx_h, block_reach); if let Some(buf) = &m.palette_y { let tu_buf: Vec = (0..tx_h) .flat_map(|i| { let base = (row_off + i) * bw + col_off; buf[base..base + tx_w].iter().copied() }) .collect(); *fctx.palette_pred.borrow_mut() = Some(tu_buf); } if m.skip { push_intra_rect( y, u, v, 0, tu_px, tu_py, tx_w, tx_h, m.mode, m.angle_delta_y, tu_reach, &zero_tu, None, m.filter_intra, m.smooth_neighbor, fctx, ); neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); } else if luma_64corner && luma_rect.is_none() { let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; let mut coding = cdfs.txb(TxbSet::Luma64, fi_tx_row(m.mode, m.filter_intra)); let coded_side = coding.side; let (corner, tu_tx_type) = read_coeffs( dec, &mut coding, default_scan(TX32), coded_side, coded_side, 0, dc_sign_ctx(tu_around.2), TxType::DctDct, Some((tx_w, tx_h)), )?; let (cw, ch) = (tx_w.min(TX32), tx_h.min(TX32)); let mut tu_grid = vec![0i32; tx_w * tx_h]; for row in 0..ch { tu_grid[row * tx_w..][..cw].copy_from_slice(&corner[row * cw..][..cw]); } let residual = dequant_and_inverse_typed_wh( &tu_grid, tx_w, tx_h, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, tu_tx_type, block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), ); push_intra_rect( y, u, v, 0, tu_px, tu_py, tx_w, tx_h, m.mode, m.angle_delta_y, tu_reach, &residual, None, m.filter_intra, m.smooth_neighbor, fctx, ); neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); } else if let Some((rect_set, rect_scan)) = luma_rect { let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; let tu_skip_ctx = if tx_w == bw && tx_h == bh { 0 } else { neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI) }; let mut coding = cdfs.txb(rect_set, fi_tx_row(m.mode, m.filter_intra)); let (tu_grid, tu_tx_type) = read_coeffs( dec, &mut coding, rect_scan, tx_w, tx_h, tu_skip_ctx, dc_sign_ctx(tu_around.2), TxType::DctDct, None, )?; let residual = dequant_and_inverse_typed_wh( &tu_grid, tx_w, tx_h, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, tu_tx_type, block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), ); push_intra_rect( y, u, v, 0, tu_px, tu_py, tx_w, tx_h, m.mode, m.angle_delta_y, tu_reach, &residual, None, m.filter_intra, m.smooth_neighbor, fctx, ); neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); } else { let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); let tu_grid = read_plane( dec, cdfs, luma_set, luma_scan, 0, tu_around, m.mode, m.mode, m.angle_delta_y, tu_reach, y, u, v, tu_px, tu_py, tx_w, tx_w, None, m.filter_intra, Some(tu_skip_ctx), m.smooth_neighbor, fctx, )?; neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); } } } let mut ll_chroma_units: ChromaUnits = Vec::new(); let mut tiled_chroma_units: ChromaUnits = Vec::new(); let ac = m.alpha.map(|_| { if chroma_422_pair_wide { cfl_src_rect((mi_c & !1) * MI, py, 8, bh) } else { cfl_src_rect(px, py, bw, bh) } }); let reach = if chroma_422_pair_wide { Reach::of_rect(8, bh, (mi_c & !1) * MI, py, y.width, y.height, fctx) } else { block_reach }; let (u_grid, v_grid) = if mono(fctx) { let zero = Grid::Zero(chroma_w * chroma_h); (zero.clone(), zero) } else if m.skip { let zero = Grid::Zero(chroma_block_w * chroma_block_h); if chroma_422_pair_wide || chroma_422_oob { let (uw, uh) = (4usize, 8usize); for plane_idx in 1..=2 { if let Some((ub, vb)) = &m.palette_uv { let buf = if plane_idx == 1 { ub } else { vb }; *fctx.palette_pred.borrow_mut() = Some(buf.clone()); } for cu_row in 0..chroma_block_h / uh { for cu_col in 0..chroma_block_w / uw { push_intra_rect( y, u, v, plane_idx, chroma_x + cu_col * uw, cpy + cu_row * uh, uw, uh, m.uv_predict_mode, m.angle_delta_uv, reach, &ZERO_RESIDUAL[..uw * uh], m.alpha .zip(ac) .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)), None, m.smooth_neighbor_uv, fctx, ); } } } } else { if let Some((ub, _)) = &m.palette_uv { *fctx.palette_pred.borrow_mut() = Some(ub.clone()); } push_intra_rect( y, u, v, 1, cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, m.alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &m.palette_uv { *fctx.palette_pred.borrow_mut() = Some(vb.clone()); } push_intra_rect( y, u, v, 2, cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, m.alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx, ); } (zero.clone(), zero) } else if lossless(fctx) { let scan4 = default_scan(4); let offset = (chroma_w > 4 || chroma_h > 4).then_some(3); let (cw_n, ch_n) = (chroma_w / 4, chroma_h / 4); let span_x = 4 << ss_x(fctx); let span_y = 4 << ss_y(fctx); let mut last = [Grid::Zero(16), Grid::Zero(16)]; for plane_idx in 1..=2 { for cu_row in 0..ch_n { for cu_col in 0..cw_n { if cpx + cu_col * 4 >= u.true_width || cpy + cu_row * 4 >= u.true_height { continue; } let cu_mi = (mi_r + cu_row * (span_y / MI), mi_c + cu_col * (span_x / MI)); let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { neighbours.around_mi_422_chroma(cu_mi, span_x, span_y) } else { neighbours.around_mi_rect(cu_mi, span_x, span_y) }; let cu_reach = tu_reach( bw, bh, cu_col * span_x, cu_row * span_y, span_x, block_reach, ); let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4); if let Some((ub, vb)) = &m.palette_uv { let pbuf = if plane_idx == 1 { ub } else { vb }; *fctx.palette_pred.borrow_mut() = Some(palette_window(pbuf, chroma_w, cu_col * 4, cu_row * 4, 4, 4)); } let grid = read_plane( dec, cdfs, TxbSet::Chroma4, scan4, plane_idx, cu_around[plane_idx], m.mode, m.uv_predict_mode, m.angle_delta_uv, cu_reach, y, u, v, cu_x, cu_y, 4, 4, m.alpha .zip(ac) .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)), None, offset, m.smooth_neighbor_uv, fctx, )?; neighbours.record_mi_chroma(cu_mi, span_x, span_y, plane_idx, &grid); ll_chroma_units.push((cu_mi, plane_idx, grid.clone(), span_x, span_y)); last[plane_idx - 1] = grid; } } } let [ug, vg] = last; (ug, vg) } else if chroma_tiled { let (uw, uh, set, scan): (usize, usize, TxbSet, &[u16]) = if chroma_422_pair_wide || chroma_422_oob { (4, 8, TxbSet::ChromaRect8x4, &SCAN_4X8) } else { match (chroma_w, chroma_h) { (64, 32) | (32, 64) | (32, 32) => (32, 32, TxbSet::Chroma32, default_scan(32)), (64, 16) => (32, 16, TxbSet::ChromaRect32x16, &SCAN_32X16), (16, 64) => (16, 32, TxbSet::ChromaRect32x16, &SCAN_16X32), (16, 16) => (16, 16, TxbSet::Chroma16, default_scan(16)), (8, 8) => (8, 8, TxbSet::Chroma8, default_scan(8)), _ => unreachable!("chroma_tiled"), } }; let (nw, nh) = (chroma_block_w / uw, chroma_block_h / uh); let bigger = chroma_422_pair_wide || chroma_w * chroma_h > uw * uh; let default_tx = if uw.max(uh) >= 32 { TxType::DctDct } else { default_intra_tx_type(m.uv_predict_mode as u8) }; let mut last = [Grid::Zero(uw * uh), Grid::Zero(uw * uh)]; for plane_idx in 1..=2 { for cu_row in 0..nh { for cu_col in 0..nw { let luma_span_x = uw << ss_x(fctx); let luma_span_y = uh << ss_y(fctx); let cu_mi = ( mi_r + cu_row * (luma_span_y / MI), if chroma_422_pair_wide { mi_c & !1 } else { mi_c } + cu_col * (luma_span_x / MI), ); let cu_around = if chroma_422_oob || (ss_x(fctx) == 1 && ss_y(fctx) == 0) { neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y) } else { neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y) }; let (cu_x, cu_y) = (chroma_x + cu_col * uw, cpy + cu_row * uh); let cfl = m.alpha.map(|(au, av)| { let alpha = if plane_idx == 1 { au } else { av }; ( alpha, if chroma_422_pair_wide { cfl_src_rect( (mi_c & !1) * MI + cu_col * luma_span_x, py + cu_row * luma_span_y, luma_span_x, luma_span_y, ) } else { cfl_src_rect( px + cu_col * luma_span_x, py + cu_row * luma_span_y, luma_span_x, luma_span_y, ) }, ) }); if let Some((ub, vb)) = &m.palette_uv { let buf = if plane_idx == 1 { ub } else { vb }; *fctx.palette_pred.borrow_mut() = Some(palette_window( buf, chroma_block_w, cu_col * uw, cu_row * uh, uw, uh, )); } let cu_reach = tu_reach_rect( bw, bh, (cu_col * uw) << ss_x(fctx), (cu_row * uh) << ss_y(fctx), uw << ss_x(fctx), uh << ss_y(fctx), reach, ); let levels = if uw == uh { read_plane( dec, cdfs, set, scan, plane_idx, cu_around[plane_idx], m.mode, m.uv_predict_mode, m.angle_delta_uv, cu_reach, y, u, v, cu_x, cu_y, uw, uw, cfl, None, bigger.then_some(3), m.smooth_neighbor_uv, fctx, )? } else { let skip_ctx = usize::from(cu_around[plane_idx].0) + usize::from(cu_around[plane_idx].1) + usize::from(bigger) * 3; let mut coding = cdfs.txb(set, m.uv_predict_mode); let (levels, tx_type) = read_coeffs( dec, &mut coding, scan, uw, uh, skip_ctx, dc_sign_ctx(cu_around[plane_idx].2), default_tx, None, )?; let residual = dequant_and_inverse_typed_wh( &levels, uw, uh, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, tx_type, block_iqmatrix(fctx, plane_idx, uw, uh, tx_type), ); push_intra_rect( y, u, v, plane_idx, cu_x, cu_y, uw, uh, m.uv_predict_mode, m.angle_delta_uv, cu_reach, &residual, cfl, None, m.smooth_neighbor_uv, fctx, ); levels }; neighbours.record_mi_chroma( cu_mi, luma_span_x, luma_span_y, plane_idx, &levels, ); tiled_chroma_units.push(( cu_mi, plane_idx, levels.clone(), luma_span_x, luma_span_y, )); last[plane_idx - 1] = levels; } } } (last[0].clone(), last[1].clone()) } else { let (chroma_set, chroma_scan) = chroma.ok_or_else(|| Error::corrupt("refused above when None"))?; let default_tx = if chroma_w.max(chroma_h) >= 32 { TxType::DctDct } else { default_intra_tx_type(m.uv_predict_mode as u8) }; let mut around = neighbours.around_mi_rect(at_mi, bw, bh); if ss_x(fctx) == 1 && ss_y(fctx) == 0 { let c = neighbours.around_mi_422_chroma(at_mi, bw, bh); around[1] = c[1]; around[2] = c[2]; } let mut grids: Vec = Vec::with_capacity(2); for plane_idx in 1..=2 { let skip_ctx = usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); let mut coding = cdfs.txb(chroma_set, m.uv_predict_mode); let (levels, tx_type) = read_coeffs( dec, &mut coding, chroma_scan, chroma_w, chroma_h, skip_ctx, dc_sign_ctx(around[plane_idx].2), default_tx, None, )?; let residual = dequant_and_inverse_typed_wh( &levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, tx_type, block_iqmatrix(fctx, plane_idx, chroma_w, chroma_h, tx_type), ); let cfl = m .alpha .zip(ac) .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); if let Some((ub, vb)) = &m.palette_uv { let buf = if plane_idx == 1 { ub } else { vb }; *fctx.palette_pred.borrow_mut() = Some(buf.clone()); } push_intra_rect( y, u, v, plane_idx, cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &residual, cfl, None, m.smooth_neighbor_uv, fctx, ); grids.push(levels); } let mut it = grids.into_iter(); (it.next().unwrap(), it.next().unwrap()) }; neighbours.record_split_luma_rect_mi( at_mi, bw, bh, m.mode, m.uv_predict_mode, [&u_grid, &v_grid], ); for (cu_mi, plane, unit, span_w, span_h) in &ll_chroma_units { neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); } for (cu_mi, plane, unit, span_w, span_h) in &tiled_chroma_units { neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); } neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, m.skip); neighbours.fill_lf_grid_rect( (mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx, ); Ok(()) } fn intra_in_inter_size_row(bw: usize, bh: usize) -> Option<(usize, usize)> { Some(match (bw.min(bh), bw.max(bh)) { (8, 16) => (1usize, 1usize), (16, 32) => (2, 2), (32, 64) => (3, 3), (8, 32) => (1, 2), (16, 64) => (2, 3), _ => return None, }) } #[test] fn every_intra_in_inter_shape_the_census_lists_has_a_size_group_row() { let mut checked = 0; for &(side, w, h) in &INTER_BLOCK_SHAPES { if (w, h) == (side, side) || w.min(h) < 8 || w.max(h) > 64 { continue; } assert!( intra_in_inter_size_row(w, h).is_some(), "the inter path decodes {w}x{h} blocks, and an INTRA one of that shape would hit \ the \"no size-group/tx-category row\" refusal" ); checked += 1; } assert_eq!( checked, 10, "the block-shape census changed: {checked} rect shapes reach the intra-in-inter row \ lookup, not the 10 this claim was measured over" ); } pub(crate) fn motion_mode_cdf_row(write_w: usize, write_h: usize) -> Option { Some(match (write_w, write_h) { (64, 128) => 14, (128, 64) => 15, (128, 128) => 16, (w, h) if w == h => (w.trailing_zeros() - 3) as usize, (16, 32) => 4, (32, 16) => 5, (32, 64) => 6, (64, 32) => 7, (8, 32) => 8, (32, 8) => 9, (16, 64) => 10, (64, 16) => 11, (8, 16) => 12, (16, 8) => 13, _ => return None, }) } #[cfg(test)] pub(super) const BLOCK_SIZES_ALL_WH: [(usize, usize); 22] = [ (4, 4), (4, 8), (8, 4), (8, 8), (8, 16), (16, 8), (16, 16), (16, 32), (32, 16), (32, 32), (32, 64), (64, 32), (64, 64), (64, 128), (128, 64), (128, 128), (4, 16), (16, 4), (8, 32), (32, 8), (16, 64), (64, 16), ]; #[test] fn every_shape_that_allows_motion_variation_has_a_motion_mode_cdf_row() { let mut rows = std::collections::BTreeSet::new(); let mut excluded = 0; for (w, h) in BLOCK_SIZES_ALL_WH { if w.min(h) < 8 { excluded += 1; assert!( w == 4 || h == 4, "{w}x{h} is not one of the sub-8 shapes is_motion_variation_allowed_bsize rejects" ); continue; } let row = motion_mode_cdf_row(w, h).unwrap_or_else(|| { panic!( "a {w}x{h} block passes is_motion_variation_allowed_bsize and reads a motion_mode \ symbol, but has no CDF row -- the refusal is a real gap, not a dead arm" ) }); assert!( row < 17, "{w}x{h} maps to motion_mode row {row}, outside the 17-row table" ); assert!(rows.insert(row), "{w}x{h} shares motion_mode row {row}"); } assert_eq!(excluded, 5, "the sub-8 footprint set changed"); assert_eq!( rows.len(), 17, "17 eligible footprints must fill all 17 rows of the motion_mode table" ); } #[test] fn chroma422_pair_plane_matches_the_ss_size_lookup_cells() { let ss10_invalid: &[(&str, bool)] = &[ ("4x4", false), ("4x8", true), ("8x4", false), ("8x8", false), ("8x16", true), ("16x8", false), ("16x16", false), ("16x32", true), ("32x16", false), ("32x32", false), ("32x64", true), ("64x32", false), ("64x64", false), ("64x128", true), ("128x64", false), ("128x128", false), ("4x16", true), ("16x4", false), ("8x32", true), ("32x8", false), ("16x64", true), ("64x16", false), ]; for (footprint, invalid10) in ss10_invalid { let (w, h) = footprint.split_once('x').unwrap(); let (bw, bh) = (w.parse::().unwrap(), h.parse::().unwrap()); if (bw, bh) == (4, 4) { continue; } for (ss_x, ss_y) in [(false, false), (true, true)] { assert!( chroma422_pair_plane(ss_x, ss_y, 7, bw, bh).is_none(), "{bw}x{bh} at ss ({ss_x}, {ss_y}) must keep its own plane block" ); } let pair = chroma422_pair_plane(true, false, 7, bw, bh); assert_eq!( pair.is_some(), *invalid10 && bw == 4, "{bw}x{bh} at ss (1, 0): pair routing disagrees with ss_size_lookup" ); if let Some((block_w, block_h, x, ctx_col)) = pair { assert_eq!((block_w, block_h), (4, bh), "{bw}x{bh} pair plane shape"); assert_eq!(x, ((7 & !1) * MI) >> 1, "{bw}x{bh} floored write anchor"); assert_eq!(ctx_col, 6, "{bw}x{bh} entropy cell is the floored column"); let (x_even, col_even) = match chroma422_pair_plane(true, false, 6, bw, bh) { Some((_, _, x, c)) => (x, c), None => panic!("{bw}x{bh} lost its pair arm at an even mi_col"), }; assert_eq!( (x_even, col_even), (12, 6), "even column keeps its own cell" ); } } for (bw, bh) in [ (8usize, 16), (16, 32), (32, 64), (64, 128), (8, 32), (16, 64), ] { assert!(chroma422_pair_plane(true, false, 7, bw, bh).is_none()); } } pub(super) fn decode_intra_rect_in_inter( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, at: (usize, usize), bw: usize, bh: usize, skip: bool, allow_screen_content_tools: bool, strip16: Option<(bool, bool)>, y: &mut PlaneBuf<'static>, u: &mut PlaneBuf<'static>, v: &mut PlaneBuf<'static>, base_q_idx: u8, fctx: &crate::decode::FrameCtx, ) -> Result<()> { let (mi_r, mi_c) = at; let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); if let Some((horz, has_chroma)) = strip16 { debug_assert_eq!((bw, bh), if horz { (16, 4) } else { (4, 16) }); fctx.intra_in_inter_mode.set(Some((0, skip))); let res = decode_rect4_16_strip( dec, cdfs, neighbours, (mi_r, mi_c), horz, has_chroma, None, y, u, v, fctx.enable_filter_intra_inter.get(), allow_screen_content_tools, base_q_idx, fctx.tx_select_inter.get(), fctx.reduced_tx_set_inter.get(), fctx, ); fctx.intra_in_inter_mode.set(None); let (mode, uv, (tx_w, tx_h)) = res?; txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); for i in 0..bw / MI { if let Some(cell) = neighbours.above_txfm.get_mut(mi_c + i) { *cell = tx_w as u8; } } for i in 0..bh / MI { if let Some(cell) = neighbours.left_txfm.get_mut(mi_r + i) { *cell = tx_h as u8; } } neighbours.above_mode[c] = mode; neighbours.left_mode[r] = mode; if let Some(uvm) = uv { neighbours.above_uv_mode[c] = uvm; neighbours.left_uv_mode[r] = uvm; } neighbours.above_side[c] = bw; neighbours.left_side[r] = bh; return Ok(()); } if bw.max(bh) == 128 { fctx.intra_in_inter_mode.set(Some((3, skip))); let res = decode_block_128rect( dec, cdfs, neighbours, (r, c), bw, bh, y, u, v, base_q_idx, fctx.enable_filter_intra_inter.get(), allow_screen_content_tools, fctx.tx_select_inter.get(), fctx.reduced_tx_set_inter.get(), fctx, ); fctx.intra_in_inter_mode.set(None); return res; } let Some((size_group, tx_cat)) = intra_in_inter_size_row(bw, bh) else { return Err(unsupported(format!( "an intra-coded {bw}x{bh} block on the inter block path (no size-group/tx-category row for that shape here)" ))); }; if bw.max(bh) == 4 * bw.min(bh) { debug_assert_eq!(tx_cat, if bw.max(bh) == 64 { 3 } else { 2 }); fctx.intra_in_inter_mode.set(Some((size_group, skip))); let res = if bw.max(bh) == 64 { decode_block_rect64( dec, cdfs, neighbours, (mi_r / (SUB / MI), mi_c / (SUB / MI)), bw, bh, y, u, v, fctx.enable_filter_intra_inter.get(), allow_screen_content_tools, base_q_idx, fctx.tx_select_inter.get(), fctx.reduced_tx_set_inter.get(), fctx, ) } else { decode_block_rect4( dec, cdfs, neighbours, (mi_r, mi_c), bw, bh, None, y, u, v, fctx.enable_filter_intra_inter.get(), allow_screen_content_tools, base_q_idx, fctx.tx_select_inter.get(), fctx, ) .map(|_| ()) }; fctx.intra_in_inter_mode.set(None); res?; return Ok(()); } let mode = dec.symbol(&mut cdfs.y_mode[size_group]); if mode >= 13 { return Err(unsupported( "an intra mode this decoder does not code (round 2)", )); } let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) } else { 0 }; let has_chroma = !mono(fctx); let cfl_allowed = has_chroma && cfl_allowed_px(bw, bh, fctx); let uv_mode = if !has_chroma { DC_PRED } else if cfl_allowed { dec.symbol(&mut cdfs.uv_mode_cfl[mode]) } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) }; let alpha = if cfl_allowed && uv_mode == UV_CFL_PRED { Some(read_cfl_alphas(dec, cdfs)) } else { None }; let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) } else { 0 }; let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((mi_r, mi_c)); let palette_uv_cache = neighbours.palette_uv_cache_mi((mi_r, mi_c)); let mut palette_y_pending: Option<(usize, [u16; 8])> = None; let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; if allow_screen_content_tools && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) { let use_palette_y = mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0; if use_palette_y { let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache, fctx))); } let use_palette_uv = has_chroma && uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; if use_palette_uv { let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache, fctx); palette_uv_pending = Some((n, u_colors, v_colors)); } } let mut filter_intra = None; if mode == DC_PRED && fctx.enable_filter_intra_inter.get() && palette_y_pending.is_none() && let Some(class) = filter_intra_size_class_rect(bw, bh) && dec.symbol(&mut cdfs.filter_intra[class]) != 0 { filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode)); } let palette_y = palette_y_pending.map(|(n, colors)| { let map = decode_color_index_map_wh( dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false, ); PaletteY { size: n, colors, map, } }); let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); let on = palette_onscreen_uv( (bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx)), ); let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); PaletteUv { size: n, u_colors, v_colors, map, } }); let coarse_pair = neighbours.modes_above_left(r, c); let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left_mi(mi_r, mi_c, coarse_pair); let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx); let depth = if fctx.tx_select_inter.get() && !lossless(fctx) { let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64)); match tx_cat { 1 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), 2 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]), _ => dec.symbol(&mut cdfs.tx_size_cat3[ctx]), } } else { 0 }; let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); let modes = RectStripModes { skip, mode, angle_delta_y, uv_predict_mode, angle_delta_uv, alpha, filter_intra, smooth_neighbor, smooth_neighbor_uv, palette_y: palette_y .as_ref() .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), palette_uv: palette_uv.as_ref().map(|p| { ( p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), ) }), }; decode_rect_split( dec, cdfs, neighbours, (mi_r, mi_c), bw, bh, tx_w, tx_h, &modes, y, u, v, fctx.reduced_tx_set_inter.get(), fctx, )?; for i in 0..bw / MI { if let Some(cell) = neighbours.above_txfm.get_mut(mi_c + i) { *cell = tx_w as u8; } } for i in 0..bh / MI { if let Some(cell) = neighbours.left_txfm.get_mut(mi_r + i) { *cell = tx_h as u8; } } record_strip_palette( neighbours, (mi_r, mi_c), bw, bh, palette_y.as_ref().map_or(0, |p| p.size), palette_y.as_ref().map_or([0u16; 8], |p| p.colors), palette_uv.as_ref().map_or(0, |p| p.size), palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors), ); Ok(()) } pub(super) fn decode_intrabc_rect( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, (mi_r, mi_c): (usize, usize), (bw, bh): (usize, usize), skip: bool, (dv_row, dv_col): (i32, i32), y: &mut PlaneBuf<'static>, u: &mut PlaneBuf<'static>, v: &mut PlaneBuf<'static>, base_q_idx: u8, fctx: &crate::decode::FrameCtx, ) -> Result<()> { let (px, py) = (mi_c * MI, mi_r * MI); let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); let (cw, ch) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let side = bw.max(bh); let cside = cw.max(ch); let mi_cols = 2 * y.true_width.div_ceil(8); let mi_rows = 2 * y.true_height.div_ceil(8); let leaves = read_block_tx_size_rect( dec, cdfs, neighbours, (mi_r, mi_c), (bw, bh), (mi_cols, mi_rows), skip, fctx, )?; let mut pred_y_dense = vec![0u16; bw * bh]; mc::predict_with_filter( &y.data, y.width, y.true_width, y.true_height, mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, ); let mut pred_y = vec![0u16; side * side]; for row in 0..bh { pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); } let mut pred_u_dense = vec![0u16; cw * ch]; mc::predict_with_filter( &u.data, u.width, u.true_width, u.true_height, mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u_dense, fctx, ); let mut pred_u = vec![0u16; cside * cside]; for row in 0..ch { pred_u[row * cside..][..cw].copy_from_slice(&pred_u_dense[row * cw..][..cw]); } let mut pred_v_dense = vec![0u16; cw * ch]; mc::predict_with_filter( &v.data, v.width, v.true_width, v.true_height, mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v_dense, fctx, ); let mut pred_v = vec![0u16; cside * cside]; for row in 0..ch { pred_v[row * cside..][..cw].copy_from_slice(&pred_v_dense[row * cw..][..cw]); } let reduced = fctx.reduced_tx_set_inter.get(); let luma_tx_type: TxType; let (luma_grid, u_grid, v_grid); let mut ll_chroma: Option<(Vec, Vec)> = None; if skip { push_mc_rect( y, u, v, 0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * side], fctx, ); push_mc_rect( y, u, v, 1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * cside], fctx, ); push_mc_rect( y, u, v, 2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * cside], fctx, ); if let Some(ls) = leaves.as_ref() { for &(row, col, tw, th) in ls { neighbours.record_mi_luma_rect( (mi_r + row, mi_c + col), tw, th, &ZERO_RESIDUAL[..tw * th], ); } } luma_tx_type = TxType::DctDct; luma_grid = Grid::Zero(side * side); u_grid = Grid::Zero(cside * cside); v_grid = Grid::Zero(cside * cside); } else { match leaves.as_ref() { None => { let mut around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); if ss_x(fctx) == 1 && ss_y(fctx) == 0 { let c = neighbours.around_mi_422_chroma((mi_r, mi_c), bw, bh); around[1] = c[1]; around[2] = c[2]; } let (grid, tx_type) = read_inter_plane_rect( dec, cdfs, rect_inter_luma_set(bw, bh, fctx)?, (bw, bh), side, 0, around[0], 0, y, u, v, px, py, Pred::Inline(&pred_y, side), None, None, fctx, )?; luma_tx_type = tx_type; luma_grid = embed_grid_stride(&grid, bw, bh, side); let (ug, _) = read_inter_plane_rect( dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, y, u, v, cpx, cpy, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, )?; u_grid = embed_grid_stride(&ug, cw, ch, cside); let (vg, _) = read_inter_plane_rect( dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, y, u, v, cpx, cpy, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, )?; v_grid = embed_grid_stride(&vg, cw, ch, cside); } Some(leaves) => { for &(row, col, tw, th) in leaves.iter() { neighbours.fill_lf_grid_leaf_luma( (mi_r + row, mi_c + col), tw / MI, th / MI, tw as u8, th as u8, ); } let mut first_tx_type = TxType::DctDct; for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { let tu_mi = (mi_r + row, mi_c + col); let (tu_px, tu_py) = (px + col * MI, py + row * MI); let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); let (tu_grid, tu_tx_type) = if tw == th { let scan = default_scan(tw.min(32)); let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tw / MI); read_inter_plane( dec, cdfs, inter_txbset_for(tw, reduced), scan, 0, neighbours.around_mi(tu_mi, tw)[0], 0, y, u, v, tu_px, tu_py, tw, base_q_idx, tu_pred, None, Some(tu_skip_ctx), fctx, )? } else { let tu_skip_ctx = neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); read_inter_plane_rect( dec, cdfs, rect_inter_luma_set(tw, th, fctx)?, (tw, th), side, 0, neighbours.around_mi_rect(tu_mi, tw, th)[0], 0, y, u, v, tu_px, tu_py, tu_pred, None, Some(tu_skip_ctx), fctx, )? }; if idx == 0 { first_tx_type = tu_tx_type; } neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); } luma_tx_type = first_tx_type; let mut around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); if ss_x(fctx) == 1 && ss_y(fctx) == 0 { let c = neighbours.around_mi_422_chroma((mi_r, mi_c), bw, bh); around[1] = c[1]; around[2] = c[2]; } if lossless(fctx) { let mut uo = vec![0i32; cside * cside]; let mut vo = vec![0i32; cside * cside]; read_inter_chroma_lossless( y, u, v, dec, cdfs, neighbours, Pred::Inline(&pred_u, cside), Pred::Inline(&pred_v, cside), (mi_r, mi_c), (cpx, cpy), (0, 0), (cw, ch), (cw, ch), cside, 0, base_q_idx, &mut uo, &mut vo, fctx, )?; u_grid = embed_grid_stride(&Grid::Own(uo.clone()), cw, ch, cside); v_grid = embed_grid_stride(&Grid::Own(vo.clone()), cw, ch, cside); ll_chroma = Some((uo, vo)); luma_grid = Grid::Zero(side * side); } else { let (ug, _) = read_inter_plane_rect( dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, y, u, v, cpx, cpy, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, )?; u_grid = embed_grid_stride(&ug, cw, ch, cside); let (vg, _) = read_inter_plane_rect( dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, y, u, v, cpx, cpy, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, )?; v_grid = embed_grid_stride(&vg, cw, ch, cside); luma_grid = Grid::Zero(side * side); } } } } let _ = luma_tx_type; if leaves.is_some() { neighbours.record_split_luma_rect_mi( (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid, &v_grid], ); } else { neighbours.record_rect_mi( (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, &[&luma_grid, &u_grid, &v_grid], ); } if let Some((uo, vo)) = ll_chroma { for (plane_idx, comp) in [(1usize, &uo), (2, &vo)] { for ur in 0..ch / 4 { for uc in 0..cw / 4 { let mut unit = Vec::with_capacity(16); for r in 0..4 { unit.extend_from_slice(&comp[(ur * 4 + r) * cside + uc * 4..][..4]); } neighbours.record_mi_chroma( ( mi_r + ur * ((4 << ss_y(fctx)) / MI), mi_c + uc * ((4 << ss_x(fctx)) / MI), ), 4 << ss_x(fctx), 4 << ss_y(fctx), plane_idx, &Grid::Own(unit), ); } } } } neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); neighbours.fill_lf_grid_rect_after_vartx( (mi_r, mi_c), bw / MI, bh / MI, bw.min(64) as u8, bh.min(64) as u8, 0, fctx, ); record_intrabc_mi_rect( mi_r, mi_c, bw / MI, bh / MI, Some((dv_row, dv_col)), neighbours, fctx, ); Ok(()) } pub(super) fn decode_intrabc_128rect( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, (mi_r, mi_c): (usize, usize), (bw, bh): (usize, usize), skip: bool, (dv_row, dv_col): (i32, i32), y: &mut PlaneBuf<'static>, u: &mut PlaneBuf<'static>, v: &mut PlaneBuf<'static>, base_q_idx: u8, fctx: &crate::decode::FrameCtx, ) -> Result<()> { let (px, py) = (mi_c * MI, mi_r * MI); let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); let (cw, ch) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let side = bw.max(bh); let cside = cw.max(ch); let mi_cols = 2 * y.true_width.div_ceil(8); let mi_rows = 2 * y.true_height.div_ceil(8); let leaves = read_block_tx_size_rect( dec, cdfs, neighbours, (mi_r, mi_c), (bw, bh), (mi_cols, mi_rows), skip, fctx, )?; let mut pred_y_dense = vec![0u16; bw * bh]; mc::predict_with_filter( &y.data, y.width, y.true_width, y.true_height, mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, ); let mut pred_y = vec![0u16; side * side]; for row in 0..bh { pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); } let mut pred_u_dense = vec![0u16; cw * ch]; mc::predict_with_filter( &u.data, u.width, u.true_width, u.true_height, mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u_dense, fctx, ); let mut pred_u = vec![0u16; cside * cside]; for row in 0..ch { pred_u[row * cside..][..cw].copy_from_slice(&pred_u_dense[row * cw..][..cw]); } let mut pred_v_dense = vec![0u16; cw * ch]; mc::predict_with_filter( &v.data, v.width, v.true_width, v.true_height, mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v_dense, fctx, ); let mut pred_v = vec![0u16; cside * cside]; for row in 0..ch { pred_v[row * cside..][..cw].copy_from_slice(&pred_v_dense[row * cw..][..cw]); } let (luma_grid, u_grid, v_grid); let mut mu_chroma = false; if skip { push_mc_rect( y, u, v, 0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * side], fctx, ); push_mc_rect( y, u, v, 1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * cside], fctx, ); push_mc_rect( y, u, v, 2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * cside], fctx, ); if let Some(ls) = leaves.as_ref() { for &(row, col, tw, th) in ls { neighbours.record_mi_luma_rect( (mi_r + row, mi_c + col), tw, th, &ZERO_RESIDUAL[..tw * th], ); } } luma_grid = Grid::Zero(side * side); u_grid = Grid::Zero(cside * cside); v_grid = Grid::Zero(cside * cside); } else { let leaves = leaves .ok_or_else(|| Error::corrupt("read_block_tx_size_rect returns None only for skip below 128 at this shape"))?; for &(row, col, tw, th) in leaves.iter() { neighbours.fill_lf_grid_leaf_luma( (mi_r + row, mi_c + col), tw / MI, th / MI, tw as u8, th as u8, ); } let reduced = fctx.reduced_tx_set_inter.get(); let mut u_units: Vec = Vec::new(); let mut v_units: Vec = Vec::new(); let mut leaf_tx_types: Vec<(usize, usize, usize, usize, TxType)> = Vec::new(); let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c)); let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r)); let (order, _mu_reordered) = mu_chunk_order(&leaves, side); for (pos, &idx) in order.iter().enumerate() { let (row, col, tw, th) = leaves[idx]; debug_assert_eq!( tw, th, "a square TX_64X64 tree root only yields square leaves" ); let tu_mi = (mi_r + row, mi_c + col); let (tu_px, tu_py) = (px + col * MI, py + row * MI); if tu_px >= y.true_width || tu_py >= y.true_height { continue; } let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); let scan = default_scan(tw.min(32)); let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tw / MI); let (tu_grid, tu_tx_type) = read_inter_plane( dec, cdfs, inter_txbset_for(tw, reduced), scan, 0, neighbours.around_mi(tu_mi, tw)[0], 0, y, u, v, tu_px, tu_py, tw, base_q_idx, tu_pred, None, Some(tu_skip_ctx), fctx, )?; leaf_tx_types.push((row, col, tw, th, tu_tx_type)); neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); let chunk = (row / 16, col / 16); let done = pos + 1 == order.len() || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16) != chunk; if done { let (cr, cc) = chunk; let cu_tx = 32usize; let chunk_chroma_w = (64usize) >> ss_x(fctx); let chunk_chroma_h = (64usize) >> ss_y(fctx); let unit_luma_w = cu_tx << ss_x(fctx); let unit_luma_h = cu_tx << ss_y(fctx); INTRABC_128RECT_CHUNK_SHAPE.with(|c| { c.set((chunk_chroma_w << 8) | chunk_chroma_h); }); let chunk_w_mi = 16.min(max_w_mi.saturating_sub(cc * 16)); let chunk_h_mi = 16.min(max_h_mi.saturating_sub(cr * 16)); let units_x = chunk_w_mi / (unit_luma_w / MI); let units_y = chunk_h_mi / (unit_luma_h / MI); if lossless(fctx) { read_inter_chroma_lossless( y, u, v, dec, cdfs, neighbours, Pred::Inline(&pred_u, cside), Pred::Inline(&pred_v, cside), (mi_r, mi_c), (cpx, cpy), (cc * chunk_chroma_w, cr * chunk_chroma_h), (chunk_chroma_w, chunk_chroma_h), (cw, ch), cside, 0, base_q_idx, &mut u_units, &mut v_units, fctx, )?; mu_chroma = true; continue; } let cu_scan = default_scan(cu_tx); for plane_idx in 1..3 { let src = if plane_idx == 1 { Pred::Inline(&pred_u, cside) } else { Pred::Inline(&pred_v, cside) }; for ur in 0..units_y { for uc in 0..units_x { let unit_mi = ( mi_r + cr * 16 + ur * (unit_luma_h / MI), mi_c + cc * 16 + uc * (unit_luma_w / MI), ); let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { neighbours.around_mi_422_chroma(unit_mi, unit_luma_w, unit_luma_h) } else { neighbours.around_mi_rect(unit_mi, unit_luma_w, unit_luma_h) }; let (cu_x, cu_y) = ( cpx + cc * chunk_chroma_w + uc * cu_tx, cpy + cr * chunk_chroma_h + ur * cu_tx, ); let unit_rel_mi = (unit_mi.0 - mi_r, unit_mi.1 - mi_c); let covering = covering_leaf_tx_type(&leaf_tx_types, unit_rel_mi); let cu_pred = src.offset( (cr * chunk_chroma_h + ur * cu_tx) * cside + cc * chunk_chroma_w + uc * cu_tx, ); let cu_grid = read_inter_plane( dec, cdfs, TxbSet::Chroma32, cu_scan, plane_idx, cu_around[plane_idx], 0, y, u, v, cu_x, cu_y, cu_tx, base_q_idx, cu_pred, Some(covering.unwrap_or(TxType::DctDct)), Some(3), fctx, )? .0; neighbours.record_mi_chroma( unit_mi, unit_luma_w, unit_luma_h, plane_idx, &cu_grid, ); mu_chroma = true; let dst = mu_units( if plane_idx == 1 { &mut u_units } else { &mut v_units }, cside * cside, ); for rr in 0..cu_tx { let start = (cr * chunk_chroma_h + ur * cu_tx + rr) * cside + cc * chunk_chroma_w + uc * cu_tx; dst[start..start + cu_tx] .copy_from_slice(&cu_grid[rr * cu_tx..][..cu_tx]); } } } } } } luma_grid = Grid::Zero(side * side); u_grid = if u_units.is_empty() { Grid::Zero(cside * cside) } else { Grid::Own(std::mem::take(&mut u_units)) }; v_grid = if v_units.is_empty() { Grid::Zero(cside * cside) } else { Grid::Own(std::mem::take(&mut v_units)) }; } if skip { neighbours.record_rect_mi( (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, &[&luma_grid[..], &u_grid[..], &v_grid[..]], ); } else { neighbours.record_split_luma_rect_mi( (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid[..], &v_grid[..]], ); } if mu_chroma { let cu = if lossless(fctx) { 4usize } else { 32usize }; let unit_luma_w = cu << ss_x(fctx); let unit_luma_h = cu << ss_y(fctx); let chunk_chroma_w = (64usize) >> ss_x(fctx); let chunk_chroma_h = (64usize) >> ss_y(fctx); if lossless(fctx) { let (rows, cols) = ((ch / chunk_chroma_h).max(1), (cw / chunk_chroma_w).max(1)); for cr in 0..rows { for cc in 0..cols { let unit_mi = ( mi_r + cr * (unit_luma_h / MI), mi_c + cc * (unit_luma_w / MI), ); for (plane_idx, grid) in [(1usize, &u_grid), (2usize, &v_grid)] { let mut unit = Vec::with_capacity(cu * cu); for rr in 0..cu { let start = (cr * chunk_chroma_h + rr) * cside + cc * chunk_chroma_w; unit.extend_from_slice(&grid[start..start + cu]); } neighbours.record_mi_chroma( unit_mi, unit_luma_w, unit_luma_h, plane_idx, &unit, ); } } } } else { let (ur, uc) = (chunk_chroma_h / cu, chunk_chroma_w / cu); let (rows, cols) = ((ch / chunk_chroma_h).max(1), (cw / chunk_chroma_w).max(1)); for cr in 0..rows { for cc in 0..cols { for kr in 0..ur { for kc in 0..uc { let unit_mi = ( mi_r + (cr * 64 + kr * unit_luma_h) / MI, mi_c + (cc * 64 + kc * unit_luma_w) / MI, ); let (cy, cx) = (cr * chunk_chroma_h + kr * cu, cc * chunk_chroma_w + kc * cu); for (plane_idx, grid) in [(1usize, &u_grid), (2usize, &v_grid)] { let mut unit = Vec::with_capacity(cu * cu); for rr in 0..cu { let start = (cy + rr) * cside + cx; unit.extend_from_slice(&grid[start..start + cu]); } neighbours.record_mi_chroma( unit_mi, unit_luma_w, unit_luma_h, plane_idx, &unit, ); } } } } } } } neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); neighbours.fill_lf_grid_rect_after_vartx( (mi_r, mi_c), bw / MI, bh / MI, bw.min(64) as u8, bh.min(64) as u8, 0, fctx, ); record_intrabc_mi_rect( mi_r, mi_c, bw / MI, bh / MI, Some((dv_row, dv_col)), neighbours, fctx, ); Ok(()) } pub(super) fn decode_block_rect( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, at: (usize, usize), bw: usize, bh: usize, y: &mut PlaneBuf<'static>, u: &mut PlaneBuf<'static>, v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, tx_select: bool, reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, ) -> Result<()> { let (r, c) = at; let (px, py) = (c * SUB, r * SUB); let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); let palette_uv_cache = neighbours.palette_uv_cache(at); let ( skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc, ) = read_intra_mode_rect( dec, cdfs, nb_above_mode, nb_left_mode, true, true, bw, bh, enable_filter_intra, neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), allow_screen_content_tools, Some((palette_ctx, &palette_cache)), &palette_uv_cache, r * (SUB / MI), c * (SUB / MI), fctx, )?; if let Some((dv_row, dv_col)) = intrabc { return decode_intrabc_rect( dec, cdfs, neighbours, (r * (SUB / MI), c * (SUB / MI)), (bw, bh), skip, (dv_row, dv_col), y, u, v, base_q_idx, fctx, ); } let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx); let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); let depth = if tx_select && !lossless(fctx) { let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh); dec.symbol(&mut cdfs.tx_size_cat2[ctx]) } else { 0 }; publish_txfm_bands_if_in_inter( neighbours, (mi_r, mi_c), depth_to_tx_wh(bw, bh, depth, fctx), (bw, bh), fctx, ); if depth != 0 || lossless(fctx) { let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); let modes = RectStripModes { skip, mode, angle_delta_y, uv_predict_mode, angle_delta_uv, alpha, filter_intra, smooth_neighbor, smooth_neighbor_uv, palette_y: palette_y .as_ref() .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), palette_uv: palette_uv.as_ref().map(|p| { ( p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), ) }), }; decode_rect_split( dec, cdfs, neighbours, (mi_r, mi_c), bw, bh, tx_w, tx_h, &modes, y, u, v, reduced_tx_set, fctx, )?; let (py_size, py_colors) = palette_y .as_ref() .map_or((0, [0u16; 8]), |p| (p.size, p.colors)); neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); let (puv_size, puv_colors) = palette_uv .as_ref() .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); return Ok(()); } let (tx_w, tx_h) = (bw, bh); let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); if let Some(ref pyv) = palette_y { let buf: Vec = pyv .map .iter() .map(|&idx| pyv.colors[idx as usize]) .collect(); *fctx.palette_pred.borrow_mut() = Some(buf); } let palette_uv_bufs: Option<(Vec, Vec)> = palette_uv.as_ref().map(|puv| { ( puv.map .iter() .map(|&idx| puv.u_colors[idx as usize]) .collect(), puv.map .iter() .map(|&idx| puv.v_colors[idx as usize]) .collect(), ) }); if skip { push_intra_rect( y, u, v, 0, px, py, bw, bh, mode, angle_delta_y, reach, &ZERO_RESIDUAL[..bw * bh], None, filter_intra, smooth_neighbor, fctx, ); let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(ub.clone()); } push_intra_rect( y, u, v, 1, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &ZERO_RESIDUAL[..chroma_w * chroma_h], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(vb.clone()); } push_intra_rect( y, u, v, 2, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &ZERO_RESIDUAL[..chroma_w * chroma_h], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); neighbours.record_rect( at, bw, bh, mode, uv_predict_mode, &[ &ZERO_RESIDUAL[..bw * bh], &ZERO_RESIDUAL[..chroma_w * chroma_h], &ZERO_RESIDUAL[..chroma_w * chroma_h], ], ); } else { let luma_scan: &[u16] = if bw > bh { &SCAN_32X16 } else { &SCAN_16X32 }; let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (chroma_w, chroma_h) { (32, 16) => (TxbSet::ChromaRect32x16, &SCAN_32X16), (16, 32) => (TxbSet::ChromaRect32x16, &SCAN_16X32), (16, 8) => (TxbSet::ChromaRect16x8, &SCAN_16X8), (8, 16) => (TxbSet::ChromaRect16x8, &SCAN_8X16), (16, 16) => (TxbSet::Chroma16, default_scan(16)), (8, 32) => (TxbSet::Chroma16, &SCAN_8X32), _ => { return Err(unsupported( "a rectangular chroma transform whose size has no coefficient table", )); } }; let chroma_default_tx = if chroma_w.max(chroma_h) >= 32 { TxType::DctDct } else { default_intra_tx_type(uv_predict_mode as u8) }; { let mut around = neighbours.around_rect(at, bw, bh); if ss_x(fctx) == 1 && ss_y(fctx) == 0 { let c2 = neighbours.around_mi_422_chroma((at.0 * (SUB / MI), at.1 * (SUB / MI)), bw, bh); around[1] = c2[1]; around[2] = c2[2]; } let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, fi_tx_row(mode, filter_intra)); let (luma_levels, luma_tx_type) = read_coeffs( dec, &mut luma_coding, luma_scan, bw, bh, 0, dc_sign_ctx(around[0].2), TxType::DctDct, None, )?; let luma_residual = dequant_and_inverse_typed_wh( &luma_levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type, block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), ); push_intra_rect( y, u, v, 0, px, py, bw, bh, mode, angle_delta_y, reach, &luma_residual, None, filter_intra, smooth_neighbor, fctx, ); let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(ub.clone()); } let u_default_tx = chroma_default_tx; let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); let (u_levels, u_tx_type) = read_chroma_coeffs_rect( dec, &mut u_coding, chroma_scan, chroma_w, chroma_h, u_skip_ctx, dc_sign_ctx(around[1].2), u_default_tx, fctx, )?; let u_residual = dequant_and_inverse_typed_wh( &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type, block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type), ); push_intra_rect( y, u, v, 1, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_residual, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(vb.clone()); } let v_default_tx = chroma_default_tx; let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); let (v_levels, v_tx_type) = read_chroma_coeffs_rect( dec, &mut v_coding, chroma_scan, chroma_w, chroma_h, v_skip_ctx, dc_sign_ctx(around[2].2), v_default_tx, fctx, )?; let v_residual = dequant_and_inverse_typed_wh( &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type, block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type), ); push_intra_rect( y, u, v, 2, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_residual, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); neighbours.record_rect( at, bw, bh, mode, uv_predict_mode, &[&luma_levels[..], &u_levels[..], &v_levels[..]], ); } } let (py_size, py_colors) = palette_y .as_ref() .map_or((0, [0u16; 8]), |p| (p.size, p.colors)); neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); let (puv_size, puv_colors) = palette_uv .as_ref() .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); neighbours.fill_lf_grid_rect( (mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx, ); Ok(()) } pub(super) fn decode_leaf_rect( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, outer_at: (usize, usize), leaf_mi: (usize, usize), bw: usize, bh: usize, prev_leaf: Option<((usize, usize), usize)>, y: &mut PlaneBuf<'static>, u: &mut PlaneBuf<'static>, v: &mut PlaneBuf<'static>, enable_filter_intra: bool, allow_screen_content_tools: bool, base_q_idx: u8, tx_select: bool, reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, ) -> Result { let _ = base_q_idx; let (r, c) = outer_at; let mut above_mode = neighbours.above_mode[c]; let mut left_mode = neighbours.left_mode[r]; let (mi_w, mi_h) = (bw / MI, bh / MI); if let Some(((pr, pc), pmode)) = prev_leaf { if pc == leaf_mi.1 && leaf_mi.0 == pr + mi_h { above_mode = pmode; } else if pr == leaf_mi.0 && leaf_mi.1 == pc + mi_w { left_mode = pmode; } } let (above_mode, left_mode) = neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode)); let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); let pal_at = leaf_mi; let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at); let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at); let ( skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc, ) = read_intra_mode_rect( dec, cdfs, above_mode, left_mode, true, true, bw, bh, enable_filter_intra, neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), allow_screen_content_tools, Some((palette_ctx, &palette_cache)), &palette_uv_cache, leaf_mi.0, leaf_mi.1, fctx, )?; if let Some((dv_row, dv_col)) = intrabc { decode_intrabc_rect( dec, cdfs, neighbours, leaf_mi, (bw, bh), skip, (dv_row, dv_col), y, u, v, base_q_idx, fctx, )?; return Ok(DC_PRED); } let palette_y_buf: Option> = palette_y .as_ref() .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); let palette_uv_bufs: Option<(Vec, Vec)> = palette_uv.as_ref().map(|p| { ( p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), ) }); let (pal_y_size, pal_y_colors) = palette_y .as_ref() .map_or((0, [0u16; 8]), |p| (p.size, p.colors)); let (pal_uv_size, pal_uv_colors) = palette_uv .as_ref() .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); let depth = if tx_select && !lossless(fctx) { let ctx = tx_size_context_txfm_rect(neighbours, leaf_mi, bw, bh); dec.symbol(&mut cdfs.tx_size_cat1[ctx]) } else { 0 }; publish_txfm_bands_if_in_inter( neighbours, leaf_mi, depth_to_tx_wh(bw, bh, depth, fctx), (bw, bh), fctx, ); if depth != 0 || lossless(fctx) { let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); let modes = RectStripModes { skip, mode, angle_delta_y, uv_predict_mode, angle_delta_uv, alpha, filter_intra, smooth_neighbor, smooth_neighbor_uv, palette_y: palette_y_buf.clone(), palette_uv: palette_uv_bufs.clone(), }; decode_rect_split( dec, cdfs, neighbours, leaf_mi, bw, bh, tx_w, tx_h, &modes, y, u, v, reduced_tx_set, fctx, )?; record_strip_palette( neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, ); return Ok(mode); } let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let (luma_levels, u_levels, v_levels); if skip { luma_levels = Grid::Zero(bw * bh); u_levels = Grid::Zero(chroma_w * chroma_h); v_levels = Grid::Zero(chroma_w * chroma_h); if let Some(b) = &palette_y_buf { *fctx.palette_pred.borrow_mut() = Some(b.clone()); } push_intra_rect( y, u, v, 0, px, py, bw, bh, mode, angle_delta_y, reach, &luma_levels, None, filter_intra, smooth_neighbor, fctx, ); let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(ub.clone()); } push_intra_rect( y, u, v, 1, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_levels, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(vb.clone()); } push_intra_rect( y, u, v, 2, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_levels, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); } else { let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; let (luma_scan, chroma_scan): (&[u16], &[u16]) = if ss_x(fctx) == 0 { if bw == 16 { (&SCAN_16X8, &SCAN_16X8) } else { (&SCAN_8X16, &SCAN_8X16) } } else if chroma_422 { if bw == 16 { (&SCAN_16X8, default_scan(8)) } else { (&SCAN_8X16, default_scan(8)) } } else if bw == 16 { (&SCAN_16X8, &SCAN_8X4) } else { (&SCAN_8X16, &SCAN_4X8) }; let chroma_rect_set = if ss_x(fctx) == 0 { TxbSet::ChromaRect16x8 } else if chroma_422 { TxbSet::Chroma8 } else { TxbSet::ChromaRect8x4 }; let mut around = neighbours.around_mi_rect(leaf_mi, bw, bh); if chroma_422 { let c = neighbours.around_mi_422_chroma(leaf_mi, bw, bh); around[1] = c[1]; around[2] = c[2]; } let luma_set = if reduced_tx_set { TxbSet::LumaRect16x8 } else { TxbSet::LumaRect16x8Set1 }; let mut luma_coding = cdfs.txb(luma_set, fi_tx_row(mode, filter_intra)); let (l_levels, luma_tx_type) = read_coeffs( dec, &mut luma_coding, luma_scan, bw, bh, 0, dc_sign_ctx(around[0].2), TxType::DctDct, None, )?; luma_levels = l_levels; let luma_residual = dequant_and_inverse_typed_wh( &luma_levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type, block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), ); if let Some(b) = &palette_y_buf { *fctx.palette_pred.borrow_mut() = Some(b.clone()); } push_intra_rect( y, u, v, 0, px, py, bw, bh, mode, angle_delta_y, reach, &luma_residual, None, filter_intra, smooth_neighbor, fctx, ); let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); let mut u_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); let (u_l, u_tx_type) = read_chroma_coeffs_rect( dec, &mut u_coding, chroma_scan, chroma_w, chroma_h, u_skip_ctx, dc_sign_ctx(around[1].2), u_default_tx, fctx, )?; u_levels = u_l; let u_residual = dequant_and_inverse_typed_wh( &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type, block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type), ); if let Some((ub, _)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(ub.clone()); } push_intra_rect( y, u, v, 1, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_residual, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); let mut v_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); let (v_l, v_tx_type) = read_chroma_coeffs_rect( dec, &mut v_coding, chroma_scan, chroma_w, chroma_h, v_skip_ctx, dc_sign_ctx(around[2].2), v_default_tx, fctx, )?; v_levels = v_l; let v_residual = dequant_and_inverse_typed_wh( &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type, block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type), ); if let Some((_, vb)) = &palette_uv_bufs { *fctx.palette_pred.borrow_mut() = Some(vb.clone()); } push_intra_rect( y, u, v, 2, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_residual, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); } neighbours.record_mi_rect( leaf_mi, bw, bh, &[&luma_levels[..], &u_levels[..], &v_levels[..]], ); neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0, fctx); record_strip_palette( neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, ); neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, mode); neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, uv_predict_mode); Ok(mode) }