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_integer.h"
17 #include "vp9/common/vp9_blockd.h"
23 #define FRAME_OVERHEAD_BITS 200
25 // Bits Per MB at different Q (Multiplied by 512)
26 #define BPER_MB_NORMBITS 9
29 // Rate targetting variables
30 int this_frame_target;
31 int projected_frame_size;
33 int last_q[3]; // Separate values for Intra/Inter/ARF-GF
34 int last_boosted_qindex; // Last boosted GF/KF/ARF q
40 double rate_correction_factor;
41 double key_frame_rate_correction_factor;
42 double gf_rate_correction_factor;
44 int frames_since_golden;
45 int frames_till_gf_update_due;
47 int static_scene_max_gf_interval;
48 int baseline_gf_interval;
51 int this_key_frame_forced;
52 int next_key_frame_forced;
53 int source_alt_ref_pending;
54 int source_alt_ref_active;
55 int is_src_frame_alt_ref;
57 int av_per_frame_bandwidth; // Average frame size target for clip
58 int min_frame_bandwidth; // Minimum allocation used for any frame
59 int max_frame_bandwidth; // Maximum burst rate allowed for a frame.
64 int avg_frame_qindex[3]; // 0 - KEY, 1 - INTER, 2 - ARF/GF
69 int64_t bits_off_target;
71 int decimation_factor;
74 int rolling_target_bits;
75 int rolling_actual_bits;
77 int long_rolling_target_bits;
78 int long_rolling_actual_bits;
80 int64_t total_actual_bits;
81 int64_t total_target_bits;
82 int64_t total_target_vs_actual;
86 // int active_best_quality;
92 void vp9_rc_init(const struct VP9_CONFIG *oxcf, int pass, RATE_CONTROL *rc);
94 double vp9_convert_qindex_to_q(int qindex);
96 void vp9_rc_init_minq_luts();
98 // Generally at the high level, the following flow is expected
99 // to be enforced for rate control:
100 // First call per frame, one of:
101 // vp9_rc_get_one_pass_vbr_params()
102 // vp9_rc_get_one_pass_cbr_params()
103 // vp9_rc_get_svc_params()
104 // vp9_rc_get_first_pass_params()
105 // vp9_rc_get_second_pass_params()
106 // depending on the usage to set the rate control encode parameters desired.
108 // Then, call encode_frame_to_data_rate() to perform the
109 // actual encode. This function will in turn call encode_frame()
110 // one or more times, followed by one of:
111 // vp9_rc_postencode_update()
112 // vp9_rc_postencode_update_drop_frame()
114 // The majority of rate control parameters are only expected
115 // to be set in the vp9_rc_get_..._params() functions and
116 // updated during the vp9_rc_postencode_update...() functions.
117 // The only exceptions are vp9_rc_drop_frame() and
118 // vp9_rc_update_rate_correction_factors() functions.
120 // Functions to set parameters for encoding before the actual
121 // encode_frame_to_data_rate() function.
122 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
123 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
124 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
126 // Post encode update of the rate control parameters based
128 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
129 // Post encode update of the rate control parameters for dropped frames
130 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
132 // Updates rate correction factors
133 // Changes only the rate correction factors in the rate control structure.
134 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
136 // Decide if we should drop this frame: For 1-pass CBR.
137 // Changes only the decimation count in the rate control structure
138 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
140 // Computes frame size bounds.
141 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
142 int this_frame_target,
143 int *frame_under_shoot_limit,
144 int *frame_over_shoot_limit);
146 // Picks q and q bounds given the target for bits
147 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
151 // Estimates q to achieve a target bits per frame
152 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
153 int active_best_quality, int active_worst_quality);
155 // Estimates bits per mb for a given qindex and correction factor.
156 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
157 double correction_factor);
159 // Clamping utilities for bitrate targets for iframes and pframes.
160 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
162 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
164 // Utility to set frame_target into the RATE_CONTROL structure
165 // This function is called only from the vp9_rc_get_..._params() functions.
166 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
168 // Computes a q delta (in "q index" terms) to get from a starting q value
169 // to a target q value
170 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget);
172 // Computes a q delta (in "q index" terms) to get from a starting q value
173 // to a value that should equate to the given rate ratio.
174 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
175 int qindex, double rate_target_ratio);
177 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
183 #endif // VP9_ENCODER_VP9_RATECTRL_H_