ffmpeg_next/codec/decoder/
decoder.rs1use std::ops::{Deref, DerefMut};
2use std::ptr;
3
4use super::{Audio, Check, Conceal, Opened, Subtitle, Video};
5use crate::codec::{Context, traits};
6use crate::ffi::*;
7use crate::{Dictionary, Discard, Error, Rational};
8
9pub struct Decoder(pub Context);
10
11impl Decoder {
12 pub fn open(mut self) -> Result<Opened, Error> {
13 unsafe {
14 match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
15 0 => Ok(Opened(self)),
16 e => Err(Error::from(e)),
17 }
18 }
19 }
20
21 pub fn open_as<D: traits::Decoder>(mut self, codec: D) -> Result<Opened, Error> {
22 unsafe {
23 if let Some(codec) = codec.decoder() {
24 match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
25 0 => Ok(Opened(self)),
26 e => Err(Error::from(e)),
27 }
28 } else {
29 Err(Error::DecoderNotFound)
30 }
31 }
32 }
33
34 pub fn open_with(mut self, options: Dictionary) -> Result<Opened, Error> {
35 unsafe {
36 let mut opts = options.disown();
37 let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts);
38
39 Dictionary::own(opts);
40
41 match res {
42 0 => Ok(Opened(self)),
43 e => Err(Error::from(e)),
44 }
45 }
46 }
47
48 pub fn open_as_with<D: traits::Decoder>(
49 mut self,
50 codec: D,
51 options: Dictionary,
52 ) -> Result<Opened, Error> {
53 unsafe {
54 if let Some(codec) = codec.decoder() {
55 let mut opts = options.disown();
56 let res = avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut opts);
57
58 Dictionary::own(opts);
59
60 match res {
61 0 => Ok(Opened(self)),
62 e => Err(Error::from(e)),
63 }
64 } else {
65 Err(Error::DecoderNotFound)
66 }
67 }
68 }
69
70 pub fn video(self) -> Result<Video, Error> {
71 if let Some(codec) = self.codec() {
72 self.open_as(codec).and_then(|o| o.video())
73 } else if let Some(codec) = super::find(self.id()) {
74 self.open_as(codec).and_then(|o| o.video())
75 } else {
76 Err(Error::DecoderNotFound)
77 }
78 }
79
80 pub fn audio(self) -> Result<Audio, Error> {
81 if let Some(codec) = self.codec() {
82 self.open_as(codec).and_then(|o| o.audio())
83 } else if let Some(codec) = super::find(self.id()) {
84 self.open_as(codec).and_then(|o| o.audio())
85 } else {
86 Err(Error::DecoderNotFound)
87 }
88 }
89
90 pub fn subtitle(self) -> Result<Subtitle, Error> {
91 if let Some(codec) = super::find(self.id()) {
92 self.open_as(codec).and_then(|o| o.subtitle())
93 } else {
94 Err(Error::DecoderNotFound)
95 }
96 }
97
98 pub fn conceal(&mut self, value: Conceal) {
99 unsafe {
100 (*self.as_mut_ptr()).error_concealment = value.bits();
101 }
102 }
103
104 pub fn check(&mut self, value: Check) {
105 unsafe {
106 (*self.as_mut_ptr()).err_recognition = value.bits();
107 }
108 }
109
110 pub fn skip_loop_filter(&mut self, value: Discard) {
111 unsafe {
112 (*self.as_mut_ptr()).skip_loop_filter = value.into();
113 }
114 }
115
116 pub fn skip_idct(&mut self, value: Discard) {
117 unsafe {
118 (*self.as_mut_ptr()).skip_idct = value.into();
119 }
120 }
121
122 pub fn skip_frame(&mut self, value: Discard) {
123 unsafe {
124 (*self.as_mut_ptr()).skip_frame = value.into();
125 }
126 }
127
128 pub fn packet_time_base(&self) -> Rational {
129 unsafe { Rational::from((*self.as_ptr()).pkt_timebase) }
130 }
131
132 pub fn set_packet_time_base<R: Into<Rational>>(&mut self, value: R) {
133 unsafe {
134 (*self.as_mut_ptr()).pkt_timebase = value.into().into();
135 }
136 }
137}
138
139impl Deref for Decoder {
140 type Target = Context;
141
142 fn deref(&self) -> &<Self as Deref>::Target {
143 &self.0
144 }
145}
146
147impl DerefMut for Decoder {
148 fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
149 &mut self.0
150 }
151}
152
153impl AsRef<Context> for Decoder {
154 fn as_ref(&self) -> &Context {
155 self
156 }
157}
158
159impl AsMut<Context> for Decoder {
160 fn as_mut(&mut self) -> &mut Context {
161 &mut self.0
162 }
163}