media/libvpx/vp8/encoder/firstpass.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/encoder/firstpass.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,3367 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +#include <math.h>
    1.15 +#include <limits.h>
    1.16 +#include <stdio.h>
    1.17 +
    1.18 +#include "./vpx_scale_rtcd.h"
    1.19 +#include "block.h"
    1.20 +#include "onyx_int.h"
    1.21 +#include "vp8/common/variance.h"
    1.22 +#include "encodeintra.h"
    1.23 +#include "vp8/common/setupintrarecon.h"
    1.24 +#include "vp8/common/systemdependent.h"
    1.25 +#include "mcomp.h"
    1.26 +#include "firstpass.h"
    1.27 +#include "vpx_scale/vpx_scale.h"
    1.28 +#include "encodemb.h"
    1.29 +#include "vp8/common/extend.h"
    1.30 +#include "vpx_mem/vpx_mem.h"
    1.31 +#include "vp8/common/swapyv12buffer.h"
    1.32 +#include "rdopt.h"
    1.33 +#include "vp8/common/quant_common.h"
    1.34 +#include "encodemv.h"
    1.35 +#include "encodeframe.h"
    1.36 +
    1.37 +/* #define OUTPUT_FPF 1 */
    1.38 +
    1.39 +extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
    1.40 +extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv);
    1.41 +extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
    1.42 +
    1.43 +#define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
    1.44 +extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
    1.45 +
    1.46 +extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
    1.47 +
    1.48 +#define IIFACTOR   1.5
    1.49 +#define IIKFACTOR1 1.40
    1.50 +#define IIKFACTOR2 1.5
    1.51 +#define RMAX       14.0
    1.52 +#define GF_RMAX    48.0
    1.53 +
    1.54 +#define KF_MB_INTRA_MIN 300
    1.55 +#define GF_MB_INTRA_MIN 200
    1.56 +
    1.57 +#define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
    1.58 +
    1.59 +#define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
    1.60 +#define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
    1.61 +
    1.62 +#define NEW_BOOST 1
    1.63 +
    1.64 +static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
    1.65 +static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
    1.66 +
    1.67 +
    1.68 +static const int cq_level[QINDEX_RANGE] =
    1.69 +{
    1.70 +    0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
    1.71 +    9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
    1.72 +    20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
    1.73 +    32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
    1.74 +    44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
    1.75 +    57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
    1.76 +    71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
    1.77 +    86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
    1.78 +};
    1.79 +
    1.80 +static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
    1.81 +
    1.82 +/* Resets the first pass file to the given position using a relative seek
    1.83 + * from the current position
    1.84 + */
    1.85 +static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
    1.86 +{
    1.87 +    cpi->twopass.stats_in = Position;
    1.88 +}
    1.89 +
    1.90 +static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
    1.91 +{
    1.92 +    if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
    1.93 +        return EOF;
    1.94 +
    1.95 +    *next_frame = *cpi->twopass.stats_in;
    1.96 +    return 1;
    1.97 +}
    1.98 +
    1.99 +/* Read frame stats at an offset from the current position */
   1.100 +static int read_frame_stats( VP8_COMP *cpi,
   1.101 +                             FIRSTPASS_STATS *frame_stats,
   1.102 +                             int offset )
   1.103 +{
   1.104 +    FIRSTPASS_STATS * fps_ptr = cpi->twopass.stats_in;
   1.105 +
   1.106 +    /* Check legality of offset */
   1.107 +    if ( offset >= 0 )
   1.108 +    {
   1.109 +        if ( &fps_ptr[offset] >= cpi->twopass.stats_in_end )
   1.110 +             return EOF;
   1.111 +    }
   1.112 +    else if ( offset < 0 )
   1.113 +    {
   1.114 +        if ( &fps_ptr[offset] < cpi->twopass.stats_in_start )
   1.115 +             return EOF;
   1.116 +    }
   1.117 +
   1.118 +    *frame_stats = fps_ptr[offset];
   1.119 +    return 1;
   1.120 +}
   1.121 +
   1.122 +static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
   1.123 +{
   1.124 +    if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
   1.125 +        return EOF;
   1.126 +
   1.127 +    *fps = *cpi->twopass.stats_in;
   1.128 +    cpi->twopass.stats_in =
   1.129 +         (void*)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
   1.130 +    return 1;
   1.131 +}
   1.132 +
   1.133 +static void output_stats(const VP8_COMP            *cpi,
   1.134 +                         struct vpx_codec_pkt_list *pktlist,
   1.135 +                         FIRSTPASS_STATS            *stats)
   1.136 +{
   1.137 +    struct vpx_codec_cx_pkt pkt;
   1.138 +    pkt.kind = VPX_CODEC_STATS_PKT;
   1.139 +    pkt.data.twopass_stats.buf = stats;
   1.140 +    pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
   1.141 +    vpx_codec_pkt_list_add(pktlist, &pkt);
   1.142 +
   1.143 +/* TEMP debug code */
   1.144 +#if OUTPUT_FPF
   1.145 +
   1.146 +    {
   1.147 +        FILE *fpfile;
   1.148 +        fpfile = fopen("firstpass.stt", "a");
   1.149 +
   1.150 +        fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
   1.151 +                " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
   1.152 +                " %12.0f %12.0f %12.4f\n",
   1.153 +                stats->frame,
   1.154 +                stats->intra_error,
   1.155 +                stats->coded_error,
   1.156 +                stats->ssim_weighted_pred_err,
   1.157 +                stats->pcnt_inter,
   1.158 +                stats->pcnt_motion,
   1.159 +                stats->pcnt_second_ref,
   1.160 +                stats->pcnt_neutral,
   1.161 +                stats->MVr,
   1.162 +                stats->mvr_abs,
   1.163 +                stats->MVc,
   1.164 +                stats->mvc_abs,
   1.165 +                stats->MVrv,
   1.166 +                stats->MVcv,
   1.167 +                stats->mv_in_out_count,
   1.168 +                stats->new_mv_count,
   1.169 +                stats->count,
   1.170 +                stats->duration);
   1.171 +        fclose(fpfile);
   1.172 +    }
   1.173 +#endif
   1.174 +}
   1.175 +
   1.176 +static void zero_stats(FIRSTPASS_STATS *section)
   1.177 +{
   1.178 +    section->frame      = 0.0;
   1.179 +    section->intra_error = 0.0;
   1.180 +    section->coded_error = 0.0;
   1.181 +    section->ssim_weighted_pred_err = 0.0;
   1.182 +    section->pcnt_inter  = 0.0;
   1.183 +    section->pcnt_motion  = 0.0;
   1.184 +    section->pcnt_second_ref = 0.0;
   1.185 +    section->pcnt_neutral = 0.0;
   1.186 +    section->MVr        = 0.0;
   1.187 +    section->mvr_abs     = 0.0;
   1.188 +    section->MVc        = 0.0;
   1.189 +    section->mvc_abs     = 0.0;
   1.190 +    section->MVrv       = 0.0;
   1.191 +    section->MVcv       = 0.0;
   1.192 +    section->mv_in_out_count  = 0.0;
   1.193 +    section->new_mv_count = 0.0;
   1.194 +    section->count      = 0.0;
   1.195 +    section->duration   = 1.0;
   1.196 +}
   1.197 +
   1.198 +static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
   1.199 +{
   1.200 +    section->frame += frame->frame;
   1.201 +    section->intra_error += frame->intra_error;
   1.202 +    section->coded_error += frame->coded_error;
   1.203 +    section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
   1.204 +    section->pcnt_inter  += frame->pcnt_inter;
   1.205 +    section->pcnt_motion += frame->pcnt_motion;
   1.206 +    section->pcnt_second_ref += frame->pcnt_second_ref;
   1.207 +    section->pcnt_neutral += frame->pcnt_neutral;
   1.208 +    section->MVr        += frame->MVr;
   1.209 +    section->mvr_abs     += frame->mvr_abs;
   1.210 +    section->MVc        += frame->MVc;
   1.211 +    section->mvc_abs     += frame->mvc_abs;
   1.212 +    section->MVrv       += frame->MVrv;
   1.213 +    section->MVcv       += frame->MVcv;
   1.214 +    section->mv_in_out_count  += frame->mv_in_out_count;
   1.215 +    section->new_mv_count += frame->new_mv_count;
   1.216 +    section->count      += frame->count;
   1.217 +    section->duration   += frame->duration;
   1.218 +}
   1.219 +
   1.220 +static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
   1.221 +{
   1.222 +    section->frame -= frame->frame;
   1.223 +    section->intra_error -= frame->intra_error;
   1.224 +    section->coded_error -= frame->coded_error;
   1.225 +    section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
   1.226 +    section->pcnt_inter  -= frame->pcnt_inter;
   1.227 +    section->pcnt_motion -= frame->pcnt_motion;
   1.228 +    section->pcnt_second_ref -= frame->pcnt_second_ref;
   1.229 +    section->pcnt_neutral -= frame->pcnt_neutral;
   1.230 +    section->MVr        -= frame->MVr;
   1.231 +    section->mvr_abs     -= frame->mvr_abs;
   1.232 +    section->MVc        -= frame->MVc;
   1.233 +    section->mvc_abs     -= frame->mvc_abs;
   1.234 +    section->MVrv       -= frame->MVrv;
   1.235 +    section->MVcv       -= frame->MVcv;
   1.236 +    section->mv_in_out_count  -= frame->mv_in_out_count;
   1.237 +    section->new_mv_count -= frame->new_mv_count;
   1.238 +    section->count      -= frame->count;
   1.239 +    section->duration   -= frame->duration;
   1.240 +}
   1.241 +
   1.242 +static void avg_stats(FIRSTPASS_STATS *section)
   1.243 +{
   1.244 +    if (section->count < 1.0)
   1.245 +        return;
   1.246 +
   1.247 +    section->intra_error /= section->count;
   1.248 +    section->coded_error /= section->count;
   1.249 +    section->ssim_weighted_pred_err /= section->count;
   1.250 +    section->pcnt_inter  /= section->count;
   1.251 +    section->pcnt_second_ref /= section->count;
   1.252 +    section->pcnt_neutral /= section->count;
   1.253 +    section->pcnt_motion /= section->count;
   1.254 +    section->MVr        /= section->count;
   1.255 +    section->mvr_abs     /= section->count;
   1.256 +    section->MVc        /= section->count;
   1.257 +    section->mvc_abs     /= section->count;
   1.258 +    section->MVrv       /= section->count;
   1.259 +    section->MVcv       /= section->count;
   1.260 +    section->mv_in_out_count   /= section->count;
   1.261 +    section->duration   /= section->count;
   1.262 +}
   1.263 +
   1.264 +/* Calculate a modified Error used in distributing bits between easier
   1.265 + * and harder frames
   1.266 + */
   1.267 +static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
   1.268 +{
   1.269 +    double av_err = ( cpi->twopass.total_stats.ssim_weighted_pred_err /
   1.270 +                      cpi->twopass.total_stats.count );
   1.271 +    double this_err = this_frame->ssim_weighted_pred_err;
   1.272 +    double modified_err;
   1.273 +
   1.274 +    if (this_err > av_err)
   1.275 +        modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
   1.276 +    else
   1.277 +        modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
   1.278 +
   1.279 +    return modified_err;
   1.280 +}
   1.281 +
   1.282 +static const double weight_table[256] = {
   1.283 +0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.284 +0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.285 +0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.286 +0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.287 +0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
   1.288 +0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
   1.289 +0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
   1.290 +0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
   1.291 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.292 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.293 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.294 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.295 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.296 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.297 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.298 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.299 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.300 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.301 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.302 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.303 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.304 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.305 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.306 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.307 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.308 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.309 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.310 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.311 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.312 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.313 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.314 +1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
   1.315 +};
   1.316 +
   1.317 +static double simple_weight(YV12_BUFFER_CONFIG *source)
   1.318 +{
   1.319 +    int i, j;
   1.320 +
   1.321 +    unsigned char *src = source->y_buffer;
   1.322 +    double sum_weights = 0.0;
   1.323 +
   1.324 +    /* Loop throught the Y plane raw examining levels and creating a weight
   1.325 +     * for the image
   1.326 +     */
   1.327 +    i = source->y_height;
   1.328 +    do
   1.329 +    {
   1.330 +        j = source->y_width;
   1.331 +        do
   1.332 +        {
   1.333 +            sum_weights += weight_table[ *src];
   1.334 +            src++;
   1.335 +        }while(--j);
   1.336 +        src -= source->y_width;
   1.337 +        src += source->y_stride;
   1.338 +    }while(--i);
   1.339 +
   1.340 +    sum_weights /= (source->y_height * source->y_width);
   1.341 +
   1.342 +    return sum_weights;
   1.343 +}
   1.344 +
   1.345 +
   1.346 +/* This function returns the current per frame maximum bitrate target */
   1.347 +static int frame_max_bits(VP8_COMP *cpi)
   1.348 +{
   1.349 +    /* Max allocation for a single frame based on the max section guidelines
   1.350 +     * passed in and how many bits are left
   1.351 +     */
   1.352 +    int max_bits;
   1.353 +
   1.354 +    /* For CBR we need to also consider buffer fullness.
   1.355 +     * If we are running below the optimal level then we need to gradually
   1.356 +     * tighten up on max_bits.
   1.357 +     */
   1.358 +    if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
   1.359 +    {
   1.360 +        double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
   1.361 +
   1.362 +        /* For CBR base this on the target average bits per frame plus the
   1.363 +         * maximum sedction rate passed in by the user
   1.364 +         */
   1.365 +        max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
   1.366 +
   1.367 +        /* If our buffer is below the optimum level */
   1.368 +        if (buffer_fullness_ratio < 1.0)
   1.369 +        {
   1.370 +            /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */
   1.371 +            int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
   1.372 +
   1.373 +            max_bits = (int)(max_bits * buffer_fullness_ratio);
   1.374 +
   1.375 +            /* Lowest value we will set ... which should allow the buffer to
   1.376 +             * refill.
   1.377 +             */
   1.378 +            if (max_bits < min_max_bits)
   1.379 +                max_bits = min_max_bits;
   1.380 +        }
   1.381 +    }
   1.382 +    /* VBR */
   1.383 +    else
   1.384 +    {
   1.385 +        /* For VBR base this on the bits and frames left plus the
   1.386 +         * two_pass_vbrmax_section rate passed in by the user
   1.387 +         */
   1.388 +        max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats.count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
   1.389 +    }
   1.390 +
   1.391 +    /* Trap case where we are out of bits */
   1.392 +    if (max_bits < 0)
   1.393 +        max_bits = 0;
   1.394 +
   1.395 +    return max_bits;
   1.396 +}
   1.397 +
   1.398 +void vp8_init_first_pass(VP8_COMP *cpi)
   1.399 +{
   1.400 +    zero_stats(&cpi->twopass.total_stats);
   1.401 +}
   1.402 +
   1.403 +void vp8_end_first_pass(VP8_COMP *cpi)
   1.404 +{
   1.405 +    output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
   1.406 +}
   1.407 +
   1.408 +static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x,
   1.409 +                              YV12_BUFFER_CONFIG * raw_buffer,
   1.410 +                              int * raw_motion_err,
   1.411 +                              YV12_BUFFER_CONFIG * recon_buffer,
   1.412 +                              int * best_motion_err, int recon_yoffset)
   1.413 +{
   1.414 +    MACROBLOCKD * const xd = & x->e_mbd;
   1.415 +    BLOCK *b = &x->block[0];
   1.416 +    BLOCKD *d = &x->e_mbd.block[0];
   1.417 +
   1.418 +    unsigned char *src_ptr = (*(b->base_src) + b->src);
   1.419 +    int src_stride = b->src_stride;
   1.420 +    unsigned char *raw_ptr;
   1.421 +    int raw_stride = raw_buffer->y_stride;
   1.422 +    unsigned char *ref_ptr;
   1.423 +    int ref_stride = x->e_mbd.pre.y_stride;
   1.424 +
   1.425 +    /* Set up pointers for this macro block raw buffer */
   1.426 +    raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset
   1.427 +                                + d->offset);
   1.428 +    vp8_mse16x16 ( src_ptr, src_stride, raw_ptr, raw_stride,
   1.429 +                   (unsigned int *)(raw_motion_err));
   1.430 +
   1.431 +    /* Set up pointers for this macro block recon buffer */
   1.432 +    xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
   1.433 +    ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset );
   1.434 +    vp8_mse16x16 ( src_ptr, src_stride, ref_ptr, ref_stride,
   1.435 +                   (unsigned int *)(best_motion_err));
   1.436 +}
   1.437 +
   1.438 +static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
   1.439 +                                     int_mv *ref_mv, MV *best_mv,
   1.440 +                                     YV12_BUFFER_CONFIG *recon_buffer,
   1.441 +                                     int *best_motion_err, int recon_yoffset )
   1.442 +{
   1.443 +    MACROBLOCKD *const xd = & x->e_mbd;
   1.444 +    BLOCK *b = &x->block[0];
   1.445 +    BLOCKD *d = &x->e_mbd.block[0];
   1.446 +    int num00;
   1.447 +
   1.448 +    int_mv tmp_mv;
   1.449 +    int_mv ref_mv_full;
   1.450 +
   1.451 +    int tmp_err;
   1.452 +    int step_param = 3; /* Dont search over full range for first pass */
   1.453 +    int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
   1.454 +    int n;
   1.455 +    vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
   1.456 +    int new_mv_mode_penalty = 256;
   1.457 +
   1.458 +    /* override the default variance function to use MSE */
   1.459 +    v_fn_ptr.vf    = vp8_mse16x16;
   1.460 +
   1.461 +    /* Set up pointers for this macro block recon buffer */
   1.462 +    xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
   1.463 +
   1.464 +    /* Initial step/diamond search centred on best mv */
   1.465 +    tmp_mv.as_int = 0;
   1.466 +    ref_mv_full.as_mv.col = ref_mv->as_mv.col>>3;
   1.467 +    ref_mv_full.as_mv.row = ref_mv->as_mv.row>>3;
   1.468 +    tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
   1.469 +                                      x->sadperbit16, &num00, &v_fn_ptr,
   1.470 +                                      x->mvcost, ref_mv);
   1.471 +    if ( tmp_err < INT_MAX-new_mv_mode_penalty )
   1.472 +        tmp_err += new_mv_mode_penalty;
   1.473 +
   1.474 +    if (tmp_err < *best_motion_err)
   1.475 +    {
   1.476 +        *best_motion_err = tmp_err;
   1.477 +        best_mv->row = tmp_mv.as_mv.row;
   1.478 +        best_mv->col = tmp_mv.as_mv.col;
   1.479 +    }
   1.480 +
   1.481 +    /* Further step/diamond searches as necessary */
   1.482 +    n = num00;
   1.483 +    num00 = 0;
   1.484 +
   1.485 +    while (n < further_steps)
   1.486 +    {
   1.487 +        n++;
   1.488 +
   1.489 +        if (num00)
   1.490 +            num00--;
   1.491 +        else
   1.492 +        {
   1.493 +            tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
   1.494 +                                              step_param + n, x->sadperbit16,
   1.495 +                                              &num00, &v_fn_ptr, x->mvcost,
   1.496 +                                              ref_mv);
   1.497 +            if ( tmp_err < INT_MAX-new_mv_mode_penalty )
   1.498 +                tmp_err += new_mv_mode_penalty;
   1.499 +
   1.500 +            if (tmp_err < *best_motion_err)
   1.501 +            {
   1.502 +                *best_motion_err = tmp_err;
   1.503 +                best_mv->row = tmp_mv.as_mv.row;
   1.504 +                best_mv->col = tmp_mv.as_mv.col;
   1.505 +            }
   1.506 +        }
   1.507 +    }
   1.508 +}
   1.509 +
   1.510 +void vp8_first_pass(VP8_COMP *cpi)
   1.511 +{
   1.512 +    int mb_row, mb_col;
   1.513 +    MACROBLOCK *const x = & cpi->mb;
   1.514 +    VP8_COMMON *const cm = & cpi->common;
   1.515 +    MACROBLOCKD *const xd = & x->e_mbd;
   1.516 +
   1.517 +    int recon_yoffset, recon_uvoffset;
   1.518 +    YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
   1.519 +    YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
   1.520 +    YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
   1.521 +    int recon_y_stride = lst_yv12->y_stride;
   1.522 +    int recon_uv_stride = lst_yv12->uv_stride;
   1.523 +    int64_t intra_error = 0;
   1.524 +    int64_t coded_error = 0;
   1.525 +
   1.526 +    int sum_mvr = 0, sum_mvc = 0;
   1.527 +    int sum_mvr_abs = 0, sum_mvc_abs = 0;
   1.528 +    int sum_mvrs = 0, sum_mvcs = 0;
   1.529 +    int mvcount = 0;
   1.530 +    int intercount = 0;
   1.531 +    int second_ref_count = 0;
   1.532 +    int intrapenalty = 256;
   1.533 +    int neutral_count = 0;
   1.534 +    int new_mv_count = 0;
   1.535 +    int sum_in_vectors = 0;
   1.536 +    uint32_t lastmv_as_int = 0;
   1.537 +
   1.538 +    int_mv zero_ref_mv;
   1.539 +
   1.540 +    zero_ref_mv.as_int = 0;
   1.541 +
   1.542 +    vp8_clear_system_state();
   1.543 +
   1.544 +    x->src = * cpi->Source;
   1.545 +    xd->pre = *lst_yv12;
   1.546 +    xd->dst = *new_yv12;
   1.547 +
   1.548 +    x->partition_info = x->pi;
   1.549 +
   1.550 +    xd->mode_info_context = cm->mi;
   1.551 +
   1.552 +    if(!cm->use_bilinear_mc_filter)
   1.553 +    {
   1.554 +         xd->subpixel_predict        = vp8_sixtap_predict4x4;
   1.555 +         xd->subpixel_predict8x4     = vp8_sixtap_predict8x4;
   1.556 +         xd->subpixel_predict8x8     = vp8_sixtap_predict8x8;
   1.557 +         xd->subpixel_predict16x16   = vp8_sixtap_predict16x16;
   1.558 +     }
   1.559 +     else
   1.560 +     {
   1.561 +         xd->subpixel_predict        = vp8_bilinear_predict4x4;
   1.562 +         xd->subpixel_predict8x4     = vp8_bilinear_predict8x4;
   1.563 +         xd->subpixel_predict8x8     = vp8_bilinear_predict8x8;
   1.564 +         xd->subpixel_predict16x16   = vp8_bilinear_predict16x16;
   1.565 +     }
   1.566 +
   1.567 +    vp8_build_block_offsets(x);
   1.568 +
   1.569 +    /* set up frame new frame for intra coded blocks */
   1.570 +    vp8_setup_intra_recon(new_yv12);
   1.571 +    vp8cx_frame_init_quantizer(cpi);
   1.572 +
   1.573 +    /* Initialise the MV cost table to the defaults */
   1.574 +    {
   1.575 +        int flag[2] = {1, 1};
   1.576 +        vp8_initialize_rd_consts(cpi, x, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
   1.577 +        vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
   1.578 +        vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
   1.579 +    }
   1.580 +
   1.581 +    /* for each macroblock row in image */
   1.582 +    for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
   1.583 +    {
   1.584 +        int_mv best_ref_mv;
   1.585 +
   1.586 +        best_ref_mv.as_int = 0;
   1.587 +
   1.588 +        /* reset above block coeffs */
   1.589 +        xd->up_available = (mb_row != 0);
   1.590 +        recon_yoffset = (mb_row * recon_y_stride * 16);
   1.591 +        recon_uvoffset = (mb_row * recon_uv_stride * 8);
   1.592 +
   1.593 +        /* Set up limit values for motion vectors to prevent them extending
   1.594 +         * outside the UMV borders
   1.595 +         */
   1.596 +        x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
   1.597 +        x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
   1.598 +
   1.599 +
   1.600 +        /* for each macroblock col in image */
   1.601 +        for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
   1.602 +        {
   1.603 +            int this_error;
   1.604 +            int gf_motion_error = INT_MAX;
   1.605 +            int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
   1.606 +
   1.607 +            xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
   1.608 +            xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
   1.609 +            xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
   1.610 +            xd->left_available = (mb_col != 0);
   1.611 +
   1.612 +            /* Copy current mb to a buffer */
   1.613 +            vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
   1.614 +
   1.615 +            /* do intra 16x16 prediction */
   1.616 +            this_error = vp8_encode_intra(cpi, x, use_dc_pred);
   1.617 +
   1.618 +            /* "intrapenalty" below deals with situations where the intra
   1.619 +             * and inter error scores are very low (eg a plain black frame)
   1.620 +             * We do not have special cases in first pass for 0,0 and
   1.621 +             * nearest etc so all inter modes carry an overhead cost
   1.622 +             * estimate fot the mv. When the error score is very low this
   1.623 +             * causes us to pick all or lots of INTRA modes and throw lots
   1.624 +             * of key frames. This penalty adds a cost matching that of a
   1.625 +             * 0,0 mv to the intra case.
   1.626 +             */
   1.627 +            this_error += intrapenalty;
   1.628 +
   1.629 +            /* Cumulative intra error total */
   1.630 +            intra_error += (int64_t)this_error;
   1.631 +
   1.632 +            /* Set up limit values for motion vectors to prevent them
   1.633 +             * extending outside the UMV borders
   1.634 +             */
   1.635 +            x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
   1.636 +            x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
   1.637 +
   1.638 +            /* Other than for the first frame do a motion search */
   1.639 +            if (cm->current_video_frame > 0)
   1.640 +            {
   1.641 +                BLOCKD *d = &x->e_mbd.block[0];
   1.642 +                MV tmp_mv = {0, 0};
   1.643 +                int tmp_err;
   1.644 +                int motion_error = INT_MAX;
   1.645 +                int raw_motion_error = INT_MAX;
   1.646 +
   1.647 +                /* Simple 0,0 motion with no mv overhead */
   1.648 +                zz_motion_search( cpi, x, cpi->last_frame_unscaled_source,
   1.649 +                                  &raw_motion_error, lst_yv12, &motion_error,
   1.650 +                                  recon_yoffset );
   1.651 +                d->bmi.mv.as_mv.row = 0;
   1.652 +                d->bmi.mv.as_mv.col = 0;
   1.653 +
   1.654 +                if (raw_motion_error < cpi->oxcf.encode_breakout)
   1.655 +                    goto skip_motion_search;
   1.656 +
   1.657 +                /* Test last reference frame using the previous best mv as the
   1.658 +                 * starting point (best reference) for the search
   1.659 +                 */
   1.660 +                first_pass_motion_search(cpi, x, &best_ref_mv,
   1.661 +                                        &d->bmi.mv.as_mv, lst_yv12,
   1.662 +                                        &motion_error, recon_yoffset);
   1.663 +
   1.664 +                /* If the current best reference mv is not centred on 0,0
   1.665 +                 * then do a 0,0 based search as well
   1.666 +                 */
   1.667 +                if (best_ref_mv.as_int)
   1.668 +                {
   1.669 +                   tmp_err = INT_MAX;
   1.670 +                   first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
   1.671 +                                     lst_yv12, &tmp_err, recon_yoffset);
   1.672 +
   1.673 +                   if ( tmp_err < motion_error )
   1.674 +                   {
   1.675 +                        motion_error = tmp_err;
   1.676 +                        d->bmi.mv.as_mv.row = tmp_mv.row;
   1.677 +                        d->bmi.mv.as_mv.col = tmp_mv.col;
   1.678 +                   }
   1.679 +                }
   1.680 +
   1.681 +                /* Experimental search in a second reference frame ((0,0)
   1.682 +                 * based only)
   1.683 +                 */
   1.684 +                if (cm->current_video_frame > 1)
   1.685 +                {
   1.686 +                    first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
   1.687 +
   1.688 +                    if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
   1.689 +                    {
   1.690 +                        second_ref_count++;
   1.691 +                    }
   1.692 +
   1.693 +                    /* Reset to last frame as reference buffer */
   1.694 +                    xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
   1.695 +                    xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
   1.696 +                    xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
   1.697 +                }
   1.698 +
   1.699 +skip_motion_search:
   1.700 +                /* Intra assumed best */
   1.701 +                best_ref_mv.as_int = 0;
   1.702 +
   1.703 +                if (motion_error <= this_error)
   1.704 +                {
   1.705 +                    /* Keep a count of cases where the inter and intra were
   1.706 +                     * very close and very low. This helps with scene cut
   1.707 +                     * detection for example in cropped clips with black bars
   1.708 +                     * at the sides or top and bottom.
   1.709 +                     */
   1.710 +                    if( (((this_error-intrapenalty) * 9) <=
   1.711 +                         (motion_error*10)) &&
   1.712 +                        (this_error < (2*intrapenalty)) )
   1.713 +                    {
   1.714 +                        neutral_count++;
   1.715 +                    }
   1.716 +
   1.717 +                    d->bmi.mv.as_mv.row *= 8;
   1.718 +                    d->bmi.mv.as_mv.col *= 8;
   1.719 +                    this_error = motion_error;
   1.720 +                    vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
   1.721 +                    vp8_encode_inter16x16y(x);
   1.722 +                    sum_mvr += d->bmi.mv.as_mv.row;
   1.723 +                    sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
   1.724 +                    sum_mvc += d->bmi.mv.as_mv.col;
   1.725 +                    sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
   1.726 +                    sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
   1.727 +                    sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
   1.728 +                    intercount++;
   1.729 +
   1.730 +                    best_ref_mv.as_int = d->bmi.mv.as_int;
   1.731 +
   1.732 +                    /* Was the vector non-zero */
   1.733 +                    if (d->bmi.mv.as_int)
   1.734 +                    {
   1.735 +                        mvcount++;
   1.736 +
   1.737 +                        /* Was it different from the last non zero vector */
   1.738 +                        if ( d->bmi.mv.as_int != lastmv_as_int )
   1.739 +                            new_mv_count++;
   1.740 +                        lastmv_as_int = d->bmi.mv.as_int;
   1.741 +
   1.742 +                        /* Does the Row vector point inwards or outwards */
   1.743 +                        if (mb_row < cm->mb_rows / 2)
   1.744 +                        {
   1.745 +                            if (d->bmi.mv.as_mv.row > 0)
   1.746 +                                sum_in_vectors--;
   1.747 +                            else if (d->bmi.mv.as_mv.row < 0)
   1.748 +                                sum_in_vectors++;
   1.749 +                        }
   1.750 +                        else if (mb_row > cm->mb_rows / 2)
   1.751 +                        {
   1.752 +                            if (d->bmi.mv.as_mv.row > 0)
   1.753 +                                sum_in_vectors++;
   1.754 +                            else if (d->bmi.mv.as_mv.row < 0)
   1.755 +                                sum_in_vectors--;
   1.756 +                        }
   1.757 +
   1.758 +                        /* Does the Row vector point inwards or outwards */
   1.759 +                        if (mb_col < cm->mb_cols / 2)
   1.760 +                        {
   1.761 +                            if (d->bmi.mv.as_mv.col > 0)
   1.762 +                                sum_in_vectors--;
   1.763 +                            else if (d->bmi.mv.as_mv.col < 0)
   1.764 +                                sum_in_vectors++;
   1.765 +                        }
   1.766 +                        else if (mb_col > cm->mb_cols / 2)
   1.767 +                        {
   1.768 +                            if (d->bmi.mv.as_mv.col > 0)
   1.769 +                                sum_in_vectors++;
   1.770 +                            else if (d->bmi.mv.as_mv.col < 0)
   1.771 +                                sum_in_vectors--;
   1.772 +                        }
   1.773 +                    }
   1.774 +                }
   1.775 +            }
   1.776 +
   1.777 +            coded_error += (int64_t)this_error;
   1.778 +
   1.779 +            /* adjust to the next column of macroblocks */
   1.780 +            x->src.y_buffer += 16;
   1.781 +            x->src.u_buffer += 8;
   1.782 +            x->src.v_buffer += 8;
   1.783 +
   1.784 +            recon_yoffset += 16;
   1.785 +            recon_uvoffset += 8;
   1.786 +        }
   1.787 +
   1.788 +        /* adjust to the next row of mbs */
   1.789 +        x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
   1.790 +        x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
   1.791 +        x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
   1.792 +
   1.793 +        /* extend the recon for intra prediction */
   1.794 +        vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
   1.795 +        vp8_clear_system_state();
   1.796 +    }
   1.797 +
   1.798 +    vp8_clear_system_state();
   1.799 +    {
   1.800 +        double weight = 0.0;
   1.801 +
   1.802 +        FIRSTPASS_STATS fps;
   1.803 +
   1.804 +        fps.frame      = cm->current_video_frame ;
   1.805 +        fps.intra_error = (double)(intra_error >> 8);
   1.806 +        fps.coded_error = (double)(coded_error >> 8);
   1.807 +        weight = simple_weight(cpi->Source);
   1.808 +
   1.809 +
   1.810 +        if (weight < 0.1)
   1.811 +            weight = 0.1;
   1.812 +
   1.813 +        fps.ssim_weighted_pred_err = fps.coded_error * weight;
   1.814 +
   1.815 +        fps.pcnt_inter  = 0.0;
   1.816 +        fps.pcnt_motion = 0.0;
   1.817 +        fps.MVr        = 0.0;
   1.818 +        fps.mvr_abs     = 0.0;
   1.819 +        fps.MVc        = 0.0;
   1.820 +        fps.mvc_abs     = 0.0;
   1.821 +        fps.MVrv       = 0.0;
   1.822 +        fps.MVcv       = 0.0;
   1.823 +        fps.mv_in_out_count  = 0.0;
   1.824 +        fps.new_mv_count = 0.0;
   1.825 +        fps.count      = 1.0;
   1.826 +
   1.827 +        fps.pcnt_inter   = 1.0 * (double)intercount / cm->MBs;
   1.828 +        fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
   1.829 +        fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
   1.830 +
   1.831 +        if (mvcount > 0)
   1.832 +        {
   1.833 +            fps.MVr = (double)sum_mvr / (double)mvcount;
   1.834 +            fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
   1.835 +            fps.MVc = (double)sum_mvc / (double)mvcount;
   1.836 +            fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
   1.837 +            fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
   1.838 +            fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
   1.839 +            fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
   1.840 +            fps.new_mv_count = new_mv_count;
   1.841 +
   1.842 +            fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
   1.843 +        }
   1.844 +
   1.845 +        /* TODO:  handle the case when duration is set to 0, or something less
   1.846 +         * than the full time between subsequent cpi->source_time_stamps
   1.847 +         */
   1.848 +        fps.duration = (double)(cpi->source->ts_end
   1.849 +                       - cpi->source->ts_start);
   1.850 +
   1.851 +        /* don't want to do output stats with a stack variable! */
   1.852 +        memcpy(&cpi->twopass.this_frame_stats,
   1.853 +               &fps,
   1.854 +               sizeof(FIRSTPASS_STATS));
   1.855 +        output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
   1.856 +        accumulate_stats(&cpi->twopass.total_stats, &fps);
   1.857 +    }
   1.858 +
   1.859 +    /* Copy the previous Last Frame into the GF buffer if specific
   1.860 +     * conditions for doing so are met
   1.861 +     */
   1.862 +    if ((cm->current_video_frame > 0) &&
   1.863 +        (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
   1.864 +        ((cpi->twopass.this_frame_stats.intra_error /
   1.865 +          DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
   1.866 +         2.0))
   1.867 +    {
   1.868 +        vp8_yv12_copy_frame(lst_yv12, gld_yv12);
   1.869 +    }
   1.870 +
   1.871 +    /* swap frame pointers so last frame refers to the frame we just
   1.872 +     * compressed
   1.873 +     */
   1.874 +    vp8_swap_yv12_buffer(lst_yv12, new_yv12);
   1.875 +    vp8_yv12_extend_frame_borders(lst_yv12);
   1.876 +
   1.877 +    /* Special case for the first frame. Copy into the GF buffer as a
   1.878 +     * second reference.
   1.879 +     */
   1.880 +    if (cm->current_video_frame == 0)
   1.881 +    {
   1.882 +        vp8_yv12_copy_frame(lst_yv12, gld_yv12);
   1.883 +    }
   1.884 +
   1.885 +
   1.886 +    /* use this to see what the first pass reconstruction looks like */
   1.887 +    if (0)
   1.888 +    {
   1.889 +        char filename[512];
   1.890 +        FILE *recon_file;
   1.891 +        sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
   1.892 +
   1.893 +        if (cm->current_video_frame == 0)
   1.894 +            recon_file = fopen(filename, "wb");
   1.895 +        else
   1.896 +            recon_file = fopen(filename, "ab");
   1.897 +
   1.898 +        (void) fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1,
   1.899 +                      recon_file);
   1.900 +        fclose(recon_file);
   1.901 +    }
   1.902 +
   1.903 +    cm->current_video_frame++;
   1.904 +
   1.905 +}
   1.906 +extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
   1.907 +
   1.908 +/* Estimate a cost per mb attributable to overheads such as the coding of
   1.909 + * modes and motion vectors.
   1.910 + * Currently simplistic in its assumptions for testing.
   1.911 + */
   1.912 +
   1.913 +static double bitcost( double prob )
   1.914 +{
   1.915 +  if (prob > 0.000122)
   1.916 +    return -log(prob) / log(2.0);
   1.917 +  else
   1.918 +    return 13.0;
   1.919 +}
   1.920 +static int64_t estimate_modemvcost(VP8_COMP *cpi,
   1.921 +                                     FIRSTPASS_STATS * fpstats)
   1.922 +{
   1.923 +    int mv_cost;
   1.924 +    int64_t mode_cost;
   1.925 +
   1.926 +    double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
   1.927 +    double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
   1.928 +    double av_intra = (1.0 - av_pct_inter);
   1.929 +
   1.930 +    double zz_cost;
   1.931 +    double motion_cost;
   1.932 +    double intra_cost;
   1.933 +
   1.934 +    zz_cost = bitcost(av_pct_inter - av_pct_motion);
   1.935 +    motion_cost = bitcost(av_pct_motion);
   1.936 +    intra_cost = bitcost(av_intra);
   1.937 +
   1.938 +    /* Estimate of extra bits per mv overhead for mbs
   1.939 +     * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb
   1.940 +     */
   1.941 +    mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
   1.942 +
   1.943 +    /* Crude estimate of overhead cost from modes
   1.944 +     * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
   1.945 +     */
   1.946 +    mode_cost =((((av_pct_inter - av_pct_motion) * zz_cost) +
   1.947 +                (av_pct_motion * motion_cost) +
   1.948 +                (av_intra * intra_cost)) * cpi->common.MBs) * 512;
   1.949 +
   1.950 +    return mv_cost + mode_cost;
   1.951 +}
   1.952 +
   1.953 +static double calc_correction_factor( double err_per_mb,
   1.954 +                                      double err_devisor,
   1.955 +                                      double pt_low,
   1.956 +                                      double pt_high,
   1.957 +                                      int Q )
   1.958 +{
   1.959 +    double power_term;
   1.960 +    double error_term = err_per_mb / err_devisor;
   1.961 +    double correction_factor;
   1.962 +
   1.963 +    /* Adjustment based on Q to power term. */
   1.964 +    power_term = pt_low + (Q * 0.01);
   1.965 +    power_term = (power_term > pt_high) ? pt_high : power_term;
   1.966 +
   1.967 +    /* Adjustments to error term */
   1.968 +    /* TBD */
   1.969 +
   1.970 +    /* Calculate correction factor */
   1.971 +    correction_factor = pow(error_term, power_term);
   1.972 +
   1.973 +    /* Clip range */
   1.974 +    correction_factor =
   1.975 +        (correction_factor < 0.05)
   1.976 +            ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
   1.977 +
   1.978 +    return correction_factor;
   1.979 +}
   1.980 +
   1.981 +static int estimate_max_q(VP8_COMP *cpi,
   1.982 +                          FIRSTPASS_STATS * fpstats,
   1.983 +                          int section_target_bandwitdh,
   1.984 +                          int overhead_bits )
   1.985 +{
   1.986 +    int Q;
   1.987 +    int num_mbs = cpi->common.MBs;
   1.988 +    int target_norm_bits_per_mb;
   1.989 +
   1.990 +    double section_err = (fpstats->coded_error / fpstats->count);
   1.991 +    double err_per_mb = section_err / num_mbs;
   1.992 +    double err_correction_factor;
   1.993 +    double speed_correction = 1.0;
   1.994 +    int overhead_bits_per_mb;
   1.995 +
   1.996 +    if (section_target_bandwitdh <= 0)
   1.997 +        return cpi->twopass.maxq_max_limit;       /* Highest value allowed */
   1.998 +
   1.999 +    target_norm_bits_per_mb =
  1.1000 +        (section_target_bandwitdh < (1 << 20))
  1.1001 +            ? (512 * section_target_bandwitdh) / num_mbs
  1.1002 +            : 512 * (section_target_bandwitdh / num_mbs);
  1.1003 +
  1.1004 +    /* Calculate a corrective factor based on a rolling ratio of bits spent
  1.1005 +     * vs target bits
  1.1006 +     */
  1.1007 +    if ((cpi->rolling_target_bits > 0) &&
  1.1008 +        (cpi->active_worst_quality < cpi->worst_quality))
  1.1009 +    {
  1.1010 +        double rolling_ratio;
  1.1011 +
  1.1012 +        rolling_ratio = (double)cpi->rolling_actual_bits /
  1.1013 +                        (double)cpi->rolling_target_bits;
  1.1014 +
  1.1015 +        if (rolling_ratio < 0.95)
  1.1016 +            cpi->twopass.est_max_qcorrection_factor -= 0.005;
  1.1017 +        else if (rolling_ratio > 1.05)
  1.1018 +            cpi->twopass.est_max_qcorrection_factor += 0.005;
  1.1019 +
  1.1020 +        cpi->twopass.est_max_qcorrection_factor =
  1.1021 +            (cpi->twopass.est_max_qcorrection_factor < 0.1)
  1.1022 +                ? 0.1
  1.1023 +                : (cpi->twopass.est_max_qcorrection_factor > 10.0)
  1.1024 +                    ? 10.0 : cpi->twopass.est_max_qcorrection_factor;
  1.1025 +    }
  1.1026 +
  1.1027 +    /* Corrections for higher compression speed settings
  1.1028 +     * (reduced compression expected)
  1.1029 +     */
  1.1030 +    if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
  1.1031 +    {
  1.1032 +        if (cpi->oxcf.cpu_used <= 5)
  1.1033 +            speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  1.1034 +        else
  1.1035 +            speed_correction = 1.25;
  1.1036 +    }
  1.1037 +
  1.1038 +    /* Estimate of overhead bits per mb */
  1.1039 +    /* Correction to overhead bits for min allowed Q. */
  1.1040 +    overhead_bits_per_mb = overhead_bits / num_mbs;
  1.1041 +    overhead_bits_per_mb = (int)(overhead_bits_per_mb *
  1.1042 +                            pow( 0.98, (double)cpi->twopass.maxq_min_limit ));
  1.1043 +
  1.1044 +    /* Try and pick a max Q that will be high enough to encode the
  1.1045 +     * content at the given rate.
  1.1046 +     */
  1.1047 +    for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++)
  1.1048 +    {
  1.1049 +        int bits_per_mb_at_this_q;
  1.1050 +
  1.1051 +        /* Error per MB based correction factor */
  1.1052 +        err_correction_factor =
  1.1053 +            calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
  1.1054 +
  1.1055 +        bits_per_mb_at_this_q =
  1.1056 +            vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
  1.1057 +
  1.1058 +        bits_per_mb_at_this_q = (int)(.5 + err_correction_factor
  1.1059 +            * speed_correction * cpi->twopass.est_max_qcorrection_factor
  1.1060 +            * cpi->twopass.section_max_qfactor
  1.1061 +            * (double)bits_per_mb_at_this_q);
  1.1062 +
  1.1063 +        /* Mode and motion overhead */
  1.1064 +        /* As Q rises in real encode loop rd code will force overhead down
  1.1065 +         * We make a crude adjustment for this here as *.98 per Q step.
  1.1066 +         */
  1.1067 +        overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
  1.1068 +
  1.1069 +        if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
  1.1070 +            break;
  1.1071 +    }
  1.1072 +
  1.1073 +    /* Restriction on active max q for constrained quality mode. */
  1.1074 +    if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
  1.1075 +         (Q < cpi->cq_target_quality) )
  1.1076 +    {
  1.1077 +        Q = cpi->cq_target_quality;
  1.1078 +    }
  1.1079 +
  1.1080 +    /* Adjust maxq_min_limit and maxq_max_limit limits based on
  1.1081 +     * average q observed in clip for non kf/gf.arf frames
  1.1082 +     * Give average a chance to settle though.
  1.1083 +     */
  1.1084 +    if ( (cpi->ni_frames >
  1.1085 +                  ((int)cpi->twopass.total_stats.count >> 8)) &&
  1.1086 +         (cpi->ni_frames > 150) )
  1.1087 +    {
  1.1088 +        cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
  1.1089 +                                  ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
  1.1090 +        cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
  1.1091 +                                  ? (cpi->ni_av_qi - 32) : cpi->best_quality;
  1.1092 +    }
  1.1093 +
  1.1094 +    return Q;
  1.1095 +}
  1.1096 +
  1.1097 +/* For cq mode estimate a cq level that matches the observed
  1.1098 + * complexity and data rate.
  1.1099 + */
  1.1100 +static int estimate_cq( VP8_COMP *cpi,
  1.1101 +                        FIRSTPASS_STATS * fpstats,
  1.1102 +                        int section_target_bandwitdh,
  1.1103 +                        int overhead_bits )
  1.1104 +{
  1.1105 +    int Q;
  1.1106 +    int num_mbs = cpi->common.MBs;
  1.1107 +    int target_norm_bits_per_mb;
  1.1108 +
  1.1109 +    double section_err = (fpstats->coded_error / fpstats->count);
  1.1110 +    double err_per_mb = section_err / num_mbs;
  1.1111 +    double err_correction_factor;
  1.1112 +    double speed_correction = 1.0;
  1.1113 +    double clip_iiratio;
  1.1114 +    double clip_iifactor;
  1.1115 +    int overhead_bits_per_mb;
  1.1116 +
  1.1117 +    if (0)
  1.1118 +    {
  1.1119 +        FILE *f = fopen("epmp.stt", "a");
  1.1120 +        fprintf(f, "%10.2f\n", err_per_mb );
  1.1121 +        fclose(f);
  1.1122 +    }
  1.1123 +
  1.1124 +    target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
  1.1125 +                              ? (512 * section_target_bandwitdh) / num_mbs
  1.1126 +                              : 512 * (section_target_bandwitdh / num_mbs);
  1.1127 +
  1.1128 +    /* Estimate of overhead bits per mb */
  1.1129 +    overhead_bits_per_mb = overhead_bits / num_mbs;
  1.1130 +
  1.1131 +    /* Corrections for higher compression speed settings
  1.1132 +     * (reduced compression expected)
  1.1133 +     */
  1.1134 +    if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
  1.1135 +    {
  1.1136 +        if (cpi->oxcf.cpu_used <= 5)
  1.1137 +            speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  1.1138 +        else
  1.1139 +            speed_correction = 1.25;
  1.1140 +    }
  1.1141 +
  1.1142 +    /* II ratio correction factor for clip as a whole */
  1.1143 +    clip_iiratio = cpi->twopass.total_stats.intra_error /
  1.1144 +                   DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
  1.1145 +    clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
  1.1146 +    if (clip_iifactor < 0.80)
  1.1147 +        clip_iifactor = 0.80;
  1.1148 +
  1.1149 +    /* Try and pick a Q that can encode the content at the given rate. */
  1.1150 +    for (Q = 0; Q < MAXQ; Q++)
  1.1151 +    {
  1.1152 +        int bits_per_mb_at_this_q;
  1.1153 +
  1.1154 +        /* Error per MB based correction factor */
  1.1155 +        err_correction_factor =
  1.1156 +            calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q);
  1.1157 +
  1.1158 +        bits_per_mb_at_this_q =
  1.1159 +            vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
  1.1160 +
  1.1161 +        bits_per_mb_at_this_q =
  1.1162 +            (int)( .5 + err_correction_factor *
  1.1163 +                        speed_correction *
  1.1164 +                        clip_iifactor *
  1.1165 +                        (double)bits_per_mb_at_this_q);
  1.1166 +
  1.1167 +        /* Mode and motion overhead */
  1.1168 +        /* As Q rises in real encode loop rd code will force overhead down
  1.1169 +         * We make a crude adjustment for this here as *.98 per Q step.
  1.1170 +         */
  1.1171 +        overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
  1.1172 +
  1.1173 +        if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
  1.1174 +            break;
  1.1175 +    }
  1.1176 +
  1.1177 +    /* Clip value to range "best allowed to (worst allowed - 1)" */
  1.1178 +    Q = cq_level[Q];
  1.1179 +    if ( Q >= cpi->worst_quality )
  1.1180 +        Q = cpi->worst_quality - 1;
  1.1181 +    if ( Q < cpi->best_quality )
  1.1182 +        Q = cpi->best_quality;
  1.1183 +
  1.1184 +    return Q;
  1.1185 +}
  1.1186 +
  1.1187 +static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
  1.1188 +{
  1.1189 +    int Q;
  1.1190 +    int num_mbs = cpi->common.MBs;
  1.1191 +    int target_norm_bits_per_mb;
  1.1192 +
  1.1193 +    double err_per_mb = section_err / num_mbs;
  1.1194 +    double err_correction_factor;
  1.1195 +    double speed_correction = 1.0;
  1.1196 +
  1.1197 +    target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
  1.1198 +
  1.1199 +    /* Corrections for higher compression speed settings
  1.1200 +     * (reduced compression expected)
  1.1201 +     */
  1.1202 +    if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
  1.1203 +    {
  1.1204 +        if (cpi->oxcf.cpu_used <= 5)
  1.1205 +            speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  1.1206 +        else
  1.1207 +            speed_correction = 1.25;
  1.1208 +    }
  1.1209 +
  1.1210 +    /* Try and pick a Q that can encode the content at the given rate. */
  1.1211 +    for (Q = 0; Q < MAXQ; Q++)
  1.1212 +    {
  1.1213 +        int bits_per_mb_at_this_q;
  1.1214 +
  1.1215 +        /* Error per MB based correction factor */
  1.1216 +        err_correction_factor =
  1.1217 +            calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
  1.1218 +
  1.1219 +        bits_per_mb_at_this_q =
  1.1220 +            (int)( .5 + ( err_correction_factor *
  1.1221 +                          speed_correction *
  1.1222 +                          cpi->twopass.est_max_qcorrection_factor *
  1.1223 +                          (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0 ) );
  1.1224 +
  1.1225 +        if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
  1.1226 +            break;
  1.1227 +    }
  1.1228 +
  1.1229 +    return Q;
  1.1230 +}
  1.1231 +
  1.1232 +/* Estimate a worst case Q for a KF group */
  1.1233 +static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio)
  1.1234 +{
  1.1235 +    int Q;
  1.1236 +    int num_mbs = cpi->common.MBs;
  1.1237 +    int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
  1.1238 +    int bits_per_mb_at_this_q;
  1.1239 +
  1.1240 +    double err_per_mb = section_err / num_mbs;
  1.1241 +    double err_correction_factor;
  1.1242 +    double speed_correction = 1.0;
  1.1243 +    double current_spend_ratio = 1.0;
  1.1244 +
  1.1245 +    double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
  1.1246 +    double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
  1.1247 +
  1.1248 +    double iiratio_correction_factor = 1.0;
  1.1249 +
  1.1250 +    double combined_correction_factor;
  1.1251 +
  1.1252 +    /* Trap special case where the target is <= 0 */
  1.1253 +    if (target_norm_bits_per_mb <= 0)
  1.1254 +        return MAXQ * 2;
  1.1255 +
  1.1256 +    /* Calculate a corrective factor based on a rolling ratio of bits spent
  1.1257 +     *  vs target bits
  1.1258 +     * This is clamped to the range 0.1 to 10.0
  1.1259 +     */
  1.1260 +    if (cpi->long_rolling_target_bits <= 0)
  1.1261 +        current_spend_ratio = 10.0;
  1.1262 +    else
  1.1263 +    {
  1.1264 +        current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
  1.1265 +        current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
  1.1266 +    }
  1.1267 +
  1.1268 +    /* Calculate a correction factor based on the quality of prediction in
  1.1269 +     * the sequence as indicated by intra_inter error score ratio (IIRatio)
  1.1270 +     * The idea here is to favour subsampling in the hardest sections vs
  1.1271 +     * the easyest.
  1.1272 +     */
  1.1273 +    iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
  1.1274 +
  1.1275 +    if (iiratio_correction_factor < 0.5)
  1.1276 +        iiratio_correction_factor = 0.5;
  1.1277 +
  1.1278 +    /* Corrections for higher compression speed settings
  1.1279 +     * (reduced compression expected)
  1.1280 +     */
  1.1281 +    if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
  1.1282 +    {
  1.1283 +        if (cpi->oxcf.cpu_used <= 5)
  1.1284 +            speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  1.1285 +        else
  1.1286 +            speed_correction = 1.25;
  1.1287 +    }
  1.1288 +
  1.1289 +    /* Combine the various factors calculated above */
  1.1290 +    combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
  1.1291 +
  1.1292 +    /* Try and pick a Q that should be high enough to encode the content at
  1.1293 +     * the given rate.
  1.1294 +     */
  1.1295 +    for (Q = 0; Q < MAXQ; Q++)
  1.1296 +    {
  1.1297 +        /* Error per MB based correction factor */
  1.1298 +        err_correction_factor =
  1.1299 +            calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q);
  1.1300 +
  1.1301 +        bits_per_mb_at_this_q =
  1.1302 +            (int)(.5 + ( err_correction_factor *
  1.1303 +                         combined_correction_factor *
  1.1304 +                         (double)vp8_bits_per_mb[INTER_FRAME][Q]) );
  1.1305 +
  1.1306 +        if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
  1.1307 +            break;
  1.1308 +    }
  1.1309 +
  1.1310 +    /* If we could not hit the target even at Max Q then estimate what Q
  1.1311 +     * would have been required
  1.1312 +     */
  1.1313 +    while ((bits_per_mb_at_this_q > target_norm_bits_per_mb)  && (Q < (MAXQ * 2)))
  1.1314 +    {
  1.1315 +
  1.1316 +        bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
  1.1317 +        Q++;
  1.1318 +    }
  1.1319 +
  1.1320 +    if (0)
  1.1321 +    {
  1.1322 +        FILE *f = fopen("estkf_q.stt", "a");
  1.1323 +        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,
  1.1324 +                target_norm_bits_per_mb, err_per_mb, err_correction_factor,
  1.1325 +                current_spend_ratio, group_iiratio, iiratio_correction_factor,
  1.1326 +                (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
  1.1327 +        fclose(f);
  1.1328 +    }
  1.1329 +
  1.1330 +    return Q;
  1.1331 +}
  1.1332 +
  1.1333 +extern void vp8_new_framerate(VP8_COMP *cpi, double framerate);
  1.1334 +
  1.1335 +void vp8_init_second_pass(VP8_COMP *cpi)
  1.1336 +{
  1.1337 +    FIRSTPASS_STATS this_frame;
  1.1338 +    FIRSTPASS_STATS *start_pos;
  1.1339 +
  1.1340 +    double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
  1.1341 +
  1.1342 +    zero_stats(&cpi->twopass.total_stats);
  1.1343 +    zero_stats(&cpi->twopass.total_left_stats);
  1.1344 +
  1.1345 +    if (!cpi->twopass.stats_in_end)
  1.1346 +        return;
  1.1347 +
  1.1348 +    cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
  1.1349 +    cpi->twopass.total_left_stats = cpi->twopass.total_stats;
  1.1350 +
  1.1351 +    /* each frame can have a different duration, as the frame rate in the
  1.1352 +     * source isn't guaranteed to be constant.   The frame rate prior to
  1.1353 +     * the first frame encoded in the second pass is a guess.  However the
  1.1354 +     * sum duration is not. Its calculated based on the actual durations of
  1.1355 +     * all frames from the first pass.
  1.1356 +     */
  1.1357 +    vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count / cpi->twopass.total_stats.duration);
  1.1358 +
  1.1359 +    cpi->output_framerate = cpi->framerate;
  1.1360 +    cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
  1.1361 +    cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration * two_pass_min_rate / 10000000.0);
  1.1362 +
  1.1363 +    /* Calculate a minimum intra value to be used in determining the IIratio
  1.1364 +     * scores used in the second pass. We have this minimum to make sure
  1.1365 +     * that clips that are static but "low complexity" in the intra domain
  1.1366 +     * are still boosted appropriately for KF/GF/ARF
  1.1367 +     */
  1.1368 +    cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
  1.1369 +    cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
  1.1370 +
  1.1371 +    /* Scan the first pass file and calculate an average Intra / Inter error
  1.1372 +     * score ratio for the sequence
  1.1373 +     */
  1.1374 +    {
  1.1375 +        double sum_iiratio = 0.0;
  1.1376 +        double IIRatio;
  1.1377 +
  1.1378 +        start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
  1.1379 +
  1.1380 +        while (input_stats(cpi, &this_frame) != EOF)
  1.1381 +        {
  1.1382 +            IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
  1.1383 +            IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
  1.1384 +            sum_iiratio += IIRatio;
  1.1385 +        }
  1.1386 +
  1.1387 +        cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
  1.1388 +
  1.1389 +        /* Reset file position */
  1.1390 +        reset_fpf_position(cpi, start_pos);
  1.1391 +    }
  1.1392 +
  1.1393 +    /* Scan the first pass file and calculate a modified total error based
  1.1394 +     * upon the bias/power function used to allocate bits
  1.1395 +     */
  1.1396 +    {
  1.1397 +        start_pos = cpi->twopass.stats_in;  /* Note starting "file" position */
  1.1398 +
  1.1399 +        cpi->twopass.modified_error_total = 0.0;
  1.1400 +        cpi->twopass.modified_error_used = 0.0;
  1.1401 +
  1.1402 +        while (input_stats(cpi, &this_frame) != EOF)
  1.1403 +        {
  1.1404 +            cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame);
  1.1405 +        }
  1.1406 +        cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
  1.1407 +
  1.1408 +        reset_fpf_position(cpi, start_pos);  /* Reset file position */
  1.1409 +
  1.1410 +    }
  1.1411 +}
  1.1412 +
  1.1413 +void vp8_end_second_pass(VP8_COMP *cpi)
  1.1414 +{
  1.1415 +}
  1.1416 +
  1.1417 +/* This function gives and estimate of how badly we believe the prediction
  1.1418 + * quality is decaying from frame to frame.
  1.1419 + */
  1.1420 +static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
  1.1421 +{
  1.1422 +    double prediction_decay_rate;
  1.1423 +    double motion_decay;
  1.1424 +    double motion_pct = next_frame->pcnt_motion;
  1.1425 +
  1.1426 +    /* Initial basis is the % mbs inter coded */
  1.1427 +    prediction_decay_rate = next_frame->pcnt_inter;
  1.1428 +
  1.1429 +    /* High % motion -> somewhat higher decay rate */
  1.1430 +    motion_decay = (1.0 - (motion_pct / 20.0));
  1.1431 +    if (motion_decay < prediction_decay_rate)
  1.1432 +        prediction_decay_rate = motion_decay;
  1.1433 +
  1.1434 +    /* Adjustment to decay rate based on speed of motion */
  1.1435 +    {
  1.1436 +        double this_mv_rabs;
  1.1437 +        double this_mv_cabs;
  1.1438 +        double distance_factor;
  1.1439 +
  1.1440 +        this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
  1.1441 +        this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
  1.1442 +
  1.1443 +        distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
  1.1444 +                               (this_mv_cabs * this_mv_cabs)) / 250.0;
  1.1445 +        distance_factor = ((distance_factor > 1.0)
  1.1446 +                                ? 0.0 : (1.0 - distance_factor));
  1.1447 +        if (distance_factor < prediction_decay_rate)
  1.1448 +            prediction_decay_rate = distance_factor;
  1.1449 +    }
  1.1450 +
  1.1451 +    return prediction_decay_rate;
  1.1452 +}
  1.1453 +
  1.1454 +/* Function to test for a condition where a complex transition is followed
  1.1455 + * by a static section. For example in slide shows where there is a fade
  1.1456 + * between slides. This is to help with more optimal kf and gf positioning.
  1.1457 + */
  1.1458 +static int detect_transition_to_still(
  1.1459 +    VP8_COMP *cpi,
  1.1460 +    int frame_interval,
  1.1461 +    int still_interval,
  1.1462 +    double loop_decay_rate,
  1.1463 +    double decay_accumulator )
  1.1464 +{
  1.1465 +    int trans_to_still = 0;
  1.1466 +
  1.1467 +    /* Break clause to detect very still sections after motion
  1.1468 +     * For example a static image after a fade or other transition
  1.1469 +     * instead of a clean scene cut.
  1.1470 +     */
  1.1471 +    if ( (frame_interval > MIN_GF_INTERVAL) &&
  1.1472 +         (loop_decay_rate >= 0.999) &&
  1.1473 +         (decay_accumulator < 0.9) )
  1.1474 +    {
  1.1475 +        int j;
  1.1476 +        FIRSTPASS_STATS * position = cpi->twopass.stats_in;
  1.1477 +        FIRSTPASS_STATS tmp_next_frame;
  1.1478 +        double decay_rate;
  1.1479 +
  1.1480 +        /* Look ahead a few frames to see if static condition persists... */
  1.1481 +        for ( j = 0; j < still_interval; j++ )
  1.1482 +        {
  1.1483 +            if (EOF == input_stats(cpi, &tmp_next_frame))
  1.1484 +                break;
  1.1485 +
  1.1486 +            decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
  1.1487 +            if ( decay_rate < 0.999 )
  1.1488 +                break;
  1.1489 +        }
  1.1490 +        /* Reset file position */
  1.1491 +        reset_fpf_position(cpi, position);
  1.1492 +
  1.1493 +        /* Only if it does do we signal a transition to still */
  1.1494 +        if ( j == still_interval )
  1.1495 +            trans_to_still = 1;
  1.1496 +    }
  1.1497 +
  1.1498 +    return trans_to_still;
  1.1499 +}
  1.1500 +
  1.1501 +/* This function detects a flash through the high relative pcnt_second_ref
  1.1502 + * score in the frame following a flash frame. The offset passed in should
  1.1503 + * reflect this
  1.1504 + */
  1.1505 +static int detect_flash( VP8_COMP *cpi, int offset )
  1.1506 +{
  1.1507 +    FIRSTPASS_STATS next_frame;
  1.1508 +
  1.1509 +    int flash_detected = 0;
  1.1510 +
  1.1511 +    /* Read the frame data. */
  1.1512 +    /* The return is 0 (no flash detected) if not a valid frame */
  1.1513 +    if ( read_frame_stats(cpi, &next_frame, offset) != EOF )
  1.1514 +    {
  1.1515 +        /* What we are looking for here is a situation where there is a
  1.1516 +         * brief break in prediction (such as a flash) but subsequent frames
  1.1517 +         * are reasonably well predicted by an earlier (pre flash) frame.
  1.1518 +         * The recovery after a flash is indicated by a high pcnt_second_ref
  1.1519 +         * comapred to pcnt_inter.
  1.1520 +         */
  1.1521 +        if ( (next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
  1.1522 +             (next_frame.pcnt_second_ref >= 0.5 ) )
  1.1523 +        {
  1.1524 +            flash_detected = 1;
  1.1525 +
  1.1526 +            /*if (1)
  1.1527 +            {
  1.1528 +                FILE *f = fopen("flash.stt", "a");
  1.1529 +                fprintf(f, "%8.0f %6.2f %6.2f\n",
  1.1530 +                    next_frame.frame,
  1.1531 +                    next_frame.pcnt_inter,
  1.1532 +                    next_frame.pcnt_second_ref);
  1.1533 +                fclose(f);
  1.1534 +            }*/
  1.1535 +        }
  1.1536 +    }
  1.1537 +
  1.1538 +    return flash_detected;
  1.1539 +}
  1.1540 +
  1.1541 +/* Update the motion related elements to the GF arf boost calculation */
  1.1542 +static void accumulate_frame_motion_stats(
  1.1543 +    VP8_COMP *cpi,
  1.1544 +    FIRSTPASS_STATS * this_frame,
  1.1545 +    double * this_frame_mv_in_out,
  1.1546 +    double * mv_in_out_accumulator,
  1.1547 +    double * abs_mv_in_out_accumulator,
  1.1548 +    double * mv_ratio_accumulator )
  1.1549 +{
  1.1550 +    double this_frame_mvr_ratio;
  1.1551 +    double this_frame_mvc_ratio;
  1.1552 +    double motion_pct;
  1.1553 +
  1.1554 +    /* Accumulate motion stats. */
  1.1555 +    motion_pct = this_frame->pcnt_motion;
  1.1556 +
  1.1557 +    /* Accumulate Motion In/Out of frame stats */
  1.1558 +    *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
  1.1559 +    *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
  1.1560 +    *abs_mv_in_out_accumulator +=
  1.1561 +        fabs(this_frame->mv_in_out_count * motion_pct);
  1.1562 +
  1.1563 +    /* Accumulate a measure of how uniform (or conversely how random)
  1.1564 +     * the motion field is. (A ratio of absmv / mv)
  1.1565 +     */
  1.1566 +    if (motion_pct > 0.05)
  1.1567 +    {
  1.1568 +        this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
  1.1569 +                               DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
  1.1570 +
  1.1571 +        this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
  1.1572 +                               DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
  1.1573 +
  1.1574 +         *mv_ratio_accumulator +=
  1.1575 +            (this_frame_mvr_ratio < this_frame->mvr_abs)
  1.1576 +                ? (this_frame_mvr_ratio * motion_pct)
  1.1577 +                : this_frame->mvr_abs * motion_pct;
  1.1578 +
  1.1579 +        *mv_ratio_accumulator +=
  1.1580 +            (this_frame_mvc_ratio < this_frame->mvc_abs)
  1.1581 +                ? (this_frame_mvc_ratio * motion_pct)
  1.1582 +                : this_frame->mvc_abs * motion_pct;
  1.1583 +
  1.1584 +    }
  1.1585 +}
  1.1586 +
  1.1587 +/* Calculate a baseline boost number for the current frame. */
  1.1588 +static double calc_frame_boost(
  1.1589 +    VP8_COMP *cpi,
  1.1590 +    FIRSTPASS_STATS * this_frame,
  1.1591 +    double this_frame_mv_in_out )
  1.1592 +{
  1.1593 +    double frame_boost;
  1.1594 +
  1.1595 +    /* Underlying boost factor is based on inter intra error ratio */
  1.1596 +    if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
  1.1597 +        frame_boost = (IIFACTOR * this_frame->intra_error /
  1.1598 +                      DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
  1.1599 +    else
  1.1600 +        frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
  1.1601 +                      DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
  1.1602 +
  1.1603 +    /* Increase boost for frames where new data coming into frame
  1.1604 +     * (eg zoom out). Slightly reduce boost if there is a net balance
  1.1605 +     * of motion out of the frame (zoom in).
  1.1606 +     * The range for this_frame_mv_in_out is -1.0 to +1.0
  1.1607 +     */
  1.1608 +    if (this_frame_mv_in_out > 0.0)
  1.1609 +        frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
  1.1610 +    /* In extreme case boost is halved */
  1.1611 +    else
  1.1612 +        frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
  1.1613 +
  1.1614 +    /* Clip to maximum */
  1.1615 +    if (frame_boost > GF_RMAX)
  1.1616 +        frame_boost = GF_RMAX;
  1.1617 +
  1.1618 +    return frame_boost;
  1.1619 +}
  1.1620 +
  1.1621 +#if NEW_BOOST
  1.1622 +static int calc_arf_boost(
  1.1623 +    VP8_COMP *cpi,
  1.1624 +    int offset,
  1.1625 +    int f_frames,
  1.1626 +    int b_frames,
  1.1627 +    int *f_boost,
  1.1628 +    int *b_boost )
  1.1629 +{
  1.1630 +    FIRSTPASS_STATS this_frame;
  1.1631 +
  1.1632 +    int i;
  1.1633 +    double boost_score = 0.0;
  1.1634 +    double mv_ratio_accumulator = 0.0;
  1.1635 +    double decay_accumulator = 1.0;
  1.1636 +    double this_frame_mv_in_out = 0.0;
  1.1637 +    double mv_in_out_accumulator = 0.0;
  1.1638 +    double abs_mv_in_out_accumulator = 0.0;
  1.1639 +    double r;
  1.1640 +    int flash_detected = 0;
  1.1641 +
  1.1642 +    /* Search forward from the proposed arf/next gf position */
  1.1643 +    for ( i = 0; i < f_frames; i++ )
  1.1644 +    {
  1.1645 +        if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
  1.1646 +            break;
  1.1647 +
  1.1648 +        /* Update the motion related elements to the boost calculation */
  1.1649 +        accumulate_frame_motion_stats( cpi, &this_frame,
  1.1650 +            &this_frame_mv_in_out, &mv_in_out_accumulator,
  1.1651 +            &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
  1.1652 +
  1.1653 +        /* Calculate the baseline boost number for this frame */
  1.1654 +        r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
  1.1655 +
  1.1656 +        /* We want to discount the the flash frame itself and the recovery
  1.1657 +         * frame that follows as both will have poor scores.
  1.1658 +         */
  1.1659 +        flash_detected = detect_flash(cpi, (i+offset)) ||
  1.1660 +                         detect_flash(cpi, (i+offset+1));
  1.1661 +
  1.1662 +        /* Cumulative effect of prediction quality decay */
  1.1663 +        if ( !flash_detected )
  1.1664 +        {
  1.1665 +            decay_accumulator =
  1.1666 +                decay_accumulator *
  1.1667 +                get_prediction_decay_rate(cpi, &this_frame);
  1.1668 +            decay_accumulator =
  1.1669 +                decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  1.1670 +        }
  1.1671 +        boost_score += (decay_accumulator * r);
  1.1672 +
  1.1673 +        /* Break out conditions. */
  1.1674 +        if  ( (!flash_detected) &&
  1.1675 +              ((mv_ratio_accumulator > 100.0) ||
  1.1676 +               (abs_mv_in_out_accumulator > 3.0) ||
  1.1677 +               (mv_in_out_accumulator < -2.0) ) )
  1.1678 +        {
  1.1679 +            break;
  1.1680 +        }
  1.1681 +    }
  1.1682 +
  1.1683 +    *f_boost = (int)(boost_score * 100.0) >> 4;
  1.1684 +
  1.1685 +    /* Reset for backward looking loop */
  1.1686 +    boost_score = 0.0;
  1.1687 +    mv_ratio_accumulator = 0.0;
  1.1688 +    decay_accumulator = 1.0;
  1.1689 +    this_frame_mv_in_out = 0.0;
  1.1690 +    mv_in_out_accumulator = 0.0;
  1.1691 +    abs_mv_in_out_accumulator = 0.0;
  1.1692 +
  1.1693 +    /* Search forward from the proposed arf/next gf position */
  1.1694 +    for ( i = -1; i >= -b_frames; i-- )
  1.1695 +    {
  1.1696 +        if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
  1.1697 +            break;
  1.1698 +
  1.1699 +        /* Update the motion related elements to the boost calculation */
  1.1700 +        accumulate_frame_motion_stats( cpi, &this_frame,
  1.1701 +            &this_frame_mv_in_out, &mv_in_out_accumulator,
  1.1702 +            &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
  1.1703 +
  1.1704 +        /* Calculate the baseline boost number for this frame */
  1.1705 +        r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
  1.1706 +
  1.1707 +        /* We want to discount the the flash frame itself and the recovery
  1.1708 +         * frame that follows as both will have poor scores.
  1.1709 +         */
  1.1710 +        flash_detected = detect_flash(cpi, (i+offset)) ||
  1.1711 +                         detect_flash(cpi, (i+offset+1));
  1.1712 +
  1.1713 +        /* Cumulative effect of prediction quality decay */
  1.1714 +        if ( !flash_detected )
  1.1715 +        {
  1.1716 +            decay_accumulator =
  1.1717 +                decay_accumulator *
  1.1718 +                get_prediction_decay_rate(cpi, &this_frame);
  1.1719 +            decay_accumulator =
  1.1720 +                decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  1.1721 +        }
  1.1722 +
  1.1723 +        boost_score += (decay_accumulator * r);
  1.1724 +
  1.1725 +        /* Break out conditions. */
  1.1726 +        if  ( (!flash_detected) &&
  1.1727 +              ((mv_ratio_accumulator > 100.0) ||
  1.1728 +               (abs_mv_in_out_accumulator > 3.0) ||
  1.1729 +               (mv_in_out_accumulator < -2.0) ) )
  1.1730 +        {
  1.1731 +            break;
  1.1732 +        }
  1.1733 +    }
  1.1734 +    *b_boost = (int)(boost_score * 100.0) >> 4;
  1.1735 +
  1.1736 +    return (*f_boost + *b_boost);
  1.1737 +}
  1.1738 +#endif
  1.1739 +
  1.1740 +/* Analyse and define a gf/arf group . */
  1.1741 +static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
  1.1742 +{
  1.1743 +    FIRSTPASS_STATS next_frame;
  1.1744 +    FIRSTPASS_STATS *start_pos;
  1.1745 +    int i;
  1.1746 +    double r;
  1.1747 +    double boost_score = 0.0;
  1.1748 +    double old_boost_score = 0.0;
  1.1749 +    double gf_group_err = 0.0;
  1.1750 +    double gf_first_frame_err = 0.0;
  1.1751 +    double mod_frame_err = 0.0;
  1.1752 +
  1.1753 +    double mv_ratio_accumulator = 0.0;
  1.1754 +    double decay_accumulator = 1.0;
  1.1755 +
  1.1756 +    double loop_decay_rate = 1.00;          /* Starting decay rate */
  1.1757 +
  1.1758 +    double this_frame_mv_in_out = 0.0;
  1.1759 +    double mv_in_out_accumulator = 0.0;
  1.1760 +    double abs_mv_in_out_accumulator = 0.0;
  1.1761 +    double mod_err_per_mb_accumulator = 0.0;
  1.1762 +
  1.1763 +    int max_bits = frame_max_bits(cpi);     /* Max for a single frame */
  1.1764 +
  1.1765 +    unsigned int allow_alt_ref =
  1.1766 +                    cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
  1.1767 +
  1.1768 +    int alt_boost = 0;
  1.1769 +    int f_boost = 0;
  1.1770 +    int b_boost = 0;
  1.1771 +    int flash_detected;
  1.1772 +
  1.1773 +    cpi->twopass.gf_group_bits = 0;
  1.1774 +    cpi->twopass.gf_decay_rate = 0;
  1.1775 +
  1.1776 +    vp8_clear_system_state();
  1.1777 +
  1.1778 +    start_pos = cpi->twopass.stats_in;
  1.1779 +
  1.1780 +    vpx_memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */
  1.1781 +
  1.1782 +    /* Load stats for the current frame. */
  1.1783 +    mod_frame_err = calculate_modified_err(cpi, this_frame);
  1.1784 +
  1.1785 +    /* Note the error of the frame at the start of the group (this will be
  1.1786 +     * the GF frame error if we code a normal gf
  1.1787 +     */
  1.1788 +    gf_first_frame_err = mod_frame_err;
  1.1789 +
  1.1790 +    /* Special treatment if the current frame is a key frame (which is also
  1.1791 +     * a gf). If it is then its error score (and hence bit allocation) need
  1.1792 +     * to be subtracted out from the calculation for the GF group
  1.1793 +     */
  1.1794 +    if (cpi->common.frame_type == KEY_FRAME)
  1.1795 +        gf_group_err -= gf_first_frame_err;
  1.1796 +
  1.1797 +    /* Scan forward to try and work out how many frames the next gf group
  1.1798 +     * should contain and what level of boost is appropriate for the GF
  1.1799 +     * or ARF that will be coded with the group
  1.1800 +     */
  1.1801 +    i = 0;
  1.1802 +
  1.1803 +    while (((i < cpi->twopass.static_scene_max_gf_interval) ||
  1.1804 +            ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
  1.1805 +           (i < cpi->twopass.frames_to_key))
  1.1806 +    {
  1.1807 +        i++;
  1.1808 +
  1.1809 +        /* Accumulate error score of frames in this gf group */
  1.1810 +        mod_frame_err = calculate_modified_err(cpi, this_frame);
  1.1811 +
  1.1812 +        gf_group_err += mod_frame_err;
  1.1813 +
  1.1814 +        mod_err_per_mb_accumulator +=
  1.1815 +            mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
  1.1816 +
  1.1817 +        if (EOF == input_stats(cpi, &next_frame))
  1.1818 +            break;
  1.1819 +
  1.1820 +        /* Test for the case where there is a brief flash but the prediction
  1.1821 +         * quality back to an earlier frame is then restored.
  1.1822 +         */
  1.1823 +        flash_detected = detect_flash(cpi, 0);
  1.1824 +
  1.1825 +        /* Update the motion related elements to the boost calculation */
  1.1826 +        accumulate_frame_motion_stats( cpi, &next_frame,
  1.1827 +            &this_frame_mv_in_out, &mv_in_out_accumulator,
  1.1828 +            &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
  1.1829 +
  1.1830 +        /* Calculate a baseline boost number for this frame */
  1.1831 +        r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out );
  1.1832 +
  1.1833 +        /* Cumulative effect of prediction quality decay */
  1.1834 +        if ( !flash_detected )
  1.1835 +        {
  1.1836 +            loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  1.1837 +            decay_accumulator = decay_accumulator * loop_decay_rate;
  1.1838 +            decay_accumulator =
  1.1839 +                decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  1.1840 +        }
  1.1841 +        boost_score += (decay_accumulator * r);
  1.1842 +
  1.1843 +        /* Break clause to detect very still sections after motion
  1.1844 +         * For example a staic image after a fade or other transition.
  1.1845 +         */
  1.1846 +        if ( detect_transition_to_still( cpi, i, 5,
  1.1847 +                                         loop_decay_rate,
  1.1848 +                                         decay_accumulator ) )
  1.1849 +        {
  1.1850 +            allow_alt_ref = 0;
  1.1851 +            boost_score = old_boost_score;
  1.1852 +            break;
  1.1853 +        }
  1.1854 +
  1.1855 +        /* Break out conditions. */
  1.1856 +        if  (
  1.1857 +            /* Break at cpi->max_gf_interval unless almost totally static */
  1.1858 +            (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
  1.1859 +            (
  1.1860 +                /* Dont break out with a very short interval */
  1.1861 +                (i > MIN_GF_INTERVAL) &&
  1.1862 +                /* Dont break out very close to a key frame */
  1.1863 +                ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
  1.1864 +                ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
  1.1865 +                (!flash_detected) &&
  1.1866 +                ((mv_ratio_accumulator > 100.0) ||
  1.1867 +                 (abs_mv_in_out_accumulator > 3.0) ||
  1.1868 +                 (mv_in_out_accumulator < -2.0) ||
  1.1869 +                 ((boost_score - old_boost_score) < 2.0))
  1.1870 +            ) )
  1.1871 +        {
  1.1872 +            boost_score = old_boost_score;
  1.1873 +            break;
  1.1874 +        }
  1.1875 +
  1.1876 +        vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
  1.1877 +
  1.1878 +        old_boost_score = boost_score;
  1.1879 +    }
  1.1880 +
  1.1881 +    cpi->twopass.gf_decay_rate =
  1.1882 +        (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
  1.1883 +
  1.1884 +    /* When using CBR apply additional buffer related upper limits */
  1.1885 +    if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
  1.1886 +    {
  1.1887 +        double max_boost;
  1.1888 +
  1.1889 +        /* For cbr apply buffer related limits */
  1.1890 +        if (cpi->drop_frames_allowed)
  1.1891 +        {
  1.1892 +            int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark *
  1.1893 +                                  (cpi->oxcf.optimal_buffer_level / 100);
  1.1894 +
  1.1895 +            if (cpi->buffer_level > df_buffer_level)
  1.1896 +                max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  1.1897 +            else
  1.1898 +                max_boost = 0.0;
  1.1899 +        }
  1.1900 +        else if (cpi->buffer_level > 0)
  1.1901 +        {
  1.1902 +            max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  1.1903 +        }
  1.1904 +        else
  1.1905 +        {
  1.1906 +            max_boost = 0.0;
  1.1907 +        }
  1.1908 +
  1.1909 +        if (boost_score > max_boost)
  1.1910 +            boost_score = max_boost;
  1.1911 +    }
  1.1912 +
  1.1913 +    /* Dont allow conventional gf too near the next kf */
  1.1914 +    if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)
  1.1915 +    {
  1.1916 +        while (i < cpi->twopass.frames_to_key)
  1.1917 +        {
  1.1918 +            i++;
  1.1919 +
  1.1920 +            if (EOF == input_stats(cpi, this_frame))
  1.1921 +                break;
  1.1922 +
  1.1923 +            if (i < cpi->twopass.frames_to_key)
  1.1924 +            {
  1.1925 +                mod_frame_err = calculate_modified_err(cpi, this_frame);
  1.1926 +                gf_group_err += mod_frame_err;
  1.1927 +            }
  1.1928 +        }
  1.1929 +    }
  1.1930 +
  1.1931 +    cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
  1.1932 +
  1.1933 +#if NEW_BOOST
  1.1934 +    /* Alterrnative boost calculation for alt ref */
  1.1935 +    alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost );
  1.1936 +#endif
  1.1937 +
  1.1938 +    /* Should we use the alternate refernce frame */
  1.1939 +    if (allow_alt_ref &&
  1.1940 +        (i >= MIN_GF_INTERVAL) &&
  1.1941 +        /* dont use ARF very near next kf */
  1.1942 +        (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
  1.1943 +#if NEW_BOOST
  1.1944 +        ((next_frame.pcnt_inter > 0.75) ||
  1.1945 +         (next_frame.pcnt_second_ref > 0.5)) &&
  1.1946 +        ((mv_in_out_accumulator / (double)i > -0.2) ||
  1.1947 +         (mv_in_out_accumulator > -2.0)) &&
  1.1948 +        (b_boost > 100) &&
  1.1949 +        (f_boost > 100) )
  1.1950 +#else
  1.1951 +        (next_frame.pcnt_inter > 0.75) &&
  1.1952 +        ((mv_in_out_accumulator / (double)i > -0.2) ||
  1.1953 +         (mv_in_out_accumulator > -2.0)) &&
  1.1954 +        (cpi->gfu_boost > 100) &&
  1.1955 +        (cpi->twopass.gf_decay_rate <=
  1.1956 +            (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) )
  1.1957 +#endif
  1.1958 +    {
  1.1959 +        int Boost;
  1.1960 +        int allocation_chunks;
  1.1961 +        int Q = (cpi->oxcf.fixed_q < 0)
  1.1962 +                ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
  1.1963 +        int tmp_q;
  1.1964 +        int arf_frame_bits = 0;
  1.1965 +        int group_bits;
  1.1966 +
  1.1967 +#if NEW_BOOST
  1.1968 +        cpi->gfu_boost = alt_boost;
  1.1969 +#endif
  1.1970 +
  1.1971 +        /* Estimate the bits to be allocated to the group as a whole */
  1.1972 +        if ((cpi->twopass.kf_group_bits > 0) &&
  1.1973 +            (cpi->twopass.kf_group_error_left > 0))
  1.1974 +        {
  1.1975 +            group_bits = (int)((double)cpi->twopass.kf_group_bits *
  1.1976 +                (gf_group_err / (double)cpi->twopass.kf_group_error_left));
  1.1977 +        }
  1.1978 +        else
  1.1979 +            group_bits = 0;
  1.1980 +
  1.1981 +        /* Boost for arf frame */
  1.1982 +#if NEW_BOOST
  1.1983 +        Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
  1.1984 +#else
  1.1985 +        Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
  1.1986 +#endif
  1.1987 +        Boost += (i * 50);
  1.1988 +
  1.1989 +        /* Set max and minimum boost and hence minimum allocation */
  1.1990 +        if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
  1.1991 +            Boost = ((cpi->baseline_gf_interval + 1) * 200);
  1.1992 +        else if (Boost < 125)
  1.1993 +            Boost = 125;
  1.1994 +
  1.1995 +        allocation_chunks = (i * 100) + Boost;
  1.1996 +
  1.1997 +        /* Normalize Altboost and allocations chunck down to prevent overflow */
  1.1998 +        while (Boost > 1000)
  1.1999 +        {
  1.2000 +            Boost /= 2;
  1.2001 +            allocation_chunks /= 2;
  1.2002 +        }
  1.2003 +
  1.2004 +        /* Calculate the number of bits to be spent on the arf based on the
  1.2005 +         * boost number
  1.2006 +         */
  1.2007 +        arf_frame_bits = (int)((double)Boost * (group_bits /
  1.2008 +                               (double)allocation_chunks));
  1.2009 +
  1.2010 +        /* Estimate if there are enough bits available to make worthwhile use
  1.2011 +         * of an arf.
  1.2012 +         */
  1.2013 +        tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
  1.2014 +
  1.2015 +        /* Only use an arf if it is likely we will be able to code
  1.2016 +         * it at a lower Q than the surrounding frames.
  1.2017 +         */
  1.2018 +        if (tmp_q < cpi->worst_quality)
  1.2019 +        {
  1.2020 +            int half_gf_int;
  1.2021 +            int frames_after_arf;
  1.2022 +            int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
  1.2023 +            int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
  1.2024 +
  1.2025 +            cpi->source_alt_ref_pending = 1;
  1.2026 +
  1.2027 +            /*
  1.2028 +             * For alt ref frames the error score for the end frame of the
  1.2029 +             * group (the alt ref frame) should not contribute to the group
  1.2030 +             * total and hence the number of bit allocated to the group.
  1.2031 +             * Rather it forms part of the next group (it is the GF at the
  1.2032 +             * start of the next group)
  1.2033 +             * gf_group_err -= mod_frame_err;
  1.2034 +             *
  1.2035 +             * For alt ref frames alt ref frame is technically part of the
  1.2036 +             * GF frame for the next group but we always base the error
  1.2037 +             * calculation and bit allocation on the current group of frames.
  1.2038 +             *
  1.2039 +             * Set the interval till the next gf or arf.
  1.2040 +             * For ARFs this is the number of frames to be coded before the
  1.2041 +             * future frame that is coded as an ARF.
  1.2042 +             * The future frame itself is part of the next group
  1.2043 +             */
  1.2044 +            cpi->baseline_gf_interval = i;
  1.2045 +
  1.2046 +            /*
  1.2047 +             * Define the arnr filter width for this group of frames:
  1.2048 +             * We only filter frames that lie within a distance of half
  1.2049 +             * the GF interval from the ARF frame. We also have to trap
  1.2050 +             * cases where the filter extends beyond the end of clip.
  1.2051 +             * Note: this_frame->frame has been updated in the loop
  1.2052 +             * so it now points at the ARF frame.
  1.2053 +             */
  1.2054 +            half_gf_int = cpi->baseline_gf_interval >> 1;
  1.2055 +            frames_after_arf = (int)(cpi->twopass.total_stats.count -
  1.2056 +                               this_frame->frame - 1);
  1.2057 +
  1.2058 +            switch (cpi->oxcf.arnr_type)
  1.2059 +            {
  1.2060 +            case 1: /* Backward filter */
  1.2061 +                frames_fwd = 0;
  1.2062 +                if (frames_bwd > half_gf_int)
  1.2063 +                    frames_bwd = half_gf_int;
  1.2064 +                break;
  1.2065 +
  1.2066 +            case 2: /* Forward filter */
  1.2067 +                if (frames_fwd > half_gf_int)
  1.2068 +                    frames_fwd = half_gf_int;
  1.2069 +                if (frames_fwd > frames_after_arf)
  1.2070 +                    frames_fwd = frames_after_arf;
  1.2071 +                frames_bwd = 0;
  1.2072 +                break;
  1.2073 +
  1.2074 +            case 3: /* Centered filter */
  1.2075 +            default:
  1.2076 +                frames_fwd >>= 1;
  1.2077 +                if (frames_fwd > frames_after_arf)
  1.2078 +                    frames_fwd = frames_after_arf;
  1.2079 +                if (frames_fwd > half_gf_int)
  1.2080 +                    frames_fwd = half_gf_int;
  1.2081 +
  1.2082 +                frames_bwd = frames_fwd;
  1.2083 +
  1.2084 +                /* For even length filter there is one more frame backward
  1.2085 +                 * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
  1.2086 +                 */
  1.2087 +                if (frames_bwd < half_gf_int)
  1.2088 +                    frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
  1.2089 +                break;
  1.2090 +            }
  1.2091 +
  1.2092 +            cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
  1.2093 +        }
  1.2094 +        else
  1.2095 +        {
  1.2096 +            cpi->source_alt_ref_pending = 0;
  1.2097 +            cpi->baseline_gf_interval = i;
  1.2098 +        }
  1.2099 +    }
  1.2100 +    else
  1.2101 +    {
  1.2102 +        cpi->source_alt_ref_pending = 0;
  1.2103 +        cpi->baseline_gf_interval = i;
  1.2104 +    }
  1.2105 +
  1.2106 +    /*
  1.2107 +     * Now decide how many bits should be allocated to the GF group as  a
  1.2108 +     * proportion of those remaining in the kf group.
  1.2109 +     * The final key frame group in the clip is treated as a special case
  1.2110 +     * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
  1.2111 +     * This is also important for short clips where there may only be one
  1.2112 +     * key frame.
  1.2113 +     */
  1.2114 +    if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count -
  1.2115 +                                            cpi->common.current_video_frame))
  1.2116 +    {
  1.2117 +        cpi->twopass.kf_group_bits =
  1.2118 +            (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
  1.2119 +    }
  1.2120 +
  1.2121 +    /* Calculate the bits to be allocated to the group as a whole */
  1.2122 +    if ((cpi->twopass.kf_group_bits > 0) &&
  1.2123 +        (cpi->twopass.kf_group_error_left > 0))
  1.2124 +    {
  1.2125 +        cpi->twopass.gf_group_bits =
  1.2126 +            (int64_t)(cpi->twopass.kf_group_bits *
  1.2127 +                      (gf_group_err / cpi->twopass.kf_group_error_left));
  1.2128 +    }
  1.2129 +    else
  1.2130 +        cpi->twopass.gf_group_bits = 0;
  1.2131 +
  1.2132 +    cpi->twopass.gf_group_bits =
  1.2133 +        (cpi->twopass.gf_group_bits < 0)
  1.2134 +            ? 0
  1.2135 +            : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
  1.2136 +                ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
  1.2137 +
  1.2138 +    /* Clip cpi->twopass.gf_group_bits based on user supplied data rate
  1.2139 +     * variability limit (cpi->oxcf.two_pass_vbrmax_section)
  1.2140 +     */
  1.2141 +    if (cpi->twopass.gf_group_bits >
  1.2142 +        (int64_t)max_bits * cpi->baseline_gf_interval)
  1.2143 +        cpi->twopass.gf_group_bits =
  1.2144 +            (int64_t)max_bits * cpi->baseline_gf_interval;
  1.2145 +
  1.2146 +    /* Reset the file position */
  1.2147 +    reset_fpf_position(cpi, start_pos);
  1.2148 +
  1.2149 +    /* Update the record of error used so far (only done once per gf group) */
  1.2150 +    cpi->twopass.modified_error_used += gf_group_err;
  1.2151 +
  1.2152 +    /* Assign  bits to the arf or gf. */
  1.2153 +    for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) {
  1.2154 +        int Boost;
  1.2155 +        int allocation_chunks;
  1.2156 +        int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
  1.2157 +        int gf_bits;
  1.2158 +
  1.2159 +        /* For ARF frames */
  1.2160 +        if (cpi->source_alt_ref_pending && i == 0)
  1.2161 +        {
  1.2162 +#if NEW_BOOST
  1.2163 +            Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
  1.2164 +#else
  1.2165 +            Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
  1.2166 +#endif
  1.2167 +            Boost += (cpi->baseline_gf_interval * 50);
  1.2168 +
  1.2169 +            /* Set max and minimum boost and hence minimum allocation */
  1.2170 +            if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
  1.2171 +                Boost = ((cpi->baseline_gf_interval + 1) * 200);
  1.2172 +            else if (Boost < 125)
  1.2173 +                Boost = 125;
  1.2174 +
  1.2175 +            allocation_chunks =
  1.2176 +                ((cpi->baseline_gf_interval + 1) * 100) + Boost;
  1.2177 +        }
  1.2178 +        /* Else for standard golden frames */
  1.2179 +        else
  1.2180 +        {
  1.2181 +            /* boost based on inter / intra ratio of subsequent frames */
  1.2182 +            Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
  1.2183 +
  1.2184 +            /* Set max and minimum boost and hence minimum allocation */
  1.2185 +            if (Boost > (cpi->baseline_gf_interval * 150))
  1.2186 +                Boost = (cpi->baseline_gf_interval * 150);
  1.2187 +            else if (Boost < 125)
  1.2188 +                Boost = 125;
  1.2189 +
  1.2190 +            allocation_chunks =
  1.2191 +                (cpi->baseline_gf_interval * 100) + (Boost - 100);
  1.2192 +        }
  1.2193 +
  1.2194 +        /* Normalize Altboost and allocations chunck down to prevent overflow */
  1.2195 +        while (Boost > 1000)
  1.2196 +        {
  1.2197 +            Boost /= 2;
  1.2198 +            allocation_chunks /= 2;
  1.2199 +        }
  1.2200 +
  1.2201 +        /* Calculate the number of bits to be spent on the gf or arf based on
  1.2202 +         * the boost number
  1.2203 +         */
  1.2204 +        gf_bits = (int)((double)Boost *
  1.2205 +                        (cpi->twopass.gf_group_bits /
  1.2206 +                         (double)allocation_chunks));
  1.2207 +
  1.2208 +        /* If the frame that is to be boosted is simpler than the average for
  1.2209 +         * the gf/arf group then use an alternative calculation
  1.2210 +         * based on the error score of the frame itself
  1.2211 +         */
  1.2212 +        if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
  1.2213 +        {
  1.2214 +            double  alt_gf_grp_bits;
  1.2215 +            int     alt_gf_bits;
  1.2216 +
  1.2217 +            alt_gf_grp_bits =
  1.2218 +                (double)cpi->twopass.kf_group_bits  *
  1.2219 +                (mod_frame_err * (double)cpi->baseline_gf_interval) /
  1.2220 +                DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
  1.2221 +
  1.2222 +            alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
  1.2223 +                                                 (double)allocation_chunks));
  1.2224 +
  1.2225 +            if (gf_bits > alt_gf_bits)
  1.2226 +            {
  1.2227 +                gf_bits = alt_gf_bits;
  1.2228 +            }
  1.2229 +        }
  1.2230 +        /* Else if it is harder than other frames in the group make sure it at
  1.2231 +         * least receives an allocation in keeping with its relative error
  1.2232 +         * score, otherwise it may be worse off than an "un-boosted" frame
  1.2233 +         */
  1.2234 +        else
  1.2235 +        {
  1.2236 +            int alt_gf_bits =
  1.2237 +                (int)((double)cpi->twopass.kf_group_bits *
  1.2238 +                      mod_frame_err /
  1.2239 +                      DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
  1.2240 +
  1.2241 +            if (alt_gf_bits > gf_bits)
  1.2242 +            {
  1.2243 +                gf_bits = alt_gf_bits;
  1.2244 +            }
  1.2245 +        }
  1.2246 +
  1.2247 +        /* Apply an additional limit for CBR */
  1.2248 +        if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
  1.2249 +        {
  1.2250 +            if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1))
  1.2251 +                cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1);
  1.2252 +        }
  1.2253 +
  1.2254 +        /* Dont allow a negative value for gf_bits */
  1.2255 +        if (gf_bits < 0)
  1.2256 +            gf_bits = 0;
  1.2257 +
  1.2258 +        /* Add in minimum for a frame */
  1.2259 +        gf_bits += cpi->min_frame_bandwidth;
  1.2260 +
  1.2261 +        if (i == 0)
  1.2262 +        {
  1.2263 +            cpi->twopass.gf_bits = gf_bits;
  1.2264 +        }
  1.2265 +        if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)))
  1.2266 +        {
  1.2267 +            /* Per frame bit target for this frame */
  1.2268 +            cpi->per_frame_bandwidth = gf_bits;
  1.2269 +        }
  1.2270 +    }
  1.2271 +
  1.2272 +    {
  1.2273 +        /* Adjust KF group bits and error remainin */
  1.2274 +        cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
  1.2275 +        cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
  1.2276 +
  1.2277 +        if (cpi->twopass.kf_group_bits < 0)
  1.2278 +            cpi->twopass.kf_group_bits = 0;
  1.2279 +
  1.2280 +        /* Note the error score left in the remaining frames of the group.
  1.2281 +         * For normal GFs we want to remove the error score for the first
  1.2282 +         * frame of the group (except in Key frame case where this has
  1.2283 +         * already happened)
  1.2284 +         */
  1.2285 +        if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
  1.2286 +            cpi->twopass.gf_group_error_left = (int)(gf_group_err -
  1.2287 +                                                     gf_first_frame_err);
  1.2288 +        else
  1.2289 +            cpi->twopass.gf_group_error_left = (int) gf_group_err;
  1.2290 +
  1.2291 +        cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
  1.2292 +
  1.2293 +        if (cpi->twopass.gf_group_bits < 0)
  1.2294 +            cpi->twopass.gf_group_bits = 0;
  1.2295 +
  1.2296 +        /* This condition could fail if there are two kfs very close together
  1.2297 +         * despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the
  1.2298 +         * calculation of cpi->twopass.alt_extra_bits.
  1.2299 +         */
  1.2300 +        if ( cpi->baseline_gf_interval >= 3 )
  1.2301 +        {
  1.2302 +#if NEW_BOOST
  1.2303 +            int boost = (cpi->source_alt_ref_pending)
  1.2304 +                        ? b_boost : cpi->gfu_boost;
  1.2305 +#else
  1.2306 +            int boost = cpi->gfu_boost;
  1.2307 +#endif
  1.2308 +            if ( boost >= 150 )
  1.2309 +            {
  1.2310 +                int pct_extra;
  1.2311 +
  1.2312 +                pct_extra = (boost - 100) / 50;
  1.2313 +                pct_extra = (pct_extra > 20) ? 20 : pct_extra;
  1.2314 +
  1.2315 +                cpi->twopass.alt_extra_bits =
  1.2316 +                    (cpi->twopass.gf_group_bits * pct_extra) / 100;
  1.2317 +                cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
  1.2318 +                cpi->twopass.alt_extra_bits /=
  1.2319 +                    ((cpi->baseline_gf_interval-1)>>1);
  1.2320 +            }
  1.2321 +            else
  1.2322 +                cpi->twopass.alt_extra_bits = 0;
  1.2323 +        }
  1.2324 +        else
  1.2325 +            cpi->twopass.alt_extra_bits = 0;
  1.2326 +    }
  1.2327 +
  1.2328 +    /* Adjustments based on a measure of complexity of the section */
  1.2329 +    if (cpi->common.frame_type != KEY_FRAME)
  1.2330 +    {
  1.2331 +        FIRSTPASS_STATS sectionstats;
  1.2332 +        double Ratio;
  1.2333 +
  1.2334 +        zero_stats(&sectionstats);
  1.2335 +        reset_fpf_position(cpi, start_pos);
  1.2336 +
  1.2337 +        for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
  1.2338 +        {
  1.2339 +            input_stats(cpi, &next_frame);
  1.2340 +            accumulate_stats(&sectionstats, &next_frame);
  1.2341 +        }
  1.2342 +
  1.2343 +        avg_stats(&sectionstats);
  1.2344 +
  1.2345 +        cpi->twopass.section_intra_rating = (unsigned int)
  1.2346 +            (sectionstats.intra_error /
  1.2347 +            DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
  1.2348 +
  1.2349 +        Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
  1.2350 +        cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
  1.2351 +
  1.2352 +        if (cpi->twopass.section_max_qfactor < 0.80)
  1.2353 +            cpi->twopass.section_max_qfactor = 0.80;
  1.2354 +
  1.2355 +        reset_fpf_position(cpi, start_pos);
  1.2356 +    }
  1.2357 +}
  1.2358 +
  1.2359 +/* Allocate bits to a normal frame that is neither a gf an arf or a key frame. */
  1.2360 +static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
  1.2361 +{
  1.2362 +    int    target_frame_size;
  1.2363 +
  1.2364 +    double modified_err;
  1.2365 +    double err_fraction;
  1.2366 +
  1.2367 +    int max_bits = frame_max_bits(cpi);  /* Max for a single frame */
  1.2368 +
  1.2369 +    /* Calculate modified prediction error used in bit allocation */
  1.2370 +    modified_err = calculate_modified_err(cpi, this_frame);
  1.2371 +
  1.2372 +    /* What portion of the remaining GF group error is used by this frame */
  1.2373 +    if (cpi->twopass.gf_group_error_left > 0)
  1.2374 +        err_fraction = modified_err / cpi->twopass.gf_group_error_left;
  1.2375 +    else
  1.2376 +        err_fraction = 0.0;
  1.2377 +
  1.2378 +    /* How many of those bits available for allocation should we give it? */
  1.2379 +    target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
  1.2380 +
  1.2381 +    /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits)
  1.2382 +     * at the top end.
  1.2383 +     */
  1.2384 +    if (target_frame_size < 0)
  1.2385 +        target_frame_size = 0;
  1.2386 +    else
  1.2387 +    {
  1.2388 +        if (target_frame_size > max_bits)
  1.2389 +            target_frame_size = max_bits;
  1.2390 +
  1.2391 +        if (target_frame_size > cpi->twopass.gf_group_bits)
  1.2392 +            target_frame_size = cpi->twopass.gf_group_bits;
  1.2393 +    }
  1.2394 +
  1.2395 +    /* Adjust error and bits remaining */
  1.2396 +    cpi->twopass.gf_group_error_left -= (int)modified_err;
  1.2397 +    cpi->twopass.gf_group_bits -= target_frame_size;
  1.2398 +
  1.2399 +    if (cpi->twopass.gf_group_bits < 0)
  1.2400 +        cpi->twopass.gf_group_bits = 0;
  1.2401 +
  1.2402 +    /* Add in the minimum number of bits that is set aside for every frame. */
  1.2403 +    target_frame_size += cpi->min_frame_bandwidth;
  1.2404 +
  1.2405 +    /* Every other frame gets a few extra bits */
  1.2406 +    if ( (cpi->frames_since_golden & 0x01) &&
  1.2407 +         (cpi->frames_till_gf_update_due > 0) )
  1.2408 +    {
  1.2409 +        target_frame_size += cpi->twopass.alt_extra_bits;
  1.2410 +    }
  1.2411 +
  1.2412 +    /* Per frame bit target for this frame */
  1.2413 +    cpi->per_frame_bandwidth = target_frame_size;
  1.2414 +}
  1.2415 +
  1.2416 +void vp8_second_pass(VP8_COMP *cpi)
  1.2417 +{
  1.2418 +    int tmp_q;
  1.2419 +    int frames_left = (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
  1.2420 +
  1.2421 +    FIRSTPASS_STATS this_frame = {0};
  1.2422 +    FIRSTPASS_STATS this_frame_copy;
  1.2423 +
  1.2424 +    double this_frame_intra_error;
  1.2425 +    double this_frame_coded_error;
  1.2426 +
  1.2427 +    int overhead_bits;
  1.2428 +
  1.2429 +    if (!cpi->twopass.stats_in)
  1.2430 +    {
  1.2431 +        return ;
  1.2432 +    }
  1.2433 +
  1.2434 +    vp8_clear_system_state();
  1.2435 +
  1.2436 +    if (EOF == input_stats(cpi, &this_frame))
  1.2437 +        return;
  1.2438 +
  1.2439 +    this_frame_intra_error = this_frame.intra_error;
  1.2440 +    this_frame_coded_error = this_frame.coded_error;
  1.2441 +
  1.2442 +    /* keyframe and section processing ! */
  1.2443 +    if (cpi->twopass.frames_to_key == 0)
  1.2444 +    {
  1.2445 +        /* Define next KF group and assign bits to it */
  1.2446 +        vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1.2447 +        find_next_key_frame(cpi, &this_frame_copy);
  1.2448 +
  1.2449 +        /* Special case: Error error_resilient_mode mode does not make much
  1.2450 +         * sense for two pass but with its current meaning but this code is
  1.2451 +         * designed to stop outlandish behaviour if someone does set it when
  1.2452 +         * using two pass. It effectively disables GF groups. This is
  1.2453 +         * temporary code till we decide what should really happen in this
  1.2454 +         * case.
  1.2455 +         */
  1.2456 +        if (cpi->oxcf.error_resilient_mode)
  1.2457 +        {
  1.2458 +            cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
  1.2459 +            cpi->twopass.gf_group_error_left =
  1.2460 +                                  (int)cpi->twopass.kf_group_error_left;
  1.2461 +            cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
  1.2462 +            cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  1.2463 +            cpi->source_alt_ref_pending = 0;
  1.2464 +        }
  1.2465 +
  1.2466 +    }
  1.2467 +
  1.2468 +    /* Is this a GF / ARF (Note that a KF is always also a GF) */
  1.2469 +    if (cpi->frames_till_gf_update_due == 0)
  1.2470 +    {
  1.2471 +        /* Define next gf group and assign bits to it */
  1.2472 +        vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1.2473 +        define_gf_group(cpi, &this_frame_copy);
  1.2474 +
  1.2475 +        /* If we are going to code an altref frame at the end of the group
  1.2476 +         * and the current frame is not a key frame.... If the previous
  1.2477 +         * group used an arf this frame has already benefited from that arf
  1.2478 +         * boost and it should not be given extra bits If the previous
  1.2479 +         * group was NOT coded using arf we may want to apply some boost to
  1.2480 +         * this GF as well
  1.2481 +         */
  1.2482 +        if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
  1.2483 +        {
  1.2484 +            /* Assign a standard frames worth of bits from those allocated
  1.2485 +             * to the GF group
  1.2486 +             */
  1.2487 +            int bak = cpi->per_frame_bandwidth;
  1.2488 +            vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1.2489 +            assign_std_frame_bits(cpi, &this_frame_copy);
  1.2490 +            cpi->per_frame_bandwidth = bak;
  1.2491 +        }
  1.2492 +    }
  1.2493 +
  1.2494 +    /* Otherwise this is an ordinary frame */
  1.2495 +    else
  1.2496 +    {
  1.2497 +        /* Special case: Error error_resilient_mode mode does not make much
  1.2498 +         * sense for two pass but with its current meaning but this code is
  1.2499 +         * designed to stop outlandish behaviour if someone does set it
  1.2500 +         * when using two pass. It effectively disables GF groups. This is
  1.2501 +         * temporary code till we decide what should really happen in this
  1.2502 +         * case.
  1.2503 +         */
  1.2504 +        if (cpi->oxcf.error_resilient_mode)
  1.2505 +        {
  1.2506 +            cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
  1.2507 +
  1.2508 +            if (cpi->common.frame_type != KEY_FRAME)
  1.2509 +            {
  1.2510 +                /* Assign bits from those allocated to the GF group */
  1.2511 +                vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1.2512 +                assign_std_frame_bits(cpi, &this_frame_copy);
  1.2513 +            }
  1.2514 +        }
  1.2515 +        else
  1.2516 +        {
  1.2517 +            /* Assign bits from those allocated to the GF group */
  1.2518 +            vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1.2519 +            assign_std_frame_bits(cpi, &this_frame_copy);
  1.2520 +        }
  1.2521 +    }
  1.2522 +
  1.2523 +    /* Keep a globally available copy of this and the next frame's iiratio. */
  1.2524 +    cpi->twopass.this_iiratio = (unsigned int)(this_frame_intra_error /
  1.2525 +                        DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
  1.2526 +    {
  1.2527 +        FIRSTPASS_STATS next_frame;
  1.2528 +        if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
  1.2529 +        {
  1.2530 +            cpi->twopass.next_iiratio = (unsigned int)(next_frame.intra_error /
  1.2531 +                                DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  1.2532 +        }
  1.2533 +    }
  1.2534 +
  1.2535 +    /* Set nominal per second bandwidth for this frame */
  1.2536 +    cpi->target_bandwidth = (int)
  1.2537 +    (cpi->per_frame_bandwidth * cpi->output_framerate);
  1.2538 +    if (cpi->target_bandwidth < 0)
  1.2539 +        cpi->target_bandwidth = 0;
  1.2540 +
  1.2541 +
  1.2542 +    /* Account for mv, mode and other overheads. */
  1.2543 +    overhead_bits = (int)estimate_modemvcost(
  1.2544 +                        cpi, &cpi->twopass.total_left_stats );
  1.2545 +
  1.2546 +    /* Special case code for first frame. */
  1.2547 +    if (cpi->common.current_video_frame == 0)
  1.2548 +    {
  1.2549 +        cpi->twopass.est_max_qcorrection_factor = 1.0;
  1.2550 +
  1.2551 +        /* Set a cq_level in constrained quality mode. */
  1.2552 +        if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
  1.2553 +        {
  1.2554 +            int est_cq;
  1.2555 +
  1.2556 +            est_cq =
  1.2557 +                estimate_cq( cpi,
  1.2558 +                             &cpi->twopass.total_left_stats,
  1.2559 +                             (int)(cpi->twopass.bits_left / frames_left),
  1.2560 +                             overhead_bits );
  1.2561 +
  1.2562 +            cpi->cq_target_quality = cpi->oxcf.cq_level;
  1.2563 +            if ( est_cq > cpi->cq_target_quality )
  1.2564 +                cpi->cq_target_quality = est_cq;
  1.2565 +        }
  1.2566 +
  1.2567 +        /* guess at maxq needed in 2nd pass */
  1.2568 +        cpi->twopass.maxq_max_limit = cpi->worst_quality;
  1.2569 +        cpi->twopass.maxq_min_limit = cpi->best_quality;
  1.2570 +
  1.2571 +        tmp_q = estimate_max_q(
  1.2572 +                    cpi,
  1.2573 +                    &cpi->twopass.total_left_stats,
  1.2574 +                    (int)(cpi->twopass.bits_left / frames_left),
  1.2575 +                    overhead_bits );
  1.2576 +
  1.2577 +        /* Limit the maxq value returned subsequently.
  1.2578 +         * This increases the risk of overspend or underspend if the initial
  1.2579 +         * estimate for the clip is bad, but helps prevent excessive
  1.2580 +         * variation in Q, especially near the end of a clip
  1.2581 +         * where for example a small overspend may cause Q to crash
  1.2582 +         */
  1.2583 +        cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
  1.2584 +                                  ? (tmp_q + 32) : cpi->worst_quality;
  1.2585 +        cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
  1.2586 +                                  ? (tmp_q - 32) : cpi->best_quality;
  1.2587 +
  1.2588 +        cpi->active_worst_quality         = tmp_q;
  1.2589 +        cpi->ni_av_qi                     = tmp_q;
  1.2590 +    }
  1.2591 +
  1.2592 +    /* The last few frames of a clip almost always have to few or too many
  1.2593 +     * bits and for the sake of over exact rate control we dont want to make
  1.2594 +     * radical adjustments to the allowed quantizer range just to use up a
  1.2595 +     * few surplus bits or get beneath the target rate.
  1.2596 +     */
  1.2597 +    else if ( (cpi->common.current_video_frame <
  1.2598 +                 (((unsigned int)cpi->twopass.total_stats.count * 255)>>8)) &&
  1.2599 +              ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
  1.2600 +                 (unsigned int)cpi->twopass.total_stats.count) )
  1.2601 +    {
  1.2602 +        if (frames_left < 1)
  1.2603 +            frames_left = 1;
  1.2604 +
  1.2605 +        tmp_q = estimate_max_q(
  1.2606 +                    cpi,
  1.2607 +                    &cpi->twopass.total_left_stats,
  1.2608 +                    (int)(cpi->twopass.bits_left / frames_left),
  1.2609 +                    overhead_bits );
  1.2610 +
  1.2611 +        /* Move active_worst_quality but in a damped way */
  1.2612 +        if (tmp_q > cpi->active_worst_quality)
  1.2613 +            cpi->active_worst_quality ++;
  1.2614 +        else if (tmp_q < cpi->active_worst_quality)
  1.2615 +            cpi->active_worst_quality --;
  1.2616 +
  1.2617 +        cpi->active_worst_quality =
  1.2618 +            ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
  1.2619 +    }
  1.2620 +
  1.2621 +    cpi->twopass.frames_to_key --;
  1.2622 +
  1.2623 +    /* Update the total stats remaining sturcture */
  1.2624 +    subtract_stats(&cpi->twopass.total_left_stats, &this_frame );
  1.2625 +}
  1.2626 +
  1.2627 +
  1.2628 +static int test_candidate_kf(VP8_COMP *cpi,  FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
  1.2629 +{
  1.2630 +    int is_viable_kf = 0;
  1.2631 +
  1.2632 +    /* Does the frame satisfy the primary criteria of a key frame
  1.2633 +     *      If so, then examine how well it predicts subsequent frames
  1.2634 +     */
  1.2635 +    if ((this_frame->pcnt_second_ref < 0.10) &&
  1.2636 +        (next_frame->pcnt_second_ref < 0.10) &&
  1.2637 +        ((this_frame->pcnt_inter < 0.05) ||
  1.2638 +         (
  1.2639 +             ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
  1.2640 +             ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
  1.2641 +             ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
  1.2642 +              (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
  1.2643 +              ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
  1.2644 +             )
  1.2645 +         )
  1.2646 +        )
  1.2647 +       )
  1.2648 +    {
  1.2649 +        int i;
  1.2650 +        FIRSTPASS_STATS *start_pos;
  1.2651 +
  1.2652 +        FIRSTPASS_STATS local_next_frame;
  1.2653 +
  1.2654 +        double boost_score = 0.0;
  1.2655 +        double old_boost_score = 0.0;
  1.2656 +        double decay_accumulator = 1.0;
  1.2657 +        double next_iiratio;
  1.2658 +
  1.2659 +        vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
  1.2660 +
  1.2661 +        /* Note the starting file position so we can reset to it */
  1.2662 +        start_pos = cpi->twopass.stats_in;
  1.2663 +
  1.2664 +        /* Examine how well the key frame predicts subsequent frames */
  1.2665 +        for (i = 0 ; i < 16; i++)
  1.2666 +        {
  1.2667 +            next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
  1.2668 +
  1.2669 +            if (next_iiratio > RMAX)
  1.2670 +                next_iiratio = RMAX;
  1.2671 +
  1.2672 +            /* Cumulative effect of decay in prediction quality */
  1.2673 +            if (local_next_frame.pcnt_inter > 0.85)
  1.2674 +                decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
  1.2675 +            else
  1.2676 +                decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
  1.2677 +
  1.2678 +            /* Keep a running total */
  1.2679 +            boost_score += (decay_accumulator * next_iiratio);
  1.2680 +
  1.2681 +            /* Test various breakout clauses */
  1.2682 +            if ((local_next_frame.pcnt_inter < 0.05) ||
  1.2683 +                (next_iiratio < 1.5) ||
  1.2684 +                (((local_next_frame.pcnt_inter -
  1.2685 +                   local_next_frame.pcnt_neutral) < 0.20) &&
  1.2686 +                 (next_iiratio < 3.0)) ||
  1.2687 +                ((boost_score - old_boost_score) < 0.5) ||
  1.2688 +                (local_next_frame.intra_error < 200)
  1.2689 +               )
  1.2690 +            {
  1.2691 +                break;
  1.2692 +            }
  1.2693 +
  1.2694 +            old_boost_score = boost_score;
  1.2695 +
  1.2696 +            /* Get the next frame details */
  1.2697 +            if (EOF == input_stats(cpi, &local_next_frame))
  1.2698 +                break;
  1.2699 +        }
  1.2700 +
  1.2701 +        /* If there is tolerable prediction for at least the next 3 frames
  1.2702 +         * then break out else discard this pottential key frame and move on
  1.2703 +         */
  1.2704 +        if (boost_score > 5.0 && (i > 3))
  1.2705 +            is_viable_kf = 1;
  1.2706 +        else
  1.2707 +        {
  1.2708 +            /* Reset the file position */
  1.2709 +            reset_fpf_position(cpi, start_pos);
  1.2710 +
  1.2711 +            is_viable_kf = 0;
  1.2712 +        }
  1.2713 +    }
  1.2714 +
  1.2715 +    return is_viable_kf;
  1.2716 +}
  1.2717 +static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
  1.2718 +{
  1.2719 +    int i,j;
  1.2720 +    FIRSTPASS_STATS last_frame;
  1.2721 +    FIRSTPASS_STATS first_frame;
  1.2722 +    FIRSTPASS_STATS next_frame;
  1.2723 +    FIRSTPASS_STATS *start_position;
  1.2724 +
  1.2725 +    double decay_accumulator = 1.0;
  1.2726 +    double boost_score = 0;
  1.2727 +    double old_boost_score = 0.0;
  1.2728 +    double loop_decay_rate;
  1.2729 +
  1.2730 +    double kf_mod_err = 0.0;
  1.2731 +    double kf_group_err = 0.0;
  1.2732 +    double kf_group_intra_err = 0.0;
  1.2733 +    double kf_group_coded_err = 0.0;
  1.2734 +    double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
  1.2735 +
  1.2736 +    vpx_memset(&next_frame, 0, sizeof(next_frame));
  1.2737 +
  1.2738 +    vp8_clear_system_state();
  1.2739 +    start_position = cpi->twopass.stats_in;
  1.2740 +
  1.2741 +    cpi->common.frame_type = KEY_FRAME;
  1.2742 +
  1.2743 +    /* is this a forced key frame by interval */
  1.2744 +    cpi->this_key_frame_forced = cpi->next_key_frame_forced;
  1.2745 +
  1.2746 +    /* Clear the alt ref active flag as this can never be active on a key
  1.2747 +     * frame
  1.2748 +     */
  1.2749 +    cpi->source_alt_ref_active = 0;
  1.2750 +
  1.2751 +    /* Kf is always a gf so clear frames till next gf counter */
  1.2752 +    cpi->frames_till_gf_update_due = 0;
  1.2753 +
  1.2754 +    cpi->twopass.frames_to_key = 1;
  1.2755 +
  1.2756 +    /* Take a copy of the initial frame details */
  1.2757 +    vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
  1.2758 +
  1.2759 +    cpi->twopass.kf_group_bits = 0;
  1.2760 +    cpi->twopass.kf_group_error_left = 0;
  1.2761 +
  1.2762 +    kf_mod_err = calculate_modified_err(cpi, this_frame);
  1.2763 +
  1.2764 +    /* find the next keyframe */
  1.2765 +    i = 0;
  1.2766 +    while (cpi->twopass.stats_in < cpi->twopass.stats_in_end)
  1.2767 +    {
  1.2768 +        /* Accumulate kf group error */
  1.2769 +        kf_group_err += calculate_modified_err(cpi, this_frame);
  1.2770 +
  1.2771 +        /* These figures keep intra and coded error counts for all frames
  1.2772 +         * including key frames in the group. The effect of the key frame
  1.2773 +         * itself can be subtracted out using the first_frame data
  1.2774 +         * collected above
  1.2775 +         */
  1.2776 +        kf_group_intra_err += this_frame->intra_error;
  1.2777 +        kf_group_coded_err += this_frame->coded_error;
  1.2778 +
  1.2779 +        /* load a the next frame's stats */
  1.2780 +        vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
  1.2781 +        input_stats(cpi, this_frame);
  1.2782 +
  1.2783 +        /* Provided that we are not at the end of the file... */
  1.2784 +        if (cpi->oxcf.auto_key
  1.2785 +            && lookup_next_frame_stats(cpi, &next_frame) != EOF)
  1.2786 +        {
  1.2787 +            /* Normal scene cut check */
  1.2788 +            if ( ( i >= MIN_GF_INTERVAL ) &&
  1.2789 +                 test_candidate_kf(cpi, &last_frame, this_frame, &next_frame) )
  1.2790 +            {
  1.2791 +                break;
  1.2792 +            }
  1.2793 +
  1.2794 +            /* How fast is prediction quality decaying */
  1.2795 +            loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  1.2796 +
  1.2797 +            /* We want to know something about the recent past... rather than
  1.2798 +             * as used elsewhere where we are concened with decay in prediction
  1.2799 +             * quality since the last GF or KF.
  1.2800 +             */
  1.2801 +            recent_loop_decay[i%8] = loop_decay_rate;
  1.2802 +            decay_accumulator = 1.0;
  1.2803 +            for (j = 0; j < 8; j++)
  1.2804 +            {
  1.2805 +                decay_accumulator = decay_accumulator * recent_loop_decay[j];
  1.2806 +            }
  1.2807 +
  1.2808 +            /* Special check for transition or high motion followed by a
  1.2809 +             * static scene.
  1.2810 +             */
  1.2811 +            if ( detect_transition_to_still( cpi, i,
  1.2812 +                                             (cpi->key_frame_frequency-i),
  1.2813 +                                             loop_decay_rate,
  1.2814 +                                             decay_accumulator ) )
  1.2815 +            {
  1.2816 +                break;
  1.2817 +            }
  1.2818 +
  1.2819 +
  1.2820 +            /* Step on to the next frame */
  1.2821 +            cpi->twopass.frames_to_key ++;
  1.2822 +
  1.2823 +            /* If we don't have a real key frame within the next two
  1.2824 +             * forcekeyframeevery intervals then break out of the loop.
  1.2825 +             */
  1.2826 +            if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency)
  1.2827 +                break;
  1.2828 +        } else
  1.2829 +            cpi->twopass.frames_to_key ++;
  1.2830 +
  1.2831 +        i++;
  1.2832 +    }
  1.2833 +
  1.2834 +    /* If there is a max kf interval set by the user we must obey it.
  1.2835 +     * We already breakout of the loop above at 2x max.
  1.2836 +     * This code centers the extra kf if the actual natural
  1.2837 +     * interval is between 1x and 2x
  1.2838 +     */
  1.2839 +    if (cpi->oxcf.auto_key
  1.2840 +        && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency )
  1.2841 +    {
  1.2842 +        FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
  1.2843 +        FIRSTPASS_STATS tmp_frame;
  1.2844 +
  1.2845 +        cpi->twopass.frames_to_key /= 2;
  1.2846 +
  1.2847 +        /* Copy first frame details */
  1.2848 +        vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
  1.2849 +
  1.2850 +        /* Reset to the start of the group */
  1.2851 +        reset_fpf_position(cpi, start_position);
  1.2852 +
  1.2853 +        kf_group_err = 0;
  1.2854 +        kf_group_intra_err = 0;
  1.2855 +        kf_group_coded_err = 0;
  1.2856 +
  1.2857 +        /* Rescan to get the correct error data for the forced kf group */
  1.2858 +        for( i = 0; i < cpi->twopass.frames_to_key; i++ )
  1.2859 +        {
  1.2860 +            /* Accumulate kf group errors */
  1.2861 +            kf_group_err += calculate_modified_err(cpi, &tmp_frame);
  1.2862 +            kf_group_intra_err += tmp_frame.intra_error;
  1.2863 +            kf_group_coded_err += tmp_frame.coded_error;
  1.2864 +
  1.2865 +            /* Load a the next frame's stats */
  1.2866 +            input_stats(cpi, &tmp_frame);
  1.2867 +        }
  1.2868 +
  1.2869 +        /* Reset to the start of the group */
  1.2870 +        reset_fpf_position(cpi, current_pos);
  1.2871 +
  1.2872 +        cpi->next_key_frame_forced = 1;
  1.2873 +    }
  1.2874 +    else
  1.2875 +        cpi->next_key_frame_forced = 0;
  1.2876 +
  1.2877 +    /* Special case for the last frame of the file */
  1.2878 +    if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
  1.2879 +    {
  1.2880 +        /* Accumulate kf group error */
  1.2881 +        kf_group_err += calculate_modified_err(cpi, this_frame);
  1.2882 +
  1.2883 +        /* These figures keep intra and coded error counts for all frames
  1.2884 +         * including key frames in the group. The effect of the key frame
  1.2885 +         * itself can be subtracted out using the first_frame data
  1.2886 +         * collected above
  1.2887 +         */
  1.2888 +        kf_group_intra_err += this_frame->intra_error;
  1.2889 +        kf_group_coded_err += this_frame->coded_error;
  1.2890 +    }
  1.2891 +
  1.2892 +    /* Calculate the number of bits that should be assigned to the kf group. */
  1.2893 +    if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0))
  1.2894 +    {
  1.2895 +        /* Max for a single normal frame (not key frame) */
  1.2896 +        int max_bits = frame_max_bits(cpi);
  1.2897 +
  1.2898 +        /* Maximum bits for the kf group */
  1.2899 +        int64_t max_grp_bits;
  1.2900 +
  1.2901 +        /* Default allocation based on bits left and relative
  1.2902 +         * complexity of the section
  1.2903 +         */
  1.2904 +        cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left *
  1.2905 +                                          ( kf_group_err /
  1.2906 +                                            cpi->twopass.modified_error_left ));
  1.2907 +
  1.2908 +        /* Clip based on maximum per frame rate defined by the user. */
  1.2909 +        max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
  1.2910 +        if (cpi->twopass.kf_group_bits > max_grp_bits)
  1.2911 +            cpi->twopass.kf_group_bits = max_grp_bits;
  1.2912 +
  1.2913 +        /* Additional special case for CBR if buffer is getting full. */
  1.2914 +        if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
  1.2915 +        {
  1.2916 +            int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
  1.2917 +            int64_t buffer_lvl = cpi->buffer_level;
  1.2918 +
  1.2919 +            /* If the buffer is near or above the optimal and this kf group is
  1.2920 +             * not being allocated much then increase the allocation a bit.
  1.2921 +             */
  1.2922 +            if (buffer_lvl >= opt_buffer_lvl)
  1.2923 +            {
  1.2924 +                int64_t high_water_mark = (opt_buffer_lvl +
  1.2925 +                                       cpi->oxcf.maximum_buffer_size) >> 1;
  1.2926 +
  1.2927 +                int64_t av_group_bits;
  1.2928 +
  1.2929 +                /* Av bits per frame * number of frames */
  1.2930 +                av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
  1.2931 +                                (int64_t)cpi->twopass.frames_to_key;
  1.2932 +
  1.2933 +                /* We are at or above the maximum. */
  1.2934 +                if (cpi->buffer_level >= high_water_mark)
  1.2935 +                {
  1.2936 +                    int64_t min_group_bits;
  1.2937 +
  1.2938 +                    min_group_bits = av_group_bits +
  1.2939 +                                     (int64_t)(buffer_lvl -
  1.2940 +                                                 high_water_mark);
  1.2941 +
  1.2942 +                    if (cpi->twopass.kf_group_bits < min_group_bits)
  1.2943 +                        cpi->twopass.kf_group_bits = min_group_bits;
  1.2944 +                }
  1.2945 +                /* We are above optimal but below the maximum */
  1.2946 +                else if (cpi->twopass.kf_group_bits < av_group_bits)
  1.2947 +                {
  1.2948 +                    int64_t bits_below_av = av_group_bits -
  1.2949 +                                              cpi->twopass.kf_group_bits;
  1.2950 +
  1.2951 +                    cpi->twopass.kf_group_bits +=
  1.2952 +                       (int64_t)((double)bits_below_av *
  1.2953 +                                   (double)(buffer_lvl - opt_buffer_lvl) /
  1.2954 +                                   (double)(high_water_mark - opt_buffer_lvl));
  1.2955 +                }
  1.2956 +            }
  1.2957 +        }
  1.2958 +    }
  1.2959 +    else
  1.2960 +        cpi->twopass.kf_group_bits = 0;
  1.2961 +
  1.2962 +    /* Reset the first pass file position */
  1.2963 +    reset_fpf_position(cpi, start_position);
  1.2964 +
  1.2965 +    /* determine how big to make this keyframe based on how well the
  1.2966 +     * subsequent frames use inter blocks
  1.2967 +     */
  1.2968 +    decay_accumulator = 1.0;
  1.2969 +    boost_score = 0.0;
  1.2970 +    loop_decay_rate = 1.00;       /* Starting decay rate */
  1.2971 +
  1.2972 +    for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
  1.2973 +    {
  1.2974 +        double r;
  1.2975 +
  1.2976 +        if (EOF == input_stats(cpi, &next_frame))
  1.2977 +            break;
  1.2978 +
  1.2979 +        if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
  1.2980 +            r = (IIKFACTOR2 * next_frame.intra_error /
  1.2981 +                     DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  1.2982 +        else
  1.2983 +            r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
  1.2984 +                     DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  1.2985 +
  1.2986 +        if (r > RMAX)
  1.2987 +            r = RMAX;
  1.2988 +
  1.2989 +        /* How fast is prediction quality decaying */
  1.2990 +        loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  1.2991 +
  1.2992 +        decay_accumulator = decay_accumulator * loop_decay_rate;
  1.2993 +        decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  1.2994 +
  1.2995 +        boost_score += (decay_accumulator * r);
  1.2996 +
  1.2997 +        if ((i > MIN_GF_INTERVAL) &&
  1.2998 +            ((boost_score - old_boost_score) < 1.0))
  1.2999 +        {
  1.3000 +            break;
  1.3001 +        }
  1.3002 +
  1.3003 +        old_boost_score = boost_score;
  1.3004 +    }
  1.3005 +
  1.3006 +    if (1)
  1.3007 +    {
  1.3008 +        FIRSTPASS_STATS sectionstats;
  1.3009 +        double Ratio;
  1.3010 +
  1.3011 +        zero_stats(&sectionstats);
  1.3012 +        reset_fpf_position(cpi, start_position);
  1.3013 +
  1.3014 +        for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
  1.3015 +        {
  1.3016 +            input_stats(cpi, &next_frame);
  1.3017 +            accumulate_stats(&sectionstats, &next_frame);
  1.3018 +        }
  1.3019 +
  1.3020 +        avg_stats(&sectionstats);
  1.3021 +
  1.3022 +        cpi->twopass.section_intra_rating = (unsigned int)
  1.3023 +            (sectionstats.intra_error
  1.3024 +            / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
  1.3025 +
  1.3026 +        Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
  1.3027 +        cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
  1.3028 +
  1.3029 +        if (cpi->twopass.section_max_qfactor < 0.80)
  1.3030 +            cpi->twopass.section_max_qfactor = 0.80;
  1.3031 +    }
  1.3032 +
  1.3033 +    /* When using CBR apply additional buffer fullness related upper limits */
  1.3034 +    if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
  1.3035 +    {
  1.3036 +        double max_boost;
  1.3037 +
  1.3038 +        if (cpi->drop_frames_allowed)
  1.3039 +        {
  1.3040 +            int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark
  1.3041 +                                  * (cpi->oxcf.optimal_buffer_level / 100));
  1.3042 +
  1.3043 +            if (cpi->buffer_level > df_buffer_level)
  1.3044 +                max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  1.3045 +            else
  1.3046 +                max_boost = 0.0;
  1.3047 +        }
  1.3048 +        else if (cpi->buffer_level > 0)
  1.3049 +        {
  1.3050 +            max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  1.3051 +        }
  1.3052 +        else
  1.3053 +        {
  1.3054 +            max_boost = 0.0;
  1.3055 +        }
  1.3056 +
  1.3057 +        if (boost_score > max_boost)
  1.3058 +            boost_score = max_boost;
  1.3059 +    }
  1.3060 +
  1.3061 +    /* Reset the first pass file position */
  1.3062 +    reset_fpf_position(cpi, start_position);
  1.3063 +
  1.3064 +    /* Work out how many bits to allocate for the key frame itself */
  1.3065 +    if (1)
  1.3066 +    {
  1.3067 +        int kf_boost = (int)boost_score;
  1.3068 +        int allocation_chunks;
  1.3069 +        int Counter = cpi->twopass.frames_to_key;
  1.3070 +        int alt_kf_bits;
  1.3071 +        YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
  1.3072 +        /* Min boost based on kf interval */
  1.3073 +#if 0
  1.3074 +
  1.3075 +        while ((kf_boost < 48) && (Counter > 0))
  1.3076 +        {
  1.3077 +            Counter -= 2;
  1.3078 +            kf_boost ++;
  1.3079 +        }
  1.3080 +
  1.3081 +#endif
  1.3082 +
  1.3083 +        if (kf_boost < 48)
  1.3084 +        {
  1.3085 +            kf_boost += ((Counter + 1) >> 1);
  1.3086 +
  1.3087 +            if (kf_boost > 48) kf_boost = 48;
  1.3088 +        }
  1.3089 +
  1.3090 +        /* bigger frame sizes need larger kf boosts, smaller frames smaller
  1.3091 +         * boosts...
  1.3092 +         */
  1.3093 +        if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
  1.3094 +            kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
  1.3095 +        else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
  1.3096 +            kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
  1.3097 +
  1.3098 +        /* Min KF boost */
  1.3099 +        kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */
  1.3100 +        if (kf_boost < 250)
  1.3101 +            kf_boost = 250;
  1.3102 +
  1.3103 +        /*
  1.3104 +         * We do three calculations for kf size.
  1.3105 +         * The first is based on the error score for the whole kf group.
  1.3106 +         * The second (optionaly) on the key frames own error if this is
  1.3107 +         * smaller than the average for the group.
  1.3108 +         * The final one insures that the frame receives at least the
  1.3109 +         * allocation it would have received based on its own error score vs
  1.3110 +         * the error score remaining
  1.3111 +         * Special case if the sequence appears almost totaly static
  1.3112 +         * as measured by the decay accumulator. In this case we want to
  1.3113 +         * spend almost all of the bits on the key frame.
  1.3114 +         * cpi->twopass.frames_to_key-1 because key frame itself is taken
  1.3115 +         * care of by kf_boost.
  1.3116 +         */
  1.3117 +        if ( decay_accumulator >= 0.99 )
  1.3118 +        {
  1.3119 +            allocation_chunks =
  1.3120 +                ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
  1.3121 +        }
  1.3122 +        else
  1.3123 +        {
  1.3124 +            allocation_chunks =
  1.3125 +                ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
  1.3126 +        }
  1.3127 +
  1.3128 +        /* Normalize Altboost and allocations chunck down to prevent overflow */
  1.3129 +        while (kf_boost > 1000)
  1.3130 +        {
  1.3131 +            kf_boost /= 2;
  1.3132 +            allocation_chunks /= 2;
  1.3133 +        }
  1.3134 +
  1.3135 +        cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
  1.3136 +
  1.3137 +        /* Calculate the number of bits to be spent on the key frame */
  1.3138 +        cpi->twopass.kf_bits  = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
  1.3139 +
  1.3140 +        /* Apply an additional limit for CBR */
  1.3141 +        if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
  1.3142 +        {
  1.3143 +            if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2))
  1.3144 +                cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2);
  1.3145 +        }
  1.3146 +
  1.3147 +        /* If the key frame is actually easier than the average for the
  1.3148 +         * kf group (which does sometimes happen... eg a blank intro frame)
  1.3149 +         * Then use an alternate calculation based on the kf error score
  1.3150 +         * which should give a smaller key frame.
  1.3151 +         */
  1.3152 +        if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key)
  1.3153 +        {
  1.3154 +            double  alt_kf_grp_bits =
  1.3155 +                        ((double)cpi->twopass.bits_left *
  1.3156 +                         (kf_mod_err * (double)cpi->twopass.frames_to_key) /
  1.3157 +                         DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
  1.3158 +
  1.3159 +            alt_kf_bits = (int)((double)kf_boost *
  1.3160 +                                (alt_kf_grp_bits / (double)allocation_chunks));
  1.3161 +
  1.3162 +            if (cpi->twopass.kf_bits > alt_kf_bits)
  1.3163 +            {
  1.3164 +                cpi->twopass.kf_bits = alt_kf_bits;
  1.3165 +            }
  1.3166 +        }
  1.3167 +        /* Else if it is much harder than other frames in the group make sure
  1.3168 +         * it at least receives an allocation in keeping with its relative
  1.3169 +         * error score
  1.3170 +         */
  1.3171 +        else
  1.3172 +        {
  1.3173 +            alt_kf_bits =
  1.3174 +                (int)((double)cpi->twopass.bits_left *
  1.3175 +                      (kf_mod_err /
  1.3176 +                       DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
  1.3177 +
  1.3178 +            if (alt_kf_bits > cpi->twopass.kf_bits)
  1.3179 +            {
  1.3180 +                cpi->twopass.kf_bits = alt_kf_bits;
  1.3181 +            }
  1.3182 +        }
  1.3183 +
  1.3184 +        cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
  1.3185 +        /* Add in the minimum frame allowance */
  1.3186 +        cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
  1.3187 +
  1.3188 +        /* Peer frame bit target for this frame */
  1.3189 +        cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
  1.3190 +
  1.3191 +        /* Convert to a per second bitrate */
  1.3192 +        cpi->target_bandwidth = (int)(cpi->twopass.kf_bits *
  1.3193 +                                      cpi->output_framerate);
  1.3194 +    }
  1.3195 +
  1.3196 +    /* Note the total error score of the kf group minus the key frame itself */
  1.3197 +    cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
  1.3198 +
  1.3199 +    /* Adjust the count of total modified error left. The count of bits left
  1.3200 +     * is adjusted elsewhere based on real coded frame sizes
  1.3201 +     */
  1.3202 +    cpi->twopass.modified_error_left -= kf_group_err;
  1.3203 +
  1.3204 +    if (cpi->oxcf.allow_spatial_resampling)
  1.3205 +    {
  1.3206 +        int resample_trigger = 0;
  1.3207 +        int last_kf_resampled = 0;
  1.3208 +        int kf_q;
  1.3209 +        int scale_val = 0;
  1.3210 +        int hr, hs, vr, vs;
  1.3211 +        int new_width = cpi->oxcf.Width;
  1.3212 +        int new_height = cpi->oxcf.Height;
  1.3213 +
  1.3214 +        int projected_buffer_level = (int)cpi->buffer_level;
  1.3215 +        int tmp_q;
  1.3216 +
  1.3217 +        double projected_bits_perframe;
  1.3218 +        double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
  1.3219 +        double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
  1.3220 +        double bits_per_frame;
  1.3221 +        double av_bits_per_frame;
  1.3222 +        double effective_size_ratio;
  1.3223 +
  1.3224 +        if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
  1.3225 +            last_kf_resampled = 1;
  1.3226 +
  1.3227 +        /* Set back to unscaled by defaults */
  1.3228 +        cpi->common.horiz_scale = NORMAL;
  1.3229 +        cpi->common.vert_scale = NORMAL;
  1.3230 +
  1.3231 +        /* Calculate Average bits per frame. */
  1.3232 +        av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate);
  1.3233 +
  1.3234 +        /* CBR... Use the clip average as the target for deciding resample */
  1.3235 +        if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
  1.3236 +        {
  1.3237 +            bits_per_frame = av_bits_per_frame;
  1.3238 +        }
  1.3239 +
  1.3240 +        /* In VBR we want to avoid downsampling in easy section unless we
  1.3241 +         * are under extreme pressure So use the larger of target bitrate
  1.3242 +         * for this section or average bitrate for sequence
  1.3243 +         */
  1.3244 +        else
  1.3245 +        {
  1.3246 +            /* This accounts for how hard the section is... */
  1.3247 +            bits_per_frame = (double)
  1.3248 +                (cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key);
  1.3249 +
  1.3250 +            /* Dont turn to resampling in easy sections just because they
  1.3251 +             * have been assigned a small number of bits
  1.3252 +             */
  1.3253 +            if (bits_per_frame < av_bits_per_frame)
  1.3254 +                bits_per_frame = av_bits_per_frame;
  1.3255 +        }
  1.3256 +
  1.3257 +        /* bits_per_frame should comply with our minimum */
  1.3258 +        if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
  1.3259 +            bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
  1.3260 +
  1.3261 +        /* Work out if spatial resampling is necessary */
  1.3262 +        kf_q = estimate_kf_group_q(cpi, err_per_frame,
  1.3263 +                                  (int)bits_per_frame, group_iiratio);
  1.3264 +
  1.3265 +        /* If we project a required Q higher than the maximum allowed Q then
  1.3266 +         * make a guess at the actual size of frames in this section
  1.3267 +         */
  1.3268 +        projected_bits_perframe = bits_per_frame;
  1.3269 +        tmp_q = kf_q;
  1.3270 +
  1.3271 +        while (tmp_q > cpi->worst_quality)
  1.3272 +        {
  1.3273 +            projected_bits_perframe *= 1.04;
  1.3274 +            tmp_q--;
  1.3275 +        }
  1.3276 +
  1.3277 +        /* Guess at buffer level at the end of the section */
  1.3278 +        projected_buffer_level = (int)
  1.3279 +                    (cpi->buffer_level - (int)
  1.3280 +                    ((projected_bits_perframe - av_bits_per_frame) *
  1.3281 +                    cpi->twopass.frames_to_key));
  1.3282 +
  1.3283 +        if (0)
  1.3284 +        {
  1.3285 +            FILE *f = fopen("Subsamle.stt", "a");
  1.3286 +            fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n",  cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale,  kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width);
  1.3287 +            fclose(f);
  1.3288 +        }
  1.3289 +
  1.3290 +        /* The trigger for spatial resampling depends on the various
  1.3291 +         * parameters such as whether we are streaming (CBR) or VBR.
  1.3292 +         */
  1.3293 +        if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
  1.3294 +        {
  1.3295 +            /* Trigger resample if we are projected to fall below down
  1.3296 +             * sample level or resampled last time and are projected to
  1.3297 +             * remain below the up sample level
  1.3298 +             */
  1.3299 +            if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
  1.3300 +                (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
  1.3301 +                resample_trigger = 1;
  1.3302 +            else
  1.3303 +                resample_trigger = 0;
  1.3304 +        }
  1.3305 +        else
  1.3306 +        {
  1.3307 +            int64_t clip_bits = (int64_t)(cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate));
  1.3308 +            int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
  1.3309 +
  1.3310 +            /* If triggered last time the threshold for triggering again is
  1.3311 +             * reduced:
  1.3312 +             *
  1.3313 +             * Projected Q higher than allowed and Overspend > 5% of total
  1.3314 +             * bits
  1.3315 +             */
  1.3316 +            if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||
  1.3317 +                ((kf_q > cpi->worst_quality) &&
  1.3318 +                 (over_spend > clip_bits / 20)))
  1.3319 +                resample_trigger = 1;
  1.3320 +            else
  1.3321 +                resample_trigger = 0;
  1.3322 +
  1.3323 +        }
  1.3324 +
  1.3325 +        if (resample_trigger)
  1.3326 +        {
  1.3327 +            while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
  1.3328 +            {
  1.3329 +                scale_val ++;
  1.3330 +
  1.3331 +                cpi->common.vert_scale   = vscale_lookup[scale_val];
  1.3332 +                cpi->common.horiz_scale  = hscale_lookup[scale_val];
  1.3333 +
  1.3334 +                Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
  1.3335 +                Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
  1.3336 +
  1.3337 +                new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
  1.3338 +                new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
  1.3339 +
  1.3340 +                /* Reducing the area to 1/4 does not reduce the complexity
  1.3341 +                 * (err_per_frame) to 1/4... effective_sizeratio attempts
  1.3342 +                 * to provide a crude correction for this
  1.3343 +                 */
  1.3344 +                effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
  1.3345 +                effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
  1.3346 +
  1.3347 +                /* Now try again and see what Q we get with the smaller
  1.3348 +                 * image size
  1.3349 +                 */
  1.3350 +                kf_q = estimate_kf_group_q(cpi,
  1.3351 +                                          err_per_frame * effective_size_ratio,
  1.3352 +                                          (int)bits_per_frame, group_iiratio);
  1.3353 +
  1.3354 +                if (0)
  1.3355 +                {
  1.3356 +                    FILE *f = fopen("Subsamle.stt", "a");
  1.3357 +                    fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n",  kf_q, cpi->common.horiz_scale, cpi->common.vert_scale,  kf_group_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width);
  1.3358 +                    fclose(f);
  1.3359 +                }
  1.3360 +            }
  1.3361 +        }
  1.3362 +
  1.3363 +        if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
  1.3364 +        {
  1.3365 +            cpi->common.Width = new_width;
  1.3366 +            cpi->common.Height = new_height;
  1.3367 +            vp8_alloc_compressor_data(cpi);
  1.3368 +        }
  1.3369 +    }
  1.3370 +}

mercurial