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 #include <math.h>
     12 #include <stdio.h>
     13 #include <limits.h>
     14 
     15 #include "./vpx_config.h"
     16 #include "./vpx_scale_rtcd.h"
     17 
     18 #include "vp9/common/vp9_alloccommon.h"
     19 #include "vp9/common/vp9_filter.h"
     20 #include "vp9/common/vp9_idct.h"
     21 #if CONFIG_VP9_POSTPROC
     22 #include "vp9/common/vp9_postproc.h"
     23 #endif
     24 #include "vp9/common/vp9_reconinter.h"
     25 #include "vp9/common/vp9_systemdependent.h"
     26 #include "vp9/common/vp9_tile_common.h"
     27 #include "vp9/encoder/vp9_firstpass.h"
     28 #include "vp9/encoder/vp9_mbgraph.h"
     29 #include "vp9/encoder/vp9_onyx_int.h"
     30 #include "vp9/encoder/vp9_picklpf.h"
     31 #include "vp9/encoder/vp9_psnr.h"
     32 #include "vp9/encoder/vp9_ratectrl.h"
     33 #include "vp9/encoder/vp9_rdopt.h"
     34 #include "vp9/encoder/vp9_segmentation.h"
     35 #include "vp9/encoder/vp9_temporal_filter.h"
     36 #include "vp9/encoder/vp9_vaq.h"
     37 
     38 #include "vpx_ports/vpx_timer.h"
     39 
     40 
     41 extern void print_tree_update_probs();
     42 
     43 static void set_default_lf_deltas(struct loopfilter *lf);
     44 
     45 #define DEFAULT_INTERP_FILTER SWITCHABLE
     46 
     47 #define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */
     48 
     49 #define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
     50                                          //  for altref computation.
     51 #define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
     52                                          // mv. Choose a very high value for
     53                                          // now so that HIGH_PRECISION is always
     54                                          // chosen.
     55 
     56 // Masks for partially or completely disabling split mode
     57 #define DISABLE_ALL_SPLIT         0x3F
     58 #define DISABLE_ALL_INTER_SPLIT   0x1F
     59 #define DISABLE_COMPOUND_SPLIT    0x18
     60 #define LAST_AND_INTRA_SPLIT_ONLY 0x1E
     61 
     62 #if CONFIG_INTERNAL_STATS
     63 extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
     64                             YV12_BUFFER_CONFIG *dest, int lumamask,
     65                             double *weight);
     66 
     67 
     68 extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
     69                              YV12_BUFFER_CONFIG *dest, double *ssim_y,
     70                              double *ssim_u, double *ssim_v);
     71 
     72 
     73 #endif
     74 
     75 // #define OUTPUT_YUV_REC
     76 
     77 #ifdef OUTPUT_YUV_SRC
     78 FILE *yuv_file;
     79 #endif
     80 #ifdef OUTPUT_YUV_REC
     81 FILE *yuv_rec_file;
     82 #endif
     83 
     84 #if 0
     85 FILE *framepsnr;
     86 FILE *kf_list;
     87 FILE *keyfile;
     88 #endif
     89 
     90 
     91 #ifdef ENTROPY_STATS
     92 extern int intra_mode_stats[INTRA_MODES]
     93                            [INTRA_MODES]
     94                            [INTRA_MODES];
     95 #endif
     96 
     97 #ifdef MODE_STATS
     98 extern void init_tx_count_stats();
     99 extern void write_tx_count_stats();
    100 extern void init_switchable_interp_stats();
    101 extern void write_switchable_interp_stats();
    102 #endif
    103 
    104 #ifdef SPEEDSTATS
    105 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    106                                     0, 0, 0};
    107 #endif
    108 
    109 #if defined(SECTIONBITS_OUTPUT)
    110 extern unsigned __int64 Sectionbits[500];
    111 #endif
    112 
    113 extern void vp9_init_quantizer(VP9_COMP *cpi);
    114 
    115 // Tables relating active max Q to active min Q
    116 static int kf_low_motion_minq[QINDEX_RANGE];
    117 static int kf_high_motion_minq[QINDEX_RANGE];
    118 static int gf_low_motion_minq[QINDEX_RANGE];
    119 static int gf_high_motion_minq[QINDEX_RANGE];
    120 static int inter_minq[QINDEX_RANGE];
    121 static int afq_low_motion_minq[QINDEX_RANGE];
    122 static int afq_high_motion_minq[QINDEX_RANGE];
    123 
    124 static INLINE void Scale2Ratio(int mode, int *hr, int *hs) {
    125   switch (mode) {
    126     case NORMAL:
    127       *hr = 1;
    128       *hs = 1;
    129       break;
    130     case FOURFIVE:
    131       *hr = 4;
    132       *hs = 5;
    133       break;
    134     case THREEFIVE:
    135       *hr = 3;
    136       *hs = 5;
    137     break;
    138     case ONETWO:
    139       *hr = 1;
    140       *hs = 2;
    141     break;
    142     default:
    143       *hr = 1;
    144       *hs = 1;
    145        assert(0);
    146       break;
    147   }
    148 }
    149 
    150 // Functions to compute the active minq lookup table entries based on a
    151 // formulaic approach to facilitate easier adjustment of the Q tables.
    152 // The formulae were derived from computing a 3rd order polynomial best
    153 // fit to the original data (after plotting real maxq vs minq (not q index))
    154 static int calculate_minq_index(double maxq,
    155                                 double x3, double x2, double x1, double c) {
    156   int i;
    157   const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
    158                                 maxq);
    159 
    160   // Special case handling to deal with the step from q2.0
    161   // down to lossless mode represented by q 1.0.
    162   if (minqtarget <= 2.0)
    163     return 0;
    164 
    165   for (i = 0; i < QINDEX_RANGE; i++) {
    166     if (minqtarget <= vp9_convert_qindex_to_q(i))
    167       return i;
    168   }
    169 
    170   return QINDEX_RANGE - 1;
    171 }
    172 
    173 static void init_minq_luts(void) {
    174   int i;
    175 
    176   for (i = 0; i < QINDEX_RANGE; i++) {
    177     const double maxq = vp9_convert_qindex_to_q(i);
    178 
    179 
    180     kf_low_motion_minq[i] = calculate_minq_index(maxq,
    181                                                  0.000001,
    182                                                  -0.0004,
    183                                                  0.15,
    184                                                  0.0);
    185     kf_high_motion_minq[i] = calculate_minq_index(maxq,
    186                                                   0.000002,
    187                                                   -0.0012,
    188                                                   0.5,
    189                                                   0.0);
    190 
    191     gf_low_motion_minq[i] = calculate_minq_index(maxq,
    192                                                  0.0000015,
    193                                                  -0.0009,
    194                                                  0.32,
    195                                                  0.0);
    196     gf_high_motion_minq[i] = calculate_minq_index(maxq,
    197                                                   0.0000021,
    198                                                   -0.00125,
    199                                                   0.50,
    200                                                   0.0);
    201     inter_minq[i] = calculate_minq_index(maxq,
    202                                          0.00000271,
    203                                          -0.00113,
    204                                          0.75,
    205                                          0.0);
    206     afq_low_motion_minq[i] = calculate_minq_index(maxq,
    207                                                   0.0000015,
    208                                                   -0.0009,
    209                                                   0.33,
    210                                                   0.0);
    211     afq_high_motion_minq[i] = calculate_minq_index(maxq,
    212                                                    0.0000021,
    213                                                    -0.00125,
    214                                                    0.55,
    215                                                    0.0);
    216   }
    217 }
    218 
    219 static int get_active_quality(int q,
    220                               int gfu_boost,
    221                               int low,
    222                               int high,
    223                               int *low_motion_minq,
    224                               int *high_motion_minq) {
    225   int active_best_quality;
    226   if (gfu_boost > high) {
    227     active_best_quality = low_motion_minq[q];
    228   } else if (gfu_boost < low) {
    229     active_best_quality = high_motion_minq[q];
    230   } else {
    231     const int gap = high - low;
    232     const int offset = high - gfu_boost;
    233     const int qdiff = high_motion_minq[q] - low_motion_minq[q];
    234     const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
    235     active_best_quality = low_motion_minq[q] + adjustment;
    236   }
    237   return active_best_quality;
    238 }
    239 
    240 static void set_mvcost(VP9_COMP *cpi) {
    241   MACROBLOCK *const mb = &cpi->mb;
    242   if (cpi->common.allow_high_precision_mv) {
    243     mb->mvcost = mb->nmvcost_hp;
    244     mb->mvsadcost = mb->nmvsadcost_hp;
    245   } else {
    246     mb->mvcost = mb->nmvcost;
    247     mb->mvsadcost = mb->nmvsadcost;
    248   }
    249 }
    250 
    251 void vp9_initialize_enc() {
    252   static int init_done = 0;
    253 
    254   if (!init_done) {
    255     vp9_initialize_common();
    256     vp9_tokenize_initialize();
    257     vp9_init_quant_tables();
    258     vp9_init_me_luts();
    259     init_minq_luts();
    260     // init_base_skip_probs();
    261     init_done = 1;
    262   }
    263 }
    264 
    265 static void setup_features(VP9_COMMON *cm) {
    266   struct loopfilter *const lf = &cm->lf;
    267   struct segmentation *const seg = &cm->seg;
    268 
    269   // Set up default state for MB feature flags
    270   seg->enabled = 0;
    271 
    272   seg->update_map = 0;
    273   seg->update_data = 0;
    274   vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs));
    275 
    276   vp9_clearall_segfeatures(seg);
    277 
    278   lf->mode_ref_delta_enabled = 0;
    279   lf->mode_ref_delta_update = 0;
    280   vp9_zero(lf->ref_deltas);
    281   vp9_zero(lf->mode_deltas);
    282   vp9_zero(lf->last_ref_deltas);
    283   vp9_zero(lf->last_mode_deltas);
    284 
    285   set_default_lf_deltas(lf);
    286 }
    287 
    288 static void dealloc_compressor_data(VP9_COMP *cpi) {
    289   // Delete sementation map
    290   vpx_free(cpi->segmentation_map);
    291   cpi->segmentation_map = 0;
    292   vpx_free(cpi->common.last_frame_seg_map);
    293   cpi->common.last_frame_seg_map = 0;
    294   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
    295   cpi->coding_context.last_frame_seg_map_copy = 0;
    296 
    297   vpx_free(cpi->active_map);
    298   cpi->active_map = 0;
    299 
    300   vp9_free_frame_buffers(&cpi->common);
    301 
    302   vp9_free_frame_buffer(&cpi->last_frame_uf);
    303   vp9_free_frame_buffer(&cpi->scaled_source);
    304   vp9_free_frame_buffer(&cpi->alt_ref_buffer);
    305   vp9_lookahead_destroy(cpi->lookahead);
    306 
    307   vpx_free(cpi->tok);
    308   cpi->tok = 0;
    309 
    310   // Activity mask based per mb zbin adjustments
    311   vpx_free(cpi->mb_activity_map);
    312   cpi->mb_activity_map = 0;
    313   vpx_free(cpi->mb_norm_activity_map);
    314   cpi->mb_norm_activity_map = 0;
    315 
    316   vpx_free(cpi->above_context[0]);
    317   cpi->above_context[0] = NULL;
    318 
    319   vpx_free(cpi->above_seg_context);
    320   cpi->above_seg_context = NULL;
    321 }
    322 
    323 // Computes a q delta (in "q index" terms) to get from a starting q value
    324 // to a target value
    325 // target q value
    326 int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
    327   int i;
    328   int start_index = cpi->worst_quality;
    329   int target_index = cpi->worst_quality;
    330 
    331   // Convert the average q value to an index.
    332   for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    333     start_index = i;
    334     if (vp9_convert_qindex_to_q(i) >= qstart)
    335       break;
    336   }
    337 
    338   // Convert the q target to an index
    339   for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
    340     target_index = i;
    341     if (vp9_convert_qindex_to_q(i) >= qtarget)
    342       break;
    343   }
    344 
    345   return target_index - start_index;
    346 }
    347 
    348 static void configure_static_seg_features(VP9_COMP *cpi) {
    349   VP9_COMMON *cm = &cpi->common;
    350   struct segmentation *seg = &cm->seg;
    351 
    352   int high_q = (int)(cpi->avg_q > 48.0);
    353   int qi_delta;
    354 
    355   // Disable and clear down for KF
    356   if (cm->frame_type == KEY_FRAME) {
    357     // Clear down the global segmentation map
    358     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    359     seg->update_map = 0;
    360     seg->update_data = 0;
    361     cpi->static_mb_pct = 0;
    362 
    363     // Disable segmentation
    364     vp9_disable_segmentation((VP9_PTR)cpi);
    365 
    366     // Clear down the segment features.
    367     vp9_clearall_segfeatures(seg);
    368   } else if (cpi->refresh_alt_ref_frame) {
    369     // If this is an alt ref frame
    370     // Clear down the global segmentation map
    371     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    372     seg->update_map = 0;
    373     seg->update_data = 0;
    374     cpi->static_mb_pct = 0;
    375 
    376     // Disable segmentation and individual segment features by default
    377     vp9_disable_segmentation((VP9_PTR)cpi);
    378     vp9_clearall_segfeatures(seg);
    379 
    380     // Scan frames from current to arf frame.
    381     // This function re-enables segmentation if appropriate.
    382     vp9_update_mbgraph_stats(cpi);
    383 
    384     // If segmentation was enabled set those features needed for the
    385     // arf itself.
    386     if (seg->enabled) {
    387       seg->update_map = 1;
    388       seg->update_data = 1;
    389 
    390       qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
    391       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
    392       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
    393 
    394       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
    395       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
    396 
    397       // Where relevant assume segment data is delta data
    398       seg->abs_delta = SEGMENT_DELTADATA;
    399     }
    400   } else if (seg->enabled) {
    401     // All other frames if segmentation has been enabled
    402 
    403     // First normal frame in a valid gf or alt ref group
    404     if (cpi->frames_since_golden == 0) {
    405       // Set up segment features for normal frames in an arf group
    406       if (cpi->source_alt_ref_active) {
    407         seg->update_map = 0;
    408         seg->update_data = 1;
    409         seg->abs_delta = SEGMENT_DELTADATA;
    410 
    411         qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q,
    412                                       (cpi->avg_q * 1.125));
    413         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
    414         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
    415 
    416         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
    417         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
    418 
    419         // Segment coding disabled for compred testing
    420         if (high_q || (cpi->static_mb_pct == 100)) {
    421           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
    422           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
    423           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
    424         }
    425       } else {
    426         // Disable segmentation and clear down features if alt ref
    427         // is not active for this group
    428 
    429         vp9_disable_segmentation((VP9_PTR)cpi);
    430 
    431         vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    432 
    433         seg->update_map = 0;
    434         seg->update_data = 0;
    435 
    436         vp9_clearall_segfeatures(seg);
    437       }
    438     } else if (cpi->is_src_frame_alt_ref) {
    439       // Special case where we are coding over the top of a previous
    440       // alt ref frame.
    441       // Segment coding disabled for compred testing
    442 
    443       // Enable ref frame features for segment 0 as well
    444       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
    445       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
    446 
    447       // All mbs should use ALTREF_FRAME
    448       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
    449       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
    450       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
    451       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
    452 
    453       // Skip all MBs if high Q (0,0 mv and skip coeffs)
    454       if (high_q) {
    455         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
    456         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
    457       }
    458       // Enable data update
    459       seg->update_data = 1;
    460     } else {
    461       // All other frames.
    462 
    463       // No updates.. leave things as they are.
    464       seg->update_map = 0;
    465       seg->update_data = 0;
    466     }
    467   }
    468 }
    469 
    470 #ifdef ENTROPY_STATS
    471 void vp9_update_mode_context_stats(VP9_COMP *cpi) {
    472   VP9_COMMON *cm = &cpi->common;
    473   int i, j;
    474   unsigned int (*inter_mode_counts)[INTER_MODES - 1][2] =
    475       cm->fc.inter_mode_counts;
    476   int64_t (*mv_ref_stats)[INTER_MODES - 1][2] = cpi->mv_ref_stats;
    477   FILE *f;
    478 
    479   // Read the past stats counters
    480   f = fopen("mode_context.bin",  "rb");
    481   if (!f) {
    482     vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats));
    483   } else {
    484     fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
    485     fclose(f);
    486   }
    487 
    488   // Add in the values for this frame
    489   for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
    490     for (j = 0; j < INTER_MODES - 1; j++) {
    491       mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0];
    492       mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1];
    493     }
    494   }
    495 
    496   // Write back the accumulated stats
    497   f = fopen("mode_context.bin",  "wb");
    498   fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
    499   fclose(f);
    500 }
    501 
    502 void print_mode_context(VP9_COMP *cpi) {
    503   FILE *f = fopen("vp9_modecont.c", "a");
    504   int i, j;
    505 
    506   fprintf(f, "#include \"vp9_entropy.h\"\n");
    507   fprintf(
    508       f,
    509       "const int inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1] =");
    510   fprintf(f, "{\n");
    511   for (j = 0; j < INTER_MODE_CONTEXTS; j++) {
    512     fprintf(f, "  {/* %d */ ", j);
    513     fprintf(f, "    ");
    514     for (i = 0; i < INTER_MODES - 1; i++) {
    515       int this_prob;
    516       int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1];
    517       if (count)
    518         this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count;
    519       else
    520         this_prob = 128;
    521 
    522       // context probs
    523       fprintf(f, "%5d, ", this_prob);
    524     }
    525     fprintf(f, "  },\n");
    526   }
    527 
    528   fprintf(f, "};\n");
    529   fclose(f);
    530 }
    531 #endif  // ENTROPY_STATS
    532 
    533 // DEBUG: Print out the segment id of each MB in the current frame.
    534 static void print_seg_map(VP9_COMP *cpi) {
    535   VP9_COMMON *cm = &cpi->common;
    536   int row, col;
    537   int map_index = 0;
    538   FILE *statsfile = fopen("segmap.stt", "a");
    539 
    540   fprintf(statsfile, "%10d\n", cm->current_video_frame);
    541 
    542   for (row = 0; row < cpi->common.mi_rows; row++) {
    543     for (col = 0; col < cpi->common.mi_cols; col++) {
    544       fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
    545       map_index++;
    546     }
    547     fprintf(statsfile, "\n");
    548   }
    549   fprintf(statsfile, "\n");
    550 
    551   fclose(statsfile);
    552 }
    553 
    554 static void update_reference_segmentation_map(VP9_COMP *cpi) {
    555   VP9_COMMON *const cm = &cpi->common;
    556   int row, col;
    557   MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible;
    558   uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;
    559 
    560   for (row = 0; row < cm->mi_rows; row++) {
    561     mi_8x8 = mi_8x8_ptr;
    562     cache = cache_ptr;
    563     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
    564       cache[0] = mi_8x8[0]->mbmi.segment_id;
    565     mi_8x8_ptr += cm->mode_info_stride;
    566     cache_ptr += cm->mi_cols;
    567   }
    568 }
    569 
    570 static void set_default_lf_deltas(struct loopfilter *lf) {
    571   lf->mode_ref_delta_enabled = 1;
    572   lf->mode_ref_delta_update = 1;
    573 
    574   vp9_zero(lf->ref_deltas);
    575   vp9_zero(lf->mode_deltas);
    576 
    577   // Test of ref frame deltas
    578   lf->ref_deltas[INTRA_FRAME] = 2;
    579   lf->ref_deltas[LAST_FRAME] = 0;
    580   lf->ref_deltas[GOLDEN_FRAME] = -2;
    581   lf->ref_deltas[ALTREF_FRAME] = -2;
    582 
    583   lf->mode_deltas[0] = 0;   // Zero
    584   lf->mode_deltas[1] = 0;   // New mv
    585 }
    586 
    587 static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode) {
    588   SPEED_FEATURES *sf = &cpi->sf;
    589   int i;
    590 
    591   // Set baseline threshold values
    592   for (i = 0; i < MAX_MODES; ++i)
    593     sf->thresh_mult[i] = mode == 0 ? -500 : 0;
    594 
    595   sf->thresh_mult[THR_NEARESTMV] = 0;
    596   sf->thresh_mult[THR_NEARESTG] = 0;
    597   sf->thresh_mult[THR_NEARESTA] = 0;
    598 
    599   sf->thresh_mult[THR_DC] += 1000;
    600 
    601   sf->thresh_mult[THR_NEWMV] += 1000;
    602   sf->thresh_mult[THR_NEWA] += 1000;
    603   sf->thresh_mult[THR_NEWG] += 1000;
    604 
    605   sf->thresh_mult[THR_NEARMV] += 1000;
    606   sf->thresh_mult[THR_NEARA] += 1000;
    607   sf->thresh_mult[THR_COMP_NEARESTLA] += 1000;
    608   sf->thresh_mult[THR_COMP_NEARESTGA] += 1000;
    609 
    610   sf->thresh_mult[THR_TM] += 1000;
    611 
    612   sf->thresh_mult[THR_COMP_NEARLA] += 1500;
    613   sf->thresh_mult[THR_COMP_NEWLA] += 2000;
    614   sf->thresh_mult[THR_NEARG] += 1000;
    615   sf->thresh_mult[THR_COMP_NEARGA] += 1500;
    616   sf->thresh_mult[THR_COMP_NEWGA] += 2000;
    617 
    618   sf->thresh_mult[THR_ZEROMV] += 2000;
    619   sf->thresh_mult[THR_ZEROG] += 2000;
    620   sf->thresh_mult[THR_ZEROA] += 2000;
    621   sf->thresh_mult[THR_COMP_ZEROLA] += 2500;
    622   sf->thresh_mult[THR_COMP_ZEROGA] += 2500;
    623 
    624   sf->thresh_mult[THR_H_PRED] += 2000;
    625   sf->thresh_mult[THR_V_PRED] += 2000;
    626   sf->thresh_mult[THR_D45_PRED ] += 2500;
    627   sf->thresh_mult[THR_D135_PRED] += 2500;
    628   sf->thresh_mult[THR_D117_PRED] += 2500;
    629   sf->thresh_mult[THR_D153_PRED] += 2500;
    630   sf->thresh_mult[THR_D207_PRED] += 2500;
    631   sf->thresh_mult[THR_D63_PRED] += 2500;
    632 
    633   /* disable frame modes if flags not set */
    634   if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
    635     sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
    636     sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
    637     sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
    638     sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
    639   }
    640   if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
    641     sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
    642     sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
    643     sf->thresh_mult[THR_NEARG    ] = INT_MAX;
    644     sf->thresh_mult[THR_NEWG     ] = INT_MAX;
    645   }
    646   if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
    647     sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
    648     sf->thresh_mult[THR_ZEROA    ] = INT_MAX;
    649     sf->thresh_mult[THR_NEARA    ] = INT_MAX;
    650     sf->thresh_mult[THR_NEWA     ] = INT_MAX;
    651   }
    652 
    653   if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
    654       (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
    655     sf->thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
    656     sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
    657     sf->thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
    658     sf->thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
    659   }
    660   if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
    661       (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
    662     sf->thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
    663     sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
    664     sf->thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
    665     sf->thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
    666   }
    667 }
    668 
    669 static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi, int mode) {
    670   SPEED_FEATURES *sf = &cpi->sf;
    671   int i;
    672 
    673   for (i = 0; i < MAX_REFS; ++i)
    674     sf->thresh_mult_sub8x8[i] = mode == 0 ? -500 : 0;
    675 
    676   sf->thresh_mult_sub8x8[THR_LAST] += 2500;
    677   sf->thresh_mult_sub8x8[THR_GOLD] += 2500;
    678   sf->thresh_mult_sub8x8[THR_ALTR] += 2500;
    679   sf->thresh_mult_sub8x8[THR_INTRA] += 2500;
    680   sf->thresh_mult_sub8x8[THR_COMP_LA] += 4500;
    681   sf->thresh_mult_sub8x8[THR_COMP_GA] += 4500;
    682 
    683   // Check for masked out split cases.
    684   for (i = 0; i < MAX_REFS; i++) {
    685     if (sf->disable_split_mask & (1 << i))
    686       sf->thresh_mult_sub8x8[i] = INT_MAX;
    687   }
    688 
    689   // disable mode test if frame flag is not set
    690   if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
    691     sf->thresh_mult_sub8x8[THR_LAST] = INT_MAX;
    692   if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
    693     sf->thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
    694   if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
    695     sf->thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
    696   if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
    697       (VP9_LAST_FLAG | VP9_ALT_FLAG))
    698     sf->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
    699   if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
    700       (VP9_GOLD_FLAG | VP9_ALT_FLAG))
    701     sf->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
    702 }
    703 
    704 void vp9_set_speed_features(VP9_COMP *cpi) {
    705   SPEED_FEATURES *sf = &cpi->sf;
    706   int mode = cpi->compressor_speed;
    707   int speed = cpi->speed;
    708   int i;
    709 
    710   // Only modes 0 and 1 supported for now in experimental code basae
    711   if (mode > 1)
    712     mode = 1;
    713 
    714   for (i = 0; i < MAX_MODES; ++i)
    715     cpi->mode_chosen_counts[i] = 0;
    716 
    717   // best quality defaults
    718   sf->RD = 1;
    719   sf->search_method = NSTEP;
    720   sf->auto_filter = 1;
    721   sf->recode_loop = 1;
    722   sf->subpel_search_method = SUBPEL_TREE;
    723   sf->subpel_iters_per_step = 2;
    724   sf->optimize_coefficients = !cpi->oxcf.lossless;
    725   sf->reduce_first_step_size = 0;
    726   sf->auto_mv_step_size = 0;
    727   sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
    728   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
    729   sf->adaptive_rd_thresh = 0;
    730   sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
    731   sf->tx_size_search_method = USE_FULL_RD;
    732   sf->use_lp32x32fdct = 0;
    733   sf->adaptive_motion_search = 0;
    734   sf->use_avoid_tested_higherror = 0;
    735   sf->reference_masking = 0;
    736   sf->use_one_partition_size_always = 0;
    737   sf->less_rectangular_check = 0;
    738   sf->use_square_partition_only = 0;
    739   sf->auto_min_max_partition_size = 0;
    740   sf->max_partition_size = BLOCK_64X64;
    741   sf->min_partition_size = BLOCK_4X4;
    742   sf->adjust_partitioning_from_last_frame = 0;
    743   sf->last_partitioning_redo_frequency = 4;
    744   sf->disable_split_mask = 0;
    745   sf->mode_search_skip_flags = 0;
    746   sf->disable_split_var_thresh = 0;
    747   sf->disable_filter_search_var_thresh = 0;
    748   for (i = 0; i < TX_SIZES; i++) {
    749     sf->intra_y_mode_mask[i] = ALL_INTRA_MODES;
    750     sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES;
    751   }
    752   sf->use_rd_breakout = 0;
    753   sf->skip_encode_sb = 0;
    754   sf->use_uv_intra_rd_estimate = 0;
    755   sf->use_fast_lpf_pick = 0;
    756   sf->use_fast_coef_updates = 0;
    757   sf->using_small_partition_info = 0;
    758   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
    759 
    760 #if CONFIG_MULTIPLE_ARF
    761   // Switch segmentation off.
    762   sf->static_segmentation = 0;
    763 #else
    764   sf->static_segmentation = 0;
    765 #endif
    766 
    767   sf->variance_adaptive_quantization = 0;
    768 
    769   switch (mode) {
    770     case 0:  // This is the best quality mode.
    771       break;
    772 
    773     case 1:
    774 #if CONFIG_MULTIPLE_ARF
    775       // Switch segmentation off.
    776       sf->static_segmentation = 0;
    777 #else
    778       sf->static_segmentation = 0;
    779 #endif
    780       sf->use_avoid_tested_higherror = 1;
    781       sf->adaptive_rd_thresh = 1;
    782       sf->recode_loop = (speed < 1);
    783 
    784       if (speed == 1) {
    785         sf->use_square_partition_only = !frame_is_intra_only(&cpi->common);
    786         sf->less_rectangular_check  = 1;
    787         sf->tx_size_search_method = frame_is_intra_only(&cpi->common)
    788                                      ? USE_FULL_RD : USE_LARGESTALL;
    789 
    790         if (MIN(cpi->common.width, cpi->common.height) >= 720)
    791           sf->disable_split_mask = cpi->common.show_frame ?
    792               DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
    793         else
    794           sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
    795 
    796         sf->use_rd_breakout = 1;
    797         sf->adaptive_motion_search = 1;
    798         sf->auto_mv_step_size = 1;
    799         sf->adaptive_rd_thresh = 2;
    800         sf->recode_loop = 2;
    801         sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    802         sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    803         sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
    804       }
    805       if (speed == 2) {
    806         sf->use_square_partition_only = !frame_is_intra_only(&cpi->common);
    807         sf->less_rectangular_check  = 1;
    808         sf->tx_size_search_method = frame_is_intra_only(&cpi->common)
    809                                      ? USE_FULL_RD : USE_LARGESTALL;
    810 
    811         if (MIN(cpi->common.width, cpi->common.height) >= 720)
    812           sf->disable_split_mask = cpi->common.show_frame ?
    813               DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
    814         else
    815           sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
    816 
    817 
    818         sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
    819                                      FLAG_SKIP_INTRA_BESTINTER |
    820                                      FLAG_SKIP_COMP_BESTINTRA |
    821                                      FLAG_SKIP_INTRA_LOWVAR;
    822 
    823         sf->use_rd_breakout = 1;
    824         sf->adaptive_motion_search = 1;
    825         sf->auto_mv_step_size = 1;
    826 
    827         sf->disable_filter_search_var_thresh = 16;
    828         sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    829 
    830         sf->auto_min_max_partition_size = 1;
    831         sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
    832         sf->adjust_partitioning_from_last_frame = 1;
    833         sf->last_partitioning_redo_frequency = 3;
    834 
    835         sf->adaptive_rd_thresh = 2;
    836         sf->recode_loop = 2;
    837         sf->use_lp32x32fdct = 1;
    838         sf->mode_skip_start = 11;
    839         sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
    840         sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
    841         sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
    842         sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
    843       }
    844       if (speed == 3) {
    845         sf->use_square_partition_only = 1;
    846         sf->tx_size_search_method = USE_LARGESTALL;
    847 
    848         if (MIN(cpi->common.width, cpi->common.height) >= 720)
    849           sf->disable_split_mask = DISABLE_ALL_SPLIT;
    850         else
    851           sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
    852 
    853         sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
    854                                      FLAG_SKIP_INTRA_BESTINTER |
    855                                      FLAG_SKIP_COMP_BESTINTRA |
    856                                      FLAG_SKIP_INTRA_LOWVAR;
    857 
    858         sf->use_rd_breakout = 1;
    859         sf->adaptive_motion_search = 1;
    860         sf->auto_mv_step_size = 1;
    861 
    862         sf->disable_filter_search_var_thresh = 16;
    863         sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    864 
    865         sf->auto_min_max_partition_size = 1;
    866         sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
    867         sf->adjust_partitioning_from_last_frame = 1;
    868         sf->last_partitioning_redo_frequency = 3;
    869 
    870         sf->use_uv_intra_rd_estimate = 1;
    871         sf->skip_encode_sb = 1;
    872         sf->use_lp32x32fdct = 1;
    873         sf->subpel_iters_per_step = 1;
    874         sf->use_fast_coef_updates = 2;
    875 
    876         sf->adaptive_rd_thresh = 4;
    877         sf->mode_skip_start = 6;
    878       }
    879       if (speed == 4) {
    880         sf->use_square_partition_only = 1;
    881         sf->tx_size_search_method = USE_LARGESTALL;
    882         sf->disable_split_mask = DISABLE_ALL_SPLIT;
    883 
    884         sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
    885                                      FLAG_SKIP_INTRA_BESTINTER |
    886                                      FLAG_SKIP_COMP_BESTINTRA |
    887                                      FLAG_SKIP_COMP_REFMISMATCH |
    888                                      FLAG_SKIP_INTRA_LOWVAR |
    889                                      FLAG_EARLY_TERMINATE;
    890 
    891         sf->use_rd_breakout = 1;
    892         sf->adaptive_motion_search = 1;
    893         sf->auto_mv_step_size = 1;
    894 
    895         sf->disable_filter_search_var_thresh = 16;
    896         sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    897 
    898         sf->auto_min_max_partition_size = 1;
    899         sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
    900         sf->adjust_partitioning_from_last_frame = 1;
    901         sf->last_partitioning_redo_frequency = 3;
    902 
    903         sf->use_uv_intra_rd_estimate = 1;
    904         sf->skip_encode_sb = 1;
    905         sf->use_lp32x32fdct = 1;
    906         sf->subpel_iters_per_step = 1;
    907         sf->use_fast_coef_updates = 2;
    908 
    909         sf->adaptive_rd_thresh = 4;
    910         sf->mode_skip_start = 6;
    911 
    912         /* sf->intra_y_mode_mask = INTRA_DC_ONLY;
    913         sf->intra_uv_mode_mask = INTRA_DC_ONLY;
    914         sf->search_method = BIGDIA;
    915         sf->disable_split_var_thresh = 64;
    916         sf->disable_filter_search_var_thresh = 64; */
    917       }
    918       if (speed == 5) {
    919         sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
    920         sf->use_one_partition_size_always = 1;
    921         sf->always_this_block_size = BLOCK_16X16;
    922         sf->tx_size_search_method = frame_is_intra_only(&cpi->common) ?
    923                                      USE_FULL_RD : USE_LARGESTALL;
    924         sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
    925                                      FLAG_SKIP_INTRA_BESTINTER |
    926                                      FLAG_SKIP_COMP_BESTINTRA |
    927                                      FLAG_SKIP_COMP_REFMISMATCH |
    928                                      FLAG_SKIP_INTRA_LOWVAR |
    929                                      FLAG_EARLY_TERMINATE;
    930         sf->use_rd_breakout = 1;
    931         sf->use_lp32x32fdct = 1;
    932         sf->optimize_coefficients = 0;
    933         sf->auto_mv_step_size = 1;
    934         // sf->reduce_first_step_size = 1;
    935         // sf->reference_masking = 1;
    936 
    937         sf->disable_split_mask = DISABLE_ALL_SPLIT;
    938         sf->search_method = HEX;
    939         sf->subpel_iters_per_step = 1;
    940         sf->disable_split_var_thresh = 64;
    941         sf->disable_filter_search_var_thresh = 96;
    942         for (i = 0; i < TX_SIZES; i++) {
    943           sf->intra_y_mode_mask[i] = INTRA_DC_ONLY;
    944           sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
    945         }
    946         sf->use_fast_coef_updates = 2;
    947         sf->adaptive_rd_thresh = 4;
    948         sf->mode_skip_start = 6;
    949       }
    950       break;
    951   }; /* switch */
    952 
    953   // Set rd thresholds based on mode and speed setting
    954   set_rd_speed_thresholds(cpi, mode);
    955   set_rd_speed_thresholds_sub8x8(cpi, mode);
    956 
    957   // Slow quant, dct and trellis not worthwhile for first pass
    958   // so make sure they are always turned off.
    959   if (cpi->pass == 1) {
    960     sf->optimize_coefficients = 0;
    961   }
    962 
    963   // No recode for 1 pass.
    964   if (cpi->pass == 0) {
    965     sf->recode_loop = 0;
    966     sf->optimize_coefficients = 0;
    967   }
    968 
    969   cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
    970   if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
    971     cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
    972   }
    973 
    974   if (cpi->sf.subpel_search_method == SUBPEL_ITERATIVE) {
    975     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_iterative;
    976     cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_iterative;
    977   } else if (cpi->sf.subpel_search_method == SUBPEL_TREE) {
    978     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
    979     cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree;
    980   }
    981 
    982   cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
    983 
    984 #ifdef SPEEDSTATS
    985   frames_at_speed[cpi->speed]++;
    986 #endif
    987 }
    988 
    989 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
    990   VP9_COMMON *cm = &cpi->common;
    991 
    992   cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
    993                                       cm->subsampling_x, cm->subsampling_y,
    994                                       cpi->oxcf.lag_in_frames);
    995   if (!cpi->lookahead)
    996     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
    997                        "Failed to allocate lag buffers");
    998 
    999   if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
   1000                                cpi->oxcf.width, cpi->oxcf.height,
   1001                                cm->subsampling_x, cm->subsampling_y,
   1002                                VP9BORDERINPIXELS))
   1003     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
   1004                        "Failed to allocate altref buffer");
   1005 }
   1006 
   1007 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
   1008   VP9_COMMON *cm = &cpi->common;
   1009 
   1010   if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
   1011     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
   1012                        "Failed to allocate frame buffers");
   1013 
   1014   if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
   1015                              cm->width, cm->height,
   1016                              cm->subsampling_x, cm->subsampling_y,
   1017                              VP9BORDERINPIXELS))
   1018     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
   1019                        "Failed to allocate last frame buffer");
   1020 
   1021   if (vp9_alloc_frame_buffer(&cpi->scaled_source,
   1022                              cm->width, cm->height,
   1023                              cm->subsampling_x, cm->subsampling_y,
   1024                              VP9BORDERINPIXELS))
   1025     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
   1026                        "Failed to allocate scaled source buffer");
   1027 
   1028   vpx_free(cpi->tok);
   1029 
   1030   {
   1031     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
   1032 
   1033     CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
   1034   }
   1035 
   1036   vpx_free(cpi->mb_activity_map);
   1037   CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
   1038                   vpx_calloc(sizeof(unsigned int),
   1039                              cm->mb_rows * cm->mb_cols));
   1040 
   1041   vpx_free(cpi->mb_norm_activity_map);
   1042   CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
   1043                   vpx_calloc(sizeof(unsigned int),
   1044                              cm->mb_rows * cm->mb_cols));
   1045 
   1046   // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
   1047   // block where mi unit size is 8x8.
   1048   vpx_free(cpi->above_context[0]);
   1049   CHECK_MEM_ERROR(cm, cpi->above_context[0],
   1050                   vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) *
   1051                              MAX_MB_PLANE,
   1052                              sizeof(*cpi->above_context[0])));
   1053 
   1054   vpx_free(cpi->above_seg_context);
   1055   CHECK_MEM_ERROR(cm, cpi->above_seg_context,
   1056                   vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols),
   1057                              sizeof(*cpi->above_seg_context)));
   1058 }
   1059 
   1060 
   1061 static void update_frame_size(VP9_COMP *cpi) {
   1062   VP9_COMMON *cm = &cpi->common;
   1063 
   1064   vp9_update_frame_size(cm);
   1065 
   1066   // Update size of buffers local to this frame
   1067   if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
   1068                                cm->width, cm->height,
   1069                                cm->subsampling_x, cm->subsampling_y,
   1070                                VP9BORDERINPIXELS))
   1071     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
   1072                        "Failed to reallocate last frame buffer");
   1073 
   1074   if (vp9_realloc_frame_buffer(&cpi->scaled_source,
   1075                                cm->width, cm->height,
   1076                                cm->subsampling_x, cm->subsampling_y,
   1077                                VP9BORDERINPIXELS))
   1078     vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
   1079                        "Failed to reallocate scaled source buffer");
   1080 
   1081   {
   1082     int y_stride = cpi->scaled_source.y_stride;
   1083 
   1084     if (cpi->sf.search_method == NSTEP) {
   1085       vp9_init3smotion_compensation(&cpi->mb, y_stride);
   1086     } else if (cpi->sf.search_method == DIAMOND) {
   1087       vp9_init_dsmotion_compensation(&cpi->mb, y_stride);
   1088     }
   1089   }
   1090 
   1091   {
   1092     int i;
   1093     for (i = 1; i < MAX_MB_PLANE; ++i) {
   1094       cpi->above_context[i] = cpi->above_context[0] +
   1095                               i * sizeof(*cpi->above_context[0]) * 2 *
   1096                               mi_cols_aligned_to_sb(cm->mi_cols);
   1097     }
   1098   }
   1099 }
   1100 
   1101 
   1102 // Table that converts 0-63 Q range values passed in outside to the Qindex
   1103 // range used internally.
   1104 static const int q_trans[] = {
   1105   0,    4,   8,  12,  16,  20,  24,  28,
   1106   32,   36,  40,  44,  48,  52,  56,  60,
   1107   64,   68,  72,  76,  80,  84,  88,  92,
   1108   96,  100, 104, 108, 112, 116, 120, 124,
   1109   128, 132, 136, 140, 144, 148, 152, 156,
   1110   160, 164, 168, 172, 176, 180, 184, 188,
   1111   192, 196, 200, 204, 208, 212, 216, 220,
   1112   224, 228, 232, 236, 240, 244, 249, 255,
   1113 };
   1114 
   1115 int vp9_reverse_trans(int x) {
   1116   int i;
   1117 
   1118   for (i = 0; i < 64; i++)
   1119     if (q_trans[i] >= x)
   1120       return i;
   1121 
   1122   return 63;
   1123 };
   1124 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
   1125   if (framerate < 0.1)
   1126     framerate = 30;
   1127 
   1128   cpi->oxcf.framerate = framerate;
   1129   cpi->output_framerate = cpi->oxcf.framerate;
   1130   cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth
   1131                              / cpi->output_framerate);
   1132   cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth
   1133                                 / cpi->output_framerate);
   1134   cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
   1135                                    cpi->oxcf.two_pass_vbrmin_section / 100);
   1136 
   1137 
   1138   cpi->min_frame_bandwidth = MAX(cpi->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
   1139 
   1140   // Set Maximum gf/arf interval
   1141   cpi->max_gf_interval = 16;
   1142 
   1143   // Extended interval for genuinely static scenes
   1144   cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
   1145 
   1146   // Special conditions when alt ref frame enabled in lagged compress mode
   1147   if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
   1148     if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
   1149       cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
   1150 
   1151     if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
   1152       cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
   1153   }
   1154 
   1155   if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval)
   1156     cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
   1157 }
   1158 
   1159 static int64_t rescale(int val, int64_t num, int denom) {
   1160   int64_t llnum = num;
   1161   int64_t llden = denom;
   1162   int64_t llval = val;
   1163 
   1164   return (llval * llnum / llden);
   1165 }
   1166 
   1167 static void set_tile_limits(VP9_COMP *cpi) {
   1168   VP9_COMMON *const cm = &cpi->common;
   1169 
   1170   int min_log2_tile_cols, max_log2_tile_cols;
   1171   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
   1172 
   1173   cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
   1174                              min_log2_tile_cols, max_log2_tile_cols);
   1175   cm->log2_tile_rows = cpi->oxcf.tile_rows;
   1176 }
   1177 
   1178 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
   1179   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   1180   VP9_COMMON *const cm = &cpi->common;
   1181   int i;
   1182 
   1183   cpi->oxcf = *oxcf;
   1184 
   1185   cm->version = oxcf->version;
   1186 
   1187   cm->width = oxcf->width;
   1188   cm->height = oxcf->height;
   1189   cm->subsampling_x = 0;
   1190   cm->subsampling_y = 0;
   1191   vp9_alloc_compressor_data(cpi);
   1192 
   1193   // change includes all joint functionality
   1194   vp9_change_config(ptr, oxcf);
   1195 
   1196   // Initialize active best and worst q and average q values.
   1197   cpi->active_worst_quality         = cpi->oxcf.worst_allowed_q;
   1198   cpi->active_best_quality          = cpi->oxcf.best_allowed_q;
   1199   cpi->avg_frame_qindex             = cpi->oxcf.worst_allowed_q;
   1200 
   1201   // Initialise the starting buffer levels
   1202   cpi->buffer_level                 = cpi->oxcf.starting_buffer_level;
   1203   cpi->bits_off_target              = cpi->oxcf.starting_buffer_level;
   1204 
   1205   cpi->rolling_target_bits          = cpi->av_per_frame_bandwidth;
   1206   cpi->rolling_actual_bits          = cpi->av_per_frame_bandwidth;
   1207   cpi->long_rolling_target_bits     = cpi->av_per_frame_bandwidth;
   1208   cpi->long_rolling_actual_bits     = cpi->av_per_frame_bandwidth;
   1209 
   1210   cpi->total_actual_bits            = 0;
   1211   cpi->total_target_vs_actual       = 0;
   1212 
   1213   cpi->static_mb_pct = 0;
   1214 
   1215   cpi->lst_fb_idx = 0;
   1216   cpi->gld_fb_idx = 1;
   1217   cpi->alt_fb_idx = 2;
   1218 
   1219   cpi->current_layer = 0;
   1220   cpi->use_svc = 0;
   1221 
   1222   set_tile_limits(cpi);
   1223 
   1224   cpi->fixed_divide[0] = 0;
   1225   for (i = 1; i < 512; i++)
   1226     cpi->fixed_divide[i] = 0x80000 / i;
   1227 }
   1228 
   1229 
   1230 void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
   1231   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   1232   VP9_COMMON *const cm = &cpi->common;
   1233 
   1234   if (!cpi || !oxcf)
   1235     return;
   1236 
   1237   if (cm->version != oxcf->version) {
   1238     cm->version = oxcf->version;
   1239   }
   1240 
   1241   cpi->oxcf = *oxcf;
   1242 
   1243   switch (cpi->oxcf.Mode) {
   1244       // Real time and one pass deprecated in test code base
   1245     case MODE_GOODQUALITY:
   1246       cpi->pass = 0;
   1247       cpi->compressor_speed = 2;
   1248       cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
   1249       break;
   1250 
   1251     case MODE_FIRSTPASS:
   1252       cpi->pass = 1;
   1253       cpi->compressor_speed = 1;
   1254       break;
   1255 
   1256     case MODE_SECONDPASS:
   1257       cpi->pass = 2;
   1258       cpi->compressor_speed = 1;
   1259       cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
   1260       break;
   1261 
   1262     case MODE_SECONDPASS_BEST:
   1263       cpi->pass = 2;
   1264       cpi->compressor_speed = 0;
   1265       break;
   1266   }
   1267 
   1268   cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
   1269   cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
   1270   cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
   1271 
   1272   cpi->oxcf.lossless = oxcf->lossless;
   1273   cpi->mb.e_mbd.itxm_add = cpi->oxcf.lossless ? vp9_iwht4x4_add
   1274                                               : vp9_idct4x4_add;
   1275   cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
   1276 
   1277   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
   1278 
   1279   // cpi->use_golden_frame_only = 0;
   1280   // cpi->use_last_frame_only = 0;
   1281   cpi->refresh_golden_frame = 0;
   1282   cpi->refresh_last_frame = 1;
   1283   cm->refresh_frame_context = 1;
   1284   cm->reset_frame_context = 0;
   1285 
   1286   setup_features(cm);
   1287   cpi->common.allow_high_precision_mv = 0;  // Default mv precision
   1288   set_mvcost(cpi);
   1289 
   1290   {
   1291     int i;
   1292 
   1293     for (i = 0; i < MAX_SEGMENTS; i++)
   1294       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
   1295   }
   1296 
   1297   // At the moment the first order values may not be > MAXQ
   1298   cpi->oxcf.fixed_q = MIN(cpi->oxcf.fixed_q, MAXQ);
   1299 
   1300   // local file playback mode == really big buffer
   1301   if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
   1302     cpi->oxcf.starting_buffer_level   = 60000;
   1303     cpi->oxcf.optimal_buffer_level    = 60000;
   1304     cpi->oxcf.maximum_buffer_size     = 240000;
   1305   }
   1306 
   1307   // Convert target bandwidth from Kbit/s to Bit/s
   1308   cpi->oxcf.target_bandwidth       *= 1000;
   1309 
   1310   cpi->oxcf.starting_buffer_level = rescale(cpi->oxcf.starting_buffer_level,
   1311                                             cpi->oxcf.target_bandwidth, 1000);
   1312 
   1313   // Set or reset optimal and maximum buffer levels.
   1314   if (cpi->oxcf.optimal_buffer_level == 0)
   1315     cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
   1316   else
   1317     cpi->oxcf.optimal_buffer_level = rescale(cpi->oxcf.optimal_buffer_level,
   1318                                              cpi->oxcf.target_bandwidth, 1000);
   1319 
   1320   if (cpi->oxcf.maximum_buffer_size == 0)
   1321     cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
   1322   else
   1323     cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size,
   1324                                             cpi->oxcf.target_bandwidth, 1000);
   1325 
   1326   // Set up frame rate and related parameters rate control values.
   1327   vp9_new_framerate(cpi, cpi->oxcf.framerate);
   1328 
   1329   // Set absolute upper and lower quality limits
   1330   cpi->worst_quality = cpi->oxcf.worst_allowed_q;
   1331   cpi->best_quality = cpi->oxcf.best_allowed_q;
   1332 
   1333   // active values should only be modified if out of new range
   1334   cpi->active_worst_quality = clamp(cpi->active_worst_quality,
   1335                                     cpi->oxcf.best_allowed_q,
   1336                                     cpi->oxcf.worst_allowed_q);
   1337 
   1338   cpi->active_best_quality = clamp(cpi->active_best_quality,
   1339                                    cpi->oxcf.best_allowed_q,
   1340                                    cpi->oxcf.worst_allowed_q);
   1341 
   1342   cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
   1343 
   1344   cpi->cq_target_quality = cpi->oxcf.cq_level;
   1345 
   1346   cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
   1347 
   1348   cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
   1349 
   1350   cm->display_width = cpi->oxcf.width;
   1351   cm->display_height = cpi->oxcf.height;
   1352 
   1353   // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
   1354   cpi->oxcf.Sharpness = MIN(7, cpi->oxcf.Sharpness);
   1355 
   1356   cpi->common.lf.sharpness_level = cpi->oxcf.Sharpness;
   1357 
   1358   if (cpi->initial_width) {
   1359     // Increasing the size of the frame beyond the first seen frame, or some
   1360     // otherwise signalled maximum size, is not supported.
   1361     // TODO(jkoleszar): exit gracefully.
   1362     assert(cm->width <= cpi->initial_width);
   1363     assert(cm->height <= cpi->initial_height);
   1364   }
   1365   update_frame_size(cpi);
   1366 
   1367   if (cpi->oxcf.fixed_q >= 0) {
   1368     cpi->last_q[0] = cpi->oxcf.fixed_q;
   1369     cpi->last_q[1] = cpi->oxcf.fixed_q;
   1370     cpi->last_boosted_qindex = cpi->oxcf.fixed_q;
   1371   }
   1372 
   1373   cpi->speed = cpi->oxcf.cpu_used;
   1374 
   1375   if (cpi->oxcf.lag_in_frames == 0) {
   1376     // force to allowlag to 0 if lag_in_frames is 0;
   1377     cpi->oxcf.allow_lag = 0;
   1378   } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) {
   1379      // Limit on lag buffers as these are not currently dynamically allocated
   1380     cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
   1381   }
   1382 
   1383   // YX Temp
   1384 #if CONFIG_MULTIPLE_ARF
   1385   vp9_zero(cpi->alt_ref_source);
   1386 #else
   1387   cpi->alt_ref_source = NULL;
   1388 #endif
   1389   cpi->is_src_frame_alt_ref = 0;
   1390 
   1391 #if 0
   1392   // Experimental RD Code
   1393   cpi->frame_distortion = 0;
   1394   cpi->last_frame_distortion = 0;
   1395 #endif
   1396 
   1397   set_tile_limits(cpi);
   1398 }
   1399 
   1400 #define M_LOG2_E 0.693147180559945309417
   1401 #define log2f(x) (log (x) / (float) M_LOG2_E)
   1402 
   1403 static void cal_nmvjointsadcost(int *mvjointsadcost) {
   1404   mvjointsadcost[0] = 600;
   1405   mvjointsadcost[1] = 300;
   1406   mvjointsadcost[2] = 300;
   1407   mvjointsadcost[0] = 300;
   1408 }
   1409 
   1410 static void cal_nmvsadcosts(int *mvsadcost[2]) {
   1411   int i = 1;
   1412 
   1413   mvsadcost[0][0] = 0;
   1414   mvsadcost[1][0] = 0;
   1415 
   1416   do {
   1417     double z = 256 * (2 * (log2f(8 * i) + .6));
   1418     mvsadcost[0][i] = (int)z;
   1419     mvsadcost[1][i] = (int)z;
   1420     mvsadcost[0][-i] = (int)z;
   1421     mvsadcost[1][-i] = (int)z;
   1422   } while (++i <= MV_MAX);
   1423 }
   1424 
   1425 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
   1426   int i = 1;
   1427 
   1428   mvsadcost[0][0] = 0;
   1429   mvsadcost[1][0] = 0;
   1430 
   1431   do {
   1432     double z = 256 * (2 * (log2f(8 * i) + .6));
   1433     mvsadcost[0][i] = (int)z;
   1434     mvsadcost[1][i] = (int)z;
   1435     mvsadcost[0][-i] = (int)z;
   1436     mvsadcost[1][-i] = (int)z;
   1437   } while (++i <= MV_MAX);
   1438 }
   1439 
   1440 static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk,
   1441                                PICK_MODE_CONTEXT *ctx) {
   1442   int num_pix = num_4x4_blk << 4;
   1443   int i, k;
   1444   ctx->num_4x4_blk = num_4x4_blk;
   1445   CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
   1446                   vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
   1447   for (i = 0; i < MAX_MB_PLANE; ++i) {
   1448     for (k = 0; k < 3; ++k) {
   1449       CHECK_MEM_ERROR(cm, ctx->coeff[i][k],
   1450                       vpx_memalign(16, num_pix * sizeof(int16_t)));
   1451       CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k],
   1452                       vpx_memalign(16, num_pix * sizeof(int16_t)));
   1453       CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k],
   1454                       vpx_memalign(16, num_pix * sizeof(int16_t)));
   1455       CHECK_MEM_ERROR(cm, ctx->eobs[i][k],
   1456                       vpx_memalign(16, num_pix * sizeof(uint16_t)));
   1457       ctx->coeff_pbuf[i][k]   = ctx->coeff[i][k];
   1458       ctx->qcoeff_pbuf[i][k]  = ctx->qcoeff[i][k];
   1459       ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k];
   1460       ctx->eobs_pbuf[i][k]    = ctx->eobs[i][k];
   1461     }
   1462   }
   1463 }
   1464 
   1465 static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
   1466   int i, k;
   1467   vpx_free(ctx->zcoeff_blk);
   1468   ctx->zcoeff_blk = 0;
   1469   for (i = 0; i < MAX_MB_PLANE; ++i) {
   1470     for (k = 0; k < 3; ++k) {
   1471       vpx_free(ctx->coeff[i][k]);
   1472       ctx->coeff[i][k] = 0;
   1473       vpx_free(ctx->qcoeff[i][k]);
   1474       ctx->qcoeff[i][k] = 0;
   1475       vpx_free(ctx->dqcoeff[i][k]);
   1476       ctx->dqcoeff[i][k] = 0;
   1477       vpx_free(ctx->eobs[i][k]);
   1478       ctx->eobs[i][k] = 0;
   1479     }
   1480   }
   1481 }
   1482 
   1483 static void init_pick_mode_context(VP9_COMP *cpi) {
   1484   int i;
   1485   VP9_COMMON *const cm = &cpi->common;
   1486   MACROBLOCK *const x  = &cpi->mb;
   1487 
   1488 
   1489   for (i = 0; i < BLOCK_SIZES; ++i) {
   1490     const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
   1491     const int num_4x4_h = num_4x4_blocks_high_lookup[i];
   1492     const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
   1493     if (i < BLOCK_16X16) {
   1494       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
   1495         for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
   1496           for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
   1497             PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1498             alloc_mode_context(cm, num_4x4_blk, ctx);
   1499           }
   1500         }
   1501       }
   1502     } else if (i < BLOCK_32X32) {
   1503       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
   1504         for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
   1505           PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1506           ctx->num_4x4_blk = num_4x4_blk;
   1507           alloc_mode_context(cm, num_4x4_blk, ctx);
   1508         }
   1509       }
   1510     } else if (i < BLOCK_64X64) {
   1511       for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
   1512         PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1513         ctx->num_4x4_blk = num_4x4_blk;
   1514         alloc_mode_context(cm, num_4x4_blk, ctx);
   1515       }
   1516     } else {
   1517       PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1518       ctx->num_4x4_blk = num_4x4_blk;
   1519       alloc_mode_context(cm, num_4x4_blk, ctx);
   1520     }
   1521   }
   1522 }
   1523 
   1524 static void free_pick_mode_context(MACROBLOCK *x) {
   1525   int i;
   1526 
   1527   for (i = 0; i < BLOCK_SIZES; ++i) {
   1528     const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
   1529     const int num_4x4_h = num_4x4_blocks_high_lookup[i];
   1530     const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
   1531     if (i < BLOCK_16X16) {
   1532       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
   1533         for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
   1534           for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
   1535             PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1536             free_mode_context(ctx);
   1537           }
   1538         }
   1539       }
   1540     } else if (i < BLOCK_32X32) {
   1541       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
   1542         for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
   1543           PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1544           free_mode_context(ctx);
   1545         }
   1546       }
   1547     } else if (i < BLOCK_64X64) {
   1548       for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
   1549         PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1550         free_mode_context(ctx);
   1551       }
   1552     } else {
   1553       PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
   1554       free_mode_context(ctx);
   1555     }
   1556   }
   1557 }
   1558 
   1559 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
   1560   int i, j;
   1561   volatile union {
   1562     VP9_COMP *cpi;
   1563     VP9_PTR   ptr;
   1564   } ctx;
   1565 
   1566   VP9_COMP *cpi;
   1567   VP9_COMMON *cm;
   1568 
   1569   cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP));
   1570   // Check that the CPI instance is valid
   1571   if (!cpi)
   1572     return 0;
   1573 
   1574   cm = &cpi->common;
   1575 
   1576   vp9_zero(*cpi);
   1577 
   1578   if (setjmp(cm->error.jmp)) {
   1579     VP9_PTR ptr = ctx.ptr;
   1580 
   1581     ctx.cpi->common.error.setjmp = 0;
   1582     vp9_remove_compressor(&ptr);
   1583     return 0;
   1584   }
   1585 
   1586   cm->error.setjmp = 1;
   1587 
   1588   CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site),
   1589                                              (MAX_MVSEARCH_STEPS * 8) + 1));
   1590 
   1591   vp9_create_common(cm);
   1592 
   1593   init_config((VP9_PTR)cpi, oxcf);
   1594 
   1595   init_pick_mode_context(cpi);
   1596 
   1597   cm->current_video_frame   = 0;
   1598   cpi->kf_overspend_bits            = 0;
   1599   cpi->kf_bitrate_adjustment        = 0;
   1600   cpi->frames_till_gf_update_due    = 0;
   1601   cpi->gf_overspend_bits            = 0;
   1602   cpi->non_gf_bitrate_adjustment    = 0;
   1603 
   1604   // Set reference frame sign bias for ALTREF frame to 1 (for now)
   1605   cm->ref_frame_sign_bias[ALTREF_FRAME] = 1;
   1606 
   1607   cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
   1608 
   1609   cpi->gold_is_last = 0;
   1610   cpi->alt_is_last  = 0;
   1611   cpi->gold_is_alt  = 0;
   1612 
   1613   // Spatial scalability
   1614   cpi->number_spatial_layers = oxcf->ss_number_layers;
   1615 
   1616   // Create the encoder segmentation map and set all entries to 0
   1617   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
   1618                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
   1619 
   1620   // And a place holder structure is the coding context
   1621   // for use if we want to save and restore it
   1622   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
   1623                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
   1624 
   1625   CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1));
   1626   vpx_memset(cpi->active_map, 1, cm->MBs);
   1627   cpi->active_map_enabled = 0;
   1628 
   1629   for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
   1630                    sizeof(cpi->mbgraph_stats[0])); i++) {
   1631     CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
   1632                     vpx_calloc(cm->MBs *
   1633                                sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
   1634   }
   1635 
   1636 #ifdef ENTROPY_STATS
   1637   if (cpi->pass != 1)
   1638     init_context_counters();
   1639 #endif
   1640 
   1641 #ifdef MODE_STATS
   1642   init_tx_count_stats();
   1643   init_switchable_interp_stats();
   1644 #endif
   1645 
   1646   /*Initialize the feed-forward activity masking.*/
   1647   cpi->activity_avg = 90 << 12;
   1648 
   1649   cpi->frames_since_key = 8;  // Sensible default for first frame.
   1650   cpi->key_frame_frequency = cpi->oxcf.key_freq;
   1651   cpi->this_key_frame_forced = 0;
   1652   cpi->next_key_frame_forced = 0;
   1653 
   1654   cpi->source_alt_ref_pending = 0;
   1655   cpi->source_alt_ref_active = 0;
   1656   cpi->refresh_alt_ref_frame = 0;
   1657 
   1658 #if CONFIG_MULTIPLE_ARF
   1659   // Turn multiple ARF usage on/off. This is a quick hack for the initial test
   1660   // version. It should eventually be set via the codec API.
   1661   cpi->multi_arf_enabled = 1;
   1662 
   1663   if (cpi->multi_arf_enabled) {
   1664     cpi->sequence_number = 0;
   1665     cpi->frame_coding_order_period = 0;
   1666     vp9_zero(cpi->frame_coding_order);
   1667     vp9_zero(cpi->arf_buffer_idx);
   1668   }
   1669 #endif
   1670 
   1671   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
   1672 #if CONFIG_INTERNAL_STATS
   1673   cpi->b_calculate_ssimg = 0;
   1674 
   1675   cpi->count = 0;
   1676   cpi->bytes = 0;
   1677 
   1678   if (cpi->b_calculate_psnr) {
   1679     cpi->total_sq_error = 0.0;
   1680     cpi->total_sq_error2 = 0.0;
   1681     cpi->total_y = 0.0;
   1682     cpi->total_u = 0.0;
   1683     cpi->total_v = 0.0;
   1684     cpi->total = 0.0;
   1685     cpi->totalp_y = 0.0;
   1686     cpi->totalp_u = 0.0;
   1687     cpi->totalp_v = 0.0;
   1688     cpi->totalp = 0.0;
   1689     cpi->tot_recode_hits = 0;
   1690     cpi->summed_quality = 0;
   1691     cpi->summed_weights = 0;
   1692     cpi->summedp_quality = 0;
   1693     cpi->summedp_weights = 0;
   1694   }
   1695 
   1696   if (cpi->b_calculate_ssimg) {
   1697     cpi->total_ssimg_y = 0;
   1698     cpi->total_ssimg_u = 0;
   1699     cpi->total_ssimg_v = 0;
   1700     cpi->total_ssimg_all = 0;
   1701   }
   1702 
   1703 #endif
   1704 
   1705   cpi->first_time_stamp_ever = INT64_MAX;
   1706 
   1707   cpi->frames_till_gf_update_due      = 0;
   1708   cpi->key_frame_count              = 1;
   1709 
   1710   cpi->ni_av_qi                     = cpi->oxcf.worst_allowed_q;
   1711   cpi->ni_tot_qi                    = 0;
   1712   cpi->ni_frames                   = 0;
   1713   cpi->tot_q = 0.0;
   1714   cpi->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q);
   1715   cpi->total_byte_count             = 0;
   1716 
   1717   cpi->rate_correction_factor         = 1.0;
   1718   cpi->key_frame_rate_correction_factor = 1.0;
   1719   cpi->gf_rate_correction_factor  = 1.0;
   1720   cpi->twopass.est_max_qcorrection_factor  = 1.0;
   1721 
   1722   cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
   1723   cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
   1724   cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
   1725   cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
   1726   cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
   1727   cal_nmvsadcosts(cpi->mb.nmvsadcost);
   1728 
   1729   cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
   1730   cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
   1731   cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
   1732   cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
   1733   cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
   1734 
   1735   for (i = 0; i < KEY_FRAME_CONTEXT; i++)
   1736     cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
   1737 
   1738 #ifdef OUTPUT_YUV_SRC
   1739   yuv_file = fopen("bd.yuv", "ab");
   1740 #endif
   1741 #ifdef OUTPUT_YUV_REC
   1742   yuv_rec_file = fopen("rec.yuv", "wb");
   1743 #endif
   1744 
   1745 #if 0
   1746   framepsnr = fopen("framepsnr.stt", "a");
   1747   kf_list = fopen("kf_list.stt", "w");
   1748 #endif
   1749 
   1750   cpi->output_pkt_list = oxcf->output_pkt_list;
   1751 
   1752   cpi->enable_encode_breakout = 1;
   1753 
   1754   if (cpi->pass == 1) {
   1755     vp9_init_first_pass(cpi);
   1756   } else if (cpi->pass == 2) {
   1757     size_t packet_sz = sizeof(FIRSTPASS_STATS);
   1758     int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
   1759 
   1760     cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
   1761     cpi->twopass.stats_in = cpi->twopass.stats_in_start;
   1762     cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in
   1763                                          + (packets - 1) * packet_sz);
   1764     vp9_init_second_pass(cpi);
   1765   }
   1766 
   1767   vp9_set_speed_features(cpi);
   1768 
   1769   // Default rd threshold factors for mode selection
   1770   for (i = 0; i < BLOCK_SIZES; ++i) {
   1771     for (j = 0; j < MAX_MODES; ++j)
   1772       cpi->rd_thresh_freq_fact[i][j] = 32;
   1773     for (j = 0; j < MAX_REFS; ++j)
   1774       cpi->rd_thresh_freq_sub8x8[i][j] = 32;
   1775   }
   1776 
   1777 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \
   1778             SDX3F, SDX8F, SDX4DF)\
   1779     cpi->fn_ptr[BT].sdf            = SDF; \
   1780     cpi->fn_ptr[BT].sdaf           = SDAF; \
   1781     cpi->fn_ptr[BT].vf             = VF; \
   1782     cpi->fn_ptr[BT].svf            = SVF; \
   1783     cpi->fn_ptr[BT].svaf           = SVAF; \
   1784     cpi->fn_ptr[BT].svf_halfpix_h  = SVFHH; \
   1785     cpi->fn_ptr[BT].svf_halfpix_v  = SVFHV; \
   1786     cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
   1787     cpi->fn_ptr[BT].sdx3f          = SDX3F; \
   1788     cpi->fn_ptr[BT].sdx8f          = SDX8F; \
   1789     cpi->fn_ptr[BT].sdx4df         = SDX4DF;
   1790 
   1791   BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
   1792       vp9_variance32x16, vp9_sub_pixel_variance32x16,
   1793       vp9_sub_pixel_avg_variance32x16, NULL, NULL,
   1794       NULL, NULL, NULL,
   1795       vp9_sad32x16x4d)
   1796 
   1797   BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
   1798       vp9_variance16x32, vp9_sub_pixel_variance16x32,
   1799       vp9_sub_pixel_avg_variance16x32, NULL, NULL,
   1800       NULL, NULL, NULL,
   1801       vp9_sad16x32x4d)
   1802 
   1803   BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
   1804       vp9_variance64x32, vp9_sub_pixel_variance64x32,
   1805       vp9_sub_pixel_avg_variance64x32, NULL, NULL,
   1806       NULL, NULL, NULL,
   1807       vp9_sad64x32x4d)
   1808 
   1809   BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
   1810       vp9_variance32x64, vp9_sub_pixel_variance32x64,
   1811       vp9_sub_pixel_avg_variance32x64, NULL, NULL,
   1812       NULL, NULL, NULL,
   1813       vp9_sad32x64x4d)
   1814 
   1815   BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
   1816       vp9_variance32x32, vp9_sub_pixel_variance32x32,
   1817       vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h,
   1818       vp9_variance_halfpixvar32x32_v,
   1819       vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8,
   1820       vp9_sad32x32x4d)
   1821 
   1822   BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
   1823       vp9_variance64x64, vp9_sub_pixel_variance64x64,
   1824       vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h,
   1825       vp9_variance_halfpixvar64x64_v,
   1826       vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8,
   1827       vp9_sad64x64x4d)
   1828 
   1829   BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
   1830       vp9_variance16x16, vp9_sub_pixel_variance16x16,
   1831       vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h,
   1832       vp9_variance_halfpixvar16x16_v,
   1833       vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8,
   1834       vp9_sad16x16x4d)
   1835 
   1836   BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
   1837       vp9_variance16x8, vp9_sub_pixel_variance16x8,
   1838       vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL,
   1839       vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
   1840 
   1841   BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
   1842       vp9_variance8x16, vp9_sub_pixel_variance8x16,
   1843       vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL,
   1844       vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
   1845 
   1846   BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
   1847       vp9_variance8x8, vp9_sub_pixel_variance8x8,
   1848       vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL,
   1849       vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
   1850 
   1851   BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
   1852       vp9_variance8x4, vp9_sub_pixel_variance8x4,
   1853       vp9_sub_pixel_avg_variance8x4, NULL, NULL,
   1854       NULL, NULL, vp9_sad8x4x8,
   1855       vp9_sad8x4x4d)
   1856 
   1857   BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
   1858       vp9_variance4x8, vp9_sub_pixel_variance4x8,
   1859       vp9_sub_pixel_avg_variance4x8, NULL, NULL,
   1860       NULL, NULL, vp9_sad4x8x8,
   1861       vp9_sad4x8x4d)
   1862 
   1863   BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
   1864       vp9_variance4x4, vp9_sub_pixel_variance4x4,
   1865       vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL,
   1866       vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
   1867 
   1868   cpi->full_search_sad = vp9_full_search_sad;
   1869   cpi->diamond_search_sad = vp9_diamond_search_sad;
   1870   cpi->refining_search_sad = vp9_refining_search_sad;
   1871 
   1872   // make sure frame 1 is okay
   1873   cpi->error_bins[0] = cpi->common.MBs;
   1874 
   1875   /* vp9_init_quantizer() is first called here. Add check in
   1876    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
   1877    * called later when needed. This will avoid unnecessary calls of
   1878    * vp9_init_quantizer() for every frame.
   1879    */
   1880   vp9_init_quantizer(cpi);
   1881 
   1882   vp9_loop_filter_init(cm);
   1883 
   1884   cpi->common.error.setjmp = 0;
   1885 
   1886   vp9_zero(cpi->y_uv_mode_count);
   1887 
   1888 #ifdef MODE_TEST_HIT_STATS
   1889   vp9_zero(cpi->mode_test_hits);
   1890 #endif
   1891 
   1892   return (VP9_PTR) cpi;
   1893 }
   1894 
   1895 void vp9_remove_compressor(VP9_PTR *ptr) {
   1896   VP9_COMP *cpi = (VP9_COMP *)(*ptr);
   1897   int i;
   1898 
   1899   if (!cpi)
   1900     return;
   1901 
   1902   if (cpi && (cpi->common.current_video_frame > 0)) {
   1903     if (cpi->pass == 2) {
   1904       vp9_end_second_pass(cpi);
   1905     }
   1906 
   1907 #ifdef ENTROPY_STATS
   1908     if (cpi->pass != 1) {
   1909       print_context_counters();
   1910       print_tree_update_probs();
   1911       print_mode_context(cpi);
   1912     }
   1913 #endif
   1914 
   1915 #ifdef MODE_STATS
   1916     if (cpi->pass != 1) {
   1917       write_tx_count_stats();
   1918       write_switchable_interp_stats();
   1919     }
   1920 #endif
   1921 
   1922 #if CONFIG_INTERNAL_STATS
   1923 
   1924     vp9_clear_system_state();
   1925 
   1926     // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
   1927     if (cpi->pass != 1) {
   1928       FILE *f = fopen("opsnr.stt", "a");
   1929       double time_encoded = (cpi->last_end_time_stamp_seen
   1930                              - cpi->first_time_stamp_ever) / 10000000.000;
   1931       double total_encode_time = (cpi->time_receive_data +
   1932                                   cpi->time_compress_data)   / 1000.000;
   1933       double dr = (double)cpi->bytes * (double) 8 / (double)1000
   1934                   / time_encoded;
   1935 
   1936       if (cpi->b_calculate_psnr) {
   1937         YV12_BUFFER_CONFIG *lst_yv12 =
   1938             &cpi->common.yv12_fb[cpi->common.ref_frame_map[cpi->lst_fb_idx]];
   1939         double samples = 3.0 / 2 * cpi->count *
   1940                          lst_yv12->y_width * lst_yv12->y_height;
   1941         double total_psnr = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error);
   1942         double total_psnr2 = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error2);
   1943         double total_ssim = 100 * pow(cpi->summed_quality /
   1944                                       cpi->summed_weights, 8.0);
   1945         double total_ssimp = 100 * pow(cpi->summedp_quality /
   1946                                        cpi->summedp_weights, 8.0);
   1947 
   1948         fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
   1949                 "VPXSSIM\tVPSSIMP\t  Time(ms)\n");
   1950         fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n",
   1951                 dr, cpi->total / cpi->count, total_psnr,
   1952                 cpi->totalp / cpi->count, total_psnr2, total_ssim, total_ssimp,
   1953                 total_encode_time);
   1954       }
   1955 
   1956       if (cpi->b_calculate_ssimg) {
   1957         fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t  Time(ms)\n");
   1958         fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
   1959                 cpi->total_ssimg_y / cpi->count,
   1960                 cpi->total_ssimg_u / cpi->count,
   1961                 cpi->total_ssimg_v / cpi->count,
   1962                 cpi->total_ssimg_all / cpi->count, total_encode_time);
   1963       }
   1964 
   1965       fclose(f);
   1966     }
   1967 
   1968 #endif
   1969 
   1970 #ifdef MODE_TEST_HIT_STATS
   1971     if (cpi->pass != 1) {
   1972       double norm_per_pixel_mode_tests = 0;
   1973       double norm_counts[BLOCK_SIZES];
   1974       int i;
   1975       int sb64_per_frame;
   1976       int norm_factors[BLOCK_SIZES] =
   1977         {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1};
   1978       FILE *f = fopen("mode_hit_stats.stt", "a");
   1979 
   1980       // On average, how many mode tests do we do
   1981       for (i = 0; i < BLOCK_SIZES; ++i) {
   1982         norm_counts[i] = (double)cpi->mode_test_hits[i] /
   1983                          (double)norm_factors[i];
   1984         norm_per_pixel_mode_tests += norm_counts[i];
   1985       }
   1986       // Convert to a number per 64x64 and per frame
   1987       sb64_per_frame = ((cpi->common.height + 63) / 64) *
   1988                        ((cpi->common.width + 63) / 64);
   1989       norm_per_pixel_mode_tests =
   1990         norm_per_pixel_mode_tests /
   1991         (double)(cpi->common.current_video_frame * sb64_per_frame);
   1992 
   1993       fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests);
   1994       fclose(f);
   1995     }
   1996 #endif
   1997 
   1998 #ifdef ENTROPY_STATS
   1999     {
   2000       int i, j, k;
   2001       FILE *fmode = fopen("vp9_modecontext.c", "w");
   2002 
   2003       fprintf(fmode, "\n#include \"vp9_entropymode.h\"\n\n");
   2004       fprintf(fmode, "const unsigned int vp9_kf_default_bmode_counts ");
   2005       fprintf(fmode, "[INTRA_MODES][INTRA_MODES]"
   2006                      "[INTRA_MODES] =\n{\n");
   2007 
   2008       for (i = 0; i < INTRA_MODES; i++) {
   2009         fprintf(fmode, "    { // Above Mode :  %d\n", i);
   2010 
   2011         for (j = 0; j < INTRA_MODES; j++) {
   2012           fprintf(fmode, "        {");
   2013 
   2014           for (k = 0; k < INTRA_MODES; k++) {
   2015             if (!intra_mode_stats[i][j][k])
   2016               fprintf(fmode, " %5d, ", 1);
   2017             else
   2018               fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
   2019           }
   2020 
   2021           fprintf(fmode, "}, // left_mode %d\n", j);
   2022         }
   2023 
   2024         fprintf(fmode, "    },\n");
   2025       }
   2026 
   2027       fprintf(fmode, "};\n");
   2028       fclose(fmode);
   2029     }
   2030 #endif
   2031 
   2032 
   2033 #if defined(SECTIONBITS_OUTPUT)
   2034 
   2035     if (0) {
   2036       int i;
   2037       FILE *f = fopen("tokenbits.stt", "a");
   2038 
   2039       for (i = 0; i < 28; i++)
   2040         fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
   2041 
   2042       fprintf(f, "\n");
   2043       fclose(f);
   2044     }
   2045 
   2046 #endif
   2047 
   2048 #if 0
   2049     {
   2050       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
   2051       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
   2052       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
   2053              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
   2054              cpi->time_compress_data / 1000,
   2055              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
   2056     }
   2057 #endif
   2058   }
   2059 
   2060   free_pick_mode_context(&cpi->mb);
   2061   dealloc_compressor_data(cpi);
   2062   vpx_free(cpi->mb.ss);
   2063   vpx_free(cpi->tok);
   2064 
   2065   for (i = 0; i < sizeof(cpi->mbgraph_stats) /
   2066                   sizeof(cpi->mbgraph_stats[0]); ++i) {
   2067     vpx_free(cpi->mbgraph_stats[i].mb_stats);
   2068   }
   2069 
   2070   vp9_remove_common(&cpi->common);
   2071   vpx_free(cpi);
   2072   *ptr = 0;
   2073 
   2074 #ifdef OUTPUT_YUV_SRC
   2075   fclose(yuv_file);
   2076 #endif
   2077 #ifdef OUTPUT_YUV_REC
   2078   fclose(yuv_rec_file);
   2079 #endif
   2080 
   2081 #if 0
   2082 
   2083   if (keyfile)
   2084     fclose(keyfile);
   2085 
   2086   if (framepsnr)
   2087     fclose(framepsnr);
   2088 
   2089   if (kf_list)
   2090     fclose(kf_list);
   2091 
   2092 #endif
   2093 }
   2094 
   2095 
   2096 static uint64_t calc_plane_error(uint8_t *orig, int orig_stride,
   2097                                  uint8_t *recon, int recon_stride,
   2098                                  unsigned int cols, unsigned int rows) {
   2099   unsigned int row, col;
   2100   uint64_t total_sse = 0;
   2101   int diff;
   2102 
   2103   for (row = 0; row + 16 <= rows; row += 16) {
   2104     for (col = 0; col + 16 <= cols; col += 16) {
   2105       unsigned int sse;
   2106 
   2107       vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
   2108       total_sse += sse;
   2109     }
   2110 
   2111     /* Handle odd-sized width */
   2112     if (col < cols) {
   2113       unsigned int border_row, border_col;
   2114       uint8_t *border_orig = orig;
   2115       uint8_t *border_recon = recon;
   2116 
   2117       for (border_row = 0; border_row < 16; border_row++) {
   2118         for (border_col = col; border_col < cols; border_col++) {
   2119           diff = border_orig[border_col] - border_recon[border_col];
   2120           total_sse += diff * diff;
   2121         }
   2122 
   2123         border_orig += orig_stride;
   2124         border_recon += recon_stride;
   2125       }
   2126     }
   2127 
   2128     orig += orig_stride * 16;
   2129     recon += recon_stride * 16;
   2130   }
   2131 
   2132   /* Handle odd-sized height */
   2133   for (; row < rows; row++) {
   2134     for (col = 0; col < cols; col++) {
   2135       diff = orig[col] - recon[col];
   2136       total_sse += diff * diff;
   2137     }
   2138 
   2139     orig += orig_stride;
   2140     recon += recon_stride;
   2141   }
   2142 
   2143   return total_sse;
   2144 }
   2145 
   2146 
   2147 static void generate_psnr_packet(VP9_COMP *cpi) {
   2148   YV12_BUFFER_CONFIG      *orig = cpi->Source;
   2149   YV12_BUFFER_CONFIG      *recon = cpi->common.frame_to_show;
   2150   struct vpx_codec_cx_pkt  pkt;
   2151   uint64_t                 sse;
   2152   int                      i;
   2153   unsigned int             width = orig->y_crop_width;
   2154   unsigned int             height = orig->y_crop_height;
   2155 
   2156   pkt.kind = VPX_CODEC_PSNR_PKT;
   2157   sse = calc_plane_error(orig->y_buffer, orig->y_stride,
   2158                          recon->y_buffer, recon->y_stride,
   2159                          width, height);
   2160   pkt.data.psnr.sse[0] = sse;
   2161   pkt.data.psnr.sse[1] = sse;
   2162   pkt.data.psnr.samples[0] = width * height;
   2163   pkt.data.psnr.samples[1] = width * height;
   2164 
   2165   width = orig->uv_crop_width;
   2166   height = orig->uv_crop_height;
   2167 
   2168   sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
   2169                          recon->u_buffer, recon->uv_stride,
   2170                          width, height);
   2171   pkt.data.psnr.sse[0] += sse;
   2172   pkt.data.psnr.sse[2] = sse;
   2173   pkt.data.psnr.samples[0] += width * height;
   2174   pkt.data.psnr.samples[2] = width * height;
   2175 
   2176   sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
   2177                          recon->v_buffer, recon->uv_stride,
   2178                          width, height);
   2179   pkt.data.psnr.sse[0] += sse;
   2180   pkt.data.psnr.sse[3] = sse;
   2181   pkt.data.psnr.samples[0] += width * height;
   2182   pkt.data.psnr.samples[3] = width * height;
   2183 
   2184   for (i = 0; i < 4; i++)
   2185     pkt.data.psnr.psnr[i] = vp9_mse2psnr(pkt.data.psnr.samples[i], 255.0,
   2186                                          (double)pkt.data.psnr.sse[i]);
   2187 
   2188   vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
   2189 }
   2190 
   2191 
   2192 int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) {
   2193   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   2194 
   2195   if (ref_frame_flags > 7)
   2196     return -1;
   2197 
   2198   cpi->ref_frame_flags = ref_frame_flags;
   2199   return 0;
   2200 }
   2201 int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) {
   2202   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   2203 
   2204   if (ref_frame_flags > 7)
   2205     return -1;
   2206 
   2207   cpi->refresh_golden_frame = 0;
   2208   cpi->refresh_alt_ref_frame = 0;
   2209   cpi->refresh_last_frame   = 0;
   2210 
   2211   if (ref_frame_flags & VP9_LAST_FLAG)
   2212     cpi->refresh_last_frame = 1;
   2213 
   2214   if (ref_frame_flags & VP9_GOLD_FLAG)
   2215     cpi->refresh_golden_frame = 1;
   2216 
   2217   if (ref_frame_flags & VP9_ALT_FLAG)
   2218     cpi->refresh_alt_ref_frame = 1;
   2219 
   2220   return 0;
   2221 }
   2222 
   2223 int vp9_copy_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
   2224                            YV12_BUFFER_CONFIG *sd) {
   2225   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   2226   VP9_COMMON *cm = &cpi->common;
   2227   int ref_fb_idx;
   2228 
   2229   if (ref_frame_flag == VP9_LAST_FLAG)
   2230     ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx];
   2231   else if (ref_frame_flag == VP9_GOLD_FLAG)
   2232     ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx];
   2233   else if (ref_frame_flag == VP9_ALT_FLAG)
   2234     ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx];
   2235   else
   2236     return -1;
   2237 
   2238   vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
   2239 
   2240   return 0;
   2241 }
   2242 
   2243 int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) {
   2244   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   2245   VP9_COMMON *cm = &cpi->common;
   2246 
   2247   if (index < 0 || index >= NUM_REF_FRAMES)
   2248     return -1;
   2249 
   2250   *fb = &cm->yv12_fb[cm->ref_frame_map[index]];
   2251   return 0;
   2252 }
   2253 
   2254 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
   2255                           YV12_BUFFER_CONFIG *sd) {
   2256   VP9_COMP *cpi = (VP9_COMP *)(ptr);
   2257   VP9_COMMON *cm = &cpi->common;
   2258 
   2259   int ref_fb_idx;
   2260 
   2261   if (ref_frame_flag == VP9_LAST_FLAG)
   2262     ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx];
   2263   else if (ref_frame_flag == VP9_GOLD_FLAG)
   2264     ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx];
   2265   else if (ref_frame_flag == VP9_ALT_FLAG)
   2266     ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx];
   2267   else
   2268     return -1;
   2269 
   2270   vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
   2271 
   2272   return 0;
   2273 }
   2274 int vp9_update_entropy(VP9_PTR comp, int update) {
   2275   ((VP9_COMP *)comp)->common.refresh_frame_context = update;
   2276   return 0;
   2277 }
   2278 
   2279 
   2280 #ifdef OUTPUT_YUV_SRC
   2281 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) {
   2282   uint8_t *src = s->y_buffer;
   2283   int h = s->y_height;
   2284 
   2285   do {
   2286     fwrite(src, s->y_width, 1,  yuv_file);
   2287     src += s->y_stride;
   2288   } while (--h);
   2289 
   2290   src = s->u_buffer;
   2291   h = s->uv_height;
   2292 
   2293   do {
   2294     fwrite(src, s->uv_width, 1,  yuv_file);
   2295     src += s->uv_stride;
   2296   } while (--h);
   2297 
   2298   src = s->v_buffer;
   2299   h = s->uv_height;
   2300 
   2301   do {
   2302     fwrite(src, s->uv_width, 1, yuv_file);
   2303     src += s->uv_stride;
   2304   } while (--h);
   2305 }
   2306 #endif
   2307 
   2308 #ifdef OUTPUT_YUV_REC
   2309 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
   2310   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
   2311   uint8_t *src = s->y_buffer;
   2312   int h = cm->height;
   2313 
   2314   do {
   2315     fwrite(src, s->y_width, 1,  yuv_rec_file);
   2316     src += s->y_stride;
   2317   } while (--h);
   2318 
   2319   src = s->u_buffer;
   2320   h = s->uv_height;
   2321 
   2322   do {
   2323     fwrite(src, s->uv_width, 1,  yuv_rec_file);
   2324     src += s->uv_stride;
   2325   } while (--h);
   2326 
   2327   src = s->v_buffer;
   2328   h = s->uv_height;
   2329 
   2330   do {
   2331     fwrite(src, s->uv_width, 1, yuv_rec_file);
   2332     src += s->uv_stride;
   2333   } while (--h);
   2334 
   2335 #if CONFIG_ALPHA
   2336   if (s->alpha_buffer) {
   2337     src = s->alpha_buffer;
   2338     h = s->alpha_height;
   2339     do {
   2340       fwrite(src, s->alpha_width, 1,  yuv_rec_file);
   2341       src += s->alpha_stride;
   2342     } while (--h);
   2343   }
   2344 #endif
   2345 
   2346   fflush(yuv_rec_file);
   2347 }
   2348 #endif
   2349 
   2350 static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb,
   2351                                    YV12_BUFFER_CONFIG *dst_fb) {
   2352   const int in_w = src_fb->y_crop_width;
   2353   const int in_h = src_fb->y_crop_height;
   2354   const int out_w = dst_fb->y_crop_width;
   2355   const int out_h = dst_fb->y_crop_height;
   2356   int x, y, i;
   2357 
   2358   uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
   2359                       src_fb->alpha_buffer};
   2360   int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
   2361                         src_fb->alpha_stride};
   2362 
   2363   uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
   2364                       dst_fb->alpha_buffer};
   2365   int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
   2366                         dst_fb->alpha_stride};
   2367 
   2368   for (y = 0; y < out_h; y += 16) {
   2369     for (x = 0; x < out_w; x += 16) {
   2370       for (i = 0; i < MAX_MB_PLANE; ++i) {
   2371         const int factor = i == 0 ? 1 : 2;
   2372         const int x_q4 = x * (16 / factor) * in_w / out_w;
   2373         const int y_q4 = y * (16 / factor) * in_h / out_h;
   2374         const int src_stride = src_strides[i];
   2375         const int dst_stride = dst_strides[i];
   2376         uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride +
   2377                                  x / factor * in_w / out_w;
   2378         uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor;
   2379 
   2380         vp9_convolve8(src, src_stride, dst, dst_stride,
   2381                       vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w,
   2382                       vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h,
   2383                       16 / factor, 16 / factor);
   2384       }
   2385     }
   2386   }
   2387 
   2388   vp8_yv12_extend_frame_borders(dst_fb);
   2389 }
   2390 
   2391 
   2392 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
   2393   // this frame refreshes means next frames don't unless specified by user
   2394   cpi->frames_since_golden = 0;
   2395 
   2396 #if CONFIG_MULTIPLE_ARF
   2397   if (!cpi->multi_arf_enabled)
   2398 #endif
   2399     // Clear the alternate reference update pending flag.
   2400     cpi->source_alt_ref_pending = 0;
   2401 
   2402   // Set the alternate reference frame active flag
   2403   cpi->source_alt_ref_active = 1;
   2404 }
   2405 static void update_golden_frame_stats(VP9_COMP *cpi) {
   2406   // Update the Golden frame usage counts.
   2407   if (cpi->refresh_golden_frame) {
   2408     // this frame refreshes means next frames don't unless specified by user
   2409     cpi->refresh_golden_frame = 0;
   2410     cpi->frames_since_golden = 0;
   2411 
   2412     // ******** Fixed Q test code only ************
   2413     // If we are going to use the ALT reference for the next group of frames
   2414     // set a flag to say so.
   2415     if (cpi->oxcf.fixed_q >= 0 &&
   2416         cpi->oxcf.play_alternate && !cpi->refresh_alt_ref_frame) {
   2417       cpi->source_alt_ref_pending = 1;
   2418       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
   2419 
   2420       // TODO(ivan): For SVC encoder, GF automatic update is disabled by using
   2421       // a large GF_interval.
   2422       if (cpi->use_svc) {
   2423         cpi->frames_till_gf_update_due = INT_MAX;
   2424       }
   2425     }
   2426 
   2427     if (!cpi->source_alt_ref_pending)
   2428       cpi->source_alt_ref_active = 0;
   2429 
   2430     // Decrement count down till next gf
   2431     if (cpi->frames_till_gf_update_due > 0)
   2432       cpi->frames_till_gf_update_due--;
   2433 
   2434   } else if (!cpi->refresh_alt_ref_frame) {
   2435     // Decrement count down till next gf
   2436     if (cpi->frames_till_gf_update_due > 0)
   2437       cpi->frames_till_gf_update_due--;
   2438 
   2439     if (cpi->frames_till_alt_ref_frame)
   2440       cpi->frames_till_alt_ref_frame--;
   2441 
   2442     cpi->frames_since_golden++;
   2443   }
   2444 }
   2445 
   2446 static int find_fp_qindex() {
   2447   int i;
   2448 
   2449   for (i = 0; i < QINDEX_RANGE; i++) {
   2450     if (vp9_convert_qindex_to_q(i) >= 30.0) {
   2451       break;
   2452     }
   2453   }
   2454 
   2455   if (i == QINDEX_RANGE)
   2456     i--;
   2457 
   2458   return i;
   2459 }
   2460 
   2461 static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest,
   2462                         unsigned int *frame_flags) {
   2463   (void) size;
   2464   (void) dest;
   2465   (void) frame_flags;
   2466 
   2467   vp9_set_quantizer(cpi, find_fp_qindex());
   2468   vp9_first_pass(cpi);
   2469 }
   2470 
   2471 #define WRITE_RECON_BUFFER 0
   2472 #if WRITE_RECON_BUFFER
   2473 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
   2474   FILE *yframe;
   2475   int i;
   2476   char filename[255];
   2477 
   2478   snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame);
   2479   yframe = fopen(filename, "wb");
   2480 
   2481   for (i = 0; i < frame->y_height; i++)
   2482     fwrite(frame->y_buffer + i * frame->y_stride,
   2483            frame->y_width, 1, yframe);
   2484 
   2485   fclose(yframe);
   2486   snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame);
   2487   yframe = fopen(filename, "wb");
   2488 
   2489   for (i = 0; i < frame->uv_height; i++)
   2490     fwrite(frame->u_buffer + i * frame->uv_stride,
   2491            frame->uv_width, 1, yframe);
   2492 
   2493   fclose(yframe);
   2494   snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame);
   2495   yframe = fopen(filename, "wb");
   2496 
   2497   for (i = 0; i < frame->uv_height; i++)
   2498     fwrite(frame->v_buffer + i * frame->uv_stride,
   2499            frame->uv_width, 1, yframe);
   2500 
   2501   fclose(yframe);
   2502 }
   2503 #endif
   2504 
   2505 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) {
   2506 #define EDGE_THRESH 128
   2507   int i, j;
   2508   int num_edge_pels = 0;
   2509   int num_pels = (frame->y_height - 2) * (frame->y_width - 2);
   2510   uint8_t *prev = frame->y_buffer + 1;
   2511   uint8_t *curr = frame->y_buffer + 1 + frame->y_stride;
   2512   uint8_t *next = frame->y_buffer + 1 + 2 * frame->y_stride;
   2513   for (i = 1; i < frame->y_height - 1; i++) {
   2514     for (j = 1; j < frame->y_width - 1; j++) {
   2515       /* Sobel hor and ver gradients */
   2516       int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) +
   2517               (next[1] - next[-1]);
   2518       int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) +
   2519               (prev[-1] - next[-1]);
   2520       h = (h < 0 ? -h : h);
   2521       v = (v < 0 ? -v : v);
   2522       if (h > EDGE_THRESH || v > EDGE_THRESH)
   2523         num_edge_pels++;
   2524       curr++;
   2525       prev++;
   2526       next++;
   2527     }
   2528     curr += frame->y_stride - frame->y_width + 2;
   2529     prev += frame->y_stride - frame->y_width + 2;
   2530     next += frame->y_stride - frame->y_width + 2;
   2531   }
   2532   return (double)num_edge_pels / num_pels;
   2533 }
   2534 
   2535 // Function to test for conditions that indicate we should loop
   2536 // back and recode a frame.
   2537 static int recode_loop_test(VP9_COMP *cpi,
   2538                             int high_limit, int low_limit,
   2539                             int q, int maxq, int minq) {
   2540   int force_recode = 0;
   2541   VP9_COMMON *cm = &cpi->common;
   2542 
   2543   // Is frame recode allowed at all
   2544   // Yes if either recode mode 1 is selected or mode two is selected
   2545   // and the frame is a key frame. golden frame or alt_ref_frame
   2546   if ((cpi->sf.recode_loop == 1) ||
   2547       ((cpi->sf.recode_loop == 2) &&
   2548        ((cm->frame_type == KEY_FRAME) ||
   2549         cpi->refresh_golden_frame ||
   2550         cpi->refresh_alt_ref_frame))) {
   2551     // General over and under shoot tests
   2552     if (((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
   2553         ((cpi->projected_frame_size < low_limit) && (q > minq))) {
   2554       force_recode = 1;
   2555     } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
   2556       // Deal with frame undershoot and whether or not we are
   2557       // below the automatically set cq level.
   2558       if (q > cpi->cq_target_quality &&
   2559           cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3)) {
   2560         force_recode = 1;
   2561       } else if (q > cpi->oxcf.cq_level &&
   2562                  cpi->projected_frame_size < cpi->min_frame_bandwidth &&
   2563                  cpi->active_best_quality > cpi->oxcf.cq_level) {
   2564         // Severe undershoot and between auto and user cq level
   2565         force_recode = 1;
   2566         cpi->active_best_quality = cpi->oxcf.cq_level;
   2567       }
   2568     }
   2569   }
   2570 
   2571   return force_recode;
   2572 }
   2573 
   2574 static void update_reference_frames(VP9_COMP * const cpi) {
   2575   VP9_COMMON * const cm = &cpi->common;
   2576 
   2577   // At this point the new frame has been encoded.
   2578   // If any buffer copy / swapping is signaled it should be done here.
   2579   if (cm->frame_type == KEY_FRAME) {
   2580     ref_cnt_fb(cm->fb_idx_ref_cnt,
   2581                &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
   2582     ref_cnt_fb(cm->fb_idx_ref_cnt,
   2583                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
   2584   }
   2585 #if CONFIG_MULTIPLE_ARF
   2586   else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
   2587       !cpi->refresh_alt_ref_frame) {
   2588 #else
   2589   else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame &&
   2590            !cpi->use_svc) {
   2591 #endif
   2592     /* Preserve the previously existing golden frame and update the frame in
   2593      * the alt ref slot instead. This is highly specific to the current use of
   2594      * alt-ref as a forward reference, and this needs to be generalized as
   2595      * other uses are implemented (like RTC/temporal scaling)
   2596      *
   2597      * The update to the buffer in the alt ref slot was signaled in
   2598      * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
   2599      * as the golden frame next time.
   2600      */
   2601     int tmp;
   2602 
   2603     ref_cnt_fb(cm->fb_idx_ref_cnt,
   2604                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
   2605 
   2606     tmp = cpi->alt_fb_idx;
   2607     cpi->alt_fb_idx = cpi->gld_fb_idx;
   2608     cpi->gld_fb_idx = tmp;
   2609   }  else { /* For non key/golden frames */
   2610     if (cpi->refresh_alt_ref_frame) {
   2611       int arf_idx = cpi->alt_fb_idx;
   2612 #if CONFIG_MULTIPLE_ARF
   2613       if (cpi->multi_arf_enabled) {
   2614         arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1];
   2615       }
   2616 #endif
   2617       ref_cnt_fb(cm->fb_idx_ref_cnt,
   2618                  &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
   2619     }
   2620 
   2621     if (cpi->refresh_golden_frame) {
   2622       ref_cnt_fb(cm->fb_idx_ref_cnt,
   2623                  &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
   2624     }
   2625   }
   2626 
   2627   if (cpi->refresh_last_frame) {
   2628     ref_cnt_fb(cm->fb_idx_ref_cnt,
   2629                &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
   2630   }
   2631 }
   2632 
   2633 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
   2634   MACROBLOCKD *xd = &cpi->mb.e_mbd;
   2635   struct loopfilter *lf = &cm->lf;
   2636   if (xd->lossless) {
   2637       lf->filter_level = 0;
   2638   } else {
   2639     struct vpx_usec_timer timer;
   2640 
   2641     vp9_clear_system_state();
   2642 
   2643     vpx_usec_timer_start(&timer);
   2644 
   2645     vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.use_fast_lpf_pick);
   2646 
   2647     vpx_usec_timer_mark(&timer);
   2648     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
   2649   }
   2650 
   2651   if (lf->filter_level > 0) {
   2652     vp9_set_alt_lf_level(cpi, lf->filter_level);
   2653     vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0);
   2654   }
   2655 
   2656   vp9_extend_frame_inner_borders(cm->frame_to_show,
   2657                                  cm->subsampling_x, cm->subsampling_y);
   2658 }
   2659 
   2660 static void scale_references(VP9_COMP *cpi) {
   2661   VP9_COMMON *cm = &cpi->common;
   2662   int i;
   2663   int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx,
   2664                                       cpi->alt_fb_idx};
   2665 
   2666   for (i = 0; i < 3; i++) {
   2667     YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[refs[i]]];
   2668 
   2669     if (ref->y_crop_width != cm->width ||
   2670         ref->y_crop_height != cm->height) {
   2671       int new_fb = get_free_fb(cm);
   2672 
   2673       vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb],
   2674                                cm->width, cm->height,
   2675                                cm->subsampling_x, cm->subsampling_y,
   2676                                VP9BORDERINPIXELS);
   2677       scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]);
   2678       cpi->scaled_ref_idx[i] = new_fb;
   2679     } else {
   2680       cpi->scaled_ref_idx[i] = cm->ref_frame_map[refs[i]];
   2681       cm->fb_idx_ref_cnt[cm->ref_frame_map[refs[i]]]++;
   2682     }
   2683   }
   2684 }
   2685 
   2686 static void release_scaled_references(VP9_COMP *cpi) {
   2687   VP9_COMMON *cm = &cpi->common;
   2688   int i;
   2689 
   2690   for (i = 0; i < 3; i++)
   2691     cm->fb_idx_ref_cnt[cpi->scaled_ref_idx[i]]--;
   2692 }
   2693 
   2694 static void full_to_model_count(unsigned int *model_count,
   2695                                 unsigned int *full_count) {
   2696   int n;
   2697   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
   2698   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
   2699   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
   2700   for (n = THREE_TOKEN; n < DCT_EOB_TOKEN; ++n)
   2701     model_count[TWO_TOKEN] += full_count[n];
   2702   model_count[DCT_EOB_MODEL_TOKEN] = full_count[DCT_EOB_TOKEN];
   2703 }
   2704 
   2705 static void full_to_model_counts(
   2706     vp9_coeff_count_model *model_count, vp9_coeff_count *full_count) {
   2707   int i, j, k, l;
   2708   for (i = 0; i < BLOCK_TYPES; ++i)
   2709     for (j = 0; j < REF_TYPES; ++j)
   2710       for (k = 0; k < COEF_BANDS; ++k)
   2711         for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
   2712           if (l >= 3 && k == 0)
   2713             continue;
   2714           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
   2715         }
   2716 }
   2717 
   2718 #if 0 && CONFIG_INTERNAL_STATS
   2719 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
   2720   VP9_COMMON *const cm = &cpi->common;
   2721   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
   2722   int recon_err;
   2723 
   2724   vp9_clear_system_state();  // __asm emms;
   2725 
   2726   recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
   2727 
   2728   if (cpi->twopass.total_left_stats.coded_error != 0.0)
   2729     fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d"
   2730         "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"
   2731         "%6d %6d %5d %5d %5d %8.2f %10d %10.3f"
   2732         "%10.3f %8d %10d %10d %10d\n",
   2733         cpi->common.current_video_frame, cpi->this_frame_target,
   2734         cpi->projected_frame_size, 0,
   2735         (cpi->projected_frame_size - cpi->this_frame_target),
   2736         (int)cpi->total_target_vs_actual,
   2737         (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target),
   2738         (int)cpi->total_actual_bits, cm->base_qindex,
   2739         vp9_convert_qindex_to_q(cm->base_qindex),
   2740         (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
   2741         vp9_convert_qindex_to_q(cpi->active_best_quality),
   2742         vp9_convert_qindex_to_q(cpi->active_worst_quality), cpi->avg_q,
   2743         vp9_convert_qindex_to_q(cpi->ni_av_qi),
   2744         vp9_convert_qindex_to_q(cpi->cq_target_quality),
   2745         cpi->refresh_last_frame, cpi->refresh_golden_frame,
   2746         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->gfu_boost,
   2747         cpi->twopass.est_max_qcorrection_factor, (int)cpi->twopass.bits_left,
   2748         cpi->twopass.total_left_stats.coded_error,
   2749         (double)cpi->twopass.bits_left /
   2750             (1 + cpi->twopass.total_left_stats.coded_error),
   2751         cpi->tot_recode_hits, recon_err, cpi->kf_boost, cpi->kf_zeromotion_pct);
   2752 
   2753   fclose(f);
   2754 
   2755   if (0) {
   2756     FILE *const fmodes = fopen("Modes.stt", "a");
   2757     int i;
   2758 
   2759     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
   2760             cm->frame_type, cpi->refresh_golden_frame,
   2761             cpi->refresh_alt_ref_frame);
   2762 
   2763     for (i = 0; i < MAX_MODES; ++i)
   2764       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
   2765     for (i = 0; i < MAX_REFS; ++i)
   2766       fprintf(fmodes, "%5d ", cpi->sub8x8_mode_chosen_counts[i]);
   2767 
   2768     fprintf(fmodes, "\n");
   2769 
   2770     fclose(fmodes);
   2771   }
   2772 }
   2773 #endif
   2774 
   2775 static int pick_q_and_adjust_q_bounds(VP9_COMP *cpi,
   2776                                       int * bottom_index, int * top_index) {
   2777   // Set an active best quality and if necessary active worst quality
   2778   int q = cpi->active_worst_quality;
   2779   VP9_COMMON *const cm = &cpi->common;
   2780 
   2781   if (frame_is_intra_only(cm)) {
   2782 #if !CONFIG_MULTIPLE_ARF
   2783     // Handle the special case for key frames forced when we have75 reached
   2784     // the maximum key frame interval. Here force the Q to a range
   2785     // based on the ambient Q to reduce the risk of popping.
   2786     if (cpi->this_key_frame_forced) {
   2787       int delta_qindex;
   2788       int qindex = cpi->last_boosted_qindex;
   2789       double last_boosted_q = vp9_convert_qindex_to_q(qindex);
   2790 
   2791       delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q,
   2792                                         (last_boosted_q * 0.75));
   2793 
   2794       cpi->active_best_quality = MAX(qindex + delta_qindex,
   2795                                      cpi->best_quality);
   2796     } else {
   2797       int high = 5000;
   2798       int low = 400;
   2799       double q_adj_factor = 1.0;
   2800       double q_val;
   2801 
   2802       // Baseline value derived from cpi->active_worst_quality and kf boost
   2803       cpi->active_best_quality = get_active_quality(q, cpi->kf_boost,
   2804                                                     low, high,
   2805                                                     kf_low_motion_minq,
   2806                                                     kf_high_motion_minq);
   2807 
   2808       // Allow somewhat lower kf minq with small image formats.
   2809       if ((cm->width * cm->height) <= (352 * 288)) {
   2810         q_adj_factor -= 0.25;
   2811       }
   2812 
   2813       // Make a further adjustment based on the kf zero motion measure.
   2814       q_adj_factor += 0.05 - (0.001 * (double)cpi->kf_zeromotion_pct);
   2815 
   2816       // Convert the adjustment factor to a qindex delta
   2817       // on active_best_quality.
   2818       q_val = vp9_convert_qindex_to_q(cpi->active_best_quality);
   2819       cpi->active_best_quality +=
   2820           vp9_compute_qdelta(cpi, q_val, (q_val * q_adj_factor));
   2821     }
   2822 #else
   2823     double current_q;
   2824     // Force the KF quantizer to be 30% of the active_worst_quality.
   2825     current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality);
   2826     cpi->active_best_quality = cpi->active_worst_quality
   2827         + vp9_compute_qdelta(cpi, current_q, current_q * 0.3);
   2828 #endif
   2829   } else if (!cpi->is_src_frame_alt_ref &&
   2830              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
   2831     int high = 2000;
   2832     int low = 400;
   2833 
   2834     // Use the lower of cpi->active_worst_quality and recent
   2835     // average Q as basis for GF/ARF best Q limit unless last frame was
   2836     // a key frame.
   2837     if (cpi->frames_since_key > 1 &&
   2838         cpi->avg_frame_qindex < cpi->active_worst_quality) {
   2839       q = cpi->avg_frame_qindex;
   2840     }
   2841     // For constrained quality dont allow Q less than the cq level
   2842     if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
   2843       if (q < cpi->cq_target_quality)
   2844         q = cpi->cq_target_quality;
   2845       if (cpi->frames_since_key > 1) {
   2846         cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost,
   2847                                                       low, high,
   2848                                                       afq_low_motion_minq,
   2849                                                       afq_high_motion_minq);
   2850       } else {
   2851         cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost,
   2852                                                       low, high,
   2853                                                       gf_low_motion_minq,
   2854                                                       gf_high_motion_minq);
   2855       }
   2856       // Constrained quality use slightly lower active best.
   2857       cpi->active_best_quality = cpi->active_best_quality * 15 / 16;
   2858 
   2859     } else if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
   2860       if (!cpi->refresh_alt_ref_frame) {
   2861         cpi->active_best_quality = cpi->cq_target_quality;
   2862       } else {
   2863         if (cpi->frames_since_key > 1) {
   2864           cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost,
   2865                                                         low, high,
   2866                                                         afq_low_motion_minq,
   2867                                                         afq_high_motion_minq);
   2868         } else {
   2869           cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost,
   2870                                                         low, high,
   2871                                                         gf_low_motion_minq,
   2872                                                         gf_high_motion_minq);
   2873         }
   2874       }
   2875     } else {
   2876         cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost,
   2877                                                       low, high,
   2878                                                       gf_low_motion_minq,
   2879                                                       gf_high_motion_minq);
   2880     }
   2881   } else {
   2882     if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
   2883       cpi->active_best_quality = cpi->cq_target_quality;
   2884     } else {
   2885       cpi->active_best_quality = inter_minq[q];
   2886       // 1-pass: for now, use the average Q for the active_best, if its lower
   2887       // than active_worst.
   2888       if (cpi->pass == 0 && (cpi->avg_frame_qindex < q))
   2889         cpi->active_best_quality = inter_minq[cpi->avg_frame_qindex];
   2890 
   2891       // For the constrained quality mode we don't want
   2892       // q to fall below the cq level.
   2893       if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
   2894           (cpi->active_best_quality < cpi->cq_target_quality)) {
   2895         // If we are strongly undershooting the target rate in the last
   2896         // frames then use the user passed in cq value not the auto
   2897         // cq value.
   2898         if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth)
   2899           cpi->active_best_quality = cpi->oxcf.cq_level;
   2900         else
   2901           cpi->active_best_quality = cpi->cq_target_quality;
   2902       }
   2903     }
   2904   }
   2905 
   2906   // Clip the active best and worst quality values to limits
   2907   if (cpi->active_worst_quality > cpi->worst_quality)
   2908     cpi->active_worst_quality = cpi->worst_quality;
   2909 
   2910   if (cpi->active_best_quality < cpi->best_quality)
   2911     cpi->active_best_quality = cpi->best_quality;
   2912 
   2913   if (cpi->active_best_quality > cpi->worst_quality)
   2914     cpi->active_best_quality = cpi->worst_quality;
   2915 
   2916   if (cpi->active_worst_quality < cpi->active_best_quality)
   2917     cpi->active_worst_quality = cpi->active_best_quality;
   2918 
   2919   // Limit Q range for the adaptive loop.
   2920   if (cm->frame_type == KEY_FRAME && !cpi->this_key_frame_forced) {
   2921     *top_index =
   2922       (cpi->active_worst_quality + cpi->active_best_quality * 3) / 4;
   2923     // If this is the first (key) frame in 1-pass, active best is the user
   2924     // best-allowed, and leave the top_index to active_worst.
   2925     if (cpi->pass == 0 && cpi->common.current_video_frame == 0) {
   2926       cpi->active_best_quality = cpi->oxcf.best_allowed_q;
   2927       *top_index = cpi->oxcf.worst_allowed_q;
   2928     }
   2929   } else if (!cpi->is_src_frame_alt_ref &&
   2930              (cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER) &&
   2931              (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
   2932     *top_index =
   2933       (cpi->active_worst_quality + cpi->active_best_quality) / 2;
   2934   } else {
   2935     *top_index = cpi->active_worst_quality;
   2936   }
   2937   *bottom_index = cpi->active_best_quality;
   2938 
   2939   if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
   2940     q = cpi->active_best_quality;
   2941   // Special case code to try and match quality with forced key frames
   2942   } else if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
   2943     q = cpi->last_boosted_qindex;
   2944   } else {
   2945     // Determine initial Q to try.
   2946     if (cpi->pass == 0) {
   2947       // 1-pass: for now, use per-frame-bw for target size of frame, scaled
   2948       // by |x| for key frame.
   2949       int scale = (cm->frame_type == KEY_FRAME) ? 5 : 1;
   2950       q = vp9_regulate_q(cpi, scale * cpi->av_per_frame_bandwidth);
   2951     } else {
   2952       q = vp9_regulate_q(cpi, cpi->this_frame_target);
   2953     }
   2954     if (q > *top_index)
   2955       q = *top_index;
   2956   }
   2957 
   2958   return q;
   2959 }
   2960 static void encode_frame_to_data_rate(VP9_COMP *cpi,
   2961                                       unsigned long *size,
   2962                                       unsigned char *dest,
   2963                                       unsigned int *frame_flags) {
   2964   VP9_COMMON *const cm = &cpi->common;
   2965   TX_SIZE t;
   2966   int q;
   2967   int frame_over_shoot_limit;
   2968   int frame_under_shoot_limit;
   2969 
   2970   int loop = 0;
   2971   int loop_count;
   2972 
   2973   int q_low;
   2974   int q_high;
   2975 
   2976   int top_index;
   2977   int bottom_index;
   2978   int active_worst_qchanged = 0;
   2979 
   2980   int overshoot_seen = 0;
   2981   int undershoot_seen = 0;
   2982 
   2983   SPEED_FEATURES *const sf = &cpi->sf;
   2984   unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height);
   2985   struct segmentation *const seg = &cm->seg;
   2986 
   2987   /* Scale the source buffer, if required. */
   2988   if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width ||
   2989       cm->mi_rows * 8 != cpi->un_scaled_source->y_height) {
   2990     scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source);
   2991     cpi->Source = &cpi->scaled_source;
   2992   } else {
   2993     cpi->Source = cpi->un_scaled_source;
   2994   }
   2995   scale_references(cpi);
   2996 
   2997   // Clear down mmx registers to allow floating point in what follows.
   2998   vp9_clear_system_state();
   2999 
   3000   // For an alt ref frame in 2 pass we skip the call to the second
   3001   // pass function that sets the target bandwidth so we must set it here.
   3002   if (cpi->refresh_alt_ref_frame) {
   3003     // Set a per frame bit target for the alt ref frame.
   3004     cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
   3005     // Set a per second target bitrate.
   3006     cpi->target_bandwidth = (int)(cpi->twopass.gf_bits * cpi->output_framerate);
   3007   }
   3008 
   3009   // Clear zbin over-quant value and mode boost values.
   3010   cpi->zbin_mode_boost = 0;
   3011 
   3012   // Enable or disable mode based tweaking of the zbin.
   3013   // For 2 pass only used where GF/ARF prediction quality
   3014   // is above a threshold.
   3015   cpi->zbin_mode_boost = 0;
   3016   cpi->zbin_mode_boost_enabled = 0;
   3017 
   3018   // Current default encoder behavior for the altref sign bias.
   3019   cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = cpi->source_alt_ref_active;
   3020 
   3021   // Check to see if a key frame is signaled.
   3022   // For two pass with auto key frame enabled cm->frame_type may already be
   3023   // set, but not for one pass.
   3024   if ((cm->current_video_frame == 0) ||
   3025       (cm->frame_flags & FRAMEFLAGS_KEY) ||
   3026       (cpi->oxcf.auto_key && (cpi->frames_since_key %
   3027                               cpi->key_frame_frequency == 0))) {
   3028     // Set frame type to key frame for the force key frame, if we exceed the
   3029     // maximum distance in an automatic keyframe selection or for the first
   3030     // frame.
   3031     cm->frame_type = KEY_FRAME;
   3032   }
   3033 
   3034   // Set default state for segment based loop filter update flags.
   3035   cm->lf.mode_ref_delta_update = 0;
   3036 
   3037   // Initialize cpi->mv_step_param to default based on max resolution.
   3038   cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def);
   3039   // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
   3040   if (sf->auto_mv_step_size) {
   3041     if (frame_is_intra_only(&cpi->common)) {
   3042       // Initialize max_mv_magnitude for use in the first INTER frame
   3043       // after a key/intra-only frame.
   3044       cpi->max_mv_magnitude = max_mv_def;
   3045     } else {
   3046       if (cm->show_frame)
   3047         // Allow mv_steps to correspond to twice the max mv magnitude found
   3048         // in the previous frame, capped by the default max_mv_magnitude based
   3049         // on resolution.
   3050         cpi->mv_step_param = vp9_init_search_range(
   3051             cpi, MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
   3052       cpi->max_mv_magnitude = 0;
   3053     }
   3054   }
   3055 
   3056   // Set various flags etc to special state if it is a key frame.
   3057   if (frame_is_intra_only(cm)) {
   3058     vp9_setup_key_frame(cpi);
   3059     // Reset the loop filter deltas and segmentation map.
   3060     setup_features(cm);
   3061 
   3062     // If segmentation is enabled force a map update for key frames.
   3063     if (seg->enabled) {
   3064       seg->update_map = 1;
   3065       seg->update_data = 1;
   3066     }
   3067 
   3068     // The alternate reference frame cannot be active for a key frame.
   3069     cpi->source_alt_ref_active = 0;
   3070 
   3071     cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
   3072     cm->frame_parallel_decoding_mode =
   3073       (cpi->oxcf.frame_parallel_decoding_mode != 0);
   3074     if (cm->error_resilient_mode) {
   3075       cm->frame_parallel_decoding_mode = 1;
   3076       cm->reset_frame_context = 0;
   3077       cm->refresh_frame_context = 0;
   3078     } else if (cm->intra_only) {
   3079       // Only reset the current context.
   3080       cm->reset_frame_context = 2;
   3081     }
   3082   }
   3083 
   3084   // Configure experimental use of segmentation for enhanced coding of
   3085   // static regions if indicated.
   3086   // Only allowed in second pass of two pass (as requires lagged coding)
   3087   // and if the relevant speed feature flag is set.
   3088   if ((cpi->pass == 2) && (cpi->sf.static_segmentation)) {
   3089     configure_static_seg_features(cpi);
   3090   }
   3091 
   3092   // Decide how big to make the frame.
   3093   vp9_pick_frame_size(cpi);
   3094 
   3095   vp9_clear_system_state();
   3096 
   3097   q = pick_q_and_adjust_q_bounds(cpi, &bottom_index, &top_index);
   3098 
   3099   q_high = top_index;
   3100   q_low  = bottom_index;
   3101 
   3102   vp9_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
   3103                                 &frame_over_shoot_limit);
   3104 
   3105 #if CONFIG_MULTIPLE_ARF
   3106   // Force the quantizer determined by the coding order pattern.
   3107   if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
   3108       cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) {
   3109     double new_q;
   3110     double current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality);
   3111     int level = cpi->this_frame_weight;
   3112     assert(level >= 0);
   3113 
   3114     // Set quantizer steps at 10% increments.
   3115     new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
   3116     q = cpi->active_worst_quality + vp9_compute_qdelta(cpi, current_q, new_q);
   3117 
   3118     bottom_index = q;
   3119     top_index    = q;
   3120     q_low  = q;
   3121     q_high = q;
   3122 
   3123     printf("frame:%d q:%d\n", cm->current_video_frame, q);
   3124   }
   3125 #endif
   3126 
   3127   loop_count = 0;
   3128   vp9_zero(cpi->rd_tx_select_threshes);
   3129 
   3130   if (!frame_is_intra_only(cm)) {
   3131     cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
   3132     /* TODO: Decide this more intelligently */
   3133     cm->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH;
   3134     set_mvcost(cpi);
   3135   }
   3136 
   3137 #if CONFIG_VP9_POSTPROC
   3138 
   3139   if (cpi->oxcf.noise_sensitivity > 0) {
   3140     int l = 0;
   3141 
   3142     switch (cpi->oxcf.noise_sensitivity) {
   3143       case 1:
   3144         l = 20;
   3145         break;
   3146       case 2:
   3147         l = 40;
   3148         break;
   3149       case 3:
   3150         l = 60;
   3151         break;
   3152       case 4:
   3153       case 5:
   3154         l = 100;
   3155         break;
   3156       case 6:
   3157         l = 150;
   3158         break;
   3159     }
   3160 
   3161     vp9_denoise(cpi->Source, cpi->Source, l);
   3162   }
   3163 
   3164 #endif
   3165 
   3166 #ifdef OUTPUT_YUV_SRC
   3167   vp9_write_yuv_frame(cpi->Source);
   3168 #endif
   3169 
   3170   do {
   3171     vp9_clear_system_state();  // __asm emms;
   3172 
   3173     vp9_set_quantizer(cpi, q);
   3174 
   3175     if (loop_count == 0) {
   3176       // Set up entropy context depending on frame type. The decoder mandates
   3177       // the use of the default context, index 0, for keyframes and inter
   3178       // frames where the error_resilient_mode or intra_only flag is set. For
   3179       // other inter-frames the encoder currently uses only two contexts;
   3180       // context 1 for ALTREF frames and context 0 for the others.
   3181       if (cm->frame_type == KEY_FRAME) {
   3182         vp9_setup_key_frame(cpi);
   3183       } else {
   3184         if (!cm->intra_only && !cm->error_resilient_mode) {
   3185           cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
   3186         }
   3187         vp9_setup_inter_frame(cpi);
   3188       }
   3189     }
   3190 
   3191     if (cpi->sf.variance_adaptive_quantization) {
   3192         vp9_vaq_frame_setup(cpi);
   3193     }
   3194 
   3195     // transform / motion compensation build reconstruction frame
   3196 
   3197     vp9_encode_frame(cpi);
   3198 
   3199     // Update the skip mb flag probabilities based on the distribution
   3200     // seen in the last encoder iteration.
   3201     // update_base_skip_probs(cpi);
   3202 
   3203     vp9_clear_system_state();  // __asm emms;
   3204 
   3205     // Dummy pack of the bitstream using up to date stats to get an
   3206     // accurate estimate of output frame size to determine if we need
   3207     // to recode.
   3208     vp9_save_coding_context(cpi);
   3209     cpi->dummy_packing = 1;
   3210     vp9_pack_bitstream(cpi, dest, size);
   3211     cpi->projected_frame_size = (*size) << 3;
   3212     vp9_restore_coding_context(cpi);
   3213 
   3214     if (frame_over_shoot_limit == 0)
   3215       frame_over_shoot_limit = 1;
   3216     active_worst_qchanged = 0;
   3217 
   3218     if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
   3219       loop = 0;
   3220     } else {
   3221       // Special case handling for forced key frames
   3222       if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
   3223         int last_q = q;
   3224         int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
   3225 
   3226         int high_err_target = cpi->ambient_err;
   3227         int low_err_target = cpi->ambient_err >> 1;
   3228 
   3229         // Prevent possible divide by zero error below for perfect KF
   3230         kf_err += !kf_err;
   3231 
   3232         // The key frame is not good enough or we can afford
   3233         // to make it better without undue risk of popping.
   3234         if ((kf_err > high_err_target &&
   3235              cpi->projected_frame_size <= frame_over_shoot_limit) ||
   3236             (kf_err > low_err_target &&
   3237              cpi->projected_frame_size <= frame_under_shoot_limit)) {
   3238           // Lower q_high
   3239           q_high = q > q_low ? q - 1 : q_low;
   3240 
   3241           // Adjust Q
   3242           q = (q * high_err_target) / kf_err;
   3243           q = MIN(q, (q_high + q_low) >> 1);
   3244         } else if (kf_err < low_err_target &&
   3245                    cpi->projected_frame_size >= frame_under_shoot_limit) {
   3246           // The key frame is much better than the previous frame
   3247           // Raise q_low
   3248           q_low = q < q_high ? q + 1 : q_high;
   3249 
   3250           // Adjust Q
   3251           q = (q * low_err_target) / kf_err;
   3252           q = MIN(q, (q_high + q_low + 1) >> 1);
   3253         }
   3254 
   3255         // Clamp Q to upper and lower limits:
   3256         q = clamp(q, q_low, q_high);
   3257 
   3258         loop = q != last_q;
   3259       } else if (recode_loop_test(
   3260           cpi, frame_over_shoot_limit, frame_under_shoot_limit,
   3261           q, top_index, bottom_index)) {
   3262         // Is the projected frame size out of range and are we allowed
   3263         // to attempt to recode.
   3264         int last_q = q;
   3265         int retries = 0;
   3266 
   3267         // Frame size out of permitted range:
   3268         // Update correction factor & compute new Q to try...
   3269 
   3270         // Frame is too large
   3271         if (cpi->projected_frame_size > cpi->this_frame_target) {
   3272           // Raise Qlow as to at least the current value
   3273           q_low = q < q_high ? q + 1 : q_high;
   3274 
   3275           if (undershoot_seen || loop_count > 1) {
   3276             // Update rate_correction_factor unless
   3277             // cpi->active_worst_quality has changed.
   3278             if (!active_worst_qchanged)
   3279               vp9_update_rate_correction_factors(cpi, 1);
   3280 
   3281             q = (q_high + q_low + 1) / 2;
   3282           } else {
   3283             // Update rate_correction_factor unless
   3284             // cpi->active_worst_quality has changed.
   3285             if (!active_worst_qchanged)
   3286               vp9_update_rate_correction_factors(cpi, 0);
   3287 
   3288             q = vp9_regulate_q(cpi, cpi->this_frame_target);
   3289 
   3290             while (q < q_low && retries < 10) {
   3291               vp9_update_rate_correction_factors(cpi, 0);
   3292               q = vp9_regulate_q(cpi, cpi->this_frame_target);
   3293               retries++;
   3294             }
   3295           }
   3296 
   3297           overshoot_seen = 1;
   3298         } else {
   3299           // Frame is too small
   3300           q_high = q > q_low ? q - 1 : q_low;
   3301 
   3302           if (overshoot_seen || loop_count > 1) {
   3303             // Update rate_correction_factor unless
   3304             // cpi->active_worst_quality has changed.
   3305             if (!active_worst_qchanged)
   3306               vp9_update_rate_correction_factors(cpi, 1);
   3307 
   3308             q = (q_high + q_low) / 2;
   3309           } else {
   3310             // Update rate_correction_factor unless
   3311             // cpi->active_worst_quality has changed.
   3312             if (!active_worst_qchanged)
   3313               vp9_update_rate_correction_factors(cpi, 0);
   3314 
   3315             q = vp9_regulate_q(cpi, cpi->this_frame_target);
   3316 
   3317             // Special case reset for qlow for constrained quality.
   3318             // This should only trigger where there is very substantial
   3319             // undershoot on a frame and the auto cq level is above
   3320             // the user passsed in value.
   3321             if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && q < q_low) {
   3322               q_low = q;
   3323             }
   3324 
   3325             while (q > q_high && retries < 10) {
   3326               vp9_update_rate_correction_factors(cpi, 0);
   3327               q = vp9_regulate_q(cpi, cpi->this_frame_target);
   3328               retries++;
   3329             }
   3330           }
   3331 
   3332           undershoot_seen = 1;
   3333         }
   3334 
   3335         // Clamp Q to upper and lower limits:
   3336         q = clamp(q, q_low, q_high);
   3337 
   3338         loop = q != last_q;
   3339       } else {
   3340         loop = 0;
   3341       }
   3342     }
   3343 
   3344     if (cpi->is_src_frame_alt_ref)
   3345       loop = 0;
   3346 
   3347     if (loop) {
   3348       loop_count++;
   3349 
   3350 #if CONFIG_INTERNAL_STATS
   3351       cpi->tot_recode_hits++;
   3352 #endif
   3353     }
   3354   } while (loop);
   3355 
   3356   // Special case code to reduce pulsing when key frames are forced at a
   3357   // fixed interval. Note the reconstruction error if it is the frame before
   3358   // the force key frame
   3359   if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) {
   3360     cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
   3361   }
   3362 
   3363   if (cm->frame_type == KEY_FRAME)
   3364     cpi->refresh_last_frame = 1;
   3365 
   3366   cm->frame_to_show = get_frame_new_buffer(cm);
   3367 
   3368 #if WRITE_RECON_BUFFER
   3369   if (cm->show_frame)
   3370     write_cx_frame_to_file(cm->frame_to_show,
   3371                            cm->current_video_frame);
   3372   else
   3373     write_cx_frame_to_file(cm->frame_to_show,
   3374                            cm->current_video_frame + 1000);
   3375 #endif
   3376 
   3377   // Pick the loop filter level for the frame.
   3378   loopfilter_frame(cpi, cm);
   3379 
   3380 #if WRITE_RECON_BUFFER
   3381   if (cm->show_frame)
   3382     write_cx_frame_to_file(cm->frame_to_show,
   3383                            cm->current_video_frame + 2000);
   3384   else
   3385     write_cx_frame_to_file(cm->frame_to_show,
   3386                            cm->current_video_frame + 3000);
   3387 #endif
   3388 
   3389   // build the bitstream
   3390   cpi->dummy_packing = 0;
   3391   vp9_pack_bitstream(cpi, dest, size);
   3392 
   3393   if (cm->seg.update_map)
   3394     update_reference_segmentation_map(cpi);
   3395 
   3396   release_scaled_references(cpi);
   3397   update_reference_frames(cpi);
   3398 
   3399   for (t = TX_4X4; t <= TX_32X32; t++)
   3400     full_to_model_counts(cpi->common.counts.coef[t],
   3401                          cpi->coef_counts[t]);
   3402   if (!cpi->common.error_resilient_mode &&
   3403       !cpi->common.frame_parallel_decoding_mode) {
   3404     vp9_adapt_coef_probs(&cpi->common);
   3405   }
   3406 
   3407   if (!frame_is_intra_only(&cpi->common)) {
   3408     FRAME_COUNTS *counts = &cpi->common.counts;
   3409 
   3410     vp9_copy(counts->y_mode, cpi->y_mode_count);
   3411     vp9_copy(counts->uv_mode, cpi->y_uv_mode_count);
   3412     vp9_copy(counts->partition, cpi->partition_count);
   3413     vp9_copy(counts->intra_inter, cpi->intra_inter_count);
   3414     vp9_copy(counts->comp_inter, cpi->comp_inter_count);
   3415     vp9_copy(counts->single_ref, cpi->single_ref_count);
   3416     vp9_copy(counts->comp_ref, cpi->comp_ref_count);
   3417     counts->mv = cpi->NMVcount;
   3418     if (!cpi->common.error_resilient_mode &&
   3419         !cpi->common.frame_parallel_decoding_mode) {
   3420       vp9_adapt_mode_probs(&cpi->common);
   3421       vp9_adapt_mv_probs(&cpi->common, cpi->common.allow_high_precision_mv);
   3422     }
   3423   }
   3424 
   3425 #ifdef ENTROPY_STATS
   3426   vp9_update_mode_context_stats(cpi);
   3427 #endif
   3428 
   3429   /* Move storing frame_type out of the above loop since it is also
   3430    * needed in motion search besides loopfilter */
   3431   cm->last_frame_type = cm->frame_type;
   3432 
   3433   // Update rate control heuristics
   3434   cpi->total_byte_count += (*size);
   3435   cpi->projected_frame_size = (*size) << 3;
   3436 
   3437   // Post encode loop adjustment of Q prediction.
   3438   if (!active_worst_qchanged)
   3439     vp9_update_rate_correction_factors(cpi, (cpi->sf.recode_loop ||
   3440         cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0);
   3441 
   3442 
   3443   cpi->last_q[cm->frame_type] = cm->base_qindex;
   3444 
   3445   // Keep record of last boosted (KF/KF/ARF) Q value.
   3446   // If the current frame is coded at a lower Q then we also update it.
   3447   // If all mbs in this group are skipped only update if the Q value is
   3448   // better than that already stored.
   3449   // This is used to help set quality in forced key frames to reduce popping
   3450   if ((cm->base_qindex < cpi->last_boosted_qindex) ||
   3451       ((cpi->static_mb_pct < 100) &&
   3452        ((cm->frame_type == KEY_FRAME) ||
   3453         cpi->refresh_alt_ref_frame ||
   3454         (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) {
   3455     cpi->last_boosted_qindex = cm->base_qindex;
   3456   }
   3457 
   3458   if (cm->frame_type == KEY_FRAME) {
   3459     vp9_adjust_key_frame_context(cpi);
   3460   }
   3461 
   3462   // Keep a record of ambient average Q.
   3463   if (cm->frame_type != KEY_FRAME)
   3464     cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex +
   3465                             cm->base_qindex) >> 2;
   3466 
   3467   // Keep a record from which we can calculate the average Q excluding GF
   3468   // updates and key frames.
   3469   if (cm->frame_type != KEY_FRAME &&
   3470       !cpi->refresh_golden_frame &&
   3471       !cpi->refresh_alt_ref_frame) {
   3472     cpi->ni_frames++;
   3473     cpi->tot_q += vp9_convert_qindex_to_q(q);
   3474     cpi->avg_q = cpi->tot_q / (double)cpi->ni_frames;
   3475 
   3476     // Calculate the average Q for normal inter frames (not key or GFU frames).
   3477     cpi->ni_tot_qi += q;
   3478     cpi->ni_av_qi = cpi->ni_tot_qi / cpi->ni_frames;
   3479   }
   3480 
   3481   // Update the buffer level variable.
   3482   // Non-viewable frames are a special case and are treated as pure overhead.
   3483   if (!cm->show_frame)
   3484     cpi->bits_off_target -= cpi->projected_frame_size;
   3485   else
   3486     cpi->bits_off_target += cpi->av_per_frame_bandwidth -
   3487                             cpi->projected_frame_size;
   3488 
   3489   // Clip the buffer level at the maximum buffer size
   3490   if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
   3491     cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
   3492 
   3493   // Rolling monitors of whether we are over or underspending used to help
   3494   // regulate min and Max Q in two pass.
   3495   if (cm->frame_type != KEY_FRAME) {
   3496     cpi->rolling_target_bits =
   3497       ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
   3498     cpi->rolling_actual_bits =
   3499       ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
   3500     cpi->long_rolling_target_bits =
   3501       ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
   3502     cpi->long_rolling_actual_bits =
   3503       ((cpi->long_rolling_actual_bits * 31) +
   3504        cpi->projected_frame_size + 16) / 32;
   3505   }
   3506 
   3507   // Actual bits spent
   3508   cpi->total_actual_bits += cpi->projected_frame_size;
   3509 
   3510   // Debug stats
   3511   cpi->total_target_vs_actual += (cpi->this_frame_target -
   3512                                   cpi->projected_frame_size);
   3513 
   3514   cpi->buffer_level = cpi->bits_off_target;
   3515 
   3516 #ifndef DISABLE_RC_LONG_TERM_MEM
   3517   // Update bits left to the kf and gf groups to account for overshoot or
   3518   // undershoot on these frames
   3519   if (cm->frame_type == KEY_FRAME) {
   3520     cpi->twopass.kf_group_bits += cpi->this_frame_target -
   3521                                   cpi->projected_frame_size;
   3522 
   3523     cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0);
   3524   } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) {
   3525     cpi->twopass.gf_group_bits += cpi->this_frame_target -
   3526                                   cpi->projected_frame_size;
   3527 
   3528     cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0);
   3529   }
   3530 #endif
   3531 
   3532 #if 0
   3533   output_frame_level_debug_stats(cpi);
   3534 #endif
   3535   if (cpi->refresh_golden_frame == 1)
   3536     cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
   3537   else
   3538     cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
   3539 
   3540   if (cpi->refresh_alt_ref_frame == 1)
   3541     cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
   3542   else
   3543     cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
   3544 
   3545 
   3546   if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
   3547     cpi->gold_is_last = 1;
   3548   else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
   3549     cpi->gold_is_last = 0;
   3550 
   3551   if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
   3552     cpi->alt_is_last = 1;
   3553   else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
   3554     cpi->alt_is_last = 0;
   3555 
   3556   if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
   3557     cpi->gold_is_alt = 1;
   3558   else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
   3559     cpi->gold_is_alt = 0;
   3560 
   3561   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
   3562 
   3563   if (cpi->gold_is_last)
   3564     cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
   3565 
   3566   if (cpi->alt_is_last)
   3567     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
   3568 
   3569   if (cpi->gold_is_alt)
   3570     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
   3571 
   3572   if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame
   3573       && (cm->frame_type != KEY_FRAME))
   3574     // Update the alternate reference frame stats as appropriate.
   3575     update_alt_ref_frame_stats(cpi);
   3576   else
   3577     // Update the Golden frame stats as appropriate.
   3578     update_golden_frame_stats(cpi);
   3579 
   3580   if (cm->frame_type == KEY_FRAME) {
   3581     // Tell the caller that the frame was coded as a key frame
   3582     *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
   3583 
   3584 #if CONFIG_MULTIPLE_ARF
   3585     // Reset the sequence number.
   3586     if (cpi->multi_arf_enabled) {
   3587       cpi->sequence_number = 0;
   3588       cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
   3589       cpi->new_frame_coding_order_period = -1;
   3590     }
   3591 #endif
   3592 
   3593     // As this frame is a key frame the next defaults to an inter frame.
   3594     cm->frame_type = INTER_FRAME;
   3595   } else {
   3596     *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
   3597 
   3598 #if CONFIG_MULTIPLE_ARF
   3599     /* Increment position in the coded frame sequence. */
   3600     if (cpi->multi_arf_enabled) {
   3601       ++cpi->sequence_number;
   3602       if (cpi->sequence_number >= cpi->frame_coding_order_period) {
   3603         cpi->sequence_number = 0;
   3604         cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
   3605         cpi->new_frame_coding_order_period = -1;
   3606       }
   3607       cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
   3608       assert(cpi->this_frame_weight >= 0);
   3609     }
   3610 #endif
   3611   }
   3612 
   3613   // Clear the one shot update flags for segmentation map and mode/ref loop
   3614   // filter deltas.
   3615   cm->seg.update_map = 0;
   3616   cm->seg.update_data = 0;
   3617   cm->lf.mode_ref_delta_update = 0;
   3618 
   3619   // keep track of the last coded dimensions
   3620   cm->last_width = cm->width;
   3621   cm->last_height = cm->height;
   3622 
   3623   // reset to normal state now that we are done.
   3624   cm->last_show_frame = cm->show_frame;
   3625   if (cm->show_frame) {
   3626     // current mip will be the prev_mip for the next frame
   3627     MODE_INFO *temp = cm->prev_mip;
   3628     MODE_INFO **temp2 = cm->prev_mi_grid_base;
   3629     cm->prev_mip = cm->mip;
   3630     cm->mip = temp;
   3631     cm->prev_mi_grid_base = cm->mi_grid_base;
   3632     cm->mi_grid_base = temp2;
   3633 
   3634     // update the upper left visible macroblock ptrs
   3635     cm->mi = cm->mip + cm->mode_info_stride + 1;
   3636     cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1;
   3637 
   3638     cpi->mb.e_mbd.mi_8x8 = cm->mi_grid_visible;
   3639     cpi->mb.e_mbd.mi_8x8[0] = cm->mi;
   3640 
   3641     // Don't increment frame counters if this was an altref buffer
   3642     // update not a real frame
   3643     ++cm->current_video_frame;
   3644     ++cpi->frames_since_key;
   3645   }
   3646   // restore prev_mi
   3647   cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;
   3648   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1;
   3649 }
   3650 
   3651 static void Pass2Encode(VP9_COMP *cpi, unsigned long *size,
   3652                         unsigned char *dest, unsigned int *frame_flags) {
   3653   cpi->enable_encode_breakout = 1;
   3654 
   3655   if (!cpi->refresh_alt_ref_frame)
   3656     vp9_second_pass(cpi);
   3657 
   3658   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
   3659   // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt");
   3660 #ifdef DISABLE_RC_LONG_TERM_MEM
   3661   cpi->twopass.bits_left -=  cpi->this_frame_target;
   3662 #else
   3663   cpi->twopass.bits_left -= 8 * *size;
   3664 #endif
   3665 
   3666   if (!cpi->refresh_alt_ref_frame) {
   3667     double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
   3668     double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
   3669                                         * cpi->oxcf.two_pass_vbrmin_section
   3670                                         / 100);
   3671 
   3672     if (two_pass_min_rate < lower_bounds_min_rate)
   3673       two_pass_min_rate = lower_bounds_min_rate;
   3674 
   3675     cpi->twopass.bits_left += (int64_t)(two_pass_min_rate
   3676                               / cpi->oxcf.framerate);
   3677   }
   3678 }
   3679 
   3680 static void check_initial_width(VP9_COMP *cpi, YV12_BUFFER_CONFIG *sd) {
   3681   VP9_COMMON            *cm = &cpi->common;
   3682   if (!cpi->initial_width) {
   3683     // TODO(jkoleszar): Support 1/4 subsampling?
   3684     cm->subsampling_x = (sd != NULL) && sd->uv_width < sd->y_width;
   3685     cm->subsampling_y = (sd != NULL) && sd->uv_height < sd->y_height;
   3686     alloc_raw_frame_buffers(cpi);
   3687 
   3688     cpi->initial_width = cm->width;
   3689     cpi->initial_height = cm->height;
   3690   }
   3691 }
   3692 
   3693 
   3694 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags,
   3695                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
   3696                           int64_t end_time) {
   3697   VP9_COMP              *cpi = (VP9_COMP *) ptr;
   3698   struct vpx_usec_timer  timer;
   3699   int                    res = 0;
   3700 
   3701   check_initial_width(cpi, sd);
   3702   vpx_usec_timer_start(&timer);
   3703   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
   3704                          cpi->active_map_enabled ? cpi->active_map : NULL))
   3705     res = -1;
   3706   vpx_usec_timer_mark(&timer);
   3707   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
   3708 
   3709   return res;
   3710 }
   3711 
   3712 
   3713 static int frame_is_reference(const VP9_COMP *cpi) {
   3714   const VP9_COMMON *cm = &cpi->common;
   3715 
   3716   return cm->frame_type == KEY_FRAME ||
   3717          cpi->refresh_last_frame ||
   3718          cpi->refresh_golden_frame ||
   3719          cpi->refresh_alt_ref_frame ||
   3720          cm->refresh_frame_context ||
   3721          cm->lf.mode_ref_delta_update ||
   3722          cm->seg.update_map ||
   3723          cm->seg.update_data;
   3724 }
   3725 
   3726 #if CONFIG_MULTIPLE_ARF
   3727 int is_next_frame_arf(VP9_COMP *cpi) {
   3728   // Negative entry in frame_coding_order indicates an ARF at this position.
   3729   return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0;
   3730 }
   3731 #endif
   3732 
   3733 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
   3734                             unsigned long *size, unsigned char *dest,
   3735                             int64_t *time_stamp, int64_t *time_end, int flush) {
   3736   VP9_COMP *cpi = (VP9_COMP *) ptr;
   3737   VP9_COMMON *cm = &cpi->common;
   3738   struct vpx_usec_timer  cmptimer;
   3739   YV12_BUFFER_CONFIG    *force_src_buffer = NULL;
   3740   int i;
   3741   // FILE *fp_out = fopen("enc_frame_type.txt", "a");
   3742 
   3743   if (!cpi)
   3744     return -1;
   3745 
   3746   vpx_usec_timer_start(&cmptimer);
   3747 
   3748   cpi->source = NULL;
   3749 
   3750   cpi->common.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV;
   3751   set_mvcost(cpi);
   3752 
   3753   // Should we code an alternate reference frame.
   3754   if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) {
   3755     int frames_to_arf;
   3756 
   3757 #if CONFIG_MULTIPLE_ARF
   3758     assert(!cpi->multi_arf_enabled ||
   3759            cpi->frame_coding_order[cpi->sequence_number] < 0);
   3760 
   3761     if (cpi->multi_arf_enabled && (cpi->pass == 2))
   3762       frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number])
   3763         - cpi->next_frame_in_order;
   3764     else
   3765 #endif
   3766       frames_to_arf = cpi->frames_till_gf_update_due;
   3767 
   3768     assert(frames_to_arf < cpi->twopass.frames_to_key);
   3769 
   3770     if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) {
   3771 #if CONFIG_MULTIPLE_ARF
   3772       cpi->alt_ref_source[cpi->arf_buffered] = cpi->source;
   3773 #else
   3774       cpi->alt_ref_source = cpi->source;
   3775 #endif
   3776 
   3777       if (cpi->oxcf.arnr_max_frames > 0) {
   3778         // Produce the filtered ARF frame.
   3779         // TODO(agrange) merge these two functions.
   3780         configure_arnr_filter(cpi, cm->current_video_frame + frames_to_arf,
   3781                               cpi->gfu_boost);
   3782         vp9_temporal_filter_prepare(cpi, frames_to_arf);
   3783         vp9_extend_frame_borders(&cpi->alt_ref_buffer,
   3784                                  cm->subsampling_x, cm->subsampling_y);
   3785         force_src_buffer = &cpi->alt_ref_buffer;
   3786       }
   3787 
   3788       cm->show_frame = 0;
   3789       cpi->refresh_alt_ref_frame = 1;
   3790       cpi->refresh_golden_frame = 0;
   3791       cpi->refresh_last_frame = 0;
   3792       cpi->is_src_frame_alt_ref = 0;
   3793 
   3794       // TODO(agrange) This needs to vary depending on where the next ARF is.
   3795       cpi->frames_till_alt_ref_frame = frames_to_arf;
   3796 
   3797 #if CONFIG_MULTIPLE_ARF
   3798       if (!cpi->multi_arf_enabled)
   3799 #endif
   3800         cpi->source_alt_ref_pending = 0;   // Clear Pending altf Ref flag.
   3801     }
   3802   }
   3803 
   3804   if (!cpi->source) {
   3805 #if CONFIG_MULTIPLE_ARF
   3806     int i;
   3807 #endif
   3808     if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
   3809       cm->show_frame = 1;
   3810       cm->intra_only = 0;
   3811 
   3812 #if CONFIG_MULTIPLE_ARF
   3813       // Is this frame the ARF overlay.
   3814       cpi->is_src_frame_alt_ref = 0;
   3815       for (i = 0; i < cpi->arf_buffered; ++i) {
   3816         if (cpi->source == cpi->alt_ref_source[i]) {
   3817           cpi->is_src_frame_alt_ref = 1;
   3818           cpi->refresh_golden_frame = 1;
   3819           break;
   3820         }
   3821       }
   3822 #else
   3823       cpi->is_src_frame_alt_ref = cpi->alt_ref_source
   3824                                   && (cpi->source == cpi->alt_ref_source);
   3825 #endif
   3826       if (cpi->is_src_frame_alt_ref) {
   3827         // Current frame is an ARF overlay frame.
   3828 #if CONFIG_MULTIPLE_ARF
   3829         cpi->alt_ref_source[i] = NULL;
   3830 #else
   3831         cpi->alt_ref_source = NULL;
   3832 #endif
   3833         // Don't refresh the last buffer for an ARF overlay frame. It will
   3834         // become the GF so preserve last as an alternative prediction option.
   3835         cpi->refresh_last_frame = 0;
   3836       }
   3837 #if CONFIG_MULTIPLE_ARF
   3838       ++cpi->next_frame_in_order;
   3839 #endif
   3840     }
   3841   }
   3842 
   3843   if (cpi->source) {
   3844     cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
   3845                                                            : &cpi->source->img;
   3846     *time_stamp = cpi->source->ts_start;
   3847     *time_end = cpi->source->ts_end;
   3848     *frame_flags = cpi->source->flags;
   3849 
   3850     // fprintf(fp_out, "   Frame:%d", cm->current_video_frame);
   3851 #if CONFIG_MULTIPLE_ARF
   3852     if (cpi->multi_arf_enabled) {
   3853       // fprintf(fp_out, "   seq_no:%d  this_frame_weight:%d",
   3854       //         cpi->sequence_number, cpi->this_frame_weight);
   3855     } else {
   3856       // fprintf(fp_out, "\n");
   3857     }
   3858 #else
   3859     // fprintf(fp_out, "\n");
   3860 #endif
   3861 
   3862 #if CONFIG_MULTIPLE_ARF
   3863     if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2))
   3864       cpi->source_alt_ref_pending = is_next_frame_arf(cpi);
   3865 #endif
   3866   } else {
   3867     *size = 0;
   3868     if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
   3869       vp9_end_first_pass(cpi);    /* get last stats packet */
   3870       cpi->twopass.first_pass_done = 1;
   3871     }
   3872 
   3873     // fclose(fp_out);
   3874     return -1;
   3875   }
   3876 
   3877   if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
   3878     cpi->first_time_stamp_ever = cpi->source->ts_start;
   3879     cpi->last_end_time_stamp_seen = cpi->source->ts_start;
   3880   }
   3881 
   3882   // adjust frame rates based on timestamps given
   3883   if (!cpi->refresh_alt_ref_frame) {
   3884     int64_t this_duration;
   3885     int step = 0;
   3886 
   3887     if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
   3888       this_duration = cpi->source->ts_end - cpi->source->ts_start;
   3889       step = 1;
   3890     } else {
   3891       int64_t last_duration = cpi->last_end_time_stamp_seen
   3892                                 - cpi->last_time_stamp_seen;
   3893 
   3894       this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
   3895 
   3896       // do a step update if the duration changes by 10%
   3897       if (last_duration)
   3898         step = (int)((this_duration - last_duration) * 10 / last_duration);
   3899     }
   3900 
   3901     if (this_duration) {
   3902       if (step) {
   3903         vp9_new_framerate(cpi, 10000000.0 / this_duration);
   3904       } else {
   3905         // Average this frame's rate into the last second's average
   3906         // frame rate. If we haven't seen 1 second yet, then average
   3907         // over the whole interval seen.
   3908         const double interval = MIN((double)(cpi->source->ts_end
   3909                                      - cpi->first_time_stamp_ever), 10000000.0);
   3910         double avg_duration = 10000000.0 / cpi->oxcf.framerate;
   3911         avg_duration *= (interval - avg_duration + this_duration);
   3912         avg_duration /= interval;
   3913 
   3914         vp9_new_framerate(cpi, 10000000.0 / avg_duration);
   3915       }
   3916     }
   3917 
   3918     cpi->last_time_stamp_seen = cpi->source->ts_start;
   3919     cpi->last_end_time_stamp_seen = cpi->source->ts_end;
   3920   }
   3921 
   3922   // start with a 0 size frame
   3923   *size = 0;
   3924 
   3925   // Clear down mmx registers
   3926   vp9_clear_system_state();  // __asm emms;
   3927 
   3928   /* find a free buffer for the new frame, releasing the reference previously
   3929    * held.
   3930    */
   3931   cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
   3932   cm->new_fb_idx = get_free_fb(cm);
   3933 
   3934 #if CONFIG_MULTIPLE_ARF
   3935   /* Set up the correct ARF frame. */
   3936   if (cpi->refresh_alt_ref_frame) {
   3937     ++cpi->arf_buffered;
   3938   }
   3939   if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
   3940       (cpi->pass == 2)) {
   3941     cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number];
   3942   }
   3943 #endif
   3944 
   3945   /* Get the mapping of L/G/A to the reference buffer pool */
   3946   cm->active_ref_idx[0] = cm->ref_frame_map[cpi->lst_fb_idx];
   3947   cm->active_ref_idx[1] = cm->ref_frame_map[cpi->gld_fb_idx];
   3948   cm->active_ref_idx[2] = cm->ref_frame_map[cpi->alt_fb_idx];
   3949 
   3950 #if 0  // CONFIG_MULTIPLE_ARF
   3951   if (cpi->multi_arf_enabled) {
   3952     fprintf(fp_out, "      idx(%d, %d, %d, %d) active(%d, %d, %d)",
   3953         cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx, cm->new_fb_idx,
   3954         cm->active_ref_idx[0], cm->active_ref_idx[1], cm->active_ref_idx[2]);
   3955     if (cpi->refresh_alt_ref_frame)
   3956       fprintf(fp_out, "  type:ARF");
   3957     if (cpi->is_src_frame_alt_ref)
   3958       fprintf(fp_out, "  type:OVERLAY[%d]", cpi->alt_fb_idx);
   3959     fprintf(fp_out, "\n");
   3960   }
   3961 #endif
   3962 
   3963   cm->frame_type = INTER_FRAME;
   3964   cm->frame_flags = *frame_flags;
   3965 
   3966   // Reset the frame pointers to the current frame size
   3967   vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
   3968                            cm->width, cm->height,
   3969                            cm->subsampling_x, cm->subsampling_y,
   3970                            VP9BORDERINPIXELS);
   3971 
   3972   // Calculate scaling factors for each of the 3 available references
   3973   for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i)
   3974     vp9_setup_scale_factors(cm, i);
   3975 
   3976   vp9_setup_interp_filters(&cpi->mb.e_mbd, DEFAULT_INTERP_FILTER, cm);
   3977 
   3978   if (cpi->sf.variance_adaptive_quantization) {
   3979       vp9_vaq_init();
   3980   }
   3981 
   3982   if (cpi->pass == 1) {
   3983     Pass1Encode(cpi, size, dest, frame_flags);
   3984   } else if (cpi->pass == 2) {
   3985     Pass2Encode(cpi, size, dest, frame_flags);
   3986   } else {
   3987     encode_frame_to_data_rate(cpi, size, dest, frame_flags);
   3988   }
   3989 
   3990   if (cm->refresh_frame_context)
   3991     cm->frame_contexts[cm->frame_context_idx] = cm->fc;
   3992 
   3993   if (*size > 0) {
   3994     // if its a dropped frame honor the requests on subsequent frames
   3995     cpi->droppable = !frame_is_reference(cpi);
   3996 
   3997     // return to normal state
   3998     cm->reset_frame_context = 0;
   3999     cm->refresh_frame_context = 1;
   4000     cpi->refresh_alt_ref_frame = 0;
   4001     cpi->refresh_golden_frame = 0;
   4002     cpi->refresh_last_frame = 1;
   4003     cm->frame_type = INTER_FRAME;
   4004   }
   4005 
   4006   vpx_usec_timer_mark(&cmptimer);
   4007   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
   4008 
   4009   if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
   4010     generate_psnr_packet(cpi);
   4011 
   4012 #if CONFIG_INTERNAL_STATS
   4013 
   4014   if (cpi->pass != 1) {
   4015     cpi->bytes += *size;
   4016 
   4017     if (cm->show_frame) {
   4018       cpi->count++;
   4019 
   4020       if (cpi->b_calculate_psnr) {
   4021         double ye, ue, ve;
   4022         double frame_psnr;
   4023         YV12_BUFFER_CONFIG      *orig = cpi->Source;
   4024         YV12_BUFFER_CONFIG      *recon = cpi->common.frame_to_show;
   4025         YV12_BUFFER_CONFIG      *pp = &cm->post_proc_buffer;
   4026         int y_samples = orig->y_height * orig->y_width;
   4027         int uv_samples = orig->uv_height * orig->uv_width;
   4028         int t_samples = y_samples + 2 * uv_samples;
   4029         double sq_error;
   4030 
   4031         ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride,
   4032                               recon->y_buffer, recon->y_stride,
   4033                               orig->y_crop_width, orig->y_crop_height);
   4034 
   4035         ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride,
   4036                               recon->u_buffer, recon->uv_stride,
   4037                               orig->uv_crop_width, orig->uv_crop_height);
   4038 
   4039         ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride,
   4040                               recon->v_buffer, recon->uv_stride,
   4041                               orig->uv_crop_width, orig->uv_crop_height);
   4042 
   4043         sq_error = ye + ue + ve;
   4044 
   4045         frame_psnr = vp9_mse2psnr(t_samples, 255.0, sq_error);
   4046 
   4047         cpi->total_y += vp9_mse2psnr(y_samples, 255.0, ye);
   4048         cpi->total_u += vp9_mse2psnr(uv_samples, 255.0, ue);
   4049         cpi->total_v += vp9_mse2psnr(uv_samples, 255.0, ve);
   4050         cpi->total_sq_error += sq_error;
   4051         cpi->total  += frame_psnr;
   4052         {
   4053           double frame_psnr2, frame_ssim2 = 0;
   4054           double weight = 0;
   4055 #if CONFIG_VP9_POSTPROC
   4056           vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
   4057                       cm->lf.filter_level * 10 / 6);
   4058 #endif
   4059           vp9_clear_system_state();
   4060 
   4061           ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride,
   4062                                 pp->y_buffer, pp->y_stride,
   4063                                 orig->y_crop_width, orig->y_crop_height);
   4064 
   4065           ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride,
   4066                                 pp->u_buffer, pp->uv_stride,
   4067                                 orig->uv_crop_width, orig->uv_crop_height);
   4068 
   4069           ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride,
   4070                                 pp->v_buffer, pp->uv_stride,
   4071                                 orig->uv_crop_width, orig->uv_crop_height);
   4072 
   4073           sq_error = ye + ue + ve;
   4074 
   4075           frame_psnr2 = vp9_mse2psnr(t_samples, 255.0, sq_error);
   4076 
   4077           cpi->totalp_y += vp9_mse2psnr(y_samples, 255.0, ye);
   4078           cpi->totalp_u += vp9_mse2psnr(uv_samples, 255.0, ue);
   4079           cpi->totalp_v += vp9_mse2psnr(uv_samples, 255.0, ve);
   4080           cpi->total_sq_error2 += sq_error;
   4081           cpi->totalp  += frame_psnr2;
   4082 
   4083           frame_ssim2 = vp9_calc_ssim(cpi->Source,
   4084                                       recon, 1, &weight);
   4085 
   4086           cpi->summed_quality += frame_ssim2 * weight;
   4087           cpi->summed_weights += weight;
   4088 
   4089           frame_ssim2 = vp9_calc_ssim(cpi->Source,
   4090                                       &cm->post_proc_buffer, 1, &weight);
   4091 
   4092           cpi->summedp_quality += frame_ssim2 * weight;
   4093           cpi->summedp_weights += weight;
   4094 #if 0
   4095           {
   4096             FILE *f = fopen("q_used.stt", "a");
   4097             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
   4098                     cpi->common.current_video_frame, y2, u2, v2,
   4099                     frame_psnr2, frame_ssim2);
   4100             fclose(f);
   4101           }
   4102 #endif
   4103         }
   4104       }
   4105 
   4106       if (cpi->b_calculate_ssimg) {
   4107         double y, u, v, frame_all;
   4108         frame_all =  vp9_calc_ssimg(cpi->Source, cm->frame_to_show,
   4109                                     &y, &u, &v);
   4110         cpi->total_ssimg_y += y;
   4111         cpi->total_ssimg_u += u;
   4112         cpi->total_ssimg_v += v;
   4113         cpi->total_ssimg_all += frame_all;
   4114       }
   4115     }
   4116   }
   4117 
   4118 #endif
   4119   // fclose(fp_out);
   4120   return 0;
   4121 }
   4122 
   4123 int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest,
   4124                               vp9_ppflags_t *flags) {
   4125   VP9_COMP *cpi = (VP9_COMP *) comp;
   4126 
   4127   if (!cpi->common.show_frame) {
   4128     return -1;
   4129   } else {
   4130     int ret;
   4131 #if CONFIG_VP9_POSTPROC
   4132     ret = vp9_post_proc_frame(&cpi->common, dest, flags);
   4133 #else
   4134 
   4135     if (cpi->common.frame_to_show) {
   4136       *dest = *cpi->common.frame_to_show;
   4137       dest->y_width = cpi->common.width;
   4138       dest->y_height = cpi->common.height;
   4139       dest->uv_height = cpi->common.height / 2;
   4140       ret = 0;
   4141     } else {
   4142       ret = -1;
   4143     }
   4144 
   4145 #endif  // !CONFIG_VP9_POSTPROC
   4146     vp9_clear_system_state();
   4147     return ret;
   4148   }
   4149 }
   4150 
   4151 int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows,
   4152                    unsigned int cols, int delta_q[MAX_SEGMENTS],
   4153                    int delta_lf[MAX_SEGMENTS],
   4154                    unsigned int threshold[MAX_SEGMENTS]) {
   4155   VP9_COMP *cpi = (VP9_COMP *) comp;
   4156   signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS];
   4157   struct segmentation *seg = &cpi->common.seg;
   4158   int i;
   4159 
   4160   if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
   4161     return -1;
   4162 
   4163   if (!map) {
   4164     vp9_disable_segmentation((VP9_PTR)cpi);
   4165     return 0;
   4166   }
   4167 
   4168   // Set the segmentation Map
   4169   vp9_set_segmentation_map((VP9_PTR)cpi, map);
   4170 
   4171   // Activate segmentation.
   4172   vp9_enable_segmentation((VP9_PTR)cpi);
   4173 
   4174   // Set up the quant, LF and breakout threshold segment data
   4175   for (i = 0; i < MAX_SEGMENTS; i++) {
   4176     feature_data[SEG_LVL_ALT_Q][i] = delta_q[i];
   4177     feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i];
   4178     cpi->segment_encode_breakout[i] = threshold[i];
   4179   }
   4180 
   4181   // Enable the loop and quant changes in the feature mask
   4182   for (i = 0; i < MAX_SEGMENTS; i++) {
   4183     if (delta_q[i])
   4184       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
   4185     else
   4186       vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q);
   4187 
   4188     if (delta_lf[i])
   4189       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF);
   4190     else
   4191       vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF);
   4192   }
   4193 
   4194   // Initialize the feature data structure
   4195   // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
   4196   vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
   4197 
   4198   return 0;
   4199 }
   4200 
   4201 int vp9_set_active_map(VP9_PTR comp, unsigned char *map,
   4202                        unsigned int rows, unsigned int cols) {
   4203   VP9_COMP *cpi = (VP9_COMP *) comp;
   4204 
   4205   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
   4206     if (map) {
   4207       vpx_memcpy(cpi->active_map, map, rows * cols);
   4208       cpi->active_map_enabled = 1;
   4209     } else {
   4210       cpi->active_map_enabled = 0;
   4211     }
   4212 
   4213     return 0;
   4214   } else {
   4215     // cpi->active_map_enabled = 0;
   4216     return -1;
   4217   }
   4218 }
   4219 
   4220 int vp9_set_internal_size(VP9_PTR comp,
   4221                           VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
   4222   VP9_COMP *cpi = (VP9_COMP *) comp;
   4223   VP9_COMMON *cm = &cpi->common;
   4224   int hr = 0, hs = 0, vr = 0, vs = 0;
   4225 
   4226   if (horiz_mode > ONETWO || vert_mode > ONETWO)
   4227     return -1;
   4228 
   4229   Scale2Ratio(horiz_mode, &hr, &hs);
   4230   Scale2Ratio(vert_mode, &vr, &vs);
   4231 
   4232   // always go to the next whole number
   4233   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
   4234   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
   4235 
   4236   assert(cm->width <= cpi->initial_width);
   4237   assert(cm->height <= cpi->initial_height);
   4238   update_frame_size(cpi);
   4239   return 0;
   4240 }
   4241 
   4242 int vp9_set_size_literal(VP9_PTR comp, unsigned int width,
   4243                          unsigned int height) {
   4244   VP9_COMP *cpi = (VP9_COMP *)comp;
   4245   VP9_COMMON *cm = &cpi->common;
   4246 
   4247   check_initial_width(cpi, NULL);
   4248 
   4249   if (width) {
   4250     cm->width = width;
   4251     if (cm->width * 5 < cpi->initial_width) {
   4252       cm->width = cpi->initial_width / 5 + 1;
   4253       printf("Warning: Desired width too small, changed to %d \n", cm->width);
   4254     }
   4255     if (cm->width > cpi->initial_width) {
   4256       cm->width = cpi->initial_width;
   4257       printf("Warning: Desired width too large, changed to %d \n", cm->width);
   4258     }
   4259   }
   4260 
   4261   if (height) {
   4262     cm->height = height;
   4263     if (cm->height * 5 < cpi->initial_height) {
   4264       cm->height = cpi->initial_height / 5 + 1;
   4265       printf("Warning: Desired height too small, changed to %d \n", cm->height);
   4266     }
   4267     if (cm->height > cpi->initial_height) {
   4268       cm->height = cpi->initial_height;
   4269       printf("Warning: Desired height too large, changed to %d \n", cm->height);
   4270     }
   4271   }
   4272 
   4273   assert(cm->width <= cpi->initial_width);
   4274   assert(cm->height <= cpi->initial_height);
   4275   update_frame_size(cpi);
   4276   return 0;
   4277 }
   4278 
   4279 void vp9_set_svc(VP9_PTR comp, int use_svc) {
   4280   VP9_COMP *cpi = (VP9_COMP *)comp;
   4281   cpi->use_svc = use_svc;
   4282   return;
   4283 }
   4284 
   4285 int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) {
   4286   int i, j;
   4287   int total = 0;
   4288 
   4289   uint8_t *src = source->y_buffer;
   4290   uint8_t *dst = dest->y_buffer;
   4291 
   4292   // Loop through the Y plane raw and reconstruction data summing
   4293   // (square differences)
   4294   for (i = 0; i < source->y_height; i += 16) {
   4295     for (j = 0; j < source->y_width; j += 16) {
   4296       unsigned int sse;
   4297       total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
   4298                             &sse);
   4299     }
   4300 
   4301     src += 16 * source->y_stride;
   4302     dst += 16 * dest->y_stride;
   4303   }
   4304 
   4305   return total;
   4306 }
   4307 
   4308 
   4309 int vp9_get_quantizer(VP9_PTR c) {
   4310   return ((VP9_COMP *)c)->common.base_qindex;
   4311 }
   4312