-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathNvCodecTest.cpp
More file actions
331 lines (278 loc) · 13.6 KB
/
NvCodecTest.cpp
File metadata and controls
331 lines (278 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "pch.h"
#include "Codec/NvCodec/NvCodec.h"
#include "FrameGenerator.h"
#include "GraphicsDevice/IGraphicsDevice.h"
#include "GraphicsDeviceContainer.h"
#include "NvCodecUtils.h"
#include "VideoCodecTest.h"
#include <common_video/h264/h264_bitstream_parser.h>
#include <rtc_base/thread.h>
namespace unity
{
namespace webrtc
{
using testing::Values;
class NvCodecTest : public VideoCodecTest
{
public:
NvCodecTest()
: container_(CreateGraphicsDeviceContainer(GetParam()))
, device_(container_->device())
{
}
~NvCodecTest() override
{
if (encoder_)
encoder_ = nullptr;
if (decoder_)
decoder_ = nullptr;
}
void SetUp() override
{
if (!device_)
GTEST_SKIP() << "The graphics driver is not installed on the device.";
if (!device_->IsCudaSupport())
GTEST_SKIP() << "CUDA is not supported on this device.";
context_ = device_->GetCUcontext();
if (!NvEncoder::IsSupported(context_))
GTEST_SKIP() << "Current Driver Version does not support this NvEncodeAPI version.";
VideoCodecTest::SetUp();
}
protected:
std::unique_ptr<VideoEncoder> CreateEncoder() override
{
cricket::VideoCodec codec = cricket::CreateVideoCodec(cricket::kH264CodecName);
codec.SetParam(cricket::kH264FmtpProfileLevelId, kProfileLevelIdString());
return NvEncoder::Create(codec, context_, CU_MEMORYTYPE_ARRAY, NV_ENC_BUFFER_FORMAT_ARGB, nullptr);
}
std::unique_ptr<VideoDecoder> CreateDecoder() override
{
cricket::VideoCodec codec = cricket::CreateVideoCodec(cricket::kH264CodecName);
codec.SetParam(cricket::kH264FmtpProfileLevelId, kProfileLevelIdString());
return NvDecoder::Create(codec, context_, nullptr);
}
std::unique_ptr<FrameGeneratorInterface> CreateFrameGenerator(
int width,
int height,
absl::optional<FrameGeneratorInterface::OutputType> type,
absl::optional<int> num_squares) override
{
return CreateVideoFrameGenerator(device_, width, height, type, num_squares);
}
void ModifyCodecSettings(VideoCodec* codec_settings) override { SetDefaultSettings(codec_settings); }
void EncodeAndWaitForFrame(
const VideoFrame& inputFrame,
EncodedImage* encodedFrame,
CodecSpecificInfo* codec_specific_info,
bool keyframe = false)
{
std::vector<VideoFrameType> frame_types;
if (keyframe)
{
frame_types.emplace_back(VideoFrameType::kVideoFrameKey);
}
else
{
frame_types.emplace_back(VideoFrameType::kVideoFrameDelta);
}
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(inputFrame, &frame_types));
ASSERT_TRUE(WaitForEncodedFrame(encodedFrame, codec_specific_info));
VerifyQpParser(*encodedFrame);
EXPECT_EQ(kVideoCodecH264, codec_specific_info->codecType);
EXPECT_TRUE(encodedFrame->SimulcastIndex().has_value());
EXPECT_EQ(0, encodedFrame->SimulcastIndex());
}
void VerifyQpParser(const EncodedImage& encoded_frame)
{
EXPECT_GT(encoded_frame.size(), 0u);
bitstreamParser_.ParseBitstream(rtc::ArrayView<const uint8_t>(encoded_frame.data(), encoded_frame.size()));
int qp = bitstreamParser_.GetLastSliceQp().value_or(-1);
EXPECT_EQ(encoded_frame.qp_, qp) << "Encoder QP != parsed bitstream QP.";
}
CUdevice cudevice_;
CUcontext context_;
H264BitstreamParser bitstreamParser_;
std::unique_ptr<GraphicsDeviceContainer> container_;
IGraphicsDevice* device_;
};
TEST_P(NvCodecTest, SupportedNvEncoderCodecs)
{
std::vector<SdpVideoFormat> formats = SupportedNvEncoderCodecs(context_);
EXPECT_GT(formats.size(), 0);
}
TEST_P(NvCodecTest, SupportedNvDecoderCodecs)
{
std::vector<SdpVideoFormat> formats = SupportedNvDecoderCodecs(context_);
EXPECT_GT(formats.size(), 0);
}
TEST_P(NvCodecTest, SupportedEncoderCount) { EXPECT_GT(SupportedEncoderCount(context_), 0); }
TEST_P(NvCodecTest, SetRates)
{
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
const uint32_t kBitrateBps = 300000;
VideoBitrateAllocation bitrate_allocation;
bitrate_allocation.SetBitrate(0, 0, kBitrateBps);
// EXPECT_CALL(
// *vpx,
// codec_enc_config_set(
// _,
// AllOf(
// Field(&vpx_codec_enc_cfg_t::rc_target_bitrate, kBitrateBps / 1000),
// Field(&vpx_codec_enc_cfg_t::rc_undershoot_pct, 100u),
// Field(&vpx_codec_enc_cfg_t::rc_overshoot_pct, 15u),
// Field(&vpx_codec_enc_cfg_t::rc_buf_sz, 1000u),
// Field(&vpx_codec_enc_cfg_t::rc_buf_optimal_sz, 600u),
// Field(&vpx_codec_enc_cfg_t::rc_dropframe_thresh, 30u))))
// .WillOnce(Return(VPX_CODEC_OK));
encoder_->SetRates(
VideoEncoder::RateControlParameters(bitrate_allocation, static_cast<double>(codecSettings_.maxFramerate)));
}
TEST_P(NvCodecTest, EncodeFrameAndRelease)
{
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
EncodedImage encoded_frame;
CodecSpecificInfo codec_specific_info;
EncodeAndWaitForFrame(NextInputFrame(), &encoded_frame, &codec_specific_info);
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_UNINITIALIZED, encoder_->Encode(NextInputFrame(), nullptr));
}
TEST_P(NvCodecTest, EncodeOnWorkerThread)
{
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
EncodedImage encoded_frame;
CodecSpecificInfo codec_specific_info;
std::unique_ptr<rtc::Thread> thread = rtc::Thread::CreateWithSocketServer();
thread->Start();
// Test for executing command on several thread asyncnously.
int count = 100;
std::queue<VideoFrame> frames;
while (count)
{
frames.push(NextInputFrame());
thread->PostTask([&]() {
VideoFrame frame = frames.front();
EncodeAndWaitForFrame(frame, &encoded_frame, &codec_specific_info);
frames.pop();
});
count--;
}
thread->Stop();
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
}
TEST_P(NvCodecTest, EncodeDecode)
{
decoderSettings_.set_codec_type(VideoCodecType::kVideoCodecH264);
decoderSettings_.set_max_render_resolution({ codecSettings_.width, codecSettings_.height });
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Release());
EXPECT_TRUE(decoder_->Configure(decoderSettings_));
EncodedImage encoded_frame;
CodecSpecificInfo codec_specific_info;
EncodeAndWaitForFrame(NextInputFrame(), &encoded_frame, &codec_specific_info);
// First frame should be a key frame.
encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
std::unique_ptr<VideoFrame> decoded_frame;
absl::optional<uint8_t> decoded_qp;
ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
ASSERT_TRUE(decoded_frame);
// todo: set color space data on decode frame
const ColorSpace color_space = *decoded_frame->color_space();
EXPECT_EQ(ColorSpace::PrimaryID::kUnspecified, color_space.primaries());
EXPECT_EQ(ColorSpace::TransferID::kUnspecified, color_space.transfer());
EXPECT_EQ(ColorSpace::MatrixID::kUnspecified, color_space.matrix());
EXPECT_EQ(ColorSpace::RangeID::kInvalid, color_space.range());
EXPECT_EQ(ColorSpace::ChromaSiting::kUnspecified, color_space.chroma_siting_horizontal());
EXPECT_EQ(ColorSpace::ChromaSiting::kUnspecified, color_space.chroma_siting_vertical());
}
TEST_P(NvCodecTest, ReconfigureDecoder)
{
decoderSettings_.set_codec_type(VideoCodecType::kVideoCodecH264);
decoderSettings_.set_max_render_resolution({ codecSettings_.width, codecSettings_.height });
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Release());
EXPECT_TRUE(decoder_->Configure(decoderSettings_));
EncodedImage encoded_frame;
CodecSpecificInfo codec_specific_info;
VideoFrame frame = NextInputFrame();
EncodeAndWaitForFrame(frame, &encoded_frame, &codec_specific_info);
encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
std::unique_ptr<VideoFrame> decoded_frame;
absl::optional<uint8_t> decoded_qp;
ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
ASSERT_TRUE(decoded_frame);
EXPECT_EQ(decoded_frame->width(), frame.width());
EXPECT_EQ(decoded_frame->height(), frame.height());
// change resolution
uint16_t width = codecSettings_.width / 2;
uint16_t height = codecSettings_.height / 2;
codecSettings_.width = width;
codecSettings_.height = height;
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
ChangeFrameResolution(static_cast<size_t>(width), static_cast<size_t>(height));
VideoFrame frame2 = NextInputFrame();
EncodeAndWaitForFrame(frame2, &encoded_frame, &codec_specific_info);
encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 33));
ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
ASSERT_TRUE(decoded_frame);
// todo(kazuki): `pfnSequenceCallback` in NvEncoder.cpp is not called from NvDec when
// the first frame after changing resolution, so the resolution of the first frame is old one.
EXPECT_EQ(decoded_frame->width(), frame.width());
EXPECT_EQ(decoded_frame->height(), frame.height());
// The second frame after changing resolution is fine.
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 66));
ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
EXPECT_EQ(decoded_frame->width(), frame2.width());
EXPECT_EQ(decoded_frame->height(), frame2.height());
}
TEST_P(NvCodecTest, DecodedQpEqualsEncodedQp)
{
decoderSettings_.set_codec_type(VideoCodecType::kVideoCodecH264);
decoderSettings_.set_max_render_resolution({ codecSettings_.width, codecSettings_.height });
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Release());
EXPECT_TRUE(decoder_->Configure(decoderSettings_));
EncodedImage encoded_frame;
CodecSpecificInfo codec_specific_info;
EncodeAndWaitForFrame(NextInputFrame(), &encoded_frame, &codec_specific_info);
// First frame should be a key frame.
encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
std::unique_ptr<VideoFrame> decoded_frame;
absl::optional<uint8_t> decoded_qp;
ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
ASSERT_TRUE(decoded_frame);
ASSERT_TRUE(decoded_qp);
EXPECT_EQ(encoded_frame.qp_, *decoded_qp);
}
TEST_P(NvCodecTest, DecodedTimeStampEqualsEncodedTimeStamp)
{
decoderSettings_.set_codec_type(VideoCodecType::kVideoCodecH264);
decoderSettings_.set_max_render_resolution({ codecSettings_.width, codecSettings_.height });
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Release());
EXPECT_TRUE(decoder_->Configure(decoderSettings_));
EncodedImage encoded_frame;
CodecSpecificInfo codec_specific_info;
EncodeAndWaitForFrame(NextInputFrame(), &encoded_frame, &codec_specific_info);
// First frame should be a key frame.
encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
std::unique_ptr<VideoFrame> decoded_frame;
absl::optional<uint8_t> decoded_qp;
ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
ASSERT_TRUE(decoded_frame);
EXPECT_EQ(encoded_frame.Timestamp(), decoded_frame->timestamp());
}
INSTANTIATE_TEST_SUITE_P(GfxDevice, NvCodecTest, testing::ValuesIn(supportedGfxDevices));
}
}