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