blob: 6807698246e8ff3355e9ac2ac0452fb91aea6ab0 [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
“Michael277a6562018-06-01 14:09:19 -050017#include "modules/video_coding/codecs/vp9/svc_rate_allocator.h"
Sergey Silkin86684962018-03-28 19:32:37 +020018#include "modules/video_coding/include/video_codec_interface.h"
19
20namespace webrtc {
21
“Michaelb54500e2018-05-14 08:35:00 -050022namespace {
Sergey Silkindfe8ca02018-05-14 19:31:07 +020023const size_t kMinVp9SvcBitrateKbps = 30;
24
25const size_t kMaxNumLayersForScreenSharing = 2;
Sergey Silkin1946a3f2018-08-22 11:42:16 +020026const float kMaxScreenSharingLayerFramerateFps[] = {5.0, 5.0};
Sergey Silkindfe8ca02018-05-14 19:31:07 +020027const size_t kMaxScreenSharingLayerBitrateKbps[] = {200, 500};
“Michaelb54500e2018-05-14 08:35:00 -050028} // namespace
29
Sergey Silkindfe8ca02018-05-14 19:31:07 +020030std::vector<SpatialLayer> ConfigureSvcScreenSharing(size_t input_width,
31 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +020032 float max_framerate_fps,
Sergey Silkindfe8ca02018-05-14 19:31:07 +020033 size_t num_spatial_layers) {
34 num_spatial_layers =
35 std::min(num_spatial_layers, kMaxNumLayersForScreenSharing);
36 std::vector<SpatialLayer> spatial_layers;
Sergey Silkin86684962018-03-28 19:32:37 +020037
Sergey Silkindfe8ca02018-05-14 19:31:07 +020038 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
39 SpatialLayer spatial_layer = {0};
40 spatial_layer.width = input_width;
41 spatial_layer.height = input_height;
Sergey Silkin1946a3f2018-08-22 11:42:16 +020042 spatial_layer.maxFramerate =
43 std::min(kMaxScreenSharingLayerFramerateFps[sl_idx], max_framerate_fps);
Sergey Silkindfe8ca02018-05-14 19:31:07 +020044 spatial_layer.numberOfTemporalLayers = 1;
45 spatial_layer.minBitrate = static_cast<int>(kMinVp9SvcBitrateKbps);
46 spatial_layer.maxBitrate =
47 static_cast<int>(kMaxScreenSharingLayerBitrateKbps[sl_idx]);
48 spatial_layer.targetBitrate = spatial_layer.maxBitrate;
49 spatial_layers.push_back(spatial_layer);
50 }
51
52 return spatial_layers;
53}
54
55std::vector<SpatialLayer> ConfigureSvcNormalVideo(size_t input_width,
56 size_t input_height,
Sergey Silkin1946a3f2018-08-22 11:42:16 +020057 float max_framerate_fps,
Sergey Silkindfe8ca02018-05-14 19:31:07 +020058 size_t num_spatial_layers,
59 size_t num_temporal_layers) {
Sergey Silkin86684962018-03-28 19:32:37 +020060 std::vector<SpatialLayer> spatial_layers;
61
62 // Limit number of layers for given resolution.
63 const size_t num_layers_fit_horz = static_cast<size_t>(std::floor(
64 1 + std::max(0.0f,
65 std::log2(1.0f * input_width / kMinVp9SpatialLayerWidth))));
66 const size_t num_layers_fit_vert = static_cast<size_t>(
67 std::floor(1 + std::max(0.0f, std::log2(1.0f * input_height /
68 kMinVp9SpatialLayerHeight))));
69 num_spatial_layers =
70 std::min({num_spatial_layers, num_layers_fit_horz, num_layers_fit_vert});
71
“Michael277a6562018-06-01 14:09:19 -050072 float top_fraction = 0.;
Sergey Silkin86684962018-03-28 19:32:37 +020073 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
74 SpatialLayer spatial_layer = {0};
75 spatial_layer.width = input_width >> (num_spatial_layers - sl_idx - 1);
76 spatial_layer.height = input_height >> (num_spatial_layers - sl_idx - 1);
Sergey Silkin1946a3f2018-08-22 11:42:16 +020077 spatial_layer.maxFramerate = max_framerate_fps;
Sergey Silkin86684962018-03-28 19:32:37 +020078 spatial_layer.numberOfTemporalLayers = num_temporal_layers;
79
“Michaelb54500e2018-05-14 08:35:00 -050080 // minBitrate and maxBitrate formulas were derived from
81 // subjective-quality data to determing bit rates below which video
82 // quality is unacceptable and above which additional bits do not provide
83 // benefit. The formulas express rate in units of kbps.
84
Sergey Silkin86684962018-03-28 19:32:37 +020085 // TODO(ssilkin): Add to the comment PSNR/SSIM we get at encoding certain
86 // video to min/max bitrate specified by those formulas.
87 const size_t num_pixels = spatial_layer.width * spatial_layer.height;
“Michael277a6562018-06-01 14:09:19 -050088 int min_bitrate =
“Michaelb54500e2018-05-14 08:35:00 -050089 static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.);
“Michael277a6562018-06-01 14:09:19 -050090 min_bitrate = std::max(min_bitrate, 0);
91 spatial_layer.minBitrate =
92 std::max(static_cast<size_t>(min_bitrate), kMinVp9SvcBitrateKbps);
Sergey Silkin86684962018-03-28 19:32:37 +020093 spatial_layer.maxBitrate =
Yves Gerey665174f2018-06-19 15:03:05 +020094 static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000);
Sergey Silkin86684962018-03-28 19:32:37 +020095 spatial_layer.targetBitrate =
“Michael277a6562018-06-01 14:09:19 -050096 (spatial_layer.minBitrate + spatial_layer.maxBitrate) / 2;
Sergey Silkin86684962018-03-28 19:32:37 +020097 spatial_layers.push_back(spatial_layer);
“Michael277a6562018-06-01 14:09:19 -050098 top_fraction += std::pow(kSpatialLayeringRateScalingFactor, sl_idx);
Sergey Silkin86684962018-03-28 19:32:37 +020099 }
“Michael277a6562018-06-01 14:09:19 -0500100 // Compute spatial_layers[num_spatial_layers - 1].targetBitrate, which is
101 // used to set max_padding_bitrate_. Set max_padding_bitrate_ equal to the
102 // minimum total bit rate required to support all spatial layers.
103 spatial_layers[num_spatial_layers - 1].targetBitrate =
104 static_cast<unsigned int>(
105 spatial_layers[num_spatial_layers - 1].minBitrate * top_fraction);
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