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