Skip to content

Commit

Permalink
fn rav1d_{thread_,}picture_{ref,move_ref,unref}: Replace with `.clo…
Browse files Browse the repository at this point in the history
…ne()` and `mem::take` (#996)
  • Loading branch information
kkysen authored Apr 24, 2024
2 parents 83bbcc9 + f4ff654 commit 5af3bd0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 104 deletions.
42 changes: 19 additions & 23 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ use crate::src::msac::rav1d_msac_decode_uniform;
use crate::src::msac::rav1d_msac_init;
use crate::src::pal::Rav1dPalDSPContext;
use crate::src::picture::rav1d_picture_alloc_copy;
use crate::src::picture::rav1d_picture_ref;
use crate::src::picture::rav1d_picture_unref_internal;
use crate::src::picture::rav1d_thread_picture_alloc;
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::recon::debug_block_info;
Expand Down Expand Up @@ -4580,12 +4576,12 @@ pub(crate) unsafe fn rav1d_decode_frame_exit(
// TODO(kkysen) use array::zip when stable
for i in 0..7 {
if f.refp[i].p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut f.refp[i]);
let _ = mem::take(&mut f.refp[i]);
}
let _ = mem::take(&mut f.ref_mvs[i]);
}
rav1d_picture_unref_internal(&mut f.cur);
rav1d_thread_picture_unref(&mut f.sr_cur);
let _ = mem::take(&mut f.cur);
let _ = mem::take(&mut f.sr_cur);
let _ = mem::take(&mut f.in_cdf);
if let Some(frame_hdr) = &f.frame_hdr {
if frame_hdr.refresh_context != 0 {
Expand Down Expand Up @@ -4699,14 +4695,14 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
if error.is_err() {
c.cached_error = mem::replace(&mut error, Ok(()));
*c.cached_error_props.get_mut().unwrap() = out_delayed.p.m.clone();
rav1d_thread_picture_unref(out_delayed);
let _ = mem::take(out_delayed);
} else if out_delayed.p.data.is_some() {
let progress = out_delayed.progress.as_ref().unwrap()[1].load(Ordering::Relaxed);
if (out_delayed.visible || c.output_invisible_frames) && progress != FRAME_ERROR {
rav1d_thread_picture_ref(&mut c.out, out_delayed);
c.out = out_delayed.clone();
c.event_flags |= out_delayed.flags.into();
}
rav1d_thread_picture_unref(out_delayed);
let _ = mem::take(out_delayed);
}
}
(f, out_delayed as *mut _, Some(task_thread_lock))
Expand All @@ -4726,13 +4722,13 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
}
for i in 0..7 {
if f.refp[i].p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut f.refp[i]);
let _ = mem::take(&mut f.refp[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);
let _ = mem::take(&mut *out);
let _ = mem::take(&mut f.cur);
let _ = mem::take(&mut f.sr_cur);
let _ = mem::take(&mut f.mvs);
let _ = mem::take(&mut f.seq_hdr);
let _ = mem::take(&mut f.frame_hdr);
Expand Down Expand Up @@ -4777,12 +4773,12 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
|| bpc != c.refs[refidx].p.p.p.bpc
{
for j in 0..i {
rav1d_thread_picture_unref(&mut f.refp[j]);
let _ = mem::take(&mut f.refp[j]);
}
on_error(f, c, out);
return Err(EINVAL);
}
rav1d_thread_picture_ref(&mut f.refp[i], &mut c.refs[refidx].p);
f.refp[i] = c.refs[refidx].p.clone();
ref_coded_width[i] = c.refs[refidx].p.p.frame_hdr.as_ref().unwrap().size.width[0];
if frame_hdr.size.width[0] != c.refs[refidx].p.p.p.w
|| frame_hdr.size.height != c.refs[refidx].p.p.p.h
Expand Down Expand Up @@ -4845,7 +4841,7 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
return res;
}
} else {
rav1d_picture_ref(&mut f.cur, &mut f.sr_cur.p);
f.cur = f.sr_cur.p.clone();
}
if frame_hdr.size.width[0] != frame_hdr.size.width[1] {
f.resize_step[0] = scale_fac(f.cur.p.w, f.sr_cur.p.p.w);
Expand All @@ -4860,11 +4856,11 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
// move f->cur into output queue
if c.n_fc == 1 {
if frame_hdr.show_frame != 0 || c.output_invisible_frames {
rav1d_thread_picture_ref(&mut c.out, &mut f.sr_cur);
c.out = f.sr_cur.clone();
c.event_flags |= f.sr_cur.flags.into();
}
} else {
rav1d_thread_picture_ref(out, &mut f.sr_cur);
*out = f.sr_cur.clone();
}

f.w4 = frame_hdr.size.width[0] + 3 >> 2;
Expand Down Expand Up @@ -4972,9 +4968,9 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
for i in 0..8 {
if refresh_frame_flags & (1 << i) != 0 {
if c.refs[i].p.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut c.refs[i].p);
let _ = mem::take(&mut c.refs[i].p);
}
rav1d_thread_picture_ref(&mut c.refs[i].p, &mut f.sr_cur);
c.refs[i].p = f.sr_cur.clone();

if frame_hdr.refresh_context != 0 {
c.cdf[i] = f.out_cdf.clone();
Expand All @@ -4994,11 +4990,11 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
if c.n_fc == 1 {
let res = rav1d_decode_frame(c, f);
if res.is_err() {
rav1d_thread_picture_unref(&mut c.out);
let _ = mem::take(&mut c.out);
for i in 0..8 {
if refresh_frame_flags & (1 << i) != 0 {
if c.refs[i].p.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut c.refs[i].p);
let _ = mem::take(&mut c.refs[i].p);
}
let _ = mem::take(&mut c.cdf[i]);
let _ = mem::take(&mut c.refs[i].segmap);
Expand Down
46 changes: 18 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ use crate::src::pal::rav1d_pal_dsp_init;
use crate::src::picture::dav1d_default_picture_alloc;
use crate::src::picture::dav1d_default_picture_release;
use crate::src::picture::rav1d_picture_alloc_copy;
use crate::src::picture::rav1d_picture_move_ref;
use crate::src::picture::rav1d_picture_ref;
use crate::src::picture::rav1d_picture_unref_internal;
use crate::src::picture::rav1d_thread_picture_move_ref;
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::refmvs::rav1d_refmvs_clear;
Expand Down Expand Up @@ -397,14 +391,14 @@ unsafe fn output_image(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRes
&mut c.cache
};
if !c.apply_grain || !(*r#in).p.has_grain() {
rav1d_picture_move_ref(out, &mut (*r#in).p);
*out = mem::take(&mut (*r#in).p);
} else {
res = rav1d_apply_grain(c, out, &(*r#in).p);
}
rav1d_thread_picture_unref(&mut *r#in);
let _ = mem::take(&mut *r#in);

if !c.all_layers && c.max_spatial_id && c.out.p.data.is_some() {
rav1d_thread_picture_move_ref(r#in, &mut c.out);
*r#in = mem::take(&mut c.out);
}
res
}
Expand All @@ -420,15 +414,15 @@ unsafe fn output_picture_ready(c: &mut Rav1dContext, drain: bool) -> bool {
{
return true;
}
rav1d_thread_picture_unref(&mut c.cache);
rav1d_thread_picture_move_ref(&mut c.cache, &mut c.out);
let _ = mem::take(&mut c.cache);
c.cache = mem::take(&mut c.out);
return false;
} else {
if c.cache.p.data.is_some() && drain {
return true;
} else {
if c.out.p.data.is_some() {
rav1d_thread_picture_move_ref(&mut c.cache, &mut c.out);
c.cache = mem::take(&mut c.out);
return false;
}
}
Expand Down Expand Up @@ -478,16 +472,16 @@ unsafe fn drain_picture(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRe
let error = mem::replace(&mut *f.task_thread.retval.try_lock().unwrap(), Ok(()));
if error.is_err() {
*c.cached_error_props.get_mut().unwrap() = out_delayed.p.m.clone();
rav1d_thread_picture_unref(out_delayed);
let _ = mem::take(out_delayed);
return error;
}
if out_delayed.p.data.is_some() {
let progress = out_delayed.progress.as_ref().unwrap()[1].load(Ordering::Relaxed);
if (out_delayed.visible || c.output_invisible_frames) && progress != FRAME_ERROR {
rav1d_thread_picture_ref(&mut c.out, out_delayed);
c.out = out_delayed.clone();
c.event_flags |= out_delayed.flags.into();
}
rav1d_thread_picture_unref(out_delayed);
let _ = mem::take(out_delayed);
if output_picture_ready(c, false) {
return output_image(c, out);
}
Expand Down Expand Up @@ -606,12 +600,12 @@ pub(crate) unsafe fn rav1d_apply_grain(
in_0: &Rav1dPicture,
) -> Rav1dResult {
if !in_0.has_grain() {
rav1d_picture_ref(out, in_0);
*out = in_0.clone();
return Ok(());
}
let res = rav1d_picture_alloc_copy(c, out, in_0.p.w, in_0);
if res.is_err() {
rav1d_picture_unref_internal(out);
let _ = mem::take(out);
return res;
} else {
if c.tc.len() > 1 {
Expand Down Expand Up @@ -670,17 +664,17 @@ pub unsafe extern "C" fn dav1d_apply_grain(
pub(crate) unsafe fn rav1d_flush(c: *mut Rav1dContext) {
let _ = mem::take(&mut (*c).in_0);
if (*c).out.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut (*c).out);
let _ = mem::take(&mut (*c).out);
}
if (*c).cache.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut (*c).cache);
let _ = mem::take(&mut (*c).cache);
}
(*c).drain = 0 as c_int;
(*c).cached_error = Ok(());
let mut i = 0;
while i < 8 {
if (*c).refs[i as usize].p.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut (*((*c).refs).as_mut_ptr().offset(i as isize)).p);
let _ = mem::take(&mut (*((*c).refs).as_mut_ptr().offset(i as isize)).p);
}
let _ = mem::take(&mut (*c).refs[i as usize].segmap);
let _ = mem::take(&mut (*c).refs[i as usize].refmvs);
Expand Down Expand Up @@ -739,7 +733,7 @@ pub(crate) unsafe fn rav1d_flush(c: *mut Rav1dContext) {
*f.task_thread.retval.try_lock().unwrap() = Ok(());
let out_delayed = &mut (*c).frame_thread.out_delayed[next as usize];
if out_delayed.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(out_delayed);
let _ = mem::take(out_delayed);
}
n = n.wrapping_add(1);
next = next.wrapping_add(1);
Expand Down Expand Up @@ -838,9 +832,7 @@ impl Drop for Rav1dContext {
.frame_hdr
.is_some()
{
rav1d_thread_picture_unref(
&mut self.frame_thread.out_delayed[n_2 as usize],
);
let _ = mem::take(&mut self.frame_thread.out_delayed[n_2 as usize]);
}
n_2 = n_2.wrapping_add(1);
}
Expand All @@ -850,9 +842,7 @@ impl Drop for Rav1dContext {
let mut n_4 = 0;
while n_4 < 8 {
if self.refs[n_4 as usize].p.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(
&mut (*(self.refs).as_mut_ptr().offset(n_4 as isize)).p,
);
let _ = mem::take(&mut (*(self.refs).as_mut_ptr().offset(n_4 as isize)).p);
}
let _ = mem::take(&mut self.refs[n_4 as usize].refmvs);
let _ = mem::take(&mut self.refs[n_4 as usize].segmap);
Expand Down Expand Up @@ -897,7 +887,7 @@ pub unsafe extern "C" fn dav1d_get_decode_error_data_props(
}

pub(crate) unsafe fn rav1d_picture_unref(p: &mut Rav1dPicture) {
rav1d_picture_unref_internal(p);
let _ = mem::take(p);
}

#[no_mangle]
Expand Down
26 changes: 9 additions & 17 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ use crate::src::internal::Rav1dTileGroupHeader;
use crate::src::levels::ObuMetaType;
use crate::src::log::Rav1dLog as _;
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::thread_task::FRAME_ERROR;
use std::array;
Expand Down Expand Up @@ -2137,7 +2135,7 @@ unsafe fn parse_obus(
// update refs with only the headers in case we skip the frame
for i in 0..8 {
if c.frame_hdr.as_ref().unwrap().refresh_frame_flags & (1 << i) != 0 {
rav1d_thread_picture_unref(&mut c.refs[i as usize].p);
let _ = mem::take(&mut c.refs[i as usize].p);
c.refs[i as usize].p.p.frame_hdr = c.frame_hdr.clone();
c.refs[i as usize].p.p.seq_hdr = c.seq_hdr.clone();
}
Expand Down Expand Up @@ -2292,7 +2290,7 @@ unsafe fn parse_obus(
let _ = mem::take(&mut c.mastering_display);
for i in 0..8 {
if c.refs[i as usize].p.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut c.refs[i as usize].p);
let _ = mem::take(&mut c.refs[i as usize].p);
}
let _ = mem::take(&mut c.refs[i as usize].segmap);
let _ = mem::take(&mut c.refs[i as usize].refmvs);
Expand Down Expand Up @@ -2525,10 +2523,7 @@ unsafe fn parse_obus(
return Err(EINVAL);
}
if c.n_fc == 1 {
rav1d_thread_picture_ref(
&mut c.out,
&mut c.refs[frame_hdr.existing_frame_idx as usize].p,
);
c.out = c.refs[frame_hdr.existing_frame_idx as usize].p.clone();
rav1d_picture_copy_props(
&mut (*c).out.p,
c.content_light.clone(),
Expand Down Expand Up @@ -2575,21 +2570,18 @@ unsafe fn parse_obus(
if error.is_err() {
c.cached_error = mem::replace(&mut *error, Ok(()));
*c.cached_error_props.get_mut().unwrap() = out_delayed.p.m.clone();
rav1d_thread_picture_unref(out_delayed);
let _ = mem::take(out_delayed);
} else if out_delayed.p.data.is_some() {
let progress =
out_delayed.progress.as_ref().unwrap()[1].load(Ordering::Relaxed);
if (out_delayed.visible || c.output_invisible_frames) && progress != FRAME_ERROR
{
rav1d_thread_picture_ref(&mut c.out, out_delayed);
c.out = out_delayed.clone();
c.event_flags |= out_delayed.flags.into();
}
rav1d_thread_picture_unref(out_delayed);
let _ = mem::take(out_delayed);
}
rav1d_thread_picture_ref(
out_delayed,
&mut c.refs[frame_hdr.existing_frame_idx as usize].p,
);
*out_delayed = c.refs[frame_hdr.existing_frame_idx as usize].p.clone();
out_delayed.visible = true;
rav1d_picture_copy_props(
&mut out_delayed.p,
Expand Down Expand Up @@ -2617,9 +2609,9 @@ unsafe fn parse_obus(
}

if c.refs[i as usize].p.p.frame_hdr.is_some() {
rav1d_thread_picture_unref(&mut c.refs[i as usize].p);
let _ = mem::take(&mut c.refs[i as usize].p);
}
rav1d_thread_picture_ref(&mut c.refs[i as usize].p, &mut c.refs[r as usize].p);
c.refs[i as usize].p = c.refs[r as usize].p.clone();

c.cdf[i as usize] = c.cdf[r as usize].clone();

Expand Down
37 changes: 1 addition & 36 deletions src/picture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl From<PictureFlags> for Rav1dEventFlags {
}
}

#[derive(Default)]
#[derive(Clone, Default)]
#[repr(C)]
pub(crate) struct Rav1dThreadPicture {
pub p: Rav1dPicture,
Expand Down Expand Up @@ -310,38 +310,3 @@ pub(crate) unsafe fn rav1d_picture_alloc_copy(
);
Ok(())
}

pub(crate) unsafe fn rav1d_picture_ref(dst: &mut Rav1dPicture, src: &Rav1dPicture) {
*dst = src.clone();
}

pub(crate) unsafe fn rav1d_picture_move_ref(dst: &mut Rav1dPicture, src: &mut Rav1dPicture) {
*dst = mem::take(src);
}

pub(crate) unsafe fn rav1d_thread_picture_ref(
dst: *mut Rav1dThreadPicture,
src: *const Rav1dThreadPicture,
) {
rav1d_picture_ref(&mut (*dst).p, &(*src).p);
(*dst).visible = (*src).visible;
(*dst).showable = (*src).showable;
(*dst).progress = (*src).progress.clone();
(*dst).flags = (*src).flags;
}

pub(crate) unsafe fn rav1d_thread_picture_move_ref(
dst: *mut Rav1dThreadPicture,
src: *mut Rav1dThreadPicture,
) {
*dst = mem::take(&mut *src);
}

pub(crate) unsafe fn rav1d_picture_unref_internal(p: &mut Rav1dPicture) {
let _ = mem::take(p);
}

pub(crate) unsafe fn rav1d_thread_picture_unref(p: *mut Rav1dThreadPicture) {
rav1d_picture_unref_internal(&mut (*p).p);
let _ = mem::take(&mut (*p).progress);
}

0 comments on commit 5af3bd0

Please sign in to comment.