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