Skip to content

Commit

Permalink
fn read_restoration_info: Cleanup and make mostly safe (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen authored Aug 28, 2023
2 parents 7c6bf46 + 11d8fd5 commit 83683dc
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 127 deletions.
2 changes: 1 addition & 1 deletion include/dav1d/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub type Dav1dAdaptiveBoolean = libc::c_uint;
pub const DAV1D_ADAPTIVE: Dav1dAdaptiveBoolean = 2;
pub const DAV1D_ON: Dav1dAdaptiveBoolean = 1;
pub const DAV1D_OFF: Dav1dAdaptiveBoolean = 0;
pub type Dav1dRestorationType = libc::c_uint;
pub type Dav1dRestorationType = u8;
pub const DAV1D_RESTORATION_SGRPROJ: Dav1dRestorationType = 3;
pub const DAV1D_RESTORATION_WIENER: Dav1dRestorationType = 2;
pub const DAV1D_RESTORATION_SWITCHABLE: Dav1dRestorationType = 1;
Expand Down
216 changes: 99 additions & 117 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4713,159 +4713,141 @@ unsafe fn setup_tile(
}
}

unsafe extern "C" fn read_restoration_info(
t: *mut Dav1dTaskContext,
lr: *mut Av1RestorationUnit,
p: libc::c_int,
unsafe fn read_restoration_info(
t: &mut Dav1dTaskContext,
lr: &mut Av1RestorationUnit,
p: usize,
frame_type: Dav1dRestorationType,
) {
let f: *const Dav1dFrameContext = (*t).f;
let ts: *mut Dav1dTileState = (*t).ts;
if frame_type as libc::c_uint == DAV1D_RESTORATION_SWITCHABLE as libc::c_int as libc::c_uint {
let filter = dav1d_msac_decode_symbol_adapt4(
&mut (*ts).msac,
&mut (*ts).cdf.m.restore_switchable.0,
2 as libc::c_int as size_t,
) as libc::c_int;
(*lr).type_0 = (if filter != 0 {
let f = &*t.f;
let ts = &mut *t.ts;
let lr_ref = &*ts.lr_ref[p];

if frame_type == DAV1D_RESTORATION_SWITCHABLE {
let filter =
dav1d_msac_decode_symbol_adapt4(&mut ts.msac, &mut ts.cdf.m.restore_switchable.0, 2);
lr.r#type = if filter != 0 {
if filter == 2 {
DAV1D_RESTORATION_SGRPROJ as libc::c_int
DAV1D_RESTORATION_SGRPROJ
} else {
DAV1D_RESTORATION_WIENER as libc::c_int
DAV1D_RESTORATION_WIENER
}
} else {
DAV1D_RESTORATION_NONE as libc::c_int
}) as uint8_t;
DAV1D_RESTORATION_NONE
};
} else {
let type_0: libc::c_uint = dav1d_msac_decode_bool_adapt(
&mut (*ts).msac,
if frame_type as libc::c_uint == DAV1D_RESTORATION_WIENER as libc::c_int as libc::c_uint
{
&mut (*ts).cdf.m.restore_wiener.0
let r#type = dav1d_msac_decode_bool_adapt(
&mut ts.msac,
if frame_type == DAV1D_RESTORATION_WIENER {
&mut ts.cdf.m.restore_wiener.0
} else {
&mut (*ts).cdf.m.restore_sgrproj.0
&mut ts.cdf.m.restore_sgrproj.0
},
) as libc::c_uint;
(*lr).type_0 = (if type_0 != 0 {
frame_type as libc::c_uint
);
lr.r#type = if r#type {
frame_type
} else {
DAV1D_RESTORATION_NONE as libc::c_int as libc::c_uint
}) as uint8_t;
DAV1D_RESTORATION_NONE
};
}
if (*lr).type_0 as libc::c_int == DAV1D_RESTORATION_WIENER as libc::c_int {
(*lr).filter_v[0] = (if p != 0 {
0 as libc::c_int

if lr.r#type == DAV1D_RESTORATION_WIENER {
lr.filter_v[0] = if p != 0 {
0
} else {
dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).filter_v[0] + 5) as libc::c_uint,
(dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.filter_v[0] + 5) as libc::c_uint,
16,
1,
) - 5
}) as int8_t;
(*lr).filter_v[1] = (dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).filter_v[1] + 23) as libc::c_uint,
) - 5) as i8
};
lr.filter_v[1] = (dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.filter_v[1] + 23) as libc::c_uint,
32,
2,
) - 23) as int8_t;
(*lr).filter_v[2] = (dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).filter_v[2] + 17) as libc::c_uint,
) - 23) as i8;
lr.filter_v[2] = (dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.filter_v[2] + 17) as libc::c_uint,
64,
3,
) - 17) as int8_t;
(*lr).filter_h[0] = (if p != 0 {
0 as libc::c_int
) - 17) as i8;

