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 VP9_ENCODER_VP9_ENCODER_H_
     12 #define VP9_ENCODER_VP9_ENCODER_H_
     13 
     14 #include <stdio.h>
     15 
     16 #include "./vpx_config.h"
     17 #include "vpx/internal/vpx_codec_internal.h"
     18 #include "vpx/vp8cx.h"
     19 #if CONFIG_INTERNAL_STATS
     20 #include "vpx_dsp/ssim.h"
     21 #endif
     22 #include "vpx_dsp/variance.h"
     23 #include "vpx_ports/system_state.h"
     24 #include "vpx_util/vpx_thread.h"
     25 
     26 #include "vp9/common/vp9_alloccommon.h"
     27 #include "vp9/common/vp9_ppflags.h"
     28 #include "vp9/common/vp9_entropymode.h"
     29 #include "vp9/common/vp9_thread_common.h"
     30 #include "vp9/common/vp9_onyxc_int.h"
     31 
     32 #include "vp9/encoder/vp9_alt_ref_aq.h"
     33 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
     34 #include "vp9/encoder/vp9_context_tree.h"
     35 #include "vp9/encoder/vp9_encodemb.h"
     36 #include "vp9/encoder/vp9_ethread.h"
     37 #include "vp9/encoder/vp9_firstpass.h"
     38 #include "vp9/encoder/vp9_job_queue.h"
     39 #include "vp9/encoder/vp9_lookahead.h"
     40 #include "vp9/encoder/vp9_mbgraph.h"
     41 #include "vp9/encoder/vp9_mcomp.h"
     42 #include "vp9/encoder/vp9_noise_estimate.h"
     43 #include "vp9/encoder/vp9_quantize.h"
     44 #include "vp9/encoder/vp9_ratectrl.h"
     45 #include "vp9/encoder/vp9_rd.h"
     46 #include "vp9/encoder/vp9_speed_features.h"
     47 #include "vp9/encoder/vp9_svc_layercontext.h"
     48 #include "vp9/encoder/vp9_tokenize.h"
     49 
     50 #if CONFIG_VP9_TEMPORAL_DENOISING
     51 #include "vp9/encoder/vp9_denoiser.h"
     52 #endif
     53 
     54 #ifdef __cplusplus
     55 extern "C" {
     56 #endif
     57 
     58 // vp9 uses 10,000,000 ticks/second as time stamp
     59 #define TICKS_PER_SEC 10000000
     60 
     61 typedef struct {
     62   int nmvjointcost[MV_JOINTS];
     63   int nmvcosts[2][MV_VALS];
     64   int nmvcosts_hp[2][MV_VALS];
     65 
     66   vpx_prob segment_pred_probs[PREDICTION_PROBS];
     67 
     68   unsigned char *last_frame_seg_map_copy;
     69 
     70   // 0 = Intra, Last, GF, ARF
     71   signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
     72   // 0 = ZERO_MV, MV
     73   signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
     74 
     75   FRAME_CONTEXT fc;
     76 } CODING_CONTEXT;
     77 
     78 typedef enum {
     79   // encode_breakout is disabled.
     80   ENCODE_BREAKOUT_DISABLED = 0,
     81   // encode_breakout is enabled.
     82   ENCODE_BREAKOUT_ENABLED = 1,
     83   // encode_breakout is enabled with small max_thresh limit.
     84   ENCODE_BREAKOUT_LIMITED = 2
     85 } ENCODE_BREAKOUT_TYPE;
     86 
     87 typedef enum {
     88   NORMAL = 0,
     89   FOURFIVE = 1,
     90   THREEFIVE = 2,
     91   ONETWO = 3
     92 } VPX_SCALING;
     93 
     94 typedef enum {
     95   // Good Quality Fast Encoding. The encoder balances quality with the amount of
     96   // time it takes to encode the output. Speed setting controls how fast.
     97   GOOD,
     98 
     99   // The encoder places priority on the quality of the output over encoding
    100   // speed. The output is compressed at the highest possible quality. This
    101   // option takes the longest amount of time to encode. Speed setting ignored.
    102   BEST,
    103 
    104   // Realtime/Live Encoding. This mode is optimized for realtime encoding (for
    105   // example, capturing a television signal or feed from a live camera). Speed
    106   // setting controls how fast.
    107   REALTIME
    108 } MODE;
    109 
    110 typedef enum {
    111   FRAMEFLAGS_KEY = 1 << 0,
    112   FRAMEFLAGS_GOLDEN = 1 << 1,
    113   FRAMEFLAGS_ALTREF = 1 << 2,
    114 } FRAMETYPE_FLAGS;
    115 
    116 typedef enum {
    117   NO_AQ = 0,
    118   VARIANCE_AQ = 1,
    119   COMPLEXITY_AQ = 2,
    120   CYCLIC_REFRESH_AQ = 3,
    121   EQUATOR360_AQ = 4,
    122   // AQ based on lookahead temporal
    123   // variance (only valid for altref frames)
    124   LOOKAHEAD_AQ = 5,
    125   AQ_MODE_COUNT  // This should always be the last member of the enum
    126 } AQ_MODE;
    127 
    128 typedef enum {
    129   RESIZE_NONE = 0,    // No frame resizing allowed (except for SVC).
    130   RESIZE_FIXED = 1,   // All frames are coded at the specified dimension.
    131   RESIZE_DYNAMIC = 2  // Coded size of each frame is determined by the codec.
    132 } RESIZE_TYPE;
    133 
    134 typedef enum {
    135   kInvalid = 0,
    136   kLowSadLowSumdiff = 1,
    137   kLowSadHighSumdiff = 2,
    138   kHighSadLowSumdiff = 3,
    139   kHighSadHighSumdiff = 4,
    140   kLowVarHighSumdiff = 5,
    141   kVeryHighSad = 6,
    142 } CONTENT_STATE_SB;
    143 
    144 typedef struct VP9EncoderConfig {
    145   BITSTREAM_PROFILE profile;
    146   vpx_bit_depth_t bit_depth;     // Codec bit-depth.
    147   int width;                     // width of data passed to the compressor
    148   int height;                    // height of data passed to the compressor
    149   unsigned int input_bit_depth;  // Input bit depth.
    150   double init_framerate;         // set to passed in framerate
    151   int64_t target_bandwidth;      // bandwidth to be used in bits per second
    152 
    153   int noise_sensitivity;  // pre processing blur: recommendation 0
    154   int sharpness;          // sharpening output: recommendation 0:
    155   int speed;
    156   // maximum allowed bitrate for any intra frame in % of bitrate target.
    157   unsigned int rc_max_intra_bitrate_pct;
    158   // maximum allowed bitrate for any inter frame in % of bitrate target.
    159   unsigned int rc_max_inter_bitrate_pct;
    160   // percent of rate boost for golden frame in CBR mode.
    161   unsigned int gf_cbr_boost_pct;
    162 
    163   MODE mode;
    164   int pass;
    165 
    166   // Key Framing Operations
    167   int auto_key;  // autodetect cut scenes and set the keyframes
    168   int key_freq;  // maximum distance to key frame.
    169 
    170   int lag_in_frames;  // how many frames lag before we start encoding
    171 
    172   // ----------------------------------------------------------------
    173   // DATARATE CONTROL OPTIONS
    174 
    175   // vbr, cbr, constrained quality or constant quality
    176   enum vpx_rc_mode rc_mode;
    177 
    178   // buffer targeting aggressiveness
    179   int under_shoot_pct;
    180   int over_shoot_pct;
    181 
    182   // buffering parameters
    183   int64_t starting_buffer_level_ms;
    184   int64_t optimal_buffer_level_ms;
    185   int64_t maximum_buffer_size_ms;
    186 
    187   // Frame drop threshold.
    188   int drop_frames_water_mark;
    189 
    190   // controlling quality
    191   int fixed_q;
    192   int worst_allowed_q;
    193   int best_allowed_q;
    194   int cq_level;
    195   AQ_MODE aq_mode;  // Adaptive Quantization mode
    196 
    197   // Special handling of Adaptive Quantization for AltRef frames
    198   int alt_ref_aq;
    199 
    200   // Internal frame size scaling.
    201   RESIZE_TYPE resize_mode;
    202   int scaled_frame_width;
    203   int scaled_frame_height;
    204 
    205   // Enable feature to reduce the frame quantization every x frames.
    206   int frame_periodic_boost;
    207 
    208   // two pass datarate control
    209   int two_pass_vbrbias;  // two pass datarate control tweaks
    210   int two_pass_vbrmin_section;
    211   int two_pass_vbrmax_section;
    212   int vbr_corpus_complexity;  // 0 indicates corpus vbr disabled
    213   // END DATARATE CONTROL OPTIONS
    214   // ----------------------------------------------------------------
    215 
    216   // Spatial and temporal scalability.
    217   int ss_number_layers;  // Number of spatial layers.
    218   int ts_number_layers;  // Number of temporal layers.
    219   // Bitrate allocation for spatial layers.
    220   int layer_target_bitrate[VPX_MAX_LAYERS];
    221   int ss_target_bitrate[VPX_SS_MAX_LAYERS];
    222   int ss_enable_auto_arf[VPX_SS_MAX_LAYERS];
    223   // Bitrate allocation (CBR mode) and framerate factor, for temporal layers.
    224   int ts_rate_decimator[VPX_TS_MAX_LAYERS];
    225 
    226   int enable_auto_arf;
    227 
    228   int encode_breakout;  // early breakout : for video conf recommend 800
    229 
    230   /* Bitfield defining the error resiliency features to enable.
    231    * Can provide decodable frames after losses in previous
    232    * frames and decodable partitions after losses in the same frame.
    233    */
    234   unsigned int error_resilient_mode;
    235 
    236   /* Bitfield defining the parallel decoding mode where the
    237    * decoding in successive frames may be conducted in parallel
    238    * just by decoding the frame headers.
    239    */
    240   unsigned int frame_parallel_decoding_mode;
    241 
    242   int arnr_max_frames;
    243   int arnr_strength;
    244 
    245   int min_gf_interval;
    246   int max_gf_interval;
    247 
    248   int tile_columns;
    249   int tile_rows;
    250 
    251   int max_threads;
    252 
    253   unsigned int target_level;
    254 
    255   vpx_fixed_buf_t two_pass_stats_in;
    256   struct vpx_codec_pkt_list *output_pkt_list;
    257 
    258 #if CONFIG_FP_MB_STATS
    259   vpx_fixed_buf_t firstpass_mb_stats_in;
    260 #endif
    261 
    262   vp8e_tuning tuning;
    263   vp9e_tune_content content;
    264 #if CONFIG_VP9_HIGHBITDEPTH
    265   int use_highbitdepth;
    266 #endif
    267   vpx_color_space_t color_space;
    268   vpx_color_range_t color_range;
    269   int render_width;
    270   int render_height;
    271   VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
    272 
    273   int row_mt;
    274   unsigned int motion_vector_unit_test;
    275 } VP9EncoderConfig;
    276 
    277 static INLINE int is_lossless_requested(const VP9EncoderConfig *cfg) {
    278   return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
    279 }
    280 
    281 // TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
    282 typedef struct TileDataEnc {
    283   TileInfo tile_info;
    284   int thresh_freq_fact[BLOCK_SIZES][MAX_MODES];
    285   int mode_map[BLOCK_SIZES][MAX_MODES];
    286   FIRSTPASS_DATA fp_data;
    287   VP9RowMTSync row_mt_sync;
    288 
    289   // Used for adaptive_rd_thresh with row multithreading
    290   int *row_base_thresh_freq_fact;
    291 } TileDataEnc;
    292 
    293 typedef struct RowMTInfo {
    294   JobQueueHandle job_queue_hdl;
    295 #if CONFIG_MULTITHREAD
    296   pthread_mutex_t job_mutex;
    297 #endif
    298 } RowMTInfo;
    299 
    300 typedef struct {
    301   TOKENEXTRA *start;
    302   TOKENEXTRA *stop;
    303   unsigned int count;
    304 } TOKENLIST;
    305 
    306 typedef struct MultiThreadHandle {
    307   int allocated_tile_rows;
    308   int allocated_tile_cols;
    309   int allocated_vert_unit_rows;
    310 
    311   // Frame level params
    312   int num_tile_vert_sbs[MAX_NUM_TILE_ROWS];
    313 
    314   // Job Queue structure and handles
    315   JobQueue *job_queue;
    316 
    317   int jobs_per_tile_col;
    318 
    319   RowMTInfo row_mt_info[MAX_NUM_TILE_COLS];
    320   int thread_id_to_tile_id[MAX_NUM_THREADS];  // Mapping of threads to tiles
    321 } MultiThreadHandle;
    322 
    323 typedef struct RD_COUNTS {
    324   vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
    325   int64_t comp_pred_diff[REFERENCE_MODES];
    326   int64_t filter_diff[SWITCHABLE_FILTER_CONTEXTS];
    327 } RD_COUNTS;
    328 
    329 typedef struct ThreadData {
    330   MACROBLOCK mb;
    331   RD_COUNTS rd_counts;
    332   FRAME_COUNTS *counts;
    333 
    334   PICK_MODE_CONTEXT *leaf_tree;
    335   PC_TREE *pc_tree;
    336   PC_TREE *pc_root;
    337 } ThreadData;
    338 
    339 struct EncWorkerData;
    340 
    341 typedef struct ActiveMap {
    342   int enabled;
    343   int update;
    344   unsigned char *map;
    345 } ActiveMap;
    346 
    347 typedef enum { Y, U, V, ALL } STAT_TYPE;
    348 
    349 typedef struct IMAGE_STAT {
    350   double stat[ALL + 1];
    351   double worst;
    352 } ImageStat;
    353 
    354 // Kf noise filtering currently disabled by default in build.
    355 // #define ENABLE_KF_DENOISE 1
    356 
    357 #define CPB_WINDOW_SIZE 4
    358 #define FRAME_WINDOW_SIZE 128
    359 #define SAMPLE_RATE_GRACE_P 0.015
    360 #define VP9_LEVELS 14
    361 
    362 typedef enum {
    363   LEVEL_UNKNOWN = 0,
    364   LEVEL_AUTO = 1,
    365   LEVEL_1 = 10,
    366   LEVEL_1_1 = 11,
    367   LEVEL_2 = 20,
    368   LEVEL_2_1 = 21,
    369   LEVEL_3 = 30,
    370   LEVEL_3_1 = 31,
    371   LEVEL_4 = 40,
    372   LEVEL_4_1 = 41,
    373   LEVEL_5 = 50,
    374   LEVEL_5_1 = 51,
    375   LEVEL_5_2 = 52,
    376   LEVEL_6 = 60,
    377   LEVEL_6_1 = 61,
    378   LEVEL_6_2 = 62,
    379   LEVEL_MAX = 255
    380 } VP9_LEVEL;
    381 
    382 typedef struct {
    383   VP9_LEVEL level;
    384   uint64_t max_luma_sample_rate;
    385   uint32_t max_luma_picture_size;
    386   uint32_t max_luma_picture_breadth;
    387   double average_bitrate;  // in kilobits per second
    388   double max_cpb_size;     // in kilobits
    389   double compression_ratio;
    390   uint8_t max_col_tiles;
    391   uint32_t min_altref_distance;
    392   uint8_t max_ref_frame_buffers;
    393 } Vp9LevelSpec;
    394 
    395 extern const Vp9LevelSpec vp9_level_defs[VP9_LEVELS];
    396 
    397 typedef struct {
    398   int64_t ts;  // timestamp
    399   uint32_t luma_samples;
    400   uint32_t size;  // in bytes
    401 } FrameRecord;
    402 
    403 typedef struct {
    404   FrameRecord buf[FRAME_WINDOW_SIZE];
    405   uint8_t start;
    406   uint8_t len;
    407 } FrameWindowBuffer;
    408 
    409 typedef struct {
    410   uint8_t seen_first_altref;
    411   uint32_t frames_since_last_altref;
    412   uint64_t total_compressed_size;
    413   uint64_t total_uncompressed_size;
    414   double time_encoded;  // in seconds
    415   FrameWindowBuffer frame_window_buffer;
    416   int ref_refresh_map;
    417 } Vp9LevelStats;
    418 
    419 typedef struct {
    420   Vp9LevelStats level_stats;
    421   Vp9LevelSpec level_spec;
    422 } Vp9LevelInfo;
    423 
    424 typedef enum {
    425   BITRATE_TOO_LARGE = 0,
    426   LUMA_PIC_SIZE_TOO_LARGE,
    427   LUMA_PIC_BREADTH_TOO_LARGE,
    428   LUMA_SAMPLE_RATE_TOO_LARGE,
    429   CPB_TOO_LARGE,
    430   COMPRESSION_RATIO_TOO_SMALL,
    431   TOO_MANY_COLUMN_TILE,
    432   ALTREF_DIST_TOO_SMALL,
    433   TOO_MANY_REF_BUFFER,
    434   TARGET_LEVEL_FAIL_IDS
    435 } TARGET_LEVEL_FAIL_ID;
    436 
    437 typedef struct {
    438   int8_t level_index;
    439   uint8_t rc_config_updated;
    440   uint8_t fail_flag;
    441   int max_frame_size;   // in bits
    442   double max_cpb_size;  // in bits
    443 } LevelConstraint;
    444 
    445 typedef struct ARNRFilterData {
    446   YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
    447   int strength;
    448   int frame_count;
    449   int alt_ref_index;
    450   struct scale_factors sf;
    451 } ARNRFilterData;
    452 
    453 typedef struct VP9_COMP {
    454   QUANTS quants;
    455   ThreadData td;
    456   MB_MODE_INFO_EXT *mbmi_ext_base;
    457   DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);
    458   DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);
    459   VP9_COMMON common;
    460   VP9EncoderConfig oxcf;
    461   struct lookahead_ctx *lookahead;
    462   struct lookahead_entry *alt_ref_source;
    463 
    464   YV12_BUFFER_CONFIG *Source;
    465   YV12_BUFFER_CONFIG *Last_Source;  // NULL for first frame and alt_ref frames
    466   YV12_BUFFER_CONFIG *un_scaled_source;
    467   YV12_BUFFER_CONFIG scaled_source;
    468   YV12_BUFFER_CONFIG *unscaled_last_source;
    469   YV12_BUFFER_CONFIG scaled_last_source;
    470 #ifdef ENABLE_KF_DENOISE
    471   YV12_BUFFER_CONFIG raw_unscaled_source;
    472   YV12_BUFFER_CONFIG raw_scaled_source;
    473 #endif
    474   YV12_BUFFER_CONFIG *raw_source_frame;
    475 
    476   TileDataEnc *tile_data;
    477   int allocated_tiles;  // Keep track of memory allocated for tiles.
    478 
    479   // For a still frame, this flag is set to 1 to skip partition search.
    480   int partition_search_skippable_frame;
    481 
    482   int scaled_ref_idx[MAX_REF_FRAMES];
    483   int lst_fb_idx;
    484   int gld_fb_idx;
    485   int alt_fb_idx;
    486 
    487   int refresh_last_frame;
    488   int refresh_golden_frame;
    489   int refresh_alt_ref_frame;
    490 
    491   int ext_refresh_frame_flags_pending;
    492   int ext_refresh_last_frame;
    493   int ext_refresh_golden_frame;
    494   int ext_refresh_alt_ref_frame;
    495 
    496   int ext_refresh_frame_context_pending;
    497   int ext_refresh_frame_context;
    498 
    499   YV12_BUFFER_CONFIG last_frame_uf;
    500 
    501   TOKENEXTRA *tile_tok[4][1 << 6];
    502   uint32_t tok_count[4][1 << 6];
    503   TOKENLIST *tplist[4][1 << 6];
    504 
    505   // Ambient reconstruction err target for force key frames
    506   int64_t ambient_err;
    507 
    508   RD_OPT rd;
    509 
    510   CODING_CONTEXT coding_context;
    511 
    512   int *nmvcosts[2];
    513   int *nmvcosts_hp[2];
    514   int *nmvsadcosts[2];
    515   int *nmvsadcosts_hp[2];
    516 
    517   int64_t last_time_stamp_seen;
    518   int64_t last_end_time_stamp_seen;
    519   int64_t first_time_stamp_ever;
    520 
    521   RATE_CONTROL rc;
    522   double framerate;
    523 
    524   int interp_filter_selected[MAX_REF_FRAMES][SWITCHABLE];
    525 
    526   struct vpx_codec_pkt_list *output_pkt_list;
    527 
    528   MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
    529   int mbgraph_n_frames;  // number of frames filled in the above
    530   int static_mb_pct;     // % forced skip mbs by segmentation
    531   int ref_frame_flags;
    532 
    533   SPEED_FEATURES sf;
    534 
    535   uint32_t max_mv_magnitude;
    536   int mv_step_param;
    537 
    538   int allow_comp_inter_inter;
    539 
    540   // Default value is 1. From first pass stats, encode_breakout may be disabled.
    541   ENCODE_BREAKOUT_TYPE allow_encode_breakout;
    542 
    543   // Get threshold from external input. A suggested threshold is 800 for HD
    544   // clips, and 300 for < HD clips.
    545   int encode_breakout;
    546 
    547   uint8_t *segmentation_map;
    548 
    549   uint8_t *skin_map;
    550 
    551   // segment threashold for encode breakout
    552   int segment_encode_breakout[MAX_SEGMENTS];
    553 
    554   CYCLIC_REFRESH *cyclic_refresh;
    555   ActiveMap active_map;
    556 
    557   fractional_mv_step_fp *find_fractional_mv_step;
    558   vp9_diamond_search_fn_t diamond_search_sad;
    559   vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
    560   uint64_t time_receive_data;
    561   uint64_t time_compress_data;
    562   uint64_t time_pick_lpf;
    563   uint64_t time_encode_sb_row;
    564 
    565 #if CONFIG_FP_MB_STATS
    566   int use_fp_mb_stats;
    567 #endif
    568 
    569   TWO_PASS twopass;
    570 
    571   // Force recalculation of segment_ids for each mode info
    572   uint8_t force_update_segmentation;
    573 
    574   YV12_BUFFER_CONFIG alt_ref_buffer;
    575 
    576   // class responsible for adaptive
    577   // quantization of altref frames
    578   struct ALT_REF_AQ *alt_ref_aq;
    579 
    580 #if CONFIG_INTERNAL_STATS
    581   unsigned int mode_chosen_counts[MAX_MODES];
    582 
    583   int count;
    584   uint64_t total_sq_error;
    585   uint64_t total_samples;
    586   ImageStat psnr;
    587 
    588   uint64_t totalp_sq_error;
    589   uint64_t totalp_samples;
    590   ImageStat psnrp;
    591 
    592   double total_blockiness;
    593   double worst_blockiness;
    594 
    595   int bytes;
    596   double summed_quality;
    597   double summed_weights;
    598   double summedp_quality;
    599   double summedp_weights;
    600   unsigned int tot_recode_hits;
    601   double worst_ssim;
    602 
    603   ImageStat ssimg;
    604   ImageStat fastssim;
    605   ImageStat psnrhvs;
    606 
    607   int b_calculate_ssimg;
    608   int b_calculate_blockiness;
    609 
    610   int b_calculate_consistency;
    611 
    612   double total_inconsistency;
    613   double worst_consistency;
    614   Ssimv *ssim_vars;
    615   Metrics metrics;
    616 #endif
    617   int b_calculate_psnr;
    618 
    619   int droppable;
    620 
    621   int initial_width;
    622   int initial_height;
    623   int initial_mbs;  // Number of MBs in the full-size frame; to be used to
    624                     // normalize the firstpass stats. This will differ from the
    625                     // number of MBs in the current frame when the frame is
    626                     // scaled.
    627 
    628   int use_svc;
    629 
    630   SVC svc;
    631 
    632   // Store frame variance info in SOURCE_VAR_BASED_PARTITION search type.
    633   diff *source_diff_var;
    634   // The threshold used in SOURCE_VAR_BASED_PARTITION search type.
    635   unsigned int source_var_thresh;
    636   int frames_till_next_var_check;
    637 
    638   int frame_flags;
    639 
    640   search_site_config ss_cfg;
    641 
    642   int mbmode_cost[INTRA_MODES];
    643   unsigned int inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
    644   int intra_uv_mode_cost[FRAME_TYPES][INTRA_MODES][INTRA_MODES];
    645   int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
    646   int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
    647   int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
    648 
    649   int multi_arf_allowed;
    650   int multi_arf_enabled;
    651   int multi_arf_last_grp_enabled;
    652 
    653 #if CONFIG_VP9_TEMPORAL_DENOISING
    654   VP9_DENOISER denoiser;
    655 #endif
    656 
    657   int resize_pending;
    658   RESIZE_STATE resize_state;
    659   int external_resize;
    660   int resize_scale_num;
    661   int resize_scale_den;
    662   int resize_avg_qp;
    663   int resize_buffer_underflow;
    664   int resize_count;
    665 
    666   int use_skin_detection;
    667 
    668   int target_level;
    669 
    670   NOISE_ESTIMATE noise_estimate;
    671 
    672   // Count on how many consecutive times a block uses small/zeromv for encoding.
    673   uint8_t *consec_zero_mv;
    674 
    675   // VAR_BASED_PARTITION thresholds
    676   // 0 - threshold_64x64; 1 - threshold_32x32;
    677   // 2 - threshold_16x16; 3 - vbp_threshold_8x8;
    678   int64_t vbp_thresholds[4];
    679   int64_t vbp_threshold_minmax;
    680   int64_t vbp_threshold_sad;
    681   // Threshold used for partition copy
    682   int64_t vbp_threshold_copy;
    683   BLOCK_SIZE vbp_bsize_min;
    684 
    685   // Multi-threading
    686   int num_workers;
    687   VPxWorker *workers;
    688   struct EncWorkerData *tile_thr_data;
    689   VP9LfSync lf_row_sync;
    690   struct VP9BitstreamWorkerData *vp9_bitstream_worker_data;
    691 
    692   int keep_level_stats;
    693   Vp9LevelInfo level_info;
    694   MultiThreadHandle multi_thread_ctxt;
    695   void (*row_mt_sync_read_ptr)(VP9RowMTSync *const, int, int);
    696   void (*row_mt_sync_write_ptr)(VP9RowMTSync *const, int, int, const int);
    697   ARNRFilterData arnr_filter_data;
    698 
    699   int row_mt;
    700   unsigned int row_mt_bit_exact;
    701 
    702   // Previous Partition Info
    703   BLOCK_SIZE *prev_partition;
    704   int8_t *prev_segment_id;
    705   // Used to save the status of whether a block has a low variance in
    706   // choose_partitioning. 0 for 64x64, 1~2 for 64x32, 3~4 for 32x64, 5~8 for
    707   // 32x32, 9~24 for 16x16.
    708   // This is for the last frame and is copied to the current frame
    709   // when partition copy happens.
    710   uint8_t *prev_variance_low;
    711   uint8_t *copied_frame_cnt;
    712   uint8_t max_copied_frame;
    713   // If the last frame is dropped, we don't copy partition.
    714   uint8_t last_frame_dropped;
    715 
    716   // For each superblock: keeps track of the last time (in frame distance) the
    717   // the superblock did not have low source sad.
    718   uint8_t *content_state_sb_fd;
    719 
    720   int compute_source_sad_onepass;
    721 
    722   LevelConstraint level_constraint;
    723 
    724   uint8_t *count_arf_frame_usage;
    725   uint8_t *count_lastgolden_frame_usage;
    726 } VP9_COMP;
    727 
    728 void vp9_initialize_enc(void);
    729 
    730 struct VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
    731                                        BufferPool *const pool);
    732 void vp9_remove_compressor(VP9_COMP *cpi);
    733 
    734 void vp9_change_config(VP9_COMP *cpi, const VP9EncoderConfig *oxcf);
    735 
    736 // receive a frames worth of data. caller can assume that a copy of this
    737 // frame is made and not just a copy of the pointer..
    738 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
    739                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
    740                           int64_t end_time_stamp);
    741 
    742 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
    743                             size_t *size, uint8_t *dest, int64_t *time_stamp,
    744                             int64_t *time_end, int flush);
    745 
    746 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
    747                               vp9_ppflags_t *flags);
    748 
    749 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags);
    750 
    751 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags);
    752 
    753 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
    754                            YV12_BUFFER_CONFIG *sd);
    755 
    756 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
    757                           YV12_BUFFER_CONFIG *sd);
    758 
    759 int vp9_update_entropy(VP9_COMP *cpi, int update);
    760 
    761 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
    762 
    763 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
    764 
    765 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
    766                           VPX_SCALING vert_mode);
    767 
    768 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
    769                          unsigned int height);
    770 
    771 void vp9_set_svc(VP9_COMP *cpi, int use_svc);
    772 
    773 int vp9_get_quantizer(struct VP9_COMP *cpi);
    774 
    775 static INLINE int frame_is_kf_gf_arf(const VP9_COMP *cpi) {
    776   return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
    777          (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
    778 }
    779 
    780 static INLINE int get_ref_frame_map_idx(const VP9_COMP *cpi,
    781                                         MV_REFERENCE_FRAME ref_frame) {
    782   if (ref_frame == LAST_FRAME) {
    783     return cpi->lst_fb_idx;
    784   } else if (ref_frame == GOLDEN_FRAME) {
    785     return cpi->gld_fb_idx;
    786   } else {
    787     return cpi->alt_fb_idx;
    788   }
    789 }
    790 
    791 static INLINE int get_ref_frame_buf_idx(const VP9_COMP *const cpi,
    792                                         int ref_frame) {
    793   const VP9_COMMON *const cm = &cpi->common;
    794   const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
    795   return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
    796 }
    797 
    798 static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
    799     VP9_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
    800   VP9_COMMON *const cm = &cpi->common;
    801   const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
    802   return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
    803                                 : NULL;
    804 }
    805 
    806 static INLINE int get_token_alloc(int mb_rows, int mb_cols) {
    807   // TODO(JBB): double check we can't exceed this token count if we have a
    808   // 32x32 transform crossing a boundary at a multiple of 16.
    809   // mb_rows, cols are in units of 16 pixels. We assume 3 planes all at full
    810   // resolution. We assume up to 1 token per pixel, and then allow
    811   // a head room of 4.
    812   return mb_rows * mb_cols * (16 * 16 * 3 + 4);
    813 }
    814 
    815 // Get the allocated token size for a tile. It does the same calculation as in
    816 // the frame token allocation.
    817 static INLINE int allocated_tokens(TileInfo tile) {
    818   int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 1) >> 1;
    819   int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 1) >> 1;
    820 
    821   return get_token_alloc(tile_mb_rows, tile_mb_cols);
    822 }
    823 
    824 static INLINE void get_start_tok(VP9_COMP *cpi, int tile_row, int tile_col,
    825                                  int mi_row, TOKENEXTRA **tok) {
    826   VP9_COMMON *const cm = &cpi->common;
    827   const int tile_cols = 1 << cm->log2_tile_cols;
    828   TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
    829   const TileInfo *const tile_info = &this_tile->tile_info;
    830 
    831   int tile_mb_cols = (tile_info->mi_col_end - tile_info->mi_col_start + 1) >> 1;
    832   const int mb_row = (mi_row - tile_info->mi_row_start) >> 1;
    833 
    834   *tok =
    835       cpi->tile_tok[tile_row][tile_col] + get_token_alloc(mb_row, tile_mb_cols);
    836 }
    837 
    838 int64_t vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b);
    839 #if CONFIG_VP9_HIGHBITDEPTH
    840 int64_t vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
    841                              const YV12_BUFFER_CONFIG *b);
    842 #endif  // CONFIG_VP9_HIGHBITDEPTH
    843 
    844 void vp9_scale_references(VP9_COMP *cpi);
    845 
    846 void vp9_update_reference_frames(VP9_COMP *cpi);
    847 
    848 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv);
    849 
    850 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(
    851     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
    852     YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
    853     int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2);
    854 
    855 YV12_BUFFER_CONFIG *vp9_scale_if_required(
    856     VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
    857     int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler);
    858 
    859 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags);
    860 
    861 static INLINE int is_two_pass_svc(const struct VP9_COMP *const cpi) {
    862   return cpi->use_svc && cpi->oxcf.pass != 0;
    863 }
    864 
    865 static INLINE int is_one_pass_cbr_svc(const struct VP9_COMP *const cpi) {
    866   return (cpi->use_svc && cpi->oxcf.pass == 0);
    867 }
    868 
    869 #if CONFIG_VP9_TEMPORAL_DENOISING
    870 static INLINE int denoise_svc(const struct VP9_COMP *const cpi) {
    871   return (!cpi->use_svc ||
    872           (cpi->use_svc &&
    873            cpi->svc.spatial_layer_id >= cpi->svc.first_layer_denoise));
    874 }
    875 #endif
    876 
    877 #define MIN_LOOKAHEAD_FOR_ARFS 4
    878 static INLINE int is_altref_enabled(const VP9_COMP *const cpi) {
    879   return !(cpi->oxcf.mode == REALTIME && cpi->oxcf.rc_mode == VPX_CBR) &&
    880          cpi->oxcf.lag_in_frames >= MIN_LOOKAHEAD_FOR_ARFS &&
    881          (cpi->oxcf.enable_auto_arf &&
    882           (!is_two_pass_svc(cpi) ||
    883            cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]));
    884 }
    885 
    886 static INLINE void set_ref_ptrs(VP9_COMMON *cm, MACROBLOCKD *xd,
    887                                 MV_REFERENCE_FRAME ref0,
    888                                 MV_REFERENCE_FRAME ref1) {
    889   xd->block_refs[0] =
    890       &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
    891   xd->block_refs[1] =
    892       &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
    893 }
    894 
    895 static INLINE int get_chessboard_index(const int frame_index) {
    896   return frame_index & 0x1;
    897 }
    898 
    899 static INLINE int *cond_cost_list(const struct VP9_COMP *cpi, int *cost_list) {
    900   return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
    901 }
    902 
    903 static INLINE int get_num_vert_units(TileInfo tile, int shift) {
    904   int num_vert_units =
    905       (tile.mi_row_end - tile.mi_row_start + (1 << shift) - 1) >> shift;
    906   return num_vert_units;
    907 }
    908 
    909 static INLINE int get_num_cols(TileInfo tile, int shift) {
    910   int num_cols =
    911       (tile.mi_col_end - tile.mi_col_start + (1 << shift) - 1) >> shift;
    912   return num_cols;
    913 }
    914 
    915 static INLINE int get_level_index(VP9_LEVEL level) {
    916   int i;
    917   for (i = 0; i < VP9_LEVELS; ++i) {
    918     if (level == vp9_level_defs[i].level) return i;
    919   }
    920   return -1;
    921 }
    922 
    923 // Return the log2 value of max column tiles corresponding to the level that
    924 // the picture size fits into.
    925 static INLINE int log_tile_cols_from_picsize_level(uint32_t width,
    926                                                    uint32_t height) {
    927   int i;
    928   const uint32_t pic_size = width * height;
    929   const uint32_t pic_breadth = VPXMAX(width, height);
    930   for (i = LEVEL_1; i < LEVEL_MAX; ++i) {
    931     if (vp9_level_defs[i].max_luma_picture_size >= pic_size &&
    932         vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) {
    933       return get_msb(vp9_level_defs[i].max_col_tiles);
    934     }
    935   }
    936   return INT_MAX;
    937 }
    938 
    939 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec);
    940 
    941 void vp9_new_framerate(VP9_COMP *cpi, double framerate);
    942 
    943 void vp9_set_row_mt(VP9_COMP *cpi);
    944 
    945 #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
    946 
    947 #ifdef __cplusplus
    948 }  // extern "C"
    949 #endif
    950 
    951 #endif  // VP9_ENCODER_VP9_ENCODER_H_
    952