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#include "modules/video_coding/svc/scalable_video_controller.h"
31
35
36namespace cuttlefish {
37
38struct CudaFunctions;
39
41 const NV_ENCODE_API_FUNCTION_LIST* funcs = nullptr;
42 void operator()(void* encoder) const;
43};
44
45using NvencEncoderHandle = std::unique_ptr<void, NvencEncoderDeleter>;
46
47// Hardware-accelerated video encoder using NVIDIA NVENC.
48//
49// Accepts native ARGB/ABGR buffers and uses NVENC's internal RGB-to-YUV
50// conversion. CUDA and NVENC libraries are loaded at runtime via dlopen,
51// so this links without any CUDA shared libraries present.
52class NvencVideoEncoder : public webrtc::VideoEncoder {
53 public:
55 const webrtc::SdpVideoFormat& format);
56 ~NvencVideoEncoder() override;
57
58 int InitEncode(const webrtc::VideoCodec* codec_settings,
59 const webrtc::VideoEncoder::Settings& settings) override;
61 webrtc::EncodedImageCallback* callback) override;
62 int32_t Release() override;
63 int32_t Encode(
64 const webrtc::VideoFrame& frame,
65 const std::vector<webrtc::VideoFrameType>* frame_types) override;
66 void SetRates(const RateControlParameters& parameters) override;
67 EncoderInfo GetEncoderInfo() const override;
68
69 private:
72 Result<void> RegisterInputResource(uint32_t pixel_format);
74 const webrtc::VideoFrame& frame,
75 const std::vector<webrtc::VideoFrameType>* frame_types);
76 void DestroyEncoder();
77 void Reconfigure(const RateControlParameters& parameters);
78
80 webrtc::SdpVideoFormat format_;
81 webrtc::VideoCodec codec_settings_;
82 int32_t bitrate_bps_ = 0;
83 uint32_t framerate_ = 30;
84 bool inited_ = false;
85
86 // Dynamically loaded CUDA functions
87 const CudaFunctions* cuda_ = nullptr;
88
89 // CUDA context
90 std::shared_ptr<CudaContext> shared_context_;
91 CUcontext cuda_context_ = nullptr;
92 CUstream cuda_stream_ = nullptr;
93
94 // GPU buffer for RGBA input
95 CUdeviceptr d_rgba_buffer_ = 0;
96 NV_ENC_INPUT_PTR nvenc_input_buffer_ = nullptr;
97 NV_ENC_OUTPUT_PTR nvenc_output_bitstream_ = nullptr;
98
99 // Input resource registration (lazy, on first frame)
102 NV_ENC_BUFFER_FORMAT nvenc_input_format_ = NV_ENC_BUFFER_FORMAT_UNDEFINED;
103
104 // NVENC encoder session
106 const NV_ENCODE_API_FUNCTION_LIST* nvenc_funcs_ = nullptr;
107
108 // Stored initialization params for reconfiguration
109 NV_ENC_INITIALIZE_PARAMS stored_init_params_{};
110 NV_ENC_CONFIG stored_encode_config_{};
111
112 // Frame dimensions
113 int width_ = 0;
114 int height_ = 0;
115 size_t rgba_pitch_ = 0;
116
117 std::unique_ptr<webrtc::ScalableVideoController> svc_controller_;
118 webrtc::EncodedImageCallback* callback_ = nullptr;
119};
120
121} // namespace cuttlefish
Definition: nvenc_video_encoder.h:52
Result< void > InitCuda()
Definition: nvenc_video_encoder.cpp:178
int32_t Encode(const webrtc::VideoFrame &frame, const std::vector< webrtc::VideoFrameType > *frame_types) override
Definition: nvenc_video_encoder.cpp:390
NvencEncoderConfig config_
Definition: nvenc_video_encoder.h:79
~NvencVideoEncoder() override
Definition: nvenc_video_encoder.cpp:65
NvencVideoEncoder(const NvencEncoderConfig &config, const webrtc::SdpVideoFormat &format)
Definition: nvenc_video_encoder.cpp:58
std::unique_ptr< webrtc::ScalableVideoController > svc_controller_
Definition: nvenc_video_encoder.h:117
bool inited_
Definition: nvenc_video_encoder.h:84
size_t rgba_pitch_
Definition: nvenc_video_encoder.h:115
NV_ENC_OUTPUT_PTR nvenc_output_bitstream_
Definition: nvenc_video_encoder.h:97
NV_ENC_CONFIG stored_encode_config_
Definition: nvenc_video_encoder.h:110
int width_
Definition: nvenc_video_encoder.h:113
EncoderInfo GetEncoderInfo() const override
Definition: nvenc_video_encoder.cpp:571
Result< void > InitNvenc()
Definition: nvenc_video_encoder.cpp:202
NV_ENC_INITIALIZE_PARAMS stored_init_params_
Definition: nvenc_video_encoder.h:109
void SetRates(const RateControlParameters &parameters) override
Definition: nvenc_video_encoder.cpp:324
CUcontext cuda_context_
Definition: nvenc_video_encoder.h:91
int32_t RegisterEncodeCompleteCallback(webrtc::EncodedImageCallback *callback) override
Definition: nvenc_video_encoder.cpp:318
CUdeviceptr d_rgba_buffer_
Definition: nvenc_video_encoder.h:95
CUstream cuda_stream_
Definition: nvenc_video_encoder.h:92
std::shared_ptr< CudaContext > shared_context_
Definition: nvenc_video_encoder.h:90
int32_t bitrate_bps_
Definition: nvenc_video_encoder.h:82
void Reconfigure(const RateControlParameters &parameters)
Definition: nvenc_video_encoder.cpp:350
int32_t Release() override
Definition: nvenc_video_encoder.cpp:67
const NV_ENCODE_API_FUNCTION_LIST * nvenc_funcs_
Definition: nvenc_video_encoder.h:106
bool input_resource_registered_
Definition: nvenc_video_encoder.h:100
const CudaFunctions * cuda_
Definition: nvenc_video_encoder.h:87
webrtc::VideoCodec codec_settings_
Definition: nvenc_video_encoder.h:81
NV_ENC_BUFFER_FORMAT nvenc_input_format_
Definition: nvenc_video_encoder.h:102
NvencEncoderHandle encoder_
Definition: nvenc_video_encoder.h:105
webrtc::EncodedImageCallback * callback_
Definition: nvenc_video_encoder.h:118
int InitEncode(const webrtc::VideoCodec *codec_settings, const webrtc::VideoEncoder::Settings &settings) override
Definition: nvenc_video_encoder.cpp:121
int height_
Definition: nvenc_video_encoder.h:114
uint32_t framerate_
Definition: nvenc_video_encoder.h:83
uint32_t registered_pixel_format_
Definition: nvenc_video_encoder.h:101
void DestroyEncoder()
Definition: nvenc_video_encoder.cpp:72
Result< void > EncodeInner(const webrtc::VideoFrame &frame, const std::vector< webrtc::VideoFrameType > *frame_types)
Definition: nvenc_video_encoder.cpp:405
webrtc::SdpVideoFormat format_
Definition: nvenc_video_encoder.h:80
Result< void > RegisterInputResource(uint32_t pixel_format)
Definition: nvenc_video_encoder.cpp:282
NV_ENC_INPUT_PTR nvenc_input_buffer_
Definition: nvenc_video_encoder.h:96
EventFormat format
Definition: kernel_log_server.cc:61
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
std::unique_ptr< void, NvencEncoderDeleter > NvencEncoderHandle
Definition: nvenc_video_encoder.h:45
Definition: cuda_loader.h:28
Definition: nvenc_encoder_config.h:37
Definition: nvenc_video_encoder.h:40
const NV_ENCODE_API_FUNCTION_LIST * funcs
Definition: nvenc_video_encoder.h:41
void operator()(void *encoder) const
Definition: nvenc_video_encoder.cpp:38