AOMedia AV1 Codec
firstpass.h
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #ifndef AOM_AV1_ENCODER_FIRSTPASS_H_
13 #define AOM_AV1_ENCODER_FIRSTPASS_H_
14 
15 #include "av1/common/av1_common_int.h"
16 #include "av1/common/enums.h"
17 #include "av1/encoder/lookahead.h"
18 #include "av1/encoder/ratectrl.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x)-0.000001 : (x) + 0.000001)
25 
26 #define MIN_ZERO_MOTION 0.95
27 #define MAX_SR_CODED_ERROR 40
28 #define MAX_RAW_ERR_VAR 2000
29 #define MIN_MV_IN_OUT 0.4
30 
31 #define VLOW_MOTION_THRESHOLD 950
32 
36 typedef struct {
41  double frame;
47  double weight;
51  double intra_error;
59  double coded_error;
71  double pcnt_inter;
75  double pcnt_motion;
91  double pcnt_neutral;
111  double MVr;
115  double mvr_abs;
119  double MVc;
123  double mvc_abs;
127  double MVrv;
131  double MVcv;
141  double new_mv_count;
145  double duration;
150  double count;
156 
159 #define FC_ANIMATION_THRESH 0.15
160 enum {
161  FC_NORMAL = 0,
162  FC_GRAPHICS_ANIMATION = 1,
163  FRAME_CONTENT_TYPES = 2
164 } UENUM1BYTE(FRAME_CONTENT_TYPE);
165 
171 typedef struct {
173  // Frame update type, e.g. ARF/GF/LF/Overlay
174  FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH];
175  unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH];
176  // The number of frames displayed so far within the GOP at a given coding
177  // frame.
178  unsigned char cur_frame_idx[MAX_STATIC_GF_GROUP_LENGTH];
179  int layer_depth[MAX_STATIC_GF_GROUP_LENGTH];
180  int arf_boost[MAX_STATIC_GF_GROUP_LENGTH];
181  int max_layer_depth;
182  int max_layer_depth_allowed;
183  // This is currently only populated for AOM_Q mode
184  unsigned char q_val[MAX_STATIC_GF_GROUP_LENGTH];
185  int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH];
186  // The frame coding type - inter/intra frame
187  FRAME_TYPE frame_type[MAX_STATIC_GF_GROUP_LENGTH];
188  // The reference frame buffer control - update or reset
189  REFBUF_STATE refbuf_state[MAX_STATIC_GF_GROUP_LENGTH];
190  int arf_index; // the index in the gf group of ARF, if no arf, then -1
191  int size; // The total length of a GOP
193 } GF_GROUP;
196 typedef struct {
197  // Track if the last frame in a GOP has higher quality.
198  int arf_gf_boost_lst;
199 } GF_STATE;
200 
201 typedef struct {
202  FIRSTPASS_STATS *stats_in_start;
203  FIRSTPASS_STATS *stats_in_end;
204  FIRSTPASS_STATS *stats_in_buf_end;
205  FIRSTPASS_STATS *total_stats;
206  FIRSTPASS_STATS *total_left_stats;
207 } STATS_BUFFER_CTX;
208 
214 typedef struct {
216  unsigned int section_intra_rating;
217  // Circular queue of first pass stats stored for most recent frames.
218  // cpi->output_pkt_list[i].data.twopass_stats.buf points to actual data stored
219  // here.
220  FIRSTPASS_STATS *frame_stats_arr[MAX_LAP_BUFFERS + 1];
221  int frame_stats_next_idx; // Index to next unused element in frame_stats_arr.
222  const FIRSTPASS_STATS *stats_in;
223  STATS_BUFFER_CTX *stats_buf_ctx;
224  int first_pass_done;
225  int64_t bits_left;
226  double modified_error_min;
227  double modified_error_max;
228  double modified_error_left;
229  double mb_av_energy;
230  double frame_avg_haar_energy;
231 
232  // An indication of the content type of the current frame
233  FRAME_CONTENT_TYPE fr_content_type;
234 
235  // Projected total bits available for a key frame group of frames
236  int64_t kf_group_bits;
237 
238  // Error score of frames still to be coded in kf group
239  int64_t kf_group_error_left;
240 
241  // Over time correction for bits per macro block estimation
242  double bpm_factor;
243 
244  // Record of target and actual bits spent in current ARF group
245  int rolling_arf_group_target_bits;
246  int rolling_arf_group_actual_bits;
247 
248  int sr_update_lag;
249 
250  int kf_zeromotion_pct;
251  int last_kfgroup_zeromotion_pct;
252  int extend_minq;
253  int extend_maxq;
254  int extend_minq_fast;
256 } TWO_PASS;
257 
260 // This structure contains several key parameters to be accumulated for this
261 // frame.
262 typedef struct {
263  // Intra prediction error.
264  int64_t intra_error;
265  // Average wavelet energy computed using Discrete Wavelet Transform (DWT).
266  int64_t frame_avg_wavelet_energy;
267  // Best of intra pred error and inter pred error using last frame as ref.
268  int64_t coded_error;
269  // Best of intra pred error and inter pred error using golden frame as ref.
270  int64_t sr_coded_error;
271  // Best of intra pred error and inter pred error using altref frame as ref.
272  int64_t tr_coded_error;
273  // Count of motion vector.
274  int mv_count;
275  // Count of blocks that pick inter prediction (inter pred error is smaller
276  // than intra pred error).
277  int inter_count;
278  // Count of blocks that pick second ref (golden frame).
279  int second_ref_count;
280  // Count of blocks that pick third ref (altref frame).
281  int third_ref_count;
282  // Count of blocks where the inter and intra are very close and very low.
283  double neutral_count;
284  // Count of blocks where intra error is very small.
285  int intra_skip_count;
286  // Start row.
287  int image_data_start_row;
288  // Count of unique non-zero motion vectors.
289  int new_mv_count;
290  // Sum of inward motion vectors.
291  int sum_in_vectors;
292  // Sum of motion vector row.
293  int sum_mvr;
294  // Sum of motion vector column.
295  int sum_mvc;
296  // Sum of absolute value of motion vector row.
297  int sum_mvr_abs;
298  // Sum of absolute value of motion vector column.
299  int sum_mvc_abs;
300  // Sum of the square of motion vector row.
301  int64_t sum_mvrs;
302  // Sum of the square of motion vector column.
303  int64_t sum_mvcs;
304  // A factor calculated using intra pred error.
305  double intra_factor;
306  // A factor that measures brightness.
307  double brightness_factor;
308 } FRAME_STATS;
309 
310 // This structure contains first pass data.
311 typedef struct {
312  // Buffer holding frame stats for all MACROBLOCKs.
313  // mb_stats[i] stores the FRAME_STATS of the ith
314  // MB in raster scan order.
315  FRAME_STATS *mb_stats;
316  // Buffer to store the prediction error of the (0,0) motion
317  // vector using the last source frame as the reference.
318  // raw_motion_err_list[i] stores the raw_motion_err of
319  // the ith MB in raster scan order.
320  int *raw_motion_err_list;
321 } FirstPassData;
322 
323 struct AV1_COMP;
324 struct EncodeFrameParams;
325 struct AV1EncoderConfig;
326 struct TileDataEnc;
327 
328 int av1_get_unit_rows_in_tile(TileInfo tile, const BLOCK_SIZE fp_block_size);
329 int av1_get_unit_cols_in_tile(TileInfo tile, const BLOCK_SIZE fp_block_size);
330 
331 void av1_rc_get_first_pass_params(struct AV1_COMP *cpi);
332 void av1_first_pass_row(struct AV1_COMP *cpi, struct ThreadData *td,
333  struct TileDataEnc *tile_data, const int mb_row,
334  const BLOCK_SIZE fp_block_size);
335 void av1_end_first_pass(struct AV1_COMP *cpi);
336 
337 void av1_twopass_zero_stats(FIRSTPASS_STATS *section);
338 void av1_accumulate_stats(FIRSTPASS_STATS *section,
339  const FIRSTPASS_STATS *frame);
360 void av1_first_pass(struct AV1_COMP *cpi, const int64_t ts_duration);
361 
362 #ifdef __cplusplus
363 } // extern "C"
364 #endif
365 
366 #endif // AOM_AV1_ENCODER_FIRSTPASS_H_
double weight
Definition: firstpass.h:47
double tr_coded_error
Definition: firstpass.h:67
double MVrv
Definition: firstpass.h:127
double pcnt_third_ref
Definition: firstpass.h:85
double sr_coded_error
Definition: firstpass.h:63
double frame
Definition: firstpass.h:41
double mv_in_out_count
Definition: firstpass.h:137
double pcnt_motion
Definition: firstpass.h:75
double pcnt_second_ref
Definition: firstpass.h:81
double MVcv
Definition: firstpass.h:131
Data related to the current GF/ARF group and the individual frames within the group.
Definition: firstpass.h:171
Two pass status and control data.
Definition: firstpass.h:214
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition: encoder.h:2726
double mvc_abs
Definition: firstpass.h:123
double inactive_zone_rows
Definition: firstpass.h:103
double new_mv_count
Definition: firstpass.h:141
Describes look ahead buffer operations.
double MVc
Definition: firstpass.h:119
double pcnt_inter
Definition: firstpass.h:71
The stucture of acummulated frame stats in the first pass.
Definition: firstpass.h:36
double intra_skip_pct
Definition: firstpass.h:99
double count
Definition: firstpass.h:150
void av1_first_pass(struct AV1_COMP *cpi, const int64_t ts_duration)
AV1 first pass encoding.
double duration
Definition: firstpass.h:145
Top level encoder structure.
Definition: encoder.h:2095
double intra_error
Definition: firstpass.h:51
double coded_error
Definition: firstpass.h:59
Main encoder configuration data structure.
Definition: encoder.h:805
double inactive_zone_cols
Definition: firstpass.h:107
double frame_avg_wavelet_energy
Definition: firstpass.h:55
double mvr_abs
Definition: firstpass.h:115
double raw_error_stdev
Definition: firstpass.h:154
double MVr
Definition: firstpass.h:111
double pcnt_neutral
Definition: firstpass.h:91