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 { |
| 22 | const int kMinVp9SvcBitrateKbps = 30; // Lowest VP9 video rate in kbps. |
| 23 | } // namespace |
| 24 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 25 | std::vector<SpatialLayer> GetSvcConfig(size_t input_width, |
| 26 | size_t input_height, |
| 27 | size_t num_spatial_layers, |
| 28 | size_t num_temporal_layers) { |
| 29 | RTC_DCHECK_GT(input_width, 0); |
| 30 | RTC_DCHECK_GT(input_height, 0); |
| 31 | RTC_DCHECK_GT(num_spatial_layers, 0); |
| 32 | RTC_DCHECK_GT(num_temporal_layers, 0); |
| 33 | |
| 34 | std::vector<SpatialLayer> spatial_layers; |
| 35 | |
| 36 | // Limit number of layers for given resolution. |
| 37 | const size_t num_layers_fit_horz = static_cast<size_t>(std::floor( |
| 38 | 1 + std::max(0.0f, |
| 39 | std::log2(1.0f * input_width / kMinVp9SpatialLayerWidth)))); |
| 40 | const size_t num_layers_fit_vert = static_cast<size_t>( |
| 41 | std::floor(1 + std::max(0.0f, std::log2(1.0f * input_height / |
| 42 | kMinVp9SpatialLayerHeight)))); |
| 43 | num_spatial_layers = |
| 44 | std::min({num_spatial_layers, num_layers_fit_horz, num_layers_fit_vert}); |
| 45 | |
| 46 | for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) { |
| 47 | SpatialLayer spatial_layer = {0}; |
| 48 | spatial_layer.width = input_width >> (num_spatial_layers - sl_idx - 1); |
| 49 | spatial_layer.height = input_height >> (num_spatial_layers - sl_idx - 1); |
| 50 | spatial_layer.numberOfTemporalLayers = num_temporal_layers; |
| 51 | |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame^] | 52 | // minBitrate and maxBitrate formulas were derived from |
| 53 | // subjective-quality data to determing bit rates below which video |
| 54 | // quality is unacceptable and above which additional bits do not provide |
| 55 | // benefit. The formulas express rate in units of kbps. |
| 56 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 57 | // TODO(ssilkin): Add to the comment PSNR/SSIM we get at encoding certain |
| 58 | // video to min/max bitrate specified by those formulas. |
| 59 | const size_t num_pixels = spatial_layer.width * spatial_layer.height; |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame^] | 60 | const int min_bitrate = |
| 61 | static_cast<int>((600. * std::sqrt(num_pixels) - 95000.) / 1000.); |
| 62 | spatial_layer.minBitrate = std::max(min_bitrate, kMinVp9SvcBitrateKbps); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 63 | spatial_layer.maxBitrate = |
“Michael | b54500e | 2018-05-14 08:35:00 -0500 | [diff] [blame^] | 64 | static_cast<int>((1.6 * num_pixels + 50 * 1000) / 1000); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 65 | spatial_layer.targetBitrate = |
Sergey Silkin | 1322dbc | 2018-04-23 12:30:35 +0200 | [diff] [blame] | 66 | (spatial_layer.maxBitrate + spatial_layer.minBitrate) / 2; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 67 | |
| 68 | spatial_layers.push_back(spatial_layer); |
| 69 | } |
| 70 | |
| 71 | return spatial_layers; |
| 72 | } |
| 73 | |
| 74 | } // namespace webrtc |