Skip to content

Commit

Permalink
Merge pull request #2 from phial3/develop
Browse files Browse the repository at this point in the history
Optimized code
  • Loading branch information
phial3 authored Jan 20, 2025
2 parents 5c08036 + 20aab72 commit 328c748
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
8 changes: 4 additions & 4 deletions examples/dump-frames.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate ffmpeg_the_third as ffmpeg;

use crate::ffmpeg::format::{self, Pixel};
use crate::ffmpeg::media::Type;
use crate::ffmpeg::software::scaling::{context::Context, flag::Flags};
use crate::ffmpeg::util::frame::video::Video;
use ffmpeg::format::{self, Pixel};
use ffmpeg::media::Type;
use ffmpeg::software::scaling::{context::Context, flag::Flags};
use ffmpeg::util::frame::video::Video;
use std::env;
use std::fs::File;
use std::io::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg-sys-third/src/avutil/channel_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ mod test {
for (i, (layout, valid)) in tests.iter().enumerate() {
unsafe {
println!("{i}");
assert!((av_channel_layout_check(layout as _) != 0) == *valid);
assert_eq!((av_channel_layout_check(layout as _) != 0), *valid);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/format/format/output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::path::Path;

use std::ffi::CString;
use std::ptr::{self, NonNull};

use super::Flags;
Expand Down Expand Up @@ -59,7 +58,7 @@ impl Output {

pub fn codec<P: AsRef<Path>>(self, path: P, kind: media::Type) -> codec::Id {
// XXX: use to_cstring when stable
let path = CString::new(path.as_ref().to_str().unwrap()).unwrap();
let path = utils::from_path(path);

unsafe {
codec::Id::from(av_guess_codec(
Expand Down
19 changes: 7 additions & 12 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@ pub fn license() -> &'static str {
unsafe { utils::str_from_c_ptr(avformat_license()) }
}

// XXX: use to_cstring when stable
fn from_path<P: AsRef<Path>>(path: P) -> CString {
CString::new(path.as_ref().as_os_str().to_str().unwrap()).unwrap()
}

pub fn input<P: AsRef<Path>>(path: P) -> Result<context::Input, Error> {
unsafe {
let mut ps = ptr::null_mut();
let path = from_path(path);
let path = utils::from_path(path);

match avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), ptr::null_mut()) {
0 => match avformat_find_stream_info(ps, ptr::null_mut()) {
Expand All @@ -65,7 +60,7 @@ pub fn input_with_dictionary<P: AsRef<Path>>(
) -> Result<context::Input, Error> {
unsafe {
let mut ps = ptr::null_mut();
let path = from_path(path);
let path = utils::from_path(path);
let mut opts = options.disown();
let res = avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), &mut opts);

Expand All @@ -91,7 +86,7 @@ where
{
unsafe {
let mut ps = avformat_alloc_context();
let path = from_path(path);
let path = utils::from_path(path);
(*ps).interrupt_callback = interrupt::new(Box::new(closure)).interrupt;

match avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), ptr::null_mut()) {
Expand All @@ -111,7 +106,7 @@ where
pub fn output<P: AsRef<Path>>(path: P) -> Result<context::Output, Error> {
unsafe {
let mut ps = ptr::null_mut();
let path = from_path(path);
let path = utils::from_path(path);

match avformat_alloc_output_context2(&mut ps, ptr::null_mut(), ptr::null(), path.as_ptr()) {
0 => match avio_open(&mut (*ps).pb, path.as_ptr(), AVIO_FLAG_WRITE) {
Expand All @@ -127,7 +122,7 @@ pub fn output<P: AsRef<Path>>(path: P) -> Result<context::Output, Error> {
pub fn output_with<P: AsRef<Path>>(path: P, options: Dictionary) -> Result<context::Output, Error> {
unsafe {
let mut ps = ptr::null_mut();
let path = from_path(path);
let path = utils::from_path(path);
let mut opts = options.disown();

match avformat_alloc_output_context2(&mut ps, ptr::null_mut(), ptr::null(), path.as_ptr()) {
Expand Down Expand Up @@ -156,7 +151,7 @@ pub fn output_with<P: AsRef<Path>>(path: P, options: Dictionary) -> Result<conte
pub fn output_as<P: AsRef<Path>>(path: P, format: &str) -> Result<context::Output, Error> {
unsafe {
let mut ps = ptr::null_mut();
let path = from_path(path);
let path = utils::from_path(path);
let format = CString::new(format).unwrap();

match avformat_alloc_output_context2(
Expand All @@ -182,7 +177,7 @@ pub fn output_as_with<P: AsRef<Path>>(
) -> Result<context::Output, Error> {
unsafe {
let mut ps = ptr::null_mut();
let path = from_path(path);
let path = utils::from_path(path);
let format = CString::new(format).unwrap();
let mut opts = options.disown();

Expand Down
5 changes: 1 addition & 4 deletions src/util/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ impl Rational {

#[inline]
pub fn reduce(&self) -> Rational {
match self.reduce_with_limit(i32::MAX) {
Ok(r) => r,
Err(r) => r,
}
self.reduce_with_limit(i32::MAX).unwrap_or_else(|r| r)
}

#[inline]
Expand Down
21 changes: 19 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
//! Internal utils, not related to `avutil`
use std::ffi::CStr;
use std::path::Path;
use std::{
ffi::{CStr, CString},
str::from_utf8_unchecked,
};

/// `ptr` must be non-null and valid.
/// Ensure that the returned lifetime is correctly bounded.
#[inline]
pub unsafe fn str_from_c_ptr<'s>(ptr: *const libc::c_char) -> &'s str {
unsafe { std::str::from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes()) }
unsafe { from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes()) }
}

/// `ptr` must be null or valid.
Expand All @@ -19,3 +23,16 @@ pub unsafe fn optional_str_from_c_ptr<'s>(ptr: *const libc::c_char) -> Option<&'
Some(str_from_c_ptr(ptr))
}
}

// XXX: use to_cstring when stable
pub fn from_path<P: AsRef<Path>>(path: P) -> CString {
// 只接受固定大小类型 ,可以获取所有权
CString::new(path.as_ref().to_str().unwrap()).unwrap()
}

#[allow(dead_code)]
pub fn from_path_dyn<P: AsRef<Path> + ?Sized>(path: &P) -> CString {
// 可以接受不定大小类型,必须通过引用使用
// &P 实现了 AsRef<Path>,可以直接传递给 from_path
from_path(path)
}

0 comments on commit 328c748

Please sign in to comment.