vp9_firstpass.c 91.2 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
 */

#include "math.h"
#include "limits.h"
13
14
15
16
17
18
#include "vp9/encoder/vp9_block.h"
#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_variance.h"
#include "vp9/encoder/vp9_encodeintra.h"
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_firstpass.h"
Johann's avatar
Johann committed
19
#include "vpx_scale/vpx_scale.h"
20
#include "vp9/encoder/vp9_encodeframe.h"
21
#include "vp9/encoder/vp9_encodemb.h"
22
23
#include "vp9/common/vp9_extend.h"
#include "vp9/common/vp9_systemdependent.h"
John Koleszar's avatar
John Koleszar committed
24
#include "vpx_mem/vpx_mem.h"
25
#include "vpx_scale/yv12config.h"
John Koleszar's avatar
John Koleszar committed
26
#include <stdio.h>
27
#include "vp9/encoder/vp9_quantize.h"
28
29
#include "vp9/encoder/vp9_rdopt.h"
#include "vp9/encoder/vp9_ratectrl.h"
30
31
#include "vp9/common/vp9_quant_common.h"
#include "vp9/common/vp9_entropymv.h"
32
#include "vp9/encoder/vp9_encodemv.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

John Koleszar's avatar
John Koleszar committed
79
80

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

85
static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) {
John Koleszar's avatar
John Koleszar committed
86
87
  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
    return EOF;
John Koleszar's avatar
John Koleszar committed
88

John Koleszar's avatar
John Koleszar committed
89
90
  *next_frame = *cpi->twopass.stats_in;
  return 1;
John Koleszar's avatar
John Koleszar committed
91
92
}

93
// Read frame stats at an offset from the current position
94
static int read_frame_stats(VP9_COMP *cpi,
John Koleszar's avatar
John Koleszar committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
                            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;
110
111
}

112
static int input_stats(VP9_COMP *cpi, FIRSTPASS_STATS *fps) {
John Koleszar's avatar
John Koleszar committed
113
114
  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
    return EOF;
115

John Koleszar's avatar
John Koleszar committed
116
117
118
119
  *fps = *cpi->twopass.stats_in;
  cpi->twopass.stats_in =
    (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
  return 1;
120
121
}

122
static void output_stats(const VP9_COMP            *cpi,
123
                         struct vpx_codec_pkt_list *pktlist,
John Koleszar's avatar
John Koleszar committed
124
125
126
127
128
129
                         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);
130
131
132
133

// TEMP debug code
#if OUTPUT_FPF

John Koleszar's avatar
John Koleszar committed
134
135
136
137
  {
    FILE *fpfile;
    fpfile = fopen("firstpass.stt", "a");

138
    fprintf(stdout, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
John Koleszar's avatar
John Koleszar committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
            "%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);
  }
162
163
164
#endif
}

John Koleszar's avatar
John Koleszar committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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;
185
186
}

John Koleszar's avatar
John Koleszar committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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;
207
208
}

John Koleszar's avatar
John Koleszar committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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;
229
230
}

John Koleszar's avatar
John Koleszar committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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;
251
252
}

John Koleszar's avatar
John Koleszar committed
253
// Calculate a modified Error used in distributing bits between easier and harder frames
254
static double calculate_modified_err(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
255
256
257
258
259
  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
260
261
}

262
static const double weight_table[256] = {
John Koleszar's avatar
John Koleszar committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  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
295
296
};

John Koleszar's avatar
John Koleszar committed
297
298
299
static double simple_weight(YV12_BUFFER_CONFIG *source) {
  int i, j;

300
  uint8_t *src = source->y_buffer;
John Koleszar's avatar
John Koleszar committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  double sum_weights = 0.0;

  // Loop throught the Y plane raw examining levels and creating a weight for the image
  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
318
319
}

320

321
// This function returns the current per frame maximum bitrate target.
322
static int frame_max_bits(VP9_COMP *cpi) {
323
324
325
326
  // 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.
327
328
329
  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);
John Koleszar's avatar
John Koleszar committed
330

331
  // Trap case where we are out of bits.
332
  return MAX((int)max_bits, 0);
John Koleszar's avatar
John Koleszar committed
333
334
}

