| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use std::slice; |
| 16 | |
| 17 | use cxx::UniquePtr; |
| 18 | use webrtc_sys::{video_frame as vf_sys, video_frame_buffer as vfb_sys}; |
| 19 | |
| 20 | use super::yuv_helper; |
| 21 | use crate::video_frame::{self as vf, VideoFormatType, VideoRotation}; |
| 22 | |
| 23 | /// We don't use vf::VideoFrameBuffer trait for the types inside this module to avoid confusion |
| 24 | /// because directly using platform specific types is not valid (e.g user callback) |
| 25 | /// All the types inside this module are only used internally. For public types, see the top level |
| 26 | /// video_frame.rs |
| 27 | |
| 28 | pub fn new_video_frame_buffer( |
| 29 | mut sys_handle: UniquePtr<vfb_sys::ffi::VideoFrameBuffer>, |
| 30 | ) -> Box<dyn vf::VideoBuffer + Send + Sync> { |
| 31 | unsafe { |
| 32 | match sys_handle.buffer_type() { |
| 33 | vfb_sys::ffi::VideoFrameBufferType::Native => { |
| 34 | Box::new(vf::native::NativeBuffer { handle: NativeBuffer { sys_handle } }) |
| 35 | } |
| 36 | vfb_sys::ffi::VideoFrameBufferType::I420 => Box::new(vf::I420Buffer { |
| 37 | handle: I420Buffer { sys_handle: sys_handle.pin_mut().get_i420() }, |
| 38 | }), |
| 39 | vfb_sys::ffi::VideoFrameBufferType::I420A => Box::new(vf::I420ABuffer { |
| 40 | handle: I420ABuffer { sys_handle: sys_handle.pin_mut().get_i420a() }, |
| 41 | }), |
| 42 | vfb_sys::ffi::VideoFrameBufferType::I422 => Box::new(vf::I422Buffer { |
| 43 | handle: I422Buffer { sys_handle: sys_handle.pin_mut().get_i422() }, |
| 44 | }), |
| 45 | vfb_sys::ffi::VideoFrameBufferType::I444 => Box::new(vf::I444Buffer { |
| 46 | handle: I444Buffer { sys_handle: sys_handle.pin_mut().get_i444() }, |
| 47 | }), |
| 48 | vfb_sys::ffi::VideoFrameBufferType::I010 => Box::new(vf::I010Buffer { |
| 49 | handle: I010Buffer { sys_handle: sys_handle.pin_mut().get_i010() }, |
| 50 | }), |
| 51 | vfb_sys::ffi::VideoFrameBufferType::NV12 => Box::new(vf::NV12Buffer { |
| 52 | handle: NV12Buffer { sys_handle: sys_handle.pin_mut().get_nv12() }, |
| 53 | }), |
| 54 | _ => unreachable!(), |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl From<vf_sys::ffi::VideoRotation> for VideoRotation { |
| 60 | fn from(rotation: vf_sys::ffi::VideoRotation) -> Self { |
| 61 | match rotation { |
| 62 | vf_sys::ffi::VideoRotation::VideoRotation0 => Self::VideoRotation0, |
| 63 | vf_sys::ffi::VideoRotation::VideoRotation90 => Self::VideoRotation90, |
| 64 | vf_sys::ffi::VideoRotation::VideoRotation180 => Self::VideoRotation180, |
| 65 | vf_sys::ffi::VideoRotation::VideoRotation270 => Self::VideoRotation270, |
| 66 | _ => panic!("invalid VideoRotation"), |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl From<VideoRotation> for vf_sys::ffi::VideoRotation { |
| 72 | fn from(rotation: VideoRotation) -> Self { |
| 73 | match rotation { |
| 74 | VideoRotation::VideoRotation0 => Self::VideoRotation0, |
| 75 | VideoRotation::VideoRotation90 => Self::VideoRotation90, |
| 76 | VideoRotation::VideoRotation180 => Self::VideoRotation180, |
| 77 | VideoRotation::VideoRotation270 => Self::VideoRotation270, |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | macro_rules! recursive_cast { |
| 83 | ($ptr:expr $(, $fnc:ident)*) => { |
| 84 | { |
| 85 | let ptr = $ptr; |
| 86 | $( |
| 87 | let ptr = vfb_sys::ffi::$fnc(ptr); |
| 88 | )* |
| 89 | ptr |
| 90 | } |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | pub struct NativeBuffer { |
| 95 | sys_handle: UniquePtr<vfb_sys::ffi::VideoFrameBuffer>, |
| 96 | } |
| 97 | |
| 98 | pub struct I420Buffer { |
| 99 | sys_handle: UniquePtr<vfb_sys::ffi::I420Buffer>, |
| 100 | } |
| 101 | |
| 102 | pub struct I420ABuffer { |
| 103 | sys_handle: UniquePtr<vfb_sys::ffi::I420ABuffer>, |
| 104 | } |
| 105 | |
| 106 | pub struct I422Buffer { |
| 107 | sys_handle: UniquePtr<vfb_sys::ffi::I422Buffer>, |
| 108 | } |
| 109 | |
| 110 | pub struct I444Buffer { |
| 111 | sys_handle: UniquePtr<vfb_sys::ffi::I444Buffer>, |
| 112 | } |
| 113 | |
| 114 | pub struct I010Buffer { |
| 115 | sys_handle: UniquePtr<vfb_sys::ffi::I010Buffer>, |
| 116 | } |
| 117 | |
| 118 | pub struct NV12Buffer { |
| 119 | sys_handle: UniquePtr<vfb_sys::ffi::NV12Buffer>, |
| 120 | } |
| 121 | |
| 122 | macro_rules! impl_to_argb { |
| 123 | (I420Buffer [$($variant:ident: $fnc:ident),+], $format:ident, $self:ident, $dst:ident, $dst_stride:ident, $dst_width:ident, $dst_height:ident) => { |
| 124 | match $format { |
| 125 | $( |
| 126 | VideoFormatType::$variant => { |
| 127 | let (data_y, data_u, data_v) = $self.data(); |
| 128 | yuv_helper::$fnc( |
| 129 | data_y, |
| 130 | $self.stride_y(), |
| 131 | data_u, |
| 132 | $self.stride_u(), |
| 133 | data_v, |
| 134 | $self.stride_v(), |
| 135 | $dst, |
| 136 | $dst_stride, |
| 137 | $dst_width, |
| 138 | $dst_height, |
| 139 | ) |
| 140 | } |
| 141 | )+ |
| 142 | } |
| 143 | }; |
| 144 | (I420ABuffer) => { |
| 145 | todo!(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | #[allow(unused_unsafe)] |
| 150 | impl NativeBuffer { |
| 151 | #[cfg(any(target_os = "macos", target_os = "ios"))] |
| 152 | pub unsafe fn from_cv_pixel_buffer( |
| 153 | cv_pixel_buffer: *mut std::ffi::c_void, |
| 154 | ) -> vf::native::NativeBuffer { |
| 155 | vf::native::NativeBuffer { |
| 156 | handle: NativeBuffer { |
| 157 | sys_handle: vfb_sys::ffi::new_native_buffer_from_platform_image_buffer( |
| 158 | cv_pixel_buffer as *mut _, |
| 159 | ), |
| 160 | }, |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #[cfg(any(target_os = "macos", target_os = "ios"))] |
| 165 | pub fn get_cv_pixel_buffer(&self) -> *mut std::ffi::c_void { |
| 166 | unsafe { vfb_sys::ffi::native_buffer_to_platform_image_buffer(&self.sys_handle) as *mut _ } |
| 167 | } |
| 168 | |
| 169 | #[cfg(target_os = "linux")] |
| 170 | pub fn from_dmabuf( |
| 171 | dmabuf_fd: vf::native::DmaBufFileDescriptor, |
| 172 | resolution: crate::video_source::VideoResolution, |
| 173 | pixel_format: vf::native::DmaBufPixelFormat, |
| 174 | ) -> vf::native::NativeBuffer { |
| 175 | vf::native::NativeBuffer { |
| 176 | handle: NativeBuffer { |
| 177 | sys_handle: vfb_sys::ffi::new_native_buffer_from_dmabuf( |
| 178 | dmabuf_fd, |
| 179 | resolution.width as i32, |
| 180 | resolution.height as i32, |
| 181 | pixel_format as i32, |
| 182 | ), |
| 183 | }, |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[cfg(target_os = "linux")] |
| 188 | pub fn get_dmabuf_fd(&self) -> Option<vf::native::DmaBufFileDescriptor> { |
| 189 | let fd = vfb_sys::ffi::native_buffer_to_dmabuf_fd(&self.sys_handle); |
| 190 | (fd >= 0).then_some(fd) |
| 191 | } |
| 192 | |
| 193 | pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer { |
| 194 | &self.sys_handle |
| 195 | } |
| 196 | |
| 197 | pub fn width(&self) -> u32 { |
| 198 | self.sys_handle.width() |
| 199 | } |
| 200 | |
| 201 | pub fn height(&self) -> u32 { |
| 202 | self.sys_handle.height() |
| 203 | } |
| 204 | |
| 205 | pub fn to_i420(&self) -> I420Buffer { |
| 206 | I420Buffer { sys_handle: unsafe { self.sys_handle.to_i420() } } |
| 207 | } |
| 208 | |
| 209 | pub fn to_argb( |
| 210 | &self, |
| 211 | format: VideoFormatType, |
| 212 | dst: &mut [u8], |
| 213 | dst_stride: u32, |
| 214 | dst_width: i32, |
| 215 | dst_height: i32, |
| 216 | ) { |
| 217 | self.to_i420().to_argb(format, dst, dst_stride, dst_width, dst_height) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | impl I420Buffer { |
| 222 | pub fn new( |
| 223 | width: u32, |
| 224 | height: u32, |
| 225 | stride_y: u32, |
| 226 | stride_u: u32, |
| 227 | stride_v: u32, |
| 228 | ) -> vf::I420Buffer { |
| 229 | vf::I420Buffer { |
| 230 | handle: I420Buffer { |
| 231 | sys_handle: vfb_sys::ffi::new_i420_buffer( |
| 232 | width.try_into().unwrap(), |
| 233 | height.try_into().unwrap(), |
| 234 | stride_y.try_into().unwrap(), |
| 235 | stride_u.try_into().unwrap(), |
| 236 | stride_v.try_into().unwrap(), |
| 237 | ), |
| 238 | }, |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | pub fn new_black( |
| 243 | width: u32, |
| 244 | height: u32, |
| 245 | stride_y: u32, |
| 246 | stride_u: u32, |
| 247 | stride_v: u32, |
| 248 | ) -> vf::I420Buffer { |
| 249 | vf::I420Buffer { |
| 250 | handle: I420Buffer { |
| 251 | sys_handle: vfb_sys::ffi::new_black_i420_buffer( |
| 252 | width.try_into().unwrap(), |
| 253 | height.try_into().unwrap(), |
| 254 | stride_y.try_into().unwrap(), |
| 255 | stride_u.try_into().unwrap(), |
| 256 | stride_v.try_into().unwrap(), |
| 257 | ), |
| 258 | }, |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer { |
| 263 | unsafe { &*recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv, yuv_to_vfb) } |
| 264 | } |
| 265 | |
| 266 | pub fn width(&self) -> u32 { |
| 267 | unsafe { |
| 268 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 269 | (*ptr).width() |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | pub fn height(&self) -> u32 { |
| 274 | unsafe { |
| 275 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 276 | (*ptr).height() |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | pub fn chroma_width(&self) -> u32 { |
| 281 | unsafe { |
| 282 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv); |
| 283 | (*ptr).chroma_width() |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | pub fn chroma_height(&self) -> u32 { |
| 288 | unsafe { |
| 289 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv); |
| 290 | (*ptr).chroma_height() |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | pub fn stride_y(&self) -> u32 { |
| 295 | unsafe { |
| 296 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv); |
| 297 | (*ptr).stride_y() |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | pub fn stride_u(&self) -> u32 { |
| 302 | unsafe { |
| 303 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv); |
| 304 | (*ptr).stride_u() |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | pub fn stride_v(&self) -> u32 { |
| 309 | unsafe { |
| 310 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8, yuv8_to_yuv); |
| 311 | (*ptr).stride_v() |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | pub fn to_i420(&self) -> I420Buffer { |
| 316 | I420Buffer { |
| 317 | sys_handle: unsafe { |
| 318 | // We make a copy of the buffer because internally, when calling ToI420() |
| 319 | // if the buffer is of type I420, libwebrtc will reuse the same underlying pointer |
| 320 | // for the new created type |
| 321 | let copy = vfb_sys::ffi::copy_i420_buffer(&self.sys_handle); |
| 322 | let ptr = recursive_cast!(&*copy, i420_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 323 | (*ptr).to_i420() |
| 324 | }, |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | pub fn to_argb( |
| 329 | &self, |
| 330 | format: VideoFormatType, |
| 331 | dst: &mut [u8], |
| 332 | dst_stride: u32, |
| 333 | dst_width: i32, |
| 334 | dst_height: i32, |
| 335 | ) { |
| 336 | impl_to_argb!( |
| 337 | I420Buffer |
| 338 | [ |
| 339 | ARGB: i420_to_argb, |
| 340 | BGRA: i420_to_bgra, |
| 341 | ABGR: i420_to_abgr, |
| 342 | RGBA: i420_to_rgba |
| 343 | ], |
| 344 | format, self, dst, dst_stride, dst_width, dst_height |
| 345 | ) |
| 346 | } |
| 347 | |
| 348 | pub fn data(&self) -> (&[u8], &[u8], &[u8]) { |
| 349 | unsafe { |
| 350 | let ptr = recursive_cast!(&*self.sys_handle, i420_to_yuv8); |
| 351 | let chroma_height = (self.height() + 1) / 2; |
| 352 | ( |
| 353 | slice::from_raw_parts((*ptr).data_y(), (self.stride_y() * self.height()) as usize), |
| 354 | slice::from_raw_parts((*ptr).data_u(), (self.stride_u() * chroma_height) as usize), |
| 355 | slice::from_raw_parts((*ptr).data_v(), (self.stride_v() * chroma_height) as usize), |
| 356 | ) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> vf::I420Buffer { |
| 361 | vf::I420Buffer { |
| 362 | handle: I420Buffer { |
| 363 | sys_handle: self.sys_handle.pin_mut().scale(scaled_width, scaled_height), |
| 364 | }, |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | impl I420ABuffer { |
| 370 | pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer { |
| 371 | unsafe { &*recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv, yuv_to_vfb) } |
| 372 | } |
| 373 | |
| 374 | pub fn width(&self) -> u32 { |
| 375 | unsafe { |
| 376 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 377 | (*ptr).width() |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | pub fn height(&self) -> u32 { |
| 382 | unsafe { |
| 383 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 384 | (*ptr).height() |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | pub fn chroma_width(&self) -> u32 { |
| 389 | unsafe { |
| 390 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv); |
| 391 | (*ptr).chroma_width() |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | pub fn chroma_height(&self) -> u32 { |
| 396 | unsafe { |
| 397 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv); |
| 398 | (*ptr).chroma_height() |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | pub fn stride_y(&self) -> u32 { |
| 403 | unsafe { |
| 404 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv); |
| 405 | (*ptr).stride_y() |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | pub fn stride_u(&self) -> u32 { |
| 410 | unsafe { |
| 411 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv); |
| 412 | (*ptr).stride_u() |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | pub fn stride_v(&self) -> u32 { |
| 417 | unsafe { |
| 418 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv); |
| 419 | (*ptr).stride_v() |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | pub fn stride_a(&self) -> u32 { |
| 424 | self.sys_handle.stride_a() |
| 425 | } |
| 426 | |
| 427 | pub fn to_i420(&self) -> I420Buffer { |
| 428 | I420Buffer { |
| 429 | sys_handle: unsafe { |
| 430 | let ptr = |
| 431 | recursive_cast!(&*self.sys_handle, i420a_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 432 | (*ptr).to_i420() |
| 433 | }, |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | pub fn to_argb( |
| 438 | &self, |
| 439 | format: VideoFormatType, |
| 440 | dst: &mut [u8], |
| 441 | dst_stride: u32, |
| 442 | dst_width: i32, |
| 443 | dst_height: i32, |
| 444 | ) { |
| 445 | self.to_i420().to_argb(format, dst, dst_stride, dst_width, dst_height) |
| 446 | } |
| 447 | |
| 448 | pub fn data(&self) -> (&[u8], &[u8], &[u8], Option<&[u8]>) { |
| 449 | unsafe { |
| 450 | let ptr = recursive_cast!(&*self.sys_handle, i420a_to_yuv8); |
| 451 | let chroma_height = (self.height() + 1) / 2; |
| 452 | let data_a = self.sys_handle.data_a(); |
| 453 | let has_data_a = !data_a.is_null(); |
| 454 | ( |
| 455 | slice::from_raw_parts((*ptr).data_y(), (self.stride_y() * self.height()) as usize), |
| 456 | slice::from_raw_parts((*ptr).data_u(), (self.stride_u() * chroma_height) as usize), |
| 457 | slice::from_raw_parts((*ptr).data_v(), (self.stride_v() * chroma_height) as usize), |
| 458 | has_data_a.then_some(slice::from_raw_parts( |
| 459 | data_a, |
| 460 | (self.stride_a() * self.height()) as usize, |
| 461 | )), |
| 462 | ) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> vf::I420ABuffer { |
| 467 | vf::I420ABuffer { |
| 468 | handle: I420ABuffer { |
| 469 | sys_handle: self.sys_handle.pin_mut().scale(scaled_width, scaled_height), |
| 470 | }, |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | impl I422Buffer { |
| 476 | pub fn new( |
| 477 | width: u32, |
| 478 | height: u32, |
| 479 | stride_y: u32, |
| 480 | stride_u: u32, |
| 481 | stride_v: u32, |
| 482 | ) -> vf::I422Buffer { |
| 483 | vf::I422Buffer { |
| 484 | handle: I422Buffer { |
| 485 | sys_handle: vfb_sys::ffi::new_i422_buffer( |
| 486 | width.try_into().unwrap(), |
| 487 | height.try_into().unwrap(), |
| 488 | stride_y.try_into().unwrap(), |
| 489 | stride_u.try_into().unwrap(), |
| 490 | stride_v.try_into().unwrap(), |
| 491 | ), |
| 492 | }, |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer { |
| 497 | unsafe { &*recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv, yuv_to_vfb) } |
| 498 | } |
| 499 | |
| 500 | pub fn width(&self) -> u32 { |
| 501 | unsafe { |
| 502 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 503 | (*ptr).width() |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | pub fn height(&self) -> u32 { |
| 508 | unsafe { |
| 509 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 510 | (*ptr).height() |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | pub fn chroma_width(&self) -> u32 { |
| 515 | unsafe { |
| 516 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv); |
| 517 | (*ptr).chroma_width() |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | pub fn chroma_height(&self) -> u32 { |
| 522 | unsafe { |
| 523 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv); |
| 524 | (*ptr).chroma_height() |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | pub fn stride_y(&self) -> u32 { |
| 529 | unsafe { |
| 530 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv); |
| 531 | (*ptr).stride_y() |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | pub fn stride_u(&self) -> u32 { |
| 536 | unsafe { |
| 537 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv); |
| 538 | (*ptr).stride_u() |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | pub fn stride_v(&self) -> u32 { |
| 543 | unsafe { |
| 544 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv); |
| 545 | (*ptr).stride_v() |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | pub fn to_i420(&self) -> I420Buffer { |
| 550 | I420Buffer { |
| 551 | sys_handle: unsafe { |
| 552 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 553 | (*ptr).to_i420() |
| 554 | }, |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | pub fn to_argb( |
| 559 | &self, |
| 560 | format: VideoFormatType, |
| 561 | dst: &mut [u8], |
| 562 | dst_stride: u32, |
| 563 | dst_width: i32, |
| 564 | dst_height: i32, |
| 565 | ) { |
| 566 | self.to_i420().to_argb(format, dst, dst_stride, dst_width, dst_height) |
| 567 | } |
| 568 | |
| 569 | pub fn data(&self) -> (&[u8], &[u8], &[u8]) { |
| 570 | unsafe { |
| 571 | let ptr = recursive_cast!(&*self.sys_handle, i422_to_yuv8); |
| 572 | ( |
| 573 | slice::from_raw_parts((*ptr).data_y(), (self.stride_y() * self.height()) as usize), |
| 574 | slice::from_raw_parts((*ptr).data_u(), (self.stride_u() * self.height()) as usize), |
| 575 | slice::from_raw_parts((*ptr).data_v(), (self.stride_v() * self.height()) as usize), |
| 576 | ) |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> vf::I422Buffer { |
| 581 | vf::I422Buffer { |
| 582 | handle: I422Buffer { |
| 583 | sys_handle: self.sys_handle.pin_mut().scale(scaled_width, scaled_height), |
| 584 | }, |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | impl I444Buffer { |
| 589 | pub fn new( |
| 590 | width: u32, |
| 591 | height: u32, |
| 592 | stride_y: u32, |
| 593 | stride_u: u32, |
| 594 | stride_v: u32, |
| 595 | ) -> vf::I444Buffer { |
| 596 | vf::I444Buffer { |
| 597 | handle: I444Buffer { |
| 598 | sys_handle: vfb_sys::ffi::new_i444_buffer( |
| 599 | width.try_into().unwrap(), |
| 600 | height.try_into().unwrap(), |
| 601 | stride_y.try_into().unwrap(), |
| 602 | stride_u.try_into().unwrap(), |
| 603 | stride_v.try_into().unwrap(), |
| 604 | ), |
| 605 | }, |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer { |
| 610 | unsafe { &*recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv, yuv_to_vfb) } |
| 611 | } |
| 612 | |
| 613 | pub fn width(&self) -> u32 { |
| 614 | unsafe { |
| 615 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 616 | (*ptr).width() |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | pub fn height(&self) -> u32 { |
| 621 | unsafe { |
| 622 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 623 | (*ptr).height() |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | pub fn chroma_width(&self) -> u32 { |
| 628 | unsafe { |
| 629 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv); |
| 630 | (*ptr).chroma_width() |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | pub fn chroma_height(&self) -> u32 { |
| 635 | unsafe { |
| 636 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv); |
| 637 | (*ptr).chroma_height() |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | pub fn stride_y(&self) -> u32 { |
| 642 | unsafe { |
| 643 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv); |
| 644 | (*ptr).stride_y() |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | pub fn stride_u(&self) -> u32 { |
| 649 | unsafe { |
| 650 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv); |
| 651 | (*ptr).stride_u() |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | pub fn stride_v(&self) -> u32 { |
| 656 | unsafe { |
| 657 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv); |
| 658 | (*ptr).stride_v() |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | pub fn to_i420(&self) -> I420Buffer { |
| 663 | I420Buffer { |
| 664 | sys_handle: unsafe { |
| 665 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv, yuv_to_vfb); |
| 666 | (*ptr).to_i420() |
| 667 | }, |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | pub fn to_argb( |
| 672 | &self, |
| 673 | format: VideoFormatType, |
| 674 | dst: &mut [u8], |
| 675 | dst_stride: u32, |
| 676 | dst_width: i32, |
| 677 | dst_height: i32, |
| 678 | ) { |
| 679 | self.to_i420().to_argb(format, dst, dst_stride, dst_width, dst_height) |
| 680 | } |
| 681 | |
| 682 | pub fn data(&self) -> (&[u8], &[u8], &[u8]) { |
| 683 | unsafe { |
| 684 | let ptr = recursive_cast!(&*self.sys_handle, i444_to_yuv8); |
| 685 | ( |
| 686 | slice::from_raw_parts((*ptr).data_y(), (self.stride_y() * self.height()) as usize), |
| 687 | slice::from_raw_parts((*ptr).data_u(), (self.stride_u() * self.height()) as usize), |
| 688 | slice::from_raw_parts((*ptr).data_v(), (self.stride_v() * self.height()) as usize), |
| 689 | ) |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> vf::I444Buffer { |
| 694 | vf::I444Buffer { |
| 695 | handle: I444Buffer { |
| 696 | sys_handle: self.sys_handle.pin_mut().scale(scaled_width, scaled_height), |
| 697 | }, |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | impl I010Buffer { |
| 703 | pub fn new( |
| 704 | width: u32, |
| 705 | height: u32, |
| 706 | stride_y: u32, |
| 707 | stride_u: u32, |
| 708 | stride_v: u32, |
| 709 | ) -> vf::I010Buffer { |
| 710 | vf::I010Buffer { |
| 711 | handle: I010Buffer { |
| 712 | sys_handle: vfb_sys::ffi::new_i010_buffer( |
| 713 | width.try_into().unwrap(), |
| 714 | height.try_into().unwrap(), |
| 715 | stride_y.try_into().unwrap(), |
| 716 | stride_u.try_into().unwrap(), |
| 717 | stride_v.try_into().unwrap(), |
| 718 | ), |
| 719 | }, |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer { |
| 724 | unsafe { &*recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv, yuv_to_vfb) } |
| 725 | } |
| 726 | |
| 727 | pub fn width(&self) -> u32 { |
| 728 | unsafe { |
| 729 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv, yuv_to_vfb); |
| 730 | (*ptr).width() |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | pub fn height(&self) -> u32 { |
| 735 | unsafe { |
| 736 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv, yuv_to_vfb); |
| 737 | (*ptr).height() |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | pub fn chroma_width(&self) -> u32 { |
| 742 | unsafe { |
| 743 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv); |
| 744 | (*ptr).chroma_width() |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | pub fn chroma_height(&self) -> u32 { |
| 749 | unsafe { |
| 750 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv); |
| 751 | (*ptr).chroma_height() |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | pub fn stride_y(&self) -> u32 { |
| 756 | unsafe { |
| 757 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv); |
| 758 | (*ptr).stride_y() |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | pub fn stride_u(&self) -> u32 { |
| 763 | unsafe { |
| 764 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv); |
| 765 | (*ptr).stride_u() |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | pub fn stride_v(&self) -> u32 { |
| 770 | unsafe { |
| 771 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv); |
| 772 | (*ptr).stride_v() |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | pub fn to_i420(&self) -> I420Buffer { |
| 777 | I420Buffer { |
| 778 | sys_handle: unsafe { |
| 779 | let ptr = |
| 780 | recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv, yuv_to_vfb); |
| 781 | (*ptr).to_i420() |
| 782 | }, |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | pub fn to_argb( |
| 787 | &self, |
| 788 | format: VideoFormatType, |
| 789 | dst: &mut [u8], |
| 790 | dst_stride: u32, |
| 791 | dst_width: i32, |
| 792 | dst_height: i32, |
| 793 | ) { |
| 794 | self.to_i420().to_argb(format, dst, dst_stride, dst_width, dst_height) |
| 795 | } |
| 796 | |
| 797 | pub fn data(&self) -> (&[u16], &[u16], &[u16]) { |
| 798 | unsafe { |
| 799 | let ptr = recursive_cast!(&*self.sys_handle, i010_to_yuv16b); |
| 800 | let chroma_height = (self.height() + 1) / 2; |
| 801 | ( |
| 802 | slice::from_raw_parts( |
| 803 | (*ptr).data_y(), |
| 804 | (self.stride_y() * self.height()) as usize / 2, |
| 805 | ), |
| 806 | slice::from_raw_parts( |
| 807 | (*ptr).data_u(), |
| 808 | (self.stride_u() * chroma_height) as usize / 2, |
| 809 | ), |
| 810 | slice::from_raw_parts( |
| 811 | (*ptr).data_v(), |
| 812 | (self.stride_v() * chroma_height) as usize / 2, |
| 813 | ), |
| 814 | ) |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> vf::I010Buffer { |
| 819 | vf::I010Buffer { |
| 820 | handle: I010Buffer { |
| 821 | sys_handle: self.sys_handle.pin_mut().scale(scaled_width, scaled_height), |
| 822 | }, |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | impl NV12Buffer { |
| 828 | pub fn new(width: u32, height: u32, stride_y: u32, stride_uv: u32) -> vf::NV12Buffer { |
| 829 | vf::NV12Buffer { |
| 830 | handle: NV12Buffer { |
| 831 | sys_handle: vfb_sys::ffi::new_nv12_buffer( |
| 832 | width.try_into().unwrap(), |
| 833 | height.try_into().unwrap(), |
| 834 | stride_y.try_into().unwrap(), |
| 835 | stride_uv.try_into().unwrap(), |
| 836 | ), |
| 837 | }, |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer { |
| 842 | unsafe { |
| 843 | &*recursive_cast!(&*self.sys_handle, nv12_to_biyuv8, biyuv8_to_biyuv, biyuv_to_vfb) |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | pub fn width(&self) -> u32 { |
| 848 | unsafe { |
| 849 | let ptr = |
| 850 | recursive_cast!(&*self.sys_handle, nv12_to_biyuv8, biyuv8_to_biyuv, biyuv_to_vfb); |
| 851 | (*ptr).width() |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | pub fn height(&self) -> u32 { |
| 856 | unsafe { |
| 857 | let ptr = |
| 858 | recursive_cast!(&*self.sys_handle, nv12_to_biyuv8, biyuv8_to_biyuv, biyuv_to_vfb); |
| 859 | (*ptr).height() |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | pub fn chroma_width(&self) -> u32 { |
| 864 | unsafe { |
| 865 | let ptr = recursive_cast!(&*self.sys_handle, nv12_to_biyuv8, biyuv8_to_biyuv); |
| 866 | (*ptr).chroma_width() |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | pub fn chroma_height(&self) -> u32 { |
| 871 | unsafe { |
| 872 | let ptr = recursive_cast!(&*self.sys_handle, nv12_to_biyuv8, biyuv8_to_biyuv); |
| 873 | (*ptr).chroma_height() |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | pub fn stride_y(&self) -> u32 { |
| 878 | unsafe { |
| 879 | let ptr = recursive_cast!(&*self.sys_handle, nv12_to_biyuv8, biyuv8_to_biyuv); |
| 880 | (*ptr).stride_y() |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | pub fn stride_uv(&self) -> u32 { |
| 885 | unsafe { |
| 886 | let ptr = recursive_cast!(&*self.sys_handle, nv12_to_biyuv8, biyuv8_to_biyuv); |
| 887 | (*ptr).stride_uv() |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | pub fn to_i420(&self) -> I420Buffer { |
| 892 | I420Buffer { |
| 893 | sys_handle: unsafe { |
| 894 | let ptr = recursive_cast!( |
| 895 | &*self.sys_handle, |
| 896 | nv12_to_biyuv8, |
| 897 | biyuv8_to_biyuv, |
| 898 | biyuv_to_vfb |
| 899 | ); |
| 900 | (*ptr).to_i420() |
| 901 | }, |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | pub fn to_argb( |
| 906 | &self, |
| 907 | format: VideoFormatType, |
| 908 | dst: &mut [u8], |
| 909 | dst_stride: u32, |
| 910 | dst_width: i32, |
| 911 | dst_height: i32, |
| 912 | ) { |
| 913 | self.to_i420().to_argb(format, dst, dst_stride, dst_width, dst_height) |
| 914 | } |
| 915 | |
| 916 | pub fn data(&self) -> (&[u8], &[u8]) { |
| 917 | unsafe { |
| 918 | let ptr = recursive_cast!(&*self.sys_handle, nv12_to_biyuv8); |
| 919 | let chroma_height = (self.height() + 1) / 2; |
| 920 | |
| 921 | ( |
| 922 | slice::from_raw_parts((*ptr).data_y(), (self.stride_y() * self.height()) as usize), |
| 923 | slice::from_raw_parts( |
| 924 | (*ptr).data_uv(), |
| 925 | (self.stride_uv() * chroma_height) as usize, |
| 926 | ), |
| 927 | ) |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> vf::NV12Buffer { |
| 932 | vf::NV12Buffer { |
| 933 | handle: NV12Buffer { |
| 934 | sys_handle: self.sys_handle.pin_mut().scale(scaled_width, scaled_height), |
| 935 | }, |
| 936 | } |
| 937 | } |
| 938 | } |