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