Skip to main content

ffmpeg_next/codec/
video.rs

1use std::ops::Deref;
2
3use super::codec::Codec;
4use crate::ffi::*;
5use crate::{Rational, format};
6
7#[derive(PartialEq, Eq, Copy, Clone)]
8pub struct Video {
9    codec: Codec,
10}
11
12impl Video {
13    pub unsafe fn new(codec: Codec) -> Video {
14        Video { codec }
15    }
16}
17
18impl Video {
19    pub fn rates(&self) -> Option<RateIter> {
20        unsafe {
21            #[cfg(feature = "ffmpeg_9_0")]
22            let ptr = super::supported_config::<AVRational>(
23                self.codec.as_ptr(),
24                AVCodecConfig::AV_CODEC_CONFIG_FRAME_RATE,
25            );
26            #[cfg(not(feature = "ffmpeg_9_0"))]
27            let ptr = (*self.codec.as_ptr()).supported_framerates;
28
29            if ptr.is_null() {
30                None
31            } else {
32                Some(RateIter::new(ptr))
33            }
34        }
35    }
36
37    pub fn formats(&self) -> Option<FormatIter> {
38        unsafe {
39            #[cfg(feature = "ffmpeg_9_0")]
40            let ptr = super::supported_config::<AVPixelFormat>(
41                self.codec.as_ptr(),
42                AVCodecConfig::AV_CODEC_CONFIG_PIX_FORMAT,
43            );
44            #[cfg(not(feature = "ffmpeg_9_0"))]
45            let ptr = (*self.codec.as_ptr()).pix_fmts;
46
47            if ptr.is_null() {
48                None
49            } else {
50                Some(FormatIter::new(ptr))
51            }
52        }
53    }
54}
55
56impl Deref for Video {
57    type Target = Codec;
58
59    fn deref(&self) -> &Self::Target {
60        &self.codec
61    }
62}
63
64pub struct RateIter {
65    ptr: *const AVRational,
66}
67
68impl RateIter {
69    pub fn new(ptr: *const AVRational) -> Self {
70        RateIter { ptr }
71    }
72}
73
74impl Iterator for RateIter {
75    type Item = Rational;
76
77    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
78        unsafe {
79            if (*self.ptr).num == 0 && (*self.ptr).den == 0 {
80                return None;
81            }
82
83            let rate = (*self.ptr).into();
84            self.ptr = self.ptr.offset(1);
85
86            Some(rate)
87        }
88    }
89}
90
91pub struct FormatIter {
92    ptr: *const AVPixelFormat,
93}
94
95impl FormatIter {
96    pub fn new(ptr: *const AVPixelFormat) -> Self {
97        FormatIter { ptr }
98    }
99}
100
101impl Iterator for FormatIter {
102    type Item = format::Pixel;
103
104    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
105        unsafe {
106            if *self.ptr == AVPixelFormat::AV_PIX_FMT_NONE {
107                return None;
108            }
109
110            let format = (*self.ptr).into();
111            self.ptr = self.ptr.offset(1);
112
113            Some(format)
114        }
115    }
116}