335
void vp9_init_first_pass(VP9_COMP *cpi) {
336
  zero_stats(&cpi->twopass.total_stats);
John Koleszar's avatar
John Koleszar committed
337
338
}

339
void vp9_end_first_pass(VP9_COMP *cpi) {
340
  output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
341
}
John Koleszar's avatar
John Koleszar committed
342

343
static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset) {
John Koleszar's avatar
John Koleszar committed
344
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
345

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

349
  switch (xd->mode_info_context->mbmi.sb_type) {
350
    case BLOCK_8X8:
351
352
353
354
      vp9_mse8x8(x->plane[0].src.buf, x->plane[0].src.stride,
                 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
                 (unsigned int *)(best_motion_err));
      break;
355
    case BLOCK_16X8:
356
357
358
359
      vp9_mse16x8(x->plane[0].src.buf, x->plane[0].src.stride,
                  xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
                  (unsigned int *)(best_motion_err));
      break;
360
    case BLOCK_8X16:
361
362
363
364
365
366
367
368
369
370
371
372
      vp9_mse8x16(x->plane[0].src.buf, x->plane[0].src.stride,
                  xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
                  (unsigned int *)(best_motion_err));
      break;
    default:
      vp9_mse16x16(x->plane[0].src.buf, x->plane[0].src.stride,
                   xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
                   (unsigned int *)(best_motion_err));
      break;
  }
}

373
static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
Scott LaVarnway's avatar
Scott LaVarnway committed
374
375
                                     int_mv *ref_mv, MV *best_mv,
                                     YV12_BUFFER_CONFIG *recon_buffer,
John Koleszar's avatar
John Koleszar committed
376
                                     int *best_motion_err, int recon_yoffset) {
John Koleszar's avatar
John Koleszar committed
377
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
378
379
380
381
382
383
384
385
386
  int num00;

  int_mv tmp_mv;
  int_mv ref_mv_full;

  int tmp_err;
  int step_param = 3;
  int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
  int n;
387
  vp9_variance_fn_ptr_t v_fn_ptr =
388
      cpi->fn_ptr[xd->mode_info_context->mbmi.sb_type];
John Koleszar's avatar
John Koleszar committed
389
390
  int new_mv_mode_penalty = 256;

391
  int sr = 0;
392
  int quart_frm = MIN(cpi->common.width, cpi->common.height);
393
394
395
396
397
398
399
400
401
402
403

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

  step_param    += sr;
  further_steps -= sr;

John Koleszar's avatar
John Koleszar committed
404
  // override the default variance function to use MSE
405
  switch (xd->mode_info_context->mbmi.sb_type) {
406
    case BLOCK_8X8:
407
408
      v_fn_ptr.vf = vp9_mse8x8;
      break;
409
    case BLOCK_16X8:
410
411
      v_fn_ptr.vf = vp9_mse16x8;
      break;
412
    case BLOCK_8X16:
413
414
415
416
417
418
      v_fn_ptr.vf = vp9_mse8x16;
      break;
    default:
      v_fn_ptr.vf = vp9_mse16x16;
      break;
  }
John Koleszar's avatar
John Koleszar committed
419
420

  // Set up pointers for this macro block recon buffer
421
  xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset;
John Koleszar's avatar
John Koleszar committed
422
423
424
425
426

  // Initial step/diamond search centred on best mv
  tmp_mv.as_int = 0;
  ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3;
  ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3;
John Koleszar's avatar
John Koleszar committed
427
  tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, step_param,
John Koleszar's avatar
John Koleszar committed
428
                                    x->sadperbit16, &num00, &v_fn_ptr,
429
430
                                    x->nmvjointcost,
                                    x->mvcost, ref_mv);
