Fork VP9 and VP10 codebase
[platform/upstream/libvpx.git] / vp10 / encoder / vp9_ratectrl.h
1 /*
2  *  Copyright (c) 2010 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
12 #ifndef VP9_ENCODER_VP9_RATECTRL_H_
13 #define VP9_ENCODER_VP9_RATECTRL_H_
14
15 #include "vpx/vpx_codec.h"
16 #include "vpx/vpx_integer.h"
17
18 #include "vp10/common/vp9_blockd.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 // Bits Per MB at different Q (Multiplied by 512)
25 #define BPER_MB_NORMBITS    9
26
27 #define MIN_GF_INTERVAL     4
28 #define MAX_GF_INTERVAL     16
29
30 typedef enum {
31   INTER_NORMAL = 0,
32   INTER_HIGH = 1,
33   GF_ARF_LOW = 2,
34   GF_ARF_STD = 3,
35   KF_STD = 4,
36   RATE_FACTOR_LEVELS = 5
37 } RATE_FACTOR_LEVEL;
38
39 // Internal frame scaling level.
40 typedef enum {
41   UNSCALED = 0,     // Frame is unscaled.
42   SCALE_STEP1 = 1,  // First-level down-scaling.
43   FRAME_SCALE_STEPS
44 } FRAME_SCALE_LEVEL;
45
46 // Frame dimensions multiplier wrt the native frame size, in 1/16ths,
47 // specified for the scale-up case.
48 // e.g. 24 => 16/24 = 2/3 of native size. The restriction to 1/16th is
49 // intended to match the capabilities of the normative scaling filters,
50 // giving precedence to the up-scaling accuracy.
51 static const int frame_scale_factor[FRAME_SCALE_STEPS] = {16, 24};
52
53 // Multiplier of the target rate to be used as threshold for triggering scaling.
54 static const double rate_thresh_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
55
56 // Scale dependent Rate Correction Factor multipliers. Compensates for the
57 // greater number of bits per pixel generated in down-scaled frames.
58 static const double rcf_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
59
60 typedef struct {
61   // Rate targetting variables
62   int base_frame_target;           // A baseline frame target before adjustment
63                                    // for previous under or over shoot.
64   int this_frame_target;           // Actual frame target after rc adjustment.
65   int projected_frame_size;
66   int sb64_target_rate;
67   int last_q[FRAME_TYPES];         // Separate values for Intra/Inter
68   int last_boosted_qindex;         // Last boosted GF/KF/ARF q
69   int last_kf_qindex;              // Q index of the last key frame coded.
70
71   int gfu_boost;
72   int last_boost;
73   int kf_boost;
74
75   double rate_correction_factors[RATE_FACTOR_LEVELS];
76
77   int frames_since_golden;
78   int frames_till_gf_update_due;
79   int min_gf_interval;
80   int max_gf_interval;
81   int static_scene_max_gf_interval;
82   int baseline_gf_interval;
83   int constrained_gf_group;
84   int frames_to_key;
85   int frames_since_key;
86   int this_key_frame_forced;
87   int next_key_frame_forced;
88   int source_alt_ref_pending;
89   int source_alt_ref_active;
90   int is_src_frame_alt_ref;
91
92   int avg_frame_bandwidth;  // Average frame size target for clip
93   int min_frame_bandwidth;  // Minimum allocation used for any frame
94   int max_frame_bandwidth;  // Maximum burst rate allowed for a frame.
95
96   int ni_av_qi;
97   int ni_tot_qi;
98   int ni_frames;
99   int avg_frame_qindex[FRAME_TYPES];
100   double tot_q;
101   double avg_q;
102
103   int64_t buffer_level;
104   int64_t bits_off_target;
105   int64_t vbr_bits_off_target;
106   int64_t vbr_bits_off_target_fast;
107
108   int decimation_factor;
109   int decimation_count;
110
111   int rolling_target_bits;
112   int rolling_actual_bits;
113
114   int long_rolling_target_bits;
115   int long_rolling_actual_bits;
116
117   int rate_error_estimate;
118
119   int64_t total_actual_bits;
120   int64_t total_target_bits;
121   int64_t total_target_vs_actual;
122
123   int worst_quality;
124   int best_quality;
125
126   int64_t starting_buffer_level;
127   int64_t optimal_buffer_level;
128   int64_t maximum_buffer_size;
129
130   // rate control history for last frame(1) and the frame before(2).
131   // -1: undershot
132   //  1: overshoot
133   //  0: not initialized.
134   int rc_1_frame;
135   int rc_2_frame;
136   int q_1_frame;
137   int q_2_frame;
138
139   // Auto frame-scaling variables.
140   FRAME_SCALE_LEVEL frame_size_selector;
141   FRAME_SCALE_LEVEL next_frame_size_selector;
142   int frame_width[FRAME_SCALE_STEPS];
143   int frame_height[FRAME_SCALE_STEPS];
144   int rf_level_maxq[RATE_FACTOR_LEVELS];
145 } RATE_CONTROL;
146
147 struct VP9_COMP;
148 struct VP9EncoderConfig;
149
150 void vp10_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
151                  RATE_CONTROL *rc);
152
153 int vp10_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs,
154                            double correction_factor,
155                            vpx_bit_depth_t bit_depth);
156
157 double vp10_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
158
159 void vp10_rc_init_minq_luts(void);
160
161 int vp10_rc_get_default_min_gf_interval(int width, int height, double framerate);
162 // Note vp10_rc_get_default_max_gf_interval() requires the min_gf_interval to
163 // be passed in to ensure that the max_gf_interval returned is at least as bis
164 // as that.
165 int vp10_rc_get_default_max_gf_interval(double framerate, int min_frame_rate);
166
167 // Generally at the high level, the following flow is expected
168 // to be enforced for rate control:
169 // First call per frame, one of:
170 //   vp10_rc_get_one_pass_vbr_params()
171 //   vp10_rc_get_one_pass_cbr_params()
172 //   vp10_rc_get_svc_params()
173 //   vp10_rc_get_first_pass_params()
174 //   vp10_rc_get_second_pass_params()
175 // depending on the usage to set the rate control encode parameters desired.
176 //
177 // Then, call encode_frame_to_data_rate() to perform the
178 // actual encode. This function will in turn call encode_frame()
179 // one or more times, followed by one of:
180 //   vp10_rc_postencode_update()
181 //   vp10_rc_postencode_update_drop_frame()
182 //
183 // The majority of rate control parameters are only expected
184 // to be set in the vp10_rc_get_..._params() functions and
185 // updated during the vp10_rc_postencode_update...() functions.
186 // The only exceptions are vp10_rc_drop_frame() and
187 // vp10_rc_update_rate_correction_factors() functions.
188
189 // Functions to set parameters for encoding before the actual
190 // encode_frame_to_data_rate() function.
191 void vp10_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
192 void vp10_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
193 void vp10_rc_get_svc_params(struct VP9_COMP *cpi);
194
195 // Post encode update of the rate control parameters based
196 // on bytes used
197 void vp10_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
198 // Post encode update of the rate control parameters for dropped frames
199 void vp10_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
200
201 // Updates rate correction factors
202 // Changes only the rate correction factors in the rate control structure.
203 void vp10_rc_update_rate_correction_factors(struct VP9_COMP *cpi);
204
205 // Decide if we should drop this frame: For 1-pass CBR.
206 // Changes only the decimation count in the rate control structure
207 int vp10_rc_drop_frame(struct VP9_COMP *cpi);
208
209 // Computes frame size bounds.
210 void vp10_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
211                                       int this_frame_target,
212                                       int *frame_under_shoot_limit,
213                                       int *frame_over_shoot_limit);
214
215 // Picks q and q bounds given the target for bits
216 int vp10_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
217                              int *bottom_index,
218                              int *top_index);
219
220 // Estimates q to achieve a target bits per frame
221 int vp10_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
222                       int active_best_quality, int active_worst_quality);
223
224 // Estimates bits per mb for a given qindex and correction factor.
225 int vp10_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
226                        double correction_factor, vpx_bit_depth_t bit_depth);
227
228 // Clamping utilities for bitrate targets for iframes and pframes.
229 int vp10_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
230                                     int target);
231 int vp10_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
232                                     int target);
233 // Utility to set frame_target into the RATE_CONTROL structure
234 // This function is called only from the vp10_rc_get_..._params() functions.
235 void vp10_rc_set_frame_target(struct VP9_COMP *cpi, int target);
236
237 // Computes a q delta (in "q index" terms) to get from a starting q value
238 // to a target q value
239 int vp10_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
240                        vpx_bit_depth_t bit_depth);
241
242 // Computes a q delta (in "q index" terms) to get from a starting q value
243 // to a value that should equate to the given rate ratio.
244 int vp10_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
245                                int qindex, double rate_target_ratio,
246                                vpx_bit_depth_t bit_depth);
247
248 int vp10_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
249
250 void vp10_rc_update_framerate(struct VP9_COMP *cpi);
251
252 void vp10_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
253                                   RATE_CONTROL *const rc);
254
255 void vp10_set_target_rate(struct VP9_COMP *cpi);
256
257 int vp10_resize_one_pass_cbr(struct VP9_COMP *cpi);
258
259 #ifdef __cplusplus
260 }  // extern "C"
261 #endif
262
263 #endif  // VP9_ENCODER_VP9_RATECTRL_H_