blob: 137b167e788d5230cc32af41eeb537681e98b4a0 [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
21std::vector<SpatialLayer> GetSvcConfig(size_t input_width,
22 size_t input_height,
23 size_t num_spatial_layers,
24 size_t num_temporal_layers) {
25 RTC_DCHECK_GT(input_width, 0);
26 RTC_DCHECK_GT(input_height, 0);
27 RTC_DCHECK_GT(num_spatial_layers, 0);
28 RTC_DCHECK_GT(num_temporal_layers, 0);
29
30 std::vector<SpatialLayer> spatial_layers;
31
32 // Limit number of layers for given resolution.
33 const size_t num_layers_fit_horz = static_cast<size_t>(std::floor(
34 1 + std::max(0.0f,
35 std::log2(1.0f * input_width / kMinVp9SpatialLayerWidth))));
36 const size_t num_layers_fit_vert = static_cast<size_t>(
37 std::floor(1 + std::max(0.0f, std::log2(1.0f * input_height /
38 kMinVp9SpatialLayerHeight))));
39 num_spatial_layers =
40 std::min({num_spatial_layers, num_layers_fit_horz, num_layers_fit_vert});
41
42 for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
43 SpatialLayer spatial_layer = {0};
44 spatial_layer.width = input_width >> (num_spatial_layers - sl_idx - 1);
45 spatial_layer.height = input_height >> (num_spatial_layers - sl_idx - 1);
46 spatial_layer.numberOfTemporalLayers = num_temporal_layers;
47
48 // minBitrate and maxBitrate formulas were derived to fit VP9
49 // subjective-quality data for bit rate below which video quality is
50 // unacceptable and above which additional bits do not provide benefit.
51 // TODO(ssilkin): Add to the comment PSNR/SSIM we get at encoding certain
52 // video to min/max bitrate specified by those formulas.
53 const size_t num_pixels = spatial_layer.width * spatial_layer.height;
54 spatial_layer.minBitrate =
55 static_cast<int>(360 * std::sqrt(num_pixels) / 1000);
56 spatial_layer.maxBitrate =
57 static_cast<int>((1.5 * num_pixels + 75 * 1000) / 1000);
58 spatial_layer.targetBitrate =
59 (spatial_layer.maxBitrate - spatial_layer.minBitrate) / 2;
60
61 spatial_layers.push_back(spatial_layer);
62 }
63
64 return spatial_layers;
65}
66
67} // namespace webrtc