Skip to main content

ffmpeg_the_third/codec/encoder/
audio.rs

1use std::ops::{Deref, DerefMut};
2use std::ptr;
3
4use crate::ffi::*;
5
6use super::Encoder as Super;
7use crate::codec::{traits, Context};
8use crate::util::format;
9use crate::{AsMutPtr, ChannelLayout, Error};
10
11#[cfg(not(feature = "ffmpeg_7_0"))]
12use crate::ChannelLayoutMask;
13
14pub struct Audio(pub Super);
15
16impl Audio {
17    pub fn open(mut self) -> Result<Encoder, Error> {
18        unsafe {
19            match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
20                0 => Ok(Encoder(self)),
21                e => Err(Error::from(e)),
22            }
23        }
24    }
25
26    pub fn open_as<T, Enc>(mut self, codec: Enc) -> Result<Encoder, Error>
27    where
28        Enc: traits::Encoder<T>,
29    {
30        unsafe {
31            if let Some(codec) = codec.encoder() {
32                match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
33                    0 => Ok(Encoder(self)),
34                    e => Err(Error::from(e)),
35                }
36            } else {
37                Err(Error::EncoderNotFound)
38            }
39        }
40    }
41
42    pub fn open_with<Dict>(mut self, mut options: Dict) -> Result<Encoder, Error>
43    where
44        Dict: AsMutPtr<*mut AVDictionary>,
45    {
46        unsafe {
47            let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), options.as_mut_ptr());
48
49            match res {
50                0 => Ok(Encoder(self)),
51                e => Err(Error::from(e)),
52            }
53        }
54    }
55
56    pub fn open_as_with<T, Enc, Dict>(
57        mut self,
58        codec: Enc,
59        mut options: Dict,
60    ) -> Result<Encoder, Error>
61    where
62        Enc: traits::Encoder<T>,
63        Dict: AsMutPtr<*mut AVDictionary>,
64    {
65        unsafe {
66            if let Some(codec) = codec.encoder() {
67                let res = avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), options.as_mut_ptr());
68
69                match res {
70                    0 => Ok(Encoder(self)),
71                    e => Err(Error::from(e)),
72                }
73            } else {
74                Err(Error::EncoderNotFound)
75            }
76        }
77    }
78
79    pub fn set_rate(&mut self, rate: i32) {
80        unsafe {
81            (*self.as_mut_ptr()).sample_rate = rate;
82        }
83    }
84
85    pub fn rate(&self) -> u32 {
86        unsafe { (*self.as_ptr()).sample_rate as u32 }
87    }
88
89    pub fn set_format(&mut self, value: format::Sample) {
90        unsafe {
91            (*self.as_mut_ptr()).sample_fmt = value.into();
92        }
93    }
94
95    pub fn format(&self) -> format::Sample {
96        unsafe { format::Sample::from((*self.as_ptr()).sample_fmt) }
97    }
98
99    #[cfg(not(feature = "ffmpeg_7_0"))]
100    pub fn set_channel_layout(&mut self, value: ChannelLayoutMask) {
101        unsafe {
102            (*self.as_mut_ptr()).channel_layout = value.bits();
103        }
104    }
105
106    #[cfg(not(feature = "ffmpeg_7_0"))]
107    pub fn channel_layout(&self) -> ChannelLayoutMask {
108        unsafe { ChannelLayoutMask::from_bits_truncate((*self.as_ptr()).channel_layout) }
109    }
110
111    #[cfg(not(feature = "ffmpeg_7_0"))]
112    pub fn set_channels(&mut self, value: i32) {
113        unsafe {
114            (*self.as_mut_ptr()).channels = value;
115        }
116    }
117
118    #[cfg(not(feature = "ffmpeg_7_0"))]
119    pub fn channels(&self) -> u16 {
120        unsafe { (*self.as_ptr()).channels as u16 }
121    }
122
123    pub fn ch_layout(&self) -> ChannelLayout<'_> {
124        unsafe { ChannelLayout::from(&self.as_ptr().as_ref().unwrap().ch_layout) }
125    }
126
127    pub fn set_ch_layout(&mut self, value: ChannelLayout) {
128        unsafe {
129            self.as_mut_ptr().as_mut().unwrap().ch_layout = value.into_owned();
130        }
131    }
132}
133
134impl Deref for Audio {
135    type Target = Super;
136
137    fn deref(&self) -> &<Self as Deref>::Target {
138        &self.0
139    }
140}
141
142impl DerefMut for Audio {
143    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
144        &mut self.0
145    }
146}
147
148impl AsRef<Context> for Audio {
149    fn as_ref(&self) -> &Context {
150        self
151    }
152}
153
154impl AsMut<Context> for Audio {
155    fn as_mut(&mut self) -> &mut Context {
156        &mut self.0
157    }
158}
159
160pub struct Encoder(pub Audio);
161
162impl Encoder {
163    pub fn frame_size(&self) -> u32 {
164        unsafe { (*self.as_ptr()).frame_size as u32 }
165    }
166}
167
168impl Deref for Encoder {
169    type Target = Audio;
170
171    fn deref(&self) -> &<Self as Deref>::Target {
172        &self.0
173    }
174}
175
176impl DerefMut for Encoder {
177    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
178        &mut self.0
179    }
180}
181
182impl AsRef<Context> for Encoder {
183    fn as_ref(&self) -> &Context {
184        self
185    }
186}
187
188impl AsMut<Context> for Encoder {
189    fn as_mut(&mut self) -> &mut Context {
190        &mut self.0
191    }
192}