use super::PART128_GATHERED_SYMBOLS; use super::PARTITION_HORZ_B; use super::Result; use super::block::{ PlaneBuf, chroma_plane_block_codable, decode_block, fresh_plane, refuse_invalid_plane_block, refuse_invalid_subsize, }; use super::coeffs::{Neighbours, apply_deblock}; use super::deblock::{capture_filter_replay, plane_snapshot}; use super::leaf_sub8::{decode_leaf_rect8, decode_leaf_split4}; use super::leaf8::{decode_block_128rect, decode_leaf8, write_back_split_modes}; use super::post_filter::{apply_cdef, apply_loop_restoration, capture_stages}; use super::rect::{decode_block_rect, decode_leaf_rect}; use super::rect4::{decode_block_rect4, decode_block_rect64, decode_rect4_16}; use super::replay::{crop_owned, scratch_return}; use super::{ BLOCK, BLOCK_MI, CdefParams, Cdfs, DeltaParams, HORZ_ALIKE, HORZ_ALIKE128, LoopFilterParams, LoopRestorationParams, MI, PARTITION_HORZ, PARTITION_HORZ_4, PARTITION_HORZ_A, PARTITION_NONE, PARTITION_SPLIT, PARTITION_VERT, PARTITION_VERT_4, PARTITION_VERT_A, PARTITION_VERT_B, Picture, SB, SB_MI, SUB, SUB_MI, SymbolDecoder, TX4, TX8, TX16, TX32, TileInfo, TxbSet, VERT_ALIKE, VERT_ALIKE128, bit_depth, block_grid, cdf_update_disabled, default_scan, gather, gather_of, has_half, q_ctx_of, round_ss, sb128, ss_x, ss_y, superres, unsupported, }; #[cfg(test)] pub(crate) fn decode_key_frame_tile_lr( data: &[u8], mi_cols: u32, mi_rows: u32, base_q_idx: u8, frame_width: u32, frame_height: u32, enable_filter_intra: bool, cdef: &CdefParams, loop_filter: &LoopFilterParams, tx_select: bool, reduced_tx_set: bool, allow_screen_content_tools: bool, allow_intrabc: bool, lr: &LoopRestorationParams, fctx: &crate::decode::FrameCtx, ) -> Result { let single_tile = TileInfo { mi_col_starts: vec![0, mi_cols], mi_row_starts: vec![0, mi_rows], ..TileInfo::default() }; decode_key_frame_tiles_lr( &[data], &single_tile, mi_cols, mi_rows, base_q_idx, frame_width, frame_height, enable_filter_intra, cdef, loop_filter, tx_select, reduced_tx_set, allow_screen_content_tools, allow_intrabc, lr, fctx, ) } pub(crate) fn decode_key_frame_tiles_lr( tiles: &[&[u8]], tile_info: &TileInfo, mi_cols: u32, mi_rows: u32, base_q_idx: u8, frame_width: u32, frame_height: u32, enable_filter_intra: bool, cdef: &CdefParams, loop_filter: &LoopFilterParams, tx_select: bool, reduced_tx_set: bool, allow_screen_content_tools: bool, allow_intrabc: bool, lr: &LoopRestorationParams, fctx: &crate::decode::FrameCtx, ) -> Result { let delta = DeltaParams::default(); decode_key_frame_tile_with_cdfs( tiles, tile_info, mi_cols, mi_rows, base_q_idx, crate::quant::QuantDeltas::default(), frame_width, frame_height, enable_filter_intra, false, cdef, loop_filter, lr, None, tx_select, reduced_tx_set, allow_screen_content_tools, allow_intrabc, delta, fctx, ) .map(|(picture, _)| picture) } pub(super) fn sb_visit_order( r0: u32, r1: u32, c0: u32, c1: u32, sb128: bool, ) -> Vec<(u32, u32, bool, bool)> { let mut out = Vec::new(); if !sb128 { for r in r0..r1 { for c in c0..c1 { out.push((r, c, c == c0, true)); } } return out; } let mut r = r0; while r < r1 { let mut first_in_row = true; let mut c = c0; while c < c1 { let mut first_in_sb = true; for qr in 0..2 { for qc in 0..2 { let (rr, cc) = (r + qr, c + qc); if rr >= r1 || cc >= c1 { continue; } out.push((rr, cc, first_in_row, first_in_sb)); first_in_row = false; first_in_sb = false; } } c += 2; } r += 2; } out } thread_local! { pub(crate) static MU_CHUNK_COMPOUND_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } thread_local! { pub(crate) static MU_CHUNK_INTRA_IN_INTER_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } thread_local! { pub(crate) static MU_CHUNK_WALKS: std::cell::Cell = const { std::cell::Cell::new(0) }; pub(crate) static MU_CHUNK_WALK_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; pub(crate) static LOSSLESS_EDGE_CLIPPED: std::cell::Cell = const { std::cell::Cell::new(0) }; pub(crate) static LOSSLESS_EDGE_CLIP_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; pub(crate) static SUB8_LOSSLESS_NO_VARTX: std::cell::Cell = const { std::cell::Cell::new(0) }; } pub(super) fn read_sb128_root( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, neighbours: &Neighbours, lr: &LoopRestorationParams, lr_grid: &mut crate::restoration::RestorationGrid, lr_reference: &mut [( crate::restoration::WienerInfo, crate::restoration::SgrprojInfo, ); 3], sb_r: u32, sb_c: u32, mi_cols: u32, mi_rows: u32, fctx: &crate::decode::FrameCtx, ) -> Result { let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); crate::restoration::read_lr( dec, cdfs, lr, lr_grid, lr_reference, (sb_r & !1) * (SB_MI / 2), (sb_c & !1) * (SB_MI / 2), SB_MI, fctx, ); let at128 = ((mi_r128 / SUB_MI) as usize, (mi_c128 / SUB_MI) as usize); let ctx128 = neighbours.partition_ctx(at128, 128); let (has_cols128, has_rows128) = (mi_c128 + SB_MI < mi_cols, mi_r128 + SB_MI < mi_rows); let part128 = if has_cols128 && has_rows128 { dec.symbol(&mut cdfs.partition_w128[ctx128]) } else { match (has_cols128, has_rows128) { (true, false) => { if dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &VERT_ALIKE128)) == 1 { PARTITION_SPLIT } else { PARTITION_HORZ } } (false, true) => { if dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &HORZ_ALIKE128)) == 1 { PARTITION_SPLIT } else { PARTITION_VERT } } _ => { PART128_GATHERED_SYMBOLS.with(|c| c.set(c.get() - 1)); PARTITION_SPLIT } } }; refuse_invalid_subsize((128, 128), part128, fctx)?; match part128 { PARTITION_SPLIT | PARTITION_NONE | PARTITION_HORZ | PARTITION_VERT => {} p if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&p) => {} _ => { return Err(unsupported( "a 128x128 superblock partition value outside the 8-symbol alphabet", )); } } Ok(part128) } pub(crate) fn decode_key_frame_tile_with_cdfs( tiles: &[&[u8]], tile_info: &TileInfo, mi_cols: u32, mi_rows: u32, base_q_idx: u8, deltas: crate::quant::QuantDeltas, frame_width: u32, frame_height: u32, enable_filter_intra: bool, enable_edge_filter: bool, cdef: &CdefParams, loop_filter: &LoopFilterParams, lr: &LoopRestorationParams, initial_cdfs: Option, tx_select: bool, reduced_tx_set: bool, allow_screen_content_tools: bool, allow_intrabc: bool, delta: DeltaParams, fctx: &crate::decode::FrameCtx, ) -> Result<(Picture, Cdfs)> { fctx.tx_select_inter.set(false); fctx.tx_select_frame.set(tx_select); fctx.reduced_tx_set_inter.set(reduced_tx_set); fctx.intra_only.set(true); if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); } fctx.enable_edge_filter.set(enable_edge_filter); let sb128 = sb128(fctx); let (sb_cols, sb_rows) = (mi_cols.div_ceil(SB_MI), mi_rows.div_ceil(SB_MI)); { *fctx.intrabc_mi_grid.borrow_mut() = allow_intrabc.then(|| { ( crate::mvstack::MiGrid::new(mi_cols as usize, mi_rows as usize), mi_cols as usize, mi_rows as usize, ) }); }; fctx.intrabc_dv.set(None); fctx.cdef_bits.set(cdef.bits); fctx.cdef_sb_cols.set(sb_cols as usize); *fctx.cdef_idx_grid.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]; fctx.delta_q_present.set(delta.q_present); fctx.delta_q_res.set(1i32 << delta.q_res); fctx.delta_lf_present.set(delta.lf_present); fctx.delta_lf_res.set(1i32 << delta.lf_res); fctx.delta_lf_multi.set(delta.lf_multi); let (cols32, rows32) = block_grid(mi_cols, mi_rows); let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize); let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK); let mut y = PlaneBuf { data: std::borrow::Cow::Owned(fresh_plane(width * height)), width, height, true_width, true_height, tile_x0: 0, tile_y0: 0, tile_x1: width, tile_y1: height, plane: 0, }; let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx)); let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx)); let mut u = PlaneBuf { data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), width: cw, height: ch, true_width: ctw, true_height: cth, tile_x0: 0, tile_y0: 0, tile_x1: cw, tile_y1: ch, plane: 1, }; let mut v = PlaneBuf { data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), width: cw, height: ch, true_width: ctw, true_height: cth, tile_x0: 0, tile_y0: 0, tile_x1: cw, tile_y1: ch, plane: 2, }; let scan32 = default_scan(TX32); let scan16 = default_scan(TX16); let scan8 = default_scan(TX8); let scan4 = default_scan(TX4); let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx))); let mut result_cdfs = base_cdfs.clone(); let mut neighbours = Neighbours::new( cols32 as usize * 2, rows32 as usize * 2, mi_cols as usize, mi_rows as usize, fctx, ); let mut lr_grid = crate::restoration::RestorationGrid::with_ss( lr, superres(fctx).map_or(frame_width, |(w, _)| w), frame_height, fctx.subsampling_x.get(), fctx.subsampling_y.get(), ); for (tile_idx, &tile_bytes) in tiles.iter().enumerate() { let tile_num = tile_idx as u32; let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols); let mi_row0 = tile_info.mi_row_starts[trow as usize]; let mi_row1 = tile_info.mi_row_starts[trow as usize + 1]; let mi_col0 = tile_info.mi_col_starts[tcol as usize]; let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1]; let (sb_r0, sb_r1) = ( (mi_row0 / SB_MI).min(sb_rows), mi_row1.div_ceil(SB_MI).min(sb_rows), ); let (sb_c0, sb_c1) = ( (mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols), ); let mut lr_reference = [( crate::restoration::WienerInfo::default(), crate::restoration::SgrprojInfo::default(), ); 3]; let mut cdfs = base_cdfs.clone(); fctx.current_q_idx.set(i32::from(base_q_idx)); fctx.quant_deltas.set(deltas); fctx.current_delta_lf.set([0; 4]); let mut dec = SymbolDecoder::new(tile_bytes); dec.set_cdf_update(!cdf_update_disabled(fctx)); neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize, fctx); y.set_tile_origin( mi_col0 as usize * 4, mi_row0 as usize * 4, (mi_col1 as usize * 4).min(y.width), (mi_row1 as usize * 4).min(y.height), ); u.set_tile_origin( (mi_col0 as usize * 4) >> ss_x(fctx), (mi_row0 as usize * 4) >> ss_y(fctx), ((mi_col1 as usize * 4) >> ss_x(fctx)).min(u.width), ((mi_row1 as usize * 4) >> ss_y(fctx)).min(u.height), ); v.set_tile_origin( (mi_col0 as usize * 4) >> ss_x(fctx), (mi_row0 as usize * 4) >> ss_y(fctx), ((mi_col1 as usize * 4) >> ss_x(fctx)).min(v.width), ((mi_row1 as usize * 4) >> ss_y(fctx)).min(v.height), ); let mut sb128_none = false; for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) { if row_start { neighbours.start_row(); } if sb_start { sb128_none = false; } if sb128_none { continue; } { if sb128 && sb_start { let part128 = read_sb128_root( &mut dec, &mut cdfs, &neighbours, lr, &mut lr_grid, &mut lr_reference, sb_r, sb_c, mi_cols, mi_rows, fctx, )?; if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&part128) { sb128_none = true; let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4); let (br, bc) = (at128.0 + 4, at128.1 + 4); let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize); let _vert_ab = (part128 == PARTITION_VERT_A || part128 == PARTITION_VERT_B) .then(crate::encode::Reach::vert_ab_partition); let cdef_copy = |src: (usize, usize), dst: (usize, usize)| { 'cells: { let mut g = fctx.cdef_idx_grid.borrow_mut(); let cols = fctx.cdef_sb_cols.get(); if cols == 0 { break 'cells; } let (si, di) = ( (q_r + src.0) * cols + q_c + src.1, (q_r + dst.0) * cols + q_c + dst.1, ); if q_c + src.1 < cols && q_c + dst.1 < cols && si < g.len() && di < g.len() { g[di] = g[si]; } }; }; macro_rules! square64 { ($at:expr) => {{ fctx.cdef_transmitted.set(false); decode_block( &mut dec, &mut cdfs, &mut neighbours, $at, SB, TxbSet::Luma64, TxbSet::Chroma32, TX32, TX32, (&scan32, &scan32), false, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, &scan32, &scan16, &scan8, &scan4, tx_select, reduced_tx_set, fctx, )? }}; } macro_rules! half128 { ($at:expr, $bw:expr, $bh:expr, $src:expr, $dst:expr) => {{ fctx.cdef_transmitted.set(false); decode_block_128rect( &mut dec, &mut cdfs, &mut neighbours, $at, $bw, $bh, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, tx_select, reduced_tx_set, fctx, )?; cdef_copy($src, $dst); }}; } match part128 { PARTITION_HORZ_A => { square64!(at128); square64!((at128.0, bc)); half128!((br, at128.1), 128, 64, (1, 0), (1, 1)); } PARTITION_HORZ_B => { half128!(at128, 128, 64, (0, 0), (0, 1)); square64!((br, at128.1)); square64!((br, bc)); } PARTITION_VERT_A => { square64!(at128); square64!((br, at128.1)); half128!((at128.0, bc), 64, 128, (0, 1), (1, 1)); } _ => { half128!(at128, 64, 128, (0, 0), (1, 0)); square64!((at128.0, bc)); square64!((br, bc)); } } continue; } if matches!(part128, PARTITION_HORZ | PARTITION_VERT) { sb128_none = true; let horz = part128 == PARTITION_HORZ; let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4); let (bw, bh) = if horz { (128usize, 64usize) } else { (64, 128) }; for i in 0..2 { let at = if horz { (at128.0 + i * 4, at128.1) } else { (at128.0, at128.1 + i * 4) }; let (blk_mi_r, blk_mi_c) = (at.0 * (SUB / MI), at.1 * (SUB / MI)); if i > 0 && (blk_mi_r >= mi_rows as usize || blk_mi_c >= mi_cols as usize) { break; } fctx.cdef_transmitted.set(false); decode_block_128rect( &mut dec, &mut cdfs, &mut neighbours, at, bw, bh, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, tx_select, reduced_tx_set, fctx, )?; } let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize); 'cells: { let mut g = fctx.cdef_idx_grid.borrow_mut(); let cols = fctx.cdef_sb_cols.get(); if cols == 0 { break 'cells; } for (src, dst) in if horz { [((0, 0), (0, 1)), ((1, 0), (1, 1))] } else { [((0, 0), (1, 0)), ((0, 1), (1, 1))] } { let (si, di) = ( (q_r + src.0) * cols + q_c + src.1, (q_r + dst.0) * cols + q_c + dst.1, ); if q_c + src.1 < cols && q_c + dst.1 < cols && si < g.len() && di < g.len() { g[di] = g[si]; } } }; continue; } if part128 == PARTITION_NONE { sb128_none = true; fctx.cdef_transmitted.set(false); let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4); decode_block( &mut dec, &mut cdfs, &mut neighbours, at128, SB * 2, TxbSet::Luma64, TxbSet::Chroma32, TX32, TX32, (scan32, scan32), false, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize); { let mut g = fctx.cdef_idx_grid.borrow_mut(); let cols = fctx.cdef_sb_cols.get(); if cols > 0 && q_r * cols + q_c < g.len() { let idx = g[q_r * cols + q_c]; for (dr, dc) in [(0, 1), (1, 0), (1, 1)] { let i = (q_r + dr) * cols + q_c + dc; if q_c + dc < cols && i < g.len() { g[i] = idx; } } } }; continue; } } if !sb128 { crate::restoration::read_lr( &mut dec, &mut cdfs, lr, &mut lr_grid, &mut lr_reference, sb_r * (SB_MI / 2), sb_c * (SB_MI / 2), SB_MI / 2, fctx, ); } fctx.cdef_transmitted.set(false); let at = (sb_r as usize * 4, sb_c as usize * 4); let ctx = neighbours.partition_ctx(at, SB); let (has_cols, has_rows) = ( sb_c * SB_MI + SB_MI / 2 < mi_cols, sb_r * SB_MI + SB_MI / 2 < mi_rows, ); let part = if has_cols && has_rows { dec.symbol(&mut cdfs.partition_w64[ctx]) } else { match (has_cols, has_rows) { (true, false) => { let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE)); if b == 1 { PARTITION_SPLIT } else { PARTITION_HORZ } } (false, true) => { let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE)); if b == 1 { PARTITION_SPLIT } else { PARTITION_VERT } } _ => PARTITION_SPLIT, } }; refuse_invalid_subsize((64, 64), part, fctx)?; match part { PARTITION_NONE => { decode_block( &mut dec, &mut cdfs, &mut neighbours, at, SB, TxbSet::Luma64, TxbSet::Chroma32, TX32, TX32, (scan32, scan32), false, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; } PARTITION_SPLIT => { for q in 0..4 { let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); if r32 >= rows32 || c32 >= cols32 { continue; } let at32 = (r32 as usize * 2, c32 as usize * 2); let ctx32 = neighbours.partition_ctx(at32, BLOCK); let (has_cols32, has_rows32) = ( has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), ); let part32 = if has_cols32 && has_rows32 { dec.symbol(&mut cdfs.partition_w32[ctx32]) } else { match (has_cols32, has_rows32) { (true, false) => { let b = dec.symbol_fixed(&gather( &cdfs.partition_w32[ctx32], VERT_ALIKE, )); if b == 1 { PARTITION_SPLIT } else { PARTITION_HORZ } } (false, true) => { let b = dec.symbol_fixed(&gather( &cdfs.partition_w32[ctx32], HORZ_ALIKE, )); if b == 1 { PARTITION_SPLIT } else { PARTITION_VERT } } _ => PARTITION_SPLIT, } }; refuse_invalid_subsize((32, 32), part32, fctx)?; match part32 { PARTITION_NONE => { decode_block( &mut dec, &mut cdfs, &mut neighbours, at32, BLOCK, TxbSet::Luma32, TxbSet::Chroma16, TX32, TX16, (scan32, scan16), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; } PARTITION_SPLIT => { for sub in 0..4 { let (sr, sc) = ( r32 as usize * 2 + sub / 2, c32 as usize * 2 + sub % 2, ); if (sr as u32) * SUB_MI >= mi_rows || (sc as u32) * SUB_MI >= mi_cols { continue; } let (has_cols16, has_rows16) = ( has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), ); let at16 = (sr, sc); let ctx16 = neighbours.partition_ctx(at16, SUB); if has_cols16 && has_rows16 { let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]); refuse_invalid_subsize((16, 16), part16, fctx)?; if part16 == PARTITION_HORZ || part16 == PARTITION_VERT { let (bw, bh) = if part16 == PARTITION_HORZ { (16, 8) } else { (8, 16) }; if !chroma_plane_block_codable( bw, bh, ss_x(fctx), ss_y(fctx), ) { return Err(refuse_invalid_plane_block( bw, bh, fctx, )); } } if part16 == PARTITION_NONE { decode_block( &mut dec, &mut cdfs, &mut neighbours, at16, SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; continue; } if (PARTITION_HORZ_A..=PARTITION_VERT_B) .contains(&part16) { let _vert_guard = if part16 == PARTITION_VERT_A || part16 == PARTITION_VERT_B { Some(crate::encode::Reach::vert_ab_partition()) } else { None }; let (mi_row0, mi_col0) = (sr * SUB_MI as usize, sc * SUB_MI as usize); macro_rules! ab_leaf8 { ($mi:expr, $prev:expr) => { decode_leaf8( &mut dec, &mut cdfs, &mut neighbours, at16, $mi, (&scan8, &scan4), $prev, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, allow_intrabc, tx_select, reduced_tx_set, fctx, )? }; } macro_rules! ab_strip { ($mi:expr, $bw:expr, $bh:expr, $prev:expr) => { decode_leaf_rect( &mut dec, &mut cdfs, &mut neighbours, at16, $mi, $bw, $bh, $prev, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )? }; } let tl = (mi_row0, mi_col0); let tr = (mi_row0, mi_col0 + 2); let bl = (mi_row0 + 2, mi_col0); let br = (mi_row0 + 2, mi_col0 + 2); let mut done: Vec<((usize, usize), usize)> = Vec::new(); match part16 { PARTITION_HORZ_A => { let m_tl = ab_leaf8!(tl, done.last().copied()); done.push((tl, m_tl)); let m_tr = ab_leaf8!(tr, done.last().copied()); done.push((tr, m_tr)); let m_bot = ab_strip!(bl, 16, 8, Some((tl, m_tl))); done.push((bl, m_bot)); } PARTITION_HORZ_B => { let m_top = ab_strip!(tl, 16, 8, None); done.push((tl, m_top)); let m_bl = ab_leaf8!(bl, done.last().copied()); done.push((bl, m_bl)); let m_br = ab_leaf8!(br, done.last().copied()); done.push((br, m_br)); } PARTITION_VERT_A => { let m_tl = ab_leaf8!(tl, done.last().copied()); done.push((tl, m_tl)); let m_bl = ab_leaf8!(bl, done.last().copied()); done.push((bl, m_bl)); let m_right = ab_strip!(tr, 8, 16, Some((tl, m_tl))); done.push((tr, m_right)); } _ => { let m_left = ab_strip!(tl, 8, 16, None); done.push((tl, m_left)); let m_tr = ab_leaf8!(tr, done.last().copied()); done.push((tr, m_tr)); let m_br = ab_leaf8!(br, done.last().copied()); done.push((br, m_br)); } } write_back_split_modes( &mut neighbours, &done, (mi_row0, mi_col0), (sr, sc), ); continue; } if part16 == PARTITION_HORZ { let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI); let top = (mi_row0 as usize, mi_col0 as usize); let bot = (mi_row0 as usize + 2, mi_col0 as usize); let mode_top = decode_leaf_rect( &mut dec, &mut cdfs, &mut neighbours, at16, top, 16, 8, None, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; let mode_bot = decode_leaf_rect( &mut dec, &mut cdfs, &mut neighbours, at16, bot, 16, 8, Some((top, mode_top)), &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; neighbours.above_mode[sc] = mode_bot; neighbours.left_mode[sr] = mode_bot; continue; } if part16 == PARTITION_VERT { let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI); let left = (mi_row0 as usize, mi_col0 as usize); let right = (mi_row0 as usize, mi_col0 as usize + 2); let mode_left = decode_leaf_rect( &mut dec, &mut cdfs, &mut neighbours, at16, left, 8, 16, None, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; let mode_right = decode_leaf_rect( &mut dec, &mut cdfs, &mut neighbours, at16, right, 8, 16, Some((left, mode_left)), &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; neighbours.above_mode[sc] = mode_right; neighbours.left_mode[sr] = mode_right; continue; } if part16 == PARTITION_HORZ_4 || part16 == PARTITION_VERT_4 { decode_rect4_16( &mut dec, &mut cdfs, &mut neighbours, at16, part16 == PARTITION_HORZ_4, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; continue; } debug_assert_eq!(part16, PARTITION_SPLIT); let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI); let mut sib_modes: [Option<(usize, usize)>; 4] = [None; 4]; for i in 0..4 { let (mr, mc) = (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2); let leaf_mi = (mr as usize, mc as usize); let li = i as usize; let saved_modes = ( neighbours.above_mode[sc], neighbours.left_mode[sr], ); if li >= 2 && let Some((below, _)) = sib_modes[li - 2] { neighbours.above_mode[sc] = below; } if li % 2 == 1 && let Some((_, right)) = sib_modes[li - 1] { neighbours.left_mode[sr] = right; } let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); refuse_invalid_subsize((8, 8), part8, fctx)?; if part8 == PARTITION_HORZ || part8 == PARTITION_VERT { let (bw, bh) = if part8 == PARTITION_HORZ { (8, 4) } else { (4, 8) }; if !chroma_plane_block_codable( bw, bh, ss_x(fctx), ss_y(fctx), ) { return Err(refuse_invalid_plane_block( bw, bh, fctx, )); } } let leaf_mode = if part8 == PARTITION_NONE { decode_leaf8( &mut dec, &mut cdfs, &mut neighbours, at16, leaf_mi, (scan8, scan4), None, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, allow_intrabc, tx_select, reduced_tx_set, fctx, ) .map(|m| (m, m))? } else if part8 == PARTITION_SPLIT { decode_leaf_split4( &mut dec, &mut cdfs, &mut neighbours, at16, leaf_mi, scan4, None, &mut y, &mut u, &mut v, enable_filter_intra, allow_intrabc, reduced_tx_set, fctx, )? } else if part8 == PARTITION_HORZ || part8 == PARTITION_VERT { decode_leaf_rect8( &mut dec, &mut cdfs, &mut neighbours, at16, leaf_mi, part8 == PARTITION_VERT, scan4, &mut y, &mut u, &mut v, enable_filter_intra, allow_intrabc, tx_select, reduced_tx_set, fctx, )? } else { unreachable!( "partition_w8 is a 4-symbol CDF: NONE/HORZ/VERT/SPLIT only" ); }; neighbours.above_mode[sc] = saved_modes.0; neighbours.left_mode[sr] = saved_modes.1; sib_modes[li] = Some(leaf_mode); } if let Some((below, _)) = sib_modes[2].or(sib_modes[3]).or(sib_modes[0]) { neighbours.above_mode[sc] = below; } if let Some((_, right)) = sib_modes[1].or(sib_modes[3]).or(sib_modes[0]) { neighbours.left_mode[sr] = right; } continue; } if !has_cols16 && !has_rows16 { } else { let edge_split = if has_cols16 { dec.symbol_fixed(&gather( &cdfs.partition_w16[ctx16], VERT_ALIKE, )) } else { dec.symbol_fixed(&gather( &cdfs.partition_w16[ctx16], HORZ_ALIKE, )) }; if edge_split == 0 { let edge_part = if has_cols16 { PARTITION_HORZ } else { PARTITION_VERT }; refuse_invalid_subsize((16, 16), edge_part, fctx)?; let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI); let (bw, bh) = if has_cols16 { (16, 8) } else { (8, 16) }; let mode = decode_leaf_rect( &mut dec, &mut cdfs, &mut neighbours, at16, (mi_row0 as usize, mi_col0 as usize), bw, bh, None, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; neighbours.above_mode[sc] = mode; neighbours.left_mode[sr] = mode; continue; } } let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI); let leaf_positions: Vec<(u32, u32)> = (0..4) .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) .collect(); let mut sib_modes: [Option<(usize, usize)>; 4] = [None; 4]; for (mr, mc) in leaf_positions { let leaf_mi = (mr as usize, mc as usize); let li = (((mr - mi_row0) / 2) * 2 + (mc - mi_col0) / 2) as usize; let saved_modes = ( neighbours.above_mode[sc], neighbours.left_mode[sr], ); if li >= 2 && let Some((below, _)) = sib_modes[li - 2] { neighbours.above_mode[sc] = below; } if li % 2 == 1 && let Some((_, right)) = sib_modes[li - 1] { neighbours.left_mode[sr] = right; } let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); refuse_invalid_subsize((8, 8), part8, fctx)?; let leaf_mode = if part8 == PARTITION_NONE { decode_leaf8( &mut dec, &mut cdfs, &mut neighbours, at16, leaf_mi, (scan8, scan4), None, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, allow_intrabc, tx_select, reduced_tx_set, fctx, ) .map(|m| (m, m))? } else if part8 == PARTITION_SPLIT { decode_leaf_split4( &mut dec, &mut cdfs, &mut neighbours, at16, leaf_mi, scan4, None, &mut y, &mut u, &mut v, enable_filter_intra, allow_intrabc, reduced_tx_set, fctx, )? } else if part8 == PARTITION_HORZ || part8 == PARTITION_VERT { decode_leaf_rect8( &mut dec, &mut cdfs, &mut neighbours, at16, leaf_mi, part8 == PARTITION_VERT, scan4, &mut y, &mut u, &mut v, enable_filter_intra, allow_intrabc, tx_select, reduced_tx_set, fctx, )? } else { unreachable!( "partition_w8 is a 4-symbol CDF: NONE/HORZ/VERT/SPLIT only" ); }; neighbours.above_mode[sc] = saved_modes.0; neighbours.left_mode[sr] = saved_modes.1; sib_modes[li] = Some(leaf_mode); } if let Some((below, _)) = sib_modes[2].or(sib_modes[3]).or(sib_modes[0]) { neighbours.above_mode[sc] = below; } if let Some((_, right)) = sib_modes[1].or(sib_modes[3]).or(sib_modes[0]) { neighbours.left_mode[sr] = right; } } } PARTITION_HORZ => { decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, at32, 32, 16, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; if has_rows32 { decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, (at32.0 + 1, at32.1), 32, 16, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; } } PARTITION_VERT => { decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, at32, 16, 32, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; if has_cols32 { decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, (at32.0, at32.1 + 1), 16, 32, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; } } PARTITION_HORZ_A => { decode_block( &mut dec, &mut cdfs, &mut neighbours, at32, SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; decode_block( &mut dec, &mut cdfs, &mut neighbours, (at32.0, at32.1 + 1), SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, (at32.0 + 1, at32.1), 32, 16, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; } PARTITION_HORZ_B => { decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, at32, 32, 16, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; decode_block( &mut dec, &mut cdfs, &mut neighbours, (at32.0 + 1, at32.1), SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; decode_block( &mut dec, &mut cdfs, &mut neighbours, (at32.0 + 1, at32.1 + 1), SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; } PARTITION_VERT_A => { let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_block( &mut dec, &mut cdfs, &mut neighbours, at32, SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; decode_block( &mut dec, &mut cdfs, &mut neighbours, (at32.0 + 1, at32.1), SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, (at32.0, at32.1 + 1), 16, 32, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; } PARTITION_VERT_B => { let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_block_rect( &mut dec, &mut cdfs, &mut neighbours, at32, 16, 32, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; decode_block( &mut dec, &mut cdfs, &mut neighbours, (at32.0, at32.1 + 1), SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; decode_block( &mut dec, &mut cdfs, &mut neighbours, (at32.0 + 1, at32.1 + 1), SUB, TxbSet::Luma16, TxbSet::Chroma8, TX16, TX8, (scan16, scan8), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, scan32, scan16, scan8, scan4, tx_select, reduced_tx_set, fctx, )?; } PARTITION_HORZ_4 | PARTITION_VERT_4 => { let horz = part32 == PARTITION_HORZ_4; let base_mi = (at32.0 * (SUB / MI), at32.1 * (SUB / MI)); let mut prev: Option<((usize, usize), usize)> = None; for i in 0..4 { let strip_mi = if horz { (base_mi.0 + i * 2, base_mi.1) } else { (base_mi.0, base_mi.1 + i * 2) }; if i > 0 && (strip_mi.0 >= mi_rows as usize || strip_mi.1 >= mi_cols as usize) { break; } let (bw, bh) = if horz { (32, 8) } else { (8, 32) }; let (mode, _uv) = decode_block_rect4( &mut dec, &mut cdfs, &mut neighbours, strip_mi, bw, bh, prev, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, fctx, )?; prev = Some((strip_mi, mode)); } } _ => { return Err(unsupported(format!( "a 32x32 partition type this decoder does not code (value={part32})" ))); } } } } PARTITION_HORZ => { decode_block_rect64( &mut dec, &mut cdfs, &mut neighbours, at, 64, 32, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; if has_rows { decode_block_rect64( &mut dec, &mut cdfs, &mut neighbours, (at.0 + 2, at.1), 64, 32, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; } } PARTITION_VERT => { decode_block_rect64( &mut dec, &mut cdfs, &mut neighbours, at, 32, 64, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; if has_cols { decode_block_rect64( &mut dec, &mut cdfs, &mut neighbours, (at.0, at.1 + 2), 32, 64, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; } } PARTITION_HORZ_A | PARTITION_HORZ_B | PARTITION_VERT_A | PARTITION_VERT_B => { let _vert_ab = (part == PARTITION_VERT_A || part == PARTITION_VERT_B) .then(crate::encode::Reach::vert_ab_partition); macro_rules! square32 { ($at:expr) => { decode_block( &mut dec, &mut cdfs, &mut neighbours, $at, BLOCK, TxbSet::Luma32, TxbSet::Chroma16, TX32, TX16, (&scan32, &scan16), true, &mut y, &mut u, &mut v, base_q_idx, enable_filter_intra, allow_screen_content_tools, allow_intrabc, &scan32, &scan16, &scan8, &scan4, tx_select, reduced_tx_set, fctx, )? }; } macro_rules! strip64 { ($at:expr, $bw:expr, $bh:expr) => { decode_block_rect64( &mut dec, &mut cdfs, &mut neighbours, $at, $bw, $bh, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )? }; } let (br, bc) = (at.0 + 2, at.1 + 2); match part { PARTITION_HORZ_A => { square32!(at); square32!((at.0, bc)); strip64!((br, at.1), 64, 32); } PARTITION_HORZ_B => { strip64!(at, 64, 32); square32!((br, at.1)); square32!((br, bc)); } PARTITION_VERT_A => { square32!(at); square32!((br, at.1)); strip64!((at.0, bc), 32, 64); } _ => { strip64!(at, 32, 64); square32!((at.0, bc)); square32!((br, bc)); } } } PARTITION_HORZ_4 | PARTITION_VERT_4 => { let horz = part == PARTITION_HORZ_4; for i in 0..4 { let step_mi = i * (SB_MI as usize / 4); let (this_r_mi, this_c_mi) = if horz { ( sb_r as usize * SB_MI as usize + step_mi, sb_c as usize * SB_MI as usize, ) } else { ( sb_r as usize * SB_MI as usize, sb_c as usize * SB_MI as usize + step_mi, ) }; if i > 0 && (this_r_mi >= mi_rows as usize || this_c_mi >= mi_cols as usize) { break; } let strip_at = if horz { (at.0 + i, at.1) } else { (at.0, at.1 + i) }; let (bw, bh) = if horz { (64, 16) } else { (16, 64) }; decode_block_rect64( &mut dec, &mut cdfs, &mut neighbours, strip_at, bw, bh, &mut y, &mut u, &mut v, enable_filter_intra, allow_screen_content_tools, base_q_idx, tx_select, reduced_tx_set, fctx, )?; } } _ => { return Err(unsupported( "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", )); } } } } crate::timeline::tile_done(); if tile_num == tile_info.context_update_tile_id { result_cdfs = cdfs; } } crate::timeline::recon_done(); if superres(fctx).is_none() && !(0..3).any(|p| { lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None && lr.loop_restoration_size[p] != 0 }) { capture_filter_replay( &y, &u, &v, &neighbours, frame_width as usize, frame_height as usize, width, height, true_width, true_height, ); } apply_deblock( &mut y, &mut u, &mut v, loop_filter, &neighbours, frame_width as usize, frame_height as usize, fctx, ); let lr_active = (0..3).any(|p| { lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None && lr.loop_restoration_size[p] != 0 }); let deblocked = lr_active.then(|| { ( plane_snapshot(&y, 0), plane_snapshot(&u, ss_y(fctx) as u32), plane_snapshot(&v, ss_y(fctx) as u32), ) }); apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours, fctx, None, None); capture_stages(&y, &u, &v, deblocked.as_ref(), fctx); let mut deblocked = deblocked; let mut lr_w = frame_width as usize; if let Some((upscaled_w, _)) = superres(fctx) { let bd = u32::from(bit_depth(fctx)); let (uw, fw, fh) = ( upscaled_w as usize, frame_width as usize, frame_height as usize, ); let up = |p: &PlaneBuf<'_>, in_w: usize, in_h: usize, out_w: usize| PlaneBuf { data: std::borrow::Cow::Owned(crate::superres::upscale_plane_strided( &p.data, p.width, in_w, in_h, p.true_width, out_w, bd, )), width: out_w, height: in_h, true_width: out_w, true_height: in_h, tile_x0: 0, tile_y0: 0, tile_x1: out_w, tile_y1: in_h, plane: p.plane, }; let (cw, ch, cuw) = ( round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx)), round_ss(uw, ss_x(fctx)), ); y = up(&y, fw, fh, uw); u = up(&u, cw, ch, cuw); v = up(&v, cw, ch, cuw); if let Some((dy, du, dv)) = deblocked.take() { deblocked = Some(( up(&dy, fw, fh, uw), up(&du, cw, ch, cuw), up(&dv, cw, ch, cuw), )); } lr_w = uw; crate::superres::note_upscaled_picture(); } if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { apply_loop_restoration( &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, (lr_w, frame_height as usize), fctx, ); } if let Some((db_y, db_u, db_v)) = deblocked { scratch_return(db_v.data.into_owned()); scratch_return(db_u.data.into_owned()); scratch_return(db_y.data.into_owned()); } crate::timeline::filters_done(); let (fw, fh) = (lr_w, frame_height as usize); let (true_width, true_height) = if superres(fctx).is_some() { (fw, fh) } else { (true_width, true_height) }; if fw == width && fh == height { *fctx.last_frame_wide_margin.borrow_mut() = None; return Ok(( Picture { width, height, y: y.data.into_owned(), u: u.data.into_owned(), v: v.data.into_owned(), }, result_cdfs, )); } let crop = |plane: &PlaneBuf<'_>, w: usize, h: usize| -> Vec { let mut out = Vec::with_capacity(w * h); for row in 0..h { out.extend(plane.data[row * plane.width..][..w].iter().copied()); } out }; *fctx.last_frame_wide_margin.borrow_mut() = if true_width > fw || true_height > fh { let yc = crop(&y, true_width, true_height); Some(Picture { width: true_width, height: true_height, y: yc, u: crop( &u, round_ss(true_width, ss_x(fctx)), round_ss(true_height, ss_y(fctx)), ), v: crop( &v, round_ss(true_width, ss_x(fctx)), round_ss(true_height, ss_y(fctx)), ), }) } else { None }; Ok(( Picture { width: fw, height: fh, y: crop_owned(y, fw, fh), u: crop_owned(u, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), v: crop_owned(v, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), }, result_cdfs, )) }