Skip to content

Commit

Permalink
Merge pull request #5 from phial3/develop
Browse files Browse the repository at this point in the history
optimize code:
  • Loading branch information
phial3 authored Feb 10, 2025
2 parents dc97ba5 + 371dcfd commit d5258f0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
19 changes: 17 additions & 2 deletions examples/dump-frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ fn main() -> Result<(), ffmpeg::Error> {
.ok_or(ffmpeg::Error::StreamNotFound)?;
let video_stream_index = input.index();

let context_decoder = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
let mut context_decoder =
ffmpeg::codec::context::Context::from_parameters(input.parameters())?;

if let Ok(parallelism) = std::thread::available_parallelism() {
context_decoder.set_threading(ffmpeg::threading::Config {
kind: ffmpeg::threading::Type::Frame,
count: parallelism.get(),
#[cfg(not(feature = "ffmpeg_6_0"))]
safe: false,
});
}

let mut decoder = context_decoder.decoder().video()?;

let mut scaler = Context::get(
Expand Down Expand Up @@ -59,7 +70,11 @@ fn main() -> Result<(), ffmpeg::Error> {
}

fn save_file(frame: &Video, index: usize) -> std::result::Result<(), std::io::Error> {
let mut file = File::create(format!("frame{index}.ppm"))?;
let dump_dir = std::path::Path::new("/tmp/dump");
if !dump_dir.exists() {
std::fs::create_dir_all(dump_dir)?;
}
let mut file = File::create(format!("{}/frame{index}.ppm", dump_dir.to_str().unwrap()))?;
file.write_all(format!("P6\n{} {}\n255\n", frame.width(), frame.height()).as_bytes())?;
file.write_all(frame.data(0))?;
Ok(())
Expand Down
18 changes: 17 additions & 1 deletion src/codec/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use super::encoder::Encoder;
use super::{threading, Compliance, Debug, Flags, Id};
use crate::ffi::*;
use crate::media;
use crate::AsPtr;
use crate::option;
use crate::{AsMutPtr, AsPtr};
use crate::{Codec, Error};
use libc::c_int;

Expand Down Expand Up @@ -167,3 +168,18 @@ impl Clone for Context {
}
}
}

/// `AVCodecContext` in `Context` is the target of `option` operations.
impl AsPtr<AVCodecContext> for Context {
fn as_ptr(&self) -> *const AVCodecContext {
self.ptr as *const _
}
}

impl AsMutPtr<AVCodecContext> for Context {
fn as_mut_ptr(&mut self) -> *mut AVCodecContext {
self.ptr as *mut _
}
}

impl option::Settable<AVCodecContext> for Context {}
13 changes: 7 additions & 6 deletions src/filter/context/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::marker::PhantomData;

use super::{Sink, Source};
use crate::ffi::*;
use crate::{format, option};
use libc::c_void;
use crate::{format, option, AsMutPtr, AsPtr};

#[cfg(feature = "ffmpeg_5_1")]
use crate::ChannelLayout;
Expand Down Expand Up @@ -66,14 +65,16 @@ impl<'a> Context<'a> {
}
}

unsafe impl<'a> option::Target for Context<'a> {
fn as_ptr(&self) -> *const c_void {
impl<'a> AsPtr<AVFilterContext> for Context<'a> {
fn as_ptr(&self) -> *const AVFilterContext {
self.ptr as *const _
}
}

fn as_mut_ptr(&mut self) -> *mut c_void {
impl<'a> AsMutPtr<AVFilterContext> for Context<'a> {
fn as_mut_ptr(&mut self) -> *mut AVFilterContext {
self.ptr as *mut _
}
}

impl<'a> option::Settable for Context<'a> {}
impl<'a> option::Settable<AVFilterContext> for Context<'a> {}
2 changes: 1 addition & 1 deletion src/util/option/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod traits;
pub use self::traits::{Gettable, Iterable, Settable, Target};
pub use self::traits::{Gettable, Iterable, Settable};

use crate::ffi::*;
use libc::c_uint;
Expand Down
39 changes: 17 additions & 22 deletions src/util/option/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::mem;

use crate::ffi::*;
use crate::util::format;
use crate::{Error, Rational};
use libc::{c_int, c_void};
use crate::{AsMutPtr, AsPtr, Error, Rational};
use libc::c_int;

#[cfg(not(feature = "ffmpeg_7_0"))]
use crate::ChannelLayoutMask;
Expand All @@ -20,21 +20,16 @@ macro_rules! check {
};
}

pub unsafe trait Target {
fn as_ptr(&self) -> *const c_void;
fn as_mut_ptr(&mut self) -> *mut c_void;
}

pub trait Settable: Target {
fn set<T: 'static>(&mut self, name: &str, value: &T) -> Result<(), Error> {
pub trait Settable<T>: AsPtr<T> + AsMutPtr<T> {
fn set<V: 'static>(&mut self, name: &str, value: &V) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();

check!(av_opt_set_bin(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
value as *const _ as *const _,
mem::size_of::<T>() as c_int,
mem::size_of::<V>() as c_int,
AV_OPT_SEARCH_CHILDREN
))
}
Expand All @@ -46,7 +41,7 @@ pub trait Settable: Target {
let value = CString::new(value).unwrap();

check!(av_opt_set(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
value.as_ptr(),
AV_OPT_SEARCH_CHILDREN
Expand All @@ -59,7 +54,7 @@ pub trait Settable: Target {
let name = CString::new(name).unwrap();

check!(av_opt_set_int(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
value,
AV_OPT_SEARCH_CHILDREN
Expand All @@ -72,20 +67,20 @@ pub trait Settable: Target {
let name = CString::new(name).unwrap();

check!(av_opt_set_double(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
value,
AV_OPT_SEARCH_CHILDREN
))
}
}

fn set_rational<T: Into<Rational>>(&mut self, name: &str, value: T) -> Result<(), Error> {
fn set_rational<V: Into<Rational>>(&mut self, name: &str, value: V) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();

check!(av_opt_set_q(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
value.into().into(),
AV_OPT_SEARCH_CHILDREN
Expand All @@ -98,7 +93,7 @@ pub trait Settable: Target {
let name = CString::new(name).unwrap();

check!(av_opt_set_image_size(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
w as c_int,
h as c_int,
Expand All @@ -112,7 +107,7 @@ pub trait Settable: Target {
let name = CString::new(name).unwrap();

check!(av_opt_set_pixel_fmt(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
format.into(),
AV_OPT_SEARCH_CHILDREN
Expand All @@ -125,7 +120,7 @@ pub trait Settable: Target {
let name = CString::new(name).unwrap();

check!(av_opt_set_sample_fmt(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
format.into(),
AV_OPT_SEARCH_CHILDREN
Expand All @@ -139,7 +134,7 @@ pub trait Settable: Target {
let name = CString::new(name).unwrap();

check!(av_opt_set_channel_layout(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
layout.bits() as i64,
AV_OPT_SEARCH_CHILDREN
Expand All @@ -148,6 +143,6 @@ pub trait Settable: Target {
}
}

pub trait Gettable: Target {}
pub trait Gettable<T>: AsPtr<T> + AsMutPtr<T> {}

pub trait Iterable: Target {}
pub trait Iterable<T>: AsPtr<T> + AsMutPtr<T> {}

0 comments on commit d5258f0

Please sign in to comment.