John Koleszar's avatar
John Koleszar committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
  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;
    best_mv->row = tmp_mv.as_mv.row;
    best_mv->col = tmp_mv.as_mv.col;
  }

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

  while (n < further_steps) {
    n++;

    if (num00)
      num00--;
    else {
John Koleszar's avatar
John Koleszar committed
450
      tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
John Koleszar's avatar
John Koleszar committed
451
452
                                        step_param + n, x->sadperbit16,
                                        &num00, &v_fn_ptr,
453
454
                                        x->nmvjointcost,
                                        x->mvcost, ref_mv);
John Koleszar's avatar
John Koleszar committed
455
      if (tmp_err < INT_MAX - new_mv_mode_penalty)
John Koleszar's avatar
John Koleszar committed
456
457
        tmp_err += new_mv_mode_penalty;

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

467
void vp9_first_pass(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
468
  int mb_row, mb_col;
John Koleszar's avatar
John Koleszar committed
469
  MACROBLOCK *const x = &cpi->mb;
470
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
471
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
472
473

  int recon_yoffset, recon_uvoffset;
474
475
476
477
478
479
480
  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 new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
  YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx];
  const int recon_y_stride = lst_yv12->y_stride;
  const int recon_uv_stride = lst_yv12->uv_stride;
John Koleszar's avatar
John Koleszar committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
  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;

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

John Koleszar's avatar
John Koleszar committed
503
  vp9_setup_src_planes(x, cpi->Source, 0, 0);
504
  setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL);
505
  setup_dst_planes(xd, new_yv12, 0, 0);
John Koleszar's avatar
John Koleszar committed
506
507

  x->partition_info = x->pi;
508
509

  xd->mode_info_context = cm->mi;
John Koleszar's avatar
John Koleszar committed
510

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

513
  vp9_frame_init_quantizer(cpi);
John Koleszar's avatar
John Koleszar committed
514
515
516
517
518

  // Initialise the MV cost table to the defaults
  // if( cm->current_video_frame == 0)
  // if ( 0 )
  {
519
    vp9_init_mv_probs(cm);
520
    vp9_initialize_rd_consts(cpi, cm->base_qindex + cm->y_dc_delta_q);
John Koleszar's avatar
John Koleszar committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
  }

  // 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);
    recon_uvoffset = (mb_row * recon_uv_stride * 8);

    // Set up limit values for motion vectors to prevent them extending outside the UMV borders
Jingning Han's avatar
Jingning Han committed
535
    x->mv_row_min = -((mb_row * 16) + (VP9BORDERINPIXELS - 8));
John Koleszar's avatar
John Koleszar committed
536
    x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
Jingning Han's avatar
Jingning Han committed
537
                    + (VP9BORDERINPIXELS - 8);
John Koleszar's avatar
John Koleszar committed
538
539
540
541
542
543
544

    // 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);

545
546
547
      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
548
549
      xd->left_available = (mb_col != 0);

550
551
      if (mb_col * 2 + 1 < cm->mi_cols) {
        if (mb_row * 2 + 1 < cm->mi_rows) {
552
          xd->mode_info_context->mbmi.sb_type = BLOCK_16X16;
553
        } else {
554
          xd->mode_info_context->mbmi.sb_type = BLOCK_16X8;
555
556
557
        }
      } else {
        if (mb_row * 2 + 1 < cm->mi_rows) {
558
          xd->mode_info_context->mbmi.sb_type = BLOCK_8X16;
559
        } else {
560
          xd->mode_info_context->mbmi.sb_type = BLOCK_8X8;
561
562
        }
      }
563
      xd->mode_info_context->mbmi.ref_frame[0] = INTRA_FRAME;
564
565
      set_mi_row_col(cm, xd,
                     mb_row << 1,
566
                     1 << mi_height_log2(xd->mode_info_context->mbmi.sb_type),
567
                     mb_col << 1,
568
                     1 << mi_height_log2(xd->mode_info_context->mbmi.sb_type));
Jingning Han's avatar
Jingning Han committed
569

John Koleszar's avatar
John Koleszar committed
570
      // do intra 16x16 prediction
571
      this_error = vp9_encode_intra(x, use_dc_pred);
John Koleszar's avatar
John Koleszar committed
572
573
574
575
576
577
578
579
580
581
582

      // "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 fot 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.
      // 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;

      // Set up limit values for motion vectors to prevent them extending outside the UMV borders
Jingning Han's avatar
Jingning Han committed
583
      x->mv_col_min = -((mb_col * 16) + (VP9BORDERINPIXELS - 8));
John Koleszar's avatar
John Koleszar committed
584
      x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
