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 | |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame^] | 17 | #include "modules/video_coding/codecs/vp9/svc_rate_allocator.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 18 | #include "modules/video_coding/include/video_codec_interface.h" |
| 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 | |
| 25 | const size_t kMaxNumLayersForScreenSharing = 2; |
| 26 | const size_t kMaxScreenSharingLayerBitrateKbps[] = {200, 500}; |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 27 | } // namespace |
| 28 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 29 | std::vector<SpatialLayer> ConfigureSvcScreenSharing(size_t input_width, |
| 30 | size_t input_height, |
| 31 | size_t num_spatial_layers) { |
| 32 | num_spatial_layers = |
| 33 | std::min(num_spatial_layers, kMaxNumLayersForScreenSharing); |
| 34 | std::vector<SpatialLayer> spatial_layers; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 35 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 36 | for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) { |
| 37 | SpatialLayer spatial_layer = {0}; |
| 38 | spatial_layer.width = input_width; |
| 39 | spatial_layer.height = input_height; |
| 40 | spatial_layer.numberOfTemporalLayers = 1; |
| 41 | spatial_layer.minBitrate = static_cast<int>(kMinVp9SvcBitrateKbps); |
| 42 | spatial_layer.maxBitrate = |
| 43 | static_cast<int>(kMaxScreenSharingLayerBitrateKbps[sl_idx]); |
| 44 | spatial_layer.targetBitrate = spatial_layer.maxBitrate; |
| 45 | spatial_layers.push_back(spatial_layer); |
| 46 | } |
| 47 | |
| 48 | return spatial_layers; |
| 49 | } |
| 50 | |
| 51 | std::vector<SpatialLayer> ConfigureSvcNormalVideo(size_t input_width, |
| 52 | size_t input_height, |
| 53 | size_t num_spatial_layers, |
| 54 | size_t num_temporal_layers) { |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 55 | std::vector<SpatialLayer> spatial_layers; |
| 56 | |
| 57 | // Limit number of layers for given resolution. |
| 58 | const size_t num_layers_fit_horz = static_cast<size_t>(std::floor( |
| 59 | 1 + std::max(0.0f, |
| 60 | std::log2(1.0f * input_width / kMinVp9SpatialLayerWidth)))); |
| 61 | const size_t num_layers_fit_vert = static_cast<size_t>( |
| 62 | std::floor(1 + std::max(0.0f, std::log2(1.0f * input_height / |
| 63 | kMinVp9SpatialLayerHeight)))); |
| 64 | num_spatial_layers = |
| 65 | std::min({num_spatial_layers, num_layers_fit_horz, num_layers_fit_vert}); |
| 66 | |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame^] | 67 | float top_fraction = 0.; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 68 | for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) { |
| 69 | SpatialLayer spatial_layer = {0}; |
| 70 | spatial_layer.width = input_width >> (num_spatial_layers - sl_idx - 1); |
| 71 | spatial_layer.height = input_height >> (num_spatial_layers - sl_idx - 1); |
| 72 | spatial_layer.numberOfTemporalLayers = num_temporal_layers; |
| 73 | |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 74 | // minBitrate and maxBitrate formulas were derived from |
| 75 | // subjective-quality data to determing bit rates below which video |
| 76 | // quality is unacceptable and above which additional bits do not provide |
| 77 | // benefit. The formulas express rate in units of kbps. |
| 78 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 79 | // TODO(ssilkin): Add to the comment PSNR/SSIM we get at encoding certain |
| 80 | // video to min/max bitrate specified by those formulas. |
| 81 | const size_t num_pixels = spatial_layer.width * spatial_layer.height; |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame^] | 82 | int min_bitrate = |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 83 | static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.); |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame^] | 84 | min_bitrate = std::max(min_bitrate, 0); |
| 85 | spatial_layer.minBitrate = |
| 86 | std::max(static_cast<size_t>(min_bitrate), kMinVp9SvcBitrateKbps); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 87 | spatial_layer.maxBitrate = |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 88 | static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 89 | spatial_layer.targetBitrate = |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame^] | 90 | (spatial_layer.minBitrate + spatial_layer.maxBitrate) / 2; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 91 | spatial_layers.push_back(spatial_layer); |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame^] | 92 | top_fraction += std::pow(kSpatialLayeringRateScalingFactor, sl_idx); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 93 | } |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame^] | 94 | // Compute spatial_layers[num_spatial_layers - 1].targetBitrate, which is |
| 95 | // used to set max_padding_bitrate_. Set max_padding_bitrate_ equal to the |
| 96 | // minimum total bit rate required to support all spatial layers. |
| 97 | spatial_layers[num_spatial_layers - 1].targetBitrate = |
| 98 | static_cast<unsigned int>( |
| 99 | spatial_layers[num_spatial_layers - 1].minBitrate * top_fraction); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 100 | return spatial_layers; |
| 101 | } |
| 102 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame] | 103 | std::vector<SpatialLayer> GetSvcConfig(size_t input_width, |
| 104 | size_t input_height, |
| 105 | size_t num_spatial_layers, |
| 106 | size_t num_temporal_layers, |
| 107 | bool is_screen_sharing) { |
| 108 | RTC_DCHECK_GT(input_width, 0); |
| 109 | RTC_DCHECK_GT(input_height, 0); |
| 110 | RTC_DCHECK_GT(num_spatial_layers, 0); |
| 111 | RTC_DCHECK_GT(num_temporal_layers, 0); |
| 112 | |
| 113 | if (is_screen_sharing) { |
| 114 | return ConfigureSvcScreenSharing(input_width, input_height, |
| 115 | num_spatial_layers); |
| 116 | } else { |
| 117 | return ConfigureSvcNormalVideo(input_width, input_height, |
| 118 | num_spatial_layers, num_temporal_layers); |
| 119 | } |
| 120 | } |
| 121 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 122 | } // namespace webrtc |