blob: ed60a0d9fd23e770a0619e784c3172af66861794 [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
17#include "modules/video_coding/include/video_codec_interface.h"
18
19namespace webrtc {
20
“Michaelb54500e2018-05-14 08:35:00 -050021namespace {
Sergey Silkindfe8ca02018-05-14 19:31:07 +020022const size_t kMinVp9SvcBitrateKbps = 30;
23
24const size_t kMaxNumLayersForScreenSharing = 2;
25const size_t kMaxScreenSharingLayerBitrateKbps[] = {200, 500};
“Michaelb54500e2018-05-14 08:35:00 -050026} // namespace
27
Sergey Silkindfe8ca02018-05-14 19:31:07 +020028std::vector<SpatialLayer> ConfigureSvcScreenSharing(size_t input_width,
29 size_t input_height,
30 size_t num_spatial_layers) {
31 num_spatial_layers =
32 std::min(num_spatial_layers, kMaxNumLayersForScreenSharing);
33 std::vector<SpatialLayer> spatial_layers;
Sergey Silkin86684962018-03-28 19:32:37 +020034
Sergey Silkindfe8ca02018-05-14 19:31:07 +020035 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
36 SpatialLayer spatial_layer = {0};
37 spatial_layer.width = input_width;
38 spatial_layer.height = input_height;
39 spatial_layer.numberOfTemporalLayers = 1;
40 spatial_layer.minBitrate = static_cast<int>(kMinVp9SvcBitrateKbps);
41 spatial_layer.maxBitrate =
42 static_cast<int>(kMaxScreenSharingLayerBitrateKbps[sl_idx]);
43 spatial_layer.targetBitrate = spatial_layer.maxBitrate;
44 spatial_layers.push_back(spatial_layer);
45 }
46
47 return spatial_layers;
48}
49
50std::vector<SpatialLayer> ConfigureSvcNormalVideo(size_t input_width,
51 size_t input_height,
52 size_t num_spatial_layers,
53 size_t num_temporal_layers) {
Sergey Silkin86684962018-03-28 19:32:37 +020054 std::vector<SpatialLayer> spatial_layers;
55
56 // Limit number of layers for given resolution.
57 const size_t num_layers_fit_horz = static_cast<size_t>(std::floor(
58 1 + std::max(0.0f,
59 std::log2(1.0f * input_width / kMinVp9SpatialLayerWidth))));
60 const size_t num_layers_fit_vert = static_cast<size_t>(
61 std::floor(1 + std::max(0.0f, std::log2(1.0f * input_height /
62 kMinVp9SpatialLayerHeight))));
63 num_spatial_layers =
64 std::min({num_spatial_layers, num_layers_fit_horz, num_layers_fit_vert});
65
66 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
67 SpatialLayer spatial_layer = {0};
68 spatial_layer.width = input_width >> (num_spatial_layers - sl_idx - 1);
69 spatial_layer.height = input_height >> (num_spatial_layers - sl_idx - 1);
70 spatial_layer.numberOfTemporalLayers = num_temporal_layers;
71
“Michaelb54500e2018-05-14 08:35:00 -050072 // minBitrate and maxBitrate formulas were derived from
73 // subjective-quality data to determing bit rates below which video
74 // quality is unacceptable and above which additional bits do not provide
75 // benefit. The formulas express rate in units of kbps.
76
Sergey Silkin86684962018-03-28 19:32:37 +020077 // TODO(ssilkin): Add to the comment PSNR/SSIM we get at encoding certain
78 // video to min/max bitrate specified by those formulas.
79 const size_t num_pixels = spatial_layer.width * spatial_layer.height;
Sergey Silkindfe8ca02018-05-14 19:31:07 +020080 const size_t min_bitrate =
“Michaelb54500e2018-05-14 08:35:00 -050081 static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.);
82 spatial_layer.minBitrate = std::max(min_bitrate, kMinVp9SvcBitrateKbps);
Sergey Silkin86684962018-03-28 19:32:37 +020083 spatial_layer.maxBitrate =
“Michaelb54500e2018-05-14 08:35:00 -050084 static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000);
Sergey Silkin86684962018-03-28 19:32:37 +020085 spatial_layer.targetBitrate =
Sergey Silkin1322dbc2018-04-23 12:30:35 +020086 (spatial_layer.maxBitrate + spatial_layer.minBitrate) / 2;
Sergey Silkin86684962018-03-28 19:32:37 +020087
88 spatial_layers.push_back(spatial_layer);
89 }
90
91 return spatial_layers;
92}
93
Sergey Silkindfe8ca02018-05-14 19:31:07 +020094std::vector<SpatialLayer> GetSvcConfig(size_t input_width,
95 size_t input_height,
96 size_t num_spatial_layers,
97 size_t num_temporal_layers,
98 bool is_screen_sharing) {
99 RTC_DCHECK_GT(input_width, 0);
100 RTC_DCHECK_GT(input_height, 0);
101 RTC_DCHECK_GT(num_spatial_layers, 0);
102 RTC_DCHECK_GT(num_temporal_layers, 0);
103
104 if (is_screen_sharing) {
105 return ConfigureSvcScreenSharing(input_width, input_height,
106 num_spatial_layers);
107 } else {
108 return ConfigureSvcNormalVideo(input_width, input_height,
109 num_spatial_layers, num_temporal_layers);
110 }
111}
112
Sergey Silkin86684962018-03-28 19:32:37 +0200113} // namespace webrtc