lr.filter_h[0] = if p != 0 {
0
} else {
dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).filter_h[0] + 5) as libc::c_uint,
(dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.filter_h[0] + 5) as libc::c_uint,
16,
1,
) - 5
}) as int8_t;
(*lr).filter_h[1] = (dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).filter_h[1] + 23) as libc::c_uint,
) - 5) as i8
};
lr.filter_h[1] = (dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.filter_h[1] + 23) as libc::c_uint,
32,
2,
) - 23) as int8_t;
(*lr).filter_h[2] = (dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).filter_h[2] + 17) as libc::c_uint,
) - 23) as i8;
lr.filter_h[2] = (dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.filter_h[2] + 17) as libc::c_uint,
64,
3,
) - 17) as int8_t;
memcpy(
((*lr).sgr_weights).as_mut_ptr() as *mut libc::c_void,
((*(*ts).lr_ref[p as usize]).sgr_weights).as_mut_ptr() as *const libc::c_void,
::core::mem::size_of::<[int8_t; 2]>() as libc::c_ulong,
);
(*ts).lr_ref[p as usize] = lr;
if DEBUG_BLOCK_INFO(&*f, &*t) {
printf(
b"Post-lr_wiener[pl=%d,v[%d,%d,%d],h[%d,%d,%d]]: r=%d\n\0" as *const u8
as *const libc::c_char,
) - 17) as i8;
lr.sgr_weights = lr_ref.sgr_weights;
ts.lr_ref[p] = lr;
if DEBUG_BLOCK_INFO(f, t) {
println!(
"Post-lr_wiener[pl={},v[{},{},{}],h[{},{},{}]]: r={}",
p,
(*lr).filter_v[0] as libc::c_int,
(*lr).filter_v[1] as libc::c_int,
(*lr).filter_v[2] as libc::c_int,
(*lr).filter_h[0] as libc::c_int,
(*lr).filter_h[1] as libc::c_int,
(*lr).filter_h[2] as libc::c_int,
(*ts).msac.rng,
lr.filter_v[0],
lr.filter_v[1],
lr.filter_v[2],
lr.filter_h[0],
lr.filter_h[1],
lr.filter_h[2],
ts.msac.rng,
);
}
} else if (*lr).type_0 as libc::c_int == DAV1D_RESTORATION_SGRPROJ as libc::c_int {
let idx: libc::c_uint =
dav1d_msac_decode_bools(&mut (*ts).msac, 4 as libc::c_int as libc::c_uint);
let sgr_params: *const uint16_t = (dav1d_sgr_params[idx as usize]).as_ptr();
(*lr).sgr_idx = idx as uint8_t;
(*lr).sgr_weights[0] = (if *sgr_params.offset(0) as libc::c_int != 0 {
dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).sgr_weights[0] + 96) as libc::c_uint,
} else if lr.r#type == DAV1D_RESTORATION_SGRPROJ {
let idx = dav1d_msac_decode_bools(&mut ts.msac, 4) as u8;
let sgr_params = &dav1d_sgr_params[idx.into()];
lr.sgr_idx = idx;
lr.sgr_weights[0] = if sgr_params[0] != 0 {
(dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.sgr_weights[0] + 96) as libc::c_uint,
128,
4,
) - 96
) - 96) as i8
} else {
0 as libc::c_int
}) as int8_t;
(*lr).sgr_weights[1] = (if *sgr_params.offset(1) as libc::c_int != 0 {
dav1d_msac_decode_subexp(
&mut (*ts).msac,
((*(*ts).lr_ref[p as usize]).sgr_weights[1] + 32) as libc::c_uint,
0
};
lr.sgr_weights[1] = if sgr_params[1] != 0 {
(dav1d_msac_decode_subexp(
&mut ts.msac,
(lr_ref.sgr_weights[1] + 32) as libc::c_uint,
128,
4,
) - 32
) - 32) as i8
} else {
95 as libc::c_int
}) as int8_t;
memcpy(
((*lr).filter_v).as_mut_ptr() as *mut libc::c_void,
((*(*ts).lr_ref[p as usize]).filter_v).as_mut_ptr() as *const libc::c_void,
::core::mem::size_of::<[int8_t; 3]>() as libc::c_ulong,
);
memcpy(
((*lr).filter_h).as_mut_ptr() as *mut libc::c_void,
((*(*ts).lr_ref[p as usize]).filter_h).as_mut_ptr() as *const libc::c_void,
::core::mem::size_of::<[int8_t; 3]>() as libc::c_ulong,
);
(*ts).lr_ref[p as usize] = lr;
if DEBUG_BLOCK_INFO(&*f, &*t) {
printf(
b"Post-lr_sgrproj[pl=%d,idx=%d,w[%d,%d]]: r=%d\n\0" as *const u8
as *const libc::c_char,
p,
(*lr).sgr_idx as libc::c_int,
(*lr).sgr_weights[0] as libc::c_int,
(*lr).sgr_weights[1] as libc::c_int,
(*ts).msac.rng,
95
};
lr.filter_v = lr_ref.filter_v;
lr.filter_h = lr_ref.filter_h;
ts.lr_ref[p] = lr;
if DEBUG_BLOCK_INFO(f, t) {
println!(
"Post-lr_sgrproj[pl={},idx={},w[{},{}]]: r={}",
p, lr.sgr_idx, lr.sgr_weights[0], lr.sgr_weights[1], ts.msac.rng,
);
}
}
}

