vp9_firstpass.c 88.9 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11 12 13
#include <math.h>
#include <limits.h>
#include <stdio.h>
14
#include "vp9/common/vp9_systemdependent.h"
15
#include "vp9/encoder/vp9_block.h"
16 17 18 19 20
#include "vp9/encoder/vp9_encodeframe.h"
#include "vp9/encoder/vp9_encodemb.h"
#include "vp9/encoder/vp9_extend.h"
#include "vp9/encoder/vp9_firstpass.h"
#include "vp9/encoder/vp9_mcomp.h"
21 22
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_variance.h"
Johann's avatar
Johann committed
23
#include "vpx_scale/vpx_scale.h"
John Koleszar's avatar
John Koleszar committed
24
#include "vpx_mem/vpx_mem.h"
25
#include "vpx_scale/yv12config.h"
26
#include "vp9/encoder/vp9_quantize.h"
27 28
#include "vp9/encoder/vp9_rdopt.h"
#include "vp9/encoder/vp9_ratectrl.h"
29 30
#include "vp9/common/vp9_quant_common.h"
#include "vp9/common/vp9_entropymv.h"
31
#include "vp9/encoder/vp9_encodemv.h"
32
#include "vp9/encoder/vp9_vaq.h"
33
#include "./vpx_scale_rtcd.h"
34 35
// TODO(jkoleszar): for setup_dst_planes
#include "vp9/common/vp9_reconinter.h"
John Koleszar's avatar
John Koleszar committed
36

37
#define OUTPUT_FPF 0
John Koleszar's avatar
John Koleszar committed
38

39 40
#define IIFACTOR   12.5
#define IIKFACTOR1 12.5
41
#define IIKFACTOR2 15.0
42
#define RMAX       512.0
43
#define GF_RMAX    96.0
Paul Wilkins's avatar
Paul Wilkins committed
44
#define ERR_DIVISOR   150.0
45
#define MIN_DECAY_FACTOR 0.1
John Koleszar's avatar
John Koleszar committed
46

47 48
#define KF_MB_INTRA_MIN 150
#define GF_MB_INTRA_MIN 100
Paul Wilkins's avatar
CQ Mode  
Paul Wilkins committed
49

50
#define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
John Koleszar's avatar
John Koleszar committed
51 52 53 54

#define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
#define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0

55 56 57 58 59 60
static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
  YV12_BUFFER_CONFIG temp = *a;
  *a = *b;
  *b = temp;
}

61
static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame);
Paul Wilkins's avatar
Paul Wilkins committed
62

John Koleszar's avatar
John Koleszar committed
63 64 65
static int select_cq_level(int qindex) {
  int ret_val = QINDEX_RANGE - 1;
  int i;
Paul Wilkins's avatar
Paul Wilkins committed
66

67
  double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0;
Paul Wilkins's avatar
Paul Wilkins committed
68

John Koleszar's avatar
John Koleszar committed
69
  for (i = 0; i < QINDEX_RANGE; i++) {
70
    if (target_q <= vp9_convert_qindex_to_q(i)) {
John Koleszar's avatar
John Koleszar committed
71 72
      ret_val = i;
      break;
Paul Wilkins's avatar
Paul Wilkins committed
73
    }
John Koleszar's avatar
John Koleszar committed
74
  }
Paul Wilkins's avatar
Paul Wilkins committed
75

John Koleszar's avatar
John Koleszar committed
76
  return ret_val;
Paul Wilkins's avatar
Paul Wilkins committed
77
}
Paul Wilkins's avatar
CQ Mode  
Paul Wilkins committed
78

79 80 81 82 83 84 85 86 87 88 89 90 91
static int gfboost_qadjust(int qindex) {
  const double q = vp9_convert_qindex_to_q(qindex);
  return (int)((0.00000828 * q * q * q) +
               (-0.0055 * q * q) +
               (1.32 * q) + 79.3);
}

static int kfboost_qadjust(int qindex) {
  const double q = vp9_convert_qindex_to_q(qindex);
  return (int)((0.00000973 * q * q * q) +
               (-0.00613 * q * q) +
               (1.316 * q) + 121.2);
}
John Koleszar's avatar
John Koleszar committed
92

93 94
// Resets the first pass file to the given position using a relative seek from
// the current position.
95 96
static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) {
  cpi->twopass.stats_in = position;
John Koleszar's avatar
John Koleszar committed
97 98
}

99
static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) {
John Koleszar's avatar
John Koleszar committed
100 101
  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
    return EOF;
John Koleszar's avatar
John Koleszar committed
102

John Koleszar's avatar
John Koleszar committed
103 104
  *next_frame = *cpi->twopass.stats_in;
  return 1;
John Koleszar's avatar
John Koleszar committed
105 106
}

