ffmpeg_rs/codec/decoder/
video.rs

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