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