107
// Read frame stats at an offset from the current position
108
static int read_frame_stats(VP9_COMP *cpi,
John Koleszar's avatar
John Koleszar committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
                            FIRSTPASS_STATS *frame_stats,
                            int offset) {
  FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in;

  // Check legality of offset
  if (offset >= 0) {
    if (&fps_ptr[offset] >= cpi->twopass.stats_in_end)
      return EOF;
  } else if (offset < 0) {
    if (&fps_ptr[offset] < cpi->twopass.stats_in_start)
      return EOF;
  }

  *frame_stats = fps_ptr[offset];
  return 1;
124 125
}

126
static int input_stats(VP9_COMP *cpi, FIRSTPASS_STATS *fps) {
John Koleszar's avatar
John Koleszar committed
127 128
  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
    return EOF;
129

John Koleszar's avatar
John Koleszar committed
130 131 132 133
  *fps = *cpi->twopass.stats_in;
  cpi->twopass.stats_in =
    (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
  return 1;
134 135
}

136
static void output_stats(const VP9_COMP            *cpi,
137
                         struct vpx_codec_pkt_list *pktlist,
John Koleszar's avatar
John Koleszar committed
138 139 140 141 142 143
                         FIRSTPASS_STATS            *stats) {
  struct vpx_codec_cx_pkt pkt;
  pkt.kind = VPX_CODEC_STATS_PKT;
  pkt.data.twopass_stats.buf = stats;
  pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
  vpx_codec_pkt_list_add(pktlist, &pkt);
144 145 146 147

// TEMP debug code
#if OUTPUT_FPF

John Koleszar's avatar
John Koleszar committed
148 149 150 151
  {
    FILE *fpfile;
    fpfile = fopen("firstpass.stt", "a");

152
    fprintf(stdout, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
John Koleszar's avatar
John Koleszar committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
            "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
            stats->frame,
            stats->intra_error,
            stats->coded_error,
            stats->sr_coded_error,
            stats->ssim_weighted_pred_err,
            stats->pcnt_inter,
            stats->pcnt_motion,
            stats->pcnt_second_ref,
            stats->pcnt_neutral,
            stats->MVr,
            stats->mvr_abs,
            stats->MVc,
            stats->mvc_abs,
            stats->MVrv,
            stats->MVcv,
            stats->mv_in_out_count,
            stats->new_mv_count,
            stats->count,
            stats->duration);
    fclose(fpfile);
  }
176 177 178
#endif
}

John Koleszar's avatar
John Koleszar committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
static void zero_stats(FIRSTPASS_STATS *section) {
  section->frame      = 0.0;
  section->intra_error = 0.0;
  section->coded_error = 0.0;
  section->sr_coded_error = 0.0;
  section->ssim_weighted_pred_err = 0.0;
  section->pcnt_inter  = 0.0;
  section->pcnt_motion  = 0.0;
  section->pcnt_second_ref = 0.0;
  section->pcnt_neutral = 0.0;
  section->MVr        = 0.0;
  section->mvr_abs     = 0.0;
  section->MVc        = 0.0;
  section->mvc_abs     = 0.0;
  section->MVrv       = 0.0;
  section->MVcv       = 0.0;
  section->mv_in_out_count  = 0.0;
  section->new_mv_count = 0.0;
  section->count      = 0.0;
  section->duration   = 1.0;
199 200
}

John Koleszar's avatar
John Koleszar committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
  section->frame += frame->frame;
  section->intra_error += frame->intra_error;
  section->coded_error += frame->coded_error;
  section->sr_coded_error += frame->sr_coded_error;
  section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
  section->pcnt_inter  += frame->pcnt_inter;
  section->pcnt_motion += frame->pcnt_motion;
  section->pcnt_second_ref += frame->pcnt_second_ref;
  section->pcnt_neutral += frame->pcnt_neutral;
  section->MVr        += frame->MVr;
  section->mvr_abs     += frame->mvr_abs;
  section->MVc        += frame->MVc;
  section->mvc_abs     += frame->mvc_abs;
  section->MVrv       += frame->MVrv;
  section->MVcv       += frame->MVcv;
  section->mv_in_out_count  += frame->mv_in_out_count;
  section->new_mv_count += frame->new_mv_count;
  section->count      += frame->count;
  section->duration   += frame->duration;
221 222
}

John Koleszar's avatar
John Koleszar committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
  section->frame -= frame->frame;
  section->intra_error -= frame->intra_error;
  section->coded_error -= frame->coded_error;
  section->sr_coded_error -= frame->sr_coded_error;
  section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
  section->pcnt_inter  -= frame->pcnt_inter;
  section->pcnt_motion -= frame->pcnt_motion;
  section->pcnt_second_ref -= frame->pcnt_second_ref;
  section->pcnt_neutral -= frame->pcnt_neutral;
  section->MVr        -= frame->MVr;
  section->mvr_abs     -= frame->mvr_abs;
  section->MVc        -= frame->MVc;
  section->mvc_abs     -= frame->mvc_abs;
  section->MVrv       -= frame->MVrv;
  section->MVcv       -= frame->MVcv;
  section->mv_in_out_count  -= frame->mv_in_out_count;
  section->new_mv_count -= frame->new_mv_count;
  section->count      -= frame->count;
  section->duration   -= frame->duration;
