Skip to content

Commit

Permalink
struct Rav1dFrameData::{mvs,ref_mvs}: Arcify with Option<Disjoi…
Browse files Browse the repository at this point in the history
…ntMutArcSlice<refmvs_temporal_block>>`s.
  • Loading branch information
kkysen committed Apr 19, 2024
1 parent 3eb384e commit 6420e5a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 128 deletions.
63 changes: 22 additions & 41 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,13 @@ use crate::src::picture::rav1d_thread_picture_ref;
use crate::src::picture::rav1d_thread_picture_unref;
use crate::src::picture::Rav1dThreadPicture;
use crate::src::qm::dav1d_qm_tbl;
use crate::src::r#ref::rav1d_ref_create_using_pool;
use crate::src::r#ref::rav1d_ref_dec;
use crate::src::r#ref::rav1d_ref_inc;
use crate::src::recon::debug_block_info;
use crate::src::refmvs::rav1d_refmvs_find;
use crate::src::refmvs::rav1d_refmvs_init_frame;
use crate::src::refmvs::rav1d_refmvs_tile_sbrow_init;
use crate::src::refmvs::refmvs_block;
use crate::src::refmvs::refmvs_mvpair;
use crate::src::refmvs::refmvs_refpair;
use crate::src::refmvs::refmvs_temporal_block;
use crate::src::refmvs::RefMvsFrame;
use crate::src::tables::cfl_allowed_mask;
use crate::src::tables::dav1d_al_part_ctx;
Expand Down Expand Up @@ -183,7 +179,6 @@ use std::ffi::c_uint;
use std::ffi::c_void;
use std::iter;
use std::mem;
use std::ptr;
use std::ptr::addr_of_mut;
use std::slice;
use std::sync::atomic::AtomicI32;
Expand Down Expand Up @@ -3913,7 +3908,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(
if c.tc.len() > 1 && frame_hdr.use_ref_frame_mvs != 0 {
c.refmvs_dsp.load_tmvs(
&f.rf,
f.mvs,
&f.mvs,
&f.ref_mvs,
ts.tiling.row,
ts.tiling.col_start >> 1,
Expand Down Expand Up @@ -4025,7 +4020,7 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(
c.refmvs_dsp.save_tmvs(
&t.rt,
&f.rf,
f.mvs,
&f.mvs,
ts.tiling.col_start >> 1,
ts.tiling.col_end >> 1,
t.b.y >> 1,
Expand Down Expand Up @@ -4518,7 +4513,7 @@ unsafe fn rav1d_decode_frame_main(c: &Rav1dContext, f: &mut Rav1dFrameData) -> R
if frame_hdr.use_ref_frame_mvs != 0 {
c.refmvs_dsp.load_tmvs(
&f.rf,
f.mvs,
&f.mvs,
&f.ref_mvs,
tile_row as c_int,
0,
Expand All @@ -4533,7 +4528,7 @@ unsafe fn rav1d_decode_frame_main(c: &Rav1dContext, f: &mut Rav1dFrameData) -> R
}
if f.frame_hdr().frame_type.is_inter_or_switch() {
c.refmvs_dsp
.save_tmvs(&t.rt, &f.rf, f.mvs, 0, f.bw >> 1, t.b.y >> 1, by_end);
.save_tmvs(&t.rt, &f.rf, &f.mvs, 0, f.bw >> 1, t.b.y >> 1, by_end);
}

// loopfilter + cdef + restoration
Expand All @@ -4560,7 +4555,7 @@ pub(crate) unsafe fn rav1d_decode_frame_exit(
if f.refp[i].p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut f.refp[i]);
}
rav1d_ref_dec(&mut f.ref_mvs_ref[i]);
let _ = mem::take(&mut f.ref_mvs[i]);
}
rav1d_picture_unref_internal(&mut f.cur);
rav1d_thread_picture_unref(&mut f.sr_cur);
Expand All @@ -4579,7 +4574,7 @@ pub(crate) unsafe fn rav1d_decode_frame_exit(

let _ = mem::take(&mut f.cur_segmap);
let _ = mem::take(&mut f.prev_segmap);
rav1d_ref_dec(&mut f.mvs_ref);
let _ = mem::take(&mut f.mvs);
let _ = mem::take(&mut f.seq_hdr);
let _ = mem::take(&mut f.frame_hdr);
f.tiles.clear();
Expand Down Expand Up @@ -4710,12 +4705,12 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
if f.refp[i].p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut f.refp[i]);
}
rav1d_ref_dec(&mut f.ref_mvs_ref[i]);
let _ = mem::take(&mut f.ref_mvs[i]);
}
rav1d_thread_picture_unref(out);
rav1d_picture_unref_internal(&mut f.cur);
rav1d_thread_picture_unref(&mut f.sr_cur);
rav1d_ref_dec(&mut f.mvs_ref);
let _ = mem::take(&mut f.mvs);
let _ = mem::take(&mut f.seq_hdr);
let _ = mem::take(&mut f.frame_hdr);
*c.cached_error_props.lock().unwrap() = c.in_0.m.clone();
Expand Down Expand Up @@ -4900,18 +4895,12 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {

// ref_mvs
if frame_hdr.frame_type.is_inter_or_switch() || frame_hdr.allow_intrabc {
f.mvs_ref = rav1d_ref_create_using_pool(
c.refmvs_pool,
::core::mem::size_of::<refmvs_temporal_block>()
* f.sb128h as usize
* 16
* (f.b4_stride >> 1) as usize,
// TODO fallible allocation
f.mvs = Some(
(0..f.sb128h as usize * 16 * (f.b4_stride >> 1) as usize)
.map(|_| Default::default())
.collect(),
);
if f.mvs_ref.is_null() {
on_error(f, c, out);
return Err(ENOMEM);
}
f.mvs = (*f.mvs_ref).data.cast::<refmvs_temporal_block>();
if !frame_hdr.allow_intrabc {
for i in 0..7 {
f.refpoc[i] = f.refp[i].p.frame_hdr.as_ref().unwrap().frame_offset as c_uint;
Expand All @@ -4924,24 +4913,19 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
let refidx = frame_hdr.refidx[i] as usize;
let ref_w = (ref_coded_width[i] + 7 >> 3) << 1;
let ref_h = (f.refp[i].p.p.h + 7 >> 3) << 1;
if !c.refs[refidx].refmvs.is_null() && ref_w == f.bw && ref_h == f.bh {
f.ref_mvs_ref[i] = c.refs[refidx].refmvs;
rav1d_ref_inc(f.ref_mvs_ref[i]);
f.ref_mvs[i] = (*c.refs[refidx].refmvs)
.data
.cast::<refmvs_temporal_block>();
if ref_w == f.bw && ref_h == f.bh {
f.ref_mvs[i] = c.refs[refidx].refmvs.clone();
} else {
f.ref_mvs[i] = ptr::null_mut();
f.ref_mvs_ref[i] = ptr::null_mut();
f.ref_mvs[i] = None;
}
f.refrefpoc[i] = c.refs[refidx].refpoc;
}
} else {
f.ref_mvs_ref.fill_with(ptr::null_mut);
f.ref_mvs.fill_with(Default::default);
}
} else {
f.mvs_ref = ptr::null_mut();
f.ref_mvs_ref.fill_with(ptr::null_mut);
f.mvs = None;
f.ref_mvs.fill_with(Default::default);
}

// segmap
Expand Down Expand Up @@ -5006,12 +4990,9 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
}

c.refs[i].segmap = f.cur_segmap.clone();
rav1d_ref_dec(&mut c.refs[i].refmvs);
let _ = mem::take(&mut c.refs[i].refmvs);
if !frame_hdr.allow_intrabc {
c.refs[i].refmvs = f.mvs_ref;
if !f.mvs_ref.is_null() {
rav1d_ref_inc(f.mvs_ref);
}
c.refs[i].refmvs = f.mvs.clone();
}
c.refs[i].refpoc = f.refpoc;
}
Expand All @@ -5028,7 +5009,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
}
let _ = mem::take(&mut c.cdf[i]);
let _ = mem::take(&mut c.refs[i].segmap);
rav1d_ref_dec(&mut c.refs[i].refmvs);
let _ = mem::take(&mut c.refs[i].refmvs);
}
}
on_error(f, c, out);
Expand Down
9 changes: 3 additions & 6 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ use crate::src::mem::Rav1dMemPool;
use crate::src::msac::MsacContext;
use crate::src::picture::PictureFlags;
use crate::src::picture::Rav1dThreadPicture;
use crate::src::r#ref::Rav1dRef;
use crate::src::recon::backup_ipred_edge_fn;
use crate::src::recon::copy_pal_block_fn;
use crate::src::recon::filter_sbrow_fn;
Expand Down Expand Up @@ -225,7 +224,7 @@ pub(crate) struct TaskThreadData {
pub(crate) struct Rav1dContext_refs {
pub p: Rav1dThreadPicture,
pub segmap: Option<DisjointMutArcSlice<u8>>,
pub refmvs: *mut Rav1dRef,
pub refmvs: Option<DisjointMutArcSlice<refmvs_temporal_block>>,
pub refpoc: [c_uint; 7],
}

Expand Down Expand Up @@ -738,10 +737,8 @@ pub(crate) struct Rav1dFrameData {
pub cur: Rav1dPicture,
// after super-resolution upscaling
pub sr_cur: Rav1dThreadPicture,
pub mvs_ref: *mut Rav1dRef,
pub mvs: *mut refmvs_temporal_block,
pub ref_mvs: [*mut refmvs_temporal_block; 7],
pub ref_mvs_ref: [*mut Rav1dRef; 7],
pub mvs: Option<DisjointMutArcSlice<refmvs_temporal_block>>, // Previously pooled.
pub ref_mvs: [Option<DisjointMutArcSlice<refmvs_temporal_block>>; 7],
pub cur_segmap: Option<DisjointMutArcSlice<u8>>, // Previously pooled.
pub prev_segmap: Option<DisjointMutArcSlice<u8>>,
pub refpoc: [c_uint; 7],
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ use crate::src::picture::rav1d_thread_picture_ref;
use crate::src::picture::rav1d_thread_picture_unref;
use crate::src::picture::PictureFlags;
use crate::src::picture::Rav1dThreadPicture;
use crate::src::r#ref::rav1d_ref_dec;
use crate::src::refmvs::rav1d_refmvs_clear;
use crate::src::refmvs::rav1d_refmvs_dsp_init;
use crate::src::refmvs::rav1d_refmvs_init;
Expand Down Expand Up @@ -679,7 +678,7 @@ pub(crate) unsafe fn rav1d_flush(c: *mut Rav1dContext) {
rav1d_thread_picture_unref(&mut (*((*c).refs).as_mut_ptr().offset(i as isize)).p);
}
let _ = mem::take(&mut (*c).refs[i as usize].segmap);
rav1d_ref_dec(&mut (*((*c).refs).as_mut_ptr().offset(i as isize)).refmvs);
let _ = mem::take(&mut (*c).refs[i as usize].refmvs);
let _ = mem::take(&mut (*c).cdf[i]);
i += 1;
}
Expand Down Expand Up @@ -850,7 +849,7 @@ impl Drop for Rav1dContext {
&mut (*(self.refs).as_mut_ptr().offset(n_4 as isize)).p,
);
}
rav1d_ref_dec(&mut (*(self.refs).as_mut_ptr().offset(n_4 as isize)).refmvs);
let _ = mem::take(&mut self.refs[n_4 as usize].refmvs);
let _ = mem::take(&mut self.refs[n_4 as usize].segmap);
n_4 += 1;
}
Expand Down
5 changes: 2 additions & 3 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ use crate::src::picture::rav1d_picture_copy_props;
use crate::src::picture::rav1d_thread_picture_ref;
use crate::src::picture::rav1d_thread_picture_unref;
use crate::src::picture::PictureFlags;
use crate::src::r#ref::rav1d_ref_dec;
use crate::src::thread_task::FRAME_ERROR;
use std::array;
use std::cmp;
Expand Down Expand Up @@ -2296,7 +2295,7 @@ unsafe fn parse_obus(
rav1d_thread_picture_unref(&mut c.refs[i as usize].p);
}
let _ = mem::take(&mut c.refs[i as usize].segmap);
rav1d_ref_dec(&mut c.refs[i as usize].refmvs);
let _ = mem::take(&mut c.refs[i as usize].refmvs);
let _ = mem::take(&mut c.cdf[i]);
}
c.frame_flags
Expand Down Expand Up @@ -2622,7 +2621,7 @@ unsafe fn parse_obus(
c.cdf[i as usize] = c.cdf[r as usize].clone();

c.refs[i as usize].segmap = c.refs[r as usize].segmap.clone();
rav1d_ref_dec(&mut c.refs[i as usize].refmvs);
let _ = mem::take(&mut c.refs[i as usize].refmvs);
}
}
c.frame_hdr = None;
Expand Down
35 changes: 0 additions & 35 deletions src/ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use crate::src::mem::rav1d_mem_pool_pop;
use crate::src::mem::rav1d_mem_pool_push;
use crate::src::mem::Rav1dMemPool;
use crate::src::mem::Rav1dMemPoolBuffer;
use libc::free;
use libc::malloc;
use std::ffi::c_int;
Expand All @@ -24,37 +20,6 @@ pub unsafe fn rav1d_ref_inc(r#ref: *mut Rav1dRef) {
(*r#ref).ref_cnt.fetch_add(1, Ordering::Relaxed);
}

unsafe extern "C" fn pool_free_callback(data: *const u8, user_data: *mut c_void) {
rav1d_mem_pool_push(
data as *mut Rav1dMemPool,
user_data as *mut Rav1dMemPoolBuffer,
);
}

pub unsafe fn rav1d_ref_create_using_pool(
pool: *mut Rav1dMemPool,
mut size: usize,
) -> *mut Rav1dRef {
size = size
.wrapping_add(::core::mem::size_of::<*mut c_void>())
.wrapping_sub(1)
& !(::core::mem::size_of::<*mut c_void>()).wrapping_sub(1);
let buf: *mut Rav1dMemPoolBuffer =
rav1d_mem_pool_pop(pool, size.wrapping_add(::core::mem::size_of::<Rav1dRef>()));
if buf.is_null() {
return 0 as *mut Rav1dRef;
}
let res: *mut Rav1dRef =
&mut *(buf as *mut Rav1dRef).offset(-(1 as c_int) as isize) as *mut Rav1dRef;
(*res).data = (*buf).data;
(*res).const_data = pool as *const c_void;
(*res).ref_cnt = AtomicI32::new(1);
(*res).free_ref = 0 as c_int;
(*res).free_callback = Some(pool_free_callback);
(*res).user_data = buf as *mut c_void;
return res;
}

pub unsafe fn rav1d_ref_wrap(
ptr: *const u8,
free_callback: Option<unsafe extern "C" fn(*const u8, *mut c_void) -> ()>,
Expand Down
Loading

0 comments on commit 6420e5a

Please sign in to comment.