Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 1 | /* |
| 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 Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 22 | namespace { |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 23 | const size_t kMinVp9SvcBitrateKbps = 30; |
| 24 | |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 25 | const size_t kMaxNumLayersForScreenSharing = 3; |
Ilya Nikolaevskiy | e01857c | 2019-03-08 14:25:42 +0000 | [diff] [blame] | 26 | const float kMaxScreenSharingLayerFramerateFps[] = {5.0, 5.0, 30.0}; |
| 27 | const size_t kMinScreenSharingLayerBitrateKbps[] = {30, 150, 500}; |
| 28 | const size_t kTargetScreenSharingLayerBitrateKbps[] = {150, 350, 1000}; |
| 29 | const size_t kMaxScreenSharingLayerBitrateKbps[] = {200, 500, 1000}; |
Ilya Nikolaevskiy | 3a656d1 | 2019-02-14 14:44:22 +0100 | [diff] [blame] | 30 | |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 31 | } // namespace |
| 32 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 33 | std::vector<SpatialLayer> ConfigureSvcScreenSharing(size_t input_width, |
| 34 | size_t input_height, |
Sergey Silkin | 1946a3f | 2018-08-22 11:42:16 +0200 | [diff] [blame] | 35 | float max_framerate_fps, |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 36 | size_t num_spatial_layers) { |
| 37 | num_spatial_layers = |
| 38 | std::min(num_spatial_layers, kMaxNumLayersForScreenSharing); |
| 39 | std::vector<SpatialLayer> spatial_layers; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 40 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 41 | 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 Silkin | 1946a3f | 2018-08-22 11:42:16 +0200 | [diff] [blame] | 45 | spatial_layer.maxFramerate = |
| 46 | std::min(kMaxScreenSharingLayerFramerateFps[sl_idx], max_framerate_fps); |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 47 | spatial_layer.numberOfTemporalLayers = 1; |
Ilya Nikolaevskiy | 3a656d1 | 2019-02-14 14:44:22 +0100 | [diff] [blame] | 48 | spatial_layer.minBitrate = |
| 49 | static_cast<int>(kMinScreenSharingLayerBitrateKbps[sl_idx]); |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 50 | spatial_layer.maxBitrate = |
| 51 | static_cast<int>(kMaxScreenSharingLayerBitrateKbps[sl_idx]); |
Ilya Nikolaevskiy | 3a656d1 | 2019-02-14 14:44:22 +0100 | [diff] [blame] | 52 | spatial_layer.targetBitrate = |
| 53 | static_cast<int>(kTargetScreenSharingLayerBitrateKbps[sl_idx]); |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 54 | spatial_layer.active = true; |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 55 | spatial_layers.push_back(spatial_layer); |
| 56 | } |
| 57 | |
| 58 | return spatial_layers; |
| 59 | } |
| 60 | |
| 61 | std::vector<SpatialLayer> ConfigureSvcNormalVideo(size_t input_width, |
| 62 | size_t input_height, |
Sergey Silkin | 1946a3f | 2018-08-22 11:42:16 +0200 | [diff] [blame] | 63 | float max_framerate_fps, |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 64 | size_t num_spatial_layers, |
| 65 | size_t num_temporal_layers) { |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 66 | 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 Silkin | 1946a3f | 2018-08-22 11:42:16 +0200 | [diff] [blame] | 82 | spatial_layer.maxFramerate = max_framerate_fps; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 83 | spatial_layer.numberOfTemporalLayers = num_temporal_layers; |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 84 | spatial_layer.active = true; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 85 | |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 86 | // 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 Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 91 | // 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; |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 94 | int min_bitrate = |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 95 | static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.); |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 96 | min_bitrate = std::max(min_bitrate, 0); |
| 97 | spatial_layer.minBitrate = |
| 98 | std::max(static_cast<size_t>(min_bitrate), kMinVp9SvcBitrateKbps); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 99 | spatial_layer.maxBitrate = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 101 | spatial_layer.targetBitrate = |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 102 | (spatial_layer.minBitrate + spatial_layer.maxBitrate) / 2; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 103 | spatial_layers.push_back(spatial_layer); |
| 104 | } |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 105 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 106 | return spatial_layers; |
| 107 | } |
| 108 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 109 | std::vector<SpatialLayer> GetSvcConfig(size_t input_width, |
| 110 | size_t input_height, |
Sergey Silkin | 1946a3f | 2018-08-22 11:42:16 +0200 | [diff] [blame] | 111 | float max_framerate_fps, |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 112 | 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 Silkin | 1946a3f | 2018-08-22 11:42:16 +0200 | [diff] [blame] | 122 | max_framerate_fps, num_spatial_layers); |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 123 | } else { |
Sergey Silkin | 1946a3f | 2018-08-22 11:42:16 +0200 | [diff] [blame] | 124 | return ConfigureSvcNormalVideo(input_width, input_height, max_framerate_fps, |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 125 | num_spatial_layers, num_temporal_layers); |
| 126 | } |
| 127 | } |
| 128 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 129 | } // namespace webrtc |