| 1 | /* |
| 2 | * Copyright 2026 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 | #ifndef LIVEKIT_JETSON_MMAPI_ENCODER_H_ |
| 18 | #define LIVEKIT_JETSON_MMAPI_ENCODER_H_ |
| 19 | |
| 20 | #include <linux/videodev2.h> |
| 21 | |
| 22 | #include <cstdint> |
| 23 | #include <optional> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | class NvVideoEncoder; |
| 28 | |
| 29 | namespace livekit { |
| 30 | |
| 31 | enum class JetsonCodec : uint8_t { kH264, kH265, kAV1 }; |
| 32 | |
| 33 | class JetsonMmapiEncoder { |
| 34 | public: |
| 35 | explicit JetsonMmapiEncoder(JetsonCodec codec); |
| 36 | ~JetsonMmapiEncoder(); |
| 37 | |
| 38 | static bool IsSupported(); |
| 39 | static bool IsCodecSupported(JetsonCodec codec); |
| 40 | |
| 41 | bool Initialize(int width, |
| 42 | int height, |
| 43 | int framerate, |
| 44 | int bitrate_bps, |
| 45 | int keyframe_interval); |
| 46 | void Destroy(); |
| 47 | bool IsInitialized() const; |
| 48 | |
| 49 | bool Encode(const uint8_t* src_y, |
| 50 | int stride_y, |
| 51 | const uint8_t* src_u, |
| 52 | int stride_u, |
| 53 | const uint8_t* src_v, |
| 54 | int stride_v, |
| 55 | bool force_keyframe, |
| 56 | std::vector<uint8_t>* encoded, |
| 57 | bool* is_keyframe); |
| 58 | |
| 59 | // NV12 input: full-resolution luma plane (Y) + interleaved chroma plane (UV). |
| 60 | // This matches Jetson MMAPI encoder output-plane expectations on most Jetsons. |
| 61 | bool EncodeNV12(const uint8_t* src_y, |
| 62 | int stride_y, |
| 63 | const uint8_t* src_uv, |
| 64 | int stride_uv, |
| 65 | bool force_keyframe, |
| 66 | std::vector<uint8_t>* encoded, |
| 67 | bool* is_keyframe); |
| 68 | |
| 69 | // Zero-copy DMA buffer encode: pass an NvBufSurface DMA fd directly to the |
| 70 | // encoder output plane via V4L2_MEMORY_DMABUF. No pixel data is copied. |
| 71 | bool EncodeDmaBuf(int dmabuf_fd, |
| 72 | bool force_keyframe, |
| 73 | std::vector<uint8_t>* encoded, |
| 74 | bool* is_keyframe); |
| 75 | |
| 76 | void SetRates(int framerate, int bitrate_bps); |
| 77 | void SetKeyframeInterval(int keyframe_interval); |
| 78 | |
| 79 | private: |
| 80 | bool CreateEncoder(); |
| 81 | bool ConfigureEncoder(); |
| 82 | void ConfigureAv1HeadersWithFrame(); |
| 83 | bool ConfigureAv1Encoder(); |
| 84 | bool SetupPlanes(); |
| 85 | bool QueueCaptureBuffers(); |
| 86 | bool StartStreaming(); |
| 87 | void StopStreaming(); |
| 88 | bool QueueOutputBuffer(const uint8_t* src_y, |
| 89 | int stride_y, |
| 90 | const uint8_t* src_u, |
| 91 | int stride_u, |
| 92 | const uint8_t* src_v, |
| 93 | int stride_v); |
| 94 | bool QueueOutputBufferNV12(const uint8_t* src_y, |
| 95 | int stride_y, |
| 96 | const uint8_t* src_uv, |
| 97 | int stride_uv); |
| 98 | bool QueueOutputBufferDmaBuf(int dmabuf_fd); |
| 99 | bool SetupPlanesDmaBuf(); |
| 100 | bool DequeueCaptureBuffer(std::vector<uint8_t>* encoded, bool* is_keyframe); |
| 101 | bool DequeueOutputBuffer(); |
| 102 | bool ForceKeyframe(); |
| 103 | |
| 104 | static std::optional<std::string> FindEncoderDevice(); |
| 105 | static uint32_t CodecToV4L2PixFmt(JetsonCodec codec); |
| 106 | static uint32_t CodecToV4L2FallbackPixFmt(JetsonCodec codec); |
| 107 | |
| 108 | JetsonCodec codec_; |
| 109 | NvVideoEncoder* encoder_ = nullptr; |
| 110 | bool initialized_ = false; |
| 111 | bool streaming_ = false; |
| 112 | |
| 113 | int width_ = 0; |
| 114 | int height_ = 0; |
| 115 | int framerate_ = 0; |
| 116 | int bitrate_bps_ = 0; |
| 117 | int keyframe_interval_ = 0; |
| 118 | |
| 119 | int output_buffer_count_ = 0; |
| 120 | int capture_buffer_count_ = 0; |
| 121 | int next_output_index_ = 0; |
| 122 | int output_y_stride_ = 0; |
| 123 | int output_u_stride_ = 0; |
| 124 | int output_v_stride_ = 0; |
| 125 | bool output_is_nv12_ = false; |
| 126 | bool use_dmabuf_input_ = false; |
| 127 | bool dmabuf_planes_setup_ = false; |
| 128 | |
| 129 | // Cached NvBufSurface plane metadata for DmaBuf encode path. |
| 130 | // Populated on the first QueueOutputBufferDmaBuf call to avoid |
| 131 | // repeated NvBufSurfaceFromFd lookups (which trigger "Wrong buffer |
| 132 | // index" warnings on some JetPack versions). |
| 133 | bool dmabuf_meta_cached_ = false; |
| 134 | uint32_t dmabuf_num_planes_ = 0; |
| 135 | uint32_t dmabuf_plane_bytesused_[VIDEO_MAX_PLANES] = {}; |
| 136 | |
| 137 | // Whether NvBufSurfaceSyncForDevice works for MMAP plane fds. |
| 138 | // Set to false on the first failure so we stop attempting (and |
| 139 | // spamming) on JetPack versions where MMAP fds aren't recognised |
| 140 | // by the NvBufSurface API. V4L2 MMAP + qBuffer handles cache |
| 141 | // coherency on its own, so skipping the explicit sync is safe. |
| 142 | bool mmap_sync_supported_ = true; |
| 143 | }; |
| 144 | |
| 145 | } // namespace livekit |
| 146 | |
| 147 | #endif // LIVEKIT_JETSON_MMAPI_ENCODER_H_ |