ffav 4.3.12

Safe FFmpeg wrapper (FFmpeg 4 compatible fork of the ffmpeg-next crate)
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use super::AVResult;
use crate::ffi::*;
use std::convert::TryInto;
use std::error::Error;
use std::ffi::{CStr, CString};
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_char;
use std::path::Path;
use std::str::FromStr;

#[derive(Debug)]
pub enum AVBSFError {
    Again,
    Reason(String),
}

#[repr(transparent)]
#[derive(Debug)]
pub struct AVBSFContextOwned {
    ptr: *mut AVBSFContext,
}

impl Default for AVBSFContextOwned {
    fn default() -> Self {
        unsafe {
            let mut ptr: *mut AVBSFContext = std::ptr::null_mut();
            if av_bsf_get_null_filter(&mut ptr) == 0 {
                av_bsf_init(ptr);
            }
            Self { ptr }
        }
    }
}

impl Drop for AVBSFContextOwned {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                av_bsf_free(&mut self.ptr);
            }
        }
    }
}

impl Deref for AVBSFContextOwned {
    type Target = AVBSFContext;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.ptr }
    }
}

impl DerefMut for AVBSFContextOwned {
    fn deref_mut(&mut self) -> &mut AVBSFContext {
        unsafe { &mut *self.ptr }
    }
}

impl AVBSFContextOwned {
    /// Allocate a context for a given bitstream filter.
    /// The caller must fill in the context parameters as described in the
    /// documentation and then call init() before sending any data to the filter.
    pub fn new(name: &str) -> AVResult<Self> {
        unsafe {
            let cname = CString::new(name).unwrap();
            let filter = av_bsf_get_by_name(cname.as_ptr());
            if filter.is_null() {
                Err(format!("Bitstream Filter {:?} does not exists!", name).into())
            } else {
                let mut ptr: *mut AVBSFContext = std::ptr::null_mut();
                let err = av_bsf_alloc(filter, &mut ptr);
                if err < 0 {
                    Err(av_err2str(err).into())
                } else {
                    Ok(Self { ptr })
                }
            }
        }
    }

    /// Reset the internal bitstream filter state / flush internal buffers.
    pub fn flush(&mut self) {
        unsafe {
            av_bsf_flush(self.ptr);
        }
    }

    /// Prepare the filter for use, after all the parameters and options have been set.
    pub fn prepare(&mut self, codecpar: Option<&AVCodecParameters>) -> AVResult<()> {
        unsafe {
            if let Some(codecpar) = codecpar {
                avcodec_parameters_copy(self.par_in, codecpar);
            }
            let err = av_bsf_init(self.ptr);
            if err < 0 {
                Err(av_err2str(err).into())
            } else {
                Ok(())
            }
        }
    }

    /// Retrieve a filtered packet.
    pub fn receive_packet(&mut self) -> Result<AVPacketOwned, AVBSFError> {
        unsafe {
            let mut packet = AVPacketOwned::default();
            let err = av_bsf_receive_packet(self.ptr, packet.as_mut_ptr());
            if err < 0 {
                if err == AVERROR(11) {
                    Err(AVBSFError::Again)
                } else {
                    Err(AVBSFError::Reason(av_err2str(err)))
                }
            } else {
                Ok(packet)
            }
        }
    }

    /// Submit a packet for filtering.
    pub fn send_packet(&mut self, packet: &mut AVPacket) -> Result<(), AVBSFError> {
        unsafe {
            let err = av_bsf_send_packet(self.ptr, packet);
            if err < 0 {
                if err == AVERROR(11) {
                    Err(AVBSFError::Again)
                } else {
                    Err(AVBSFError::Reason(av_err2str(err)))
                }
            } else {
                Ok(())
            }
        }
    }

    pub fn as_ptr(&self) -> *const AVBSFContext {
        self.ptr as *const AVBSFContext
    }

    pub fn as_mut_ptr(&mut self) -> *mut AVBSFContext {
        self.ptr
    }

    pub fn as_mut_ptr_ref(&mut self) -> &mut *mut AVBSFContext {
        &mut self.ptr
    }
}

