blob: 25ef3e77bfa55a83f9bdf3d0770c3233653fc265 [file] [log] [blame]
sprang429600d2017-01-26 06:12:26 -08001/*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/include/video_codec_initializer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <stddef.h>
14#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <memory>
17
18#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010019#include "api/scoped_refptr.h"
Elad Alon45befc52019-07-02 11:20:09 +020020#include "api/test/mock_fec_controller_override.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080021#include "api/video/builtin_video_bitrate_allocator_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "api/video/video_bitrate_allocation.h"
23#include "api/video/video_bitrate_allocator.h"
24#include "api/video/video_bitrate_allocator_factory.h"
Sergey Silkin86684962018-03-28 19:32:37 +020025#include "api/video_codecs/video_encoder.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020026#include "api/video_codecs/vp8_temporal_layers.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010027#include "api/video_codecs/vp8_temporal_layers_factory.h"
Sergey Silkin86684962018-03-28 19:32:37 +020028#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080029#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/ref_counted_object.h"
Elad Alon45befc52019-07-02 11:20:09 +020031#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "test/gtest.h"
sprang429600d2017-01-26 06:12:26 -080033
34namespace webrtc {
35
36namespace {
sprang429600d2017-01-26 06:12:26 -080037static const int kDefaultWidth = 1280;
38static const int kDefaultHeight = 720;
39static const int kDefaultFrameRate = 30;
40static const uint32_t kDefaultMinBitrateBps = 60000;
41static const uint32_t kDefaultTargetBitrateBps = 2000000;
42static const uint32_t kDefaultMaxBitrateBps = 2000000;
43static const uint32_t kDefaultMinTransmitBitrateBps = 400000;
44static const int kDefaultMaxQp = 48;
Erik Språng5e898d62018-07-06 16:32:20 +020045static const uint32_t kScreenshareTl0BitrateBps = 200000;
sprang429600d2017-01-26 06:12:26 -080046static const uint32_t kScreenshareCodecTargetBitrateBps = 200000;
47static const uint32_t kScreenshareDefaultFramerate = 5;
48// Bitrates for the temporal layers of the higher screenshare simulcast stream.
49static const uint32_t kHighScreenshareTl0Bps = 800000;
50static const uint32_t kHighScreenshareTl1Bps = 1200000;
51} // namespace
52
sprang429600d2017-01-26 06:12:26 -080053// TODO(sprang): Extend coverage to handle the rest of the codec initializer.
54class VideoCodecInitializerTest : public ::testing::Test {
55 public:
Niels Möllerf1338562018-04-26 09:51:47 +020056 VideoCodecInitializerTest() {}
sprang429600d2017-01-26 06:12:26 -080057 virtual ~VideoCodecInitializerTest() {}
58
59 protected:
60 void SetUpFor(VideoCodecType type,
61 int num_spatial_streams,
62 int num_temporal_streams,
63 bool screenshare) {
64 config_ = VideoEncoderConfig();
Niels Möller259a4972018-04-05 15:36:51 +020065 config_.codec_type = type;
66
sprang429600d2017-01-26 06:12:26 -080067 if (screenshare) {
68 config_.min_transmit_bitrate_bps = kDefaultMinTransmitBitrateBps;
69 config_.content_type = VideoEncoderConfig::ContentType::kScreen;
70 }
71
72 if (type == VideoCodecType::kVideoCodecVP8) {
73 config_.number_of_streams = num_spatial_streams;
74 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
75 vp8_settings.numberOfTemporalLayers = num_temporal_streams;
76 config_.encoder_specific_settings = new rtc::RefCountedObject<
77 webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
Sergey Silkin86684962018-03-28 19:32:37 +020078 } else if (type == VideoCodecType::kVideoCodecVP9) {
79 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
80 vp9_settings.numberOfSpatialLayers = num_spatial_streams;
81 vp9_settings.numberOfTemporalLayers = num_temporal_streams;
82 config_.encoder_specific_settings = new rtc::RefCountedObject<
83 webrtc::VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
84 } else if (type != VideoCodecType::kVideoCodecMultiplex) {
sprang429600d2017-01-26 06:12:26 -080085 ADD_FAILURE() << "Unexpected codec type: " << type;
86 }
87 }
88
89 bool InitializeCodec() {
90 codec_out_ = VideoCodec();
Elad Aloncde8ab22019-03-20 11:56:20 +010091 frame_buffer_controller_.reset();
Jiawei Ouc2ebe212018-11-08 10:02:56 -080092 if (!VideoCodecInitializer::SetupCodec(config_, streams_, &codec_out_)) {
sprang429600d2017-01-26 06:12:26 -080093 return false;
94 }
Jiawei Ouc2ebe212018-11-08 10:02:56 -080095 bitrate_allocator_ = CreateBuiltinVideoBitrateAllocatorFactory()
96 ->CreateVideoBitrateAllocator(codec_out_);
97 RTC_CHECK(bitrate_allocator_);
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080098 if (codec_out_.codecType == VideoCodecType::kVideoCodecMultiplex)
Emircan Uysaler0a375472017-12-11 12:21:02 +053099 return true;
Erik Språng82fad3d2018-03-21 09:57:23 +0100100
sprang429600d2017-01-26 06:12:26 -0800101 // Make sure temporal layers instances have been created.
102 if (codec_out_.codecType == VideoCodecType::kVideoCodecVP8) {
Elad Aloncde8ab22019-03-20 11:56:20 +0100103 Vp8TemporalLayersFactory factory;
Elad Alona2795842019-06-07 23:10:00 +0200104 const VideoEncoder::Settings settings(VideoEncoder::Capabilities(false),
105 1, 1000);
Elad Alon45befc52019-07-02 11:20:09 +0200106 frame_buffer_controller_ =
107 factory.Create(codec_out_, settings, &fec_controller_override_);
sprang429600d2017-01-26 06:12:26 -0800108 }
109 return true;
110 }
111
112 VideoStream DefaultStream() {
113 VideoStream stream;
114 stream.width = kDefaultWidth;
115 stream.height = kDefaultHeight;
116 stream.max_framerate = kDefaultFrameRate;
117 stream.min_bitrate_bps = kDefaultMinBitrateBps;
118 stream.target_bitrate_bps = kDefaultTargetBitrateBps;
119 stream.max_bitrate_bps = kDefaultMaxBitrateBps;
120 stream.max_qp = kDefaultMaxQp;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100121 stream.num_temporal_layers = 1;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800122 stream.active = true;
sprang429600d2017-01-26 06:12:26 -0800123 return stream;
124 }
125
126 VideoStream DefaultScreenshareStream() {
127 VideoStream stream = DefaultStream();
128 stream.min_bitrate_bps = 30000;
129 stream.target_bitrate_bps = kScreenshareTl0BitrateBps;
130 stream.max_bitrate_bps = 1000000;
131 stream.max_framerate = kScreenshareDefaultFramerate;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100132 stream.num_temporal_layers = 2;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800133 stream.active = true;
sprang429600d2017-01-26 06:12:26 -0800134 return stream;
135 }
136
Elad Alon45befc52019-07-02 11:20:09 +0200137 MockFecControllerOverride fec_controller_override_;
138
sprang429600d2017-01-26 06:12:26 -0800139 // Input settings.
140 VideoEncoderConfig config_;
sprang429600d2017-01-26 06:12:26 -0800141 std::vector<VideoStream> streams_;
sprang429600d2017-01-26 06:12:26 -0800142
143 // Output.
144 VideoCodec codec_out_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800145 std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
Elad Aloncde8ab22019-03-20 11:56:20 +0100146 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_;
sprang429600d2017-01-26 06:12:26 -0800147};
148
149TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) {
150 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
151 streams_.push_back(DefaultStream());
152 EXPECT_TRUE(InitializeCodec());
153
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800154 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
155 kDefaultTargetBitrateBps, kDefaultFrameRate);
sprang429600d2017-01-26 06:12:26 -0800156 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
157 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
158 EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps());
159}
160
Seth Hampson46e31ba2018-01-18 10:39:54 -0800161TEST_F(VideoCodecInitializerTest, SingleStreamVp8ScreenshareInactive) {
162 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
163 VideoStream inactive_stream = DefaultStream();
164 inactive_stream.active = false;
165 streams_.push_back(inactive_stream);
166 EXPECT_TRUE(InitializeCodec());
167
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800168 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
169 kDefaultTargetBitrateBps, kDefaultFrameRate);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800170 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
171 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
172 EXPECT_EQ(0U, bitrate_allocation.get_sum_bps());
173}
174
sprang429600d2017-01-26 06:12:26 -0800175TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) {
176 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true);
177 streams_.push_back(DefaultScreenshareStream());
178 EXPECT_TRUE(InitializeCodec());
179
180 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
181 EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers);
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800182 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
183 kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate);
sprang429600d2017-01-26 06:12:26 -0800184 EXPECT_EQ(kScreenshareCodecTargetBitrateBps,
185 bitrate_allocation.get_sum_bps());
186 EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0));
187}
188
Seth Hampson46e31ba2018-01-18 10:39:54 -0800189TEST_F(VideoCodecInitializerTest, SimulcastVp8Screenshare) {
sprang429600d2017-01-26 06:12:26 -0800190 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
191 streams_.push_back(DefaultScreenshareStream());
192 VideoStream video_stream = DefaultStream();
193 video_stream.max_framerate = kScreenshareDefaultFramerate;
194 streams_.push_back(video_stream);
195 EXPECT_TRUE(InitializeCodec());
196
197 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
198 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
199 const uint32_t max_bitrate_bps =
200 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800201 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
202 max_bitrate_bps, kScreenshareDefaultFramerate);
sprang429600d2017-01-26 06:12:26 -0800203 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
204 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
205 bitrate_allocation.GetSpatialLayerSum(0));
206 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
207 bitrate_allocation.GetSpatialLayerSum(1));
208}
209
Seth Hampson46e31ba2018-01-18 10:39:54 -0800210// Tests that when a video stream is inactive, then the bitrate allocation will
211// be 0 for that stream.
212TEST_F(VideoCodecInitializerTest, SimulcastVp8ScreenshareInactive) {
213 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
214 streams_.push_back(DefaultScreenshareStream());
215 VideoStream inactive_video_stream = DefaultStream();
216 inactive_video_stream.active = false;
217 inactive_video_stream.max_framerate = kScreenshareDefaultFramerate;
218 streams_.push_back(inactive_video_stream);
219 EXPECT_TRUE(InitializeCodec());
220
221 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
222 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
223 const uint32_t target_bitrate =
224 streams_[0].target_bitrate_bps + streams_[1].target_bitrate_bps;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800225 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
226 target_bitrate, kScreenshareDefaultFramerate);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800227 EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
228 bitrate_allocation.get_sum_bps());
229 EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
230 bitrate_allocation.GetSpatialLayerSum(0));
231 EXPECT_EQ(0U, bitrate_allocation.GetSpatialLayerSum(1));
232}
233
234TEST_F(VideoCodecInitializerTest, HighFpsSimulcastVp8Screenshare) {
sprang429600d2017-01-26 06:12:26 -0800235 // Two simulcast streams, the lower one using legacy settings (two temporal
236 // streams, 5fps), the higher one using 3 temporal streams and 30fps.
237 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true);
238 streams_.push_back(DefaultScreenshareStream());
239 VideoStream video_stream = DefaultStream();
Sergey Silkina796a7e2018-03-01 15:11:29 +0100240 video_stream.num_temporal_layers = 3;
sprang429600d2017-01-26 06:12:26 -0800241 streams_.push_back(video_stream);
242 EXPECT_TRUE(InitializeCodec());
243
244 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
245 EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers);
246 const uint32_t max_bitrate_bps =
247 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
Erik Språng566124a2018-04-23 12:32:22 +0200248 VideoBitrateAllocation bitrate_allocation =
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800249 bitrate_allocator_->GetAllocation(max_bitrate_bps, kDefaultFrameRate);
sprang429600d2017-01-26 06:12:26 -0800250 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
251 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
252 bitrate_allocation.GetSpatialLayerSum(0));
253 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
254 bitrate_allocation.GetSpatialLayerSum(1));
255 EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0));
256 EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps,
257 bitrate_allocation.GetBitrate(1, 1));
258}
259
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -0800260TEST_F(VideoCodecInitializerTest, SingleStreamMultiplexCodec) {
261 SetUpFor(VideoCodecType::kVideoCodecMultiplex, 1, 1, true);
Emircan Uysaler0a375472017-12-11 12:21:02 +0530262 streams_.push_back(DefaultStream());
263 EXPECT_TRUE(InitializeCodec());
264}
265
Sergey Silkin86684962018-03-28 19:32:37 +0200266TEST_F(VideoCodecInitializerTest, Vp9SvcDefaultLayering) {
267 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
268 VideoStream stream = DefaultStream();
269 stream.num_temporal_layers = 3;
270 streams_.push_back(stream);
271
272 EXPECT_TRUE(InitializeCodec());
273 EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3u);
274 EXPECT_EQ(codec_out_.VP9()->numberOfTemporalLayers, 3u);
275}
276
277TEST_F(VideoCodecInitializerTest, Vp9SvcAdjustedLayering) {
278 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
279 VideoStream stream = DefaultStream();
280 stream.num_temporal_layers = 3;
281 // Set resolution which is only enough to produce 2 spatial layers.
282 stream.width = kMinVp9SpatialLayerWidth * 2;
283 stream.height = kMinVp9SpatialLayerHeight * 2;
284
285 streams_.push_back(stream);
286
287 EXPECT_TRUE(InitializeCodec());
288 EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 2u);
289}
290
Sergey Silkinfafeac32018-04-13 16:36:39 +0200291TEST_F(VideoCodecInitializerTest,
292 Vp9SingleSpatialLayerMaxBitrateIsEqualToCodecMaxBitrate) {
293 SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 3, false);
294 VideoStream stream = DefaultStream();
295 stream.num_temporal_layers = 3;
296 streams_.push_back(stream);
297
298 EXPECT_TRUE(InitializeCodec());
299 EXPECT_EQ(codec_out_.spatialLayers[0].maxBitrate,
300 kDefaultMaxBitrateBps / 1000);
301}
302
Sergey Silkin33120922018-11-28 13:32:13 +0100303TEST_F(VideoCodecInitializerTest,
Ilya Nikolaevskiy9ef5e052019-03-05 10:08:35 +0100304 Vp9SingleSpatialLayerTargetBitrateIsEqualToCodecMaxBitrate) {
305 SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 1, true);
306 VideoStream stream = DefaultStream();
307 stream.num_temporal_layers = 1;
308 streams_.push_back(stream);
309
310 EXPECT_TRUE(InitializeCodec());
311 EXPECT_EQ(codec_out_.spatialLayers[0].targetBitrate,
312 kDefaultMaxBitrateBps / 1000);
313}
314
315TEST_F(VideoCodecInitializerTest,
Sergey Silkin33120922018-11-28 13:32:13 +0100316 Vp9KeepBitrateLimitsIfNumberOfSpatialLayersIsReducedToOne) {
317 // Request 3 spatial layers for 320x180 input. Actual number of layers will be
318 // reduced to 1 due to low input resolution but SVC bitrate limits should be
319 // applied.
320 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
321 VideoStream stream = DefaultStream();
322 stream.width = 320;
323 stream.height = 180;
324 stream.num_temporal_layers = 3;
325 streams_.push_back(stream);
326
327 EXPECT_TRUE(InitializeCodec());
328 EXPECT_LT(codec_out_.spatialLayers[0].maxBitrate,
329 kDefaultMaxBitrateBps / 1000);
330}
331
Sergey Silkin8b9b5f92018-12-10 09:28:53 +0100332TEST_F(VideoCodecInitializerTest, Vp9DeactivateLayers) {
333 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 1, false);
334 VideoStream stream = DefaultStream();
335 streams_.push_back(stream);
336
337 config_.simulcast_layers.resize(3);
338
339 // Activate all layers.
340 config_.simulcast_layers[0].active = true;
341 config_.simulcast_layers[1].active = true;
342 config_.simulcast_layers[2].active = true;
343 EXPECT_TRUE(InitializeCodec());
344 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
345 EXPECT_TRUE(codec_out_.spatialLayers[1].active);
346 EXPECT_TRUE(codec_out_.spatialLayers[2].active);
347
348 // Deactivate top layer.
349 config_.simulcast_layers[0].active = false;
350 EXPECT_TRUE(InitializeCodec());
351 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
352 EXPECT_TRUE(codec_out_.spatialLayers[1].active);
353 EXPECT_FALSE(codec_out_.spatialLayers[2].active);
354
355 // Deactivate middle layer.
356 config_.simulcast_layers[0].active = true;
357 config_.simulcast_layers[1].active = false;
358 EXPECT_TRUE(InitializeCodec());
359 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
360 EXPECT_FALSE(codec_out_.spatialLayers[1].active);
361 EXPECT_TRUE(codec_out_.spatialLayers[2].active);
362}
363
sprang429600d2017-01-26 06:12:26 -0800364} // namespace webrtc