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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::ptr;
use super::Delay;
use ffi::*;
use libc::c_int;
use std::ffi::c_void;
use util::format;
use Dictionary;
use {frame, ChannelLayout, Error};
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct Definition {
pub format: format::Sample,
pub channel_layout: ChannelLayout,
pub rate: u32,
}
pub struct Context {
ptr: *mut SwrContext,
input: Definition,
output: Definition,
}
unsafe impl Send for Context {}
impl Context {
#[doc(hidden)]
pub unsafe fn as_ptr(&self) -> *const SwrContext {
self.ptr as *const _
}
#[doc(hidden)]
pub unsafe fn as_mut_ptr(&mut self) -> *mut SwrContext {
self.ptr
}
}
impl Context {
pub fn get(
src_format: format::Sample,
src_channel_layout: ChannelLayout,
src_rate: u32,
dst_format: format::Sample,
dst_channel_layout: ChannelLayout,
dst_rate: u32,
) -> Result<Self, Error> {
Self::get_with(
src_format,
src_channel_layout,
src_rate,
dst_format,
dst_channel_layout,
dst_rate,
Dictionary::new(),
)
}
pub fn get_with(
src_format: format::Sample,
src_channel_layout: ChannelLayout,
src_rate: u32,
dst_format: format::Sample,
dst_channel_layout: ChannelLayout,
dst_rate: u32,
options: Dictionary,
) -> Result<Self, Error> {
unsafe {
let ptr = swr_alloc_set_opts(
ptr::null_mut(),
dst_channel_layout.bits() as i64,
dst_format.into(),
dst_rate as c_int,
src_channel_layout.bits() as i64,
src_format.into(),
src_rate as c_int,
0,
ptr::null_mut(),
);
let mut opts = options.disown();
let res = av_opt_set_dict(ptr as *mut c_void, &mut opts);
Dictionary::own(opts);
if res != 0 {
return Err(Error::from(res));
}
if !ptr.is_null() {
match swr_init(ptr) {
e if e < 0 => Err(Error::from(e)),
_ => Ok(Context {
ptr,
input: Definition {
format: src_format,
channel_layout: src_channel_layout,
rate: src_rate,
},
output: Definition {
format: dst_format,
channel_layout: dst_channel_layout,
rate: dst_rate,
},
}),
}
} else {
Err(Error::InvalidData)
}
}
}
pub fn input(&self) -> &Definition {
&self.input
}
pub fn output(&self) -> &Definition {
&self.output
}
pub fn delay(&self) -> Option<Delay> {
unsafe {
match swr_get_delay(self.as_ptr() as *mut _, 1) {
0 => None,
_ => Some(Delay::from(self)),
}
}
}
pub fn run(
&mut self,
input: &frame::Audio,
output: &mut frame::Audio,
) -> Result<Option<Delay>, Error> {
unsafe {
(*output.as_mut_ptr()).sample_rate = self.output.rate as i32;
}
unsafe {
if output.is_empty() {
output.alloc(
self.output.format,
input.samples(),
self.output.channel_layout,
);
}
match swr_convert_frame(self.as_mut_ptr(), output.as_mut_ptr(), input.as_ptr()) {
0 => Ok(self.delay()),
e => Err(Error::from(e)),
}
}
}
pub fn flush(&mut self, output: &mut frame::Audio) -> Result<Option<Delay>, Error> {
unsafe {
(*output.as_mut_ptr()).sample_rate = self.output.rate as i32;
}
unsafe {
match swr_convert_frame(self.as_mut_ptr(), output.as_mut_ptr(), ptr::null()) {
0 => Ok(self.delay()),
e => Err(Error::from(e)),
}
}
}
}
impl Drop for Context {
fn drop(&mut self) {
unsafe {
swr_free(&mut self.as_mut_ptr());
}
}
}