1233d2500723e5594f3e7c70896ffeeef32b9c950ywan/*
2233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3233d2500723e5594f3e7c70896ffeeef32b9c950ywan *
4233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  Use of this source code is governed by a BSD-style license
5233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  that can be found in the LICENSE file in the root of the source
6233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  tree. An additional intellectual property rights grant can be found
7233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  in the file PATENTS.  All contributing project authors may
8233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  be found in the AUTHORS file in the root of the source tree.
9233d2500723e5594f3e7c70896ffeeef32b9c950ywan */
10233d2500723e5594f3e7c70896ffeeef32b9c950ywan
11233d2500723e5594f3e7c70896ffeeef32b9c950ywan
12233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifndef VP9_ENCODER_VP9_RATECTRL_H_
13233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define VP9_ENCODER_VP9_RATECTRL_H_
14233d2500723e5594f3e7c70896ffeeef32b9c950ywan
15233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include "vpx/vpx_integer.h"
16233d2500723e5594f3e7c70896ffeeef32b9c950ywan
17233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include "vp9/common/vp9_blockd.h"
18233d2500723e5594f3e7c70896ffeeef32b9c950ywan
19233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifdef __cplusplus
20233d2500723e5594f3e7c70896ffeeef32b9c950ywanextern "C" {
21233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
22233d2500723e5594f3e7c70896ffeeef32b9c950ywan
23233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define FRAME_OVERHEAD_BITS 200
24233d2500723e5594f3e7c70896ffeeef32b9c950ywan
25233d2500723e5594f3e7c70896ffeeef32b9c950ywantypedef struct {
26233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Rate targetting variables
27233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int this_frame_target;
28233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int projected_frame_size;
29233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int sb64_target_rate;
30233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int last_q[3];                   // Separate values for Intra/Inter/ARF-GF
31233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int last_boosted_qindex;         // Last boosted GF/KF/ARF q
32233d2500723e5594f3e7c70896ffeeef32b9c950ywan
33233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int gfu_boost;
34233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int last_boost;
35233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int kf_boost;
36233d2500723e5594f3e7c70896ffeeef32b9c950ywan
37233d2500723e5594f3e7c70896ffeeef32b9c950ywan  double rate_correction_factor;
38233d2500723e5594f3e7c70896ffeeef32b9c950ywan  double key_frame_rate_correction_factor;
39233d2500723e5594f3e7c70896ffeeef32b9c950ywan  double gf_rate_correction_factor;
40233d2500723e5594f3e7c70896ffeeef32b9c950ywan
41233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int frames_since_golden;
42233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int frames_till_gf_update_due;
43233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int max_gf_interval;
44233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int static_scene_max_gf_interval;
45233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int baseline_gf_interval;
46233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int frames_to_key;
47233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int frames_since_key;
48233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int this_key_frame_forced;
49233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int next_key_frame_forced;
50233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int source_alt_ref_pending;
51233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int source_alt_ref_active;
52233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int is_src_frame_alt_ref;
53233d2500723e5594f3e7c70896ffeeef32b9c950ywan
54233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int av_per_frame_bandwidth;     // Average frame size target for clip
55233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int min_frame_bandwidth;        // Minimum allocation used for any frame
56233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int max_frame_bandwidth;        // Maximum burst rate allowed for a frame.
57233d2500723e5594f3e7c70896ffeeef32b9c950ywan
58233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int ni_av_qi;
59233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int ni_tot_qi;
60233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int ni_frames;
61233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int avg_frame_qindex[3];        // 0 - KEY, 1 - INTER, 2 - ARF/GF
62233d2500723e5594f3e7c70896ffeeef32b9c950ywan  double tot_q;
63233d2500723e5594f3e7c70896ffeeef32b9c950ywan  double avg_q;
64233d2500723e5594f3e7c70896ffeeef32b9c950ywan
65233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int64_t buffer_level;
66233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int64_t bits_off_target;
67233d2500723e5594f3e7c70896ffeeef32b9c950ywan
68233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int decimation_factor;
69233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int decimation_count;
70233d2500723e5594f3e7c70896ffeeef32b9c950ywan
71233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int rolling_target_bits;
72233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int rolling_actual_bits;
73233d2500723e5594f3e7c70896ffeeef32b9c950ywan
74233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int long_rolling_target_bits;
75233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int long_rolling_actual_bits;
76233d2500723e5594f3e7c70896ffeeef32b9c950ywan
77233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int64_t total_actual_bits;
78233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int64_t total_target_bits;
79233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int64_t total_target_vs_actual;
80233d2500723e5594f3e7c70896ffeeef32b9c950ywan
81233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int worst_quality;
82233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int best_quality;
83233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // int active_best_quality;
84233d2500723e5594f3e7c70896ffeeef32b9c950ywan} RATE_CONTROL;
85233d2500723e5594f3e7c70896ffeeef32b9c950ywan
86233d2500723e5594f3e7c70896ffeeef32b9c950ywanstruct VP9_COMP;
87233d2500723e5594f3e7c70896ffeeef32b9c950ywan
88233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_save_coding_context(struct VP9_COMP *cpi);
89233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_restore_coding_context(struct VP9_COMP *cpi);
90233d2500723e5594f3e7c70896ffeeef32b9c950ywan
91233d2500723e5594f3e7c70896ffeeef32b9c950ywandouble vp9_convert_qindex_to_q(int qindex);
92233d2500723e5594f3e7c70896ffeeef32b9c950ywan
93233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_init_minq_luts();
94233d2500723e5594f3e7c70896ffeeef32b9c950ywan
95233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Generally at the high level, the following flow is expected
96233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to be enforced for rate control:
97233d2500723e5594f3e7c70896ffeeef32b9c950ywan// First call per frame, one of:
98233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   vp9_rc_get_one_pass_vbr_params()
99233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   vp9_rc_get_one_pass_cbr_params()
100233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   vp9_rc_get_svc_params()
101233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   vp9_rc_get_first_pass_params()
102233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   vp9_rc_get_second_pass_params()
103233d2500723e5594f3e7c70896ffeeef32b9c950ywan// depending on the usage to set the rate control encode parameters desired.
104233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
105233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Then, call encode_frame_to_data_rate() to perform the
106233d2500723e5594f3e7c70896ffeeef32b9c950ywan// actual encode. This function will in turn call encode_frame()
107233d2500723e5594f3e7c70896ffeeef32b9c950ywan// one or more times, followed by one of:
108233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   vp9_rc_postencode_update()
109233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   vp9_rc_postencode_update_drop_frame()
110233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
111233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The majority of rate control parameters are only expected
112233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to be set in the vp9_rc_get_..._params() functions and
113233d2500723e5594f3e7c70896ffeeef32b9c950ywan// updated during the vp9_rc_postencode_update...() functions.
114233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The only exceptions are vp9_rc_drop_frame() and
115233d2500723e5594f3e7c70896ffeeef32b9c950ywan// vp9_rc_update_rate_correction_factors() functions.
116233d2500723e5594f3e7c70896ffeeef32b9c950ywan
117233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Functions to set parameters for encoding before the actual
118233d2500723e5594f3e7c70896ffeeef32b9c950ywan// encode_frame_to_data_rate() function.
119233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
120233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
121233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_get_svc_params(struct VP9_COMP *cpi);
122233d2500723e5594f3e7c70896ffeeef32b9c950ywan
123233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Post encode update of the rate control parameters based
124233d2500723e5594f3e7c70896ffeeef32b9c950ywan// on bytes used
125233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_postencode_update(struct VP9_COMP *cpi,
126233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              uint64_t bytes_used);
127233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Post encode update of the rate control parameters for dropped frames
128233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
129233d2500723e5594f3e7c70896ffeeef32b9c950ywan
130233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Updates rate correction factors
131233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Changes only the rate correction factors in the rate control structure.
132233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi, int damp_var);
133233d2500723e5594f3e7c70896ffeeef32b9c950ywan
134233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Decide if we should drop this frame: For 1-pass CBR.
135233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Changes only the decimation count in the rate control structure
136233d2500723e5594f3e7c70896ffeeef32b9c950ywanint vp9_rc_drop_frame(struct VP9_COMP *cpi);
137233d2500723e5594f3e7c70896ffeeef32b9c950ywan
138233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Computes frame size bounds.
139233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
140233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                      int this_frame_target,
141233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                      int *frame_under_shoot_limit,
142233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                      int *frame_over_shoot_limit);
143233d2500723e5594f3e7c70896ffeeef32b9c950ywan
144233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Picks q and q bounds given the target for bits
145233d2500723e5594f3e7c70896ffeeef32b9c950ywanint vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
146233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             int *bottom_index,
147233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             int *top_index);
148233d2500723e5594f3e7c70896ffeeef32b9c950ywan
149233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Estimates q to achieve a target bits per frame
150233d2500723e5594f3e7c70896ffeeef32b9c950ywanint vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
151233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      int active_best_quality, int active_worst_quality);
152233d2500723e5594f3e7c70896ffeeef32b9c950ywan
153233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Estimates bits per mb for a given qindex and correction factor.
154233d2500723e5594f3e7c70896ffeeef32b9c950ywanint vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
155233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       double correction_factor);
156233d2500723e5594f3e7c70896ffeeef32b9c950ywan
157233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Clamping utilities for bitrate targets for iframes and pframes.
158233d2500723e5594f3e7c70896ffeeef32b9c950ywanint vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
159233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    int target);
160233d2500723e5594f3e7c70896ffeeef32b9c950ywanint vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
161233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    int target);
162233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Utility to set frame_target into the RATE_CONTROL structure
163233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This function is called only from the vp9_rc_get_..._params() functions.
164233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
165233d2500723e5594f3e7c70896ffeeef32b9c950ywan
166233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifdef __cplusplus
167233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // extern "C"
168233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
169233d2500723e5594f3e7c70896ffeeef32b9c950ywan
170233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // VP9_ENCODER_VP9_RATECTRL_H_
171