ffmpeg_next_crossfix/codec/subtitle/
rect.rs

1use std::ffi::CStr;
2use std::marker::PhantomData;
3use std::str::from_utf8_unchecked;
4
5use super::{Flags, Type};
6use ffi::*;
7use {format, Picture};
8
9pub enum Rect<'a> {
10    None(*const AVSubtitleRect),
11    Bitmap(Bitmap<'a>),
12    Text(Text<'a>),
13    Ass(Ass<'a>),
14}
15
16impl<'a> Rect<'a> {
17    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
18        match Type::from((*ptr).type_) {
19            Type::None => Rect::None(ptr),
20            Type::Bitmap => Rect::Bitmap(Bitmap::wrap(ptr)),
21            Type::Text => Rect::Text(Text::wrap(ptr)),
22            Type::Ass => Rect::Ass(Ass::wrap(ptr)),
23        }
24    }
25
26    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
27        match *self {
28            Rect::None(ptr) => ptr,
29            Rect::Bitmap(ref b) => b.as_ptr(),
30            Rect::Text(ref t) => t.as_ptr(),
31            Rect::Ass(ref a) => a.as_ptr(),
32        }
33    }
34}
35
36impl<'a> Rect<'a> {
37    pub fn flags(&self) -> Flags {
38        unsafe {
39            Flags::from_bits_truncate(match *self {
40                Rect::None(ptr) => (*ptr).flags,
41                Rect::Bitmap(ref b) => (*b.as_ptr()).flags,
42                Rect::Text(ref t) => (*t.as_ptr()).flags,
43                Rect::Ass(ref a) => (*a.as_ptr()).flags,
44            })
45        }
46    }
47}
48
49pub struct Bitmap<'a> {
50    ptr: *const AVSubtitleRect,
51
52    _marker: PhantomData<&'a ()>,
53}
54
55impl<'a> Bitmap<'a> {
56    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
57        Bitmap {
58            ptr,
59            _marker: PhantomData,
60        }
61    }
62
63    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
64        self.ptr
65    }
66}
67
68impl<'a> Bitmap<'a> {
69    pub fn x(&self) -> usize {
70        unsafe { (*self.as_ptr()).x as usize }
71    }
72
73    pub fn y(&self) -> usize {
74        unsafe { (*self.as_ptr()).y as usize }
75    }
76
77    pub fn width(&self) -> u32 {
78        unsafe { (*self.as_ptr()).w as u32 }
79    }
80
81    pub fn height(&self) -> u32 {
82        unsafe { (*self.as_ptr()).h as u32 }
83    }
84
85    pub fn colors(&self) -> usize {
86        unsafe { (*self.as_ptr()).nb_colors as usize }
87    }
88
89    // XXX: must split Picture and PictureMut
90    pub fn picture(&self, format: format::Pixel) -> Picture<'a> {
91        unsafe {
92            Picture::wrap(
93                &(*self.as_ptr()).pict as *const _ as *mut _,
94                format,
95                (*self.as_ptr()).w as u32,
96                (*self.as_ptr()).h as u32,
97            )
98        }
99    }
100}
101
102pub struct Text<'a> {
103    ptr: *const AVSubtitleRect,
104
105    _marker: PhantomData<&'a ()>,
106}
107
108impl<'a> Text<'a> {
109    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
110        Text {
111            ptr,
112            _marker: PhantomData,
113        }
114    }
115
116    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
117        self.ptr
118    }
119}
120
121impl<'a> Text<'a> {
122    pub fn get(&self) -> &str {
123        unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).text).to_bytes()) }
124    }
125}
126
127pub struct Ass<'a> {
128    ptr: *const AVSubtitleRect,
129
130    _marker: PhantomData<&'a ()>,
131}
132
133impl<'a> Ass<'a> {
134    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
135        Ass {
136            ptr,
137            _marker: PhantomData,
138        }
139    }
140
141    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
142        self.ptr
143    }
144}
145
146impl<'a> Ass<'a> {
147    pub fn get(&self) -> &str {
148        unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).ass).to_bytes()) }
149    }
150}