243 244
}

John Koleszar's avatar
John Koleszar committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
static void avg_stats(FIRSTPASS_STATS *section) {
  if (section->count < 1.0)
    return;

  section->intra_error /= section->count;
  section->coded_error /= section->count;
  section->sr_coded_error /= section->count;
  section->ssim_weighted_pred_err /= section->count;
  section->pcnt_inter  /= section->count;
  section->pcnt_second_ref /= section->count;
  section->pcnt_neutral /= section->count;
  section->pcnt_motion /= section->count;
  section->MVr        /= section->count;
  section->mvr_abs     /= section->count;
  section->MVc        /= section->count;
  section->mvc_abs     /= section->count;
  section->MVrv       /= section->count;
  section->MVcv       /= section->count;
  section->mv_in_out_count   /= section->count;
  section->duration   /= section->count;
265 266
}

267 268 269 270
// Calculate a modified Error used in distributing bits between easier and
// harder frames.
static double calculate_modified_err(VP9_COMP *cpi,
                                     FIRSTPASS_STATS *this_frame) {
271 272 273 274 275
  const FIRSTPASS_STATS *const stats = &cpi->twopass.total_stats;
  const double av_err = stats->ssim_weighted_pred_err / stats->count;
  const double this_err = this_frame->ssim_weighted_pred_err;
  return av_err * pow(this_err / DOUBLE_DIVIDE_CHECK(av_err),
                      this_err > av_err ? POW1 : POW2);
John Koleszar's avatar
John Koleszar committed
276 277
}

278
static const double weight_table[256] = {
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
  0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
  0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
  0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
  0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
  0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  1.000000, 1.000000, 1.000000, 1.000000
316 317
};

John Koleszar's avatar
John Koleszar committed
318 319 320
static double simple_weight(YV12_BUFFER_CONFIG *source) {
  int i, j;

321
  uint8_t *src = source->y_buffer;
John Koleszar's avatar
John Koleszar committed
322 323
  double sum_weights = 0.0;

324 325
  // Loop through the Y plane examining levels and creating a weight for
  // the image.
John Koleszar's avatar
John Koleszar committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339
  i = source->y_height;
  do {
    j = source->y_width;
    do {
      sum_weights += weight_table[ *src];
      src++;
    } while (--j);
    src -= source->y_width;
    src += source->y_stride;
  } while (--i);

  sum_weights /= (source->y_height * source->y_width);

  return sum_weights;
John Koleszar's avatar
John Koleszar committed
340 341
}

342

343
// This function returns the current per frame maximum bitrate target.
344
static int frame_max_bits(VP9_COMP *cpi) {
345 346 347 348
  // Max allocation for a single frame based on the max section guidelines
  // passed in and how many bits are left.
  // For VBR base this on the bits and frames left plus the
  // two_pass_vbrmax_section rate passed in by the user.
349 350 351
  const double max_bits = (1.0 * cpi->twopass.bits_left /
      (cpi->twopass.total_stats.count - cpi->common.current_video_frame)) *
      (cpi->oxcf.two_pass_vbrmax_section / 100.0);
Yaowu Xu's avatar
Yaowu Xu committed
352 353 354 355 356
  if (max_bits < 0)
      return 0;
  if (max_bits >= INT_MAX)
    return INT_MAX;
  return (int)max_bits;
John Koleszar's avatar
John Koleszar committed
357 358
}

359
void vp9_init_first_pass(VP9_COMP *cpi) {
360
  zero_stats(&cpi->twopass.total_stats);
John Koleszar's avatar
John Koleszar committed
361 362
}

363
void vp9_end_first_pass(VP9_COMP *cpi) {
364
  output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
365
}
John Koleszar's avatar
John Koleszar committed
366

