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  // The frame processing order within a GOP
174  unsigned char index;
175  // Frame update type, e.g. ARF/GF/LF/Overlay
176  FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH];
177  unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH];
178  // The number of frames displayed so far within the GOP at a given coding
179  // frame.
180  unsigned char cur_frame_idx[MAX_STATIC_GF_GROUP_LENGTH];
181  int layer_depth[MAX_STATIC_GF_GROUP_LENGTH];
182  int arf_boost[MAX_STATIC_GF_GROUP_LENGTH];
183  int max_layer_depth;
184  int max_layer_depth_allowed;
185  // This is currently only populated for AOM_Q mode
186  unsigned char q_val[MAX_STATIC_GF_GROUP_LENGTH];
187  int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH];
188  // The frame coding type - inter/intra frame
189  FRAME_TYPE frame_type[MAX_STATIC_GF_GROUP_LENGTH];
190  // The reference frame buffer control - update or reset
191  REFBUF_STATE refbuf_state[MAX_STATIC_GF_GROUP_LENGTH];
192  int arf_index; // the index in the gf group of ARF, if no arf, then -1
193  int size; // The total length of a GOP
195 } GF_GROUP;
198 typedef struct {
199  // Track if the last frame in a GOP has higher quality.
200  int arf_gf_boost_lst;
201 } GF_STATE;
202 
203 typedef struct {
204  FIRSTPASS_STATS *stats_in_start;
205  FIRSTPASS_STATS *stats_in_end;
206  FIRSTPASS_STATS *stats_in_buf_end;
207  FIRSTPASS_STATS *total_stats;
208  FIRSTPASS_STATS *total_left_stats;
209 } STATS_BUFFER_CTX;
210 
216 typedef struct {
218  unsigned int section_intra_rating;
219  // Circular queue of first pass stats stored for most recent frames.
220  // cpi->output_pkt_list[i].data.twopass_stats.buf points to actual data stored
221  // here.
222  FIRSTPASS_STATS *frame_stats_arr[MAX_LAP_BUFFERS + 1];
223  int frame_stats_next_idx; // Index to next unused element in frame_stats_arr.
224  const FIRSTPASS_STATS *stats_in;
225  STATS_BUFFER_CTX *stats_buf_ctx;
226  int first_pass_done;
227  int64_t bits_left;
228  double modified_error_min;
229  double modified_error_max;
230  double modified_error_left;
231  double mb_av_energy;
232  double frame_avg_haar_energy;
233 
234  // An indication of the content type of the current frame
235  FRAME_CONTENT_TYPE fr_content_type;
236 
237  // Projected total bits available for a key frame group of frames
238  int64_t kf_group_bits;
239 
240  // Error score of frames still to be coded in kf group
241  int64_t kf_group_error_left;
242 
243  // Over time correction for bits per macro block estimation
244  double bpm_factor;
245 
246  // Record of target and actual bits spent in current ARF group
247  int rolling_arf_group_target_bits;
248  int rolling_arf_group_actual_bits;
249 
250  int sr_update_lag;
251 
252  int kf_zeromotion_pct;
253  int last_kfgroup_zeromotion_pct;
254  int extend_minq;
255  int extend_maxq;
256  int extend_minq_fast;
258 } TWO_PASS;
259 
262 // This structure contains several key parameters to be accumulated for this
263 // frame.
264 typedef struct {
265  // Intra prediction error.
266  int64_t intra_error;
267  // Average wavelet energy computed using Discrete Wavelet Transform (DWT).
268  int64_t frame_avg_wavelet_energy;
269  // Best of intra pred error and inter pred error using last frame as ref.
270  int64_t coded_error;
271  // Best of intra pred error and inter pred error using golden frame as ref.
272  int64_t sr_coded_error;
273  // Best of intra pred error and inter pred error using altref frame as ref.
274  int64_t tr_coded_error;
275  // Count of motion vector.
276  int mv_count;
277  // Count of blocks that pick inter prediction (inter pred error is smaller
278  // than intra pred error).
279  int inter_count;
280  // Count of blocks that pick second ref (golden frame).
281  int second_ref_count;
282  // Count of blocks that pick third ref (altref frame).
283  int third_ref_count;
284  // Count of blocks where the inter and intra are very close and very low.
285  double neutral_count;
286  // Count of blocks where intra error is very small.
287  int intra_skip_count;
288  // Start row.
289  int image_data_start_row;
290  // Count of unique non-zero motion vectors.
291  int new_mv_count;
292  // Sum of inward motion vectors.
293  int sum_in_vectors;
294  // Sum of motion vector row.
295  int sum_mvr;
296  // Sum of motion vector column.
297  int sum_mvc;
298  // Sum of absolute value of motion vector row.
299  int sum_mvr_abs;
300  // Sum of absolute value of motion vector column.
301  int sum_mvc_abs;
302  // Sum of the square of motion vector row.
303  int64_t sum_mvrs;
304  // Sum of the square of motion vector column.
305  int64_t sum_mvcs;
306  // A factor calculated using intra pred error.
307  double intra_factor;
308  // A factor that measures brightness.
309  double brightness_factor;
310 } FRAME_STATS;
311 
312 // This structure contains first pass data.
313 typedef struct {
314  // Buffer holding frame stats for all MACROBLOCKs.
315  // mb_stats[i] stores the FRAME_STATS of the ith
316  // MB in raster scan order.
317  FRAME_STATS *mb_stats;
318  // Buffer to store the prediction error of the (0,0) motion
319  // vector using the last source frame as the reference.
320  // raw_motion_err_list[i] stores the raw_motion_err of
321  // the ith MB in raster scan order.
322  int *raw_motion_err_list;
323 } FirstPassData;
324 
325 struct AV1_COMP;
326 struct EncodeFrameParams;
327 struct AV1EncoderConfig;
328 struct TileDataEnc;
329 
330 int av1_get_unit_rows_in_tile(TileInfo tile, const BLOCK_SIZE fp_block_size);
331 int av1_get_unit_cols_in_tile(TileInfo tile, const BLOCK_SIZE fp_block_size);
332 
333 void av1_rc_get_first_pass_params(struct AV1_COMP *cpi);
334 void av1_first_pass_row(struct AV1_COMP *cpi, struct ThreadData *td,
335  struct TileDataEnc *tile_data, const int mb_row,
336  const BLOCK_SIZE fp_block_size);
337 void av1_end_first_pass(struct AV1_COMP *cpi);
338 
339 void av1_twopass_zero_stats(FIRSTPASS_STATS *section);
340 void av1_accumulate_stats(FIRSTPASS_STATS *section,
341  const FIRSTPASS_STATS *frame);
362 void av1_first_pass(struct AV1_COMP *cpi, const int64_t ts_duration);
363 
364 #ifdef __cplusplus
365 } // extern "C"
366 #endif
367 
368 #endif // AOM_AV1_ENCODER_FIRSTPASS_H_
void av1_first_pass(struct AV1_COMP *cpi, const int64_t ts_duration)
AV1 first pass encoding.
Describes look ahead buffer operations.
Main encoder configuration data structure.
Definition: encoder.h:791
Top level encoder structure.
Definition: encoder.h:2042
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition: encoder.h:2675
The stucture of acummulated frame stats in the first pass.
Definition: firstpass.h:36
double pcnt_neutral
Definition: firstpass.h:91
double MVr
Definition: firstpass.h:111
double MVc
Definition: firstpass.h:119
double pcnt_second_ref
Definition: firstpass.h:81
double mvr_abs
Definition: firstpass.h:115
double pcnt_inter
Definition: firstpass.h:71
double intra_error
Definition: firstpass.h:51
double new_mv_count
Definition: firstpass.h:141
double pcnt_third_ref
Definition: firstpass.h:85
double count
Definition: firstpass.h:150
double coded_error
Definition: firstpass.h:59
double raw_error_stdev
Definition: firstpass.h:154
double pcnt_motion
Definition: firstpass.h:75
double frame
Definition: firstpass.h:41
double mv_in_out_count
Definition: firstpass.h:137
double MVcv
Definition: firstpass.h:131
double inactive_zone_cols
Definition: firstpass.h:107
double frame_avg_wavelet_energy
Definition: firstpass.h:55
double duration
Definition: firstpass.h:145
double tr_coded_error
Definition: firstpass.h:67
double sr_coded_error
Definition: firstpass.h:63
double mvc_abs
Definition: firstpass.h:123
double intra_skip_pct
Definition: firstpass.h:99
double MVrv
Definition: firstpass.h:127
double inactive_zone_rows
Definition: firstpass.h:103
double weight
Definition: firstpass.h:47
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:216