/// Wrap an owned AVDictionary pointer.
#[repr(transparent)]
#[derive(Debug)]
pub struct AVDictionaryOwned {
    ptr: *mut AVDictionary,
}

impl Default for AVDictionaryOwned {
    fn default() -> Self {
        Self {
            ptr: std::ptr::null_mut(),
        }
    }
}

impl Drop for AVDictionaryOwned {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                av_dict_free(&mut self.ptr);
            }
        }
    }
}

impl Deref for AVDictionaryOwned {
    type Target = AVDictionary;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.ptr }
    }
}

impl DerefMut for AVDictionaryOwned {
    fn deref_mut(&mut self) -> &mut AVDictionary {
        unsafe { &mut *self.ptr }
    }
}

impl FromStr for AVDictionaryOwned {
    type Err = Box<dyn Error>;
    /// Create an an owned AVDictionary from string.
    ///
    /// The format of the string like: "key1=value1:key2=value2"
    fn from_str(options: &str) -> Result<Self, Self::Err> {
        unsafe {
            let mut ptr: *mut AVDictionary = std::ptr::null_mut();
            let options = CString::new(options).unwrap();
            let kv_sep = CString::new("=").unwrap();
            let pair_sep = CString::new(":").unwrap();
            let err = av_dict_parse_string(
                &mut ptr,
                options.as_ptr(),
                kv_sep.as_ptr(),
                pair_sep.as_ptr(),
                0,
            );
            if err < 0 {
                Err(av_err2str(err).into())
            } else {
                Ok(Self { ptr })
            }
        }
    }
}

impl AVDictionaryOwned {
    pub fn as_ptr(&self) -> *const AVDictionary {
        self.ptr as *const AVDictionary
    }

    pub fn as_mut_ptr(&mut self) -> *mut AVDictionary {
        self.ptr
    }

    pub fn as_mut_ptr_ref(&mut self) -> &mut *mut AVDictionary {
        &mut self.ptr
    }
}

/// Format context I/O mode.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd)]
pub enum AVFormatContextMode {
    Input,
    Output,
}

/// Format I/O context.
#[derive(Debug)]
pub struct AVFormatContextOwned {
    ptr: *mut AVFormatContext,
    mode: AVFormatContextMode,
}

impl Drop for AVFormatContextOwned {
    fn drop(&mut self) {
        match self.mode {
            AVFormatContextMode::Input => unsafe {
                avformat_close_input(&mut self.ptr);
            },

            AVFormatContextMode::Output => unsafe {
                avio_close((*self.ptr).pb);
                avformat_free_context(self.ptr);
            },
        }
    }
}

impl Deref for AVFormatContextOwned {
    type Target = AVFormatContext;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.ptr }
    }
}

impl DerefMut for AVFormatContextOwned {
    fn deref_mut(&mut self) -> &mut AVFormatContext {
        unsafe { &mut *self.ptr }
    }
}

impl AVFormatContextOwned {
    /// Wrap an exists AVFormatContext ptr.
    pub fn from_ptr(ptr: *mut AVFormatContext, mode: AVFormatContextMode) -> Self {
        Self { ptr, mode }
    }

    /// Create a new AVFormatContext for input.
    pub fn with_input<P>(path: P, format_options: Option<&str>) -> AVResult<Self>
    where
        P: AsRef<Path>,
    {
        unsafe {
            let path = CString::new(path.as_ref().as_os_str().to_str().unwrap()).unwrap();
            let mut options = AVDictionaryOwned::from_str(format_options.unwrap_or("")).unwrap();
            let mut ps = std::ptr::null_mut();
            let err = avformat_open_input(
                &mut ps,
                path.as_ptr(),
                std::ptr::null_mut(),
                options.as_mut_ptr_ref(),
            );
            if err < 0 {
                return Err(av_err2str(err).into());
            }
            let err = avformat_find_stream_info(ps, std::ptr::null_mut());
            if err < 0 {
                return Err(av_err2str(err).into());
            }
            Ok(Self {
                ptr: ps,
                mode: AVFormatContextMode::Input,
            })
        }
    }

