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 #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 #define INVALID_ROW (-1)
46 
47 #define MAX_ARF_LAYERS 6
48 #define SECTION_NOISE_DEF 250.0
49 
50 typedef struct {
51   double frame_mb_intra_factor;
52   double frame_mb_brightness_factor;
53   double frame_mb_neutral_count;
54 } FP_MB_FLOAT_STATS;
55 
56 typedef struct {
57   double intra_factor;
58   double brightness_factor;
59   int64_t coded_error;
60   int64_t sr_coded_error;
61   int64_t frame_noise_energy;
62   int64_t intra_error;
63   int intercount;
64   int second_ref_count;
65   double neutral_count;
66   double intra_count_low;   // Coded intra but low variance
67   double intra_count_high;  // Coded intra high variance
68   int intra_skip_count;
69   int image_data_start_row;
70   int mvcount;
71   int sum_mvr;
72   int sum_mvr_abs;
73   int sum_mvc;
74   int sum_mvc_abs;
75   int64_t sum_mvrs;
76   int64_t sum_mvcs;
77   int sum_in_vectors;
78   int intra_smooth_count;
79 } FIRSTPASS_DATA;
80 
81 typedef struct {
82   double frame;
83   double weight;
84   double intra_error;
85   double coded_error;
86   double sr_coded_error;
87   double frame_noise_energy;
88   double pcnt_inter;
89   double pcnt_motion;
90   double pcnt_second_ref;
91   double pcnt_neutral;
92   double pcnt_intra_low;   // Coded intra but low variance
93   double pcnt_intra_high;  // Coded intra high variance
94   double intra_skip_pct;
95   double intra_smooth_pct;    // % of blocks that are smooth
96   double inactive_zone_rows;  // Image mask rows top and bottom.
97   double inactive_zone_cols;  // Image mask columns at left and right edges.
98   double MVr;
99   double mvr_abs;
100   double MVc;
101   double mvc_abs;
102   double MVrv;
103   double MVcv;
104   double mv_in_out_count;
105   double duration;
106   double count;
107   int64_t spatial_layer_id;
108 } FIRSTPASS_STATS;
109 
110 typedef enum {
111   KF_UPDATE = 0,
112   LF_UPDATE = 1,
113   GF_UPDATE = 2,
114   ARF_UPDATE = 3,
115   OVERLAY_UPDATE = 4,
116   MID_OVERLAY_UPDATE = 5,
117   USE_BUF_FRAME = 6,  // Use show existing frame, no ref buffer update
118   FRAME_UPDATE_TYPES = 7
119 } FRAME_UPDATE_TYPE;
120 
121 #define FC_ANIMATION_THRESH 0.15
122 typedef enum {
123   FC_NORMAL = 0,
124   FC_GRAPHICS_ANIMATION = 1,
125   FRAME_CONTENT_TYPES = 2
126 } FRAME_CONTENT_TYPE;
127 
128 typedef struct {
129   unsigned char index;
130   RATE_FACTOR_LEVEL rf_level[MAX_STATIC_GF_GROUP_LENGTH + 2];
131   FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH + 2];
132   unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH + 2];
133   unsigned char layer_depth[MAX_STATIC_GF_GROUP_LENGTH + 2];
134   unsigned char frame_gop_index[MAX_STATIC_GF_GROUP_LENGTH + 2];
135   int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH + 2];
136   int gfu_boost[MAX_STATIC_GF_GROUP_LENGTH + 2];
137 
138   int frame_start;
139   int frame_end;
140   // TODO(jingning): The array size of arf_stack could be reduced.
141   int arf_index_stack[MAX_LAG_BUFFERS * 2];
142   int top_arf_idx;
143   int stack_size;
144   int gf_group_size;
145   int max_layer_depth;
146   int allowed_max_layer_depth;
147   int group_noise_energy;
148 } GF_GROUP;
149 
150 typedef struct {
151   const FIRSTPASS_STATS *stats;
152   int num_frames;
153 } FIRST_PASS_INFO;
154 
fps_init_first_pass_info(FIRST_PASS_INFO * first_pass_info,const FIRSTPASS_STATS * stats,int num_frames)155 static INLINE void fps_init_first_pass_info(FIRST_PASS_INFO *first_pass_info,
156                                             const FIRSTPASS_STATS *stats,
157                                             int num_frames) {
158   first_pass_info->stats = stats;
159   first_pass_info->num_frames = num_frames;
160 }
161 
fps_get_num_frames(const FIRST_PASS_INFO * first_pass_info)162 static INLINE int fps_get_num_frames(const FIRST_PASS_INFO *first_pass_info) {
163   return first_pass_info->num_frames;
164 }
165 
fps_get_frame_stats(const FIRST_PASS_INFO * first_pass_info,int show_idx)166 static INLINE const FIRSTPASS_STATS *fps_get_frame_stats(
167     const FIRST_PASS_INFO *first_pass_info, int show_idx) {
168   if (show_idx < 0 || show_idx >= first_pass_info->num_frames) {
169     return NULL;
170   }
171   return &first_pass_info->stats[show_idx];
172 }
173 
174 typedef struct {
175   unsigned int section_intra_rating;
176   unsigned int key_frame_section_intra_rating;
177   FIRSTPASS_STATS total_stats;
178   FIRSTPASS_STATS this_frame_stats;
179   const FIRSTPASS_STATS *stats_in;
180   const FIRSTPASS_STATS *stats_in_start;
181   const FIRSTPASS_STATS *stats_in_end;
182   FIRST_PASS_INFO first_pass_info;
183   FIRSTPASS_STATS total_left_stats;
184   int first_pass_done;
185   int64_t bits_left;
186   double mean_mod_score;
187   double normalized_score_left;
188   double mb_av_energy;
189   double mb_smooth_pct;
190 
191 #if CONFIG_FP_MB_STATS
192   uint8_t *frame_mb_stats_buf;
193   uint8_t *this_frame_mb_stats;
194   FIRSTPASS_MB_STATS firstpass_mb_stats;
195 #endif
196 
197   FP_MB_FLOAT_STATS *fp_mb_float_stats;
198 
199   // An indication of the content type of the current frame
200   FRAME_CONTENT_TYPE fr_content_type;
201 
202   // Projected total bits available for a key frame group of frames
203   int64_t kf_group_bits;
204 
205   // Error score of frames still to be coded in kf group
206   double kf_group_error_left;
207 
208   double bpm_factor;
209   int rolling_arf_group_target_bits;
210   int rolling_arf_group_actual_bits;
211 
212   int sr_update_lag;
213   int kf_zeromotion_pct;
214   int last_kfgroup_zeromotion_pct;
215   int active_worst_quality;
216   int baseline_active_worst_quality;
217   int extend_minq;
218   int extend_maxq;
219   int extend_minq_fast;
220   int arnr_strength_adjustment;
221   int last_qindex_of_arf_layer[MAX_ARF_LAYERS];
222 
223   GF_GROUP gf_group;
224 } TWO_PASS;
225 
226 struct VP9_COMP;
227 struct ThreadData;
228 struct TileDataEnc;
229 
230 void vp9_init_first_pass(struct VP9_COMP *cpi);
231 void vp9_first_pass(struct VP9_COMP *cpi, const struct lookahead_entry *source);
232 void vp9_end_first_pass(struct VP9_COMP *cpi);
233 
234 void vp9_first_pass_encode_tile_mb_row(struct VP9_COMP *cpi,
235                                        struct ThreadData *td,
236                                        FIRSTPASS_DATA *fp_acc_data,
237                                        struct TileDataEnc *tile_data,
238                                        MV *best_ref_mv, int mb_row);
239 
240 void vp9_init_second_pass(struct VP9_COMP *cpi);
241 void vp9_rc_get_second_pass_params(struct VP9_COMP *cpi);
242 
243 // Post encode update of the rate control parameters for 2-pass
244 void vp9_twopass_postencode_update(struct VP9_COMP *cpi);
245 
246 void calculate_coded_size(struct VP9_COMP *cpi, int *scaled_frame_width,
247                           int *scaled_frame_height);
248 
249 struct VP9EncoderConfig;
250 int vp9_get_frames_to_next_key(const struct VP9EncoderConfig *oxcf,
251                                const FRAME_INFO *frame_info,
252                                const FIRST_PASS_INFO *first_pass_info,
253                                int kf_show_idx, int min_gf_interval);
254 #if CONFIG_RATE_CTRL
255 int vp9_get_coding_frame_num(const struct VP9EncoderConfig *oxcf,
256                              const FRAME_INFO *frame_info,
257                              const FIRST_PASS_INFO *first_pass_info,
258                              int multi_layer_arf, int allow_alt_ref);
259 #endif
260 
261 FIRSTPASS_STATS vp9_get_frame_stats(const TWO_PASS *two_pass);
262 FIRSTPASS_STATS vp9_get_total_stats(const TWO_PASS *two_pass);
263 
264 #ifdef __cplusplus
265 }  // extern "C"
266 #endif
267 
268 #endif  // VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
269