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 <limits.h>
     12 #include <math.h>
     13 #include <stdio.h>
     14 
     15 #include "./vpx_scale_rtcd.h"
     16 
     17 #include "vpx_mem/vpx_mem.h"
     18 #include "vpx_scale/vpx_scale.h"
     19 #include "vpx_scale/yv12config.h"
     20 
     21 #include "vp9/common/vp9_entropymv.h"
     22 #include "vp9/common/vp9_quant_common.h"
     23 #include "vp9/common/vp9_reconinter.h"  // vp9_setup_dst_planes()
     24 #include "vp9/common/vp9_systemdependent.h"
     25 
     26 #include "vp9/encoder/vp9_aq_variance.h"
     27 #include "vp9/encoder/vp9_block.h"
     28 #include "vp9/encoder/vp9_encodeframe.h"
     29 #include "vp9/encoder/vp9_encodemb.h"
     30 #include "vp9/encoder/vp9_encodemv.h"
     31 #include "vp9/encoder/vp9_extend.h"
     32 #include "vp9/encoder/vp9_firstpass.h"
     33 #include "vp9/encoder/vp9_mcomp.h"
     34 #include "vp9/encoder/vp9_onyx_int.h"
     35 #include "vp9/encoder/vp9_quantize.h"
     36 #include "vp9/encoder/vp9_ratectrl.h"
     37 #include "vp9/encoder/vp9_rdopt.h"
     38 #include "vp9/encoder/vp9_variance.h"
     39 
     40 #define OUTPUT_FPF 0
     41 
     42 #define IIFACTOR   12.5
     43 #define IIKFACTOR1 12.5
     44 #define IIKFACTOR2 15.0
     45 #define RMAX       512.0
     46 #define GF_RMAX    96.0
     47 #define ERR_DIVISOR   150.0
     48 #define MIN_DECAY_FACTOR 0.1
     49 
     50 #define KF_MB_INTRA_MIN 150
     51 #define GF_MB_INTRA_MIN 100
     52 
     53 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
     54 
     55 #define MIN_KF_BOOST        300
     56 
     57 #if CONFIG_MULTIPLE_ARF
     58 // Set MIN_GF_INTERVAL to 1 for the full decomposition.
     59 #define MIN_GF_INTERVAL             2
     60 #else
     61 #define MIN_GF_INTERVAL             4
     62 #endif
     63 
     64 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
     65   YV12_BUFFER_CONFIG temp = *a;
     66   *a = *b;
     67   *b = temp;
     68 }
     69 
     70 static int gfboost_qadjust(int qindex) {
     71   const double q = vp9_convert_qindex_to_q(qindex);
     72   return (int)((0.00000828 * q * q * q) +
     73                (-0.0055 * q * q) +
     74                (1.32 * q) + 79.3);
     75 }
     76 
     77 // Resets the first pass file to the given position using a relative seek from
     78 // the current position.
     79 static void reset_fpf_position(struct twopass_rc *p,
     80                                const FIRSTPASS_STATS *position) {
     81   p->stats_in = position;
     82 }
     83 
     84 static int lookup_next_frame_stats(const struct twopass_rc *p,
     85                                    FIRSTPASS_STATS *next_frame) {
     86   if (p->stats_in >= p->stats_in_end)
     87     return EOF;
     88 
     89   *next_frame = *p->stats_in;
     90   return 1;
     91 }
     92 
     93 
     94 // Read frame stats at an offset from the current position.
     95 static int read_frame_stats(const struct twopass_rc *p,
     96                             FIRSTPASS_STATS *frame_stats, int offset) {
     97   const FIRSTPASS_STATS *fps_ptr = p->stats_in;
     98 
     99   // Check legality of offset.
    100   if (offset >= 0) {
    101     if (&fps_ptr[offset] >= p->stats_in_end)
    102       return EOF;
    103   } else if (offset < 0) {
    104     if (&fps_ptr[offset] < p->stats_in_start)
    105       return EOF;
    106   }
    107 
    108   *frame_stats = fps_ptr[offset];
    109   return 1;
    110 }
    111 
    112 static int input_stats(struct twopass_rc *p, FIRSTPASS_STATS *fps) {
    113   if (p->stats_in >= p->stats_in_end)
    114     return EOF;
    115 
    116   *fps = *p->stats_in;
    117   ++p->stats_in;
    118   return 1;
    119 }
    120 
    121 static void output_stats(FIRSTPASS_STATS *stats,
    122                          struct vpx_codec_pkt_list *pktlist) {
    123   struct vpx_codec_cx_pkt pkt;
    124   pkt.kind = VPX_CODEC_STATS_PKT;
    125   pkt.data.twopass_stats.buf = stats;
    126   pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
    127   vpx_codec_pkt_list_add(pktlist, &pkt);
    128 
    129 // TEMP debug code
    130 #if OUTPUT_FPF
    131   {
    132     FILE *fpfile;
    133     fpfile = fopen("firstpass.stt", "a");
    134 
    135     fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
    136             "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
    137             "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
    138             stats->frame,
    139             stats->intra_error,
    140             stats->coded_error,
    141             stats->sr_coded_error,
    142             stats->ssim_weighted_pred_err,
    143             stats->pcnt_inter,
    144             stats->pcnt_motion,
    145             stats->pcnt_second_ref,
    146             stats->pcnt_neutral,
    147             stats->MVr,
    148             stats->mvr_abs,
    149             stats->MVc,
    150             stats->mvc_abs,
    151             stats->MVrv,
    152             stats->MVcv,
    153             stats->mv_in_out_count,
    154             stats->new_mv_count,
    155             stats->count,
    156             stats->duration);
    157     fclose(fpfile);
    158   }
    159 #endif
    160 }
    161 
    162 static void zero_stats(FIRSTPASS_STATS *section) {
    163   section->frame      = 0.0;
    164   section->intra_error = 0.0;
    165   section->coded_error = 0.0;
    166   section->sr_coded_error = 0.0;
    167   section->ssim_weighted_pred_err = 0.0;
    168   section->pcnt_inter  = 0.0;
    169   section->pcnt_motion  = 0.0;
    170   section->pcnt_second_ref = 0.0;
    171   section->pcnt_neutral = 0.0;
    172   section->MVr        = 0.0;
    173   section->mvr_abs     = 0.0;
    174   section->MVc        = 0.0;
    175   section->mvc_abs     = 0.0;
    176   section->MVrv       = 0.0;
    177   section->MVcv       = 0.0;
    178   section->mv_in_out_count  = 0.0;
    179   section->new_mv_count = 0.0;
    180   section->count      = 0.0;
    181   section->duration   = 1.0;
    182   section->spatial_layer_id = 0;
    183 }
    184 
    185 static void accumulate_stats(FIRSTPASS_STATS *section,
    186                              const FIRSTPASS_STATS *frame) {
    187   section->frame += frame->frame;
    188   section->spatial_layer_id = frame->spatial_layer_id;
    189   section->intra_error += frame->intra_error;
    190   section->coded_error += frame->coded_error;
    191   section->sr_coded_error += frame->sr_coded_error;
    192   section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
    193   section->pcnt_inter  += frame->pcnt_inter;
    194   section->pcnt_motion += frame->pcnt_motion;
    195   section->pcnt_second_ref += frame->pcnt_second_ref;
    196   section->pcnt_neutral += frame->pcnt_neutral;
    197   section->MVr        += frame->MVr;
    198   section->mvr_abs     += frame->mvr_abs;
    199   section->MVc        += frame->MVc;
    200   section->mvc_abs     += frame->mvc_abs;
    201   section->MVrv       += frame->MVrv;
    202   section->MVcv       += frame->MVcv;
    203   section->mv_in_out_count  += frame->mv_in_out_count;
    204   section->new_mv_count += frame->new_mv_count;
    205   section->count      += frame->count;
    206   section->duration   += frame->duration;
    207 }
    208 
    209 static void subtract_stats(FIRSTPASS_STATS *section,
    210                            const FIRSTPASS_STATS *frame) {
    211   section->frame -= frame->frame;
    212   section->intra_error -= frame->intra_error;
    213   section->coded_error -= frame->coded_error;
    214   section->sr_coded_error -= frame->sr_coded_error;
    215   section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
    216   section->pcnt_inter  -= frame->pcnt_inter;
    217   section->pcnt_motion -= frame->pcnt_motion;
    218   section->pcnt_second_ref -= frame->pcnt_second_ref;
    219   section->pcnt_neutral -= frame->pcnt_neutral;
    220   section->MVr        -= frame->MVr;
    221   section->mvr_abs     -= frame->mvr_abs;
    222   section->MVc        -= frame->MVc;
    223   section->mvc_abs     -= frame->mvc_abs;
    224   section->MVrv       -= frame->MVrv;
    225   section->MVcv       -= frame->MVcv;
    226   section->mv_in_out_count  -= frame->mv_in_out_count;
    227   section->new_mv_count -= frame->new_mv_count;
    228   section->count      -= frame->count;
    229   section->duration   -= frame->duration;
    230 }
    231 
    232 static void avg_stats(FIRSTPASS_STATS *section) {
    233   if (section->count < 1.0)
    234     return;
    235 
    236   section->intra_error /= section->count;
    237   section->coded_error /= section->count;
    238   section->sr_coded_error /= section->count;
    239   section->ssim_weighted_pred_err /= section->count;
    240   section->pcnt_inter  /= section->count;
    241   section->pcnt_second_ref /= section->count;
    242   section->pcnt_neutral /= section->count;
    243   section->pcnt_motion /= section->count;
    244   section->MVr        /= section->count;
    245   section->mvr_abs     /= section->count;
    246   section->MVc        /= section->count;
    247   section->mvc_abs     /= section->count;
    248   section->MVrv       /= section->count;
    249   section->MVcv       /= section->count;
    250   section->mv_in_out_count   /= section->count;
    251   section->duration   /= section->count;
    252 }
    253 
    254 // Calculate a modified Error used in distributing bits between easier and
    255 // harder frames.
    256 static double calculate_modified_err(const VP9_COMP *cpi,
    257                                      const FIRSTPASS_STATS *this_frame) {
    258   const struct twopass_rc *twopass = &cpi->twopass;
    259   const FIRSTPASS_STATS *stats;
    260   double av_err;
    261   double modified_error;
    262 
    263   if (cpi->svc.number_spatial_layers > 1 &&
    264       cpi->svc.number_temporal_layers == 1) {
    265     twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass;
    266   }
    267 
    268   stats = &twopass->total_stats;
    269   av_err = stats->ssim_weighted_pred_err / stats->count;
    270   modified_error = av_err * pow(this_frame->ssim_weighted_pred_err /
    271                    DOUBLE_DIVIDE_CHECK(av_err),
    272                    cpi->oxcf.two_pass_vbrbias / 100.0);
    273 
    274   return fclamp(modified_error,
    275                 twopass->modified_error_min, twopass->modified_error_max);
    276 }
    277 
    278 static const double weight_table[256] = {
    279   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    280   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    281   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    282   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    283   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
    284   0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
    285   0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
    286   0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
    287   0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
    288   0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    289   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    290   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    291   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    292   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    293   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    294   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    295   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    296   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    297   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    298   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    299   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    300   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    301   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    302   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    303   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    304   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    305   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    306   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    307   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    308   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    309   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    310   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    311   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    312   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    313   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    314   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    315   1.000000, 1.000000, 1.000000, 1.000000
    316 };
    317 
    318 static double simple_weight(const YV12_BUFFER_CONFIG *buf) {
    319   int i, j;
    320   double sum = 0.0;
    321   const int w = buf->y_crop_width;
    322   const int h = buf->y_crop_height;
    323   const uint8_t *row = buf->y_buffer;
    324 
    325   for (i = 0; i < h; ++i) {
    326     const uint8_t *pixel = row;
    327     for (j = 0; j < w; ++j)
    328       sum += weight_table[*pixel++];
    329     row += buf->y_stride;
    330   }
    331 
    332   return MAX(0.1, sum / (w * h));
    333 }
    334 
    335 // This function returns the maximum target rate per frame.
    336 static int frame_max_bits(const VP9_COMP *cpi) {
    337   int64_t max_bits =
    338     ((int64_t)cpi->rc.av_per_frame_bandwidth *
    339      (int64_t)cpi->oxcf.two_pass_vbrmax_section) / 100;
    340 
    341   if (max_bits < 0)
    342     max_bits = 0;
    343   else if (max_bits > cpi->rc.max_frame_bandwidth)
    344     max_bits = cpi->rc.max_frame_bandwidth;
    345 
    346   return (int)max_bits;
    347 }
    348 
    349 void vp9_init_first_pass(VP9_COMP *cpi) {
    350   zero_stats(&cpi->twopass.total_stats);
    351 }
    352 
    353 void vp9_end_first_pass(VP9_COMP *cpi) {
    354   if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
    355     int i;
    356     for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
    357       output_stats(&cpi->svc.layer_context[i].twopass.total_stats,
    358                    cpi->output_pkt_list);
    359     }
    360   } else {
    361     output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
    362   }
    363 }
    364 
    365 static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
    366   switch (bsize) {
    367     case BLOCK_8X8:
    368       return vp9_mse8x8;
    369     case BLOCK_16X8:
    370       return vp9_mse16x8;
    371     case BLOCK_8X16:
    372       return vp9_mse8x16;
    373     default:
    374       return vp9_mse16x16;
    375   }
    376 }
    377 
    378 static unsigned int zz_motion_search(const MACROBLOCK *x) {
    379   const MACROBLOCKD *const xd = &x->e_mbd;
    380   const uint8_t *const src = x->plane[0].src.buf;
    381   const int src_stride = x->plane[0].src.stride;
    382   const uint8_t *const ref = xd->plane[0].pre[0].buf;
    383   const int ref_stride = xd->plane[0].pre[0].stride;
    384   unsigned int sse;
    385   vp9_variance_fn_t fn = get_block_variance_fn(xd->mi[0]->mbmi.sb_type);
    386   fn(src, src_stride, ref, ref_stride, &sse);
    387   return sse;
    388 }
    389 
    390 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
    391                                      const MV *ref_mv, MV *best_mv,
    392                                      int *best_motion_err) {
    393   MACROBLOCKD *const xd = &x->e_mbd;
    394   MV tmp_mv = {0, 0};
    395   MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3};
    396   int num00, tmp_err, n, sr = 0;
    397   int step_param = 3;
    398   int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
    399   const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
    400   vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
    401   int new_mv_mode_penalty = 256;
    402   const int quart_frm = MIN(cpi->common.width, cpi->common.height);
    403 
    404   // Refine the motion search range according to the frame dimension
    405   // for first pass test.
    406   while ((quart_frm << sr) < MAX_FULL_PEL_VAL)
    407     ++sr;
    408 
    409   step_param += sr;
    410   further_steps -= sr;
    411 
    412   // Override the default variance function to use MSE.
    413   v_fn_ptr.vf = get_block_variance_fn(bsize);
    414 
    415   // Center the initial step/diamond search on best mv.
    416   tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
    417                                     step_param,
    418                                     x->sadperbit16, &num00, &v_fn_ptr,
    419                                     x->nmvjointcost,
    420                                     x->mvcost, ref_mv);
    421   if (tmp_err < INT_MAX)
    422     tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
    423   if (tmp_err < INT_MAX - new_mv_mode_penalty)
    424     tmp_err += new_mv_mode_penalty;
    425 
    426   if (tmp_err < *best_motion_err) {
    427     *best_motion_err = tmp_err;
    428     best_mv->row = tmp_mv.row;
    429     best_mv->col = tmp_mv.col;
    430   }
    431 
    432   // Carry out further step/diamond searches as necessary.
    433   n = num00;
    434   num00 = 0;
    435 
    436   while (n < further_steps) {
    437     ++n;
    438 
    439     if (num00) {
    440       --num00;
    441     } else {
    442       tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
    443                                         step_param + n, x->sadperbit16,
    444                                         &num00, &v_fn_ptr,
    445                                         x->nmvjointcost,
    446                                         x->mvcost, ref_mv);
    447       if (tmp_err < INT_MAX)
    448         tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
    449       if (tmp_err < INT_MAX - new_mv_mode_penalty)
    450         tmp_err += new_mv_mode_penalty;
    451 
    452       if (tmp_err < *best_motion_err) {
    453         *best_motion_err = tmp_err;
    454         best_mv->row = tmp_mv.row;
    455         best_mv->col = tmp_mv.col;
    456       }
    457     }
    458   }
    459 }
    460 
    461 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
    462   if (2 * mb_col + 1 < cm->mi_cols) {
    463     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16
    464                                         : BLOCK_16X8;
    465   } else {
    466     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16
    467                                         : BLOCK_8X8;
    468   }
    469 }
    470 
    471 void vp9_first_pass(VP9_COMP *cpi) {
    472   int mb_row, mb_col;
    473   MACROBLOCK *const x = &cpi->mb;
    474   VP9_COMMON *const cm = &cpi->common;
    475   MACROBLOCKD *const xd = &x->e_mbd;
    476   TileInfo tile;
    477   struct macroblock_plane *const p = x->plane;
    478   struct macroblockd_plane *const pd = xd->plane;
    479   const PICK_MODE_CONTEXT *ctx = &x->sb64_context;
    480   int i;
    481 
    482   int recon_yoffset, recon_uvoffset;
    483   YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
    484   YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
    485   YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
    486   int recon_y_stride = lst_yv12->y_stride;
    487   int recon_uv_stride = lst_yv12->uv_stride;
    488   int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height);
    489   int64_t intra_error = 0;
    490   int64_t coded_error = 0;
    491   int64_t sr_coded_error = 0;
    492 
    493   int sum_mvr = 0, sum_mvc = 0;
    494   int sum_mvr_abs = 0, sum_mvc_abs = 0;
    495   int64_t sum_mvrs = 0, sum_mvcs = 0;
    496   int mvcount = 0;
    497   int intercount = 0;
    498   int second_ref_count = 0;
    499   int intrapenalty = 256;
    500   int neutral_count = 0;
    501   int new_mv_count = 0;
    502   int sum_in_vectors = 0;
    503   uint32_t lastmv_as_int = 0;
    504   struct twopass_rc *twopass = &cpi->twopass;
    505   const MV zero_mv = {0, 0};
    506   const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
    507 
    508   vp9_clear_system_state();
    509 
    510   if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
    511     MV_REFERENCE_FRAME ref_frame = LAST_FRAME;
    512     const YV12_BUFFER_CONFIG *scaled_ref_buf = NULL;
    513     twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass;
    514 
    515     vp9_scale_references(cpi);
    516 
    517     // Use either last frame or alt frame for motion search.
    518     if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
    519       scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
    520       ref_frame = LAST_FRAME;
    521     } else if (cpi->ref_frame_flags & VP9_ALT_FLAG) {
    522       scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, ALTREF_FRAME);
    523       ref_frame = ALTREF_FRAME;
    524     }
    525 
    526     if (scaled_ref_buf != NULL) {
    527       // Update the stride since we are using scaled reference buffer
    528       first_ref_buf = scaled_ref_buf;
    529       recon_y_stride = first_ref_buf->y_stride;
    530       recon_uv_stride = first_ref_buf->uv_stride;
    531       uv_mb_height = 16 >> (first_ref_buf->y_height > first_ref_buf->uv_height);
    532     }
    533 
    534     // Disable golden frame for svc first pass for now.
    535     gld_yv12 = NULL;
    536     set_ref_ptrs(cm, xd, ref_frame, NONE);
    537   }
    538 
    539   vp9_setup_src_planes(x, cpi->Source, 0, 0);
    540   vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
    541   vp9_setup_dst_planes(xd, new_yv12, 0, 0);
    542 
    543   xd->mi = cm->mi_grid_visible;
    544   xd->mi[0] = cm->mi;
    545 
    546   vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
    547 
    548   vp9_frame_init_quantizer(cpi);
    549 
    550   for (i = 0; i < MAX_MB_PLANE; ++i) {
    551     p[i].coeff = ctx->coeff_pbuf[i][1];
    552     p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
    553     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
    554     p[i].eobs = ctx->eobs_pbuf[i][1];
    555   }
    556   x->skip_recode = 0;
    557 
    558   vp9_init_mv_probs(cm);
    559   vp9_initialize_rd_consts(cpi);
    560 
    561   // Tiling is ignored in the first pass.
    562   vp9_tile_init(&tile, cm, 0, 0);
    563 
    564   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
    565     int_mv best_ref_mv;
    566 
    567     best_ref_mv.as_int = 0;
    568 
    569     // Reset above block coeffs.
    570     xd->up_available = (mb_row != 0);
    571     recon_yoffset = (mb_row * recon_y_stride * 16);
    572     recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height);
    573 
    574     // Set up limit values for motion vectors to prevent them extending
    575     // outside the UMV borders.
    576     x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
    577     x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
    578                     + BORDER_MV_PIXELS_B16;
    579 
    580     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
    581       int this_error;
    582       const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
    583       double error_weight = 1.0;
    584       const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
    585 
    586       vp9_clear_system_state();
    587 
    588       xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
    589       xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
    590       xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
    591       xd->left_available = (mb_col != 0);
    592       xd->mi[0]->mbmi.sb_type = bsize;
    593       xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME;
    594       set_mi_row_col(xd, &tile,
    595                      mb_row << 1, num_8x8_blocks_high_lookup[bsize],
    596                      mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
    597                      cm->mi_rows, cm->mi_cols);
    598 
    599       if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
    600         const int energy = vp9_block_energy(cpi, x, bsize);
    601         error_weight = vp9_vaq_inv_q_ratio(energy);
    602       }
    603 
    604       // Do intra 16x16 prediction.
    605       this_error = vp9_encode_intra(x, use_dc_pred);
    606       if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
    607         vp9_clear_system_state();
    608         this_error = (int)(this_error * error_weight);
    609       }
    610 
    611       // Intrapenalty below deals with situations where the intra and inter
    612       // error scores are very low (e.g. a plain black frame).
    613       // We do not have special cases in first pass for 0,0 and nearest etc so
    614       // all inter modes carry an overhead cost estimate for the mv.
    615       // When the error score is very low this causes us to pick all or lots of
    616       // INTRA modes and throw lots of key frames.
    617       // This penalty adds a cost matching that of a 0,0 mv to the intra case.
    618       this_error += intrapenalty;
    619 
    620       // Accumulate the intra error.
    621       intra_error += (int64_t)this_error;
    622 
    623       // Set up limit values for motion vectors to prevent them extending
    624       // outside the UMV borders.
    625       x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
    626       x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
    627 
    628       // Other than for the first frame do a motion search.
    629       if (cm->current_video_frame > 0) {
    630         int tmp_err, motion_error;
    631         int_mv mv, tmp_mv;
    632 
    633         xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
    634         motion_error = zz_motion_search(x);
    635         // Assume 0,0 motion with no mv overhead.
    636         mv.as_int = tmp_mv.as_int = 0;
    637 
    638         // Test last reference frame using the previous best mv as the
    639         // starting point (best reference) for the search.
    640         first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv,
    641                                  &motion_error);
    642         if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
    643           vp9_clear_system_state();
    644           motion_error = (int)(motion_error * error_weight);
    645         }
    646 
    647         // If the current best reference mv is not centered on 0,0 then do a 0,0
    648         // based search as well.
    649         if (best_ref_mv.as_int) {
    650           tmp_err = INT_MAX;
    651           first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv,
    652                                    &tmp_err);
    653           if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
    654             vp9_clear_system_state();
    655             tmp_err = (int)(tmp_err * error_weight);
    656           }
    657 
    658           if (tmp_err < motion_error) {
    659             motion_error = tmp_err;
    660             mv.as_int = tmp_mv.as_int;
    661           }
    662         }
    663 
    664         // Search in an older reference frame.
    665         if (cm->current_video_frame > 1 && gld_yv12 != NULL) {
    666           // Assume 0,0 motion with no mv overhead.
    667           int gf_motion_error;
    668 
    669           xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
    670           gf_motion_error = zz_motion_search(x);
    671 
    672           first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv,
    673                                    &gf_motion_error);
    674           if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
    675             vp9_clear_system_state();
    676             gf_motion_error = (int)(gf_motion_error * error_weight);
    677           }
    678 
    679           if (gf_motion_error < motion_error && gf_motion_error < this_error)
    680             ++second_ref_count;
    681 
    682           // Reset to last frame as reference buffer.
    683           xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
    684           xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
    685           xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
    686 
    687           // In accumulating a score for the older reference frame take the
    688           // best of the motion predicted score and the intra coded error
    689           // (just as will be done for) accumulation of "coded_error" for
    690           // the last frame.
    691           if (gf_motion_error < this_error)
    692             sr_coded_error += gf_motion_error;
    693           else
    694             sr_coded_error += this_error;
    695         } else {
    696           sr_coded_error += motion_error;
    697         }
    698         // Start by assuming that intra mode is best.
    699         best_ref_mv.as_int = 0;
    700 
    701         if (motion_error <= this_error) {
    702           // Keep a count of cases where the inter and intra were very close
    703           // and very low. This helps with scene cut detection for example in
    704           // cropped clips with black bars at the sides or top and bottom.
    705           if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
    706               this_error < 2 * intrapenalty)
    707             ++neutral_count;
    708 
    709           mv.as_mv.row *= 8;
    710           mv.as_mv.col *= 8;
    711           this_error = motion_error;
    712           xd->mi[0]->mbmi.mode = NEWMV;
    713           xd->mi[0]->mbmi.mv[0] = mv;
    714           xd->mi[0]->mbmi.tx_size = TX_4X4;
    715           xd->mi[0]->mbmi.ref_frame[0] = LAST_FRAME;
    716           xd->mi[0]->mbmi.ref_frame[1] = NONE;
    717           vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
    718           vp9_encode_sby_pass1(x, bsize);
    719           sum_mvr += mv.as_mv.row;
    720           sum_mvr_abs += abs(mv.as_mv.row);
    721           sum_mvc += mv.as_mv.col;
    722           sum_mvc_abs += abs(mv.as_mv.col);
    723           sum_mvrs += mv.as_mv.row * mv.as_mv.row;
    724           sum_mvcs += mv.as_mv.col * mv.as_mv.col;
    725           ++intercount;
    726 
    727           best_ref_mv.as_int = mv.as_int;
    728 
    729           if (mv.as_int) {
    730             ++mvcount;
    731 
    732             // Non-zero vector, was it different from the last non zero vector?
    733             if (mv.as_int != lastmv_as_int)
    734               ++new_mv_count;
    735             lastmv_as_int = mv.as_int;
    736 
    737             // Does the row vector point inwards or outwards?
    738             if (mb_row < cm->mb_rows / 2) {
    739               if (mv.as_mv.row > 0)
    740                 --sum_in_vectors;
    741               else if (mv.as_mv.row < 0)
    742                 ++sum_in_vectors;
    743             } else if (mb_row > cm->mb_rows / 2) {
    744               if (mv.as_mv.row > 0)
    745                 ++sum_in_vectors;
    746               else if (mv.as_mv.row < 0)
    747                 --sum_in_vectors;
    748             }
    749 
    750             // Does the col vector point inwards or outwards?
    751             if (mb_col < cm->mb_cols / 2) {
    752               if (mv.as_mv.col > 0)
    753                 --sum_in_vectors;
    754               else if (mv.as_mv.col < 0)
    755                 ++sum_in_vectors;
    756             } else if (mb_col > cm->mb_cols / 2) {
    757               if (mv.as_mv.col > 0)
    758                 ++sum_in_vectors;
    759               else if (mv.as_mv.col < 0)
    760                 --sum_in_vectors;
    761             }
    762           }
    763         }
    764       } else {
    765         sr_coded_error += (int64_t)this_error;
    766       }
    767       coded_error += (int64_t)this_error;
    768 
    769       // Adjust to the next column of MBs.
    770       x->plane[0].src.buf += 16;
    771       x->plane[1].src.buf += uv_mb_height;
    772       x->plane[2].src.buf += uv_mb_height;
    773 
    774       recon_yoffset += 16;
    775       recon_uvoffset += uv_mb_height;
    776     }
    777 
    778     // Adjust to the next row of MBs.
    779     x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
    780     x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
    781                            uv_mb_height * cm->mb_cols;
    782     x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
    783                            uv_mb_height * cm->mb_cols;
    784 
    785     vp9_clear_system_state();
    786   }
    787 
    788   vp9_clear_system_state();
    789   {
    790     FIRSTPASS_STATS fps;
    791 
    792     fps.frame = cm->current_video_frame;
    793     fps.spatial_layer_id = cpi->svc.spatial_layer_id;
    794     fps.intra_error = (double)(intra_error >> 8);
    795     fps.coded_error = (double)(coded_error >> 8);
    796     fps.sr_coded_error = (double)(sr_coded_error >> 8);
    797     fps.ssim_weighted_pred_err = fps.coded_error * simple_weight(cpi->Source);
    798     fps.count = 1.0;
    799     fps.pcnt_inter = (double)intercount / cm->MBs;
    800     fps.pcnt_second_ref = (double)second_ref_count / cm->MBs;
    801     fps.pcnt_neutral = (double)neutral_count / cm->MBs;
    802 
    803     if (mvcount > 0) {
    804       fps.MVr = (double)sum_mvr / mvcount;
    805       fps.mvr_abs = (double)sum_mvr_abs / mvcount;
    806       fps.MVc = (double)sum_mvc / mvcount;
    807       fps.mvc_abs = (double)sum_mvc_abs / mvcount;
    808       fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount;
    809       fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount;
    810       fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
    811       fps.new_mv_count = new_mv_count;
    812       fps.pcnt_motion = (double)mvcount / cm->MBs;
    813     } else {
    814       fps.MVr = 0.0;
    815       fps.mvr_abs = 0.0;
    816       fps.MVc = 0.0;
    817       fps.mvc_abs = 0.0;
    818       fps.MVrv = 0.0;
    819       fps.MVcv = 0.0;
    820       fps.mv_in_out_count = 0.0;
    821       fps.new_mv_count = 0.0;
    822       fps.pcnt_motion = 0.0;
    823     }
    824 
    825     // TODO(paulwilkins):  Handle the case when duration is set to 0, or
    826     // something less than the full time between subsequent values of
    827     // cpi->source_time_stamp.
    828     fps.duration = (double)(cpi->source->ts_end - cpi->source->ts_start);
    829 
    830     // Don't want to do output stats with a stack variable!
    831     twopass->this_frame_stats = fps;
    832     output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
    833     accumulate_stats(&twopass->total_stats, &fps);
    834   }
    835 
    836   // Copy the previous Last Frame back into gf and and arf buffers if
    837   // the prediction is good enough... but also don't allow it to lag too far.
    838   if ((twopass->sr_update_lag > 3) ||
    839       ((cm->current_video_frame > 0) &&
    840        (twopass->this_frame_stats.pcnt_inter > 0.20) &&
    841        ((twopass->this_frame_stats.intra_error /
    842          DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
    843     if (gld_yv12 != NULL) {
    844       vp8_yv12_copy_frame(lst_yv12, gld_yv12);
    845     }
    846     twopass->sr_update_lag = 1;
    847   } else {
    848     ++twopass->sr_update_lag;
    849   }
    850 
    851   if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
    852     vp9_update_reference_frames(cpi);
    853   } else {
    854     // Swap frame pointers so last frame refers to the frame we just compressed.
    855     swap_yv12(lst_yv12, new_yv12);
    856   }
    857 
    858   vp9_extend_frame_borders(lst_yv12);
    859 
    860   // Special case for the first frame. Copy into the GF buffer as a second
    861   // reference.
    862   if (cm->current_video_frame == 0 && gld_yv12 != NULL) {
    863     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
    864   }
    865 
    866   // Use this to see what the first pass reconstruction looks like.
    867   if (0) {
    868     char filename[512];
    869     FILE *recon_file;
    870     snprintf(filename, sizeof(filename), "enc%04d.yuv",
    871              (int)cm->current_video_frame);
    872 
    873     if (cm->current_video_frame == 0)
    874       recon_file = fopen(filename, "wb");
    875     else
    876       recon_file = fopen(filename, "ab");
    877 
    878     (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
    879     fclose(recon_file);
    880   }
    881 
    882   ++cm->current_video_frame;
    883 }
    884 
    885 static double calc_correction_factor(double err_per_mb,
    886                                      double err_divisor,
    887                                      double pt_low,
    888                                      double pt_high,
    889                                      int q) {
    890   const double error_term = err_per_mb / err_divisor;
    891 
    892   // Adjustment based on actual quantizer to power term.
    893   const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low,
    894                                 pt_high);
    895 
    896   // Calculate correction factor.
    897   if (power_term < 1.0)
    898     assert(error_term >= 0.0);
    899 
    900   return fclamp(pow(error_term, power_term), 0.05, 5.0);
    901 }
    902 
    903 int vp9_twopass_worst_quality(VP9_COMP *cpi, FIRSTPASS_STATS *fpstats,
    904                               int section_target_bandwitdh) {
    905   int q;
    906   const int num_mbs = cpi->common.MBs;
    907   int target_norm_bits_per_mb;
    908   const RATE_CONTROL *const rc = &cpi->rc;
    909 
    910   const double section_err = fpstats->coded_error / fpstats->count;
    911   const double err_per_mb = section_err / num_mbs;
    912   const double speed_term = 1.0 + ((double)cpi->speed * 0.04);
    913 
    914   if (section_target_bandwitdh <= 0)
    915     return rc->worst_quality;          // Highest value allowed
    916 
    917   target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20)
    918                               ? (512 * section_target_bandwitdh) / num_mbs
    919                               : 512 * (section_target_bandwitdh / num_mbs);
    920 
    921   // Try and pick a max Q that will be high enough to encode the
    922   // content at the given rate.
    923   for (q = rc->best_quality; q < rc->worst_quality; ++q) {
    924     const double err_correction_factor = calc_correction_factor(err_per_mb,
    925                                              ERR_DIVISOR, 0.5, 0.90, q);
    926     const int bits_per_mb_at_this_q =
    927       vp9_rc_bits_per_mb(INTER_FRAME, q, (err_correction_factor * speed_term));
    928     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
    929       break;
    930   }
    931 
    932   // Restriction on active max q for constrained quality mode.
    933   if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
    934     q = MAX(q, cpi->cq_target_quality);
    935 
    936   return q;
    937 }
    938 
    939 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate);
    940 
    941 void vp9_init_second_pass(VP9_COMP *cpi) {
    942   FIRSTPASS_STATS this_frame;
    943   const FIRSTPASS_STATS *start_pos;
    944   struct twopass_rc *twopass = &cpi->twopass;
    945   const VP9_CONFIG *const oxcf = &cpi->oxcf;
    946   const int is_spatial_svc = (cpi->svc.number_spatial_layers > 1) &&
    947                              (cpi->svc.number_temporal_layers == 1);
    948   double frame_rate;
    949 
    950   if (is_spatial_svc) {
    951     twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass;
    952   }
    953 
    954   zero_stats(&twopass->total_stats);
    955   zero_stats(&twopass->total_left_stats);
    956 
    957   if (!twopass->stats_in_end)
    958     return;
    959 
    960   twopass->total_stats = *twopass->stats_in_end;
    961   twopass->total_left_stats = twopass->total_stats;
    962 
    963   frame_rate = 10000000.0 * twopass->total_stats.count /
    964                twopass->total_stats.duration;
    965   // Each frame can have a different duration, as the frame rate in the source
    966   // isn't guaranteed to be constant. The frame rate prior to the first frame
    967   // encoded in the second pass is a guess. However, the sum duration is not.
    968   // It is calculated based on the actual durations of all frames from the
    969   // first pass.
    970 
    971   if (is_spatial_svc) {
    972     vp9_update_spatial_layer_framerate(cpi, frame_rate);
    973     twopass->bits_left =
    974         (int64_t)(twopass->total_stats.duration *
    975         cpi->svc.layer_context[cpi->svc.spatial_layer_id].target_bandwidth /
    976         10000000.0);
    977   } else {
    978     vp9_new_framerate(cpi, frame_rate);
    979     twopass->bits_left = (int64_t)(twopass->total_stats.duration *
    980                                    oxcf->target_bandwidth / 10000000.0);
    981   }
    982 
    983   cpi->output_framerate = oxcf->framerate;
    984 
    985   // Calculate a minimum intra value to be used in determining the IIratio
    986   // scores used in the second pass. We have this minimum to make sure
    987   // that clips that are static but "low complexity" in the intra domain
    988   // are still boosted appropriately for KF/GF/ARF.
    989   if (!is_spatial_svc) {
    990     // We don't know the number of MBs for each layer at this point.
    991     // So we will do it later.
    992     twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
    993     twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
    994   }
    995 
    996   // This variable monitors how far behind the second ref update is lagging.
    997   twopass->sr_update_lag = 1;
    998 
    999   // Scan the first pass file and calculate an average Intra / Inter error
   1000   // score ratio for the sequence.
   1001   {
   1002     double sum_iiratio = 0.0;
   1003     start_pos = twopass->stats_in;
   1004 
   1005     while (input_stats(twopass, &this_frame) != EOF) {
   1006       const double iiratio = this_frame.intra_error /
   1007                                  DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
   1008       sum_iiratio += fclamp(iiratio, 1.0, 20.0);
   1009     }
   1010 
   1011     twopass->avg_iiratio = sum_iiratio /
   1012         DOUBLE_DIVIDE_CHECK((double)twopass->total_stats.count);
   1013 
   1014     reset_fpf_position(twopass, start_pos);
   1015   }
   1016 
   1017   // Scan the first pass file and calculate a modified total error based upon
   1018   // the bias/power function used to allocate bits.
   1019   {
   1020     double av_error = twopass->total_stats.ssim_weighted_pred_err /
   1021                       DOUBLE_DIVIDE_CHECK(twopass->total_stats.count);
   1022 
   1023     start_pos = twopass->stats_in;
   1024 
   1025     twopass->modified_error_total = 0.0;
   1026     twopass->modified_error_min =
   1027       (av_error * oxcf->two_pass_vbrmin_section) / 100;
   1028     twopass->modified_error_max =
   1029       (av_error * oxcf->two_pass_vbrmax_section) / 100;
   1030 
   1031     while (input_stats(twopass, &this_frame) != EOF) {
   1032       twopass->modified_error_total +=
   1033           calculate_modified_err(cpi, &this_frame);
   1034     }
   1035     twopass->modified_error_left = twopass->modified_error_total;
   1036 
   1037     reset_fpf_position(twopass, start_pos);
   1038   }
   1039 }
   1040 
   1041 // This function gives an estimate of how badly we believe the prediction
   1042 // quality is decaying from frame to frame.
   1043 static double get_prediction_decay_rate(const VP9_COMMON *cm,
   1044                                         const FIRSTPASS_STATS *next_frame) {
   1045   // Look at the observed drop in prediction quality between the last frame
   1046   // and the GF buffer (which contains an older frame).
   1047   const double mb_sr_err_diff = (next_frame->sr_coded_error -
   1048                                      next_frame->coded_error) / cm->MBs;
   1049   const double second_ref_decay = mb_sr_err_diff <= 512.0
   1050       ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0)
   1051       : 0.85;
   1052 
   1053   return MIN(second_ref_decay, next_frame->pcnt_inter);
   1054 }
   1055 
   1056 // Function to test for a condition where a complex transition is followed
   1057 // by a static section. For example in slide shows where there is a fade
   1058 // between slides. This is to help with more optimal kf and gf positioning.
   1059 static int detect_transition_to_still(VP9_COMP *cpi, int frame_interval,
   1060                                       int still_interval,
   1061                                       double loop_decay_rate,
   1062                                       double last_decay_rate) {
   1063   int trans_to_still = 0;
   1064 
   1065   // Break clause to detect very still sections after motion
   1066   // For example a static image after a fade or other transition
   1067   // instead of a clean scene cut.
   1068   if (frame_interval > MIN_GF_INTERVAL &&
   1069       loop_decay_rate >= 0.999 &&
   1070       last_decay_rate < 0.9) {
   1071     int j;
   1072     const FIRSTPASS_STATS *position = cpi->twopass.stats_in;
   1073     FIRSTPASS_STATS tmp_next_frame;
   1074 
   1075     // Look ahead a few frames to see if static condition persists...
   1076     for (j = 0; j < still_interval; ++j) {
   1077       if (EOF == input_stats(&cpi->twopass, &tmp_next_frame))
   1078         break;
   1079 
   1080       if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999)
   1081         break;
   1082     }
   1083 
   1084     reset_fpf_position(&cpi->twopass, position);
   1085 
   1086     // Only if it does do we signal a transition to still.
   1087     if (j == still_interval)
   1088       trans_to_still = 1;
   1089   }
   1090 
   1091   return trans_to_still;
   1092 }
   1093 
   1094 // This function detects a flash through the high relative pcnt_second_ref
   1095 // score in the frame following a flash frame. The offset passed in should
   1096 // reflect this.
   1097 static int detect_flash(const struct twopass_rc *twopass, int offset) {
   1098   FIRSTPASS_STATS next_frame;
   1099 
   1100   int flash_detected = 0;
   1101 
   1102   // Read the frame data.
   1103   // The return is FALSE (no flash detected) if not a valid frame
   1104   if (read_frame_stats(twopass, &next_frame, offset) != EOF) {
   1105     // What we are looking for here is a situation where there is a
   1106     // brief break in prediction (such as a flash) but subsequent frames
   1107     // are reasonably well predicted by an earlier (pre flash) frame.
   1108     // The recovery after a flash is indicated by a high pcnt_second_ref
   1109     // compared to pcnt_inter.
   1110     if (next_frame.pcnt_second_ref > next_frame.pcnt_inter &&
   1111         next_frame.pcnt_second_ref >= 0.5)
   1112       flash_detected = 1;
   1113   }
   1114 
   1115   return flash_detected;
   1116 }
   1117 
   1118 // Update the motion related elements to the GF arf boost calculation.
   1119 static void accumulate_frame_motion_stats(
   1120   FIRSTPASS_STATS *this_frame,
   1121   double *this_frame_mv_in_out,
   1122   double *mv_in_out_accumulator,
   1123   double *abs_mv_in_out_accumulator,
   1124   double *mv_ratio_accumulator) {
   1125   double motion_pct;
   1126 
   1127   // Accumulate motion stats.
   1128   motion_pct = this_frame->pcnt_motion;
   1129 
   1130   // Accumulate Motion In/Out of frame stats.
   1131   *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
   1132   *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
   1133   *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct);
   1134 
   1135   // Accumulate a measure of how uniform (or conversely how random)
   1136   // the motion field is (a ratio of absmv / mv).
   1137   if (motion_pct > 0.05) {
   1138     const double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
   1139                            DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
   1140 
   1141     const double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
   1142                            DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
   1143 
   1144     *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs)
   1145       ? (this_frame_mvr_ratio * motion_pct)
   1146       : this_frame->mvr_abs * motion_pct;
   1147 
   1148     *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs)
   1149       ? (this_frame_mvc_ratio * motion_pct)
   1150       : this_frame->mvc_abs * motion_pct;
   1151   }
   1152 }
   1153 
   1154 // Calculate a baseline boost number for the current frame.
   1155 static double calc_frame_boost(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame,
   1156                                double this_frame_mv_in_out) {
   1157   double frame_boost;
   1158 
   1159   // Underlying boost factor is based on inter intra error ratio.
   1160   if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
   1161     frame_boost = (IIFACTOR * this_frame->intra_error /
   1162                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
   1163   else
   1164     frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
   1165                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
   1166 
   1167   // Increase boost for frames where new data coming into frame (e.g. zoom out).
   1168   // Slightly reduce boost if there is a net balance of motion out of the frame
   1169   // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
   1170   if (this_frame_mv_in_out > 0.0)
   1171     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
   1172   // In the extreme case the boost is halved.
   1173   else
   1174     frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
   1175 
   1176   return MIN(frame_boost, GF_RMAX);
   1177 }
   1178 
   1179 static int calc_arf_boost(VP9_COMP *cpi, int offset,
   1180                           int f_frames, int b_frames,
   1181                           int *f_boost, int *b_boost) {
   1182   FIRSTPASS_STATS this_frame;
   1183   struct twopass_rc *const twopass = &cpi->twopass;
   1184   int i;
   1185   double boost_score = 0.0;
   1186   double mv_ratio_accumulator = 0.0;
   1187   double decay_accumulator = 1.0;
   1188   double this_frame_mv_in_out = 0.0;
   1189   double mv_in_out_accumulator = 0.0;
   1190   double abs_mv_in_out_accumulator = 0.0;
   1191   int arf_boost;
   1192   int flash_detected = 0;
   1193 
   1194   // Search forward from the proposed arf/next gf position.
   1195   for (i = 0; i < f_frames; ++i) {
   1196     if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF)
   1197       break;
   1198 
   1199     // Update the motion related elements to the boost calculation.
   1200     accumulate_frame_motion_stats(&this_frame,
   1201                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
   1202                                   &abs_mv_in_out_accumulator,
   1203                                   &mv_ratio_accumulator);
   1204 
   1205     // We want to discount the flash frame itself and the recovery
   1206     // frame that follows as both will have poor scores.
   1207     flash_detected = detect_flash(twopass, i + offset) ||
   1208                      detect_flash(twopass, i + offset + 1);
   1209 
   1210     // Accumulate the effect of prediction quality decay.
   1211     if (!flash_detected) {
   1212       decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame);
   1213       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
   1214                           ? MIN_DECAY_FACTOR : decay_accumulator;
   1215     }
   1216 
   1217     boost_score += (decay_accumulator *
   1218                     calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
   1219   }
   1220 
   1221   *f_boost = (int)boost_score;
   1222 
   1223   // Reset for backward looking loop.
   1224   boost_score = 0.0;
   1225   mv_ratio_accumulator = 0.0;
   1226   decay_accumulator = 1.0;
   1227   this_frame_mv_in_out = 0.0;
   1228   mv_in_out_accumulator = 0.0;
   1229   abs_mv_in_out_accumulator = 0.0;
   1230 
   1231   // Search backward towards last gf position.
   1232   for (i = -1; i >= -b_frames; --i) {
   1233     if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF)
   1234       break;
   1235 
   1236     // Update the motion related elements to the boost calculation.
   1237     accumulate_frame_motion_stats(&this_frame,
   1238                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
   1239                                   &abs_mv_in_out_accumulator,
   1240                                   &mv_ratio_accumulator);
   1241 
   1242     // We want to discount the the flash frame itself and the recovery
   1243     // frame that follows as both will have poor scores.
   1244     flash_detected = detect_flash(twopass, i + offset) ||
   1245                      detect_flash(twopass, i + offset + 1);
   1246 
   1247     // Cumulative effect of prediction quality decay.
   1248     if (!flash_detected) {
   1249       decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame);
   1250       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
   1251                               ? MIN_DECAY_FACTOR : decay_accumulator;
   1252     }
   1253 
   1254     boost_score += (decay_accumulator *
   1255                     calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
   1256   }
   1257   *b_boost = (int)boost_score;
   1258 
   1259   arf_boost = (*f_boost + *b_boost);
   1260   if (arf_boost < ((b_frames + f_frames) * 20))
   1261     arf_boost = ((b_frames + f_frames) * 20);
   1262 
   1263   return arf_boost;
   1264 }
   1265 
   1266 #if CONFIG_MULTIPLE_ARF
   1267 // Work out the frame coding order for a GF or an ARF group.
   1268 // The current implementation codes frames in their natural order for a
   1269 // GF group, and inserts additional ARFs into an ARF group using a
   1270 // binary split approach.
   1271 // NOTE: this function is currently implemented recursively.
   1272 static void schedule_frames(VP9_COMP *cpi, const int start, const int end,
   1273                             const int arf_idx, const int gf_or_arf_group,
   1274                             const int level) {
   1275   int i, abs_end, half_range;
   1276   int *cfo = cpi->frame_coding_order;
   1277   int idx = cpi->new_frame_coding_order_period;
   1278 
   1279   // If (end < 0) an ARF should be coded at position (-end).
   1280   assert(start >= 0);
   1281 
   1282   // printf("start:%d end:%d\n", start, end);
   1283 
   1284   // GF Group: code frames in logical order.
   1285   if (gf_or_arf_group == 0) {
   1286     assert(end >= start);
   1287     for (i = start; i <= end; ++i) {
   1288       cfo[idx] = i;
   1289       cpi->arf_buffer_idx[idx] = arf_idx;
   1290       cpi->arf_weight[idx] = -1;
   1291       ++idx;
   1292     }
   1293     cpi->new_frame_coding_order_period = idx;
   1294     return;
   1295   }
   1296 
   1297   // ARF Group: Work out the ARF schedule and mark ARF frames as negative.
   1298   if (end < 0) {
   1299     // printf("start:%d end:%d\n", -end, -end);
   1300     // ARF frame is at the end of the range.
   1301     cfo[idx] = end;
   1302     // What ARF buffer does this ARF use as predictor.
   1303     cpi->arf_buffer_idx[idx] = (arf_idx > 2) ? (arf_idx - 1) : 2;
   1304     cpi->arf_weight[idx] = level;
   1305     ++idx;
   1306     abs_end = -end;
   1307   } else {
   1308     abs_end = end;
   1309   }
   1310 
   1311   half_range = (abs_end - start) >> 1;
   1312 
   1313   // ARFs may not be adjacent, they must be separated by at least
   1314   // MIN_GF_INTERVAL non-ARF frames.
   1315   if ((start + MIN_GF_INTERVAL) >= (abs_end - MIN_GF_INTERVAL)) {
   1316     // printf("start:%d end:%d\n", start, abs_end);
   1317     // Update the coding order and active ARF.
   1318     for (i = start; i <= abs_end; ++i) {
   1319       cfo[idx] = i;
   1320       cpi->arf_buffer_idx[idx] = arf_idx;
   1321       cpi->arf_weight[idx] = -1;
   1322       ++idx;
   1323     }
   1324     cpi->new_frame_coding_order_period = idx;
   1325   } else {
   1326     // Place a new ARF at the mid-point of the range.
   1327     cpi->new_frame_coding_order_period = idx;
   1328     schedule_frames(cpi, start, -(start + half_range), arf_idx + 1,
   1329                     gf_or_arf_group, level + 1);
   1330     schedule_frames(cpi, start + half_range + 1, abs_end, arf_idx,
   1331                     gf_or_arf_group, level + 1);
   1332   }
   1333 }
   1334 
   1335 #define FIXED_ARF_GROUP_SIZE 16
   1336 
   1337 void define_fixed_arf_period(VP9_COMP *cpi) {
   1338   int i;
   1339   int max_level = INT_MIN;
   1340 
   1341   assert(cpi->multi_arf_enabled);
   1342   assert(cpi->oxcf.lag_in_frames >= FIXED_ARF_GROUP_SIZE);
   1343 
   1344   // Save the weight of the last frame in the sequence before next
   1345   // sequence pattern overwrites it.
   1346   cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
   1347   assert(cpi->this_frame_weight >= 0);
   1348 
   1349   cpi->twopass.gf_zeromotion_pct = 0;
   1350 
   1351   // Initialize frame coding order variables.
   1352   cpi->new_frame_coding_order_period = 0;
   1353   cpi->next_frame_in_order = 0;
   1354   cpi->arf_buffered = 0;
   1355   vp9_zero(cpi->frame_coding_order);
   1356   vp9_zero(cpi->arf_buffer_idx);
   1357   vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
   1358 
   1359   if (cpi->rc.frames_to_key <= (FIXED_ARF_GROUP_SIZE + 8)) {
   1360     // Setup a GF group close to the keyframe.
   1361     cpi->rc.source_alt_ref_pending = 0;
   1362     cpi->rc.baseline_gf_interval = cpi->rc.frames_to_key;
   1363     schedule_frames(cpi, 0, (cpi->rc.baseline_gf_interval - 1), 2, 0, 0);
   1364   } else {
   1365     // Setup a fixed period ARF group.
   1366     cpi->rc.source_alt_ref_pending = 1;
   1367     cpi->rc.baseline_gf_interval = FIXED_ARF_GROUP_SIZE;
   1368     schedule_frames(cpi, 0, -(cpi->rc.baseline_gf_interval - 1), 2, 1, 0);
   1369   }
   1370 
   1371   // Replace level indicator of -1 with correct level.
   1372   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1373     if (cpi->arf_weight[i] > max_level) {
   1374       max_level = cpi->arf_weight[i];
   1375     }
   1376   }
   1377   ++max_level;
   1378   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1379     if (cpi->arf_weight[i] == -1) {
   1380       cpi->arf_weight[i] = max_level;
   1381     }
   1382   }
   1383   cpi->max_arf_level = max_level;
   1384 #if 0
   1385   printf("\nSchedule: ");
   1386   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1387     printf("%4d ", cpi->frame_coding_order[i]);
   1388   }
   1389   printf("\n");
   1390   printf("ARFref:   ");
   1391   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1392     printf("%4d ", cpi->arf_buffer_idx[i]);
   1393   }
   1394   printf("\n");
   1395   printf("Weight:   ");
   1396   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1397     printf("%4d ", cpi->arf_weight[i]);
   1398   }
   1399   printf("\n");
   1400 #endif
   1401 }
   1402 #endif
   1403 
   1404 // Analyse and define a gf/arf group.
   1405 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   1406   FIRSTPASS_STATS next_frame = { 0 };
   1407   const FIRSTPASS_STATS *start_pos;
   1408   struct twopass_rc *const twopass = &cpi->twopass;
   1409   int i;
   1410   double boost_score = 0.0;
   1411   double old_boost_score = 0.0;
   1412   double gf_group_err = 0.0;
   1413   double gf_first_frame_err = 0.0;
   1414   double mod_frame_err = 0.0;
   1415 
   1416   double mv_ratio_accumulator = 0.0;
   1417   double decay_accumulator = 1.0;
   1418   double zero_motion_accumulator = 1.0;
   1419 
   1420   double loop_decay_rate = 1.00;
   1421   double last_loop_decay_rate = 1.00;
   1422 
   1423   double this_frame_mv_in_out = 0.0;
   1424   double mv_in_out_accumulator = 0.0;
   1425   double abs_mv_in_out_accumulator = 0.0;
   1426   double mv_ratio_accumulator_thresh;
   1427   const int max_bits = frame_max_bits(cpi);  // Max bits for a single frame.
   1428 
   1429   unsigned int allow_alt_ref = cpi->oxcf.play_alternate &&
   1430                                cpi->oxcf.lag_in_frames;
   1431 
   1432   int f_boost = 0;
   1433   int b_boost = 0;
   1434   int flash_detected;
   1435   int active_max_gf_interval;
   1436   RATE_CONTROL *const rc = &cpi->rc;
   1437 
   1438   twopass->gf_group_bits = 0;
   1439 
   1440   vp9_clear_system_state();
   1441 
   1442   start_pos = twopass->stats_in;
   1443 
   1444   // Load stats for the current frame.
   1445   mod_frame_err = calculate_modified_err(cpi, this_frame);
   1446 
   1447   // Note the error of the frame at the start of the group. This will be
   1448   // the GF frame error if we code a normal gf.
   1449   gf_first_frame_err = mod_frame_err;
   1450 
   1451   // If this is a key frame or the overlay from a previous arf then
   1452   // the error score / cost of this frame has already been accounted for.
   1453   if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
   1454     gf_group_err -= gf_first_frame_err;
   1455 
   1456   // Motion breakout threshold for loop below depends on image size.
   1457   mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0;
   1458 
   1459   // Work out a maximum interval for the GF.
   1460   // If the image appears completely static we can extend beyond this.
   1461   // The value chosen depends on the active Q range. At low Q we have
   1462   // bits to spare and are better with a smaller interval and smaller boost.
   1463   // At high Q when there are few bits to spare we are better with a longer
   1464   // interval to spread the cost of the GF.
   1465   //
   1466   active_max_gf_interval =
   1467     12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5);
   1468 
   1469   if (active_max_gf_interval > rc->max_gf_interval)
   1470     active_max_gf_interval = rc->max_gf_interval;
   1471 
   1472   i = 0;
   1473   while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
   1474     ++i;
   1475 
   1476     // Accumulate error score of frames in this gf group.
   1477     mod_frame_err = calculate_modified_err(cpi, this_frame);
   1478     gf_group_err += mod_frame_err;
   1479 
   1480     if (EOF == input_stats(twopass, &next_frame))
   1481       break;
   1482 
   1483     // Test for the case where there is a brief flash but the prediction
   1484     // quality back to an earlier frame is then restored.
   1485     flash_detected = detect_flash(twopass, 0);
   1486 
   1487     // Update the motion related elements to the boost calculation.
   1488     accumulate_frame_motion_stats(&next_frame,
   1489                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
   1490                                   &abs_mv_in_out_accumulator,
   1491                                   &mv_ratio_accumulator);
   1492 
   1493     // Accumulate the effect of prediction quality decay.
   1494     if (!flash_detected) {
   1495       last_loop_decay_rate = loop_decay_rate;
   1496       loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
   1497       decay_accumulator = decay_accumulator * loop_decay_rate;
   1498 
   1499       // Monitor for static sections.
   1500       if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
   1501           zero_motion_accumulator) {
   1502         zero_motion_accumulator = next_frame.pcnt_inter -
   1503                                       next_frame.pcnt_motion;
   1504       }
   1505 
   1506       // Break clause to detect very still sections after motion. For example,
   1507       // a static image after a fade or other transition.
   1508       if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
   1509                                      last_loop_decay_rate)) {
   1510         allow_alt_ref = 0;
   1511         break;
   1512       }
   1513     }
   1514 
   1515     // Calculate a boost number for this frame.
   1516     boost_score += (decay_accumulator *
   1517        calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out));
   1518 
   1519     // Break out conditions.
   1520     if (
   1521       // Break at cpi->max_gf_interval unless almost totally static.
   1522       (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
   1523       (
   1524         // Don't break out with a very short interval.
   1525         (i > MIN_GF_INTERVAL) &&
   1526         ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) &&
   1527         (!flash_detected) &&
   1528         ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
   1529          (abs_mv_in_out_accumulator > 3.0) ||
   1530          (mv_in_out_accumulator < -2.0) ||
   1531          ((boost_score - old_boost_score) < IIFACTOR)))) {
   1532       boost_score = old_boost_score;
   1533       break;
   1534     }
   1535 
   1536     *this_frame = next_frame;
   1537 
   1538     old_boost_score = boost_score;
   1539   }
   1540 
   1541   twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
   1542 
   1543   // Don't allow a gf too near the next kf.
   1544   if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) {
   1545     while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) {
   1546       ++i;
   1547 
   1548       if (EOF == input_stats(twopass, this_frame))
   1549         break;
   1550 
   1551       if (i < rc->frames_to_key) {
   1552         mod_frame_err = calculate_modified_err(cpi, this_frame);
   1553         gf_group_err += mod_frame_err;
   1554       }
   1555     }
   1556   }
   1557 
   1558 #if CONFIG_MULTIPLE_ARF
   1559   if (cpi->multi_arf_enabled) {
   1560     // Initialize frame coding order variables.
   1561     cpi->new_frame_coding_order_period = 0;
   1562     cpi->next_frame_in_order = 0;
   1563     cpi->arf_buffered = 0;
   1564     vp9_zero(cpi->frame_coding_order);
   1565     vp9_zero(cpi->arf_buffer_idx);
   1566     vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
   1567   }
   1568 #endif
   1569 
   1570   // Set the interval until the next gf.
   1571   if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
   1572     rc->baseline_gf_interval = i - 1;
   1573   else
   1574     rc->baseline_gf_interval = i;
   1575 
   1576   // Should we use the alternate reference frame.
   1577   if (allow_alt_ref &&
   1578       (i < cpi->oxcf.lag_in_frames) &&
   1579       (i >= MIN_GF_INTERVAL) &&
   1580       // For real scene cuts (not forced kfs) don't allow arf very near kf.
   1581       (rc->next_key_frame_forced ||
   1582       (i <= (rc->frames_to_key - MIN_GF_INTERVAL)))) {
   1583     // Calculate the boost for alt ref.
   1584     rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
   1585                                    &b_boost);
   1586     rc->source_alt_ref_pending = 1;
   1587 
   1588 #if CONFIG_MULTIPLE_ARF
   1589     // Set the ARF schedule.
   1590     if (cpi->multi_arf_enabled) {
   1591       schedule_frames(cpi, 0, -(rc->baseline_gf_interval - 1), 2, 1, 0);
   1592     }
   1593 #endif
   1594   } else {
   1595     rc->gfu_boost = (int)boost_score;
   1596     rc->source_alt_ref_pending = 0;
   1597 #if CONFIG_MULTIPLE_ARF
   1598     // Set the GF schedule.
   1599     if (cpi->multi_arf_enabled) {
   1600       schedule_frames(cpi, 0, rc->baseline_gf_interval - 1, 2, 0, 0);
   1601       assert(cpi->new_frame_coding_order_period ==
   1602              rc->baseline_gf_interval);
   1603     }
   1604 #endif
   1605   }
   1606 
   1607 #if CONFIG_MULTIPLE_ARF
   1608   if (cpi->multi_arf_enabled && (cpi->common.frame_type != KEY_FRAME)) {
   1609     int max_level = INT_MIN;
   1610     // Replace level indicator of -1 with correct level.
   1611     for (i = 0; i < cpi->frame_coding_order_period; ++i) {
   1612       if (cpi->arf_weight[i] > max_level) {
   1613         max_level = cpi->arf_weight[i];
   1614       }
   1615     }
   1616     ++max_level;
   1617     for (i = 0; i < cpi->frame_coding_order_period; ++i) {
   1618       if (cpi->arf_weight[i] == -1) {
   1619         cpi->arf_weight[i] = max_level;
   1620       }
   1621     }
   1622     cpi->max_arf_level = max_level;
   1623   }
   1624 #if 0
   1625   if (cpi->multi_arf_enabled) {
   1626     printf("\nSchedule: ");
   1627     for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1628       printf("%4d ", cpi->frame_coding_order[i]);
   1629     }
   1630     printf("\n");
   1631     printf("ARFref:   ");
   1632     for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1633       printf("%4d ", cpi->arf_buffer_idx[i]);
   1634     }
   1635     printf("\n");
   1636     printf("Weight:   ");
   1637     for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
   1638       printf("%4d ", cpi->arf_weight[i]);
   1639     }
   1640     printf("\n");
   1641   }
   1642 #endif
   1643 #endif
   1644 
   1645   // Calculate the bits to be allocated to the group as a whole.
   1646   if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) {
   1647     twopass->gf_group_bits = (int64_t)(twopass->kf_group_bits *
   1648                 (gf_group_err / twopass->kf_group_error_left));
   1649   } else {
   1650     twopass->gf_group_bits = 0;
   1651   }
   1652   twopass->gf_group_bits = (twopass->gf_group_bits < 0) ?
   1653      0 : (twopass->gf_group_bits > twopass->kf_group_bits) ?
   1654      twopass->kf_group_bits : twopass->gf_group_bits;
   1655 
   1656   // Clip cpi->twopass.gf_group_bits based on user supplied data rate
   1657   // variability limit, cpi->oxcf.two_pass_vbrmax_section.
   1658   if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
   1659     twopass->gf_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
   1660 
   1661   // Reset the file position.
   1662   reset_fpf_position(twopass, start_pos);
   1663 
   1664   // Assign  bits to the arf or gf.
   1665   for (i = 0; i <= (rc->source_alt_ref_pending &&
   1666                     cpi->common.frame_type != KEY_FRAME); ++i) {
   1667     int allocation_chunks;
   1668     int q = rc->last_q[INTER_FRAME];
   1669     int gf_bits;
   1670 
   1671     int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100;
   1672 
   1673     // Set max and minimum boost and hence minimum allocation.
   1674     boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200);
   1675 
   1676     if (rc->source_alt_ref_pending && i == 0)
   1677       allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost;
   1678     else
   1679       allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100);
   1680 
   1681     // Prevent overflow.
   1682     if (boost > 1023) {
   1683       int divisor = boost >> 10;
   1684       boost /= divisor;
   1685       allocation_chunks /= divisor;
   1686     }
   1687 
   1688     // Calculate the number of bits to be spent on the gf or arf based on
   1689     // the boost number.
   1690     gf_bits = (int)((double)boost * (twopass->gf_group_bits /
   1691                   (double)allocation_chunks));
   1692 
   1693     // If the frame that is to be boosted is simpler than the average for
   1694     // the gf/arf group then use an alternative calculation
   1695     // based on the error score of the frame itself.
   1696     if (rc->baseline_gf_interval < 1 ||
   1697         mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) {
   1698       double alt_gf_grp_bits = (double)twopass->kf_group_bits  *
   1699         (mod_frame_err * (double)rc->baseline_gf_interval) /
   1700         DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left);
   1701 
   1702       int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits /
   1703                                            (double)allocation_chunks));
   1704 
   1705       if (gf_bits > alt_gf_bits)
   1706         gf_bits = alt_gf_bits;
   1707     } else {
   1708       // If it is harder than other frames in the group make sure it at
   1709       // least receives an allocation in keeping with its relative error
   1710       // score, otherwise it may be worse off than an "un-boosted" frame.
   1711       int alt_gf_bits = (int)((double)twopass->kf_group_bits *
   1712                         mod_frame_err /
   1713                         DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left));
   1714 
   1715       if (alt_gf_bits > gf_bits)
   1716         gf_bits = alt_gf_bits;
   1717     }
   1718 
   1719     // Don't allow a negative value for gf_bits.
   1720     if (gf_bits < 0)
   1721       gf_bits = 0;
   1722 
   1723     if (i == 0) {
   1724       twopass->gf_bits = gf_bits;
   1725     }
   1726     if (i == 1 ||
   1727         (!rc->source_alt_ref_pending &&
   1728          cpi->common.frame_type != KEY_FRAME)) {
   1729       // Calculate the per frame bit target for this frame.
   1730       vp9_rc_set_frame_target(cpi, gf_bits);
   1731     }
   1732   }
   1733 
   1734   {
   1735     // Adjust KF group bits and error remaining.
   1736     twopass->kf_group_error_left -= (int64_t)gf_group_err;
   1737 
   1738     // If this is an arf update we want to remove the score for the overlay
   1739     // frame at the end which will usually be very cheap to code.
   1740     // The overlay frame has already, in effect, been coded so we want to spread
   1741     // the remaining bits among the other frames.
   1742     // For normal GFs remove the score for the GF itself unless this is
   1743     // also a key frame in which case it has already been accounted for.
   1744     if (rc->source_alt_ref_pending) {
   1745       twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err);
   1746     } else if (cpi->common.frame_type != KEY_FRAME) {
   1747       twopass->gf_group_error_left = (int64_t)(gf_group_err
   1748                                                    - gf_first_frame_err);
   1749     } else {
   1750       twopass->gf_group_error_left = (int64_t)gf_group_err;
   1751     }
   1752 
   1753     // This condition could fail if there are two kfs very close together
   1754     // despite MIN_GF_INTERVAL and would cause a divide by 0 in the
   1755     // calculation of alt_extra_bits.
   1756     if (rc->baseline_gf_interval >= 3) {
   1757       const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost;
   1758 
   1759       if (boost >= 150) {
   1760         const int pct_extra = MIN(20, (boost - 100) / 50);
   1761         const int alt_extra_bits = (int)((
   1762             MAX(twopass->gf_group_bits - twopass->gf_bits, 0) *
   1763             pct_extra) / 100);
   1764         twopass->gf_group_bits -= alt_extra_bits;
   1765       }
   1766     }
   1767   }
   1768 
   1769   if (cpi->common.frame_type != KEY_FRAME) {
   1770     FIRSTPASS_STATS sectionstats;
   1771 
   1772     zero_stats(&sectionstats);
   1773     reset_fpf_position(twopass, start_pos);
   1774 
   1775     for (i = 0; i < rc->baseline_gf_interval; ++i) {
   1776       input_stats(twopass, &next_frame);
   1777       accumulate_stats(&sectionstats, &next_frame);
   1778     }
   1779 
   1780     avg_stats(&sectionstats);
   1781 
   1782     twopass->section_intra_rating = (int)
   1783       (sectionstats.intra_error /
   1784       DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
   1785 
   1786     reset_fpf_position(twopass, start_pos);
   1787   }
   1788 }
   1789 
   1790 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
   1791 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   1792   int target_frame_size;
   1793   double modified_err;
   1794   double err_fraction;
   1795   const int max_bits = frame_max_bits(cpi);  // Max for a single frame.
   1796 
   1797   // Calculate modified prediction error used in bit allocation.
   1798   modified_err = calculate_modified_err(cpi, this_frame);
   1799 
   1800   if (cpi->twopass.gf_group_error_left > 0)
   1801     // What portion of the remaining GF group error is used by this frame.
   1802     err_fraction = modified_err / cpi->twopass.gf_group_error_left;
   1803   else
   1804     err_fraction = 0.0;
   1805 
   1806   // How many of those bits available for allocation should we give it?
   1807   target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
   1808 
   1809   // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at
   1810   // the top end.
   1811   target_frame_size = clamp(target_frame_size, 0,
   1812                             MIN(max_bits, (int)cpi->twopass.gf_group_bits));
   1813 
   1814   // Adjust error and bits remaining.
   1815   cpi->twopass.gf_group_error_left -= (int64_t)modified_err;
   1816 
   1817   // Per frame bit target for this frame.
   1818   vp9_rc_set_frame_target(cpi, target_frame_size);
   1819 }
   1820 
   1821 static int test_candidate_kf(VP9_COMP *cpi,
   1822                              const FIRSTPASS_STATS *last_frame,
   1823                              const FIRSTPASS_STATS *this_frame,
   1824                              const FIRSTPASS_STATS *next_frame) {
   1825   int is_viable_kf = 0;
   1826 
   1827   // Does the frame satisfy the primary criteria of a key frame?
   1828   // If so, then examine how well it predicts subsequent frames.
   1829   if ((this_frame->pcnt_second_ref < 0.10) &&
   1830       (next_frame->pcnt_second_ref < 0.10) &&
   1831       ((this_frame->pcnt_inter < 0.05) ||
   1832        (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) &&
   1833         ((this_frame->intra_error /
   1834           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
   1835         ((fabs(last_frame->coded_error - this_frame->coded_error) /
   1836               DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) ||
   1837          (fabs(last_frame->intra_error - this_frame->intra_error) /
   1838               DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) ||
   1839          ((next_frame->intra_error /
   1840            DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
   1841     int i;
   1842     const FIRSTPASS_STATS *start_pos = cpi->twopass.stats_in;
   1843     FIRSTPASS_STATS local_next_frame = *next_frame;
   1844     double boost_score = 0.0;
   1845     double old_boost_score = 0.0;
   1846     double decay_accumulator = 1.0;
   1847 
   1848     // Examine how well the key frame predicts subsequent frames.
   1849     for (i = 0; i < 16; ++i) {
   1850       double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
   1851                              DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
   1852 
   1853       if (next_iiratio > RMAX)
   1854         next_iiratio = RMAX;
   1855 
   1856       // Cumulative effect of decay in prediction quality.
   1857       if (local_next_frame.pcnt_inter > 0.85)
   1858         decay_accumulator *= local_next_frame.pcnt_inter;
   1859       else
   1860         decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
   1861 
   1862       // Keep a running total.
   1863       boost_score += (decay_accumulator * next_iiratio);
   1864 
   1865       // Test various breakout clauses.
   1866       if ((local_next_frame.pcnt_inter < 0.05) ||
   1867           (next_iiratio < 1.5) ||
   1868           (((local_next_frame.pcnt_inter -
   1869              local_next_frame.pcnt_neutral) < 0.20) &&
   1870            (next_iiratio < 3.0)) ||
   1871           ((boost_score - old_boost_score) < 3.0) ||
   1872           (local_next_frame.intra_error < 200)) {
   1873         break;
   1874       }
   1875 
   1876       old_boost_score = boost_score;
   1877 
   1878       // Get the next frame details
   1879       if (EOF == input_stats(&cpi->twopass, &local_next_frame))
   1880         break;
   1881     }
   1882 
   1883     // If there is tolerable prediction for at least the next 3 frames then
   1884     // break out else discard this potential key frame and move on
   1885     if (boost_score > 30.0 && (i > 3)) {
   1886       is_viable_kf = 1;
   1887     } else {
   1888       // Reset the file position
   1889       reset_fpf_position(&cpi->twopass, start_pos);
   1890 
   1891       is_viable_kf = 0;
   1892     }
   1893   }
   1894 
   1895   return is_viable_kf;
   1896 }
   1897 
   1898 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   1899   int i, j;
   1900   RATE_CONTROL *const rc = &cpi->rc;
   1901   struct twopass_rc *const twopass = &cpi->twopass;
   1902   FIRSTPASS_STATS last_frame;
   1903   const FIRSTPASS_STATS first_frame = *this_frame;
   1904   FIRSTPASS_STATS next_frame;
   1905   const FIRSTPASS_STATS *start_position = twopass->stats_in;
   1906 
   1907   double decay_accumulator = 1.0;
   1908   double zero_motion_accumulator = 1.0;
   1909   double boost_score = 0;
   1910   double loop_decay_rate;
   1911 
   1912   double kf_mod_err = 0.0;
   1913   double kf_group_err = 0.0;
   1914   double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
   1915 
   1916   vp9_zero(next_frame);
   1917 
   1918   cpi->common.frame_type = KEY_FRAME;
   1919 
   1920   // Is this a forced key frame by interval.
   1921   rc->this_key_frame_forced = rc->next_key_frame_forced;
   1922 
   1923   // Clear the alt ref active flag as this can never be active on a key frame.
   1924   rc->source_alt_ref_active = 0;
   1925 
   1926   // KF is always a GF so clear frames till next gf counter.
   1927   rc->frames_till_gf_update_due = 0;
   1928 
   1929   rc->frames_to_key = 1;
   1930 
   1931   twopass->kf_group_bits = 0;        // Total bits available to kf group
   1932   twopass->kf_group_error_left = 0;  // Group modified error score.
   1933 
   1934   kf_mod_err = calculate_modified_err(cpi, this_frame);
   1935 
   1936   // Find the next keyframe.
   1937   i = 0;
   1938   while (twopass->stats_in < twopass->stats_in_end) {
   1939     // Accumulate kf group error.
   1940     kf_group_err += calculate_modified_err(cpi, this_frame);
   1941 
   1942     // Load the next frame's stats.
   1943     last_frame = *this_frame;
   1944     input_stats(twopass, this_frame);
   1945 
   1946     // Provided that we are not at the end of the file...
   1947     if (cpi->oxcf.auto_key &&
   1948         lookup_next_frame_stats(twopass, &next_frame) != EOF) {
   1949       // Check for a scene cut.
   1950       if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
   1951         break;
   1952 
   1953       // How fast is the prediction quality decaying?
   1954       loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
   1955 
   1956       // We want to know something about the recent past... rather than
   1957       // as used elsewhere where we are concerned with decay in prediction
   1958       // quality since the last GF or KF.
   1959       recent_loop_decay[i % 8] = loop_decay_rate;
   1960       decay_accumulator = 1.0;
   1961       for (j = 0; j < 8; ++j)
   1962         decay_accumulator *= recent_loop_decay[j];
   1963 
   1964       // Special check for transition or high motion followed by a
   1965       // static scene.
   1966       if (detect_transition_to_still(cpi, i, cpi->key_frame_frequency - i,
   1967                                      loop_decay_rate, decay_accumulator))
   1968         break;
   1969 
   1970       // Step on to the next frame.
   1971       ++rc->frames_to_key;
   1972 
   1973       // If we don't have a real key frame within the next two
   1974       // key_frame_frequency intervals then break out of the loop.
   1975       if (rc->frames_to_key >= 2 * (int)cpi->key_frame_frequency)
   1976         break;
   1977     } else {
   1978       ++rc->frames_to_key;
   1979     }
   1980     ++i;
   1981   }
   1982 
   1983   // If there is a max kf interval set by the user we must obey it.
   1984   // We already breakout of the loop above at 2x max.
   1985   // This code centers the extra kf if the actual natural interval
   1986   // is between 1x and 2x.
   1987   if (cpi->oxcf.auto_key &&
   1988       rc->frames_to_key > (int)cpi->key_frame_frequency) {
   1989     FIRSTPASS_STATS tmp_frame = first_frame;
   1990 
   1991     rc->frames_to_key /= 2;
   1992 
   1993     // Reset to the start of the group.
   1994     reset_fpf_position(twopass, start_position);
   1995 
   1996     kf_group_err = 0;
   1997 
   1998     // Rescan to get the correct error data for the forced kf group.
   1999     for (i = 0; i < rc->frames_to_key; ++i) {
   2000       kf_group_err += calculate_modified_err(cpi, &tmp_frame);
   2001       input_stats(twopass, &tmp_frame);
   2002     }
   2003     rc->next_key_frame_forced = 1;
   2004   } else if (twopass->stats_in == twopass->stats_in_end) {
   2005     rc->next_key_frame_forced = 1;
   2006   } else {
   2007     rc->next_key_frame_forced = 0;
   2008   }
   2009 
   2010   // Special case for the last key frame of the file.
   2011   if (twopass->stats_in >= twopass->stats_in_end) {
   2012     // Accumulate kf group error.
   2013     kf_group_err += calculate_modified_err(cpi, this_frame);
   2014   }
   2015 
   2016   // Calculate the number of bits that should be assigned to the kf group.
   2017   if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) {
   2018     // Maximum number of bits for a single normal frame (not key frame).
   2019     const int max_bits = frame_max_bits(cpi);
   2020 
   2021     // Maximum number of bits allocated to the key frame group.
   2022     int64_t max_grp_bits;
   2023 
   2024     // Default allocation based on bits left and relative
   2025     // complexity of the section.
   2026     twopass->kf_group_bits = (int64_t)(twopass->bits_left *
   2027        (kf_group_err / twopass->modified_error_left));
   2028 
   2029     // Clip based on maximum per frame rate defined by the user.
   2030     max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
   2031     if (twopass->kf_group_bits > max_grp_bits)
   2032       twopass->kf_group_bits = max_grp_bits;
   2033   } else {
   2034     twopass->kf_group_bits = 0;
   2035   }
   2036   // Reset the first pass file position.
   2037   reset_fpf_position(twopass, start_position);
   2038 
   2039   // Determine how big to make this keyframe based on how well the subsequent
   2040   // frames use inter blocks.
   2041   decay_accumulator = 1.0;
   2042   boost_score = 0.0;
   2043 
   2044   // Scan through the kf group collating various stats.
   2045   for (i = 0; i < rc->frames_to_key; ++i) {
   2046     if (EOF == input_stats(twopass, &next_frame))
   2047       break;
   2048 
   2049     // Monitor for static sections.
   2050     if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
   2051             zero_motion_accumulator) {
   2052       zero_motion_accumulator = (next_frame.pcnt_inter -
   2053                                      next_frame.pcnt_motion);
   2054     }
   2055 
   2056     // For the first few frames collect data to decide kf boost.
   2057     if (i <= (rc->max_gf_interval * 2)) {
   2058       double r;
   2059       if (next_frame.intra_error > twopass->kf_intra_err_min)
   2060         r = (IIKFACTOR2 * next_frame.intra_error /
   2061              DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
   2062       else
   2063         r = (IIKFACTOR2 * twopass->kf_intra_err_min /
   2064              DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
   2065 
   2066       if (r > RMAX)
   2067         r = RMAX;
   2068 
   2069       // How fast is prediction quality decaying.
   2070       if (!detect_flash(twopass, 0)) {
   2071         loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
   2072         decay_accumulator *= loop_decay_rate;
   2073         decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
   2074                               ? MIN_DECAY_FACTOR : decay_accumulator;
   2075       }
   2076 
   2077       boost_score += (decay_accumulator * r);
   2078     }
   2079   }
   2080 
   2081   {
   2082     FIRSTPASS_STATS sectionstats;
   2083 
   2084     zero_stats(&sectionstats);
   2085     reset_fpf_position(twopass, start_position);
   2086 
   2087     for (i = 0; i < rc->frames_to_key; ++i) {
   2088       input_stats(twopass, &next_frame);
   2089       accumulate_stats(&sectionstats, &next_frame);
   2090     }
   2091 
   2092     avg_stats(&sectionstats);
   2093 
   2094     twopass->section_intra_rating = (int) (sectionstats.intra_error /
   2095         DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
   2096   }
   2097 
   2098   // Reset the first pass file position.
   2099   reset_fpf_position(twopass, start_position);
   2100 
   2101   // Work out how many bits to allocate for the key frame itself.
   2102   if (1) {
   2103     int kf_boost = (int)boost_score;
   2104     int allocation_chunks;
   2105     int alt_kf_bits;
   2106 
   2107     if (kf_boost < (rc->frames_to_key * 3))
   2108       kf_boost = (rc->frames_to_key * 3);
   2109 
   2110     if (kf_boost < MIN_KF_BOOST)
   2111       kf_boost = MIN_KF_BOOST;
   2112 
   2113     // Make a note of baseline boost and the zero motion
   2114     // accumulator value for use elsewhere.
   2115     rc->kf_boost = kf_boost;
   2116     twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
   2117 
   2118     // Key frame size depends on:
   2119     // (1) the error score for the whole key frame group,
   2120     // (2) the key frames' own error if this is smaller than the
   2121     //     average for the group (optional),
   2122     // (3) insuring that the frame receives at least the allocation it would
   2123     //     have received based on its own error score vs the error score
   2124     //     remaining.
   2125     // Special case:
   2126     // If the sequence appears almost totally static we want to spend almost
   2127     // all of the bits on the key frame.
   2128     //
   2129     // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is
   2130     // taken care of by kf_boost.
   2131     if (zero_motion_accumulator >= 0.99) {
   2132       allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost;
   2133     } else {
   2134       allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost;
   2135     }
   2136 
   2137     // Prevent overflow.
   2138     if (kf_boost > 1028) {
   2139       int divisor = kf_boost >> 10;
   2140       kf_boost /= divisor;
   2141       allocation_chunks /= divisor;
   2142     }
   2143 
   2144     twopass->kf_group_bits = (twopass->kf_group_bits < 0) ? 0
   2145            : twopass->kf_group_bits;
   2146 
   2147     // Calculate the number of bits to be spent on the key frame.
   2148     twopass->kf_bits = (int)((double)kf_boost *
   2149         ((double)twopass->kf_group_bits / allocation_chunks));
   2150 
   2151     // If the key frame is actually easier than the average for the
   2152     // kf group (which does sometimes happen, e.g. a blank intro frame)
   2153     // then use an alternate calculation based on the kf error score
   2154     // which should give a smaller key frame.
   2155     if (kf_mod_err < kf_group_err / rc->frames_to_key) {
   2156       double  alt_kf_grp_bits = ((double)twopass->bits_left *
   2157          (kf_mod_err * (double)rc->frames_to_key) /
   2158          DOUBLE_DIVIDE_CHECK(twopass->modified_error_left));
   2159 
   2160       alt_kf_bits = (int)((double)kf_boost *
   2161                           (alt_kf_grp_bits / (double)allocation_chunks));
   2162 
   2163       if (twopass->kf_bits > alt_kf_bits)
   2164         twopass->kf_bits = alt_kf_bits;
   2165     } else {
   2166       // Else if it is much harder than other frames in the group make sure
   2167       // it at least receives an allocation in keeping with its relative
   2168       // error score.
   2169       alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err /
   2170                DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)));
   2171 
   2172       if (alt_kf_bits > twopass->kf_bits) {
   2173         twopass->kf_bits = alt_kf_bits;
   2174       }
   2175     }
   2176     twopass->kf_group_bits -= twopass->kf_bits;
   2177     // Per frame bit target for this frame.
   2178     vp9_rc_set_frame_target(cpi, twopass->kf_bits);
   2179   }
   2180 
   2181   // Note the total error score of the kf group minus the key frame itself.
   2182   twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
   2183 
   2184   // Adjust the count of total modified error left.
   2185   // The count of bits left is adjusted elsewhere based on real coded frame
   2186   // sizes.
   2187   twopass->modified_error_left -= kf_group_err;
   2188 }
   2189 
   2190 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) {
   2191   VP9_COMMON *const cm = &cpi->common;
   2192   if (!cpi->refresh_alt_ref_frame &&
   2193       (cm->current_video_frame == 0 ||
   2194        (cm->frame_flags & FRAMEFLAGS_KEY))) {
   2195     cm->frame_type = KEY_FRAME;
   2196   } else {
   2197     cm->frame_type = INTER_FRAME;
   2198   }
   2199   // Do not use periodic key frames.
   2200   cpi->rc.frames_to_key = INT_MAX;
   2201 }
   2202 
   2203 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
   2204   VP9_COMMON *const cm = &cpi->common;
   2205   RATE_CONTROL *const rc = &cpi->rc;
   2206   struct twopass_rc *const twopass = &cpi->twopass;
   2207   int frames_left;
   2208   FIRSTPASS_STATS this_frame;
   2209   FIRSTPASS_STATS this_frame_copy;
   2210 
   2211   double this_frame_intra_error;
   2212   double this_frame_coded_error;
   2213   int target;
   2214   LAYER_CONTEXT *lc = NULL;
   2215   int is_spatial_svc = (cpi->use_svc && cpi->svc.number_temporal_layers == 1);
   2216 
   2217   if (is_spatial_svc) {
   2218     lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
   2219     frames_left = (int)(twopass->total_stats.count -
   2220                   lc->current_video_frame_in_layer);
   2221   } else {
   2222     frames_left = (int)(twopass->total_stats.count -
   2223                   cm->current_video_frame);
   2224   }
   2225 
   2226   if (!twopass->stats_in)
   2227     return;
   2228 
   2229   if (cpi->refresh_alt_ref_frame) {
   2230     cm->frame_type = INTER_FRAME;
   2231     vp9_rc_set_frame_target(cpi, twopass->gf_bits);
   2232     return;
   2233   }
   2234 
   2235   vp9_clear_system_state();
   2236 
   2237   if (is_spatial_svc && twopass->kf_intra_err_min == 0) {
   2238     twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
   2239     twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
   2240   }
   2241 
   2242   if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
   2243     twopass->active_worst_quality = cpi->oxcf.cq_level;
   2244   } else if (cm->current_video_frame == 0 ||
   2245              (is_spatial_svc && lc->current_video_frame_in_layer == 0)) {
   2246     // Special case code for first frame.
   2247     const int section_target_bandwidth = (int)(twopass->bits_left /
   2248                                                frames_left);
   2249     const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats,
   2250                                                 section_target_bandwidth);
   2251     twopass->active_worst_quality = tmp_q;
   2252     rc->ni_av_qi = tmp_q;
   2253     rc->avg_q = vp9_convert_qindex_to_q(tmp_q);
   2254   }
   2255   vp9_zero(this_frame);
   2256   if (EOF == input_stats(twopass, &this_frame))
   2257     return;
   2258 
   2259   this_frame_intra_error = this_frame.intra_error;
   2260   this_frame_coded_error = this_frame.coded_error;
   2261 
   2262   // Keyframe and section processing.
   2263   if (rc->frames_to_key == 0 ||
   2264       (cm->frame_flags & FRAMEFLAGS_KEY)) {
   2265     // Define next KF group and assign bits to it.
   2266     this_frame_copy = this_frame;
   2267     find_next_key_frame(cpi, &this_frame_copy);
   2268     // Don't place key frame in any enhancement layers in spatial svc
   2269     if (cpi->use_svc && cpi->svc.number_temporal_layers == 1 &&
   2270         cpi->svc.spatial_layer_id > 0) {
   2271       cm->frame_type = INTER_FRAME;
   2272     }
   2273   } else {
   2274     cm->frame_type = INTER_FRAME;
   2275   }
   2276 
   2277   // Is this frame a GF / ARF? (Note: a key frame is always also a GF).
   2278   if (rc->frames_till_gf_update_due == 0) {
   2279     // Define next gf group and assign bits to it.
   2280     this_frame_copy = this_frame;
   2281 
   2282 #if CONFIG_MULTIPLE_ARF
   2283     if (cpi->multi_arf_enabled) {
   2284       define_fixed_arf_period(cpi);
   2285     } else {
   2286 #endif
   2287       define_gf_group(cpi, &this_frame_copy);
   2288 #if CONFIG_MULTIPLE_ARF
   2289     }
   2290 #endif
   2291 
   2292     if (twopass->gf_zeromotion_pct > 995) {
   2293       // As long as max_thresh for encode breakout is small enough, it is ok
   2294       // to enable it for show frame, i.e. set allow_encode_breakout to
   2295       // ENCODE_BREAKOUT_LIMITED.
   2296       if (!cm->show_frame)
   2297         cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED;
   2298       else
   2299         cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED;
   2300     }
   2301 
   2302     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
   2303     cpi->refresh_golden_frame = 1;
   2304   } else {
   2305     // Otherwise this is an ordinary frame.
   2306     // Assign bits from those allocated to the GF group.
   2307     this_frame_copy =  this_frame;
   2308     assign_std_frame_bits(cpi, &this_frame_copy);
   2309   }
   2310 
   2311   // Keep a globally available copy of this and the next frame's iiratio.
   2312   twopass->this_iiratio = (int)(this_frame_intra_error /
   2313                               DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
   2314   {
   2315     FIRSTPASS_STATS next_frame;
   2316     if (lookup_next_frame_stats(twopass, &next_frame) != EOF) {
   2317       twopass->next_iiratio = (int)(next_frame.intra_error /
   2318                                  DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
   2319     }
   2320   }
   2321 
   2322   if (cpi->common.frame_type == KEY_FRAME)
   2323     target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target);
   2324   else
   2325     target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target);
   2326   vp9_rc_set_frame_target(cpi, target);
   2327 
   2328   // Update the total stats remaining structure.
   2329   subtract_stats(&twopass->total_left_stats, &this_frame);
   2330 }
   2331 
   2332 void vp9_twopass_postencode_update(VP9_COMP *cpi) {
   2333   const uint64_t bits_used = cpi->rc.projected_frame_size;
   2334   cpi->twopass.bits_left -= bits_used;
   2335   cpi->twopass.bits_left = MAX(cpi->twopass.bits_left, 0);
   2336   // Update bits left to the kf and gf groups to account for overshoot or
   2337   // undershoot on these frames.
   2338   if (cpi->common.frame_type == KEY_FRAME) {
   2339     // For key frames kf_group_bits already had the target bits subtracted out.
   2340     // So now update to the correct value based on the actual bits used.
   2341     cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - bits_used;
   2342   } else {
   2343     cpi->twopass.kf_group_bits -= bits_used;
   2344     cpi->twopass.gf_group_bits -= bits_used;
   2345     cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0);
   2346   }
   2347   cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0);
   2348 }
   2349