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 int64_t total_actual_bits;
91 int64_t total_target_bits;
92 int64_t total_target_vs_actual;
97 int64_t starting_buffer_level;
98 int64_t optimal_buffer_level;
99 int64_t maximum_buffer_size;
100 // int active_best_quality;
104 struct VP9EncoderConfig;
106 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
109 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
111 void vp9_rc_init_minq_luts();
113 // Generally at the high level, the following flow is expected
114 // to be enforced for rate control:
115 // First call per frame, one of:
116 // vp9_rc_get_one_pass_vbr_params()
117 // vp9_rc_get_one_pass_cbr_params()
118 // vp9_rc_get_svc_params()
119 // vp9_rc_get_first_pass_params()
120 // vp9_rc_get_second_pass_params()
121 // depending on the usage to set the rate control encode parameters desired.
123 // Then, call encode_frame_to_data_rate() to perform the
124 // actual encode. This function will in turn call encode_frame()
125 // one or more times, followed by one of:
126 // vp9_rc_postencode_update()
127 // vp9_rc_postencode_update_drop_frame()
129 // The majority of rate control parameters are only expected
130 // to be set in the vp9_rc_get_..._params() functions and
131 // updated during the vp9_rc_postencode_update...() functions.
132 // The only exceptions are vp9_rc_drop_frame() and
133 // vp9_rc_update_rate_correction_factors() functions.
135 // Functions to set parameters for encoding before the actual
136 // encode_frame_to_data_rate() function.
137 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
138 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
139 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
141 // Post encode update of the rate control parameters based
143 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
144 // Post encode update of the rate control parameters for dropped frames
145 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
147 // Updates rate correction factors
148 // Changes only the rate correction factors in the rate control structure.
149 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
151 // Decide if we should drop this frame: For 1-pass CBR.
152 // Changes only the decimation count in the rate control structure
153 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
155 // Computes frame size bounds.
156 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
157 int this_frame_target,
158 int *frame_under_shoot_limit,
159 int *frame_over_shoot_limit);
161 // Picks q and q bounds given the target for bits
162 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
166 // Estimates q to achieve a target bits per frame
167 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
168 int active_best_quality, int active_worst_quality);
170 // Estimates bits per mb for a given qindex and correction factor.
171 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
172 double correction_factor, vpx_bit_depth_t bit_depth);
174 // Clamping utilities for bitrate targets for iframes and pframes.
175 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
177 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
179 // Utility to set frame_target into the RATE_CONTROL structure
180 // This function is called only from the vp9_rc_get_..._params() functions.
181 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
183 // Computes a q delta (in "q index" terms) to get from a starting q value
184 // to a target q value
185 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
186 vpx_bit_depth_t bit_depth);
188 // Computes a q delta (in "q index" terms) to get from a starting q value
189 // to a value that should equate to the given rate ratio.
190 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
191 int qindex, double rate_target_ratio,
192 vpx_bit_depth_t bit_depth);
194 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
196 void vp9_rc_set_gf_max_interval(const struct VP9_COMP *const cpi,
197 RATE_CONTROL *const rc);
203 #endif // VP9_ENCODER_VP9_RATECTRL_H_