webrtc_sys/
video_frame_buffer.rs

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
15use crate::impl_thread_safety;
16
17#[cxx::bridge(namespace = "livekit")]
18pub 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        fn new_i422_buffer(
116            width: i32,
117            height: i32,
118            stride_y: i32,
119            stride_u: i32,
120            stride_v: i32,
121        ) -> UniquePtr<I422Buffer>;
122
123        fn new_i444_buffer(
124            width: i32,
125            height: i32,
126            stride_y: i32,
127            stride_u: i32,
128            stride_v: i32,
129        ) -> UniquePtr<I444Buffer>;
130
131        fn new_i010_buffer(
132            width: i32,
133            height: i32,
134            stride_y: i32,
135            stride_u: i32,
136            stride_v: i32,
137        ) -> UniquePtr<I010Buffer>;
138
139        fn new_nv12_buffer(
140            width: i32,
141            height: i32,
142            stride_y: i32,
143            stride_uv: i32,
144        ) -> UniquePtr<NV12Buffer>;
145
146        unsafe fn new_native_buffer_from_platform_image_buffer(
147            platform_native_buffer: *mut PlatformImageBuffer,
148        ) -> UniquePtr<VideoFrameBuffer>;
149        unsafe fn native_buffer_to_platform_image_buffer(
150            buffer: &UniquePtr<VideoFrameBuffer>,
151        ) -> *mut PlatformImageBuffer;
152
153        unsafe fn yuv_to_vfb(yuv: *const PlanarYuvBuffer) -> *const VideoFrameBuffer;
154        unsafe fn biyuv_to_vfb(yuv: *const BiplanarYuvBuffer) -> *const VideoFrameBuffer;
155        unsafe fn yuv8_to_yuv(yuv8: *const PlanarYuv8Buffer) -> *const PlanarYuvBuffer;
156        unsafe fn yuv16b_to_yuv(yuv16b: *const PlanarYuv16BBuffer) -> *const PlanarYuvBuffer;
157        unsafe fn biyuv8_to_biyuv(biyuv8: *const BiplanarYuv8Buffer) -> *const BiplanarYuvBuffer;
158        unsafe fn i420_to_yuv8(i420: *const I420Buffer) -> *const PlanarYuv8Buffer;
159        unsafe fn i420a_to_yuv8(i420a: *const I420ABuffer) -> *const PlanarYuv8Buffer;
160        unsafe fn i422_to_yuv8(i422: *const I422Buffer) -> *const PlanarYuv8Buffer;
161        unsafe fn i444_to_yuv8(i444: *const I444Buffer) -> *const PlanarYuv8Buffer;
162        unsafe fn i010_to_yuv16b(i010: *const I010Buffer) -> *const PlanarYuv16BBuffer;
163        unsafe fn nv12_to_biyuv8(nv12: *const NV12Buffer) -> *const BiplanarYuv8Buffer;
164
165        fn _unique_video_frame_buffer() -> UniquePtr<VideoFrameBuffer>;
166    }
167}
168
169impl_thread_safety!(ffi::VideoFrameBuffer, Send + Sync);
170impl_thread_safety!(ffi::PlanarYuvBuffer, Send + Sync);
171impl_thread_safety!(ffi::PlanarYuv8Buffer, Send + Sync);
172impl_thread_safety!(ffi::PlanarYuv16BBuffer, Send + Sync);
173impl_thread_safety!(ffi::BiplanarYuvBuffer, Send + Sync);
174impl_thread_safety!(ffi::BiplanarYuv8Buffer, Send + Sync);
175impl_thread_safety!(ffi::I420Buffer, Send + Sync);
176impl_thread_safety!(ffi::I420ABuffer, Send + Sync);
177impl_thread_safety!(ffi::I422Buffer, Send + Sync);
178impl_thread_safety!(ffi::I444Buffer, Send + Sync);
179impl_thread_safety!(ffi::I010Buffer, Send + Sync);
180impl_thread_safety!(ffi::NV12Buffer, Send + Sync);