• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 typedef struct {
34   // Frame number in display order, if stats are for a single frame.
35   // No real meaning for a collection of frames.
36   double frame;
37   // Weight assigned to this frame (or total weight for the collection of
38   // frames) currently based on intra factor and brightness factor. This is used
39   // to distribute bits betweeen easier and harder frames.
40   double weight;
41   // Intra prediction error.
42   double intra_error;
43   // Average wavelet energy computed using Discrete Wavelet Transform (DWT).
44   double frame_avg_wavelet_energy;
45   // Best of intra pred error and inter pred error using last frame as ref.
46   double coded_error;
47   // Best of intra pred error and inter pred error using golden frame as ref.
48   double sr_coded_error;
49   // Best of intra pred error and inter pred error using altref frame as ref.
50   double tr_coded_error;
51   // Percentage of blocks with inter pred error < intra pred error.
52   double pcnt_inter;
53   // Percentage of blocks using (inter prediction and) non-zero motion vectors.
54   double pcnt_motion;
55   // Percentage of blocks where golden frame was better than last or intra:
56   // inter pred error using golden frame < inter pred error using last frame and
57   // inter pred error using golden frame < intra pred error
58   double pcnt_second_ref;
59   // Percentage of blocks where altref frame was better than intra, last, golden
60   double pcnt_third_ref;
61   // Percentage of blocks where intra and inter prediction errors were very
62   // close. Note that this is a 'weighted count', that is, the so blocks may be
63   // weighted by how close the two errors were.
64   double pcnt_neutral;
65   // Percentage of blocks that have almost no intra error residual
66   // (i.e. are in effect completely flat and untextured in the intra
67   // domain). In natural videos this is uncommon, but it is much more
68   // common in animations, graphics and screen content, so may be used
69   // as a signal to detect these types of content.
70   double intra_skip_pct;
71   // Image mask rows top and bottom.
72   double inactive_zone_rows;
73   // Image mask columns at left and right edges.
74   double inactive_zone_cols;
75   // Average of row motion vectors.
76   double MVr;
77   // Mean of absolute value of row motion vectors.
78   double mvr_abs;
79   // Mean of column motion vectors.
80   double MVc;
81   // Mean of absolute value of column motion vectors.
82   double mvc_abs;
83   // Variance of row motion vectors.
84   double MVrv;
85   // Variance of column motion vectors.
86   double MVcv;
87   // Value in range [-1,1] indicating fraction of row and column motion vectors
88   // that point inwards (negative MV value) or outwards (positive MV value).
89   // For example, value of 1 indicates, all row/column MVs are inwards.
90   double mv_in_out_count;
91   // Count of unique non-zero motion vectors.
92   double new_mv_count;
93   // Duration of the frame / collection of frames.
94   double duration;
95   // 1.0 if stats are for a single frame, OR
96   // Number of frames in this collection for which the stats are accumulated.
97   double count;
98   // standard deviation for (0, 0) motion prediction error
99   double raw_error_stdev;
100 } FIRSTPASS_STATS;
101 
102 #define FC_ANIMATION_THRESH 0.15
103 enum {
104   FC_NORMAL = 0,
105   FC_GRAPHICS_ANIMATION = 1,
106   FRAME_CONTENT_TYPES = 2
107 } UENUM1BYTE(FRAME_CONTENT_TYPE);
108 
109 typedef struct {
110   unsigned char index;
111   FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH];
112   unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH];
113   // The number of frames displayed so far within the GOP at a given coding
114   // frame.
115   unsigned char cur_frame_idx[MAX_STATIC_GF_GROUP_LENGTH];
116   unsigned char frame_disp_idx[MAX_STATIC_GF_GROUP_LENGTH];
117   int ref_frame_disp_idx[MAX_STATIC_GF_GROUP_LENGTH][REF_FRAMES];
118   int ref_frame_gop_idx[MAX_STATIC_GF_GROUP_LENGTH][REF_FRAMES];
119 
120   // TODO(jingning): Unify the data structure used here after the new control
121   // mechanism is in place.
122   int layer_depth[MAX_STATIC_GF_GROUP_LENGTH];
123   int arf_boost[MAX_STATIC_GF_GROUP_LENGTH];
124   int max_layer_depth;
125   int max_layer_depth_allowed;
126   // This is currently only populated for AOM_Q mode
127   unsigned char q_val[MAX_STATIC_GF_GROUP_LENGTH];
128   int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH];
129   int size;
130 } GF_GROUP;
131 
132 typedef struct {
133   FIRSTPASS_STATS *stats_in_start;
134   FIRSTPASS_STATS *stats_in_end;
135   FIRSTPASS_STATS *stats_in_buf_end;
136   FIRSTPASS_STATS *total_stats;
137   FIRSTPASS_STATS *total_left_stats;
138 } STATS_BUFFER_CTX;
139 
140 typedef struct {
141   unsigned int section_intra_rating;
142   // Circular queue of first pass stats stored for most recent frames.
143   // cpi->output_pkt_list[i].data.twopass_stats.buf points to actual data stored
144   // here.
145   FIRSTPASS_STATS *frame_stats_arr[MAX_LAP_BUFFERS + 1];
146   int frame_stats_next_idx;  // Index to next unused element in frame_stats_arr.
147   const FIRSTPASS_STATS *stats_in;
148   STATS_BUFFER_CTX *stats_buf_ctx;
149   int first_pass_done;
150   int64_t bits_left;
151   double modified_error_min;
152   double modified_error_max;
153   double modified_error_left;
154   double mb_av_energy;
155   double frame_avg_haar_energy;
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   int64_t kf_group_error_left;
165 
166   // Over time correction for bits per macro block estimation
167   double bpm_factor;
168 
169   // Record of target and actual bits spent in current ARF group
170   int rolling_arf_group_target_bits;
171   int rolling_arf_group_actual_bits;
172 
173   int sr_update_lag;
174 
175   int kf_zeromotion_pct;
176   int last_kfgroup_zeromotion_pct;
177   int extend_minq;
178   int extend_maxq;
179   int extend_minq_fast;
180 } TWO_PASS;
181 
182 struct AV1_COMP;
183 struct EncodeFrameParams;
184 struct AV1EncoderConfig;
185 
186 void av1_rc_get_first_pass_params(struct AV1_COMP *cpi);
187 void av1_first_pass(struct AV1_COMP *cpi, const int64_t ts_duration);
188 void av1_end_first_pass(struct AV1_COMP *cpi);
189 
190 void av1_twopass_zero_stats(FIRSTPASS_STATS *section);
191 
192 #ifdef __cplusplus
193 }  // extern "C"
194 #endif
195 
196 #endif  // AOM_AV1_ENCODER_FIRSTPASS_H_
197