Jingning Han's avatar
Jingning Han committed
585
                      + (VP9BORDERINPIXELS - 8);
John Koleszar's avatar
John Koleszar committed
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613

      // Other than for the first frame do a motion search
      if (cm->current_video_frame > 0) {
        int tmp_err;
        int motion_error = INT_MAX;
        int_mv mv, tmp_mv;

        // Simple 0,0 motion with no mv overhead
        zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset);
        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
        first_pass_motion_search(cpi, x, &best_ref_mv,
                                 &mv.as_mv, lst_yv12,
                                 &motion_error, recon_yoffset);

        // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
        if (best_ref_mv.as_int) {
          tmp_err = INT_MAX;
          first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv,
                                   lst_yv12, &tmp_err, recon_yoffset);

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

John Koleszar's avatar
John Koleszar committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
        // Experimental search in an older reference frame
        if (cm->current_video_frame > 1) {
          // Simple 0,0 motion with no mv overhead
          zz_motion_search(cpi, x, gld_yv12,
                           &gf_motion_error, recon_yoffset);

          first_pass_motion_search(cpi, x, &zero_ref_mv,
                                   &tmp_mv.as_mv, gld_yv12,
                                   &gf_motion_error, recon_yoffset);

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

          // Reset to last frame as reference buffer
631
632
633
          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
634
635
636
637
638
639
640
641
642
643
644

          // 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;
        } else
          sr_coded_error += motion_error;
645

John Koleszar's avatar
John Koleszar committed
646
        /* Intra assumed best */
647
        best_ref_mv.as_int = 0;
John Koleszar's avatar
John Koleszar committed
648

John Koleszar's avatar
John Koleszar committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
        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.
          if ((((this_error - intrapenalty) * 9) <=
               (motion_error * 10)) &&
              (this_error < (2 * intrapenalty))) {
            neutral_count++;
          }

          mv.as_mv.row <<= 3;
          mv.as_mv.col <<= 3;
          this_error = motion_error;
663
          vp9_set_mbmode_and_mvs(x, NEWMV, &mv);
664
665
666
          xd->mode_info_context->mbmi.tx_size = TX_4X4;
          xd->mode_info_context->mbmi.ref_frame[0] = LAST_FRAME;
          xd->mode_info_context->mbmi.ref_frame[1] = NONE;
Jingning Han's avatar
Jingning Han committed
667
668
          vp9_build_inter_predictors_sby(xd, mb_row << 1,
                                         mb_col << 1,
669
670
                                         xd->mode_info_context->mbmi.sb_type);
          vp9_encode_sby(x, xd->mode_info_context->mbmi.sb_type);
John Koleszar's avatar
John Koleszar committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
          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
701
702
            }

John Koleszar's avatar
John Koleszar committed
703
704
705
706
707
708
709
710
711
712
713
714
715
            // 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
716
        }
John Koleszar's avatar
John Koleszar committed
717
718
      } else
        sr_coded_error += (int64_t)this_error;
John Koleszar's avatar
John Koleszar committed
719

John Koleszar's avatar
John Koleszar committed
720
      coded_error += (int64_t)this_error;
John Koleszar's avatar
John Koleszar committed
721

John Koleszar's avatar
John Koleszar committed
722
      // adjust to the next column of macroblocks
John Koleszar's avatar
John Koleszar committed
723
724
725
      x->plane[0].src.buf += 16;
      x->plane[1].src.buf += 8;
      x->plane[2].src.buf += 8;
John Koleszar's avatar
John Koleszar committed
726

John Koleszar's avatar
John Koleszar committed
727
728
      recon_yoffset += 16;
      recon_uvoffset += 8;
John Koleszar's avatar
John Koleszar committed
729
730
    }

John Koleszar's avatar
John Koleszar committed
731
    // adjust to the next row of mbs
John Koleszar's avatar
John Koleszar committed
732
733
734
    x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
    x->plane[1].src.buf += 8 * x->plane[1].src.stride - 8 * cm->mb_cols;
    x->plane[2].src.buf += 8 * x->plane[1].src.stride - 8 * cm->mb_cols;
John Koleszar's avatar
John Koleszar committed
735

736
    vp9_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
