playa_ffmpeg/codec/encoder/
video.rs1use std::{
2 ops::{Deref, DerefMut},
3 ptr,
4};
5
6use crate::ffi::*;
7use libc::{c_float, c_int};
8
9use super::{Comparison, Decision, Encoder as Super};
10#[cfg(not(feature = "ffmpeg_5_0"))]
11use super::{MotionEstimation, Prediction};
12use crate::{
13 Dictionary, Error, Rational,
14 codec::{Context, traits},
15 color, format,
16};
17#[cfg(not(feature = "ffmpeg_5_0"))]
18use {crate::frame, crate::packet};
19
20pub struct Video(pub Super);
21
22impl Video {
23 #[inline]
24 pub fn open(mut self) -> Result<Encoder, Error> {
25 unsafe {
26 match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
27 0 => Ok(Encoder(self)),
28 e => Err(Error::from(e)),
29 }
30 }
31 }
32
33 #[inline]
34 pub fn open_as<E: traits::Encoder>(mut self, codec: E) -> Result<Encoder, Error> {
35 unsafe {
36 if let Some(codec) = codec.encoder() {
37 match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
38 0 => Ok(Encoder(self)),
39 e => Err(Error::from(e)),
40 }
41 } else {
42 Err(Error::EncoderNotFound)
43 }
44 }
45 }
46
47 #[inline]
48 pub fn open_with(mut self, options: Dictionary) -> Result<Encoder, Error> {
49 unsafe {
50 let mut opts = options.disown();
51 let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts);
52
53 Dictionary::own(opts);
54
55 match res {
56 0 => Ok(Encoder(self)),
57 e => Err(Error::from(e)),
58 }
59 }
60 }
61
62 #[inline]
63 pub fn open_as_with<E: traits::Encoder>(mut self, codec: E, options: Dictionary) -> 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 #[inline]
369 pub fn set_colorspace(&mut self, value: color::Space) {
370 unsafe {
371 (*self.as_mut_ptr()).colorspace = value.into();
372 }
373 }
374
375 #[inline]
376 pub fn colorspace(&self) -> color::Space {
377 unsafe { (*self.as_ptr()).colorspace.into() }
378 }
379
380 #[inline]
381 pub fn set_color_range(&mut self, value: color::Range) {
382 unsafe {
383 (*self.as_mut_ptr()).color_range = value.into();
384 }
385 }
386
387 #[inline]
388 pub fn color_range(&self) -> color::Range {
389 unsafe { (*self.as_ptr()).color_range.into() }
390 }
391}
392
393impl Deref for Video {
394 type Target = Super;
395
396 #[inline(always)]
397 fn deref(&self) -> &<Self as Deref>::Target {
398 &self.0
399 }
400}
401
402impl DerefMut for Video {
403 #[inline(always)]
404 fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
405 &mut self.0
406 }
407}
408
409impl AsRef<Context> for Video {
410 fn as_ref(&self) -> &Context {
411 self
412 }
413}
414
415impl AsMut<Context> for Video {
416 fn as_mut(&mut self) -> &mut Context {
417 &mut self.0
418 }
419}
420
421pub struct Encoder(pub Video);
422
423impl Encoder {
424 #[deprecated(
425 since = "4.4.0",
426 note = "Underlying API avcodec_encode_video2 has been deprecated since FFmpeg 3.1; \
427 consider switching to send_frame() and receive_packet()"
428 )]
429 #[inline]
430 #[cfg(not(feature = "ffmpeg_5_0"))]
431 pub fn encode<P: packet::Mut>(&mut self, frame: &frame::Video, out: &mut P) -> Result<bool, Error> {
432 unsafe {
433 if self.format() != frame.format() || self.width() != frame.width() || self.height() != frame.height() {
434 return Err(Error::InvalidData);
435 }
436
437 let mut got: c_int = 0;
438
439 match avcodec_encode_video2(self.0.as_mut_ptr(), out.as_mut_ptr(), frame.as_ptr(), &mut got) {
440 e if e < 0 => Err(Error::from(e)),
441 _ => Ok(got != 0),
442 }
443 }
444 }
445
446 #[deprecated(
447 since = "4.4.0",
448 note = "Underlying API avcodec_encode_video2 has been deprecated since FFmpeg 3.1; \
449 consider switching to send_frame() and receive_packet()"
450 )]
451 #[inline]
452 #[cfg(not(feature = "ffmpeg_5_0"))]
453 pub fn flush<P: packet::Mut>(&mut self, out: &mut P) -> Result<bool, Error> {
454 unsafe {
455 let mut got: c_int = 0;
456
457 match avcodec_encode_video2(self.0.as_mut_ptr(), out.as_mut_ptr(), ptr::null(), &mut got) {
458 e if e < 0 => Err(Error::from(e)),
459 _ => Ok(got != 0),
460 }
461 }
462 }
463
464 #[inline]
465 pub fn frame_size(&self) -> u32 {
466 unsafe { (*self.as_ptr()).frame_size as u32 }
467 }
468}
469
470impl Deref for Encoder {
471 type Target = Video;
472
473 #[inline]
474 fn deref(&self) -> &<Self as Deref>::Target {
475 &self.0
476 }
477}
478
479impl DerefMut for Encoder {
480 #[inline]
481 fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
482 &mut self.0
483 }
484}
485
486impl AsRef<Context> for Encoder {
487 fn as_ref(&self) -> &Context {
488 self
489 }
490}
491
492impl AsMut<Context> for Encoder {
493 fn as_mut(&mut self) -> &mut Context {
494 &mut self.0
495 }
496}