Skip to content

Commit

Permalink
Rav1dFrameContext_frame_thread::pal: Make into a Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison committed Feb 9, 2024
1 parent 90fa006 commit b1ffbf9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
39 changes: 14 additions & 25 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ unsafe fn read_pal_plane(
let not_pl = !pl as u16;

let ts = &mut *t.ts;
let f = &*t.f;
let f = &mut *t.f;

// Must come before `pal`, which mutably borrows `t`.
// TODO: `DEBUG_BLOCK_INFO` really should take a subset of `f` and `t`,
Expand Down Expand Up @@ -793,10 +793,8 @@ unsafe fn read_pal_plane(

// parse new entries
let pal = if t.frame_thread.pass != 0 {
&mut (*(f.frame_thread.pal).offset(
((t.by >> 1) + (t.bx & 1)) as isize * (f.b4_stride >> 1)
+ ((t.bx >> 1) + (t.by & 1)) as isize,
))[pli]
&mut f.frame_thread.pal[(((t.by >> 1) + (t.bx & 1)) as isize * (f.b4_stride >> 1)
+ ((t.bx >> 1) + (t.by & 1)) as isize) as usize][pli]
} else {
&mut t.scratch.c2rust_unnamed_0.pal[pli]
};
Expand Down Expand Up @@ -874,17 +872,15 @@ unsafe fn read_pal_uv(

// V pal coding
let ts = &mut *t.ts;
let f = &*t.f;
let f = &mut *t.f;

// Hoisted so the `&` borrow of `t`
// doesn't conflict with `pal`'s `&mut` borrow of `t`.
let dbg = DEBUG_BLOCK_INFO(&*f, &*t);

let pal = if t.frame_thread.pass != 0 {
&mut (*(f.frame_thread.pal).offset(
((t.by >> 1) + (t.bx & 1)) as isize * (f.b4_stride >> 1)
+ ((t.bx >> 1) + (t.by & 1)) as isize,
))[2]
&mut f.frame_thread.pal[(((t.by >> 1) + (t.bx & 1)) as isize * (f.b4_stride >> 1)
+ ((t.bx >> 1) + (t.by & 1)) as isize) as usize][2]
} else {
&mut t.scratch.c2rust_unnamed_0.pal[2]
};
Expand Down Expand Up @@ -2226,7 +2222,7 @@ unsafe fn decode_b(
let pal = if t.frame_thread.pass != 0 {
let index = ((t.by >> 1) + (t.bx & 1)) as isize * (f.b4_stride >> 1)
+ ((t.bx >> 1) + (t.by & 1)) as isize;
&(*f.frame_thread.pal.offset(index))[0]
&f.frame_thread.pal[index as usize][0]
} else {
&t.scratch.c2rust_unnamed_0.pal[0]
};
Expand All @@ -2250,7 +2246,7 @@ unsafe fn decode_b(
let pal = if t.frame_thread.pass != 0 {
let index = ((t.by >> 1) + (t.bx & 1)) as isize * (f.b4_stride >> 1)
+ ((t.bx >> 1) + (t.by & 1)) as isize;
&*f.frame_thread.pal.offset(index)
&f.frame_thread.pal[index as usize]
} else {
&t.scratch.c2rust_unnamed_0.pal
};
Expand Down Expand Up @@ -4361,17 +4357,10 @@ pub(crate) unsafe fn rav1d_decode_frame_init(

if frame_hdr.allow_screen_content_tools != 0 {
if num_sb128 != f.frame_thread.pal_sz {
rav1d_freep_aligned(
&mut f.frame_thread.pal as *mut *mut [[u16; 8]; 3] as *mut c_void,
);
f.frame_thread.pal = rav1d_alloc_aligned(
::core::mem::size_of::<[[u16; 8]; 3]>() * num_sb128 as usize * 16 * 16,
64,
) as *mut [[u16; 8]; 3];
if f.frame_thread.pal.is_null() {
f.frame_thread.pal_sz = 0;
return Err(ENOMEM);
}
// TODO: Fallible allocation
f.frame_thread
.pal
.resize_with(num_sb128 as usize * 16 * 16, Default::default);
f.frame_thread.pal_sz = num_sb128;
}

Expand All @@ -4388,8 +4377,8 @@ pub(crate) unsafe fn rav1d_decode_frame_init(
}
f.frame_thread.pal_idx_sz = pal_idx_sz;
}
} else if !f.frame_thread.pal.is_null() {
rav1d_freep_aligned(&mut f.frame_thread.pal as *mut *mut [[u16; 8]; 3] as *mut c_void);
} else if !f.frame_thread.pal.is_empty() {
let _ = mem::take(&mut f.frame_thread.pal);
rav1d_freep_aligned(&mut f.frame_thread.pal_idx as *mut *mut u8 as *mut c_void);
f.frame_thread.pal_idx_sz = 0;
f.frame_thread.pal_sz = f.frame_thread.pal_idx_sz;
Expand Down
2 changes: 1 addition & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub struct Rav1dFrameContext_frame_thread {
pub b: Vec<Av1Block>,
pub cbi: Vec<CodedBlockInfo>,
// indexed using (t->by >> 1) * (f->b4_stride >> 1) + (t->bx >> 1)
pub pal: *mut [[u16; 8]; 3], /* [3 plane][8 idx] */
pub pal: Vec<[[u16; 8]; 3]>, /* [3 plane][8 idx] */
// iterated over inside tile state
pub pal_idx: *mut u8,
pub cf: *mut DynCoef,
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,9 +958,7 @@ impl Drop for Rav1dContext {
&mut (*f).frame_thread.cf as *mut *mut DynCoef as *mut c_void,
);
freep(&mut (*f).frame_thread.tile_start_off as *mut *mut c_int as *mut c_void);
rav1d_freep_aligned(
&mut (*f).frame_thread.pal as *mut *mut [[u16; 8]; 3] as *mut c_void,
);
let _ = mem::take(&mut (*f).frame_thread.pal); // TODO: remove when context is owned
let _ = mem::take(&mut (*f).frame_thread.cbi); // TODO: remove when context is owned
}
if self.n_tc > 1 as c_uint {
Expand Down
17 changes: 7 additions & 10 deletions src/recon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2568,11 +2568,10 @@ pub(crate) unsafe fn rav1d_recon_b_intra<BD: BitDepth>(
pal_idx = (t.scratch.c2rust_unnamed_0.pal_idx).as_mut_ptr();
}
let pal: *const u16 = if t.frame_thread.pass != 0 {
((*((*f).frame_thread.pal).offset(
let index =
(((t.by as isize >> 1) + (t.bx as isize & 1)) * ((*f).b4_stride >> 1)
+ ((t.bx >> 1) + (t.by & 1)) as isize) as isize,
))[0])
.as_mut_ptr()
+ ((t.bx >> 1) + (t.by & 1)) as isize) as isize;
(*f).frame_thread.pal[index as usize][0].as_mut_ptr()
} else {
(t.scratch.c2rust_unnamed_0.pal[0]).as_mut_ptr()
};
Expand Down Expand Up @@ -2919,12 +2918,10 @@ pub(crate) unsafe fn rav1d_recon_b_intra<BD: BitDepth>(
if ((*ts).frame_thread[p as usize].pal_idx).is_null() {
unreachable!();
}
pal = (*((*f).frame_thread.pal).offset(
(((t.by >> 1) + (t.bx & 1)) as isize * ((*f).b4_stride >> 1)
+ ((t.bx as isize >> 1) as isize + (t.by as isize & 1)) as isize)
as isize,
))
.as_mut_ptr() as *const [u16; 8];
let index = (((t.by >> 1) + (t.bx & 1)) as isize * ((*f).b4_stride >> 1)
+ ((t.bx as isize >> 1) as isize + (t.by as isize & 1)) as isize)
as isize;
pal = &(*f).frame_thread.pal[index as usize][0] as *const [u16; 8];
pal_idx = (*ts).frame_thread[p as usize].pal_idx;
(*ts).frame_thread[p as usize].pal_idx = ((*ts).frame_thread[p as usize]
.pal_idx)
Expand Down

0 comments on commit b1ffbf9

Please sign in to comment.