Skip to content

Commit 328c748

Browse files
authored
Merge pull request #2 from phial3/develop
Optimized code
2 parents 5c08036 + 20aab72 commit 328c748

File tree

6 files changed

+33
-25
lines changed

6 files changed

+33
-25
lines changed

examples/dump-frames.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
extern crate ffmpeg_the_third as ffmpeg;
22

3-
use crate::ffmpeg::format::{self, Pixel};
4-
use crate::ffmpeg::media::Type;
5-
use crate::ffmpeg::software::scaling::{context::Context, flag::Flags};
6-
use crate::ffmpeg::util::frame::video::Video;
3+
use ffmpeg::format::{self, Pixel};
4+
use ffmpeg::media::Type;
5+
use ffmpeg::software::scaling::{context::Context, flag::Flags};
6+
use ffmpeg::util::frame::video::Video;
77
use std::env;
88
use std::fs::File;
99
use std::io::prelude::*;

ffmpeg-sys-third/src/avutil/channel_layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ mod test {
408408
for (i, (layout, valid)) in tests.iter().enumerate() {
409409
unsafe {
410410
println!("{i}");
411-
assert!((av_channel_layout_check(layout as _) != 0) == *valid);
411+
assert_eq!((av_channel_layout_check(layout as _) != 0), *valid);
412412
}
413413
}
414414
}

src/format/format/output.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::path::Path;
22

3-
use std::ffi::CString;
43
use std::ptr::{self, NonNull};
54

65
use super::Flags;
@@ -59,7 +58,7 @@ impl Output {
5958

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

6463
unsafe {
6564
codec::Id::from(av_guess_codec(

src/format/mod.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,10 @@ pub fn license() -> &'static str {
3535
unsafe { utils::str_from_c_ptr(avformat_license()) }
3636
}
3737

38-
// XXX: use to_cstring when stable
39-
fn from_path<P: AsRef<Path>>(path: P) -> CString {
40-
CString::new(path.as_ref().as_os_str().to_str().unwrap()).unwrap()
41-
}
42-
4338
pub fn input<P: AsRef<Path>>(path: P) -> Result<context::Input, Error> {
4439
unsafe {
4540
let mut ps = ptr::null_mut();
46-
let path = from_path(path);
41+
let path = utils::from_path(path);
4742

4843
match avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), ptr::null_mut()) {
4944
0 => match avformat_find_stream_info(ps, ptr::null_mut()) {
@@ -65,7 +60,7 @@ pub fn input_with_dictionary<P: AsRef<Path>>(
6560
) -> Result<context::Input, Error> {
6661
unsafe {
6762
let mut ps = ptr::null_mut();
68-
let path = from_path(path);
63+
let path = utils::from_path(path);
6964
let mut opts = options.disown();
7065
let res = avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), &mut opts);
7166

@@ -91,7 +86,7 @@ where
9186
{
9287
unsafe {
9388
let mut ps = avformat_alloc_context();
94-
let path = from_path(path);
89+
let path = utils::from_path(path);
9590
(*ps).interrupt_callback = interrupt::new(Box::new(closure)).interrupt;
9691

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

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

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

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

src/util/rational.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ impl Rational {
2626

2727
#[inline]
2828
pub fn reduce(&self) -> Rational {
29-
match self.reduce_with_limit(i32::MAX) {
30-
Ok(r) => r,
31-
Err(r) => r,
32-
}
29+
self.reduce_with_limit(i32::MAX).unwrap_or_else(|r| r)
3330
}
3431

3532
#[inline]

src/utils.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
//! Internal utils, not related to `avutil`
22
3-
use std::ffi::CStr;
3+
use std::path::Path;
4+
use std::{
5+
ffi::{CStr, CString},
6+
str::from_utf8_unchecked,
7+
};
48

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

1216
/// `ptr` must be null or valid.
@@ -19,3 +23,16 @@ pub unsafe fn optional_str_from_c_ptr<'s>(ptr: *const libc::c_char) -> Option<&'
1923
Some(str_from_c_ptr(ptr))
2024
}
2125
}
26+
27+
// XXX: use to_cstring when stable
28+
pub fn from_path<P: AsRef<Path>>(path: P) -> CString {
29+
// 只接受固定大小类型 ,可以获取所有权
30+
CString::new(path.as_ref().to_str().unwrap()).unwrap()
31+
}
32+
33+
#[allow(dead_code)]
34+
pub fn from_path_dyn<P: AsRef<Path> + ?Sized>(path: &P) -> CString {
35+
// 可以接受不定大小类型,必须通过引用使用
36+
// &P 实现了 AsRef<Path>,可以直接传递给 from_path
37+
from_path(path)
38+
}

0 commit comments

Comments
 (0)