vp9_firstpass.c revision f3bed9137f66ef693bd406e43b17e9a1114f1e14
1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "math.h"
12#include "limits.h"
13#include "vp9/encoder/vp9_block.h"
14#include "vp9/encoder/vp9_onyx_int.h"
15#include "vp9/encoder/vp9_variance.h"
16#include "vp9/encoder/vp9_encodeintra.h"
17#include "vp9/encoder/vp9_mcomp.h"
18#include "vp9/encoder/vp9_firstpass.h"
19#include "vpx_scale/vpx_scale.h"
20#include "vp9/encoder/vp9_encodeframe.h"
21#include "vp9/encoder/vp9_encodemb.h"
22#include "vp9/common/vp9_extend.h"
23#include "vp9/common/vp9_systemdependent.h"
24#include "vpx_mem/vpx_mem.h"
25#include "vpx_scale/yv12config.h"
26#include <stdio.h>
27#include "vp9/encoder/vp9_quantize.h"
28#include "vp9/encoder/vp9_rdopt.h"
29#include "vp9/encoder/vp9_ratectrl.h"
30#include "vp9/common/vp9_quant_common.h"
31#include "vp9/common/vp9_entropymv.h"
32#include "vp9/encoder/vp9_encodemv.h"
33#include "./vpx_scale_rtcd.h"
34// TODO(jkoleszar): for setup_dst_planes
35#include "vp9/common/vp9_reconinter.h"
36
37#define OUTPUT_FPF 0
38
39#define IIFACTOR   12.5
40#define IIKFACTOR1 12.5
41#define IIKFACTOR2 15.0
42#define RMAX       512.0
43#define GF_RMAX    96.0
44#define ERR_DIVISOR   150.0
45#define MIN_DECAY_FACTOR 0.1
46
47#define KF_MB_INTRA_MIN 150
48#define GF_MB_INTRA_MIN 100
49
50#define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
51
52#define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
53#define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
54
55static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
56  YV12_BUFFER_CONFIG temp = *a;
57  *a = *b;
58  *b = temp;
59}
60
61static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame);
62
63static int select_cq_level(int qindex) {
64  int ret_val = QINDEX_RANGE - 1;
65  int i;
66
67  double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0;
68
69  for (i = 0; i < QINDEX_RANGE; i++) {
70    if (target_q <= vp9_convert_qindex_to_q(i)) {
71      ret_val = i;
72      break;
73    }
74  }
75
76  return ret_val;
77}
78
79
80// Resets the first pass file to the given position using a relative seek from the current position
81static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) {
82  cpi->twopass.stats_in = position;
83}
84
85static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) {
86  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
87    return EOF;
88
89  *next_frame = *cpi->twopass.stats_in;
90  return 1;
91}
92
93// Read frame stats at an offset from the current position
94static int read_frame_stats(VP9_COMP *cpi,
95                            FIRSTPASS_STATS *frame_stats,
96                            int offset) {
97  FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in;
98
99  // Check legality of offset
100  if (offset >= 0) {
101    if (&fps_ptr[offset] >= cpi->twopass.stats_in_end)
102      return EOF;
103  } else if (offset < 0) {
104    if (&fps_ptr[offset] < cpi->twopass.stats_in_start)
105      return EOF;
106  }
107
108  *frame_stats = fps_ptr[offset];
109  return 1;
110}
111
112static int input_stats(VP9_COMP *cpi, FIRSTPASS_STATS *fps) {
113  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
114    return EOF;
115
116  *fps = *cpi->twopass.stats_in;
117  cpi->twopass.stats_in =
118    (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
119  return 1;
120}
121
122static void output_stats(const VP9_COMP            *cpi,
123                         struct vpx_codec_pkt_list *pktlist,
124                         FIRSTPASS_STATS            *stats) {
125  struct vpx_codec_cx_pkt pkt;
126  pkt.kind = VPX_CODEC_STATS_PKT;
127  pkt.data.twopass_stats.buf = stats;
128  pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
129  vpx_codec_pkt_list_add(pktlist, &pkt);
130
131// TEMP debug code
132#if OUTPUT_FPF
133
134  {
135    FILE *fpfile;
136    fpfile = fopen("firstpass.stt", "a");
137
138    fprintf(stdout, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
139            "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
140            "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
141            stats->frame,
142            stats->intra_error,
143            stats->coded_error,
144            stats->sr_coded_error,
145            stats->ssim_weighted_pred_err,
146            stats->pcnt_inter,
147            stats->pcnt_motion,
148            stats->pcnt_second_ref,
149            stats->pcnt_neutral,
150            stats->MVr,
151            stats->mvr_abs,
152            stats->MVc,
153            stats->mvc_abs,
154            stats->MVrv,
155            stats->MVcv,
156            stats->mv_in_out_count,
157            stats->new_mv_count,
158            stats->count,
159            stats->duration);
160    fclose(fpfile);
161  }
162#endif
163}
164
165static void zero_stats(FIRSTPASS_STATS *section) {
166  section->frame      = 0.0;
167  section->intra_error = 0.0;
168  section->coded_error = 0.0;
169  section->sr_coded_error = 0.0;
170  section->ssim_weighted_pred_err = 0.0;
171  section->pcnt_inter  = 0.0;
172  section->pcnt_motion  = 0.0;
173  section->pcnt_second_ref = 0.0;
174  section->pcnt_neutral = 0.0;
175  section->MVr        = 0.0;
176  section->mvr_abs     = 0.0;
177  section->MVc        = 0.0;
178  section->mvc_abs     = 0.0;
179  section->MVrv       = 0.0;
180  section->MVcv       = 0.0;
181  section->mv_in_out_count  = 0.0;
182  section->new_mv_count = 0.0;
183  section->count      = 0.0;
184  section->duration   = 1.0;
185}
186
187static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
188  section->frame += frame->frame;
189  section->intra_error += frame->intra_error;
190  section->coded_error += frame->coded_error;
191  section->sr_coded_error += frame->sr_coded_error;
192  section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
193  section->pcnt_inter  += frame->pcnt_inter;
194  section->pcnt_motion += frame->pcnt_motion;
195  section->pcnt_second_ref += frame->pcnt_second_ref;
196  section->pcnt_neutral += frame->pcnt_neutral;
197  section->MVr        += frame->MVr;
198  section->mvr_abs     += frame->mvr_abs;
199  section->MVc        += frame->MVc;
200  section->mvc_abs     += frame->mvc_abs;
201  section->MVrv       += frame->MVrv;
202  section->MVcv       += frame->MVcv;
203  section->mv_in_out_count  += frame->mv_in_out_count;
204  section->new_mv_count += frame->new_mv_count;
205  section->count      += frame->count;
206  section->duration   += frame->duration;
207}
208
209static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
210  section->frame -= frame->frame;
211  section->intra_error -= frame->intra_error;
212  section->coded_error -= frame->coded_error;
213  section->sr_coded_error -= frame->sr_coded_error;
214  section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
215  section->pcnt_inter  -= frame->pcnt_inter;
216  section->pcnt_motion -= frame->pcnt_motion;
217  section->pcnt_second_ref -= frame->pcnt_second_ref;
218  section->pcnt_neutral -= frame->pcnt_neutral;
219  section->MVr        -= frame->MVr;
220  section->mvr_abs     -= frame->mvr_abs;
221  section->MVc        -= frame->MVc;
222  section->mvc_abs     -= frame->mvc_abs;
223  section->MVrv       -= frame->MVrv;
224  section->MVcv       -= frame->MVcv;
225  section->mv_in_out_count  -= frame->mv_in_out_count;
226  section->new_mv_count -= frame->new_mv_count;
227  section->count      -= frame->count;
228  section->duration   -= frame->duration;
229}
230
231static void avg_stats(FIRSTPASS_STATS *section) {
232  if (section->count < 1.0)
233    return;
234
235  section->intra_error /= section->count;
236  section->coded_error /= section->count;
237  section->sr_coded_error /= section->count;
238  section->ssim_weighted_pred_err /= section->count;
239  section->pcnt_inter  /= section->count;
240  section->pcnt_second_ref /= section->count;
241  section->pcnt_neutral /= section->count;
242  section->pcnt_motion /= section->count;
243  section->MVr        /= section->count;
244  section->mvr_abs     /= section->count;
245  section->MVc        /= section->count;
246  section->mvc_abs     /= section->count;
247  section->MVrv       /= section->count;
248  section->MVcv       /= section->count;
249  section->mv_in_out_count   /= section->count;
250  section->duration   /= section->count;
251}
252
253// Calculate a modified Error used in distributing bits between easier and harder frames
254static double calculate_modified_err(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
255  const FIRSTPASS_STATS *const stats = &cpi->twopass.total_stats;
256  const double av_err = stats->ssim_weighted_pred_err / stats->count;
257  const double this_err = this_frame->ssim_weighted_pred_err;
258  return av_err * pow(this_err / DOUBLE_DIVIDE_CHECK(av_err),
259                      this_err > av_err ? POW1 : POW2);
260}
261
262static const double weight_table[256] = {
263  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
264  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
265  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
266  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
267  0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
268  0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
269  0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
270  0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
271  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
272  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
273  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
274  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
275  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
276  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
277  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
278  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
279  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
280  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
281  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
282  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
283  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
284  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
285  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
286  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
287  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
288  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
289  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
290  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
291  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
292  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
293  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
294  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
295};
296
297static double simple_weight(YV12_BUFFER_CONFIG *source) {
298  int i, j;
299
300  uint8_t *src = source->y_buffer;
301  double sum_weights = 0.0;
302
303  // Loop throught the Y plane raw examining levels and creating a weight for the image
304  i = source->y_height;
305  do {
306    j = source->y_width;
307    do {
308      sum_weights += weight_table[ *src];
309      src++;
310    } while (--j);
311    src -= source->y_width;
312    src += source->y_stride;
313  } while (--i);
314
315  sum_weights /= (source->y_height * source->y_width);
316
317  return sum_weights;
318}
319
320
321// This function returns the current per frame maximum bitrate target.
322static int frame_max_bits(VP9_COMP *cpi) {
323  // Max allocation for a single frame based on the max section guidelines
324  // passed in and how many bits are left.
325  // For VBR base this on the bits and frames left plus the
326  // two_pass_vbrmax_section rate passed in by the user.
327  const double max_bits = (1.0 * cpi->twopass.bits_left /
328      (cpi->twopass.total_stats.count - cpi->common.current_video_frame)) *
329      (cpi->oxcf.two_pass_vbrmax_section / 100.0);
330
331  // Trap case where we are out of bits.
332  return MAX((int)max_bits, 0);
333}
334
335void vp9_init_first_pass(VP9_COMP *cpi) {
336  zero_stats(&cpi->twopass.total_stats);
337}
338
339void vp9_end_first_pass(VP9_COMP *cpi) {
340  output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
341}
342
343static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset) {
344  MACROBLOCKD *const xd = &x->e_mbd;
345
346  // Set up pointers for this macro block recon buffer
347  xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset;
348
349  switch (xd->mode_info_context->mbmi.sb_type) {
350    case BLOCK_8X8:
351      vp9_mse8x8(x->plane[0].src.buf, x->plane[0].src.stride,
352                 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
353                 (unsigned int *)(best_motion_err));
354      break;
355    case BLOCK_16X8:
356      vp9_mse16x8(x->plane[0].src.buf, x->plane[0].src.stride,
357                  xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
358                  (unsigned int *)(best_motion_err));
359      break;
360    case BLOCK_8X16:
361      vp9_mse8x16(x->plane[0].src.buf, x->plane[0].src.stride,
362                  xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
363                  (unsigned int *)(best_motion_err));
364      break;
365    default:
366      vp9_mse16x16(x->plane[0].src.buf, x->plane[0].src.stride,
367                   xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride,
368                   (unsigned int *)(best_motion_err));
369      break;
370  }
371}
372
373static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
374                                     int_mv *ref_mv, MV *best_mv,
375                                     YV12_BUFFER_CONFIG *recon_buffer,
376                                     int *best_motion_err, int recon_yoffset) {
377  MACROBLOCKD *const xd = &x->e_mbd;
378  int num00;
379
380  int_mv tmp_mv;
381  int_mv ref_mv_full;
382
383  int tmp_err;
384  int step_param = 3;
385  int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
386  int n;
387  vp9_variance_fn_ptr_t v_fn_ptr =
388      cpi->fn_ptr[xd->mode_info_context->mbmi.sb_type];
389  int new_mv_mode_penalty = 256;
390
391  int sr = 0;
392  int quart_frm = MIN(cpi->common.width, cpi->common.height);
393
394  // refine the motion search range accroding to the frame dimension
395  // for first pass test
396  while ((quart_frm << sr) < MAX_FULL_PEL_VAL)
397    sr++;
398  if (sr)
399    sr--;
400
401  step_param    += sr;
402  further_steps -= sr;
403
404  // override the default variance function to use MSE
405  switch (xd->mode_info_context->mbmi.sb_type) {
406    case BLOCK_8X8:
407      v_fn_ptr.vf = vp9_mse8x8;
408      break;
409    case BLOCK_16X8:
410      v_fn_ptr.vf = vp9_mse16x8;
411      break;
412    case BLOCK_8X16:
413      v_fn_ptr.vf = vp9_mse8x16;
414      break;
415    default:
416      v_fn_ptr.vf = vp9_mse16x16;
417      break;
418  }
419
420  // Set up pointers for this macro block recon buffer
421  xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset;
422
423  // Initial step/diamond search centred on best mv
424  tmp_mv.as_int = 0;
425  ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3;
426  ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3;
427  tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, step_param,
428                                    x->sadperbit16, &num00, &v_fn_ptr,
429                                    x->nmvjointcost,
430                                    x->mvcost, ref_mv);
431  if (tmp_err < INT_MAX - new_mv_mode_penalty)
432    tmp_err += new_mv_mode_penalty;
433
434  if (tmp_err < *best_motion_err) {
435    *best_motion_err = tmp_err;
436    best_mv->row = tmp_mv.as_mv.row;
437    best_mv->col = tmp_mv.as_mv.col;
438  }
439
440  // Further step/diamond searches as necessary
441  n = num00;
442  num00 = 0;
443
444  while (n < further_steps) {
445    n++;
446
447    if (num00)
448      num00--;
449    else {
450      tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
451                                        step_param + n, x->sadperbit16,
452                                        &num00, &v_fn_ptr,
453                                        x->nmvjointcost,
454                                        x->mvcost, ref_mv);
455      if (tmp_err < INT_MAX - new_mv_mode_penalty)
456        tmp_err += new_mv_mode_penalty;
457
458      if (tmp_err < *best_motion_err) {
459        *best_motion_err = tmp_err;
460        best_mv->row = tmp_mv.as_mv.row;
461        best_mv->col = tmp_mv.as_mv.col;
462      }
463    }
464  }
465}
466
467void vp9_first_pass(VP9_COMP *cpi) {
468  int mb_row, mb_col;
469  MACROBLOCK *const x = &cpi->mb;
470  VP9_COMMON *const cm = &cpi->common;
471  MACROBLOCKD *const xd = &x->e_mbd;
472
473  int recon_yoffset, recon_uvoffset;
474  const int lst_yv12_idx = cm->ref_frame_map[cpi->lst_fb_idx];
475  const int gld_yv12_idx = cm->ref_frame_map[cpi->gld_fb_idx];
476  YV12_BUFFER_CONFIG *const lst_yv12 = &cm->yv12_fb[lst_yv12_idx];
477  YV12_BUFFER_CONFIG *const new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
478  YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx];
479  const int recon_y_stride = lst_yv12->y_stride;
480  const int recon_uv_stride = lst_yv12->uv_stride;
481  int64_t intra_error = 0;
482  int64_t coded_error = 0;
483  int64_t sr_coded_error = 0;
484
485  int sum_mvr = 0, sum_mvc = 0;
486  int sum_mvr_abs = 0, sum_mvc_abs = 0;
487  int sum_mvrs = 0, sum_mvcs = 0;
488  int mvcount = 0;
489  int intercount = 0;
490  int second_ref_count = 0;
491  int intrapenalty = 256;
492  int neutral_count = 0;
493  int new_mv_count = 0;
494  int sum_in_vectors = 0;
495  uint32_t lastmv_as_int = 0;
496
497  int_mv zero_ref_mv;
498
499  zero_ref_mv.as_int = 0;
500
501  vp9_clear_system_state();  // __asm emms;
502
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);
506
507  x->partition_info = x->pi;
508
509  xd->mode_info_context = cm->mi;
510
511  setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
512
513  vp9_frame_init_quantizer(cpi);
514
515  // Initialise the MV cost table to the defaults
516  // if( cm->current_video_frame == 0)
517  // if ( 0 )
518  {
519    vp9_init_mv_probs(cm);
520    vp9_initialize_rd_consts(cpi, cm->base_qindex + cm->y_dc_delta_q);
521  }
522
523  // for each macroblock row in image
524  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
525    int_mv best_ref_mv;
526
527    best_ref_mv.as_int = 0;
528
529    // reset above block coeffs
530    xd->up_available = (mb_row != 0);
531    recon_yoffset = (mb_row * recon_y_stride * 16);
532    recon_uvoffset = (mb_row * recon_uv_stride * 8);
533
534    // Set up limit values for motion vectors to prevent them extending outside the UMV borders
535    x->mv_row_min = -((mb_row * 16) + (VP9BORDERINPIXELS - 8));
536    x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
537                    + (VP9BORDERINPIXELS - 8);
538
539    // for each macroblock col in image
540    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
541      int this_error;
542      int gf_motion_error = INT_MAX;
543      int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
544
545      xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
546      xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
547      xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
548      xd->left_available = (mb_col != 0);
549
550      if (mb_col * 2 + 1 < cm->mi_cols) {
551        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      } else {
557        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      set_mi_row_col(cm, xd,
565                     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));
569
570      // do intra 16x16 prediction
571      this_error = vp9_encode_intra(cpi, x, use_dc_pred);
572
573      // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
574      // 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.
575      // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
576      // This penalty adds a cost matching that of a 0,0 mv to the intra case.
577      this_error += intrapenalty;
578
579      // Cumulative intra error total
580      intra_error += (int64_t)this_error;
581
582      // Set up limit values for motion vectors to prevent them extending outside the UMV borders
583      x->mv_col_min = -((mb_col * 16) + (VP9BORDERINPIXELS - 8));
584      x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
585                      + (VP9BORDERINPIXELS - 8);
586
587      // Other than for the first frame do a motion search
588      if (cm->current_video_frame > 0) {
589        int tmp_err;
590        int motion_error = INT_MAX;
591        int_mv mv, tmp_mv;
592
593        // Simple 0,0 motion with no mv overhead
594        zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset);
595        mv.as_int = tmp_mv.as_int = 0;
596
597        // Test last reference frame using the previous best mv as the
598        // starting point (best reference) for the search
599        first_pass_motion_search(cpi, x, &best_ref_mv,
600                                 &mv.as_mv, lst_yv12,
601                                 &motion_error, recon_yoffset);
602
603        // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
604        if (best_ref_mv.as_int) {
605          tmp_err = INT_MAX;
606          first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv,
607                                   lst_yv12, &tmp_err, recon_yoffset);
608
609          if (tmp_err < motion_error) {
610            motion_error = tmp_err;
611            mv.as_int = tmp_mv.as_int;
612          }
613        }
614
615        // Experimental search in an older reference frame
616        if (cm->current_video_frame > 1) {
617          // Simple 0,0 motion with no mv overhead
618          zz_motion_search(cpi, x, gld_yv12,
619                           &gf_motion_error, recon_yoffset);
620
621          first_pass_motion_search(cpi, x, &zero_ref_mv,
622                                   &tmp_mv.as_mv, gld_yv12,
623                                   &gf_motion_error, recon_yoffset);
624
625          if ((gf_motion_error < motion_error) &&
626              (gf_motion_error < this_error)) {
627            second_ref_count++;
628          }
629
630          // Reset to last frame as reference buffer
631          xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset;
632          xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset;
633          xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset;
634
635          // In accumulating a score for the older reference frame
636          // take the best of the motion predicted score and
637          // the intra coded error (just as will be done for)
638          // accumulation of "coded_error" for the last frame.
639          if (gf_motion_error < this_error)
640            sr_coded_error += gf_motion_error;
641          else
642            sr_coded_error += this_error;
643        } else
644          sr_coded_error += motion_error;
645
646        /* Intra assumed best */
647        best_ref_mv.as_int = 0;
648
649        if (motion_error <= this_error) {
650          // Keep a count of cases where the inter and intra were
651          // very close and very low. This helps with scene cut
652          // detection for example in cropped clips with black bars
653          // at the sides or top and bottom.
654          if ((((this_error - intrapenalty) * 9) <=
655               (motion_error * 10)) &&
656              (this_error < (2 * intrapenalty))) {
657            neutral_count++;
658          }
659
660          mv.as_mv.row <<= 3;
661          mv.as_mv.col <<= 3;
662          this_error = motion_error;
663          vp9_set_mbmode_and_mvs(x, NEWMV, &mv);
664          xd->mode_info_context->mbmi.txfm_size = TX_4X4;
665          xd->mode_info_context->mbmi.ref_frame[0] = LAST_FRAME;
666          xd->mode_info_context->mbmi.ref_frame[1] = NONE;
667          vp9_build_inter_predictors_sby(xd, mb_row << 1,
668                                         mb_col << 1,
669                                         xd->mode_info_context->mbmi.sb_type);
670          vp9_encode_sby(cm, x, xd->mode_info_context->mbmi.sb_type);
671          sum_mvr += mv.as_mv.row;
672          sum_mvr_abs += abs(mv.as_mv.row);
673          sum_mvc += mv.as_mv.col;
674          sum_mvc_abs += abs(mv.as_mv.col);
675          sum_mvrs += mv.as_mv.row * mv.as_mv.row;
676          sum_mvcs += mv.as_mv.col * mv.as_mv.col;
677          intercount++;
678
679          best_ref_mv.as_int = mv.as_int;
680
681          // Was the vector non-zero
682          if (mv.as_int) {
683            mvcount++;
684
685            // Was it different from the last non zero vector
686            if (mv.as_int != lastmv_as_int)
687              new_mv_count++;
688            lastmv_as_int = mv.as_int;
689
690            // Does the Row vector point inwards or outwards
691            if (mb_row < cm->mb_rows / 2) {
692              if (mv.as_mv.row > 0)
693                sum_in_vectors--;
694              else if (mv.as_mv.row < 0)
695                sum_in_vectors++;
696            } else if (mb_row > cm->mb_rows / 2) {
697              if (mv.as_mv.row > 0)
698                sum_in_vectors++;
699              else if (mv.as_mv.row < 0)
700                sum_in_vectors--;
701            }
702
703            // Does the Row vector point inwards or outwards
704            if (mb_col < cm->mb_cols / 2) {
705              if (mv.as_mv.col > 0)
706                sum_in_vectors--;
707              else if (mv.as_mv.col < 0)
708                sum_in_vectors++;
709            } else if (mb_col > cm->mb_cols / 2) {
710              if (mv.as_mv.col > 0)
711                sum_in_vectors++;
712              else if (mv.as_mv.col < 0)
713                sum_in_vectors--;
714            }
715          }
716        }
717      } else
718        sr_coded_error += (int64_t)this_error;
719
720      coded_error += (int64_t)this_error;
721
722      // adjust to the next column of macroblocks
723      x->plane[0].src.buf += 16;
724      x->plane[1].src.buf += 8;
725      x->plane[2].src.buf += 8;
726
727      recon_yoffset += 16;
728      recon_uvoffset += 8;
729    }
730
731    // adjust to the next row of mbs
732    x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
733    x->plane[1].src.buf += 8 * x->plane[1].src.stride - 8 * cm->mb_cols;
734    x->plane[2].src.buf += 8 * x->plane[1].src.stride - 8 * cm->mb_cols;
735
736    vp9_clear_system_state();  // __asm emms;
737  }
738
739  vp9_clear_system_state();  // __asm emms;
740  {
741    double weight = 0.0;
742
743    FIRSTPASS_STATS fps;
744
745    fps.frame      = cm->current_video_frame;
746    fps.intra_error = (double)(intra_error >> 8);
747    fps.coded_error = (double)(coded_error >> 8);
748    fps.sr_coded_error = (double)(sr_coded_error >> 8);
749    weight = simple_weight(cpi->Source);
750
751
752    if (weight < 0.1)
753      weight = 0.1;
754
755    fps.ssim_weighted_pred_err = fps.coded_error * weight;
756
757    fps.pcnt_inter  = 0.0;
758    fps.pcnt_motion = 0.0;
759    fps.MVr        = 0.0;
760    fps.mvr_abs     = 0.0;
761    fps.MVc        = 0.0;
762    fps.mvc_abs     = 0.0;
763    fps.MVrv       = 0.0;
764    fps.MVcv       = 0.0;
765    fps.mv_in_out_count  = 0.0;
766    fps.new_mv_count = 0.0;
767    fps.count      = 1.0;
768
769    fps.pcnt_inter   = 1.0 * (double)intercount / cm->MBs;
770    fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
771    fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
772
773    if (mvcount > 0) {
774      fps.MVr = (double)sum_mvr / (double)mvcount;
775      fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
776      fps.MVc = (double)sum_mvc / (double)mvcount;
777      fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
778      fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
779      fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
780      fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
781      fps.new_mv_count = new_mv_count;
782
783      fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
784    }
785
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    fps.duration = (double)(cpi->source->ts_end
789                            - cpi->source->ts_start);
790
791    // don't want to do output stats with a stack variable!
792    cpi->twopass.this_frame_stats = fps;
793    output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
794    accumulate_stats(&cpi->twopass.total_stats, &fps);
795  }
796
797  // Copy the previous Last Frame back into gf and and arf buffers if
798  // the prediction is good enough... but also dont allow it to lag too far
799  if ((cpi->twopass.sr_update_lag > 3) ||
800      ((cm->current_video_frame > 0) &&
801       (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
802       ((cpi->twopass.this_frame_stats.intra_error /
803         DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
804        2.0))) {
805    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
806    cpi->twopass.sr_update_lag = 1;
807  } else
808    cpi->twopass.sr_update_lag++;
809
810  // swap frame pointers so last frame refers to the frame we just compressed
811  swap_yv12(lst_yv12, new_yv12);
812
813  vp9_extend_frame_borders(lst_yv12, cm->subsampling_x, cm->subsampling_y);
814
815  // Special case for the first frame. Copy into the GF buffer as a second reference.
816  if (cm->current_video_frame == 0)
817    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
818
819  // use this to see what the first pass reconstruction looks like
820  if (0) {
821    char filename[512];
822    FILE *recon_file;
823    sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
824
825    if (cm->current_video_frame == 0)
826      recon_file = fopen(filename, "wb");
827    else
828      recon_file = fopen(filename, "ab");
829
830    (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
831    fclose(recon_file);
832  }
833
834  cm->current_video_frame++;
835
836}
837
838// Estimate a cost per mb attributable to overheads such as the coding of
839// modes and motion vectors.
840// Currently simplistic in its assumptions for testing.
841//
842
843
844static double bitcost(double prob) {
845  return -(log(prob) / log(2.0));
846}
847
848static int64_t estimate_modemvcost(VP9_COMP *cpi,
849                                     FIRSTPASS_STATS *fpstats) {
850#if 0
851  int mv_cost;
852  int mode_cost;
853
854  double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
855  double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
856  double av_intra = (1.0 - av_pct_inter);
857
858  double zz_cost;
859  double motion_cost;
860  double intra_cost;
861
862  zz_cost = bitcost(av_pct_inter - av_pct_motion);
863  motion_cost = bitcost(av_pct_motion);
864  intra_cost = bitcost(av_intra);
865
866  // Estimate of extra bits per mv overhead for mbs
867  // << 9 is the normalization to the (bits * 512) used in vp9_bits_per_mb
868  mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
869
870  // Crude estimate of overhead cost from modes
871  // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb
872  mode_cost =
873    (int)((((av_pct_inter - av_pct_motion) * zz_cost) +
874           (av_pct_motion * motion_cost) +
875           (av_intra * intra_cost)) * cpi->common.MBs) << 9;
876
877  // return mv_cost + mode_cost;
878  // TODO PGW Fix overhead costs for extended Q range
879#endif
880  return 0;
881}
882
883static double calc_correction_factor(double err_per_mb,
884                                     double err_divisor,
885                                     double pt_low,
886                                     double pt_high,
887                                     int q) {
888  const double error_term = err_per_mb / err_divisor;
889
890  // Adjustment based on actual quantizer to power term.
891  const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.01 + pt_low,
892                                pt_high);
893
894  // Calculate correction factor
895  if (power_term < 1.0)
896    assert(error_term >= 0.0);
897
898  return fclamp(pow(error_term, power_term), 0.05, 5.0);
899}
900
901// Given a current maxQ value sets a range for future values.
902// PGW TODO..
903// This code removes direct dependency on QIndex to determine the range
904// (now uses the actual quantizer) but has not been tuned.
905static void adjust_maxq_qrange(VP9_COMP *cpi) {
906  int i;
907  // Set the max corresponding to cpi->avg_q * 2.0
908  double q = cpi->avg_q * 2.0;
909  cpi->twopass.maxq_max_limit = cpi->worst_quality;
910  for (i = cpi->best_quality; i <= cpi->worst_quality; i++) {
911    cpi->twopass.maxq_max_limit = i;
912    if (vp9_convert_qindex_to_q(i) >= q)
913      break;
914  }
915
916  // Set the min corresponding to cpi->avg_q * 0.5
917  q = cpi->avg_q * 0.5;
918  cpi->twopass.maxq_min_limit = cpi->best_quality;
919  for (i = cpi->worst_quality; i >= cpi->best_quality; i--) {
920    cpi->twopass.maxq_min_limit = i;
921    if (vp9_convert_qindex_to_q(i) <= q)
922      break;
923  }
924}
925
926static int estimate_max_q(VP9_COMP *cpi,
927                          FIRSTPASS_STATS *fpstats,
928                          int section_target_bandwitdh) {
929  int q;
930  int num_mbs = cpi->common.MBs;
931  int target_norm_bits_per_mb;
932
933  double section_err = fpstats->coded_error / fpstats->count;
934  double sr_correction;
935  double err_per_mb = section_err / num_mbs;
936  double err_correction_factor;
937  double speed_correction = 1.0;
938
939  if (section_target_bandwitdh <= 0)
940    return cpi->twopass.maxq_max_limit;          // Highest value allowed
941
942  target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20)
943                              ? (512 * section_target_bandwitdh) / num_mbs
944                              : 512 * (section_target_bandwitdh / num_mbs);
945
946  // Look at the drop in prediction quality between the last frame
947  // and the GF buffer (which contained an older frame).
948  if (fpstats->sr_coded_error > fpstats->coded_error) {
949    double sr_err_diff = (fpstats->sr_coded_error - fpstats->coded_error) /
950                             (fpstats->count * cpi->common.MBs);
951    sr_correction = fclamp(pow(sr_err_diff / 32.0, 0.25), 0.75, 1.25);
952  } else {
953    sr_correction = 0.75;
954  }
955
956  // Calculate a corrective factor based on a rolling ratio of bits spent
957  // vs target bits
958  if (cpi->rolling_target_bits > 0 &&
959      cpi->active_worst_quality < cpi->worst_quality) {
960    double rolling_ratio = (double)cpi->rolling_actual_bits /
961                               (double)cpi->rolling_target_bits;
962
963    if (rolling_ratio < 0.95)
964      cpi->twopass.est_max_qcorrection_factor -= 0.005;
965    else if (rolling_ratio > 1.05)
966      cpi->twopass.est_max_qcorrection_factor += 0.005;
967
968    cpi->twopass.est_max_qcorrection_factor = fclamp(
969        cpi->twopass.est_max_qcorrection_factor, 0.1, 10.0);
970  }
971
972  // Corrections for higher compression speed settings
973  // (reduced compression expected)
974  // FIXME(jimbankoski): Once we settle on vp9 speed features we need to
975  // change this code.
976  if (cpi->compressor_speed == 1)
977    speed_correction = cpi->oxcf.cpu_used <= 5 ?
978                          1.04 + (/*cpi->oxcf.cpu_used*/0 * 0.04) :
979                          1.25;
980
981  // Try and pick a max Q that will be high enough to encode the
982  // content at the given rate.
983  for (q = cpi->twopass.maxq_min_limit; q < cpi->twopass.maxq_max_limit; q++) {
984    int bits_per_mb_at_this_q;
985
986    err_correction_factor = calc_correction_factor(err_per_mb,
987                                                   ERR_DIVISOR, 0.4, 0.90, q) *
988                                sr_correction * speed_correction *
989                                cpi->twopass.est_max_qcorrection_factor;
990
991    bits_per_mb_at_this_q = vp9_bits_per_mb(INTER_FRAME, q,
992                                            err_correction_factor);
993
994    if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
995      break;
996  }
997
998  // Restriction on active max q for constrained quality mode.
999  if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
1000      q < cpi->cq_target_quality)
1001    q = cpi->cq_target_quality;
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
1005  // Give average a chance to settle though.
1006  // PGW TODO.. This code is broken for the extended Q range
1007  if (cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8) &&
1008      cpi->ni_frames > 25)
1009    adjust_maxq_qrange(cpi);
1010
1011  return q;
1012}
1013
1014// For cq mode estimate a cq level that matches the observed
1015// complexity and data rate.
1016static int estimate_cq(VP9_COMP *cpi,
1017                       FIRSTPASS_STATS *fpstats,
1018                       int section_target_bandwitdh) {
1019  int q;
1020  int num_mbs = cpi->common.MBs;
1021  int target_norm_bits_per_mb;
1022
1023  double section_err = (fpstats->coded_error / fpstats->count);
1024  double err_per_mb = section_err / num_mbs;
1025  double err_correction_factor;
1026  double sr_err_diff;
1027  double sr_correction;
1028  double speed_correction = 1.0;
1029  double clip_iiratio;
1030  double clip_iifactor;
1031
1032  target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1033                            ? (512 * section_target_bandwitdh) / num_mbs
1034                            : 512 * (section_target_bandwitdh / num_mbs);
1035
1036
1037  // Corrections for higher compression speed settings
1038  // (reduced compression expected)
1039  if (cpi->compressor_speed == 1) {
1040    if (cpi->oxcf.cpu_used <= 5)
1041      speed_correction = 1.04 + (/*cpi->oxcf.cpu_used*/ 0 * 0.04);
1042    else
1043      speed_correction = 1.25;
1044  }
1045
1046  // Look at the drop in prediction quality between the last frame
1047  // and the GF buffer (which contained an older frame).
1048  if (fpstats->sr_coded_error > fpstats->coded_error) {
1049    sr_err_diff =
1050      (fpstats->sr_coded_error - fpstats->coded_error) /
1051      (fpstats->count * cpi->common.MBs);
1052    sr_correction = (sr_err_diff / 32.0);
1053    sr_correction = pow(sr_correction, 0.25);
1054    if (sr_correction < 0.75)
1055      sr_correction = 0.75;
1056    else if (sr_correction > 1.25)
1057      sr_correction = 1.25;
1058  } else {
1059    sr_correction = 0.75;
1060  }
1061
1062  // II ratio correction factor for clip as a whole
1063  clip_iiratio = cpi->twopass.total_stats.intra_error /
1064                 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
1065  clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1066  if (clip_iifactor < 0.80)
1067    clip_iifactor = 0.80;
1068
1069  // Try and pick a Q that can encode the content at the given rate.
1070  for (q = 0; q < MAXQ; q++) {
1071    int bits_per_mb_at_this_q;
1072
1073    // Error per MB based correction factor
1074    err_correction_factor =
1075      calc_correction_factor(err_per_mb, 100.0, 0.4, 0.90, q) *
1076      sr_correction * speed_correction * clip_iifactor;
1077
1078    bits_per_mb_at_this_q =
1079      vp9_bits_per_mb(INTER_FRAME, q, err_correction_factor);
1080
1081    if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1082      break;
1083  }
1084
1085  // Clip value to range "best allowed to (worst allowed - 1)"
1086  q = select_cq_level(q);
1087  if (q >= cpi->worst_quality)
1088    q = cpi->worst_quality - 1;
1089  if (q < cpi->best_quality)
1090    q = cpi->best_quality;
1091
1092  return q;
1093}
1094
1095
1096extern void vp9_new_framerate(VP9_COMP *cpi, double framerate);
1097
1098void vp9_init_second_pass(VP9_COMP *cpi) {
1099  FIRSTPASS_STATS this_frame;
1100  FIRSTPASS_STATS *start_pos;
1101
1102  double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
1103  double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
1104                                      * cpi->oxcf.two_pass_vbrmin_section / 100);
1105
1106  if (two_pass_min_rate < lower_bounds_min_rate)
1107    two_pass_min_rate = lower_bounds_min_rate;
1108
1109  zero_stats(&cpi->twopass.total_stats);
1110  zero_stats(&cpi->twopass.total_left_stats);
1111
1112  if (!cpi->twopass.stats_in_end)
1113    return;
1114
1115  cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1116  cpi->twopass.total_left_stats = cpi->twopass.total_stats;
1117
1118  // each frame can have a different duration, as the frame rate in the source
1119  // isn't guaranteed to be constant.   The frame rate prior to the first frame
1120  // encoded in the second pass is a guess.  However the sum duration is not.
1121  // Its calculated based on the actual durations of all frames from the first
1122  // pass.
1123  vp9_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count /
1124                       cpi->twopass.total_stats.duration);
1125
1126  cpi->output_framerate = cpi->oxcf.framerate;
1127  cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration *
1128                                     cpi->oxcf.target_bandwidth / 10000000.0);
1129  cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration *
1130                                      two_pass_min_rate / 10000000.0);
1131
1132  // Calculate a minimum intra value to be used in determining the IIratio
1133  // scores used in the second pass. We have this minimum to make sure
1134  // that clips that are static but "low complexity" in the intra domain
1135  // are still boosted appropriately for KF/GF/ARF
1136  cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1137  cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1138
1139  // This variable monitors how far behind the second ref update is lagging
1140  cpi->twopass.sr_update_lag = 1;
1141
1142  // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1143  {
1144    double sum_iiratio = 0.0;
1145    double IIRatio;
1146
1147    start_pos = cpi->twopass.stats_in;               // Note starting "file" position
1148
1149    while (input_stats(cpi, &this_frame) != EOF) {
1150      IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1151      IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1152      sum_iiratio += IIRatio;
1153    }
1154
1155    cpi->twopass.avg_iiratio = sum_iiratio /
1156        DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
1157
1158    // Reset file position
1159    reset_fpf_position(cpi, start_pos);
1160  }
1161
1162  // Scan the first pass file and calculate a modified total error based upon the bias/power function
1163  // used to allocate bits
1164  {
1165    start_pos = cpi->twopass.stats_in;               // Note starting "file" position
1166
1167    cpi->twopass.modified_error_total = 0.0;
1168    cpi->twopass.modified_error_used = 0.0;
1169
1170    while (input_stats(cpi, &this_frame) != EOF) {
1171      cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame);
1172    }
1173    cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1174
1175    reset_fpf_position(cpi, start_pos);            // Reset file position
1176
1177  }
1178}
1179
1180void vp9_end_second_pass(VP9_COMP *cpi) {
1181}
1182
1183// This function gives and estimate of how badly we believe
1184// the prediction quality is decaying from frame to frame.
1185static double get_prediction_decay_rate(VP9_COMP *cpi,
1186                                        FIRSTPASS_STATS *next_frame) {
1187  double prediction_decay_rate;
1188  double second_ref_decay;
1189  double mb_sr_err_diff;
1190
1191  // Initial basis is the % mbs inter coded
1192  prediction_decay_rate = next_frame->pcnt_inter;
1193
1194  // Look at the observed drop in prediction quality between the last frame
1195  // and the GF buffer (which contains an older frame).
1196  mb_sr_err_diff = (next_frame->sr_coded_error - next_frame->coded_error) /
1197                   cpi->common.MBs;
1198  if (mb_sr_err_diff <= 512.0) {
1199    second_ref_decay = 1.0 - (mb_sr_err_diff / 512.0);
1200    second_ref_decay = pow(second_ref_decay, 0.5);
1201    if (second_ref_decay < 0.85)
1202      second_ref_decay = 0.85;
1203    else if (second_ref_decay > 1.0)
1204      second_ref_decay = 1.0;
1205  } else {
1206    second_ref_decay = 0.85;
1207  }
1208
1209  if (second_ref_decay < prediction_decay_rate)
1210    prediction_decay_rate = second_ref_decay;
1211
1212  return prediction_decay_rate;
1213}
1214
1215// Function to test for a condition where a complex transition is followed
1216// by a static section. For example in slide shows where there is a fade
1217// between slides. This is to help with more optimal kf and gf positioning.
1218static int detect_transition_to_still(
1219  VP9_COMP *cpi,
1220  int frame_interval,
1221  int still_interval,
1222  double loop_decay_rate,
1223  double last_decay_rate) {
1224  int trans_to_still = 0;
1225
1226  // Break clause to detect very still sections after motion
1227  // For example a static image after a fade or other transition
1228  // instead of a clean scene cut.
1229  if (frame_interval > MIN_GF_INTERVAL &&
1230      loop_decay_rate >= 0.999 &&
1231      last_decay_rate < 0.9) {
1232    int j;
1233    FIRSTPASS_STATS *position = cpi->twopass.stats_in;
1234    FIRSTPASS_STATS tmp_next_frame;
1235    double zz_inter;
1236
1237    // Look ahead a few frames to see if static condition
1238    // persists...
1239    for (j = 0; j < still_interval; j++) {
1240      if (EOF == input_stats(cpi, &tmp_next_frame))
1241        break;
1242
1243      zz_inter =
1244        (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion);
1245      if (zz_inter < 0.999)
1246        break;
1247    }
1248    // Reset file position
1249    reset_fpf_position(cpi, position);
1250
1251    // Only if it does do we signal a transition to still
1252    if (j == still_interval)
1253      trans_to_still = 1;
1254  }
1255
1256  return trans_to_still;
1257}
1258
1259// This function detects a flash through the high relative pcnt_second_ref
1260// score in the frame following a flash frame. The offset passed in should
1261// reflect this
1262static int detect_flash(VP9_COMP *cpi, int offset) {
1263  FIRSTPASS_STATS next_frame;
1264
1265  int flash_detected = 0;
1266
1267  // Read the frame data.
1268  // The return is FALSE (no flash detected) if not a valid frame
1269  if (read_frame_stats(cpi, &next_frame, offset) != EOF) {
1270    // What we are looking for here is a situation where there is a
1271    // brief break in prediction (such as a flash) but subsequent frames
1272    // are reasonably well predicted by an earlier (pre flash) frame.
1273    // The recovery after a flash is indicated by a high pcnt_second_ref
1274    // comapred to pcnt_inter.
1275    if (next_frame.pcnt_second_ref > next_frame.pcnt_inter &&
1276        next_frame.pcnt_second_ref >= 0.5)
1277      flash_detected = 1;
1278  }
1279
1280  return flash_detected;
1281}
1282
1283// Update the motion related elements to the GF arf boost calculation
1284static void accumulate_frame_motion_stats(
1285  FIRSTPASS_STATS *this_frame,
1286  double *this_frame_mv_in_out,
1287  double *mv_in_out_accumulator,
1288  double *abs_mv_in_out_accumulator,
1289  double *mv_ratio_accumulator) {
1290  // double this_frame_mv_in_out;
1291  double this_frame_mvr_ratio;
1292  double this_frame_mvc_ratio;
1293  double motion_pct;
1294
1295  // Accumulate motion stats.
1296  motion_pct = this_frame->pcnt_motion;
1297
1298  // Accumulate Motion In/Out of frame stats
1299  *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1300  *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1301  *abs_mv_in_out_accumulator +=
1302    fabs(this_frame->mv_in_out_count * motion_pct);
1303
1304  // Accumulate a measure of how uniform (or conversely how random)
1305  // the motion field is. (A ratio of absmv / mv)
1306  if (motion_pct > 0.05) {
1307    this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1308                           DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1309
1310    this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1311                           DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1312
1313    *mv_ratio_accumulator +=
1314      (this_frame_mvr_ratio < this_frame->mvr_abs)
1315      ? (this_frame_mvr_ratio * motion_pct)
1316      : this_frame->mvr_abs * motion_pct;
1317
1318    *mv_ratio_accumulator +=
1319      (this_frame_mvc_ratio < this_frame->mvc_abs)
1320      ? (this_frame_mvc_ratio * motion_pct)
1321      : this_frame->mvc_abs * motion_pct;
1322
1323  }
1324}
1325
1326// Calculate a baseline boost number for the current frame.
1327static double calc_frame_boost(
1328  VP9_COMP *cpi,
1329  FIRSTPASS_STATS *this_frame,
1330  double this_frame_mv_in_out) {
1331  double frame_boost;
1332
1333  // Underlying boost factor is based on inter intra error ratio
1334  if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1335    frame_boost = (IIFACTOR * this_frame->intra_error /
1336                   DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1337  else
1338    frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1339                   DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1340
1341  // Increase boost for frames where new data coming into frame
1342  // (eg zoom out). Slightly reduce boost if there is a net balance
1343  // of motion out of the frame (zoom in).
1344  // The range for this_frame_mv_in_out is -1.0 to +1.0
1345  if (this_frame_mv_in_out > 0.0)
1346    frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1347  // In extreme case boost is halved
1348  else
1349    frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1350
1351  // Clip to maximum
1352  if (frame_boost > GF_RMAX)
1353    frame_boost = GF_RMAX;
1354
1355  return frame_boost;
1356}
1357
1358static int calc_arf_boost(VP9_COMP *cpi, int offset,
1359                          int f_frames, int b_frames,
1360                          int *f_boost, int *b_boost) {
1361  FIRSTPASS_STATS this_frame;
1362
1363  int i;
1364  double boost_score = 0.0;
1365  double mv_ratio_accumulator = 0.0;
1366  double decay_accumulator = 1.0;
1367  double this_frame_mv_in_out = 0.0;
1368  double mv_in_out_accumulator = 0.0;
1369  double abs_mv_in_out_accumulator = 0.0;
1370  int arf_boost;
1371  int flash_detected = 0;
1372
1373  // Search forward from the proposed arf/next gf position
1374  for (i = 0; i < f_frames; i++) {
1375    if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF)
1376      break;
1377
1378    // Update the motion related elements to the boost calculation
1379    accumulate_frame_motion_stats(&this_frame,
1380                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
1381                                  &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1382
1383    // We want to discount the flash frame itself and the recovery
1384    // frame that follows as both will have poor scores.
1385    flash_detected = detect_flash(cpi, (i + offset)) ||
1386                     detect_flash(cpi, (i + offset + 1));
1387
1388    // Cumulative effect of prediction quality decay
1389    if (!flash_detected) {
1390      decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame);
1391      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1392                          ? MIN_DECAY_FACTOR : decay_accumulator;
1393    }
1394
1395    boost_score += (decay_accumulator *
1396                    calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1397  }
1398
1399  *f_boost = (int)boost_score;
1400
1401  // Reset for backward looking loop
1402  boost_score = 0.0;
1403  mv_ratio_accumulator = 0.0;
1404  decay_accumulator = 1.0;
1405  this_frame_mv_in_out = 0.0;
1406  mv_in_out_accumulator = 0.0;
1407  abs_mv_in_out_accumulator = 0.0;
1408
1409  // Search backward towards last gf position
1410  for (i = -1; i >= -b_frames; i--) {
1411    if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF)
1412      break;
1413
1414    // Update the motion related elements to the boost calculation
1415    accumulate_frame_motion_stats(&this_frame,
1416                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
1417                                  &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1418
1419    // We want to discount the the flash frame itself and the recovery
1420    // frame that follows as both will have poor scores.
1421    flash_detected = detect_flash(cpi, (i + offset)) ||
1422                     detect_flash(cpi, (i + offset + 1));
1423
1424    // Cumulative effect of prediction quality decay
1425    if (!flash_detected) {
1426      decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame);
1427      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1428                              ? MIN_DECAY_FACTOR : decay_accumulator;
1429    }
1430
1431    boost_score += (decay_accumulator *
1432                    calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1433
1434  }
1435  *b_boost = (int)boost_score;
1436
1437  arf_boost = (*f_boost + *b_boost);
1438  if (arf_boost < ((b_frames + f_frames) * 20))
1439    arf_boost = ((b_frames + f_frames) * 20);
1440
1441  return arf_boost;
1442}
1443
1444#if CONFIG_MULTIPLE_ARF
1445// Work out the frame coding order for a GF or an ARF group.
1446// The current implementation codes frames in their natural order for a
1447// GF group, and inserts additional ARFs into an ARF group using a
1448// binary split approach.
1449// NOTE: this function is currently implemented recursively.
1450static void schedule_frames(VP9_COMP *cpi, const int start, const int end,
1451                            const int arf_idx, const int gf_or_arf_group,
1452                            const int level) {
1453  int i, abs_end, half_range;
1454  int *cfo = cpi->frame_coding_order;
1455  int idx = cpi->new_frame_coding_order_period;
1456
1457  // If (end < 0) an ARF should be coded at position (-end).
1458  assert(start >= 0);
1459
1460  // printf("start:%d end:%d\n", start, end);
1461
1462  // GF Group: code frames in logical order.
1463  if (gf_or_arf_group == 0) {
1464    assert(end >= start);
1465    for (i = start; i <= end; ++i) {
1466      cfo[idx] = i;
1467      cpi->arf_buffer_idx[idx] = arf_idx;
1468      cpi->arf_weight[idx] = -1;
1469      ++idx;
1470    }
1471    cpi->new_frame_coding_order_period = idx;
1472    return;
1473  }
1474
1475  // ARF Group: work out the ARF schedule.
1476  // Mark ARF frames as negative.
1477  if (end < 0) {
1478    // printf("start:%d end:%d\n", -end, -end);
1479    // ARF frame is at the end of the range.
1480    cfo[idx] = end;
1481    // What ARF buffer does this ARF use as predictor.
1482    cpi->arf_buffer_idx[idx] = (arf_idx > 2) ? (arf_idx - 1) : 2;
1483    cpi->arf_weight[idx] = level;
1484    ++idx;
1485    abs_end = -end;
1486  } else {
1487    abs_end = end;
1488  }
1489
1490  half_range = (abs_end - start) >> 1;
1491
1492  // ARFs may not be adjacent, they must be separated by at least
1493  // MIN_GF_INTERVAL non-ARF frames.
1494  if ((start + MIN_GF_INTERVAL) >= (abs_end - MIN_GF_INTERVAL)) {
1495    // printf("start:%d end:%d\n", start, abs_end);
1496    // Update the coding order and active ARF.
1497    for (i = start; i <= abs_end; ++i) {
1498      cfo[idx] = i;
1499      cpi->arf_buffer_idx[idx] = arf_idx;
1500      cpi->arf_weight[idx] = -1;
1501      ++idx;
1502    }
1503    cpi->new_frame_coding_order_period = idx;
1504  } else {
1505    // Place a new ARF at the mid-point of the range.
1506    cpi->new_frame_coding_order_period = idx;
1507    schedule_frames(cpi, start, -(start + half_range), arf_idx + 1,
1508                    gf_or_arf_group, level + 1);
1509    schedule_frames(cpi, start + half_range + 1, abs_end, arf_idx,
1510                    gf_or_arf_group, level + 1);
1511  }
1512}
1513
1514#define FIXED_ARF_GROUP_SIZE 16
1515
1516void define_fixed_arf_period(VP9_COMP *cpi) {
1517  int i;
1518  int max_level = INT_MIN;
1519
1520  assert(cpi->multi_arf_enabled);
1521  assert(cpi->oxcf.lag_in_frames >= FIXED_ARF_GROUP_SIZE);
1522
1523  // Save the weight of the last frame in the sequence before next
1524  // sequence pattern overwrites it.
1525  cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
1526  assert(cpi->this_frame_weight >= 0);
1527
1528  // Initialize frame coding order variables.
1529  cpi->new_frame_coding_order_period = 0;
1530  cpi->next_frame_in_order = 0;
1531  cpi->arf_buffered = 0;
1532  vp9_zero(cpi->frame_coding_order);
1533  vp9_zero(cpi->arf_buffer_idx);
1534  vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
1535
1536  if (cpi->twopass.frames_to_key <= (FIXED_ARF_GROUP_SIZE + 8)) {
1537    // Setup a GF group close to the keyframe.
1538    cpi->source_alt_ref_pending = 0;
1539    cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
1540    schedule_frames(cpi, 0, (cpi->baseline_gf_interval - 1), 2, 0, 0);
1541  } else {
1542    // Setup a fixed period ARF group.
1543    cpi->source_alt_ref_pending = 1;
1544    cpi->baseline_gf_interval = FIXED_ARF_GROUP_SIZE;
1545    schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0);
1546  }
1547
1548  // Replace level indicator of -1 with correct level.
1549  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1550    if (cpi->arf_weight[i] > max_level) {
1551      max_level = cpi->arf_weight[i];
1552    }
1553  }
1554  ++max_level;
1555  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1556    if (cpi->arf_weight[i] == -1) {
1557      cpi->arf_weight[i] = max_level;
1558    }
1559  }
1560  cpi->max_arf_level = max_level;
1561#if 0
1562  printf("\nSchedule: ");
1563  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1564    printf("%4d ", cpi->frame_coding_order[i]);
1565  }
1566  printf("\n");
1567  printf("ARFref:   ");
1568  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1569    printf("%4d ", cpi->arf_buffer_idx[i]);
1570  }
1571  printf("\n");
1572  printf("Weight:   ");
1573  for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1574    printf("%4d ", cpi->arf_weight[i]);
1575  }
1576  printf("\n");
1577#endif
1578}
1579#endif
1580
1581// Analyse and define a gf/arf group.
1582static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1583  FIRSTPASS_STATS next_frame;
1584  FIRSTPASS_STATS *start_pos;
1585  int i;
1586  double boost_score = 0.0;
1587  double old_boost_score = 0.0;
1588  double gf_group_err = 0.0;
1589  double gf_first_frame_err = 0.0;
1590  double mod_frame_err = 0.0;
1591
1592  double mv_ratio_accumulator = 0.0;
1593  double decay_accumulator = 1.0;
1594  double zero_motion_accumulator = 1.0;
1595
1596  double loop_decay_rate = 1.00;          // Starting decay rate
1597  double last_loop_decay_rate = 1.00;
1598
1599  double this_frame_mv_in_out = 0.0;
1600  double mv_in_out_accumulator = 0.0;
1601  double abs_mv_in_out_accumulator = 0.0;
1602  double mv_ratio_accumulator_thresh;
1603  int max_bits = frame_max_bits(cpi);     // Max for a single frame
1604
1605  unsigned int allow_alt_ref =
1606    cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1607
1608  int f_boost = 0;
1609  int b_boost = 0;
1610  int flash_detected;
1611  int active_max_gf_interval;
1612
1613  cpi->twopass.gf_group_bits = 0;
1614
1615  vp9_clear_system_state();  // __asm emms;
1616
1617  start_pos = cpi->twopass.stats_in;
1618
1619  vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1620
1621  // Load stats for the current frame.
1622  mod_frame_err = calculate_modified_err(cpi, this_frame);
1623
1624  // Note the error of the frame at the start of the group (this will be
1625  // the GF frame error if we code a normal gf
1626  gf_first_frame_err = mod_frame_err;
1627
1628  // Special treatment if the current frame is a key frame (which is also
1629  // a gf). If it is then its error score (and hence bit allocation) need
1630  // to be subtracted out from the calculation for the GF group
1631  if (cpi->common.frame_type == KEY_FRAME)
1632    gf_group_err -= gf_first_frame_err;
1633
1634  // Motion breakout threshold for loop below depends on image size.
1635  mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0;
1636
1637  // Work out a maximum interval for the GF.
1638  // If the image appears completely static we can extend beyond this.
1639  // The value chosen depends on the active Q range. At low Q we have
1640  // bits to spare and are better with a smaller interval and smaller boost.
1641  // At high Q when there are few bits to spare we are better with a longer
1642  // interval to spread the cost of the GF.
1643  active_max_gf_interval =
1644    12 + ((int)vp9_convert_qindex_to_q(cpi->active_worst_quality) >> 5);
1645
1646  if (active_max_gf_interval > cpi->max_gf_interval)
1647    active_max_gf_interval = cpi->max_gf_interval;
1648
1649  i = 0;
1650  while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1651          ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1652         (i < cpi->twopass.frames_to_key)) {
1653    i++;    // Increment the loop counter
1654
1655    // Accumulate error score of frames in this gf group
1656    mod_frame_err = calculate_modified_err(cpi, this_frame);
1657    gf_group_err += mod_frame_err;
1658
1659    if (EOF == input_stats(cpi, &next_frame))
1660      break;
1661
1662    // Test for the case where there is a brief flash but the prediction
1663    // quality back to an earlier frame is then restored.
1664    flash_detected = detect_flash(cpi, 0);
1665
1666    // Update the motion related elements to the boost calculation
1667    accumulate_frame_motion_stats(&next_frame,
1668                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
1669                                  &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1670
1671    // Cumulative effect of prediction quality decay
1672    if (!flash_detected) {
1673      last_loop_decay_rate = loop_decay_rate;
1674      loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1675      decay_accumulator = decay_accumulator * loop_decay_rate;
1676
1677      // Monitor for static sections.
1678      if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
1679          zero_motion_accumulator) {
1680        zero_motion_accumulator =
1681          (next_frame.pcnt_inter - next_frame.pcnt_motion);
1682      }
1683
1684      // Break clause to detect very still sections after motion
1685      // (for example a static image after a fade or other transition).
1686      if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
1687                                     last_loop_decay_rate)) {
1688        allow_alt_ref = 0;
1689        break;
1690      }
1691    }
1692
1693    // Calculate a boost number for this frame
1694    boost_score +=
1695      (decay_accumulator *
1696       calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out));
1697
1698    // Break out conditions.
1699    if (
1700      // Break at cpi->max_gf_interval unless almost totally static
1701      (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
1702      (
1703        // Don't break out with a very short interval
1704        (i > MIN_GF_INTERVAL) &&
1705        // Don't break out very close to a key frame
1706        ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1707        ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) &&
1708        (!flash_detected) &&
1709        ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
1710         (abs_mv_in_out_accumulator > 3.0) ||
1711         (mv_in_out_accumulator < -2.0) ||
1712         ((boost_score - old_boost_score) < IIFACTOR))
1713      )) {
1714      boost_score = old_boost_score;
1715      break;
1716    }
1717
1718    *this_frame = next_frame;
1719
1720    old_boost_score = boost_score;
1721  }
1722
1723  // Don't allow a gf too near the next kf
1724  if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) {
1725    while (i < cpi->twopass.frames_to_key) {
1726      i++;
1727
1728      if (EOF == input_stats(cpi, this_frame))
1729        break;
1730
1731      if (i < cpi->twopass.frames_to_key) {
1732        mod_frame_err = calculate_modified_err(cpi, this_frame);
1733        gf_group_err += mod_frame_err;
1734      }
1735    }
1736  }
1737
1738  // Set the interval until the next gf or arf.
1739  cpi->baseline_gf_interval = i;
1740
1741#if CONFIG_MULTIPLE_ARF
1742  if (cpi->multi_arf_enabled) {
1743    // Initialize frame coding order variables.
1744    cpi->new_frame_coding_order_period = 0;
1745    cpi->next_frame_in_order = 0;
1746    cpi->arf_buffered = 0;
1747    vp9_zero(cpi->frame_coding_order);
1748    vp9_zero(cpi->arf_buffer_idx);
1749    vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
1750  }
1751#endif
1752
1753  // Should we use the alternate reference frame
1754  if (allow_alt_ref &&
1755      (i < cpi->oxcf.lag_in_frames) &&
1756      (i >= MIN_GF_INTERVAL) &&
1757      // dont use ARF very near next kf
1758      (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1759      ((next_frame.pcnt_inter > 0.75) ||
1760       (next_frame.pcnt_second_ref > 0.5)) &&
1761      ((mv_in_out_accumulator / (double)i > -0.2) ||
1762       (mv_in_out_accumulator > -2.0)) &&
1763      (boost_score > 100)) {
1764    // Alternative boost calculation for alt ref
1765    cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
1766    cpi->source_alt_ref_pending = 1;
1767
1768#if CONFIG_MULTIPLE_ARF
1769    // Set the ARF schedule.
1770    if (cpi->multi_arf_enabled) {
1771      schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0);
1772    }
1773#endif
1774  } else {
1775    cpi->gfu_boost = (int)boost_score;
1776    cpi->source_alt_ref_pending = 0;
1777#if CONFIG_MULTIPLE_ARF
1778    // Set the GF schedule.
1779    if (cpi->multi_arf_enabled) {
1780      schedule_frames(cpi, 0, cpi->baseline_gf_interval - 1, 2, 0, 0);
1781      assert(cpi->new_frame_coding_order_period == cpi->baseline_gf_interval);
1782    }
1783#endif
1784  }
1785
1786#if CONFIG_MULTIPLE_ARF
1787  if (cpi->multi_arf_enabled && (cpi->common.frame_type != KEY_FRAME)) {
1788    int max_level = INT_MIN;
1789    // Replace level indicator of -1 with correct level.
1790    for (i = 0; i < cpi->frame_coding_order_period; ++i) {
1791      if (cpi->arf_weight[i] > max_level) {
1792        max_level = cpi->arf_weight[i];
1793      }
1794    }
1795    ++max_level;
1796    for (i = 0; i < cpi->frame_coding_order_period; ++i) {
1797      if (cpi->arf_weight[i] == -1) {
1798        cpi->arf_weight[i] = max_level;
1799      }
1800    }
1801    cpi->max_arf_level = max_level;
1802  }
1803#if 0
1804  if (cpi->multi_arf_enabled) {
1805    printf("\nSchedule: ");
1806    for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1807      printf("%4d ", cpi->frame_coding_order[i]);
1808    }
1809    printf("\n");
1810    printf("ARFref:   ");
1811    for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1812      printf("%4d ", cpi->arf_buffer_idx[i]);
1813    }
1814    printf("\n");
1815    printf("Weight:   ");
1816    for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1817      printf("%4d ", cpi->arf_weight[i]);
1818    }
1819    printf("\n");
1820  }
1821#endif
1822#endif
1823
1824  // Now decide how many bits should be allocated to the GF group as  a
1825  // proportion of those remaining in the kf group.
1826  // The final key frame group in the clip is treated as a special case
1827  // where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
1828  // This is also important for short clips where there may only be one
1829  // key frame.
1830  if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count -
1831                                          cpi->common.current_video_frame)) {
1832    cpi->twopass.kf_group_bits =
1833      (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
1834  }
1835
1836  // Calculate the bits to be allocated to the group as a whole
1837  if ((cpi->twopass.kf_group_bits > 0) &&
1838      (cpi->twopass.kf_group_error_left > 0)) {
1839    cpi->twopass.gf_group_bits =
1840      (int64_t)(cpi->twopass.kf_group_bits *
1841                (gf_group_err / cpi->twopass.kf_group_error_left));
1842  } else
1843    cpi->twopass.gf_group_bits = 0;
1844
1845  cpi->twopass.gf_group_bits =
1846    (cpi->twopass.gf_group_bits < 0)
1847    ? 0
1848    : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
1849    ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
1850
1851  // Clip cpi->twopass.gf_group_bits based on user supplied data rate
1852  // variability limit (cpi->oxcf.two_pass_vbrmax_section)
1853  if (cpi->twopass.gf_group_bits >
1854      (int64_t)max_bits * cpi->baseline_gf_interval)
1855    cpi->twopass.gf_group_bits = (int64_t)max_bits * cpi->baseline_gf_interval;
1856
1857  // Reset the file position
1858  reset_fpf_position(cpi, start_pos);
1859
1860  // Update the record of error used so far (only done once per gf group)
1861  cpi->twopass.modified_error_used += gf_group_err;
1862
1863  // Assign  bits to the arf or gf.
1864  for (i = 0;
1865      i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME);
1866      ++i) {
1867    int allocation_chunks;
1868    int q = cpi->oxcf.fixed_q < 0 ? cpi->last_q[INTER_FRAME]
1869                                  : cpi->oxcf.fixed_q;
1870    int gf_bits;
1871
1872    int boost = (cpi->gfu_boost * vp9_gfboost_qadjust(q)) / 100;
1873
1874    // Set max and minimum boost and hence minimum allocation
1875    boost = clamp(boost, 125, (cpi->baseline_gf_interval + 1) * 200);
1876
1877    if (cpi->source_alt_ref_pending && i == 0)
1878      allocation_chunks = ((cpi->baseline_gf_interval + 1) * 100) + boost;
1879    else
1880      allocation_chunks = (cpi->baseline_gf_interval * 100) + (boost - 100);
1881
1882    // Prevent overflow
1883    if (boost > 1023) {
1884      int divisor = boost >> 10;
1885      boost /= divisor;
1886      allocation_chunks /= divisor;
1887    }
1888
1889    // Calculate the number of bits to be spent on the gf or arf based on
1890    // the boost number
1891    gf_bits = (int)((double)boost * (cpi->twopass.gf_group_bits /
1892                                       (double)allocation_chunks));
1893
1894    // If the frame that is to be boosted is simpler than the average for
1895    // the gf/arf group then use an alternative calculation
1896    // based on the error score of the frame itself
1897    if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) {
1898      double alt_gf_grp_bits =
1899        (double)cpi->twopass.kf_group_bits  *
1900        (mod_frame_err * (double)cpi->baseline_gf_interval) /
1901        DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left);
1902
1903      int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits /
1904                                           (double)allocation_chunks));
1905
1906      if (gf_bits > alt_gf_bits)
1907        gf_bits = alt_gf_bits;
1908    }
1909    // Else if it is harder than other frames in the group make sure it at
1910    // least receives an allocation in keeping with its relative error
1911    // score, otherwise it may be worse off than an "un-boosted" frame
1912    else {
1913      int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits *
1914                        mod_frame_err /
1915                        DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left));
1916
1917      if (alt_gf_bits > gf_bits)
1918        gf_bits = alt_gf_bits;
1919    }
1920
1921    // Dont allow a negative value for gf_bits
1922    if (gf_bits < 0)
1923      gf_bits = 0;
1924
1925    // Add in minimum for a frame
1926    gf_bits += cpi->min_frame_bandwidth;
1927
1928    if (i == 0) {
1929      cpi->twopass.gf_bits = gf_bits;
1930    }
1931    if (i == 1 || (!cpi->source_alt_ref_pending
1932        && (cpi->common.frame_type != KEY_FRAME))) {
1933      // Per frame bit target for this frame
1934      cpi->per_frame_bandwidth = gf_bits;
1935    }
1936  }
1937
1938  {
1939    // Adjust KF group bits and error remaining
1940    cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
1941    cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
1942
1943    if (cpi->twopass.kf_group_bits < 0)
1944      cpi->twopass.kf_group_bits = 0;
1945
1946    // Note the error score left in the remaining frames of the group.
1947    // For normal GFs we want to remove the error score for the first frame
1948    // of the group (except in Key frame case where this has already
1949    // happened)
1950    if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
1951      cpi->twopass.gf_group_error_left = (int64_t)(gf_group_err
1952                                                   - gf_first_frame_err);
1953    else
1954      cpi->twopass.gf_group_error_left = (int64_t)gf_group_err;
1955
1956    cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits
1957        - cpi->min_frame_bandwidth;
1958
1959    if (cpi->twopass.gf_group_bits < 0)
1960      cpi->twopass.gf_group_bits = 0;
1961
1962    // This condition could fail if there are two kfs very close together
1963    // despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the
1964    // calculation of alt_extra_bits.
1965    if (cpi->baseline_gf_interval >= 3) {
1966      const int boost = cpi->source_alt_ref_pending ? b_boost : cpi->gfu_boost;
1967
1968      if (boost >= 150) {
1969        int alt_extra_bits;
1970        int pct_extra = (boost - 100) / 50;
1971        pct_extra = (pct_extra > 20) ? 20 : pct_extra;
1972
1973        alt_extra_bits = (int)((cpi->twopass.gf_group_bits * pct_extra) / 100);
1974        cpi->twopass.gf_group_bits -= alt_extra_bits;
1975      }
1976    }
1977  }
1978
1979  if (cpi->common.frame_type != KEY_FRAME) {
1980    FIRSTPASS_STATS sectionstats;
1981
1982    zero_stats(&sectionstats);
1983    reset_fpf_position(cpi, start_pos);
1984
1985    for (i = 0; i < cpi->baseline_gf_interval; i++) {
1986      input_stats(cpi, &next_frame);
1987      accumulate_stats(&sectionstats, &next_frame);
1988    }
1989
1990    avg_stats(&sectionstats);
1991
1992    cpi->twopass.section_intra_rating = (int)
1993      (sectionstats.intra_error /
1994      DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
1995
1996    reset_fpf_position(cpi, start_pos);
1997  }
1998}
1999
2000// Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2001static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2002  int target_frame_size;
2003
2004  double modified_err;
2005  double err_fraction;
2006
2007  // Max for a single frame.
2008  int max_bits = frame_max_bits(cpi);
2009
2010  // Calculate modified prediction error used in bit allocation.
2011  modified_err = calculate_modified_err(cpi, this_frame);
2012
2013  if (cpi->twopass.gf_group_error_left > 0)
2014    // What portion of the remaining GF group error is used by this frame.
2015    err_fraction = modified_err / cpi->twopass.gf_group_error_left;
2016  else
2017    err_fraction = 0.0;
2018
2019  // How many of those bits available for allocation should we give it?
2020  target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
2021
2022  // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at
2023  // the top end.
2024  if (target_frame_size < 0)
2025    target_frame_size = 0;
2026  else {
2027    if (target_frame_size > max_bits)
2028      target_frame_size = max_bits;
2029
2030    if (target_frame_size > cpi->twopass.gf_group_bits)
2031      target_frame_size = (int)cpi->twopass.gf_group_bits;
2032  }
2033
2034  // Adjust error and bits remaining.
2035  cpi->twopass.gf_group_error_left -= (int64_t)modified_err;
2036  cpi->twopass.gf_group_bits -= target_frame_size;
2037
2038  if (cpi->twopass.gf_group_bits < 0)
2039    cpi->twopass.gf_group_bits = 0;
2040
2041  // Add in the minimum number of bits that is set aside for every frame.
2042  target_frame_size += cpi->min_frame_bandwidth;
2043
2044  // Per frame bit target for this frame.
2045  cpi->per_frame_bandwidth = target_frame_size;
2046}
2047
2048// Make a damped adjustment to the active max q.
2049static int adjust_active_maxq(int old_maxqi, int new_maxqi) {
2050  int i;
2051  const double old_q = vp9_convert_qindex_to_q(old_maxqi);
2052  const double new_q = vp9_convert_qindex_to_q(new_maxqi);
2053  const double target_q = ((old_q * 7.0) + new_q) / 8.0;
2054
2055  if (target_q > old_q) {
2056    for (i = old_maxqi; i <= new_maxqi; i++)
2057      if (vp9_convert_qindex_to_q(i) >= target_q)
2058        return i;
2059  } else {
2060    for (i = old_maxqi; i >= new_maxqi; i--)
2061      if (vp9_convert_qindex_to_q(i) <= target_q)
2062        return i;
2063  }
2064
2065  return new_maxqi;
2066}
2067
2068void vp9_second_pass(VP9_COMP *cpi) {
2069  int tmp_q;
2070  int frames_left = (int)(cpi->twopass.total_stats.count -
2071                          cpi->common.current_video_frame);
2072
2073  FIRSTPASS_STATS this_frame;
2074  FIRSTPASS_STATS this_frame_copy;
2075
2076  double this_frame_intra_error;
2077  double this_frame_coded_error;
2078
2079  if (!cpi->twopass.stats_in)
2080    return;
2081
2082  vp9_clear_system_state();
2083
2084  // Special case code for first frame.
2085  if (cpi->common.current_video_frame == 0) {
2086    cpi->twopass.est_max_qcorrection_factor = 1.0;
2087
2088    // Set a cq_level in constrained quality mode.
2089    if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2090      int est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats,
2091                               (int)(cpi->twopass.bits_left / frames_left));
2092
2093      cpi->cq_target_quality = cpi->oxcf.cq_level;
2094      if (est_cq > cpi->cq_target_quality)
2095        cpi->cq_target_quality = est_cq;
2096    }
2097
2098    // guess at maxq needed in 2nd pass
2099    cpi->twopass.maxq_max_limit = cpi->worst_quality;
2100    cpi->twopass.maxq_min_limit = cpi->best_quality;
2101
2102    tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
2103                           (int)(cpi->twopass.bits_left / frames_left));
2104
2105    cpi->active_worst_quality = tmp_q;
2106    cpi->ni_av_qi = tmp_q;
2107    cpi->avg_q = vp9_convert_qindex_to_q(tmp_q);
2108
2109#ifndef ONE_SHOT_Q_ESTIMATE
2110    // Limit the maxq value returned subsequently.
2111    // This increases the risk of overspend or underspend if the initial
2112    // estimate for the clip is bad, but helps prevent excessive
2113    // variation in Q, especially near the end of a clip
2114    // where for example a small overspend may cause Q to crash
2115    adjust_maxq_qrange(cpi);
2116#endif
2117  }
2118
2119#ifndef ONE_SHOT_Q_ESTIMATE
2120  // The last few frames of a clip almost always have to few or too many
2121  // bits and for the sake of over exact rate control we dont want to make
2122  // radical adjustments to the allowed quantizer range just to use up a
2123  // few surplus bits or get beneath the target rate.
2124  else if ((cpi->common.current_video_frame <
2125            (((unsigned int)cpi->twopass.total_stats.count * 255) >> 8)) &&
2126           ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2127            (unsigned int)cpi->twopass.total_stats.count)) {
2128    if (frames_left < 1)
2129      frames_left = 1;
2130
2131    tmp_q = estimate_max_q(
2132              cpi,
2133              &cpi->twopass.total_left_stats,
2134              (int)(cpi->twopass.bits_left / frames_left));
2135
2136    // Make a damped adjustment to active max Q
2137    cpi->active_worst_quality =
2138      adjust_active_maxq(cpi->active_worst_quality, tmp_q);
2139  }
2140#endif
2141  vp9_zero(this_frame);
2142  if (EOF == input_stats(cpi, &this_frame))
2143    return;
2144
2145  this_frame_intra_error = this_frame.intra_error;
2146  this_frame_coded_error = this_frame.coded_error;
2147
2148  // keyframe and section processing !
2149  if (cpi->twopass.frames_to_key == 0) {
2150    // Define next KF group and assign bits to it
2151    this_frame_copy = this_frame;
2152    find_next_key_frame(cpi, &this_frame_copy);
2153  }
2154
2155  // Is this a GF / ARF (Note that a KF is always also a GF)
2156  if (cpi->frames_till_gf_update_due == 0) {
2157    // Define next gf group and assign bits to it
2158    this_frame_copy = this_frame;
2159
2160#if CONFIG_MULTIPLE_ARF
2161    if (cpi->multi_arf_enabled) {
2162      define_fixed_arf_period(cpi);
2163    } else {
2164#endif
2165      define_gf_group(cpi, &this_frame_copy);
2166#if CONFIG_MULTIPLE_ARF
2167    }
2168#endif
2169
2170    // If we are going to code an altref frame at the end of the group
2171    // and the current frame is not a key frame....
2172    // If the previous group used an arf this frame has already benefited
2173    // from that arf boost and it should not be given extra bits
2174    // If the previous group was NOT coded using arf we may want to apply
2175    // some boost to this GF as well
2176    if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) {
2177      // Assign a standard frames worth of bits from those allocated
2178      // to the GF group
2179      int bak = cpi->per_frame_bandwidth;
2180      this_frame_copy = this_frame;
2181      assign_std_frame_bits(cpi, &this_frame_copy);
2182      cpi->per_frame_bandwidth = bak;
2183    }
2184  } else {
2185    // Otherwise this is an ordinary frame
2186    // Assign bits from those allocated to the GF group
2187    this_frame_copy =  this_frame;
2188    assign_std_frame_bits(cpi, &this_frame_copy);
2189  }
2190
2191  // Keep a globally available copy of this and the next frame's iiratio.
2192  cpi->twopass.this_iiratio = (int)(this_frame_intra_error /
2193                              DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
2194  {
2195    FIRSTPASS_STATS next_frame;
2196    if (lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2197      cpi->twopass.next_iiratio = (int)(next_frame.intra_error /
2198                                  DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2199    }
2200  }
2201
2202  // Set nominal per second bandwidth for this frame
2203  cpi->target_bandwidth = (int)(cpi->per_frame_bandwidth
2204                                * cpi->output_framerate);
2205  if (cpi->target_bandwidth < 0)
2206    cpi->target_bandwidth = 0;
2207
2208  cpi->twopass.frames_to_key--;
2209
2210  // Update the total stats remaining structure
2211  subtract_stats(&cpi->twopass.total_left_stats, &this_frame);
2212}
2213
2214static int test_candidate_kf(VP9_COMP *cpi,
2215                             FIRSTPASS_STATS *last_frame,
2216                             FIRSTPASS_STATS *this_frame,
2217                             FIRSTPASS_STATS *next_frame) {
2218  int is_viable_kf = 0;
2219
2220  // Does the frame satisfy the primary criteria of a key frame
2221  //      If so, then examine how well it predicts subsequent frames
2222  if ((this_frame->pcnt_second_ref < 0.10) &&
2223      (next_frame->pcnt_second_ref < 0.10) &&
2224      ((this_frame->pcnt_inter < 0.05) ||
2225       (
2226         ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) &&
2227         ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2228         ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2229          (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2230          ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2231         )
2232       )
2233      )
2234     ) {
2235    int i;
2236    FIRSTPASS_STATS *start_pos;
2237
2238    FIRSTPASS_STATS local_next_frame;
2239
2240    double boost_score = 0.0;
2241    double old_boost_score = 0.0;
2242    double decay_accumulator = 1.0;
2243    double next_iiratio;
2244
2245    local_next_frame = *next_frame;
2246
2247    // Note the starting file position so we can reset to it
2248    start_pos = cpi->twopass.stats_in;
2249
2250    // Examine how well the key frame predicts subsequent frames
2251    for (i = 0; i < 16; i++) {
2252      next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
2253
2254      if (next_iiratio > RMAX)
2255        next_iiratio = RMAX;
2256
2257      // Cumulative effect of decay in prediction quality
2258      if (local_next_frame.pcnt_inter > 0.85)
2259        decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2260      else
2261        decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2262
2263      // decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2264
2265      // Keep a running total
2266      boost_score += (decay_accumulator * next_iiratio);
2267
2268      // Test various breakout clauses
2269      if ((local_next_frame.pcnt_inter < 0.05) ||
2270          (next_iiratio < 1.5) ||
2271          (((local_next_frame.pcnt_inter -
2272             local_next_frame.pcnt_neutral) < 0.20) &&
2273           (next_iiratio < 3.0)) ||
2274          ((boost_score - old_boost_score) < 3.0) ||
2275          (local_next_frame.intra_error < 200)
2276         ) {
2277        break;
2278      }
2279
2280      old_boost_score = boost_score;
2281
2282      // Get the next frame details
2283      if (EOF == input_stats(cpi, &local_next_frame))
2284        break;
2285    }
2286
2287    // If there is tolerable prediction for at least the next 3 frames then
2288    // break out else discard this potential key frame and move on
2289    if (boost_score > 30.0 && (i > 3))
2290      is_viable_kf = 1;
2291    else {
2292      // Reset the file position
2293      reset_fpf_position(cpi, start_pos);
2294
2295      is_viable_kf = 0;
2296    }
2297  }
2298
2299  return is_viable_kf;
2300}
2301static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2302  int i, j;
2303  FIRSTPASS_STATS last_frame;
2304  FIRSTPASS_STATS first_frame;
2305  FIRSTPASS_STATS next_frame;
2306  FIRSTPASS_STATS *start_position;
2307
2308  double decay_accumulator = 1.0;
2309  double zero_motion_accumulator = 1.0;
2310  double boost_score = 0;
2311  double loop_decay_rate;
2312
2313  double kf_mod_err = 0.0;
2314  double kf_group_err = 0.0;
2315  double kf_group_intra_err = 0.0;
2316  double kf_group_coded_err = 0.0;
2317  double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
2318
2319  vp9_zero(next_frame);
2320
2321  vp9_clear_system_state();  // __asm emms;
2322  start_position = cpi->twopass.stats_in;
2323
2324  cpi->common.frame_type = KEY_FRAME;
2325
2326  // is this a forced key frame by interval
2327  cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2328
2329  // Clear the alt ref active flag as this can never be active on a key frame
2330  cpi->source_alt_ref_active = 0;
2331
2332  // Kf is always a gf so clear frames till next gf counter
2333  cpi->frames_till_gf_update_due = 0;
2334
2335  cpi->twopass.frames_to_key = 1;
2336
2337  // Take a copy of the initial frame details
2338  first_frame = *this_frame;
2339
2340  cpi->twopass.kf_group_bits = 0;        // Total bits available to kf group
2341  cpi->twopass.kf_group_error_left = 0;  // Group modified error score.
2342
2343  kf_mod_err = calculate_modified_err(cpi, this_frame);
2344
2345  // find the next keyframe
2346  i = 0;
2347  while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) {
2348    // Accumulate kf group error
2349    kf_group_err += calculate_modified_err(cpi, this_frame);
2350
2351    // These figures keep intra and coded error counts for all frames including key frames in the group.
2352    // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2353    kf_group_intra_err += this_frame->intra_error;
2354    kf_group_coded_err += this_frame->coded_error;
2355
2356    // load a the next frame's stats
2357    last_frame = *this_frame;
2358    input_stats(cpi, this_frame);
2359
2360    // Provided that we are not at the end of the file...
2361    if (cpi->oxcf.auto_key
2362        && lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2363      // Normal scene cut check
2364      if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
2365        break;
2366
2367
2368      // How fast is prediction quality decaying
2369      loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2370
2371      // We want to know something about the recent past... rather than
2372      // as used elsewhere where we are concened with decay in prediction
2373      // quality since the last GF or KF.
2374      recent_loop_decay[i % 8] = loop_decay_rate;
2375      decay_accumulator = 1.0;
2376      for (j = 0; j < 8; j++)
2377        decay_accumulator *= recent_loop_decay[j];
2378
2379      // Special check for transition or high motion followed by a
2380      // to a static scene.
2381      if (detect_transition_to_still(cpi, i, cpi->key_frame_frequency - i,
2382                                     loop_decay_rate, decay_accumulator))
2383        break;
2384
2385      // Step on to the next frame
2386      cpi->twopass.frames_to_key++;
2387
2388      // If we don't have a real key frame within the next two
2389      // forcekeyframeevery intervals then break out of the loop.
2390      if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency)
2391        break;
2392    } else
2393      cpi->twopass.frames_to_key++;
2394
2395    i++;
2396  }
2397
2398  // If there is a max kf interval set by the user we must obey it.
2399  // We already breakout of the loop above at 2x max.
2400  // This code centers the extra kf if the actual natural
2401  // interval is between 1x and 2x
2402  if (cpi->oxcf.auto_key
2403      && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) {
2404    FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2405    FIRSTPASS_STATS tmp_frame;
2406
2407    cpi->twopass.frames_to_key /= 2;
2408
2409    // Copy first frame details
2410    tmp_frame = first_frame;
2411
2412    // Reset to the start of the group
2413    reset_fpf_position(cpi, start_position);
2414
2415    kf_group_err = 0;
2416    kf_group_intra_err = 0;
2417    kf_group_coded_err = 0;
2418
2419    // Rescan to get the correct error data for the forced kf group
2420    for (i = 0; i < cpi->twopass.frames_to_key; i++) {
2421      // Accumulate kf group errors
2422      kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2423      kf_group_intra_err += tmp_frame.intra_error;
2424      kf_group_coded_err += tmp_frame.coded_error;
2425
2426      // Load a the next frame's stats
2427      input_stats(cpi, &tmp_frame);
2428    }
2429
2430    // Reset to the start of the group
2431    reset_fpf_position(cpi, current_pos);
2432
2433    cpi->next_key_frame_forced = 1;
2434  } else
2435    cpi->next_key_frame_forced = 0;
2436
2437  // Special case for the last frame of the file
2438  if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
2439    // Accumulate kf group error
2440    kf_group_err += calculate_modified_err(cpi, this_frame);
2441
2442    // These figures keep intra and coded error counts for all frames including key frames in the group.
2443    // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2444    kf_group_intra_err += this_frame->intra_error;
2445    kf_group_coded_err += this_frame->coded_error;
2446  }
2447
2448  // Calculate the number of bits that should be assigned to the kf group.
2449  if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0)) {
2450    // Max for a single normal frame (not key frame)
2451    int max_bits = frame_max_bits(cpi);
2452
2453    // Maximum bits for the kf group
2454    int64_t max_grp_bits;
2455
2456    // Default allocation based on bits left and relative
2457    // complexity of the section
2458    cpi->twopass.kf_group_bits = (int64_t)(cpi->twopass.bits_left *
2459                                           (kf_group_err /
2460                                            cpi->twopass.modified_error_left));
2461
2462    // Clip based on maximum per frame rate defined by the user.
2463    max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2464    if (cpi->twopass.kf_group_bits > max_grp_bits)
2465      cpi->twopass.kf_group_bits = max_grp_bits;
2466  } else
2467    cpi->twopass.kf_group_bits = 0;
2468
2469  // Reset the first pass file position
2470  reset_fpf_position(cpi, start_position);
2471
2472  // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2473  decay_accumulator = 1.0;
2474  boost_score = 0.0;
2475  loop_decay_rate = 1.00;       // Starting decay rate
2476
2477  // Scan through the kf group collating various stats.
2478  for (i = 0; i < cpi->twopass.frames_to_key; i++) {
2479    double r;
2480
2481    if (EOF == input_stats(cpi, &next_frame))
2482      break;
2483
2484    // Monitor for static sections.
2485    if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
2486        zero_motion_accumulator) {
2487      zero_motion_accumulator =
2488        (next_frame.pcnt_inter - next_frame.pcnt_motion);
2489    }
2490
2491    // For the first few frames collect data to decide kf boost.
2492    if (i <= (cpi->max_gf_interval * 2)) {
2493      if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
2494        r = (IIKFACTOR2 * next_frame.intra_error /
2495             DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2496      else
2497        r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2498             DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2499
2500      if (r > RMAX)
2501        r = RMAX;
2502
2503      // How fast is prediction quality decaying
2504      if (!detect_flash(cpi, 0)) {
2505        loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2506        decay_accumulator = decay_accumulator * loop_decay_rate;
2507        decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
2508                              ? MIN_DECAY_FACTOR : decay_accumulator;
2509      }
2510
2511      boost_score += (decay_accumulator * r);
2512    }
2513  }
2514
2515  {
2516    FIRSTPASS_STATS sectionstats;
2517
2518    zero_stats(&sectionstats);
2519    reset_fpf_position(cpi, start_position);
2520
2521    for (i = 0; i < cpi->twopass.frames_to_key; i++) {
2522      input_stats(cpi, &next_frame);
2523      accumulate_stats(&sectionstats, &next_frame);
2524    }
2525
2526    avg_stats(&sectionstats);
2527
2528    cpi->twopass.section_intra_rating = (int)
2529      (sectionstats.intra_error
2530      / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2531  }
2532
2533  // Reset the first pass file position
2534  reset_fpf_position(cpi, start_position);
2535
2536  // Work out how many bits to allocate for the key frame itself
2537  if (1) {
2538    int kf_boost = (int)boost_score;
2539    int allocation_chunks;
2540    int alt_kf_bits;
2541
2542    if (kf_boost < (cpi->twopass.frames_to_key * 3))
2543      kf_boost = (cpi->twopass.frames_to_key * 3);
2544
2545    if (kf_boost < 300) // Min KF boost
2546      kf_boost = 300;
2547
2548    // Make a note of baseline boost and the zero motion
2549    // accumulator value for use elsewhere.
2550    cpi->kf_boost = kf_boost;
2551    cpi->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2552
2553    // We do three calculations for kf size.
2554    // The first is based on the error score for the whole kf group.
2555    // The second (optionaly) on the key frames own error if this is
2556    // smaller than the average for the group.
2557    // The final one insures that the frame receives at least the
2558    // allocation it would have received based on its own error score vs
2559    // the error score remaining
2560    // Special case if the sequence appears almost totaly static
2561    // In this case we want to spend almost all of the bits on the
2562    // key frame.
2563    // cpi->twopass.frames_to_key-1 because key frame itself is taken
2564    // care of by kf_boost.
2565    if (zero_motion_accumulator >= 0.99) {
2566      allocation_chunks =
2567        ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
2568    } else {
2569      allocation_chunks =
2570        ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
2571    }
2572
2573    // Prevent overflow
2574    if (kf_boost > 1028) {
2575      int divisor = kf_boost >> 10;
2576      kf_boost /= divisor;
2577      allocation_chunks /= divisor;
2578    }
2579
2580    cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
2581
2582    // Calculate the number of bits to be spent on the key frame
2583    cpi->twopass.kf_bits  = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
2584
2585    // If the key frame is actually easier than the average for the
2586    // kf group (which does sometimes happen... eg a blank intro frame)
2587    // Then use an alternate calculation based on the kf error score
2588    // which should give a smaller key frame.
2589    if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) {
2590      double  alt_kf_grp_bits =
2591        ((double)cpi->twopass.bits_left *
2592         (kf_mod_err * (double)cpi->twopass.frames_to_key) /
2593         DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
2594
2595      alt_kf_bits = (int)((double)kf_boost *
2596                          (alt_kf_grp_bits / (double)allocation_chunks));
2597
2598      if (cpi->twopass.kf_bits > alt_kf_bits) {
2599        cpi->twopass.kf_bits = alt_kf_bits;
2600      }
2601    }
2602    // Else if it is much harder than other frames in the group make sure
2603    // it at least receives an allocation in keeping with its relative
2604    // error score
2605    else {
2606      alt_kf_bits =
2607        (int)((double)cpi->twopass.bits_left *
2608              (kf_mod_err /
2609               DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
2610
2611      if (alt_kf_bits > cpi->twopass.kf_bits) {
2612        cpi->twopass.kf_bits = alt_kf_bits;
2613      }
2614    }
2615
2616    cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
2617    // Add in the minimum frame allowance
2618    cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
2619
2620    // Peer frame bit target for this frame
2621    cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
2622    // Convert to a per second bitrate
2623    cpi->target_bandwidth = (int)(cpi->twopass.kf_bits *
2624                                  cpi->output_framerate);
2625  }
2626
2627  // Note the total error score of the kf group minus the key frame itself
2628  cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2629
2630  // Adjust the count of total modified error left.
2631  // The count of bits left is adjusted elsewhere based on real coded frame sizes
2632  cpi->twopass.modified_error_left -= kf_group_err;
2633}
2634