• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef VPX_VP9_ENCODER_VP9_RATECTRL_H_
12 #define VPX_VP9_ENCODER_VP9_RATECTRL_H_
13 
14 #include "vpx/vpx_codec.h"
15 #include "vpx/vpx_integer.h"
16 
17 #include "vp9/common/vp9_blockd.h"
18 #include "vp9/encoder/vp9_lookahead.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 // Used to control aggressive VBR mode.
25 // #define AGGRESSIVE_VBR 1
26 
27 // Bits Per MB at different Q (Multiplied by 512)
28 #define BPER_MB_NORMBITS 9
29 
30 #define DEFAULT_KF_BOOST 2000
31 #define DEFAULT_GF_BOOST 2000
32 
33 #define MIN_GF_INTERVAL 4
34 #define MAX_GF_INTERVAL 16
35 #define FIXED_GF_INTERVAL 8  // Used in some testing modes only
36 #define ONEHALFONLY_RESIZE 0
37 
38 #define FRAME_OVERHEAD_BITS 200
39 
40 // Threshold used to define a KF group as static (e.g. a slide show).
41 // Essentially this means that no frame in the group has more than 1% of MBs
42 // that are not marked as coded with 0,0 motion in the first pass.
43 #define STATIC_KF_GROUP_THRESH 99
44 
45 // The maximum duration of a GF group that is static (for example a slide show).
46 #define MAX_STATIC_GF_GROUP_LENGTH 250
47 
48 typedef enum {
49   INTER_NORMAL = 0,
50   INTER_HIGH = 1,
51   GF_ARF_LOW = 2,
52   GF_ARF_STD = 3,
53   KF_STD = 4,
54   RATE_FACTOR_LEVELS = 5
55 } RATE_FACTOR_LEVEL;
56 
57 // Internal frame scaling level.
58 typedef enum {
59   UNSCALED = 0,     // Frame is unscaled.
60   SCALE_STEP1 = 1,  // First-level down-scaling.
61   FRAME_SCALE_STEPS
62 } FRAME_SCALE_LEVEL;
63 
64 typedef enum {
65   NO_RESIZE = 0,
66   DOWN_THREEFOUR = 1,  // From orig to 3/4.
67   DOWN_ONEHALF = 2,    // From orig or 3/4 to 1/2.
68   UP_THREEFOUR = -1,   // From 1/2 to 3/4.
69   UP_ORIG = -2,        // From 1/2 or 3/4 to orig.
70 } RESIZE_ACTION;
71 
72 typedef enum { ORIG = 0, THREE_QUARTER = 1, ONE_HALF = 2 } RESIZE_STATE;
73 
74 // Frame dimensions multiplier wrt the native frame size, in 1/16ths,
75 // specified for the scale-up case.
76 // e.g. 24 => 16/24 = 2/3 of native size. The restriction to 1/16th is
77 // intended to match the capabilities of the normative scaling filters,
78 // giving precedence to the up-scaling accuracy.
79 static const int frame_scale_factor[FRAME_SCALE_STEPS] = { 16, 24 };
80 
81 // Multiplier of the target rate to be used as threshold for triggering scaling.
82 static const double rate_thresh_mult[FRAME_SCALE_STEPS] = { 1.0, 2.0 };
83 
84 // Scale dependent Rate Correction Factor multipliers. Compensates for the
85 // greater number of bits per pixel generated in down-scaled frames.
86 static const double rcf_mult[FRAME_SCALE_STEPS] = { 1.0, 2.0 };
87 
88 typedef struct {
89   // Rate targeting variables
90   int base_frame_target;  // A baseline frame target before adjustment
91                           // for previous under or over shoot.
92   int this_frame_target;  // Actual frame target after rc adjustment.
93   int projected_frame_size;
94   int sb64_target_rate;
95   int last_q[FRAME_TYPES];  // Separate values for Intra/Inter
96   int last_boosted_qindex;  // Last boosted GF/KF/ARF q
97   int last_kf_qindex;       // Q index of the last key frame coded.
98 
99   int gfu_boost;
100   int last_boost;
101   int kf_boost;
102 
103   double rate_correction_factors[RATE_FACTOR_LEVELS];
104 
105   int frames_since_golden;
106   int frames_till_gf_update_due;
107   int min_gf_interval;
108   int max_gf_interval;
109   int static_scene_max_gf_interval;
110   int baseline_gf_interval;
111   int constrained_gf_group;
112   int frames_to_key;
113   int frames_since_key;
114   int this_key_frame_forced;
115   int next_key_frame_forced;
116   int source_alt_ref_pending;
117   int source_alt_ref_active;
118   int is_src_frame_alt_ref;
119 
120   int avg_frame_bandwidth;  // Average frame size target for clip
121   int min_frame_bandwidth;  // Minimum allocation used for any frame
122   int max_frame_bandwidth;  // Maximum burst rate allowed for a frame.
123 
124   int ni_av_qi;
125   int ni_tot_qi;
126   int ni_frames;
127   int avg_frame_qindex[FRAME_TYPES];
128   double tot_q;
129   double avg_q;
130 
131   int64_t buffer_level;
132   int64_t bits_off_target;
133   int64_t vbr_bits_off_target;
134   int64_t vbr_bits_off_target_fast;
135 
136   int decimation_factor;
137   int decimation_count;
138 
139   int rolling_target_bits;
140   int rolling_actual_bits;
141 
142   int long_rolling_target_bits;
143   int long_rolling_actual_bits;
144 
145   int rate_error_estimate;
146 
147   int64_t total_actual_bits;
148   int64_t total_target_bits;
149   int64_t total_target_vs_actual;
150 
151   int worst_quality;
152   int best_quality;
153 
154   int64_t starting_buffer_level;
155   int64_t optimal_buffer_level;
156   int64_t maximum_buffer_size;
157 
158   // rate control history for last frame(1) and the frame before(2).
159   // -1: undershot
160   //  1: overshoot
161   //  0: not initialized.
162   int rc_1_frame;
163   int rc_2_frame;
164   int q_1_frame;
165   int q_2_frame;
166   // Keep track of the last target average frame bandwidth.
167   int last_avg_frame_bandwidth;
168 
169   // Auto frame-scaling variables.
170   FRAME_SCALE_LEVEL frame_size_selector;
171   FRAME_SCALE_LEVEL next_frame_size_selector;
172   int frame_width[FRAME_SCALE_STEPS];
173   int frame_height[FRAME_SCALE_STEPS];
174   int rf_level_maxq[RATE_FACTOR_LEVELS];
175 
176   int fac_active_worst_inter;
177   int fac_active_worst_gf;
178   uint64_t avg_source_sad[MAX_LAG_BUFFERS];
179   uint64_t prev_avg_source_sad_lag;
180   int high_source_sad_lagindex;
181   int high_num_blocks_with_motion;
182   int alt_ref_gf_group;
183   int last_frame_is_src_altref;
184   int high_source_sad;
185   int count_last_scene_change;
186   int hybrid_intra_scene_change;
187   int re_encode_maxq_scene_change;
188   int avg_frame_low_motion;
189   int af_ratio_onepass_vbr;
190   int force_qpmin;
191   int reset_high_source_sad;
192   double perc_arf_usage;
193   int force_max_q;
194   // Last frame was dropped post encode on scene change.
195   int last_post_encode_dropped_scene_change;
196   // Enable post encode frame dropping for screen content. Only enabled when
197   // ext_use_post_encode_drop is enabled by user.
198   int use_post_encode_drop;
199   // External flag to enable post encode frame dropping, controlled by user.
200   int ext_use_post_encode_drop;
201   // Flag to disable CBR feature to increase Q on overshoot detection.
202   int disable_overshoot_maxq_cbr;
203   int damped_adjustment[RATE_FACTOR_LEVELS];
204   double arf_active_best_quality_adjustment_factor;
205   int arf_increase_active_best_quality;
206 
207   int preserve_arf_as_gld;
208   int preserve_next_arf_as_gld;
209   int show_arf_as_gld;
210 
211   // Flag to constrain golden frame interval on key frame frequency for 1 pass
212   // VBR.
213   int constrain_gf_key_freq_onepass_vbr;
214 } RATE_CONTROL;
215 
216 struct VP9_COMP;
217 struct VP9EncoderConfig;
218 
219 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
220                  RATE_CONTROL *rc);
221 
222 int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
223                            double correction_factor, vpx_bit_depth_t bit_depth);
224 
225 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
226 
227 int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth);
228 
229 void vp9_rc_init_minq_luts(void);
230 
231 int vp9_rc_get_default_min_gf_interval(int width, int height, double framerate);
232 // Note vp9_rc_get_default_max_gf_interval() requires the min_gf_interval to
233 // be passed in to ensure that the max_gf_interval returned is at least as big
234 // as that.
235 int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval);
236 
237 // Generally at the high level, the following flow is expected
238 // to be enforced for rate control:
239 // First call per frame, one of:
240 //   vp9_rc_get_one_pass_vbr_params()
241 //   vp9_rc_get_one_pass_cbr_params()
242 //   vp9_rc_get_svc_params()
243 //   vp9_rc_get_first_pass_params()
244 //   vp9_rc_get_second_pass_params()
245 // depending on the usage to set the rate control encode parameters desired.
246 //
247 // Then, call encode_frame_to_data_rate() to perform the
248 // actual encode. This function will in turn call encode_frame()
249 // one or more times, followed by one of:
250 //   vp9_rc_postencode_update()
251 //   vp9_rc_postencode_update_drop_frame()
252 //
253 // The majority of rate control parameters are only expected
254 // to be set in the vp9_rc_get_..._params() functions and
255 // updated during the vp9_rc_postencode_update...() functions.
256 // The only exceptions are vp9_rc_drop_frame() and
257 // vp9_rc_update_rate_correction_factors() functions.
258 
259 // Functions to set parameters for encoding before the actual
260 // encode_frame_to_data_rate() function.
261 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
262 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
263 int vp9_calc_pframe_target_size_one_pass_cbr(const struct VP9_COMP *cpi);
264 int vp9_calc_iframe_target_size_one_pass_cbr(const struct VP9_COMP *cpi);
265 int vp9_calc_pframe_target_size_one_pass_vbr(const struct VP9_COMP *cpi);
266 int vp9_calc_iframe_target_size_one_pass_vbr(const struct VP9_COMP *cpi);
267 void vp9_set_gf_update_one_pass_vbr(struct VP9_COMP *const cpi);
268 void vp9_update_buffer_level_preencode(struct VP9_COMP *cpi);
269 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
270 
271 // Post encode update of the rate control parameters based
272 // on bytes used
273 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
274 // Post encode update of the rate control parameters for dropped frames
275 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
276 
277 // Updates rate correction factors
278 // Changes only the rate correction factors in the rate control structure.
279 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi);
280 
281 // Post encode drop for CBR mode.
282 int post_encode_drop_cbr(struct VP9_COMP *cpi, size_t *size);
283 
284 int vp9_test_drop(struct VP9_COMP *cpi);
285 
286 // Decide if we should drop this frame: For 1-pass CBR.
287 // Changes only the decimation count in the rate control structure
288 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
289 
290 // Computes frame size bounds.
291 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
292                                       int frame_target,
293                                       int *frame_under_shoot_limit,
294                                       int *frame_over_shoot_limit);
295 
296 // Picks q and q bounds given the target for bits
297 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi, int *bottom_index,
298                              int *top_index);
299 
300 // Estimates q to achieve a target bits per frame
301 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
302                       int active_best_quality, int active_worst_quality);
303 
304 // Estimates bits per mb for a given qindex and correction factor.
305 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
306                        double correction_factor, vpx_bit_depth_t bit_depth);
307 
308 // Clamping utilities for bitrate targets for iframes and pframes.
309 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
310                                     int target);
311 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
312                                     int target);
313 // Utility to set frame_target into the RATE_CONTROL structure
314 // This function is called only from the vp9_rc_get_..._params() functions.
315 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
316 
317 // Computes a q delta (in "q index" terms) to get from a starting q value
318 // to a target q value
319 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
320                        vpx_bit_depth_t bit_depth);
321 
322 // Computes a q delta (in "q index" terms) to get from a starting q value
323 // to a value that should equate to the given rate ratio.
324 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
325                                int qindex, double rate_target_ratio,
326                                vpx_bit_depth_t bit_depth);
327 
328 int vp9_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
329 
330 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
331 
332 void vp9_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
333                                   RATE_CONTROL *const rc);
334 
335 void vp9_set_target_rate(struct VP9_COMP *cpi);
336 
337 int vp9_resize_one_pass_cbr(struct VP9_COMP *cpi);
338 
339 void vp9_scene_detection_onepass(struct VP9_COMP *cpi);
340 
341 int vp9_encodedframe_overshoot(struct VP9_COMP *cpi, int frame_size, int *q);
342 
343 void vp9_configure_buffer_updates(struct VP9_COMP *cpi, int gf_group_index);
344 
345 void vp9_estimate_qp_gop(struct VP9_COMP *cpi);
346 
347 void vp9_compute_frame_low_motion(struct VP9_COMP *const cpi);
348 
349 #ifdef __cplusplus
350 }  // extern "C"
351 #endif
352 
353 #endif  // VPX_VP9_ENCODER_VP9_RATECTRL_H_
354