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/enums.h"
16 #include "av1/common/onyxc_int.h"
17 #include "av1/encoder/lookahead.h"
18 #include "av1/encoder/ratectrl.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #if CONFIG_FP_MB_STATS
25 
26 #define FPMB_DCINTRA_MASK 0x01
27 
28 #define FPMB_MOTION_ZERO_MASK 0x02
29 #define FPMB_MOTION_LEFT_MASK 0x04
30 #define FPMB_MOTION_RIGHT_MASK 0x08
31 #define FPMB_MOTION_UP_MASK 0x10
32 #define FPMB_MOTION_DOWN_MASK 0x20
33 
34 #define FPMB_ERROR_SMALL_MASK 0x40
35 #define FPMB_ERROR_LARGE_MASK 0x80
36 #define FPMB_ERROR_SMALL_TH 2000
37 #define FPMB_ERROR_LARGE_TH 48000
38 
39 typedef struct {
40   uint8_t *mb_stats_start;
41   uint8_t *mb_stats_end;
42 } FIRSTPASS_MB_STATS;
43 #endif
44 
45 // Length of the bi-predictive frame group (BFG)
46 // NOTE: Currently each BFG contains one backward ref (BWF) frame plus a certain
47 //       number of bi-predictive frames.
48 #define BFG_INTERVAL 2
49 // The maximum number of extra ALTREF's except ALTREF_FRAME
50 #define MAX_EXT_ARFS (REF_FRAMES - BWDREF_FRAME - 1)
51 
52 #define MIN_EXT_ARF_INTERVAL 4
53 
54 #define MIN_ZERO_MOTION 0.95
55 #define MAX_SR_CODED_ERROR 40
56 #define MAX_RAW_ERR_VAR 2000
57 #define MIN_MV_IN_OUT 0.4
58 
59 #define VLOW_MOTION_THRESHOLD 950
60 
61 typedef struct {
62   double frame;
63   double weight;
64   double intra_error;
65   double frame_avg_wavelet_energy;
66   double coded_error;
67   double sr_coded_error;
68   double pcnt_inter;
69   double pcnt_motion;
70   double pcnt_second_ref;
71   double pcnt_neutral;
72   double intra_skip_pct;
73   double inactive_zone_rows;  // Image mask rows top and bottom.
74   double inactive_zone_cols;  // Image mask columns at left and right edges.
75   double MVr;
76   double mvr_abs;
77   double MVc;
78   double mvc_abs;
79   double MVrv;
80   double MVcv;
81   double mv_in_out_count;
82   double new_mv_count;
83   double duration;
84   double count;
85   // standard deviation for (0, 0) motion prediction error
86   double raw_error_stdev;
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   BRF_UPDATE = 5,            // Backward Reference Frame
96   LAST_BIPRED_UPDATE = 6,    // Last Bi-predictive Frame
97   BIPRED_UPDATE = 7,         // Bi-predictive Frame, but not the last one
98   INTNL_OVERLAY_UPDATE = 8,  // Internal Overlay Frame
99   INTNL_ARF_UPDATE = 9,      // Internal Altref Frame (candidate for ALTREF2)
100   FRAME_UPDATE_TYPES = 10
101 } FRAME_UPDATE_TYPE;
102 
103 #define FC_ANIMATION_THRESH 0.15
104 typedef enum {
105   FC_NORMAL = 0,
106   FC_GRAPHICS_ANIMATION = 1,
107   FRAME_CONTENT_TYPES = 2
108 } FRAME_CONTENT_TYPE;
109 
110 typedef struct {
111   unsigned char index;
112   RATE_FACTOR_LEVEL rf_level[(MAX_LAG_BUFFERS * 2) + 1];
113   FRAME_UPDATE_TYPE update_type[(MAX_LAG_BUFFERS * 2) + 1];
114   unsigned char arf_src_offset[(MAX_LAG_BUFFERS * 2) + 1];
115   unsigned char arf_update_idx[(MAX_LAG_BUFFERS * 2) + 1];
116   unsigned char arf_ref_idx[(MAX_LAG_BUFFERS * 2) + 1];
117 #if USE_SYMM_MULTI_LAYER
118   unsigned char arf_pos_in_gf[(MAX_LAG_BUFFERS * 2) + 1];
119   unsigned char pyramid_level[(MAX_LAG_BUFFERS * 2) + 1];
120   unsigned char pyramid_height;
121   unsigned char pyramid_lvl_nodes[MAX_PYRAMID_LVL];
122 #endif
123   unsigned char brf_src_offset[(MAX_LAG_BUFFERS * 2) + 1];
124   unsigned char bidir_pred_enabled[(MAX_LAG_BUFFERS * 2) + 1];
125   unsigned char ref_fb_idx_map[(MAX_LAG_BUFFERS * 2) + 1][REF_FRAMES];
126   unsigned char refresh_idx[(MAX_LAG_BUFFERS * 2) + 1];
127   unsigned char refresh_flag[(MAX_LAG_BUFFERS * 2) + 1];
128   int bit_allocation[(MAX_LAG_BUFFERS * 2) + 1];
129 } GF_GROUP;
130 
131 typedef struct {
132   unsigned int section_intra_rating;
133   FIRSTPASS_STATS total_stats;
134   FIRSTPASS_STATS this_frame_stats;
135   const FIRSTPASS_STATS *stats_in;
136   const FIRSTPASS_STATS *stats_in_start;
137   const FIRSTPASS_STATS *stats_in_end;
138   FIRSTPASS_STATS total_left_stats;
139   int first_pass_done;
140   int64_t bits_left;
141   double modified_error_min;
142   double modified_error_max;
143   double modified_error_left;
144   double mb_av_energy;
145   double frame_avg_haar_energy;
146 
147 #if CONFIG_FP_MB_STATS
148   uint8_t *frame_mb_stats_buf;
149   uint8_t *this_frame_mb_stats;
150   FIRSTPASS_MB_STATS firstpass_mb_stats;
151 #endif
152   // An indication of the content type of the current frame
153   FRAME_CONTENT_TYPE fr_content_type;
154 
155   // Projected total bits available for a key frame group of frames
156   int64_t kf_group_bits;
157 
158   // Error score of frames still to be coded in kf group
159   int64_t kf_group_error_left;
160 
161   // The fraction for a kf groups total bits allocated to the inter frames
162   double kfgroup_inter_fraction;
163 
164   int sr_update_lag;
165 
166   int kf_zeromotion_pct;
167   int last_kfgroup_zeromotion_pct;
168   int gf_zeromotion_pct;
169   int active_worst_quality;
170   int baseline_active_worst_quality;
171   int extend_minq;
172   int extend_maxq;
173   int extend_minq_fast;
174 
175   GF_GROUP gf_group;
176 } TWO_PASS;
177 
178 struct AV1_COMP;
179 
180 void av1_init_first_pass(struct AV1_COMP *cpi);
181 void av1_rc_get_first_pass_params(struct AV1_COMP *cpi);
182 void av1_first_pass(struct AV1_COMP *cpi, const struct lookahead_entry *source);
183 void av1_end_first_pass(struct AV1_COMP *cpi);
184 
185 void av1_init_second_pass(struct AV1_COMP *cpi);
186 void av1_rc_get_second_pass_params(struct AV1_COMP *cpi);
187 void av1_configure_buffer_updates_firstpass(struct AV1_COMP *cpi,
188                                             FRAME_UPDATE_TYPE update_type);
189 
190 // Post encode update of the rate control parameters for 2-pass
191 void av1_twopass_postencode_update(struct AV1_COMP *cpi);
192 
get_number_of_extra_arfs(int interval,int arf_pending)193 static INLINE int get_number_of_extra_arfs(int interval, int arf_pending) {
194   if (arf_pending && MAX_EXT_ARFS > 0)
195     return interval >= MIN_EXT_ARF_INTERVAL * (MAX_EXT_ARFS + 1)
196                ? MAX_EXT_ARFS
197                : interval >= MIN_EXT_ARF_INTERVAL * MAX_EXT_ARFS
198                      ? MAX_EXT_ARFS - 1
199                      : 0;
200   else
201     return 0;
202 }
203 
204 #ifdef __cplusplus
205 }  // extern "C"
206 #endif
207 
208 #endif  // AOM_AV1_ENCODER_FIRSTPASS_H_
209