Dmitry Kovalev's avatar
Dmitry Kovalev committed
367 368 369
static unsigned int zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
                                     YV12_BUFFER_CONFIG *recon_buffer,
                                     int recon_yoffset) {
John Koleszar's avatar
John Koleszar committed
370
  MACROBLOCKD *const xd = &x->e_mbd;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
371 372 373 374 375
  const uint8_t *const src = x->plane[0].src.buf;
  const int src_stride = x->plane[0].src.stride;
  const uint8_t *const ref = xd->plane[0].pre[0].buf
                           = recon_buffer->y_buffer + recon_yoffset;
  const int ref_stride = xd->plane[0].pre[0].stride;
John Koleszar's avatar
John Koleszar committed
376

Dmitry Kovalev's avatar
Dmitry Kovalev committed
377
  unsigned int sse;
378
  switch (xd->mi_8x8[0]->mbmi.sb_type) {
379
    case BLOCK_8X8:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
380
      vp9_mse8x8(src, src_stride, ref, ref_stride, &sse);
381
      break;
382
    case BLOCK_16X8:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
383
      vp9_mse16x8(src, src_stride, ref, ref_stride, &sse);
384
      break;
385
    case BLOCK_8X16:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
386
      vp9_mse8x16(src, src_stride, ref, ref_stride, &sse);
387 388
      break;
    default:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
389
      vp9_mse16x16(src, src_stride, ref, ref_stride, &sse);
390 391
      break;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
392
  return sse;
393 394
}

395
static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
396
                                     MV *ref_mv, MV *best_mv,
Scott LaVarnway's avatar
Scott LaVarnway committed
397
                                     YV12_BUFFER_CONFIG *recon_buffer,
John Koleszar's avatar
John Koleszar committed
398
                                     int *best_motion_err, int recon_yoffset) {
John Koleszar's avatar
John Koleszar committed
399
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
400 401
  int num00;

402 403
  MV tmp_mv = {0, 0};
  MV ref_mv_full;
John Koleszar's avatar
John Koleszar committed
404 405 406 407 408

  int tmp_err;
  int step_param = 3;
  int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
  int n;
409
  vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[xd->mi_8x8[0]->mbmi.sb_type];
John Koleszar's avatar
John Koleszar committed
410 411
  int new_mv_mode_penalty = 256;

412
  int sr = 0;
413
  int quart_frm = MIN(cpi->common.width, cpi->common.height);
414 415 416 417 418 419 420 421 422

  // refine the motion search range accroding to the frame dimension
  // for first pass test
  while ((quart_frm << sr) < MAX_FULL_PEL_VAL)
    sr++;

  step_param    += sr;
  further_steps -= sr;

John Koleszar's avatar
John Koleszar committed
423
  // override the default variance function to use MSE
424
  switch (xd->mi_8x8[0]->mbmi.sb_type) {
425
    case BLOCK_8X8:
426 427
      v_fn_ptr.vf = vp9_mse8x8;
      break;
428
    case BLOCK_16X8:
429 430
      v_fn_ptr.vf = vp9_mse16x8;
      break;
431
    case BLOCK_8X16:
432 433 434 435 436 437
      v_fn_ptr.vf = vp9_mse8x16;
      break;
    default:
      v_fn_ptr.vf = vp9_mse16x16;
      break;
  }
John Koleszar's avatar
John Koleszar committed
438 439

  // Set up pointers for this macro block recon buffer
440
  xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset;
John Koleszar's avatar
John Koleszar committed
441 442

  // Initial step/diamond search centred on best mv
443 444 445
  ref_mv_full.col = ref_mv->col >> 3;
  ref_mv_full.row = ref_mv->row >> 3;
  tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
446
                                    step_param,
John Koleszar's avatar
John Koleszar committed
447
                                    x->sadperbit16, &num00, &v_fn_ptr,
448
                                    x->nmvjointcost,
449
                                    x->mvcost, ref_mv);
John Koleszar's avatar
John Koleszar committed
450 451 452 453 454
  if (tmp_err < INT_MAX - new_mv_mode_penalty)
    tmp_err += new_mv_mode_penalty;

  if (tmp_err < *best_motion_err) {
    *best_motion_err = tmp_err;
455 456
    best_mv->row = tmp_mv.row;
    best_mv->col = tmp_mv.col;
John Koleszar's avatar
John Koleszar committed
457 458 459 460 461 462 463 464 465
  }

  // Further step/diamond searches as necessary
  n = num00;
  num00 = 0;

  while (n < further_steps) {
    n++;

466
    if (num00) {
John Koleszar's avatar
John Koleszar committed
467
      num00--;
468
    } else {
469
      tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
John Koleszar's avatar
John Koleszar committed
470 471
                                        step_param + n, x->sadperbit16,
                                        &num00, &v_fn_ptr,
472
                                        x->nmvjointcost,
473
                                        x->mvcost, ref_mv);
John Koleszar's avatar
John Koleszar committed
474
      if (tmp_err < INT_MAX - new_mv_mode_penalty)
John Koleszar's avatar
John Koleszar committed
475 476
        tmp_err += new_mv_mode_penalty;

John Koleszar's avatar
John Koleszar committed
477
      if (tmp_err < *best_motion_err) {
John Koleszar's avatar
John Koleszar committed
478
        *best_motion_err = tmp_err;
479 480
        best_mv->row = tmp_mv.row;
        best_mv->col = tmp_mv.col;
John Koleszar's avatar
John Koleszar committed
481
      }
John Koleszar's avatar
John Koleszar committed
482
    }
