| 1 | /* |
| 2 | * Copyright 2025 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "api/video/i420_buffer.h" |
| 22 | #include "api/video/i422_buffer.h" |
| 23 | #include "api/video/i444_buffer.h" |
| 24 | #include "api/video/i010_buffer.h" |
| 25 | #include "api/video/nv12_buffer.h" |
| 26 | #include "api/video/video_frame_buffer.h" |
| 27 | |
| 28 | namespace livekit_ffi { |
| 29 | class VideoFrameBuffer; |
| 30 | class PlanarYuvBuffer; |
| 31 | class PlanarYuv8Buffer; |
| 32 | class PlanarYuv16BBuffer; |
| 33 | class BiplanarYuvBuffer; |
| 34 | class BiplanarYuv8Buffer; |
| 35 | class I420Buffer; |
| 36 | class I420ABuffer; |
| 37 | class I422Buffer; |
| 38 | class I444Buffer; |
| 39 | class I010Buffer; |
| 40 | class NV12Buffer; |
| 41 | } // namespace livekit_ffi |
| 42 | |
| 43 | #ifdef __APPLE__ |
| 44 | #include <CoreVideo/CoreVideo.h> |
| 45 | namespace livekit_ffi { |
| 46 | typedef __CVBuffer PlatformImageBuffer; |
| 47 | } // namespace livekit_ffi |
| 48 | #else |
| 49 | namespace livekit_ffi { |
| 50 | typedef void PlatformImageBuffer; |
| 51 | } // namespace livekit_ffi |
| 52 | #endif |
| 53 | |
| 54 | #include "webrtc-sys/src/video_frame_buffer.rs.h" |
| 55 | |
| 56 | namespace livekit_ffi { |
| 57 | |
| 58 | class VideoFrameBuffer { |
| 59 | public: |
| 60 | explicit VideoFrameBuffer( |
| 61 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer); |
| 62 | |
| 63 | VideoFrameBufferType buffer_type() const; |
| 64 | |
| 65 | unsigned int width() const; |
| 66 | unsigned int height() const; |
| 67 | |
| 68 | std::unique_ptr<I420Buffer> to_i420() const; |
| 69 | |
| 70 | // Requires ownership |
| 71 | std::unique_ptr<I420Buffer> get_i420(); |
| 72 | std::unique_ptr<I420ABuffer> get_i420a(); |
| 73 | std::unique_ptr<I422Buffer> get_i422(); |
| 74 | std::unique_ptr<I444Buffer> get_i444(); |
| 75 | std::unique_ptr<I010Buffer> get_i010(); |
| 76 | std::unique_ptr<NV12Buffer> get_nv12(); |
| 77 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> get() const; |
| 78 | |
| 79 | protected: |
| 80 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_; |
| 81 | }; |
| 82 | |
| 83 | class PlanarYuvBuffer : public VideoFrameBuffer { |
| 84 | public: |
| 85 | explicit PlanarYuvBuffer(webrtc::scoped_refptr<webrtc::PlanarYuvBuffer> buffer); |
| 86 | |
| 87 | unsigned int chroma_width() const; |
| 88 | unsigned int chroma_height() const; |
| 89 | |
| 90 | unsigned int stride_y() const; |
| 91 | unsigned int stride_u() const; |
| 92 | unsigned int stride_v() const; |
| 93 | |
| 94 | private: |
| 95 | webrtc::PlanarYuvBuffer* buffer() const; |
| 96 | }; |
| 97 | |
| 98 | class PlanarYuv8Buffer : public PlanarYuvBuffer { |
| 99 | public: |
| 100 | explicit PlanarYuv8Buffer( |
| 101 | webrtc::scoped_refptr<webrtc::PlanarYuv8Buffer> buffer); |
| 102 | |
| 103 | const uint8_t* data_y() const; |
| 104 | const uint8_t* data_u() const; |
| 105 | const uint8_t* data_v() const; |
| 106 | |
| 107 | private: |
| 108 | webrtc::PlanarYuv8Buffer* buffer() const; |
| 109 | }; |
| 110 | |
| 111 | class PlanarYuv16BBuffer : public PlanarYuvBuffer { |
| 112 | public: |
| 113 | explicit PlanarYuv16BBuffer( |
| 114 | webrtc::scoped_refptr<webrtc::PlanarYuv16BBuffer> buffer); |
| 115 | |
| 116 | const uint16_t* data_y() const; |
| 117 | const uint16_t* data_u() const; |
| 118 | const uint16_t* data_v() const; |
| 119 | |
| 120 | private: |
| 121 | webrtc::PlanarYuv16BBuffer* buffer() const; |
| 122 | }; |
| 123 | |
| 124 | class BiplanarYuvBuffer : public VideoFrameBuffer { |
| 125 | public: |
| 126 | explicit BiplanarYuvBuffer( |
| 127 | webrtc::scoped_refptr<webrtc::BiplanarYuvBuffer> buffer); |
| 128 | |
| 129 | unsigned int chroma_width() const; |
| 130 | unsigned int chroma_height() const; |
| 131 | |
| 132 | unsigned int stride_y() const; |
| 133 | unsigned int stride_uv() const; |
| 134 | |
| 135 | private: |
| 136 | webrtc::BiplanarYuvBuffer* buffer() const; |
| 137 | }; |
| 138 | |
| 139 | class BiplanarYuv8Buffer : public BiplanarYuvBuffer { |
| 140 | public: |
| 141 | explicit BiplanarYuv8Buffer( |
| 142 | webrtc::scoped_refptr<webrtc::BiplanarYuv8Buffer> buffer); |
| 143 | |
| 144 | const uint8_t* data_y() const; |
| 145 | const uint8_t* data_uv() const; |
| 146 | |
| 147 | private: |
| 148 | webrtc::BiplanarYuv8Buffer* buffer() const; |
| 149 | }; |
| 150 | |
| 151 | class I420Buffer : public PlanarYuv8Buffer { |
| 152 | public: |
| 153 | explicit I420Buffer(webrtc::scoped_refptr<webrtc::I420BufferInterface> buffer); |
| 154 | |
| 155 | std::unique_ptr<I420Buffer> scale(int scaled_width, int scaled_height) const; |
| 156 | |
| 157 | private: |
| 158 | webrtc::I420BufferInterface* buffer() const; |
| 159 | }; |
| 160 | |
| 161 | class I420ABuffer : public I420Buffer { |
| 162 | public: |
| 163 | explicit I420ABuffer(webrtc::scoped_refptr<webrtc::I420ABufferInterface> buffer); |
| 164 | |
| 165 | unsigned int stride_a() const; |
| 166 | const uint8_t* data_a() const; |
| 167 | |
| 168 | std::unique_ptr<I420ABuffer> scale(int scaled_width, int scaled_height) const; |
| 169 | |
| 170 | private: |
| 171 | webrtc::I420ABufferInterface* buffer() const; |
| 172 | }; |
| 173 | |
| 174 | class I422Buffer : public PlanarYuv8Buffer { |
| 175 | public: |
| 176 | explicit I422Buffer(webrtc::scoped_refptr<webrtc::I422BufferInterface> buffer); |
| 177 | |
| 178 | std::unique_ptr<I422Buffer> scale(int scaled_width, int scaled_height) const; |
| 179 | |
| 180 | private: |
| 181 | webrtc::I422BufferInterface* buffer() const; |
| 182 | }; |
| 183 | |
| 184 | class I444Buffer : public PlanarYuv8Buffer { |
| 185 | public: |
| 186 | explicit I444Buffer(webrtc::scoped_refptr<webrtc::I444BufferInterface> buffer); |
| 187 | |
| 188 | std::unique_ptr<I444Buffer> scale(int scaled_width, int scaled_height) const; |
| 189 | |
| 190 | private: |
| 191 | webrtc::I444BufferInterface* buffer() const; |
| 192 | }; |
| 193 | |
| 194 | class I010Buffer : public PlanarYuv16BBuffer { |
| 195 | public: |
| 196 | explicit I010Buffer(webrtc::scoped_refptr<webrtc::I010BufferInterface> buffer); |
| 197 | |
| 198 | std::unique_ptr<I010Buffer> scale(int scaled_width, int scaled_height) const; |
| 199 | |
| 200 | private: |
| 201 | webrtc::I010BufferInterface* buffer() const; |
| 202 | }; |
| 203 | |
| 204 | class NV12Buffer : public BiplanarYuv8Buffer { |
| 205 | public: |
| 206 | explicit NV12Buffer(webrtc::scoped_refptr<webrtc::NV12BufferInterface> buffer); |
| 207 | |
| 208 | std::unique_ptr<NV12Buffer> scale(int scaled_width, int scaled_height) const; |
| 209 | |
| 210 | private: |
| 211 | webrtc::NV12BufferInterface* buffer() const; |
| 212 | }; |
| 213 | |
| 214 | std::unique_ptr<I420Buffer> copy_i420_buffer( |
| 215 | const std::unique_ptr<I420Buffer>& i420); |
| 216 | std::unique_ptr<I420Buffer> new_i420_buffer(int width, int height, int stride_y, int stride_u, int stride_v); |
| 217 | std::unique_ptr<I420Buffer> new_black_i420_buffer(int width, int height, int stride_y, int stride_u, int stride_v); |
| 218 | std::unique_ptr<I422Buffer> new_i422_buffer(int width, int height, int stride_y, int stride_u, int stride_v); |
| 219 | std::unique_ptr<I444Buffer> new_i444_buffer(int width, int height, int stride_y, int stride_u, int stride_v); |
| 220 | std::unique_ptr<I010Buffer> new_i010_buffer(int width, int height, int stride_y, int stride_u, int stride_v); |
| 221 | std::unique_ptr<NV12Buffer> new_nv12_buffer(int width, int height, int stride_y, int stride_uv); |
| 222 | |
| 223 | std::unique_ptr<VideoFrameBuffer> new_native_buffer_from_platform_image_buffer(PlatformImageBuffer *buffer); |
| 224 | PlatformImageBuffer* native_buffer_to_platform_image_buffer(const std::unique_ptr<VideoFrameBuffer> &); |
| 225 | |
| 226 | std::unique_ptr<VideoFrameBuffer> new_native_buffer_from_dmabuf(int dmabuf_fd, int width, int height, int pixel_format); |
| 227 | int native_buffer_to_dmabuf_fd(const std::unique_ptr<VideoFrameBuffer> &); |
| 228 | |
| 229 | static const VideoFrameBuffer* yuv_to_vfb(const PlanarYuvBuffer* yuv) { |
| 230 | return yuv; |
| 231 | } |
| 232 | |
| 233 | static const VideoFrameBuffer* biyuv_to_vfb(const BiplanarYuvBuffer* biyuv) { |
| 234 | return biyuv; |
| 235 | } |
| 236 | |
| 237 | static const PlanarYuvBuffer* yuv8_to_yuv(const PlanarYuv8Buffer* yuv8) { |
| 238 | return yuv8; |
| 239 | } |
| 240 | |
| 241 | static const PlanarYuvBuffer* yuv16b_to_yuv(const PlanarYuv16BBuffer* yuv16) { |
| 242 | return yuv16; |
| 243 | } |
| 244 | |
| 245 | static const BiplanarYuvBuffer* biyuv8_to_biyuv( |
| 246 | const BiplanarYuv8Buffer* biyuv8) { |
| 247 | return biyuv8; |
| 248 | } |
| 249 | |
| 250 | static const PlanarYuv8Buffer* i420_to_yuv8(const I420Buffer* i420) { |
| 251 | return i420; |
| 252 | } |
| 253 | |
| 254 | static const PlanarYuv8Buffer* i420a_to_yuv8(const I420ABuffer* i420a) { |
| 255 | return i420a; |
| 256 | } |
| 257 | |
| 258 | static const PlanarYuv8Buffer* i422_to_yuv8(const I422Buffer* i422) { |
| 259 | return i422; |
| 260 | } |
| 261 | |
| 262 | static const PlanarYuv8Buffer* i444_to_yuv8(const I444Buffer* i444) { |
| 263 | return i444; |
| 264 | } |
| 265 | |
| 266 | static const PlanarYuv16BBuffer* i010_to_yuv16b(const I010Buffer* i010) { |
| 267 | return i010; |
| 268 | } |
| 269 | |
| 270 | static const BiplanarYuv8Buffer* nv12_to_biyuv8(const NV12Buffer* nv12) { |
| 271 | return nv12; |
| 272 | } |
| 273 | |
| 274 | static std::unique_ptr<VideoFrameBuffer> _unique_video_frame_buffer() { |
| 275 | return nullptr; |
| 276 | } |
| 277 | |
| 278 | } // namespace livekit_ffi |