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