blob: c53a49d483c8490618f1edc566182d76b5c292fa [file] [log] [blame]
buildbot@webrtc.orga8530772014-12-10 09:01:18 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +00009 */
10
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000011#include <stdio.h>
Steve Antone78bcb92017-10-31 09:53:08 -070012#include <algorithm>
13#include <string>
sprang@webrtc.org46d4d292014-12-23 15:19:35 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "media/base/streamparams.h"
16#include "media/engine/constants.h"
17#include "media/engine/simulcast.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020018#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/arraysize.h"
20#include "rtc_base/logging.h"
21#include "system_wrappers/include/field_trial.h"
tfarina5237aaf2015-11-10 23:44:30 -080022
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000023namespace cricket {
24
Rasmus Brandt195d1d72018-05-09 11:28:01 +020025namespace {
26
27constexpr int kScreenshareDefaultTl0BitrateKbps = 200;
28constexpr int kScreenshareDefaultTl1BitrateKbps = 1000;
29
Rasmus Brandt195d1d72018-05-09 11:28:01 +020030} // namespace
31
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000032struct SimulcastFormat {
33 int width;
34 int height;
35 // The maximum number of simulcast layers can be used for
36 // resolutions at |widthxheigh|.
37 size_t max_layers;
38 // The maximum bitrate for encoding stream at |widthxheight|, when we are
39 // not sending the next higher spatial stream.
pbosbe16f792015-10-16 12:49:39 -070040 int max_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000041 // The target bitrate for encoding stream at |widthxheight|, when this layer
42 // is not the highest layer (i.e., when we are sending another higher spatial
43 // stream).
pbosbe16f792015-10-16 12:49:39 -070044 int target_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000045 // The minimum bitrate needed for encoding stream at |widthxheight|.
pbosbe16f792015-10-16 12:49:39 -070046 int min_bitrate_kbps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000047};
48
49// These tables describe from which resolution we can use how many
50// simulcast layers at what bitrates (maximum, target, and minimum).
51// Important!! Keep this table from high resolution to low resolution.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020052// clang-format off
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000053const SimulcastFormat kSimulcastFormats[] = {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020054 {1920, 1080, 3, 5000, 4000, 800},
55 {1280, 720, 3, 2500, 2500, 600},
56 {960, 540, 3, 900, 900, 450},
57 {640, 360, 2, 700, 500, 150},
58 {480, 270, 2, 450, 350, 150},
59 {320, 180, 1, 200, 150, 30},
60 {0, 0, 1, 200, 150, 30}
61};
62// clang-format on
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000063
Seth Hampson1370e302018-02-07 08:50:36 -080064const int kMaxScreenshareSimulcastLayers = 2;
sprang429600d2017-01-26 06:12:26 -080065
Erik Språngbfe3d852018-05-15 09:54:14 +020066// Multiway: Number of temporal layers for each simulcast stream.
67int DefaultNumberOfTemporalLayers(int simulcast_id) {
68 RTC_CHECK_GE(simulcast_id, 0);
69 RTC_CHECK_LT(simulcast_id, webrtc::kMaxSimulcastStreams);
70
71 const int kDefaultNumTemporalLayers = 3;
72
73 const std::string group_name =
74 webrtc::field_trial::FindFullName("WebRTC-VP8ConferenceTemporalLayers");
75 if (group_name.empty())
76 return kDefaultNumTemporalLayers;
77
78 int num_temporal_layers = kDefaultNumTemporalLayers;
79 if (sscanf(group_name.c_str(), "%d", &num_temporal_layers) == 1 &&
80 num_temporal_layers > 0 &&
81 num_temporal_layers <= webrtc::kMaxTemporalStreams) {
82 return num_temporal_layers;
83 }
84
85 RTC_LOG(LS_WARNING) << "Attempt to set number of temporal layers to "
86 "incorrect value: "
87 << group_name;
88
89 return kDefaultNumTemporalLayers;
90}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000091
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000092int FindSimulcastFormatIndex(int width, int height) {
Seth Hampson438663e2018-01-09 11:14:14 -080093 RTC_DCHECK_GE(width, 0);
94 RTC_DCHECK_GE(height, 0);
kjellandera96e2d72016-02-04 23:52:28 -080095 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -080096 if (width * height >=
97 kSimulcastFormats[i].width * kSimulcastFormats[i].height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000098 return i;
99 }
100 }
Seth Hampson438663e2018-01-09 11:14:14 -0800101 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000102 return -1;
103}
104
105int FindSimulcastFormatIndex(int width, int height, size_t max_layers) {
Seth Hampson438663e2018-01-09 11:14:14 -0800106 RTC_DCHECK_GE(width, 0);
107 RTC_DCHECK_GE(height, 0);
108 RTC_DCHECK_GT(max_layers, 0);
kjellandera96e2d72016-02-04 23:52:28 -0800109 for (uint32_t i = 0; i < arraysize(kSimulcastFormats); ++i) {
sprang429600d2017-01-26 06:12:26 -0800110 if (width * height >=
111 kSimulcastFormats[i].width * kSimulcastFormats[i].height &&
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000112 max_layers == kSimulcastFormats[i].max_layers) {
113 return i;
114 }
115 }
Seth Hampson438663e2018-01-09 11:14:14 -0800116 RTC_NOTREACHED();
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000117 return -1;
118}
119
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000120// Simulcast stream width and height must both be dividable by
Rasmus Brandtefbdcb02018-04-26 17:47:42 +0200121// |2 ^ (simulcast_layers - 1)|.
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000122int NormalizeSimulcastSize(int size, size_t simulcast_layers) {
123 const int base2_exponent = static_cast<int>(simulcast_layers) - 1;
124 return ((size >> base2_exponent) << base2_exponent);
125}
126
127size_t FindSimulcastMaxLayers(int width, int height) {
128 int index = FindSimulcastFormatIndex(width, height);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000129 return kSimulcastFormats[index].max_layers;
130}
131
sprang429600d2017-01-26 06:12:26 -0800132int FindSimulcastMaxBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000133 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700134 return kSimulcastFormats[format_index].max_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000135}
136
sprang429600d2017-01-26 06:12:26 -0800137int FindSimulcastTargetBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000138 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700139 return kSimulcastFormats[format_index].target_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000140}
141
sprang429600d2017-01-26 06:12:26 -0800142int FindSimulcastMinBitrateBps(int width, int height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000143 const int format_index = FindSimulcastFormatIndex(width, height);
pbosbe16f792015-10-16 12:49:39 -0700144 return kSimulcastFormats[format_index].min_bitrate_kbps * 1000;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000145}
146
Seth Hampson438663e2018-01-09 11:14:14 -0800147void SlotSimulcastMaxResolution(size_t max_layers, int* width, int* height) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000148 int index = FindSimulcastFormatIndex(*width, *height, max_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000149 *width = kSimulcastFormats[index].width;
150 *height = kSimulcastFormats[index].height;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(LS_INFO) << "SlotSimulcastMaxResolution to width:" << *width
152 << " height:" << *height;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000153}
154
Seth Hampson1370e302018-02-07 08:50:36 -0800155void BoostMaxSimulcastLayer(int max_bitrate_bps,
156 std::vector<webrtc::VideoStream>* layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200157 if (layers->empty())
158 return;
159
Seth Hampson1370e302018-02-07 08:50:36 -0800160 // Spend additional bits to boost the max layer.
161 int bitrate_left_bps = max_bitrate_bps - GetTotalMaxBitrateBps(*layers);
162 if (bitrate_left_bps > 0) {
163 layers->back().max_bitrate_bps += bitrate_left_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000164 }
Seth Hampson1370e302018-02-07 08:50:36 -0800165}
166
167int GetTotalMaxBitrateBps(const std::vector<webrtc::VideoStream>& layers) {
Åsa Persson55659812018-06-18 17:51:32 +0200168 if (layers.empty())
169 return 0;
170
Seth Hampson1370e302018-02-07 08:50:36 -0800171 int total_max_bitrate_bps = 0;
172 for (size_t s = 0; s < layers.size() - 1; ++s) {
173 total_max_bitrate_bps += layers[s].target_bitrate_bps;
174 }
175 total_max_bitrate_bps += layers.back().max_bitrate_bps;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000176 return total_max_bitrate_bps;
177}
178
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200179std::vector<webrtc::VideoStream> GetSimulcastConfig(
180 size_t max_layers,
181 int width,
182 int height,
183 int /*max_bitrate_bps*/,
184 double bitrate_priority,
185 int max_qp,
186 int max_framerate,
187 bool is_screenshare,
188 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800189 if (is_screenshare) {
Åsa Persson55659812018-06-18 17:51:32 +0200190 return GetScreenshareLayers(max_layers, width, height, bitrate_priority,
191 max_qp, max_framerate,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200192 temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800193 } else {
Åsa Persson55659812018-06-18 17:51:32 +0200194 return GetNormalSimulcastLayers(max_layers, width, height, bitrate_priority,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200195 max_qp, max_framerate,
196 temporal_layers_supported);
sprang429600d2017-01-26 06:12:26 -0800197 }
Seth Hampson1370e302018-02-07 08:50:36 -0800198}
sprang429600d2017-01-26 06:12:26 -0800199
Seth Hampson1370e302018-02-07 08:50:36 -0800200std::vector<webrtc::VideoStream> GetNormalSimulcastLayers(
201 size_t max_layers,
202 int width,
203 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800204 double bitrate_priority,
205 int max_qp,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200206 int max_framerate,
207 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800208 // TODO(bugs.webrtc.org/8785): Currently if the resolution isn't large enough
209 // (defined in kSimulcastFormats) we scale down the number of simulcast
210 // layers. Consider changing this so that the application can have more
211 // control over exactly how many simulcast layers are used.
212 size_t num_simulcast_layers = FindSimulcastMaxLayers(width, height);
213 if (num_simulcast_layers > max_layers) {
214 // TODO(bugs.webrtc.org/8486): This scales down the resolution if the
215 // number of simulcast layers created by the application isn't sufficient
216 // (defined in kSimulcastFormats). For example if the input frame's
217 // resolution is HD, but there are only 2 simulcast layers, the
218 // resolution gets scaled down to VGA. Consider taking this logic out to
219 // allow the application more control over the resolutions.
220 SlotSimulcastMaxResolution(max_layers, &width, &height);
221 num_simulcast_layers = max_layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000222 }
Seth Hampson1370e302018-02-07 08:50:36 -0800223 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000224
Seth Hampson1370e302018-02-07 08:50:36 -0800225 // Format width and height has to be divisible by |2 ^ num_simulcast_layers -
226 // 1|.
227 width = NormalizeSimulcastSize(width, num_simulcast_layers);
228 height = NormalizeSimulcastSize(height, num_simulcast_layers);
229 // Add simulcast streams, from highest resolution (|s| = num_simulcast_layers
230 // -1) to lowest resolution at |s| = 0.
231 for (size_t s = num_simulcast_layers - 1;; --s) {
232 layers[s].width = width;
233 layers[s].height = height;
234 // TODO(pbos): Fill actual temporal-layer bitrate thresholds.
235 layers[s].max_qp = max_qp;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200236 layers[s].num_temporal_layers =
237 temporal_layers_supported ? DefaultNumberOfTemporalLayers(s)
238 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800239 layers[s].max_bitrate_bps = FindSimulcastMaxBitrateBps(width, height);
240 layers[s].target_bitrate_bps = FindSimulcastTargetBitrateBps(width, height);
Erik Språng79478ad2018-05-18 09:39:55 +0200241 int num_temporal_layers = DefaultNumberOfTemporalLayers(s);
Erik Språngd497e6b2018-07-06 17:17:10 +0200242 if (s == 0) {
Erik Språng79478ad2018-05-18 09:39:55 +0200243 // If alternative number temporal layers is selected, adjust the
Erik Språngd92288f2018-07-04 10:07:40 +0200244 // bitrate of the lowest simulcast stream so that absolute bitrate for
245 // the base temporal layer matches the bitrate for the base temporal
246 // layer with the default 3 simulcast streams. Otherwise we risk a
247 // higher threshold for receiving a feed at all.
Erik Språngd497e6b2018-07-06 17:17:10 +0200248 float rate_factor = 1.0;
249 if (num_temporal_layers == 3) {
250 if (webrtc::field_trial::IsEnabled("WebRTC-UseShortVP8TL3Pattern")) {
251 // Shortened pattern increases TL0 bitrate from 40% to 60%.
252 rate_factor = 0.4 / 0.6;
253 }
254 } else {
255 rate_factor =
256 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(3, 0) /
257 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
258 num_temporal_layers, 0);
259 }
260
Erik Språng79478ad2018-05-18 09:39:55 +0200261 layers[s].max_bitrate_bps =
262 static_cast<int>(layers[s].max_bitrate_bps * rate_factor);
263 layers[s].target_bitrate_bps =
264 static_cast<int>(layers[s].target_bitrate_bps * rate_factor);
265 }
Seth Hampson1370e302018-02-07 08:50:36 -0800266 layers[s].min_bitrate_bps = FindSimulcastMinBitrateBps(width, height);
267 layers[s].max_framerate = max_framerate;
sprang02569ad2017-07-06 05:05:50 -0700268
Seth Hampson1370e302018-02-07 08:50:36 -0800269 width /= 2;
270 height /= 2;
sprang02569ad2017-07-06 05:05:50 -0700271
Seth Hampson1370e302018-02-07 08:50:36 -0800272 if (s == 0) {
273 break;
sprang02569ad2017-07-06 05:05:50 -0700274 }
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000275 }
Seth Hampson1370e302018-02-07 08:50:36 -0800276 // Currently the relative bitrate priority of the sender is controlled by
277 // the value of the lowest VideoStream.
278 // TODO(bugs.webrtc.org/8630): The web specification describes being able to
279 // control relative bitrate for each individual simulcast layer, but this
280 // is currently just implemented per rtp sender.
281 layers[0].bitrate_priority = bitrate_priority;
282 return layers;
283}
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000284
Seth Hampson1370e302018-02-07 08:50:36 -0800285std::vector<webrtc::VideoStream> GetScreenshareLayers(
286 size_t max_layers,
287 int width,
288 int height,
Seth Hampson1370e302018-02-07 08:50:36 -0800289 double bitrate_priority,
290 int max_qp,
291 int max_framerate,
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200292 bool temporal_layers_supported) {
Seth Hampson1370e302018-02-07 08:50:36 -0800293 size_t num_simulcast_layers =
Ilya Nikolaevskiya3df0f22018-07-24 17:02:07 +0200294 std::min<int>(max_layers, kMaxScreenshareSimulcastLayers);
Seth Hampson1370e302018-02-07 08:50:36 -0800295
296 std::vector<webrtc::VideoStream> layers(num_simulcast_layers);
Seth Hampson1370e302018-02-07 08:50:36 -0800297 // For legacy screenshare in conference mode, tl0 and tl1 bitrates are
298 // piggybacked on the VideoCodec struct as target and max bitrates,
Erik Språngcc681cc2018-03-14 17:52:55 +0100299 // respectively. See eg. webrtc::LibvpxVp8Encoder::SetRates().
Seth Hampson1370e302018-02-07 08:50:36 -0800300 layers[0].width = width;
301 layers[0].height = height;
302 layers[0].max_qp = max_qp;
303 layers[0].max_framerate = 5;
304 layers[0].min_bitrate_bps = kMinVideoBitrateBps;
Rasmus Brandt195d1d72018-05-09 11:28:01 +0200305 layers[0].target_bitrate_bps = kScreenshareDefaultTl0BitrateKbps * 1000;
306 layers[0].max_bitrate_bps = kScreenshareDefaultTl1BitrateKbps * 1000;
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200307 layers[0].num_temporal_layers = temporal_layers_supported ? 2 : 0;
Seth Hampson1370e302018-02-07 08:50:36 -0800308
309 // With simulcast enabled, add another spatial layer. This one will have a
310 // more normal layout, with the regular 3 temporal layer pattern and no fps
311 // restrictions. The base simulcast layer will still use legacy setup.
312 if (num_simulcast_layers == kMaxScreenshareSimulcastLayers) {
313 // Add optional upper simulcast layer.
314 // Lowest temporal layers of a 3 layer setup will have 40% of the total
315 // bitrate allocation for that simulcast layer. Make sure the gap between
316 // the target of the lower simulcast layer and first temporal layer of the
317 // higher one is at most 2x the bitrate, so that upswitching is not hampered
318 // by stalled bitrate estimates.
319 int max_bitrate_bps = 2 * ((layers[0].target_bitrate_bps * 10) / 4);
320 // Cap max bitrate so it isn't overly high for the given resolution.
321 max_bitrate_bps = std::min<int>(max_bitrate_bps,
322 FindSimulcastMaxBitrateBps(width, height));
323
324 layers[1].width = width;
325 layers[1].height = height;
326 layers[1].max_qp = max_qp;
327 layers[1].max_framerate = max_framerate;
Sergey Silkinca091252018-03-14 17:00:50 +0100328 layers[1].num_temporal_layers = 3;
Seth Hampson1370e302018-02-07 08:50:36 -0800329 layers[1].min_bitrate_bps = layers[0].target_bitrate_bps * 2;
330 layers[1].target_bitrate_bps = max_bitrate_bps;
331 layers[1].max_bitrate_bps = max_bitrate_bps;
332 }
333
Seth Hampson24722b32017-12-22 09:36:42 -0800334 // The bitrate priority currently implemented on a per-sender level, so we
Seth Hampson1370e302018-02-07 08:50:36 -0800335 // just set it for the first simulcast layer.
336 layers[0].bitrate_priority = bitrate_priority;
337 return layers;
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000338}
339
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000340} // namespace cricket