playa_ffmpeg/codec/decoder/
video.rs1use std::ops::{Deref, DerefMut};
2
3#[cfg(not(feature = "ffmpeg_5_0"))]
4use crate::ffi::*;
5use libc::c_int;
6
7use super::{Opened, slice};
8#[cfg(not(feature = "ffmpeg_5_0"))]
9use crate::frame;
10use crate::{
11 FieldOrder, Rational,
12 codec::Context,
13 color,
14 util::{chroma, format},
15};
16#[cfg(not(feature = "ffmpeg_5_0"))]
17use {crate::Error, crate::packet};
18
19pub struct Video(pub Opened);
20
21impl Video {
22 #[deprecated(
23 since = "4.4.0",
24 note = "Underlying API avcodec_decode_video2 has been deprecated since FFmpeg 3.1; \
25 consider switching to send_packet() and receive_frame()"
26 )]
27 #[cfg(not(feature = "ffmpeg_5_0"))]
28 pub fn decode<P: packet::Ref>(&mut self, packet: &P, out: &mut frame::Video) -> Result<bool, Error> {
29 unsafe {
30 let mut got: c_int = 0;
31
32 match avcodec_decode_video2(self.as_mut_ptr(), out.as_mut_ptr(), &mut got, packet.as_ptr()) {
33 e if e < 0 => Err(Error::from(e)),
34 _ => Ok(got != 0),
35 }
36 }
37 }
38
39 pub fn width(&self) -> u32 {
40 unsafe { (*self.as_ptr()).width as u32 }
41 }
42
43 pub fn height(&self) -> u32 {
44 unsafe { (*self.as_ptr()).height as u32 }
45 }
46
47 pub fn format(&self) -> format::Pixel {
48 unsafe { format::Pixel::from((*self.as_ptr()).pix_fmt) }
49 }
50
51 pub fn has_b_frames(&self) -> bool {
52 unsafe { (*self.as_ptr()).has_b_frames != 0 }
53 }
54
55 pub fn aspect_ratio(&self) -> Rational {
56 unsafe { Rational::from((*self.as_ptr()).sample_aspect_ratio) }
57 }
58
59 pub fn color_space(&self) -> color::Space {
60 unsafe { color::Space::from((*self.as_ptr()).colorspace) }
61 }
62
63 pub fn color_range(&self) -> color::Range {
64 unsafe { color::Range::from((*self.as_ptr()).color_range) }
65 }
66
67 pub fn color_primaries(&self) -> color::Primaries {
68 unsafe { color::Primaries::from((*self.as_ptr()).color_primaries) }
69 }
70
71 pub fn color_transfer_characteristic(&self) -> color::TransferCharacteristic {
72 unsafe { color::TransferCharacteristic::from((*self.as_ptr()).color_trc) }
73 }
74
75 pub fn chroma_location(&self) -> chroma::Location {
76 unsafe { chroma::Location::from((*self.as_ptr()).chroma_sample_location) }
77 }
78
79 #[cfg(not(feature = "ffmpeg_7_0"))]
80 pub fn set_slice_count(&mut self, value: usize) {
81 unsafe {
82 (*self.as_mut_ptr()).slice_count = value as c_int;
83 }
84 }
85
86 pub fn set_slice_flags(&mut self, value: slice::Flags) {
87 unsafe {
88 (*self.as_mut_ptr()).slice_flags = value.bits();
89 }
90 }
91
92 pub fn skip_top(&mut self, value: usize) {
93 unsafe {
94 (*self.as_mut_ptr()).skip_top = value as c_int;
95 }
96 }
97
98 pub fn skip_bottom(&mut self, value: usize) {
99 unsafe {
100 (*self.as_mut_ptr()).skip_bottom = value as c_int;
101 }
102 }
103
104 pub fn references(&self) -> usize {
105 unsafe { (*self.as_ptr()).refs as usize }
106 }
107
108 pub fn set_field_order(&mut self, value: FieldOrder) {
109 unsafe {
110 (*self.as_mut_ptr()).field_order = value.into();
111 }
112 }
113
114 pub fn intra_dc_precision(&self) -> u8 {
118 unsafe { (*self.as_ptr()).intra_dc_precision as u8 }
119 }
120
121 pub fn max_bit_rate(&self) -> usize {
122 unsafe { (*self.as_ptr()).rc_max_rate as usize }
123 }
124}
125
126impl Deref for Video {
127 type Target = Opened;
128
129 fn deref(&self) -> &<Self as Deref>::Target {
130 &self.0
131 }
132}
133
134impl DerefMut for Video {
135 fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
136 &mut self.0
137 }
138}
139
140impl AsRef<Context> for Video {
141 fn as_ref(&self) -> &Context {
142 self
143 }
144}
145
146impl AsMut<Context> for Video {
147 fn as_mut(&mut self) -> &mut Context {
148 &mut self.0
149 }
150}