BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / nvidia / h264_decoder_impl.cpp

h264_decoder_impl.cpp

h264_decoder_impl.cpp 6.2 KiB Download Raw
1#include "h264_decoder_impl.h"
2
3#include <api/video/i420_buffer.h>
4#include <api/video/video_codec_type.h>
5#include <modules/video_coding/include/video_error_codes.h>
6#include <third_party/libyuv/include/libyuv/convert.h>
7
8#include "NvDecoder/NvDecoder.h"
9#include "Utils/NvCodecUtils.h"
10#include "rtc_base/checks.h"
11#include "rtc_base/logging.h"
12
13namespace webrtc {
14
15ColorSpace ExtractH264ColorSpace(const CUVIDEOFORMAT& format) {
16 return ColorSpace(
17 static_cast<ColorSpace::PrimaryID>(
18 format.video_signal_description.color_primaries),
19 static_cast<ColorSpace::TransferID>(
20 format.video_signal_description.transfer_characteristics),
21 static_cast<ColorSpace::MatrixID>(
22 format.video_signal_description.matrix_coefficients),
23 static_cast<ColorSpace::RangeID>(
24 format.video_signal_description.video_full_range_flag));
25}
26
27NvidiaH264DecoderImpl::NvidiaH264DecoderImpl(CUcontext context)
28 : cu_context_(context),
29 decoder_(nullptr),
30 is_configured_decoder_(false),
31 decoded_complete_callback_(nullptr),
32 buffer_pool_(false) {}
33
34NvidiaH264DecoderImpl::~NvidiaH264DecoderImpl() {
35 Release();
36}
37
38VideoDecoder::DecoderInfo NvidiaH264DecoderImpl::GetDecoderInfo() const {
39 VideoDecoder::DecoderInfo info;
40 info.implementation_name = "NVIDIA H264 Decoder";
41 info.is_hardware_accelerated = true;
42 return info;
43}
44
45bool NvidiaH264DecoderImpl::Configure(const Settings& settings) {
46 if (settings.codec_type() != kVideoCodecH264) {
47 RTC_LOG(LS_ERROR)
48 << "initialization failed on codectype is not kVideoCodecH264";
49 return false;
50 }
51 if (!settings.max_render_resolution().Valid()) {
52 RTC_LOG(LS_ERROR)
53 << "initialization failed on codec_settings width < 0 or height < 0";
54 return false;
55 }
56
57 settings_ = settings;
58
59 const CUresult result = cuCtxSetCurrent(cu_context_);
60 if (!ck(result)) {
61 RTC_LOG(LS_ERROR) << "initialization failed on cuCtxSetCurrent result"
62 << result;
63 return false;
64 }
65
66 // todo(kazuki): Max resolution is differred each architecture.
67 // Refer to the table in Video Decoder Capabilities.
68 // https://docs.nvidia.com/video-technologies/video-codec-sdk/nvdec-video-decoder-api-prog-guide
69 int maxWidth = 4096;
70 int maxHeight = 4096;
71
72 // bUseDeviceFrame: allocate in memory or cuda device memory
73 decoder_ = std::make_unique<NvDecoder>(
74 cu_context_, false, cudaVideoCodec_H264, true, false, nullptr, nullptr,
75 false, maxWidth, maxHeight);
76 return true;
77}
78
79int32_t NvidiaH264DecoderImpl::RegisterDecodeCompleteCallback(
80 DecodedImageCallback* callback) {
81 this->decoded_complete_callback_ = callback;
82 return WEBRTC_VIDEO_CODEC_OK;
83}
84
85int32_t NvidiaH264DecoderImpl::Release() {
86 buffer_pool_.Release();
87 return WEBRTC_VIDEO_CODEC_OK;
88}
89
90int32_t NvidiaH264DecoderImpl::Decode(const EncodedImage& input_image,
91 bool missing_frames,
92 int64_t render_time_ms) {
93 CUcontext current;
94 if (!ck(cuCtxGetCurrent(&current))) {
95 RTC_LOG(LS_ERROR) << "decode failed on cuCtxGetCurrent is failed";
96 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
97 }
98 if (current != cu_context_) {
99 RTC_LOG(LS_ERROR)
100 << "decode failed on not match current context and hold context";
101 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
102 }
103 if (decoded_complete_callback_ == nullptr) {
104 RTC_LOG(LS_ERROR) << "decode failed on not set m_decodedCompleteCallback";
105 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
106 }
107 if (!input_image.data() || !input_image.size()) {
108 RTC_LOG(LS_ERROR) << "decode failed on input image is null";
109 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
110 }
111
112 h264_bitstream_parser_.ParseBitstream(input_image);
113 std::optional<int> qp = h264_bitstream_parser_.GetLastSliceQp();
114 std::optional<SpsParser::SpsState> sps = h264_bitstream_parser_.sps();
115
116 if (is_configured_decoder_) {
117 if (!sps ||
118 sps.value().width != static_cast<uint32_t>(decoder_->GetWidth()) ||
119 sps.value().height != static_cast<uint32_t>(decoder_->GetHeight())) {
120 decoder_->setReconfigParams(nullptr, nullptr);
121 }
122 }
123
124 int nFrameReturnd = 0;
125 do {
126 nFrameReturnd = decoder_->Decode(
127 input_image.data(), static_cast<int>(input_image.size()),
128 CUVID_PKT_TIMESTAMP, input_image.RtpTimestamp());
129 } while (nFrameReturnd == 0);
130
131 is_configured_decoder_ = true;
132
133 // todo: support other output format
134 // Chromium's H264 Encoder is output on NV12, so currently only NV12 is
135 // supported.
136 if (decoder_->GetOutputFormat() != cudaVideoSurfaceFormat_NV12) {
137 RTC_LOG(LS_ERROR) << "not supported this format: "
138 << decoder_->GetOutputFormat();
139 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
140 }
141
142 // Pass on color space from input frame if explicitly specified.
143 const ColorSpace& color_space =
144 input_image.ColorSpace()
145 ? *input_image.ColorSpace()
146 : ExtractH264ColorSpace(decoder_->GetVideoFormatInfo());
147
148 for (int i = 0; i < nFrameReturnd; i++) {
149 int64_t timeStamp;
150 uint8_t* pFrame = decoder_->GetFrame(&timeStamp);
151
152 webrtc::scoped_refptr<webrtc::I420Buffer> i420_buffer =
153 buffer_pool_.CreateI420Buffer(decoder_->GetWidth(),
154 decoder_->GetHeight());
155
156 int result;
157 {
158 result = libyuv::NV12ToI420(
159 pFrame, decoder_->GetDeviceFramePitch(),
160 pFrame + decoder_->GetHeight() * decoder_->GetDeviceFramePitch(),
161 decoder_->GetDeviceFramePitch(), i420_buffer->MutableDataY(),
162 i420_buffer->StrideY(), i420_buffer->MutableDataU(),
163 i420_buffer->StrideU(), i420_buffer->MutableDataV(),
164 i420_buffer->StrideV(), decoder_->GetWidth(), decoder_->GetHeight());
165 }
166
167 if (result) {
168 RTC_LOG(LS_INFO) << "libyuv::NV12ToI420 failed. error:" << result;
169 }
170
171 VideoFrame decoded_frame =
172 VideoFrame::Builder()
173 .set_video_frame_buffer(i420_buffer)
174 .set_timestamp_rtp(static_cast<uint32_t>(timeStamp))
175 .set_color_space(color_space)
176 .build();
177
178 // todo: measurement decoding time
179 std::optional<int32_t> decodetime;
180 decoded_complete_callback_->Decoded(decoded_frame, decodetime, qp);
181 }
182
183 return WEBRTC_VIDEO_CODEC_OK;
184}
185
186} // end namespace webrtc