2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #ifndef VP9_ENCODER_VP9_RATECTRL_H_
13 #define VP9_ENCODER_VP9_RATECTRL_H_
15 #include "vpx/vpx_codec.h"
16 #include "vpx/vpx_integer.h"
18 #include "vp9/common/vp9_blockd.h"
24 // Bits Per MB at different Q (Multiplied by 512)
25 #define BPER_MB_NORMBITS 9
33 RATE_FACTOR_LEVELS = 5
36 // Internal frame scaling level.
38 UNSCALED = 0, // Frame is unscaled.
39 SCALE_STEP1 = 1, // First-level down-scaling.
43 // Frame dimensions multiplier wrt the native frame size, in 1/16ths,
44 // specified for the scale-up case.
45 // e.g. 24 => 16/24 = 2/3 of native size. The restriction to 1/16th is
46 // intended to match the capabilities of the normative scaling filters,
47 // giving precedence to the up-scaling accuracy.
48 static const int frame_scale_factor[FRAME_SCALE_STEPS] = {16, 24};
50 // Multiplier of the target rate to be used as threshold for triggering scaling.
51 static const double rate_thresh_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
53 // Scale dependent Rate Correction Factor multipliers. Compensates for the
54 // greater number of bits per pixel generated in down-scaled frames.
55 static const double rcf_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
58 // Rate targetting variables
59 int base_frame_target; // A baseline frame target before adjustment
60 // for previous under or over shoot.
61 int this_frame_target; // Actual frame target after rc adjustment.
62 int projected_frame_size;
64 int last_q[FRAME_TYPES]; // Separate values for Intra/Inter
65 int last_boosted_qindex; // Last boosted GF/KF/ARF q
66 int last_kf_qindex; // Q index of the last key frame coded.
72 double rate_correction_factors[RATE_FACTOR_LEVELS];
74 int frames_since_golden;
75 int frames_till_gf_update_due;
78 int static_scene_max_gf_interval;
79 int baseline_gf_interval;
80 int constrained_gf_group;
83 int this_key_frame_forced;
84 int next_key_frame_forced;
85 int source_alt_ref_pending;
86 int source_alt_ref_active;
87 int is_src_frame_alt_ref;
89 int avg_frame_bandwidth; // Average frame size target for clip
90 int min_frame_bandwidth; // Minimum allocation used for any frame
91 int max_frame_bandwidth; // Maximum burst rate allowed for a frame.
96 int avg_frame_qindex[FRAME_TYPES];
100 int64_t buffer_level;
101 int64_t bits_off_target;
102 int64_t vbr_bits_off_target;
104 int decimation_factor;
105 int decimation_count;
107 int rolling_target_bits;
108 int rolling_actual_bits;
110 int long_rolling_target_bits;
111 int long_rolling_actual_bits;
113 int rate_error_estimate;
115 int64_t total_actual_bits;
116 int64_t total_target_bits;
117 int64_t total_target_vs_actual;
122 int64_t starting_buffer_level;
123 int64_t optimal_buffer_level;
124 int64_t maximum_buffer_size;
126 // rate control history for last frame(1) and the frame before(2).
129 // 0: not initialized.
135 // Auto frame-scaling variables.
136 FRAME_SCALE_LEVEL frame_size_selector;
137 FRAME_SCALE_LEVEL next_frame_size_selector;
138 int frame_width[FRAME_SCALE_STEPS];
139 int frame_height[FRAME_SCALE_STEPS];
140 int rf_level_maxq[RATE_FACTOR_LEVELS];
144 struct VP9EncoderConfig;
146 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
149 int vp9_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs,
150 double correction_factor,
151 vpx_bit_depth_t bit_depth);
153 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
155 void vp9_rc_init_minq_luts(void);
157 // Generally at the high level, the following flow is expected
158 // to be enforced for rate control:
159 // First call per frame, one of:
160 // vp9_rc_get_one_pass_vbr_params()
161 // vp9_rc_get_one_pass_cbr_params()
162 // vp9_rc_get_svc_params()
163 // vp9_rc_get_first_pass_params()
164 // vp9_rc_get_second_pass_params()
165 // depending on the usage to set the rate control encode parameters desired.
167 // Then, call encode_frame_to_data_rate() to perform the
168 // actual encode. This function will in turn call encode_frame()
169 // one or more times, followed by one of:
170 // vp9_rc_postencode_update()
171 // vp9_rc_postencode_update_drop_frame()
173 // The majority of rate control parameters are only expected
174 // to be set in the vp9_rc_get_..._params() functions and
175 // updated during the vp9_rc_postencode_update...() functions.
176 // The only exceptions are vp9_rc_drop_frame() and
177 // vp9_rc_update_rate_correction_factors() functions.
179 // Functions to set parameters for encoding before the actual
180 // encode_frame_to_data_rate() function.
181 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
182 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
183 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
185 // Post encode update of the rate control parameters based
187 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
188 // Post encode update of the rate control parameters for dropped frames
189 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
191 // Updates rate correction factors
192 // Changes only the rate correction factors in the rate control structure.
193 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi);
195 // Decide if we should drop this frame: For 1-pass CBR.
196 // Changes only the decimation count in the rate control structure
197 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
199 // Computes frame size bounds.
200 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
201 int this_frame_target,
202 int *frame_under_shoot_limit,
203 int *frame_over_shoot_limit);
205 // Picks q and q bounds given the target for bits
206 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
210 // Estimates q to achieve a target bits per frame
211 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
212 int active_best_quality, int active_worst_quality);
214 // Estimates bits per mb for a given qindex and correction factor.
215 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
216 double correction_factor, vpx_bit_depth_t bit_depth);
218 // Clamping utilities for bitrate targets for iframes and pframes.
219 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
221 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
223 // Utility to set frame_target into the RATE_CONTROL structure
224 // This function is called only from the vp9_rc_get_..._params() functions.
225 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
227 // Computes a q delta (in "q index" terms) to get from a starting q value
228 // to a target q value
229 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
230 vpx_bit_depth_t bit_depth);
232 // Computes a q delta (in "q index" terms) to get from a starting q value
233 // to a value that should equate to the given rate ratio.
234 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
235 int qindex, double rate_target_ratio,
236 vpx_bit_depth_t bit_depth);
238 int vp9_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
240 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
242 void vp9_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
243 RATE_CONTROL *const rc);
245 void vp9_set_target_rate(struct VP9_COMP *cpi);
251 #endif // VP9_ENCODER_VP9_RATECTRL_H_