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