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;
103 int64_t vbr_bits_off_target_fast;
105 int decimation_factor;
106 int decimation_count;
108 int rolling_target_bits;
109 int rolling_actual_bits;
111 int long_rolling_target_bits;
112 int long_rolling_actual_bits;
114 int rate_error_estimate;
116 int64_t total_actual_bits;
117 int64_t total_target_bits;
118 int64_t total_target_vs_actual;
123 int64_t starting_buffer_level;
124 int64_t optimal_buffer_level;
125 int64_t maximum_buffer_size;
127 // rate control history for last frame(1) and the frame before(2).
130 // 0: not initialized.
136 // Auto frame-scaling variables.
137 FRAME_SCALE_LEVEL frame_size_selector;
138 FRAME_SCALE_LEVEL next_frame_size_selector;
139 int frame_width[FRAME_SCALE_STEPS];
140 int frame_height[FRAME_SCALE_STEPS];
141 int rf_level_maxq[RATE_FACTOR_LEVELS];
145 struct VP9EncoderConfig;
147 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
150 int vp9_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs,
151 double correction_factor,
152 vpx_bit_depth_t bit_depth);
154 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
156 void vp9_rc_init_minq_luts(void);
158 // Generally at the high level, the following flow is expected
159 // to be enforced for rate control:
160 // First call per frame, one of:
161 // vp9_rc_get_one_pass_vbr_params()
162 // vp9_rc_get_one_pass_cbr_params()
163 // vp9_rc_get_svc_params()
164 // vp9_rc_get_first_pass_params()
165 // vp9_rc_get_second_pass_params()
166 // depending on the usage to set the rate control encode parameters desired.
168 // Then, call encode_frame_to_data_rate() to perform the
169 // actual encode. This function will in turn call encode_frame()
170 // one or more times, followed by one of:
171 // vp9_rc_postencode_update()
172 // vp9_rc_postencode_update_drop_frame()
174 // The majority of rate control parameters are only expected
175 // to be set in the vp9_rc_get_..._params() functions and
176 // updated during the vp9_rc_postencode_update...() functions.
177 // The only exceptions are vp9_rc_drop_frame() and
178 // vp9_rc_update_rate_correction_factors() functions.
180 // Functions to set parameters for encoding before the actual
181 // encode_frame_to_data_rate() function.
182 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
183 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
184 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
186 // Post encode update of the rate control parameters based
188 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
189 // Post encode update of the rate control parameters for dropped frames
190 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
192 // Updates rate correction factors
193 // Changes only the rate correction factors in the rate control structure.
194 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi);
196 // Decide if we should drop this frame: For 1-pass CBR.
197 // Changes only the decimation count in the rate control structure
198 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
200 // Computes frame size bounds.
201 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
202 int this_frame_target,
203 int *frame_under_shoot_limit,
204 int *frame_over_shoot_limit);
206 // Picks q and q bounds given the target for bits
207 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
211 // Estimates q to achieve a target bits per frame
212 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
213 int active_best_quality, int active_worst_quality);
215 // Estimates bits per mb for a given qindex and correction factor.
216 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
217 double correction_factor, vpx_bit_depth_t bit_depth);
219 // Clamping utilities for bitrate targets for iframes and pframes.
220 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
222 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
224 // Utility to set frame_target into the RATE_CONTROL structure
225 // This function is called only from the vp9_rc_get_..._params() functions.
226 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
228 // Computes a q delta (in "q index" terms) to get from a starting q value
229 // to a target q value
230 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
231 vpx_bit_depth_t bit_depth);
233 // Computes a q delta (in "q index" terms) to get from a starting q value
234 // to a value that should equate to the given rate ratio.
235 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
236 int qindex, double rate_target_ratio,
237 vpx_bit_depth_t bit_depth);
239 int vp9_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
241 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
243 void vp9_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
244 RATE_CONTROL *const rc);
246 void vp9_set_target_rate(struct VP9_COMP *cpi);
252 #endif // VP9_ENCODER_VP9_RATECTRL_H_