blob: 138822ab925402c1545fc19d00dc985d798d55c9 [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"
Sergey Silkin86684962018-03-28 19:32:37 +020019
20namespace webrtc {
21
“Michaelb54500e2018-05-14 08:35:00 -050022namespace {
Sergey Silkindfe8ca02018-05-14 19:31:07 +020023const size_t kMinVp9SvcBitrateKbps = 30;
24
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010025const size_t kMaxNumLayersForScreenSharing = 3;
Ilya Nikolaevskiye01857c2019-03-08 14:25:42 +000026const float kMaxScreenSharingLayerFramerateFps[] = {5.0, 5.0, 30.0};
27const size_t kMinScreenSharingLayerBitrateKbps[] = {30, 150, 500};
28const size_t kTargetScreenSharingLayerBitrateKbps[] = {150, 350, 1000};
29const size_t kMaxScreenSharingLayerBitrateKbps[] = {200, 500, 1000};
Ilya Nikolaevskiy3a656d12019-02-14 14:44:22 +010030
“Michaelb54500e2018-05-14 08:35:00 -050031} // namespace
32
Sergey Silkindfe8ca02018-05-14 19:31:07 +020033std::vector<SpatialLayer> ConfigureSvcScreenSharing(size_t input_width,
34 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +020035 float max_framerate_fps,
Sergey Silkindfe8ca02018-05-14 19:31:07 +020036 size_t num_spatial_layers) {
37 num_spatial_layers =
38 std::min(num_spatial_layers, kMaxNumLayersForScreenSharing);
39 std::vector<SpatialLayer> spatial_layers;
Sergey Silkin86684962018-03-28 19:32:37 +020040
Sergey Silkindfe8ca02018-05-14 19:31:07 +020041 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
42 SpatialLayer spatial_layer = {0};
43 spatial_layer.width = input_width;
44 spatial_layer.height = input_height;
Sergey Silkin1946a3f2018-08-22 11:42:16 +020045 spatial_layer.maxFramerate =
46 std::min(kMaxScreenSharingLayerFramerateFps[sl_idx], max_framerate_fps);
Sergey Silkindfe8ca02018-05-14 19:31:07 +020047 spatial_layer.numberOfTemporalLayers = 1;
Ilya Nikolaevskiy3a656d12019-02-14 14:44:22 +010048 spatial_layer.minBitrate =
49 static_cast<int>(kMinScreenSharingLayerBitrateKbps[sl_idx]);
Sergey Silkindfe8ca02018-05-14 19:31:07 +020050 spatial_layer.maxBitrate =
51 static_cast<int>(kMaxScreenSharingLayerBitrateKbps[sl_idx]);
Ilya Nikolaevskiy3a656d12019-02-14 14:44:22 +010052 spatial_layer.targetBitrate =
53 static_cast<int>(kTargetScreenSharingLayerBitrateKbps[sl_idx]);
Sergey Silkin8b9b5f92018-12-10 09:28:53 +010054 spatial_layer.active = true;
Sergey Silkindfe8ca02018-05-14 19:31:07 +020055 spatial_layers.push_back(spatial_layer);
56 }
57
58 return spatial_layers;
59}
60
61std::vector<SpatialLayer> ConfigureSvcNormalVideo(size_t input_width,
62 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +020063 float max_framerate_fps,
Sergey Silkindfe8ca02018-05-14 19:31:07 +020064 size_t num_spatial_layers,
65 size_t num_temporal_layers) {
Sergey Silkin86684962018-03-28 19:32:37 +020066 std::vector<SpatialLayer> spatial_layers;
67
68 // Limit number of layers for given resolution.
69 const size_t num_layers_fit_horz = static_cast<size_t>(std::floor(
70 1 + std::max(0.0f,
71 std::log2(1.0f * input_width / kMinVp9SpatialLayerWidth))));
72 const size_t num_layers_fit_vert = static_cast<size_t>(
73 std::floor(1 + std::max(0.0f, std::log2(1.0f * input_height /
74 kMinVp9SpatialLayerHeight))));
75 num_spatial_layers =
76 std::min({num_spatial_layers, num_layers_fit_horz, num_layers_fit_vert});
77
78 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
79 SpatialLayer spatial_layer = {0};
80 spatial_layer.width = input_width >> (num_spatial_layers - sl_idx - 1);
81 spatial_layer.height = input_height >> (num_spatial_layers - sl_idx - 1);
Sergey Silkin1946a3f2018-08-22 11:42:16 +020082 spatial_layer.maxFramerate = max_framerate_fps;
Sergey Silkin86684962018-03-28 19:32:37 +020083 spatial_layer.numberOfTemporalLayers = num_temporal_layers;
Sergey Silkin8b9b5f92018-12-10 09:28:53 +010084 spatial_layer.active = true;
Sergey Silkin86684962018-03-28 19:32:37 +020085
“Michaelb54500e2018-05-14 08:35:00 -050086 // minBitrate and maxBitrate formulas were derived from
87 // subjective-quality data to determing bit rates below which video
88 // quality is unacceptable and above which additional bits do not provide
89 // benefit. The formulas express rate in units of kbps.
90
Sergey Silkin86684962018-03-28 19:32:37 +020091 // TODO(ssilkin): Add to the comment PSNR/SSIM we get at encoding certain
92 // video to min/max bitrate specified by those formulas.
93 const size_t num_pixels = spatial_layer.width * spatial_layer.height;
“Michael277a6562018-06-01 14:09:19 -050094 int min_bitrate =
“Michaelb54500e2018-05-14 08:35:00 -050095 static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.);
“Michael277a6562018-06-01 14:09:19 -050096 min_bitrate = std::max(min_bitrate, 0);
97 spatial_layer.minBitrate =
98 std::max(static_cast<size_t>(min_bitrate), kMinVp9SvcBitrateKbps);
Sergey Silkin86684962018-03-28 19:32:37 +020099 spatial_layer.maxBitrate =
Yves Gerey665174f2018-06-19 15:03:05 +0200100 static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000);
Sergey Silkin86684962018-03-28 19:32:37 +0200101 spatial_layer.targetBitrate =
“Michael277a6562018-06-01 14:09:19 -0500102 (spatial_layer.minBitrate + spatial_layer.maxBitrate) / 2;
Sergey Silkin86684962018-03-28 19:32:37 +0200103 spatial_layers.push_back(spatial_layer);
104 }
Sergey Silkin8b9b5f92018-12-10 09:28:53 +0100105
Sergey Silkin86684962018-03-28 19:32:37 +0200106 return spatial_layers;
107}
108
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200109std::vector<SpatialLayer> GetSvcConfig(size_t input_width,
110 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200111 float max_framerate_fps,
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200112 size_t num_spatial_layers,
113 size_t num_temporal_layers,
114 bool is_screen_sharing) {
115 RTC_DCHECK_GT(input_width, 0);
116 RTC_DCHECK_GT(input_height, 0);
117 RTC_DCHECK_GT(num_spatial_layers, 0);
118 RTC_DCHECK_GT(num_temporal_layers, 0);
119
120 if (is_screen_sharing) {
121 return ConfigureSvcScreenSharing(input_width, input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200122 max_framerate_fps, num_spatial_layers);
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200123 } else {
Sergey Silkin1946a3f2018-08-22 11:42:16 +0200124 return ConfigureSvcNormalVideo(input_width, input_height, max_framerate_fps,
Sergey Silkindfe8ca02018-05-14 19:31:07 +0200125 num_spatial_layers, num_temporal_layers);
126 }
127}
128
Sergey Silkin86684962018-03-28 19:32:37 +0200129} // namespace webrtc