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_
19 #define FRAME_OVERHEAD_BITS 200
22 // Rate targetting variables
23 int this_frame_target;
24 int projected_frame_size;
26 int last_q[3]; // Separate values for Intra/Inter/ARF-GF
27 int last_boosted_qindex; // Last boosted GF/KF/ARF q
33 double rate_correction_factor;
34 double key_frame_rate_correction_factor;
35 double gf_rate_correction_factor;
37 unsigned int frames_since_golden;
38 unsigned int frames_till_gf_update_due; // Count down till next GF
39 unsigned int max_gf_interval;
40 unsigned int baseline_gf_interval;
41 unsigned int frames_to_key;
42 unsigned int frames_since_key;
43 unsigned int this_key_frame_forced;
44 unsigned int next_key_frame_forced;
45 unsigned int source_alt_ref_pending;
46 unsigned int source_alt_ref_active;
47 unsigned int is_src_frame_alt_ref;
49 int av_per_frame_bandwidth; // Average frame size target for clip
50 int min_frame_bandwidth; // Minimum allocation used for any frame
51 int max_frame_bandwidth; // Maximum burst rate allowed for a frame.
56 int avg_frame_qindex[3]; // 0 - KEY, 1 - INTER, 2 - ARF/GF
63 int decimation_factor;
66 int rolling_target_bits;
67 int rolling_actual_bits;
69 int long_rolling_target_bits;
70 int long_rolling_actual_bits;
72 int64_t total_actual_bits;
73 int total_target_vs_actual; // debug stats
77 // int active_best_quality;
82 void vp9_save_coding_context(struct VP9_COMP *cpi);
83 void vp9_restore_coding_context(struct VP9_COMP *cpi);
85 void vp9_setup_key_frame(struct VP9_COMP *cpi);
86 void vp9_setup_inter_frame(struct VP9_COMP *cpi);
88 double vp9_convert_qindex_to_q(int qindex);
90 // initialize luts for minq
91 void vp9_rc_init_minq_luts(void);
93 // Generally at the high level, the following flow is expected
94 // to be enforced for rate control:
95 // First call per frame, one of:
96 // vp9_rc_get_one_pass_vbr_params()
97 // vp9_rc_get_one_pass_cbr_params()
98 // vp9_rc_get_svc_params()
99 // vp9_rc_get_first_pass_params()
100 // vp9_rc_get_second_pass_params()
101 // depending on the usage to set the rate control encode parameters desired.
103 // Then, call encode_frame_to_data_rate() to perform the
104 // actual encode. This function will in turn call encode_frame()
105 // one or more times, followed by one of:
106 // vp9_rc_postencode_update()
107 // vp9_rc_postencode_update_drop_frame()
109 // The majority of rate control parameters are only expected
110 // to be set in the vp9_rc_get_..._params() functions and
111 // updated during the vp9_rc_postencode_update...() functions.
112 // The only exceptions are vp9_rc_drop_frame() and
113 // vp9_rc_update_rate_correction_factors() functions.
115 // Functions to set parameters for encoding before the actual
116 // encode_frame_to_data_rate() function.
117 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
118 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
119 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
121 // Post encode update of the rate control parameters based
123 void vp9_rc_postencode_update(struct VP9_COMP *cpi,
124 uint64_t bytes_used);
125 // Post encode update of the rate control parameters for dropped frames
126 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
128 // Updates rate correction factors
129 // Changes only the rate correction factors in the rate control structure.
130 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
132 // Decide if we should drop this frame: For 1-pass CBR.
133 // Changes only the decimation count in the rate control structure
134 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
136 // Computes frame size bounds.
137 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
138 int this_frame_target,
139 int *frame_under_shoot_limit,
140 int *frame_over_shoot_limit);
142 // Picks q and q bounds given the target for bits
143 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
147 // Estimates q to achieve a target bits per frame
148 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
149 int active_best_quality, int active_worst_quality);
151 // Estimates bits per mb for a given qindex and correction factor.
152 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
153 double correction_factor);
155 // Clamping utilities for bitrate targets for iframes and pframes.
156 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
158 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
160 // Utility to set frame_target into the RATE_CONTROL structure
161 // This function is called only from the vp9_rc_get_..._params() functions.
162 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
168 #endif // VP9_ENCODER_VP9_RATECTRL_H_