John Koleszar's avatar
John Koleszar committed
483
  }
John Koleszar's avatar
John Koleszar committed
484 485
}

486
void vp9_first_pass(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
487
  int mb_row, mb_col;
John Koleszar's avatar
John Koleszar committed
488
  MACROBLOCK *const x = &cpi->mb;
489
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
490
  MACROBLOCKD *const xd = &x->e_mbd;
James Zern's avatar
James Zern committed
491
  TileInfo tile;
492 493 494 495
  struct macroblock_plane *const p = x->plane;
  struct macroblockd_plane *const pd = xd->plane;
  PICK_MODE_CONTEXT *ctx = &x->sb64_context;
  int i;
John Koleszar's avatar
John Koleszar committed
496 497

  int recon_yoffset, recon_uvoffset;
498 499 500 501
  const int lst_yv12_idx = cm->ref_frame_map[cpi->lst_fb_idx];
  const int gld_yv12_idx = cm->ref_frame_map[cpi->gld_fb_idx];
  YV12_BUFFER_CONFIG *const lst_yv12 = &cm->yv12_fb[lst_yv12_idx];
  YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx];
502
  YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
503 504
  const int recon_y_stride = lst_yv12->y_stride;
  const int recon_uv_stride = lst_yv12->uv_stride;
Alex Converse's avatar
Alex Converse committed
505
  const int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height);
John Koleszar's avatar
John Koleszar committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
  int64_t intra_error = 0;
  int64_t coded_error = 0;
  int64_t sr_coded_error = 0;

  int sum_mvr = 0, sum_mvc = 0;
  int sum_mvr_abs = 0, sum_mvc_abs = 0;
  int sum_mvrs = 0, sum_mvcs = 0;
  int mvcount = 0;
  int intercount = 0;
  int second_ref_count = 0;
  int intrapenalty = 256;
  int neutral_count = 0;
  int new_mv_count = 0;
  int sum_in_vectors = 0;
  uint32_t lastmv_as_int = 0;

  int_mv zero_ref_mv;

  zero_ref_mv.as_int = 0;

526
  vp9_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
527

John Koleszar's avatar
John Koleszar committed
528
  vp9_setup_src_planes(x, cpi->Source, 0, 0);
529
  setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL);
530
  setup_dst_planes(xd, new_yv12, 0, 0);
John Koleszar's avatar
John Koleszar committed
531

532 533 534
  xd->mi_8x8 = cm->mi_grid_visible;
  // required for vp9_frame_init_quantizer
  xd->mi_8x8[0] = cm->mi;
John Koleszar's avatar
John Koleszar committed
535

536
  setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
John Koleszar's avatar
John Koleszar committed
537

538
  vp9_frame_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
539

540 541
  for (i = 0; i < MAX_MB_PLANE; ++i) {
    p[i].coeff = ctx->coeff_pbuf[i][1];
542
    p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
543
    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
544
    p[i].eobs = ctx->eobs_pbuf[i][1];
545
  }
546
  x->skip_recode = 0;
547 548


John Koleszar's avatar
John Koleszar committed
549 550 551 552
  // Initialise the MV cost table to the defaults
  // if( cm->current_video_frame == 0)
  // if ( 0 )
  {
553
    vp9_init_mv_probs(cm);
554
    vp9_initialize_rd_consts(cpi);
John Koleszar's avatar
John Koleszar committed
555 556
  }

James Zern's avatar
James Zern committed
557 558 559
  // tiling is ignored in the first pass
  vp9_tile_init(&tile, cm, 0, 0);

John Koleszar's avatar
John Koleszar committed
560 561 562 563 564 565 566 567 568
  // for each macroblock row in image
  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
    int_mv best_ref_mv;

    best_ref_mv.as_int = 0;

    // reset above block coeffs
    xd->up_available = (mb_row != 0);
    recon_yoffset = (mb_row * recon_y_stride * 16);
Alex Converse's avatar
Alex Converse committed
569
    recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height);
John Koleszar's avatar
John Koleszar committed
570

571 572 573
    // Set up limit values for motion vectors to prevent them extending
    // outside the UMV borders
    x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
John Koleszar's avatar
John Koleszar committed
574
    x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
575
                    + BORDER_MV_PIXELS_B16;
John Koleszar's avatar
John Koleszar committed
576 577 578 579 580 581

    // for each macroblock col in image
    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
      int this_error;
      int gf_motion_error = INT_MAX;
      int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
582
      double error_weight = 1.0;
583 584

      vp9_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
585

586 587 588
      xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
      xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
      xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
John Koleszar's avatar
John Koleszar committed
589 590
      xd->left_available = (mb_col != 0);

591 592
      if (mb_col * 2 + 1 < cm->mi_cols) {
        if (mb_row * 2 + 1 < cm->mi_rows) {
593
          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X16;
594
        } else {
595
          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X8;
596 597 598
        }
      } else {
        if (mb_row * 2 + 1 < cm->mi_rows) {
599
          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X16;
600
        } else {
601
          xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X8;
602 603
        }
      }
