Skip to content

Commit

Permalink
struct Rav1dFrameContext_frame_thread: Add inner mutability (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
rinon authored Apr 22, 2024
2 parents 18b6321 + 5711842 commit 8eb6932
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 140 deletions.
8 changes: 0 additions & 8 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,6 @@ pub trait BitDepth: Clone + Copy {

fn get_intermediate_bits(&self) -> u8;

fn _cast_coef_slice(bytes: &[u8]) -> &[Self::Coef] {
Self::Coef::slice_from(bytes).unwrap()
}

fn cast_coef_slice_mut(bytes: &mut [u8]) -> &mut [Self::Coef] {
Self::Coef::mut_slice_from(bytes).unwrap()
}

const PREP_BIAS: i16;

unsafe fn select<T>(bd: &BitDepthUnion<T>) -> &T::T<Self>
Expand Down
6 changes: 3 additions & 3 deletions include/common/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub unsafe fn hex_dump<BD: BitDepth>(
#[inline]
pub fn coef_dump<Coef: Display>(buf: &[Coef], w: usize, h: usize, len: usize, what: &str) {
println!("{}", what);
for buf in buf[..w * h].chunks_exact(w).take(h) {
for x in buf {
print!(" {:0len$}", x, len = len);
for row in buf[..w * h].chunks_exact(w).take(h) {
for coef in row {
print!(" {:0len$}", coef, len = len);
}
println!();
}
Expand Down
2 changes: 1 addition & 1 deletion src/cdef_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn adjust_strength(strength: c_int, var: c_uint) -> c_int {
pub(crate) unsafe fn rav1d_cdef_brow<BD: BitDepth>(
c: &Rav1dContext,
tc: &mut Rav1dTaskContext,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
p: &[*mut BD::Pixel; 3],
lflvl_offset: i32,
by_start: c_int,
Expand Down
94 changes: 49 additions & 45 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ unsafe fn read_pal_indices(

unsafe fn read_vartx_tree(
t: &mut Rav1dTaskContext,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
b: &mut Av1Block,
bs: BlockSize,
bx4: c_int,
Expand Down Expand Up @@ -1108,40 +1108,11 @@ unsafe fn obmc_lowest_px(
unsafe fn decode_b(
c: &Rav1dContext,
t: &mut Rav1dTaskContext,
f: &mut Rav1dFrameData,
bl: BlockLevel,
bs: BlockSize,
bp: BlockPartition,
intra_edge_flags: EdgeFlags,
) -> Result<(), ()> {
// Pull out the current block from Rav1dFrameData so that we can operate on
// it without borrow check errors.
let (mut b_mem, b_idx) = if t.frame_thread.pass != 0 {
let b_idx = (t.b.y as isize * f.b4_stride + t.b.x as isize) as usize;
(mem::take(&mut f.frame_thread.b[b_idx]), Some(b_idx))
} else {
(Default::default(), None)
};
let b = &mut b_mem;
let res = decode_b_inner(c, t, f, bl, bs, bp, intra_edge_flags, b);
if let Some(i) = b_idx {
let _old_b = mem::replace(&mut f.frame_thread.b[i], b_mem);
// TODO(SJC): We should be able to compare Av1Blocks, but there are C
// unions in them.
// assert_eq!(old_b, Default::default());
}
res
}

unsafe fn decode_b_inner(
c: &Rav1dContext,
t: &mut Rav1dTaskContext,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
bl: BlockLevel,
bs: BlockSize,
bp: BlockPartition,
intra_edge_flags: EdgeFlags,
b: &mut Av1Block,
) -> Result<(), ()> {
use std::fmt;

Expand All @@ -1155,6 +1126,18 @@ unsafe fn decode_b_inner(
}
}

let mut b_guard;
let mut b_mem = Av1Block::default();
let b = if t.frame_thread.pass != 0 {
b_guard = f
.frame_thread
.b
.index_mut((t.b.y as isize * f.b4_stride + t.b.x as isize) as usize);
&mut *b_guard
} else {
&mut b_mem
};

let ts = &mut *f.ts.offset(t.ts as isize);
let bd_fn = f.bd_fn();
let b_dim = &dav1d_block_dimensions[bs as usize];
Expand Down Expand Up @@ -1784,13 +1767,17 @@ unsafe fn decode_b_inner(
}

if b.pal_sz()[0] != 0 {
let mut pal_idx_guard;
let pal_idx = if t.frame_thread.pass != 0 {
let p = t.frame_thread.pass & 1;
let frame_thread = &mut ts.frame_thread[p as usize];
let len = usize::try_from(bw4 * bh4 * 16).unwrap();
let pal_idx = &mut f.frame_thread.pal_idx[frame_thread.pal_idx..][..len];
pal_idx_guard = f
.frame_thread
.pal_idx
.index_mut(frame_thread.pal_idx..frame_thread.pal_idx + len);
frame_thread.pal_idx += len;
pal_idx
&mut *pal_idx_guard
} else {
&mut t.scratch.c2rust_unnamed_0.pal_idx
};
Expand All @@ -1811,13 +1798,17 @@ unsafe fn decode_b_inner(
}

if has_chroma && b.pal_sz()[1] != 0 {
let mut pal_idx_guard;
let pal_idx = if t.frame_thread.pass != 0 {
let p = t.frame_thread.pass & 1;
let frame_thread = &mut ts.frame_thread[p as usize];
let len = usize::try_from(cbw4 * cbh4 * 16).unwrap();
let pal_idx = &mut f.frame_thread.pal_idx[frame_thread.pal_idx..][..len];
pal_idx_guard = f
.frame_thread
.pal_idx
.index_mut(frame_thread.pal_idx..frame_thread.pal_idx + len);
frame_thread.pal_idx += len;
pal_idx
&mut *pal_idx_guard
} else {
&mut t.scratch.c2rust_unnamed_0.pal_idx[(bw4 * bh4 * 16) as usize..]
};
Expand Down Expand Up @@ -1879,7 +1870,7 @@ unsafe fn decode_b_inner(
TileStateRef::Local => &ts.lflvlmem,
};
rav1d_create_lf_mask_intra(
&mut f.lf.mask[t.lf_mask.unwrap()],
&f.lf.mask[t.lf_mask.unwrap()],
&f.lf.level,
f.b4_stride,
&lflvl[b.seg_id as usize],
Expand Down Expand Up @@ -2897,7 +2888,7 @@ unsafe fn decode_b_inner(
TileStateRef::Local => &ts.lflvlmem,
};
rav1d_create_lf_mask_inter(
&mut f.lf.mask[t.lf_mask.unwrap()],
&f.lf.mask[t.lf_mask.unwrap()],
&f.lf.level,
f.b4_stride,
// In C, the inner dimensions (`ref`, `is_gmv`) are offset,
Expand Down Expand Up @@ -2989,7 +2980,7 @@ unsafe fn decode_b_inner(
if b.skip == 0 {
let mask = !0u32 >> 32 - bw4 << (bx4 & 15);
let bx_idx = (bx4 & 16) >> 4;
for noskip_mask in &mut f.lf.mask[t.lf_mask.unwrap()].noskip_mask[by4 as usize >> 1..]
for noskip_mask in &f.lf.mask[t.lf_mask.unwrap()].noskip_mask[by4 as usize >> 1..]
[..(bh4 as usize + 1) / 2]
{
noskip_mask[bx_idx as usize].fetch_or(mask as u16, Ordering::Relaxed);
Expand Down Expand Up @@ -3237,7 +3228,7 @@ unsafe fn decode_b_inner(
unsafe fn decode_sb(
c: &Rav1dContext,
t: &mut Rav1dTaskContext,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
bl: BlockLevel,
edge_index: EdgeIndex,
) -> Result<(), ()> {
Expand Down Expand Up @@ -3310,7 +3301,10 @@ unsafe fn decode_sb(
);
}
} else {
let b = &f.frame_thread.b[(t.b.y as isize * f.b4_stride + t.b.x as isize) as usize];
let b = f
.frame_thread
.b
.index((t.b.y as isize * f.b4_stride + t.b.x as isize) as usize);
bp = if b.bl == bl {
b.bp
} else {
Expand Down Expand Up @@ -3471,7 +3465,10 @@ unsafe fn decode_sb(
);
}
} else {
let b = &f.frame_thread.b[(t.b.y as isize * f.b4_stride + t.b.x as isize) as usize];
let b = &f
.frame_thread
.b
.index((t.b.y as isize * f.b4_stride + t.b.x as isize) as usize);
is_split = b.bl != bl;
}

Expand Down Expand Up @@ -3524,7 +3521,10 @@ unsafe fn decode_sb(
);
}
} else {
let b = &f.frame_thread.b[(t.b.y as isize * f.b4_stride + t.b.x as isize) as usize];
let b = &f
.frame_thread
.b
.index((t.b.y as isize * f.b4_stride + t.b.x as isize) as usize);
is_split = b.bl != bl;
}

Expand Down Expand Up @@ -4123,7 +4123,10 @@ pub(crate) unsafe fn rav1d_decode_frame_init(

let cf_sz = (num_sb128 * size_mul[0] as c_int) << hbd;
// TODO: Fallible allocation
f.frame_thread.cf.resize(cf_sz as usize * 128 * 128 / 2, 0);
f.frame_thread
.cf
.get_mut()
.resize(cf_sz as usize * 128 * 128 / 2, 0);

if frame_hdr.allow_screen_content_tools {
// TODO: Fallible allocation
Expand Down Expand Up @@ -4539,8 +4542,9 @@ pub(crate) unsafe fn rav1d_decode_frame_exit(
if !f.sr_cur.p.data.data[0].is_null() {
f.task_thread.error = AtomicI32::new(0);
}
if c.n_fc > 1 && retval.is_err() && !f.frame_thread.cf.is_empty() {
f.frame_thread.cf.fill_with(Default::default)
let cf = f.frame_thread.cf.get_mut();
if c.n_fc > 1 && retval.is_err() && !cf.is_empty() {
cf.fill_with(Default::default);
}
// TODO(kkysen) use array::zip when stable
for i in 0..7 {
Expand Down
14 changes: 7 additions & 7 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ pub(crate) struct Rav1dFrameContext_bd_fn {
pub filter_sbrow_deblock_cols: filter_sbrow_fn,
pub filter_sbrow_deblock_rows: filter_sbrow_fn,
pub filter_sbrow_cdef:
unsafe fn(&Rav1dContext, &mut Rav1dFrameData, &mut Rav1dTaskContext, c_int) -> (),
unsafe fn(&Rav1dContext, &Rav1dFrameData, &mut Rav1dTaskContext, c_int) -> (),
pub filter_sbrow_resize: filter_sbrow_fn,
pub filter_sbrow_lr: filter_sbrow_fn,
pub backup_ipred_edge: backup_ipred_edge_fn,
Expand Down Expand Up @@ -444,7 +444,7 @@ impl Rav1dFrameContext_bd_fn {

pub unsafe fn recon_b_intra(
&self,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
context: &mut Rav1dTaskContext,
block_size: BlockSize,
flags: EdgeFlags,
Expand All @@ -455,7 +455,7 @@ impl Rav1dFrameContext_bd_fn {

pub unsafe fn recon_b_inter(
&self,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
context: &mut Rav1dTaskContext,
block_size: BlockSize,
block: &Av1Block,
Expand All @@ -465,7 +465,7 @@ impl Rav1dFrameContext_bd_fn {

pub unsafe fn read_coef_blocks(
&self,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
context: &mut Rav1dTaskContext,
block_size: BlockSize,
block: &Av1Block,
Expand Down Expand Up @@ -548,7 +548,7 @@ pub struct Rav1dFrameContext_frame_thread {
pub next_tile_row: [AtomicI32; 2],

/// Indexed using `t.b.y * f.b4_stride + t.b.x`.
pub b: Vec<Av1Block>,
pub b: DisjointMut<Vec<Av1Block>>,

pub cbi: Vec<[Atomic<CodedBlockInfo>; 3]>,

Expand All @@ -559,10 +559,10 @@ pub struct Rav1dFrameContext_frame_thread {
pub pal: Pal,

/// Iterated over inside tile state.
pub pal_idx: AlignedVec64<u8>,
pub pal_idx: DisjointMut<AlignedVec64<u8>>,

/// [`AlignedVec64`]`<`[`DynCoef`]`>`
pub cf: AlignedVec64<u8>,
pub cf: DisjointMut<AlignedVec64<u8>>,

/// Start offsets per tile
pub tile_start_off: Vec<u32>,
Expand Down
6 changes: 3 additions & 3 deletions src/lf_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,14 @@ unsafe fn filter_plane_rows_uv<BD: BitDepth>(
}

pub(crate) unsafe fn rav1d_loopfilter_sbrow_cols<BD: BitDepth>(
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
p: &mut [&mut [BD::Pixel]; 3],
p_offset: &[usize; 2],
lflvl_offset: usize,
sby: c_int,
start_of_tile_row: c_int,
) {
let lflvl = &mut f.lf.mask[lflvl_offset..];
let lflvl = &f.lf.mask[lflvl_offset..];
let mut have_left; // Don't filter outside the frame
let seq_hdr = &***f.seq_hdr.as_ref().unwrap();
let is_sb64 = (seq_hdr.sb128 == 0) as c_int;
Expand Down Expand Up @@ -751,7 +751,7 @@ pub(crate) unsafe fn rav1d_loopfilter_sbrow_cols<BD: BitDepth>(
}

pub(crate) unsafe fn rav1d_loopfilter_sbrow_rows<BD: BitDepth>(
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
p: &mut [&mut [BD::Pixel]; 3],
p_offset: &[usize; 2],
lflvl_offset: usize,
Expand Down
2 changes: 1 addition & 1 deletion src/lr_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ unsafe fn lr_sbrow<BD: BitDepth>(

pub(crate) unsafe fn rav1d_lr_sbrow<BD: BitDepth>(
c: &Rav1dContext,
f: &mut Rav1dFrameData,
f: &Rav1dFrameData,
dst: &mut [&mut [BD::Pixel]; 3],
dst_offset: &[usize; 2],
sby: c_int,
Expand Down
Loading

0 comments on commit 8eb6932

Please sign in to comment.