blob: 0eca06de799f85e2e422bb9bd91c74c03552cd2c [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 "common_types.h" // NOLINT(build/include)
Sergey Silkin86684962018-03-28 19:32:37 +020027#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080028#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "test/gtest.h"
sprang429600d2017-01-26 06:12:26 -080031
32namespace webrtc {
33
34namespace {
sprang429600d2017-01-26 06:12:26 -080035static const int kDefaultWidth = 1280;
36static const int kDefaultHeight = 720;
37static const int kDefaultFrameRate = 30;
38static const uint32_t kDefaultMinBitrateBps = 60000;
39static const uint32_t kDefaultTargetBitrateBps = 2000000;
40static const uint32_t kDefaultMaxBitrateBps = 2000000;
41static const uint32_t kDefaultMinTransmitBitrateBps = 400000;
42static const int kDefaultMaxQp = 48;
Erik Språng5e898d62018-07-06 16:32:20 +020043static const uint32_t kScreenshareTl0BitrateBps = 200000;
sprang429600d2017-01-26 06:12:26 -080044static const uint32_t kScreenshareCodecTargetBitrateBps = 200000;
45static const uint32_t kScreenshareDefaultFramerate = 5;
46// Bitrates for the temporal layers of the higher screenshare simulcast stream.
47static const uint32_t kHighScreenshareTl0Bps = 800000;
48static const uint32_t kHighScreenshareTl1Bps = 1200000;
49} // namespace
50
sprang429600d2017-01-26 06:12:26 -080051// TODO(sprang): Extend coverage to handle the rest of the codec initializer.
52class VideoCodecInitializerTest : public ::testing::Test {
53 public:
Niels Möllerf1338562018-04-26 09:51:47 +020054 VideoCodecInitializerTest() {}
sprang429600d2017-01-26 06:12:26 -080055 virtual ~VideoCodecInitializerTest() {}
56
57 protected:
58 void SetUpFor(VideoCodecType type,
59 int num_spatial_streams,
60 int num_temporal_streams,
61 bool screenshare) {
62 config_ = VideoEncoderConfig();
Niels Möller259a4972018-04-05 15:36:51 +020063 config_.codec_type = type;
64
sprang429600d2017-01-26 06:12:26 -080065 if (screenshare) {
66 config_.min_transmit_bitrate_bps = kDefaultMinTransmitBitrateBps;
67 config_.content_type = VideoEncoderConfig::ContentType::kScreen;
68 }
69
70 if (type == VideoCodecType::kVideoCodecVP8) {
71 config_.number_of_streams = num_spatial_streams;
72 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
73 vp8_settings.numberOfTemporalLayers = num_temporal_streams;
74 config_.encoder_specific_settings = new rtc::RefCountedObject<
75 webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
Sergey Silkin86684962018-03-28 19:32:37 +020076 } else if (type == VideoCodecType::kVideoCodecVP9) {
77 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
78 vp9_settings.numberOfSpatialLayers = num_spatial_streams;
79 vp9_settings.numberOfTemporalLayers = num_temporal_streams;
80 config_.encoder_specific_settings = new rtc::RefCountedObject<
81 webrtc::VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
82 } else if (type != VideoCodecType::kVideoCodecMultiplex) {
sprang429600d2017-01-26 06:12:26 -080083 ADD_FAILURE() << "Unexpected codec type: " << type;
84 }
85 }
86
87 bool InitializeCodec() {
88 codec_out_ = VideoCodec();
Elad Aloncde8ab22019-03-20 11:56:20 +010089 frame_buffer_controller_.reset();
Jiawei Ouc2ebe212018-11-08 10:02:56 -080090 if (!VideoCodecInitializer::SetupCodec(config_, streams_, &codec_out_)) {
sprang429600d2017-01-26 06:12:26 -080091 return false;
92 }
Jiawei Ouc2ebe212018-11-08 10:02:56 -080093 bitrate_allocator_ = CreateBuiltinVideoBitrateAllocatorFactory()
94 ->CreateVideoBitrateAllocator(codec_out_);
95 RTC_CHECK(bitrate_allocator_);
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080096 if (codec_out_.codecType == VideoCodecType::kVideoCodecMultiplex)
Emircan Uysaler0a375472017-12-11 12:21:02 +053097 return true;
Erik Språng82fad3d2018-03-21 09:57:23 +010098
sprang429600d2017-01-26 06:12:26 -080099 // Make sure temporal layers instances have been created.
100 if (codec_out_.codecType == VideoCodecType::kVideoCodecVP8) {
Elad Aloncde8ab22019-03-20 11:56:20 +0100101 Vp8TemporalLayersFactory factory;
102 frame_buffer_controller_ = factory.Create(codec_out_);
sprang429600d2017-01-26 06:12:26 -0800103 }
104 return true;
105 }
106
107 VideoStream DefaultStream() {
108 VideoStream stream;
109 stream.width = kDefaultWidth;
110 stream.height = kDefaultHeight;
111 stream.max_framerate = kDefaultFrameRate;
112 stream.min_bitrate_bps = kDefaultMinBitrateBps;
113 stream.target_bitrate_bps = kDefaultTargetBitrateBps;
114 stream.max_bitrate_bps = kDefaultMaxBitrateBps;
115 stream.max_qp = kDefaultMaxQp;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100116 stream.num_temporal_layers = 1;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800117 stream.active = true;
sprang429600d2017-01-26 06:12:26 -0800118 return stream;
119 }
120
121 VideoStream DefaultScreenshareStream() {
122 VideoStream stream = DefaultStream();
123 stream.min_bitrate_bps = 30000;
124 stream.target_bitrate_bps = kScreenshareTl0BitrateBps;
125 stream.max_bitrate_bps = 1000000;
126 stream.max_framerate = kScreenshareDefaultFramerate;
Sergey Silkina796a7e2018-03-01 15:11:29 +0100127 stream.num_temporal_layers = 2;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800128 stream.active = true;
sprang429600d2017-01-26 06:12:26 -0800129 return stream;
130 }
131
132 // Input settings.
133 VideoEncoderConfig config_;
sprang429600d2017-01-26 06:12:26 -0800134 std::vector<VideoStream> streams_;
sprang429600d2017-01-26 06:12:26 -0800135
136 // Output.
137 VideoCodec codec_out_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800138 std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
Elad Aloncde8ab22019-03-20 11:56:20 +0100139 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_;
sprang429600d2017-01-26 06:12:26 -0800140};
141
142TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) {
143 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
144 streams_.push_back(DefaultStream());
145 EXPECT_TRUE(InitializeCodec());
146
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800147 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
148 kDefaultTargetBitrateBps, kDefaultFrameRate);
sprang429600d2017-01-26 06:12:26 -0800149 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
150 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
151 EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps());
152}
153
Seth Hampson46e31ba2018-01-18 10:39:54 -0800154TEST_F(VideoCodecInitializerTest, SingleStreamVp8ScreenshareInactive) {
155 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
156 VideoStream inactive_stream = DefaultStream();
157 inactive_stream.active = false;
158 streams_.push_back(inactive_stream);
159 EXPECT_TRUE(InitializeCodec());
160
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800161 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
162 kDefaultTargetBitrateBps, kDefaultFrameRate);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800163 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
164 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
165 EXPECT_EQ(0U, bitrate_allocation.get_sum_bps());
166}
167
sprang429600d2017-01-26 06:12:26 -0800168TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) {
169 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true);
170 streams_.push_back(DefaultScreenshareStream());
171 EXPECT_TRUE(InitializeCodec());
172
173 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
174 EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers);
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800175 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
176 kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate);
sprang429600d2017-01-26 06:12:26 -0800177 EXPECT_EQ(kScreenshareCodecTargetBitrateBps,
178 bitrate_allocation.get_sum_bps());
179 EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0));
180}
181
Seth Hampson46e31ba2018-01-18 10:39:54 -0800182TEST_F(VideoCodecInitializerTest, SimulcastVp8Screenshare) {
sprang429600d2017-01-26 06:12:26 -0800183 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
184 streams_.push_back(DefaultScreenshareStream());
185 VideoStream video_stream = DefaultStream();
186 video_stream.max_framerate = kScreenshareDefaultFramerate;
187 streams_.push_back(video_stream);
188 EXPECT_TRUE(InitializeCodec());
189
190 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
191 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
192 const uint32_t max_bitrate_bps =
193 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800194 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
195 max_bitrate_bps, kScreenshareDefaultFramerate);
sprang429600d2017-01-26 06:12:26 -0800196 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
197 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
198 bitrate_allocation.GetSpatialLayerSum(0));
199 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
200 bitrate_allocation.GetSpatialLayerSum(1));
201}
202
Seth Hampson46e31ba2018-01-18 10:39:54 -0800203// Tests that when a video stream is inactive, then the bitrate allocation will
204// be 0 for that stream.
205TEST_F(VideoCodecInitializerTest, SimulcastVp8ScreenshareInactive) {
206 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
207 streams_.push_back(DefaultScreenshareStream());
208 VideoStream inactive_video_stream = DefaultStream();
209 inactive_video_stream.active = false;
210 inactive_video_stream.max_framerate = kScreenshareDefaultFramerate;
211 streams_.push_back(inactive_video_stream);
212 EXPECT_TRUE(InitializeCodec());
213
214 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
215 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
216 const uint32_t target_bitrate =
217 streams_[0].target_bitrate_bps + streams_[1].target_bitrate_bps;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800218 VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
219 target_bitrate, kScreenshareDefaultFramerate);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800220 EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
221 bitrate_allocation.get_sum_bps());
222 EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
223 bitrate_allocation.GetSpatialLayerSum(0));
224 EXPECT_EQ(0U, bitrate_allocation.GetSpatialLayerSum(1));
225}
226
227TEST_F(VideoCodecInitializerTest, HighFpsSimulcastVp8Screenshare) {
sprang429600d2017-01-26 06:12:26 -0800228 // Two simulcast streams, the lower one using legacy settings (two temporal
229 // streams, 5fps), the higher one using 3 temporal streams and 30fps.
230 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true);
231 streams_.push_back(DefaultScreenshareStream());
232 VideoStream video_stream = DefaultStream();
Sergey Silkina796a7e2018-03-01 15:11:29 +0100233 video_stream.num_temporal_layers = 3;
sprang429600d2017-01-26 06:12:26 -0800234 streams_.push_back(video_stream);
235 EXPECT_TRUE(InitializeCodec());
236
237 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
238 EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers);
239 const uint32_t max_bitrate_bps =
240 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
Erik Språng566124a2018-04-23 12:32:22 +0200241 VideoBitrateAllocation bitrate_allocation =
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800242 bitrate_allocator_->GetAllocation(max_bitrate_bps, kDefaultFrameRate);
sprang429600d2017-01-26 06:12:26 -0800243 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
244 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
245 bitrate_allocation.GetSpatialLayerSum(0));
246 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
247 bitrate_allocation.GetSpatialLayerSum(1));
248 EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0));
249 EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps,
250 bitrate_allocation.GetBitrate(1, 1));
251}
252
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -0800253TEST_F(VideoCodecInitializerTest, SingleStreamMultiplexCodec) {
254 SetUpFor(VideoCodecType::kVideoCodecMultiplex, 1, 1, true);
Emircan Uysaler0a375472017-12-11 12:21:02 +0530255 streams_.push_back(DefaultStream());
256 EXPECT_TRUE(InitializeCodec());
257}
258
Sergey Silkin86684962018-03-28 19:32:37 +0200259TEST_F(VideoCodecInitializerTest, Vp9SvcDefaultLayering) {
260 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
261 VideoStream stream = DefaultStream();
262 stream.num_temporal_layers = 3;
263 streams_.push_back(stream);
264
265 EXPECT_TRUE(InitializeCodec());
266 EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3u);
267 EXPECT_EQ(codec_out_.VP9()->numberOfTemporalLayers, 3u);
268}
269
270TEST_F(VideoCodecInitializerTest, Vp9SvcAdjustedLayering) {
271 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
272 VideoStream stream = DefaultStream();
273 stream.num_temporal_layers = 3;
274 // Set resolution which is only enough to produce 2 spatial layers.
275 stream.width = kMinVp9SpatialLayerWidth * 2;
276 stream.height = kMinVp9SpatialLayerHeight * 2;
277
278 streams_.push_back(stream);
279
280 EXPECT_TRUE(InitializeCodec());
281 EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 2u);
282}
283
Sergey Silkinfafeac32018-04-13 16:36:39 +0200284TEST_F(VideoCodecInitializerTest,
285 Vp9SingleSpatialLayerMaxBitrateIsEqualToCodecMaxBitrate) {
286 SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 3, false);
287 VideoStream stream = DefaultStream();
288 stream.num_temporal_layers = 3;
289 streams_.push_back(stream);
290
291 EXPECT_TRUE(InitializeCodec());
292 EXPECT_EQ(codec_out_.spatialLayers[0].maxBitrate,
293 kDefaultMaxBitrateBps / 1000);
294}
295
Sergey Silkin33120922018-11-28 13:32:13 +0100296TEST_F(VideoCodecInitializerTest,
Ilya Nikolaevskiy9ef5e052019-03-05 10:08:35 +0100297 Vp9SingleSpatialLayerTargetBitrateIsEqualToCodecMaxBitrate) {
298 SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 1, true);
299 VideoStream stream = DefaultStream();
300 stream.num_temporal_layers = 1;
301 streams_.push_back(stream);
302
303 EXPECT_TRUE(InitializeCodec());
304 EXPECT_EQ(codec_out_.spatialLayers[0].targetBitrate,
305 kDefaultMaxBitrateBps / 1000);
306}
307
308TEST_F(VideoCodecInitializerTest,
Sergey Silkin33120922018-11-28 13:32:13 +0100309 Vp9KeepBitrateLimitsIfNumberOfSpatialLayersIsReducedToOne) {
310 // Request 3 spatial layers for 320x180 input. Actual number of layers will be
311 // reduced to 1 due to low input resolution but SVC bitrate limits should be
312 // applied.
313 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
314 VideoStream stream = DefaultStream();
315 stream.width = 320;
316 stream.height = 180;
317 stream.num_temporal_layers = 3;
318 streams_.push_back(stream);
319
320 EXPECT_TRUE(InitializeCodec());
321 EXPECT_LT(codec_out_.spatialLayers[0].maxBitrate,
322 kDefaultMaxBitrateBps / 1000);
323}
324
Sergey Silkin8b9b5f92018-12-10 09:28:53 +0100325TEST_F(VideoCodecInitializerTest, Vp9DeactivateLayers) {
326 SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 1, false);
327 VideoStream stream = DefaultStream();
328 streams_.push_back(stream);
329
330 config_.simulcast_layers.resize(3);
331
332 // Activate all layers.
333 config_.simulcast_layers[0].active = true;
334 config_.simulcast_layers[1].active = true;
335 config_.simulcast_layers[2].active = true;
336 EXPECT_TRUE(InitializeCodec());
337 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
338 EXPECT_TRUE(codec_out_.spatialLayers[1].active);
339 EXPECT_TRUE(codec_out_.spatialLayers[2].active);
340
341 // Deactivate top layer.
342 config_.simulcast_layers[0].active = false;
343 EXPECT_TRUE(InitializeCodec());
344 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
345 EXPECT_TRUE(codec_out_.spatialLayers[1].active);
346 EXPECT_FALSE(codec_out_.spatialLayers[2].active);
347
348 // Deactivate middle layer.
349 config_.simulcast_layers[0].active = true;
350 config_.simulcast_layers[1].active = false;
351 EXPECT_TRUE(InitializeCodec());
352 EXPECT_TRUE(codec_out_.spatialLayers[0].active);
353 EXPECT_FALSE(codec_out_.spatialLayers[1].active);
354 EXPECT_TRUE(codec_out_.spatialLayers[2].active);
355}
356
sprang429600d2017-01-26 06:12:26 -0800357} // namespace webrtc