    /// Create a new AVFormatContext for output.
    pub fn with_output<P>(
        path: P,
        format: Option<&str>,
        oformat: Option<&AVOutputFormat>,
    ) -> AVResult<Self>
    where
        P: AsRef<Path>,
    {
        unsafe {
            let mut ps = std::ptr::null_mut();
            let path = CString::new(path.as_ref().as_os_str().to_str().unwrap()).unwrap();
            let mut format_ptr = std::ptr::null();
            let cformat = CString::new(format.unwrap_or(""))?;
            if format.is_some() {
                format_ptr = cformat.as_ptr();
            }
            let err = avformat_alloc_output_context2(
                &mut ps,
                oformat.map_or(std::ptr::null_mut(), |x| {
                    x as *const AVOutputFormat as *mut AVOutputFormat
                }),
                format_ptr,
                path.as_ptr(),
            );
            if err < 0 {
                return Err(av_err2str(err).into());
            }
            let ofmt = AVOutputFormatOwned::from_ptr((*ps).oformat);
            if (ofmt.flags & AVFMT_NOFILE) != AVFMT_NOFILE {
                let err = avio_open(&mut (*ps).pb, path.as_ptr(), AVIO_FLAG_WRITE);
                if err < 0 {
                    avformat_free_context(ps);
                    return Err(av_err2str(err).into());
                }
            }
            Ok(Self {
                ptr: ps,
                mode: AVFormatContextMode::Output,
            })
        }
    }

    /// Add a new stream to a media file.
    pub fn new_stream(&mut self, codec_id: AVCodecID) -> AVResult<AVStreamOwned> {
        unsafe {
            // The codec is optional
            let codec = avcodec_find_encoder(codec_id);
            let stream = avformat_new_stream(self.ptr, codec);
            if stream.is_null() {
                Err(format!("Failed to create new stream for {:?}", codec_id).into())
            } else {
                let stream = AVStreamOwned::from_ptr(stream);
                Ok(stream)
            }
        }
    }

    /// Return the next frame of a stream.
    pub fn read_frame(&mut self) -> Option<AVPacketOwned> {
        let mut pkt = AVPacketOwned::new();
        let err = unsafe { av_read_frame(self.ptr, &mut *pkt) };
        if err < 0 {
            None
        } else {
            Some(pkt)
        }
    }

    /// Allocate the stream private data and write the stream header to an output media file.
    pub fn write_header(&mut self, options: Option<&str>) -> AVResult<()> {
        unsafe {
            let mut opt = AVDictionaryOwned::from_str(options.unwrap_or("")).unwrap();
            let err = avformat_write_header(self.ptr, opt.as_mut_ptr_ref());
            if err < 0 {
                Err(av_err2str(err).into())
            } else {
                Ok(())
            }
        }
    }

    /// Write the stream trailer to an output media file and free the file private data.
    pub fn write_trailer(&mut self) -> AVResult<()> {
        unsafe {
            av_write_trailer(self.ptr);
        }
        Ok(())
    }

    /// Write a packet to an output media file ensuring correct interleaving.
    pub fn write_frame_interleaved(&mut self, packet: &mut AVPacket) -> AVResult<()> {
        unsafe {
            let err = av_interleaved_write_frame(self.ptr, packet);
            if err < 0 {
                Err(av_err2str(err).into())
            } else {
                Ok(())
            }
        }
    }

    /// Flush all buffered data to stream destionation.
    pub fn flush(&mut self) {
        if let AVFormatContextMode::Output = self.mode {
            if let Some(pb) = self.pb_mut() {
                unsafe {
                    avio_flush(pb);
                }
            }
        }
    }

    /// Returns the size of the stream processed.
    pub fn size(&self) -> u64 {
        if let Some(pb) = self.pb_mut() {
            unsafe { avio_size(pb).try_into().unwrap() }
        } else {
            0
        }
    }
}

#[repr(transparent)]
pub struct AVPacketBoxed {
    ptr: *mut AVPacket,
}

impl Debug for AVPacketBoxed {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        unsafe {
            if f.alternate() {
                write!(f, "{:#?}", *self.ptr)
            } else {
                write!(f, "{:?}", *self.ptr)
            }
        }
    }
}

