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