ffmpeg-the-third 6.0.0+ffmpeg-9.0

Safe FFmpeg wrapper (FFmpeg 5+ compatible fork of the ffmpeg 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
use std::marker::PhantomData;
use std::ptr::NonNull;

use super::config::{FrameRateIter, PixelFormatIter, SampleFormatIter, SampleRateIter};
use super::descriptor::{CodecDescriptor, CodecDescriptorIter};
use super::profile::ProfileIter;
use super::{Capabilities, Id};
use crate::ffi::*;
use crate::iters::TerminatedPtrIter;
use crate::ChannelLayout;
use crate::{media, utils};

#[cfg(feature = "ffmpeg_7_1")]
use crate::codec::config::{ColorRangeIter, ColorSpaceIter, Supported};

pub fn list_descriptors() -> CodecDescriptorIter {
    CodecDescriptorIter::new()
}

pub type Audio = Codec<AudioType>;
pub type Video = Codec<VideoType>;
pub type Data = Codec<DataType>;
pub type Subtitle = Codec<SubtitleType>;
pub type Attachment = Codec<AttachmentType>;

#[derive(PartialEq, Eq, Copy, Clone)]
pub struct Codec<Type = UnknownType> {
    ptr: NonNull<AVCodec>,
    _marker: PhantomData<Type>,
}

#[derive(PartialEq, Eq, Copy, Clone)]
pub struct UnknownType;
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct VideoType;
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct AudioType;
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct DataType;
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct SubtitleType;
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct AttachmentType;

unsafe impl<T> Send for Codec<T> {}
unsafe impl<T> Sync for Codec<T> {}

impl Codec<UnknownType> {
    /// Create a new reference to a codec from a raw pointer.
    ///
    /// Returns `None` if `ptr` is null.
    pub unsafe fn from_raw(ptr: *const AVCodec) -> Option<Self> {
        NonNull::new(ptr as *mut _).map(|ptr| Self {
            ptr,
            _marker: PhantomData,
        })
    }

    // Helper function to easily convert to another codec type.
    // TODO: Does this need to be unsafe?
    /// Ensure that `self.medium()` is correct for `Codec<U>`.
    fn as_other_codec<U>(self) -> Codec<U> {
        Codec {
            ptr: self.ptr,
            _marker: PhantomData,
        }
    }

    pub fn is_video(&self) -> bool {
        self.medium() == media::Type::Video
    }

    pub fn video(self) -> Option<Video> {
        if self.is_video() {
            Some(self.as_other_codec())
        } else {
            None
        }
    }

    pub fn is_audio(&self) -> bool {
        self.medium() == media::Type::Audio
    }

    pub fn audio(self) -> Option<Audio> {
        if self.is_audio() {
            Some(self.as_other_codec())
        } else {
            None
        }
    }

    pub fn is_data(&self) -> bool {
        self.medium() == media::Type::Data
    }

    pub fn data(self) -> Option<Data> {
        if self.is_data() {
            Some(self.as_other_codec())
        } else {
            None
        }
    }

    pub fn is_subtitle(&self) -> bool {
        self.medium() == media::Type::Subtitle
    }

    pub fn subtitle(self) -> Option<Subtitle> {
        if self.is_subtitle() {
            Some(self.as_other_codec())
        } else {
            None
        }
    }

    pub fn is_attachment(&self) -> bool {
        self.medium() == media::Type::Attachment
    }

    pub fn attachment(self) -> Option<Attachment> {
        if self.is_attachment() {
            Some(self.as_other_codec())
        } else {
            None
        }
    }
}

impl<T> Codec<T> {
    pub fn as_ptr(&self) -> *const AVCodec {
        self.ptr.as_ptr()
    }

    pub fn is_encoder(&self) -> bool {
        unsafe { av_codec_is_encoder(self.as_ptr()) != 0 }
    }

    pub fn is_decoder(&self) -> bool {
        unsafe { av_codec_is_decoder(self.as_ptr()) != 0 }
    }

    pub fn name(&self) -> &str {
        unsafe { utils::str_from_c_ptr((*self.as_ptr()).name) }
    }

    pub fn description(&self) -> &str {
        unsafe { utils::optional_str_from_c_ptr((*self.as_ptr()).long_name).unwrap_or("") }
    }

    pub fn medium(&self) -> media::Type {
        unsafe { media::Type::from((*self.as_ptr()).type_) }
    }

    pub fn id(&self) -> Id {
        unsafe { Id::from((*self.as_ptr()).id) }
    }

    pub fn max_lowres(&self) -> i32 {
        unsafe { (*self.as_ptr()).max_lowres.into() }
    }

    pub fn capabilities(&self) -> Capabilities {
        unsafe { Capabilities::from_bits_truncate((*self.as_ptr()).capabilities as u32) }
    }

    pub fn profiles(&self) -> Option<ProfileIter> {
        unsafe {
            if (*self.as_ptr()).profiles.is_null() {
                None
            } else {
                Some(ProfileIter::new(self.id(), (*self.as_ptr()).profiles))
            }
        }
    }

    pub fn descriptor(self) -> Option<CodecDescriptor> {
        unsafe {
            let ptr = avcodec_descriptor_get(self.id().into());
            CodecDescriptor::from_raw(ptr)
        }
    }
}

impl Codec<AudioType> {
    /// Checks if the given sample rate is supported by this audio codec.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supports_rate(self, rate: libc::c_int) -> bool {
        self.supported_rates().supports(rate)
    }

    /// Returns a [`Supported`] representing the supported sample rates.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supported_rates(self) -> Supported<SampleRateIter<'static>> {
        use super::config::supported_sample_rates;
        supported_sample_rates(self, None).expect("audio codec returns supported sample rates")
    }

    #[cfg(not(feature = "ffmpeg_9_0"))]
    pub fn rates(&self) -> Option<SampleRateIter<'_>> {
        unsafe { SampleRateIter::from_raw((*self.as_ptr()).supported_samplerates) }
    }

    /// Checks if the given sample format is supported by this audio codec.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supports_format(self, format: crate::format::Sample) -> bool {
        self.supported_formats().supports(format)
    }

    /// Returns a [`Supported`] representing the supported sample formats.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supported_formats(self) -> Supported<SampleFormatIter<'static>> {
        use super::config::supported_sample_formats;
        supported_sample_formats(self, None).expect("audio codec returns supported sample formats")
    }

    /// Returns a [`Supported`] representing the supported channel layouts.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supported_layouts(self) -> Supported<ChannelLayoutIter<'static>> {
        use super::config::supported_channel_layouts;
        supported_channel_layouts(self, None)
            .expect("audio codec returns supported channel layouts")
    }

    #[cfg(not(feature = "ffmpeg_9_0"))]
    pub fn formats(&self) -> Option<SampleFormatIter<'_>> {
        unsafe { SampleFormatIter::from_raw((*self.as_ptr()).sample_fmts) }
    }

    #[cfg(not(feature = "ffmpeg_7_0"))]
    pub fn channel_layouts(&self) -> Option<ChannelLayoutMaskIter> {
        unsafe { ChannelLayoutMaskIter::from_raw((*self.as_ptr()).channel_layouts) }
    }

    #[cfg(not(feature = "ffmpeg_9_0"))]
    pub fn ch_layouts(&self) -> Option<ChannelLayoutIter<'_>> {
        unsafe { ChannelLayoutIter::from_raw((*self.as_ptr()).ch_layouts) }
    }
}

