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 | |
| 17 | #include "modules/video_coding/include/video_codec_interface.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 21 | namespace { |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame^] | 22 | const size_t kMinVp9SvcBitrateKbps = 30; |
| 23 | |
| 24 | const size_t kMaxNumLayersForScreenSharing = 2; |
| 25 | const size_t kMaxScreenSharingLayerBitrateKbps[] = {200, 500}; |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 26 | } // namespace |
| 27 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame^] | 28 | std::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 Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 34 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame^] | 35 | 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 | |
| 50 | std::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 Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 54 | 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 | |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 72 | // 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 Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 77 | // 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 Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame^] | 80 | const size_t min_bitrate = |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 81 | static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.); |
| 82 | spatial_layer.minBitrate = std::max(min_bitrate, kMinVp9SvcBitrateKbps); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 83 | spatial_layer.maxBitrate = |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame] | 84 | static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 85 | spatial_layer.targetBitrate = |
Sergey Silkin | 1322dbc | 2018-04-23 12:30:35 +0200 | [diff] [blame] | 86 | (spatial_layer.maxBitrate + spatial_layer.minBitrate) / 2; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 87 | |
| 88 | spatial_layers.push_back(spatial_layer); |
| 89 | } |
| 90 | |
| 91 | return spatial_layers; |
| 92 | } |
| 93 | |
Sergey Silkin | dfe8ca0 | 2018-05-14 19:31:07 +0200 | [diff] [blame^] | 94 | std::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 Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 113 | } // namespace webrtc |