]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_ratectrl.h
Merge "[svc] Make size of empty frame to be 16x16 all the time"
[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
104   int decimation_factor;
105   int decimation_count;
106
107   int rolling_target_bits;
108   int rolling_actual_bits;
109
110   int long_rolling_target_bits;
111   int long_rolling_actual_bits;
112
113   int rate_error_estimate;
114
115   int64_t total_actual_bits;
116   int64_t total_target_bits;
117   int64_t total_target_vs_actual;
118
119   int worst_quality;
120   int best_quality;
121
122   int64_t starting_buffer_level;
123   int64_t optimal_buffer_level;
124   int64_t maximum_buffer_size;
125
126   // rate control history for last frame(1) and the frame before(2).
127   // -1: undershot
128   //  1: overshoot
129   //  0: not initialized.
130   int rc_1_frame;
131   int rc_2_frame;
132   int q_1_frame;
133   int q_2_frame;
134
135   // Auto frame-scaling variables.
136   FRAME_SCALE_LEVEL frame_size_selector;
137   FRAME_SCALE_LEVEL next_frame_size_selector;
138   int frame_width[FRAME_SCALE_STEPS];
139   int frame_height[FRAME_SCALE_STEPS];
140   int rf_level_maxq[RATE_FACTOR_LEVELS];
141 } RATE_CONTROL;
142
143 struct VP9_COMP;
144 struct VP9EncoderConfig;
145
146 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
147                  RATE_CONTROL *rc);
148
149 int vp9_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs,
150                            double correction_factor,
151                            vpx_bit_depth_t bit_depth);
152
153 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
154
155 void vp9_rc_init_minq_luts(void);
156
157 // Generally at the high level, the following flow is expected
158 // to be enforced for rate control:
159 // First call per frame, one of:
160 //   vp9_rc_get_one_pass_vbr_params()
161 //   vp9_rc_get_one_pass_cbr_params()
162 //   vp9_rc_get_svc_params()
163 //   vp9_rc_get_first_pass_params()
164 //   vp9_rc_get_second_pass_params()
165 // depending on the usage to set the rate control encode parameters desired.
166 //
167 // Then, call encode_frame_to_data_rate() to perform the
168 // actual encode. This function will in turn call encode_frame()
169 // one or more times, followed by one of:
170 //   vp9_rc_postencode_update()
171 //   vp9_rc_postencode_update_drop_frame()
172 //
173 // The majority of rate control parameters are only expected
174 // to be set in the vp9_rc_get_..._params() functions and
175 // updated during the vp9_rc_postencode_update...() functions.
176 // The only exceptions are vp9_rc_drop_frame() and
177 // vp9_rc_update_rate_correction_factors() functions.
178
179 // Functions to set parameters for encoding before the actual
180 // encode_frame_to_data_rate() function.
181 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
182 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
183 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
184
185 // Post encode update of the rate control parameters based
186 // on bytes used
187 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
188 // Post encode update of the rate control parameters for dropped frames
189 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
190
191 // Updates rate correction factors
192 // Changes only the rate correction factors in the rate control structure.
193 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi);
194
195 // Decide if we should drop this frame: For 1-pass CBR.
196 // Changes only the decimation count in the rate control structure
197 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
198
199 // Computes frame size bounds.
200 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
201                                       int this_frame_target,
202                                       int *frame_under_shoot_limit,
203                                       int *frame_over_shoot_limit);
204
205 // Picks q and q bounds given the target for bits
206 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
207                              int *bottom_index,
208                              int *top_index);
209
210 // Estimates q to achieve a target bits per frame
211 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
212                       int active_best_quality, int active_worst_quality);
213
214 // Estimates bits per mb for a given qindex and correction factor.
215 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
216                        double correction_factor, vpx_bit_depth_t bit_depth);
217
218 // Clamping utilities for bitrate targets for iframes and pframes.
219 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
220                                     int target);
221 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
222                                     int target);
223 // Utility to set frame_target into the RATE_CONTROL structure
224 // This function is called only from the vp9_rc_get_..._params() functions.
225 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
226
227 // Computes a q delta (in "q index" terms) to get from a starting q value
228 // to a target q value
229 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
230                        vpx_bit_depth_t bit_depth);
231
232 // Computes a q delta (in "q index" terms) to get from a starting q value
233 // to a value that should equate to the given rate ratio.
234 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
235                                int qindex, double rate_target_ratio,
236                                vpx_bit_depth_t bit_depth);
237
238 int vp9_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
239
240 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
241
242 void vp9_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
243                                   RATE_CONTROL *const rc);
244
245 void vp9_set_target_rate(struct VP9_COMP *cpi);
246
247 #ifdef __cplusplus
248 }  // extern "C"
249 #endif
250
251 #endif  // VP9_ENCODER_VP9_RATECTRL_H_