ffmpeg_rs/codec/decoder/
opened.rs

1use std::ops::{Deref, DerefMut};
2use std::ptr;
3
4use super::{Audio, Decoder, Subtitle, Video};
5use codec::{Context, Profile};
6use ffi::*;
7use {media, packet, Error, Frame, Rational};
8
9pub struct Opened(pub Decoder);
10
11impl Opened {
12    pub fn video(self) -> Result<Video, Error> {
13        if self.medium() == media::Type::Video {
14            Ok(Video(self))
15        } else {
16            Err(Error::InvalidData)
17        }
18    }
19
20    pub fn audio(self) -> Result<Audio, Error> {
21        if self.medium() == media::Type::Audio {
22            Ok(Audio(self))
23        } else {
24            Err(Error::InvalidData)
25        }
26    }
27
28    pub fn subtitle(self) -> Result<Subtitle, Error> {
29        if self.medium() == media::Type::Subtitle {
30            Ok(Subtitle(self))
31        } else {
32            Err(Error::InvalidData)
33        }
34    }
35
36    pub fn send_packet<P: packet::Ref>(&mut self, packet: &P) -> Result<(), Error> {
37        unsafe {
38            match avcodec_send_packet(self.as_mut_ptr(), packet.as_ptr()) {
39                e if e < 0 => Err(Error::from(e)),
40                _ => Ok(()),
41            }
42        }
43    }
44
45    /// Sends a NULL packet to the decoder to signal end of stream and enter
46    /// draining mode.
47    pub fn send_eof(&mut self) -> Result<(), Error> {
48        unsafe {
49            match avcodec_send_packet(self.as_mut_ptr(), ptr::null()) {
50                e if e < 0 => Err(Error::from(e)),
51                _ => Ok(()),
52            }
53        }
54    }
55
56    pub fn receive_frame(&mut self, frame: &mut Frame) -> Result<(), Error> {
57        unsafe {
58            match avcodec_receive_frame(self.as_mut_ptr(), frame.as_mut_ptr()) {
59                e if e < 0 => Err(Error::from(e)),
60                _ => Ok(()),
61            }
62        }
63    }
64
65    pub fn gop_size(&self) -> usize {
66        unsafe { (*self.as_ptr()).gop_size as usize }
67    }
68
69    pub fn bit_rate(&self) -> usize {
70        unsafe { (*self.as_ptr()).bit_rate as usize }
71    }
72
73    pub fn delay(&self) -> usize {
74        unsafe { (*self.as_ptr()).delay as usize }
75    }
76
77    pub fn profile(&self) -> Profile {
78        unsafe { Profile::from((self.id(), (*self.as_ptr()).profile)) }
79    }
80
81    pub fn frame_rate(&self) -> Option<Rational> {
82        unsafe {
83            let value = (*self.as_ptr()).framerate;
84
85            if value == (AVRational { num: 0, den: 1 }) {
86                None
87            } else {
88                Some(Rational::from(value))
89            }
90        }
91    }
92
93    pub fn flush(&mut self) {
94        unsafe {
95            avcodec_flush_buffers(self.as_mut_ptr());
96        }
97    }
98}
99
100impl Drop for Opened {
101    fn drop(&mut self) {
102        unsafe {
103            avcodec_close(self.as_mut_ptr());
104        }
105    }
106}
107
108impl Deref for Opened {
109    type Target = Decoder;
110
111    fn deref(&self) -> &<Self as Deref>::Target {
112        &self.0
113    }
114}
115
116impl DerefMut for Opened {
117    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
118        &mut self.0
119    }
120}
121
122impl AsRef<Context> for Opened {
123    fn as_ref(&self) -> &Context {
124        self
125    }
126}
127
128impl AsMut<Context> for Opened {
129    fn as_mut(&mut self) -> &mut Context {
130        &mut self.0
131    }
132}