playa_ffmpeg/codec/encoder/
subtitle.rs

1use std::{
2    ops::{Deref, DerefMut},
3    ptr,
4};
5
6use crate::ffi::*;
7use libc::c_int;
8
9use super::Encoder as Super;
10use crate::{
11    Dictionary, Error,
12    codec::{Context, traits},
13};
14
15pub struct Subtitle(pub Super);
16
17impl Subtitle {
18    pub fn open(mut self) -> Result<Encoder, Error> {
19        unsafe {
20            match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
21                0 => Ok(Encoder(self)),
22                e => Err(Error::from(e)),
23            }
24        }
25    }
26
27    pub fn open_as<E: traits::Encoder>(mut self, codec: E) -> Result<Encoder, Error> {
28        unsafe {
29            if let Some(codec) = codec.encoder() {
30                match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
31                    0 => Ok(Encoder(self)),
32                    e => Err(Error::from(e)),
33                }
34            } else {
35                Err(Error::EncoderNotFound)
36            }
37        }
38    }
39
40    pub fn open_as_with<E: traits::Encoder>(mut self, codec: E, options: Dictionary) -> 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: &crate::Subtitle, out: &mut [u8]) -> Result<bool, Error> {
89        unsafe {
90            match avcodec_encode_subtitle(self.0.as_mut_ptr(), out.as_mut_ptr(), out.len() as c_int, subtitle.as_ptr()) {
91                e if e < 0 => Err(Error::from(e)),
92                _ => Ok(true),
93            }
94        }
95    }
96}
97
98impl Deref for Encoder {
99    type Target = Subtitle;
100
101    fn deref(&self) -> &<Self as Deref>::Target {
102        &self.0
103    }
104}
105
106impl DerefMut for Encoder {
107    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
108        &mut self.0
109    }
110}
111
112impl AsRef<Context> for Encoder {
113    fn as_ref(&self) -> &Context {
114        self
115    }
116}
117
118impl AsMut<Context> for Encoder {
119    fn as_mut(&mut self) -> &mut Context {
120        &mut self.0
121    }
122}