737
738
  }

739
  vp9_clear_system_state();  // __asm emms;
John Koleszar's avatar
John Koleszar committed
740
741
742
743
744
745
  {
    double weight = 0.0;

    FIRSTPASS_STATS fps;

    fps.frame      = cm->current_video_frame;
746
747
748
    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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
    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;
      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;
      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
784
    }
John Koleszar's avatar
John Koleszar committed
785

John Koleszar's avatar
John Koleszar committed
786
    // TODO:  handle the case when duration is set to 0, or something less
787
    // than the full time between subsequent values of cpi->source_time_stamp.
788
789
    fps.duration = (double)(cpi->source->ts_end
                            - cpi->source->ts_start);
John Koleszar's avatar
John Koleszar committed
790
791

    // don't want to do output stats with a stack variable!
792
    cpi->twopass.this_frame_stats = fps;
793
794
    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
795
796
797
798
799
800
  }

  // 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) &&
801
802
803
       (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)) >
804
        2.0))) {
John Koleszar's avatar
John Koleszar committed
805
    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
John Koleszar's avatar
John Koleszar committed
806
807
808
    cpi->twopass.sr_update_lag = 1;
  } else
    cpi->twopass.sr_update_lag++;
John Koleszar's avatar
John Koleszar committed
809

John Koleszar's avatar
John Koleszar committed
810
  // swap frame pointers so last frame refers to the frame we just compressed
811
812
  swap_yv12(lst_yv12, new_yv12);

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

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

John Koleszar's avatar
John Koleszar committed
819
820
821
822
823
  // use this to see what the first pass reconstruction looks like
  if (0) {
    char filename[512];
    FILE *recon_file;
    sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
John Koleszar's avatar
John Koleszar committed
824

John Koleszar's avatar
John Koleszar committed
825
826
827
828
829
    if (cm->current_video_frame == 0)
      recon_file = fopen(filename, "wb");
    else
      recon_file = fopen(filename, "ab");

Frank Galligan's avatar
Frank Galligan committed
830
    (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
John Koleszar's avatar
John Koleszar committed
831
832
    fclose(recon_file);
  }
John Koleszar's avatar
John Koleszar committed
833

John Koleszar's avatar
John Koleszar committed
834
  cm->current_video_frame++;
John Koleszar's avatar
John Koleszar committed
835
836
837

}

838
839
840
841
842
843
// Estimate a cost per mb attributable to overheads such as the coding of
// modes and motion vectors.
// Currently simplistic in its assumptions for testing.
//


844
static double bitcost(double prob) {
John Koleszar's avatar
John Koleszar committed
845
  return -(log(prob) / log(2.0));
846
}
847

848
static int64_t estimate_modemvcost(VP9_COMP *cpi,
John Koleszar's avatar
John Koleszar committed
849
                                     FIRSTPASS_STATS *fpstats) {
850
#if 0
John Koleszar's avatar
John Koleszar committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
  int mv_cost;
  int mode_cost;

  double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
  double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
  double av_intra = (1.0 - av_pct_inter);

  double zz_cost;
  double motion_cost;
  double intra_cost;

  zz_cost = bitcost(av_pct_inter - av_pct_motion);
  motion_cost = bitcost(av_pct_motion);
  intra_cost = bitcost(av_intra);

  // Estimate of extra bits per mv overhead for mbs
867
  // << 9 is the normalization to the (bits * 512) used in vp9_bits_per_mb
John Koleszar's avatar
John Koleszar committed
868
869
870
  mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;

  // Crude estimate of overhead cost from modes
871
  // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb
John Koleszar's avatar
John Koleszar committed
872
873
874
875
876
877
878
  mode_cost =
    (int)((((av_pct_inter - av_pct_motion) * zz_cost) +
           (av_pct_motion * motion_cost) +
           (av_intra * intra_cost)) * cpi->common.MBs) << 9;

  // return mv_cost + mode_cost;
  // TODO PGW Fix overhead costs for extended Q range
879
#endif
John Koleszar's avatar
John Koleszar committed
880
  return 0;
881
882
}

