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