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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use std::{ffi::CString, io::Write, ptr};
use libc::{c_char, c_int, c_uint, c_void};
use crate::{codec::CodecParameters, format::io::IO, packet::Packet, Error};
extern "C" {
fn ffw_guess_output_format(
short_name: *const c_char,
file_name: *const c_char,
mime_type: *const c_char,
) -> *mut c_void;
fn ffw_muxer_new() -> *mut c_void;
fn ffw_muxer_get_nb_streams(muxer: *const c_void) -> c_uint;
fn ffw_muxer_new_stream(muxer: *mut c_void, params: *const c_void) -> c_int;
fn ffw_muxer_init(muxer: *mut c_void, io_context: *mut c_void, format: *mut c_void) -> c_int;
fn ffw_muxer_set_initial_option(
muxer: *mut c_void,
key: *const c_char,
value: *const c_char,
) -> c_int;
fn ffw_muxer_set_option(muxer: *mut c_void, key: *const c_char, value: *const c_char) -> c_int;
fn ffw_muxer_write_frame(
muxer: *mut c_void,
packet: *mut c_void,
tb_num: u32,
tb_den: u32,
) -> c_int;
fn ffw_muxer_interleaved_write_frame(
muxer: *mut c_void,
packet: *mut c_void,
tb_num: u32,
tb_den: u32,
) -> c_int;
fn ffw_muxer_free(muxer: *mut c_void);
}
pub struct MuxerBuilder {
ptr: *mut c_void,
interleaved: bool,
}
impl MuxerBuilder {
fn new() -> MuxerBuilder {
let ptr = unsafe { ffw_muxer_new() };
if ptr.is_null() {
panic!("unable to allocate a muxer context");
}
MuxerBuilder {
ptr,
interleaved: false,
}
}
pub fn add_stream(&mut self, params: &CodecParameters) -> Result<(), Error> {
let ret = unsafe { ffw_muxer_new_stream(self.ptr, params.as_ptr()) };
if ret < 0 {
return Err(Error::from_raw_error_code(ret));
}
Ok(())
}
pub fn set_option<V>(self, name: &str, value: V) -> MuxerBuilder
where
V: ToString,
{
let name = CString::new(name).expect("invalid option name");
let value = CString::new(value.to_string()).expect("invalid option value");
let ret = unsafe {
ffw_muxer_set_initial_option(self.ptr, name.as_ptr() as _, value.as_ptr() as _)
};
if ret < 0 {
panic!("unable to allocate an option");
}
self
}
pub fn interleaved(mut self, interleaved: bool) -> MuxerBuilder {
self.interleaved = interleaved;
self
}
pub fn build<T>(mut self, mut io: IO<T>, format: OutputFormat) -> Result<Muxer<T>, Error>
where
T: Write,
{
let io_context_ptr = io.io_context_mut().as_mut_ptr();
let format_ptr = format.ptr;
let ret = unsafe { ffw_muxer_init(self.ptr, io_context_ptr, format_ptr) };
if ret < 0 {
return Err(Error::from_raw_error_code(ret));
}
let muxer_ptr = self.ptr;
self.ptr = ptr::null_mut();
let res = Muxer {
ptr: muxer_ptr,
io,
interleaved: self.interleaved,
};
Ok(res)
}
}
impl Drop for MuxerBuilder {
fn drop(&mut self) {
unsafe { ffw_muxer_free(self.ptr) }
}
}
unsafe impl Send for MuxerBuilder {}
unsafe impl Sync for MuxerBuilder {}
pub struct Muxer<T> {
ptr: *mut c_void,
io: IO<T>,
interleaved: bool,
}
impl Muxer<()> {
pub fn builder() -> MuxerBuilder {
MuxerBuilder::new()
}
}
impl<T> Muxer<T> {
pub fn set_option<V>(&mut self, name: &str, value: V) -> Result<(), Error>
where
V: ToString,
{
let name = CString::new(name).expect("invalid option name");
let value = CString::new(value.to_string()).expect("invalid option value");
let ret =
unsafe { ffw_muxer_set_option(self.ptr, name.as_ptr() as _, value.as_ptr() as _) };
if ret < 0 {
Err(Error::from_raw_error_code(ret))
} else {
Ok(())
}
}
pub fn push(&mut self, mut packet: Packet) -> Result<(), Error> {
let nb_streams = unsafe { ffw_muxer_get_nb_streams(self.ptr) as usize };
assert!(packet.stream_index() < nb_streams);
let tb = packet.time_base();
let ret = unsafe {
if self.interleaved {
ffw_muxer_interleaved_write_frame(self.ptr, packet.as_mut_ptr(), tb.num(), tb.den())
} else {
ffw_muxer_write_frame(self.ptr, packet.as_mut_ptr(), tb.num(), tb.den())
}
};
if ret < 0 {
Err(Error::from_raw_error_code(ret))
} else {
Ok(())
}
}
pub fn flush(&mut self) -> Result<(), Error> {
let ret = unsafe {
if self.interleaved {
ffw_muxer_interleaved_write_frame(self.ptr, ptr::null_mut(), 1, 1_000_000)
} else {
ffw_muxer_write_frame(self.ptr, ptr::null_mut(), 1, 1_000_000)
}
};
if ret < 0 {
Err(Error::from_raw_error_code(ret))
} else {
Ok(())
}
}
pub fn io(&self) -> &IO<T> {
&self.io
}
pub fn io_mut(&mut self) -> &mut IO<T> {
&mut self.io
}
}
impl<T> Drop for Muxer<T> {
fn drop(&mut self) {
unsafe { ffw_muxer_free(self.ptr) }
}
}
unsafe impl<T> Send for Muxer<T> where T: Send {}
unsafe impl<T> Sync for Muxer<T> where T: Sync {}
pub struct OutputFormat {
ptr: *mut c_void,
}
impl OutputFormat {
pub fn find_by_name(name: &str) -> Option<OutputFormat> {
let name = CString::new(name).expect("invalid format name");
let ptr =
unsafe { ffw_guess_output_format(name.as_ptr() as *const _, ptr::null(), ptr::null()) };
if ptr.is_null() {
return None;
}
let res = OutputFormat { ptr };
Some(res)
}
pub fn find_by_mime_type(mime_type: &str) -> Option<OutputFormat> {
let mime_type = CString::new(mime_type).expect("invalid MIME type");
let ptr = unsafe {
ffw_guess_output_format(ptr::null(), ptr::null(), mime_type.as_ptr() as *const _)
};
if ptr.is_null() {
return None;
}
let res = OutputFormat { ptr };
Some(res)
}
pub fn guess_from_file_name(file_name: &str) -> Option<OutputFormat> {
let file_name = CString::new(file_name).expect("invalid file name");
let ptr = unsafe {
ffw_guess_output_format(ptr::null(), file_name.as_ptr() as *const _, ptr::null())
};
if ptr.is_null() {
return None;
}
let res = OutputFormat { ptr };
Some(res)
}
}
unsafe impl Send for OutputFormat {}
unsafe impl Sync for OutputFormat {}