Home | History | Annotate | Download | only in encoder
      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 #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   // Percentage of blocks with inter pred error < intra pred error.
     50   double pcnt_inter;
     51   // Percentage of blocks using (inter prediction and) non-zero motion vectors.
     52   double pcnt_motion;
     53   // Percentage of blocks where golden frame was the best reference. That is:
     54   // inter pred error using golden frame < inter pred error using last frame and
     55   // inter pred error using golden frame < intra pred error
     56   double pcnt_second_ref;
     57   // Percentage of blocks where intra and inter prediction errors were very
     58   // close. Note that this is a 'weighted count', that is, the so blocks may be
     59   // weighted by how close the two errors were.
     60   double pcnt_neutral;
     61   // Percentage of blocks that have almost no intra error residual
     62   // (i.e. are in effect completely flat and untextured in the intra
     63   // domain). In natural videos this is uncommon, but it is much more
     64   // common in animations, graphics and screen content, so may be used
     65   // as a signal to detect these types of content.
     66   double intra_skip_pct;
     67   // Image mask rows top and bottom.
     68   double inactive_zone_rows;
     69   // Image mask columns at left and right edges.
     70   double inactive_zone_cols;
     71   // Average of row motion vectors.
     72   double MVr;
     73   // Mean of absolute value of row motion vectors.
     74   double mvr_abs;
     75   // Mean of column motion vectors.
     76   double MVc;
     77   // Mean of absolute value of column motion vectors.
     78   double mvc_abs;
     79   // Variance of row motion vectors.
     80   double MVrv;
     81   // Variance of column motion vectors.
     82   double MVcv;
     83   // Value in range [-1,1] indicating fraction of row and column motion vectors
     84   // that point inwards (negative MV value) or outwards (positive MV value).
     85   // For example, value of 1 indicates, all row/column MVs are inwards.
     86   double mv_in_out_count;
     87   // Count of unique non-zero motion vectors.
     88   double new_mv_count;
     89   // Duration of the frame / collection of frames.
     90   double duration;
     91   // 1.0 if stats are for a single frame, OR
     92   // Number of frames in this collection for which the stats are accumulated.
     93   double count;
     94   // standard deviation for (0, 0) motion prediction error
     95   double raw_error_stdev;
     96 } FIRSTPASS_STATS;
     97 
     98 enum {
     99   KF_UPDATE,
    100   LF_UPDATE,
    101   GF_UPDATE,
    102   ARF_UPDATE,
    103   OVERLAY_UPDATE,
    104   INTNL_OVERLAY_UPDATE,  // Internal Overlay Frame
    105   INTNL_ARF_UPDATE,      // Internal Altref Frame
    106   FRAME_UPDATE_TYPES
    107 } UENUM1BYTE(FRAME_UPDATE_TYPE);
    108 
    109 #define FC_ANIMATION_THRESH 0.15
    110 enum {
    111   FC_NORMAL = 0,
    112   FC_GRAPHICS_ANIMATION = 1,
    113   FRAME_CONTENT_TYPES = 2
    114 } UENUM1BYTE(FRAME_CONTENT_TYPE);
    115 
    116 typedef struct {
    117   unsigned char index;
    118   FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH + 1];
    119   unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH + 1];
    120   unsigned char arf_update_idx[MAX_STATIC_GF_GROUP_LENGTH + 1];
    121   unsigned char arf_pos_in_gf[MAX_STATIC_GF_GROUP_LENGTH + 1];
    122   unsigned char pyramid_level[MAX_STATIC_GF_GROUP_LENGTH + 1];
    123   unsigned char pyramid_height;
    124   unsigned char pyramid_lvl_nodes[MAX_PYRAMID_LVL];
    125   int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH + 1];
    126   int size;
    127 } GF_GROUP;
    128 
    129 typedef struct {
    130   unsigned int section_intra_rating;
    131   FIRSTPASS_STATS total_stats;
    132   FIRSTPASS_STATS this_frame_stats;
    133   const FIRSTPASS_STATS *stats_in;
    134   const FIRSTPASS_STATS *stats_in_start;
    135   const FIRSTPASS_STATS *stats_in_end;
    136   FIRSTPASS_STATS total_left_stats;
    137   int first_pass_done;
    138   int64_t bits_left;
    139   double modified_error_min;
    140   double modified_error_max;
    141   double modified_error_left;
    142   double mb_av_energy;
    143   double frame_avg_haar_energy;
    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   int64_t kf_group_error_left;
    153 
    154   // The fraction for a kf groups total bits allocated to the inter frames
    155   double kfgroup_inter_fraction;
    156 
    157   int sr_update_lag;
    158 
    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 
    167   GF_GROUP gf_group;
    168 } TWO_PASS;
    169 
    170 struct AV1_COMP;
    171 struct EncodeFrameParams;
    172 struct AV1EncoderConfig;
    173 
    174 void av1_init_first_pass(struct AV1_COMP *cpi);
    175 void av1_rc_get_first_pass_params(struct AV1_COMP *cpi);
    176 void av1_first_pass(struct AV1_COMP *cpi, const int64_t ts_duration);
    177 void av1_end_first_pass(struct AV1_COMP *cpi);
    178 
    179 void av1_twopass_zero_stats(FIRSTPASS_STATS *section);
    180 
    181 #ifdef __cplusplus
    182 }  // extern "C"
    183 #endif
    184 
    185 #endif  // AOM_AV1_ENCODER_FIRSTPASS_H_
    186