Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove libc dependency #176

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ rust-version = "1.70.0"

[dependencies]
rusty_ffmpeg = "0.14.0"
libc = "0.2"
paste = "1.0"
thiserror = "1.0"

[dev-dependencies]
libc = "0.2"
anyhow = "1.0.57"
cstr = "0.2.11"
once_cell = "1.12.0"
Expand Down
33 changes: 21 additions & 12 deletions src/avcodec/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,52 +79,61 @@ impl Iterator for AVCodecIter {
}

impl<'codec> AVCodec {
/// Convenient function for probing a pointer until met specific memory
/// pattern.
fn probe_len<T>(mut ptr: *const T, tail: T) -> usize {
/// Probing specific memory pattern and return the offset.
///
/// # Safety
/// ptr needs to be terminated by tail
unsafe fn probe_len<T>(mut ptr: *const T, tail: T) -> usize {
for len in 0.. {
if unsafe { libc::memcmp(ptr as _, &tail as *const _ as _, mem::size_of::<T>()) } == 0 {
let left = ptr as *const u8;
let left = unsafe { slice::from_raw_parts(left, mem::size_of::<T>()) };
let right = &tail as *const _ as *const u8;
let right = unsafe { slice::from_raw_parts(right, mem::size_of::<T>()) };
if left == right {
return len;
}
unsafe {
ptr = ptr.add(1);
}
}
unreachable!()
usize::MAX
}

/// Convenient function for building a memory slice.
fn build_array<'a, T>(ptr: *const T, tail: T) -> Option<&'a [T]> {
/// Building a memory slice ends begin with `ptr` and ends with given `tail`.
///
/// # Safety
/// ptr needs to be terminated by tail
unsafe fn build_array<'a, T>(ptr: *const T, tail: T) -> Option<&'a [T]> {
if ptr.is_null() {
None
} else {
let len = Self::probe_len(ptr, tail);
let len = unsafe { Self::probe_len(ptr, tail) };
Some(unsafe { slice::from_raw_parts(ptr, len) })
}
}

/// Return supported framerates of this [`AVCodec`].
pub fn supported_framerates(&'codec self) -> Option<&'codec [AVRational]> {
// terminates with AVRational{0, 0}
Self::build_array(self.supported_framerates, AVRational { den: 0, num: 0 })
unsafe { Self::build_array(self.supported_framerates, AVRational { den: 0, num: 0 }) }
}

/// Return supported pix_fmts of this [`AVCodec`].
pub fn pix_fmts(&'codec self) -> Option<&'codec [AVPixelFormat]> {
// terminates with -1
Self::build_array(self.pix_fmts, -1)
unsafe { Self::build_array(self.pix_fmts, -1) }
}

/// Return supported samplerates of this [`AVCodec`].
pub fn supported_samplerates(&'codec self) -> Option<&'codec [i32]> {
// terminates with 0
Self::build_array(self.supported_samplerates, 0)
unsafe { Self::build_array(self.supported_samplerates, 0) }
}

/// Return supported sample_fmts of this [`AVCodec`].
pub fn sample_fmts(&'codec self) -> Option<&'codec [ffi::AVSampleFormat]> {
// terminates with -1
Self::build_array(self.sample_fmts, -1)
unsafe { Self::build_array(self.sample_fmts, -1) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/avutil/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
ffi,
shared::{PointerUpgrade, RetUpgrade},
};
use libc::c_int;
use std::os::raw::c_int;

wrap!(AVBufferRef: ffi::AVBufferRef);

Expand Down
2 changes: 1 addition & 1 deletion src/avutil/channel_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::{
ffi,
shared::{PointerUpgrade, RetUpgrade},
};
use libc::c_void;
use std::{
ffi::{CStr, CString},
mem::MaybeUninit,
os::raw::c_void,
ptr::NonNull,
};

Expand Down
3 changes: 2 additions & 1 deletion src/avutil/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{error::Result, ffi, shared::*};

use std::{
ffi::{CStr, CString},
os::raw::c_void,
ptr::{self, NonNull},
};

Expand Down Expand Up @@ -120,7 +121,7 @@ impl AVDictionary {
.upgrade()?;
let result = unsafe { CStr::from_ptr(s).to_owned() };
unsafe {
ffi::av_freep(&mut s as *mut _ as *mut libc::c_void);
ffi::av_freep(&mut s as *mut _ as *mut c_void);
}
Ok(result)
}
Expand Down
4 changes: 2 additions & 2 deletions src/avutil/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
shared::*,
};

use std::{fmt, mem::size_of, ptr::NonNull, slice};
use std::{fmt, mem::size_of, os::raw::c_int, ptr::NonNull, slice};

wrap!(AVFrame: ffi::AVFrame);
settable!(AVFrame {
Expand Down Expand Up @@ -93,7 +93,7 @@ impl AVFrame {
unsafe { &mut self.deref_mut().data }
}

pub fn linesize_mut(&mut self) -> &mut [libc::c_int; 8] {
pub fn linesize_mut(&mut self) -> &mut [c_int; 8] {
unsafe { &mut self.deref_mut().linesize }
}

Expand Down
8 changes: 5 additions & 3 deletions src/avutil/rational.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::os::raw::c_int;

use crate::ffi;

pub use ffi::AVRational;
Expand All @@ -15,7 +17,7 @@ pub use ffi::{av_cmp_q, av_inv_q, av_make_q, av_q2d};
///
/// `d` - double to convert
/// `max` - Maximum allowed numerator and denominator
pub fn av_d2q(d: f64, max: libc::c_int) -> AVRational {
pub fn av_d2q(d: f64, max: c_int) -> AVRational {
unsafe { ffi::av_d2q(d, max) }
}

Expand Down Expand Up @@ -49,7 +51,7 @@ pub fn av_sub_q(b: AVRational, c: AVRational) -> AVRational {
/// Return `1` if `q1` is nearer to `q` than `q2`.
/// `-1` if `q2` is nearer to `q` than `q1`.
/// `0` if they have the same distance.
pub fn av_nearer_q(q: AVRational, q1: AVRational, q2: AVRational) -> libc::c_int {
pub fn av_nearer_q(q: AVRational, q1: AVRational, q2: AVRational) -> c_int {
unsafe { ffi::av_nearer_q(q, q1, q2) }
}

Expand All @@ -63,7 +65,7 @@ pub fn av_q2intfloat(q: AVRational) -> u32 {
#[inline]
/// Return the best rational so that a and b are multiple of it. If the
/// resulting denominator is larger than max_den, return def.
pub fn av_gcd_q(a: AVRational, b: AVRational, max_den: libc::c_int, def: AVRational) -> AVRational {
pub fn av_gcd_q(a: AVRational, b: AVRational, max_den: c_int, def: AVRational) -> AVRational {
unsafe { ffi::av_gcd_q(a, b, max_den, def) }
}

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Errors of the rsmpeg.
use libc::c_int;
use std::{
cmp::{Eq, PartialEq},
num::TryFromIntError,
os::raw::c_int,
};
use thiserror::Error;

Expand Down
3 changes: 1 addition & 2 deletions src/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Internal shared convenient things.
use crate::error::{Result, Ret, RsmpegError};
use libc::c_int;
use rusty_ffmpeg::ffi;
use std::{ops::Deref, ptr::NonNull};
use std::{ops::Deref, os::raw::c_int, ptr::NonNull};

/// Triage a pointer to Some(non-null) or None
pub trait PointerUpgrade<T>: Sized {
Expand Down