blob: cc7743ad251490d76671d50ec58a9ac928cc2f0e [file] [log] [blame]
Sergey Silkin86684962018-03-28 19:32:37 +02001/*
2 * Copyright (c) 2018 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
11#include "modules/video_coding/codecs/vp9/svc_config.h"
12
13#include <algorithm>
14#include <cmath>
15#include <vector>
16
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "rtc_base/checks.h"
Ilya Nikolaevskiy2899b3b2020-06-11 14:01:56 +020019#include "rtc_base/logging.h"
Sergey Silkin86684962018-03-28 19:32:37 +020020
21namespace webrtc {
22
“Michaelb54500e2018-05-14 08:35:00 -050023namespace {
Sergey Silkindfe8ca02018-05-14 19:31:07 +020024const size_t kMinVp9SvcBitrateKbps = 30;
25
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010026const size_t kMaxNumLayersForScreenSharing = 3;
Ilya Nikolaevskiycad95b82019-03-08 14:27:19 +000027const float kMaxScreenSharingLayerFramerateFps[] = {5.0, 10.0, 30.0};
28const size_t kMinScreenSharingLayerBitrateKbps[] = {30, 200, 500};
29const size_t kTargetScreenSharingLayerBitrateKbps[] = {150, 350, 950};
30const size_t kMaxScreenSharingLayerBitrateKbps[] = {250, 500, 950};
Ilya Nikolaevskiy3a656d12019-02-14 14:44:22 +010031
“Michaelb54500e2018-05-14 08:35:00 -050032} // namespace
33
Sergey Silkindfe8ca02018-05-14 19:31:07 +020034std::vector<SpatialLayer> ConfigureSvcScreenSharing(size_t input_width,
35 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +020036 float max_framerate_fps,
Sergey Silkindfe8ca02018-05-14 19:31:07 +020037 size_t num_spatial_layers) {
38 num_spatial_layers =
39 std::min(num_spatial_layers, kMaxNumLayersForScreenSharing);
40 std::vector<SpatialLayer> spatial_layers;
Sergey Silkin86684962018-03-28 19:32:37 +020041
Sergey Silkindfe8ca02018-05-14 19:31:07 +020042 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
43 SpatialLayer spatial_layer = {0};
44 spatial_layer.width = input_width;
45 spatial_layer.height = input_height;
Sergey Silkin1946a3f2018-08-22 11:42:16 +020046 spatial_layer.maxFramerate =
47 std::min(kMaxScreenSharingLayerFramerateFps[sl_idx], max_framerate_fps);
Sergey Silkindfe8ca02018-05-14 19:31:07 +020048 spatial_layer.numberOfTemporalLayers = 1;
Ilya Nikolaevskiy3a656d12019-02-14 14:44:22 +010049 spatial_layer.minBitrate =
50 static_cast<int>(kMinScreenSharingLayerBitrateKbps[sl_idx]);
Sergey Silkindfe8ca02018-05-14 19:31:07 +020051 spatial_layer.maxBitrate =
52 static_cast<int>(kMaxScreenSharingLayerBitrateKbps[sl_idx]);
Ilya Nikolaevskiy3a656d12019-02-14 14:44:22 +010053 spatial_layer.targetBitrate =
54 static_cast<int>(kTargetScreenSharingLayerBitrateKbps[sl_idx]);
Sergey Silkin8b9b5f92018-12-10 09:28:53 +010055 spatial_layer.active = true;
Sergey Silkindfe8ca02018-05-14 19:31:07 +020056 spatial_layers.push_back(spatial_layer);
57 }
58
59 return spatial_layers;
60}
61
62std::vector<SpatialLayer> ConfigureSvcNormalVideo(size_t input_width,
63 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +020064 float max_framerate_fps,
Ilya Nikolaevskiy39fb8172020-04-14 10:28:19 +020065 size_t first_active_layer,
Sergey Silkindfe8ca02018-05-14 19:31:07 +020066 size_t num_spatial_layers,
67 size_t num_temporal_layers) {
Ilya Nikolaevskiy39fb8172020-04-14 10:28:19 +020068 RTC_DCHECK_LT(first_active_layer, num_spatial_layers);
Sergey Silkin86684962018-03-28 19:32:37 +020069 std::vector<SpatialLayer> spatial_layers;
70
71 // Limit number of layers for given resolution.
72 const size_t num_layers_fit_horz = static_cast<size_t>(std::floor(
73 1 + std::max(0.0f,
74 std::log2(1.0f * input_width / kMinVp9SpatialLayerWidth))));
75 const size_t num_layers_fit_vert = static_cast<size_t>(
76 std::floor(1 + std::max(0.0f, std::log2(1.0f * input_height /
77 kMinVp9SpatialLayerHeight))));
Ilya Nikolaevskiy2899b3b2020-06-11 14:01:56 +020078 const size_t limited_num_spatial_layers =
79 std::min(num_layers_fit_horz, num_layers_fit_vert);
80 if (limited_num_spatial_layers < num_spatial_layers) {
81 RTC_LOG(LS_WARNING) << "Reducing number of spatial layers from "
82 << num_spatial_layers << " to "
83 << limited_num_spatial_layers
84 << " due to low input resolution.";
85 num_spatial_layers = limited_num_spatial_layers;
86 }
Ilya Nikolaevskiy39fb8172020-04-14 10:28:19 +020087 // First active layer must be configured.
88 num_spatial_layers = std::max(num_spatial_layers, first_active_layer + 1);
Sergey Silkin86684962018-03-28 19:32:37 +020089
Ilya Nikolaevskiy09eb6e22020-06-05 12:36:32 +020090 // Ensure top layer is even enough.
Ilya Nikolaevskiy2899b3b2020-06-11 14:01:56 +020091 int required_divisiblity = 1 << (num_spatial_layers - first_active_layer - 1);
Ilya Nikolaevskiy09eb6e22020-06-05 12:36:32 +020092 input_width = input_width - input_width % required_divisiblity;
93 input_height = input_height - input_height % required_divisiblity;
94
Ilya Nikolaevskiy39fb8172020-04-14 10:28:19 +020095 for (size_t sl_idx = first_active_layer; sl_idx < num_spatial_layers;
96 ++sl_idx) {
Sergey Silkin86684962018-03-28 19:32:37 +020097 SpatialLayer spatial_layer = {0};
98 spatial_layer.width = input_width >> (num_spatial_layers - sl_idx - 1);
99 spatial_layer.height = input_height >> (num_spatial_layers - sl_idx - 1);
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200100 spatial_layer.maxFramerate = max_framerate_fps;
Sergey Silkin86684962018-03-28 19:32:37 +0200101 spatial_layer.numberOfTemporalLayers = num_temporal_layers;
Sergey Silkin8b9b5f92018-12-10 09:28:53 +0100102 spatial_layer.active = true;
Sergey Silkin86684962018-03-28 19:32:37 +0200103
“Michaelb54500e2018-05-14 08:35:00 -0500104 // minBitrate and maxBitrate formulas were derived from
105 // subjective-quality data to determing bit rates below which video
106 // quality is unacceptable and above which additional bits do not provide
107 // benefit. The formulas express rate in units of kbps.
108
Sergey Silkin86684962018-03-28 19:32:37 +0200109 // TODO(ssilkin): Add to the comment PSNR/SSIM we get at encoding certain
110 // video to min/max bitrate specified by those formulas.
111 const size_t num_pixels = spatial_layer.width * spatial_layer.height;
“Michael277a6562018-06-01 14:09:19 -0500112 int min_bitrate =
“Michaelb54500e2018-05-14 08:35:00 -0500113 static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.);
“Michael277a6562018-06-01 14:09:19 -0500114 min_bitrate = std::max(min_bitrate, 0);
115 spatial_layer.minBitrate =
116 std::max(static_cast<size_t>(min_bitrate), kMinVp9SvcBitrateKbps);
Sergey Silkin86684962018-03-28 19:32:37 +0200117 spatial_layer.maxBitrate =
Yves Gerey665174f2018-06-19 15:03:05 +0200118 static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000);
Sergey Silkin86684962018-03-28 19:32:37 +0200119 spatial_layer.targetBitrate =
“Michael277a6562018-06-01 14:09:19 -0500120 (spatial_layer.minBitrate + spatial_layer.maxBitrate) / 2;
Sergey Silkin86684962018-03-28 19:32:37 +0200121 spatial_layers.push_back(spatial_layer);
122 }
Sergey Silkin8b9b5f92018-12-10 09:28:53 +0100123
Ilya Nikolaevskiy7a824672020-06-18 19:16:53 +0200124 // A workaround for sitiation when single HD layer is left with minBitrate
125 // about 500kbps. This would mean that there will always be at least 500kbps
126 // allocated to video regardless of how low is the actual BWE.
127 // Also, boost maxBitrate for the first layer to account for lost ability to
128 // predict from previous layers.
129 if (first_active_layer > 0) {
130 spatial_layers[0].minBitrate = kMinVp9SvcBitrateKbps;
131 // TODO(ilnik): tune this value or come up with a different formula to
132 // ensure that all singlecast configurations look good and not too much
133 // bitrate is added.
134 spatial_layers[0].maxBitrate *= 1.1;
135 }
136
Sergey Silkin86684962018-03-28 19:32:37 +0200137 return spatial_layers;
138}
139
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200140std::vector<SpatialLayer> GetSvcConfig(size_t input_width,
141 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200142 float max_framerate_fps,
Ilya Nikolaevskiy39fb8172020-04-14 10:28:19 +0200143 size_t first_active_layer,
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200144 size_t num_spatial_layers,
145 size_t num_temporal_layers,
146 bool is_screen_sharing) {
147 RTC_DCHECK_GT(input_width, 0);
148 RTC_DCHECK_GT(input_height, 0);
149 RTC_DCHECK_GT(num_spatial_layers, 0);
150 RTC_DCHECK_GT(num_temporal_layers, 0);
151
152 if (is_screen_sharing) {
153 return ConfigureSvcScreenSharing(input_width, input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200154 max_framerate_fps, num_spatial_layers);
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200155 } else {
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200156 return ConfigureSvcNormalVideo(input_width, input_height, max_framerate_fps,
Ilya Nikolaevskiy39fb8172020-04-14 10:28:19 +0200157 first_active_layer, num_spatial_layers,
Ilya Nikolaevskiy03d90962020-02-11 12:50:38 +0100158 num_temporal_layers);
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200159 }
160}
161
Sergey Silkin86684962018-03-28 19:32:37 +0200162} // namespace webrtc