#[no_mangle]
pub unsafe extern "C" fn dav1d_decode_tile_sbrow(t: *mut Dav1dTaskContext) -> libc::c_int {
let f: *const Dav1dFrameContext = (*t).f;
Expand Down Expand Up @@ -5031,7 +5013,7 @@ pub unsafe extern "C" fn dav1d_decode_tile_sbrow(t: *mut Dav1dTaskContext) -> li
.as_mut_ptr()
.offset(unit_idx as isize)
as *mut Av1RestorationUnit;
read_restoration_info(t, lr, p, frame_type);
read_restoration_info(&mut *t, &mut *lr, p, frame_type);
x += 1;
}
} else {
Expand All @@ -5048,7 +5030,7 @@ pub unsafe extern "C" fn dav1d_decode_tile_sbrow(t: *mut Dav1dTaskContext) -> li
.as_mut_ptr()
.offset(unit_idx_0 as isize)
as *mut Av1RestorationUnit;
read_restoration_info(t, lr_0, p, frame_type);
read_restoration_info(&mut *t, &mut *lr_0, p, frame_type);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lf_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::include::common::intops::iclip;
use crate::include::dav1d::headers::Dav1dFrameHeader;
use crate::include::dav1d::headers::Dav1dLoopfilterModeRefDeltas;
use crate::include::dav1d::headers::Dav1dPixelLayout;
use crate::include::dav1d::headers::Dav1dRestorationType;
use crate::include::dav1d::headers::DAV1D_PIXEL_LAYOUT_I420;
use crate::include::dav1d::headers::DAV1D_PIXEL_LAYOUT_I444;
use crate::include::stddef::ptrdiff_t;
Expand All @@ -24,7 +25,7 @@ pub struct Av1FilterLUT {
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Av1RestorationUnit {
pub type_0: u8,
pub r#type: Dav1dRestorationType,
pub filter_h: [i8; 3],
pub filter_v: [i8; 3],
pub sgr_idx: u8,
Expand Down
8 changes: 4 additions & 4 deletions src/lr_apply_tmpl_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ unsafe extern "C" fn lr_stripe(
let mut params: LooprestorationParams = LooprestorationParams {
filter: [[0; 8]; 2].into(),
};
if (*lr).type_0 as libc::c_int == DAV1D_RESTORATION_WIENER as libc::c_int {
if (*lr).r#type as libc::c_int == DAV1D_RESTORATION_WIENER as libc::c_int {
let filter: *mut [int16_t; 8] = (params.filter.0).as_mut_ptr();
let ref mut fresh0 = (*filter.offset(0))[6];
*fresh0 = (*lr).filter_h[0] as int16_t;
Expand Down Expand Up @@ -649,7 +649,7 @@ unsafe extern "C" fn lr_stripe(
| (*filter.offset(1))[0] as libc::c_int
== 0) as libc::c_int as usize];
} else {
if !((*lr).type_0 as libc::c_int == DAV1D_RESTORATION_SGRPROJ as libc::c_int) {
if !((*lr).r#type as libc::c_int == DAV1D_RESTORATION_SGRPROJ as libc::c_int) {
unreachable!();
}
let sgr_params: *const uint16_t = (dav1d_sgr_params[(*lr).sgr_idx as usize]).as_ptr();
Expand Down Expand Up @@ -756,7 +756,7 @@ unsafe extern "C" fn lr_sbrow(
.as_mut_ptr()
.offset(unit_idx as isize) as *mut Av1RestorationUnit;
let mut restore =
((*lr[0]).type_0 as libc::c_int != DAV1D_RESTORATION_NONE as libc::c_int) as libc::c_int;
((*lr[0]).r#type as libc::c_int != DAV1D_RESTORATION_NONE as libc::c_int) as libc::c_int;
let mut x = 0;
let mut bit = 0;
while x + max_unit_size <= w {
Expand All @@ -768,7 +768,7 @@ unsafe extern "C" fn lr_sbrow(
.offset(plane as isize))
.as_mut_ptr()
.offset(next_u_idx as isize) as *mut Av1RestorationUnit;
let restore_next = ((*lr[(bit == 0) as libc::c_int as usize]).type_0 as libc::c_int
let restore_next = ((*lr[(bit == 0) as libc::c_int as usize]).r#type as libc::c_int
!= DAV1D_RESTORATION_NONE as libc::c_int) as libc::c_int;
if restore_next != 0 {
backup4xU(
Expand Down
8 changes: 4 additions & 4 deletions src/lr_apply_tmpl_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ unsafe extern "C" fn lr_stripe(
let mut params: LooprestorationParams = LooprestorationParams {
filter: [[0; 8]; 2].into(),
};
if (*lr).type_0 as libc::c_int == DAV1D_RESTORATION_WIENER as libc::c_int {
if (*lr).r#type as libc::c_int == DAV1D_RESTORATION_WIENER as libc::c_int {
let filter: *mut [int16_t; 8] = (params.filter.0).as_mut_ptr();
let ref mut fresh0 = (*filter.offset(0))[6];
*fresh0 = (*lr).filter_h[0] as int16_t;
Expand Down Expand Up @@ -625,7 +625,7 @@ unsafe extern "C" fn lr_stripe(
| (*filter.offset(1))[0] as libc::c_int
== 0) as libc::c_int as usize];
} else {
if !((*lr).type_0 as libc::c_int == DAV1D_RESTORATION_SGRPROJ as libc::c_int) {
if !((*lr).r#type as libc::c_int == DAV1D_RESTORATION_SGRPROJ as libc::c_int) {
unreachable!();
}
let sgr_params: *const uint16_t = (dav1d_sgr_params[(*lr).sgr_idx as usize]).as_ptr();
Expand Down Expand Up @@ -732,7 +732,7 @@ unsafe extern "C" fn lr_sbrow(
.as_mut_ptr()
.offset(unit_idx as isize) as *mut Av1RestorationUnit;
let mut restore =
((*lr[0]).type_0 as libc::c_int != DAV1D_RESTORATION_NONE as libc::c_int) as libc::c_int;
((*lr[0]).r#type as libc::c_int != DAV1D_RESTORATION_NONE as libc::c_int) as libc::c_int;
let mut x = 0;
let mut bit = 0;
while x + max_unit_size <= w {
Expand All @@ -744,7 +744,7 @@ unsafe extern "C" fn lr_sbrow(
.offset(plane as isize))
.as_mut_ptr()
.offset(next_u_idx as isize) as *mut Av1RestorationUnit;
let restore_next = ((*lr[(bit == 0) as libc::c_int as usize]).type_0 as libc::c_int
let restore_next = ((*lr[(bit == 0) as libc::c_int as usize]).r#type as libc::c_int
!= DAV1D_RESTORATION_NONE as libc::c_int) as libc::c_int;
if restore_next != 0 {
backup4xU(
Expand Down

0 comments on commit 83683dc

Please sign in to comment.