Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <math.h>
     12 #include <limits.h>
     13 #include <stdio.h>
     14 
     15 #include "./vpx_dsp_rtcd.h"
     16 #include "./vpx_scale_rtcd.h"
     17 #include "block.h"
     18 #include "onyx_int.h"
     19 #include "vpx_dsp/variance.h"
     20 #include "encodeintra.h"
     21 #include "vp8/common/common.h"
     22 #include "vp8/common/setupintrarecon.h"
     23 #include "vp8/common/systemdependent.h"
     24 #include "mcomp.h"
     25 #include "firstpass.h"
     26 #include "vpx_scale/vpx_scale.h"
     27 #include "encodemb.h"
     28 #include "vp8/common/extend.h"
     29 #include "vpx_ports/system_state.h"
     30 #include "vpx_mem/vpx_mem.h"
     31 #include "vp8/common/swapyv12buffer.h"
     32 #include "rdopt.h"
     33 #include "vp8/common/quant_common.h"
     34 #include "encodemv.h"
     35 #include "encodeframe.h"
     36 
     37 #define OUTPUT_FPF 0
     38 
     39 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
     40 
     41 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
     42 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
     43 
     44 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
     45 
     46 #define IIFACTOR 1.5
     47 #define IIKFACTOR1 1.40
     48 #define IIKFACTOR2 1.5
     49 #define RMAX 14.0
     50 #define GF_RMAX 48.0
     51 
     52 #define KF_MB_INTRA_MIN 300
     53 #define GF_MB_INTRA_MIN 200
     54 
     55 #define DOUBLE_DIVIDE_CHECK(X) ((X) < 0 ? (X)-.000001 : (X) + .000001)
     56 
     57 #define POW1 (double)cpi->oxcf.two_pass_vbrbias / 100.0
     58 #define POW2 (double)cpi->oxcf.two_pass_vbrbias / 100.0
     59 
     60 #define NEW_BOOST 1
     61 
     62 static int vscale_lookup[7] = { 0, 1, 1, 2, 2, 3, 3 };
     63 static int hscale_lookup[7] = { 0, 0, 1, 1, 2, 2, 3 };
     64 
     65 static const int cq_level[QINDEX_RANGE] = {
     66   0,  0,  1,  1,  2,  3,  3,  4,  4,  5,  6,  6,  7,  8,  8,  9,  9,  10, 11,
     67   11, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24,
     68   24, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38,
     69   39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53,
     70   54, 55, 55, 56, 57, 58, 59, 60, 60, 61, 62, 63, 64, 65, 66, 67, 67, 68, 69,
     71   70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 86,
     72   87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
     73 };
     74 
     75 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
     76 
     77 /* Resets the first pass file to the given position using a relative seek
     78  * from the current position
     79  */
     80 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) {
     81   cpi->twopass.stats_in = Position;
     82 }
     83 
     84 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) {
     85   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF;
     86 
     87   *next_frame = *cpi->twopass.stats_in;
     88   return 1;
     89 }
     90 
     91 /* Read frame stats at an offset from the current position */
     92 static int read_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *frame_stats,
     93                             int offset) {
     94   FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in;
     95 
     96   /* Check legality of offset */
     97   if (offset >= 0) {
     98     if (&fps_ptr[offset] >= cpi->twopass.stats_in_end) return EOF;
     99   } else if (offset < 0) {
    100     if (&fps_ptr[offset] < cpi->twopass.stats_in_start) return EOF;
    101   }
    102 
    103   *frame_stats = fps_ptr[offset];
    104   return 1;
    105 }
    106 
    107 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps) {
    108   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF;
    109 
    110   *fps = *cpi->twopass.stats_in;
    111   cpi->twopass.stats_in =
    112       (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
    113   return 1;
    114 }
    115 
    116 static void output_stats(const VP8_COMP *cpi,
    117                          struct vpx_codec_pkt_list *pktlist,
    118                          FIRSTPASS_STATS *stats) {
    119   struct vpx_codec_cx_pkt pkt;
    120   (void)cpi;
    121   pkt.kind = VPX_CODEC_STATS_PKT;
    122   pkt.data.twopass_stats.buf = stats;
    123   pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
    124   vpx_codec_pkt_list_add(pktlist, &pkt);
    125 
    126 /* TEMP debug code */
    127 #if OUTPUT_FPF
    128 
    129   {
    130     FILE *fpfile;
    131     fpfile = fopen("firstpass.stt", "a");
    132 
    133     fprintf(fpfile,
    134             "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
    135             " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
    136             " %12.0f %12.0f %12.4f\n",
    137             stats->frame, stats->intra_error, stats->coded_error,
    138             stats->ssim_weighted_pred_err, stats->pcnt_inter,
    139             stats->pcnt_motion, stats->pcnt_second_ref, stats->pcnt_neutral,
    140             stats->MVr, stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv,
    141             stats->MVcv, stats->mv_in_out_count, stats->new_mv_count,
    142             stats->count, stats->duration);
    143     fclose(fpfile);
    144   }
    145 #endif
    146 }
    147 
    148 static void zero_stats(FIRSTPASS_STATS *section) {
    149   section->frame = 0.0;
    150   section->intra_error = 0.0;
    151   section->coded_error = 0.0;
    152   section->ssim_weighted_pred_err = 0.0;
    153   section->pcnt_inter = 0.0;
    154   section->pcnt_motion = 0.0;
    155   section->pcnt_second_ref = 0.0;
    156   section->pcnt_neutral = 0.0;
    157   section->MVr = 0.0;
    158   section->mvr_abs = 0.0;
    159   section->MVc = 0.0;
    160   section->mvc_abs = 0.0;
    161   section->MVrv = 0.0;
    162   section->MVcv = 0.0;
    163   section->mv_in_out_count = 0.0;
    164   section->new_mv_count = 0.0;
    165   section->count = 0.0;
    166   section->duration = 1.0;
    167 }
    168 
    169 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
    170   section->frame += frame->frame;
    171   section->intra_error += frame->intra_error;
    172   section->coded_error += frame->coded_error;
    173   section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
    174   section->pcnt_inter += frame->pcnt_inter;
    175   section->pcnt_motion += frame->pcnt_motion;
    176   section->pcnt_second_ref += frame->pcnt_second_ref;
    177   section->pcnt_neutral += frame->pcnt_neutral;
    178   section->MVr += frame->MVr;
    179   section->mvr_abs += frame->mvr_abs;
    180   section->MVc += frame->MVc;
    181   section->mvc_abs += frame->mvc_abs;
    182   section->MVrv += frame->MVrv;
    183   section->MVcv += frame->MVcv;
    184   section->mv_in_out_count += frame->mv_in_out_count;
    185   section->new_mv_count += frame->new_mv_count;
    186   section->count += frame->count;
    187   section->duration += frame->duration;
    188 }
    189 
    190 static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
    191   section->frame -= frame->frame;
    192   section->intra_error -= frame->intra_error;
    193   section->coded_error -= frame->coded_error;
    194   section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
    195   section->pcnt_inter -= frame->pcnt_inter;
    196   section->pcnt_motion -= frame->pcnt_motion;
    197   section->pcnt_second_ref -= frame->pcnt_second_ref;
    198   section->pcnt_neutral -= frame->pcnt_neutral;
    199   section->MVr -= frame->MVr;
    200   section->mvr_abs -= frame->mvr_abs;
    201   section->MVc -= frame->MVc;
    202   section->mvc_abs -= frame->mvc_abs;
    203   section->MVrv -= frame->MVrv;
    204   section->MVcv -= frame->MVcv;
    205   section->mv_in_out_count -= frame->mv_in_out_count;
    206   section->new_mv_count -= frame->new_mv_count;
    207   section->count -= frame->count;
    208   section->duration -= frame->duration;
    209 }
    210 
    211 static void avg_stats(FIRSTPASS_STATS *section) {
    212   if (section->count < 1.0) return;
    213 
    214   section->intra_error /= section->count;
    215   section->coded_error /= section->count;
    216   section->ssim_weighted_pred_err /= section->count;
    217   section->pcnt_inter /= section->count;
    218   section->pcnt_second_ref /= section->count;
    219   section->pcnt_neutral /= section->count;
    220   section->pcnt_motion /= section->count;
    221   section->MVr /= section->count;
    222   section->mvr_abs /= section->count;
    223   section->MVc /= section->count;
    224   section->mvc_abs /= section->count;
    225   section->MVrv /= section->count;
    226   section->MVcv /= section->count;
    227   section->mv_in_out_count /= section->count;
    228   section->duration /= section->count;
    229 }
    230 
    231 /* Calculate a modified Error used in distributing bits between easier
    232  * and harder frames
    233  */
    234 static double calculate_modified_err(VP8_COMP *cpi,
    235                                      FIRSTPASS_STATS *this_frame) {
    236   double av_err = (cpi->twopass.total_stats.ssim_weighted_pred_err /
    237                    cpi->twopass.total_stats.count);
    238   double this_err = this_frame->ssim_weighted_pred_err;
    239   double modified_err;
    240 
    241   if (this_err > av_err) {
    242     modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
    243   } else {
    244     modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
    245   }
    246 
    247   return modified_err;
    248 }
    249 
    250 static const double weight_table[256] = {
    251   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    252   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    253   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    254   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
    255   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
    256   0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
    257   0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
    258   0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
    259   0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
    260   0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    261   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    262   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    263   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    264   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    265   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    266   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    267   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    268   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    269   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    270   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    271   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,
    273   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,
    275   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,
    277   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    278   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    279   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    280   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    281   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    282   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    283   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    284   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    285   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    286   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    287   1.000000, 1.000000, 1.000000, 1.000000
    288 };
    289 
    290 static double simple_weight(YV12_BUFFER_CONFIG *source) {
    291   int i, j;
    292 
    293   unsigned char *src = source->y_buffer;
    294   double sum_weights = 0.0;
    295 
    296   /* Loop throught the Y plane raw examining levels and creating a weight
    297    * for the image
    298    */
    299   i = source->y_height;
    300   do {
    301     j = source->y_width;
    302     do {
    303       sum_weights += weight_table[*src];
    304       src++;
    305     } while (--j);
    306     src -= source->y_width;
    307     src += source->y_stride;
    308   } while (--i);
    309 
    310   sum_weights /= (source->y_height * source->y_width);
    311 
    312   return sum_weights;
    313 }
    314 
    315 /* This function returns the current per frame maximum bitrate target */
    316 static int frame_max_bits(VP8_COMP *cpi) {
    317   /* Max allocation for a single frame based on the max section guidelines
    318    * passed in and how many bits are left
    319    */
    320   int max_bits;
    321 
    322   /* For CBR we need to also consider buffer fullness.
    323    * If we are running below the optimal level then we need to gradually
    324    * tighten up on max_bits.
    325    */
    326   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
    327     double buffer_fullness_ratio =
    328         (double)cpi->buffer_level /
    329         DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
    330 
    331     /* For CBR base this on the target average bits per frame plus the
    332      * maximum sedction rate passed in by the user
    333      */
    334     max_bits = (int)(cpi->av_per_frame_bandwidth *
    335                      ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
    336 
    337     /* If our buffer is below the optimum level */
    338     if (buffer_fullness_ratio < 1.0) {
    339       /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */
    340       int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2))
    341                              ? cpi->av_per_frame_bandwidth >> 2
    342                              : max_bits >> 2;
    343 
    344       max_bits = (int)(max_bits * buffer_fullness_ratio);
    345 
    346       /* Lowest value we will set ... which should allow the buffer to
    347        * refill.
    348        */
    349       if (max_bits < min_max_bits) max_bits = min_max_bits;
    350     }
    351   }
    352   /* VBR */
    353   else {
    354     /* For VBR base this on the bits and frames left plus the
    355      * two_pass_vbrmax_section rate passed in by the user
    356      */
    357     max_bits = (int)(((double)cpi->twopass.bits_left /
    358                       (cpi->twopass.total_stats.count -
    359                        (double)cpi->common.current_video_frame)) *
    360                      ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
    361   }
    362 
    363   /* Trap case where we are out of bits */
    364   if (max_bits < 0) max_bits = 0;
    365 
    366   return max_bits;
    367 }
    368 
    369 void vp8_init_first_pass(VP8_COMP *cpi) {
    370   zero_stats(&cpi->twopass.total_stats);
    371 }
    372 
    373 void vp8_end_first_pass(VP8_COMP *cpi) {
    374   output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
    375 }
    376 
    377 static void zz_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
    378                              YV12_BUFFER_CONFIG *raw_buffer,
    379                              int *raw_motion_err,
    380                              YV12_BUFFER_CONFIG *recon_buffer,
    381                              int *best_motion_err, int recon_yoffset) {
    382   MACROBLOCKD *const xd = &x->e_mbd;
    383   BLOCK *b = &x->block[0];
    384   BLOCKD *d = &x->e_mbd.block[0];
    385 
    386   unsigned char *src_ptr = (*(b->base_src) + b->src);
    387   int src_stride = b->src_stride;
    388   unsigned char *raw_ptr;
    389   int raw_stride = raw_buffer->y_stride;
    390   unsigned char *ref_ptr;
    391   int ref_stride = x->e_mbd.pre.y_stride;
    392   (void)cpi;
    393 
    394   /* Set up pointers for this macro block raw buffer */
    395   raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset + d->offset);
    396   vpx_mse16x16(src_ptr, src_stride, raw_ptr, raw_stride,
    397                (unsigned int *)(raw_motion_err));
    398 
    399   /* Set up pointers for this macro block recon buffer */
    400   xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
    401   ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset);
    402   vpx_mse16x16(src_ptr, src_stride, ref_ptr, ref_stride,
    403                (unsigned int *)(best_motion_err));
    404 }
    405 
    406 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
    407                                      int_mv *ref_mv, MV *best_mv,
    408                                      YV12_BUFFER_CONFIG *recon_buffer,
    409                                      int *best_motion_err, int recon_yoffset) {
    410   MACROBLOCKD *const xd = &x->e_mbd;
    411   BLOCK *b = &x->block[0];
    412   BLOCKD *d = &x->e_mbd.block[0];
    413   int num00;
    414 
    415   int_mv tmp_mv;
    416   int_mv ref_mv_full;
    417 
    418   int tmp_err;
    419   int step_param = 3; /* Dont search over full range for first pass */
    420   int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
    421   int n;
    422   vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
    423   int new_mv_mode_penalty = 256;
    424 
    425   /* override the default variance function to use MSE */
    426   v_fn_ptr.vf = vpx_mse16x16;
    427 
    428   /* Set up pointers for this macro block recon buffer */
    429   xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
    430 
    431   /* Initial step/diamond search centred on best mv */
    432   tmp_mv.as_int = 0;
    433   ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3;
    434   ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3;
    435   tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
    436                                     x->sadperbit16, &num00, &v_fn_ptr,
    437                                     x->mvcost, ref_mv);
    438   if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
    439 
    440   if (tmp_err < *best_motion_err) {
    441     *best_motion_err = tmp_err;
    442     best_mv->row = tmp_mv.as_mv.row;
    443     best_mv->col = tmp_mv.as_mv.col;
    444   }
    445 
    446   /* Further step/diamond searches as necessary */
    447   n = num00;
    448   num00 = 0;
    449 
    450   while (n < further_steps) {
    451     n++;
    452 
    453     if (num00) {
    454       num00--;
    455     } else {
    456       tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
    457                                         step_param + n, x->sadperbit16, &num00,
    458                                         &v_fn_ptr, x->mvcost, ref_mv);
    459       if (tmp_err < INT_MAX - new_mv_mode_penalty) {
    460         tmp_err += new_mv_mode_penalty;
    461       }
    462 
    463       if (tmp_err < *best_motion_err) {
    464         *best_motion_err = tmp_err;
    465         best_mv->row = tmp_mv.as_mv.row;
    466         best_mv->col = tmp_mv.as_mv.col;
    467       }
    468     }
    469   }
    470 }
    471 
    472 void vp8_first_pass(VP8_COMP *cpi) {
    473   int mb_row, mb_col;
    474   MACROBLOCK *const x = &cpi->mb;
    475   VP8_COMMON *const cm = &cpi->common;
    476   MACROBLOCKD *const xd = &x->e_mbd;
    477 
    478   int recon_yoffset, recon_uvoffset;
    479   YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
    480   YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
    481   YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
    482   int recon_y_stride = lst_yv12->y_stride;
    483   int recon_uv_stride = lst_yv12->uv_stride;
    484   int64_t intra_error = 0;
    485   int64_t coded_error = 0;
    486 
    487   int sum_mvr = 0, sum_mvc = 0;
    488   int sum_mvr_abs = 0, sum_mvc_abs = 0;
    489   int sum_mvrs = 0, sum_mvcs = 0;
    490   int mvcount = 0;
    491   int intercount = 0;
    492   int second_ref_count = 0;
    493   int intrapenalty = 256;
    494   int neutral_count = 0;
    495   int new_mv_count = 0;
    496   int sum_in_vectors = 0;
    497   uint32_t lastmv_as_int = 0;
    498 
    499   int_mv zero_ref_mv;
    500 
    501   zero_ref_mv.as_int = 0;
    502 
    503   vpx_clear_system_state();
    504 
    505   x->src = *cpi->Source;
    506   xd->pre = *lst_yv12;
    507   xd->dst = *new_yv12;
    508 
    509   x->partition_info = x->pi;
    510 
    511   xd->mode_info_context = cm->mi;
    512 
    513   if (!cm->use_bilinear_mc_filter) {
    514     xd->subpixel_predict = vp8_sixtap_predict4x4;
    515     xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
    516     xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
    517     xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
    518   } else {
    519     xd->subpixel_predict = vp8_bilinear_predict4x4;
    520     xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
    521     xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
    522     xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
    523   }
    524 
    525   vp8_build_block_offsets(x);
    526 
    527   /* set up frame new frame for intra coded blocks */
    528   vp8_setup_intra_recon(new_yv12);
    529   vp8cx_frame_init_quantizer(cpi);
    530 
    531   /* Initialise the MV cost table to the defaults */
    532   {
    533     int flag[2] = { 1, 1 };
    534     vp8_initialize_rd_consts(cpi, x,
    535                              vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
    536     memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
    537     vp8_build_component_cost_table(cpi->mb.mvcost,
    538                                    (const MV_CONTEXT *)cm->fc.mvc, flag);
    539   }
    540 
    541   /* for each macroblock row in image */
    542   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
    543     int_mv best_ref_mv;
    544 
    545     best_ref_mv.as_int = 0;
    546 
    547     /* reset above block coeffs */
    548     xd->up_available = (mb_row != 0);
    549     recon_yoffset = (mb_row * recon_y_stride * 16);
    550     recon_uvoffset = (mb_row * recon_uv_stride * 8);
    551 
    552     /* Set up limit values for motion vectors to prevent them extending
    553      * outside the UMV borders
    554      */
    555     x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
    556     x->mv_row_max =
    557         ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
    558 
    559     /* for each macroblock col in image */
    560     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
    561       int this_error;
    562       int gf_motion_error = INT_MAX;
    563       int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
    564 
    565       xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
    566       xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
    567       xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
    568       xd->left_available = (mb_col != 0);
    569 
    570       /* Copy current mb to a buffer */
    571       vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
    572 
    573       /* do intra 16x16 prediction */
    574       this_error = vp8_encode_intra(cpi, x, use_dc_pred);
    575 
    576       /* "intrapenalty" below deals with situations where the intra
    577        * and inter error scores are very low (eg a plain black frame)
    578        * We do not have special cases in first pass for 0,0 and
    579        * nearest etc so all inter modes carry an overhead cost
    580        * estimate fot the mv. When the error score is very low this
    581        * causes us to pick all or lots of INTRA modes and throw lots
    582        * of key frames. This penalty adds a cost matching that of a
    583        * 0,0 mv to the intra case.
    584        */
    585       this_error += intrapenalty;
    586 
    587       /* Cumulative intra error total */
    588       intra_error += (int64_t)this_error;
    589 
    590       /* Set up limit values for motion vectors to prevent them
    591        * extending outside the UMV borders
    592        */
    593       x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
    594       x->mv_col_max =
    595           ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
    596 
    597       /* Other than for the first frame do a motion search */
    598       if (cm->current_video_frame > 0) {
    599         BLOCKD *d = &x->e_mbd.block[0];
    600         MV tmp_mv = { 0, 0 };
    601         int tmp_err;
    602         int motion_error = INT_MAX;
    603         int raw_motion_error = INT_MAX;
    604 
    605         /* Simple 0,0 motion with no mv overhead */
    606         zz_motion_search(cpi, x, cpi->last_frame_unscaled_source,
    607                          &raw_motion_error, lst_yv12, &motion_error,
    608                          recon_yoffset);
    609         d->bmi.mv.as_mv.row = 0;
    610         d->bmi.mv.as_mv.col = 0;
    611 
    612         if (raw_motion_error < cpi->oxcf.encode_breakout) {
    613           goto skip_motion_search;
    614         }
    615 
    616         /* Test last reference frame using the previous best mv as the
    617          * starting point (best reference) for the search
    618          */
    619         first_pass_motion_search(cpi, x, &best_ref_mv, &d->bmi.mv.as_mv,
    620                                  lst_yv12, &motion_error, recon_yoffset);
    621 
    622         /* If the current best reference mv is not centred on 0,0
    623          * then do a 0,0 based search as well
    624          */
    625         if (best_ref_mv.as_int) {
    626           tmp_err = INT_MAX;
    627           first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, lst_yv12,
    628                                    &tmp_err, recon_yoffset);
    629 
    630           if (tmp_err < motion_error) {
    631             motion_error = tmp_err;
    632             d->bmi.mv.as_mv.row = tmp_mv.row;
    633             d->bmi.mv.as_mv.col = tmp_mv.col;
    634           }
    635         }
    636 
    637         /* Experimental search in a second reference frame ((0,0)
    638          * based only)
    639          */
    640         if (cm->current_video_frame > 1) {
    641           first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12,
    642                                    &gf_motion_error, recon_yoffset);
    643 
    644           if ((gf_motion_error < motion_error) &&
    645               (gf_motion_error < this_error)) {
    646             second_ref_count++;
    647           }
    648 
    649           /* Reset to last frame as reference buffer */
    650           xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
    651           xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
    652           xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
    653         }
    654 
    655       skip_motion_search:
    656         /* Intra assumed best */
    657         best_ref_mv.as_int = 0;
    658 
    659         if (motion_error <= this_error) {
    660           /* Keep a count of cases where the inter and intra were
    661            * very close and very low. This helps with scene cut
    662            * detection for example in cropped clips with black bars
    663            * at the sides or top and bottom.
    664            */
    665           if ((((this_error - intrapenalty) * 9) <= (motion_error * 10)) &&
    666               (this_error < (2 * intrapenalty))) {
    667             neutral_count++;
    668           }
    669 
    670           d->bmi.mv.as_mv.row *= 8;
    671           d->bmi.mv.as_mv.col *= 8;
    672           this_error = motion_error;
    673           vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
    674           vp8_encode_inter16x16y(x);
    675           sum_mvr += d->bmi.mv.as_mv.row;
    676           sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
    677           sum_mvc += d->bmi.mv.as_mv.col;
    678           sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
    679           sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
    680           sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
    681           intercount++;
    682 
    683           best_ref_mv.as_int = d->bmi.mv.as_int;
    684 
    685           /* Was the vector non-zero */
    686           if (d->bmi.mv.as_int) {
    687             mvcount++;
    688 
    689             /* Was it different from the last non zero vector */
    690             if (d->bmi.mv.as_int != lastmv_as_int) new_mv_count++;
    691             lastmv_as_int = d->bmi.mv.as_int;
    692 
    693             /* Does the Row vector point inwards or outwards */
    694             if (mb_row < cm->mb_rows / 2) {
    695               if (d->bmi.mv.as_mv.row > 0) {
    696                 sum_in_vectors--;
    697               } else if (d->bmi.mv.as_mv.row < 0) {
    698                 sum_in_vectors++;
    699               }
    700             } else if (mb_row > cm->mb_rows / 2) {
    701               if (d->bmi.mv.as_mv.row > 0) {
    702                 sum_in_vectors++;
    703               } else if (d->bmi.mv.as_mv.row < 0) {
    704                 sum_in_vectors--;
    705               }
    706             }
    707 
    708             /* Does the Row vector point inwards or outwards */
    709             if (mb_col < cm->mb_cols / 2) {
    710               if (d->bmi.mv.as_mv.col > 0) {
    711                 sum_in_vectors--;
    712               } else if (d->bmi.mv.as_mv.col < 0) {
    713                 sum_in_vectors++;
    714               }
    715             } else if (mb_col > cm->mb_cols / 2) {
    716               if (d->bmi.mv.as_mv.col > 0) {
    717                 sum_in_vectors++;
    718               } else if (d->bmi.mv.as_mv.col < 0) {
    719                 sum_in_vectors--;
    720               }
    721             }
    722           }
    723         }
    724       }
    725 
    726       coded_error += (int64_t)this_error;
    727 
    728       /* adjust to the next column of macroblocks */
    729       x->src.y_buffer += 16;
    730       x->src.u_buffer += 8;
    731       x->src.v_buffer += 8;
    732 
    733       recon_yoffset += 16;
    734       recon_uvoffset += 8;
    735     }
    736 
    737     /* adjust to the next row of mbs */
    738     x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
    739     x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
    740     x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
    741 
    742     /* extend the recon for intra prediction */
    743     vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8,
    744                       xd->dst.v_buffer + 8);
    745     vpx_clear_system_state();
    746   }
    747 
    748   vpx_clear_system_state();
    749   {
    750     double weight = 0.0;
    751 
    752     FIRSTPASS_STATS fps;
    753 
    754     fps.frame = cm->current_video_frame;
    755     fps.intra_error = (double)(intra_error >> 8);
    756     fps.coded_error = (double)(coded_error >> 8);
    757     weight = simple_weight(cpi->Source);
    758 
    759     if (weight < 0.1) weight = 0.1;
    760 
    761     fps.ssim_weighted_pred_err = fps.coded_error * weight;
    762 
    763     fps.pcnt_inter = 0.0;
    764     fps.pcnt_motion = 0.0;
    765     fps.MVr = 0.0;
    766     fps.mvr_abs = 0.0;
    767     fps.MVc = 0.0;
    768     fps.mvc_abs = 0.0;
    769     fps.MVrv = 0.0;
    770     fps.MVcv = 0.0;
    771     fps.mv_in_out_count = 0.0;
    772     fps.new_mv_count = 0.0;
    773     fps.count = 1.0;
    774 
    775     fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
    776     fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
    777     fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
    778 
    779     if (mvcount > 0) {
    780       fps.MVr = (double)sum_mvr / (double)mvcount;
    781       fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
    782       fps.MVc = (double)sum_mvc / (double)mvcount;
    783       fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
    784       fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) /
    785                  (double)mvcount;
    786       fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) /
    787                  (double)mvcount;
    788       fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
    789       fps.new_mv_count = new_mv_count;
    790 
    791       fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
    792     }
    793 
    794     /* TODO:  handle the case when duration is set to 0, or something less
    795      * than the full time between subsequent cpi->source_time_stamps
    796      */
    797     fps.duration = (double)(cpi->source->ts_end - cpi->source->ts_start);
    798 
    799     /* don't want to do output stats with a stack variable! */
    800     memcpy(&cpi->twopass.this_frame_stats, &fps, sizeof(FIRSTPASS_STATS));
    801     output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
    802     accumulate_stats(&cpi->twopass.total_stats, &fps);
    803   }
    804 
    805   /* Copy the previous Last Frame into the GF buffer if specific
    806    * conditions for doing so are met
    807    */
    808   if ((cm->current_video_frame > 0) &&
    809       (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
    810       ((cpi->twopass.this_frame_stats.intra_error /
    811         DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
    812        2.0)) {
    813     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
    814   }
    815 
    816   /* swap frame pointers so last frame refers to the frame we just
    817    * compressed
    818    */
    819   vp8_swap_yv12_buffer(lst_yv12, new_yv12);
    820   vp8_yv12_extend_frame_borders(lst_yv12);
    821 
    822   /* Special case for the first frame. Copy into the GF buffer as a
    823    * second reference.
    824    */
    825   if (cm->current_video_frame == 0) {
    826     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
    827   }
    828 
    829   /* use this to see what the first pass reconstruction looks like */
    830   if (0) {
    831     char filename[512];
    832     FILE *recon_file;
    833     sprintf(filename, "enc%04d.yuv", (int)cm->current_video_frame);
    834 
    835     if (cm->current_video_frame == 0) {
    836       recon_file = fopen(filename, "wb");
    837     } else {
    838       recon_file = fopen(filename, "ab");
    839     }
    840 
    841     (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
    842     fclose(recon_file);
    843   }
    844 
    845   cm->current_video_frame++;
    846 }
    847 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
    848 
    849 /* Estimate a cost per mb attributable to overheads such as the coding of
    850  * modes and motion vectors.
    851  * Currently simplistic in its assumptions for testing.
    852  */
    853 
    854 static double bitcost(double prob) {
    855   if (prob > 0.000122) {
    856     return -log(prob) / log(2.0);
    857   } else {
    858     return 13.0;
    859   }
    860 }
    861 static int64_t estimate_modemvcost(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats) {
    862   int mv_cost;
    863   int64_t mode_cost;
    864 
    865   double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
    866   double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
    867   double av_intra = (1.0 - av_pct_inter);
    868 
    869   double zz_cost;
    870   double motion_cost;
    871   double intra_cost;
    872 
    873   zz_cost = bitcost(av_pct_inter - av_pct_motion);
    874   motion_cost = bitcost(av_pct_motion);
    875   intra_cost = bitcost(av_intra);
    876 
    877   /* Estimate of extra bits per mv overhead for mbs
    878    * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb
    879    */
    880   mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
    881 
    882   /* Crude estimate of overhead cost from modes
    883    * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
    884    */
    885   mode_cost =
    886       (int64_t)((((av_pct_inter - av_pct_motion) * zz_cost) +
    887                  (av_pct_motion * motion_cost) + (av_intra * intra_cost)) *
    888                 cpi->common.MBs) *
    889       512;
    890 
    891   return mv_cost + mode_cost;
    892 }
    893 
    894 static double calc_correction_factor(double err_per_mb, double err_devisor,
    895                                      double pt_low, double pt_high, int Q) {
    896   double power_term;
    897   double error_term = err_per_mb / err_devisor;
    898   double correction_factor;
    899 
    900   /* Adjustment based on Q to power term. */
    901   power_term = pt_low + (Q * 0.01);
    902   power_term = (power_term > pt_high) ? pt_high : power_term;
    903 
    904   /* Adjustments to error term */
    905   /* TBD */
    906 
    907   /* Calculate correction factor */
    908   correction_factor = pow(error_term, power_term);
    909 
    910   /* Clip range */
    911   correction_factor = (correction_factor < 0.05)
    912                           ? 0.05
    913                           : (correction_factor > 5.0) ? 5.0 : correction_factor;
    914 
    915   return correction_factor;
    916 }
    917 
    918 static int estimate_max_q(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
    919                           int section_target_bandwitdh, int overhead_bits) {
    920   int Q;
    921   int num_mbs = cpi->common.MBs;
    922   int target_norm_bits_per_mb;
    923 
    924   double section_err = (fpstats->coded_error / fpstats->count);
    925   double err_per_mb = section_err / num_mbs;
    926   double err_correction_factor;
    927   double speed_correction = 1.0;
    928   int overhead_bits_per_mb;
    929 
    930   if (section_target_bandwitdh <= 0) {
    931     return cpi->twopass.maxq_max_limit; /* Highest value allowed */
    932   }
    933 
    934   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
    935                                 ? (512 * section_target_bandwitdh) / num_mbs
    936                                 : 512 * (section_target_bandwitdh / num_mbs);
    937 
    938   /* Calculate a corrective factor based on a rolling ratio of bits spent
    939    * vs target bits
    940    */
    941   if ((cpi->rolling_target_bits > 0) &&
    942       (cpi->active_worst_quality < cpi->worst_quality)) {
    943     double rolling_ratio;
    944 
    945     rolling_ratio =
    946         (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
    947 
    948     if (rolling_ratio < 0.95) {
    949       cpi->twopass.est_max_qcorrection_factor -= 0.005;
    950     } else if (rolling_ratio > 1.05) {
    951       cpi->twopass.est_max_qcorrection_factor += 0.005;
    952     }
    953 
    954     cpi->twopass.est_max_qcorrection_factor =
    955         (cpi->twopass.est_max_qcorrection_factor < 0.1)
    956             ? 0.1
    957             : (cpi->twopass.est_max_qcorrection_factor > 10.0)
    958                   ? 10.0
    959                   : cpi->twopass.est_max_qcorrection_factor;
    960   }
    961 
    962   /* Corrections for higher compression speed settings
    963    * (reduced compression expected)
    964    */
    965   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
    966     if (cpi->oxcf.cpu_used <= 5) {
    967       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
    968     } else {
    969       speed_correction = 1.25;
    970     }
    971   }
    972 
    973   /* Estimate of overhead bits per mb */
    974   /* Correction to overhead bits for min allowed Q. */
    975   overhead_bits_per_mb = overhead_bits / num_mbs;
    976   overhead_bits_per_mb = (int)(overhead_bits_per_mb *
    977                                pow(0.98, (double)cpi->twopass.maxq_min_limit));
    978 
    979   /* Try and pick a max Q that will be high enough to encode the
    980    * content at the given rate.
    981    */
    982   for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; ++Q) {
    983     int bits_per_mb_at_this_q;
    984 
    985     /* Error per MB based correction factor */
    986     err_correction_factor =
    987         calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
    988 
    989     bits_per_mb_at_this_q =
    990         vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
    991 
    992     bits_per_mb_at_this_q = (int)(.5 +
    993                                   err_correction_factor * speed_correction *
    994                                       cpi->twopass.est_max_qcorrection_factor *
    995                                       cpi->twopass.section_max_qfactor *
    996                                       (double)bits_per_mb_at_this_q);
    997 
    998     /* Mode and motion overhead */
    999     /* As Q rises in real encode loop rd code will force overhead down
   1000      * We make a crude adjustment for this here as *.98 per Q step.
   1001      */
   1002     overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
   1003 
   1004     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
   1005   }
   1006 
   1007   /* Restriction on active max q for constrained quality mode. */
   1008   if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
   1009       (Q < cpi->cq_target_quality)) {
   1010     Q = cpi->cq_target_quality;
   1011   }
   1012 
   1013   /* Adjust maxq_min_limit and maxq_max_limit limits based on
   1014    * average q observed in clip for non kf/gf.arf frames
   1015    * Give average a chance to settle though.
   1016    */
   1017   if ((cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8)) &&
   1018       (cpi->ni_frames > 150)) {
   1019     cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
   1020                                       ? (cpi->ni_av_qi + 32)
   1021                                       : cpi->worst_quality;
   1022     cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
   1023                                       ? (cpi->ni_av_qi - 32)
   1024                                       : cpi->best_quality;
   1025   }
   1026 
   1027   return Q;
   1028 }
   1029 
   1030 /* For cq mode estimate a cq level that matches the observed
   1031  * complexity and data rate.
   1032  */
   1033 static int estimate_cq(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
   1034                        int section_target_bandwitdh, int overhead_bits) {
   1035   int Q;
   1036   int num_mbs = cpi->common.MBs;
   1037   int target_norm_bits_per_mb;
   1038 
   1039   double section_err = (fpstats->coded_error / fpstats->count);
   1040   double err_per_mb = section_err / num_mbs;
   1041   double err_correction_factor;
   1042   double speed_correction = 1.0;
   1043   double clip_iiratio;
   1044   double clip_iifactor;
   1045   int overhead_bits_per_mb;
   1046 
   1047   if (0) {
   1048     FILE *f = fopen("epmp.stt", "a");
   1049     fprintf(f, "%10.2f\n", err_per_mb);
   1050     fclose(f);
   1051   }
   1052 
   1053   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
   1054                                 ? (512 * section_target_bandwitdh) / num_mbs
   1055                                 : 512 * (section_target_bandwitdh / num_mbs);
   1056 
   1057   /* Estimate of overhead bits per mb */
   1058   overhead_bits_per_mb = overhead_bits / num_mbs;
   1059 
   1060   /* Corrections for higher compression speed settings
   1061    * (reduced compression expected)
   1062    */
   1063   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
   1064     if (cpi->oxcf.cpu_used <= 5) {
   1065       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
   1066     } else {
   1067       speed_correction = 1.25;
   1068     }
   1069   }
   1070 
   1071   /* II ratio correction factor for clip as a whole */
   1072   clip_iiratio = cpi->twopass.total_stats.intra_error /
   1073                  DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
   1074   clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
   1075   if (clip_iifactor < 0.80) clip_iifactor = 0.80;
   1076 
   1077   /* Try and pick a Q that can encode the content at the given rate. */
   1078   for (Q = 0; Q < MAXQ; ++Q) {
   1079     int bits_per_mb_at_this_q;
   1080 
   1081     /* Error per MB based correction factor */
   1082     err_correction_factor =
   1083         calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q);
   1084 
   1085     bits_per_mb_at_this_q =
   1086         vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
   1087 
   1088     bits_per_mb_at_this_q =
   1089         (int)(.5 +
   1090               err_correction_factor * speed_correction * clip_iifactor *
   1091                   (double)bits_per_mb_at_this_q);
   1092 
   1093     /* Mode and motion overhead */
   1094     /* As Q rises in real encode loop rd code will force overhead down
   1095      * We make a crude adjustment for this here as *.98 per Q step.
   1096      */
   1097     overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
   1098 
   1099     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
   1100   }
   1101 
   1102   /* Clip value to range "best allowed to (worst allowed - 1)" */
   1103   Q = cq_level[Q];
   1104   if (Q >= cpi->worst_quality) Q = cpi->worst_quality - 1;
   1105   if (Q < cpi->best_quality) Q = cpi->best_quality;
   1106 
   1107   return Q;
   1108 }
   1109 
   1110 static int estimate_q(VP8_COMP *cpi, double section_err,
   1111                       int section_target_bandwitdh) {
   1112   int Q;
   1113   int num_mbs = cpi->common.MBs;
   1114   int target_norm_bits_per_mb;
   1115 
   1116   double err_per_mb = section_err / num_mbs;
   1117   double err_correction_factor;
   1118   double speed_correction = 1.0;
   1119 
   1120   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
   1121                                 ? (512 * section_target_bandwitdh) / num_mbs
   1122                                 : 512 * (section_target_bandwitdh / num_mbs);
   1123 
   1124   /* Corrections for higher compression speed settings
   1125    * (reduced compression expected)
   1126    */
   1127   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
   1128     if (cpi->oxcf.cpu_used <= 5) {
   1129       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
   1130     } else {
   1131       speed_correction = 1.25;
   1132     }
   1133   }
   1134 
   1135   /* Try and pick a Q that can encode the content at the given rate. */
   1136   for (Q = 0; Q < MAXQ; ++Q) {
   1137     int bits_per_mb_at_this_q;
   1138 
   1139     /* Error per MB based correction factor */
   1140     err_correction_factor =
   1141         calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
   1142 
   1143     bits_per_mb_at_this_q =
   1144         (int)(.5 + (err_correction_factor * speed_correction *
   1145                     cpi->twopass.est_max_qcorrection_factor *
   1146                     (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0));
   1147 
   1148     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
   1149   }
   1150 
   1151   return Q;
   1152 }
   1153 
   1154 /* Estimate a worst case Q for a KF group */
   1155 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err,
   1156                                int section_target_bandwitdh,
   1157                                double group_iiratio) {
   1158   int Q;
   1159   int num_mbs = cpi->common.MBs;
   1160   int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
   1161   int bits_per_mb_at_this_q;
   1162 
   1163   double err_per_mb = section_err / num_mbs;
   1164   double err_correction_factor;
   1165   double speed_correction = 1.0;
   1166   double current_spend_ratio = 1.0;
   1167 
   1168   double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
   1169   double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
   1170 
   1171   double iiratio_correction_factor = 1.0;
   1172 
   1173   double combined_correction_factor;
   1174 
   1175   /* Trap special case where the target is <= 0 */
   1176   if (target_norm_bits_per_mb <= 0) return MAXQ * 2;
   1177 
   1178   /* Calculate a corrective factor based on a rolling ratio of bits spent
   1179    *  vs target bits
   1180    * This is clamped to the range 0.1 to 10.0
   1181    */
   1182   if (cpi->long_rolling_target_bits <= 0) {
   1183     current_spend_ratio = 10.0;
   1184   } else {
   1185     current_spend_ratio = (double)cpi->long_rolling_actual_bits /
   1186                           (double)cpi->long_rolling_target_bits;
   1187     current_spend_ratio =
   1188         (current_spend_ratio > 10.0)
   1189             ? 10.0
   1190             : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
   1191   }
   1192 
   1193   /* Calculate a correction factor based on the quality of prediction in
   1194    * the sequence as indicated by intra_inter error score ratio (IIRatio)
   1195    * The idea here is to favour subsampling in the hardest sections vs
   1196    * the easyest.
   1197    */
   1198   iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
   1199 
   1200   if (iiratio_correction_factor < 0.5) iiratio_correction_factor = 0.5;
   1201 
   1202   /* Corrections for higher compression speed settings
   1203    * (reduced compression expected)
   1204    */
   1205   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
   1206     if (cpi->oxcf.cpu_used <= 5) {
   1207       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
   1208     } else {
   1209       speed_correction = 1.25;
   1210     }
   1211   }
   1212 
   1213   /* Combine the various factors calculated above */
   1214   combined_correction_factor =
   1215       speed_correction * iiratio_correction_factor * current_spend_ratio;
   1216 
   1217   /* Try and pick a Q that should be high enough to encode the content at
   1218    * the given rate.
   1219    */
   1220   for (Q = 0; Q < MAXQ; ++Q) {
   1221     /* Error per MB based correction factor */
   1222     err_correction_factor =
   1223         calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q);
   1224 
   1225     bits_per_mb_at_this_q =
   1226         (int)(.5 + (err_correction_factor * combined_correction_factor *
   1227                     (double)vp8_bits_per_mb[INTER_FRAME][Q]));
   1228 
   1229     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
   1230   }
   1231 
   1232   /* If we could not hit the target even at Max Q then estimate what Q
   1233    * would have been required
   1234    */
   1235   while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) &&
   1236          (Q < (MAXQ * 2))) {
   1237     bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
   1238     Q++;
   1239   }
   1240 
   1241   if (0) {
   1242     FILE *f = fopen("estkf_q.stt", "a");
   1243     fprintf(f, "%8d %8d %8d %8.2f %8.3f %8.2f %8.3f %8.3f %8.3f %8d\n",
   1244             cpi->common.current_video_frame, bits_per_mb_at_this_q,
   1245             target_norm_bits_per_mb, err_per_mb, err_correction_factor,
   1246             current_spend_ratio, group_iiratio, iiratio_correction_factor,
   1247             (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level,
   1248             Q);
   1249     fclose(f);
   1250   }
   1251 
   1252   return Q;
   1253 }
   1254 
   1255 void vp8_init_second_pass(VP8_COMP *cpi) {
   1256   FIRSTPASS_STATS this_frame;
   1257   FIRSTPASS_STATS *start_pos;
   1258 
   1259   double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
   1260                                       cpi->oxcf.two_pass_vbrmin_section / 100);
   1261 
   1262   zero_stats(&cpi->twopass.total_stats);
   1263   zero_stats(&cpi->twopass.total_left_stats);
   1264 
   1265   if (!cpi->twopass.stats_in_end) return;
   1266 
   1267   cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
   1268   cpi->twopass.total_left_stats = cpi->twopass.total_stats;
   1269 
   1270   /* each frame can have a different duration, as the frame rate in the
   1271    * source isn't guaranteed to be constant.   The frame rate prior to
   1272    * the first frame encoded in the second pass is a guess.  However the
   1273    * sum duration is not. Its calculated based on the actual durations of
   1274    * all frames from the first pass.
   1275    */
   1276   vp8_new_framerate(cpi,
   1277                     10000000.0 * cpi->twopass.total_stats.count /
   1278                         cpi->twopass.total_stats.duration);
   1279 
   1280   cpi->output_framerate = cpi->framerate;
   1281   cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration *
   1282                                      cpi->oxcf.target_bandwidth / 10000000.0);
   1283   cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration *
   1284                                       two_pass_min_rate / 10000000.0);
   1285 
   1286   /* Calculate a minimum intra value to be used in determining the IIratio
   1287    * scores used in the second pass. We have this minimum to make sure
   1288    * that clips that are static but "low complexity" in the intra domain
   1289    * are still boosted appropriately for KF/GF/ARF
   1290    */
   1291   cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
   1292   cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
   1293 
   1294   /* Scan the first pass file and calculate an average Intra / Inter error
   1295    * score ratio for the sequence
   1296    */
   1297   {
   1298     double sum_iiratio = 0.0;
   1299     double IIRatio;
   1300 
   1301     start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
   1302 
   1303     while (input_stats(cpi, &this_frame) != EOF) {
   1304       IIRatio =
   1305           this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
   1306       IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
   1307       sum_iiratio += IIRatio;
   1308     }
   1309 
   1310     cpi->twopass.avg_iiratio =
   1311         sum_iiratio /
   1312         DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
   1313 
   1314     /* Reset file position */
   1315     reset_fpf_position(cpi, start_pos);
   1316   }
   1317 
   1318   /* Scan the first pass file and calculate a modified total error based
   1319    * upon the bias/power function used to allocate bits
   1320    */
   1321   {
   1322     start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
   1323 
   1324     cpi->twopass.modified_error_total = 0.0;
   1325     cpi->twopass.modified_error_used = 0.0;
   1326 
   1327     while (input_stats(cpi, &this_frame) != EOF) {
   1328       cpi->twopass.modified_error_total +=
   1329           calculate_modified_err(cpi, &this_frame);
   1330     }
   1331     cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
   1332 
   1333     reset_fpf_position(cpi, start_pos); /* Reset file position */
   1334   }
   1335 }
   1336 
   1337 void vp8_end_second_pass(VP8_COMP *cpi) { (void)cpi; }
   1338 
   1339 /* This function gives and estimate of how badly we believe the prediction
   1340  * quality is decaying from frame to frame.
   1341  */
   1342 static double get_prediction_decay_rate(VP8_COMP *cpi,
   1343                                         FIRSTPASS_STATS *next_frame) {
   1344   double prediction_decay_rate;
   1345   double motion_decay;
   1346   double motion_pct = next_frame->pcnt_motion;
   1347   (void)cpi;
   1348 
   1349   /* Initial basis is the % mbs inter coded */
   1350   prediction_decay_rate = next_frame->pcnt_inter;
   1351 
   1352   /* High % motion -> somewhat higher decay rate */
   1353   motion_decay = (1.0 - (motion_pct / 20.0));
   1354   if (motion_decay < prediction_decay_rate) {
   1355     prediction_decay_rate = motion_decay;
   1356   }
   1357 
   1358   /* Adjustment to decay rate based on speed of motion */
   1359   {
   1360     double this_mv_rabs;
   1361     double this_mv_cabs;
   1362     double distance_factor;
   1363 
   1364     this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
   1365     this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
   1366 
   1367     distance_factor =
   1368         sqrt((this_mv_rabs * this_mv_rabs) + (this_mv_cabs * this_mv_cabs)) /
   1369         250.0;
   1370     distance_factor = ((distance_factor > 1.0) ? 0.0 : (1.0 - distance_factor));
   1371     if (distance_factor < prediction_decay_rate) {
   1372       prediction_decay_rate = distance_factor;
   1373     }
   1374   }
   1375 
   1376   return prediction_decay_rate;
   1377 }
   1378 
   1379 /* Function to test for a condition where a complex transition is followed
   1380  * by a static section. For example in slide shows where there is a fade
   1381  * between slides. This is to help with more optimal kf and gf positioning.
   1382  */
   1383 static int detect_transition_to_still(VP8_COMP *cpi, int frame_interval,
   1384                                       int still_interval,
   1385                                       double loop_decay_rate,
   1386                                       double decay_accumulator) {
   1387   int trans_to_still = 0;
   1388 
   1389   /* Break clause to detect very still sections after motion
   1390    * For example a static image after a fade or other transition
   1391    * instead of a clean scene cut.
   1392    */
   1393   if ((frame_interval > MIN_GF_INTERVAL) && (loop_decay_rate >= 0.999) &&
   1394       (decay_accumulator < 0.9)) {
   1395     int j;
   1396     FIRSTPASS_STATS *position = cpi->twopass.stats_in;
   1397     FIRSTPASS_STATS tmp_next_frame;
   1398     double decay_rate;
   1399 
   1400     /* Look ahead a few frames to see if static condition persists... */
   1401     for (j = 0; j < still_interval; ++j) {
   1402       if (EOF == input_stats(cpi, &tmp_next_frame)) break;
   1403 
   1404       decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
   1405       if (decay_rate < 0.999) break;
   1406     }
   1407     /* Reset file position */
   1408     reset_fpf_position(cpi, position);
   1409 
   1410     /* Only if it does do we signal a transition to still */
   1411     if (j == still_interval) trans_to_still = 1;
   1412   }
   1413 
   1414   return trans_to_still;
   1415 }
   1416 
   1417 /* This function detects a flash through the high relative pcnt_second_ref
   1418  * score in the frame following a flash frame. The offset passed in should
   1419  * reflect this
   1420  */
   1421 static int detect_flash(VP8_COMP *cpi, int offset) {
   1422   FIRSTPASS_STATS next_frame;
   1423 
   1424   int flash_detected = 0;
   1425 
   1426   /* Read the frame data. */
   1427   /* The return is 0 (no flash detected) if not a valid frame */
   1428   if (read_frame_stats(cpi, &next_frame, offset) != EOF) {
   1429     /* What we are looking for here is a situation where there is a
   1430      * brief break in prediction (such as a flash) but subsequent frames
   1431      * are reasonably well predicted by an earlier (pre flash) frame.
   1432      * The recovery after a flash is indicated by a high pcnt_second_ref
   1433      * comapred to pcnt_inter.
   1434      */
   1435     if ((next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
   1436         (next_frame.pcnt_second_ref >= 0.5)) {
   1437       flash_detected = 1;
   1438 
   1439       /*if (1)
   1440       {
   1441           FILE *f = fopen("flash.stt", "a");
   1442           fprintf(f, "%8.0f %6.2f %6.2f\n",
   1443               next_frame.frame,
   1444               next_frame.pcnt_inter,
   1445               next_frame.pcnt_second_ref);
   1446           fclose(f);
   1447       }*/
   1448     }
   1449   }
   1450 
   1451   return flash_detected;
   1452 }
   1453 
   1454 /* Update the motion related elements to the GF arf boost calculation */
   1455 static void accumulate_frame_motion_stats(VP8_COMP *cpi,
   1456                                           FIRSTPASS_STATS *this_frame,
   1457                                           double *this_frame_mv_in_out,
   1458                                           double *mv_in_out_accumulator,
   1459                                           double *abs_mv_in_out_accumulator,
   1460                                           double *mv_ratio_accumulator) {
   1461   double this_frame_mvr_ratio;
   1462   double this_frame_mvc_ratio;
   1463   double motion_pct;
   1464   (void)cpi;
   1465 
   1466   /* Accumulate motion stats. */
   1467   motion_pct = this_frame->pcnt_motion;
   1468 
   1469   /* Accumulate Motion In/Out of frame stats */
   1470   *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
   1471   *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
   1472   *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct);
   1473 
   1474   /* Accumulate a measure of how uniform (or conversely how random)
   1475    * the motion field is. (A ratio of absmv / mv)
   1476    */
   1477   if (motion_pct > 0.05) {
   1478     this_frame_mvr_ratio =
   1479         fabs(this_frame->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
   1480 
   1481     this_frame_mvc_ratio =
   1482         fabs(this_frame->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
   1483 
   1484     *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs)
   1485                                  ? (this_frame_mvr_ratio * motion_pct)
   1486                                  : this_frame->mvr_abs * motion_pct;
   1487 
   1488     *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs)
   1489                                  ? (this_frame_mvc_ratio * motion_pct)
   1490                                  : this_frame->mvc_abs * motion_pct;
   1491   }
   1492 }
   1493 
   1494 /* Calculate a baseline boost number for the current frame. */
   1495 static double calc_frame_boost(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame,
   1496                                double this_frame_mv_in_out) {
   1497   double frame_boost;
   1498 
   1499   /* Underlying boost factor is based on inter intra error ratio */
   1500   if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) {
   1501     frame_boost = (IIFACTOR * this_frame->intra_error /
   1502                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
   1503   } else {
   1504     frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
   1505                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
   1506   }
   1507 
   1508   /* Increase boost for frames where new data coming into frame
   1509    * (eg zoom out). Slightly reduce boost if there is a net balance
   1510    * of motion out of the frame (zoom in).
   1511    * The range for this_frame_mv_in_out is -1.0 to +1.0
   1512    */
   1513   if (this_frame_mv_in_out > 0.0) {
   1514     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
   1515     /* In extreme case boost is halved */
   1516   } else {
   1517     frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
   1518   }
   1519 
   1520   /* Clip to maximum */
   1521   if (frame_boost > GF_RMAX) frame_boost = GF_RMAX;
   1522 
   1523   return frame_boost;
   1524 }
   1525 
   1526 #if NEW_BOOST
   1527 static int calc_arf_boost(VP8_COMP *cpi, int offset, int f_frames, int b_frames,
   1528                           int *f_boost, int *b_boost) {
   1529   FIRSTPASS_STATS this_frame;
   1530 
   1531   int i;
   1532   double boost_score = 0.0;
   1533   double mv_ratio_accumulator = 0.0;
   1534   double decay_accumulator = 1.0;
   1535   double this_frame_mv_in_out = 0.0;
   1536   double mv_in_out_accumulator = 0.0;
   1537   double abs_mv_in_out_accumulator = 0.0;
   1538   double r;
   1539   int flash_detected = 0;
   1540 
   1541   /* Search forward from the proposed arf/next gf position */
   1542   for (i = 0; i < f_frames; ++i) {
   1543     if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
   1544 
   1545     /* Update the motion related elements to the boost calculation */
   1546     accumulate_frame_motion_stats(
   1547         cpi, &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
   1548         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
   1549 
   1550     /* Calculate the baseline boost number for this frame */
   1551     r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
   1552 
   1553     /* We want to discount the the flash frame itself and the recovery
   1554      * frame that follows as both will have poor scores.
   1555      */
   1556     flash_detected =
   1557         detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
   1558 
   1559     /* Cumulative effect of prediction quality decay */
   1560     if (!flash_detected) {
   1561       decay_accumulator =
   1562           decay_accumulator * get_prediction_decay_rate(cpi, &this_frame);
   1563       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
   1564     }
   1565     boost_score += (decay_accumulator * r);
   1566 
   1567     /* Break out conditions. */
   1568     if ((!flash_detected) &&
   1569         ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
   1570          (mv_in_out_accumulator < -2.0))) {
   1571       break;
   1572     }
   1573   }
   1574 
   1575   *f_boost = (int)(boost_score * 100.0) >> 4;
   1576 
   1577   /* Reset for backward looking loop */
   1578   boost_score = 0.0;
   1579   mv_ratio_accumulator = 0.0;
   1580   decay_accumulator = 1.0;
   1581   this_frame_mv_in_out = 0.0;
   1582   mv_in_out_accumulator = 0.0;
   1583   abs_mv_in_out_accumulator = 0.0;
   1584 
   1585   /* Search forward from the proposed arf/next gf position */
   1586   for (i = -1; i >= -b_frames; i--) {
   1587     if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
   1588 
   1589     /* Update the motion related elements to the boost calculation */
   1590     accumulate_frame_motion_stats(
   1591         cpi, &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
   1592         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
   1593 
   1594     /* Calculate the baseline boost number for this frame */
   1595     r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
   1596 
   1597     /* We want to discount the the flash frame itself and the recovery
   1598      * frame that follows as both will have poor scores.
   1599      */
   1600     flash_detected =
   1601         detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
   1602 
   1603     /* Cumulative effect of prediction quality decay */
   1604     if (!flash_detected) {
   1605       decay_accumulator =
   1606           decay_accumulator * get_prediction_decay_rate(cpi, &this_frame);
   1607       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
   1608     }
   1609 
   1610     boost_score += (decay_accumulator * r);
   1611 
   1612     /* Break out conditions. */
   1613     if ((!flash_detected) &&
   1614         ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
   1615          (mv_in_out_accumulator < -2.0))) {
   1616       break;
   1617     }
   1618   }
   1619   *b_boost = (int)(boost_score * 100.0) >> 4;
   1620 
   1621   return (*f_boost + *b_boost);
   1622 }
   1623 #endif
   1624 
   1625 /* Analyse and define a gf/arf group . */
   1626 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   1627   FIRSTPASS_STATS next_frame;
   1628   FIRSTPASS_STATS *start_pos;
   1629   int i;
   1630   double r;
   1631   double boost_score = 0.0;
   1632   double old_boost_score = 0.0;
   1633   double gf_group_err = 0.0;
   1634   double gf_first_frame_err = 0.0;
   1635   double mod_frame_err = 0.0;
   1636 
   1637   double mv_ratio_accumulator = 0.0;
   1638   double decay_accumulator = 1.0;
   1639 
   1640   double loop_decay_rate = 1.00; /* Starting decay rate */
   1641 
   1642   double this_frame_mv_in_out = 0.0;
   1643   double mv_in_out_accumulator = 0.0;
   1644   double abs_mv_in_out_accumulator = 0.0;
   1645   double mod_err_per_mb_accumulator = 0.0;
   1646 
   1647   int max_bits = frame_max_bits(cpi); /* Max for a single frame */
   1648 
   1649   unsigned int allow_alt_ref =
   1650       cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
   1651 
   1652   int alt_boost = 0;
   1653   int f_boost = 0;
   1654   int b_boost = 0;
   1655   int flash_detected;
   1656 
   1657   cpi->twopass.gf_group_bits = 0;
   1658   cpi->twopass.gf_decay_rate = 0;
   1659 
   1660   vpx_clear_system_state();
   1661 
   1662   start_pos = cpi->twopass.stats_in;
   1663 
   1664   memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */
   1665 
   1666   /* Load stats for the current frame. */
   1667   mod_frame_err = calculate_modified_err(cpi, this_frame);
   1668 
   1669   /* Note the error of the frame at the start of the group (this will be
   1670    * the GF frame error if we code a normal gf
   1671    */
   1672   gf_first_frame_err = mod_frame_err;
   1673 
   1674   /* Special treatment if the current frame is a key frame (which is also
   1675    * a gf). If it is then its error score (and hence bit allocation) need
   1676    * to be subtracted out from the calculation for the GF group
   1677    */
   1678   if (cpi->common.frame_type == KEY_FRAME) gf_group_err -= gf_first_frame_err;
   1679 
   1680   /* Scan forward to try and work out how many frames the next gf group
   1681    * should contain and what level of boost is appropriate for the GF
   1682    * or ARF that will be coded with the group
   1683    */
   1684   i = 0;
   1685 
   1686   while (((i < cpi->twopass.static_scene_max_gf_interval) ||
   1687           ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
   1688          (i < cpi->twopass.frames_to_key)) {
   1689     i++;
   1690 
   1691     /* Accumulate error score of frames in this gf group */
   1692     mod_frame_err = calculate_modified_err(cpi, this_frame);
   1693 
   1694     gf_group_err += mod_frame_err;
   1695 
   1696     mod_err_per_mb_accumulator +=
   1697         mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
   1698 
   1699     if (EOF == input_stats(cpi, &next_frame)) break;
   1700 
   1701     /* Test for the case where there is a brief flash but the prediction
   1702      * quality back to an earlier frame is then restored.
   1703      */
   1704     flash_detected = detect_flash(cpi, 0);
   1705 
   1706     /* Update the motion related elements to the boost calculation */
   1707     accumulate_frame_motion_stats(
   1708         cpi, &next_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
   1709         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
   1710 
   1711     /* Calculate a baseline boost number for this frame */
   1712     r = calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out);
   1713 
   1714     /* Cumulative effect of prediction quality decay */
   1715     if (!flash_detected) {
   1716       loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
   1717       decay_accumulator = decay_accumulator * loop_decay_rate;
   1718       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
   1719     }
   1720     boost_score += (decay_accumulator * r);
   1721 
   1722     /* Break clause to detect very still sections after motion
   1723      * For example a staic image after a fade or other transition.
   1724      */
   1725     if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
   1726                                    decay_accumulator)) {
   1727       allow_alt_ref = 0;
   1728       boost_score = old_boost_score;
   1729       break;
   1730     }
   1731 
   1732     /* Break out conditions. */
   1733     if (
   1734         /* Break at cpi->max_gf_interval unless almost totally static */
   1735         (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
   1736         (
   1737             /* Dont break out with a very short interval */
   1738             (i > MIN_GF_INTERVAL) &&
   1739             /* Dont break out very close to a key frame */
   1740             ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
   1741             ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
   1742             (!flash_detected) && ((mv_ratio_accumulator > 100.0) ||
   1743                                   (abs_mv_in_out_accumulator > 3.0) ||
   1744                                   (mv_in_out_accumulator < -2.0) ||
   1745                                   ((boost_score - old_boost_score) < 2.0)))) {
   1746       boost_score = old_boost_score;
   1747       break;
   1748     }
   1749 
   1750     memcpy(this_frame, &next_frame, sizeof(*this_frame));
   1751 
   1752     old_boost_score = boost_score;
   1753   }
   1754 
   1755   cpi->twopass.gf_decay_rate =
   1756       (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
   1757 
   1758   /* When using CBR apply additional buffer related upper limits */
   1759   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
   1760     double max_boost;
   1761 
   1762     /* For cbr apply buffer related limits */
   1763     if (cpi->drop_frames_allowed) {
   1764       int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark *
   1765                                 (cpi->oxcf.optimal_buffer_level / 100);
   1766 
   1767       if (cpi->buffer_level > df_buffer_level) {
   1768         max_boost =
   1769             ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
   1770             DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   1771       } else {
   1772         max_boost = 0.0;
   1773       }
   1774     } else if (cpi->buffer_level > 0) {
   1775       max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
   1776                   DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   1777     } else {
   1778       max_boost = 0.0;
   1779     }
   1780 
   1781     if (boost_score > max_boost) boost_score = max_boost;
   1782   }
   1783 
   1784   /* Dont allow conventional gf too near the next kf */
   1785   if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) {
   1786     while (i < cpi->twopass.frames_to_key) {
   1787       i++;
   1788 
   1789       if (EOF == input_stats(cpi, this_frame)) break;
   1790 
   1791       if (i < cpi->twopass.frames_to_key) {
   1792         mod_frame_err = calculate_modified_err(cpi, this_frame);
   1793         gf_group_err += mod_frame_err;
   1794       }
   1795     }
   1796   }
   1797 
   1798   cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
   1799 
   1800 #if NEW_BOOST
   1801   /* Alterrnative boost calculation for alt ref */
   1802   alt_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
   1803 #endif
   1804 
   1805   /* Should we use the alternate refernce frame */
   1806   if (allow_alt_ref && (i >= MIN_GF_INTERVAL) &&
   1807       /* dont use ARF very near next kf */
   1808       (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
   1809 #if NEW_BOOST
   1810       ((next_frame.pcnt_inter > 0.75) || (next_frame.pcnt_second_ref > 0.5)) &&
   1811       ((mv_in_out_accumulator / (double)i > -0.2) ||
   1812        (mv_in_out_accumulator > -2.0)) &&
   1813       (b_boost > 100) && (f_boost > 100))
   1814 #else
   1815       (next_frame.pcnt_inter > 0.75) &&
   1816       ((mv_in_out_accumulator / (double)i > -0.2) ||
   1817        (mv_in_out_accumulator > -2.0)) &&
   1818       (cpi->gfu_boost > 100) && (cpi->twopass.gf_decay_rate <=
   1819                                  (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))))
   1820 #endif
   1821   {
   1822     int Boost;
   1823     int allocation_chunks;
   1824     int Q =
   1825         (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
   1826     int tmp_q;
   1827     int arf_frame_bits = 0;
   1828     int group_bits;
   1829 
   1830 #if NEW_BOOST
   1831     cpi->gfu_boost = alt_boost;
   1832 #endif
   1833 
   1834     /* Estimate the bits to be allocated to the group as a whole */
   1835     if ((cpi->twopass.kf_group_bits > 0) &&
   1836         (cpi->twopass.kf_group_error_left > 0)) {
   1837       group_bits =
   1838           (int)((double)cpi->twopass.kf_group_bits *
   1839                 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
   1840     } else {
   1841       group_bits = 0;
   1842     }
   1843 
   1844 /* Boost for arf frame */
   1845 #if NEW_BOOST
   1846     Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
   1847 #else
   1848     Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
   1849 #endif
   1850     Boost += (i * 50);
   1851 
   1852     /* Set max and minimum boost and hence minimum allocation */
   1853     if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
   1854       Boost = ((cpi->baseline_gf_interval + 1) * 200);
   1855     } else if (Boost < 125) {
   1856       Boost = 125;
   1857     }
   1858 
   1859     allocation_chunks = (i * 100) + Boost;
   1860 
   1861     /* Normalize Altboost and allocations chunck down to prevent overflow */
   1862     while (Boost > 1000) {
   1863       Boost /= 2;
   1864       allocation_chunks /= 2;
   1865     }
   1866 
   1867     /* Calculate the number of bits to be spent on the arf based on the
   1868      * boost number
   1869      */
   1870     arf_frame_bits =
   1871         (int)((double)Boost * (group_bits / (double)allocation_chunks));
   1872 
   1873     /* Estimate if there are enough bits available to make worthwhile use
   1874      * of an arf.
   1875      */
   1876     tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
   1877 
   1878     /* Only use an arf if it is likely we will be able to code
   1879      * it at a lower Q than the surrounding frames.
   1880      */
   1881     if (tmp_q < cpi->worst_quality) {
   1882       int half_gf_int;
   1883       int frames_after_arf;
   1884       int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
   1885       int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
   1886 
   1887       cpi->source_alt_ref_pending = 1;
   1888 
   1889       /*
   1890        * For alt ref frames the error score for the end frame of the
   1891        * group (the alt ref frame) should not contribute to the group
   1892        * total and hence the number of bit allocated to the group.
   1893        * Rather it forms part of the next group (it is the GF at the
   1894        * start of the next group)
   1895        * gf_group_err -= mod_frame_err;
   1896        *
   1897        * For alt ref frames alt ref frame is technically part of the
   1898        * GF frame for the next group but we always base the error
   1899        * calculation and bit allocation on the current group of frames.
   1900        *
   1901        * Set the interval till the next gf or arf.
   1902        * For ARFs this is the number of frames to be coded before the
   1903        * future frame that is coded as an ARF.
   1904        * The future frame itself is part of the next group
   1905        */
   1906       cpi->baseline_gf_interval = i;
   1907 
   1908       /*
   1909        * Define the arnr filter width for this group of frames:
   1910        * We only filter frames that lie within a distance of half
   1911        * the GF interval from the ARF frame. We also have to trap
   1912        * cases where the filter extends beyond the end of clip.
   1913        * Note: this_frame->frame has been updated in the loop
   1914        * so it now points at the ARF frame.
   1915        */
   1916       half_gf_int = cpi->baseline_gf_interval >> 1;
   1917       frames_after_arf =
   1918           (int)(cpi->twopass.total_stats.count - this_frame->frame - 1);
   1919 
   1920       switch (cpi->oxcf.arnr_type) {
   1921         case 1: /* Backward filter */
   1922           frames_fwd = 0;
   1923           if (frames_bwd > half_gf_int) frames_bwd = half_gf_int;
   1924           break;
   1925 
   1926         case 2: /* Forward filter */
   1927           if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
   1928           if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
   1929           frames_bwd = 0;
   1930           break;
   1931 
   1932         case 3: /* Centered filter */
   1933         default:
   1934           frames_fwd >>= 1;
   1935           if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
   1936           if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
   1937 
   1938           frames_bwd = frames_fwd;
   1939 
   1940           /* For even length filter there is one more frame backward
   1941            * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
   1942            */
   1943           if (frames_bwd < half_gf_int) {
   1944             frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
   1945           }
   1946           break;
   1947       }
   1948 
   1949       cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
   1950     } else {
   1951       cpi->source_alt_ref_pending = 0;
   1952       cpi->baseline_gf_interval = i;
   1953     }
   1954   } else {
   1955     cpi->source_alt_ref_pending = 0;
   1956     cpi->baseline_gf_interval = i;
   1957   }
   1958 
   1959   /*
   1960    * Now decide how many bits should be allocated to the GF group as  a
   1961    * proportion of those remaining in the kf group.
   1962    * The final key frame group in the clip is treated as a special case
   1963    * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
   1964    * This is also important for short clips where there may only be one
   1965    * key frame.
   1966    */
   1967   if (cpi->twopass.frames_to_key >=
   1968       (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame)) {
   1969     cpi->twopass.kf_group_bits =
   1970         (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
   1971   }
   1972 
   1973   /* Calculate the bits to be allocated to the group as a whole */
   1974   if ((cpi->twopass.kf_group_bits > 0) &&
   1975       (cpi->twopass.kf_group_error_left > 0)) {
   1976     cpi->twopass.gf_group_bits =
   1977         (int64_t)(cpi->twopass.kf_group_bits *
   1978                   (gf_group_err / cpi->twopass.kf_group_error_left));
   1979   } else {
   1980     cpi->twopass.gf_group_bits = 0;
   1981   }
   1982 
   1983   cpi->twopass.gf_group_bits =
   1984       (cpi->twopass.gf_group_bits < 0)
   1985           ? 0
   1986           : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
   1987                 ? cpi->twopass.kf_group_bits
   1988                 : cpi->twopass.gf_group_bits;
   1989 
   1990   /* Clip cpi->twopass.gf_group_bits based on user supplied data rate
   1991    * variability limit (cpi->oxcf.two_pass_vbrmax_section)
   1992    */
   1993   if (cpi->twopass.gf_group_bits >
   1994       (int64_t)max_bits * cpi->baseline_gf_interval) {
   1995     cpi->twopass.gf_group_bits = (int64_t)max_bits * cpi->baseline_gf_interval;
   1996   }
   1997 
   1998   /* Reset the file position */
   1999   reset_fpf_position(cpi, start_pos);
   2000 
   2001   /* Update the record of error used so far (only done once per gf group) */
   2002   cpi->twopass.modified_error_used += gf_group_err;
   2003 
   2004   /* Assign  bits to the arf or gf. */
   2005   for (i = 0; i <= (cpi->source_alt_ref_pending &&
   2006                     cpi->common.frame_type != KEY_FRAME);
   2007        i++) {
   2008     int Boost;
   2009     int allocation_chunks;
   2010     int Q =
   2011         (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
   2012     int gf_bits;
   2013 
   2014     /* For ARF frames */
   2015     if (cpi->source_alt_ref_pending && i == 0) {
   2016 #if NEW_BOOST
   2017       Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
   2018 #else
   2019       Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
   2020 #endif
   2021       Boost += (cpi->baseline_gf_interval * 50);
   2022 
   2023       /* Set max and minimum boost and hence minimum allocation */
   2024       if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
   2025         Boost = ((cpi->baseline_gf_interval + 1) * 200);
   2026       } else if (Boost < 125) {
   2027         Boost = 125;
   2028       }
   2029 
   2030       allocation_chunks = ((cpi->baseline_gf_interval + 1) * 100) + Boost;
   2031     }
   2032     /* Else for standard golden frames */
   2033     else {
   2034       /* boost based on inter / intra ratio of subsequent frames */
   2035       Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
   2036 
   2037       /* Set max and minimum boost and hence minimum allocation */
   2038       if (Boost > (cpi->baseline_gf_interval * 150)) {
   2039         Boost = (cpi->baseline_gf_interval * 150);
   2040       } else if (Boost < 125) {
   2041         Boost = 125;
   2042       }
   2043 
   2044       allocation_chunks = (cpi->baseline_gf_interval * 100) + (Boost - 100);
   2045     }
   2046 
   2047     /* Normalize Altboost and allocations chunck down to prevent overflow */
   2048     while (Boost > 1000) {
   2049       Boost /= 2;
   2050       allocation_chunks /= 2;
   2051     }
   2052 
   2053     /* Calculate the number of bits to be spent on the gf or arf based on
   2054      * the boost number
   2055      */
   2056     gf_bits = (int)((double)Boost *
   2057                     (cpi->twopass.gf_group_bits / (double)allocation_chunks));
   2058 
   2059     /* If the frame that is to be boosted is simpler than the average for
   2060      * the gf/arf group then use an alternative calculation
   2061      * based on the error score of the frame itself
   2062      */
   2063     if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) {
   2064       double alt_gf_grp_bits;
   2065       int alt_gf_bits;
   2066 
   2067       alt_gf_grp_bits =
   2068           (double)cpi->twopass.kf_group_bits *
   2069           (mod_frame_err * (double)cpi->baseline_gf_interval) /
   2070           DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
   2071 
   2072       alt_gf_bits =
   2073           (int)((double)Boost * (alt_gf_grp_bits / (double)allocation_chunks));
   2074 
   2075       if (gf_bits > alt_gf_bits) {
   2076         gf_bits = alt_gf_bits;
   2077       }
   2078     }
   2079     /* Else if it is harder than other frames in the group make sure it at
   2080      * least receives an allocation in keeping with its relative error
   2081      * score, otherwise it may be worse off than an "un-boosted" frame
   2082      */
   2083     else {
   2084       int alt_gf_bits =
   2085           (int)((double)cpi->twopass.kf_group_bits * mod_frame_err /
   2086                 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
   2087 
   2088       if (alt_gf_bits > gf_bits) {
   2089         gf_bits = alt_gf_bits;
   2090       }
   2091     }
   2092 
   2093     /* Apply an additional limit for CBR */
   2094     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
   2095       if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1)) {
   2096         cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1);
   2097       }
   2098     }
   2099 
   2100     /* Dont allow a negative value for gf_bits */
   2101     if (gf_bits < 0) gf_bits = 0;
   2102 
   2103     /* Add in minimum for a frame */
   2104     gf_bits += cpi->min_frame_bandwidth;
   2105 
   2106     if (i == 0) {
   2107       cpi->twopass.gf_bits = gf_bits;
   2108     }
   2109     if (i == 1 || (!cpi->source_alt_ref_pending &&
   2110                    (cpi->common.frame_type != KEY_FRAME))) {
   2111       /* Per frame bit target for this frame */
   2112       cpi->per_frame_bandwidth = gf_bits;
   2113     }
   2114   }
   2115 
   2116   {
   2117     /* Adjust KF group bits and error remainin */
   2118     cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
   2119     cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
   2120 
   2121     if (cpi->twopass.kf_group_bits < 0) cpi->twopass.kf_group_bits = 0;
   2122 
   2123     /* Note the error score left in the remaining frames of the group.
   2124      * For normal GFs we want to remove the error score for the first
   2125      * frame of the group (except in Key frame case where this has
   2126      * already happened)
   2127      */
   2128     if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) {
   2129       cpi->twopass.gf_group_error_left =
   2130           (int)(gf_group_err - gf_first_frame_err);
   2131     } else {
   2132       cpi->twopass.gf_group_error_left = (int)gf_group_err;
   2133     }
   2134 
   2135     cpi->twopass.gf_group_bits -=
   2136         cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
   2137 
   2138     if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
   2139 
   2140     /* This condition could fail if there are two kfs very close together
   2141      * despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the
   2142      * calculation of cpi->twopass.alt_extra_bits.
   2143      */
   2144     if (cpi->baseline_gf_interval >= 3) {
   2145 #if NEW_BOOST
   2146       int boost = (cpi->source_alt_ref_pending) ? b_boost : cpi->gfu_boost;
   2147 #else
   2148       int boost = cpi->gfu_boost;
   2149 #endif
   2150       if (boost >= 150) {
   2151         int pct_extra;
   2152 
   2153         pct_extra = (boost - 100) / 50;
   2154         pct_extra = (pct_extra > 20) ? 20 : pct_extra;
   2155 
   2156         cpi->twopass.alt_extra_bits =
   2157             (int)(cpi->twopass.gf_group_bits * pct_extra) / 100;
   2158         cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
   2159         cpi->twopass.alt_extra_bits /= ((cpi->baseline_gf_interval - 1) >> 1);
   2160       } else {
   2161         cpi->twopass.alt_extra_bits = 0;
   2162       }
   2163     } else {
   2164       cpi->twopass.alt_extra_bits = 0;
   2165     }
   2166   }
   2167 
   2168   /* Adjustments based on a measure of complexity of the section */
   2169   if (cpi->common.frame_type != KEY_FRAME) {
   2170     FIRSTPASS_STATS sectionstats;
   2171     double Ratio;
   2172 
   2173     zero_stats(&sectionstats);
   2174     reset_fpf_position(cpi, start_pos);
   2175 
   2176     for (i = 0; i < cpi->baseline_gf_interval; ++i) {
   2177       input_stats(cpi, &next_frame);
   2178       accumulate_stats(&sectionstats, &next_frame);
   2179     }
   2180 
   2181     avg_stats(&sectionstats);
   2182 
   2183     cpi->twopass.section_intra_rating =
   2184         (unsigned int)(sectionstats.intra_error /
   2185                        DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
   2186 
   2187     Ratio = sectionstats.intra_error /
   2188             DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
   2189     cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
   2190 
   2191     if (cpi->twopass.section_max_qfactor < 0.80) {
   2192       cpi->twopass.section_max_qfactor = 0.80;
   2193     }
   2194 
   2195     reset_fpf_position(cpi, start_pos);
   2196   }
   2197 }
   2198 
   2199 /* Allocate bits to a normal frame that is neither a gf an arf or a key frame.
   2200  */
   2201 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   2202   int target_frame_size;
   2203 
   2204   double modified_err;
   2205   double err_fraction;
   2206 
   2207   int max_bits = frame_max_bits(cpi); /* Max for a single frame */
   2208 
   2209   /* Calculate modified prediction error used in bit allocation */
   2210   modified_err = calculate_modified_err(cpi, this_frame);
   2211 
   2212   /* What portion of the remaining GF group error is used by this frame */
   2213   if (cpi->twopass.gf_group_error_left > 0) {
   2214     err_fraction = modified_err / cpi->twopass.gf_group_error_left;
   2215   } else {
   2216     err_fraction = 0.0;
   2217   }
   2218 
   2219   /* How many of those bits available for allocation should we give it? */
   2220   target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
   2221 
   2222   /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits)
   2223    * at the top end.
   2224    */
   2225   if (target_frame_size < 0) {
   2226     target_frame_size = 0;
   2227   } else {
   2228     if (target_frame_size > max_bits) target_frame_size = max_bits;
   2229 
   2230     if (target_frame_size > cpi->twopass.gf_group_bits) {
   2231       target_frame_size = (int)cpi->twopass.gf_group_bits;
   2232     }
   2233   }
   2234 
   2235   /* Adjust error and bits remaining */
   2236   cpi->twopass.gf_group_error_left -= (int)modified_err;
   2237   cpi->twopass.gf_group_bits -= target_frame_size;
   2238 
   2239   if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
   2240 
   2241   /* Add in the minimum number of bits that is set aside for every frame. */
   2242   target_frame_size += cpi->min_frame_bandwidth;
   2243 
   2244   /* Every other frame gets a few extra bits */
   2245   if ((cpi->frames_since_golden & 0x01) &&
   2246       (cpi->frames_till_gf_update_due > 0)) {
   2247     target_frame_size += cpi->twopass.alt_extra_bits;
   2248   }
   2249 
   2250   /* Per frame bit target for this frame */
   2251   cpi->per_frame_bandwidth = target_frame_size;
   2252 }
   2253 
   2254 void vp8_second_pass(VP8_COMP *cpi) {
   2255   int tmp_q;
   2256   int frames_left =
   2257       (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
   2258 
   2259   FIRSTPASS_STATS this_frame;
   2260   FIRSTPASS_STATS this_frame_copy;
   2261 
   2262   double this_frame_intra_error;
   2263   double this_frame_coded_error;
   2264 
   2265   int overhead_bits;
   2266 
   2267   vp8_zero(this_frame);
   2268 
   2269   if (!cpi->twopass.stats_in) {
   2270     return;
   2271   }
   2272 
   2273   vpx_clear_system_state();
   2274 
   2275   if (EOF == input_stats(cpi, &this_frame)) return;
   2276 
   2277   this_frame_intra_error = this_frame.intra_error;
   2278   this_frame_coded_error = this_frame.coded_error;
   2279 
   2280   /* keyframe and section processing ! */
   2281   if (cpi->twopass.frames_to_key == 0) {
   2282     /* Define next KF group and assign bits to it */
   2283     memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   2284     find_next_key_frame(cpi, &this_frame_copy);
   2285 
   2286     /* Special case: Error error_resilient_mode mode does not make much
   2287      * sense for two pass but with its current meaning this code is
   2288      * designed to stop outlandish behaviour if someone does set it when
   2289      * using two pass. It effectively disables GF groups. This is
   2290      * temporary code until we decide what should really happen in this
   2291      * case.
   2292      */
   2293     if (cpi->oxcf.error_resilient_mode) {
   2294       cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
   2295       cpi->twopass.gf_group_error_left = (int)cpi->twopass.kf_group_error_left;
   2296       cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
   2297       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
   2298       cpi->source_alt_ref_pending = 0;
   2299     }
   2300   }
   2301 
   2302   /* Is this a GF / ARF (Note that a KF is always also a GF) */
   2303   if (cpi->frames_till_gf_update_due == 0) {
   2304     /* Define next gf group and assign bits to it */
   2305     memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   2306     define_gf_group(cpi, &this_frame_copy);
   2307 
   2308     /* If we are going to code an altref frame at the end of the group
   2309      * and the current frame is not a key frame.... If the previous
   2310      * group used an arf this frame has already benefited from that arf
   2311      * boost and it should not be given extra bits If the previous
   2312      * group was NOT coded using arf we may want to apply some boost to
   2313      * this GF as well
   2314      */
   2315     if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) {
   2316       /* Assign a standard frames worth of bits from those allocated
   2317        * to the GF group
   2318        */
   2319       int bak = cpi->per_frame_bandwidth;
   2320       memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   2321       assign_std_frame_bits(cpi, &this_frame_copy);
   2322       cpi->per_frame_bandwidth = bak;
   2323     }
   2324   }
   2325 
   2326   /* Otherwise this is an ordinary frame */
   2327   else {
   2328     /* Special case: Error error_resilient_mode mode does not make much
   2329      * sense for two pass but with its current meaning but this code is
   2330      * designed to stop outlandish behaviour if someone does set it
   2331      * when using two pass. It effectively disables GF groups. This is
   2332      * temporary code till we decide what should really happen in this
   2333      * case.
   2334      */
   2335     if (cpi->oxcf.error_resilient_mode) {
   2336       cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
   2337 
   2338       if (cpi->common.frame_type != KEY_FRAME) {
   2339         /* Assign bits from those allocated to the GF group */
   2340         memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   2341         assign_std_frame_bits(cpi, &this_frame_copy);
   2342       }
   2343     } else {
   2344       /* Assign bits from those allocated to the GF group */
   2345       memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   2346       assign_std_frame_bits(cpi, &this_frame_copy);
   2347     }
   2348   }
   2349 
   2350   /* Keep a globally available copy of this and the next frame's iiratio. */
   2351   cpi->twopass.this_iiratio =
   2352       (unsigned int)(this_frame_intra_error /
   2353                      DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
   2354   {
   2355     FIRSTPASS_STATS next_frame;
   2356     if (lookup_next_frame_stats(cpi, &next_frame) != EOF) {
   2357       cpi->twopass.next_iiratio =
   2358           (unsigned int)(next_frame.intra_error /
   2359                          DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
   2360     }
   2361   }
   2362 
   2363   /* Set nominal per second bandwidth for this frame */
   2364   cpi->target_bandwidth =
   2365       (int)(cpi->per_frame_bandwidth * cpi->output_framerate);
   2366   if (cpi->target_bandwidth < 0) cpi->target_bandwidth = 0;
   2367 
   2368   /* Account for mv, mode and other overheads. */
   2369   overhead_bits = (int)estimate_modemvcost(cpi, &cpi->twopass.total_left_stats);
   2370 
   2371   /* Special case code for first frame. */
   2372   if (cpi->common.current_video_frame == 0) {
   2373     cpi->twopass.est_max_qcorrection_factor = 1.0;
   2374 
   2375     /* Set a cq_level in constrained quality mode. */
   2376     if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
   2377       int est_cq;
   2378 
   2379       est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats,
   2380                            (int)(cpi->twopass.bits_left / frames_left),
   2381                            overhead_bits);
   2382 
   2383       cpi->cq_target_quality = cpi->oxcf.cq_level;
   2384       if (est_cq > cpi->cq_target_quality) cpi->cq_target_quality = est_cq;
   2385     }
   2386 
   2387     /* guess at maxq needed in 2nd pass */
   2388     cpi->twopass.maxq_max_limit = cpi->worst_quality;
   2389     cpi->twopass.maxq_min_limit = cpi->best_quality;
   2390 
   2391     tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
   2392                            (int)(cpi->twopass.bits_left / frames_left),
   2393                            overhead_bits);
   2394 
   2395     /* Limit the maxq value returned subsequently.
   2396      * This increases the risk of overspend or underspend if the initial
   2397      * estimate for the clip is bad, but helps prevent excessive
   2398      * variation in Q, especially near the end of a clip
   2399      * where for example a small overspend may cause Q to crash
   2400      */
   2401     cpi->twopass.maxq_max_limit =
   2402         ((tmp_q + 32) < cpi->worst_quality) ? (tmp_q + 32) : cpi->worst_quality;
   2403     cpi->twopass.maxq_min_limit =
   2404         ((tmp_q - 32) > cpi->best_quality) ? (tmp_q - 32) : cpi->best_quality;
   2405 
   2406     cpi->active_worst_quality = tmp_q;
   2407     cpi->ni_av_qi = tmp_q;
   2408   }
   2409 
   2410   /* The last few frames of a clip almost always have to few or too many
   2411    * bits and for the sake of over exact rate control we dont want to make
   2412    * radical adjustments to the allowed quantizer range just to use up a
   2413    * few surplus bits or get beneath the target rate.
   2414    */
   2415   else if ((cpi->common.current_video_frame <
   2416             (((unsigned int)cpi->twopass.total_stats.count * 255) >> 8)) &&
   2417            ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
   2418             (unsigned int)cpi->twopass.total_stats.count)) {
   2419     if (frames_left < 1) frames_left = 1;
   2420 
   2421     tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
   2422                            (int)(cpi->twopass.bits_left / frames_left),
   2423                            overhead_bits);
   2424 
   2425     /* Move active_worst_quality but in a damped way */
   2426     if (tmp_q > cpi->active_worst_quality) {
   2427       cpi->active_worst_quality++;
   2428     } else if (tmp_q < cpi->active_worst_quality) {
   2429       cpi->active_worst_quality--;
   2430     }
   2431 
   2432     cpi->active_worst_quality =
   2433         ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
   2434   }
   2435 
   2436   cpi->twopass.frames_to_key--;
   2437 
   2438   /* Update the total stats remaining sturcture */
   2439   subtract_stats(&cpi->twopass.total_left_stats, &this_frame);
   2440 }
   2441 
   2442 static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame,
   2443                              FIRSTPASS_STATS *this_frame,
   2444                              FIRSTPASS_STATS *next_frame) {
   2445   int is_viable_kf = 0;
   2446 
   2447   /* Does the frame satisfy the primary criteria of a key frame
   2448    *      If so, then examine how well it predicts subsequent frames
   2449    */
   2450   if ((this_frame->pcnt_second_ref < 0.10) &&
   2451       (next_frame->pcnt_second_ref < 0.10) &&
   2452       ((this_frame->pcnt_inter < 0.05) ||
   2453        (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
   2454         ((this_frame->intra_error /
   2455           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
   2456         ((fabs(last_frame->coded_error - this_frame->coded_error) /
   2457               DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
   2458           .40) ||
   2459          (fabs(last_frame->intra_error - this_frame->intra_error) /
   2460               DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
   2461           .40) ||
   2462          ((next_frame->intra_error /
   2463            DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
   2464     int i;
   2465     FIRSTPASS_STATS *start_pos;
   2466 
   2467     FIRSTPASS_STATS local_next_frame;
   2468 
   2469     double boost_score = 0.0;
   2470     double old_boost_score = 0.0;
   2471     double decay_accumulator = 1.0;
   2472     double next_iiratio;
   2473 
   2474     memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
   2475 
   2476     /* Note the starting file position so we can reset to it */
   2477     start_pos = cpi->twopass.stats_in;
   2478 
   2479     /* Examine how well the key frame predicts subsequent frames */
   2480     for (i = 0; i < 16; ++i) {
   2481       next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
   2482                       DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
   2483 
   2484       if (next_iiratio > RMAX) next_iiratio = RMAX;
   2485 
   2486       /* Cumulative effect of decay in prediction quality */
   2487       if (local_next_frame.pcnt_inter > 0.85) {
   2488         decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
   2489       } else {
   2490         decay_accumulator =
   2491             decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
   2492       }
   2493 
   2494       /* Keep a running total */
   2495       boost_score += (decay_accumulator * next_iiratio);
   2496 
   2497       /* Test various breakout clauses */
   2498       if ((local_next_frame.pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
   2499           (((local_next_frame.pcnt_inter - local_next_frame.pcnt_neutral) <
   2500             0.20) &&
   2501            (next_iiratio < 3.0)) ||
   2502           ((boost_score - old_boost_score) < 0.5) ||
   2503           (local_next_frame.intra_error < 200)) {
   2504         break;
   2505       }
   2506 
   2507       old_boost_score = boost_score;
   2508 
   2509       /* Get the next frame details */
   2510       if (EOF == input_stats(cpi, &local_next_frame)) break;
   2511     }
   2512 
   2513     /* If there is tolerable prediction for at least the next 3 frames
   2514      * then break out else discard this pottential key frame and move on
   2515      */
   2516     if (boost_score > 5.0 && (i > 3)) {
   2517       is_viable_kf = 1;
   2518     } else {
   2519       /* Reset the file position */
   2520       reset_fpf_position(cpi, start_pos);
   2521 
   2522       is_viable_kf = 0;
   2523     }
   2524   }
   2525 
   2526   return is_viable_kf;
   2527 }
   2528 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
   2529   int i, j;
   2530   FIRSTPASS_STATS last_frame;
   2531   FIRSTPASS_STATS first_frame;
   2532   FIRSTPASS_STATS next_frame;
   2533   FIRSTPASS_STATS *start_position;
   2534 
   2535   double decay_accumulator = 1.0;
   2536   double boost_score = 0;
   2537   double old_boost_score = 0.0;
   2538   double loop_decay_rate;
   2539 
   2540   double kf_mod_err = 0.0;
   2541   double kf_group_err = 0.0;
   2542   double kf_group_intra_err = 0.0;
   2543   double kf_group_coded_err = 0.0;
   2544   double recent_loop_decay[8] = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
   2545 
   2546   memset(&next_frame, 0, sizeof(next_frame));
   2547 
   2548   vpx_clear_system_state();
   2549   start_position = cpi->twopass.stats_in;
   2550 
   2551   cpi->common.frame_type = KEY_FRAME;
   2552 
   2553   /* is this a forced key frame by interval */
   2554   cpi->this_key_frame_forced = cpi->next_key_frame_forced;
   2555 
   2556   /* Clear the alt ref active flag as this can never be active on a key
   2557    * frame
   2558    */
   2559   cpi->source_alt_ref_active = 0;
   2560 
   2561   /* Kf is always a gf so clear frames till next gf counter */
   2562   cpi->frames_till_gf_update_due = 0;
   2563 
   2564   cpi->twopass.frames_to_key = 1;
   2565 
   2566   /* Take a copy of the initial frame details */
   2567   memcpy(&first_frame, this_frame, sizeof(*this_frame));
   2568 
   2569   cpi->twopass.kf_group_bits = 0;
   2570   cpi->twopass.kf_group_error_left = 0;
   2571 
   2572   kf_mod_err = calculate_modified_err(cpi, this_frame);
   2573 
   2574   /* find the next keyframe */
   2575   i = 0;
   2576   while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) {
   2577     /* Accumulate kf group error */
   2578     kf_group_err += calculate_modified_err(cpi, this_frame);
   2579 
   2580     /* These figures keep intra and coded error counts for all frames
   2581      * including key frames in the group. The effect of the key frame
   2582      * itself can be subtracted out using the first_frame data
   2583      * collected above
   2584      */
   2585     kf_group_intra_err += this_frame->intra_error;
   2586     kf_group_coded_err += this_frame->coded_error;
   2587 
   2588     /* Load the next frame's stats. */
   2589     memcpy(&last_frame, this_frame, sizeof(*this_frame));
   2590     input_stats(cpi, this_frame);
   2591 
   2592     /* Provided that we are not at the end of the file... */
   2593     if (cpi->oxcf.auto_key &&
   2594         lookup_next_frame_stats(cpi, &next_frame) != EOF) {
   2595       /* Normal scene cut check */
   2596       if ((i >= MIN_GF_INTERVAL) &&
   2597           test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) {
   2598         break;
   2599       }
   2600 
   2601       /* How fast is prediction quality decaying */
   2602       loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
   2603 
   2604       /* We want to know something about the recent past... rather than
   2605        * as used elsewhere where we are concened with decay in prediction
   2606        * quality since the last GF or KF.
   2607        */
   2608       recent_loop_decay[i % 8] = loop_decay_rate;
   2609       decay_accumulator = 1.0;
   2610       for (j = 0; j < 8; ++j) {
   2611         decay_accumulator = decay_accumulator * recent_loop_decay[j];
   2612       }
   2613 
   2614       /* Special check for transition or high motion followed by a
   2615        * static scene.
   2616        */
   2617       if (detect_transition_to_still(cpi, i,
   2618                                      ((int)(cpi->key_frame_frequency) - (int)i),
   2619                                      loop_decay_rate, decay_accumulator)) {
   2620         break;
   2621       }
   2622 
   2623       /* Step on to the next frame */
   2624       cpi->twopass.frames_to_key++;
   2625 
   2626       /* If we don't have a real key frame within the next two
   2627        * forcekeyframeevery intervals then break out of the loop.
   2628        */
   2629       if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency) {
   2630         break;
   2631       }
   2632     } else {
   2633       cpi->twopass.frames_to_key++;
   2634     }
   2635 
   2636     i++;
   2637   }
   2638 
   2639   /* If there is a max kf interval set by the user we must obey it.
   2640    * We already breakout of the loop above at 2x max.
   2641    * This code centers the extra kf if the actual natural
   2642    * interval is between 1x and 2x
   2643    */
   2644   if (cpi->oxcf.auto_key &&
   2645       cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) {
   2646     FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
   2647     FIRSTPASS_STATS tmp_frame;
   2648 
   2649     cpi->twopass.frames_to_key /= 2;
   2650 
   2651     /* Copy first frame details */
   2652     memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
   2653 
   2654     /* Reset to the start of the group */
   2655     reset_fpf_position(cpi, start_position);
   2656 
   2657     kf_group_err = 0;
   2658     kf_group_intra_err = 0;
   2659     kf_group_coded_err = 0;
   2660 
   2661     /* Rescan to get the correct error data for the forced kf group */
   2662     for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
   2663       /* Accumulate kf group errors */
   2664       kf_group_err += calculate_modified_err(cpi, &tmp_frame);
   2665       kf_group_intra_err += tmp_frame.intra_error;
   2666       kf_group_coded_err += tmp_frame.coded_error;
   2667 
   2668       /* Load a the next frame's stats */
   2669       input_stats(cpi, &tmp_frame);
   2670     }
   2671 
   2672     /* Reset to the start of the group */
   2673     reset_fpf_position(cpi, current_pos);
   2674 
   2675     cpi->next_key_frame_forced = 1;
   2676   } else {
   2677     cpi->next_key_frame_forced = 0;
   2678   }
   2679 
   2680   /* Special case for the last frame of the file */
   2681   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
   2682     /* Accumulate kf group error */
   2683     kf_group_err += calculate_modified_err(cpi, this_frame);
   2684 
   2685     /* These figures keep intra and coded error counts for all frames
   2686      * including key frames in the group. The effect of the key frame
   2687      * itself can be subtracted out using the first_frame data
   2688      * collected above
   2689      */
   2690     kf_group_intra_err += this_frame->intra_error;
   2691     kf_group_coded_err += this_frame->coded_error;
   2692   }
   2693 
   2694   /* Calculate the number of bits that should be assigned to the kf group. */
   2695   if ((cpi->twopass.bits_left > 0) &&
   2696       (cpi->twopass.modified_error_left > 0.0)) {
   2697     /* Max for a single normal frame (not key frame) */
   2698     int max_bits = frame_max_bits(cpi);
   2699 
   2700     /* Maximum bits for the kf group */
   2701     int64_t max_grp_bits;
   2702 
   2703     /* Default allocation based on bits left and relative
   2704      * complexity of the section
   2705      */
   2706     cpi->twopass.kf_group_bits =
   2707         (int64_t)(cpi->twopass.bits_left *
   2708                   (kf_group_err / cpi->twopass.modified_error_left));
   2709 
   2710     /* Clip based on maximum per frame rate defined by the user. */
   2711     max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
   2712     if (cpi->twopass.kf_group_bits > max_grp_bits) {
   2713       cpi->twopass.kf_group_bits = max_grp_bits;
   2714     }
   2715 
   2716     /* Additional special case for CBR if buffer is getting full. */
   2717     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
   2718       int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
   2719       int64_t buffer_lvl = cpi->buffer_level;
   2720 
   2721       /* If the buffer is near or above the optimal and this kf group is
   2722        * not being allocated much then increase the allocation a bit.
   2723        */
   2724       if (buffer_lvl >= opt_buffer_lvl) {
   2725         int64_t high_water_mark =
   2726             (opt_buffer_lvl + cpi->oxcf.maximum_buffer_size) >> 1;
   2727 
   2728         int64_t av_group_bits;
   2729 
   2730         /* Av bits per frame * number of frames */
   2731         av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
   2732                         (int64_t)cpi->twopass.frames_to_key;
   2733 
   2734         /* We are at or above the maximum. */
   2735         if (cpi->buffer_level >= high_water_mark) {
   2736           int64_t min_group_bits;
   2737 
   2738           min_group_bits =
   2739               av_group_bits + (int64_t)(buffer_lvl - high_water_mark);
   2740 
   2741           if (cpi->twopass.kf_group_bits < min_group_bits) {
   2742             cpi->twopass.kf_group_bits = min_group_bits;
   2743           }
   2744         }
   2745         /* We are above optimal but below the maximum */
   2746         else if (cpi->twopass.kf_group_bits < av_group_bits) {
   2747           int64_t bits_below_av = av_group_bits - cpi->twopass.kf_group_bits;
   2748 
   2749           cpi->twopass.kf_group_bits += (int64_t)(
   2750               (double)bits_below_av * (double)(buffer_lvl - opt_buffer_lvl) /
   2751               (double)(high_water_mark - opt_buffer_lvl));
   2752         }
   2753       }
   2754     }
   2755   } else {
   2756     cpi->twopass.kf_group_bits = 0;
   2757   }
   2758 
   2759   /* Reset the first pass file position */
   2760   reset_fpf_position(cpi, start_position);
   2761 
   2762   /* determine how big to make this keyframe based on how well the
   2763    * subsequent frames use inter blocks
   2764    */
   2765   decay_accumulator = 1.0;
   2766   boost_score = 0.0;
   2767 
   2768   for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
   2769     double r;
   2770 
   2771     if (EOF == input_stats(cpi, &next_frame)) break;
   2772 
   2773     if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) {
   2774       r = (IIKFACTOR2 * next_frame.intra_error /
   2775            DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
   2776     } else {
   2777       r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
   2778            DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
   2779     }
   2780 
   2781     if (r > RMAX) r = RMAX;
   2782 
   2783     /* How fast is prediction quality decaying */
   2784     loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
   2785 
   2786     decay_accumulator = decay_accumulator * loop_decay_rate;
   2787     decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
   2788 
   2789     boost_score += (decay_accumulator * r);
   2790 
   2791     if ((i > MIN_GF_INTERVAL) && ((boost_score - old_boost_score) < 1.0)) {
   2792       break;
   2793     }
   2794 
   2795     old_boost_score = boost_score;
   2796   }
   2797 
   2798   if (1) {
   2799     FIRSTPASS_STATS sectionstats;
   2800     double Ratio;
   2801 
   2802     zero_stats(&sectionstats);
   2803     reset_fpf_position(cpi, start_position);
   2804 
   2805     for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
   2806       input_stats(cpi, &next_frame);
   2807       accumulate_stats(&sectionstats, &next_frame);
   2808     }
   2809 
   2810     avg_stats(&sectionstats);
   2811 
   2812     cpi->twopass.section_intra_rating =
   2813         (unsigned int)(sectionstats.intra_error /
   2814                        DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
   2815 
   2816     Ratio = sectionstats.intra_error /
   2817             DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
   2818     cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
   2819 
   2820     if (cpi->twopass.section_max_qfactor < 0.80) {
   2821       cpi->twopass.section_max_qfactor = 0.80;
   2822     }
   2823   }
   2824 
   2825   /* When using CBR apply additional buffer fullness related upper limits */
   2826   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
   2827     double max_boost;
   2828 
   2829     if (cpi->drop_frames_allowed) {
   2830       int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark *
   2831                                   (cpi->oxcf.optimal_buffer_level / 100));
   2832 
   2833       if (cpi->buffer_level > df_buffer_level) {
   2834         max_boost =
   2835             ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
   2836             DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   2837       } else {
   2838         max_boost = 0.0;
   2839       }
   2840     } else if (cpi->buffer_level > 0) {
   2841       max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
   2842                   DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   2843     } else {
   2844       max_boost = 0.0;
   2845     }
   2846 
   2847     if (boost_score > max_boost) boost_score = max_boost;
   2848   }
   2849 
   2850   /* Reset the first pass file position */
   2851   reset_fpf_position(cpi, start_position);
   2852 
   2853   /* Work out how many bits to allocate for the key frame itself */
   2854   if (1) {
   2855     int kf_boost = (int)boost_score;
   2856     int allocation_chunks;
   2857     int Counter = cpi->twopass.frames_to_key;
   2858     int alt_kf_bits;
   2859     YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
   2860 /* Min boost based on kf interval */
   2861 #if 0
   2862 
   2863         while ((kf_boost < 48) && (Counter > 0))
   2864         {
   2865             Counter -= 2;
   2866             kf_boost ++;
   2867         }
   2868 
   2869 #endif
   2870 
   2871     if (kf_boost < 48) {
   2872       kf_boost += ((Counter + 1) >> 1);
   2873 
   2874       if (kf_boost > 48) kf_boost = 48;
   2875     }
   2876 
   2877     /* bigger frame sizes need larger kf boosts, smaller frames smaller
   2878      * boosts...
   2879      */
   2880     if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240)) {
   2881       kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
   2882     } else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240)) {
   2883       kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
   2884     }
   2885 
   2886     /* Min KF boost */
   2887     kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */
   2888     if (kf_boost < 250) kf_boost = 250;
   2889 
   2890     /*
   2891      * We do three calculations for kf size.
   2892      * The first is based on the error score for the whole kf group.
   2893      * The second (optionaly) on the key frames own error if this is
   2894      * smaller than the average for the group.
   2895      * The final one insures that the frame receives at least the
   2896      * allocation it would have received based on its own error score vs
   2897      * the error score remaining
   2898      * Special case if the sequence appears almost totaly static
   2899      * as measured by the decay accumulator. In this case we want to
   2900      * spend almost all of the bits on the key frame.
   2901      * cpi->twopass.frames_to_key-1 because key frame itself is taken
   2902      * care of by kf_boost.
   2903      */
   2904     if (decay_accumulator >= 0.99) {
   2905       allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
   2906     } else {
   2907       allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
   2908     }
   2909 
   2910     /* Normalize Altboost and allocations chunck down to prevent overflow */
   2911     while (kf_boost > 1000) {
   2912       kf_boost /= 2;
   2913       allocation_chunks /= 2;
   2914     }
   2915 
   2916     cpi->twopass.kf_group_bits =
   2917         (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
   2918 
   2919     /* Calculate the number of bits to be spent on the key frame */
   2920     cpi->twopass.kf_bits =
   2921         (int)((double)kf_boost *
   2922               ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
   2923 
   2924     /* Apply an additional limit for CBR */
   2925     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
   2926       if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2)) {
   2927         cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2);
   2928       }
   2929     }
   2930 
   2931     /* If the key frame is actually easier than the average for the
   2932      * kf group (which does sometimes happen... eg a blank intro frame)
   2933      * Then use an alternate calculation based on the kf error score
   2934      * which should give a smaller key frame.
   2935      */
   2936     if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) {
   2937       double alt_kf_grp_bits =
   2938           ((double)cpi->twopass.bits_left *
   2939            (kf_mod_err * (double)cpi->twopass.frames_to_key) /
   2940            DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
   2941 
   2942       alt_kf_bits = (int)((double)kf_boost *
   2943                           (alt_kf_grp_bits / (double)allocation_chunks));
   2944 
   2945       if (cpi->twopass.kf_bits > alt_kf_bits) {
   2946         cpi->twopass.kf_bits = alt_kf_bits;
   2947       }
   2948     }
   2949     /* Else if it is much harder than other frames in the group make sure
   2950      * it at least receives an allocation in keeping with its relative
   2951      * error score
   2952      */
   2953     else {
   2954       alt_kf_bits = (int)((double)cpi->twopass.bits_left *
   2955                           (kf_mod_err / DOUBLE_DIVIDE_CHECK(
   2956                                             cpi->twopass.modified_error_left)));
   2957 
   2958       if (alt_kf_bits > cpi->twopass.kf_bits) {
   2959         cpi->twopass.kf_bits = alt_kf_bits;
   2960       }
   2961     }
   2962 
   2963     cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
   2964     /* Add in the minimum frame allowance */
   2965     cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
   2966 
   2967     /* Peer frame bit target for this frame */
   2968     cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
   2969 
   2970     /* Convert to a per second bitrate */
   2971     cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * cpi->output_framerate);
   2972   }
   2973 
   2974   /* Note the total error score of the kf group minus the key frame itself */
   2975   cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
   2976 
   2977   /* Adjust the count of total modified error left. The count of bits left
   2978    * is adjusted elsewhere based on real coded frame sizes
   2979    */
   2980   cpi->twopass.modified_error_left -= kf_group_err;
   2981 
   2982   if (cpi->oxcf.allow_spatial_resampling) {
   2983     int resample_trigger = 0;
   2984     int last_kf_resampled = 0;
   2985     int kf_q;
   2986     int scale_val = 0;
   2987     int hr, hs, vr, vs;
   2988     int new_width = cpi->oxcf.Width;
   2989     int new_height = cpi->oxcf.Height;
   2990 
   2991     int projected_buffer_level;
   2992     int tmp_q;
   2993 
   2994     double projected_bits_perframe;
   2995     double group_iiratio = (kf_group_intra_err - first_frame.intra_error) /
   2996                            (kf_group_coded_err - first_frame.coded_error);
   2997     double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
   2998     double bits_per_frame;
   2999     double av_bits_per_frame;
   3000     double effective_size_ratio;
   3001 
   3002     if ((cpi->common.Width != cpi->oxcf.Width) ||
   3003         (cpi->common.Height != cpi->oxcf.Height)) {
   3004       last_kf_resampled = 1;
   3005     }
   3006 
   3007     /* Set back to unscaled by defaults */
   3008     cpi->common.horiz_scale = NORMAL;
   3009     cpi->common.vert_scale = NORMAL;
   3010 
   3011     /* Calculate Average bits per frame. */
   3012     av_bits_per_frame = cpi->oxcf.target_bandwidth /
   3013                         DOUBLE_DIVIDE_CHECK((double)cpi->framerate);
   3014 
   3015     /* CBR... Use the clip average as the target for deciding resample */
   3016     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
   3017       bits_per_frame = av_bits_per_frame;
   3018     }
   3019 
   3020     /* In VBR we want to avoid downsampling in easy section unless we
   3021      * are under extreme pressure So use the larger of target bitrate
   3022      * for this section or average bitrate for sequence
   3023      */
   3024     else {
   3025       /* This accounts for how hard the section is... */
   3026       bits_per_frame =
   3027           (double)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key);
   3028 
   3029       /* Dont turn to resampling in easy sections just because they
   3030        * have been assigned a small number of bits
   3031        */
   3032       if (bits_per_frame < av_bits_per_frame) {
   3033         bits_per_frame = av_bits_per_frame;
   3034       }
   3035     }
   3036 
   3037     /* bits_per_frame should comply with our minimum */
   3038     if (bits_per_frame < (cpi->oxcf.target_bandwidth *
   3039                           cpi->oxcf.two_pass_vbrmin_section / 100)) {
   3040       bits_per_frame = (cpi->oxcf.target_bandwidth *
   3041                         cpi->oxcf.two_pass_vbrmin_section / 100);
   3042     }
   3043 
   3044     /* Work out if spatial resampling is necessary */
   3045     kf_q = estimate_kf_group_q(cpi, err_per_frame, (int)bits_per_frame,
   3046                                group_iiratio);
   3047 
   3048     /* If we project a required Q higher than the maximum allowed Q then
   3049      * make a guess at the actual size of frames in this section
   3050      */
   3051     projected_bits_perframe = bits_per_frame;
   3052     tmp_q = kf_q;
   3053 
   3054     while (tmp_q > cpi->worst_quality) {
   3055       projected_bits_perframe *= 1.04;
   3056       tmp_q--;
   3057     }
   3058 
   3059     /* Guess at buffer level at the end of the section */
   3060     projected_buffer_level =
   3061         (int)(cpi->buffer_level -
   3062               (int)((projected_bits_perframe - av_bits_per_frame) *
   3063                     cpi->twopass.frames_to_key));
   3064 
   3065     if (0) {
   3066       FILE *f = fopen("Subsamle.stt", "a");
   3067       fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n",
   3068               cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale,
   3069               cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key,
   3070               (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key),
   3071               new_height, new_width);
   3072       fclose(f);
   3073     }
   3074 
   3075     /* The trigger for spatial resampling depends on the various
   3076      * parameters such as whether we are streaming (CBR) or VBR.
   3077      */
   3078     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
   3079       /* Trigger resample if we are projected to fall below down
   3080        * sample level or resampled last time and are projected to
   3081        * remain below the up sample level
   3082        */
   3083       if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark *
   3084                                      cpi->oxcf.optimal_buffer_level / 100)) ||
   3085           (last_kf_resampled &&
   3086            (projected_buffer_level < (cpi->oxcf.resample_up_water_mark *
   3087                                       cpi->oxcf.optimal_buffer_level / 100)))) {
   3088         resample_trigger = 1;
   3089       } else {
   3090         resample_trigger = 0;
   3091       }
   3092     } else {
   3093       int64_t clip_bits = (int64_t)(
   3094           cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth /
   3095           DOUBLE_DIVIDE_CHECK((double)cpi->framerate));
   3096       int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
   3097 
   3098       /* If triggered last time the threshold for triggering again is
   3099        * reduced:
   3100        *
   3101        * Projected Q higher than allowed and Overspend > 5% of total
   3102        * bits
   3103        */
   3104       if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||
   3105           ((kf_q > cpi->worst_quality) && (over_spend > clip_bits / 20))) {
   3106         resample_trigger = 1;
   3107       } else {
   3108         resample_trigger = 0;
   3109       }
   3110     }
   3111 
   3112     if (resample_trigger) {
   3113       while ((kf_q >= cpi->worst_quality) && (scale_val < 6)) {
   3114         scale_val++;
   3115 
   3116         cpi->common.vert_scale = vscale_lookup[scale_val];
   3117         cpi->common.horiz_scale = hscale_lookup[scale_val];
   3118 
   3119         Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
   3120         Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
   3121 
   3122         new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
   3123         new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
   3124 
   3125         /* Reducing the area to 1/4 does not reduce the complexity
   3126          * (err_per_frame) to 1/4... effective_sizeratio attempts
   3127          * to provide a crude correction for this
   3128          */
   3129         effective_size_ratio = (double)(new_width * new_height) /
   3130                                (double)(cpi->oxcf.Width * cpi->oxcf.Height);
   3131         effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
   3132 
   3133         /* Now try again and see what Q we get with the smaller
   3134          * image size
   3135          */
   3136         kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio,
   3137                                    (int)bits_per_frame, group_iiratio);
   3138 
   3139         if (0) {
   3140           FILE *f = fopen("Subsamle.stt", "a");
   3141           fprintf(
   3142               f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q,
   3143               cpi->common.horiz_scale, cpi->common.vert_scale,
   3144               kf_group_err / cpi->twopass.frames_to_key,
   3145               (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key),
   3146               new_height, new_width);
   3147           fclose(f);
   3148         }
   3149       }
   3150     }
   3151 
   3152     if ((cpi->common.Width != new_width) ||
   3153         (cpi->common.Height != new_height)) {
   3154       cpi->common.Width = new_width;
   3155       cpi->common.Height = new_height;
   3156       vp8_alloc_compressor_data(cpi);
   3157     }
   3158   }
   3159 }
   3160