Home | History | Annotate | Download | only in encoder
      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/encoder/vp9_lookahead.h"
     17 #include "vp9/encoder/vp9_ratectrl.h"
     18 
     19 #ifdef __cplusplus
     20 extern "C" {
     21 #endif
     22 
     23 #if CONFIG_FP_MB_STATS
     24 
     25 #define FPMB_DCINTRA_MASK 0x01
     26 
     27 #define FPMB_MOTION_ZERO_MASK 0x02
     28 #define FPMB_MOTION_LEFT_MASK 0x04
     29 #define FPMB_MOTION_RIGHT_MASK 0x08
     30 #define FPMB_MOTION_UP_MASK 0x10
     31 #define FPMB_MOTION_DOWN_MASK 0x20
     32 
     33 #define FPMB_ERROR_SMALL_MASK 0x40
     34 #define FPMB_ERROR_LARGE_MASK 0x80
     35 #define FPMB_ERROR_SMALL_TH 2000
     36 #define FPMB_ERROR_LARGE_TH 48000
     37 
     38 typedef struct {
     39   uint8_t *mb_stats_start;
     40   uint8_t *mb_stats_end;
     41 } FIRSTPASS_MB_STATS;
     42 #endif
     43 
     44 #define INVALID_ROW -1
     45 
     46 #define MAX_ARF_LAYERS 6
     47 
     48 typedef struct {
     49   double frame_mb_intra_factor;
     50   double frame_mb_brightness_factor;
     51   double frame_mb_neutral_count;
     52 } FP_MB_FLOAT_STATS;
     53 
     54 typedef struct {
     55   double intra_factor;
     56   double brightness_factor;
     57   int64_t coded_error;
     58   int64_t sr_coded_error;
     59   int64_t frame_noise_energy;
     60   int64_t intra_error;
     61   int intercount;
     62   int second_ref_count;
     63   double neutral_count;
     64   double intra_count_low;   // Coded intra but low variance
     65   double intra_count_high;  // Coded intra high variance
     66   int intra_skip_count;
     67   int image_data_start_row;
     68   int mvcount;
     69   int sum_mvr;
     70   int sum_mvr_abs;
     71   int sum_mvc;
     72   int sum_mvc_abs;
     73   int64_t sum_mvrs;
     74   int64_t sum_mvcs;
     75   int sum_in_vectors;
     76   int intra_smooth_count;
     77 } FIRSTPASS_DATA;
     78 
     79 typedef struct {
     80   double frame;
     81   double weight;
     82   double intra_error;
     83   double coded_error;
     84   double sr_coded_error;
     85   double frame_noise_energy;
     86   double pcnt_inter;
     87   double pcnt_motion;
     88   double pcnt_second_ref;
     89   double pcnt_neutral;
     90   double pcnt_intra_low;   // Coded intra but low variance
     91   double pcnt_intra_high;  // Coded intra high variance
     92   double intra_skip_pct;
     93   double intra_smooth_pct;    // % of blocks that are smooth
     94   double inactive_zone_rows;  // Image mask rows top and bottom.
     95   double inactive_zone_cols;  // Image mask columns at left and right edges.
     96   double MVr;
     97   double mvr_abs;
     98   double MVc;
     99   double mvc_abs;
    100   double MVrv;
    101   double MVcv;
    102   double mv_in_out_count;
    103   double duration;
    104   double count;
    105   int64_t spatial_layer_id;
    106 } FIRSTPASS_STATS;
    107 
    108 typedef enum {
    109   KF_UPDATE = 0,
    110   LF_UPDATE = 1,
    111   GF_UPDATE = 2,
    112   ARF_UPDATE = 3,
    113   OVERLAY_UPDATE = 4,
    114   MID_OVERLAY_UPDATE = 5,
    115   USE_BUF_FRAME = 6,  // Use show existing frame, no ref buffer update
    116   FRAME_UPDATE_TYPES = 7
    117 } FRAME_UPDATE_TYPE;
    118 
    119 #define FC_ANIMATION_THRESH 0.15
    120 typedef enum {
    121   FC_NORMAL = 0,
    122   FC_GRAPHICS_ANIMATION = 1,
    123   FRAME_CONTENT_TYPES = 2
    124 } FRAME_CONTENT_TYPE;
    125 
    126 typedef struct {
    127   unsigned char index;
    128   RATE_FACTOR_LEVEL rf_level[MAX_STATIC_GF_GROUP_LENGTH + 2];
    129   FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH + 2];
    130   unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH + 2];
    131   unsigned char layer_depth[MAX_STATIC_GF_GROUP_LENGTH + 2];
    132   unsigned char frame_gop_index[MAX_STATIC_GF_GROUP_LENGTH + 2];
    133   int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH + 2];
    134   int gfu_boost[MAX_STATIC_GF_GROUP_LENGTH + 2];
    135 
    136   int frame_start;
    137   int frame_end;
    138   // TODO(jingning): The array size of arf_stack could be reduced.
    139   int arf_index_stack[MAX_LAG_BUFFERS * 2];
    140   int top_arf_idx;
    141   int stack_size;
    142   int gf_group_size;
    143   int max_layer_depth;
    144   int allowed_max_layer_depth;
    145 } GF_GROUP;
    146 
    147 typedef struct {
    148   unsigned int section_intra_rating;
    149   FIRSTPASS_STATS total_stats;
    150   FIRSTPASS_STATS this_frame_stats;
    151   const FIRSTPASS_STATS *stats_in;
    152   const FIRSTPASS_STATS *stats_in_start;
    153   const FIRSTPASS_STATS *stats_in_end;
    154   FIRSTPASS_STATS total_left_stats;
    155   int first_pass_done;
    156   int64_t bits_left;
    157   double mean_mod_score;
    158   double normalized_score_left;
    159   double mb_av_energy;
    160   double mb_smooth_pct;
    161 
    162 #if CONFIG_FP_MB_STATS
    163   uint8_t *frame_mb_stats_buf;
    164   uint8_t *this_frame_mb_stats;
    165   FIRSTPASS_MB_STATS firstpass_mb_stats;
    166 #endif
    167 
    168   FP_MB_FLOAT_STATS *fp_mb_float_stats;
    169 
    170   // An indication of the content type of the current frame
    171   FRAME_CONTENT_TYPE fr_content_type;
    172 
    173   // Projected total bits available for a key frame group of frames
    174   int64_t kf_group_bits;
    175 
    176   // Error score of frames still to be coded in kf group
    177   double kf_group_error_left;
    178 
    179   double bpm_factor;
    180   int rolling_arf_group_target_bits;
    181   int rolling_arf_group_actual_bits;
    182 
    183   int sr_update_lag;
    184   int kf_zeromotion_pct;
    185   int last_kfgroup_zeromotion_pct;
    186   int active_worst_quality;
    187   int baseline_active_worst_quality;
    188   int extend_minq;
    189   int extend_maxq;
    190   int extend_minq_fast;
    191   int arnr_strength_adjustment;
    192 
    193   GF_GROUP gf_group;
    194 } TWO_PASS;
    195 
    196 struct VP9_COMP;
    197 struct ThreadData;
    198 struct TileDataEnc;
    199 
    200 void vp9_init_first_pass(struct VP9_COMP *cpi);
    201 void vp9_first_pass(struct VP9_COMP *cpi, const struct lookahead_entry *source);
    202 void vp9_end_first_pass(struct VP9_COMP *cpi);
    203 
    204 void vp9_first_pass_encode_tile_mb_row(struct VP9_COMP *cpi,
    205                                        struct ThreadData *td,
    206                                        FIRSTPASS_DATA *fp_acc_data,
    207                                        struct TileDataEnc *tile_data,
    208                                        MV *best_ref_mv, int mb_row);
    209 
    210 void vp9_init_second_pass(struct VP9_COMP *cpi);
    211 void vp9_rc_get_second_pass_params(struct VP9_COMP *cpi);
    212 
    213 // Post encode update of the rate control parameters for 2-pass
    214 void vp9_twopass_postencode_update(struct VP9_COMP *cpi);
    215 
    216 void calculate_coded_size(struct VP9_COMP *cpi, int *scaled_frame_width,
    217                           int *scaled_frame_height);
    218 
    219 #ifdef __cplusplus
    220 }  // extern "C"
    221 #endif
    222 
    223 #endif  // VPX_VP9_ENCODER_VP9_FIRSTPASS_H_
    224