John Koleszar's avatar
John Koleszar committed
883
884
885
886
static double calc_correction_factor(double err_per_mb,
                                     double err_divisor,
                                     double pt_low,
                                     double pt_high,
887
888
                                     int q) {
  const double error_term = err_per_mb / err_divisor;
889

John Koleszar's avatar
John Koleszar committed
890
  // Adjustment based on actual quantizer to power term.
891
892
  const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.01 + pt_low,
                                pt_high);
893

John Koleszar's avatar
John Koleszar committed
894
  // Calculate correction factor
John Koleszar's avatar
John Koleszar committed
895
896
  if (power_term < 1.0)
    assert(error_term >= 0.0);
897

898
  return fclamp(pow(error_term, power_term), 0.05, 5.0);
899
900
}

Paul Wilkins's avatar
Paul Wilkins committed
901
902
// Given a current maxQ value sets a range for future values.
// PGW TODO..
903
// This code removes direct dependency on QIndex to determine the range
Paul Wilkins's avatar
Paul Wilkins committed
904
// (now uses the actual quantizer) but has not been tuned.
905
static void adjust_maxq_qrange(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
906
907
  int i;
  // Set the max corresponding to cpi->avg_q * 2.0
908
  double q = cpi->avg_q * 2.0;
John Koleszar's avatar
John Koleszar committed
909
910
911
  cpi->twopass.maxq_max_limit = cpi->worst_quality;
  for (i = cpi->best_quality; i <= cpi->worst_quality; i++) {
    cpi->twopass.maxq_max_limit = i;
912
    if (vp9_convert_qindex_to_q(i) >= q)
John Koleszar's avatar
John Koleszar committed
913
914
915
916
917
918
919
920
      break;
  }

  // Set the min corresponding to cpi->avg_q * 0.5
  q = cpi->avg_q * 0.5;
  cpi->twopass.maxq_min_limit = cpi->best_quality;
  for (i = cpi->worst_quality; i >= cpi->best_quality; i--) {
    cpi->twopass.maxq_min_limit = i;
921
    if (vp9_convert_qindex_to_q(i) <= q)
John Koleszar's avatar
John Koleszar committed
922
923
      break;
  }
Paul Wilkins's avatar
Paul Wilkins committed
924
}
Paul Wilkins's avatar
Paul Wilkins committed
925

926
static int estimate_max_q(VP9_COMP *cpi,
John Koleszar's avatar
John Koleszar committed
927
                          FIRSTPASS_STATS *fpstats,
928
                          int section_target_bandwitdh) {
929
  int q;
John Koleszar's avatar
John Koleszar committed
930
931
932
  int num_mbs = cpi->common.MBs;
  int target_norm_bits_per_mb;

933
  double section_err = fpstats->coded_error / fpstats->count;
John Koleszar's avatar
John Koleszar committed
934
935
936
937
938
939
940
941
  double sr_correction;
  double err_per_mb = section_err / num_mbs;
  double err_correction_factor;
  double speed_correction = 1.0;

  if (section_target_bandwitdh <= 0)
    return cpi->twopass.maxq_max_limit;          // Highest value allowed

942
943
944
  target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20)
                              ? (512 * section_target_bandwitdh) / num_mbs
                              : 512 * (section_target_bandwitdh / num_mbs);
John Koleszar's avatar
John Koleszar committed
945
946
947

  // Look at the drop in prediction quality between the last frame
  // and the GF buffer (which contained an older frame).
John Koleszar's avatar
John Koleszar committed
948
  if (fpstats->sr_coded_error > fpstats->coded_error) {
949
950
951
    double sr_err_diff = (fpstats->sr_coded_error - fpstats->coded_error) /
                             (fpstats->count * cpi->common.MBs);
    sr_correction = fclamp(pow(sr_err_diff / 32.0, 0.25), 0.75, 1.25);
John Koleszar's avatar
John Koleszar committed
952
  } else {
John Koleszar's avatar
John Koleszar committed
953
    sr_correction = 0.75;
John Koleszar's avatar
John Koleszar committed
954
  }
John Koleszar's avatar
John Koleszar committed
955
956
957

  // Calculate a corrective factor based on a rolling ratio of bits spent
  // vs target bits
