Skip to main content

ffmpeg_the_third/codec/decoder/
decoder.rs

1use std::ops::{Deref, DerefMut};
2use std::ptr;
3
4use super::{Audio, Check, Conceal, Opened, Subtitle, Video};
5use crate::codec::{traits, Context};
6use crate::ffi::*;
7use crate::{AsMutPtr, 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<T, Dec>(mut self, codec: Dec) -> Result<Opened, Error>
22    where
23        Dec: traits::Decoder<T>,
24    {
25        unsafe {
26            if let Some(codec) = codec.decoder() {
27                match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
28                    0 => Ok(Opened(self)),
29                    e => Err(Error::from(e)),
30                }
31            } else {
32                Err(Error::DecoderNotFound)
33            }
34        }
35    }
36
37    pub fn open_as_with<T, Dec, Dict>(
38        mut self,
39        codec: Dec,
40        mut options: Dict,
41    ) -> Result<Opened, Error>
42    where
43        Dec: traits::Decoder<T>,
44        Dict: AsMutPtr<*mut AVDictionary>,
45    {
46        unsafe {
47            if let Some(codec) = codec.decoder() {
48                let res = avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), options.as_mut_ptr());
49
50                match res {
51                    0 => Ok(Opened(self)),
52                    e => Err(Error::from(e)),
53                }
54            } else {
55                Err(Error::DecoderNotFound)
56            }
57        }
58    }
59
60    pub fn video(self) -> Result<Video, Error> {
61        if let Some(codec) = super::find(self.id()) {
62            self.open_as(codec).and_then(|o| o.video())
63        } else {
64            Err(Error::DecoderNotFound)
65        }
66    }
67
68    pub fn audio(self) -> Result<Audio, Error> {
69        if let Some(codec) = super::find(self.id()) {
70            self.open_as(codec).and_then(|o| o.audio())
71        } else {
72            Err(Error::DecoderNotFound)
73        }
74    }
75
76    pub fn subtitle(self) -> Result<Subtitle, Error> {
77        if let Some(codec) = super::find(self.id()) {
78            self.open_as(codec).and_then(|o| o.subtitle())
79        } else {
80            Err(Error::DecoderNotFound)
81        }
82    }
83
84    pub fn conceal(&mut self, value: Conceal) {
85        unsafe {
86            (*self.as_mut_ptr()).error_concealment = value.bits();
87        }
88    }
89
90    pub fn check(&mut self, value: Check) {
91        unsafe {
92            (*self.as_mut_ptr()).err_recognition = value.bits();
93        }
94    }
95
96    pub fn skip_loop_filter(&mut self, value: Discard) {
97        unsafe {
98            (*self.as_mut_ptr()).skip_loop_filter = value.into();
99        }
100    }
101
102    pub fn skip_idct(&mut self, value: Discard) {
103        unsafe {
104            (*self.as_mut_ptr()).skip_idct = value.into();
105        }
106    }
107
108    pub fn skip_frame(&mut self, value: Discard) {
109        unsafe {
110            (*self.as_mut_ptr()).skip_frame = value.into();
111        }
112    }
113
114    pub fn time_base(&self) -> Rational {
115        unsafe { Rational::from((*self.as_ptr()).time_base) }
116    }
117}
118
119impl Deref for Decoder {
120    type Target = Context;
121
122    fn deref(&self) -> &<Self as Deref>::Target {
123        &self.0
124    }
125}
126
127impl DerefMut for Decoder {
128    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
129        &mut self.0
130    }
131}
132
133impl AsRef<Context> for Decoder {
134    fn as_ref(&self) -> &Context {
135        self
136    }
137}
138
139impl AsMut<Context> for Decoder {
140    fn as_mut(&mut self) -> &mut Context {
141        &mut self.0
142    }
143}