vp9_cx_iface: set default cpu_used=5 w/CONFIG_REALTIME_ONLY
[platform/upstream/libvpx.git] / test / svc_test.cc
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 {
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
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     if (number_temporal_layers_ > 1 || number_spatial_layers_ > 1) {
47       svc_params_.speed_per_layer[0] = base_speed_setting_;
48       for (int i = 1; i < VPX_SS_MAX_LAYERS; ++i) {
49         svc_params_.speed_per_layer[i] = speed_setting_;
50       }
51       encoder->Control(VP9E_SET_SVC, 1);
52       encoder->Control(VP9E_SET_SVC_PARAMETERS, &svc_params_);
53     }
54     encoder->Control(VP8E_SET_CPUUSED, speed_setting_);
55     encoder->Control(VP9E_SET_AQ_MODE, 3);
56     encoder->Control(VP8E_SET_MAX_INTRA_BITRATE_PCT, 300);
57     encoder->Control(VP9E_SET_TILE_COLUMNS, get_msb(cfg_.g_threads));
58     encoder->Control(VP9E_SET_ROW_MT, 1);
59     encoder->Control(VP8E_SET_STATIC_THRESHOLD, 1);
60   }
61
62   superframe_count_++;
63   temporal_layer_id_ = 0;
64   if (number_temporal_layers_ == 2) {
65     temporal_layer_id_ = (superframe_count_ % 2 != 0);
66   } else if (number_temporal_layers_ == 3) {
67     if (superframe_count_ % 2 != 0) temporal_layer_id_ = 2;
68     if (superframe_count_ > 1) {
69       if ((superframe_count_ - 2) % 4 == 0) temporal_layer_id_ = 1;
70     }
71   }
72
73   frame_flags_ = 0;
74 }
75
76 void OnePassCbrSvc::PostEncodeFrameHook(::libvpx_test::Encoder *encoder) {
77   vpx_svc_layer_id_t layer_id;
78   encoder->Control(VP9E_GET_SVC_LAYER_ID, &layer_id);
79   temporal_layer_id_ = layer_id.temporal_layer_id;
80   for (int sl = 0; sl < number_spatial_layers_; ++sl) {
81     for (int tl = temporal_layer_id_; tl < number_temporal_layers_; ++tl) {
82       const int layer = sl * number_temporal_layers_ + tl;
83       bits_in_buffer_model_[layer] +=
84           static_cast<int64_t>(layer_target_avg_bandwidth_[layer]);
85     }
86   }
87 }
88
89 void OnePassCbrSvc::AssignLayerBitrates() {
90   int sl, spatial_layer_target;
91   int spatial_layers = cfg_.ss_number_layers;
92   int temporal_layers = cfg_.ts_number_layers;
93   float total = 0;
94   float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
95   float framerate = 30.0;
96   for (sl = 0; sl < spatial_layers; ++sl) {
97     if (svc_params_.scaling_factor_den[sl] > 0) {
98       alloc_ratio[sl] =
99           static_cast<float>((svc_params_.scaling_factor_num[sl] * 1.0 /
100                               svc_params_.scaling_factor_den[sl]));
101       total += alloc_ratio[sl];
102     }
103   }
104   for (sl = 0; sl < spatial_layers; ++sl) {
105     cfg_.ss_target_bitrate[sl] = spatial_layer_target =
106         static_cast<unsigned int>(cfg_.rc_target_bitrate * alloc_ratio[sl] /
107                                   total);
108     const int index = sl * temporal_layers;
109     if (cfg_.temporal_layering_mode == 3) {
110       cfg_.layer_target_bitrate[index] = spatial_layer_target >> 1;
111       cfg_.layer_target_bitrate[index + 1] =
112           (spatial_layer_target >> 1) + (spatial_layer_target >> 2);
113       cfg_.layer_target_bitrate[index + 2] = spatial_layer_target;
114     } else if (cfg_.temporal_layering_mode == 2) {
115       cfg_.layer_target_bitrate[index] = spatial_layer_target * 2 / 3;
116       cfg_.layer_target_bitrate[index + 1] = spatial_layer_target;
117     } else if (cfg_.temporal_layering_mode <= 1) {
118       cfg_.layer_target_bitrate[index] = spatial_layer_target;
119     }
120   }
121   for (sl = 0; sl < spatial_layers; ++sl) {
122     for (int tl = 0; tl < temporal_layers; ++tl) {
123       const int layer = sl * temporal_layers + tl;
124       float layer_framerate = framerate;
125       if (temporal_layers == 2 && tl == 0) layer_framerate = framerate / 2;
126       if (temporal_layers == 3 && tl == 0) layer_framerate = framerate / 4;
127       if (temporal_layers == 3 && tl == 1) layer_framerate = framerate / 2;
128       layer_target_avg_bandwidth_[layer] = static_cast<int>(
129           cfg_.layer_target_bitrate[layer] * 1000.0 / layer_framerate);
130       bits_in_buffer_model_[layer] =
131           cfg_.layer_target_bitrate[layer] * cfg_.rc_buf_initial_sz;
132     }
133   }
134 }
135 }  // namespace svc_test