impl Codec<VideoType> {
    /// Checks if the given frame rate is supported by this video codec.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supports_rate(self, rate: crate::Rational) -> bool {
        self.supported_rates().supports(rate)
    }

    /// Returns a [`Supported`] representing the supported frame rates.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supported_rates(self) -> Supported<FrameRateIter<'static>> {
        use crate::codec::config::supported_frame_rates;
        supported_frame_rates(self, None).expect("video codec returns supported frame rates")
    }

    #[cfg(not(feature = "ffmpeg_9_0"))]
    pub fn rates(&self) -> Option<FrameRateIter<'_>> {
        unsafe { FrameRateIter::from_raw((*self.as_ptr()).supported_framerates) }
    }

    /// Checks if the given pixel format is supported by this video codec.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supports_format(self, format: crate::format::Pixel) -> bool {
        self.supported_formats().supports(format)
    }

    /// Returns a [`Supported`] representing the supported pixel formats.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supported_formats(self) -> Supported<PixelFormatIter<'static>> {
        use crate::codec::config::supported_pixel_formats;
        supported_pixel_formats(self, None).expect("video codec returns supported pixel formats")
    }

    #[cfg(not(feature = "ffmpeg_9_0"))]
    pub fn formats(&self) -> Option<PixelFormatIter<'_>> {
        unsafe { PixelFormatIter::from_raw((*self.as_ptr()).pix_fmts) }
    }

    /// Checks if the given color space is supported by this video codec.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supports_color_space(self, space: crate::color::Space) -> bool {
        self.supported_color_spaces().supports(space)
    }

    /// Returns a [`Supported`] representing the supported color spaces.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supported_color_spaces(self) -> Supported<ColorSpaceIter<'static>> {
        use crate::codec::config::supported_color_spaces;
        supported_color_spaces(self, None).expect("video codec returns supported color spaces")
    }

    /// Checks if the given color range is supported by this video codec.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supports_color_range(self, range: crate::color::Range) -> bool {
        self.supported_color_ranges().supports(range)
    }

    /// Returns a [`Supported`] representing the supported color ranges.
    #[cfg(feature = "ffmpeg_7_1")]
    pub fn supported_color_ranges(self) -> Supported<ColorRangeIter<'static>> {
        use crate::codec::config::supported_color_ranges;
        supported_color_ranges(self, None).expect("video codec returns supported color ranges")
    }
}

