ffmpeg_next/codec/encoder/
subtitle.rs

1use std::ops::{Deref, DerefMut};
2use std::ptr;
3
4use ffi::*;
5use libc::c_int;
6
7use super::Encoder as Super;
8use codec::{traits, Context};
9use {Dictionary, Error};
10
11pub struct Subtitle(pub Super);
12
13impl Subtitle {
14    pub fn open(mut self) -> Result<Encoder, Error> {
15        unsafe {
16            match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
17                0 => Ok(Encoder(self)),
18                e => Err(Error::from(e)),
19            }
20        }
21    }
22
23    pub fn open_as<E: traits::Encoder>(mut self, codec: E) -> Result<Encoder, Error> {
24        unsafe {
25            if let Some(codec) = codec.encoder() {
26                match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
27                    0 => Ok(Encoder(self)),
28                    e => Err(Error::from(e)),
29                }
30            } else {
31                Err(Error::EncoderNotFound)
32            }
33        }
34    }
35
36    pub fn open_as_with<E: traits::Encoder>(
37        mut self,
38        codec: E,
39        options: Dictionary,
40    ) -> Result<Encoder, Error> {
41        unsafe {
42            if let Some(codec) = codec.encoder() {
43                let mut opts = options.disown();
44                let res = avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut opts);
45
46                Dictionary::own(opts);
47
48                match res {
49                    0 => Ok(Encoder(self)),
50                    e => Err(Error::from(e)),
51                }
52            } else {
53                Err(Error::EncoderNotFound)
54            }
55        }
56    }
57}
58
59impl Deref for Subtitle {
60    type Target = Super;
61
62    fn deref(&self) -> &<Self as Deref>::Target {
63        &self.0
64    }
65}
66
67impl DerefMut for Subtitle {
68    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
69        &mut self.0
70    }
71}
72
73impl AsRef<Context> for Subtitle {
74    fn as_ref(&self) -> &Context {
75        self
76    }
77}
78
79impl AsMut<Context> for Subtitle {
80    fn as_mut(&mut self) -> &mut Context {
81        &mut self.0
82    }
83}
84
85pub struct Encoder(pub Subtitle);
86
87impl Encoder {
88    pub fn encode(&mut self, subtitle: &::Subtitle, out: &mut [u8]) -> Result<bool, Error> {
89        unsafe {
90            match avcodec_encode_subtitle(
91                self.0.as_mut_ptr(),
92                out.as_mut_ptr(),
93                out.len() as c_int,
94                subtitle.as_ptr(),
95            ) {
96                e if e < 0 => Err(Error::from(e)),
97                _ => Ok(true),
98            }
99        }
100    }
101}
102
103impl Deref for Encoder {
104    type Target = Subtitle;
105
106    fn deref(&self) -> &<Self as Deref>::Target {
107        &self.0
108    }
109}
110
111impl DerefMut for Encoder {
112    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
113        &mut self.0
114    }
115}
116
117impl AsRef<Context> for Encoder {
118    fn as_ref(&self) -> &Context {
119        self
120    }
121}
122
123impl AsMut<Context> for Encoder {
124    fn as_mut(&mut self) -> &mut Context {
125        &mut self.0
126    }
127}