Skip to content

Commit

Permalink
Check data.is_none() in addition to data.data[0].is_null().
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen committed Apr 23, 2024
1 parent e38f69c commit 4094895
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 23 deletions.
41 changes: 36 additions & 5 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4539,7 +4539,14 @@ pub(crate) unsafe fn rav1d_decode_frame_exit(
f: &mut Rav1dFrameData,
retval: Rav1dResult,
) {
if !f.sr_cur.p.data.as_ref().unwrap().data[0].is_null() {
if !f
.sr_cur
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
f.task_thread.error = AtomicI32::new(0);
}
let cf = f.frame_thread.cf.get_mut();
Expand Down Expand Up @@ -4644,7 +4651,12 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
task_thread_lock = f.task_thread.cond.wait(task_thread_lock).unwrap();
}
let out_delayed = &mut c.frame_thread.out_delayed[next as usize];
if !out_delayed.p.data.as_ref().unwrap().data[0].is_null()
if !out_delayed
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
|| f.task_thread.error.load(Ordering::SeqCst) != 0
{
let first = c.task_thread.first.load(Ordering::SeqCst);
Expand All @@ -4671,7 +4683,13 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
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);
} else if !out_delayed.p.data.as_ref().unwrap().data[0].is_null() {
} else if !out_delayed
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
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);
Expand Down Expand Up @@ -4732,14 +4750,27 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
if frame_hdr.frame_type.is_inter_or_switch() {
if frame_hdr.primary_ref_frame != RAV1D_PRIMARY_REF_NONE {
let pri_ref = frame_hdr.refidx[frame_hdr.primary_ref_frame as usize] as usize;
if c.refs[pri_ref].p.p.data.as_ref().unwrap().data[0].is_null() {
if c.refs[pri_ref]
.p
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
on_error(f, c, out);
return Err(EINVAL);
}
}
for i in 0..7 {
let refidx = frame_hdr.refidx[i] as usize;
if c.refs[refidx].p.p.data.as_ref().unwrap().data[0].is_null()
if c.refs[refidx]
.p
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
|| (frame_hdr.size.width[0] * 2) < c.refs[refidx].p.p.p.w
|| (frame_hdr.size.height * 2) < c.refs[refidx].p.p.p.h
|| frame_hdr.size.width[0] > c.refs[refidx].p.p.p.w * 16
Expand Down
68 changes: 60 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,16 @@ unsafe fn output_image(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRes
}
rav1d_thread_picture_unref(&mut *r#in);

if !c.all_layers && c.max_spatial_id && !(c.out.p.data.as_ref().unwrap().data[0]).is_null() {
if !c.all_layers
&& c.max_spatial_id
&& !c
.out
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
rav1d_thread_picture_move_ref(r#in, &mut c.out);
}
res
Expand All @@ -411,8 +420,20 @@ unsafe fn output_picture_ready(c: &mut Rav1dContext, drain: bool) -> bool {
return true;
}
if !c.all_layers && c.max_spatial_id {
if !c.out.p.data.as_ref().unwrap().data[0].is_null()
&& !c.cache.p.data.as_ref().unwrap().data[0].is_null()
if !c
.out
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
&& !c
.cache
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
if c.max_spatial_id == (c.cache.p.frame_hdr.as_ref().unwrap().spatial_id != 0)
|| c.out.flags.contains(PictureFlags::NEW_TEMPORAL_UNIT)
Expand All @@ -423,17 +444,37 @@ unsafe fn output_picture_ready(c: &mut Rav1dContext, drain: bool) -> bool {
rav1d_thread_picture_move_ref(&mut c.cache, &mut c.out);
return false;
} else {
if !c.cache.p.data.as_ref().unwrap().data[0].is_null() && drain {
if !c
.cache
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
&& drain
{
return true;
} else {
if !c.out.p.data.as_ref().unwrap().data[0].is_null() {
if !c
.out
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
rav1d_thread_picture_move_ref(&mut c.cache, &mut c.out);
return false;
}
}
}
}
!c.out.p.data.as_ref().unwrap().data[0].is_null()
!c.out
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
}

unsafe fn drain_picture(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dResult {
Expand All @@ -447,7 +488,12 @@ unsafe fn drain_picture(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRe
task_thread_lock = f.task_thread.cond.wait(task_thread_lock).unwrap();
}
let out_delayed = &mut c.frame_thread.out_delayed[next as usize];
if !out_delayed.p.data.as_ref().unwrap().data[0].is_null()
if !out_delayed
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
|| f.task_thread.error.load(Ordering::SeqCst) != 0
{
let first: c_uint = c.task_thread.first.load(Ordering::SeqCst);
Expand Down Expand Up @@ -482,7 +528,13 @@ unsafe fn drain_picture(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRe
rav1d_thread_picture_unref(out_delayed);
return error;
}
if !(out_delayed.p.data.as_ref().unwrap().data[0]).is_null() {
if !out_delayed
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
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);
Expand Down
20 changes: 15 additions & 5 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2517,9 +2517,8 @@ unsafe fn parse_obus(
.p
.data
.as_ref()
.unwrap()
.data[0]
.is_null()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
return Err(EINVAL);
}
Expand Down Expand Up @@ -2555,7 +2554,12 @@ unsafe fn parse_obus(
task_thread_lock = f.task_thread.cond.wait(task_thread_lock).unwrap();
}
let out_delayed = &mut c.frame_thread.out_delayed[next as usize];
if !out_delayed.p.data.as_ref().unwrap().data[0].is_null()
if !out_delayed
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
|| f.task_thread.error.load(Ordering::SeqCst) != 0
{
let first = c.task_thread.first.load(Ordering::SeqCst);
Expand All @@ -2581,7 +2585,13 @@ unsafe fn parse_obus(
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);
} else if !(out_delayed.p.data.as_ref().unwrap().data[0]).is_null() {
} else if !out_delayed
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
let progress =
out_delayed.progress.as_ref().unwrap()[1].load(Ordering::Relaxed);
if (out_delayed.visible || c.output_invisible_frames) && progress != FRAME_ERROR
Expand Down
23 changes: 20 additions & 3 deletions src/picture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,12 @@ unsafe fn picture_alloc_with_edges(
bpc: c_int,
p_allocator: &Rav1dPicAllocator,
) -> Rav1dResult {
if !p.data.as_ref().unwrap().data[0].is_null() {
if !p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
writeln!(logger, "Picture already allocated!",);
return Err(EGeneric);
}
Expand Down Expand Up @@ -310,7 +315,13 @@ pub(crate) unsafe fn rav1d_picture_alloc_copy(
}

pub(crate) unsafe fn rav1d_picture_ref(dst: &mut Rav1dPicture, src: &Rav1dPicture) {
if validate_input!(dst.data.as_ref().unwrap().data[0].is_null()).is_err() {
if validate_input!(dst
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true))
.is_err()
{
return;
}
if let Some(data) = &src.data {
Expand All @@ -322,7 +333,13 @@ pub(crate) unsafe fn rav1d_picture_ref(dst: &mut Rav1dPicture, src: &Rav1dPictur
}

pub(crate) unsafe fn rav1d_picture_move_ref(dst: &mut Rav1dPicture, src: &mut Rav1dPicture) {
if validate_input!(dst.data.as_ref().unwrap().data[0].is_null()).is_err() {
if validate_input!(dst
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true))
.is_err()
{
return;
}
if let Some(data) = &src.data {
Expand Down
18 changes: 16 additions & 2 deletions src/thread_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,14 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
};
// Note that `progress.is_some() == c.n_fc > 1`.
let progress = &**f.sr_cur.progress.as_ref().unwrap();
if !(f.sr_cur.p.data.as_ref().unwrap().data[0]).is_null() {
if !f
.sr_cur
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
progress[0].store(if error_0 != 0 { FRAME_ERROR } else { y }, Ordering::SeqCst);
}
f.frame_thread_progress.entropy.store(
Expand Down Expand Up @@ -1310,7 +1317,14 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
// Note that `progress.is_some() == c.n_fc > 1`.
if let Some(progress) = &f.sr_cur.progress {
// upon flush, this can be free'ed already
if !(f.sr_cur.p.data.as_ref().unwrap().data[0]).is_null() {
if !f
.sr_cur
.p
.data
.as_ref()
.map(|data| data.data[0].is_null())
.unwrap_or(true)
{
progress[1].store(
if error_0 != 0 { FRAME_ERROR } else { y_0 },
Ordering::SeqCst,
Expand Down

0 comments on commit 4094895

Please sign in to comment.