playa_ffmpeg/codec/subtitle/
rect_mut.rs

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