604
      xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME;
James Zern's avatar
James Zern committed
605
      set_mi_row_col(xd, &tile,
606
                     mb_row << 1,
607
                     num_8x8_blocks_high_lookup[xd->mi_8x8[0]->mbmi.sb_type],
608
                     mb_col << 1,
609
                     num_8x8_blocks_wide_lookup[xd->mi_8x8[0]->mbmi.sb_type],
James Zern's avatar
James Zern committed
610
                     cm->mi_rows, cm->mi_cols);
Jingning Han's avatar
Jingning Han committed
611

612
      if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
613
        int energy = vp9_block_energy(cpi, x, xd->mi_8x8[0]->mbmi.sb_type);
614 615 616
        error_weight = vp9_vaq_inv_q_ratio(energy);
      }

John Koleszar's avatar
John Koleszar committed
617
      // do intra 16x16 prediction
618
      this_error = vp9_encode_intra(x, use_dc_pred);
619
      if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
620 621 622
        vp9_clear_system_state();  // __asm emms;
        this_error *= error_weight;
      }
John Koleszar's avatar
John Koleszar committed
623

624 625 626 627 628 629
      // intrapenalty below deals with situations where the intra and inter
      // error scores are very low (eg a plain black frame).
      // We do not have special cases in first pass for 0,0 and nearest etc so
      // all inter modes carry an overhead cost estimate for the mv.
      // When the error score is very low this causes us to pick all or lots of
      // INTRA modes and throw lots of key frames.
John Koleszar's avatar
John Koleszar committed
630 631 632 633 634 635
      // This penalty adds a cost matching that of a 0,0 mv to the intra case.
      this_error += intrapenalty;

      // Cumulative intra error total
      intra_error += (int64_t)this_error;

636 637
      // Set up limit values for motion vectors to prevent them extending
      // outside the UMV borders.
638
      x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
John Koleszar's avatar
John Koleszar committed
639
      x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
640
                      + BORDER_MV_PIXELS_B16;
John Koleszar's avatar
John Koleszar committed
641 642 643 644

      // Other than for the first frame do a motion search
      if (cm->current_video_frame > 0) {
        int tmp_err;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
645
        int motion_error = zz_motion_search(cpi, x, lst_yv12, recon_yoffset);
John Koleszar's avatar
John Koleszar committed
646 647 648 649 650 651
        int_mv mv, tmp_mv;
        // Simple 0,0 motion with no mv overhead
        mv.as_int = tmp_mv.as_int = 0;

        // Test last reference frame using the previous best mv as the
        // starting point (best reference) for the search
652 653
        first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv,
                                 lst_yv12, &motion_error, recon_yoffset);
654
        if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
655 656 657
          vp9_clear_system_state();  // __asm emms;
          motion_error *= error_weight;
        }
John Koleszar's avatar
John Koleszar committed
658

659 660
        // If the current best reference mv is not centered on 0,0 then do a 0,0
        // based search as well.
John Koleszar's avatar
John Koleszar committed
661 662
        if (best_ref_mv.as_int) {
          tmp_err = INT_MAX;
663
          first_pass_motion_search(cpi, x, &zero_ref_mv.as_mv, &tmp_mv.as_mv,
John Koleszar's avatar
John Koleszar committed
664
                                   lst_yv12, &tmp_err, recon_yoffset);
665
          if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
666 667 668
            vp9_clear_system_state();  // __asm emms;
            tmp_err *= error_weight;
          }
John Koleszar's avatar
John Koleszar committed
669 670 671 672 673 674

          if (tmp_err < motion_error) {
            motion_error = tmp_err;
            mv.as_int = tmp_mv.as_int;
          }
        }
John Koleszar's avatar
John Koleszar committed
675

John Koleszar's avatar
John Koleszar committed
676 677 678
        // Experimental search in an older reference frame
        if (cm->current_video_frame > 1) {
          // Simple 0,0 motion with no mv overhead
Dmitry Kovalev's avatar
Dmitry Kovalev committed
679
          gf_motion_error = zz_motion_search(cpi, x, gld_yv12, recon_yoffset);
John Koleszar's avatar
John Koleszar committed
680

681 682
          first_pass_motion_search(cpi, x, &zero_ref_mv.as_mv, &tmp_mv.as_mv,
                                   gld_yv12, &gf_motion_error, recon_yoffset);
683
          if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
684 685 686
            vp9_clear_system_state();  // __asm emms;
            gf_motion_error *= error_weight;
          }
John Koleszar's avatar
John Koleszar committed
687 688 689 690 691 692 693

          if ((gf_motion_error < motion_error) &&
              (gf_motion_error < this_error)) {
            second_ref_count++;
          }

          // Reset to last frame as reference buffer
