ffmpeg_rs/codec/
parameters.rs

1use std::any::Any;
2use std::rc::Rc;
3
4use super::Context;
5use ffi::*;
6use libc::c_int;
7use media;
8use {codec, format};
9
10pub struct Parameters {
11    ptr: *mut AVCodecParameters,
12    owner: Option<Rc<dyn Any>>,
13}
14
15unsafe impl Send for Parameters {}
16
17impl Parameters {
18    pub unsafe fn wrap(ptr: *mut AVCodecParameters, owner: Option<Rc<dyn Any>>) -> Self {
19        Parameters { ptr, owner }
20    }
21
22    pub unsafe fn as_ptr(&self) -> *const AVCodecParameters {
23        self.ptr as *const _
24    }
25
26    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVCodecParameters {
27        self.ptr
28    }
29}
30
31impl Parameters {
32    pub fn new() -> Self {
33        unsafe {
34            Parameters {
35                ptr: avcodec_parameters_alloc(),
36                owner: None,
37            }
38        }
39    }
40
41    #[deprecated(since = "5.1.1", note = "Use codec_type instead")]
42    pub fn medium(&self) -> media::Type {
43        unsafe { media::Type::from((*self.as_ptr()).codec_type) }
44    }
45
46    #[deprecated(since = "5.1.1", note = "Use codec_id instead")]
47    pub fn id(&self) -> codec::Id {
48        unsafe { codec::Id::from((*self.as_ptr()).codec_id) }
49    }
50
51    #[inline]
52    pub fn width(&self) -> u32 {
53        unsafe { (*self.as_ptr()).width as u32 }
54    }
55
56    #[inline]
57    pub fn set_width(&mut self, value: u32) {
58        unsafe {
59            (*self.as_mut_ptr()).width = value as c_int;
60        }
61    }
62
63    #[inline]
64    pub fn height(&self) -> u32 {
65        unsafe { (*self.as_ptr()).height as u32 }
66    }
67
68    #[inline]
69    pub fn set_height(&mut self, value: u32) {
70        unsafe {
71            (*self.as_mut_ptr()).height = value as c_int;
72        }
73    }
74
75    #[inline]
76    pub fn format(&self) -> codec::AVPixelFormat {
77        unsafe { std::mem::transmute::<_, AVPixelFormat>((*self.as_ptr()).format) }
78    }
79
80    #[inline]
81    pub fn set_format(&mut self, format: format::Pixel) {
82        let format: codec::AVPixelFormat = format.into();
83        unsafe {
84            (*self.as_mut_ptr()).format = format as c_int;
85        }
86    }
87
88    #[inline]
89    pub fn codec_type(&self) -> media::Type {
90        unsafe { (*self.as_ptr()).codec_type.into() }
91    }
92
93    #[inline]
94    pub fn set_codec_type(&mut self, codec_type: media::Type) {
95        unsafe {
96            (*self.as_mut_ptr()).codec_type = codec_type.into();
97        }
98    }
99
100    #[inline]
101    pub fn codec_id(&self) -> codec::Id {
102        unsafe { (*self.as_ptr()).codec_id.into() }
103    }
104
105    #[inline]
106    pub fn set_codec_id(&mut self, codec_id: codec::Id) {
107        unsafe {
108            (*self.as_mut_ptr()).codec_id = codec_id.into();
109        }
110    }
111
112    #[inline]
113    pub fn extradata(&mut self) -> &[u8] {
114        let extradata_size = unsafe { (*self.as_ptr()).extradata_size } as i32 as usize;
115        unsafe { std::slice::from_raw_parts((*self.as_ptr()).extradata, extradata_size) }
116    }
117
118    #[inline]
119    pub fn set_extradata(&mut self, mut extradata: Vec<u8>) {
120        unsafe {
121            (*self.as_mut_ptr()).extradata_size = extradata.len() as libc::c_int;
122        }
123        extradata.extend(vec![0; AV_INPUT_BUFFER_PADDING_SIZE as usize]);
124        let mut slice = extradata.into_boxed_slice();
125        let ptr = slice.as_mut_ptr();
126
127        // Leave the memory untracked so it can be freed when the parameters are
128        // dropped through the avcodec_parameters_free call.
129        std::mem::forget(slice);
130        unsafe {
131            (*self.as_mut_ptr()).extradata = ptr;
132        }
133    }
134}
135
136impl Default for Parameters {
137    fn default() -> Self {
138        Self::new()
139    }
140}
141
142impl Drop for Parameters {
143    fn drop(&mut self) {
144        unsafe {
145            if self.owner.is_none() {
146                avcodec_parameters_free(&mut self.as_mut_ptr());
147            }
148        }
149    }
150}
151
152impl Clone for Parameters {
153    fn clone(&self) -> Self {
154        let mut ctx = Parameters::new();
155        ctx.clone_from(self);
156
157        ctx
158    }
159
160    fn clone_from(&mut self, source: &Self) {
161        unsafe {
162            avcodec_parameters_copy(self.as_mut_ptr(), source.as_ptr());
163        }
164    }
165}
166
167impl<C: AsRef<Context>> From<C> for Parameters {
168    fn from(context: C) -> Parameters {
169        let mut parameters = Parameters::new();
170        let context = context.as_ref();
171        unsafe {
172            avcodec_parameters_from_context(parameters.as_mut_ptr(), context.as_ptr());
173        }
174        parameters
175    }
176}