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
21extern "C" {
22#endif
23
24// Bits Per MB at different Q (Multiplied by 512)
25#define BPER_MB_NORMBITS    9
26
27typedef 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
36typedef 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  int64_t total_actual_bits;
91  int64_t total_target_bits;
92  int64_t total_target_vs_actual;
93
94  int worst_quality;
95  int best_quality;
96
97  int64_t starting_buffer_level;
98  int64_t optimal_buffer_level;
99  int64_t maximum_buffer_size;
100  // int active_best_quality;
101} RATE_CONTROL;
102
103struct VP9_COMP;
104struct VP9EncoderConfig;
105
106void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
107                 RATE_CONTROL *rc);
108
109double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
110
111void vp9_rc_init_minq_luts();
112
113// Generally at the high level, the following flow is expected
114// to be enforced for rate control:
115// First call per frame, one of:
116//   vp9_rc_get_one_pass_vbr_params()
117//   vp9_rc_get_one_pass_cbr_params()
118//   vp9_rc_get_svc_params()
119//   vp9_rc_get_first_pass_params()
120//   vp9_rc_get_second_pass_params()
121// depending on the usage to set the rate control encode parameters desired.
122//
123// Then, call encode_frame_to_data_rate() to perform the
124// actual encode. This function will in turn call encode_frame()
125// one or more times, followed by one of:
126//   vp9_rc_postencode_update()
127//   vp9_rc_postencode_update_drop_frame()
128//
129// The majority of rate control parameters are only expected
130// to be set in the vp9_rc_get_..._params() functions and
131// updated during the vp9_rc_postencode_update...() functions.
132// The only exceptions are vp9_rc_drop_frame() and
133// vp9_rc_update_rate_correction_factors() functions.
134
135// Functions to set parameters for encoding before the actual
136// encode_frame_to_data_rate() function.
137void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
138void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
139void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
140
141// Post encode update of the rate control parameters based
142// on bytes used
143void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
144// Post encode update of the rate control parameters for dropped frames
145void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
146
147// Updates rate correction factors
148// Changes only the rate correction factors in the rate control structure.
149void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
150
151// Decide if we should drop this frame: For 1-pass CBR.
152// Changes only the decimation count in the rate control structure
153int vp9_rc_drop_frame(struct VP9_COMP *cpi);
154
155// Computes frame size bounds.
156void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
157                                      int this_frame_target,
158                                      int *frame_under_shoot_limit,
159                                      int *frame_over_shoot_limit);
160
161// Picks q and q bounds given the target for bits
162int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
163                             int *bottom_index,
164                             int *top_index);
165
166// Estimates q to achieve a target bits per frame
167int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
168                      int active_best_quality, int active_worst_quality);
169
170// Estimates bits per mb for a given qindex and correction factor.
171int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
172                       double correction_factor, vpx_bit_depth_t bit_depth);
173
174// Clamping utilities for bitrate targets for iframes and pframes.
175int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
176                                    int target);
177int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
178                                    int target);
179// Utility to set frame_target into the RATE_CONTROL structure
180// This function is called only from the vp9_rc_get_..._params() functions.
181void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
182
183// Computes a q delta (in "q index" terms) to get from a starting q value
184// to a target q value
185int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
186                       vpx_bit_depth_t bit_depth);
187
188// Computes a q delta (in "q index" terms) to get from a starting q value
189// to a value that should equate to the given rate ratio.
190int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
191                               int qindex, double rate_target_ratio,
192                               vpx_bit_depth_t bit_depth);
193
194void vp9_rc_update_framerate(struct VP9_COMP *cpi);
195
196void vp9_rc_set_gf_max_interval(const struct VP9_COMP *const cpi,
197                                RATE_CONTROL *const rc);
198
199#ifdef __cplusplus
200}  // extern "C"
201#endif
202
203#endif  // VP9_ENCODER_VP9_RATECTRL_H_
204