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