| 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 | #include "livekit/dmabuf_video_frame_buffer.h" |
| 18 | |
| 19 | #include <cstdio> |
| 20 | #include <cstring> |
| 21 | #include <mutex> |
| 22 | #include <unordered_map> |
| 23 | |
| 24 | #include "api/make_ref_counted.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 | |
| 27 | #ifdef USE_JETSON_VIDEO_CODEC |
| 28 | #include "NvBufSurface.h" |
| 29 | #include "jetson_runtime_loader.h" |
| 30 | #endif |
| 31 | |
| 32 | #include "third_party/libyuv/include/libyuv/convert.h" |
| 33 | |
| 34 | namespace livekit { |
| 35 | |
| 36 | DmaBufVideoFrameBuffer::DmaBufVideoFrameBuffer(int dmabuf_fd, |
| 37 | int width, |
| 38 | int height, |
| 39 | DmaBufPixelFormat pixel_format) |
| 40 | : dmabuf_fd_(dmabuf_fd), |
| 41 | width_(width), |
| 42 | height_(height), |
| 43 | pixel_format_(pixel_format) {} |
| 44 | |
| 45 | webrtc::VideoFrameBuffer::Type DmaBufVideoFrameBuffer::type() const { |
| 46 | return Type::kNative; |
| 47 | } |
| 48 | |
| 49 | int DmaBufVideoFrameBuffer::width() const { |
| 50 | return width_; |
| 51 | } |
| 52 | |
| 53 | int DmaBufVideoFrameBuffer::height() const { |
| 54 | return height_; |
| 55 | } |
| 56 | |
| 57 | webrtc::scoped_refptr<webrtc::I420BufferInterface> |
| 58 | DmaBufVideoFrameBuffer::ToI420() { |
| 59 | #ifdef USE_JETSON_VIDEO_CODEC |
| 60 | // The NvBufSurface* symbols are lazily bound and abort the process if |
| 61 | // invoked while the Jetson userspace libraries are absent. |
| 62 | if (!lk_jetson_runtime_libs_available()) { |
| 63 | RTC_LOG(LS_ERROR) << "DmaBufVideoFrameBuffer::ToI420: Jetson multimedia " |
| 64 | "libraries are not available"; |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | // Cache NvBufSurface pointers per fd to avoid calling NvBufSurfaceFromFd |
| 69 | // on every frame. On some JetPack versions the fd-to-surface lookup |
| 70 | // prints spurious "Wrong buffer index" warnings. The surface pointer is |
| 71 | // stable for the lifetime of the DMA buffer (freed only when the Argus |
| 72 | // session is destroyed), so caching is safe. |
| 73 | static std::mutex cache_mutex; |
| 74 | static std::unordered_map<int, NvBufSurface*> surface_cache; |
| 75 | |
| 76 | NvBufSurface* surface = nullptr; |
| 77 | { |
| 78 | std::lock_guard<std::mutex> lock(cache_mutex); |
| 79 | auto cache_it = surface_cache.find(dmabuf_fd_); |
| 80 | if (cache_it != surface_cache.end()) { |
| 81 | surface = cache_it->second; |
| 82 | } else { |
| 83 | int ret = NvBufSurfaceFromFd(dmabuf_fd_, reinterpret_cast<void**>(&surface)); |
| 84 | if (ret != 0 || !surface || surface->batchSize < 1) { |
| 85 | RTC_LOG(LS_ERROR) << "DmaBufVideoFrameBuffer::ToI420: " |
| 86 | "NvBufSurfaceFromFd failed (fd=" << dmabuf_fd_ |
| 87 | << ", ret=" << ret << ")"; |
| 88 | return nullptr; |
| 89 | } |
| 90 | surface_cache[dmabuf_fd_] = surface; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | int ret = NvBufSurfaceMap(surface, 0, -1, NVBUF_MAP_READ); |
| 95 | if (ret != 0) { |
| 96 | RTC_LOG(LS_ERROR) << "DmaBufVideoFrameBuffer::ToI420: " |
| 97 | "NvBufSurfaceMap failed (ret=" << ret << ")"; |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | NvBufSurfaceSyncForCpu(surface, 0, -1); |
| 102 | |
| 103 | const NvBufSurfaceParams& params = surface->surfaceList[0]; |
| 104 | webrtc::scoped_refptr<webrtc::I420Buffer> i420 = |
| 105 | webrtc::I420Buffer::Create(width_, height_); |
| 106 | |
| 107 | if (pixel_format_ == DmaBufPixelFormat::kNV12) { |
| 108 | const uint8_t* src_y = |
| 109 | static_cast<const uint8_t*>(params.mappedAddr.addr[0]); |
| 110 | const uint8_t* src_uv = |
| 111 | static_cast<const uint8_t*>(params.mappedAddr.addr[1]); |
| 112 | int src_stride_y = static_cast<int>(params.planeParams.pitch[0]); |
| 113 | int src_stride_uv = static_cast<int>(params.planeParams.pitch[1]); |
| 114 | |
| 115 | libyuv::NV12ToI420(src_y, src_stride_y, |
| 116 | src_uv, src_stride_uv, |
| 117 | i420->MutableDataY(), i420->StrideY(), |
| 118 | i420->MutableDataU(), i420->StrideU(), |
| 119 | i420->MutableDataV(), i420->StrideV(), |
| 120 | width_, height_); |
| 121 | } else { |
| 122 | // YUV420M: three separate planes |
| 123 | const uint8_t* src_y = |
| 124 | static_cast<const uint8_t*>(params.mappedAddr.addr[0]); |
| 125 | const uint8_t* src_u = |
| 126 | static_cast<const uint8_t*>(params.mappedAddr.addr[1]); |
| 127 | const uint8_t* src_v = |
| 128 | static_cast<const uint8_t*>(params.mappedAddr.addr[2]); |
| 129 | int src_stride_y = static_cast<int>(params.planeParams.pitch[0]); |
| 130 | int src_stride_u = static_cast<int>(params.planeParams.pitch[1]); |
| 131 | int src_stride_v = static_cast<int>(params.planeParams.pitch[2]); |
| 132 | |
| 133 | libyuv::I420Copy(src_y, src_stride_y, |
| 134 | src_u, src_stride_u, |
| 135 | src_v, src_stride_v, |
| 136 | i420->MutableDataY(), i420->StrideY(), |
| 137 | i420->MutableDataU(), i420->StrideU(), |
| 138 | i420->MutableDataV(), i420->StrideV(), |
| 139 | width_, height_); |
| 140 | } |
| 141 | |
| 142 | NvBufSurfaceUnMap(surface, 0, -1); |
| 143 | return i420; |
| 144 | #else |
| 145 | RTC_LOG(LS_ERROR) << "DmaBufVideoFrameBuffer::ToI420: " |
| 146 | "not supported without Jetson MMAPI"; |
| 147 | return nullptr; |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> |
| 152 | DmaBufVideoFrameBuffer::CropAndScale(int offset_x, |
| 153 | int offset_y, |
| 154 | int crop_width, |
| 155 | int crop_height, |
| 156 | int scaled_width, |
| 157 | int scaled_height) { |
| 158 | // Always return the native DmaBuf buffer unchanged. CPU-side crop/scale |
| 159 | // on a DMA buffer would call ToI420() which maps to CPU, performs |
| 160 | // NV12->I420 conversion, and destroys the zero-copy encode path. The |
| 161 | // hardware encoder handles the native resolution efficiently; bandwidth |
| 162 | // adaptation should use bitrate control, not source-side rescaling. |
| 163 | return webrtc::scoped_refptr<webrtc::VideoFrameBuffer>(this); |
| 164 | } |
| 165 | |
| 166 | DmaBufVideoFrameBuffer* DmaBufVideoFrameBuffer::FromNative( |
| 167 | webrtc::VideoFrameBuffer* buffer) { |
| 168 | if (!buffer || buffer->type() != webrtc::VideoFrameBuffer::Type::kNative) { |
| 169 | return nullptr; |
| 170 | } |
| 171 | return dynamic_cast<DmaBufVideoFrameBuffer*>(buffer); |
| 172 | } |
| 173 | |
| 174 | } // namespace livekit |