blob: 09b01f6336d133297986e7e0e70c21fad871f8fb [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>
15#include <memory>
16
17#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080019#include "api/video/builtin_video_bitrate_allocator_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "api/video/video_bitrate_allocation.h"
21#include "api/video/video_bitrate_allocator.h"
22#include "api/video/video_bitrate_allocator_factory.h"
Sergey Silkin86684962018-03-28 19:32:37 +020023#include "api/video_codecs/video_encoder.h"
Erik Språng4529fbc2018-10-12 10:30:31 +020024#include "api/video_codecs/vp8_temporal_layers.h"
Elad Aloncde8ab22019-03-20 11:56:20 +010025#include "api/video_codecs/vp8_temporal_layers_factory.h"
Sergey Silkin86684962018-03-28 19:32:37 +020026#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080027#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "test/gtest.h"
sprang429600d2017-01-26 06:12:26 -080030
31namespace webrtc {
32
33namespace {
sprang429600d2017-01-26 06:12:26 -080034static const int kDefaultWidth = 1280;
35static const int kDefaultHeight = 720;
36static const int kDefaultFrameRate = 30;
37static const uint32_t kDefaultMinBitrateBps = 60000;
38static const uint32_t kDefaultTargetBitrateBps = 2000000;
39static const uint32_t kDefaultMaxBitrateBps = 2000000;
40static const uint32_t kDefaultMinTransmitBitrateBps = 400000;
41static const int kDefaultMaxQp = 48;
Erik Språng5e898d62018-07-06 16:32:20 +020042static const uint32_t kScreenshareTl0BitrateBps = 200000;
sprang429600d2017-01-26 06:12:26 -080043static const uint32_t kScreenshareCodecTargetBitrateBps = 200000;
44static const uint32_t kScreenshareDefaultFramerate = 5;
45// Bitrates for the temporal layers of the higher screenshare simulcast stream.
46static const uint32_t kHighScreenshareTl0Bps = 800000;
47static const uint32_t kHighScreenshareTl1Bps = 1200000;
48} // namespace
49
sprang429600d2017-01-26 06:12:26 -080050// TODO(sprang): Extend coverage to handle the rest of the codec initializer.
51class VideoCodecInitializerTest : public ::testing::Test {
52 public:
Niels Möllerf1338562018-04-26 09:51:47 +020053 VideoCodecInitializerTest() {}
sprang429600d2017-01-26 06:12:26 -080054 virtual ~VideoCodecInitializerTest() {}
55
56 protected:
57 void SetUpFor(VideoCodecType type,
58 int num_spatial_streams,
59 int num_temporal_streams,
60 bool screenshare) {
61 config_ = VideoEncoderConfig();
Niels Möller259a4972018-04-05 15:36:51 +020062 config_.codec_type = type;
63
sprang429600d2017-01-26 06:12:26 -080064 if (screenshare) {
65 config_.min_transmit_bitrate_bps = kDefaultMinTransmitBitrateBps;
66 config_.content_type = VideoEncoderConfig::ContentType::kScreen;
67 }
68
69 if (type == VideoCodecType::kVideoCodecVP8) {
70 config_.number_of_streams = num_spatial_streams;
71 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
72 vp8_settings.numberOfTemporalLayers = num_temporal_streams;
73 config_.encoder_specific_settings = new rtc::RefCountedObject<
74 webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
Sergey Silkin86684962018-03-28 19:32:37 +020075 } else if (type == VideoCodecType::kVideoCodecVP9) {
76 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
77 vp9_settings.numberOfSpatialLayers = num_spatial_streams;
78 vp9_settings.numberOfTemporalLayers = num_temporal_streams;
79 config_.encoder_specific_settings = new rtc::RefCountedObject<
80 webrtc::VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
81 } else if (type != VideoCodecType::kVideoCodecMultiplex) {
sprang429600d2017-01-26 06:12:26 -080082 ADD_FAILURE() << "Unexpected codec type: " << type;
83 }
84 }
85
86 bool InitializeCodec() {
87 codec_out_ = VideoCodec();
Elad Aloncde8ab22019-03-20 11:56:20 +010088 frame_buffer_controller_.reset();
Jiawei Ouc2ebe212018-11-08 10:02:56 -080089 if (!VideoCodecInitializer::SetupCodec(config_, streams_, &codec_out_)) {
sprang429600d2017-01-26 06:12:26 -080090 return false;
91 }
Jiawei Ouc2ebe212018-11-08 10:02:56 -080092 bitrate_allocator_ = CreateBuiltinVideoBitrateAllocatorFactory()
93 ->CreateVideoBitrateAllocator(codec_out_);
94 RTC_CHECK(bitrate_allocator_);
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080095 if (codec_out_.codecType == VideoCodecType::kVideoCodecMultiplex)
Emircan Uysaler0a375472017-12-11 12:21:02 +053096 return true;
Erik Språng82fad3d2018-03-21 09:57:23 +010097
sprang429600d2017-01-26 06:12:26 -080098 // Make sure temporal layers instances have been created.
99 if (codec_out_.codecType == VideoCodecType::kVideoCodecVP8) {
Elad Aloncde8ab22019-03-20 11:56:20 +0100100 Vp8TemporalLayersFactory factory;
101 frame_buffer_controller_ = factory.Create(codec_out_);
sprang429600d2017-01-26 06:12:26 -0800102 }
103 return true;
104 }
105
106 VideoStream DefaultStream() {
107 VideoStream stream;
108 stream.width = kDefaultWidth;
109 stream.height = kDefaultHeight;
110 stream.max_framerate = kDefaultFrameRate;
111 stream.min_bitrate_bps = kDefaultMinBitrateBps;
112 stream.target_bitrate_bps = kDefaultTargetBitrateBps;
113 stream.max_bitrate_bps = kDefaultMaxBitrateBps;
114 stream.max_qp = kDefaultMaxQp;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100115 stream.num_temporal_layers = 1;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800116 stream.active = true;
sprang429600d2017-01-26 06:12:26 -0800117 return stream;
118 }
119
120 VideoStream DefaultScreenshareStream() {
121 VideoStream stream = DefaultStream();
122 stream.min_bitrate_bps = 30000;
123 stream.target_bitrate_bps = kScreenshareTl0BitrateBps;
124 stream.max_bitrate_bps = 1000000;
125 stream.max_framerate = kScreenshareDefaultFramerate;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100126 stream.num_temporal_layers = 2;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800127 stream.active = true;
sprang429600d2017-01-26 06:12:26 -0800128 return stream;
129 }
130
131 // Input settings.
132 VideoEncoderConfig config_;
sprang429600d2017-01-26 06:12:26 -0800133 std::vector<VideoStream> streams_;
sprang429600d2017-01-26 06:12:26 -0800134
135 // Output.
136 VideoCodec codec_out_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800137 std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
Elad Aloncde8ab22019-03-20 11:56:20 +0100138 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_;
sprang429600d2017-01-26 06:12:26 -0800139};
140
141TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) {
142 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
143 streams_.push_back(DefaultStream());
144 EXPECT_TRUE(InitializeCodec());
145
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800146 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
147 kDefaultTargetBitrateBps, kDefaultFrameRate);
sprang429600d2017-01-26 06:12:26 -0800148 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
149 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
150 EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps());
151}
152
Seth Hampson46e31ba2018-01-18 10:39:54 -0800153TEST_F(VideoCodecInitializerTest, SingleStreamVp8ScreenshareInactive) {
154 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
155 VideoStream inactive_stream = DefaultStream();
156 inactive_stream.active = false;
157 streams_.push_back(inactive_stream);
158 EXPECT_TRUE(InitializeCodec());
159
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800160 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
161 kDefaultTargetBitrateBps, kDefaultFrameRate);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800162 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
163 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
164 EXPECT_EQ(0U, bitrate_allocation.get_sum_bps());
165}
166
sprang429600d2017-01-26 06:12:26 -0800167TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) {
168 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true);
169 streams_.push_back(DefaultScreenshareStream());
170 EXPECT_TRUE(InitializeCodec());
171
172 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
173 EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers);
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800174 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
175 kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate);
sprang429600d2017-01-26 06:12:26 -0800176 EXPECT_EQ(kScreenshareCodecTargetBitrateBps,
177 bitrate_allocation.get_sum_bps());
178 EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0));
179}
180
Seth Hampson46e31ba2018-01-18 10:39:54 -0800181TEST_F(VideoCodecInitializerTest, SimulcastVp8Screenshare) {
sprang429600d2017-01-26 06:12:26 -0800182 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
183 streams_.push_back(DefaultScreenshareStream());
184 VideoStream video_stream = DefaultStream();
185 video_stream.max_framerate = kScreenshareDefaultFramerate;
186 streams_.push_back(video_stream);
187 EXPECT_TRUE(InitializeCodec());
188
189 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
190 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
191 const uint32_t max_bitrate_bps =
192 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800193 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
194 max_bitrate_bps, kScreenshareDefaultFramerate);
sprang429600d2017-01-26 06:12:26 -0800195 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
196 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
197 bitrate_allocation.GetSpatialLayerSum(0));
198 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
199 bitrate_allocation.GetSpatialLayerSum(1));
200}
201
Seth Hampson46e31ba2018-01-18 10:39:54 -0800202// Tests that when a video stream is inactive, then the bitrate allocation will
203// be 0 for that stream.
204TEST_F(VideoCodecInitializerTest, SimulcastVp8ScreenshareInactive) {
205 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
206 streams_.push_back(DefaultScreenshareStream());
207 VideoStream inactive_video_stream = DefaultStream();
208 inactive_video_stream.active = false;
209 inactive_video_stream.max_framerate = kScreenshareDefaultFramerate;
210 streams_.push_back(inactive_video_stream);
211 EXPECT_TRUE(InitializeCodec());
212
213 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
214 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
215 const uint32_t target_bitrate =
216 streams_[0].target_bitrate_bps + streams_[1].target_bitrate_bps;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800217 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
218 target_bitrate, kScreenshareDefaultFramerate);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800219 EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
220 bitrate_allocation.get_sum_bps());
221 EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
222 bitrate_allocation.GetSpatialLayerSum(0));
223 EXPECT_EQ(0U, bitrate_allocation.GetSpatialLayerSum(1));
224}
225
226TEST_F(VideoCodecInitializerTest, HighFpsSimulcastVp8Screenshare) {
sprang429600d2017-01-26 06:12:26 -0800227 // Two simulcast streams, the lower one using legacy settings (two temporal
228 // streams, 5fps), the higher one using 3 temporal streams and 30fps.
229 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true);
230 streams_.push_back(DefaultScreenshareStream());
231 VideoStream video_stream = DefaultStream();
Sergey Silkina796a7e2018-03-01 15:11:29 +0100232 video_stream.num_temporal_layers = 3;
sprang429600d2017-01-26 06:12:26 -0800233 streams_.push_back(video_stream);
234 EXPECT_TRUE(InitializeCodec());
235
236 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
237 EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers);
238 const uint32_t max_bitrate_bps =
239 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
Erik Språng566124a2018-04-23 12:32:22 +0200240 VideoBitrateAllocation bitrate_allocation =
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800241 bitrate_allocator_->GetAllocation(max_bitrate_bps, kDefaultFrameRate);
sprang429600d2017-01-26 06:12:26 -0800242 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
243 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
244 bitrate_allocation.GetSpatialLayerSum(0));
245 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
246 bitrate_allocation.GetSpatialLayerSum(1));
247 EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0));
248 EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps,
249 bitrate_allocation.GetBitrate(1, 1));
250}
251
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -0800252TEST_F(VideoCodecInitializerTest, SingleStreamMultiplexCodec) {
253 SetUpFor(VideoCodecType::kVideoCodecMultiplex, 1, 1, true);
Emircan Uysaler0a375472017-12-11 12:21:02 +0530254 streams_.push_back(DefaultStream());
255 EXPECT_TRUE(InitializeCodec());
256}
257
Sergey Silkin86684962018-03-28 19:32:37 +0200258TEST_F(VideoCodecInitializerTest, Vp9SvcDefaultLayering) {
259 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
260 VideoStream stream = DefaultStream();
261 stream.num_temporal_layers = 3;
262 streams_.push_back(stream);
263
264 EXPECT_TRUE(InitializeCodec());
265 EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3u);
266 EXPECT_EQ(codec_out_.VP9()->numberOfTemporalLayers, 3u);
267}
268
269TEST_F(VideoCodecInitializerTest, Vp9SvcAdjustedLayering) {
270 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
271 VideoStream stream = DefaultStream();
272 stream.num_temporal_layers = 3;
273 // Set resolution which is only enough to produce 2 spatial layers.
274 stream.width = kMinVp9SpatialLayerWidth * 2;
275 stream.height = kMinVp9SpatialLayerHeight * 2;
276
277 streams_.push_back(stream);
278
279 EXPECT_TRUE(InitializeCodec());
280 EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 2u);
281}
282
Sergey Silkinfafeac32018-04-13 16:36:39 +0200283TEST_F(VideoCodecInitializerTest,
284 Vp9SingleSpatialLayerMaxBitrateIsEqualToCodecMaxBitrate) {
285 SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 3, false);
286 VideoStream stream = DefaultStream();
287 stream.num_temporal_layers = 3;
288 streams_.push_back(stream);
289
290 EXPECT_TRUE(InitializeCodec());
291 EXPECT_EQ(codec_out_.spatialLayers[0].maxBitrate,
292 kDefaultMaxBitrateBps / 1000);
293}
294
Sergey Silkin33120922018-11-28 13:32:13 +0100295TEST_F(VideoCodecInitializerTest,
Ilya Nikolaevskiy9ef5e052019-03-05 10:08:35 +0100296 Vp9SingleSpatialLayerTargetBitrateIsEqualToCodecMaxBitrate) {
297 SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 1, true);
298 VideoStream stream = DefaultStream();
299 stream.num_temporal_layers = 1;
300 streams_.push_back(stream);
301
302 EXPECT_TRUE(InitializeCodec());
303 EXPECT_EQ(codec_out_.spatialLayers[0].targetBitrate,
304 kDefaultMaxBitrateBps / 1000);
305}
306
307TEST_F(VideoCodecInitializerTest,
Sergey Silkin33120922018-11-28 13:32:13 +0100308 Vp9KeepBitrateLimitsIfNumberOfSpatialLayersIsReducedToOne) {
309 // Request 3 spatial layers for 320x180 input. Actual number of layers will be
310 // reduced to 1 due to low input resolution but SVC bitrate limits should be
311 // applied.
312 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
313 VideoStream stream = DefaultStream();
314 stream.width = 320;
315 stream.height = 180;
316 stream.num_temporal_layers = 3;
317 streams_.push_back(stream);
318
319 EXPECT_TRUE(InitializeCodec());
320 EXPECT_LT(codec_out_.spatialLayers[0].maxBitrate,
321 kDefaultMaxBitrateBps / 1000);
322}
323
Sergey Silkin8b9b5f92018-12-10 09:28:53 +0100324TEST_F(VideoCodecInitializerTest, Vp9DeactivateLayers) {
325 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 1, false);
326 VideoStream stream = DefaultStream();
327 streams_.push_back(stream);
328
329 config_.simulcast_layers.resize(3);
330
331 // Activate all layers.
332 config_.simulcast_layers[0].active = true;
333 config_.simulcast_layers[1].active = true;
334 config_.simulcast_layers[2].active = true;
335 EXPECT_TRUE(InitializeCodec());
336 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
337 EXPECT_TRUE(codec_out_.spatialLayers[1].active);
338 EXPECT_TRUE(codec_out_.spatialLayers[2].active);
339
340 // Deactivate top layer.
341 config_.simulcast_layers[0].active = false;
342 EXPECT_TRUE(InitializeCodec());
343 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
344 EXPECT_TRUE(codec_out_.spatialLayers[1].active);
345 EXPECT_FALSE(codec_out_.spatialLayers[2].active);
346
347 // Deactivate middle layer.
348 config_.simulcast_layers[0].active = true;
349 config_.simulcast_layers[1].active = false;
350 EXPECT_TRUE(InitializeCodec());
351 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
352 EXPECT_FALSE(codec_out_.spatialLayers[1].active);
353 EXPECT_TRUE(codec_out_.spatialLayers[2].active);
354}
355
sprang429600d2017-01-26 06:12:26 -0800356} // namespace webrtc