1 /*
2 * Copyright (c) 2018 The WebM 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 "test/svc_test.h"
12
13 namespace svc_test {
SetSvcConfig(const int num_spatial_layer,const int num_temporal_layer)14 void OnePassCbrSvc::SetSvcConfig(const int num_spatial_layer,
15 const int num_temporal_layer) {
16 SetConfig(num_temporal_layer);
17 cfg_.ss_number_layers = num_spatial_layer;
18 cfg_.ts_number_layers = num_temporal_layer;
19 if (num_spatial_layer == 1) {
20 svc_params_.scaling_factor_num[0] = 288;
21 svc_params_.scaling_factor_den[0] = 288;
22 } else if (num_spatial_layer == 2) {
23 svc_params_.scaling_factor_num[0] = 144;
24 svc_params_.scaling_factor_den[0] = 288;
25 svc_params_.scaling_factor_num[1] = 288;
26 svc_params_.scaling_factor_den[1] = 288;
27 } else if (num_spatial_layer == 3) {
28 svc_params_.scaling_factor_num[0] = 72;
29 svc_params_.scaling_factor_den[0] = 288;
30 svc_params_.scaling_factor_num[1] = 144;
31 svc_params_.scaling_factor_den[1] = 288;
32 svc_params_.scaling_factor_num[2] = 288;
33 svc_params_.scaling_factor_den[2] = 288;
34 }
35 number_spatial_layers_ = cfg_.ss_number_layers;
36 number_temporal_layers_ = cfg_.ts_number_layers;
37 }
38
PreEncodeFrameHookSetup(::libvpx_test::VideoSource * video,::libvpx_test::Encoder * encoder)39 void OnePassCbrSvc::PreEncodeFrameHookSetup(::libvpx_test::VideoSource *video,
40 ::libvpx_test::Encoder *encoder) {
41 if (video->frame() == 0) {
42 for (int i = 0; i < VPX_MAX_LAYERS; ++i) {
43 svc_params_.max_quantizers[i] = 63;
44 svc_params_.min_quantizers[i] = 0;
45 }
46 svc_params_.speed_per_layer[0] = base_speed_setting_;
47 for (int i = 1; i < VPX_SS_MAX_LAYERS; ++i) {
48 svc_params_.speed_per_layer[i] = speed_setting_;
49 }
50
51 encoder->Control(VP9E_SET_SVC, 1);
52 encoder->Control(VP9E_SET_SVC_PARAMETERS, &svc_params_);
53 encoder->Control(VP8E_SET_CPUUSED, speed_setting_);
54 encoder->Control(VP9E_SET_AQ_MODE, 3);
55 encoder->Control(VP8E_SET_MAX_INTRA_BITRATE_PCT, 300);
56 encoder->Control(VP9E_SET_TILE_COLUMNS, get_msb(cfg_.g_threads));
57 encoder->Control(VP9E_SET_ROW_MT, 1);
58 encoder->Control(VP8E_SET_STATIC_THRESHOLD, 1);
59 }
60
61 superframe_count_++;
62 temporal_layer_id_ = 0;
63 if (number_temporal_layers_ == 2) {
64 temporal_layer_id_ = (superframe_count_ % 2 != 0);
65 } else if (number_temporal_layers_ == 3) {
66 if (superframe_count_ % 2 != 0) temporal_layer_id_ = 2;
67 if (superframe_count_ > 1) {
68 if ((superframe_count_ - 2) % 4 == 0) temporal_layer_id_ = 1;
69 }
70 }
71
72 frame_flags_ = 0;
73 }
74
PostEncodeFrameHook(::libvpx_test::Encoder * encoder)75 void OnePassCbrSvc::PostEncodeFrameHook(::libvpx_test::Encoder *encoder) {
76 vpx_svc_layer_id_t layer_id;
77 encoder->Control(VP9E_GET_SVC_LAYER_ID, &layer_id);
78 temporal_layer_id_ = layer_id.temporal_layer_id;
79 for (int sl = 0; sl < number_spatial_layers_; ++sl) {
80 for (int tl = temporal_layer_id_; tl < number_temporal_layers_; ++tl) {
81 const int layer = sl * number_temporal_layers_ + tl;
82 bits_in_buffer_model_[layer] +=
83 static_cast<int64_t>(layer_target_avg_bandwidth_[layer]);
84 }
85 }
86 }
87
AssignLayerBitrates()88 void OnePassCbrSvc::AssignLayerBitrates() {
89 int sl, spatial_layer_target;
90 int spatial_layers = cfg_.ss_number_layers;
91 int temporal_layers = cfg_.ts_number_layers;
92 float total = 0;
93 float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
94 float framerate = 30.0;
95 for (sl = 0; sl < spatial_layers; ++sl) {
96 if (svc_params_.scaling_factor_den[sl] > 0) {
97 alloc_ratio[sl] =
98 static_cast<float>((svc_params_.scaling_factor_num[sl] * 1.0 /
99 svc_params_.scaling_factor_den[sl]));
100 total += alloc_ratio[sl];
101 }
102 }
103 for (sl = 0; sl < spatial_layers; ++sl) {
104 cfg_.ss_target_bitrate[sl] = spatial_layer_target =
105 static_cast<unsigned int>(cfg_.rc_target_bitrate * alloc_ratio[sl] /
106 total);
107 const int index = sl * temporal_layers;
108 if (cfg_.temporal_layering_mode == 3) {
109 cfg_.layer_target_bitrate[index] = spatial_layer_target >> 1;
110 cfg_.layer_target_bitrate[index + 1] =
111 (spatial_layer_target >> 1) + (spatial_layer_target >> 2);
112 cfg_.layer_target_bitrate[index + 2] = spatial_layer_target;
113 } else if (cfg_.temporal_layering_mode == 2) {
114 cfg_.layer_target_bitrate[index] = spatial_layer_target * 2 / 3;
115 cfg_.layer_target_bitrate[index + 1] = spatial_layer_target;
116 } else if (cfg_.temporal_layering_mode <= 1) {
117 cfg_.layer_target_bitrate[index] = spatial_layer_target;
118 }
119 }
120 for (sl = 0; sl < spatial_layers; ++sl) {
121 for (int tl = 0; tl < temporal_layers; ++tl) {
122 const int layer = sl * temporal_layers + tl;
123 float layer_framerate = framerate;
124 if (temporal_layers == 2 && tl == 0) layer_framerate = framerate / 2;
125 if (temporal_layers == 3 && tl == 0) layer_framerate = framerate / 4;
126 if (temporal_layers == 3 && tl == 1) layer_framerate = framerate / 2;
127 layer_target_avg_bandwidth_[layer] = static_cast<int>(
128 cfg_.layer_target_bitrate[layer] * 1000.0 / layer_framerate);
129 bits_in_buffer_model_[layer] =
130 cfg_.layer_target_bitrate[layer] * cfg_.rc_buf_initial_sz;
131 }
132 }
133 }
134 } // namespace svc_test
135