Skip to main content

ffmpeg_next/codec/subtitle/
rect_mut.rs

1use std::ffi::CString;
2use std::ops::Deref;
3
4use super::{Ass, Bitmap, Flags, Text, Type};
5use crate::ffi::*;
6use libc::c_int;
7
8pub enum RectMut<'a> {
9    None(*mut AVSubtitleRect),
10    Bitmap(BitmapMut<'a>),
11    Text(TextMut<'a>),
12    Ass(AssMut<'a>),
13}
14
15impl<'a> RectMut<'a> {
16    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
17        unsafe {
18            match Type::from((*ptr).type_) {
19                Type::None => RectMut::None(ptr),
20                Type::Bitmap => RectMut::Bitmap(BitmapMut::wrap(ptr)),
21                Type::Text => RectMut::Text(TextMut::wrap(ptr)),
22                Type::Ass => RectMut::Ass(AssMut::wrap(ptr)),
23            }
24        }
25    }
26
27    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
28        unsafe {
29            match *self {
30                RectMut::None(ptr) => ptr as *const _,
31                RectMut::Bitmap(ref b) => b.as_ptr(),
32                RectMut::Text(ref t) => t.as_ptr(),
33                RectMut::Ass(ref a) => a.as_ptr(),
34            }
35        }
36    }
37
38    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
39        unsafe {
40            match *self {
41                RectMut::None(ptr) => ptr,
42                RectMut::Bitmap(ref mut b) => b.as_mut_ptr(),
43                RectMut::Text(ref mut t) => t.as_mut_ptr(),
44                RectMut::Ass(ref mut a) => a.as_mut_ptr(),
45            }
46        }
47    }
48}
49
50impl<'a> RectMut<'a> {
51    pub fn flags(&self) -> Flags {
52        unsafe {
53            Flags::from_bits_truncate(match *self {
54                RectMut::None(ptr) => (*ptr).flags,
55                RectMut::Bitmap(ref b) => (*b.as_ptr()).flags,
56                RectMut::Text(ref t) => (*t.as_ptr()).flags,
57                RectMut::Ass(ref a) => (*a.as_ptr()).flags,
58            })
59        }
60    }
61}
62
63pub struct BitmapMut<'a> {
64    immutable: Bitmap<'a>,
65}
66
67impl<'a> BitmapMut<'a> {
68    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
69        unsafe {
70            BitmapMut {
71                immutable: Bitmap::wrap(ptr as *const _),
72            }
73        }
74    }
75
76    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
77        unsafe { self.as_ptr() as *mut _ }
78    }
79}
80
81impl<'a> BitmapMut<'a> {
82    pub fn set_x(&mut self, value: usize) {
83        unsafe {
84            (*self.as_mut_ptr()).x = value as c_int;
85        }
86    }
87
88    pub fn set_y(&mut self, value: usize) {
89        unsafe {
90            (*self.as_mut_ptr()).y = value as c_int;
91        }
92    }
93
94    pub fn set_width(&mut self, value: u32) {
95        unsafe {
96            (*self.as_mut_ptr()).w = value as c_int;
97        }
98    }
99
100    pub fn set_height(&mut self, value: u32) {
101        unsafe {
102            (*self.as_mut_ptr()).h = value as c_int;
103        }
104    }
105
106    pub fn set_colors(&mut self, value: usize) {
107        unsafe {
108            (*self.as_mut_ptr()).nb_colors = value as c_int;
109        }
110    }
111}
112
113impl<'a> Deref for BitmapMut<'a> {
114    type Target = Bitmap<'a>;
115
116    fn deref(&self) -> &Self::Target {
117        &self.immutable
118    }
119}
120
121pub struct TextMut<'a> {
122    immutable: Text<'a>,
123}
124
125impl<'a> TextMut<'a> {
126    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
127        unsafe {
128            TextMut {
129                immutable: Text::wrap(ptr as *const _),
130            }
131        }
132    }
133
134    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
135        unsafe { self.as_ptr() as *mut _ }
136    }
137}
138
139impl<'a> TextMut<'a> {
140    pub fn set(&mut self, value: &str) {
141        let value = CString::new(value).unwrap();
142
143        unsafe {
144            (*self.as_mut_ptr()).text = av_strdup(value.as_ptr());
145        }
146    }
147}
148
149impl<'a> Deref for TextMut<'a> {
150    type Target = Text<'a>;
151
152    fn deref(&self) -> &Self::Target {
153        &self.immutable
154    }
155}
156
157pub struct AssMut<'a> {
158    immutable: Ass<'a>,
159}
160
161impl<'a> AssMut<'a> {
162    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
163        unsafe {
164            AssMut {
165                immutable: Ass::wrap(ptr),
166            }
167        }
168    }
169
170    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
171        unsafe { self.as_ptr() as *mut _ }
172    }
173}
174
175impl<'a> AssMut<'a> {
176    pub fn set(&mut self, value: &str) {
177        let value = CString::new(value).unwrap();
178
179        unsafe {
180            (*self.as_mut_ptr()).ass = av_strdup(value.as_ptr());
181        }
182    }
183}
184
185impl<'a> Deref for AssMut<'a> {
186    type Target = Ass<'a>;
187
188    fn deref(&self) -> &Self::Target {
189        &self.immutable
190    }
191}