• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_firstpass_stats.h"
18 #include "vp9/encoder/vp9_lookahead.h"
19 #include "vp9/encoder/vp9_ratectrl.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define INVALID_ROW (-1)
26 
27 #define MAX_ARF_LAYERS 6
28 #define SECTION_NOISE_DEF 250.0
29 
30 typedef struct {
31   double frame_mb_intra_factor;
32   double frame_mb_brightness_factor;
33   double frame_mb_neutral_count;
34 } FP_MB_FLOAT_STATS;
35 
36 typedef struct {
37   double intra_factor;
38   double brightness_factor;
39   int64_t coded_error;
40   int64_t sr_coded_error;
41   int64_t frame_noise_energy;
42   int64_t intra_error;
43   int intercount;
44   int second_ref_count;
45   double neutral_count;
46   double intra_count_low;   // Coded intra but low variance
47   double intra_count_high;  // Coded intra high variance
48   int intra_skip_count;
49   int image_data_start_row;
50   int mvcount;
51   int sum_mvr;
52   int sum_mvr_abs;
53   int sum_mvc;
54   int sum_mvc_abs;
55   int64_t sum_mvrs;
56   int64_t sum_mvcs;
57   int sum_in_vectors;
58   int intra_smooth_count;
59   int new_mv_count;
60 } FIRSTPASS_DATA;
61 
62 typedef enum {
63   KF_UPDATE = 0,
64   LF_UPDATE = 1,
65   GF_UPDATE = 2,
66   ARF_UPDATE = 3,
67   OVERLAY_UPDATE = 4,
68   MID_OVERLAY_UPDATE = 5,
69   USE_BUF_FRAME = 6,  // Use show existing frame, no ref buffer update
70   FRAME_UPDATE_TYPES = 7
71 } FRAME_UPDATE_TYPE;
72 
73 #define FC_ANIMATION_THRESH 0.15
74 typedef enum {
75   FC_NORMAL = 0,
76   FC_GRAPHICS_ANIMATION = 1,
77   FRAME_CONTENT_TYPES = 2
78 } FRAME_CONTENT_TYPE;
79 
80 typedef struct {
81   unsigned char index;
82   RATE_FACTOR_LEVEL rf_level[MAX_STATIC_GF_GROUP_LENGTH + 2];
83   FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH + 2];
84   unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH + 2];
85   unsigned char layer_depth[MAX_STATIC_GF_GROUP_LENGTH + 2];
86   unsigned char frame_gop_index[MAX_STATIC_GF_GROUP_LENGTH + 2];
87   int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH + 2];
88   int gfu_boost[MAX_STATIC_GF_GROUP_LENGTH + 2];
89 
90   int frame_start;
91   int frame_end;
92   // TODO(jingning): The array size of arf_stack could be reduced.
93   int arf_index_stack[MAX_LAG_BUFFERS * 2];
94   int top_arf_idx;
95   int stack_size;
96   int gf_group_size;
97   int max_layer_depth;
98   int allowed_max_layer_depth;
99   int group_noise_energy;
100 } GF_GROUP;
101 
102 typedef struct {
103   const FIRSTPASS_STATS *stats;
104   int num_frames;
105 } FIRST_PASS_INFO;
106 
fps_init_first_pass_info(FIRST_PASS_INFO * first_pass_info,const FIRSTPASS_STATS * stats,int num_frames)107 static INLINE void fps_init_first_pass_info(FIRST_PASS_INFO *first_pass_info,
108                                             const FIRSTPASS_STATS *stats,
109                                             int num_frames) {
110   first_pass_info->stats = stats;
111   first_pass_info->num_frames = num_frames;
112 }
113 
fps_get_num_frames(const FIRST_PASS_INFO * first_pass_info)114 static INLINE int fps_get_num_frames(const FIRST_PASS_INFO *first_pass_info) {
115   return first_pass_info->num_frames;
116 }
117 
fps_get_frame_stats(const FIRST_PASS_INFO * first_pass_info,int show_idx)118 static INLINE const FIRSTPASS_STATS *fps_get_frame_stats(
119     const FIRST_PASS_INFO *first_pass_info, int show_idx) {
120   if (show_idx < 0 || show_idx >= first_pass_info->num_frames) {
121     return NULL;
122   }
123   return &first_pass_info->stats[show_idx];
124 }
125 
126 typedef struct {
127   unsigned int section_intra_rating;
128   unsigned int key_frame_section_intra_rating;
129   FIRSTPASS_STATS total_stats;
130   FIRSTPASS_STATS this_frame_stats;
131   const FIRSTPASS_STATS *stats_in;
132   const FIRSTPASS_STATS *stats_in_start;
133   const FIRSTPASS_STATS *stats_in_end;
134   FIRST_PASS_INFO first_pass_info;
135   FIRSTPASS_STATS total_left_stats;
136   int first_pass_done;
137   int64_t bits_left;
138   double mean_mod_score;
139   double normalized_score_left;
140   double mb_av_energy;
141   double mb_smooth_pct;
142 
143   FP_MB_FLOAT_STATS *fp_mb_float_stats;
144 
145   // An indication of the content type of the current frame
146   FRAME_CONTENT_TYPE fr_content_type;
147 
148   // Projected total bits available for a key frame group of frames
149   int64_t kf_group_bits;
150 
151   // Error score of frames still to be coded in kf group
152   double kf_group_error_left;
153 
154   double bpm_factor;
155   int rolling_arf_group_target_bits;
156   int rolling_arf_group_actual_bits;
157 
158   int sr_update_lag;
159   int kf_zeromotion_pct;
160   int last_kfgroup_zeromotion_pct;
161   int active_worst_quality;
162   int baseline_active_worst_quality;
163   int extend_minq;
164   int extend_maxq;
165   int extend_minq_fast;
166   int arnr_strength_adjustment;
167   int last_qindex_of_arf_layer[MAX_ARF_LAYERS];
168 
169   GF_GROUP gf_group;
170 
171   // Vizeir project experimental two pass rate control parameters.
172   // When |use_vizier_rc_params| is 1, the following parameters will
173   // be overwritten by pass in values. Otherwise, they are initialized
174   // by default values.
175   int use_vizier_rc_params;
176   double active_wq_factor;
177   double err_per_mb;
178   double sr_default_decay_limit;
179   double sr_diff_factor;
180   double kf_err_per_mb;
181   double kf_frame_min_boost;
182   double kf_frame_max_boost_first;  // Max for first kf in a chunk.
183   double kf_frame_max_boost_subs;   // Max for subsequent mid chunk kfs.
184   double kf_max_total_boost;
185   double gf_max_total_boost;
186   double gf_frame_max_boost;
187   double zm_factor;
188 } TWO_PASS;
189 
190 struct VP9_COMP;
191 struct ThreadData;
192 struct TileDataEnc;
193 
194 void vp9_init_first_pass(struct VP9_COMP *cpi);
195 void vp9_first_pass(struct VP9_COMP *cpi, const struct lookahead_entry *source);
196 void vp9_end_first_pass(struct VP9_COMP *cpi);
197 
198 void vp9_first_pass_encode_tile_mb_row(struct VP9_COMP *cpi,
199                                        struct ThreadData *td,
200                                        FIRSTPASS_DATA *fp_acc_data,
201                                        struct TileDataEnc *tile_data,
202                                        MV *best_ref_mv, int mb_row);
203 
204 void vp9_init_second_pass(struct VP9_COMP *cpi);
205 void vp9_rc_get_second_pass_params(struct VP9_COMP *cpi);
206 void vp9_init_vizier_params(TWO_PASS *const twopass, int screen_area);
207 
208 // Post encode update of the rate control parameters for 2-pass
209 void vp9_twopass_postencode_update(struct VP9_COMP *cpi);
210 
211 void calculate_coded_size(struct VP9_COMP *cpi, int *scaled_frame_width,
212                           int *scaled_frame_height);
213 
214 struct VP9EncoderConfig;
215 int vp9_get_frames_to_next_key(const struct VP9EncoderConfig *oxcf,
216                                const TWO_PASS *const twopass, int kf_show_idx,
217                                int min_gf_interval);
218 #if CONFIG_RATE_CTRL
219 /* Call this function to get info about the next group of pictures.
220  * This function should be called after vp9_create_compressor() when encoding
221  * starts or after vp9_get_compressed_data() when the encoding process of
222  * the last group of pictures is just finished.
223  */
224 void vp9_get_next_group_of_picture(const struct VP9_COMP *cpi,
225                                    int *first_is_key_frame, int *use_alt_ref,
226                                    int *coding_frame_count, int *first_show_idx,
227                                    int *last_gop_use_alt_ref);
228 
229 /*!\brief Call this function before coding a new group of pictures to get
230  * information about it.
231  * \param[in] oxcf                 Encoder config
232  * \param[in] twopass              Twopass info
233  * \param[in] frame_info           Frame info
234  * \param[in] rc                   Rate control state
235  * \param[in] show_idx             Show index of the first frame in the group
236  * \param[in] multi_layer_arf      Is multi-layer alternate reference used
237  * \param[in] allow_alt_ref        Is alternate reference allowed
238  * \param[in] first_is_key_frame   Is the first frame in the group a key frame
239  * \param[in] last_gop_use_alt_ref Does the last group use alternate reference
240  *
241  * \param[out] use_alt_ref         Does this group use alternate reference
242  *
243  * \return Returns coding frame count
244  */
245 int vp9_get_gop_coding_frame_count(const struct VP9EncoderConfig *oxcf,
246                                    const TWO_PASS *const twopass,
247                                    const FRAME_INFO *frame_info,
248                                    const RATE_CONTROL *rc, int show_idx,
249                                    int multi_layer_arf, int allow_alt_ref,
250                                    int first_is_key_frame,
251                                    int last_gop_use_alt_ref, int *use_alt_ref);
252 
253 int vp9_get_coding_frame_num(const struct VP9EncoderConfig *oxcf,
254                              const TWO_PASS *const twopass,
255                              const FRAME_INFO *frame_info, int multi_layer_arf,
256                              int allow_alt_ref);
257 
258 /*!\brief Compute a key frame binary map indicates whether key frames appear
259  * in the corresponding positions. The passed in key_frame_map must point to an
260  * integer array with length equal to twopass->first_pass_info.num_frames,
261  * which is the number of show frames in the video.
262  */
263 void vp9_get_key_frame_map(const struct VP9EncoderConfig *oxcf,
264                            const TWO_PASS *const twopass, int *key_frame_map);
265 #endif  // CONFIG_RATE_CTRL
266 
267 FIRSTPASS_STATS vp9_get_frame_stats(const TWO_PASS *twopass);
268 FIRSTPASS_STATS vp9_get_total_stats(const TWO_PASS *twopass);
269 
270 #ifdef __cplusplus
271 }  // extern "C"
272 #endif
273 
274 #endif  // VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
275