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
37 // Rate targetting variables
38 int base_frame_target; // A baseline frame target before adjustment
39 // for previous under or over shoot.
40 int this_frame_target; // Actual frame target after rc adjustment.
41 int projected_frame_size;
43 int last_q[FRAME_TYPES]; // Separate values for Intra/Inter
44 int last_boosted_qindex; // Last boosted GF/KF/ARF q
45 int last_kf_qindex; // Q index of the last key frame coded.
51 double rate_correction_factors[RATE_FACTOR_LEVELS];
53 int frames_since_golden;
54 int frames_till_gf_update_due;
56 int static_scene_max_gf_interval;
57 int baseline_gf_interval;
60 int this_key_frame_forced;
61 int next_key_frame_forced;
62 int source_alt_ref_pending;
63 int source_alt_ref_active;
64 int is_src_frame_alt_ref;
66 int avg_frame_bandwidth; // Average frame size target for clip
67 int min_frame_bandwidth; // Minimum allocation used for any frame
68 int max_frame_bandwidth; // Maximum burst rate allowed for a frame.
73 int avg_frame_qindex[FRAME_TYPES];
78 int64_t bits_off_target;
79 int64_t vbr_bits_off_target;
81 int decimation_factor;
84 int rolling_target_bits;
85 int rolling_actual_bits;
87 int long_rolling_target_bits;
88 int long_rolling_actual_bits;
90 int rate_error_estimate;
92 int64_t total_actual_bits;
93 int64_t total_target_bits;
94 int64_t total_target_vs_actual;
99 int64_t starting_buffer_level;
100 int64_t optimal_buffer_level;
101 int64_t maximum_buffer_size;
102 // int active_best_quality;
106 struct VP9EncoderConfig;
108 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
111 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
113 void vp9_rc_init_minq_luts();
115 // Generally at the high level, the following flow is expected
116 // to be enforced for rate control:
117 // First call per frame, one of:
118 // vp9_rc_get_one_pass_vbr_params()
119 // vp9_rc_get_one_pass_cbr_params()
120 // vp9_rc_get_svc_params()
121 // vp9_rc_get_first_pass_params()
122 // vp9_rc_get_second_pass_params()
123 // depending on the usage to set the rate control encode parameters desired.
125 // Then, call encode_frame_to_data_rate() to perform the
126 // actual encode. This function will in turn call encode_frame()
127 // one or more times, followed by one of:
128 // vp9_rc_postencode_update()
129 // vp9_rc_postencode_update_drop_frame()
131 // The majority of rate control parameters are only expected
132 // to be set in the vp9_rc_get_..._params() functions and
133 // updated during the vp9_rc_postencode_update...() functions.
134 // The only exceptions are vp9_rc_drop_frame() and
135 // vp9_rc_update_rate_correction_factors() functions.
137 // Functions to set parameters for encoding before the actual
138 // encode_frame_to_data_rate() function.
139 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
140 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
141 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
143 // Post encode update of the rate control parameters based
145 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
146 // Post encode update of the rate control parameters for dropped frames
147 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
149 // Updates rate correction factors
150 // Changes only the rate correction factors in the rate control structure.
151 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
153 // Decide if we should drop this frame: For 1-pass CBR.
154 // Changes only the decimation count in the rate control structure
155 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
157 // Computes frame size bounds.
158 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
159 int this_frame_target,
160 int *frame_under_shoot_limit,
161 int *frame_over_shoot_limit);
163 // Picks q and q bounds given the target for bits
164 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
168 // Estimates q to achieve a target bits per frame
169 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
170 int active_best_quality, int active_worst_quality);
172 // Estimates bits per mb for a given qindex and correction factor.
173 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
174 double correction_factor, vpx_bit_depth_t bit_depth);
176 // Clamping utilities for bitrate targets for iframes and pframes.
177 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
179 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
181 // Utility to set frame_target into the RATE_CONTROL structure
182 // This function is called only from the vp9_rc_get_..._params() functions.
183 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
185 // Computes a q delta (in "q index" terms) to get from a starting q value
186 // to a target q value
187 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
188 vpx_bit_depth_t bit_depth);
190 // Computes a q delta (in "q index" terms) to get from a starting q value
191 // to a value that should equate to the given rate ratio.
192 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
193 int qindex, double rate_target_ratio,
194 vpx_bit_depth_t bit_depth);
196 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
198 void vp9_rc_set_gf_max_interval(const struct VP9_COMP *const cpi,
199 RATE_CONTROL *const rc);
205 #endif // VP9_ENCODER_VP9_RATECTRL_H_