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