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_integer.h"
16 
17 #include "vp9/common/vp9_blockd.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 // Bits Per MB at different Q (Multiplied by 512)
24 #define BPER_MB_NORMBITS    9
25 
26 typedef enum {
27   INTER_NORMAL = 0,
28   INTER_HIGH = 1,
29   GF_ARF_LOW = 2,
30   GF_ARF_STD = 3,
31   KF_STD = 4,
32   RATE_FACTOR_LEVELS = 5
33 } RATE_FACTOR_LEVEL;
34 
35 typedef struct {
36   // Rate targetting variables
37   int base_frame_target;           // A baseline frame target before adjustment
38                                    // for previous under or over shoot.
39   int this_frame_target;           // Actual frame target after rc adjustment.
40   int projected_frame_size;
41   int sb64_target_rate;
42   int last_q[FRAME_TYPES];         // Separate values for Intra/Inter
43   int last_boosted_qindex;         // Last boosted GF/KF/ARF q
44 
45   int gfu_boost;
46   int last_boost;
47   int kf_boost;
48 
49   double rate_correction_factors[RATE_FACTOR_LEVELS];
50 
51   int frames_since_golden;
52   int frames_till_gf_update_due;
53   int max_gf_interval;
54   int static_scene_max_gf_interval;
55   int baseline_gf_interval;
56   int frames_to_key;
57   int frames_since_key;
58   int this_key_frame_forced;
59   int next_key_frame_forced;
60   int source_alt_ref_pending;
61   int source_alt_ref_active;
62   int is_src_frame_alt_ref;
63 
64   int avg_frame_bandwidth;  // Average frame size target for clip
65   int min_frame_bandwidth;  // Minimum allocation used for any frame
66   int max_frame_bandwidth;  // Maximum burst rate allowed for a frame.
67 
68   int ni_av_qi;
69   int ni_tot_qi;
70   int ni_frames;
71   int avg_frame_qindex[FRAME_TYPES];
72   double tot_q;
73   double avg_q;
74 
75   int64_t buffer_level;
76   int64_t bits_off_target;
77   int64_t vbr_bits_off_target;
78 
79   int decimation_factor;
80   int decimation_count;
81 
82   int rolling_target_bits;
83   int rolling_actual_bits;
84 
85   int long_rolling_target_bits;
86   int long_rolling_actual_bits;
87 
88   int64_t total_actual_bits;
89   int64_t total_target_bits;
90   int64_t total_target_vs_actual;
91 
92   int worst_quality;
93   int best_quality;
94 
95   int64_t starting_buffer_level;
96   int64_t optimal_buffer_level;
97   int64_t maximum_buffer_size;
98   // int active_best_quality;
99 } RATE_CONTROL;
100 
101 struct VP9_COMP;
102 struct VP9EncoderConfig;
103 
104 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
105                  RATE_CONTROL *rc);
106 
107 double vp9_convert_qindex_to_q(int qindex);
108 
109 void vp9_rc_init_minq_luts();
110 
111 // Generally at the high level, the following flow is expected
112 // to be enforced for rate control:
113 // First call per frame, one of:
114 //   vp9_rc_get_one_pass_vbr_params()
115 //   vp9_rc_get_one_pass_cbr_params()
116 //   vp9_rc_get_svc_params()
117 //   vp9_rc_get_first_pass_params()
118 //   vp9_rc_get_second_pass_params()
119 // depending on the usage to set the rate control encode parameters desired.
120 //
121 // Then, call encode_frame_to_data_rate() to perform the
122 // actual encode. This function will in turn call encode_frame()
123 // one or more times, followed by one of:
124 //   vp9_rc_postencode_update()
125 //   vp9_rc_postencode_update_drop_frame()
126 //
127 // The majority of rate control parameters are only expected
128 // to be set in the vp9_rc_get_..._params() functions and
129 // updated during the vp9_rc_postencode_update...() functions.
130 // The only exceptions are vp9_rc_drop_frame() and
131 // vp9_rc_update_rate_correction_factors() functions.
132 
133 // Functions to set parameters for encoding before the actual
134 // encode_frame_to_data_rate() function.
135 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
136 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
137 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
138 
139 // Post encode update of the rate control parameters based
140 // on bytes used
141 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
142 // Post encode update of the rate control parameters for dropped frames
143 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
144 
145 // Updates rate correction factors
146 // Changes only the rate correction factors in the rate control structure.
147 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
148 
149 // Decide if we should drop this frame: For 1-pass CBR.
150 // Changes only the decimation count in the rate control structure
151 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
152 
153 // Computes frame size bounds.
154 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
155                                       int this_frame_target,
156                                       int *frame_under_shoot_limit,
157                                       int *frame_over_shoot_limit);
158 
159 // Picks q and q bounds given the target for bits
160 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
161                              int *bottom_index,
162                              int *top_index);
163 
164 // Estimates q to achieve a target bits per frame
165 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
166                       int active_best_quality, int active_worst_quality);
167 
168 // Estimates bits per mb for a given qindex and correction factor.
169 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
170                        double correction_factor);
171 
172 // Clamping utilities for bitrate targets for iframes and pframes.
173 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
174                                     int target);
175 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
176                                     int target);
177 // Utility to set frame_target into the RATE_CONTROL structure
178 // This function is called only from the vp9_rc_get_..._params() functions.
179 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
180 
181 // Computes a q delta (in "q index" terms) to get from a starting q value
182 // to a target q value
183 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget);
184 
185 // Computes a q delta (in "q index" terms) to get from a starting q value
186 // to a value that should equate to the given rate ratio.
187 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
188                                int qindex, double rate_target_ratio);
189 
190 void vp9_rc_update_framerate(struct VP9_COMP *cpi);
191 
192 void vp9_rc_set_gf_max_interval(const struct VP9_COMP *const cpi,
193                                 RATE_CONTROL *const rc);
194 
195 #ifdef __cplusplus
196 }  // extern "C"
197 #endif
198 
199 #endif  // VP9_ENCODER_VP9_RATECTRL_H_
200