Skip to content

Commit

Permalink
fn parse_frame_size: Initialize and return Rav1dFrameSize directly (
Browse files Browse the repository at this point in the history
#621)

`struct Rav1dFrameSize` was extracted from the monolithic `struct
Rav1dFrameHeader` to do this. The `fn` was also renamed from `fn
read_frame_size` to match the names and signatures of the other `fn
parse_*`s in `mod obu`.

I'd like to double-check perf on this, as due to extracting out `struct
Rav1dFrameSize`, the fields were re-arranged, and I just want to make
sure that didn't hurt cache perf.
  • Loading branch information
kkysen authored Dec 20, 2023
2 parents 12b682b + 97bab3a commit bea87b2
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 108 deletions.
47 changes: 29 additions & 18 deletions include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2122,13 +2122,23 @@ pub struct Dav1dFrameHeader {
pub gmv: [Dav1dWarpedMotionParams; DAV1D_REFS_PER_FRAME],
}

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameSize {
pub width: [c_int; 2],
pub height: c_int,
pub render_width: c_int,
pub render_height: c_int,
pub super_res: Rav1dFrameHeader_super_res,
pub have_render_size: c_int,
}

#[derive(Clone)]
#[repr(C)]
pub(crate) struct Rav1dFrameHeader {
pub size: Rav1dFrameSize,
pub film_grain: Rav1dFrameHeader_film_grain,
pub frame_type: Rav1dFrameType,
pub width: [c_int; 2],
pub height: c_int,
pub frame_offset: c_int,
pub temporal_id: c_int,
pub spatial_id: c_int,
Expand All @@ -2147,10 +2157,6 @@ pub(crate) struct Rav1dFrameHeader {
pub buffer_removal_time_present: c_int,
pub operating_points: [Rav1dFrameHeaderOperatingPoint; RAV1D_MAX_OPERATING_POINTS],
pub refresh_frame_flags: c_int,
pub render_width: c_int,
pub render_height: c_int,
pub super_res: Rav1dFrameHeader_super_res,
pub have_render_size: c_int,
pub allow_intrabc: c_int,
pub frame_ref_short_signaling: c_int,
pub refidx: [c_int; RAV1D_REFS_PER_FRAME],
Expand Down Expand Up @@ -2232,10 +2238,16 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
gmv,
} = value;
Self {
size: Rav1dFrameSize {
width,
height,
render_width,
render_height,
super_res: super_res.into(),
have_render_size,
},
film_grain: film_grain.into(),
frame_type,
width,
height,
frame_offset,
temporal_id,
spatial_id,
Expand All @@ -2254,10 +2266,6 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
buffer_removal_time_present,
operating_points: operating_points.map(|c| c.into()),
refresh_frame_flags,
render_width,
render_height,
super_res: super_res.into(),
have_render_size,
allow_intrabc,
frame_ref_short_signaling,
refidx,
Expand Down Expand Up @@ -2289,10 +2297,17 @@ impl From<Dav1dFrameHeader> for Rav1dFrameHeader {
impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
fn from(value: Rav1dFrameHeader) -> Self {
let Rav1dFrameHeader {
size:
Rav1dFrameSize {
width,
height,
render_width,
render_height,
super_res,
have_render_size,
},
film_grain,
frame_type,
width,
height,
frame_offset,
temporal_id,
spatial_id,
Expand All @@ -2311,10 +2326,6 @@ impl From<Rav1dFrameHeader> for Dav1dFrameHeader {
buffer_removal_time_present,
operating_points,
refresh_frame_flags,
render_width,
render_height,
super_res,
have_render_size,
allow_intrabc,
frame_ref_short_signaling,
refidx,
Expand Down
2 changes: 1 addition & 1 deletion src/cdef_apply_tmpl_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub(crate) unsafe fn rav1d_cdef_brow_16bpc(
.as_ptr();
let have_tt = ((*(*f).c).n_tc > 1 as c_uint) as c_int;
let sb128 = (*(*f).seq_hdr).sb128;
let resize = ((*(*f).frame_hdr).width[0] != (*(*f).frame_hdr).width[1]) as c_int;
let resize = ((*(*f).frame_hdr).size.width[0] != (*(*f).frame_hdr).size.width[1]) as c_int;
let y_stride: ptrdiff_t = PXSTRIDE((*f).cur.stride[0]);
let uv_stride: ptrdiff_t = PXSTRIDE((*f).cur.stride[1]);
let mut bit = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/cdef_apply_tmpl_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub(crate) unsafe fn rav1d_cdef_brow_8bpc(
.as_ptr();
let have_tt = ((*(*f).c).n_tc > 1 as c_uint) as c_int;
let sb128 = (*(*f).seq_hdr).sb128;
let resize = ((*(*f).frame_hdr).width[0] != (*(*f).frame_hdr).width[1]) as c_int;
let resize = ((*(*f).frame_hdr).size.width[0] != (*(*f).frame_hdr).size.width[1]) as c_int;
let y_stride: ptrdiff_t = (*f).cur.stride[0];
let uv_stride: ptrdiff_t = (*f).cur.stride[1];
let mut bit = 0;
Expand Down
43 changes: 22 additions & 21 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3878,7 +3878,7 @@ unsafe fn setup_tile(
ts.tiling.col_end = cmp::min(col_sb_end << sb_shift, f.bw);
ts.tiling.row_start = row_sb_start << sb_shift;
ts.tiling.row_end = cmp::min(row_sb_end << sb_shift, f.bh);
let diff_width = (*f.frame_hdr).width[0] != (*f.frame_hdr).width[1];
let diff_width = (*f.frame_hdr).size.width[0] != (*f.frame_hdr).size.width[1];

// Reference Restoration Unit (used for exp coding)
let (sb_idx, unit_idx) = if diff_width {
Expand All @@ -3900,7 +3900,7 @@ unsafe fn setup_tile(

let lr_ref = if diff_width {
let ss_hor = (p != 0 && f.cur.p.layout != Rav1dPixelLayout::I444) as c_int;
let d = (*f.frame_hdr).super_res.width_scale_denominator;
let d = (*f.frame_hdr).size.super_res.width_scale_denominator;
let unit_size_log2 = (*f.frame_hdr).restoration.unit_size[(p != 0) as usize];
let rnd = (8 << unit_size_log2) - 1;
let shift = unit_size_log2 + 3;
Expand Down Expand Up @@ -4155,11 +4155,11 @@ pub(crate) unsafe fn rav1d_decode_tile_sbrow(t: &mut Rav1dTaskContext) -> Result

let frame_type = (*f.frame_hdr).restoration.r#type[p as usize];

if (*f.frame_hdr).width[0] != (*f.frame_hdr).width[1] {
if (*f.frame_hdr).size.width[0] != (*f.frame_hdr).size.width[1] {
let w = f.sr_cur.p.p.w + ss_hor >> ss_hor;
let n_units = cmp::max(1, w + half_unit >> unit_size_log2);

let d = (*f.frame_hdr).super_res.width_scale_denominator;
let d = (*f.frame_hdr).size.super_res.width_scale_denominator;
let rnd = unit_size * 8 - 1;
let shift = unit_size_log2 + 3;
let x0 = (4 * t.bx * d >> ss_hor) + rnd >> shift;
Expand Down Expand Up @@ -4400,7 +4400,7 @@ pub(crate) unsafe fn rav1d_decode_frame_init(f: &mut Rav1dFrameContext) -> Rav1d
// update allocation of block contexts for above
let mut y_stride = f.cur.stride[0];
let mut uv_stride = f.cur.stride[1];
let has_resize = ((*f.frame_hdr).width[0] != (*f.frame_hdr).width[1]) as c_int;
let has_resize = ((*f.frame_hdr).size.width[0] != (*f.frame_hdr).size.width[1]) as c_int;
let need_cdef_lpf_copy = (c.n_tc > 1 && has_resize != 0) as c_int;
if y_stride * f.sbh as isize * 4 != f.lf.cdef_buf_plane_sz[0] as isize
|| uv_stride * f.sbh as isize * 8 != f.lf.cdef_buf_plane_sz[1] as isize
Expand Down Expand Up @@ -5108,10 +5108,10 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
for i in 0..7 {
let refidx = (*f.frame_hdr).refidx[i] as usize;
if c.refs[refidx].p.p.data[0].is_null()
|| ((*f.frame_hdr).width[0] * 2) < c.refs[refidx].p.p.p.w
|| ((*f.frame_hdr).height * 2) < c.refs[refidx].p.p.p.h
|| (*f.frame_hdr).width[0] > c.refs[refidx].p.p.p.w * 16
|| (*f.frame_hdr).height > c.refs[refidx].p.p.p.h * 16
|| ((*f.frame_hdr).size.width[0] * 2) < c.refs[refidx].p.p.p.w
|| ((*f.frame_hdr).size.height * 2) < c.refs[refidx].p.p.p.h
|| (*f.frame_hdr).size.width[0] > c.refs[refidx].p.p.p.w * 16
|| (*f.frame_hdr).size.height > c.refs[refidx].p.p.p.h * 16
|| (*f.seq_hdr).layout != c.refs[refidx].p.p.p.layout
|| bpc != c.refs[refidx].p.p.p.bpc
{
Expand All @@ -5122,12 +5122,12 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
return Err(EINVAL);
}
rav1d_thread_picture_ref(&mut f.refp[i], &mut c.refs[refidx].p);
ref_coded_width[i] = (*c.refs[refidx].p.p.frame_hdr).width[0];
if (*f.frame_hdr).width[0] != c.refs[refidx].p.p.p.w
|| (*f.frame_hdr).height != c.refs[refidx].p.p.p.h
ref_coded_width[i] = (*c.refs[refidx].p.p.frame_hdr).size.width[0];
if (*f.frame_hdr).size.width[0] != c.refs[refidx].p.p.p.w
|| (*f.frame_hdr).size.height != c.refs[refidx].p.p.p.h
{
f.svc[i][0].scale = scale_fac(c.refs[refidx].p.p.p.w, (*f.frame_hdr).width[0]);
f.svc[i][1].scale = scale_fac(c.refs[refidx].p.p.p.h, (*f.frame_hdr).height);
f.svc[i][0].scale = scale_fac(c.refs[refidx].p.p.p.w, (*f.frame_hdr).size.width[0]);
f.svc[i][1].scale = scale_fac(c.refs[refidx].p.p.p.h, (*f.frame_hdr).size.height);
f.svc[i][0].step = f.svc[i][0].scale + 8 >> 4;
f.svc[i][1].step = f.svc[i][1].scale + 8 >> 4;
} else {
Expand Down Expand Up @@ -5167,16 +5167,17 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
return res;
}

if (*f.frame_hdr).width[0] != (*f.frame_hdr).width[1] {
let res = rav1d_picture_alloc_copy(c, &mut f.cur, (*f.frame_hdr).width[0], &mut f.sr_cur.p);
if (*f.frame_hdr).size.width[0] != (*f.frame_hdr).size.width[1] {
let res =
rav1d_picture_alloc_copy(c, &mut f.cur, (*f.frame_hdr).size.width[0], &mut f.sr_cur.p);
if res.is_err() {
on_error(f, c, out_delayed);
return res;
}
} else {
rav1d_picture_ref(&mut f.cur, &mut f.sr_cur.p);
}
if (*f.frame_hdr).width[0] != (*f.frame_hdr).width[1] {
if (*f.frame_hdr).size.width[0] != (*f.frame_hdr).size.width[1] {
f.resize_step[0] = scale_fac(f.cur.p.w, f.sr_cur.p.p.w);
let ss_hor = (f.cur.p.layout != Rav1dPixelLayout::I444) as c_int;
let in_cw = f.cur.p.w + ss_hor >> ss_hor;
Expand All @@ -5196,10 +5197,10 @@ pub unsafe fn rav1d_submit_frame(c: &mut Rav1dContext) -> Rav1dResult {
rav1d_thread_picture_ref(out_delayed, &mut f.sr_cur);
}

f.w4 = (*f.frame_hdr).width[0] + 3 >> 2;
f.h4 = (*f.frame_hdr).height + 3 >> 2;
f.bw = ((*f.frame_hdr).width[0] + 7 >> 3) << 1;
f.bh = ((*f.frame_hdr).height + 7 >> 3) << 1;
f.w4 = (*f.frame_hdr).size.width[0] + 3 >> 2;
f.h4 = (*f.frame_hdr).size.height + 3 >> 2;
f.bw = ((*f.frame_hdr).size.width[0] + 7 >> 3) << 1;
f.bh = ((*f.frame_hdr).size.height + 7 >> 3) << 1;
f.sb128w = f.bw + 31 >> 5;
f.sb128h = f.bh + 31 >> 5;
f.sb_shift = 4 + (*f.seq_hdr).sb128;
Expand Down
8 changes: 4 additions & 4 deletions src/lf_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ unsafe fn backup_lpf<BD: BitDepth>(
lr_backup: c_int,
) {
let cdef_backup = (lr_backup == 0) as c_int;
let dst_w = if (*(*f).frame_hdr).super_res.enabled != 0 {
(*(*f).frame_hdr).width[1] + ss_hor >> ss_hor
let dst_w = if (*(*f).frame_hdr).size.super_res.enabled != 0 {
(*(*f).frame_hdr).size.width[1] + ss_hor >> ss_hor
} else {
src_w
};
Expand Down Expand Up @@ -104,7 +104,7 @@ unsafe fn backup_lpf<BD: BitDepth>(
}
dst = dst.offset(4 * BD::pxstride(dst_stride as usize) as isize);
}
if lr_backup != 0 && (*(*f).frame_hdr).width[0] != (*(*f).frame_hdr).width[1] {
if lr_backup != 0 && (*(*f).frame_hdr).size.width[0] != (*(*f).frame_hdr).size.width[1] {
while row + stripe_h <= row_h {
let n_lines = 4 - (row + stripe_h + 1 == h) as c_int;
((*(*f).dsp).mc.resize)(
Expand Down Expand Up @@ -171,7 +171,7 @@ pub(crate) unsafe fn rav1d_copy_lpf<BD: BitDepth>(
sby: c_int,
) {
let have_tt = ((*(*f).c).n_tc > 1 as c_uint) as c_int;
let resize = ((*(*f).frame_hdr).width[0] != (*(*f).frame_hdr).width[1]) as c_int;
let resize = ((*(*f).frame_hdr).size.width[0] != (*(*f).frame_hdr).size.width[1]) as c_int;
let offset = 8 * (sby != 0) as c_int;
let src_stride: *const ptrdiff_t = ((*f).cur.stride).as_mut_ptr();
let lr_stride: *const ptrdiff_t = ((*f).sr_cur.p.stride).as_mut_ptr();
Expand Down
Loading

0 comments on commit bea87b2

Please sign in to comment.