1 From a7e89f88df1bff23df6314d592d8e375dfae4048 Mon Sep 17 00:00:00 2001
2 From: Kostya Shishkov <kostya.shishkov@gmail.com>
3 Date: Wed, 27 Nov 2019 08:22:47 +0100
4 Subject: [PATCH 4/4] video: add YUV overlay support
7 src/sdl/video.rs | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
8 1 file changed, 116 insertions(+), 2 deletions(-)
10 diff --git a/src/sdl/video.rs b/src/sdl/video.rs
11 index 710a1d6..9acfcb6 100644
12 --- a/src/sdl/video.rs
13 +++ b/src/sdl/video.rs
14 @@ -51,6 +51,26 @@ pub mod ll {
18 + pub const SDL_YV12_OVERLAY: uint32_t = 0x32315659;
19 + pub const SDL_IYUV_OVERLAY: uint32_t = 0x56555949;
20 + pub const SDL_YUY2_OVERLAY: uint32_t = 0x32595559;
21 + pub const SDL_UYVY_OVERLAY: uint32_t = 0x59565955;
22 + pub const SDL_YVYU_OVERLAY: uint32_t = 0x55595659;
25 + #[derive(Copy, Clone)]
26 + pub struct SDL_Overlay {
27 + pub format: uint32_t,
31 + pub pitches: *const uint16_t,
32 + pub pixels: *const *mut uint8_t,
33 + pub hwfuncs: *mut c_void,
34 + pub hwdata: *mut c_void,
35 + pub flags: uint32_t,
39 #[derive(Copy, Clone)]
40 pub struct SDL_Color {
41 @@ -109,7 +129,7 @@ pub mod ll {
44 Amask: uint32_t) -> *mut SDL_Surface;
45 - pub fn SDL_CreateRGBSurfaceFrom(pixels: *mut c_void,
46 + pub fn SDL_CreateRGBSurfaceFrom(pixels: *const c_void,
50 @@ -181,6 +201,14 @@ pub mod ll {
51 pub fn SDL_LoadBMP_RW(src: *mut SDL_RWops, freesrc: c_int) -> *mut SDL_Surface;
52 pub fn SDL_SaveBMP_RW(surface: *mut SDL_Surface, dst: *mut SDL_RWops, freedst: c_int) -> c_int;
53 pub fn SDL_GL_SwapBuffers();
55 + pub fn SDL_CreateYUVOverlay(width: c_int, height: c_int, format: uint32_t, display: *mut SDL_Surface)
56 + -> *mut SDL_Overlay;
57 + pub fn SDL_LockYUVOverlay(overlay: *mut SDL_Overlay) -> c_int;
58 + pub fn SDL_UnlockYUVOverlay(overlay: *mut SDL_Overlay);
59 + pub fn SDL_DisplayYUVOverlay(overlay: *mut SDL_Overlay,
60 + dstrect: *mut SDL_Rect) -> c_int;
61 + pub fn SDL_FreeYUVOverlay(overlay: *mut SDL_Overlay);
65 @@ -351,6 +379,7 @@ pub enum SurfaceFlag {
66 SWSurface = 0x00000000,
67 HWSurface = 0x00000001,
68 AsyncBlit = 0x00000004,
69 + HWAccel = 0x00000100,
70 SrcColorKey = 0x00001000,
71 SrcAlpha = 0x00010000,
73 @@ -466,6 +495,15 @@ pub fn get_video_surface() -> Result<Surface, String> {
74 else { Ok(wrap_surface(raw, false)) }
77 +#[derive(PartialEq, Eq, Copy, Clone)]
78 +pub enum OverlayFormat {
86 // TODO: get_video_modes, get_video_driver_name
89 @@ -485,6 +523,38 @@ impl Surface {
93 + pub fn new_from(pixels: &[u8], pitch: i32, width: isize, height: isize, bpp: isize,
94 + rmask: u32, gmask: u32, bmask: u32, amask: u32) -> Result<Surface, String> {
96 + let raw = ll::SDL_CreateRGBSurfaceFrom(pixels.as_ptr() as *const libc::c_void, width as c_int, height as c_int, bpp as c_int, pitch as c_int,
97 + rmask, gmask, bmask, amask);
102 + Ok(Surface { raw: raw, owned: true })
107 + pub fn create_overlay(&self, width: isize, height: isize, format: OverlayFormat) -> Result<Overlay, String> {
109 + let yuv_fmt = match format {
110 + OverlayFormat::YV12 => ll::SDL_YV12_OVERLAY,
111 + OverlayFormat::IYUV => ll::SDL_IYUV_OVERLAY,
112 + OverlayFormat::YUY2 => ll::SDL_YUY2_OVERLAY,
113 + OverlayFormat::UYVY => ll::SDL_UYVY_OVERLAY,
114 + OverlayFormat::YVYU => ll::SDL_YVYU_OVERLAY,
116 + let raw = ll::SDL_CreateYUVOverlay(width as c_int, height as c_int, yuv_fmt, self.raw);
120 + Ok(Overlay { raw: raw })
125 pub fn from_bmp(path: &Path) -> Result<Surface, String> {
126 let cpath = CString::new(path.to_str().unwrap()).unwrap();
127 let mode = CString::new("rb".as_bytes()).unwrap();
128 @@ -742,4 +812,48 @@ pub fn swap_buffers() {
133 +#[derive(PartialEq)]
134 +pub struct Overlay {
135 + pub raw: *mut ll::SDL_Overlay,
138 +impl Drop for Overlay {
139 + fn drop(&mut self) {
141 + ll::SDL_FreeYUVOverlay(self.raw);
147 + pub fn display(&self, dest_rect: Option<Rect>) -> bool {
149 + ll::SDL_DisplayYUVOverlay(self.raw, match dest_rect {
150 + Some(ref rect) => mem::transmute(rect),
151 + None => ptr::null_mut()
156 + pub fn lock(&self) -> bool {
157 + unsafe { ll::SDL_LockYUVOverlay(self.raw) == 0 }
160 + pub fn unlock(&self) {
161 + unsafe { ll::SDL_UnlockYUVOverlay(self.raw) }
164 + pub unsafe fn get_pixel_ptr(&self, comp: usize) -> &mut [u8] {
165 + let pitch = self.get_pitch(comp);
166 + let len = if comp == 0 { pitch as usize * ((*self.raw).h as usize) } else
167 + { pitch as usize * (((*self.raw).h / 2) as usize) };
168 + let ptr = *((*self.raw).pixels.add(comp));
169 + let pixels: &mut [u8] = mem::transmute((ptr, len));
174 + pub fn get_pitch(&self, comp: usize) -> usize {
175 + unsafe { *((*self.raw).pitches.add(comp)) as usize }