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