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