Skip to content

Commit

Permalink
Make codec::Context Settable.
Browse files Browse the repository at this point in the history
  • Loading branch information
hgaiser committed Apr 11, 2024
1 parent ea267ee commit d0619be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
14 changes: 14 additions & 0 deletions src/codec/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::any::Any;
use std::ptr;
use std::rc::Rc;

use crate::option;

use super::decoder::Decoder;
use super::encoder::Encoder;
use super::{threading, Compliance, Debug, Flags, Id, Parameters};
Expand Down Expand Up @@ -197,3 +199,15 @@ impl Clone for Context {
}
}
}

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

fn as_mut_ptr(&mut self) -> *mut AVCodecContext {
self.ptr as *mut _
}
}

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

use super::{Sink, Source};
use ffi::*;
use libc::c_void;
use {format, option, ChannelLayout};

pub struct Context<'a> {
Expand Down Expand Up @@ -61,14 +60,14 @@ impl<'a> Context<'a> {
}
}

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

fn as_mut_ptr(&mut self) -> *mut c_void {
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> {}
40 changes: 20 additions & 20 deletions src/util/option/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ffi::CString;
use std::mem;

use ffi::*;
use libc::{c_int, c_void};
use libc::c_int;
use util::format;
use {ChannelLayout, Error, Rational};

Expand All @@ -17,21 +17,21 @@ macro_rules! check {
};
}

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

pub trait Settable: Target {
fn set<T: 'static>(&mut self, name: &str, value: &T) -> Result<(), Error> {
pub trait Settable<T>: Target<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 @@ -43,7 +43,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 @@ -56,7 +56,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 @@ -69,20 +69,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 @@ -95,7 +95,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 @@ -109,7 +109,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 @@ -122,7 +122,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 @@ -137,7 +137,7 @@ pub trait Settable: Target {
#[cfg(not(feature = "ffmpeg_7_0"))]
{
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 @@ -147,7 +147,7 @@ pub trait Settable: Target {
#[cfg(feature = "ffmpeg_7_0")]
{
check!(av_opt_set_chlayout(
self.as_mut_ptr(),
self.as_mut_ptr() as *mut _,
name.as_ptr(),
&layout.into(),
AV_OPT_SEARCH_CHILDREN
Expand All @@ -157,6 +157,6 @@ pub trait Settable: Target {
}
}

pub trait Gettable: Target {}
pub trait Gettable<T>: Target<T> {}

pub trait Iterable: Target {}
pub trait Iterable<T>: Target<T> {}

0 comments on commit d0619be

Please sign in to comment.