1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::ffi::CString;
use std::mem;
use ffi::*;
use libc::{c_int, c_void};
use util::format;
use {ChannelLayout, Error, Rational};
macro_rules! check {
($expr:expr) => {
match $expr {
0 => Ok(()),
e => Err(Error::from(e)),
}
};
}
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> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_bin(
self.as_mut_ptr(),
name.as_ptr(),
value as *const _ as *const _,
mem::size_of::<T>() as c_int,
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_str(&mut self, name: &str, value: &str) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
let value = CString::new(value).unwrap();
check!(av_opt_set(
self.as_mut_ptr(),
name.as_ptr(),
value.as_ptr(),
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_int(&mut self, name: &str, value: i64) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_int(
self.as_mut_ptr(),
name.as_ptr(),
value,
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_double(&mut self, name: &str, value: f64) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_double(
self.as_mut_ptr(),
name.as_ptr(),
value,
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_rational<T: Into<Rational>>(&mut self, name: &str, value: T) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_q(
self.as_mut_ptr(),
name.as_ptr(),
value.into().into(),
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_image_size(&mut self, name: &str, w: u32, h: u32) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_image_size(
self.as_mut_ptr(),
name.as_ptr(),
w as c_int,
h as c_int,
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_pixel_format(&mut self, name: &str, format: format::Pixel) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_pixel_fmt(
self.as_mut_ptr(),
name.as_ptr(),
format.into(),
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_sample_format(&mut self, name: &str, format: format::Sample) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_sample_fmt(
self.as_mut_ptr(),
name.as_ptr(),
format.into(),
AV_OPT_SEARCH_CHILDREN
))
}
}
fn set_channel_layout(&mut self, name: &str, layout: ChannelLayout) -> Result<(), Error> {
unsafe {
let name = CString::new(name).unwrap();
check!(av_opt_set_channel_layout(
self.as_mut_ptr(),
name.as_ptr(),
layout.bits() as i64,
AV_OPT_SEARCH_CHILDREN
))
}
}
}
pub trait Gettable: Target {}
pub trait Iterable: Target {}