958
959
960
961
  if (cpi->rolling_target_bits > 0 &&
      cpi->active_worst_quality < cpi->worst_quality) {
    double rolling_ratio = (double)cpi->rolling_actual_bits /
                               (double)cpi->rolling_target_bits;
John Koleszar's avatar
John Koleszar committed
962
963
964
965
966
967

    if (rolling_ratio < 0.95)
      cpi->twopass.est_max_qcorrection_factor -= 0.005;
    else if (rolling_ratio > 1.05)
      cpi->twopass.est_max_qcorrection_factor += 0.005;

968
969
    cpi->twopass.est_max_qcorrection_factor = fclamp(
        cpi->twopass.est_max_qcorrection_factor, 0.1, 10.0);
John Koleszar's avatar
John Koleszar committed
970
971
972
973
  }

  // Corrections for higher compression speed settings
  // (reduced compression expected)
974
975
  // FIXME(jimbankoski): Once we settle on vp9 speed features we need to
  // change this code.
976
977
  if (cpi->compressor_speed == 1)
    speed_correction = cpi->oxcf.cpu_used <= 5 ?
978
                          1.04 + (/*cpi->oxcf.cpu_used*/0 * 0.04) :
979
                          1.25;
John Koleszar's avatar
John Koleszar committed
980
981
982

  // Try and pick a max Q that will be high enough to encode the
  // content at the given rate.
983
  for (q = cpi->twopass.maxq_min_limit; q < cpi->twopass.maxq_max_limit; q++) {
John Koleszar's avatar
John Koleszar committed
984
985
    int bits_per_mb_at_this_q;

986
987
988
989
    err_correction_factor = calc_correction_factor(err_per_mb,
                                                   ERR_DIVISOR, 0.4, 0.90, q) *
                                sr_correction * speed_correction *
                                cpi->twopass.est_max_qcorrection_factor;
John Koleszar's avatar
John Koleszar committed
990

991
992
    bits_per_mb_at_this_q = vp9_bits_per_mb(INTER_FRAME, q,
                                            err_correction_factor);
John Koleszar's avatar
John Koleszar committed
993
994
995
996
997
998

    if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
      break;
  }

  // Restriction on active max q for constrained quality mode.
999
1000
1001
  if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
      q < cpi->cq_target_quality)
    q = cpi->cq_target_quality;
John Koleszar's avatar
John Koleszar committed
1002
1003

  // Adjust maxq_min_limit and maxq_max_limit limits based on
1004
  // average q observed in clip for non kf/gf/arf frames
John Koleszar's avatar
John Koleszar committed
1005
1006
  // Give average a chance to settle though.
  // PGW TODO.. This code is broken for the extended Q range
1007
1008
  if (cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8) &&
      cpi->ni_frames > 25)
John Koleszar's avatar
John Koleszar committed
1009
1010
    adjust_maxq_qrange(cpi);

1011
  return q;
John Koleszar's avatar
John Koleszar committed
1012
}
1013
1014
1015

