| 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 crate::impl_thread_safety; |
| 16 | |
| 17 | #[cxx::bridge(namespace = "livekit_ffi")] |
| 18 | pub mod ffi { |
| 19 | #[derive(Debug)] |
| 20 | #[repr(i32)] |
| 21 | pub enum VideoFrameBufferType { |
| 22 | Native, |
| 23 | I420, |
| 24 | I420A, |
| 25 | I422, |
| 26 | I444, |
| 27 | I010, |
| 28 | NV12, |
| 29 | } |
| 30 | |
| 31 | unsafe extern "C++" { |
| 32 | include!("livekit/video_frame_buffer.h"); |
| 33 | |
| 34 | type VideoFrameBuffer; |
| 35 | type PlanarYuvBuffer; |
| 36 | type PlanarYuv8Buffer; |
| 37 | type PlanarYuv16BBuffer; |
| 38 | type BiplanarYuvBuffer; |
| 39 | type BiplanarYuv8Buffer; |
| 40 | type I420Buffer; |
| 41 | type I420ABuffer; |
| 42 | type I422Buffer; |
| 43 | type I444Buffer; |
| 44 | type I010Buffer; |
| 45 | type NV12Buffer; |
| 46 | type PlatformImageBuffer; |
| 47 | |
| 48 | fn buffer_type(self: &VideoFrameBuffer) -> VideoFrameBufferType; |
| 49 | fn width(self: &VideoFrameBuffer) -> u32; |
| 50 | fn height(self: &VideoFrameBuffer) -> u32; |
| 51 | |
| 52 | /// # SAFETY |
| 53 | /// If the buffer type is I420, the buffer must be cloned before |
| 54 | unsafe fn to_i420(self: &VideoFrameBuffer) -> UniquePtr<I420Buffer>; |
| 55 | |
| 56 | /// # SAFETY |
| 57 | /// The functions require ownership |
| 58 | unsafe fn get_i420(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<I420Buffer>; |
| 59 | unsafe fn get_i420a(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<I420ABuffer>; |
| 60 | unsafe fn get_i422(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<I422Buffer>; |
| 61 | unsafe fn get_i444(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<I444Buffer>; |
| 62 | unsafe fn get_i010(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<I010Buffer>; |
| 63 | unsafe fn get_nv12(self: Pin<&mut VideoFrameBuffer>) -> UniquePtr<NV12Buffer>; |
| 64 | |
| 65 | fn chroma_width(self: &PlanarYuvBuffer) -> u32; |
| 66 | fn chroma_height(self: &PlanarYuvBuffer) -> u32; |
| 67 | fn stride_y(self: &PlanarYuvBuffer) -> u32; |
| 68 | fn stride_u(self: &PlanarYuvBuffer) -> u32; |
| 69 | fn stride_v(self: &PlanarYuvBuffer) -> u32; |
| 70 | |
| 71 | fn data_y(self: &PlanarYuv8Buffer) -> *const u8; |
| 72 | fn data_u(self: &PlanarYuv8Buffer) -> *const u8; |
| 73 | fn data_v(self: &PlanarYuv8Buffer) -> *const u8; |
| 74 | |
| 75 | fn data_y(self: &PlanarYuv16BBuffer) -> *const u16; |
| 76 | fn data_u(self: &PlanarYuv16BBuffer) -> *const u16; |
| 77 | fn data_v(self: &PlanarYuv16BBuffer) -> *const u16; |
| 78 | |
| 79 | fn chroma_width(self: &BiplanarYuvBuffer) -> u32; |
| 80 | fn chroma_height(self: &BiplanarYuvBuffer) -> u32; |
| 81 | fn stride_y(self: &BiplanarYuvBuffer) -> u32; |
| 82 | fn stride_uv(self: &BiplanarYuvBuffer) -> u32; |
| 83 | |
| 84 | fn data_y(self: &BiplanarYuv8Buffer) -> *const u8; |
| 85 | fn data_uv(self: &BiplanarYuv8Buffer) -> *const u8; |
| 86 | |
| 87 | fn stride_a(self: &I420ABuffer) -> u32; |
| 88 | fn data_a(self: &I420ABuffer) -> *const u8; |
| 89 | |
| 90 | fn scale(self: &I420Buffer, scaled_width: i32, scaled_height: i32) |
| 91 | -> UniquePtr<I420Buffer>; |
| 92 | fn scale( |
| 93 | self: &I420ABuffer, |
| 94 | scaled_width: i32, |
| 95 | scaled_height: i32, |
| 96 | ) -> UniquePtr<I420ABuffer>; |
| 97 | fn scale(self: &I422Buffer, scaled_width: i32, scaled_height: i32) |
| 98 | -> UniquePtr<I422Buffer>; |
| 99 | fn scale(self: &I444Buffer, scaled_width: i32, scaled_height: i32) |
| 100 | -> UniquePtr<I444Buffer>; |
| 101 | fn scale(self: &I010Buffer, scaled_width: i32, scaled_height: i32) |
| 102 | -> UniquePtr<I010Buffer>; |
| 103 | fn scale(self: &NV12Buffer, scaled_width: i32, scaled_height: i32) |
| 104 | -> UniquePtr<NV12Buffer>; |
| 105 | |
| 106 | fn copy_i420_buffer(i420: &UniquePtr<I420Buffer>) -> UniquePtr<I420Buffer>; |
| 107 | fn new_i420_buffer( |
| 108 | width: i32, |
| 109 | height: i32, |
| 110 | stride_y: i32, |
| 111 | stride_u: i32, |
| 112 | stride_v: i32, |
| 113 | ) -> UniquePtr<I420Buffer>; |
| 114 | |
| 115 | /// Like `new_i420_buffer`, but with the pixel data set to black |
| 116 | /// (Y=0, U=V=128) instead of left uninitialized. |
| 117 | fn new_black_i420_buffer( |
| 118 | width: i32, |
| 119 | height: i32, |
| 120 | stride_y: i32, |
| 121 | stride_u: i32, |
| 122 | stride_v: i32, |
| 123 | ) -> UniquePtr<I420Buffer>; |
| 124 | |
| 125 | fn new_i422_buffer( |
| 126 | width: i32, |
| 127 | height: i32, |
| 128 | stride_y: i32, |
| 129 | stride_u: i32, |
| 130 | stride_v: i32, |
| 131 | ) -> UniquePtr<I422Buffer>; |
| 132 | |
| 133 | fn new_i444_buffer( |
| 134 | width: i32, |
| 135 | height: i32, |
| 136 | stride_y: i32, |
| 137 | stride_u: i32, |
| 138 | stride_v: i32, |
| 139 | ) -> UniquePtr<I444Buffer>; |
| 140 | |
| 141 | fn new_i010_buffer( |
| 142 | width: i32, |
| 143 | height: i32, |
| 144 | stride_y: i32, |
| 145 | stride_u: i32, |
| 146 | stride_v: i32, |
| 147 | ) -> UniquePtr<I010Buffer>; |
| 148 | |
| 149 | fn new_nv12_buffer( |
| 150 | width: i32, |
| 151 | height: i32, |
| 152 | stride_y: i32, |
| 153 | stride_uv: i32, |
| 154 | ) -> UniquePtr<NV12Buffer>; |
| 155 | |
| 156 | unsafe fn new_native_buffer_from_platform_image_buffer( |
| 157 | platform_native_buffer: *mut PlatformImageBuffer, |
| 158 | ) -> UniquePtr<VideoFrameBuffer>; |
| 159 | unsafe fn native_buffer_to_platform_image_buffer( |
| 160 | buffer: &UniquePtr<VideoFrameBuffer>, |
| 161 | ) -> *mut PlatformImageBuffer; |
| 162 | |
| 163 | /// Creates a native buffer backed by a DMA-BUF file descriptor |
| 164 | /// (Jetson `NvBufSurface`). `pixel_format` is a |
| 165 | /// `livekit::DmaBufPixelFormat` value: `0` for NV12, `1` for YUV420M. |
| 166 | /// |
| 167 | /// The buffer does not take ownership of the fd: it must remain valid |
| 168 | /// until every frame captured from it has been consumed downstream. |
| 169 | fn new_native_buffer_from_dmabuf( |
| 170 | dmabuf_fd: i32, |
| 171 | width: i32, |
| 172 | height: i32, |
| 173 | pixel_format: i32, |
| 174 | ) -> UniquePtr<VideoFrameBuffer>; |
| 175 | |
| 176 | /// Returns the DMA-BUF fd backing this buffer, or `-1` if this buffer |
| 177 | /// is not DMA-BUF backed. |
| 178 | fn native_buffer_to_dmabuf_fd(buffer: &UniquePtr<VideoFrameBuffer>) -> i32; |
| 179 | |
| 180 | unsafe fn yuv_to_vfb(yuv: *const PlanarYuvBuffer) -> *const VideoFrameBuffer; |
| 181 | unsafe fn biyuv_to_vfb(yuv: *const BiplanarYuvBuffer) -> *const VideoFrameBuffer; |
| 182 | unsafe fn yuv8_to_yuv(yuv8: *const PlanarYuv8Buffer) -> *const PlanarYuvBuffer; |
| 183 | unsafe fn yuv16b_to_yuv(yuv16b: *const PlanarYuv16BBuffer) -> *const PlanarYuvBuffer; |
| 184 | unsafe fn biyuv8_to_biyuv(biyuv8: *const BiplanarYuv8Buffer) -> *const BiplanarYuvBuffer; |
| 185 | unsafe fn i420_to_yuv8(i420: *const I420Buffer) -> *const PlanarYuv8Buffer; |
| 186 | unsafe fn i420a_to_yuv8(i420a: *const I420ABuffer) -> *const PlanarYuv8Buffer; |
| 187 | unsafe fn i422_to_yuv8(i422: *const I422Buffer) -> *const PlanarYuv8Buffer; |
| 188 | unsafe fn i444_to_yuv8(i444: *const I444Buffer) -> *const PlanarYuv8Buffer; |
| 189 | unsafe fn i010_to_yuv16b(i010: *const I010Buffer) -> *const PlanarYuv16BBuffer; |
| 190 | unsafe fn nv12_to_biyuv8(nv12: *const NV12Buffer) -> *const BiplanarYuv8Buffer; |
| 191 | |
| 192 | fn _unique_video_frame_buffer() -> UniquePtr<VideoFrameBuffer>; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl_thread_safety!(ffi::VideoFrameBuffer, Send + Sync); |
| 197 | impl_thread_safety!(ffi::PlanarYuvBuffer, Send + Sync); |
| 198 | impl_thread_safety!(ffi::PlanarYuv8Buffer, Send + Sync); |
| 199 | impl_thread_safety!(ffi::PlanarYuv16BBuffer, Send + Sync); |
| 200 | impl_thread_safety!(ffi::BiplanarYuvBuffer, Send + Sync); |
| 201 | impl_thread_safety!(ffi::BiplanarYuv8Buffer, Send + Sync); |
| 202 | impl_thread_safety!(ffi::I420Buffer, Send + Sync); |
| 203 | impl_thread_safety!(ffi::I420ABuffer, Send + Sync); |
| 204 | impl_thread_safety!(ffi::I422Buffer, Send + Sync); |
| 205 | impl_thread_safety!(ffi::I444Buffer, Send + Sync); |
| 206 | impl_thread_safety!(ffi::I010Buffer, Send + Sync); |
| 207 | impl_thread_safety!(ffi::NV12Buffer, Send + Sync); |