impl Drop for AVPacketBoxed {
    fn drop(&mut self) {
        println!("Drop for AVPacketBoxed({:p})", self.ptr);
        unsafe {
            av_packet_free(&mut self.ptr);
        }
    }
}

impl Deref for AVPacketBoxed {
    type Target = AVPacket;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.ptr }
    }
}

impl DerefMut for AVPacketBoxed {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.ptr }
    }
}

impl AVPacketBoxed {
    pub fn from_ptr(ptr: *mut AVPacket) -> Self {
        Self { ptr }
    }

    pub fn as_ptr(&self) -> *const AVPacket {
        self.ptr as *const AVPacket
    }

    pub fn as_mut_ptr(&mut self) -> *mut AVPacket {
        self.ptr
    }

    pub fn as_mut_ptr_ref(&mut self) -> &mut *mut AVPacket {
        &mut self.ptr
    }
}

#[repr(transparent)]
#[derive(Default)]
pub struct AVPacketOwned {
    inner: AVPacket,
}

impl Debug for AVPacketOwned {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            write!(f, "{:#?}", self.inner)
        } else {
            write!(f, "{:?}", self.inner)
        }
    }
}

impl Drop for AVPacketOwned {
    fn drop(&mut self) {
        unsafe {
            av_packet_unref(&mut self.inner);
        }
    }
}

impl Deref for AVPacketOwned {
    type Target = AVPacket;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for AVPacketOwned {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl AVPacketOwned {
    pub fn new() -> Self {
        Self {
            inner: Default::default(),
        }
    }

    pub fn as_ptr(&self) -> *const AVPacket {
        &self.inner as *const AVPacket
    }

    pub fn as_mut_ptr(&mut self) -> *mut AVPacket {
        &mut self.inner
    }
}

#[derive(Debug)]
pub struct AVOutputFormatOwned {
    ptr: *mut AVOutputFormat,
}

impl Deref for AVOutputFormatOwned {
    type Target = AVOutputFormat;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.ptr }
    }
}

impl DerefMut for AVOutputFormatOwned {
    fn deref_mut(&mut self) -> &mut AVOutputFormat {
        unsafe { &mut *self.ptr }
    }
}

impl AVOutputFormatOwned {
    /// Wrap an exists AVOutputFormat ptr.
    pub fn from_ptr(ptr: *mut AVOutputFormat) -> Self {
        Self { ptr }
    }
}

#[derive(Debug)]
pub struct AVStreamOwned {
    ptr: *mut AVStream,
}

impl Deref for AVStreamOwned {
    type Target = AVStream;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.ptr }
    }
}

impl DerefMut for AVStreamOwned {
    fn deref_mut(&mut self) -> &mut AVStream {
        unsafe { &mut *self.ptr }
    }
}

impl AVStreamOwned {
    /// Wrap an exists AVStream ptr.
    pub fn from_ptr(ptr: *mut AVStream) -> Self {
        Self { ptr }
    }
}

/// Representation of a managed C string.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AVBoxedCStr<'a> {
    inner: &'a CStr,
}

impl<'a> Debug for AVBoxedCStr<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.inner)
    }
}

impl<'a> Drop for AVBoxedCStr<'a> {
    fn drop(&mut self) {
        unsafe {
            let mut ptr = self.inner.as_ptr() as *mut core::ffi::c_void;
            av_freep(std::mem::transmute::<
                &mut *mut core::ffi::c_void,
                *mut core::ffi::c_void,
            >(&mut ptr));
        }
    }
}

impl<'a> Deref for AVBoxedCStr<'a> {
    type Target = CStr;

    fn deref(&self) -> &Self::Target {
        self.inner
    }
}

impl<'a> AVBoxedCStr<'a> {
    /// Wraps a raw C string with a safe C string wrapper.
    ///
    /// The ownership of the ptr is transfered to the AVBoxedCStr.
    pub unsafe fn from_ptr(ptr: *const c_char) -> Self {
        Self {
            inner: CStr::from_ptr(ptr),
        }
    }
}