From c13f1bbddb51554bb93609846de23162b3628275 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 18 Oct 2023 23:10:17 -0700 Subject: [PATCH 1/5] `fn has_grain`: Make `pic` arg a ref. --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7275ae5a7..0d80089ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -587,8 +587,8 @@ pub unsafe extern "C" fn dav1d_parse_sequence_header( .into() } -unsafe fn has_grain(pic: *const Rav1dPicture) -> c_int { - let fgdata = &(*(*pic).frame_hdr).film_grain.data; +unsafe fn has_grain(pic: &Rav1dPicture) -> c_int { + let fgdata = &(*pic.frame_hdr).film_grain.data; return ((*fgdata).num_y_points != 0 || (*fgdata).num_uv_points[0] != 0 || (*fgdata).num_uv_points[1] != 0 From 81042320aafc7707dcd8d383b657e78a2b7fde4a Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 18 Oct 2023 23:14:26 -0700 Subject: [PATCH 2/5] `fn has_grain`: Remove redundant derefs of `fgdata` ref. --- src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0d80089ff..f2dbc977c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -589,11 +589,10 @@ pub unsafe extern "C" fn dav1d_parse_sequence_header( unsafe fn has_grain(pic: &Rav1dPicture) -> c_int { let fgdata = &(*pic.frame_hdr).film_grain.data; - return ((*fgdata).num_y_points != 0 - || (*fgdata).num_uv_points[0] != 0 - || (*fgdata).num_uv_points[1] != 0 - || (*fgdata).clip_to_restricted_range && (*fgdata).chroma_scaling_from_luma) - as c_int; + return (fgdata.num_y_points != 0 + || fgdata.num_uv_points[0] != 0 + || fgdata.num_uv_points[1] != 0 + || fgdata.clip_to_restricted_range && fgdata.chroma_scaling_from_luma) as c_int; } unsafe fn output_image(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dResult { From 03b0b7b84f39cc685f04d8ceaa036e66d42afd8e Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 18 Oct 2023 23:16:50 -0700 Subject: [PATCH 3/5] `fn has_grain`: Make the return type a `bool`. --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f2dbc977c..584511e53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -587,12 +587,12 @@ pub unsafe extern "C" fn dav1d_parse_sequence_header( .into() } -unsafe fn has_grain(pic: &Rav1dPicture) -> c_int { +unsafe fn has_grain(pic: &Rav1dPicture) -> bool { let fgdata = &(*pic.frame_hdr).film_grain.data; - return (fgdata.num_y_points != 0 + fgdata.num_y_points != 0 || fgdata.num_uv_points[0] != 0 || fgdata.num_uv_points[1] != 0 - || fgdata.clip_to_restricted_range && fgdata.chroma_scaling_from_luma) as c_int; + || fgdata.clip_to_restricted_range && fgdata.chroma_scaling_from_luma } unsafe fn output_image(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dResult { @@ -602,7 +602,7 @@ unsafe fn output_image(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRes } else { &mut c.cache }; - if c.apply_grain == 0 || has_grain(&mut (*in_0).p) == 0 { + if c.apply_grain == 0 || !has_grain(&mut (*in_0).p) { rav1d_picture_move_ref(out, &mut (*in_0).p); rav1d_thread_picture_unref(in_0); } else { @@ -857,7 +857,7 @@ pub(crate) unsafe fn rav1d_apply_grain( out: &mut Rav1dPicture, in_0: &Rav1dPicture, ) -> Rav1dResult { - if has_grain(in_0) == 0 { + if !has_grain(in_0) { rav1d_picture_ref(out, in_0); return Ok(()); } From e062dfb8f8be2b51a03b6a754cae3e359a4a4829 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 18 Oct 2023 23:19:06 -0700 Subject: [PATCH 4/5] `fn Rav1dPicture::has_grain`: Make a method. --- src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 584511e53..fe3956ea1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -587,12 +587,14 @@ pub unsafe extern "C" fn dav1d_parse_sequence_header( .into() } -unsafe fn has_grain(pic: &Rav1dPicture) -> bool { - let fgdata = &(*pic.frame_hdr).film_grain.data; - fgdata.num_y_points != 0 - || fgdata.num_uv_points[0] != 0 - || fgdata.num_uv_points[1] != 0 - || fgdata.clip_to_restricted_range && fgdata.chroma_scaling_from_luma +impl Rav1dPicture { + unsafe fn has_grain(&self) -> bool { + let fgdata = &(*self.frame_hdr).film_grain.data; + fgdata.num_y_points != 0 + || fgdata.num_uv_points[0] != 0 + || fgdata.num_uv_points[1] != 0 + || fgdata.clip_to_restricted_range && fgdata.chroma_scaling_from_luma + } } unsafe fn output_image(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dResult { @@ -602,7 +604,7 @@ unsafe fn output_image(c: &mut Rav1dContext, out: &mut Rav1dPicture) -> Rav1dRes } else { &mut c.cache }; - if c.apply_grain == 0 || !has_grain(&mut (*in_0).p) { + if c.apply_grain == 0 || !(*in_0).p.has_grain() { rav1d_picture_move_ref(out, &mut (*in_0).p); rav1d_thread_picture_unref(in_0); } else { @@ -857,7 +859,7 @@ pub(crate) unsafe fn rav1d_apply_grain( out: &mut Rav1dPicture, in_0: &Rav1dPicture, ) -> Rav1dResult { - if !has_grain(in_0) { + if !in_0.has_grain() { rav1d_picture_ref(out, in_0); return Ok(()); } From 3b275e2af98bf7a680b98236f3144dad0ca64dea Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 18 Oct 2023 23:21:04 -0700 Subject: [PATCH 5/5] `fn Rav1dFilmGrainData::has_grain`: Extract safe `fn` from `fn Rav1dPicture::has_grain`. --- src/lib.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fe3956ea1..81b8747d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ use crate::include::dav1d::headers::Dav1dFrameHeader; use crate::include::dav1d::headers::Dav1dITUTT35; use crate::include::dav1d::headers::Dav1dSequenceHeader; use crate::include::dav1d::headers::Rav1dContentLightLevel; +use crate::include::dav1d::headers::Rav1dFilmGrainData; use crate::include::dav1d::headers::Rav1dFrameHeader; use crate::include::dav1d::headers::Rav1dITUTT35; use crate::include::dav1d::headers::Rav1dMasteringDisplay; @@ -587,13 +588,18 @@ pub unsafe extern "C" fn dav1d_parse_sequence_header( .into() } +impl Rav1dFilmGrainData { + fn has_grain(&self) -> bool { + self.num_y_points != 0 + || self.num_uv_points[0] != 0 + || self.num_uv_points[1] != 0 + || self.clip_to_restricted_range && self.chroma_scaling_from_luma + } +} + impl Rav1dPicture { unsafe fn has_grain(&self) -> bool { - let fgdata = &(*self.frame_hdr).film_grain.data; - fgdata.num_y_points != 0 - || fgdata.num_uv_points[0] != 0 - || fgdata.num_uv_points[1] != 0 - || fgdata.clip_to_restricted_range && fgdata.chroma_scaling_from_luma + (*self.frame_hdr).film_grain.data.has_grain() } }