Upstream version 5.34.104.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 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #define FRAME_OVERHEAD_BITS 200
20
21 typedef struct {
22   // Rate targetting variables
23   int this_frame_target;
24   int projected_frame_size;
25   int sb64_target_rate;
26   int last_q[3];                   // Separate values for Intra/Inter/ARF-GF
27   int last_boosted_qindex;         // Last boosted GF/KF/ARF q
28
29   int gfu_boost;
30   int last_boost;
31   int kf_boost;
32
33   double rate_correction_factor;
34   double key_frame_rate_correction_factor;
35   double gf_rate_correction_factor;
36
37   unsigned int frames_since_golden;
38   unsigned int frames_till_gf_update_due;  // Count down till next GF
39   unsigned int max_gf_interval;
40   unsigned int baseline_gf_interval;
41   unsigned int frames_to_key;
42   unsigned int frames_since_key;
43   unsigned int this_key_frame_forced;
44   unsigned int next_key_frame_forced;
45   unsigned int source_alt_ref_pending;
46   unsigned int source_alt_ref_active;
47   unsigned int is_src_frame_alt_ref;
48
49   int av_per_frame_bandwidth;     // Average frame size target for clip
50   int min_frame_bandwidth;        // Minimum allocation used for any frame
51   int max_frame_bandwidth;        // Maximum burst rate allowed for a frame.
52
53   int ni_av_qi;
54   int ni_tot_qi;
55   int ni_frames;
56   int avg_frame_qindex[3];  // 0 - KEY, 1 - INTER, 2 - ARF/GF
57   double tot_q;
58   double avg_q;
59
60   int buffer_level;
61   int bits_off_target;
62
63   int decimation_factor;
64   int decimation_count;
65
66   int rolling_target_bits;
67   int rolling_actual_bits;
68
69   int long_rolling_target_bits;
70   int long_rolling_actual_bits;
71
72   int64_t total_actual_bits;
73   int total_target_vs_actual;        // debug stats
74
75   int worst_quality;
76   int best_quality;
77   // int active_best_quality;
78 } RATE_CONTROL;
79
80 struct VP9_COMP;
81
82 void vp9_save_coding_context(struct VP9_COMP *cpi);
83 void vp9_restore_coding_context(struct VP9_COMP *cpi);
84
85 void vp9_setup_key_frame(struct VP9_COMP *cpi);
86 void vp9_setup_inter_frame(struct VP9_COMP *cpi);
87
88 double vp9_convert_qindex_to_q(int qindex);
89
90 // initialize luts for minq
91 void vp9_rc_init_minq_luts(void);
92
93 // Generally at the high level, the following flow is expected
94 // to be enforced for rate control:
95 // First call per frame, one of:
96 //   vp9_rc_get_one_pass_vbr_params()
97 //   vp9_rc_get_one_pass_cbr_params()
98 //   vp9_rc_get_svc_params()
99 //   vp9_rc_get_first_pass_params()
100 //   vp9_rc_get_second_pass_params()
101 // depending on the usage to set the rate control encode parameters desired.
102 //
103 // Then, call encode_frame_to_data_rate() to perform the
104 // actual encode. This function will in turn call encode_frame()
105 // one or more times, followed by one of:
106 //   vp9_rc_postencode_update()
107 //   vp9_rc_postencode_update_drop_frame()
108 //
109 // The majority of rate control parameters are only expected
110 // to be set in the vp9_rc_get_..._params() functions and
111 // updated during the vp9_rc_postencode_update...() functions.
112 // The only exceptions are vp9_rc_drop_frame() and
113 // vp9_rc_update_rate_correction_factors() functions.
114
115 // Functions to set parameters for encoding before the actual
116 // encode_frame_to_data_rate() function.
117 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
118 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
119 void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
120
121 // Post encode update of the rate control parameters based
122 // on bytes used
123 void vp9_rc_postencode_update(struct VP9_COMP *cpi,
124                               uint64_t bytes_used);
125 // Post encode update of the rate control parameters for dropped frames
126 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
127
128 // Updates rate correction factors
129 // Changes only the rate correction factors in the rate control structure.
130 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
131
132 // Decide if we should drop this frame: For 1-pass CBR.
133 // Changes only the decimation count in the rate control structure
134 int vp9_rc_drop_frame(struct VP9_COMP *cpi);
135
136 // Computes frame size bounds.
137 void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
138                                       int this_frame_target,
139                                       int *frame_under_shoot_limit,
140                                       int *frame_over_shoot_limit);
141
142 // Picks q and q bounds given the target for bits
143 int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
144                              int *bottom_index,
145                              int *top_index);
146
147 // Estimates q to achieve a target bits per frame
148 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
149                       int active_best_quality, int active_worst_quality);
150
151 // Estimates bits per mb for a given qindex and correction factor.
152 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
153                        double correction_factor);
154
155 // Clamping utilities for bitrate targets for iframes and pframes.
156 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
157                                     int target);
158 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
159                                     int target);
160 // Utility to set frame_target into the RATE_CONTROL structure
161 // This function is called only from the vp9_rc_get_..._params() functions.
162 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
163
164 #ifdef __cplusplus
165 }  // extern "C"
166 #endif
167
168 #endif  // VP9_ENCODER_VP9_RATECTRL_H_