xref: /aosp_15_r20/external/libvpx/vp9/encoder/vp9_firstpass.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 #ifndef VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
12 #define VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
13 
14 #include <assert.h>
15 
16 #include "vp9/common/vp9_onyxc_int.h"
17 #include "vp9/encoder/vp9_firstpass_stats.h"
18 #include "vp9/encoder/vp9_lookahead.h"
19 #include "vp9/encoder/vp9_ratectrl.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define INVALID_ROW (-1)
26 
27 #define MAX_ARF_LAYERS 6
28 #define SECTION_NOISE_DEF 250.0
29 
30 typedef struct {
31   double frame_mb_intra_factor;
32   double frame_mb_brightness_factor;
33   double frame_mb_neutral_count;
34 } FP_MB_FLOAT_STATS;
35 
36 typedef struct {
37   double intra_factor;
38   double brightness_factor;
39   int64_t coded_error;
40   int64_t sr_coded_error;
41   int64_t frame_noise_energy;
42   int64_t intra_error;
43   int intercount;
44   int second_ref_count;
45   double neutral_count;
46   double intra_count_low;   // Coded intra but low variance
47   double intra_count_high;  // Coded intra high variance
48   int intra_skip_count;
49   int image_data_start_row;
50   int mvcount;
51   int sum_mvr;
52   int sum_mvr_abs;
53   int sum_mvc;
54   int sum_mvc_abs;
55   int64_t sum_mvrs;
56   int64_t sum_mvcs;
57   int sum_in_vectors;
58   int intra_smooth_count;
59   int new_mv_count;
60 } FIRSTPASS_DATA;
61 
62 typedef enum {
63   KF_UPDATE = 0,
64   LF_UPDATE = 1,
65   GF_UPDATE = 2,
66   ARF_UPDATE = 3,
67   OVERLAY_UPDATE = 4,
68   MID_OVERLAY_UPDATE = 5,
69   USE_BUF_FRAME = 6,  // Use show existing frame, no ref buffer update
70   FRAME_UPDATE_TYPES = 7
71 } FRAME_UPDATE_TYPE;
72 
73 #define FC_ANIMATION_THRESH 0.15
74 typedef enum {
75   FC_NORMAL = 0,
76   FC_GRAPHICS_ANIMATION = 1,
77   FRAME_CONTENT_TYPES = 2
78 } FRAME_CONTENT_TYPE;
79 
80 typedef struct ExternalRcReference {
81   int last_index;
82   int golden_index;
83   int altref_index;
84 } ExternalRcReference;
85 
86 typedef struct {
87   unsigned char index;
88   RATE_FACTOR_LEVEL rf_level[MAX_STATIC_GF_GROUP_LENGTH + 2];
89   FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH + 2];
90   unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH + 2];
91   unsigned char layer_depth[MAX_STATIC_GF_GROUP_LENGTH + 2];
92   unsigned char frame_gop_index[MAX_STATIC_GF_GROUP_LENGTH + 2];
93   int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH + 2];
94   int gfu_boost[MAX_STATIC_GF_GROUP_LENGTH + 2];
95   int update_ref_idx[MAX_STATIC_GF_GROUP_LENGTH + 2];
96 
97   int frame_start;
98   int frame_end;
99   // TODO(jingning): The array size of arf_stack could be reduced.
100   int arf_index_stack[MAX_LAG_BUFFERS * 2];
101   int top_arf_idx;
102   int stack_size;
103   int gf_group_size;
104   int max_layer_depth;
105   int allowed_max_layer_depth;
106   int group_noise_energy;
107 
108   // Structure to store the reference information from external RC.
109   // Used to override reference frame decisions in libvpx.
110   ExternalRcReference ext_rc_ref[MAX_STATIC_GF_GROUP_LENGTH + 2];
111   int ref_frame_list[MAX_STATIC_GF_GROUP_LENGTH + 2][REFS_PER_FRAME];
112 } GF_GROUP;
113 
114 typedef struct {
115   const FIRSTPASS_STATS *stats;
116   int num_frames;
117 } FIRST_PASS_INFO;
118 
fps_init_first_pass_info(FIRST_PASS_INFO * first_pass_info,const FIRSTPASS_STATS * stats,int num_frames)119 static INLINE void fps_init_first_pass_info(FIRST_PASS_INFO *first_pass_info,
120                                             const FIRSTPASS_STATS *stats,
121                                             int num_frames) {
122   first_pass_info->stats = stats;
123   first_pass_info->num_frames = num_frames;
124 }
125 
fps_get_num_frames(const FIRST_PASS_INFO * first_pass_info)126 static INLINE int fps_get_num_frames(const FIRST_PASS_INFO *first_pass_info) {
127   return first_pass_info->num_frames;
128 }
129 
fps_get_frame_stats(const FIRST_PASS_INFO * first_pass_info,int show_idx)130 static INLINE const FIRSTPASS_STATS *fps_get_frame_stats(
131     const FIRST_PASS_INFO *first_pass_info, int show_idx) {
132   if (show_idx < 0 || show_idx >= first_pass_info->num_frames) {
133     return NULL;
134   }
135   return &first_pass_info->stats[show_idx];
136 }
137 
138 typedef struct {
139   unsigned int section_intra_rating;
140   unsigned int key_frame_section_intra_rating;
141   FIRSTPASS_STATS total_stats;
142   FIRSTPASS_STATS this_frame_stats;
143   const FIRSTPASS_STATS *stats_in;
144   const FIRSTPASS_STATS *stats_in_start;
145   const FIRSTPASS_STATS *stats_in_end;
146   FIRST_PASS_INFO first_pass_info;
147   FIRSTPASS_STATS total_left_stats;
148   int first_pass_done;
149   int64_t bits_left;
150   double mean_mod_score;
151   double normalized_score_left;
152   double mb_av_energy;
153   double mb_smooth_pct;
154 
155   FP_MB_FLOAT_STATS *fp_mb_float_stats;
156 
157   // An indication of the content type of the current frame
158   FRAME_CONTENT_TYPE fr_content_type;
159 
160   // Projected total bits available for a key frame group of frames
161   int64_t kf_group_bits;
162 
163   // Error score of frames still to be coded in kf group
164   double kf_group_error_left;
165 
166   double bpm_factor;
167   int rolling_arf_group_target_bits;
168   int rolling_arf_group_actual_bits;
169 
170   int sr_update_lag;
171   int kf_zeromotion_pct;
172   int last_kfgroup_zeromotion_pct;
173   int active_worst_quality;
174   int baseline_active_worst_quality;
175   int extend_minq;
176   int extend_maxq;
177   int extend_minq_fast;
178   int arnr_strength_adjustment;
179   int last_qindex_of_arf_layer[MAX_ARF_LAYERS];
180 
181   GF_GROUP gf_group;
182 
183   // Vizeir project experimental two pass rate control parameters.
184   // When |use_vizier_rc_params| is 1, the following parameters will
185   // be overwritten by pass in values. Otherwise, they are initialized
186   // by default values.
187   int use_vizier_rc_params;
188   double active_wq_factor;
189   double err_per_mb;
190   double sr_default_decay_limit;
191   double sr_diff_factor;
192   double kf_err_per_mb;
193   double kf_frame_min_boost;
194   double kf_frame_max_boost_first;  // Max for first kf in a chunk.
195   double kf_frame_max_boost_subs;   // Max for subsequent mid chunk kfs.
196   double kf_max_total_boost;
197   double gf_max_total_boost;
198   double gf_frame_max_boost;
199   double zm_factor;
200 } TWO_PASS;
201 
202 struct VP9_COMP;
203 struct ThreadData;
204 struct TileDataEnc;
205 
206 void vp9_init_first_pass(struct VP9_COMP *cpi);
207 void vp9_first_pass(struct VP9_COMP *cpi, const struct lookahead_entry *source);
208 void vp9_end_first_pass(struct VP9_COMP *cpi);
209 
210 void vp9_first_pass_encode_tile_mb_row(struct VP9_COMP *cpi,
211                                        struct ThreadData *td,
212                                        FIRSTPASS_DATA *fp_acc_data,
213                                        struct TileDataEnc *tile_data,
214                                        MV *best_ref_mv, int mb_row);
215 
216 void vp9_init_second_pass(struct VP9_COMP *cpi);
217 void vp9_rc_get_second_pass_params(struct VP9_COMP *cpi);
218 void vp9_init_vizier_params(TWO_PASS *const twopass, int screen_area);
219 
220 // Post encode update of the rate control parameters for 2-pass
221 void vp9_twopass_postencode_update(struct VP9_COMP *cpi);
222 
223 void calculate_coded_size(struct VP9_COMP *cpi, int *scaled_frame_width,
224                           int *scaled_frame_height);
225 
226 struct VP9EncoderConfig;
227 int vp9_get_frames_to_next_key(const struct VP9EncoderConfig *oxcf,
228                                const TWO_PASS *const twopass, int kf_show_idx,
229                                int min_gf_interval);
230 #if CONFIG_RATE_CTRL
231 /* Call this function to get info about the next group of pictures.
232  * This function should be called after vp9_create_compressor() when encoding
233  * starts or after vp9_get_compressed_data() when the encoding process of
234  * the last group of pictures is just finished.
235  */
236 void vp9_get_next_group_of_picture(const struct VP9_COMP *cpi,
237                                    int *first_is_key_frame, int *use_alt_ref,
238                                    int *coding_frame_count, int *first_show_idx,
239                                    int *last_gop_use_alt_ref);
240 
241 /*!\brief Call this function before coding a new group of pictures to get
242  * information about it.
243  * \param[in] oxcf                 Encoder config
244  * \param[in] twopass              Twopass info
245  * \param[in] frame_info           Frame info
246  * \param[in] rc                   Rate control state
247  * \param[in] show_idx             Show index of the first frame in the group
248  * \param[in] multi_layer_arf      Is multi-layer alternate reference used
249  * \param[in] allow_alt_ref        Is alternate reference allowed
250  * \param[in] first_is_key_frame   Is the first frame in the group a key frame
251  * \param[in] last_gop_use_alt_ref Does the last group use alternate reference
252  *
253  * \param[out] use_alt_ref         Does this group use alternate reference
254  *
255  * \return Returns coding frame count
256  */
257 int vp9_get_gop_coding_frame_count(const struct VP9EncoderConfig *oxcf,
258                                    const TWO_PASS *const twopass,
259                                    const FRAME_INFO *frame_info,
260                                    const RATE_CONTROL *rc, int show_idx,
261                                    int multi_layer_arf, int allow_alt_ref,
262                                    int first_is_key_frame,
263                                    int last_gop_use_alt_ref, int *use_alt_ref);
264 
265 int vp9_get_coding_frame_num(const struct VP9EncoderConfig *oxcf,
266                              const TWO_PASS *const twopass,
267                              const FRAME_INFO *frame_info, int multi_layer_arf,
268                              int allow_alt_ref);
269 
270 /*!\brief Compute a key frame binary map indicates whether key frames appear
271  * in the corresponding positions. The passed in key_frame_map must point to an
272  * integer array with length equal to twopass->first_pass_info.num_frames,
273  * which is the number of show frames in the video.
274  */
275 void vp9_get_key_frame_map(const struct VP9EncoderConfig *oxcf,
276                            const TWO_PASS *const twopass, int *key_frame_map);
277 #endif  // CONFIG_RATE_CTRL
278 
279 FIRSTPASS_STATS vp9_get_frame_stats(const TWO_PASS *twopass);
280 FIRSTPASS_STATS vp9_get_total_stats(const TWO_PASS *twopass);
281 
282 #ifdef __cplusplus
283 }  // extern "C"
284 #endif
285 
286 #endif  // VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
287