ffmpeg_the_third/codec/
codec.rs1use std::marker::PhantomData;
2use std::ptr::NonNull;
3
4use super::config::{FrameRateIter, PixelFormatIter, SampleFormatIter, SampleRateIter};
5use super::descriptor::{CodecDescriptor, CodecDescriptorIter};
6use super::profile::ProfileIter;
7use super::{Capabilities, Id};
8use crate::ffi::*;
9use crate::iters::TerminatedPtrIter;
10use crate::ChannelLayout;
11use crate::{media, utils};
12
13#[cfg(feature = "ffmpeg_7_1")]
14use crate::codec::config::{ColorRangeIter, ColorSpaceIter, Supported};
15
16pub fn list_descriptors() -> CodecDescriptorIter {
17 CodecDescriptorIter::new()
18}
19
20pub type Audio = Codec<AudioType>;
21pub type Video = Codec<VideoType>;
22pub type Data = Codec<DataType>;
23pub type Subtitle = Codec<SubtitleType>;
24pub type Attachment = Codec<AttachmentType>;
25
26#[derive(PartialEq, Eq, Copy, Clone)]
27pub struct Codec<Type = UnknownType> {
28 ptr: NonNull<AVCodec>,
29 _marker: PhantomData<Type>,
30}
31
32#[derive(PartialEq, Eq, Copy, Clone)]
33pub struct UnknownType;
34#[derive(PartialEq, Eq, Copy, Clone)]
35pub struct VideoType;
36#[derive(PartialEq, Eq, Copy, Clone)]
37pub struct AudioType;
38#[derive(PartialEq, Eq, Copy, Clone)]
39pub struct DataType;
40#[derive(PartialEq, Eq, Copy, Clone)]
41pub struct SubtitleType;
42#[derive(PartialEq, Eq, Copy, Clone)]
43pub struct AttachmentType;
44
45unsafe impl<T> Send for Codec<T> {}
46unsafe impl<T> Sync for Codec<T> {}
47
48impl Codec<UnknownType> {
49 pub unsafe fn from_raw(ptr: *const AVCodec) -> Option<Self> {
53 NonNull::new(ptr as *mut _).map(|ptr| Self {
54 ptr,
55 _marker: PhantomData,
56 })
57 }
58
59 fn as_other_codec<U>(self) -> Codec<U> {
63 Codec {
64 ptr: self.ptr,
65 _marker: PhantomData,
66 }
67 }
68
69 pub fn is_video(&self) -> bool {
70 self.medium() == media::Type::Video
71 }
72
73 pub fn video(self) -> Option<Video> {
74 if self.is_video() {
75 Some(self.as_other_codec())
76 } else {
77 None
78 }
79 }
80
81 pub fn is_audio(&self) -> bool {
82 self.medium() == media::Type::Audio
83 }
84
85 pub fn audio(self) -> Option<Audio> {
86 if self.is_audio() {
87 Some(self.as_other_codec())
88 } else {
89 None
90 }
91 }
92
93 pub fn is_data(&self) -> bool {
94 self.medium() == media::Type::Data
95 }
96
97 pub fn data(self) -> Option<Data> {
98 if self.is_data() {
99 Some(self.as_other_codec())
100 } else {
101 None
102 }
103 }
104
105 pub fn is_subtitle(&self) -> bool {
106 self.medium() == media::Type::Subtitle
107 }
108
109 pub fn subtitle(self) -> Option<Subtitle> {
110 if self.is_subtitle() {
111 Some(self.as_other_codec())
112 } else {
113 None
114 }
115 }
116
117 pub fn is_attachment(&self) -> bool {
118 self.medium() == media::Type::Attachment
119 }
120
121 pub fn attachment(self) -> Option<Attachment> {
122 if self.is_attachment() {
123 Some(self.as_other_codec())
124 } else {
125 None
126 }
127 }
128}
129
130impl<T> Codec<T> {
131 pub fn as_ptr(&self) -> *const AVCodec {
132 self.ptr.as_ptr()
133 }
134
135 pub fn is_encoder(&self) -> bool {
136 unsafe { av_codec_is_encoder(self.as_ptr()) != 0 }
137 }
138
139 pub fn is_decoder(&self) -> bool {
140 unsafe { av_codec_is_decoder(self.as_ptr()) != 0 }
141 }
142
143 pub fn name(&self) -> &str {
144 unsafe { utils::str_from_c_ptr((*self.as_ptr()).name) }
145 }
146
147 pub fn description(&self) -> &str {
148 unsafe { utils::optional_str_from_c_ptr((*self.as_ptr()).long_name).unwrap_or("") }
149 }
150
151 pub fn medium(&self) -> media::Type {
152 unsafe { media::Type::from((*self.as_ptr()).type_) }
153 }
154
155 pub fn id(&self) -> Id {
156 unsafe { Id::from((*self.as_ptr()).id) }
157 }
158
159 pub fn max_lowres(&self) -> i32 {
160 unsafe { (*self.as_ptr()).max_lowres.into() }
161 }
162
163 pub fn capabilities(&self) -> Capabilities {
164 unsafe { Capabilities::from_bits_truncate((*self.as_ptr()).capabilities as u32) }
165 }
166
167 pub fn profiles(&self) -> Option<ProfileIter> {
168 unsafe {
169 if (*self.as_ptr()).profiles.is_null() {
170 None
171 } else {
172 Some(ProfileIter::new(self.id(), (*self.as_ptr()).profiles))
173 }
174 }
175 }
176
177 pub fn descriptor(self) -> Option<CodecDescriptor> {
178 unsafe {
179 let ptr = avcodec_descriptor_get(self.id().into());
180 CodecDescriptor::from_raw(ptr)
181 }
182 }
183}
184
185impl Codec<AudioType> {
186 #[cfg(feature = "ffmpeg_7_1")]
188 pub fn supports_rate(self, rate: libc::c_int) -> bool {
189 self.supported_rates().supports(rate)
190 }
191
192 #[cfg(feature = "ffmpeg_7_1")]
194 pub fn supported_rates(self) -> Supported<SampleRateIter<'static>> {
195 use super::config::supported_sample_rates;
196 supported_sample_rates(self, None).expect("audio codec returns supported sample rates")
197 }
198
199 pub fn rates(&self) -> Option<SampleRateIter<'_>> {
200 unsafe { SampleRateIter::from_raw((*self.as_ptr()).supported_samplerates) }
201 }
202
203 #[cfg(feature = "ffmpeg_7_1")]
205 pub fn supports_format(self, format: crate::format::Sample) -> bool {
206 self.supported_formats().supports(format)
207 }
208
209 #[cfg(feature = "ffmpeg_7_1")]
211 pub fn supported_formats(self) -> Supported<SampleFormatIter<'static>> {
212 use super::config::supported_sample_formats;
213 supported_sample_formats(self, None).expect("audio codec returns supported sample formats")
214 }
215
216 pub fn formats(&self) -> Option<SampleFormatIter<'_>> {
217 unsafe { SampleFormatIter::from_raw((*self.as_ptr()).sample_fmts) }
218 }
219
220 #[cfg(not(feature = "ffmpeg_7_0"))]
221 pub fn channel_layouts(&self) -> Option<ChannelLayoutMaskIter> {
222 unsafe { ChannelLayoutMaskIter::from_raw((*self.as_ptr()).channel_layouts) }
223 }
224
225 pub fn ch_layouts(&self) -> Option<ChannelLayoutIter<'_>> {
226 unsafe { ChannelLayoutIter::from_raw((*self.as_ptr()).ch_layouts) }
227 }
228}
229
230impl Codec<VideoType> {
231 #[cfg(feature = "ffmpeg_7_1")]
233 pub fn supports_rate(self, rate: crate::Rational) -> bool {
234 self.supported_rates().supports(rate)
235 }
236
237 #[cfg(feature = "ffmpeg_7_1")]
239 pub fn supported_rates(self) -> Supported<FrameRateIter<'static>> {
240 use crate::codec::config::supported_frame_rates;
241 supported_frame_rates(self, None).expect("video codec returns supported frame rates")
242 }
243
244 pub fn rates(&self) -> Option<FrameRateIter<'_>> {
245 unsafe { FrameRateIter::from_raw((*self.as_ptr()).supported_framerates) }
246 }
247
248 #[cfg(feature = "ffmpeg_7_1")]
250 pub fn supports_format(self, format: crate::format::Pixel) -> bool {
251 self.supported_formats().supports(format)
252 }
253
254 #[cfg(feature = "ffmpeg_7_1")]
256 pub fn supported_formats(self) -> Supported<PixelFormatIter<'static>> {
257 use crate::codec::config::supported_pixel_formats;
258 supported_pixel_formats(self, None).expect("video codec returns supported pixel formats")
259 }
260
261 pub fn formats(&self) -> Option<PixelFormatIter<'_>> {
262 unsafe { PixelFormatIter::from_raw((*self.as_ptr()).pix_fmts) }
263 }
264
265 #[cfg(feature = "ffmpeg_7_1")]
267 pub fn supports_color_space(self, space: crate::color::Space) -> bool {
268 self.supported_color_spaces().supports(space)
269 }
270
271 #[cfg(feature = "ffmpeg_7_1")]
273 pub fn supported_color_spaces(self) -> Supported<ColorSpaceIter<'static>> {
274 use crate::codec::config::supported_color_spaces;
275 supported_color_spaces(self, None).expect("video codec returns supported color spaces")
276 }
277
278 #[cfg(feature = "ffmpeg_7_1")]
280 pub fn supports_color_range(self, range: crate::color::Range) -> bool {
281 self.supported_color_ranges().supports(range)
282 }
283
284 #[cfg(feature = "ffmpeg_7_1")]
286 pub fn supported_color_ranges(self) -> Supported<ColorRangeIter<'static>> {
287 use crate::codec::config::supported_color_ranges;
288 supported_color_ranges(self, None).expect("video codec returns supported color ranges")
289 }
290}
291
292#[cfg(not(feature = "ffmpeg_7_0"))]
293use crate::ChannelLayoutMask;
294
295#[cfg(not(feature = "ffmpeg_7_0"))]
296pub struct ChannelLayoutMaskIter {
297 ptr: NonNull<u64>,
298}
299
300#[cfg(not(feature = "ffmpeg_7_0"))]
301impl ChannelLayoutMaskIter {
302 pub fn from_raw(ptr: *const u64) -> Option<Self> {
303 NonNull::new(ptr as *mut _).map(|ptr| Self { ptr })
304 }
305
306 pub fn best(self, max: i32) -> ChannelLayoutMask {
307 self.fold(ChannelLayoutMask::MONO, |acc, cur| {
308 if cur.channels() > acc.channels() && cur.channels() <= max {
309 cur
310 } else {
311 acc
312 }
313 })
314 }
315}
316
317#[cfg(not(feature = "ffmpeg_7_0"))]
318impl Iterator for ChannelLayoutMaskIter {
319 type Item = ChannelLayoutMask;
320
321 fn next(&mut self) -> Option<<Self as Iterator>::Item> {
322 unsafe {
323 let ptr = self.ptr.as_ptr();
324 if *ptr == 0 {
325 return None;
326 }
327
328 self.ptr = self.ptr.add(1);
329 Some(ChannelLayoutMask::from_bits_truncate(*ptr))
330 }
331 }
332}
333
334pub struct ChannelLayoutIter<'a> {
335 next: &'a AVChannelLayout,
336}
337
338impl<'a> ChannelLayoutIter<'a> {
339 pub unsafe fn from_raw(ptr: *const AVChannelLayout) -> Option<Self> {
340 ptr.as_ref().map(|next| Self { next })
341 }
342}
343
344impl<'a> ChannelLayoutIter<'a> {
345 pub fn best(self, max: u32) -> ChannelLayout<'a> {
346 self.fold(ChannelLayout::MONO, |acc, cur| {
347 if cur.channels() > acc.channels() && cur.channels() <= max {
348 cur
349 } else {
350 acc
351 }
352 })
353 }
354}
355
356impl<'a> Iterator for ChannelLayoutIter<'a> {
357 type Item = ChannelLayout<'a>;
358
359 fn next(&mut self) -> Option<Self::Item> {
360 const ITER_END: AVChannelLayout = unsafe { std::mem::zeroed() };
361
362 unsafe {
363 let curr = self.next;
364 if *curr == ITER_END {
365 return None;
366 }
367
368 self.next = (curr as *const AVChannelLayout).add(1).as_ref().unwrap();
371 Some(ChannelLayout::from(curr))
372 }
373 }
374}