ffmpeg_the_third/codec/subtitle/
rect.rs

1use std::marker::PhantomData;
2
3use super::{Flags, Type};
4use crate::ffi::*;
5use crate::utils;
6#[cfg(not(feature = "ffmpeg_5_0"))]
7use crate::{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    #[cfg(not(feature = "ffmpeg_5_0"))]
91    pub fn picture(&self, format: format::Pixel) -> Picture<'a> {
92        unsafe {
93            Picture::wrap(
94                &(*self.as_ptr()).pict as *const _ as *mut _,
95                format,
96                (*self.as_ptr()).w as u32,
97                (*self.as_ptr()).h as u32,
98            )
99        }
100    }
101}
102
103pub struct Text<'a> {
104    ptr: *const AVSubtitleRect,
105
106    _marker: PhantomData<&'a ()>,
107}
108
109impl<'a> Text<'a> {
110    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
111        Text {
112            ptr,
113            _marker: PhantomData,
114        }
115    }
116
117    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
118        self.ptr
119    }
120}
121
122impl<'a> Text<'a> {
123    pub fn get(&self) -> &str {
124        unsafe { utils::str_from_c_ptr((*self.as_ptr()).text) }
125    }
126}
127
128pub struct Ass<'a> {
129    ptr: *const AVSubtitleRect,
130
131    _marker: PhantomData<&'a ()>,
132}
133
134impl<'a> Ass<'a> {
135    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
136        Ass {
137            ptr,
138            _marker: PhantomData,
139        }
140    }
141
142    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
143        self.ptr
144    }
145}
146
147impl<'a> Ass<'a> {
148    pub fn get(&self) -> &str {
149        unsafe { utils::str_from_c_ptr((*self.as_ptr()).ass) }
150    }
151}