Android-cuttlefish cvd tool
nvenc_video_encoder.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 The Android Open Source Project
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 <cuda.h>
20#include <nvEncodeAPI.h>
21
22#include <memory>
23#include <vector>
24
25#include "api/video/video_codec_type.h"
26#include "api/video/video_frame.h"
27#include "api/video_codecs/sdp_video_format.h"
28#include "api/video_codecs/video_encoder.h"
29#include "modules/video_coding/include/video_codec_interface.h"
30
34
35namespace cuttlefish {
36
37struct CudaFunctions;
38
40 const NV_ENCODE_API_FUNCTION_LIST* funcs = nullptr;
41 void operator()(void* encoder) const;
42};
43
44using NvencEncoderHandle = std::unique_ptr<void, NvencEncoderDeleter>;
45
46// Hardware-accelerated video encoder using NVIDIA NVENC.
47//
48// Accepts native ARGB/ABGR buffers and uses NVENC's internal RGB-to-YUV
49// conversion. CUDA and NVENC libraries are loaded at runtime via dlopen,
50// so this links without any CUDA shared libraries present.
51class NvencVideoEncoder : public webrtc::VideoEncoder {
52 public:
54 const webrtc::SdpVideoFormat& format);
55 ~NvencVideoEncoder() override;
56
57 int InitEncode(const webrtc::VideoCodec* codec_settings,
58 const webrtc::VideoEncoder::Settings& settings) override;
60 webrtc::EncodedImageCallback* callback) override;
61 int32_t Release() override;
62 int32_t Encode(
63 const webrtc::VideoFrame& frame,
64 const std::vector<webrtc::VideoFrameType>* frame_types) override;
65 void SetRates(const RateControlParameters& parameters) override;
66 EncoderInfo GetEncoderInfo() const override;
67
68 private:
71 Result<void> RegisterInputResource(uint32_t pixel_format);
73 const webrtc::VideoFrame& frame,
74 const std::vector<webrtc::VideoFrameType>* frame_types);
75 void DestroyEncoder();
76 void Reconfigure(const RateControlParameters& parameters);
77
79 webrtc::SdpVideoFormat format_;
80 webrtc::VideoCodec codec_settings_;
81 int32_t bitrate_bps_ = 0;
82 uint32_t framerate_ = 30;
83 bool inited_ = false;
84
85 // Dynamically loaded CUDA functions
86 const CudaFunctions* cuda_ = nullptr;
87
88 // CUDA context
89 std::shared_ptr<CudaContext> shared_context_;
90 CUcontext cuda_context_ = nullptr;
91 CUstream cuda_stream_ = nullptr;
92
93 // GPU buffer for RGBA input
94 CUdeviceptr d_rgba_buffer_ = 0;
95 NV_ENC_INPUT_PTR nvenc_input_buffer_ = nullptr;
96 NV_ENC_OUTPUT_PTR nvenc_output_bitstream_ = nullptr;
97
98 // Input resource registration (lazy, on first frame)
101 NV_ENC_BUFFER_FORMAT nvenc_input_format_ = NV_ENC_BUFFER_FORMAT_UNDEFINED;
102
103 // NVENC encoder session
105 const NV_ENCODE_API_FUNCTION_LIST* nvenc_funcs_ = nullptr;
106
107 // Stored initialization params for reconfiguration
108 NV_ENC_INITIALIZE_PARAMS stored_init_params_{};
109 NV_ENC_CONFIG stored_encode_config_{};
110
111 // Frame dimensions
112 int width_ = 0;
113 int height_ = 0;
114 size_t rgba_pitch_ = 0;
115
116 webrtc::EncodedImageCallback* callback_ = nullptr;
117};
118
119} // namespace cuttlefish
Definition: nvenc_video_encoder.h:51
Result< void > InitNvenc()
Definition: nvenc_video_encoder.cpp:173
int32_t Encode(const webrtc::VideoFrame &frame, const std::vector< webrtc::VideoFrameType > *frame_types) override
Definition: nvenc_video_encoder.cpp:342
NvencEncoderConfig config_
Definition: nvenc_video_encoder.h:78
Result< void > RegisterInputResource(uint32_t pixel_format)
Definition: nvenc_video_encoder.cpp:249
Result< void > InitCuda()
Definition: nvenc_video_encoder.cpp:149
~NvencVideoEncoder() override
Definition: nvenc_video_encoder.cpp:59
NvencVideoEncoder(const NvencEncoderConfig &config, const webrtc::SdpVideoFormat &format)
Definition: nvenc_video_encoder.cpp:52
bool inited_
Definition: nvenc_video_encoder.h:83
size_t rgba_pitch_
Definition: nvenc_video_encoder.h:114
NV_ENC_OUTPUT_PTR nvenc_output_bitstream_
Definition: nvenc_video_encoder.h:96
void DestroyEncoder()
Definition: nvenc_video_encoder.cpp:66
NV_ENC_CONFIG stored_encode_config_
Definition: nvenc_video_encoder.h:109
int width_
Definition: nvenc_video_encoder.h:112
EncoderInfo GetEncoderInfo() const override
Definition: nvenc_video_encoder.cpp:480
Result< void > EncodeInner(const webrtc::VideoFrame &frame, const std::vector< webrtc::VideoFrameType > *frame_types)
Definition: nvenc_video_encoder.cpp:357
NV_ENC_INITIALIZE_PARAMS stored_init_params_
Definition: nvenc_video_encoder.h:108
void SetRates(const RateControlParameters &parameters) override
Definition: nvenc_video_encoder.cpp:291
CUcontext cuda_context_
Definition: nvenc_video_encoder.h:90
int32_t RegisterEncodeCompleteCallback(webrtc::EncodedImageCallback *callback) override
Definition: nvenc_video_encoder.cpp:285
CUdeviceptr d_rgba_buffer_
Definition: nvenc_video_encoder.h:94
CUstream cuda_stream_
Definition: nvenc_video_encoder.h:91
std::shared_ptr< CudaContext > shared_context_
Definition: nvenc_video_encoder.h:89
int32_t bitrate_bps_
Definition: nvenc_video_encoder.h:81
int32_t Release() override
Definition: nvenc_video_encoder.cpp:61
const NV_ENCODE_API_FUNCTION_LIST * nvenc_funcs_
Definition: nvenc_video_encoder.h:105
bool input_resource_registered_
Definition: nvenc_video_encoder.h:99
void Reconfigure(const RateControlParameters &parameters)
Definition: nvenc_video_encoder.cpp:309
const CudaFunctions * cuda_
Definition: nvenc_video_encoder.h:86
webrtc::VideoCodec codec_settings_
Definition: nvenc_video_encoder.h:80
NV_ENC_BUFFER_FORMAT nvenc_input_format_
Definition: nvenc_video_encoder.h:101
NvencEncoderHandle encoder_
Definition: nvenc_video_encoder.h:104
webrtc::EncodedImageCallback * callback_
Definition: nvenc_video_encoder.h:116
int InitEncode(const webrtc::VideoCodec *codec_settings, const webrtc::VideoEncoder::Settings &settings) override
Definition: nvenc_video_encoder.cpp:102
int height_
Definition: nvenc_video_encoder.h:113
uint32_t framerate_
Definition: nvenc_video_encoder.h:82
uint32_t registered_pixel_format_
Definition: nvenc_video_encoder.h:100
webrtc::SdpVideoFormat format_
Definition: nvenc_video_encoder.h:79
NV_ENC_INPUT_PTR nvenc_input_buffer_
Definition: nvenc_video_encoder.h:95
EventFormat format
Definition: kernel_log_server.cc:60
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
std::unique_ptr< void, NvencEncoderDeleter > NvencEncoderHandle
Definition: nvenc_video_encoder.h:44
Definition: cuda_loader.h:28
Definition: nvenc_encoder_config.h:37
Definition: nvenc_video_encoder.h:39
const NV_ENCODE_API_FUNCTION_LIST * funcs
Definition: nvenc_video_encoder.h:40
void operator()(void *encoder) const
Definition: nvenc_video_encoder.cpp:34