1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
use buffer::Buffer; use ffi::*; use std::mem; use std::slice::from_raw_parts; pub struct VideoFrame{ vf: GstVideoFrame, buffer: Buffer } pub struct VideoPlane<'a>{ vf: &'a GstVideoFrame, p: usize } pub struct VideoComponent<'a>{ vf: &'a GstVideoFrame, c: usize } macro_rules! GST_VIDEO_FRAME_COMP_WIDTH( ($video_frame:expr,$c:expr) => ( -((-$video_frame.info.width) >> ((*$video_frame.info.finfo).w_sub[$c]) as usize) ) ; ); macro_rules! GST_VIDEO_FRAME_COMP_HEIGHT( ($video_frame:expr,$c:expr) => ( -((-$video_frame.info.height) >> ((*$video_frame.info.finfo).h_sub[$c]) as usize) ); ); macro_rules! GST_VIDEO_FRAME_COMP_OFFSET( ($video_frame:expr,$c:expr) => ((($video_frame.info.offset)[(*$video_frame.info.finfo).plane[($c)] as usize]) as u64 + (*$video_frame.info.finfo).poffset[($c)] as u64) ); macro_rules! GST_VIDEO_FRAME_COMP_STRIDE( ($video_frame:expr,$c:expr) => (($video_frame.info.stride)[(*$video_frame.info.finfo).plane[($c)] as usize]) ); macro_rules! GST_VIDEO_FRAME_COMP_DATA( ($video_frame:expr,$c:expr) => { ($video_frame.data[(*$video_frame.info.finfo).plane[$c] as usize] as *mut u8).offset((*$video_frame.info.finfo).poffset[$c] as isize) } ); impl<'a> VideoPlane<'a>{ pub fn stride(&self) -> i32{ self.info().stride[self.p] } pub fn offset(&self) -> u64{ self.info().offset[self.p] as u64 } pub fn width(&self) -> i32{ unsafe{ GST_VIDEO_FRAME_COMP_WIDTH!(self.vf,self.p) } } pub fn height(&self) -> i32{ unsafe{ GST_VIDEO_FRAME_COMP_HEIGHT!(self.vf,self.p) } } pub fn size(&self) -> usize{ (self.stride()*self.height()) as usize } pub fn len<T>(&self) -> usize{ self.size()/mem::size_of::<T>() } pub fn depth(&self) -> u32{ self.format_info().depth[self.p] } pub fn data<T:'a>(&self) -> &'a[T]{ unsafe{ from_raw_parts( mem::transmute(self.vf.data[self.p]), self.len::<T>()) } } fn info(&self) -> &::VideoInfo{ &self.vf.info } fn format_info(&self) -> &GstVideoFormatInfo{ unsafe{ &(*self.vf.info.finfo) } } } impl<'a> VideoComponent<'a>{ pub fn stride(&self) -> i32{ unsafe{ GST_VIDEO_FRAME_COMP_STRIDE!(self.vf,self.c) } } pub fn offset(&self) -> u64{ unsafe{ GST_VIDEO_FRAME_COMP_OFFSET!(self.vf,self.c) } } pub fn width(&self) -> i32{ unsafe{ GST_VIDEO_FRAME_COMP_WIDTH!(self.vf,self.c) } } pub fn height(&self) -> i32{ unsafe{ GST_VIDEO_FRAME_COMP_HEIGHT!(self.vf,self.c) } } pub fn size(&self) -> usize{ (self.stride()*self.height()) as usize } pub fn len<T>(&self) -> usize{ self.size()/mem::size_of::<T>() } pub fn depth(&self) -> u32{ self.format_info().depth[self.c] } pub fn data<T:'a>(&self) -> &'a[T]{ unsafe{ let data = GST_VIDEO_FRAME_COMP_DATA!(self.vf,self.c); from_raw_parts( mem::transmute(data), self.len::<T>()) } } fn format_info(&self) -> &GstVideoFormatInfo{ unsafe{ &(*self.vf.info.finfo) } } } impl Drop for VideoFrame{ fn drop(&mut self){ unsafe{ gst_video_frame_unmap(&mut self.vf) }; } } impl VideoFrame{ pub unsafe fn new(mut vi: GstVideoInfo, mut buffer: Buffer) -> Option<VideoFrame>{ let mut gstframe = mem::zeroed(); if gst_video_frame_map(&mut gstframe, &mut vi, buffer.gst_buffer_mut(), GST_MAP_READ) != 0{ Some(VideoFrame{ vf: gstframe, buffer: buffer }) }else{ None } } #[inline] pub fn info(&self) -> &::VideoInfo{ &self.vf.info } #[inline] pub fn flags(&self) -> &GstVideoFlags{ &self.vf.flags } #[inline] pub fn buffer(&self) -> &Buffer{ &self.buffer } #[inline] pub fn format_info(&self) -> &GstVideoFormatInfo{ unsafe{ &(*self.vf.info.finfo) } } #[inline] pub fn format(&self) -> &GstVideoFormat{ &self.format_info().format } #[inline] pub fn width(&self) -> i32{ self.info().width } #[inline] pub fn height(&self) -> i32{ self.info().height } #[inline] pub fn size(&self) -> u64{ self.info().size as u64 } #[inline] pub fn len<T>(&self) -> usize{ (self.size() / mem::size_of::<T>() as u64) as usize } #[inline] pub fn is_interlaced(&self) -> bool{ self.flags() & GST_VIDEO_FRAME_FLAG_INTERLACED == GST_VIDEO_FRAME_FLAG_INTERLACED } #[inline] pub fn is_tff(&self) -> bool{ self.flags() & GST_VIDEO_FRAME_FLAG_TFF == GST_VIDEO_FRAME_FLAG_TFF } #[inline] pub fn is_rff(&self) -> bool{ self.flags() & GST_VIDEO_FRAME_FLAG_RFF == GST_VIDEO_FRAME_FLAG_RFF } #[inline] pub fn is_onefield(&self) -> bool{ self.flags() & GST_VIDEO_FRAME_FLAG_ONEFIELD == GST_VIDEO_FRAME_FLAG_ONEFIELD } #[inline] pub fn n_planes(&self) -> u32{ self.format_info().n_planes } #[inline] pub fn plane<'a>(&'a self, p: u32) -> Option<VideoPlane<'a>>{ if p < self.n_planes(){ Some(VideoPlane{ vf: &self.vf, p: p as usize }) }else{ None } } #[inline] pub fn n_components(&self) -> u32{ self.format_info().n_components } #[inline] pub fn component<'a>(&'a self, c: u32) -> Option<VideoComponent<'a>>{ if c < self.n_components(){ Some(VideoComponent{ vf: &self.vf, c: c as usize }) }else{ None } } }