]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_ratectrl.h
e12d200be833cd6d9ed5239f836cc67c49c91f80
[libvpx] / vp9 / encoder / vp9_ratectrl.h
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11
12 #ifndef VP9_ENCODER_VP9_RATECTRL_H_
13 #define VP9_ENCODER_VP9_RATECTRL_H_
14
15 #include "vpx/vpx_codec.h"
16 #include "vpx/vpx_integer.h"
17
18 #include "vp9/common/vp9_blockd.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 // Bits Per MB at different Q (Multiplied by 512)
25 #define BPER_MB_NORMBITS    9
26
27 typedef enum {
28   INTER_NORMAL = 0,
29   INTER_HIGH = 1,
30   GF_ARF_LOW = 2,
31   GF_ARF_STD = 3,
32   KF_STD = 4,
33   RATE_FACTOR_LEVELS = 5
34 } RATE_FACTOR_LEVEL;
35
36 // Internal frame scaling level.
37 typedef enum {
38   UNSCALED = 0,     // Frame is unscaled.
39   SCALE_STEP1 = 1,  // First-level down-scaling.
40   FRAME_SCALE_STEPS
41 } FRAME_SCALE_LEVEL;
42
43 // Frame dimensions multiplier wrt the native frame size, in 1/16ths,
44 // specified for the scale-up case.
45 // e.g. 24 => 16/24 = 2/3 of native size. The restriction to 1/16th is
46 // intended to match the capabilities of the normative scaling filters,
47 // giving precedence to the up-scaling accuracy.
48 static const int frame_scale_factor[FRAME_SCALE_STEPS] = {16, 24};
49
50 // Multiplier of the target rate to be used as threshold for triggering scaling.
51 static const double rate_thresh_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
52
53 // Scale dependent Rate Correction Factor multipliers. Compensates for the
54 // greater number of bits per pixel generated in down-scaled frames.
55 static const double rcf_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
56
57 typedef struct {
58   // Rate targetting variables
59   int base_frame_target;           // A baseline frame target before adjustment
60                                    // for previous under or over shoot.
61   int this_frame_target;           // Actual frame target after rc adjustment.
62   int projected_frame_size;
63   int sb64_target_rate;
64   int last_q[FRAME_TYPES];         // Separate values for Intra/Inter
65   int last_boosted_qindex;         // Last boosted GF/KF/ARF q
66   int last_kf_qindex;              // Q index of the last key frame coded.
67
68   int gfu_boost;
69   int last_boost;
70   int kf_boost;
71
72   double rate_correction_factors[RATE_FACTOR_LEVELS];
73
74   int frames_since_golden;
75   int frames_till_gf_update_due;
76   int min_gf_interval;
77   int max_gf_interval;
78   int static_scene_max_gf_interval;
79   int baseline_gf_interval;
80   int constrained_gf_group;
81   int frames_to_key;
82   int frames_since_key;
83   int this_key_frame_forced;
84   int next_key_frame_forced;
85   int source_alt_ref_pending;
86   int source_alt_ref_active;
87   int is_src_frame_alt_ref;
88
89   int avg_frame_bandwidth;  // Average frame size target for clip
90   int min_frame_bandwidth;  // Minimum allocation used for any frame
91   int max_frame_bandwidth;  // Maximum burst rate allowed for a frame.
92
93   int ni_av_qi;
94   int ni_tot_qi;
95   int ni_frames;
96   int avg_frame_qindex[FRAME_TYPES];
97   double tot_q;
98   double avg_q;
99
100   int64_t buffer_level;
101   int64_t bits_off_target;
102   int64_t vbr_bits_off_target;
103   int64_t vbr_bits_off_target_fast;
104
105   int decimation_factor;
106   int decimation_count;
107
108   int rolling_target_bits;
109   int rolling_actual_bits;
110
111   int long_rolling_target_bits;
112   int long_rolling_actual_bits;
113
114   int rate_error_estimate;
115
116   int64_t total_actual_bits;
117   int64_t total_target_bits;
118   int64_t total_target_vs_actual;
119
120   int worst_quality;
121   int best_quality;
122
123   int64_t starting_buffer_level;
124   int64_t optimal_buffer_level;
125   int64_t maximum_buffer_size;
126
127   // rate control history for last frame(1) and the frame before(2).
128   // -1: undershot
129   //  1: overshoot
130   //  0: not initialized.
131   int rc_1_frame;
132   int rc_2_frame;
133   int q_1_frame;
134   int q_2_frame;
135
136   // Auto frame-scaling variables.
137   FRAME_SCALE_LEVEL frame_size_selector;
138   FRAME_SCALE_LEVEL next_frame_size_selector;
139   int frame_width[FRAME_SCALE_STEPS];
140   int frame_height[FRAME_SCALE_STEPS];
141   int rf_level_maxq[RATE_FACTOR_LEVELS];
142 } RATE_CONTROL;
143
144 struct VP9_COMP;
145 struct VP9EncoderConfig;
146
147 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
148                  RATE_CONTROL *rc);
149
150 int vp9_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs,
151                            double correction_factor,
152                            vpx_bit_depth_t bit_depth);
153
154 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
155
156 void vp9_rc_init_minq_luts(void);
157
158 // Generally at the high level, the following flow is expected
159 // to be enforced for rate control:
160 // First call per frame, one of:
161 //   vp9_rc_get_one_pass_vbr_params()
162 //   vp9_rc_get_one_pass_cbr_params()
163 //   vp9_rc_get_svc_params()
164 //   vp9_rc_get_first_pass_params()
165 //   vp9_rc_get_second_pass_params()
166 // depending on the usage to set the rate control encode parameters desired.
167 //
168 // Then, call encode_frame_to_data_rate() to perform the
169 // actual encode. This function will in turn call encode_frame()
170 // one or more times, followed by one of:
171 //   vp9_rc_postencode_update()
172 //   vp9_rc_postencode_update_drop_frame()
173 //
174 // The majority of rate control parameters are only expected
175 // to be set in the vp9_rc_get_..._params() functions and
176 // updated during the vp9_rc_postencode_update...() functions.
177 // The only exceptions are vp9_rc_drop_frame() and
178 // vp9_rc_update_rate_correction_factors() functions.
179
180 // Functions to set parameters for encoding before the actual
181 // encode_frame_to_data_rate() function.
182 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
183 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
184 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
185
186 // Post encode update of the rate control parameters based
187 // on bytes used
188 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
189 // Post encode update of the rate control parameters for dropped frames
190 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
191
192 // Updates rate correction factors
193 // Changes only the rate correction factors in the rate control structure.
194 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi);
195
196 // Decide if we should drop this frame: For 1-pass CBR.
197 // Changes only the decimation count in the rate control structure
198 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
199
200 // Computes frame size bounds.
201 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
202                                       int this_frame_target,
203                                       int *frame_under_shoot_limit,
204                                       int *frame_over_shoot_limit);
205
206 // Picks q and q bounds given the target for bits
207 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
208                              int *bottom_index,
209                              int *top_index);
210
211 // Estimates q to achieve a target bits per frame
212 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
213                       int active_best_quality, int active_worst_quality);
214
215 // Estimates bits per mb for a given qindex and correction factor.
216 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
217                        double correction_factor, vpx_bit_depth_t bit_depth);
218
219 // Clamping utilities for bitrate targets for iframes and pframes.
220 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
221                                     int target);
222 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
223                                     int target);
224 // Utility to set frame_target into the RATE_CONTROL structure
225 // This function is called only from the vp9_rc_get_..._params() functions.
226 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
227
228 // Computes a q delta (in "q index" terms) to get from a starting q value
229 // to a target q value
230 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
231                        vpx_bit_depth_t bit_depth);
232
233 // Computes a q delta (in "q index" terms) to get from a starting q value
234 // to a value that should equate to the given rate ratio.
235 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
236                                int qindex, double rate_target_ratio,
237                                vpx_bit_depth_t bit_depth);
238
239 int vp9_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
240
241 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
242
243 void vp9_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
244                                   RATE_CONTROL *const rc);
245
246 void vp9_set_target_rate(struct VP9_COMP *cpi);
247
248 #ifdef __cplusplus
249 }  // extern "C"
250 #endif
251
252 #endif  // VP9_ENCODER_VP9_RATECTRL_H_