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 
     12 #include "math.h"
     13 #include "limits.h"
     14 #include "block.h"
     15 #include "onyx_int.h"
     16 #include "variance.h"
     17 #include "encodeintra.h"
     18 #include "setupintrarecon.h"
     19 #include "mcomp.h"
     20 #include "vpx_scale/vpxscale.h"
     21 #include "encodemb.h"
     22 #include "extend.h"
     23 #include "systemdependent.h"
     24 #include "vpx_scale/yv12extend.h"
     25 #include "vpx_mem/vpx_mem.h"
     26 #include "swapyv12buffer.h"
     27 #include <stdio.h>
     28 #include "rdopt.h"
     29 #include "quant_common.h"
     30 #include "encodemv.h"
     31 
     32 //#define OUTPUT_FPF 1
     33 //#define FIRSTPASS_MM 1
     34 
     35 #if CONFIG_RUNTIME_CPU_DETECT
     36 #define IF_RTCD(x) (x)
     37 #else
     38 #define IF_RTCD(x) NULL
     39 #endif
     40 
     41 extern void vp8_build_block_offsets(MACROBLOCK *x);
     42 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
     43 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
     44 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv);
     45 extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
     46 
     47 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10))
     48 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10))
     49 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
     50 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
     51 
     52 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
     53 
     54 #define IIFACTOR   1.4
     55 #define IIKFACTOR1 1.40
     56 #define IIKFACTOR2 1.5
     57 #define RMAX    14.0
     58 #define GF_RMAX 48.0        // 128.0
     59 
     60 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
     61 
     62 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
     63 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
     64 
     65 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
     66 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
     67 
     68 
     69 void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
     70 int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps);
     71 
     72 int vp8_encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred)
     73 {
     74 
     75     int i;
     76     int intra_pred_var = 0;
     77     (void) cpi;
     78 
     79     if (use_dc_pred)
     80     {
     81         x->e_mbd.mode_info_context->mbmi.mode = DC_PRED;
     82         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
     83         x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
     84 
     85         vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
     86     }
     87     else
     88     {
     89         for (i = 0; i < 16; i++)
     90         {
     91             BLOCKD *b = &x->e_mbd.block[i];
     92             BLOCK  *be = &x->block[i];
     93 
     94             vp8_encode_intra4x4block(IF_RTCD(&cpi->rtcd), x, be, b, B_DC_PRED);
     95         }
     96     }
     97 
     98     intra_pred_var = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff);
     99 
    100     return intra_pred_var;
    101 }
    102 
    103 // Resets the first pass file to the given position using a relative seek from the current position
    104 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
    105 {
    106     cpi->stats_in = Position;
    107 }
    108 
    109 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
    110 {
    111     /*FIRSTPASS_STATS * start_pos;
    112     int ret_val;
    113 
    114     start_pos = cpi->stats_in;
    115     ret_val = vp8_input_stats(cpi, next_frame);
    116     reset_fpf_position(cpi, start_pos);
    117 
    118     return ret_val;*/
    119 
    120     if (cpi->stats_in >= cpi->stats_in_end)
    121         return EOF;
    122 
    123     *next_frame = *cpi->stats_in;
    124     return 1;
    125 }
    126 
    127 // Calculate a modified Error used in distributing bits between easier and harder frames
    128 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
    129 {
    130     double av_err = cpi->total_stats.ssim_weighted_pred_err;
    131     double this_err = this_frame->ssim_weighted_pred_err;
    132     double modified_err;
    133 
    134     //double relative_next_iiratio;
    135     //double next_iiratio;
    136     //double sum_iiratio;
    137     //int i;
    138 
    139     //FIRSTPASS_STATS next_frame;
    140     //FIRSTPASS_STATS *start_pos;
    141 
    142     /*start_pos = cpi->stats_in;
    143     sum_iiratio = 0.0;
    144     i = 0;
    145     while ( (i < 1) && vp8_input_stats(cpi,&next_frame) != EOF )
    146     {
    147 
    148         next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
    149         next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio;
    150         sum_iiratio += next_iiratio;
    151         i++;
    152     }
    153     if ( i > 0 )
    154     {
    155         relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->avg_iiratio * (double)i);
    156     }
    157     else
    158     {
    159         relative_next_iiratio = 1.0;
    160     }
    161     reset_fpf_position(cpi, start_pos);*/
    162 
    163     if (this_err > av_err)
    164         modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
    165     else
    166         modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
    167 
    168     /*
    169     relative_next_iiratio = pow(relative_next_iiratio,0.25);
    170     modified_err = modified_err * relative_next_iiratio;
    171     */
    172 
    173     return modified_err;
    174 }
    175 
    176 double vp8_simple_weight(YV12_BUFFER_CONFIG *source)
    177 {
    178     int i, j;
    179 
    180     unsigned char *src = source->y_buffer;
    181     unsigned char value;
    182     double sum_weights = 0.0;
    183     double Weight;
    184 
    185     // Loop throught the Y plane raw examining levels and creating a weight for the image
    186     for (i = 0; i < source->y_height; i++)
    187     {
    188         for (j = 0; j < source->y_width; j++)
    189         {
    190             value = src[j];
    191 
    192             if (value >= 64)
    193                 Weight = 1.0;
    194             else if (value > 32)
    195                 Weight = (value - 32.0f) / 32.0f;
    196             else
    197                 Weight = 0.02;
    198 
    199             sum_weights += Weight;
    200         }
    201 
    202         src += source->y_stride;
    203     }
    204 
    205     sum_weights /= (source->y_height * source->y_width);
    206 
    207     return sum_weights;
    208 }
    209 
    210 // This function returns the current per frame maximum bitrate target
    211 int frame_max_bits(VP8_COMP *cpi)
    212 {
    213     // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
    214     int max_bits;
    215 
    216     // For CBR we need to also consider buffer fullness.
    217     // If we are running below the optimal level then we need to gradually tighten up on max_bits.
    218     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
    219     {
    220         double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
    221 
    222         // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
    223         max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
    224 
    225         // If our buffer is below the optimum level
    226         if (buffer_fullness_ratio < 1.0)
    227         {
    228             // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
    229             int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
    230 
    231             max_bits = (int)(max_bits * buffer_fullness_ratio);
    232 
    233             if (max_bits < min_max_bits)
    234                 max_bits = min_max_bits;       // Lowest value we will set ... which should allow the buffer to refil.
    235         }
    236     }
    237     // VBR
    238     else
    239     {
    240         // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
    241         max_bits = (int)(((double)cpi->bits_left / (cpi->total_stats.count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
    242     }
    243 
    244     // Trap case where we are out of bits
    245     if (max_bits < 0)
    246         max_bits = 0;
    247 
    248     return max_bits;
    249 }
    250 
    251 void vp8_output_stats(struct vpx_codec_pkt_list *pktlist,
    252                       FIRSTPASS_STATS            *stats)
    253 {
    254     struct vpx_codec_cx_pkt pkt;
    255     pkt.kind = VPX_CODEC_STATS_PKT;
    256     pkt.data.twopass_stats.buf = stats;
    257     pkt.data.twopass_stats.sz = sizeof(*stats);
    258     vpx_codec_pkt_list_add(pktlist, &pkt);
    259 
    260 // TEMP debug code
    261 #ifdef OUTPUT_FPF
    262     {
    263         FILE *fpfile;
    264         fpfile = fopen("firstpass.stt", "a");
    265 
    266         fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.0f\n",
    267                 stats->frame,
    268                 stats->intra_error,
    269                 stats->coded_error,
    270                 stats->ssim_weighted_pred_err,
    271                 stats->pcnt_inter,
    272                 stats->pcnt_motion,
    273                 stats->pcnt_second_ref,
    274                 stats->MVr,
    275                 stats->mvr_abs,
    276                 stats->MVc,
    277                 stats->mvc_abs,
    278                 stats->MVrv,
    279                 stats->MVcv,
    280                 stats->mv_in_out_count,
    281                 stats->count);
    282         fclose(fpfile);
    283     }
    284 #endif
    285 }
    286 
    287 int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
    288 {
    289     if (cpi->stats_in >= cpi->stats_in_end)
    290         return EOF;
    291 
    292     *fps = *cpi->stats_in++;
    293     return 1;
    294 }
    295 
    296 void vp8_zero_stats(FIRSTPASS_STATS *section)
    297 {
    298     section->frame      = 0.0;
    299     section->intra_error = 0.0;
    300     section->coded_error = 0.0;
    301     section->ssim_weighted_pred_err = 0.0;
    302     section->pcnt_inter  = 0.0;
    303     section->pcnt_motion  = 0.0;
    304     section->pcnt_second_ref = 0.0;
    305     section->MVr        = 0.0;
    306     section->mvr_abs     = 0.0;
    307     section->MVc        = 0.0;
    308     section->mvc_abs     = 0.0;
    309     section->MVrv       = 0.0;
    310     section->MVcv       = 0.0;
    311     section->mv_in_out_count  = 0.0;
    312     section->count      = 0.0;
    313     section->duration   = 1.0;
    314 }
    315 void vp8_accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
    316 {
    317     section->frame += frame->frame;
    318     section->intra_error += frame->intra_error;
    319     section->coded_error += frame->coded_error;
    320     section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
    321     section->pcnt_inter  += frame->pcnt_inter;
    322     section->pcnt_motion += frame->pcnt_motion;
    323     section->pcnt_second_ref += frame->pcnt_second_ref;
    324     section->MVr        += frame->MVr;
    325     section->mvr_abs     += frame->mvr_abs;
    326     section->MVc        += frame->MVc;
    327     section->mvc_abs     += frame->mvc_abs;
    328     section->MVrv       += frame->MVrv;
    329     section->MVcv       += frame->MVcv;
    330     section->mv_in_out_count  += frame->mv_in_out_count;
    331     section->count      += frame->count;
    332     section->duration   += frame->duration;
    333 }
    334 void vp8_avg_stats(FIRSTPASS_STATS *section)
    335 {
    336     if (section->count < 1.0)
    337         return;
    338 
    339     section->intra_error /= section->count;
    340     section->coded_error /= section->count;
    341     section->ssim_weighted_pred_err /= section->count;
    342     section->pcnt_inter  /= section->count;
    343     section->pcnt_second_ref /= section->count;
    344     section->pcnt_motion /= section->count;
    345     section->MVr        /= section->count;
    346     section->mvr_abs     /= section->count;
    347     section->MVc        /= section->count;
    348     section->mvc_abs     /= section->count;
    349     section->MVrv       /= section->count;
    350     section->MVcv       /= section->count;
    351     section->mv_in_out_count   /= section->count;
    352     section->duration   /= section->count;
    353 }
    354 
    355 int vp8_fpmm_get_pos(VP8_COMP *cpi)
    356 {
    357     return ftell(cpi->fp_motion_mapfile);
    358 }
    359 void vp8_fpmm_reset_pos(VP8_COMP *cpi, int target_pos)
    360 {
    361     int Offset;
    362 
    363     if (cpi->fp_motion_mapfile)
    364     {
    365         Offset = ftell(cpi->fp_motion_mapfile) - target_pos;
    366         fseek(cpi->fp_motion_mapfile, (int) - Offset, SEEK_CUR);
    367     }
    368 }
    369 
    370 void vp8_advance_fpmm(VP8_COMP *cpi, int count)
    371 {
    372 #ifdef FIRSTPASS_MM
    373     fseek(cpi->fp_motion_mapfile, (int)(count * cpi->common.MBs), SEEK_CUR);
    374 #endif
    375 }
    376 
    377 void vp8_input_fpmm(VP8_COMP *cpi, int count)
    378 {
    379 #ifdef FIRSTPASS_MM
    380 
    381     unsigned char *tmp_motion_map;
    382     int i, j;
    383 
    384     if (!cpi->fp_motion_mapfile)
    385         return;                 // Error
    386 
    387     // Create the first pass motion map structure and set to 0
    388     CHECK_MEM_ERROR(tmp_motion_map, vpx_calloc(cpi->common.MBs, 1));
    389 
    390     // Reset the state of the global map
    391     vpx_memset(cpi->fp_motion_map, 0, cpi->common.MBs);
    392 
    393     // Read the specified number of frame maps and set the global map to the highest value seen for each mb.
    394     for (i = 0; i < count; i++)
    395     {
    396         if (fread(tmp_motion_map, 1, cpi->common.MBs, cpi->fp_motion_mapfile) == cpi->common.MBs)
    397         {
    398             for (j = 0; j < cpi->common.MBs; j++)
    399             {
    400                 if (tmp_motion_map[j] > 1)
    401                     cpi->fp_motion_map[j] += 5;   // Intra is flagged
    402                 else
    403                     cpi->fp_motion_map[j] += tmp_motion_map[j];
    404             }
    405         }
    406         else
    407             break;  // Read error
    408 
    409     }
    410 
    411     if (tmp_motion_map != 0)
    412         vpx_free(tmp_motion_map);
    413 
    414 #endif
    415 
    416 }
    417 
    418 void vp8_init_first_pass(VP8_COMP *cpi)
    419 {
    420     vp8_zero_stats(&cpi->total_stats);
    421 
    422 #ifdef FIRSTPASS_MM
    423     cpi->fp_motion_mapfile = fopen("fpmotionmap.stt", "wb");
    424 #endif
    425 
    426 // TEMP debug code
    427 #ifdef OUTPUT_FPF
    428     {
    429         FILE *fpfile;
    430         fpfile = fopen("firstpass.stt", "w");
    431         fclose(fpfile);
    432     }
    433 #endif
    434 
    435 }
    436 
    437 void vp8_end_first_pass(VP8_COMP *cpi)
    438 {
    439     vp8_output_stats(cpi->output_pkt_list, &cpi->total_stats);
    440 
    441 #ifdef FIRSTPASS_MM
    442 
    443     if (cpi->fp_motion_mapfile)
    444         fclose(cpi->fp_motion_mapfile);
    445 
    446 #endif
    447 
    448 }
    449 void vp8_zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset )
    450 {
    451     MACROBLOCKD * const xd = & x->e_mbd;
    452     BLOCK *b = &x->block[0];
    453     BLOCKD *d = &x->e_mbd.block[0];
    454 
    455     unsigned char *src_ptr = (*(b->base_src) + b->src);
    456     int src_stride = b->src_stride;
    457     unsigned char *ref_ptr;
    458     int ref_stride=d->pre_stride;
    459 
    460     // Set up pointers for this macro block recon buffer
    461     xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
    462 
    463     ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre );
    464 
    465     VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_stride, ref_ptr, ref_stride, (unsigned int *)(best_motion_err));
    466 }
    467 
    468 
    469 void vp8_first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, MV *ref_mv, MV *best_mv, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset )
    470 {
    471     MACROBLOCKD *const xd = & x->e_mbd;
    472     BLOCK *b = &x->block[0];
    473     BLOCKD *d = &x->e_mbd.block[0];
    474     int num00;
    475 
    476     MV tmp_mv = {0, 0};
    477 
    478     int tmp_err;
    479     int step_param = 3;                                       //3;          // Dont search over full range for first pass
    480     int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3;
    481     int n;
    482     vp8_variance_fn_ptr_t v_fn_ptr;
    483     int new_mv_mode_penalty = 256;
    484 
    485     v_fn_ptr.vf    = VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16);
    486     v_fn_ptr.sdf   = cpi->fn_ptr.sdf;
    487     v_fn_ptr.sdx4df = cpi->fn_ptr.sdx4df;
    488 
    489     // Set up pointers for this macro block recon buffer
    490     xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
    491 
    492     // Initial step/diamond search centred on best mv
    493     tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost);
    494     if ( tmp_err < INT_MAX-new_mv_mode_penalty )
    495         tmp_err += new_mv_mode_penalty;
    496 
    497     if (tmp_err < *best_motion_err)
    498     {
    499         *best_motion_err = tmp_err;
    500         best_mv->row = tmp_mv.row;
    501         best_mv->col = tmp_mv.col;
    502     }
    503 
    504     // Further step/diamond searches as necessary
    505     n = num00;
    506     num00 = 0;
    507 
    508     while (n < further_steps)
    509     {
    510         n++;
    511 
    512         if (num00)
    513             num00--;
    514         else
    515         {
    516             tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param + n, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost);
    517             if ( tmp_err < INT_MAX-new_mv_mode_penalty )
    518                 tmp_err += new_mv_mode_penalty;
    519 
    520             if (tmp_err < *best_motion_err)
    521             {
    522                 *best_motion_err = tmp_err;
    523                 best_mv->row = tmp_mv.row;
    524                 best_mv->col = tmp_mv.col;
    525             }
    526         }
    527     }
    528 }
    529 
    530 void vp8_first_pass(VP8_COMP *cpi)
    531 {
    532     int mb_row, mb_col;
    533     MACROBLOCK *const x = & cpi->mb;
    534     VP8_COMMON *const cm = & cpi->common;
    535     MACROBLOCKD *const xd = & x->e_mbd;
    536 
    537     int col_blocks = 4 * cm->mb_cols;
    538     int recon_yoffset, recon_uvoffset;
    539     YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
    540     YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
    541     YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
    542     int recon_y_stride = lst_yv12->y_stride;
    543     int recon_uv_stride = lst_yv12->uv_stride;
    544     int intra_error = 0;
    545     int coded_error = 0;
    546 
    547     int sum_mvr = 0, sum_mvc = 0;
    548     int sum_mvr_abs = 0, sum_mvc_abs = 0;
    549     int sum_mvrs = 0, sum_mvcs = 0;
    550     int mvcount = 0;
    551     int intercount = 0;
    552     int second_ref_count = 0;
    553     int intrapenalty = 256;
    554 
    555     int sum_in_vectors = 0;
    556 
    557     MV best_ref_mv = {0, 0};
    558     MV zero_ref_mv = {0, 0};
    559 
    560     unsigned char *fp_motion_map_ptr = cpi->fp_motion_map;
    561 
    562     vp8_clear_system_state();  //__asm emms;
    563 
    564     x->src = * cpi->Source;
    565     xd->pre = *lst_yv12;
    566     xd->dst = *new_yv12;
    567 
    568     x->partition_info = x->pi;
    569 
    570     xd->mode_info_context = cm->mi;
    571 
    572     vp8_build_block_offsets(x);
    573 
    574     vp8_setup_block_dptrs(&x->e_mbd);
    575 
    576     vp8_setup_block_ptrs(x);
    577 
    578     // set up frame new frame for intra coded blocks
    579     vp8_setup_intra_recon(new_yv12);
    580     vp8cx_frame_init_quantizer(cpi);
    581 
    582     // Initialise the MV cost table to the defaults
    583     //if( cm->current_video_frame == 0)
    584     //if ( 0 )
    585     {
    586         int flag[2] = {1, 1};
    587         vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
    588         vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
    589         vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
    590     }
    591 
    592     // for each macroblock row in image
    593     for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
    594     {
    595         MV best_ref_mv = {0, 0};
    596 
    597         // reset above block coeffs
    598         xd->up_available = (mb_row != 0);
    599         recon_yoffset = (mb_row * recon_y_stride * 16);
    600         recon_uvoffset = (mb_row * recon_uv_stride * 8);
    601 
    602         // for each macroblock col in image
    603         for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
    604         {
    605             int this_error;
    606             int gf_motion_error = INT_MAX;
    607             int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
    608 
    609             xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
    610             xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
    611             xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
    612             xd->left_available = (mb_col != 0);
    613 
    614             // do intra 16x16 prediction
    615             this_error = vp8_encode_intra(cpi, x, use_dc_pred);
    616 
    617             // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
    618             // We do not have special cases in first pass for 0,0 and nearest etc so all inter modes carry an overhead cost estimate fot the mv.
    619             // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
    620             // This penalty adds a cost matching that of a 0,0 mv to the intra case.
    621             this_error += intrapenalty;
    622 
    623             // Cumulative intra error total
    624             intra_error += this_error;
    625 
    626             // Indicate default assumption of intra in the motion map
    627             *fp_motion_map_ptr = 2;
    628 
    629             // Set up limit values for motion vectors to prevent them extending outside the UMV borders
    630             x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
    631             x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
    632             x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
    633             x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
    634 
    635             // Other than for the first frame do a motion search
    636             if (cm->current_video_frame > 0)
    637             {
    638                 BLOCK *b = &x->block[0];
    639                 BLOCKD *d = &x->e_mbd.block[0];
    640                 MV tmp_mv = {0, 0};
    641                 int tmp_err;
    642                 int motion_error = INT_MAX;
    643 
    644                 // Simple 0,0 motion with no mv overhead
    645                 vp8_zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset );
    646                 d->bmi.mv.as_mv.row = 0;
    647                 d->bmi.mv.as_mv.col = 0;
    648 
    649                 // Test last reference frame using the previous best mv as the
    650                 // starting point (best reference) for the search
    651                 vp8_first_pass_motion_search(cpi, x, &best_ref_mv,
    652                                         &d->bmi.mv.as_mv, lst_yv12,
    653                                         &motion_error, recon_yoffset);
    654 
    655                 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
    656                 if ((best_ref_mv.col != 0) || (best_ref_mv.row != 0))
    657                 {
    658                    tmp_err = INT_MAX;
    659                    vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
    660                                      lst_yv12, &tmp_err, recon_yoffset);
    661 
    662                    if ( tmp_err < motion_error )
    663                    {
    664                         motion_error = tmp_err;
    665                         d->bmi.mv.as_mv.row = tmp_mv.row;
    666                         d->bmi.mv.as_mv.col = tmp_mv.col;
    667                    }
    668 
    669                 }
    670 
    671                 // Experimental search in a second reference frame ((0,0) based only)
    672                 if (cm->current_video_frame > 1)
    673                 {
    674                     vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
    675 
    676                     if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
    677                     {
    678                         second_ref_count++;
    679                         //motion_error = gf_motion_error;
    680                         //d->bmi.mv.as_mv.row = tmp_mv.row;
    681                         //d->bmi.mv.as_mv.col = tmp_mv.col;
    682                     }
    683                     /*else
    684                     {
    685                         xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
    686                         xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
    687                         xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
    688                     }*/
    689 
    690 
    691                     // Reset to last frame as reference buffer
    692                     xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
    693                     xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
    694                     xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
    695                 }
    696 
    697                 if (motion_error <= this_error)
    698                 {
    699                     d->bmi.mv.as_mv.row <<= 3;
    700                     d->bmi.mv.as_mv.col <<= 3;
    701                     this_error = motion_error;
    702                     vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv.as_mv);
    703                     vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x);
    704                     sum_mvr += d->bmi.mv.as_mv.row;
    705                     sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
    706                     sum_mvc += d->bmi.mv.as_mv.col;
    707                     sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
    708                     sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
    709                     sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
    710                     intercount++;
    711 
    712                     best_ref_mv.row = d->bmi.mv.as_mv.row;
    713                     best_ref_mv.col = d->bmi.mv.as_mv.col;
    714                     //best_ref_mv.row = 0;
    715                     //best_ref_mv.col = 0;
    716 
    717                     // Was the vector non-zero
    718                     if (d->bmi.mv.as_mv.row || d->bmi.mv.as_mv.col)
    719                     {
    720                         mvcount++;
    721 
    722                         *fp_motion_map_ptr = 1;
    723 
    724                         // Does the Row vector point inwards or outwards
    725                         if (mb_row < cm->mb_rows / 2)
    726                         {
    727                             if (d->bmi.mv.as_mv.row > 0)
    728                                 sum_in_vectors--;
    729                             else if (d->bmi.mv.as_mv.row < 0)
    730                                 sum_in_vectors++;
    731                         }
    732                         else if (mb_row > cm->mb_rows / 2)
    733                         {
    734                             if (d->bmi.mv.as_mv.row > 0)
    735                                 sum_in_vectors++;
    736                             else if (d->bmi.mv.as_mv.row < 0)
    737                                 sum_in_vectors--;
    738                         }
    739 
    740                         // Does the Row vector point inwards or outwards
    741                         if (mb_col < cm->mb_cols / 2)
    742                         {
    743                             if (d->bmi.mv.as_mv.col > 0)
    744                                 sum_in_vectors--;
    745                             else if (d->bmi.mv.as_mv.col < 0)
    746                                 sum_in_vectors++;
    747                         }
    748                         else if (mb_col > cm->mb_cols / 2)
    749                         {
    750                             if (d->bmi.mv.as_mv.col > 0)
    751                                 sum_in_vectors++;
    752                             else if (d->bmi.mv.as_mv.col < 0)
    753                                 sum_in_vectors--;
    754                         }
    755                     }
    756                     else
    757                         *fp_motion_map_ptr = 0;    // 0,0 mv was best
    758                 }
    759                 else
    760                 {
    761                     best_ref_mv.row = 0;
    762                     best_ref_mv.col = 0;
    763                 }
    764             }
    765 
    766             coded_error += this_error;
    767 
    768             // adjust to the next column of macroblocks
    769             x->src.y_buffer += 16;
    770             x->src.u_buffer += 8;
    771             x->src.v_buffer += 8;
    772 
    773             recon_yoffset += 16;
    774             recon_uvoffset += 8;
    775 
    776             // Update the motion map
    777             fp_motion_map_ptr++;
    778         }
    779 
    780         // adjust to the next row of mbs
    781         x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
    782         x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
    783         x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
    784 
    785         //extend the recon for intra prediction
    786         vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
    787         vp8_clear_system_state();  //__asm emms;
    788     }
    789 
    790     vp8_clear_system_state();  //__asm emms;
    791     {
    792         double weight = 0.0;
    793 
    794         FIRSTPASS_STATS fps;
    795 
    796         fps.frame      = cm->current_video_frame ;
    797         fps.intra_error = intra_error >> 8;
    798         fps.coded_error = coded_error >> 8;
    799         weight = vp8_simple_weight(cpi->Source);
    800 
    801         if (weight < 0.1)
    802             weight = 0.1;
    803 
    804         fps.ssim_weighted_pred_err = fps.coded_error * weight;
    805 
    806         fps.pcnt_inter  = 0.0;
    807         fps.pcnt_motion = 0.0;
    808         fps.MVr        = 0.0;
    809         fps.mvr_abs     = 0.0;
    810         fps.MVc        = 0.0;
    811         fps.mvc_abs     = 0.0;
    812         fps.MVrv       = 0.0;
    813         fps.MVcv       = 0.0;
    814         fps.mv_in_out_count  = 0.0;
    815         fps.count      = 1.0;
    816 
    817         fps.pcnt_inter   = 1.0 * (double)intercount / cm->MBs;
    818         fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
    819 
    820         if (mvcount > 0)
    821         {
    822             fps.MVr = (double)sum_mvr / (double)mvcount;
    823             fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
    824             fps.MVc = (double)sum_mvc / (double)mvcount;
    825             fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
    826             fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
    827             fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
    828             fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
    829 
    830             fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
    831         }
    832 
    833         // TODO:  handle the case when duration is set to 0, or something less
    834         // than the full time between subsequent cpi->source_time_stamp s  .
    835         fps.duration = cpi->source_end_time_stamp - cpi->source_time_stamp;
    836 
    837         // don't want to do outputstats with a stack variable!
    838         cpi->this_frame_stats = fps;
    839         vp8_output_stats(cpi->output_pkt_list, &cpi->this_frame_stats);
    840         vp8_accumulate_stats(&cpi->total_stats, &fps);
    841 
    842 #ifdef FIRSTPASS_MM
    843         fwrite(cpi->fp_motion_map, 1, cpi->common.MBs, cpi->fp_motion_mapfile);
    844 #endif
    845     }
    846 
    847     // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
    848     if ((cm->current_video_frame > 0) &&
    849         (cpi->this_frame_stats.pcnt_inter > 0.20) &&
    850         ((cpi->this_frame_stats.intra_error / cpi->this_frame_stats.coded_error) > 2.0))
    851     {
    852         vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
    853     }
    854 
    855     // swap frame pointers so last frame refers to the frame we just compressed
    856     vp8_swap_yv12_buffer(lst_yv12, new_yv12);
    857     vp8_yv12_extend_frame_borders(lst_yv12);
    858 
    859     // Special case for the first frame. Copy into the GF buffer as a second reference.
    860     if (cm->current_video_frame == 0)
    861     {
    862         vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
    863     }
    864 
    865 
    866     // use this to see what the first pass reconstruction looks like
    867     if (0)
    868     {
    869         char filename[512];
    870         FILE *recon_file;
    871         sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
    872 
    873         if (cm->current_video_frame == 0)
    874             recon_file = fopen(filename, "wb");
    875         else
    876             recon_file = fopen(filename, "ab");
    877 
    878         fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
    879         fclose(recon_file);
    880     }
    881 
    882     cm->current_video_frame++;
    883 
    884 }
    885 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
    886 
    887 #define BASE_ERRPERMB   150
    888 static int estimate_max_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width)
    889 {
    890     int Q;
    891     int num_mbs = ((Height * Width) / (16 * 16));
    892     int target_norm_bits_per_mb;
    893 
    894     double err_per_mb = section_err / num_mbs;
    895     double correction_factor;
    896     double corr_high;
    897     double speed_correction = 1.0;
    898     double rolling_ratio;
    899 
    900     double pow_highq = 0.90;
    901     double pow_lowq = 0.40;
    902 
    903     if (section_target_bandwitdh <= 0)
    904         return MAXQ;
    905 
    906     target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
    907 
    908     // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
    909     if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->worst_quality))
    910     {
    911         //double adjustment_rate = 0.985 + (0.00005 * cpi->active_worst_quality);
    912         double adjustment_rate = 0.99;
    913 
    914         rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
    915 
    916         //if ( cpi->est_max_qcorrection_factor > rolling_ratio )
    917         if (rolling_ratio < 0.95)
    918             //cpi->est_max_qcorrection_factor *= adjustment_rate;
    919             cpi->est_max_qcorrection_factor -= 0.005;
    920         //else if ( cpi->est_max_qcorrection_factor < rolling_ratio )
    921         else if (rolling_ratio > 1.05)
    922             cpi->est_max_qcorrection_factor += 0.005;
    923 
    924         //cpi->est_max_qcorrection_factor /= adjustment_rate;
    925 
    926         cpi->est_max_qcorrection_factor = (cpi->est_max_qcorrection_factor < 0.1) ? 0.1 : (cpi->est_max_qcorrection_factor > 10.0) ? 10.0 : cpi->est_max_qcorrection_factor;
    927     }
    928 
    929     // Corrections for higher compression speed settings (reduced compression expected)
    930     if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
    931     {
    932         if (cpi->oxcf.cpu_used <= 5)
    933             speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
    934         else
    935             speed_correction = 1.25;
    936     }
    937 
    938     // Correction factor used for Q values >= 20
    939     corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
    940     corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
    941 
    942     // Try and pick a Q that should be high enough to encode the content at the given rate.
    943     for (Q = 0; Q < MAXQ; Q++)
    944     {
    945         int bits_per_mb_at_this_q;
    946 
    947         if (Q < 50)
    948         {
    949             correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
    950             correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
    951         }
    952         else
    953             correction_factor = corr_high;
    954 
    955         bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * cpi->section_max_qfactor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
    956         //bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
    957 
    958         if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
    959             break;
    960     }
    961 
    962     return Q;
    963 }
    964 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width)
    965 {
    966     int Q;
    967     int num_mbs = ((Height * Width) / (16 * 16));
    968     int target_norm_bits_per_mb;
    969 
    970     double err_per_mb = section_err / num_mbs;
    971     double correction_factor;
    972     double corr_high;
    973     double speed_correction = 1.0;
    974     double pow_highq = 0.90;
    975     double pow_lowq = 0.40;
    976 
    977     target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
    978 
    979     // Corrections for higher compression speed settings (reduced compression expected)
    980     if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
    981     {
    982         if (cpi->oxcf.cpu_used <= 5)
    983             speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
    984         else
    985             speed_correction = 1.25;
    986     }
    987 
    988     // Correction factor used for Q values >= 20
    989     corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
    990     corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
    991 
    992     // Try and pick a Q that can encode the content at the given rate.
    993     for (Q = 0; Q < MAXQ; Q++)
    994     {
    995         int bits_per_mb_at_this_q;
    996 
    997         if (Q < 50)
    998         {
    999             correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
   1000             correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
   1001         }
   1002         else
   1003             correction_factor = corr_high;
   1004 
   1005         bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
   1006 
   1007         if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
   1008             break;
   1009     }
   1010 
   1011     return Q;
   1012 }
   1013 
   1014 // Estimate a worst case Q for a KF group
   1015 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width, double group_iiratio)
   1016 {
   1017     int Q;
   1018     int num_mbs = ((Height * Width) / (16 * 16));
   1019     int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
   1020     int bits_per_mb_at_this_q;
   1021 
   1022     double err_per_mb = section_err / num_mbs;
   1023     double err_correction_factor;
   1024     double corr_high;
   1025     double speed_correction = 1.0;
   1026     double current_spend_ratio = 1.0;
   1027 
   1028     double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
   1029     double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
   1030 
   1031     double iiratio_correction_factor = 1.0;
   1032 
   1033     double combined_correction_factor;
   1034 
   1035     // Trap special case where the target is <= 0
   1036     if (target_norm_bits_per_mb <= 0)
   1037         return MAXQ * 2;
   1038 
   1039     // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
   1040     // This is clamped to the range 0.1 to 10.0
   1041     if (cpi->long_rolling_target_bits <= 0)
   1042         current_spend_ratio = 10.0;
   1043     else
   1044     {
   1045         current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
   1046         current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
   1047     }
   1048 
   1049     // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
   1050     // The idea here is to favour subsampling in the hardest sections vs the easyest.
   1051     iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
   1052 
   1053     if (iiratio_correction_factor < 0.5)
   1054         iiratio_correction_factor = 0.5;
   1055 
   1056     // Corrections for higher compression speed settings (reduced compression expected)
   1057     if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
   1058     {
   1059         if (cpi->oxcf.cpu_used <= 5)
   1060             speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
   1061         else
   1062             speed_correction = 1.25;
   1063     }
   1064 
   1065     // Combine the various factors calculated above
   1066     combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
   1067 
   1068     // Correction factor used for Q values >= 20
   1069     corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
   1070     corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
   1071 
   1072     // Try and pick a Q that should be high enough to encode the content at the given rate.
   1073     for (Q = 0; Q < MAXQ; Q++)
   1074     {
   1075         // Q values < 20 treated as a special case
   1076         if (Q < 20)
   1077         {
   1078             err_correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
   1079             err_correction_factor = (err_correction_factor < 0.05) ? 0.05 : (err_correction_factor > 5.0) ? 5.0 : err_correction_factor;
   1080         }
   1081         else
   1082             err_correction_factor = corr_high;
   1083 
   1084         bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * combined_correction_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q]);
   1085 
   1086         if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
   1087             break;
   1088     }
   1089 
   1090     // If we could not hit the target even at Max Q then estimate what Q would have bee required
   1091     while ((bits_per_mb_at_this_q > target_norm_bits_per_mb)  && (Q < (MAXQ * 2)))
   1092     {
   1093 
   1094         bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
   1095         Q++;
   1096     }
   1097 
   1098     if (0)
   1099     {
   1100         FILE *f = fopen("estkf_q.stt", "a");
   1101         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,
   1102                 target_norm_bits_per_mb, err_per_mb, err_correction_factor,
   1103                 current_spend_ratio, group_iiratio, iiratio_correction_factor,
   1104                 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
   1105         fclose(f);
   1106     }
   1107 
   1108     return Q;
   1109 }
   1110 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
   1111 
   1112 void vp8_init_second_pass(VP8_COMP *cpi)
   1113 {
   1114     FIRSTPASS_STATS this_frame;
   1115     FIRSTPASS_STATS *start_pos;
   1116 
   1117     double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
   1118 
   1119     vp8_zero_stats(&cpi->total_stats);
   1120 
   1121     if (!cpi->stats_in_end)
   1122         return;
   1123 
   1124     cpi->total_stats = *cpi->stats_in_end;
   1125 
   1126     cpi->total_error_left = cpi->total_stats.ssim_weighted_pred_err;
   1127     cpi->total_intra_error_left = cpi->total_stats.intra_error;
   1128     cpi->total_coded_error_left = cpi->total_stats.coded_error;
   1129     cpi->start_tot_err_left = cpi->total_error_left;
   1130 
   1131     //cpi->bits_left = (long long)(cpi->total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
   1132     //cpi->bits_left -= (long long)(cpi->total_stats.count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
   1133 
   1134     // each frame can have a different duration, as the frame rate in the source
   1135     // isn't guaranteed to be constant.   The frame rate prior to the first frame
   1136     // encoded in the second pass is a guess.  However the sum duration is not.
   1137     // Its calculated based on the actual durations of all frames from the first
   1138     // pass.
   1139     vp8_new_frame_rate(cpi, 10000000.0 * cpi->total_stats.count / cpi->total_stats.duration);
   1140 
   1141     cpi->output_frame_rate = cpi->oxcf.frame_rate;
   1142     cpi->bits_left = (long long)(cpi->total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
   1143     cpi->bits_left -= (long long)(cpi->total_stats.duration * two_pass_min_rate / 10000000.0);
   1144 
   1145     vp8_avg_stats(&cpi->total_stats);
   1146 
   1147     // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
   1148     {
   1149         double sum_iiratio = 0.0;
   1150         double IIRatio;
   1151 
   1152         start_pos = cpi->stats_in;               // Note starting "file" position
   1153 
   1154         while (vp8_input_stats(cpi, &this_frame) != EOF)
   1155         {
   1156             IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
   1157             IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
   1158             sum_iiratio += IIRatio;
   1159         }
   1160 
   1161         cpi->avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->total_stats.count);
   1162 
   1163         // Reset file position
   1164         reset_fpf_position(cpi, start_pos);
   1165     }
   1166 
   1167     // Scan the first pass file and calculate a modified total error based upon the bias/power function
   1168     // used to allocate bits
   1169     {
   1170         start_pos = cpi->stats_in;               // Note starting "file" position
   1171 
   1172         cpi->modified_total_error_left = 0.0;
   1173 
   1174         while (vp8_input_stats(cpi, &this_frame) != EOF)
   1175         {
   1176             cpi->modified_total_error_left += calculate_modified_err(cpi, &this_frame);
   1177         }
   1178 
   1179         reset_fpf_position(cpi, start_pos);            // Reset file position
   1180 
   1181     }
   1182 
   1183 #ifdef FIRSTPASS_MM
   1184     cpi->fp_motion_mapfile = 0;
   1185     cpi->fp_motion_mapfile = fopen("fpmotionmap.stt", "rb");
   1186 #endif
   1187 
   1188 }
   1189 
   1190 void vp8_end_second_pass(VP8_COMP *cpi)
   1191 {
   1192 #ifdef FIRSTPASS_MM
   1193 
   1194     if (cpi->fp_motion_mapfile)
   1195         fclose(cpi->fp_motion_mapfile);
   1196 
   1197 #endif
   1198 }
   1199 
   1200 // Analyse and define a gf/arf group .
   1201 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
   1202 {
   1203     FIRSTPASS_STATS next_frame;
   1204     FIRSTPASS_STATS *start_pos;
   1205     int i;
   1206     int y_width  = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_width;
   1207     int y_height = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_height;
   1208     int image_size = y_width  * y_height;
   1209     double boost_score = 0.0;
   1210     double old_boost_score = 0.0;
   1211     double gf_group_err = 0.0;
   1212     double gf_first_frame_err = 0.0;
   1213     double mod_frame_err = 0.0;
   1214 
   1215     double mv_accumulator_rabs  = 0.0;
   1216     double mv_accumulator_cabs  = 0.0;
   1217     double this_mv_rabs;
   1218     double this_mv_cabs;
   1219     double mv_ratio_accumulator = 0.0;
   1220     double distance_factor = 0.0;
   1221     double decay_accumulator = 1.0;
   1222 
   1223     double boost_factor = IIFACTOR;
   1224     double loop_decay_rate = 1.00;        // Starting decay rate
   1225 
   1226     double this_frame_mv_in_out = 0.0;
   1227     double mv_in_out_accumulator = 0.0;
   1228     double abs_mv_in_out_accumulator = 0.0;
   1229     double mod_err_per_mb_accumulator = 0.0;
   1230 
   1231     int max_bits = frame_max_bits(cpi);    // Max for a single frame
   1232 
   1233 #ifdef FIRSTPASS_MM
   1234     int fpmm_pos;
   1235 #endif
   1236 
   1237     cpi->gf_group_bits = 0;
   1238     cpi->gf_decay_rate = 0;
   1239 
   1240     vp8_clear_system_state();  //__asm emms;
   1241 
   1242 #ifdef FIRSTPASS_MM
   1243     fpmm_pos = vp8_fpmm_get_pos(cpi);
   1244 #endif
   1245 
   1246     start_pos = cpi->stats_in;
   1247 
   1248     vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
   1249 
   1250     // Preload the stats for the next frame.
   1251     mod_frame_err = calculate_modified_err(cpi, this_frame);
   1252 
   1253     // Note the error of the frame at the start of the group (this will be the GF frame error if we code a normal gf
   1254     gf_first_frame_err = mod_frame_err;
   1255 
   1256     // Special treatment if the current frame is a key frame (which is also a gf).
   1257     // If it is then its error score (and hence bit allocation) need to be subtracted out
   1258     // from the calculation for the GF group
   1259     if (cpi->common.frame_type == KEY_FRAME)
   1260         gf_group_err -= gf_first_frame_err;
   1261 
   1262     // Scan forward to try and work out how many frames the next gf group should contain and
   1263     // what level of boost is appropriate for the GF or ARF that will be coded with the group
   1264     i = 0;
   1265 
   1266     while (((i < cpi->max_gf_interval) || ((cpi->frames_to_key - i) < MIN_GF_INTERVAL)) && (i < cpi->frames_to_key))
   1267     {
   1268         double r;
   1269         double motion_factor;
   1270         double this_frame_mvr_ratio;
   1271         double this_frame_mvc_ratio;
   1272 
   1273         i++;                                                    // Increment the loop counter
   1274 
   1275         // Accumulate error score of frames in this gf group
   1276         mod_frame_err = calculate_modified_err(cpi, this_frame);
   1277 
   1278         gf_group_err += mod_frame_err;
   1279 
   1280         mod_err_per_mb_accumulator += mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
   1281 
   1282         if (EOF == vp8_input_stats(cpi, &next_frame))
   1283             break;
   1284 
   1285         // Accumulate motion stats.
   1286         motion_factor = next_frame.pcnt_motion;
   1287         this_mv_rabs = fabs(next_frame.mvr_abs * motion_factor);
   1288         this_mv_cabs = fabs(next_frame.mvc_abs * motion_factor);
   1289 
   1290         mv_accumulator_rabs += fabs(next_frame.mvr_abs * motion_factor);
   1291         mv_accumulator_cabs += fabs(next_frame.mvc_abs * motion_factor);
   1292 
   1293         //Accumulate Motion In/Out of frame stats
   1294         this_frame_mv_in_out = next_frame.mv_in_out_count * next_frame.pcnt_motion;
   1295         mv_in_out_accumulator += next_frame.mv_in_out_count * next_frame.pcnt_motion;
   1296         abs_mv_in_out_accumulator += fabs(next_frame.mv_in_out_count * next_frame.pcnt_motion);
   1297 
   1298         // If there is a significant amount of motion
   1299         if (motion_factor > 0.05)
   1300         {
   1301             this_frame_mvr_ratio = fabs(next_frame.mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVr));
   1302             this_frame_mvc_ratio = fabs(next_frame.mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVc));
   1303 
   1304             mv_ratio_accumulator += (this_frame_mvr_ratio < next_frame.mvr_abs) ? (this_frame_mvr_ratio * motion_factor) : next_frame.mvr_abs * motion_factor;
   1305             mv_ratio_accumulator += (this_frame_mvc_ratio < next_frame.mvc_abs) ? (this_frame_mvc_ratio * motion_factor) : next_frame.mvc_abs * motion_factor;
   1306         }
   1307         else
   1308         {
   1309             mv_ratio_accumulator += 0.0;
   1310             this_frame_mvr_ratio = 1.0;
   1311             this_frame_mvc_ratio = 1.0;
   1312         }
   1313 
   1314         // Underlying boost factor is based on inter intra error ratio
   1315         r = (boost_factor * (next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error)));
   1316 
   1317         // Increase boost for frames where new data coming into frame (eg zoom out)
   1318         // Slightly reduce boost if there is a net balance of motion out of the frame (zoom in)
   1319         // The range for this_frame_mv_in_out is -1.0 to +1.0
   1320         if (this_frame_mv_in_out > 0.0)
   1321             r += r * (this_frame_mv_in_out * 2.0);
   1322         else
   1323             r += r * (this_frame_mv_in_out / 2.0);  // In extreme case boost is halved
   1324 
   1325         if (r > GF_RMAX)
   1326             r = GF_RMAX;
   1327 
   1328         // Adjust loop decay rate
   1329         //if ( next_frame.pcnt_inter < loop_decay_rate )
   1330         loop_decay_rate = next_frame.pcnt_inter;
   1331 
   1332         // High % motion -> somewhat higher decay rate
   1333         if ((1.0 - (next_frame.pcnt_motion / 10.0)) < loop_decay_rate)
   1334             loop_decay_rate = (1.0 - (next_frame.pcnt_motion / 10.0));
   1335 
   1336         distance_factor = sqrt((this_mv_rabs * this_mv_rabs) + (this_mv_cabs * this_mv_cabs)) / 300.0;
   1337         distance_factor = ((distance_factor > 1.0) ? 0.0 : (1.0 - distance_factor));
   1338 
   1339         if (distance_factor < loop_decay_rate)
   1340             loop_decay_rate = distance_factor;
   1341 
   1342         // Cumulative effect of decay
   1343         decay_accumulator = decay_accumulator * loop_decay_rate;
   1344         decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
   1345         //decay_accumulator = ( loop_decay_rate < decay_accumulator ) ? loop_decay_rate : decay_accumulator;
   1346 
   1347         boost_score += (decay_accumulator * r);
   1348 
   1349         // Break out conditions.
   1350         if (   /* i>4 || */
   1351             (
   1352                 (i > MIN_GF_INTERVAL) &&                            // Dont break out with a very short interval
   1353                 ((cpi->frames_to_key - i) >= MIN_GF_INTERVAL) &&      // Dont break out very close to a key frame
   1354                 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
   1355                 ((mv_ratio_accumulator > 100.0) ||
   1356                  (abs_mv_in_out_accumulator > 3.0) ||
   1357                  (mv_in_out_accumulator < -2.0) ||
   1358                  ((boost_score - old_boost_score) < 2.0)
   1359                 )
   1360             )
   1361         )
   1362         {
   1363             boost_score = old_boost_score;
   1364             break;
   1365         }
   1366 
   1367         vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
   1368 
   1369         old_boost_score = boost_score;
   1370     }
   1371 
   1372     cpi->gf_decay_rate = (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
   1373 
   1374     // When using CBR apply additional buffer related upper limits
   1375     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   1376     {
   1377         double max_boost;
   1378 
   1379         // For cbr apply buffer related limits
   1380         if (cpi->drop_frames_allowed)
   1381         {
   1382             int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
   1383 
   1384             if (cpi->buffer_level > df_buffer_level)
   1385                 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   1386             else
   1387                 max_boost = 0.0;
   1388         }
   1389         else if (cpi->buffer_level > 0)
   1390         {
   1391             max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   1392         }
   1393         else
   1394         {
   1395             max_boost = 0.0;
   1396         }
   1397 
   1398         if (boost_score > max_boost)
   1399             boost_score = max_boost;
   1400     }
   1401 
   1402     cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
   1403 
   1404     // Should we use the alternate refernce frame
   1405     if (cpi->oxcf.play_alternate &&
   1406         cpi->oxcf.lag_in_frames &&
   1407         (i >= MIN_GF_INTERVAL) &&
   1408         (i <= (cpi->frames_to_key - MIN_GF_INTERVAL)) &&          // dont use ARF very near next kf
   1409         (((next_frame.pcnt_inter > 0.75) &&
   1410           ((mv_in_out_accumulator / (double)i > -0.2) || (mv_in_out_accumulator > -2.0)) &&
   1411           //(cpi->gfu_boost>150) &&
   1412           (cpi->gfu_boost > 100) &&
   1413           //(cpi->gfu_boost>AF_THRESH2) &&
   1414           //((cpi->gfu_boost/i)>AF_THRESH) &&
   1415           //(decay_accumulator > 0.5) &&
   1416           (cpi->gf_decay_rate <= (ARF_DECAY_THRESH + (cpi->gfu_boost / 200)))
   1417          )
   1418         )
   1419        )
   1420     {
   1421         int Boost;
   1422         int allocation_chunks;
   1423         int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
   1424         int tmp_q;
   1425         int arf_frame_bits = 0;
   1426         int group_bits;
   1427 
   1428         // Estimate the bits to be allocated to the group as a whole
   1429         if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
   1430             group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
   1431         else
   1432             group_bits = 0;
   1433 
   1434         // Boost for arf frame
   1435         Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
   1436         Boost += (cpi->baseline_gf_interval * 50);
   1437         allocation_chunks = (i * 100) + Boost;
   1438 
   1439         // Normalize Altboost and allocations chunck down to prevent overflow
   1440         while (Boost > 1000)
   1441         {
   1442             Boost /= 2;
   1443             allocation_chunks /= 2;
   1444         }
   1445 
   1446         // Calculate the number of bits to be spent on the arf based on the boost number
   1447         arf_frame_bits = (int)((double)Boost * (group_bits / (double)allocation_chunks));
   1448 
   1449         // Estimate if there are enough bits available to make worthwhile use of an arf.
   1450         tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits, cpi->common.Height, cpi->common.Width);
   1451 
   1452         // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames.
   1453         if (tmp_q < cpi->worst_quality)
   1454         {
   1455             cpi->source_alt_ref_pending = TRUE;
   1456 
   1457             // For alt ref frames the error score for the end frame of the group (the alt ref frame) should not contribute to the group total and hence
   1458             // the number of bit allocated to the group. Rather it forms part of the next group (it is the GF at the start of the next group)
   1459             gf_group_err -= mod_frame_err;
   1460 
   1461             // Set the interval till the next gf or arf. For ARFs this is the number of frames to be coded before the future frame that is coded as an ARF.
   1462             // The future frame itself is part of the next group
   1463             cpi->baseline_gf_interval = i - 1;
   1464 
   1465 #ifdef FIRSTPASS_MM
   1466             // Read through the motion map to load up the entry for the ARF
   1467             {
   1468                 int j;
   1469 
   1470                 // Advance to the region of interest
   1471                 // Current default 2 frames before to 2 frames after the ARF frame itsef
   1472                 vp8_fpmm_reset_pos(cpi, cpi->fpmm_pos);
   1473 
   1474                 for (j = 0; j < cpi->baseline_gf_interval - 2; j++)
   1475                     vp8_advance_fpmm(cpi, 1);
   1476 
   1477                 // Read / create a motion map for the region of interest
   1478                 vp8_input_fpmm(cpi, 5);
   1479             }
   1480 #endif
   1481         }
   1482         else
   1483         {
   1484             cpi->source_alt_ref_pending = FALSE;
   1485             cpi->baseline_gf_interval = i;
   1486         }
   1487     }
   1488     else
   1489     {
   1490         cpi->source_alt_ref_pending = FALSE;
   1491         cpi->baseline_gf_interval = i;
   1492     }
   1493 
   1494     // Conventional GF
   1495     if (!cpi->source_alt_ref_pending)
   1496     {
   1497         // Dont allow conventional gf too near the next kf
   1498         if ((cpi->frames_to_key - cpi->baseline_gf_interval) < MIN_GF_INTERVAL)
   1499         {
   1500             while (cpi->baseline_gf_interval < cpi->frames_to_key)
   1501             {
   1502                 if (EOF == vp8_input_stats(cpi, this_frame))
   1503                     break;
   1504 
   1505                 cpi->baseline_gf_interval++;
   1506 
   1507                 if (cpi->baseline_gf_interval < cpi->frames_to_key)
   1508                     gf_group_err += calculate_modified_err(cpi, this_frame);
   1509             }
   1510         }
   1511     }
   1512 
   1513     // Now decide how many bits should be allocated to the GF group as  a proportion of those remaining in the kf group.
   1514     // The final key frame group in the clip is treated as a special case where cpi->kf_group_bits is tied to cpi->bits_left.
   1515     // This is also important for short clips where there may only be one key frame.
   1516     if (cpi->frames_to_key >= (int)(cpi->total_stats.count - cpi->common.current_video_frame))
   1517     {
   1518         cpi->kf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;
   1519     }
   1520 
   1521     // Calculate the bits to be allocated to the group as a whole
   1522     if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
   1523         cpi->gf_group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
   1524     else
   1525         cpi->gf_group_bits = 0;
   1526 
   1527     cpi->gf_group_bits = (cpi->gf_group_bits < 0) ? 0 : (cpi->gf_group_bits > cpi->kf_group_bits) ? cpi->kf_group_bits : cpi->gf_group_bits;
   1528 
   1529     // Clip cpi->gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section)
   1530     if (cpi->gf_group_bits > max_bits * cpi->baseline_gf_interval)
   1531         cpi->gf_group_bits = max_bits * cpi->baseline_gf_interval;
   1532 
   1533     // Reset the file position
   1534     reset_fpf_position(cpi, start_pos);
   1535 
   1536     // Assign  bits to the arf or gf.
   1537     {
   1538         int Boost;
   1539         int frames_in_section;
   1540         int allocation_chunks;
   1541         int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
   1542 
   1543         // For ARF frames
   1544         if (cpi->source_alt_ref_pending)
   1545         {
   1546             Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
   1547             //Boost += (cpi->baseline_gf_interval * 25);
   1548             Boost += (cpi->baseline_gf_interval * 50);
   1549 
   1550             // Set max and minimum boost and hence minimum allocation
   1551             if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
   1552                 Boost = ((cpi->baseline_gf_interval + 1) * 200);
   1553             else if (Boost < 125)
   1554                 Boost = 125;
   1555 
   1556             frames_in_section = cpi->baseline_gf_interval + 1;
   1557             allocation_chunks = (frames_in_section * 100) + Boost;
   1558         }
   1559         // Else for standard golden frames
   1560         else
   1561         {
   1562             // boost based on inter / intra ratio of subsequent frames
   1563             Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
   1564 
   1565             // Set max and minimum boost and hence minimum allocation
   1566             if (Boost > (cpi->baseline_gf_interval * 150))
   1567                 Boost = (cpi->baseline_gf_interval * 150);
   1568             else if (Boost < 125)
   1569                 Boost = 125;
   1570 
   1571             frames_in_section = cpi->baseline_gf_interval;
   1572             allocation_chunks = (frames_in_section * 100) + (Boost - 100);
   1573         }
   1574 
   1575         // Normalize Altboost and allocations chunck down to prevent overflow
   1576         while (Boost > 1000)
   1577         {
   1578             Boost /= 2;
   1579             allocation_chunks /= 2;
   1580         }
   1581 
   1582         // Calculate the number of bits to be spent on the gf or arf based on the boost number
   1583         cpi->gf_bits = (int)((double)Boost * (cpi->gf_group_bits / (double)allocation_chunks));
   1584 
   1585         // If the frame that is to be boosted is simpler than the average for
   1586         // the gf/arf group then use an alternative calculation
   1587         // based on the error score of the frame itself
   1588         if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
   1589         {
   1590             double  alt_gf_grp_bits;
   1591             int     alt_gf_bits;
   1592 
   1593             alt_gf_grp_bits =
   1594                 (double)cpi->kf_group_bits  *
   1595                 (mod_frame_err * (double)cpi->baseline_gf_interval) /
   1596                 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left);
   1597 
   1598             alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
   1599                                                  (double)allocation_chunks));
   1600 
   1601             if (cpi->gf_bits > alt_gf_bits)
   1602             {
   1603                 cpi->gf_bits = alt_gf_bits;
   1604             }
   1605         }
   1606         // Else if it is harder than other frames in the group make sure it at
   1607         // least receives an allocation in keeping with its relative error
   1608         // score, otherwise it may be worse off than an "un-boosted" frame
   1609         else
   1610         {
   1611             int alt_gf_bits =
   1612                 (int)((double)cpi->kf_group_bits *
   1613                       mod_frame_err /
   1614                       DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left));
   1615 
   1616             if (alt_gf_bits > cpi->gf_bits)
   1617             {
   1618                 cpi->gf_bits = alt_gf_bits;
   1619             }
   1620         }
   1621 
   1622         // Apply an additional limit for CBR
   1623         if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   1624         {
   1625             if (cpi->gf_bits > (cpi->buffer_level >> 1))
   1626                 cpi->gf_bits = cpi->buffer_level >> 1;
   1627         }
   1628 
   1629         // Dont allow a negative value for gf_bits
   1630         if (cpi->gf_bits < 0)
   1631             cpi->gf_bits = 0;
   1632 
   1633         // Adjust KF group bits and error remainin
   1634         cpi->kf_group_error_left -= gf_group_err;
   1635         cpi->kf_group_bits -= cpi->gf_group_bits;
   1636 
   1637         if (cpi->kf_group_bits < 0)
   1638             cpi->kf_group_bits = 0;
   1639 
   1640         // Note the error score left in the remaining frames of the group.
   1641         // For normal GFs we want to remove the error score for the first frame of the group (except in Key frame case where this has already happened)
   1642         if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
   1643             cpi->gf_group_error_left = gf_group_err - gf_first_frame_err;
   1644         else
   1645             cpi->gf_group_error_left = gf_group_err;
   1646 
   1647         cpi->gf_group_bits -= cpi->gf_bits;
   1648 
   1649         if (cpi->gf_group_bits < 0)
   1650             cpi->gf_group_bits = 0;
   1651 
   1652         // Set aside some bits for a mid gf sequence boost
   1653         if ((cpi->gfu_boost > 150) && (cpi->baseline_gf_interval > 5))
   1654         {
   1655             int pct_extra = (cpi->gfu_boost - 100) / 50;
   1656             pct_extra = (pct_extra > 10) ? 10 : pct_extra;
   1657 
   1658             cpi->mid_gf_extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
   1659             cpi->gf_group_bits -= cpi->mid_gf_extra_bits;
   1660         }
   1661         else
   1662             cpi->mid_gf_extra_bits = 0;
   1663 
   1664         cpi->gf_bits += cpi->min_frame_bandwidth;                                              // Add in minimum for a frame
   1665     }
   1666 
   1667     if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))                  // Normal GF and not a KF
   1668     {
   1669         cpi->per_frame_bandwidth = cpi->gf_bits;                                               // Per frame bit target for this frame
   1670     }
   1671 
   1672     // Adjustment to estimate_max_q based on a measure of complexity of the section
   1673     if (cpi->common.frame_type != KEY_FRAME)
   1674     {
   1675         FIRSTPASS_STATS sectionstats;
   1676         double Ratio;
   1677 
   1678         vp8_zero_stats(&sectionstats);
   1679         reset_fpf_position(cpi, start_pos);
   1680 
   1681         for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
   1682         {
   1683             vp8_input_stats(cpi, &next_frame);
   1684             vp8_accumulate_stats(&sectionstats, &next_frame);
   1685         }
   1686 
   1687         vp8_avg_stats(&sectionstats);
   1688 
   1689         if (sectionstats.pcnt_motion < .17)
   1690             cpi->section_is_low_motion = 1;
   1691         else
   1692             cpi->section_is_low_motion = 0;
   1693 
   1694         if (sectionstats.mvc_abs + sectionstats.mvr_abs > 45)
   1695             cpi->section_is_fast_motion = 1;
   1696         else
   1697             cpi->section_is_fast_motion = 0;
   1698 
   1699         cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
   1700 
   1701         Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
   1702         //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
   1703         //{
   1704         cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
   1705 
   1706         if (cpi->section_max_qfactor < 0.80)
   1707             cpi->section_max_qfactor = 0.80;
   1708 
   1709         //}
   1710         //else
   1711         //    cpi->section_max_qfactor = 1.0;
   1712 
   1713         reset_fpf_position(cpi, start_pos);
   1714     }
   1715 
   1716 #ifdef FIRSTPASS_MM
   1717     // Reset the First pass motion map file position
   1718     vp8_fpmm_reset_pos(cpi, fpmm_pos);
   1719 #endif
   1720 }
   1721 
   1722 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
   1723 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
   1724 {
   1725     int    target_frame_size;                                                             // gf_group_error_left
   1726 
   1727     double modified_err;
   1728     double err_fraction;                                                                 // What portion of the remaining GF group error is used by this frame
   1729 
   1730     int max_bits = frame_max_bits(cpi);    // Max for a single frame
   1731 
   1732     // The final few frames have special treatment
   1733     if (cpi->frames_till_gf_update_due >= (int)(cpi->total_stats.count - cpi->common.current_video_frame))
   1734     {
   1735         cpi->gf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;;
   1736     }
   1737 
   1738     // Calculate modified prediction error used in bit allocation
   1739     modified_err = calculate_modified_err(cpi, this_frame);
   1740 
   1741     if (cpi->gf_group_error_left > 0)
   1742         err_fraction = modified_err / cpi->gf_group_error_left;                              // What portion of the remaining GF group error is used by this frame
   1743     else
   1744         err_fraction = 0.0;
   1745 
   1746     target_frame_size = (int)((double)cpi->gf_group_bits * err_fraction);                    // How many of those bits available for allocation should we give it?
   1747 
   1748     // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top end.
   1749     if (target_frame_size < 0)
   1750         target_frame_size = 0;
   1751     else
   1752     {
   1753         if (target_frame_size > max_bits)
   1754             target_frame_size = max_bits;
   1755 
   1756         if (target_frame_size > cpi->gf_group_bits)
   1757             target_frame_size = cpi->gf_group_bits;
   1758     }
   1759 
   1760     cpi->gf_group_error_left -= modified_err;                                               // Adjust error remaining
   1761     cpi->gf_group_bits -= target_frame_size;                                                // Adjust bits remaining
   1762 
   1763     if (cpi->gf_group_bits < 0)
   1764         cpi->gf_group_bits = 0;
   1765 
   1766     target_frame_size += cpi->min_frame_bandwidth;                                          // Add in the minimum number of bits that is set aside for every frame.
   1767 
   1768     // Special case for the frame that lies half way between two gfs
   1769     if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2)
   1770         target_frame_size += cpi->mid_gf_extra_bits;
   1771 
   1772     cpi->per_frame_bandwidth = target_frame_size;                                           // Per frame bit target for this frame
   1773 }
   1774 
   1775 void vp8_second_pass(VP8_COMP *cpi)
   1776 {
   1777     int tmp_q;
   1778     int frames_left = (int)(cpi->total_stats.count - cpi->common.current_video_frame);
   1779 
   1780     FIRSTPASS_STATS this_frame;
   1781     FIRSTPASS_STATS this_frame_copy;
   1782 
   1783     VP8_COMMON *cm = &cpi->common;
   1784 
   1785     double this_frame_error;
   1786     double this_frame_intra_error;
   1787     double this_frame_coded_error;
   1788 
   1789     FIRSTPASS_STATS *start_pos;
   1790 
   1791     if (!cpi->stats_in)
   1792     {
   1793         return ;
   1794     }
   1795 
   1796     vp8_clear_system_state();
   1797 
   1798     if (EOF == vp8_input_stats(cpi, &this_frame))
   1799         return;
   1800 
   1801 #ifdef FIRSTPASS_MM
   1802     vpx_memset(cpi->fp_motion_map, 0, cpi->common.MBs);
   1803     cpi->fpmm_pos = vp8_fpmm_get_pos(cpi);
   1804     vp8_advance_fpmm(cpi, 1);         // Read this frame's first pass motion map
   1805 #endif
   1806 
   1807     this_frame_error = this_frame.ssim_weighted_pred_err;
   1808     this_frame_intra_error = this_frame.intra_error;
   1809     this_frame_coded_error = this_frame.coded_error;
   1810 
   1811     // Store information regarding level of motion etc for use mode decisions.
   1812     cpi->motion_speed = (int)(fabs(this_frame.MVr) + fabs(this_frame.MVc));
   1813     cpi->motion_var = (int)(fabs(this_frame.MVrv) + fabs(this_frame.MVcv));
   1814     cpi->inter_lvl = (int)(this_frame.pcnt_inter * 100);
   1815     cpi->intra_lvl = (int)((1.0 - this_frame.pcnt_inter) * 100);
   1816     cpi->motion_lvl = (int)(this_frame.pcnt_motion * 100);
   1817 
   1818     start_pos = cpi->stats_in;
   1819 
   1820     // keyframe and section processing !
   1821     if (cpi->frames_to_key == 0)
   1822     {
   1823         // Define next KF group and assign bits to it
   1824         vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   1825         vp8_find_next_key_frame(cpi, &this_frame_copy);
   1826 
   1827         // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop
   1828         // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
   1829         // This is temporary code till we decide what should really happen in this case.
   1830         if (cpi->oxcf.error_resilient_mode)
   1831         {
   1832             cpi->gf_group_bits = cpi->kf_group_bits;
   1833             cpi->gf_group_error_left = cpi->kf_group_error_left;
   1834             cpi->baseline_gf_interval = cpi->frames_to_key;
   1835             cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
   1836             cpi->source_alt_ref_pending = FALSE;
   1837         }
   1838 
   1839     }
   1840 
   1841     // Is this a GF / ARF (Note that a KF is always also a GF)
   1842     if (cpi->frames_till_gf_update_due == 0)
   1843     {
   1844         // Define next gf group and assign bits to it
   1845         vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   1846         define_gf_group(cpi, &this_frame_copy);
   1847 
   1848         // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
   1849         // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
   1850         // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
   1851         if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
   1852         {
   1853             // Assign a standard frames worth of bits from those allocated to the GF group
   1854             vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   1855             assign_std_frame_bits(cpi, &this_frame_copy);
   1856 
   1857             // If appropriate (we are switching into ARF active but it was not previously active) apply a boost for the gf at the start of the group.
   1858             //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
   1859             if (FALSE)
   1860             {
   1861                 int extra_bits;
   1862                 int pct_extra = (cpi->gfu_boost - 100) / 50;
   1863 
   1864                 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
   1865 
   1866                 extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
   1867                 cpi->gf_group_bits -= extra_bits;
   1868                 cpi->per_frame_bandwidth += extra_bits;
   1869             }
   1870         }
   1871     }
   1872 
   1873     // Otherwise this is an ordinary frame
   1874     else
   1875     {
   1876         // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop
   1877         // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
   1878         // This is temporary code till we decide what should really happen in this case.
   1879         if (cpi->oxcf.error_resilient_mode)
   1880         {
   1881             cpi->frames_till_gf_update_due = cpi->frames_to_key;
   1882 
   1883             if (cpi->common.frame_type != KEY_FRAME)
   1884             {
   1885                 // Assign bits from those allocated to the GF group
   1886                 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   1887                 assign_std_frame_bits(cpi, &this_frame_copy);
   1888             }
   1889         }
   1890         else
   1891         {
   1892             // Assign bits from those allocated to the GF group
   1893             vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
   1894             assign_std_frame_bits(cpi, &this_frame_copy);
   1895         }
   1896     }
   1897 
   1898     // Keep a globally available copy of this and the next frame's iiratio.
   1899     cpi->this_iiratio = this_frame_intra_error /
   1900                         DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
   1901     {
   1902         FIRSTPASS_STATS next_frame;
   1903         if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
   1904         {
   1905             cpi->next_iiratio = next_frame.intra_error /
   1906                                 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
   1907         }
   1908     }
   1909 
   1910     // Set nominal per second bandwidth for this frame
   1911     cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
   1912     if (cpi->target_bandwidth < 0)
   1913         cpi->target_bandwidth = 0;
   1914 
   1915     if (cpi->common.current_video_frame == 0)
   1916     {
   1917         // guess at 2nd pass q
   1918         cpi->est_max_qcorrection_factor = 1.0;
   1919         tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left), cpi->common.Height, cpi->common.Width);
   1920 
   1921         if (tmp_q < cpi->worst_quality)
   1922         {
   1923             cpi->active_worst_quality         = tmp_q;
   1924             cpi->ni_av_qi                     = tmp_q;
   1925         }
   1926         else
   1927         {
   1928             cpi->active_worst_quality         = cpi->worst_quality;
   1929             cpi->ni_av_qi                     = cpi->worst_quality;
   1930         }
   1931     }
   1932     else
   1933     {
   1934         if (frames_left < 1)
   1935             frames_left = 1;
   1936 
   1937         tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left), cpi->common.Height, cpi->common.Width);
   1938 
   1939         // Move active_worst_quality but in a damped way
   1940         if (tmp_q > cpi->active_worst_quality)
   1941             cpi->active_worst_quality ++;
   1942         else if (tmp_q < cpi->active_worst_quality)
   1943             cpi->active_worst_quality --;
   1944 
   1945         cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
   1946 
   1947         // Clamp to user set limits
   1948         if (cpi->active_worst_quality > cpi->worst_quality)
   1949             cpi->active_worst_quality = cpi->worst_quality;
   1950         else if (cpi->active_worst_quality < cpi->best_quality)
   1951             cpi->active_worst_quality = cpi->best_quality;
   1952 
   1953     }
   1954 
   1955     cpi->frames_to_key --;
   1956     cpi->total_error_left      -= this_frame_error;
   1957     cpi->total_intra_error_left -= this_frame_intra_error;
   1958     cpi->total_coded_error_left -= this_frame_coded_error;
   1959 }
   1960 
   1961 
   1962 static BOOL test_candidate_kf(VP8_COMP *cpi,  FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
   1963 {
   1964     BOOL is_viable_kf = FALSE;
   1965 
   1966     // Does the frame satisfy the primary criteria of a key frame
   1967     //      If so, then examine how well it predicts subsequent frames
   1968     if ((this_frame->pcnt_second_ref < 0.10) &&
   1969         (next_frame->pcnt_second_ref < 0.10) &&
   1970         ((this_frame->pcnt_inter < 0.05) ||
   1971          (
   1972              (this_frame->pcnt_inter < .25) &&
   1973              ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
   1974              ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
   1975               (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
   1976               ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
   1977              )
   1978          )
   1979         )
   1980        )
   1981     {
   1982         int i;
   1983         FIRSTPASS_STATS *start_pos;
   1984 
   1985         FIRSTPASS_STATS local_next_frame;
   1986 
   1987         double boost_score = 0.0;
   1988         double old_boost_score = 0.0;
   1989         double decay_accumulator = 1.0;
   1990         double next_iiratio;
   1991 
   1992         vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
   1993 
   1994         // Note the starting file position so we can reset to it
   1995         start_pos = cpi->stats_in;
   1996 
   1997         // Examine how well the key frame predicts subsequent frames
   1998         for (i = 0 ; i < 16; i++)
   1999         {
   2000             next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
   2001 
   2002             if (next_iiratio > RMAX)
   2003                 next_iiratio = RMAX;
   2004 
   2005             // Cumulative effect of decay in prediction quality
   2006             if (local_next_frame.pcnt_inter > 0.85)
   2007                 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
   2008             else
   2009                 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
   2010 
   2011             //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
   2012 
   2013             // Keep a running total
   2014             boost_score += (decay_accumulator * next_iiratio);
   2015 
   2016             // Test various breakout clauses
   2017             if ((local_next_frame.pcnt_inter < 0.05) ||
   2018                 (next_iiratio < 1.5) ||
   2019                 ((local_next_frame.pcnt_inter < 0.20) && (next_iiratio < 3.0)) ||
   2020                 ((boost_score - old_boost_score) < 0.5) ||
   2021                 (local_next_frame.intra_error < 200)
   2022                )
   2023             {
   2024                 break;
   2025             }
   2026 
   2027             old_boost_score = boost_score;
   2028 
   2029             // Get the next frame details
   2030             if (EOF == vp8_input_stats(cpi, &local_next_frame))
   2031                 break;
   2032         }
   2033 
   2034         // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
   2035         if (boost_score > 5.0 && (i > 3))
   2036             is_viable_kf = TRUE;
   2037         else
   2038         {
   2039             // Reset the file position
   2040             reset_fpf_position(cpi, start_pos);
   2041 
   2042             is_viable_kf = FALSE;
   2043         }
   2044     }
   2045 
   2046     return is_viable_kf;
   2047 }
   2048 void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
   2049 {
   2050     int i;
   2051     FIRSTPASS_STATS last_frame;
   2052     FIRSTPASS_STATS first_frame;
   2053     FIRSTPASS_STATS next_frame;
   2054     FIRSTPASS_STATS *start_position;
   2055 
   2056     double decay_accumulator = 0;
   2057     double boost_score = 0;
   2058     double old_boost_score = 0.0;
   2059     double loop_decay_rate;
   2060 
   2061     double kf_mod_err = 0.0;
   2062     double kf_group_err = 0.0;
   2063     double kf_group_intra_err = 0.0;
   2064     double kf_group_coded_err = 0.0;
   2065     double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
   2066 
   2067     vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
   2068 
   2069     vp8_clear_system_state();  //__asm emms;
   2070     start_position = cpi->stats_in;
   2071 
   2072     cpi->common.frame_type = KEY_FRAME;
   2073 
   2074     // Clear the alt ref active flag as this can never be active on a key frame
   2075     cpi->source_alt_ref_active = FALSE;
   2076 
   2077     // Kf is always a gf so clear frames till next gf counter
   2078     cpi->frames_till_gf_update_due = 0;
   2079 
   2080     cpi->frames_to_key = 1;
   2081 
   2082     // Take a copy of the initial frame details
   2083     vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
   2084 
   2085     cpi->kf_group_bits = 0;        // Total bits avaialable to kf group
   2086     cpi->kf_group_error_left = 0;  // Group modified error score.
   2087 
   2088     kf_mod_err = calculate_modified_err(cpi, this_frame);
   2089 
   2090     // find the next keyframe
   2091     while (cpi->stats_in < cpi->stats_in_end)
   2092     {
   2093         // Accumulate kf group error
   2094         kf_group_err += calculate_modified_err(cpi, this_frame);
   2095 
   2096         // These figures keep intra and coded error counts for all frames including key frames in the group.
   2097         // The effect of the key frame itself can be subtracted out using the first_frame data collected above
   2098         kf_group_intra_err += this_frame->intra_error;
   2099         kf_group_coded_err += this_frame->coded_error;
   2100 
   2101         // load a the next frame's stats
   2102         vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
   2103         vp8_input_stats(cpi, this_frame);
   2104 
   2105         // Provided that we are not at the end of the file...
   2106         if (cpi->oxcf.auto_key
   2107             && lookup_next_frame_stats(cpi, &next_frame) != EOF)
   2108         {
   2109             if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
   2110                 break;
   2111 
   2112             // Step on to the next frame
   2113             cpi->frames_to_key ++;
   2114 
   2115             // If we don't have a real key frame within the next two
   2116             // forcekeyframeevery intervals then break out of the loop.
   2117             if (cpi->frames_to_key >= 2 *(int)cpi->key_frame_frequency)
   2118                 break;
   2119         } else
   2120             cpi->frames_to_key ++;
   2121     }
   2122 
   2123     // If there is a max kf interval set by the user we must obey it.
   2124     // We already breakout of the loop above at 2x max.
   2125     // This code centers the extra kf if the actual natural
   2126     // interval is between 1x and 2x
   2127     if (cpi->oxcf.auto_key
   2128         && cpi->frames_to_key > (int)cpi->key_frame_frequency )
   2129     {
   2130         cpi->frames_to_key /= 2;
   2131 
   2132         // Estimate corrected kf group error
   2133         kf_group_err /= 2.0;
   2134         kf_group_intra_err /= 2.0;
   2135         kf_group_coded_err /= 2.0;
   2136     }
   2137 
   2138     // Special case for the last frame of the file
   2139     if (cpi->stats_in >= cpi->stats_in_end)
   2140     {
   2141         // Accumulate kf group error
   2142         kf_group_err += calculate_modified_err(cpi, this_frame);
   2143 
   2144         // These figures keep intra and coded error counts for all frames including key frames in the group.
   2145         // The effect of the key frame itself can be subtracted out using the first_frame data collected above
   2146         kf_group_intra_err += this_frame->intra_error;
   2147         kf_group_coded_err += this_frame->coded_error;
   2148     }
   2149 
   2150     // Calculate the number of bits that should be assigned to the kf group.
   2151     if ((cpi->bits_left > 0) && ((int)cpi->modified_total_error_left > 0))
   2152     {
   2153         // Max for a single normal frame (not key frame)
   2154         int max_bits = frame_max_bits(cpi);
   2155 
   2156         // Maximum bits for the kf group
   2157         long long max_grp_bits;
   2158 
   2159         // Default allocation based on bits left and relative
   2160         // complexity of the section
   2161         cpi->kf_group_bits = (long long)( cpi->bits_left *
   2162                                           ( kf_group_err /
   2163                                             cpi->modified_total_error_left ));
   2164 
   2165         // Clip based on maximum per frame rate defined by the user.
   2166         max_grp_bits = (long long)max_bits * (long long)cpi->frames_to_key;
   2167         if (cpi->kf_group_bits > max_grp_bits)
   2168             cpi->kf_group_bits = max_grp_bits;
   2169 
   2170         // Additional special case for CBR if buffer is getting full.
   2171         if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   2172         {
   2173             int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
   2174             int buffer_lvl = cpi->buffer_level;
   2175 
   2176             // If the buffer is near or above the optimal and this kf group is
   2177             // not being allocated much then increase the allocation a bit.
   2178             if (buffer_lvl >= opt_buffer_lvl)
   2179             {
   2180                 int high_water_mark = (opt_buffer_lvl +
   2181                                        cpi->oxcf.maximum_buffer_size) >> 1;
   2182 
   2183                 long long av_group_bits;
   2184 
   2185                 // Av bits per frame * number of frames
   2186                 av_group_bits = (long long)cpi->av_per_frame_bandwidth *
   2187                                 (long long)cpi->frames_to_key;
   2188 
   2189                 // We are at or above the maximum.
   2190                 if (cpi->buffer_level >= high_water_mark)
   2191                 {
   2192                     long long min_group_bits;
   2193 
   2194                     min_group_bits = av_group_bits +
   2195                                      (long long)(buffer_lvl -
   2196                                                  high_water_mark);
   2197 
   2198                     if (cpi->kf_group_bits < min_group_bits)
   2199                         cpi->kf_group_bits = min_group_bits;
   2200                 }
   2201                 // We are above optimal but below the maximum
   2202                 else if (cpi->kf_group_bits < av_group_bits)
   2203                 {
   2204                     long long bits_below_av = av_group_bits -
   2205                                               cpi->kf_group_bits;
   2206 
   2207                     cpi->kf_group_bits +=
   2208                        (long long)((double)bits_below_av *
   2209                                    (double)(buffer_lvl - opt_buffer_lvl) /
   2210                                    (double)(high_water_mark - opt_buffer_lvl));
   2211                 }
   2212             }
   2213         }
   2214     }
   2215     else
   2216         cpi->kf_group_bits = 0;
   2217 
   2218     // Reset the first pass file position
   2219     reset_fpf_position(cpi, start_position);
   2220 
   2221     // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
   2222     decay_accumulator = 1.0;
   2223     boost_score = 0.0;
   2224     loop_decay_rate = 1.00;       // Starting decay rate
   2225 
   2226     for (i = 0 ; i < cpi->frames_to_key ; i++)
   2227     {
   2228         double r;
   2229 
   2230         if (EOF == vp8_input_stats(cpi, &next_frame))
   2231             break;
   2232 
   2233         r = (IIKFACTOR2 * next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error)) ;
   2234 
   2235         if (r > RMAX)
   2236             r = RMAX;
   2237 
   2238         // Adjust loop decay rate
   2239         //if ( next_frame.pcnt_inter < loop_decay_rate )
   2240         loop_decay_rate = next_frame.pcnt_inter;
   2241 
   2242         if ((1.0 - (next_frame.pcnt_motion / 10.0)) < loop_decay_rate)
   2243             loop_decay_rate = (1.0 - (next_frame.pcnt_motion / 10.0));
   2244 
   2245         decay_accumulator = decay_accumulator * loop_decay_rate;
   2246 
   2247         boost_score += (decay_accumulator * r);
   2248 
   2249         if ((i > MIN_GF_INTERVAL) &&
   2250             ((boost_score - old_boost_score) < 1.0))
   2251         {
   2252             break;
   2253         }
   2254 
   2255         old_boost_score = boost_score;
   2256     }
   2257 
   2258     if (1)
   2259     {
   2260         FIRSTPASS_STATS sectionstats;
   2261         double Ratio;
   2262 
   2263         vp8_zero_stats(&sectionstats);
   2264         reset_fpf_position(cpi, start_position);
   2265 
   2266         for (i = 0 ; i < cpi->frames_to_key ; i++)
   2267         {
   2268             vp8_input_stats(cpi, &next_frame);
   2269             vp8_accumulate_stats(&sectionstats, &next_frame);
   2270         }
   2271 
   2272         vp8_avg_stats(&sectionstats);
   2273 
   2274         if (sectionstats.pcnt_motion < .17)
   2275             cpi->section_is_low_motion = 1;
   2276         else
   2277             cpi->section_is_low_motion = 0;
   2278 
   2279         if (sectionstats.mvc_abs + sectionstats.mvr_abs > 45)
   2280             cpi->section_is_fast_motion = 1;
   2281         else
   2282             cpi->section_is_fast_motion = 0;
   2283 
   2284         cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
   2285 
   2286         Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
   2287         // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
   2288         //{
   2289         cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
   2290 
   2291         if (cpi->section_max_qfactor < 0.80)
   2292             cpi->section_max_qfactor = 0.80;
   2293 
   2294         //}
   2295         //else
   2296         //    cpi->section_max_qfactor = 1.0;
   2297     }
   2298 
   2299     // When using CBR apply additional buffer fullness related upper limits
   2300     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   2301     {
   2302         double max_boost;
   2303 
   2304         if (cpi->drop_frames_allowed)
   2305         {
   2306             int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
   2307 
   2308             if (cpi->buffer_level > df_buffer_level)
   2309                 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   2310             else
   2311                 max_boost = 0.0;
   2312         }
   2313         else if (cpi->buffer_level > 0)
   2314         {
   2315             max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
   2316         }
   2317         else
   2318         {
   2319             max_boost = 0.0;
   2320         }
   2321 
   2322         if (boost_score > max_boost)
   2323             boost_score = max_boost;
   2324     }
   2325 
   2326     // Reset the first pass file position
   2327     reset_fpf_position(cpi, start_position);
   2328 
   2329     // Work out how many bits to allocate for the key frame itself
   2330     if (1)
   2331     {
   2332         int kf_boost = boost_score;
   2333         int allocation_chunks;
   2334         int Counter = cpi->frames_to_key;
   2335         int alt_kf_bits;
   2336         YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
   2337         // Min boost based on kf interval
   2338 #if 0
   2339 
   2340         while ((kf_boost < 48) && (Counter > 0))
   2341         {
   2342             Counter -= 2;
   2343             kf_boost ++;
   2344         }
   2345 
   2346 #endif
   2347 
   2348         if (kf_boost < 48)
   2349         {
   2350             kf_boost += ((Counter + 1) >> 1);
   2351 
   2352             if (kf_boost > 48) kf_boost = 48;
   2353         }
   2354 
   2355         // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
   2356         if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
   2357             kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
   2358         else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
   2359             kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
   2360 
   2361         kf_boost = (int)((double)kf_boost * 100.0) >> 4;                          // Scale 16 to 100
   2362 
   2363         // Adjustment to boost based on recent average q
   2364         kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
   2365 
   2366         if (kf_boost < 250)                                                      // Min KF boost
   2367             kf_boost = 250;
   2368 
   2369         // We do three calculations for kf size.
   2370         // The first is based on the error score for the whole kf group.
   2371         // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
   2372         // The final one insures that the frame receives at least the allocation it would have received based on its own error score vs the error score remaining
   2373 
   2374         allocation_chunks = ((cpi->frames_to_key - 1) * 100) + kf_boost;           // cpi->frames_to_key-1 because key frame itself is taken care of by kf_boost
   2375 
   2376         // Normalize Altboost and allocations chunck down to prevent overflow
   2377         while (kf_boost > 1000)
   2378         {
   2379             kf_boost /= 2;
   2380             allocation_chunks /= 2;
   2381         }
   2382 
   2383         cpi->kf_group_bits = (cpi->kf_group_bits < 0) ? 0 : cpi->kf_group_bits;
   2384 
   2385         // Calculate the number of bits to be spent on the key frame
   2386         cpi->kf_bits  = (int)((double)kf_boost * ((double)cpi->kf_group_bits / (double)allocation_chunks));
   2387 
   2388         // Apply an additional limit for CBR
   2389         if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   2390         {
   2391             if (cpi->kf_bits > ((3 * cpi->buffer_level) >> 2))
   2392                 cpi->kf_bits = (3 * cpi->buffer_level) >> 2;
   2393         }
   2394 
   2395         // If the key frame is actually easier than the average for the
   2396         // kf group (which does sometimes happen... eg a blank intro frame)
   2397         // Then use an alternate calculation based on the kf error score
   2398         // which should give a smaller key frame.
   2399         if (kf_mod_err < kf_group_err / cpi->frames_to_key)
   2400         {
   2401             double  alt_kf_grp_bits =
   2402                         ((double)cpi->bits_left *
   2403                          (kf_mod_err * (double)cpi->frames_to_key) /
   2404                          DOUBLE_DIVIDE_CHECK(cpi->modified_total_error_left));
   2405 
   2406             alt_kf_bits = (int)((double)kf_boost *
   2407                                 (alt_kf_grp_bits / (double)allocation_chunks));
   2408 
   2409             if (cpi->kf_bits > alt_kf_bits)
   2410             {
   2411                 cpi->kf_bits = alt_kf_bits;
   2412             }
   2413         }
   2414         // Else if it is much harder than other frames in the group make sure
   2415         // it at least receives an allocation in keeping with its relative
   2416         // error score
   2417         else
   2418         {
   2419             alt_kf_bits =
   2420                 (int)((double)cpi->bits_left *
   2421                       (kf_mod_err /
   2422                        DOUBLE_DIVIDE_CHECK(cpi->modified_total_error_left)));
   2423 
   2424             if (alt_kf_bits > cpi->kf_bits)
   2425             {
   2426                 cpi->kf_bits = alt_kf_bits;
   2427             }
   2428         }
   2429 
   2430         cpi->kf_group_bits -= cpi->kf_bits;
   2431         cpi->kf_bits += cpi->min_frame_bandwidth;                                          // Add in the minimum frame allowance
   2432 
   2433         cpi->per_frame_bandwidth = cpi->kf_bits;                                           // Peer frame bit target for this frame
   2434         cpi->target_bandwidth = cpi->kf_bits * cpi->output_frame_rate;                      // Convert to a per second bitrate
   2435     }
   2436 
   2437     // Note the total error score of the kf group minus the key frame itself
   2438     cpi->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
   2439 
   2440     // Adjust the count of total modified error left.
   2441     // The count of bits left is adjusted elsewhere based on real coded frame sizes
   2442     cpi->modified_total_error_left -= kf_group_err;
   2443 
   2444     if (cpi->oxcf.allow_spatial_resampling)
   2445     {
   2446         int resample_trigger = FALSE;
   2447         int last_kf_resampled = FALSE;
   2448         int kf_q;
   2449         int scale_val = 0;
   2450         int hr, hs, vr, vs;
   2451         int new_width = cpi->oxcf.Width;
   2452         int new_height = cpi->oxcf.Height;
   2453 
   2454         int projected_buffer_level = cpi->buffer_level;
   2455         int tmp_q;
   2456 
   2457         double projected_bits_perframe;
   2458         double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
   2459         double err_per_frame = kf_group_err / cpi->frames_to_key;
   2460         double bits_per_frame;
   2461         double av_bits_per_frame;
   2462         double effective_size_ratio;
   2463 
   2464         if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
   2465             last_kf_resampled = TRUE;
   2466 
   2467         // Set back to unscaled by defaults
   2468         cpi->common.horiz_scale = NORMAL;
   2469         cpi->common.vert_scale = NORMAL;
   2470 
   2471         // Calculate Average bits per frame.
   2472         //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats.count - cpi->common.current_video_frame);
   2473         av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate);
   2474         //if ( av_bits_per_frame < 0.0 )
   2475         //  av_bits_per_frame = 0.0
   2476 
   2477         // CBR... Use the clip average as the target for deciding resample
   2478         if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   2479         {
   2480             bits_per_frame = av_bits_per_frame;
   2481         }
   2482 
   2483         // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
   2484         // So use the larger of target bitrate for this sectoion or average bitrate for sequence
   2485         else
   2486         {
   2487             bits_per_frame = cpi->kf_group_bits / cpi->frames_to_key;     // This accounts for how hard the section is...
   2488 
   2489             if (bits_per_frame < av_bits_per_frame)                      // Dont turn to resampling in easy sections just because they have been assigned a small number of bits
   2490                 bits_per_frame = av_bits_per_frame;
   2491         }
   2492 
   2493         // bits_per_frame should comply with our minimum
   2494         if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
   2495             bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
   2496 
   2497         // Work out if spatial resampling is necessary
   2498         kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, new_height, new_width, group_iiratio);
   2499 
   2500         // If we project a required Q higher than the maximum allowed Q then make a guess at the actual size of frames in this section
   2501         projected_bits_perframe = bits_per_frame;
   2502         tmp_q = kf_q;
   2503 
   2504         while (tmp_q > cpi->worst_quality)
   2505         {
   2506             projected_bits_perframe *= 1.04;
   2507             tmp_q--;
   2508         }
   2509 
   2510         // Guess at buffer level at the end of the section
   2511         projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->frames_to_key);
   2512 
   2513         if (0)
   2514         {
   2515             FILE *f = fopen("Subsamle.stt", "a");
   2516             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->frames_to_key, cpi->kf_group_bits / cpi->frames_to_key, new_height, new_width);
   2517             fclose(f);
   2518         }
   2519 
   2520         // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
   2521         if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   2522         {
   2523             // Trigger resample if we are projected to fall below down sample level or
   2524             // resampled last time and are projected to remain below the up sample level
   2525             if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
   2526                 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
   2527                 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
   2528                 //  ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
   2529                 resample_trigger = TRUE;
   2530             else
   2531                 resample_trigger = FALSE;
   2532         }
   2533         else
   2534         {
   2535             long long clip_bits = (long long)(cpi->total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
   2536             long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
   2537             long long over_spend2 = cpi->oxcf.starting_buffer_level - projected_buffer_level;
   2538 
   2539             if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||                                               // If triggered last time the threshold for triggering again is reduced
   2540                 ((kf_q > cpi->worst_quality) &&                                                                  // Projected Q higher than allowed and ...
   2541                  (over_spend > clip_bits / 20)))                                                               // ... Overspend > 5% of total bits
   2542                 resample_trigger = TRUE;
   2543             else
   2544                 resample_trigger = FALSE;
   2545 
   2546         }
   2547 
   2548         if (resample_trigger)
   2549         {
   2550             while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
   2551             {
   2552                 scale_val ++;
   2553 
   2554                 cpi->common.vert_scale   = vscale_lookup[scale_val];
   2555                 cpi->common.horiz_scale  = hscale_lookup[scale_val];
   2556 
   2557                 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
   2558                 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
   2559 
   2560                 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
   2561                 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
   2562 
   2563                 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
   2564                 // effective_sizeratio attempts to provide a crude correction for this
   2565                 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
   2566                 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
   2567 
   2568                 // Now try again and see what Q we get with the smaller image size
   2569                 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, new_height, new_width, group_iiratio);
   2570 
   2571                 if (0)
   2572                 {
   2573                     FILE *f = fopen("Subsamle.stt", "a");
   2574                     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->frames_to_key, cpi->kf_group_bits / cpi->frames_to_key, new_height, new_width);
   2575                     fclose(f);
   2576                 }
   2577             }
   2578         }
   2579 
   2580         if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
   2581         {
   2582             cpi->common.Width = new_width;
   2583             cpi->common.Height = new_height;
   2584             vp8_alloc_compressor_data(cpi);
   2585         }
   2586     }
   2587 }
   2588