694 695 696
          xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset;
          xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset;
          xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset;
John Koleszar's avatar
John Koleszar committed
697 698 699 700 701 702 703 704 705

          // In accumulating a score for the older reference frame
          // take the best of the motion predicted score and
          // the intra coded error (just as will be done for)
          // accumulation of "coded_error" for the last frame.
          if (gf_motion_error < this_error)
            sr_coded_error += gf_motion_error;
          else
            sr_coded_error += this_error;
706
        } else {
John Koleszar's avatar
John Koleszar committed
707
          sr_coded_error += motion_error;
708
        }
John Koleszar's avatar
John Koleszar committed
709
        /* Intra assumed best */
710
        best_ref_mv.as_int = 0;
John Koleszar's avatar
John Koleszar committed
711

John Koleszar's avatar
John Koleszar committed
712 713 714 715 716
        if (motion_error <= this_error) {
          // Keep a count of cases where the inter and intra were
          // very close and very low. This helps with scene cut
          // detection for example in cropped clips with black bars
          // at the sides or top and bottom.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
717 718
          if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
              this_error < 2 * intrapenalty)
John Koleszar's avatar
John Koleszar committed
719 720
            neutral_count++;

Yaowu Xu's avatar
Yaowu Xu committed
721 722
          mv.as_mv.row *= 8;
          mv.as_mv.col *= 8;
John Koleszar's avatar
John Koleszar committed
723
          this_error = motion_error;
724
          vp9_set_mbmode_and_mvs(x, NEWMV, &mv);
725 726 727
          xd->mi_8x8[0]->mbmi.tx_size = TX_4X4;
          xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME;
          xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
728
          vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1,
729 730
                                         xd->mi_8x8[0]->mbmi.sb_type);
          vp9_encode_sby(x, xd->mi_8x8[0]->mbmi.sb_type);
John Koleszar's avatar
John Koleszar committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
          sum_mvr += mv.as_mv.row;
          sum_mvr_abs += abs(mv.as_mv.row);
          sum_mvc += mv.as_mv.col;
          sum_mvc_abs += abs(mv.as_mv.col);
          sum_mvrs += mv.as_mv.row * mv.as_mv.row;
          sum_mvcs += mv.as_mv.col * mv.as_mv.col;
          intercount++;

          best_ref_mv.as_int = mv.as_int;

          // Was the vector non-zero
          if (mv.as_int) {
            mvcount++;

            // Was it different from the last non zero vector
            if (mv.as_int != lastmv_as_int)
              new_mv_count++;
            lastmv_as_int = mv.as_int;

            // Does the Row vector point inwards or outwards
            if (mb_row < cm->mb_rows / 2) {
              if (mv.as_mv.row > 0)
                sum_in_vectors--;
              else if (mv.as_mv.row < 0)
                sum_in_vectors++;
            } else if (mb_row > cm->mb_rows / 2) {
              if (mv.as_mv.row > 0)
                sum_in_vectors++;
              else if (mv.as_mv.row < 0)
                sum_in_vectors--;
John Koleszar's avatar
John Koleszar committed
761 762
            }

John Koleszar's avatar
John Koleszar committed
763 764 765 766 767 768 769 770 771 772 773 774 775
            // Does the Row vector point inwards or outwards
            if (mb_col < cm->mb_cols / 2) {
              if (mv.as_mv.col > 0)
                sum_in_vectors--;
              else if (mv.as_mv.col < 0)
                sum_in_vectors++;
            } else if (mb_col > cm->mb_cols / 2) {
              if (mv.as_mv.col > 0)
                sum_in_vectors++;
              else if (mv.as_mv.col < 0)
                sum_in_vectors--;
            }
          }
John Koleszar's avatar
John Koleszar committed
776
        }
777
      } else {
John Koleszar's avatar
John Koleszar committed
778
        sr_coded_error += (int64_t)this_error;
779
      }
John Koleszar's avatar
John Koleszar committed
780
      coded_error += (int64_t)this_error;
John Koleszar's avatar
John Koleszar committed
781

John Koleszar's avatar
John Koleszar committed
782
      // adjust to the next column of macroblocks
John Koleszar's avatar
John Koleszar committed
783
      x->plane[0].src.buf += 16;
Alex Converse's avatar
Alex Converse committed
784 785
      x->plane[1].src.buf += uv_mb_height;
      x->plane[2].src.buf += uv_mb_height;
John Koleszar's avatar
John Koleszar committed
786

John Koleszar's avatar
John Koleszar committed
787
      recon_yoffset += 16;
Alex Converse's avatar
Alex Converse committed
788
      recon_uvoffset += uv_mb_height;
John Koleszar's avatar
John Koleszar committed
789 790
    }

John Koleszar's avatar
John Koleszar committed
791
    // adjust to the next row of mbs
