ffmpeg_next/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::{Context, traits};
9use crate::{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_with(mut self, options: Dictionary) -> Result<Encoder, Error> {
37 unsafe {
38 let mut opts = options.disown();
39 let res = avcodec_open2(self.as_mut_ptr(), ptr::null(), &mut opts);
40
41 Dictionary::own(opts);
42
43 match res {
44 0 => Ok(Encoder(self)),
45 e => Err(Error::from(e)),
46 }
47 }
48 }
49
50 pub fn open_as_with<E: traits::Encoder>(
51 mut self,
52 codec: E,
53 options: Dictionary,
54 ) -> Result<Encoder, Error> {
55 unsafe {
56 if let Some(codec) = codec.encoder() {
57 let mut opts = options.disown();
58 let res = avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut opts);
59
60 Dictionary::own(opts);
61
62 match res {
63 0 => Ok(Encoder(self)),
64 e => Err(Error::from(e)),
65 }
66 } else {
67 Err(Error::EncoderNotFound)
68 }
69 }
70 }
71}
72
73impl Deref for Subtitle {
74 type Target = Super;
75
76 fn deref(&self) -> &<Self as Deref>::Target {
77 &self.0
78 }
79}
80
81impl DerefMut for Subtitle {
82 fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
83 &mut self.0
84 }
85}
86
87impl AsRef<Context> for Subtitle {
88 fn as_ref(&self) -> &Context {
89 self
90 }
91}
92
93impl AsMut<Context> for Subtitle {
94 fn as_mut(&mut self) -> &mut Context {
95 &mut self.0
96 }
97}
98
99pub struct Encoder(pub Subtitle);
100
101impl Encoder {
102 pub fn encode(&mut self, subtitle: &crate::Subtitle, out: &mut [u8]) -> Result<bool, Error> {
103 unsafe {
104 match avcodec_encode_subtitle(
105 self.0.as_mut_ptr(),
106 out.as_mut_ptr(),
107 out.len() as c_int,
108 subtitle.as_ptr(),
109 ) {
110 e if e < 0 => Err(Error::from(e)),
111 _ => Ok(true),
112 }
113 }
114 }
115}
116
117impl Deref for Encoder {
118 type Target = Subtitle;
119
120 fn deref(&self) -> &<Self as Deref>::Target {
121 &self.0
122 }
123}
124
125impl DerefMut for Encoder {
126 fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
127 &mut self.0
128 }
129}
130
131impl AsRef<Context> for Encoder {
132 fn as_ref(&self) -> &Context {
133 self
134 }
135}
136
137impl AsMut<Context> for Encoder {
138 fn as_mut(&mut self) -> &mut Context {
139 &mut self.0
140 }
141}