media/libvpx/vp9/encoder/vp9_firstpass.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp9/encoder/vp9_firstpass.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,2729 @@
     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 +#include "vp9/encoder/vp9_block.h"
    1.18 +#include "vp9/encoder/vp9_onyx_int.h"
    1.19 +#include "vp9/encoder/vp9_variance.h"
    1.20 +#include "vp9/encoder/vp9_encodeintra.h"
    1.21 +#include "vp9/encoder/vp9_mcomp.h"
    1.22 +#include "vp9/encoder/vp9_firstpass.h"
    1.23 +#include "vpx_scale/vpx_scale.h"
    1.24 +#include "vp9/encoder/vp9_encodeframe.h"
    1.25 +#include "vp9/encoder/vp9_encodemb.h"
    1.26 +#include "vp9/common/vp9_extend.h"
    1.27 +#include "vp9/common/vp9_systemdependent.h"
    1.28 +#include "vpx_mem/vpx_mem.h"
    1.29 +#include "vpx_scale/yv12config.h"
    1.30 +#include "vp9/encoder/vp9_quantize.h"
    1.31 +#include "vp9/encoder/vp9_rdopt.h"
    1.32 +#include "vp9/encoder/vp9_ratectrl.h"
    1.33 +#include "vp9/common/vp9_quant_common.h"
    1.34 +#include "vp9/common/vp9_entropymv.h"
    1.35 +#include "vp9/encoder/vp9_encodemv.h"
    1.36 +#include "vp9/encoder/vp9_vaq.h"
    1.37 +#include "./vpx_scale_rtcd.h"
    1.38 +// TODO(jkoleszar): for setup_dst_planes
    1.39 +#include "vp9/common/vp9_reconinter.h"
    1.40 +
    1.41 +#define OUTPUT_FPF 0
    1.42 +
    1.43 +#define IIFACTOR   12.5
    1.44 +#define IIKFACTOR1 12.5
    1.45 +#define IIKFACTOR2 15.0
    1.46 +#define RMAX       512.0
    1.47 +#define GF_RMAX    96.0
    1.48 +#define ERR_DIVISOR   150.0
    1.49 +#define MIN_DECAY_FACTOR 0.1
    1.50 +
    1.51 +#define KF_MB_INTRA_MIN 150
    1.52 +#define GF_MB_INTRA_MIN 100
    1.53 +
    1.54 +#define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
    1.55 +
    1.56 +#define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
    1.57 +#define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
    1.58 +
    1.59 +static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
    1.60 +  YV12_BUFFER_CONFIG temp = *a;
    1.61 +  *a = *b;
    1.62 +  *b = temp;
    1.63 +}
    1.64 +
    1.65 +static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame);
    1.66 +
    1.67 +static int select_cq_level(int qindex) {
    1.68 +  int ret_val = QINDEX_RANGE - 1;
    1.69 +  int i;
    1.70 +
    1.71 +  double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0;
    1.72 +
    1.73 +  for (i = 0; i < QINDEX_RANGE; i++) {
    1.74 +    if (target_q <= vp9_convert_qindex_to_q(i)) {
    1.75 +      ret_val = i;
    1.76 +      break;
    1.77 +    }
    1.78 +  }
    1.79 +
    1.80 +  return ret_val;
    1.81 +}
    1.82 +
    1.83 +
    1.84 +// Resets the first pass file to the given position using a relative seek from
    1.85 +// the current position.
    1.86 +static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) {
    1.87 +  cpi->twopass.stats_in = position;
    1.88 +}
    1.89 +
    1.90 +static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) {
    1.91 +  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
    1.92 +    return EOF;
    1.93 +
    1.94 +  *next_frame = *cpi->twopass.stats_in;
    1.95 +  return 1;
    1.96 +}
    1.97 +
    1.98 +// Read frame stats at an offset from the current position
    1.99 +static int read_frame_stats(VP9_COMP *cpi,
   1.100 +                            FIRSTPASS_STATS *frame_stats,
   1.101 +                            int offset) {
   1.102 +  FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in;
   1.103 +
   1.104 +  // Check legality of offset
   1.105 +  if (offset >= 0) {
   1.106 +    if (&fps_ptr[offset] >= cpi->twopass.stats_in_end)
   1.107 +      return EOF;
   1.108 +  } else if (offset < 0) {
   1.109 +    if (&fps_ptr[offset] < cpi->twopass.stats_in_start)
   1.110 +      return EOF;
   1.111 +  }
   1.112 +
   1.113 +  *frame_stats = fps_ptr[offset];
   1.114 +  return 1;
   1.115 +}
   1.116 +
   1.117 +static int input_stats(VP9_COMP *cpi, FIRSTPASS_STATS *fps) {
   1.118 +  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
   1.119 +    return EOF;
   1.120 +
   1.121 +  *fps = *cpi->twopass.stats_in;
   1.122 +  cpi->twopass.stats_in =
   1.123 +    (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
   1.124 +  return 1;
   1.125 +}
   1.126 +
   1.127 +static void output_stats(const VP9_COMP            *cpi,
   1.128 +                         struct vpx_codec_pkt_list *pktlist,
   1.129 +                         FIRSTPASS_STATS            *stats) {
   1.130 +  struct vpx_codec_cx_pkt pkt;
   1.131 +  pkt.kind = VPX_CODEC_STATS_PKT;
   1.132 +  pkt.data.twopass_stats.buf = stats;
   1.133 +  pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
   1.134 +  vpx_codec_pkt_list_add(pktlist, &pkt);
   1.135 +
   1.136 +// TEMP debug code
   1.137 +#if OUTPUT_FPF
   1.138 +
   1.139 +  {
   1.140 +    FILE *fpfile;
   1.141 +    fpfile = fopen("firstpass.stt", "a");
   1.142 +
   1.143 +    fprintf(stdout, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
   1.144 +            "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
   1.145 +            "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
   1.146 +            stats->frame,
   1.147 +            stats->intra_error,
   1.148 +            stats->coded_error,
   1.149 +            stats->sr_coded_error,
   1.150 +            stats->ssim_weighted_pred_err,
   1.151 +            stats->pcnt_inter,
   1.152 +            stats->pcnt_motion,
   1.153 +            stats->pcnt_second_ref,
   1.154 +            stats->pcnt_neutral,
   1.155 +            stats->MVr,
   1.156 +            stats->mvr_abs,
   1.157 +            stats->MVc,
   1.158 +            stats->mvc_abs,
   1.159 +            stats->MVrv,
   1.160 +            stats->MVcv,
   1.161 +            stats->mv_in_out_count,
   1.162 +            stats->new_mv_count,
   1.163 +            stats->count,
   1.164 +            stats->duration);
   1.165 +    fclose(fpfile);
   1.166 +  }
   1.167 +#endif
   1.168 +}
   1.169 +
   1.170 +static void zero_stats(FIRSTPASS_STATS *section) {
   1.171 +  section->frame      = 0.0;
   1.172 +  section->intra_error = 0.0;
   1.173 +  section->coded_error = 0.0;
   1.174 +  section->sr_coded_error = 0.0;
   1.175 +  section->ssim_weighted_pred_err = 0.0;
   1.176 +  section->pcnt_inter  = 0.0;
   1.177 +  section->pcnt_motion  = 0.0;
   1.178 +  section->pcnt_second_ref = 0.0;
   1.179 +  section->pcnt_neutral = 0.0;
   1.180 +  section->MVr        = 0.0;
   1.181 +  section->mvr_abs     = 0.0;
   1.182 +  section->MVc        = 0.0;
   1.183 +  section->mvc_abs     = 0.0;
   1.184 +  section->MVrv       = 0.0;
   1.185 +  section->MVcv       = 0.0;
   1.186 +  section->mv_in_out_count  = 0.0;
   1.187 +  section->new_mv_count = 0.0;
   1.188 +  section->count      = 0.0;
   1.189 +  section->duration   = 1.0;
   1.190 +}
   1.191 +
   1.192 +static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
   1.193 +  section->frame += frame->frame;
   1.194 +  section->intra_error += frame->intra_error;
   1.195 +  section->coded_error += frame->coded_error;
   1.196 +  section->sr_coded_error += frame->sr_coded_error;
   1.197 +  section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
   1.198 +  section->pcnt_inter  += frame->pcnt_inter;
   1.199 +  section->pcnt_motion += frame->pcnt_motion;
   1.200 +  section->pcnt_second_ref += frame->pcnt_second_ref;
   1.201 +  section->pcnt_neutral += frame->pcnt_neutral;
   1.202 +  section->MVr        += frame->MVr;
   1.203 +  section->mvr_abs     += frame->mvr_abs;
   1.204 +  section->MVc        += frame->MVc;
   1.205 +  section->mvc_abs     += frame->mvc_abs;
   1.206 +  section->MVrv       += frame->MVrv;
   1.207 +  section->MVcv       += frame->MVcv;
   1.208 +  section->mv_in_out_count  += frame->mv_in_out_count;
   1.209 +  section->new_mv_count += frame->new_mv_count;
   1.210 +  section->count      += frame->count;
   1.211 +  section->duration   += frame->duration;
   1.212 +}
   1.213 +
   1.214 +static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
   1.215 +  section->frame -= frame->frame;
   1.216 +  section->intra_error -= frame->intra_error;
   1.217 +  section->coded_error -= frame->coded_error;
   1.218 +  section->sr_coded_error -= frame->sr_coded_error;
   1.219 +  section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
   1.220 +  section->pcnt_inter  -= frame->pcnt_inter;
   1.221 +  section->pcnt_motion -= frame->pcnt_motion;
   1.222 +  section->pcnt_second_ref -= frame->pcnt_second_ref;
   1.223 +  section->pcnt_neutral -= frame->pcnt_neutral;
   1.224 +  section->MVr        -= frame->MVr;
   1.225 +  section->mvr_abs     -= frame->mvr_abs;
   1.226 +  section->MVc        -= frame->MVc;
   1.227 +  section->mvc_abs     -= frame->mvc_abs;
   1.228 +  section->MVrv       -= frame->MVrv;
   1.229 +  section->MVcv       -= frame->MVcv;
   1.230 +  section->mv_in_out_count  -= frame->mv_in_out_count;
   1.231 +  section->new_mv_count -= frame->new_mv_count;
   1.232 +  section->count      -= frame->count;
   1.233 +  section->duration   -= frame->duration;
   1.234 +}
   1.235 +
   1.236 +static void avg_stats(FIRSTPASS_STATS *section) {
   1.237 +  if (section->count < 1.0)
   1.238 +    return;
   1.239 +
   1.240 +  section->intra_error /= section->count;
   1.241 +  section->coded_error /= section->count;
   1.242 +  section->sr_coded_error /= section->count;
   1.243 +  section->ssim_weighted_pred_err /= section->count;
   1.244 +  section->pcnt_inter  /= section->count;
   1.245 +  section->pcnt_second_ref /= section->count;
   1.246 +  section->pcnt_neutral /= section->count;
   1.247 +  section->pcnt_motion /= section->count;
   1.248 +  section->MVr        /= section->count;
   1.249 +  section->mvr_abs     /= section->count;
   1.250 +  section->MVc        /= section->count;
   1.251 +  section->mvc_abs     /= section->count;
   1.252 +  section->MVrv       /= section->count;
   1.253 +  section->MVcv       /= section->count;
   1.254 +  section->mv_in_out_count   /= section->count;
   1.255 +  section->duration   /= section->count;
   1.256 +}
   1.257 +
   1.258 +// Calculate a modified Error used in distributing bits between easier and
   1.259 +// harder frames.
   1.260 +static double calculate_modified_err(VP9_COMP *cpi,
   1.261 +                                     FIRSTPASS_STATS *this_frame) {
   1.262 +  const FIRSTPASS_STATS *const stats = &cpi->twopass.total_stats;
   1.263 +  const double av_err = stats->ssim_weighted_pred_err / stats->count;
   1.264 +  const double this_err = this_frame->ssim_weighted_pred_err;
   1.265 +  return av_err * pow(this_err / DOUBLE_DIVIDE_CHECK(av_err),
   1.266 +                      this_err > av_err ? POW1 : POW2);
   1.267 +}
   1.268 +
   1.269 +static const double weight_table[256] = {
   1.270 +  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.271 +  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.272 +  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.273 +  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
   1.274 +  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
   1.275 +  0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
   1.276 +  0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
   1.277 +  0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
   1.278 +  0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
   1.279 +  0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.280 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.281 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.282 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.283 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.284 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.285 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.286 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.287 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.288 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.289 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.290 +  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
   1.291 +  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.293 +  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.295 +  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.297 +  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.299 +  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.301 +  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.303 +  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.305 +  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.307 +};
   1.308 +
   1.309 +static double simple_weight(YV12_BUFFER_CONFIG *source) {
   1.310 +  int i, j;
   1.311 +
   1.312 +  uint8_t *src = source->y_buffer;
   1.313 +  double sum_weights = 0.0;
   1.314 +
   1.315 +  // Loop through the Y plane examining levels and creating a weight for
   1.316 +  // the image.
   1.317 +  i = source->y_height;
   1.318 +  do {
   1.319 +    j = source->y_width;
   1.320 +    do {
   1.321 +      sum_weights += weight_table[ *src];
   1.322 +      src++;
   1.323 +    } while (--j);
   1.324 +    src -= source->y_width;
   1.325 +    src += source->y_stride;
   1.326 +  } while (--i);
   1.327 +
   1.328 +  sum_weights /= (source->y_height * source->y_width);
   1.329 +
   1.330 +  return sum_weights;
   1.331 +}
   1.332 +
   1.333 +
   1.334 +// This function returns the current per frame maximum bitrate target.
   1.335 +static int frame_max_bits(VP9_COMP *cpi) {
   1.336 +  // Max allocation for a single frame based on the max section guidelines
   1.337 +  // passed in and how many bits are left.
   1.338 +  // For VBR base this on the bits and frames left plus the
   1.339 +  // two_pass_vbrmax_section rate passed in by the user.
   1.340 +  const double max_bits = (1.0 * cpi->twopass.bits_left /
   1.341 +      (cpi->twopass.total_stats.count - cpi->common.current_video_frame)) *
   1.342 +      (cpi->oxcf.two_pass_vbrmax_section / 100.0);
   1.343 +
   1.344 +  // Trap case where we are out of bits.
   1.345 +  return MAX((int)max_bits, 0);
   1.346 +}
   1.347 +
   1.348 +void vp9_init_first_pass(VP9_COMP *cpi) {
   1.349 +  zero_stats(&cpi->twopass.total_stats);
   1.350 +}
   1.351 +
   1.352 +void vp9_end_first_pass(VP9_COMP *cpi) {
   1.353 +  output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
   1.354 +}
   1.355 +
   1.356 +static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
   1.357 +                             YV12_BUFFER_CONFIG *recon_buffer,
   1.358 +                             int *best_motion_err, int recon_yoffset) {
   1.359 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.360 +
   1.361 +  // Set up pointers for this macro block recon buffer
   1.362 +  xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset;
   1.363 +
   1.364 +  switch (xd->mi_8x8[0]->mbmi.sb_type) {
   1.365 +    case BLOCK_8X8:
   1.366 +      vp9_mse8x8(x->plane[0].src.buf, x->plane[0].src.stride,
   1.367 +                 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
   1.368 +                 (unsigned int *)(best_motion_err));
   1.369 +      break;
   1.370 +    case BLOCK_16X8:
   1.371 +      vp9_mse16x8(x->plane[0].src.buf, x->plane[0].src.stride,
   1.372 +                  xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
   1.373 +                  (unsigned int *)(best_motion_err));
   1.374 +      break;
   1.375 +    case BLOCK_8X16:
   1.376 +      vp9_mse8x16(x->plane[0].src.buf, x->plane[0].src.stride,
   1.377 +                  xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
   1.378 +                  (unsigned int *)(best_motion_err));
   1.379 +      break;
   1.380 +    default:
   1.381 +      vp9_mse16x16(x->plane[0].src.buf, x->plane[0].src.stride,
   1.382 +                   xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
   1.383 +                   (unsigned int *)(best_motion_err));
   1.384 +      break;
   1.385 +  }
   1.386 +}
   1.387 +
   1.388 +static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
   1.389 +                                     int_mv *ref_mv, MV *best_mv,
   1.390 +                                     YV12_BUFFER_CONFIG *recon_buffer,
   1.391 +                                     int *best_motion_err, int recon_yoffset) {
   1.392 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.393 +  int num00;
   1.394 +
   1.395 +  int_mv tmp_mv;
   1.396 +  int_mv ref_mv_full;
   1.397 +
   1.398 +  int tmp_err;
   1.399 +  int step_param = 3;
   1.400 +  int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
   1.401 +  int n;
   1.402 +  vp9_variance_fn_ptr_t v_fn_ptr =
   1.403 +      cpi->fn_ptr[xd->mi_8x8[0]->mbmi.sb_type];
   1.404 +  int new_mv_mode_penalty = 256;
   1.405 +
   1.406 +  int sr = 0;
   1.407 +  int quart_frm = MIN(cpi->common.width, cpi->common.height);
   1.408 +
   1.409 +  // refine the motion search range accroding to the frame dimension
   1.410 +  // for first pass test
   1.411 +  while ((quart_frm << sr) < MAX_FULL_PEL_VAL)
   1.412 +    sr++;
   1.413 +  if (sr)
   1.414 +    sr--;
   1.415 +
   1.416 +  step_param    += sr;
   1.417 +  further_steps -= sr;
   1.418 +
   1.419 +  // override the default variance function to use MSE
   1.420 +  switch (xd->mi_8x8[0]->mbmi.sb_type) {
   1.421 +    case BLOCK_8X8:
   1.422 +      v_fn_ptr.vf = vp9_mse8x8;
   1.423 +      break;
   1.424 +    case BLOCK_16X8:
   1.425 +      v_fn_ptr.vf = vp9_mse16x8;
   1.426 +      break;
   1.427 +    case BLOCK_8X16:
   1.428 +      v_fn_ptr.vf = vp9_mse8x16;
   1.429 +      break;
   1.430 +    default:
   1.431 +      v_fn_ptr.vf = vp9_mse16x16;
   1.432 +      break;
   1.433 +  }
   1.434 +
   1.435 +  // Set up pointers for this macro block recon buffer
   1.436 +  xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset;
   1.437 +
   1.438 +  // Initial step/diamond search centred on best mv
   1.439 +  tmp_mv.as_int = 0;
   1.440 +  ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3;
   1.441 +  ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3;
   1.442 +  tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, step_param,
   1.443 +                                    x->sadperbit16, &num00, &v_fn_ptr,
   1.444 +                                    x->nmvjointcost,
   1.445 +                                    x->mvcost, ref_mv);
   1.446 +  if (tmp_err < INT_MAX - new_mv_mode_penalty)
   1.447 +    tmp_err += new_mv_mode_penalty;
   1.448 +
   1.449 +  if (tmp_err < *best_motion_err) {
   1.450 +    *best_motion_err = tmp_err;
   1.451 +    best_mv->row = tmp_mv.as_mv.row;
   1.452 +    best_mv->col = tmp_mv.as_mv.col;
   1.453 +  }
   1.454 +
   1.455 +  // Further step/diamond searches as necessary
   1.456 +  n = num00;
   1.457 +  num00 = 0;
   1.458 +
   1.459 +  while (n < further_steps) {
   1.460 +    n++;
   1.461 +
   1.462 +    if (num00) {
   1.463 +      num00--;
   1.464 +    } else {
   1.465 +      tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
   1.466 +                                        step_param + n, x->sadperbit16,
   1.467 +                                        &num00, &v_fn_ptr,
   1.468 +                                        x->nmvjointcost,
   1.469 +                                        x->mvcost, ref_mv);
   1.470 +      if (tmp_err < INT_MAX - new_mv_mode_penalty)
   1.471 +        tmp_err += new_mv_mode_penalty;
   1.472 +
   1.473 +      if (tmp_err < *best_motion_err) {
   1.474 +        *best_motion_err = tmp_err;
   1.475 +        best_mv->row = tmp_mv.as_mv.row;
   1.476 +        best_mv->col = tmp_mv.as_mv.col;
   1.477 +      }
   1.478 +    }
   1.479 +  }
   1.480 +}
   1.481 +
   1.482 +void vp9_first_pass(VP9_COMP *cpi) {
   1.483 +  int mb_row, mb_col;
   1.484 +  MACROBLOCK *const x = &cpi->mb;
   1.485 +  VP9_COMMON *const cm = &cpi->common;
   1.486 +  MACROBLOCKD *const xd = &x->e_mbd;
   1.487 +  TileInfo tile;
   1.488 +  struct macroblock_plane *const p = x->plane;
   1.489 +  struct macroblockd_plane *const pd = xd->plane;
   1.490 +  PICK_MODE_CONTEXT *ctx = &x->sb64_context;
   1.491 +  int i;
   1.492 +
   1.493 +  int recon_yoffset, recon_uvoffset;
   1.494 +  const int lst_yv12_idx = cm->ref_frame_map[cpi->lst_fb_idx];
   1.495 +  const int gld_yv12_idx = cm->ref_frame_map[cpi->gld_fb_idx];
   1.496 +  YV12_BUFFER_CONFIG *const lst_yv12 = &cm->yv12_fb[lst_yv12_idx];
   1.497 +  YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx];
   1.498 +  YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
   1.499 +  const int recon_y_stride = lst_yv12->y_stride;
   1.500 +  const int recon_uv_stride = lst_yv12->uv_stride;
   1.501 +  int64_t intra_error = 0;
   1.502 +  int64_t coded_error = 0;
   1.503 +  int64_t sr_coded_error = 0;
   1.504 +
   1.505 +  int sum_mvr = 0, sum_mvc = 0;
   1.506 +  int sum_mvr_abs = 0, sum_mvc_abs = 0;
   1.507 +  int sum_mvrs = 0, sum_mvcs = 0;
   1.508 +  int mvcount = 0;
   1.509 +  int intercount = 0;
   1.510 +  int second_ref_count = 0;
   1.511 +  int intrapenalty = 256;
   1.512 +  int neutral_count = 0;
   1.513 +  int new_mv_count = 0;
   1.514 +  int sum_in_vectors = 0;
   1.515 +  uint32_t lastmv_as_int = 0;
   1.516 +
   1.517 +  int_mv zero_ref_mv;
   1.518 +
   1.519 +  zero_ref_mv.as_int = 0;
   1.520 +
   1.521 +  vp9_clear_system_state();  // __asm emms;
   1.522 +
   1.523 +  vp9_setup_src_planes(x, cpi->Source, 0, 0);
   1.524 +  setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL);
   1.525 +  setup_dst_planes(xd, new_yv12, 0, 0);
   1.526 +
   1.527 +  xd->mi_8x8 = cm->mi_grid_visible;
   1.528 +  // required for vp9_frame_init_quantizer
   1.529 +  xd->mi_8x8[0] = cm->mi;
   1.530 +
   1.531 +  setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
   1.532 +
   1.533 +  vp9_frame_init_quantizer(cpi);
   1.534 +
   1.535 +  for (i = 0; i < MAX_MB_PLANE; ++i) {
   1.536 +    p[i].coeff = ctx->coeff_pbuf[i][1];
   1.537 +    pd[i].qcoeff = ctx->qcoeff_pbuf[i][1];
   1.538 +    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
   1.539 +    pd[i].eobs = ctx->eobs_pbuf[i][1];
   1.540 +  }
   1.541 +  x->skip_recode = 0;
   1.542 +
   1.543 +
   1.544 +  // Initialise the MV cost table to the defaults
   1.545 +  // if( cm->current_video_frame == 0)
   1.546 +  // if ( 0 )
   1.547 +  {
   1.548 +    vp9_init_mv_probs(cm);
   1.549 +    vp9_initialize_rd_consts(cpi);
   1.550 +  }
   1.551 +
   1.552 +  // tiling is ignored in the first pass
   1.553 +  vp9_tile_init(&tile, cm, 0, 0);
   1.554 +
   1.555 +  // for each macroblock row in image
   1.556 +  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
   1.557 +    int_mv best_ref_mv;
   1.558 +
   1.559 +    best_ref_mv.as_int = 0;
   1.560 +
   1.561 +    // reset above block coeffs
   1.562 +    xd->up_available = (mb_row != 0);
   1.563 +    recon_yoffset = (mb_row * recon_y_stride * 16);
   1.564 +    recon_uvoffset = (mb_row * recon_uv_stride * 8);
   1.565 +
   1.566 +    // Set up limit values for motion vectors to prevent them extending
   1.567 +    // outside the UMV borders
   1.568 +    x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
   1.569 +    x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
   1.570 +                    + BORDER_MV_PIXELS_B16;
   1.571 +
   1.572 +    // for each macroblock col in image
   1.573 +    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
   1.574 +      int this_error;
   1.575 +      int gf_motion_error = INT_MAX;
   1.576 +      int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
   1.577 +      double error_weight;
   1.578 +
   1.579 +      vp9_clear_system_state();  // __asm emms;
   1.580 +      error_weight = 1.0;  // avoid uninitialized warnings
   1.581 +
   1.582 +      xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
   1.583 +      xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
   1.584 +      xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
   1.585 +      xd->left_available = (mb_col != 0);
   1.586 +
   1.587 +      if (mb_col * 2 + 1 < cm->mi_cols) {
   1.588 +        if (mb_row * 2 + 1 < cm->mi_rows) {
   1.589 +          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X16;
   1.590 +        } else {
   1.591 +          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X8;
   1.592 +        }
   1.593 +      } else {
   1.594 +        if (mb_row * 2 + 1 < cm->mi_rows) {
   1.595 +          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X16;
   1.596 +        } else {
   1.597 +          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X8;
   1.598 +        }
   1.599 +      }
   1.600 +      xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME;
   1.601 +      set_mi_row_col(xd, &tile,
   1.602 +                     mb_row << 1,
   1.603 +                     num_8x8_blocks_high_lookup[xd->mi_8x8[0]->mbmi.sb_type],
   1.604 +                     mb_col << 1,
   1.605 +                     num_8x8_blocks_wide_lookup[xd->mi_8x8[0]->mbmi.sb_type],
   1.606 +                     cm->mi_rows, cm->mi_cols);
   1.607 +
   1.608 +      if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.609 +        int energy = vp9_block_energy(cpi, x, xd->mi_8x8[0]->mbmi.sb_type);
   1.610 +        error_weight = vp9_vaq_inv_q_ratio(energy);
   1.611 +      }
   1.612 +
   1.613 +      // do intra 16x16 prediction
   1.614 +      this_error = vp9_encode_intra(x, use_dc_pred);
   1.615 +      if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.616 +        vp9_clear_system_state();  // __asm emms;
   1.617 +        this_error *= error_weight;
   1.618 +      }
   1.619 +
   1.620 +      // intrapenalty below deals with situations where the intra and inter
   1.621 +      // error scores are very low (eg a plain black frame).
   1.622 +      // We do not have special cases in first pass for 0,0 and nearest etc so
   1.623 +      // all inter modes carry an overhead cost estimate for the mv.
   1.624 +      // When the error score is very low this causes us to pick all or lots of
   1.625 +      // INTRA modes and throw lots of key frames.
   1.626 +      // This penalty adds a cost matching that of a 0,0 mv to the intra case.
   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 extending
   1.633 +      // outside the UMV borders.
   1.634 +      x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
   1.635 +      x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
   1.636 +                      + BORDER_MV_PIXELS_B16;
   1.637 +
   1.638 +      // Other than for the first frame do a motion search
   1.639 +      if (cm->current_video_frame > 0) {
   1.640 +        int tmp_err;
   1.641 +        int motion_error = INT_MAX;
   1.642 +        int_mv mv, tmp_mv;
   1.643 +
   1.644 +        // Simple 0,0 motion with no mv overhead
   1.645 +        zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset);
   1.646 +        mv.as_int = tmp_mv.as_int = 0;
   1.647 +
   1.648 +        // Test last reference frame using the previous best mv as the
   1.649 +        // starting point (best reference) for the search
   1.650 +        first_pass_motion_search(cpi, x, &best_ref_mv,
   1.651 +                                 &mv.as_mv, lst_yv12,
   1.652 +                                 &motion_error, recon_yoffset);
   1.653 +        if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.654 +          vp9_clear_system_state();  // __asm emms;
   1.655 +          motion_error *= error_weight;
   1.656 +        }
   1.657 +
   1.658 +        // If the current best reference mv is not centered on 0,0 then do a 0,0
   1.659 +        // based search as well.
   1.660 +        if (best_ref_mv.as_int) {
   1.661 +          tmp_err = INT_MAX;
   1.662 +          first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv,
   1.663 +                                   lst_yv12, &tmp_err, recon_yoffset);
   1.664 +          if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.665 +            vp9_clear_system_state();  // __asm emms;
   1.666 +            tmp_err *= error_weight;
   1.667 +          }
   1.668 +
   1.669 +          if (tmp_err < motion_error) {
   1.670 +            motion_error = tmp_err;
   1.671 +            mv.as_int = tmp_mv.as_int;
   1.672 +          }
   1.673 +        }
   1.674 +
   1.675 +        // Experimental search in an older reference frame
   1.676 +        if (cm->current_video_frame > 1) {
   1.677 +          // Simple 0,0 motion with no mv overhead
   1.678 +          zz_motion_search(cpi, x, gld_yv12,
   1.679 +                           &gf_motion_error, recon_yoffset);
   1.680 +
   1.681 +          first_pass_motion_search(cpi, x, &zero_ref_mv,
   1.682 +                                   &tmp_mv.as_mv, gld_yv12,
   1.683 +                                   &gf_motion_error, recon_yoffset);
   1.684 +          if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
   1.685 +            vp9_clear_system_state();  // __asm emms;
   1.686 +            gf_motion_error *= error_weight;
   1.687 +          }
   1.688 +
   1.689 +          if ((gf_motion_error < motion_error) &&
   1.690 +              (gf_motion_error < this_error)) {
   1.691 +            second_ref_count++;
   1.692 +          }
   1.693 +
   1.694 +          // Reset to last frame as reference buffer
   1.695 +          xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset;
   1.696 +          xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset;
   1.697 +          xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset;
   1.698 +
   1.699 +          // In accumulating a score for the older reference frame
   1.700 +          // take the best of the motion predicted score and
   1.701 +          // the intra coded error (just as will be done for)
   1.702 +          // accumulation of "coded_error" for the last frame.
   1.703 +          if (gf_motion_error < this_error)
   1.704 +            sr_coded_error += gf_motion_error;
   1.705 +          else
   1.706 +            sr_coded_error += this_error;
   1.707 +        } else {
   1.708 +          sr_coded_error += motion_error;
   1.709 +        }
   1.710 +        /* Intra assumed best */
   1.711 +        best_ref_mv.as_int = 0;
   1.712 +
   1.713 +        if (motion_error <= this_error) {
   1.714 +          // Keep a count of cases where the inter and intra were
   1.715 +          // very close and very low. This helps with scene cut
   1.716 +          // detection for example in cropped clips with black bars
   1.717 +          // at the sides or top and bottom.
   1.718 +          if ((((this_error - intrapenalty) * 9) <=
   1.719 +               (motion_error * 10)) &&
   1.720 +              (this_error < (2 * intrapenalty))) {
   1.721 +            neutral_count++;
   1.722 +          }
   1.723 +
   1.724 +          mv.as_mv.row *= 8;
   1.725 +          mv.as_mv.col *= 8;
   1.726 +          this_error = motion_error;
   1.727 +          vp9_set_mbmode_and_mvs(x, NEWMV, &mv);
   1.728 +          xd->mi_8x8[0]->mbmi.tx_size = TX_4X4;
   1.729 +          xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME;
   1.730 +          xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE;
   1.731 +          vp9_build_inter_predictors_sby(xd, mb_row << 1,
   1.732 +                                         mb_col << 1,
   1.733 +                                         xd->mi_8x8[0]->mbmi.sb_type);
   1.734 +          vp9_encode_sby(x, xd->mi_8x8[0]->mbmi.sb_type);
   1.735 +          sum_mvr += mv.as_mv.row;
   1.736 +          sum_mvr_abs += abs(mv.as_mv.row);
   1.737 +          sum_mvc += mv.as_mv.col;
   1.738 +          sum_mvc_abs += abs(mv.as_mv.col);
   1.739 +          sum_mvrs += mv.as_mv.row * mv.as_mv.row;
   1.740 +          sum_mvcs += mv.as_mv.col * mv.as_mv.col;
   1.741 +          intercount++;
   1.742 +
   1.743 +          best_ref_mv.as_int = mv.as_int;
   1.744 +
   1.745 +          // Was the vector non-zero
   1.746 +          if (mv.as_int) {
   1.747 +            mvcount++;
   1.748 +
   1.749 +            // Was it different from the last non zero vector
   1.750 +            if (mv.as_int != lastmv_as_int)
   1.751 +              new_mv_count++;
   1.752 +            lastmv_as_int = mv.as_int;
   1.753 +
   1.754 +            // Does the Row vector point inwards or outwards
   1.755 +            if (mb_row < cm->mb_rows / 2) {
   1.756 +              if (mv.as_mv.row > 0)
   1.757 +                sum_in_vectors--;
   1.758 +              else if (mv.as_mv.row < 0)
   1.759 +                sum_in_vectors++;
   1.760 +            } else if (mb_row > cm->mb_rows / 2) {
   1.761 +              if (mv.as_mv.row > 0)
   1.762 +                sum_in_vectors++;
   1.763 +              else if (mv.as_mv.row < 0)
   1.764 +                sum_in_vectors--;
   1.765 +            }
   1.766 +
   1.767 +            // Does the Row vector point inwards or outwards
   1.768 +            if (mb_col < cm->mb_cols / 2) {
   1.769 +              if (mv.as_mv.col > 0)
   1.770 +                sum_in_vectors--;
   1.771 +              else if (mv.as_mv.col < 0)
   1.772 +                sum_in_vectors++;
   1.773 +            } else if (mb_col > cm->mb_cols / 2) {
   1.774 +              if (mv.as_mv.col > 0)
   1.775 +                sum_in_vectors++;
   1.776 +              else if (mv.as_mv.col < 0)
   1.777 +                sum_in_vectors--;
   1.778 +            }
   1.779 +          }
   1.780 +        }
   1.781 +      } else {
   1.782 +        sr_coded_error += (int64_t)this_error;
   1.783 +      }
   1.784 +      coded_error += (int64_t)this_error;
   1.785 +
   1.786 +      // adjust to the next column of macroblocks
   1.787 +      x->plane[0].src.buf += 16;
   1.788 +      x->plane[1].src.buf += 8;
   1.789 +      x->plane[2].src.buf += 8;
   1.790 +
   1.791 +      recon_yoffset += 16;
   1.792 +      recon_uvoffset += 8;
   1.793 +    }
   1.794 +
   1.795 +    // adjust to the next row of mbs
   1.796 +    x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
   1.797 +    x->plane[1].src.buf += 8 * x->plane[1].src.stride - 8 * cm->mb_cols;
   1.798 +    x->plane[2].src.buf += 8 * x->plane[1].src.stride - 8 * cm->mb_cols;
   1.799 +
   1.800 +    vp9_clear_system_state();  // __asm emms;
   1.801 +  }
   1.802 +
   1.803 +  vp9_clear_system_state();  // __asm emms;
   1.804 +  {
   1.805 +    double weight = 0.0;
   1.806 +
   1.807 +    FIRSTPASS_STATS fps;
   1.808 +
   1.809 +    fps.frame      = cm->current_video_frame;
   1.810 +    fps.intra_error = (double)(intra_error >> 8);
   1.811 +    fps.coded_error = (double)(coded_error >> 8);
   1.812 +    fps.sr_coded_error = (double)(sr_coded_error >> 8);
   1.813 +    weight = simple_weight(cpi->Source);
   1.814 +
   1.815 +
   1.816 +    if (weight < 0.1)
   1.817 +      weight = 0.1;
   1.818 +
   1.819 +    fps.ssim_weighted_pred_err = fps.coded_error * weight;
   1.820 +
   1.821 +    fps.pcnt_inter  = 0.0;
   1.822 +    fps.pcnt_motion = 0.0;
   1.823 +    fps.MVr        = 0.0;
   1.824 +    fps.mvr_abs     = 0.0;
   1.825 +    fps.MVc        = 0.0;
   1.826 +    fps.mvc_abs     = 0.0;
   1.827 +    fps.MVrv       = 0.0;
   1.828 +    fps.MVcv       = 0.0;
   1.829 +    fps.mv_in_out_count  = 0.0;
   1.830 +    fps.new_mv_count = 0.0;
   1.831 +    fps.count      = 1.0;
   1.832 +
   1.833 +    fps.pcnt_inter   = 1.0 * (double)intercount / cm->MBs;
   1.834 +    fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
   1.835 +    fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
   1.836 +
   1.837 +    if (mvcount > 0) {
   1.838 +      fps.MVr = (double)sum_mvr / (double)mvcount;
   1.839 +      fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
   1.840 +      fps.MVc = (double)sum_mvc / (double)mvcount;
   1.841 +      fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
   1.842 +      fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) /
   1.843 +                 (double)mvcount;
   1.844 +      fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) /
   1.845 +                 (double)mvcount;
   1.846 +      fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
   1.847 +      fps.new_mv_count = new_mv_count;
   1.848 +
   1.849 +      fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
   1.850 +    }
   1.851 +
   1.852 +    // TODO(paulwilkins):  Handle the case when duration is set to 0, or
   1.853 +    // something less than the full time between subsequent values of
   1.854 +    // cpi->source_time_stamp.
   1.855 +    fps.duration = (double)(cpi->source->ts_end
   1.856 +                            - cpi->source->ts_start);
   1.857 +
   1.858 +    // don't want to do output stats with a stack variable!
   1.859 +    cpi->twopass.this_frame_stats = fps;
   1.860 +    output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
   1.861 +    accumulate_stats(&cpi->twopass.total_stats, &fps);
   1.862 +  }
   1.863 +
   1.864 +  // Copy the previous Last Frame back into gf and and arf buffers if
   1.865 +  // the prediction is good enough... but also dont allow it to lag too far
   1.866 +  if ((cpi->twopass.sr_update_lag > 3) ||
   1.867 +      ((cm->current_video_frame > 0) &&
   1.868 +       (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
   1.869 +       ((cpi->twopass.this_frame_stats.intra_error /
   1.870 +         DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
   1.871 +        2.0))) {
   1.872 +    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
   1.873 +    cpi->twopass.sr_update_lag = 1;
   1.874 +  } else {
   1.875 +    cpi->twopass.sr_update_lag++;
   1.876 +  }
   1.877 +  // swap frame pointers so last frame refers to the frame we just compressed
   1.878 +  swap_yv12(lst_yv12, new_yv12);
   1.879 +
   1.880 +  vp9_extend_frame_borders(lst_yv12, cm->subsampling_x, cm->subsampling_y);
   1.881 +
   1.882 +  // Special case for the first frame. Copy into the GF buffer as a second
   1.883 +  // reference.
   1.884 +  if (cm->current_video_frame == 0)
   1.885 +    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
   1.886 +
   1.887 +  // use this to see what the first pass reconstruction looks like
   1.888 +  if (0) {
   1.889 +    char filename[512];
   1.890 +    FILE *recon_file;
   1.891 +    snprintf(filename, sizeof(filename), "enc%04d.yuv",
   1.892 +             (int)cm->current_video_frame);
   1.893 +
   1.894 +    if (cm->current_video_frame == 0)
   1.895 +      recon_file = fopen(filename, "wb");
   1.896 +    else
   1.897 +      recon_file = fopen(filename, "ab");
   1.898 +
   1.899 +    (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
   1.900 +    fclose(recon_file);
   1.901 +  }
   1.902 +
   1.903 +  cm->current_video_frame++;
   1.904 +}
   1.905 +
   1.906 +// Estimate a cost per mb attributable to overheads such as the coding of
   1.907 +// modes and motion vectors.
   1.908 +// Currently simplistic in its assumptions for testing.
   1.909 +//
   1.910 +
   1.911 +
   1.912 +static double bitcost(double prob) {
   1.913 +  return -(log(prob) / log(2.0));
   1.914 +}
   1.915 +
   1.916 +static int64_t estimate_modemvcost(VP9_COMP *cpi,
   1.917 +                                     FIRSTPASS_STATS *fpstats) {
   1.918 +#if 0
   1.919 +  int mv_cost;
   1.920 +  int mode_cost;
   1.921 +
   1.922 +  double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
   1.923 +  double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
   1.924 +  double av_intra = (1.0 - av_pct_inter);
   1.925 +
   1.926 +  double zz_cost;
   1.927 +  double motion_cost;
   1.928 +  double intra_cost;
   1.929 +
   1.930 +  zz_cost = bitcost(av_pct_inter - av_pct_motion);
   1.931 +  motion_cost = bitcost(av_pct_motion);
   1.932 +  intra_cost = bitcost(av_intra);
   1.933 +
   1.934 +  // Estimate of extra bits per mv overhead for mbs
   1.935 +  // << 9 is the normalization to the (bits * 512) used in vp9_bits_per_mb
   1.936 +  mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
   1.937 +
   1.938 +  // Crude estimate of overhead cost from modes
   1.939 +  // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb
   1.940 +  mode_cost =
   1.941 +    (int)((((av_pct_inter - av_pct_motion) * zz_cost) +
   1.942 +           (av_pct_motion * motion_cost) +
   1.943 +           (av_intra * intra_cost)) * cpi->common.MBs) << 9;
   1.944 +
   1.945 +  // return mv_cost + mode_cost;
   1.946 +  // TODO(paulwilkins): Fix overhead costs for extended Q range.
   1.947 +#endif
   1.948 +  return 0;
   1.949 +}
   1.950 +
   1.951 +static double calc_correction_factor(double err_per_mb,
   1.952 +                                     double err_divisor,
   1.953 +                                     double pt_low,
   1.954 +                                     double pt_high,
   1.955 +                                     int q) {
   1.956 +  const double error_term = err_per_mb / err_divisor;
   1.957 +
   1.958 +  // Adjustment based on actual quantizer to power term.
   1.959 +  const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.01 + pt_low,
   1.960 +                                pt_high);
   1.961 +
   1.962 +  // Calculate correction factor
   1.963 +  if (power_term < 1.0)
   1.964 +    assert(error_term >= 0.0);
   1.965 +
   1.966 +  return fclamp(pow(error_term, power_term), 0.05, 5.0);
   1.967 +}
   1.968 +
   1.969 +// Given a current maxQ value sets a range for future values.
   1.970 +// PGW TODO..
   1.971 +// This code removes direct dependency on QIndex to determine the range
   1.972 +// (now uses the actual quantizer) but has not been tuned.
   1.973 +static void adjust_maxq_qrange(VP9_COMP *cpi) {
   1.974 +  int i;
   1.975 +  // Set the max corresponding to cpi->avg_q * 2.0
   1.976 +  double q = cpi->avg_q * 2.0;
   1.977 +  cpi->twopass.maxq_max_limit = cpi->worst_quality;
   1.978 +  for (i = cpi->best_quality; i <= cpi->worst_quality; i++) {
   1.979 +    cpi->twopass.maxq_max_limit = i;
   1.980 +    if (vp9_convert_qindex_to_q(i) >= q)
   1.981 +      break;
   1.982 +  }
   1.983 +
   1.984 +  // Set the min corresponding to cpi->avg_q * 0.5
   1.985 +  q = cpi->avg_q * 0.5;
   1.986 +  cpi->twopass.maxq_min_limit = cpi->best_quality;
   1.987 +  for (i = cpi->worst_quality; i >= cpi->best_quality; i--) {
   1.988 +    cpi->twopass.maxq_min_limit = i;
   1.989 +    if (vp9_convert_qindex_to_q(i) <= q)
   1.990 +      break;
   1.991 +  }
   1.992 +}
   1.993 +
   1.994 +static int estimate_max_q(VP9_COMP *cpi,
   1.995 +                          FIRSTPASS_STATS *fpstats,
   1.996 +                          int section_target_bandwitdh) {
   1.997 +  int q;
   1.998 +  int num_mbs = cpi->common.MBs;
   1.999 +  int target_norm_bits_per_mb;
  1.1000 +
  1.1001 +  double section_err = fpstats->coded_error / fpstats->count;
  1.1002 +  double sr_correction;
  1.1003 +  double err_per_mb = section_err / num_mbs;
  1.1004 +  double err_correction_factor;
  1.1005 +  double speed_correction = 1.0;
  1.1006 +
  1.1007 +  if (section_target_bandwitdh <= 0)
  1.1008 +    return cpi->twopass.maxq_max_limit;          // Highest value allowed
  1.1009 +
  1.1010 +  target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20)
  1.1011 +                              ? (512 * section_target_bandwitdh) / num_mbs
  1.1012 +                              : 512 * (section_target_bandwitdh / num_mbs);
  1.1013 +
  1.1014 +  // Look at the drop in prediction quality between the last frame
  1.1015 +  // and the GF buffer (which contained an older frame).
  1.1016 +  if (fpstats->sr_coded_error > fpstats->coded_error) {
  1.1017 +    double sr_err_diff = (fpstats->sr_coded_error - fpstats->coded_error) /
  1.1018 +                             (fpstats->count * cpi->common.MBs);
  1.1019 +    sr_correction = fclamp(pow(sr_err_diff / 32.0, 0.25), 0.75, 1.25);
  1.1020 +  } else {
  1.1021 +    sr_correction = 0.75;
  1.1022 +  }
  1.1023 +
  1.1024 +  // Calculate a corrective factor based on a rolling ratio of bits spent
  1.1025 +  // vs target bits
  1.1026 +  if (cpi->rolling_target_bits > 0 &&
  1.1027 +      cpi->active_worst_quality < cpi->worst_quality) {
  1.1028 +    double rolling_ratio = (double)cpi->rolling_actual_bits /
  1.1029 +                               (double)cpi->rolling_target_bits;
  1.1030 +
  1.1031 +    if (rolling_ratio < 0.95)
  1.1032 +      cpi->twopass.est_max_qcorrection_factor -= 0.005;
  1.1033 +    else if (rolling_ratio > 1.05)
  1.1034 +      cpi->twopass.est_max_qcorrection_factor += 0.005;
  1.1035 +
  1.1036 +    cpi->twopass.est_max_qcorrection_factor = fclamp(
  1.1037 +        cpi->twopass.est_max_qcorrection_factor, 0.1, 10.0);
  1.1038 +  }
  1.1039 +
  1.1040 +  // Corrections for higher compression speed settings
  1.1041 +  // (reduced compression expected)
  1.1042 +  // FIXME(jimbankoski): Once we settle on vp9 speed features we need to
  1.1043 +  // change this code.
  1.1044 +  if (cpi->compressor_speed == 1)
  1.1045 +    speed_correction = cpi->oxcf.cpu_used <= 5 ?
  1.1046 +                          1.04 + (/*cpi->oxcf.cpu_used*/0 * 0.04) :
  1.1047 +                          1.25;
  1.1048 +
  1.1049 +  // Try and pick a max Q that will be high enough to encode the
  1.1050 +  // content at the given rate.
  1.1051 +  for (q = cpi->twopass.maxq_min_limit; q < cpi->twopass.maxq_max_limit; q++) {
  1.1052 +    int bits_per_mb_at_this_q;
  1.1053 +
  1.1054 +    err_correction_factor = calc_correction_factor(err_per_mb,
  1.1055 +                                                   ERR_DIVISOR, 0.4, 0.90, q) *
  1.1056 +                                sr_correction * speed_correction *
  1.1057 +                                cpi->twopass.est_max_qcorrection_factor;
  1.1058 +
  1.1059 +    bits_per_mb_at_this_q = vp9_bits_per_mb(INTER_FRAME, q,
  1.1060 +                                            err_correction_factor);
  1.1061 +
  1.1062 +    if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
  1.1063 +      break;
  1.1064 +  }
  1.1065 +
  1.1066 +  // Restriction on active max q for constrained quality mode.
  1.1067 +  if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
  1.1068 +      q < cpi->cq_target_quality)
  1.1069 +    q = cpi->cq_target_quality;
  1.1070 +
  1.1071 +  // Adjust maxq_min_limit and maxq_max_limit limits based on
  1.1072 +  // average q observed in clip for non kf/gf/arf frames
  1.1073 +  // Give average a chance to settle though.
  1.1074 +  // PGW TODO.. This code is broken for the extended Q range
  1.1075 +  if (cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8) &&
  1.1076 +      cpi->ni_frames > 25)
  1.1077 +    adjust_maxq_qrange(cpi);
  1.1078 +
  1.1079 +  return q;
  1.1080 +}
  1.1081 +
  1.1082 +// For cq mode estimate a cq level that matches the observed
  1.1083 +// complexity and data rate.
  1.1084 +static int estimate_cq(VP9_COMP *cpi,
  1.1085 +                       FIRSTPASS_STATS *fpstats,
  1.1086 +                       int section_target_bandwitdh) {
  1.1087 +  int q;
  1.1088 +  int num_mbs = cpi->common.MBs;
  1.1089 +  int target_norm_bits_per_mb;
  1.1090 +
  1.1091 +  double section_err = (fpstats->coded_error / fpstats->count);
  1.1092 +  double err_per_mb = section_err / num_mbs;
  1.1093 +  double err_correction_factor;
  1.1094 +  double sr_err_diff;
  1.1095 +  double sr_correction;
  1.1096 +  double speed_correction = 1.0;
  1.1097 +  double clip_iiratio;
  1.1098 +  double clip_iifactor;
  1.1099 +
  1.1100 +  target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
  1.1101 +                            ? (512 * section_target_bandwitdh) / num_mbs
  1.1102 +                            : 512 * (section_target_bandwitdh / num_mbs);
  1.1103 +
  1.1104 +
  1.1105 +  // Corrections for higher compression speed settings
  1.1106 +  // (reduced compression expected)
  1.1107 +  if (cpi->compressor_speed == 1) {
  1.1108 +    if (cpi->oxcf.cpu_used <= 5)
  1.1109 +      speed_correction = 1.04 + (/*cpi->oxcf.cpu_used*/ 0 * 0.04);
  1.1110 +    else
  1.1111 +      speed_correction = 1.25;
  1.1112 +  }
  1.1113 +
  1.1114 +  // Look at the drop in prediction quality between the last frame
  1.1115 +  // and the GF buffer (which contained an older frame).
  1.1116 +  if (fpstats->sr_coded_error > fpstats->coded_error) {
  1.1117 +    sr_err_diff =
  1.1118 +      (fpstats->sr_coded_error - fpstats->coded_error) /
  1.1119 +      (fpstats->count * cpi->common.MBs);
  1.1120 +    sr_correction = (sr_err_diff / 32.0);
  1.1121 +    sr_correction = pow(sr_correction, 0.25);
  1.1122 +    if (sr_correction < 0.75)
  1.1123 +      sr_correction = 0.75;
  1.1124 +    else if (sr_correction > 1.25)
  1.1125 +      sr_correction = 1.25;
  1.1126 +  } else {
  1.1127 +    sr_correction = 0.75;
  1.1128 +  }
  1.1129 +
  1.1130 +  // II ratio correction factor for clip as a whole
  1.1131 +  clip_iiratio = cpi->twopass.total_stats.intra_error /
  1.1132 +                 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
  1.1133 +  clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
  1.1134 +  if (clip_iifactor < 0.80)
  1.1135 +    clip_iifactor = 0.80;
  1.1136 +
  1.1137 +  // Try and pick a Q that can encode the content at the given rate.
  1.1138 +  for (q = 0; q < MAXQ; q++) {
  1.1139 +    int bits_per_mb_at_this_q;
  1.1140 +
  1.1141 +    // Error per MB based correction factor
  1.1142 +    err_correction_factor =
  1.1143 +      calc_correction_factor(err_per_mb, 100.0, 0.4, 0.90, q) *
  1.1144 +      sr_correction * speed_correction * clip_iifactor;
  1.1145 +
  1.1146 +    bits_per_mb_at_this_q =
  1.1147 +      vp9_bits_per_mb(INTER_FRAME, q, err_correction_factor);
  1.1148 +
  1.1149 +    if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
  1.1150 +      break;
  1.1151 +  }
  1.1152 +
  1.1153 +  // Clip value to range "best allowed to (worst allowed - 1)"
  1.1154 +  q = select_cq_level(q);
  1.1155 +  if (q >= cpi->worst_quality)
  1.1156 +    q = cpi->worst_quality - 1;
  1.1157 +  if (q < cpi->best_quality)
  1.1158 +    q = cpi->best_quality;
  1.1159 +
  1.1160 +  return q;
  1.1161 +}
  1.1162 +
  1.1163 +extern void vp9_new_framerate(VP9_COMP *cpi, double framerate);
  1.1164 +
  1.1165 +void vp9_init_second_pass(VP9_COMP *cpi) {
  1.1166 +  FIRSTPASS_STATS this_frame;
  1.1167 +  FIRSTPASS_STATS *start_pos;
  1.1168 +
  1.1169 +  double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
  1.1170 +  double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
  1.1171 +                                      cpi->oxcf.two_pass_vbrmin_section / 100);
  1.1172 +
  1.1173 +  if (two_pass_min_rate < lower_bounds_min_rate)
  1.1174 +    two_pass_min_rate = lower_bounds_min_rate;
  1.1175 +
  1.1176 +  zero_stats(&cpi->twopass.total_stats);
  1.1177 +  zero_stats(&cpi->twopass.total_left_stats);
  1.1178 +
  1.1179 +  if (!cpi->twopass.stats_in_end)
  1.1180 +    return;
  1.1181 +
  1.1182 +  cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
  1.1183 +  cpi->twopass.total_left_stats = cpi->twopass.total_stats;
  1.1184 +
  1.1185 +  // each frame can have a different duration, as the frame rate in the source
  1.1186 +  // isn't guaranteed to be constant.   The frame rate prior to the first frame
  1.1187 +  // encoded in the second pass is a guess.  However the sum duration is not.
  1.1188 +  // Its calculated based on the actual durations of all frames from the first
  1.1189 +  // pass.
  1.1190 +  vp9_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count /
  1.1191 +                       cpi->twopass.total_stats.duration);
  1.1192 +
  1.1193 +  cpi->output_framerate = cpi->oxcf.framerate;
  1.1194 +  cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration *
  1.1195 +                                     cpi->oxcf.target_bandwidth / 10000000.0);
  1.1196 +  cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration *
  1.1197 +                                      two_pass_min_rate / 10000000.0);
  1.1198 +
  1.1199 +  // Calculate a minimum intra value to be used in determining the IIratio
  1.1200 +  // scores used in the second pass. We have this minimum to make sure
  1.1201 +  // that clips that are static but "low complexity" in the intra domain
  1.1202 +  // are still boosted appropriately for KF/GF/ARF
  1.1203 +  cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
  1.1204 +  cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
  1.1205 +
  1.1206 +  // This variable monitors how far behind the second ref update is lagging
  1.1207 +  cpi->twopass.sr_update_lag = 1;
  1.1208 +
  1.1209 +  // Scan the first pass file and calculate an average Intra / Inter error score
  1.1210 +  // ratio for the sequence.
  1.1211 +  {
  1.1212 +    double sum_iiratio = 0.0;
  1.1213 +    double IIRatio;
  1.1214 +
  1.1215 +    start_pos = cpi->twopass.stats_in;  // Note the starting "file" position.
  1.1216 +
  1.1217 +    while (input_stats(cpi, &this_frame) != EOF) {
  1.1218 +      IIRatio = this_frame.intra_error
  1.1219 +                / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
  1.1220 +      IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
  1.1221 +      sum_iiratio += IIRatio;
  1.1222 +    }
  1.1223 +
  1.1224 +    cpi->twopass.avg_iiratio = sum_iiratio /
  1.1225 +        DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
  1.1226 +
  1.1227 +    // Reset file position
  1.1228 +    reset_fpf_position(cpi, start_pos);
  1.1229 +  }
  1.1230 +
  1.1231 +  // Scan the first pass file and calculate a modified total error based upon
  1.1232 +  // the bias/power function used to allocate bits.
  1.1233 +  {
  1.1234 +    start_pos = cpi->twopass.stats_in;  // Note starting "file" position
  1.1235 +
  1.1236 +    cpi->twopass.modified_error_total = 0.0;
  1.1237 +    cpi->twopass.modified_error_used = 0.0;
  1.1238 +
  1.1239 +    while (input_stats(cpi, &this_frame) != EOF) {
  1.1240 +      cpi->twopass.modified_error_total +=
  1.1241 +          calculate_modified_err(cpi, &this_frame);
  1.1242 +    }
  1.1243 +    cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
  1.1244 +
  1.1245 +    reset_fpf_position(cpi, start_pos);  // Reset file position
  1.1246 +  }
  1.1247 +}
  1.1248 +
  1.1249 +void vp9_end_second_pass(VP9_COMP *cpi) {
  1.1250 +}
  1.1251 +
  1.1252 +// This function gives and estimate of how badly we believe
  1.1253 +// the prediction quality is decaying from frame to frame.
  1.1254 +static double get_prediction_decay_rate(VP9_COMP *cpi,
  1.1255 +                                        FIRSTPASS_STATS *next_frame) {
  1.1256 +  double prediction_decay_rate;
  1.1257 +  double second_ref_decay;
  1.1258 +  double mb_sr_err_diff;
  1.1259 +
  1.1260 +  // Initial basis is the % mbs inter coded
  1.1261 +  prediction_decay_rate = next_frame->pcnt_inter;
  1.1262 +
  1.1263 +  // Look at the observed drop in prediction quality between the last frame
  1.1264 +  // and the GF buffer (which contains an older frame).
  1.1265 +  mb_sr_err_diff = (next_frame->sr_coded_error - next_frame->coded_error) /
  1.1266 +                   cpi->common.MBs;
  1.1267 +  if (mb_sr_err_diff <= 512.0) {
  1.1268 +    second_ref_decay = 1.0 - (mb_sr_err_diff / 512.0);
  1.1269 +    second_ref_decay = pow(second_ref_decay, 0.5);
  1.1270 +    if (second_ref_decay < 0.85)
  1.1271 +      second_ref_decay = 0.85;
  1.1272 +    else if (second_ref_decay > 1.0)
  1.1273 +      second_ref_decay = 1.0;
  1.1274 +  } else {
  1.1275 +    second_ref_decay = 0.85;
  1.1276 +  }
  1.1277 +
  1.1278 +  if (second_ref_decay < prediction_decay_rate)
  1.1279 +    prediction_decay_rate = second_ref_decay;
  1.1280 +
  1.1281 +  return prediction_decay_rate;
  1.1282 +}
  1.1283 +
  1.1284 +// Function to test for a condition where a complex transition is followed
  1.1285 +// by a static section. For example in slide shows where there is a fade
  1.1286 +// between slides. This is to help with more optimal kf and gf positioning.
  1.1287 +static int detect_transition_to_still(
  1.1288 +  VP9_COMP *cpi,
  1.1289 +  int frame_interval,
  1.1290 +  int still_interval,
  1.1291 +  double loop_decay_rate,
  1.1292 +  double last_decay_rate) {
  1.1293 +  int trans_to_still = 0;
  1.1294 +
  1.1295 +  // Break clause to detect very still sections after motion
  1.1296 +  // For example a static image after a fade or other transition
  1.1297 +  // instead of a clean scene cut.
  1.1298 +  if (frame_interval > MIN_GF_INTERVAL &&
  1.1299 +      loop_decay_rate >= 0.999 &&
  1.1300 +      last_decay_rate < 0.9) {
  1.1301 +    int j;
  1.1302 +    FIRSTPASS_STATS *position = cpi->twopass.stats_in;
  1.1303 +    FIRSTPASS_STATS tmp_next_frame;
  1.1304 +    double zz_inter;
  1.1305 +
  1.1306 +    // Look ahead a few frames to see if static condition
  1.1307 +    // persists...
  1.1308 +    for (j = 0; j < still_interval; j++) {
  1.1309 +      if (EOF == input_stats(cpi, &tmp_next_frame))
  1.1310 +        break;
  1.1311 +
  1.1312 +      zz_inter =
  1.1313 +        (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion);
  1.1314 +      if (zz_inter < 0.999)
  1.1315 +        break;
  1.1316 +    }
  1.1317 +    // Reset file position
  1.1318 +    reset_fpf_position(cpi, position);
  1.1319 +
  1.1320 +    // Only if it does do we signal a transition to still
  1.1321 +    if (j == still_interval)
  1.1322 +      trans_to_still = 1;
  1.1323 +  }
  1.1324 +
  1.1325 +  return trans_to_still;
  1.1326 +}
  1.1327 +
  1.1328 +// This function detects a flash through the high relative pcnt_second_ref
  1.1329 +// score in the frame following a flash frame. The offset passed in should
  1.1330 +// reflect this
  1.1331 +static int detect_flash(VP9_COMP *cpi, int offset) {
  1.1332 +  FIRSTPASS_STATS next_frame;
  1.1333 +
  1.1334 +  int flash_detected = 0;
  1.1335 +
  1.1336 +  // Read the frame data.
  1.1337 +  // The return is FALSE (no flash detected) if not a valid frame
  1.1338 +  if (read_frame_stats(cpi, &next_frame, offset) != EOF) {
  1.1339 +    // What we are looking for here is a situation where there is a
  1.1340 +    // brief break in prediction (such as a flash) but subsequent frames
  1.1341 +    // are reasonably well predicted by an earlier (pre flash) frame.
  1.1342 +    // The recovery after a flash is indicated by a high pcnt_second_ref
  1.1343 +    // comapred to pcnt_inter.
  1.1344 +    if (next_frame.pcnt_second_ref > next_frame.pcnt_inter &&
  1.1345 +        next_frame.pcnt_second_ref >= 0.5)
  1.1346 +      flash_detected = 1;
  1.1347 +  }
  1.1348 +
  1.1349 +  return flash_detected;
  1.1350 +}
  1.1351 +
  1.1352 +// Update the motion related elements to the GF arf boost calculation
  1.1353 +static void accumulate_frame_motion_stats(
  1.1354 +  FIRSTPASS_STATS *this_frame,
  1.1355 +  double *this_frame_mv_in_out,
  1.1356 +  double *mv_in_out_accumulator,
  1.1357 +  double *abs_mv_in_out_accumulator,
  1.1358 +  double *mv_ratio_accumulator) {
  1.1359 +  // double this_frame_mv_in_out;
  1.1360 +  double this_frame_mvr_ratio;
  1.1361 +  double this_frame_mvc_ratio;
  1.1362 +  double motion_pct;
  1.1363 +
  1.1364 +  // Accumulate motion stats.
  1.1365 +  motion_pct = this_frame->pcnt_motion;
  1.1366 +
  1.1367 +  // Accumulate Motion In/Out of frame stats
  1.1368 +  *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
  1.1369 +  *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
  1.1370 +  *abs_mv_in_out_accumulator +=
  1.1371 +    fabs(this_frame->mv_in_out_count * motion_pct);
  1.1372 +
  1.1373 +  // Accumulate a measure of how uniform (or conversely how random)
  1.1374 +  // the motion field is. (A ratio of absmv / mv)
  1.1375 +  if (motion_pct > 0.05) {
  1.1376 +    this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
  1.1377 +                           DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
  1.1378 +
  1.1379 +    this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
  1.1380 +                           DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
  1.1381 +
  1.1382 +    *mv_ratio_accumulator +=
  1.1383 +      (this_frame_mvr_ratio < this_frame->mvr_abs)
  1.1384 +      ? (this_frame_mvr_ratio * motion_pct)
  1.1385 +      : this_frame->mvr_abs * motion_pct;
  1.1386 +
  1.1387 +    *mv_ratio_accumulator +=
  1.1388 +      (this_frame_mvc_ratio < this_frame->mvc_abs)
  1.1389 +      ? (this_frame_mvc_ratio * motion_pct)
  1.1390 +      : this_frame->mvc_abs * motion_pct;
  1.1391 +  }
  1.1392 +}
  1.1393 +
  1.1394 +// Calculate a baseline boost number for the current frame.
  1.1395 +static double calc_frame_boost(
  1.1396 +  VP9_COMP *cpi,
  1.1397 +  FIRSTPASS_STATS *this_frame,
  1.1398 +  double this_frame_mv_in_out) {
  1.1399 +  double frame_boost;
  1.1400 +
  1.1401 +  // Underlying boost factor is based on inter intra error ratio
  1.1402 +  if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
  1.1403 +    frame_boost = (IIFACTOR * this_frame->intra_error /
  1.1404 +                   DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
  1.1405 +  else
  1.1406 +    frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
  1.1407 +                   DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
  1.1408 +
  1.1409 +  // Increase boost for frames where new data coming into frame
  1.1410 +  // (eg zoom out). Slightly reduce boost if there is a net balance
  1.1411 +  // of motion out of the frame (zoom in).
  1.1412 +  // The range for this_frame_mv_in_out is -1.0 to +1.0
  1.1413 +  if (this_frame_mv_in_out > 0.0)
  1.1414 +    frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
  1.1415 +  // In extreme case boost is halved
  1.1416 +  else
  1.1417 +    frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
  1.1418 +
  1.1419 +  // Clip to maximum
  1.1420 +  if (frame_boost > GF_RMAX)
  1.1421 +    frame_boost = GF_RMAX;
  1.1422 +
  1.1423 +  return frame_boost;
  1.1424 +}
  1.1425 +
  1.1426 +static int calc_arf_boost(VP9_COMP *cpi, int offset,
  1.1427 +                          int f_frames, int b_frames,
  1.1428 +                          int *f_boost, int *b_boost) {
  1.1429 +  FIRSTPASS_STATS this_frame;
  1.1430 +
  1.1431 +  int i;
  1.1432 +  double boost_score = 0.0;
  1.1433 +  double mv_ratio_accumulator = 0.0;
  1.1434 +  double decay_accumulator = 1.0;
  1.1435 +  double this_frame_mv_in_out = 0.0;
  1.1436 +  double mv_in_out_accumulator = 0.0;
  1.1437 +  double abs_mv_in_out_accumulator = 0.0;
  1.1438 +  int arf_boost;
  1.1439 +  int flash_detected = 0;
  1.1440 +
  1.1441 +  // Search forward from the proposed arf/next gf position
  1.1442 +  for (i = 0; i < f_frames; i++) {
  1.1443 +    if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF)
  1.1444 +      break;
  1.1445 +
  1.1446 +    // Update the motion related elements to the boost calculation
  1.1447 +    accumulate_frame_motion_stats(&this_frame,
  1.1448 +                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
  1.1449 +                                  &abs_mv_in_out_accumulator,
  1.1450 +                                  &mv_ratio_accumulator);
  1.1451 +
  1.1452 +    // We want to discount the flash frame itself and the recovery
  1.1453 +    // frame that follows as both will have poor scores.
  1.1454 +    flash_detected = detect_flash(cpi, (i + offset)) ||
  1.1455 +                     detect_flash(cpi, (i + offset + 1));
  1.1456 +
  1.1457 +    // Cumulative effect of prediction quality decay
  1.1458 +    if (!flash_detected) {
  1.1459 +      decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame);
  1.1460 +      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
  1.1461 +                          ? MIN_DECAY_FACTOR : decay_accumulator;
  1.1462 +    }
  1.1463 +
  1.1464 +    boost_score += (decay_accumulator *
  1.1465 +                    calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
  1.1466 +  }
  1.1467 +
  1.1468 +  *f_boost = (int)boost_score;
  1.1469 +
  1.1470 +  // Reset for backward looking loop
  1.1471 +  boost_score = 0.0;
  1.1472 +  mv_ratio_accumulator = 0.0;
  1.1473 +  decay_accumulator = 1.0;
  1.1474 +  this_frame_mv_in_out = 0.0;
  1.1475 +  mv_in_out_accumulator = 0.0;
  1.1476 +  abs_mv_in_out_accumulator = 0.0;
  1.1477 +
  1.1478 +  // Search backward towards last gf position
  1.1479 +  for (i = -1; i >= -b_frames; i--) {
  1.1480 +    if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF)
  1.1481 +      break;
  1.1482 +
  1.1483 +    // Update the motion related elements to the boost calculation
  1.1484 +    accumulate_frame_motion_stats(&this_frame,
  1.1485 +                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
  1.1486 +                                  &abs_mv_in_out_accumulator,
  1.1487 +                                  &mv_ratio_accumulator);
  1.1488 +
  1.1489 +    // We want to discount the the flash frame itself and the recovery
  1.1490 +    // frame that follows as both will have poor scores.
  1.1491 +    flash_detected = detect_flash(cpi, (i + offset)) ||
  1.1492 +                     detect_flash(cpi, (i + offset + 1));
  1.1493 +
  1.1494 +    // Cumulative effect of prediction quality decay
  1.1495 +    if (!flash_detected) {
  1.1496 +      decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame);
  1.1497 +      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
  1.1498 +                              ? MIN_DECAY_FACTOR : decay_accumulator;
  1.1499 +    }
  1.1500 +
  1.1501 +    boost_score += (decay_accumulator *
  1.1502 +                    calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
  1.1503 +  }
  1.1504 +  *b_boost = (int)boost_score;
  1.1505 +
  1.1506 +  arf_boost = (*f_boost + *b_boost);
  1.1507 +  if (arf_boost < ((b_frames + f_frames) * 20))
  1.1508 +    arf_boost = ((b_frames + f_frames) * 20);
  1.1509 +
  1.1510 +  return arf_boost;
  1.1511 +}
  1.1512 +
  1.1513 +#if CONFIG_MULTIPLE_ARF
  1.1514 +// Work out the frame coding order for a GF or an ARF group.
  1.1515 +// The current implementation codes frames in their natural order for a
  1.1516 +// GF group, and inserts additional ARFs into an ARF group using a
  1.1517 +// binary split approach.
  1.1518 +// NOTE: this function is currently implemented recursively.
  1.1519 +static void schedule_frames(VP9_COMP *cpi, const int start, const int end,
  1.1520 +                            const int arf_idx, const int gf_or_arf_group,
  1.1521 +                            const int level) {
  1.1522 +  int i, abs_end, half_range;
  1.1523 +  int *cfo = cpi->frame_coding_order;
  1.1524 +  int idx = cpi->new_frame_coding_order_period;
  1.1525 +
  1.1526 +  // If (end < 0) an ARF should be coded at position (-end).
  1.1527 +  assert(start >= 0);
  1.1528 +
  1.1529 +  // printf("start:%d end:%d\n", start, end);
  1.1530 +
  1.1531 +  // GF Group: code frames in logical order.
  1.1532 +  if (gf_or_arf_group == 0) {
  1.1533 +    assert(end >= start);
  1.1534 +    for (i = start; i <= end; ++i) {
  1.1535 +      cfo[idx] = i;
  1.1536 +      cpi->arf_buffer_idx[idx] = arf_idx;
  1.1537 +      cpi->arf_weight[idx] = -1;
  1.1538 +      ++idx;
  1.1539 +    }
  1.1540 +    cpi->new_frame_coding_order_period = idx;
  1.1541 +    return;
  1.1542 +  }
  1.1543 +
  1.1544 +  // ARF Group: work out the ARF schedule.
  1.1545 +  // Mark ARF frames as negative.
  1.1546 +  if (end < 0) {
  1.1547 +    // printf("start:%d end:%d\n", -end, -end);
  1.1548 +    // ARF frame is at the end of the range.
  1.1549 +    cfo[idx] = end;
  1.1550 +    // What ARF buffer does this ARF use as predictor.
  1.1551 +    cpi->arf_buffer_idx[idx] = (arf_idx > 2) ? (arf_idx - 1) : 2;
  1.1552 +    cpi->arf_weight[idx] = level;
  1.1553 +    ++idx;
  1.1554 +    abs_end = -end;
  1.1555 +  } else {
  1.1556 +    abs_end = end;
  1.1557 +  }
  1.1558 +
  1.1559 +  half_range = (abs_end - start) >> 1;
  1.1560 +
  1.1561 +  // ARFs may not be adjacent, they must be separated by at least
  1.1562 +  // MIN_GF_INTERVAL non-ARF frames.
  1.1563 +  if ((start + MIN_GF_INTERVAL) >= (abs_end - MIN_GF_INTERVAL)) {
  1.1564 +    // printf("start:%d end:%d\n", start, abs_end);
  1.1565 +    // Update the coding order and active ARF.
  1.1566 +    for (i = start; i <= abs_end; ++i) {
  1.1567 +      cfo[idx] = i;
  1.1568 +      cpi->arf_buffer_idx[idx] = arf_idx;
  1.1569 +      cpi->arf_weight[idx] = -1;
  1.1570 +      ++idx;
  1.1571 +    }
  1.1572 +    cpi->new_frame_coding_order_period = idx;
  1.1573 +  } else {
  1.1574 +    // Place a new ARF at the mid-point of the range.
  1.1575 +    cpi->new_frame_coding_order_period = idx;
  1.1576 +    schedule_frames(cpi, start, -(start + half_range), arf_idx + 1,
  1.1577 +                    gf_or_arf_group, level + 1);
  1.1578 +    schedule_frames(cpi, start + half_range + 1, abs_end, arf_idx,
  1.1579 +                    gf_or_arf_group, level + 1);
  1.1580 +  }
  1.1581 +}
  1.1582 +
  1.1583 +#define FIXED_ARF_GROUP_SIZE 16
  1.1584 +
  1.1585 +void define_fixed_arf_period(VP9_COMP *cpi) {
  1.1586 +  int i;
  1.1587 +  int max_level = INT_MIN;
  1.1588 +
  1.1589 +  assert(cpi->multi_arf_enabled);
  1.1590 +  assert(cpi->oxcf.lag_in_frames >= FIXED_ARF_GROUP_SIZE);
  1.1591 +
  1.1592 +  // Save the weight of the last frame in the sequence before next
  1.1593 +  // sequence pattern overwrites it.
  1.1594 +  cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
  1.1595 +  assert(cpi->this_frame_weight >= 0);
  1.1596 +
  1.1597 +  // Initialize frame coding order variables.
  1.1598 +  cpi->new_frame_coding_order_period = 0;
  1.1599 +  cpi->next_frame_in_order = 0;
  1.1600 +  cpi->arf_buffered = 0;
  1.1601 +  vp9_zero(cpi->frame_coding_order);
  1.1602 +  vp9_zero(cpi->arf_buffer_idx);
  1.1603 +  vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
  1.1604 +
  1.1605 +  if (cpi->twopass.frames_to_key <= (FIXED_ARF_GROUP_SIZE + 8)) {
  1.1606 +    // Setup a GF group close to the keyframe.
  1.1607 +    cpi->source_alt_ref_pending = 0;
  1.1608 +    cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
  1.1609 +    schedule_frames(cpi, 0, (cpi->baseline_gf_interval - 1), 2, 0, 0);
  1.1610 +  } else {
  1.1611 +    // Setup a fixed period ARF group.
  1.1612 +    cpi->source_alt_ref_pending = 1;
  1.1613 +    cpi->baseline_gf_interval = FIXED_ARF_GROUP_SIZE;
  1.1614 +    schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0);
  1.1615 +  }
  1.1616 +
  1.1617 +  // Replace level indicator of -1 with correct level.
  1.1618 +  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1619 +    if (cpi->arf_weight[i] > max_level) {
  1.1620 +      max_level = cpi->arf_weight[i];
  1.1621 +    }
  1.1622 +  }
  1.1623 +  ++max_level;
  1.1624 +  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1625 +    if (cpi->arf_weight[i] == -1) {
  1.1626 +      cpi->arf_weight[i] = max_level;
  1.1627 +    }
  1.1628 +  }
  1.1629 +  cpi->max_arf_level = max_level;
  1.1630 +#if 0
  1.1631 +  printf("\nSchedule: ");
  1.1632 +  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1633 +    printf("%4d ", cpi->frame_coding_order[i]);
  1.1634 +  }
  1.1635 +  printf("\n");
  1.1636 +  printf("ARFref:   ");
  1.1637 +  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1638 +    printf("%4d ", cpi->arf_buffer_idx[i]);
  1.1639 +  }
  1.1640 +  printf("\n");
  1.1641 +  printf("Weight:   ");
  1.1642 +  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1643 +    printf("%4d ", cpi->arf_weight[i]);
  1.1644 +  }
  1.1645 +  printf("\n");
  1.1646 +#endif
  1.1647 +}
  1.1648 +#endif
  1.1649 +
  1.1650 +// Analyse and define a gf/arf group.
  1.1651 +static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  1.1652 +  FIRSTPASS_STATS next_frame = { 0 };
  1.1653 +  FIRSTPASS_STATS *start_pos;
  1.1654 +  int i;
  1.1655 +  double boost_score = 0.0;
  1.1656 +  double old_boost_score = 0.0;
  1.1657 +  double gf_group_err = 0.0;
  1.1658 +  double gf_first_frame_err = 0.0;
  1.1659 +  double mod_frame_err = 0.0;
  1.1660 +
  1.1661 +  double mv_ratio_accumulator = 0.0;
  1.1662 +  double decay_accumulator = 1.0;
  1.1663 +  double zero_motion_accumulator = 1.0;
  1.1664 +
  1.1665 +  double loop_decay_rate = 1.00;          // Starting decay rate
  1.1666 +  double last_loop_decay_rate = 1.00;
  1.1667 +
  1.1668 +  double this_frame_mv_in_out = 0.0;
  1.1669 +  double mv_in_out_accumulator = 0.0;
  1.1670 +  double abs_mv_in_out_accumulator = 0.0;
  1.1671 +  double mv_ratio_accumulator_thresh;
  1.1672 +  int max_bits = frame_max_bits(cpi);     // Max for a single frame
  1.1673 +
  1.1674 +  unsigned int allow_alt_ref =
  1.1675 +    cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
  1.1676 +
  1.1677 +  int f_boost = 0;
  1.1678 +  int b_boost = 0;
  1.1679 +  int flash_detected;
  1.1680 +  int active_max_gf_interval;
  1.1681 +
  1.1682 +  cpi->twopass.gf_group_bits = 0;
  1.1683 +
  1.1684 +  vp9_clear_system_state();  // __asm emms;
  1.1685 +
  1.1686 +  start_pos = cpi->twopass.stats_in;
  1.1687 +
  1.1688 +  // Load stats for the current frame.
  1.1689 +  mod_frame_err = calculate_modified_err(cpi, this_frame);
  1.1690 +
  1.1691 +  // Note the error of the frame at the start of the group (this will be
  1.1692 +  // the GF frame error if we code a normal gf
  1.1693 +  gf_first_frame_err = mod_frame_err;
  1.1694 +
  1.1695 +  // Special treatment if the current frame is a key frame (which is also
  1.1696 +  // a gf). If it is then its error score (and hence bit allocation) need
  1.1697 +  // to be subtracted out from the calculation for the GF group
  1.1698 +  if (cpi->common.frame_type == KEY_FRAME)
  1.1699 +    gf_group_err -= gf_first_frame_err;
  1.1700 +
  1.1701 +  // Motion breakout threshold for loop below depends on image size.
  1.1702 +  mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0;
  1.1703 +
  1.1704 +  // Work out a maximum interval for the GF.
  1.1705 +  // If the image appears completely static we can extend beyond this.
  1.1706 +  // The value chosen depends on the active Q range. At low Q we have
  1.1707 +  // bits to spare and are better with a smaller interval and smaller boost.
  1.1708 +  // At high Q when there are few bits to spare we are better with a longer
  1.1709 +  // interval to spread the cost of the GF.
  1.1710 +  active_max_gf_interval =
  1.1711 +    12 + ((int)vp9_convert_qindex_to_q(cpi->active_worst_quality) >> 5);
  1.1712 +
  1.1713 +  if (active_max_gf_interval > cpi->max_gf_interval)
  1.1714 +    active_max_gf_interval = cpi->max_gf_interval;
  1.1715 +
  1.1716 +  i = 0;
  1.1717 +  while (((i < cpi->twopass.static_scene_max_gf_interval) ||
  1.1718 +          ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
  1.1719 +         (i < cpi->twopass.frames_to_key)) {
  1.1720 +    i++;    // Increment the loop counter
  1.1721 +
  1.1722 +    // Accumulate error score of frames in this gf group
  1.1723 +    mod_frame_err = calculate_modified_err(cpi, this_frame);
  1.1724 +    gf_group_err += mod_frame_err;
  1.1725 +
  1.1726 +    if (EOF == input_stats(cpi, &next_frame))
  1.1727 +      break;
  1.1728 +
  1.1729 +    // Test for the case where there is a brief flash but the prediction
  1.1730 +    // quality back to an earlier frame is then restored.
  1.1731 +    flash_detected = detect_flash(cpi, 0);
  1.1732 +
  1.1733 +    // Update the motion related elements to the boost calculation
  1.1734 +    accumulate_frame_motion_stats(&next_frame,
  1.1735 +                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
  1.1736 +                                  &abs_mv_in_out_accumulator,
  1.1737 +                                  &mv_ratio_accumulator);
  1.1738 +
  1.1739 +    // Cumulative effect of prediction quality decay
  1.1740 +    if (!flash_detected) {
  1.1741 +      last_loop_decay_rate = loop_decay_rate;
  1.1742 +      loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  1.1743 +      decay_accumulator = decay_accumulator * loop_decay_rate;
  1.1744 +
  1.1745 +      // Monitor for static sections.
  1.1746 +      if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
  1.1747 +          zero_motion_accumulator) {
  1.1748 +        zero_motion_accumulator =
  1.1749 +          (next_frame.pcnt_inter - next_frame.pcnt_motion);
  1.1750 +      }
  1.1751 +
  1.1752 +      // Break clause to detect very still sections after motion
  1.1753 +      // (for example a static image after a fade or other transition).
  1.1754 +      if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
  1.1755 +                                     last_loop_decay_rate)) {
  1.1756 +        allow_alt_ref = 0;
  1.1757 +        break;
  1.1758 +      }
  1.1759 +    }
  1.1760 +
  1.1761 +    // Calculate a boost number for this frame
  1.1762 +    boost_score +=
  1.1763 +      (decay_accumulator *
  1.1764 +       calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out));
  1.1765 +
  1.1766 +    // Break out conditions.
  1.1767 +    if (
  1.1768 +      // Break at cpi->max_gf_interval unless almost totally static
  1.1769 +      (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
  1.1770 +      (
  1.1771 +        // Don't break out with a very short interval
  1.1772 +        (i > MIN_GF_INTERVAL) &&
  1.1773 +        // Don't break out very close to a key frame
  1.1774 +        ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
  1.1775 +        ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) &&
  1.1776 +        (!flash_detected) &&
  1.1777 +        ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
  1.1778 +         (abs_mv_in_out_accumulator > 3.0) ||
  1.1779 +         (mv_in_out_accumulator < -2.0) ||
  1.1780 +         ((boost_score - old_boost_score) < IIFACTOR)))) {
  1.1781 +      boost_score = old_boost_score;
  1.1782 +      break;
  1.1783 +    }
  1.1784 +
  1.1785 +    *this_frame = next_frame;
  1.1786 +
  1.1787 +    old_boost_score = boost_score;
  1.1788 +  }
  1.1789 +
  1.1790 +  cpi->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
  1.1791 +
  1.1792 +  // Don't allow a gf too near the next kf
  1.1793 +  if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) {
  1.1794 +    while (i < cpi->twopass.frames_to_key) {
  1.1795 +      i++;
  1.1796 +
  1.1797 +      if (EOF == input_stats(cpi, this_frame))
  1.1798 +        break;
  1.1799 +
  1.1800 +      if (i < cpi->twopass.frames_to_key) {
  1.1801 +        mod_frame_err = calculate_modified_err(cpi, this_frame);
  1.1802 +        gf_group_err += mod_frame_err;
  1.1803 +      }
  1.1804 +    }
  1.1805 +  }
  1.1806 +
  1.1807 +  // Set the interval until the next gf or arf.
  1.1808 +  cpi->baseline_gf_interval = i;
  1.1809 +
  1.1810 +#if CONFIG_MULTIPLE_ARF
  1.1811 +  if (cpi->multi_arf_enabled) {
  1.1812 +    // Initialize frame coding order variables.
  1.1813 +    cpi->new_frame_coding_order_period = 0;
  1.1814 +    cpi->next_frame_in_order = 0;
  1.1815 +    cpi->arf_buffered = 0;
  1.1816 +    vp9_zero(cpi->frame_coding_order);
  1.1817 +    vp9_zero(cpi->arf_buffer_idx);
  1.1818 +    vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
  1.1819 +  }
  1.1820 +#endif
  1.1821 +
  1.1822 +  // Should we use the alternate reference frame
  1.1823 +  if (allow_alt_ref &&
  1.1824 +      (i < cpi->oxcf.lag_in_frames) &&
  1.1825 +      (i >= MIN_GF_INTERVAL) &&
  1.1826 +      // dont use ARF very near next kf
  1.1827 +      (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
  1.1828 +      ((next_frame.pcnt_inter > 0.75) ||
  1.1829 +       (next_frame.pcnt_second_ref > 0.5)) &&
  1.1830 +      ((mv_in_out_accumulator / (double)i > -0.2) ||
  1.1831 +       (mv_in_out_accumulator > -2.0)) &&
  1.1832 +      (boost_score > 100)) {
  1.1833 +    // Alternative boost calculation for alt ref
  1.1834 +    cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
  1.1835 +                                    &b_boost);
  1.1836 +    cpi->source_alt_ref_pending = 1;
  1.1837 +
  1.1838 +#if CONFIG_MULTIPLE_ARF
  1.1839 +    // Set the ARF schedule.
  1.1840 +    if (cpi->multi_arf_enabled) {
  1.1841 +      schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0);
  1.1842 +    }
  1.1843 +#endif
  1.1844 +  } else {
  1.1845 +    cpi->gfu_boost = (int)boost_score;
  1.1846 +    cpi->source_alt_ref_pending = 0;
  1.1847 +#if CONFIG_MULTIPLE_ARF
  1.1848 +    // Set the GF schedule.
  1.1849 +    if (cpi->multi_arf_enabled) {
  1.1850 +      schedule_frames(cpi, 0, cpi->baseline_gf_interval - 1, 2, 0, 0);
  1.1851 +      assert(cpi->new_frame_coding_order_period == cpi->baseline_gf_interval);
  1.1852 +    }
  1.1853 +#endif
  1.1854 +  }
  1.1855 +
  1.1856 +#if CONFIG_MULTIPLE_ARF
  1.1857 +  if (cpi->multi_arf_enabled && (cpi->common.frame_type != KEY_FRAME)) {
  1.1858 +    int max_level = INT_MIN;
  1.1859 +    // Replace level indicator of -1 with correct level.
  1.1860 +    for (i = 0; i < cpi->frame_coding_order_period; ++i) {
  1.1861 +      if (cpi->arf_weight[i] > max_level) {
  1.1862 +        max_level = cpi->arf_weight[i];
  1.1863 +      }
  1.1864 +    }
  1.1865 +    ++max_level;
  1.1866 +    for (i = 0; i < cpi->frame_coding_order_period; ++i) {
  1.1867 +      if (cpi->arf_weight[i] == -1) {
  1.1868 +        cpi->arf_weight[i] = max_level;
  1.1869 +      }
  1.1870 +    }
  1.1871 +    cpi->max_arf_level = max_level;
  1.1872 +  }
  1.1873 +#if 0
  1.1874 +  if (cpi->multi_arf_enabled) {
  1.1875 +    printf("\nSchedule: ");
  1.1876 +    for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1877 +      printf("%4d ", cpi->frame_coding_order[i]);
  1.1878 +    }
  1.1879 +    printf("\n");
  1.1880 +    printf("ARFref:   ");
  1.1881 +    for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1882 +      printf("%4d ", cpi->arf_buffer_idx[i]);
  1.1883 +    }
  1.1884 +    printf("\n");
  1.1885 +    printf("Weight:   ");
  1.1886 +    for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
  1.1887 +      printf("%4d ", cpi->arf_weight[i]);
  1.1888 +    }
  1.1889 +    printf("\n");
  1.1890 +  }
  1.1891 +#endif
  1.1892 +#endif
  1.1893 +
  1.1894 +  // Now decide how many bits should be allocated to the GF group as  a
  1.1895 +  // proportion of those remaining in the kf group.
  1.1896 +  // The final key frame group in the clip is treated as a special case
  1.1897 +  // where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
  1.1898 +  // This is also important for short clips where there may only be one
  1.1899 +  // key frame.
  1.1900 +  if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count -
  1.1901 +                                          cpi->common.current_video_frame)) {
  1.1902 +    cpi->twopass.kf_group_bits =
  1.1903 +      (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
  1.1904 +  }
  1.1905 +
  1.1906 +  // Calculate the bits to be allocated to the group as a whole
  1.1907 +  if ((cpi->twopass.kf_group_bits > 0) &&
  1.1908 +      (cpi->twopass.kf_group_error_left > 0)) {
  1.1909 +    cpi->twopass.gf_group_bits =
  1.1910 +      (int64_t)(cpi->twopass.kf_group_bits *
  1.1911 +                (gf_group_err / cpi->twopass.kf_group_error_left));
  1.1912 +  } else {
  1.1913 +    cpi->twopass.gf_group_bits = 0;
  1.1914 +  }
  1.1915 +  cpi->twopass.gf_group_bits =
  1.1916 +    (cpi->twopass.gf_group_bits < 0)
  1.1917 +    ? 0
  1.1918 +    : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
  1.1919 +    ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
  1.1920 +
  1.1921 +  // Clip cpi->twopass.gf_group_bits based on user supplied data rate
  1.1922 +  // variability limit (cpi->oxcf.two_pass_vbrmax_section)
  1.1923 +  if (cpi->twopass.gf_group_bits >
  1.1924 +      (int64_t)max_bits * cpi->baseline_gf_interval)
  1.1925 +    cpi->twopass.gf_group_bits = (int64_t)max_bits * cpi->baseline_gf_interval;
  1.1926 +
  1.1927 +  // Reset the file position
  1.1928 +  reset_fpf_position(cpi, start_pos);
  1.1929 +
  1.1930 +  // Update the record of error used so far (only done once per gf group)
  1.1931 +  cpi->twopass.modified_error_used += gf_group_err;
  1.1932 +
  1.1933 +  // Assign  bits to the arf or gf.
  1.1934 +  for (i = 0;
  1.1935 +      i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME);
  1.1936 +      ++i) {
  1.1937 +    int allocation_chunks;
  1.1938 +    int q = cpi->oxcf.fixed_q < 0 ? cpi->last_q[INTER_FRAME]
  1.1939 +                                  : cpi->oxcf.fixed_q;
  1.1940 +    int gf_bits;
  1.1941 +
  1.1942 +    int boost = (cpi->gfu_boost * vp9_gfboost_qadjust(q)) / 100;
  1.1943 +
  1.1944 +    // Set max and minimum boost and hence minimum allocation
  1.1945 +    boost = clamp(boost, 125, (cpi->baseline_gf_interval + 1) * 200);
  1.1946 +
  1.1947 +    if (cpi->source_alt_ref_pending && i == 0)
  1.1948 +      allocation_chunks = ((cpi->baseline_gf_interval + 1) * 100) + boost;
  1.1949 +    else
  1.1950 +      allocation_chunks = (cpi->baseline_gf_interval * 100) + (boost - 100);
  1.1951 +
  1.1952 +    // Prevent overflow
  1.1953 +    if (boost > 1023) {
  1.1954 +      int divisor = boost >> 10;
  1.1955 +      boost /= divisor;
  1.1956 +      allocation_chunks /= divisor;
  1.1957 +    }
  1.1958 +
  1.1959 +    // Calculate the number of bits to be spent on the gf or arf based on
  1.1960 +    // the boost number
  1.1961 +    gf_bits = (int)((double)boost * (cpi->twopass.gf_group_bits /
  1.1962 +                                       (double)allocation_chunks));
  1.1963 +
  1.1964 +    // If the frame that is to be boosted is simpler than the average for
  1.1965 +    // the gf/arf group then use an alternative calculation
  1.1966 +    // based on the error score of the frame itself
  1.1967 +    if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) {
  1.1968 +      double alt_gf_grp_bits =
  1.1969 +        (double)cpi->twopass.kf_group_bits  *
  1.1970 +        (mod_frame_err * (double)cpi->baseline_gf_interval) /
  1.1971 +        DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left);
  1.1972 +
  1.1973 +      int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits /
  1.1974 +                                           (double)allocation_chunks));
  1.1975 +
  1.1976 +      if (gf_bits > alt_gf_bits)
  1.1977 +        gf_bits = alt_gf_bits;
  1.1978 +    } else {
  1.1979 +      // If it is harder than other frames in the group make sure it at
  1.1980 +      // least receives an allocation in keeping with its relative error
  1.1981 +      // score, otherwise it may be worse off than an "un-boosted" frame.
  1.1982 +      int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits *
  1.1983 +                        mod_frame_err /
  1.1984 +                        DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left));
  1.1985 +
  1.1986 +      if (alt_gf_bits > gf_bits)
  1.1987 +        gf_bits = alt_gf_bits;
  1.1988 +    }
  1.1989 +
  1.1990 +    // Dont allow a negative value for gf_bits
  1.1991 +    if (gf_bits < 0)
  1.1992 +      gf_bits = 0;
  1.1993 +
  1.1994 +    // Add in minimum for a frame
  1.1995 +    gf_bits += cpi->min_frame_bandwidth;
  1.1996 +
  1.1997 +    if (i == 0) {
  1.1998 +      cpi->twopass.gf_bits = gf_bits;
  1.1999 +    }
  1.2000 +    if (i == 1 || (!cpi->source_alt_ref_pending
  1.2001 +        && (cpi->common.frame_type != KEY_FRAME))) {
  1.2002 +      // Per frame bit target for this frame
  1.2003 +      cpi->per_frame_bandwidth = gf_bits;
  1.2004 +    }
  1.2005 +  }
  1.2006 +
  1.2007 +  {
  1.2008 +    // Adjust KF group bits and error remaining
  1.2009 +    cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
  1.2010 +    cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
  1.2011 +
  1.2012 +    if (cpi->twopass.kf_group_bits < 0)
  1.2013 +      cpi->twopass.kf_group_bits = 0;
  1.2014 +
  1.2015 +    // Note the error score left in the remaining frames of the group.
  1.2016 +    // For normal GFs we want to remove the error score for the first frame
  1.2017 +    // of the group (except in Key frame case where this has already
  1.2018 +    // happened)
  1.2019 +    if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
  1.2020 +      cpi->twopass.gf_group_error_left = (int64_t)(gf_group_err
  1.2021 +                                                   - gf_first_frame_err);
  1.2022 +    else
  1.2023 +      cpi->twopass.gf_group_error_left = (int64_t)gf_group_err;
  1.2024 +
  1.2025 +    cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits
  1.2026 +        - cpi->min_frame_bandwidth;
  1.2027 +
  1.2028 +    if (cpi->twopass.gf_group_bits < 0)
  1.2029 +      cpi->twopass.gf_group_bits = 0;
  1.2030 +
  1.2031 +    // This condition could fail if there are two kfs very close together
  1.2032 +    // despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the
  1.2033 +    // calculation of alt_extra_bits.
  1.2034 +    if (cpi->baseline_gf_interval >= 3) {
  1.2035 +      const int boost = cpi->source_alt_ref_pending ? b_boost : cpi->gfu_boost;
  1.2036 +
  1.2037 +      if (boost >= 150) {
  1.2038 +        int alt_extra_bits;
  1.2039 +        int pct_extra = (boost - 100) / 50;
  1.2040 +        pct_extra = (pct_extra > 20) ? 20 : pct_extra;
  1.2041 +
  1.2042 +        alt_extra_bits = (int)((cpi->twopass.gf_group_bits * pct_extra) / 100);
  1.2043 +        cpi->twopass.gf_group_bits -= alt_extra_bits;
  1.2044 +      }
  1.2045 +    }
  1.2046 +  }
  1.2047 +
  1.2048 +  if (cpi->common.frame_type != KEY_FRAME) {
  1.2049 +    FIRSTPASS_STATS sectionstats;
  1.2050 +
  1.2051 +    zero_stats(&sectionstats);
  1.2052 +    reset_fpf_position(cpi, start_pos);
  1.2053 +
  1.2054 +    for (i = 0; i < cpi->baseline_gf_interval; i++) {
  1.2055 +      input_stats(cpi, &next_frame);
  1.2056 +      accumulate_stats(&sectionstats, &next_frame);
  1.2057 +    }
  1.2058 +
  1.2059 +    avg_stats(&sectionstats);
  1.2060 +
  1.2061 +    cpi->twopass.section_intra_rating = (int)
  1.2062 +      (sectionstats.intra_error /
  1.2063 +      DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
  1.2064 +
  1.2065 +    reset_fpf_position(cpi, start_pos);
  1.2066 +  }
  1.2067 +}
  1.2068 +
  1.2069 +// Allocate bits to a normal frame that is neither a gf an arf or a key frame.
  1.2070 +static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  1.2071 +  int target_frame_size;
  1.2072 +
  1.2073 +  double modified_err;
  1.2074 +  double err_fraction;
  1.2075 +
  1.2076 +  // Max for a single frame.
  1.2077 +  int max_bits = frame_max_bits(cpi);
  1.2078 +
  1.2079 +  // Calculate modified prediction error used in bit allocation.
  1.2080 +  modified_err = calculate_modified_err(cpi, this_frame);
  1.2081 +
  1.2082 +  if (cpi->twopass.gf_group_error_left > 0)
  1.2083 +    // What portion of the remaining GF group error is used by this frame.
  1.2084 +    err_fraction = modified_err / cpi->twopass.gf_group_error_left;
  1.2085 +  else
  1.2086 +    err_fraction = 0.0;
  1.2087 +
  1.2088 +  // How many of those bits available for allocation should we give it?
  1.2089 +  target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
  1.2090 +
  1.2091 +  // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at
  1.2092 +  // the top end.
  1.2093 +  if (target_frame_size < 0) {
  1.2094 +    target_frame_size = 0;
  1.2095 +  } else {
  1.2096 +    if (target_frame_size > max_bits)
  1.2097 +      target_frame_size = max_bits;
  1.2098 +
  1.2099 +    if (target_frame_size > cpi->twopass.gf_group_bits)
  1.2100 +      target_frame_size = (int)cpi->twopass.gf_group_bits;
  1.2101 +  }
  1.2102 +
  1.2103 +  // Adjust error and bits remaining.
  1.2104 +  cpi->twopass.gf_group_error_left -= (int64_t)modified_err;
  1.2105 +  cpi->twopass.gf_group_bits -= target_frame_size;
  1.2106 +
  1.2107 +  if (cpi->twopass.gf_group_bits < 0)
  1.2108 +    cpi->twopass.gf_group_bits = 0;
  1.2109 +
  1.2110 +  // Add in the minimum number of bits that is set aside for every frame.
  1.2111 +  target_frame_size += cpi->min_frame_bandwidth;
  1.2112 +
  1.2113 +  // Per frame bit target for this frame.
  1.2114 +  cpi->per_frame_bandwidth = target_frame_size;
  1.2115 +}
  1.2116 +
  1.2117 +// Make a damped adjustment to the active max q.
  1.2118 +static int adjust_active_maxq(int old_maxqi, int new_maxqi) {
  1.2119 +  int i;
  1.2120 +  const double old_q = vp9_convert_qindex_to_q(old_maxqi);
  1.2121 +  const double new_q = vp9_convert_qindex_to_q(new_maxqi);
  1.2122 +  const double target_q = ((old_q * 7.0) + new_q) / 8.0;
  1.2123 +
  1.2124 +  if (target_q > old_q) {
  1.2125 +    for (i = old_maxqi; i <= new_maxqi; i++)
  1.2126 +      if (vp9_convert_qindex_to_q(i) >= target_q)
  1.2127 +        return i;
  1.2128 +  } else {
  1.2129 +    for (i = old_maxqi; i >= new_maxqi; i--)
  1.2130 +      if (vp9_convert_qindex_to_q(i) <= target_q)
  1.2131 +        return i;
  1.2132 +  }
  1.2133 +
  1.2134 +  return new_maxqi;
  1.2135 +}
  1.2136 +
  1.2137 +void vp9_second_pass(VP9_COMP *cpi) {
  1.2138 +  int tmp_q;
  1.2139 +  int frames_left = (int)(cpi->twopass.total_stats.count -
  1.2140 +                          cpi->common.current_video_frame);
  1.2141 +
  1.2142 +  FIRSTPASS_STATS this_frame;
  1.2143 +  FIRSTPASS_STATS this_frame_copy;
  1.2144 +
  1.2145 +  double this_frame_intra_error;
  1.2146 +  double this_frame_coded_error;
  1.2147 +
  1.2148 +  if (!cpi->twopass.stats_in)
  1.2149 +    return;
  1.2150 +
  1.2151 +  vp9_clear_system_state();
  1.2152 +
  1.2153 +  if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
  1.2154 +    cpi->active_worst_quality = cpi->oxcf.cq_level;
  1.2155 +  } else {
  1.2156 +    // Special case code for first frame.
  1.2157 +    if (cpi->common.current_video_frame == 0) {
  1.2158 +      int section_target_bandwidth =
  1.2159 +          (int)(cpi->twopass.bits_left / frames_left);
  1.2160 +      cpi->twopass.est_max_qcorrection_factor = 1.0;
  1.2161 +
  1.2162 +      // Set a cq_level in constrained quality mode.
  1.2163 +      // Commenting this code out for now since it does not seem to be
  1.2164 +      // working well.
  1.2165 +      /*
  1.2166 +      if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
  1.2167 +        int est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats,
  1.2168 +           section_target_bandwidth);
  1.2169 +
  1.2170 +        if (est_cq > cpi->cq_target_quality)
  1.2171 +          cpi->cq_target_quality = est_cq;
  1.2172 +        else
  1.2173 +          cpi->cq_target_quality = cpi->oxcf.cq_level;
  1.2174 +      }
  1.2175 +      */
  1.2176 +
  1.2177 +      // guess at maxq needed in 2nd pass
  1.2178 +      cpi->twopass.maxq_max_limit = cpi->worst_quality;
  1.2179 +      cpi->twopass.maxq_min_limit = cpi->best_quality;
  1.2180 +
  1.2181 +      tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
  1.2182 +                             section_target_bandwidth);
  1.2183 +
  1.2184 +      cpi->active_worst_quality = tmp_q;
  1.2185 +      cpi->ni_av_qi = tmp_q;
  1.2186 +      cpi->avg_q = vp9_convert_qindex_to_q(tmp_q);
  1.2187 +
  1.2188 +      // Limit the maxq value returned subsequently.
  1.2189 +      // This increases the risk of overspend or underspend if the initial
  1.2190 +      // estimate for the clip is bad, but helps prevent excessive
  1.2191 +      // variation in Q, especially near the end of a clip
  1.2192 +      // where for example a small overspend may cause Q to crash
  1.2193 +      adjust_maxq_qrange(cpi);
  1.2194 +    }
  1.2195 +
  1.2196 +    // The last few frames of a clip almost always have to few or too many
  1.2197 +    // bits and for the sake of over exact rate control we dont want to make
  1.2198 +    // radical adjustments to the allowed quantizer range just to use up a
  1.2199 +    // few surplus bits or get beneath the target rate.
  1.2200 +    else if ((cpi->common.current_video_frame <
  1.2201 +              (((unsigned int)cpi->twopass.total_stats.count * 255) >> 8)) &&
  1.2202 +             ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
  1.2203 +              (unsigned int)cpi->twopass.total_stats.count)) {
  1.2204 +      int section_target_bandwidth =
  1.2205 +          (int)(cpi->twopass.bits_left / frames_left);
  1.2206 +      if (frames_left < 1)
  1.2207 +        frames_left = 1;
  1.2208 +
  1.2209 +      tmp_q = estimate_max_q(
  1.2210 +          cpi,
  1.2211 +          &cpi->twopass.total_left_stats,
  1.2212 +          section_target_bandwidth);
  1.2213 +
  1.2214 +      // Make a damped adjustment to active max Q
  1.2215 +      cpi->active_worst_quality =
  1.2216 +          adjust_active_maxq(cpi->active_worst_quality, tmp_q);
  1.2217 +    }
  1.2218 +  }
  1.2219 +  vp9_zero(this_frame);
  1.2220 +  if (EOF == input_stats(cpi, &this_frame))
  1.2221 +    return;
  1.2222 +
  1.2223 +  this_frame_intra_error = this_frame.intra_error;
  1.2224 +  this_frame_coded_error = this_frame.coded_error;
  1.2225 +
  1.2226 +  // keyframe and section processing !
  1.2227 +  if (cpi->twopass.frames_to_key == 0) {
  1.2228 +    // Define next KF group and assign bits to it
  1.2229 +    this_frame_copy = this_frame;
  1.2230 +    find_next_key_frame(cpi, &this_frame_copy);
  1.2231 +  }
  1.2232 +
  1.2233 +  // Is this a GF / ARF (Note that a KF is always also a GF)
  1.2234 +  if (cpi->frames_till_gf_update_due == 0) {
  1.2235 +    // Define next gf group and assign bits to it
  1.2236 +    this_frame_copy = this_frame;
  1.2237 +
  1.2238 +    cpi->gf_zeromotion_pct = 0;
  1.2239 +
  1.2240 +#if CONFIG_MULTIPLE_ARF
  1.2241 +    if (cpi->multi_arf_enabled) {
  1.2242 +      define_fixed_arf_period(cpi);
  1.2243 +    } else {
  1.2244 +#endif
  1.2245 +      define_gf_group(cpi, &this_frame_copy);
  1.2246 +#if CONFIG_MULTIPLE_ARF
  1.2247 +    }
  1.2248 +#endif
  1.2249 +
  1.2250 +    if (cpi->gf_zeromotion_pct > 995) {
  1.2251 +      // As long as max_thresh for encode breakout is small enough, it is ok
  1.2252 +      // to enable it for no-show frame, i.e. set enable_encode_breakout to 2.
  1.2253 +      if (!cpi->common.show_frame)
  1.2254 +        cpi->enable_encode_breakout = 0;
  1.2255 +      else
  1.2256 +        cpi->enable_encode_breakout = 2;
  1.2257 +    }
  1.2258 +
  1.2259 +    // If we are going to code an altref frame at the end of the group
  1.2260 +    // and the current frame is not a key frame....
  1.2261 +    // If the previous group used an arf this frame has already benefited
  1.2262 +    // from that arf boost and it should not be given extra bits
  1.2263 +    // If the previous group was NOT coded using arf we may want to apply
  1.2264 +    // some boost to this GF as well
  1.2265 +    if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) {
  1.2266 +      // Assign a standard frames worth of bits from those allocated
  1.2267 +      // to the GF group
  1.2268 +      int bak = cpi->per_frame_bandwidth;
  1.2269 +      this_frame_copy = this_frame;
  1.2270 +      assign_std_frame_bits(cpi, &this_frame_copy);
  1.2271 +      cpi->per_frame_bandwidth = bak;
  1.2272 +    }
  1.2273 +  } else {
  1.2274 +    // Otherwise this is an ordinary frame
  1.2275 +    // Assign bits from those allocated to the GF group
  1.2276 +    this_frame_copy =  this_frame;
  1.2277 +    assign_std_frame_bits(cpi, &this_frame_copy);
  1.2278 +  }
  1.2279 +
  1.2280 +  // Keep a globally available copy of this and the next frame's iiratio.
  1.2281 +  cpi->twopass.this_iiratio = (int)(this_frame_intra_error /
  1.2282 +                              DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
  1.2283 +  {
  1.2284 +    FIRSTPASS_STATS next_frame;
  1.2285 +    if (lookup_next_frame_stats(cpi, &next_frame) != EOF) {
  1.2286 +      cpi->twopass.next_iiratio = (int)(next_frame.intra_error /
  1.2287 +                                  DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  1.2288 +    }
  1.2289 +  }
  1.2290 +
  1.2291 +  // Set nominal per second bandwidth for this frame
  1.2292 +  cpi->target_bandwidth = (int)(cpi->per_frame_bandwidth
  1.2293 +                                * cpi->output_framerate);
  1.2294 +  if (cpi->target_bandwidth < 0)
  1.2295 +    cpi->target_bandwidth = 0;
  1.2296 +
  1.2297 +  cpi->twopass.frames_to_key--;
  1.2298 +
  1.2299 +  // Update the total stats remaining structure
  1.2300 +  subtract_stats(&cpi->twopass.total_left_stats, &this_frame);
  1.2301 +}
  1.2302 +
  1.2303 +static int test_candidate_kf(VP9_COMP *cpi,
  1.2304 +                             FIRSTPASS_STATS *last_frame,
  1.2305 +                             FIRSTPASS_STATS *this_frame,
  1.2306 +                             FIRSTPASS_STATS *next_frame) {
  1.2307 +  int is_viable_kf = 0;
  1.2308 +
  1.2309 +  // Does the frame satisfy the primary criteria of a key frame
  1.2310 +  //      If so, then examine how well it predicts subsequent frames
  1.2311 +  if ((this_frame->pcnt_second_ref < 0.10) &&
  1.2312 +      (next_frame->pcnt_second_ref < 0.10) &&
  1.2313 +      ((this_frame->pcnt_inter < 0.05) ||
  1.2314 +       (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) &&
  1.2315 +        ((this_frame->intra_error /
  1.2316 +          DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
  1.2317 +        ((fabs(last_frame->coded_error - this_frame->coded_error) /
  1.2318 +              DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
  1.2319 +          .40) ||
  1.2320 +         (fabs(last_frame->intra_error - this_frame->intra_error) /
  1.2321 +              DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
  1.2322 +          .40) ||
  1.2323 +         ((next_frame->intra_error /
  1.2324 +           DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
  1.2325 +    int i;
  1.2326 +    FIRSTPASS_STATS *start_pos;
  1.2327 +
  1.2328 +    FIRSTPASS_STATS local_next_frame;
  1.2329 +
  1.2330 +    double boost_score = 0.0;
  1.2331 +    double old_boost_score = 0.0;
  1.2332 +    double decay_accumulator = 1.0;
  1.2333 +    double next_iiratio;
  1.2334 +
  1.2335 +    local_next_frame = *next_frame;
  1.2336 +
  1.2337 +    // Note the starting file position so we can reset to it
  1.2338 +    start_pos = cpi->twopass.stats_in;
  1.2339 +
  1.2340 +    // Examine how well the key frame predicts subsequent frames
  1.2341 +    for (i = 0; i < 16; i++) {
  1.2342 +      next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
  1.2343 +                      DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
  1.2344 +
  1.2345 +      if (next_iiratio > RMAX)
  1.2346 +        next_iiratio = RMAX;
  1.2347 +
  1.2348 +      // Cumulative effect of decay in prediction quality
  1.2349 +      if (local_next_frame.pcnt_inter > 0.85)
  1.2350 +        decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
  1.2351 +      else
  1.2352 +        decay_accumulator =
  1.2353 +            decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
  1.2354 +
  1.2355 +      // decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
  1.2356 +
  1.2357 +      // Keep a running total
  1.2358 +      boost_score += (decay_accumulator * next_iiratio);
  1.2359 +
  1.2360 +      // Test various breakout clauses
  1.2361 +      if ((local_next_frame.pcnt_inter < 0.05) ||
  1.2362 +          (next_iiratio < 1.5) ||
  1.2363 +          (((local_next_frame.pcnt_inter -
  1.2364 +             local_next_frame.pcnt_neutral) < 0.20) &&
  1.2365 +           (next_iiratio < 3.0)) ||
  1.2366 +          ((boost_score - old_boost_score) < 3.0) ||
  1.2367 +          (local_next_frame.intra_error < 200)
  1.2368 +         ) {
  1.2369 +        break;
  1.2370 +      }
  1.2371 +
  1.2372 +      old_boost_score = boost_score;
  1.2373 +
  1.2374 +      // Get the next frame details
  1.2375 +      if (EOF == input_stats(cpi, &local_next_frame))
  1.2376 +        break;
  1.2377 +    }
  1.2378 +
  1.2379 +    // If there is tolerable prediction for at least the next 3 frames then
  1.2380 +    // break out else discard this potential key frame and move on
  1.2381 +    if (boost_score > 30.0 && (i > 3)) {
  1.2382 +      is_viable_kf = 1;
  1.2383 +    } else {
  1.2384 +      // Reset the file position
  1.2385 +      reset_fpf_position(cpi, start_pos);
  1.2386 +
  1.2387 +      is_viable_kf = 0;
  1.2388 +    }
  1.2389 +  }
  1.2390 +
  1.2391 +  return is_viable_kf;
  1.2392 +}
  1.2393 +static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  1.2394 +  int i, j;
  1.2395 +  FIRSTPASS_STATS last_frame;
  1.2396 +  FIRSTPASS_STATS first_frame;
  1.2397 +  FIRSTPASS_STATS next_frame;
  1.2398 +  FIRSTPASS_STATS *start_position;
  1.2399 +
  1.2400 +  double decay_accumulator = 1.0;
  1.2401 +  double zero_motion_accumulator = 1.0;
  1.2402 +  double boost_score = 0;
  1.2403 +  double loop_decay_rate;
  1.2404 +
  1.2405 +  double kf_mod_err = 0.0;
  1.2406 +  double kf_group_err = 0.0;
  1.2407 +  double kf_group_intra_err = 0.0;
  1.2408 +  double kf_group_coded_err = 0.0;
  1.2409 +  double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
  1.2410 +
  1.2411 +  vp9_zero(next_frame);
  1.2412 +
  1.2413 +  vp9_clear_system_state();  // __asm emms;
  1.2414 +  start_position = cpi->twopass.stats_in;
  1.2415 +
  1.2416 +  cpi->common.frame_type = KEY_FRAME;
  1.2417 +
  1.2418 +  // is this a forced key frame by interval
  1.2419 +  cpi->this_key_frame_forced = cpi->next_key_frame_forced;
  1.2420 +
  1.2421 +  // Clear the alt ref active flag as this can never be active on a key frame
  1.2422 +  cpi->source_alt_ref_active = 0;
  1.2423 +
  1.2424 +  // Kf is always a gf so clear frames till next gf counter
  1.2425 +  cpi->frames_till_gf_update_due = 0;
  1.2426 +
  1.2427 +  cpi->twopass.frames_to_key = 1;
  1.2428 +
  1.2429 +  // Take a copy of the initial frame details
  1.2430 +  first_frame = *this_frame;
  1.2431 +
  1.2432 +  cpi->twopass.kf_group_bits = 0;        // Total bits available to kf group
  1.2433 +  cpi->twopass.kf_group_error_left = 0;  // Group modified error score.
  1.2434 +
  1.2435 +  kf_mod_err = calculate_modified_err(cpi, this_frame);
  1.2436 +
  1.2437 +  // find the next keyframe
  1.2438 +  i = 0;
  1.2439 +  while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) {
  1.2440 +    // Accumulate kf group error
  1.2441 +    kf_group_err += calculate_modified_err(cpi, this_frame);
  1.2442 +
  1.2443 +    // These figures keep intra and coded error counts for all frames including
  1.2444 +    // key frames in the group. The effect of the key frame itself can be
  1.2445 +    // subtracted out using the first_frame data collected above.
  1.2446 +    kf_group_intra_err += this_frame->intra_error;
  1.2447 +    kf_group_coded_err += this_frame->coded_error;
  1.2448 +
  1.2449 +    // load a the next frame's stats
  1.2450 +    last_frame = *this_frame;
  1.2451 +    input_stats(cpi, this_frame);
  1.2452 +
  1.2453 +    // Provided that we are not at the end of the file...
  1.2454 +    if (cpi->oxcf.auto_key
  1.2455 +        && lookup_next_frame_stats(cpi, &next_frame) != EOF) {
  1.2456 +      // Normal scene cut check
  1.2457 +      if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
  1.2458 +        break;
  1.2459 +
  1.2460 +
  1.2461 +      // How fast is prediction quality decaying
  1.2462 +      loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  1.2463 +
  1.2464 +      // We want to know something about the recent past... rather than
  1.2465 +      // as used elsewhere where we are concened with decay in prediction
  1.2466 +      // quality since the last GF or KF.
  1.2467 +      recent_loop_decay[i % 8] = loop_decay_rate;
  1.2468 +      decay_accumulator = 1.0;
  1.2469 +      for (j = 0; j < 8; j++)
  1.2470 +        decay_accumulator *= recent_loop_decay[j];
  1.2471 +
  1.2472 +      // Special check for transition or high motion followed by a
  1.2473 +      // to a static scene.
  1.2474 +      if (detect_transition_to_still(cpi, i, cpi->key_frame_frequency - i,
  1.2475 +                                     loop_decay_rate, decay_accumulator))
  1.2476 +        break;
  1.2477 +
  1.2478 +      // Step on to the next frame
  1.2479 +      cpi->twopass.frames_to_key++;
  1.2480 +
  1.2481 +      // If we don't have a real key frame within the next two
  1.2482 +      // forcekeyframeevery intervals then break out of the loop.
  1.2483 +      if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency)
  1.2484 +        break;
  1.2485 +    } else {
  1.2486 +      cpi->twopass.frames_to_key++;
  1.2487 +    }
  1.2488 +    i++;
  1.2489 +  }
  1.2490 +
  1.2491 +  // If there is a max kf interval set by the user we must obey it.
  1.2492 +  // We already breakout of the loop above at 2x max.
  1.2493 +  // This code centers the extra kf if the actual natural
  1.2494 +  // interval is between 1x and 2x
  1.2495 +  if (cpi->oxcf.auto_key
  1.2496 +      && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) {
  1.2497 +    FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
  1.2498 +    FIRSTPASS_STATS tmp_frame;
  1.2499 +
  1.2500 +    cpi->twopass.frames_to_key /= 2;
  1.2501 +
  1.2502 +    // Copy first frame details
  1.2503 +    tmp_frame = first_frame;
  1.2504 +
  1.2505 +    // Reset to the start of the group
  1.2506 +    reset_fpf_position(cpi, start_position);
  1.2507 +
  1.2508 +    kf_group_err = 0;
  1.2509 +    kf_group_intra_err = 0;
  1.2510 +    kf_group_coded_err = 0;
  1.2511 +
  1.2512 +    // Rescan to get the correct error data for the forced kf group
  1.2513 +    for (i = 0; i < cpi->twopass.frames_to_key; i++) {
  1.2514 +      // Accumulate kf group errors
  1.2515 +      kf_group_err += calculate_modified_err(cpi, &tmp_frame);
  1.2516 +      kf_group_intra_err += tmp_frame.intra_error;
  1.2517 +      kf_group_coded_err += tmp_frame.coded_error;
  1.2518 +
  1.2519 +      // Load a the next frame's stats
  1.2520 +      input_stats(cpi, &tmp_frame);
  1.2521 +    }
  1.2522 +
  1.2523 +    // Reset to the start of the group
  1.2524 +    reset_fpf_position(cpi, current_pos);
  1.2525 +
  1.2526 +    cpi->next_key_frame_forced = 1;
  1.2527 +  } else {
  1.2528 +    cpi->next_key_frame_forced = 0;
  1.2529 +  }
  1.2530 +  // Special case for the last frame of the file
  1.2531 +  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
  1.2532 +    // Accumulate kf group error
  1.2533 +    kf_group_err += calculate_modified_err(cpi, this_frame);
  1.2534 +
  1.2535 +    // These figures keep intra and coded error counts for all frames including
  1.2536 +    // key frames in the group. The effect of the key frame itself can be
  1.2537 +    // subtracted out using the first_frame data collected above.
  1.2538 +    kf_group_intra_err += this_frame->intra_error;
  1.2539 +    kf_group_coded_err += this_frame->coded_error;
  1.2540 +  }
  1.2541 +
  1.2542 +  // Calculate the number of bits that should be assigned to the kf group.
  1.2543 +  if ((cpi->twopass.bits_left > 0) &&
  1.2544 +      (cpi->twopass.modified_error_left > 0.0)) {
  1.2545 +    // Max for a single normal frame (not key frame)
  1.2546 +    int max_bits = frame_max_bits(cpi);
  1.2547 +
  1.2548 +    // Maximum bits for the kf group
  1.2549 +    int64_t max_grp_bits;
  1.2550 +
  1.2551 +    // Default allocation based on bits left and relative
  1.2552 +    // complexity of the section
  1.2553 +    cpi->twopass.kf_group_bits = (int64_t)(cpi->twopass.bits_left *
  1.2554 +                                           (kf_group_err /
  1.2555 +                                            cpi->twopass.modified_error_left));
  1.2556 +
  1.2557 +    // Clip based on maximum per frame rate defined by the user.
  1.2558 +    max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
  1.2559 +    if (cpi->twopass.kf_group_bits > max_grp_bits)
  1.2560 +      cpi->twopass.kf_group_bits = max_grp_bits;
  1.2561 +  } else {
  1.2562 +    cpi->twopass.kf_group_bits = 0;
  1.2563 +  }
  1.2564 +  // Reset the first pass file position
  1.2565 +  reset_fpf_position(cpi, start_position);
  1.2566 +
  1.2567 +  // Determine how big to make this keyframe based on how well the subsequent
  1.2568 +  // frames use inter blocks.
  1.2569 +  decay_accumulator = 1.0;
  1.2570 +  boost_score = 0.0;
  1.2571 +  loop_decay_rate = 1.00;       // Starting decay rate
  1.2572 +
  1.2573 +  // Scan through the kf group collating various stats.
  1.2574 +  for (i = 0; i < cpi->twopass.frames_to_key; i++) {
  1.2575 +    double r;
  1.2576 +
  1.2577 +    if (EOF == input_stats(cpi, &next_frame))
  1.2578 +      break;
  1.2579 +
  1.2580 +    // Monitor for static sections.
  1.2581 +    if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
  1.2582 +        zero_motion_accumulator) {
  1.2583 +      zero_motion_accumulator =
  1.2584 +        (next_frame.pcnt_inter - next_frame.pcnt_motion);
  1.2585 +    }
  1.2586 +
  1.2587 +    // For the first few frames collect data to decide kf boost.
  1.2588 +    if (i <= (cpi->max_gf_interval * 2)) {
  1.2589 +      if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
  1.2590 +        r = (IIKFACTOR2 * next_frame.intra_error /
  1.2591 +             DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  1.2592 +      else
  1.2593 +        r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
  1.2594 +             DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  1.2595 +
  1.2596 +      if (r > RMAX)
  1.2597 +        r = RMAX;
  1.2598 +
  1.2599 +      // How fast is prediction quality decaying
  1.2600 +      if (!detect_flash(cpi, 0)) {
  1.2601 +        loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  1.2602 +        decay_accumulator = decay_accumulator * loop_decay_rate;
  1.2603 +        decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
  1.2604 +                              ? MIN_DECAY_FACTOR : decay_accumulator;
  1.2605 +      }
  1.2606 +
  1.2607 +      boost_score += (decay_accumulator * r);
  1.2608 +    }
  1.2609 +  }
  1.2610 +
  1.2611 +  {
  1.2612 +    FIRSTPASS_STATS sectionstats;
  1.2613 +
  1.2614 +    zero_stats(&sectionstats);
  1.2615 +    reset_fpf_position(cpi, start_position);
  1.2616 +
  1.2617 +    for (i = 0; i < cpi->twopass.frames_to_key; i++) {
  1.2618 +      input_stats(cpi, &next_frame);
  1.2619 +      accumulate_stats(&sectionstats, &next_frame);
  1.2620 +    }
  1.2621 +
  1.2622 +    avg_stats(&sectionstats);
  1.2623 +
  1.2624 +    cpi->twopass.section_intra_rating = (int)
  1.2625 +      (sectionstats.intra_error
  1.2626 +      / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
  1.2627 +  }
  1.2628 +
  1.2629 +  // Reset the first pass file position
  1.2630 +  reset_fpf_position(cpi, start_position);
  1.2631 +
  1.2632 +  // Work out how many bits to allocate for the key frame itself
  1.2633 +  if (1) {
  1.2634 +    int kf_boost = (int)boost_score;
  1.2635 +    int allocation_chunks;
  1.2636 +    int alt_kf_bits;
  1.2637 +
  1.2638 +    if (kf_boost < (cpi->twopass.frames_to_key * 3))
  1.2639 +      kf_boost = (cpi->twopass.frames_to_key * 3);
  1.2640 +
  1.2641 +    if (kf_boost < 300)  // Min KF boost
  1.2642 +      kf_boost = 300;
  1.2643 +
  1.2644 +    // Make a note of baseline boost and the zero motion
  1.2645 +    // accumulator value for use elsewhere.
  1.2646 +    cpi->kf_boost = kf_boost;
  1.2647 +    cpi->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
  1.2648 +
  1.2649 +    // We do three calculations for kf size.
  1.2650 +    // The first is based on the error score for the whole kf group.
  1.2651 +    // The second (optionaly) on the key frames own error if this is
  1.2652 +    // smaller than the average for the group.
  1.2653 +    // The final one insures that the frame receives at least the
  1.2654 +    // allocation it would have received based on its own error score vs
  1.2655 +    // the error score remaining
  1.2656 +    // Special case if the sequence appears almost totaly static
  1.2657 +    // In this case we want to spend almost all of the bits on the
  1.2658 +    // key frame.
  1.2659 +    // cpi->twopass.frames_to_key-1 because key frame itself is taken
  1.2660 +    // care of by kf_boost.
  1.2661 +    if (zero_motion_accumulator >= 0.99) {
  1.2662 +      allocation_chunks =
  1.2663 +        ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
  1.2664 +    } else {
  1.2665 +      allocation_chunks =
  1.2666 +        ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
  1.2667 +    }
  1.2668 +
  1.2669 +    // Prevent overflow
  1.2670 +    if (kf_boost > 1028) {
  1.2671 +      int divisor = kf_boost >> 10;
  1.2672 +      kf_boost /= divisor;
  1.2673 +      allocation_chunks /= divisor;
  1.2674 +    }
  1.2675 +
  1.2676 +    cpi->twopass.kf_group_bits =
  1.2677 +        (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
  1.2678 +
  1.2679 +    // Calculate the number of bits to be spent on the key frame
  1.2680 +    cpi->twopass.kf_bits =
  1.2681 +        (int)((double)kf_boost *
  1.2682 +              ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
  1.2683 +
  1.2684 +    // If the key frame is actually easier than the average for the
  1.2685 +    // kf group (which does sometimes happen... eg a blank intro frame)
  1.2686 +    // Then use an alternate calculation based on the kf error score
  1.2687 +    // which should give a smaller key frame.
  1.2688 +    if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) {
  1.2689 +      double  alt_kf_grp_bits =
  1.2690 +        ((double)cpi->twopass.bits_left *
  1.2691 +         (kf_mod_err * (double)cpi->twopass.frames_to_key) /
  1.2692 +         DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
  1.2693 +
  1.2694 +      alt_kf_bits = (int)((double)kf_boost *
  1.2695 +                          (alt_kf_grp_bits / (double)allocation_chunks));
  1.2696 +
  1.2697 +      if (cpi->twopass.kf_bits > alt_kf_bits) {
  1.2698 +        cpi->twopass.kf_bits = alt_kf_bits;
  1.2699 +      }
  1.2700 +    } else {
  1.2701 +    // Else if it is much harder than other frames in the group make sure
  1.2702 +    // it at least receives an allocation in keeping with its relative
  1.2703 +    // error score
  1.2704 +      alt_kf_bits =
  1.2705 +        (int)((double)cpi->twopass.bits_left *
  1.2706 +              (kf_mod_err /
  1.2707 +               DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
  1.2708 +
  1.2709 +      if (alt_kf_bits > cpi->twopass.kf_bits) {
  1.2710 +        cpi->twopass.kf_bits = alt_kf_bits;
  1.2711 +      }
  1.2712 +    }
  1.2713 +
  1.2714 +    cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
  1.2715 +    // Add in the minimum frame allowance
  1.2716 +    cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
  1.2717 +
  1.2718 +    // Peer frame bit target for this frame
  1.2719 +    cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
  1.2720 +    // Convert to a per second bitrate
  1.2721 +    cpi->target_bandwidth = (int)(cpi->twopass.kf_bits *
  1.2722 +                                  cpi->output_framerate);
  1.2723 +  }
  1.2724 +
  1.2725 +  // Note the total error score of the kf group minus the key frame itself
  1.2726 +  cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
  1.2727 +
  1.2728 +  // Adjust the count of total modified error left.
  1.2729 +  // The count of bits left is adjusted elsewhere based on real coded frame
  1.2730 +  // sizes.
  1.2731 +  cpi->twopass.modified_error_left -= kf_group_err;
  1.2732 +}

mercurial