Skip to content

Commit

Permalink
fn libc::*: Use libc fns instead of directly declaring them (#489)
Browse files Browse the repository at this point in the history
A bunch of these declared `fn`s have slightly incorrect (i.e.,
non-portable) signatures, so this also fixes that.

Also, where possible they are instead replaced with drop-in compatible
`std` `fn`s.
  • Loading branch information
kkysen authored Sep 25, 2023
2 parents 59c1862 + b25c368 commit 8db9f8c
Show file tree
Hide file tree
Showing 39 changed files with 327 additions and 489 deletions.
5 changes: 1 addition & 4 deletions src/cdef_apply_tmpl_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ use crate::src::internal::Dav1dDSPContext;
use crate::src::internal::Dav1dFrameContext;
use crate::src::internal::Dav1dTaskContext;
use crate::src::lf_mask::Av1Filter;
use libc::memcpy;
use libc::ptrdiff_t;
use std::cmp;
use std::ffi::c_int;
use std::ffi::c_uint;
use std::ffi::c_void;

extern "C" {
fn memcpy(_: *mut c_void, _: *const c_void, _: usize) -> *mut c_void;
}

pub type pixel = u16;

pub type Backup2x8Flags = c_uint;
Expand Down
5 changes: 1 addition & 4 deletions src/cdef_apply_tmpl_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ use crate::src::internal::Dav1dDSPContext;
use crate::src::internal::Dav1dFrameContext;
use crate::src::internal::Dav1dTaskContext;
use crate::src::lf_mask::Av1Filter;
use libc::memcpy;
use libc::ptrdiff_t;
use std::cmp;
use std::ffi::c_int;
use std::ffi::c_uint;
use std::ffi::c_void;

extern "C" {
fn memcpy(_: *mut c_void, _: *const c_void, _: usize) -> *mut c_void;
}

pub type pixel = u8;

pub type Backup2x8Flags = c_uint;
Expand Down
96 changes: 46 additions & 50 deletions src/cdf.rs

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ use crate::src::r#ref::dav1d_ref_inc;
use crate::src::r#ref::dav1d_ref_wrap;
use crate::src::r#ref::Dav1dRef;
use crate::stderr;
use libc::fprintf;
use libc::memset;
use std::ffi::c_char;
use std::ffi::c_int;
use std::ffi::c_ulong;
use std::ffi::c_void;

extern "C" {
fn memset(_: *mut c_void, _: c_int, _: c_ulong) -> *mut c_void;
fn fprintf(_: *mut libc::FILE, _: *const c_char, _: ...) -> c_int;
}

pub unsafe fn dav1d_data_create_internal(buf: *mut Dav1dData, sz: usize) -> *mut u8 {
if buf.is_null() {
fprintf(
Expand Down Expand Up @@ -194,7 +190,7 @@ pub unsafe fn dav1d_data_props_set_defaults(props: *mut Dav1dDataProps) {
memset(
props as *mut c_void,
0 as c_int,
::core::mem::size_of::<Dav1dDataProps>() as c_ulong,
::core::mem::size_of::<Dav1dDataProps>(),
);
(*props).timestamp = i64::MIN;
(*props).offset = -1;
Expand Down Expand Up @@ -248,7 +244,7 @@ pub unsafe fn dav1d_data_unref_internal(buf: *mut Dav1dData) {
memset(
buf as *mut c_void,
0 as c_int,
::core::mem::size_of::<Dav1dData>() as c_ulong,
::core::mem::size_of::<Dav1dData>(),
);
dav1d_data_props_set_defaults(&mut (*buf).m);
dav1d_ref_dec(&mut user_data_ref);
Expand Down
64 changes: 27 additions & 37 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ use crate::src::thread_task::TILE_ERROR;
use crate::src::warpmv::dav1d_find_affine_int;
use crate::src::warpmv::dav1d_get_shear_params;
use crate::src::warpmv::dav1d_set_affine_mv2d;
use libc::free;
use libc::malloc;
use libc::memcpy;
use libc::memset;
use libc::pthread_cond_signal;
use libc::pthread_cond_wait;
use libc::pthread_mutex_lock;
Expand All @@ -226,7 +230,6 @@ use std::cmp;
use std::ffi::c_char;
use std::ffi::c_int;
use std::ffi::c_uint;
use std::ffi::c_ulong;
use std::ffi::c_void;
use std::iter;
use std::ptr;
Expand All @@ -242,14 +245,10 @@ use crate::include::common::bitdepth::BitDepth16;
use crate::include::common::bitdepth::BitDepth8;

extern "C" {
fn memcpy(_: *mut c_void, _: *const c_void, _: c_ulong) -> *mut c_void;
fn memset(_: *mut c_void, _: c_int, _: usize) -> *mut c_void;
#[cfg(feature = "bitdepth_8")]
fn dav1d_cdef_dsp_init_8bpc(c: *mut Dav1dCdefDSPContext);
#[cfg(feature = "bitdepth_16")]
fn dav1d_cdef_dsp_init_16bpc(c: *mut Dav1dCdefDSPContext);
fn malloc(_: c_ulong) -> *mut c_void;
fn free(_: *mut c_void);
fn dav1d_cdf_thread_alloc(
c: *mut Dav1dContext,
cdf: *mut CdfThreadContext,
Expand Down Expand Up @@ -4422,7 +4421,7 @@ pub unsafe extern "C" fn dav1d_decode_tile_sbrow(t: *mut Dav1dTaskContext) -> c_
&mut *((*t).l.tx_lpf_y.0)
.as_mut_ptr()
.offset(((*t).by & 16) as isize) as *mut u8 as *const c_void,
sb_step as c_ulong,
sb_step as usize,
);
let ss_ver_0 =
((*f).cur.p.layout as c_uint == DAV1D_PIXEL_LAYOUT_I420 as c_int as c_uint) as c_int;
Expand All @@ -4434,7 +4433,7 @@ pub unsafe extern "C" fn dav1d_decode_tile_sbrow(t: *mut Dav1dTaskContext) -> c_
&mut *((*t).l.tx_lpf_uv.0)
.as_mut_ptr()
.offset((((*t).by & 16) >> ss_ver_0) as isize) as *mut u8 as *const c_void,
(sb_step >> ss_ver_0) as c_ulong,
(sb_step >> ss_ver_0) as usize,
);
return 0 as c_int;
}
Expand All @@ -4447,8 +4446,7 @@ pub unsafe extern "C" fn dav1d_decode_frame_init(f: *mut Dav1dFrameContext) -> c

if f.sbh > f.lf.start_of_tile_row_sz {
free(f.lf.start_of_tile_row as *mut c_void);
f.lf.start_of_tile_row =
malloc(f.sbh as c_ulong * ::core::mem::size_of::<u8>() as c_ulong) as *mut u8;
f.lf.start_of_tile_row = malloc(f.sbh as usize * ::core::mem::size_of::<u8>()) as *mut u8;
if f.lf.start_of_tile_row.is_null() {
f.lf.start_of_tile_row_sz = 0;
return -12;
Expand All @@ -4470,7 +4468,7 @@ pub unsafe extern "C" fn dav1d_decode_frame_init(f: *mut Dav1dFrameContext) -> c
if c.n_fc > 1 {
freep(&mut f.frame_thread.tile_start_off as *mut *mut c_int as *mut c_void);
f.frame_thread.tile_start_off =
malloc(::core::mem::size_of::<c_int>() as c_ulong * n_ts as c_ulong) as *mut c_int;
malloc(::core::mem::size_of::<c_int>() * n_ts as usize) as *mut c_int;
if f.frame_thread.tile_start_off.is_null() {
f.n_ts = 0;
return -12;
Expand All @@ -4488,8 +4486,7 @@ pub unsafe extern "C" fn dav1d_decode_frame_init(f: *mut Dav1dFrameContext) -> c
let a_sz = f.sb128w * (*f.frame_hdr).tiling.rows * (1 + (c.n_fc > 1 && c.n_tc > 1) as c_int);
if a_sz != f.a_sz {
freep(&mut f.a as *mut *mut BlockContext as *mut c_void);
f.a = malloc(::core::mem::size_of::<BlockContext>() as c_ulong * a_sz as c_ulong)
as *mut BlockContext;
f.a = malloc(::core::mem::size_of::<BlockContext>() * a_sz as usize) as *mut BlockContext;
if f.a.is_null() {
f.a_sz = 0;
return -12;
Expand Down Expand Up @@ -4526,10 +4523,9 @@ pub unsafe extern "C" fn dav1d_decode_frame_init(f: *mut Dav1dFrameContext) -> c
let lowest_pixel_mem_sz = (*f.frame_hdr).tiling.cols * f.sbh;
if lowest_pixel_mem_sz != f.tile_thread.lowest_pixel_mem_sz {
free(f.tile_thread.lowest_pixel_mem as *mut c_void);
f.tile_thread.lowest_pixel_mem = malloc(
lowest_pixel_mem_sz as c_ulong
* ::core::mem::size_of::<[[c_int; 2]; 7]>() as c_ulong,
) as *mut [[c_int; 2]; 7];
f.tile_thread.lowest_pixel_mem =
malloc(lowest_pixel_mem_sz as usize * ::core::mem::size_of::<[[c_int; 2]; 7]>())
as *mut [[c_int; 2]; 7];
if f.tile_thread.lowest_pixel_mem.is_null() {
f.tile_thread.lowest_pixel_mem_sz = 0;
return -12;
Expand Down Expand Up @@ -4723,29 +4719,25 @@ pub unsafe extern "C" fn dav1d_decode_frame_init(f: *mut Dav1dFrameContext) -> c
if num_sb128 != f.lf.mask_sz {
freep(&mut f.lf.mask as *mut *mut Av1Filter as *mut c_void);
freep(&mut f.lf.level as *mut *mut [u8; 4] as *mut c_void);
f.lf.mask = malloc(::core::mem::size_of::<Av1Filter>() as c_ulong * num_sb128 as c_ulong)
as *mut Av1Filter;
f.lf.mask =
malloc(::core::mem::size_of::<Av1Filter>() * num_sb128 as usize) as *mut Av1Filter;
// over-allocate by 3 bytes since some of the SIMD implementations
// index this from the level type and can thus over-read by up to 3
f.lf.level = malloc(
::core::mem::size_of::<[u8; 4]>() as c_ulong * num_sb128 as c_ulong * 32 * 32 + 3,
) as *mut [u8; 4];
f.lf.level = malloc(::core::mem::size_of::<[u8; 4]>() * num_sb128 as usize * 32 * 32 + 3)
as *mut [u8; 4];
if f.lf.mask.is_null() || f.lf.level.is_null() {
f.lf.mask_sz = 0;
return -12;
}
if c.n_fc > 1 {
freep(&mut f.frame_thread.b as *mut *mut Av1Block as *mut c_void);
freep(&mut f.frame_thread.cbi as *mut *mut CodedBlockInfo as *mut c_void);
f.frame_thread.b = malloc(
::core::mem::size_of::<Av1Block>() as c_ulong * num_sb128 as c_ulong * 32 * 32,
) as *mut Av1Block;
f.frame_thread.cbi = malloc(
::core::mem::size_of::<CodedBlockInfo>() as c_ulong
* num_sb128 as c_ulong
* 32
* 32,
) as *mut CodedBlockInfo;
f.frame_thread.b =
malloc(::core::mem::size_of::<Av1Block>() * num_sb128 as usize * 32 * 32)
as *mut Av1Block;
f.frame_thread.cbi =
malloc(::core::mem::size_of::<CodedBlockInfo>() * num_sb128 as usize * 32 * 32)
as *mut CodedBlockInfo;
if f.frame_thread.b.is_null() || f.frame_thread.cbi.is_null() {
f.lf.mask_sz = 0;
return -12;
Expand All @@ -4758,9 +4750,8 @@ pub unsafe extern "C" fn dav1d_decode_frame_init(f: *mut Dav1dFrameContext) -> c
let lr_mask_sz = f.sr_sb128w * f.sb128h;
if lr_mask_sz != f.lf.lr_mask_sz {
freep(&mut f.lf.lr_mask as *mut *mut Av1Restoration as *mut c_void);
f.lf.lr_mask =
malloc(::core::mem::size_of::<Av1Restoration>() as c_ulong * lr_mask_sz as c_ulong)
as *mut Av1Restoration;
f.lf.lr_mask = malloc(::core::mem::size_of::<Av1Restoration>() * lr_mask_sz as usize)
as *mut Av1Restoration;
if f.lf.lr_mask.is_null() {
f.lf.lr_mask_sz = 0;
return -12;
Expand Down Expand Up @@ -4802,7 +4793,7 @@ pub unsafe extern "C" fn dav1d_decode_frame_init(f: *mut Dav1dFrameContext) -> c
let re_sz = f.sb128h * (*f.frame_hdr).tiling.cols;
if re_sz != f.lf.re_sz {
freep(&mut *f.lf.tx_lpf_right_edge.as_mut_ptr().offset(0) as *mut *mut u8 as *mut c_void);
f.lf.tx_lpf_right_edge[0] = malloc(re_sz as c_ulong * 32 * 2) as *mut u8;
f.lf.tx_lpf_right_edge[0] = malloc(re_sz as usize * 32 * 2) as *mut u8;
if f.lf.tx_lpf_right_edge[0].is_null() {
f.lf.re_sz = 0;
return -12;
Expand Down Expand Up @@ -5397,9 +5388,8 @@ pub unsafe extern "C" fn dav1d_submit_frame(c: *mut Dav1dContext) -> c_int {
if f.n_tile_data_alloc < c.n_tile_data {
freep(&mut f.tile as *mut *mut Dav1dTileGroup as *mut c_void);
assert!(c.n_tile_data < i32::MAX / ::core::mem::size_of::<Dav1dTileGroup>() as c_int);
f.tile =
malloc(c.n_tile_data as c_ulong * ::core::mem::size_of::<Dav1dTileGroup>() as c_ulong)
as *mut Dav1dTileGroup;
f.tile = malloc(c.n_tile_data as usize * ::core::mem::size_of::<Dav1dTileGroup>())
as *mut Dav1dTileGroup;
if f.tile.is_null() {
f.n_tile_data = 0;
f.n_tile_data_alloc = f.n_tile_data;
Expand Down
26 changes: 11 additions & 15 deletions src/fg_apply_tmpl_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ use crate::src::align::Align1;
use crate::src::align::Align16;
use crate::src::filmgrain::Dav1dFilmGrainDSPContext;
use libc::intptr_t;
use libc::memcpy;
use libc::memset;
use libc::ptrdiff_t;
use std::cmp;
use std::ffi::c_int;
use std::ffi::c_uint;
use std::ffi::c_ulong;
use std::ffi::c_void;

extern "C" {
fn memcpy(_: *mut c_void, _: *const c_void, _: c_ulong) -> *mut c_void;
fn memset(_: *mut c_void, _: c_int, _: c_ulong) -> *mut c_void;
}

pub type pixel = u16;
pub type entry = i16;

Expand All @@ -43,13 +39,13 @@ unsafe extern "C" fn generate_scaling(
let shift_x = bitdepth - 8;
let scaling_size = (1 as c_int) << bitdepth;
if num == 0 {
memset(scaling as *mut c_void, 0 as c_int, scaling_size as c_ulong);
memset(scaling as *mut c_void, 0 as c_int, scaling_size as usize);
return;
}
memset(
scaling as *mut c_void,
(*points.offset(0))[1] as c_int,
(((*points.offset(0))[0] as c_int) << shift_x) as c_ulong,
(((*points.offset(0))[0] as c_int) << shift_x) as usize,
);
let mut i = 0;
while i < num - 1 {
Expand All @@ -76,7 +72,7 @@ unsafe extern "C" fn generate_scaling(
memset(
&mut *scaling.offset(n as isize) as *mut u8 as *mut c_void,
(*points.offset((num - 1) as isize))[1] as c_int,
(scaling_size - n) as c_ulong,
(scaling_size - n) as usize,
);
let pad = (1 as c_int) << shift_x;
let rnd = pad >> 1;
Expand Down Expand Up @@ -178,10 +174,10 @@ pub unsafe extern "C" fn dav1d_prep_grain_16bpc(
((*in_0).data[0] as *mut u8)
.offset(sz as isize)
.offset(-(stride as isize)) as *const c_void,
-sz as c_ulong,
-sz as usize,
);
} else {
memcpy((*out).data[0], (*in_0).data[0], sz as c_ulong);
memcpy((*out).data[0], (*in_0).data[0], sz as usize);
}
}
if (*in_0).p.layout as c_uint != DAV1D_PIXEL_LAYOUT_I400 as c_int as c_uint
Expand All @@ -203,7 +199,7 @@ pub unsafe extern "C" fn dav1d_prep_grain_16bpc(
((*in_0).data[1] as *mut u8)
.offset(sz_0 as isize)
.offset(-(stride_0 as isize)) as *const c_void,
-sz_0 as c_ulong,
-sz_0 as usize,
);
}
if (*data).num_uv_points[1] == 0 {
Expand All @@ -214,15 +210,15 @@ pub unsafe extern "C" fn dav1d_prep_grain_16bpc(
((*in_0).data[2] as *mut u8)
.offset(sz_0 as isize)
.offset(-(stride_0 as isize)) as *const c_void,
-sz_0 as c_ulong,
-sz_0 as usize,
);
}
} else {
if (*data).num_uv_points[0] == 0 {
memcpy((*out).data[1], (*in_0).data[1], sz_0 as c_ulong);
memcpy((*out).data[1], (*in_0).data[1], sz_0 as usize);
}
if (*data).num_uv_points[1] == 0 {
memcpy((*out).data[2], (*in_0).data[2], sz_0 as c_ulong);
memcpy((*out).data[2], (*in_0).data[2], sz_0 as usize);
}
}
}
Expand Down
Loading

0 comments on commit 8db9f8c

Please sign in to comment.