// For cq mode estimate a cq level that matches the observed
// complexity and data rate.
1016
static int estimate_cq(VP9_COMP *cpi,
John Koleszar's avatar
John Koleszar committed
1017
                       FIRSTPASS_STATS *fpstats,
1018
                       int section_target_bandwitdh) {
1019
  int q;
John Koleszar's avatar
John Koleszar committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
  int num_mbs = cpi->common.MBs;
  int target_norm_bits_per_mb;

  double section_err = (fpstats->coded_error / fpstats->count);
  double err_per_mb = section_err / num_mbs;
  double err_correction_factor;
  double sr_err_diff;
  double sr_correction;
  double speed_correction = 1.0;
  double clip_iiratio;
  double clip_iifactor;

  target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
                            ? (512 * section_target_bandwitdh) / num_mbs
                            : 512 * (section_target_bandwitdh / num_mbs);


  // Corrections for higher compression speed settings
  // (reduced compression expected)
  if (cpi->compressor_speed == 1) {
    if (cpi->oxcf.cpu_used <= 5)
1041
      speed_correction = 1.04 + (/*cpi->oxcf.cpu_used*/ 0 * 0.04);
John Koleszar's avatar
John Koleszar committed
1042
1043
1044
1045
1046
1047
    else
      speed_correction = 1.25;
  }

  // Look at the drop in prediction quality between the last frame
  // and the GF buffer (which contained an older frame).
John Koleszar's avatar
John Koleszar committed
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
  if (fpstats->sr_coded_error > fpstats->coded_error) {
    sr_err_diff =
      (fpstats->sr_coded_error - fpstats->coded_error) /
      (fpstats->count * cpi->common.MBs);
    sr_correction = (sr_err_diff / 32.0);
    sr_correction = pow(sr_correction, 0.25);
    if (sr_correction < 0.75)
      sr_correction = 0.75;
    else if (sr_correction > 1.25)
      sr_correction = 1.25;
  } else {
John Koleszar's avatar
John Koleszar committed
1059
    sr_correction = 0.75;
John Koleszar's avatar
John Koleszar committed
1060
  }
John Koleszar's avatar
John Koleszar committed
1061
1062

  // II ratio correction factor for clip as a whole
1063
1064
  clip_iiratio = cpi->twopass.total_stats.intra_error /
                 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
John Koleszar's avatar
John Koleszar committed
1065
1066
1067
1068
1069
  clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
  if (clip_iifactor < 0.80)
    clip_iifactor = 0.80;

  // Try and pick a Q that can encode the content at the given rate.
1070
  for (q = 0; q < MAXQ; q++) {
John Koleszar's avatar
John Koleszar committed
1071
1072
1073
1074
    int bits_per_mb_at_this_q;

    // Error per MB based correction factor
    err_correction_factor =
1075
      calc_correction_factor(err_per_mb, 100.0, 0.4, 0.90, q) *
John Koleszar's avatar
John Koleszar committed
1076
1077
1078
      sr_correction * speed_correction * clip_iifactor;

    bits_per_mb_at_this_q =
1079
      vp9_bits_per_mb(INTER_FRAME, q, err_correction_factor);
1080

John Koleszar's avatar
John Koleszar committed
1081
1082
1083
    if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
      break;
  }
1084

John Koleszar's avatar
John Koleszar committed
1085
  // Clip value to range "best allowed to (worst allowed - 1)"
1086
1087
1088
1089
1090
  q = select_cq_level(q);
  if (q >= cpi->worst_quality)
    q = cpi->worst_quality - 1;
  if (q < cpi->best_quality)
    q = cpi->best_quality;
1091

1092
  return q;
1093
1094
}

James Zern's avatar
James Zern committed
1095
extern void vp9_new_framerate(VP9_COMP *cpi, double framerate);
John Koleszar's avatar
John Koleszar committed
1096

1097
void vp9_init_second_pass(VP9_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
1098
1099
  FIRSTPASS_STATS this_frame;
  FIRSTPASS_STATS *start_pos;
John Koleszar's avatar
John Koleszar committed
1100

James Zern's avatar
James Zern committed
1101
  double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
John Koleszar's avatar
John Koleszar committed
1102
1103
  double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
                                      * cpi->oxcf.two_pass_vbrmin_section / 100);
1104

John Koleszar's avatar
John Koleszar committed
1105
1106
  if (two_pass_min_rate < lower_bounds_min_rate)
    two_pass_min_rate = lower_bounds_min_rate;
John Koleszar's avatar
John Koleszar committed
1107

1108
1109
  zero_stats(&cpi->twopass.total_stats);
  zero_stats(&cpi->twopass.total_left_stats);
John Koleszar's avatar
John Koleszar committed
1110

John Koleszar's avatar
John Koleszar committed
1111
1112
  if (!cpi->twopass.stats_in_end)
    return;
John Koleszar's avatar
John Koleszar committed
1113

1114
1115
  cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
  cpi->twopass.total_left_stats = cpi->twopass.total_stats;
John Koleszar's avatar
John Koleszar committed
1116

John Koleszar's avatar
John Koleszar committed
1117
1118
1119
1120
1121
  // each frame can have a different duration, as the frame rate in the source
  // isn't guaranteed to be constant.   The frame rate prior to the first frame
  // encoded in the second pass is a guess.  However the sum duration is not.
  // Its calculated based on the actual durations of all frames from the first
  // pass.
James Zern's avatar
James Zern committed
1