#[cfg(not(feature = "ffmpeg_7_0"))]
use crate::ChannelLayoutMask;

#[cfg(not(feature = "ffmpeg_7_0"))]
pub struct ChannelLayoutMaskIter {
    ptr: NonNull<u64>,
}

#[cfg(not(feature = "ffmpeg_7_0"))]
impl ChannelLayoutMaskIter {
    pub fn from_raw(ptr: *const u64) -> Option<Self> {
        NonNull::new(ptr as *mut _).map(|ptr| Self { ptr })
    }

    pub fn best(self, max: i32) -> ChannelLayoutMask {
        self.fold(ChannelLayoutMask::MONO, |acc, cur| {
            if cur.channels() > acc.channels() && cur.channels() <= max {
                cur
            } else {
                acc
            }
        })
    }
}

#[cfg(not(feature = "ffmpeg_7_0"))]
impl Iterator for ChannelLayoutMaskIter {
    type Item = ChannelLayoutMask;

    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            let ptr = self.ptr.as_ptr();
            if *ptr == 0 {
                return None;
            }

            self.ptr = self.ptr.add(1);
            Some(ChannelLayoutMask::from_bits_truncate(*ptr))
        }
    }
}

pub struct ChannelLayoutIter<'a> {
    next: &'a AVChannelLayout,
}

impl<'a> ChannelLayoutIter<'a> {
    pub unsafe fn from_raw(ptr: *const AVChannelLayout) -> Option<Self> {
        ptr.as_ref().map(|next| Self { next })
    }
}

impl<'a> TerminatedPtrIter<AVChannelLayout, ChannelLayout<'a>> for ChannelLayoutIter<'a> {
    unsafe fn from_ptr(ptr: NonNull<AVChannelLayout>) -> Self {
        Self {
            next: unsafe { ptr.as_ref() },
        }
    }
}

impl<'a> ChannelLayoutIter<'a> {
    pub fn best(self, max: u32) -> ChannelLayout<'a> {
        self.fold(ChannelLayout::MONO, |acc, cur| {
            if cur.channels() > acc.channels() && cur.channels() <= max {
                cur
            } else {
                acc
            }
        })
    }
}

impl<'a> Iterator for ChannelLayoutIter<'a> {
    type Item = ChannelLayout<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        const ITER_END: AVChannelLayout = unsafe { std::mem::zeroed() };

        unsafe {
            let curr = self.next;
            if *curr == ITER_END {
                return None;
            }

            // SAFETY: We trust that there is always an initialized layout up until
            // the zeroed-out AVChannelLayout, which signals the end of iteration.
            self.next = (curr as *const AVChannelLayout).add(1).as_ref().unwrap();
            Some(ChannelLayout::from(curr))
        }
    }
}

#[cfg(all(test, feature = "ffmpeg_9_0"))]
mod tests {
    use super::*;

    #[test]
    fn ffmpeg_9_supported_layouts_are_available() {
        let codec = crate::codec::decoder::find(Id::MP3)
            .expect("can find MP3 decoder")
            .audio()
            .expect("MP3 decoder is audio");

        match codec.supported_layouts() {
            Supported::All => {}
            Supported::Specific(mut layouts) => assert!(layouts.next().is_some()),
        }
    }
}