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 "vp10/common/vp9_blockd.h"
24 // Bits Per MB at different Q (Multiplied by 512)
25 #define BPER_MB_NORMBITS 9
27 #define MIN_GF_INTERVAL 4
28 #define MAX_GF_INTERVAL 16
36 RATE_FACTOR_LEVELS = 5
39 // Internal frame scaling level.
41 UNSCALED = 0, // Frame is unscaled.
42 SCALE_STEP1 = 1, // First-level down-scaling.
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};
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};
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};
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;
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.
75 double rate_correction_factors[RATE_FACTOR_LEVELS];
77 int frames_since_golden;
78 int frames_till_gf_update_due;
81 int static_scene_max_gf_interval;
82 int baseline_gf_interval;
83 int constrained_gf_group;
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;
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.
99 int avg_frame_qindex[FRAME_TYPES];
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;
108 int decimation_factor;
109 int decimation_count;
111 int rolling_target_bits;
112 int rolling_actual_bits;
114 int long_rolling_target_bits;
115 int long_rolling_actual_bits;
117 int rate_error_estimate;
119 int64_t total_actual_bits;
120 int64_t total_target_bits;
121 int64_t total_target_vs_actual;
126 int64_t starting_buffer_level;
127 int64_t optimal_buffer_level;
128 int64_t maximum_buffer_size;
130 // rate control history for last frame(1) and the frame before(2).
133 // 0: not initialized.
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];
148 struct VP9EncoderConfig;
150 void vp10_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
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);
157 double vp10_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
159 void vp10_rc_init_minq_luts(void);
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
165 int vp10_rc_get_default_max_gf_interval(double framerate, int min_frame_rate);
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.
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()
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.
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);
195 // Post encode update of the rate control parameters based
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);
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);
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);
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);
215 // Picks q and q bounds given the target for bits
216 int vp10_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
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);
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);
228 // Clamping utilities for bitrate targets for iframes and pframes.
229 int vp10_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
231 int vp10_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
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);
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);
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);
248 int vp10_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
250 void vp10_rc_update_framerate(struct VP9_COMP *cpi);
252 void vp10_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
253 RATE_CONTROL *const rc);
255 void vp10_set_target_rate(struct VP9_COMP *cpi);
257 int vp10_resize_one_pass_cbr(struct VP9_COMP *cpi);
263 #endif // VP9_ENCODER_VP9_RATECTRL_H_