ffmpeg_rs/codec/encoder/
video.rs

1use std::ops::{Deref, DerefMut};
2use std::ptr;
3
4use ffi::*;
5use libc::{c_float, c_int};
6
7use super::Encoder as Super;
8use super::{Comparison, Decision};
9#[cfg(not(feature = "ffmpeg_5_0"))]
10use super::{MotionEstimation, Prediction};
11use codec::{traits, Context};
12use {color, format, Dictionary, Error, Rational};
13#[cfg(not(feature = "ffmpeg_5_0"))]
14use {frame, packet};
15
16pub struct Video(pub Super);
17
18impl Video {
19    #[inline]
20    pub fn open(mut self) -> Result<Encoder, Error> {
21        unsafe {
22            match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
23                0 => Ok(Encoder(self)),
24                e => Err(Error::from(e)),
25            }
26        }
27    }
28
29    #[inline]
30    pub fn open_as<E: traits::Encoder>(mut self, codec: E) -> Result<Encoder, Error> {
31        unsafe {
32            if let Some(codec) = codec.encoder() {
33                match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
34                    0 => Ok(Encoder(self)),
35                    e => Err(Error::from(e)),
36                }
37            } else {
38                Err(Error::EncoderNotFound)
39            }
40        }
41    }
42
43    #[inline]
44    pub fn open_with(mut self, options: Dictionary) -> Result<Encoder, Error> {
45        unsafe {
46            let mut opts = options.disown();
47            let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts);
48
49            Dictionary::own(opts);
50
51            match res {
52                0 => Ok(Encoder(self)),
53                e => Err(Error::from(e)),
54            }
55        }
56    }
57
58    #[inline]
59    pub fn open_as_with<E: traits::Encoder>(
60        mut self,
61        codec: E,
62        options: Dictionary,
63    ) -> Result<Encoder, Error> {
64        unsafe {
65            if let Some(codec) = codec.encoder() {
66                let mut opts = options.disown();
67                let res = avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut opts);
68
69                Dictionary::own(opts);
70
71                match res {
72                    0 => Ok(Encoder(self)),
73                    e => Err(Error::from(e)),
74                }
75            } else {
76                Err(Error::EncoderNotFound)
77            }
78        }
79    }
80
81    #[inline]
82    pub fn set_width(&mut self, value: u32) {
83        unsafe {
84            (*self.as_mut_ptr()).width = value as c_int;
85        }
86    }
87
88    #[inline]
89    pub fn width(&self) -> u32 {
90        unsafe { (*self.as_ptr()).width as u32 }
91    }
92
93    #[inline]
94    pub fn set_height(&mut self, value: u32) {
95        unsafe {
96            (*self.as_mut_ptr()).height = value as c_int;
97        }
98    }
99
100    #[inline]
101    pub fn height(&self) -> u32 {
102        unsafe { (*self.as_ptr()).height as u32 }
103    }
104
105    #[inline]
106    pub fn set_gop(&mut self, value: u32) {
107        unsafe {
108            (*self.as_mut_ptr()).gop_size = value as c_int;
109        }
110    }
111
112    #[inline]
113    pub fn set_format(&mut self, value: format::Pixel) {
114        unsafe {
115            (*self.as_mut_ptr()).pix_fmt = value.into();
116        }
117    }
118
119    #[inline]
120    pub fn format(&self) -> format::Pixel {
121        unsafe { format::Pixel::from((*self.as_ptr()).pix_fmt) }
122    }
123
124    #[inline]
125    #[cfg(feature = "ff_api_motion_est")]
126    pub fn set_motion_estimation(&mut self, value: MotionEstimation) {
127        unsafe {
128            (*self.as_mut_ptr()).me_method = value.into();
129        }
130    }
131
132    #[inline]
133    pub fn set_max_b_frames(&mut self, value: usize) {
134        unsafe {
135            (*self.as_mut_ptr()).max_b_frames = value as c_int;
136        }
137    }
138
139    #[inline]
140    pub fn set_b_quant_factor(&mut self, value: f32) {
141        unsafe {
142            (*self.as_mut_ptr()).b_quant_factor = value as c_float;
143        }
144    }
145
146    #[inline]
147    pub fn set_b_quant_offset(&mut self, value: f32) {
148        unsafe {
149            (*self.as_mut_ptr()).b_quant_offset = value as c_float;
150        }
151    }
152
153    #[inline]
154    pub fn set_i_quant_factor(&mut self, value: f32) {
155        unsafe {
156            (*self.as_mut_ptr()).i_quant_factor = value as c_float;
157        }
158    }
159
160    #[inline]
161    pub fn set_i_quant_offset(&mut self, value: f32) {
162        unsafe {
163            (*self.as_mut_ptr()).i_quant_offset = value as c_float;
164        }
165    }
166
167    #[inline]
168    pub fn set_lumi_masking(&mut self, value: f32) {
169        unsafe {
170            (*self.as_mut_ptr()).lumi_masking = value as c_float;
171        }
172    }
173
174    #[inline]
175    pub fn set_temporal_cplx_masking(&mut self, value: f32) {
176        unsafe {
177            (*self.as_mut_ptr()).temporal_cplx_masking = value as c_float;
178        }
179    }
180
181    #[inline]
182    pub fn set_spatial_cplx_masking(&mut self, value: f32) {
183        unsafe {
184            (*self.as_mut_ptr()).spatial_cplx_masking = value as c_float;
185        }
186    }
187
188    #[inline]
189    pub fn set_p_masking(&mut self, value: f32) {
190        unsafe {
191            (*self.as_mut_ptr()).p_masking = value as c_float;
192        }
193    }
194
195    #[inline]
196    pub fn set_dark_masking(&mut self, value: f32) {
197        unsafe {
198            (*self.as_mut_ptr()).dark_masking = value as c_float;
199        }
200    }
201
202    #[inline]
203    #[cfg(not(feature = "ffmpeg_5_0"))]
204    pub fn set_prediction(&mut self, value: Prediction) {
205        unsafe {
206            (*self.as_mut_ptr()).prediction_method = value.into();
207        }
208    }
209
210    #[inline]
211    pub fn set_aspect_ratio<R: Into<Rational>>(&mut self, value: R) {
212        unsafe {
213            (*self.as_mut_ptr()).sample_aspect_ratio = value.into().into();
214        }
215    }
216
217    #[inline]
218    pub fn set_me_comparison(&mut self, value: Comparison) {
219        unsafe {
220            (*self.as_mut_ptr()).me_cmp = value.into();
221        }
222    }
223
224    #[inline]
225    pub fn set_me_sub_comparison(&mut self, value: Comparison) {
226        unsafe {
227            (*self.as_mut_ptr()).me_sub_cmp = value.into();
228        }
229    }
230
231    #[inline]
232    pub fn set_mb_comparison(&mut self, value: Comparison) {
233        unsafe {
234            (*self.as_mut_ptr()).mb_cmp = value.into();
235        }
236    }
237
238    #[inline]
239    pub fn set_ildct_comparison(&mut self, value: Comparison) {
240        unsafe {
241            (*self.as_mut_ptr()).ildct_cmp = value.into();
242        }
243    }
244
245    #[inline]
246    pub fn set_dia_size(&mut self, value: usize) {
247        unsafe {
248            (*self.as_mut_ptr()).dia_size = value as c_int;
249        }
250    }
251
252    #[inline]
253    pub fn set_last_predictors(&mut self, value: usize) {
254        unsafe {
255            (*self.as_mut_ptr()).last_predictor_count = value as c_int;
256        }
257    }
258
259    #[inline]
260    #[cfg(not(feature = "ffmpeg_5_0"))]
261    pub fn set_pre_me(&mut self, value: MotionEstimation) {
262        unsafe {
263            (*self.as_mut_ptr()).pre_me = value.into();
264        }
265    }
266
267    #[inline]
268    pub fn set_me_pre_comparison(&mut self, value: Comparison) {
269        unsafe {
270            (*self.as_mut_ptr()).me_pre_cmp = value.into();
271        }
272    }
273
274    #[inline]
275    pub fn set_pre_dia_size(&mut self, value: usize) {
276        unsafe {
277            (*self.as_mut_ptr()).pre_dia_size = value as c_int;
278        }
279    }
280
281    #[inline]
282    pub fn set_me_subpel_quality(&mut self, value: usize) {
283        unsafe {
284            (*self.as_mut_ptr()).me_subpel_quality = value as c_int;
285        }
286    }
287
288    #[inline]
289    pub fn set_me_range(&mut self, value: usize) {
290        unsafe {
291            (*self.as_mut_ptr()).me_range = value as c_int;
292        }
293    }
294
295    #[inline]
296    #[cfg(feature = "ff_api_quant_bias")]
297    pub fn set_intra_quant_bias(&mut self, value: Option<usize>) {
298        unsafe {
299            if let Some(value) = value {
300                (*self.as_mut_ptr()).intra_quant_bias = value as c_int;
301            } else {
302                (*self.as_mut_ptr()).intra_quant_bias = FF_DEFAULT_QUANT_BIAS;
303            }
304        }
305    }
306
307    #[inline]
308    #[cfg(feature = "ff_api_quant_bias")]
309    pub fn set_inter_quant_bias(&mut self, value: Option<usize>) {
310        unsafe {
311            if let Some(value) = value {
312                (*self.as_mut_ptr()).inter_quant_bias = value as c_int;
313            } else {
314                (*self.as_mut_ptr()).inter_quant_bias = FF_DEFAULT_QUANT_BIAS;
315            }
316        }
317    }
318
319    #[inline]
320    pub fn set_mb_decision(&mut self, value: Decision) {
321        unsafe {
322            (*self.as_mut_ptr()).mb_decision = value.into();
323        }
324    }
325
326    #[inline]
327    pub fn set_mb_lmin(&mut self, value: i32) {
328        unsafe {
329            (*self.as_mut_ptr()).mb_lmin = value as c_int;
330        }
331    }
332
333    #[inline]
334    pub fn set_mb_lmax(&mut self, value: i32) {
335        unsafe {
336            (*self.as_mut_ptr()).mb_lmax = value as c_int;
337        }
338    }
339
340    #[inline]
341    pub fn set_intra_dc_precision(&mut self, value: u8) {
342        unsafe {
343            (*self.as_mut_ptr()).intra_dc_precision = i32::from(value);
344        }
345    }
346
347    #[inline]
348    pub fn set_qmin(&mut self, value: i32) {
349        unsafe {
350            (*self.as_mut_ptr()).qmin = value as c_int;
351        }
352    }
353
354    #[inline]
355    pub fn set_qmax(&mut self, value: i32) {
356        unsafe {
357            (*self.as_mut_ptr()).qmax = value as c_int;
358        }
359    }
360
361    #[inline]
362    pub fn set_global_quality(&mut self, value: i32) {
363        unsafe {
364            (*self.as_mut_ptr()).global_quality = value as c_int;
365        }
366    }
367
368    // Simple function to get the extradata bytes from the encoder
369    // should it be needed in the future
370    #[inline]
371    pub fn get_extradata(&mut self) -> &[u8] {
372        let extradata_size = unsafe { (*self.as_ptr()).extradata_size } as i32 as usize;
373        unsafe { std::slice::from_raw_parts((*self.as_ptr()).extradata, extradata_size) }
374    }
375
376    #[inline]
377    pub fn set_colorspace(&mut self, value: color::Space) {
378        unsafe {
379            (*self.as_mut_ptr()).colorspace = value.into();
380        }
381    }
382
383    #[inline]
384    pub fn colorspace(&self) -> color::Space {
385        unsafe { (*self.as_ptr()).colorspace.into() }
386    }
387
388    #[inline]
389    pub fn set_color_range(&mut self, value: color::Range) {
390        unsafe {
391            (*self.as_mut_ptr()).color_range = value.into();
392        }
393    }
394
395    #[inline]
396    pub fn color_range(&self) -> color::Range {
397        unsafe { (*self.as_ptr()).color_range.into() }
398    }
399}
400
401impl Deref for Video {
402    type Target = Super;
403
404    #[inline(always)]
405    fn deref(&self) -> &<Self as Deref>::Target {
406        &self.0
407    }
408}
409
410impl DerefMut for Video {
411    #[inline(always)]
412    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
413        &mut self.0
414    }
415}
416
417impl AsRef<Context> for Video {
418    fn as_ref(&self) -> &Context {
419        self
420    }
421}
422
423impl AsMut<Context> for Video {
424    fn as_mut(&mut self) -> &mut Context {
425        &mut self.0
426    }
427}
428
429pub struct Encoder(pub Video);
430
431impl Encoder {
432    #[deprecated(
433        since = "4.4.0",
434        note = "Underlying API avcodec_encode_video2 has been deprecated since FFmpeg 3.1; \
435        consider switching to send_frame() and receive_packet()"
436    )]
437    #[inline]
438    #[cfg(not(feature = "ffmpeg_5_0"))]
439    pub fn encode<P: packet::Mut>(
440        &mut self,
441        frame: &frame::Video,
442        out: &mut P,
443    ) -> Result<bool, Error> {
444        unsafe {
445            if self.format() != frame.format()
446                || self.width() != frame.width()
447                || self.height() != frame.height()
448            {
449                return Err(Error::InvalidData);
450            }
451
452            let mut got: c_int = 0;
453
454            match avcodec_encode_video2(
455                self.0.as_mut_ptr(),
456                out.as_mut_ptr(),
457                frame.as_ptr(),
458                &mut got,
459            ) {
460                e if e < 0 => Err(Error::from(e)),
461                _ => Ok(got != 0),
462            }
463        }
464    }
465
466    #[deprecated(
467        since = "4.4.0",
468        note = "Underlying API avcodec_encode_video2 has been deprecated since FFmpeg 3.1; \
469        consider switching to send_frame() and receive_packet()"
470    )]
471    #[inline]
472    #[cfg(not(feature = "ffmpeg_5_0"))]
473    pub fn flush<P: packet::Mut>(&mut self, out: &mut P) -> Result<bool, Error> {
474        unsafe {
475            let mut got: c_int = 0;
476
477            match avcodec_encode_video2(
478                self.0.as_mut_ptr(),
479                out.as_mut_ptr(),
480                ptr::null(),
481                &mut got,
482            ) {
483                e if e < 0 => Err(Error::from(e)),
484                _ => Ok(got != 0),
485            }
486        }
487    }
488
489    #[inline]
490    pub fn frame_size(&self) -> u32 {
491        unsafe { (*self.as_ptr()).frame_size as u32 }
492    }
493}
494
495impl Deref for Encoder {
496    type Target = Video;
497
498    #[inline]
499    fn deref(&self) -> &<Self as Deref>::Target {
500        &self.0
501    }
502}
503
504impl DerefMut for Encoder {
505    #[inline]
506    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
507        &mut self.0
508    }
509}
510
511impl AsRef<Context> for Encoder {
512    fn as_ref(&self) -> &Context {
513        self
514    }
515}
516
517impl AsMut<Context> for Encoder {
518    fn as_mut(&mut self) -> &mut Context {
519        &mut self.0
520    }
521}