John Koleszar's avatar
John Koleszar committed
792
    x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
Alex Converse's avatar
Alex Converse committed
793 794 795 796
    x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
                           uv_mb_height * cm->mb_cols;
    x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
                           uv_mb_height * cm->mb_cols;
John Koleszar's avatar
John Koleszar committed
797

798
    vp9_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
799 800
  }

801
  vp9_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
802 803 804 805 806 807
  {
    double weight = 0.0;

    FIRSTPASS_STATS fps;

    fps.frame      = cm->current_video_frame;
808 809 810
    fps.intra_error = (double)(intra_error >> 8);
    fps.coded_error = (double)(coded_error >> 8);
    fps.sr_coded_error = (double)(sr_coded_error >> 8);
John Koleszar's avatar
John Koleszar committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    weight = simple_weight(cpi->Source);


    if (weight < 0.1)
      weight = 0.1;

    fps.ssim_weighted_pred_err = fps.coded_error * weight;

    fps.pcnt_inter  = 0.0;
    fps.pcnt_motion = 0.0;
    fps.MVr        = 0.0;
    fps.mvr_abs     = 0.0;
    fps.MVc        = 0.0;
    fps.mvc_abs     = 0.0;
    fps.MVrv       = 0.0;
    fps.MVcv       = 0.0;
    fps.mv_in_out_count  = 0.0;
    fps.new_mv_count = 0.0;
    fps.count      = 1.0;

    fps.pcnt_inter   = 1.0 * (double)intercount / cm->MBs;
    fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
    fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;

    if (mvcount > 0) {
      fps.MVr = (double)sum_mvr / (double)mvcount;
      fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
      fps.MVc = (double)sum_mvc / (double)mvcount;
      fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
840 841 842 843
      fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) /
                 (double)mvcount;
      fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) /
                 (double)mvcount;
John Koleszar's avatar
John Koleszar committed
844 845 846 847
      fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
      fps.new_mv_count = new_mv_count;

      fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
Paul Wilkins's avatar
Paul Wilkins committed
848
    }
John Koleszar's avatar
John Koleszar committed
849

850 851 852
    // TODO(paulwilkins):  Handle the case when duration is set to 0, or
    // something less than the full time between subsequent values of
    // cpi->source_time_stamp.
853 854
    fps.duration = (double)(cpi->source->ts_end
                            - cpi->source->ts_start);
John Koleszar's avatar
John Koleszar committed
855 856

    // don't want to do output stats with a stack variable!
857
    cpi->twopass.this_frame_stats = fps;
858 859
    output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
    accumulate_stats(&cpi->twopass.total_stats, &fps);
John Koleszar's avatar
John Koleszar committed
860 861 862 863 864 865
  }

  // Copy the previous Last Frame back into gf and and arf buffers if
  // the prediction is good enough... but also dont allow it to lag too far
  if ((cpi->twopass.sr_update_lag > 3) ||
      ((cm->current_video_frame > 0) &&
866 867 868
       (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
       ((cpi->twopass.this_frame_stats.intra_error /
         DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
869
        2.0))) {
John Koleszar's avatar
John Koleszar committed
870
    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
John Koleszar's avatar
John Koleszar committed
871
    cpi->twopass.sr_update_lag = 1;
872
  } else {
John Koleszar's avatar
John Koleszar committed
873
    cpi->twopass.sr_update_lag++;
874
  }
John Koleszar's avatar
John Koleszar committed
875
  // swap frame pointers so last frame refers to the frame we just compressed
876 877
  swap_yv12(lst_yv12, new_yv12);

John Koleszar's avatar
John Koleszar committed
878
  vp9_extend_frame_borders(lst_yv12, cm->subsampling_x, cm->subsampling_y);
John Koleszar's avatar
John Koleszar committed
879

880 881
  // Special case for the first frame. Copy into the GF buffer as a second
  // reference.
882
  if (cm->current_video_frame == 0)
John Koleszar's avatar
John Koleszar committed
883
    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
John Koleszar's avatar
John Koleszar committed
884

John Koleszar's avatar
John Koleszar committed
885 886 887 888
  // use this to see what the first pass reconstruction looks like
  if (0) {
    char filename[512];
    FILE *recon_file;
889 890
    snprintf(filename, sizeof(filename), "enc%04d.yuv",
             (int)cm->current_video_frame);
John Koleszar's avatar
John Koleszar committed
891

John Koleszar's avatar
John Koleszar committed
892 893 894 895 896
    if (cm->current_video_frame == 0)
      recon_file = fopen(filename, "wb");
    else
      recon_file = fopen(filename, "ab");

Frank Galligan's avatar
Frank Galligan committed
897
    (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
John Koleszar's avatar
John Koleszar committed
898 899
    fclose(recon_file);
  }
John Koleszar's avatar
John Koleszar committed
900