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 <limits.h>
12#include <math.h>
13#include <stdio.h>
14
15#include "./vpx_scale_rtcd.h"
16
17#include "vpx_mem/vpx_mem.h"
18#include "vpx_scale/vpx_scale.h"
19#include "vpx_scale/yv12config.h"
20
21#include "vp9/common/vp9_entropymv.h"
22#include "vp9/common/vp9_quant_common.h"
23#include "vp9/common/vp9_reconinter.h"  // vp9_setup_dst_planes()
24#include "vp9/common/vp9_systemdependent.h"
25#include "vp9/encoder/vp9_aq_variance.h"
26#include "vp9/encoder/vp9_block.h"
27#include "vp9/encoder/vp9_encodeframe.h"
28#include "vp9/encoder/vp9_encodemb.h"
29#include "vp9/encoder/vp9_encodemv.h"
30#include "vp9/encoder/vp9_encoder.h"
31#include "vp9/encoder/vp9_extend.h"
32#include "vp9/encoder/vp9_firstpass.h"
33#include "vp9/encoder/vp9_mcomp.h"
34#include "vp9/encoder/vp9_quantize.h"
35#include "vp9/encoder/vp9_rd.h"
36#include "vp9/encoder/vp9_variance.h"
37
38#define OUTPUT_FPF          0
39#define ARF_STATS_OUTPUT    0
40
41#define BOOST_FACTOR        12.5
42#define ERR_DIVISOR         100.0
43#define FACTOR_PT_LOW       0.5
44#define FACTOR_PT_HIGH      0.9
45#define FIRST_PASS_Q        10.0
46#define GF_MAX_BOOST        96.0
47#define INTRA_MODE_PENALTY  1024
48#define KF_MAX_BOOST        128.0
49#define MIN_DECAY_FACTOR    0.01
50#define MIN_GF_INTERVAL     4
51#define MIN_KF_BOOST        300
52#define NEW_MV_MODE_PENALTY 32
53#define SVC_FACTOR_PT_LOW   0.45
54
55#define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
56
57#if ARF_STATS_OUTPUT
58unsigned int arf_count = 0;
59#endif
60
61static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
62  YV12_BUFFER_CONFIG temp = *a;
63  *a = *b;
64  *b = temp;
65}
66
67static int gfboost_qadjust(int qindex, vpx_bit_depth_t bit_depth) {
68  const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
69  return (int)((0.00000828 * q * q * q) +
70               (-0.0055 * q * q) +
71               (1.32 * q) + 79.3);
72}
73
74// Resets the first pass file to the given position using a relative seek from
75// the current position.
76static void reset_fpf_position(TWO_PASS *p,
77                               const FIRSTPASS_STATS *position) {
78  p->stats_in = position;
79}
80
81// Read frame stats at an offset from the current position.
82static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) {
83  if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) ||
84      (offset < 0 && p->stats_in + offset < p->stats_in_start)) {
85    return NULL;
86  }
87
88  return &p->stats_in[offset];
89}
90
91static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) {
92  if (p->stats_in >= p->stats_in_end)
93    return EOF;
94
95  *fps = *p->stats_in;
96  ++p->stats_in;
97  return 1;
98}
99
100static void output_stats(FIRSTPASS_STATS *stats,
101                         struct vpx_codec_pkt_list *pktlist) {
102  struct vpx_codec_cx_pkt pkt;
103  pkt.kind = VPX_CODEC_STATS_PKT;
104  pkt.data.twopass_stats.buf = stats;
105  pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
106  vpx_codec_pkt_list_add(pktlist, &pkt);
107
108// TEMP debug code
109#if OUTPUT_FPF
110  {
111    FILE *fpfile;
112    fpfile = fopen("firstpass.stt", "a");
113
114    fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
115            "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
116            "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
117            stats->frame,
118            stats->intra_error,
119            stats->coded_error,
120            stats->sr_coded_error,
121            stats->pcnt_inter,
122            stats->pcnt_motion,
123            stats->pcnt_second_ref,
124            stats->pcnt_neutral,
125            stats->MVr,
126            stats->mvr_abs,
127            stats->MVc,
128            stats->mvc_abs,
129            stats->MVrv,
130            stats->MVcv,
131            stats->mv_in_out_count,
132            stats->new_mv_count,
133            stats->count,
134            stats->duration);
135    fclose(fpfile);
136  }
137#endif
138}
139
140#if CONFIG_FP_MB_STATS
141static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm,
142                         struct vpx_codec_pkt_list *pktlist) {
143  struct vpx_codec_cx_pkt pkt;
144  pkt.kind = VPX_CODEC_FPMB_STATS_PKT;
145  pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats;
146  pkt.data.firstpass_mb_stats.sz = cm->MBs * sizeof(uint8_t);
147  vpx_codec_pkt_list_add(pktlist, &pkt);
148}
149#endif
150
151static void zero_stats(FIRSTPASS_STATS *section) {
152  section->frame      = 0.0;
153  section->intra_error = 0.0;
154  section->coded_error = 0.0;
155  section->sr_coded_error = 0.0;
156  section->pcnt_inter  = 0.0;
157  section->pcnt_motion  = 0.0;
158  section->pcnt_second_ref = 0.0;
159  section->pcnt_neutral = 0.0;
160  section->MVr        = 0.0;
161  section->mvr_abs     = 0.0;
162  section->MVc        = 0.0;
163  section->mvc_abs     = 0.0;
164  section->MVrv       = 0.0;
165  section->MVcv       = 0.0;
166  section->mv_in_out_count  = 0.0;
167  section->new_mv_count = 0.0;
168  section->count      = 0.0;
169  section->duration   = 1.0;
170  section->spatial_layer_id = 0;
171}
172
173static void accumulate_stats(FIRSTPASS_STATS *section,
174                             const FIRSTPASS_STATS *frame) {
175  section->frame += frame->frame;
176  section->spatial_layer_id = frame->spatial_layer_id;
177  section->intra_error += frame->intra_error;
178  section->coded_error += frame->coded_error;
179  section->sr_coded_error += frame->sr_coded_error;
180  section->pcnt_inter  += frame->pcnt_inter;
181  section->pcnt_motion += frame->pcnt_motion;
182  section->pcnt_second_ref += frame->pcnt_second_ref;
183  section->pcnt_neutral += frame->pcnt_neutral;
184  section->MVr        += frame->MVr;
185  section->mvr_abs     += frame->mvr_abs;
186  section->MVc        += frame->MVc;
187  section->mvc_abs     += frame->mvc_abs;
188  section->MVrv       += frame->MVrv;
189  section->MVcv       += frame->MVcv;
190  section->mv_in_out_count  += frame->mv_in_out_count;
191  section->new_mv_count += frame->new_mv_count;
192  section->count      += frame->count;
193  section->duration   += frame->duration;
194}
195
196static void subtract_stats(FIRSTPASS_STATS *section,
197                           const FIRSTPASS_STATS *frame) {
198  section->frame -= frame->frame;
199  section->intra_error -= frame->intra_error;
200  section->coded_error -= frame->coded_error;
201  section->sr_coded_error -= frame->sr_coded_error;
202  section->pcnt_inter  -= frame->pcnt_inter;
203  section->pcnt_motion -= frame->pcnt_motion;
204  section->pcnt_second_ref -= frame->pcnt_second_ref;
205  section->pcnt_neutral -= frame->pcnt_neutral;
206  section->MVr        -= frame->MVr;
207  section->mvr_abs     -= frame->mvr_abs;
208  section->MVc        -= frame->MVc;
209  section->mvc_abs     -= frame->mvc_abs;
210  section->MVrv       -= frame->MVrv;
211  section->MVcv       -= frame->MVcv;
212  section->mv_in_out_count  -= frame->mv_in_out_count;
213  section->new_mv_count -= frame->new_mv_count;
214  section->count      -= frame->count;
215  section->duration   -= frame->duration;
216}
217
218
219// Calculate a modified Error used in distributing bits between easier and
220// harder frames.
221static double calculate_modified_err(const TWO_PASS *twopass,
222                                     const VP9EncoderConfig *oxcf,
223                                     const FIRSTPASS_STATS *this_frame) {
224  const FIRSTPASS_STATS *const stats = &twopass->total_stats;
225  const double av_err = stats->coded_error / stats->count;
226  const double modified_error = av_err *
227      pow(this_frame->coded_error / DOUBLE_DIVIDE_CHECK(av_err),
228          oxcf->two_pass_vbrbias / 100.0);
229  return fclamp(modified_error,
230                twopass->modified_error_min, twopass->modified_error_max);
231}
232
233// This function returns the maximum target rate per frame.
234static int frame_max_bits(const RATE_CONTROL *rc,
235                          const VP9EncoderConfig *oxcf) {
236  int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
237                          (int64_t)oxcf->two_pass_vbrmax_section) / 100;
238  if (max_bits < 0)
239    max_bits = 0;
240  else if (max_bits > rc->max_frame_bandwidth)
241    max_bits = rc->max_frame_bandwidth;
242
243  return (int)max_bits;
244}
245
246void vp9_init_first_pass(VP9_COMP *cpi) {
247  zero_stats(&cpi->twopass.total_stats);
248}
249
250void vp9_end_first_pass(VP9_COMP *cpi) {
251  if (is_two_pass_svc(cpi)) {
252    int i;
253    for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
254      output_stats(&cpi->svc.layer_context[i].twopass.total_stats,
255                   cpi->output_pkt_list);
256    }
257  } else {
258    output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
259  }
260}
261
262static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
263  switch (bsize) {
264    case BLOCK_8X8:
265      return vp9_mse8x8;
266    case BLOCK_16X8:
267      return vp9_mse16x8;
268    case BLOCK_8X16:
269      return vp9_mse8x16;
270    default:
271      return vp9_mse16x16;
272  }
273}
274
275static unsigned int get_prediction_error(BLOCK_SIZE bsize,
276                                         const struct buf_2d *src,
277                                         const struct buf_2d *ref) {
278  unsigned int sse;
279  const vp9_variance_fn_t fn = get_block_variance_fn(bsize);
280  fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
281  return sse;
282}
283
284// Refine the motion search range according to the frame dimension
285// for first pass test.
286static int get_search_range(const VP9_COMMON *cm) {
287  int sr = 0;
288  const int dim = MIN(cm->width, cm->height);
289
290  while ((dim << sr) < MAX_FULL_PEL_VAL)
291    ++sr;
292  return sr;
293}
294
295static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
296                                     const MV *ref_mv, MV *best_mv,
297                                     int *best_motion_err) {
298  MACROBLOCKD *const xd = &x->e_mbd;
299  MV tmp_mv = {0, 0};
300  MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3};
301  int num00, tmp_err, n;
302  const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
303  vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
304  const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
305
306  int step_param = 3;
307  int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
308  const int sr = get_search_range(&cpi->common);
309  step_param += sr;
310  further_steps -= sr;
311
312  // Override the default variance function to use MSE.
313  v_fn_ptr.vf = get_block_variance_fn(bsize);
314
315  // Center the initial step/diamond search on best mv.
316  tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
317                                    step_param,
318                                    x->sadperbit16, &num00, &v_fn_ptr, ref_mv);
319  if (tmp_err < INT_MAX)
320    tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
321  if (tmp_err < INT_MAX - new_mv_mode_penalty)
322    tmp_err += new_mv_mode_penalty;
323
324  if (tmp_err < *best_motion_err) {
325    *best_motion_err = tmp_err;
326    *best_mv = tmp_mv;
327  }
328
329  // Carry out further step/diamond searches as necessary.
330  n = num00;
331  num00 = 0;
332
333  while (n < further_steps) {
334    ++n;
335
336    if (num00) {
337      --num00;
338    } else {
339      tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
340                                        step_param + n, x->sadperbit16,
341                                        &num00, &v_fn_ptr, ref_mv);
342      if (tmp_err < INT_MAX)
343        tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
344      if (tmp_err < INT_MAX - new_mv_mode_penalty)
345        tmp_err += new_mv_mode_penalty;
346
347      if (tmp_err < *best_motion_err) {
348        *best_motion_err = tmp_err;
349        *best_mv = tmp_mv;
350      }
351    }
352  }
353}
354
355static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
356  if (2 * mb_col + 1 < cm->mi_cols) {
357    return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16
358                                        : BLOCK_16X8;
359  } else {
360    return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16
361                                        : BLOCK_8X8;
362  }
363}
364
365static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
366  int i;
367
368  for (i = 0; i < QINDEX_RANGE; ++i)
369    if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q)
370      break;
371
372  if (i == QINDEX_RANGE)
373    i--;
374
375  return i;
376}
377
378static void set_first_pass_params(VP9_COMP *cpi) {
379  VP9_COMMON *const cm = &cpi->common;
380  if (!cpi->refresh_alt_ref_frame &&
381      (cm->current_video_frame == 0 ||
382       (cpi->frame_flags & FRAMEFLAGS_KEY))) {
383    cm->frame_type = KEY_FRAME;
384  } else {
385    cm->frame_type = INTER_FRAME;
386  }
387  // Do not use periodic key frames.
388  cpi->rc.frames_to_key = INT_MAX;
389}
390
391void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
392  int mb_row, mb_col;
393  MACROBLOCK *const x = &cpi->mb;
394  VP9_COMMON *const cm = &cpi->common;
395  MACROBLOCKD *const xd = &x->e_mbd;
396  TileInfo tile;
397  struct macroblock_plane *const p = x->plane;
398  struct macroblockd_plane *const pd = xd->plane;
399  const PICK_MODE_CONTEXT *ctx = &cpi->pc_root->none;
400  int i;
401
402  int recon_yoffset, recon_uvoffset;
403  YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
404  YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
405  YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
406  int recon_y_stride = lst_yv12->y_stride;
407  int recon_uv_stride = lst_yv12->uv_stride;
408  int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height);
409  int64_t intra_error = 0;
410  int64_t coded_error = 0;
411  int64_t sr_coded_error = 0;
412
413  int sum_mvr = 0, sum_mvc = 0;
414  int sum_mvr_abs = 0, sum_mvc_abs = 0;
415  int64_t sum_mvrs = 0, sum_mvcs = 0;
416  int mvcount = 0;
417  int intercount = 0;
418  int second_ref_count = 0;
419  const int intrapenalty = INTRA_MODE_PENALTY;
420  int neutral_count = 0;
421  int new_mv_count = 0;
422  int sum_in_vectors = 0;
423  MV lastmv = {0, 0};
424  TWO_PASS *twopass = &cpi->twopass;
425  const MV zero_mv = {0, 0};
426  const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
427  LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
428        &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL;
429
430#if CONFIG_FP_MB_STATS
431  if (cpi->use_fp_mb_stats) {
432    vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->MBs);
433  }
434#endif
435
436  vp9_clear_system_state();
437
438  set_first_pass_params(cpi);
439  vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
440
441  if (lc != NULL) {
442    twopass = &lc->twopass;
443
444    cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
445    cpi->ref_frame_flags = VP9_LAST_FLAG;
446
447    if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id <
448        REF_FRAMES) {
449      cpi->gld_fb_idx =
450          cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id;
451      cpi->ref_frame_flags |= VP9_GOLD_FLAG;
452      cpi->refresh_golden_frame = (lc->current_video_frame_in_layer == 0);
453    } else {
454      cpi->refresh_golden_frame = 0;
455    }
456
457    if (lc->current_video_frame_in_layer == 0)
458      cpi->ref_frame_flags = 0;
459
460    vp9_scale_references(cpi);
461
462    // Use either last frame or alt frame for motion search.
463    if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
464      first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
465      if (first_ref_buf == NULL)
466        first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME);
467    }
468
469    if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
470      const int ref_idx =
471          cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)];
472      const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1];
473
474      gld_yv12 = (scaled_idx != ref_idx) ? &cm->frame_bufs[scaled_idx].buf :
475                 get_ref_frame_buffer(cpi, GOLDEN_FRAME);
476    } else {
477      gld_yv12 = NULL;
478    }
479
480    recon_y_stride = new_yv12->y_stride;
481    recon_uv_stride = new_yv12->uv_stride;
482    uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
483
484    set_ref_ptrs(cm, xd,
485                 (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME: NONE,
486                 (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE);
487
488    cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
489                                        &cpi->scaled_source);
490  }
491
492  vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
493
494  vp9_setup_src_planes(x, cpi->Source, 0, 0);
495  vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
496  vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
497
498  xd->mi = cm->mi;
499  xd->mi[0].src_mi = &xd->mi[0];
500
501  vp9_frame_init_quantizer(cpi);
502
503  for (i = 0; i < MAX_MB_PLANE; ++i) {
504    p[i].coeff = ctx->coeff_pbuf[i][1];
505    p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
506    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
507    p[i].eobs = ctx->eobs_pbuf[i][1];
508  }
509  x->skip_recode = 0;
510
511  vp9_init_mv_probs(cm);
512  vp9_initialize_rd_consts(cpi);
513
514  // Tiling is ignored in the first pass.
515  vp9_tile_init(&tile, cm, 0, 0);
516
517  for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
518    MV best_ref_mv = {0, 0};
519
520    // Reset above block coeffs.
521    xd->up_available = (mb_row != 0);
522    recon_yoffset = (mb_row * recon_y_stride * 16);
523    recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height);
524
525    // Set up limit values for motion vectors to prevent them extending
526    // outside the UMV borders.
527    x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
528    x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
529                    + BORDER_MV_PIXELS_B16;
530
531    for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
532      int this_error;
533      const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
534      double error_weight = 1.0;
535      const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
536#if CONFIG_FP_MB_STATS
537      const int mb_index = mb_row * cm->mb_cols + mb_col;
538#endif
539
540      vp9_clear_system_state();
541
542      xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
543      xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
544      xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
545      xd->left_available = (mb_col != 0);
546      xd->mi[0].src_mi->mbmi.sb_type = bsize;
547      xd->mi[0].src_mi->mbmi.ref_frame[0] = INTRA_FRAME;
548      set_mi_row_col(xd, &tile,
549                     mb_row << 1, num_8x8_blocks_high_lookup[bsize],
550                     mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
551                     cm->mi_rows, cm->mi_cols);
552
553      if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
554        const int energy = vp9_block_energy(cpi, x, bsize);
555        error_weight = vp9_vaq_inv_q_ratio(energy);
556      }
557
558      // Do intra 16x16 prediction.
559      x->skip_encode = 0;
560      xd->mi[0].src_mi->mbmi.mode = DC_PRED;
561      xd->mi[0].src_mi->mbmi.tx_size = use_dc_pred ?
562         (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
563      vp9_encode_intra_block_plane(x, bsize, 0);
564      this_error = vp9_get_mb_ss(x->plane[0].src_diff);
565
566      if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
567        vp9_clear_system_state();
568        this_error = (int)(this_error * error_weight);
569      }
570
571      // Intrapenalty below deals with situations where the intra and inter
572      // error scores are very low (e.g. a plain black frame).
573      // We do not have special cases in first pass for 0,0 and nearest etc so
574      // all inter modes carry an overhead cost estimate for the mv.
575      // When the error score is very low this causes us to pick all or lots of
576      // INTRA modes and throw lots of key frames.
577      // This penalty adds a cost matching that of a 0,0 mv to the intra case.
578      this_error += intrapenalty;
579
580      // Accumulate the intra error.
581      intra_error += (int64_t)this_error;
582
583#if CONFIG_FP_MB_STATS
584      if (cpi->use_fp_mb_stats) {
585        // initialization
586        cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
587      }
588#endif
589
590      // Set up limit values for motion vectors to prevent them extending
591      // outside the UMV borders.
592      x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
593      x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
594
595      // Other than for the first frame do a motion search.
596      if ((lc == NULL && cm->current_video_frame > 0) ||
597          (lc != NULL && lc->current_video_frame_in_layer > 0)) {
598        int tmp_err, motion_error, raw_motion_error;
599        // Assume 0,0 motion with no mv overhead.
600        MV mv = {0, 0} , tmp_mv = {0, 0};
601        struct buf_2d unscaled_last_source_buf_2d;
602
603        xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
604        motion_error = get_prediction_error(bsize, &x->plane[0].src,
605                                            &xd->plane[0].pre[0]);
606
607        // Compute the motion error of the 0,0 motion using the last source
608        // frame as the reference. Skip the further motion search on
609        // reconstructed frame if this error is small.
610        unscaled_last_source_buf_2d.buf =
611            cpi->unscaled_last_source->y_buffer + recon_yoffset;
612        unscaled_last_source_buf_2d.stride =
613            cpi->unscaled_last_source->y_stride;
614        raw_motion_error = get_prediction_error(bsize, &x->plane[0].src,
615                                                &unscaled_last_source_buf_2d);
616
617        // TODO(pengchong): Replace the hard-coded threshold
618        if (raw_motion_error > 25 || lc != NULL) {
619          // Test last reference frame using the previous best mv as the
620          // starting point (best reference) for the search.
621          first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error);
622          if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
623            vp9_clear_system_state();
624            motion_error = (int)(motion_error * error_weight);
625          }
626
627          // If the current best reference mv is not centered on 0,0 then do a
628          // 0,0 based search as well.
629          if (!is_zero_mv(&best_ref_mv)) {
630            tmp_err = INT_MAX;
631            first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
632            if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
633              vp9_clear_system_state();
634              tmp_err = (int)(tmp_err * error_weight);
635            }
636
637            if (tmp_err < motion_error) {
638              motion_error = tmp_err;
639              mv = tmp_mv;
640            }
641          }
642
643          // Search in an older reference frame.
644          if (((lc == NULL && cm->current_video_frame > 1) ||
645               (lc != NULL && lc->current_video_frame_in_layer > 1))
646              && gld_yv12 != NULL) {
647            // Assume 0,0 motion with no mv overhead.
648            int gf_motion_error;
649
650            xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
651            gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
652                                                   &xd->plane[0].pre[0]);
653
654            first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv,
655                                     &gf_motion_error);
656            if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
657              vp9_clear_system_state();
658              gf_motion_error = (int)(gf_motion_error * error_weight);
659            }
660
661            if (gf_motion_error < motion_error && gf_motion_error < this_error)
662              ++second_ref_count;
663
664            // Reset to last frame as reference buffer.
665            xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
666            xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
667            xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
668
669            // In accumulating a score for the older reference frame take the
670            // best of the motion predicted score and the intra coded error
671            // (just as will be done for) accumulation of "coded_error" for
672            // the last frame.
673            if (gf_motion_error < this_error)
674              sr_coded_error += gf_motion_error;
675            else
676              sr_coded_error += this_error;
677          } else {
678            sr_coded_error += motion_error;
679          }
680        } else {
681          sr_coded_error += motion_error;
682        }
683
684        // Start by assuming that intra mode is best.
685        best_ref_mv.row = 0;
686        best_ref_mv.col = 0;
687
688#if CONFIG_FP_MB_STATS
689        if (cpi->use_fp_mb_stats) {
690          // intra predication statistics
691          cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
692          cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK;
693          cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
694          if (this_error > FPMB_ERROR_LARGE_TH) {
695            cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
696          } else if (this_error < FPMB_ERROR_SMALL_TH) {
697            cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
698          }
699        }
700#endif
701
702        if (motion_error <= this_error) {
703          // Keep a count of cases where the inter and intra were very close
704          // and very low. This helps with scene cut detection for example in
705          // cropped clips with black bars at the sides or top and bottom.
706          if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
707              this_error < 2 * intrapenalty)
708            ++neutral_count;
709
710          mv.row *= 8;
711          mv.col *= 8;
712          this_error = motion_error;
713          xd->mi[0].src_mi->mbmi.mode = NEWMV;
714          xd->mi[0].src_mi->mbmi.mv[0].as_mv = mv;
715          xd->mi[0].src_mi->mbmi.tx_size = TX_4X4;
716          xd->mi[0].src_mi->mbmi.ref_frame[0] = LAST_FRAME;
717          xd->mi[0].src_mi->mbmi.ref_frame[1] = NONE;
718          vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
719          vp9_encode_sby_pass1(x, bsize);
720          sum_mvr += mv.row;
721          sum_mvr_abs += abs(mv.row);
722          sum_mvc += mv.col;
723          sum_mvc_abs += abs(mv.col);
724          sum_mvrs += mv.row * mv.row;
725          sum_mvcs += mv.col * mv.col;
726          ++intercount;
727
728          best_ref_mv = mv;
729
730#if CONFIG_FP_MB_STATS
731          if (cpi->use_fp_mb_stats) {
732            // inter predication statistics
733            cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
734            cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK;
735            cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
736            if (this_error > FPMB_ERROR_LARGE_TH) {
737              cpi->twopass.frame_mb_stats_buf[mb_index] |=
738                  FPMB_ERROR_LARGE_MASK;
739            } else if (this_error < FPMB_ERROR_SMALL_TH) {
740              cpi->twopass.frame_mb_stats_buf[mb_index] |=
741                  FPMB_ERROR_SMALL_MASK;
742            }
743          }
744#endif
745
746          if (!is_zero_mv(&mv)) {
747            ++mvcount;
748
749#if CONFIG_FP_MB_STATS
750            if (cpi->use_fp_mb_stats) {
751              cpi->twopass.frame_mb_stats_buf[mb_index] &=
752                  ~FPMB_MOTION_ZERO_MASK;
753              // check estimated motion direction
754              if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) {
755                // right direction
756                cpi->twopass.frame_mb_stats_buf[mb_index] |=
757                    FPMB_MOTION_RIGHT_MASK;
758              } else if (mv.as_mv.row < 0 &&
759                         abs(mv.as_mv.row) >= abs(mv.as_mv.col)) {
760                // up direction
761                cpi->twopass.frame_mb_stats_buf[mb_index] |=
762                    FPMB_MOTION_UP_MASK;
763              } else if (mv.as_mv.col < 0 &&
764                         abs(mv.as_mv.col) >= abs(mv.as_mv.row)) {
765                // left direction
766                cpi->twopass.frame_mb_stats_buf[mb_index] |=
767                    FPMB_MOTION_LEFT_MASK;
768              } else {
769                // down direction
770                cpi->twopass.frame_mb_stats_buf[mb_index] |=
771                    FPMB_MOTION_DOWN_MASK;
772              }
773            }
774#endif
775
776            // Non-zero vector, was it different from the last non zero vector?
777            if (!is_equal_mv(&mv, &lastmv))
778              ++new_mv_count;
779            lastmv = mv;
780
781            // Does the row vector point inwards or outwards?
782            if (mb_row < cm->mb_rows / 2) {
783              if (mv.row > 0)
784                --sum_in_vectors;
785              else if (mv.row < 0)
786                ++sum_in_vectors;
787            } else if (mb_row > cm->mb_rows / 2) {
788              if (mv.row > 0)
789                ++sum_in_vectors;
790              else if (mv.row < 0)
791                --sum_in_vectors;
792            }
793
794            // Does the col vector point inwards or outwards?
795            if (mb_col < cm->mb_cols / 2) {
796              if (mv.col > 0)
797                --sum_in_vectors;
798              else if (mv.col < 0)
799                ++sum_in_vectors;
800            } else if (mb_col > cm->mb_cols / 2) {
801              if (mv.col > 0)
802                ++sum_in_vectors;
803              else if (mv.col < 0)
804                --sum_in_vectors;
805            }
806          }
807        }
808      } else {
809        sr_coded_error += (int64_t)this_error;
810      }
811      coded_error += (int64_t)this_error;
812
813      // Adjust to the next column of MBs.
814      x->plane[0].src.buf += 16;
815      x->plane[1].src.buf += uv_mb_height;
816      x->plane[2].src.buf += uv_mb_height;
817
818      recon_yoffset += 16;
819      recon_uvoffset += uv_mb_height;
820    }
821
822    // Adjust to the next row of MBs.
823    x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
824    x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
825                           uv_mb_height * cm->mb_cols;
826    x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
827                           uv_mb_height * cm->mb_cols;
828
829    vp9_clear_system_state();
830  }
831
832  vp9_clear_system_state();
833  {
834    FIRSTPASS_STATS fps;
835    // The minimum error here insures some bit alocation to frames even
836    // in static regions. The allocation per MB declines for larger formats
837    // where the typical "real" energy per MB also falls.
838    // Initial estimate here uses sqrt(mbs) to define the min_err, where the
839    // number of mbs is propotional to image area.
840    const double min_err = 200 * sqrt(cm->MBs);
841
842    fps.frame = cm->current_video_frame;
843    fps.spatial_layer_id = cpi->svc.spatial_layer_id;
844    fps.coded_error = (double)(coded_error >> 8) + min_err;
845    fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
846    fps.intra_error = (double)(intra_error >> 8) + min_err;
847    fps.count = 1.0;
848    fps.pcnt_inter = (double)intercount / cm->MBs;
849    fps.pcnt_second_ref = (double)second_ref_count / cm->MBs;
850    fps.pcnt_neutral = (double)neutral_count / cm->MBs;
851
852    if (mvcount > 0) {
853      fps.MVr = (double)sum_mvr / mvcount;
854      fps.mvr_abs = (double)sum_mvr_abs / mvcount;
855      fps.MVc = (double)sum_mvc / mvcount;
856      fps.mvc_abs = (double)sum_mvc_abs / mvcount;
857      fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount;
858      fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount;
859      fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
860      fps.new_mv_count = new_mv_count;
861      fps.pcnt_motion = (double)mvcount / cm->MBs;
862    } else {
863      fps.MVr = 0.0;
864      fps.mvr_abs = 0.0;
865      fps.MVc = 0.0;
866      fps.mvc_abs = 0.0;
867      fps.MVrv = 0.0;
868      fps.MVcv = 0.0;
869      fps.mv_in_out_count = 0.0;
870      fps.new_mv_count = 0.0;
871      fps.pcnt_motion = 0.0;
872    }
873
874    // TODO(paulwilkins):  Handle the case when duration is set to 0, or
875    // something less than the full time between subsequent values of
876    // cpi->source_time_stamp.
877    fps.duration = (double)(source->ts_end - source->ts_start);
878
879    // Don't want to do output stats with a stack variable!
880    twopass->this_frame_stats = fps;
881    output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
882    accumulate_stats(&twopass->total_stats, &fps);
883
884#if CONFIG_FP_MB_STATS
885    if (cpi->use_fp_mb_stats) {
886      output_fpmb_stats(twopass->frame_mb_stats_buf, cm, cpi->output_pkt_list);
887    }
888#endif
889  }
890
891  // Copy the previous Last Frame back into gf and and arf buffers if
892  // the prediction is good enough... but also don't allow it to lag too far.
893  if ((twopass->sr_update_lag > 3) ||
894      ((cm->current_video_frame > 0) &&
895       (twopass->this_frame_stats.pcnt_inter > 0.20) &&
896       ((twopass->this_frame_stats.intra_error /
897         DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
898    if (gld_yv12 != NULL) {
899      vp8_yv12_copy_frame(lst_yv12, gld_yv12);
900    }
901    twopass->sr_update_lag = 1;
902  } else {
903    ++twopass->sr_update_lag;
904  }
905
906  vp9_extend_frame_borders(new_yv12);
907
908  if (lc != NULL) {
909    vp9_update_reference_frames(cpi);
910  } else {
911    // Swap frame pointers so last frame refers to the frame we just compressed.
912    swap_yv12(lst_yv12, new_yv12);
913  }
914
915  // Special case for the first frame. Copy into the GF buffer as a second
916  // reference.
917  if (cm->current_video_frame == 0 && gld_yv12 != NULL && lc == NULL) {
918    vp8_yv12_copy_frame(lst_yv12, gld_yv12);
919  }
920
921  // Use this to see what the first pass reconstruction looks like.
922  if (0) {
923    char filename[512];
924    FILE *recon_file;
925    snprintf(filename, sizeof(filename), "enc%04d.yuv",
926             (int)cm->current_video_frame);
927
928    if (cm->current_video_frame == 0)
929      recon_file = fopen(filename, "wb");
930    else
931      recon_file = fopen(filename, "ab");
932
933    (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
934    fclose(recon_file);
935  }
936
937  ++cm->current_video_frame;
938  if (cpi->use_svc)
939    vp9_inc_frame_in_layer(cpi);
940}
941
942static double calc_correction_factor(double err_per_mb,
943                                     double err_divisor,
944                                     double pt_low,
945                                     double pt_high,
946                                     int q,
947                                     vpx_bit_depth_t bit_depth) {
948  const double error_term = err_per_mb / err_divisor;
949
950  // Adjustment based on actual quantizer to power term.
951  const double power_term =
952      MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.0125 + pt_low, pt_high);
953
954  // Calculate correction factor.
955  if (power_term < 1.0)
956    assert(error_term >= 0.0);
957
958  return fclamp(pow(error_term, power_term), 0.05, 5.0);
959}
960
961static int get_twopass_worst_quality(const VP9_COMP *cpi,
962                                     const FIRSTPASS_STATS *stats,
963                                     int section_target_bandwidth) {
964  const RATE_CONTROL *const rc = &cpi->rc;
965  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
966
967  if (section_target_bandwidth <= 0) {
968    return rc->worst_quality;  // Highest value allowed
969  } else {
970    const int num_mbs = cpi->common.MBs;
971    const double section_err = stats->coded_error / stats->count;
972    const double err_per_mb = section_err / num_mbs;
973    const double speed_term = 1.0 + 0.04 * oxcf->speed;
974    const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth <<
975                                         BPER_MB_NORMBITS) / num_mbs;
976    int q;
977    int is_svc_upper_layer = 0;
978    if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
979      is_svc_upper_layer = 1;
980
981    // Try and pick a max Q that will be high enough to encode the
982    // content at the given rate.
983    for (q = rc->best_quality; q < rc->worst_quality; ++q) {
984      const double factor =
985          calc_correction_factor(err_per_mb, ERR_DIVISOR,
986                                 is_svc_upper_layer ? SVC_FACTOR_PT_LOW :
987                                 FACTOR_PT_LOW, FACTOR_PT_HIGH, q,
988                                 cpi->common.bit_depth);
989      const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q,
990                                                 factor * speed_term,
991                                                 cpi->common.bit_depth);
992      if (bits_per_mb <= target_norm_bits_per_mb)
993        break;
994    }
995
996    // Restriction on active max q for constrained quality mode.
997    if (cpi->oxcf.rc_mode == VPX_CQ)
998      q = MAX(q, oxcf->cq_level);
999    return q;
1000  }
1001}
1002
1003extern void vp9_new_framerate(VP9_COMP *cpi, double framerate);
1004
1005void vp9_init_second_pass(VP9_COMP *cpi) {
1006  SVC *const svc = &cpi->svc;
1007  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1008  const int is_two_pass_svc = (svc->number_spatial_layers > 1) ||
1009                              (svc->number_temporal_layers > 1);
1010  TWO_PASS *const twopass = is_two_pass_svc ?
1011      &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
1012  double frame_rate;
1013  FIRSTPASS_STATS *stats;
1014
1015  zero_stats(&twopass->total_stats);
1016  zero_stats(&twopass->total_left_stats);
1017
1018  if (!twopass->stats_in_end)
1019    return;
1020
1021  stats = &twopass->total_stats;
1022
1023  *stats = *twopass->stats_in_end;
1024  twopass->total_left_stats = *stats;
1025
1026  frame_rate = 10000000.0 * stats->count / stats->duration;
1027  // Each frame can have a different duration, as the frame rate in the source
1028  // isn't guaranteed to be constant. The frame rate prior to the first frame
1029  // encoded in the second pass is a guess. However, the sum duration is not.
1030  // It is calculated based on the actual durations of all frames from the
1031  // first pass.
1032
1033  if (is_two_pass_svc) {
1034    vp9_update_spatial_layer_framerate(cpi, frame_rate);
1035    twopass->bits_left = (int64_t)(stats->duration *
1036        svc->layer_context[svc->spatial_layer_id].target_bandwidth /
1037        10000000.0);
1038  } else {
1039    vp9_new_framerate(cpi, frame_rate);
1040    twopass->bits_left = (int64_t)(stats->duration * oxcf->target_bandwidth /
1041                             10000000.0);
1042  }
1043
1044  // This variable monitors how far behind the second ref update is lagging.
1045  twopass->sr_update_lag = 1;
1046
1047  // Scan the first pass file and calculate a modified total error based upon
1048  // the bias/power function used to allocate bits.
1049  {
1050    const double avg_error = stats->coded_error /
1051                             DOUBLE_DIVIDE_CHECK(stats->count);
1052    const FIRSTPASS_STATS *s = twopass->stats_in;
1053    double modified_error_total = 0.0;
1054    twopass->modified_error_min = (avg_error *
1055                                      oxcf->two_pass_vbrmin_section) / 100;
1056    twopass->modified_error_max = (avg_error *
1057                                      oxcf->two_pass_vbrmax_section) / 100;
1058    while (s < twopass->stats_in_end) {
1059      modified_error_total += calculate_modified_err(twopass, oxcf, s);
1060      ++s;
1061    }
1062    twopass->modified_error_left = modified_error_total;
1063  }
1064
1065  // Reset the vbr bits off target counter
1066  cpi->rc.vbr_bits_off_target = 0;
1067
1068  // Static sequence monitor variables.
1069  twopass->kf_zeromotion_pct = 100;
1070  twopass->last_kfgroup_zeromotion_pct = 100;
1071}
1072
1073#define SR_DIFF_PART 0.0015
1074#define MOTION_AMP_PART 0.003
1075#define INTRA_PART 0.005
1076#define DEFAULT_DECAY_LIMIT 0.75
1077#define LOW_SR_DIFF_TRHESH 0.1
1078#define SR_DIFF_MAX 128.0
1079
1080static double get_sr_decay_rate(const VP9_COMMON *cm,
1081                                const FIRSTPASS_STATS *frame) {
1082  double sr_diff = (frame->sr_coded_error - frame->coded_error) / cm->MBs;
1083  double sr_decay = 1.0;
1084  const double motion_amplitude_factor =
1085    frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) / 2);
1086  const double pcnt_intra = 100 * (1.0 - frame->pcnt_inter);
1087
1088  if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1089    sr_diff = MIN(sr_diff, SR_DIFF_MAX);
1090    sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) -
1091               (MOTION_AMP_PART * motion_amplitude_factor) -
1092               (INTRA_PART * pcnt_intra);
1093  }
1094  return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, frame->pcnt_inter));
1095}
1096
1097// This function gives an estimate of how badly we believe the prediction
1098// quality is decaying from frame to frame.
1099static double get_zero_motion_factor(const VP9_COMMON *cm,
1100                                     const FIRSTPASS_STATS *frame) {
1101  const double zero_motion_pct = frame->pcnt_inter -
1102                                 frame->pcnt_motion;
1103  double sr_decay = get_sr_decay_rate(cm, frame);
1104  return MIN(sr_decay, zero_motion_pct);
1105}
1106
1107#define ZM_POWER_FACTOR 0.75
1108
1109static double get_prediction_decay_rate(const VP9_COMMON *cm,
1110                                        const FIRSTPASS_STATS *next_frame) {
1111  const double sr_decay_rate = get_sr_decay_rate(cm, next_frame);
1112  const double zero_motion_factor =
1113    (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
1114                ZM_POWER_FACTOR));
1115
1116  return MAX(zero_motion_factor,
1117             (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1118}
1119
1120// Function to test for a condition where a complex transition is followed
1121// by a static section. For example in slide shows where there is a fade
1122// between slides. This is to help with more optimal kf and gf positioning.
1123static int detect_transition_to_still(const TWO_PASS *twopass,
1124                                      int frame_interval, int still_interval,
1125                                      double loop_decay_rate,
1126                                      double last_decay_rate) {
1127  // Break clause to detect very still sections after motion
1128  // For example a static image after a fade or other transition
1129  // instead of a clean scene cut.
1130  if (frame_interval > MIN_GF_INTERVAL &&
1131      loop_decay_rate >= 0.999 &&
1132      last_decay_rate < 0.9) {
1133    int j;
1134
1135    // Look ahead a few frames to see if static condition persists...
1136    for (j = 0; j < still_interval; ++j) {
1137      const FIRSTPASS_STATS *stats = &twopass->stats_in[j];
1138      if (stats >= twopass->stats_in_end)
1139        break;
1140
1141      if (stats->pcnt_inter - stats->pcnt_motion < 0.999)
1142        break;
1143    }
1144
1145    // Only if it does do we signal a transition to still.
1146    return j == still_interval;
1147  }
1148
1149  return 0;
1150}
1151
1152// This function detects a flash through the high relative pcnt_second_ref
1153// score in the frame following a flash frame. The offset passed in should
1154// reflect this.
1155static int detect_flash(const TWO_PASS *twopass, int offset) {
1156  const FIRSTPASS_STATS *const next_frame = read_frame_stats(twopass, offset);
1157
1158  // What we are looking for here is a situation where there is a
1159  // brief break in prediction (such as a flash) but subsequent frames
1160  // are reasonably well predicted by an earlier (pre flash) frame.
1161  // The recovery after a flash is indicated by a high pcnt_second_ref
1162  // compared to pcnt_inter.
1163  return next_frame != NULL &&
1164         next_frame->pcnt_second_ref > next_frame->pcnt_inter &&
1165         next_frame->pcnt_second_ref >= 0.5;
1166}
1167
1168// Update the motion related elements to the GF arf boost calculation.
1169static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats,
1170                                          double *mv_in_out,
1171                                          double *mv_in_out_accumulator,
1172                                          double *abs_mv_in_out_accumulator,
1173                                          double *mv_ratio_accumulator) {
1174  const double pct = stats->pcnt_motion;
1175
1176  // Accumulate Motion In/Out of frame stats.
1177  *mv_in_out = stats->mv_in_out_count * pct;
1178  *mv_in_out_accumulator += *mv_in_out;
1179  *abs_mv_in_out_accumulator += fabs(*mv_in_out);
1180
1181  // Accumulate a measure of how uniform (or conversely how random) the motion
1182  // field is (a ratio of abs(mv) / mv).
1183  if (pct > 0.05) {
1184    const double mvr_ratio = fabs(stats->mvr_abs) /
1185                                 DOUBLE_DIVIDE_CHECK(fabs(stats->MVr));
1186    const double mvc_ratio = fabs(stats->mvc_abs) /
1187                                 DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
1188
1189    *mv_ratio_accumulator += pct * (mvr_ratio < stats->mvr_abs ?
1190                                       mvr_ratio : stats->mvr_abs);
1191    *mv_ratio_accumulator += pct * (mvc_ratio < stats->mvc_abs ?
1192                                       mvc_ratio : stats->mvc_abs);
1193  }
1194}
1195
1196#define BASELINE_ERR_PER_MB 1000.0
1197static double calc_frame_boost(VP9_COMP *cpi,
1198                               const FIRSTPASS_STATS *this_frame,
1199                               double this_frame_mv_in_out,
1200                               double max_boost) {
1201  double frame_boost;
1202
1203  // Underlying boost factor is based on inter error ratio.
1204  frame_boost = (BASELINE_ERR_PER_MB * cpi->common.MBs) /
1205                DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1206  frame_boost = frame_boost * BOOST_FACTOR;
1207
1208  // Increase boost for frames where new data coming into frame (e.g. zoom out).
1209  // Slightly reduce boost if there is a net balance of motion out of the frame
1210  // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1211  if (this_frame_mv_in_out > 0.0)
1212    frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1213  // In the extreme case the boost is halved.
1214  else
1215    frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1216
1217  return MIN(frame_boost, max_boost);
1218}
1219
1220static int calc_arf_boost(VP9_COMP *cpi, int offset,
1221                          int f_frames, int b_frames,
1222                          int *f_boost, int *b_boost) {
1223  TWO_PASS *const twopass = &cpi->twopass;
1224  int i;
1225  double boost_score = 0.0;
1226  double mv_ratio_accumulator = 0.0;
1227  double decay_accumulator = 1.0;
1228  double this_frame_mv_in_out = 0.0;
1229  double mv_in_out_accumulator = 0.0;
1230  double abs_mv_in_out_accumulator = 0.0;
1231  int arf_boost;
1232  int flash_detected = 0;
1233
1234  // Search forward from the proposed arf/next gf position.
1235  for (i = 0; i < f_frames; ++i) {
1236    const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1237    if (this_frame == NULL)
1238      break;
1239
1240    // Update the motion related elements to the boost calculation.
1241    accumulate_frame_motion_stats(this_frame,
1242                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
1243                                  &abs_mv_in_out_accumulator,
1244                                  &mv_ratio_accumulator);
1245
1246    // We want to discount the flash frame itself and the recovery
1247    // frame that follows as both will have poor scores.
1248    flash_detected = detect_flash(twopass, i + offset) ||
1249                     detect_flash(twopass, i + offset + 1);
1250
1251    // Accumulate the effect of prediction quality decay.
1252    if (!flash_detected) {
1253      decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame);
1254      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1255                          ? MIN_DECAY_FACTOR : decay_accumulator;
1256    }
1257
1258    boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1259                                                        this_frame_mv_in_out,
1260                                                        GF_MAX_BOOST);
1261  }
1262
1263  *f_boost = (int)boost_score;
1264
1265  // Reset for backward looking loop.
1266  boost_score = 0.0;
1267  mv_ratio_accumulator = 0.0;
1268  decay_accumulator = 1.0;
1269  this_frame_mv_in_out = 0.0;
1270  mv_in_out_accumulator = 0.0;
1271  abs_mv_in_out_accumulator = 0.0;
1272
1273  // Search backward towards last gf position.
1274  for (i = -1; i >= -b_frames; --i) {
1275    const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1276    if (this_frame == NULL)
1277      break;
1278
1279    // Update the motion related elements to the boost calculation.
1280    accumulate_frame_motion_stats(this_frame,
1281                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
1282                                  &abs_mv_in_out_accumulator,
1283                                  &mv_ratio_accumulator);
1284
1285    // We want to discount the the flash frame itself and the recovery
1286    // frame that follows as both will have poor scores.
1287    flash_detected = detect_flash(twopass, i + offset) ||
1288                     detect_flash(twopass, i + offset + 1);
1289
1290    // Cumulative effect of prediction quality decay.
1291    if (!flash_detected) {
1292      decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame);
1293      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1294                              ? MIN_DECAY_FACTOR : decay_accumulator;
1295    }
1296
1297    boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1298                                                        this_frame_mv_in_out,
1299                                                        GF_MAX_BOOST);
1300  }
1301  *b_boost = (int)boost_score;
1302
1303  arf_boost = (*f_boost + *b_boost);
1304  if (arf_boost < ((b_frames + f_frames) * 20))
1305    arf_boost = ((b_frames + f_frames) * 20);
1306
1307  return arf_boost;
1308}
1309
1310// Calculate a section intra ratio used in setting max loop filter.
1311static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
1312                                         const FIRSTPASS_STATS *end,
1313                                         int section_length) {
1314  const FIRSTPASS_STATS *s = begin;
1315  double intra_error = 0.0;
1316  double coded_error = 0.0;
1317  int i = 0;
1318
1319  while (s < end && i < section_length) {
1320    intra_error += s->intra_error;
1321    coded_error += s->coded_error;
1322    ++s;
1323    ++i;
1324  }
1325
1326  return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error));
1327}
1328
1329// Calculate the total bits to allocate in this GF/ARF group.
1330static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi,
1331                                             double gf_group_err) {
1332  const RATE_CONTROL *const rc = &cpi->rc;
1333  const TWO_PASS *const twopass = &cpi->twopass;
1334  const int max_bits = frame_max_bits(rc, &cpi->oxcf);
1335  int64_t total_group_bits;
1336
1337  // Calculate the bits to be allocated to the group as a whole.
1338  if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) {
1339    total_group_bits = (int64_t)(twopass->kf_group_bits *
1340                                 (gf_group_err / twopass->kf_group_error_left));
1341  } else {
1342    total_group_bits = 0;
1343  }
1344
1345  // Clamp odd edge cases.
1346  total_group_bits = (total_group_bits < 0) ?
1347     0 : (total_group_bits > twopass->kf_group_bits) ?
1348     twopass->kf_group_bits : total_group_bits;
1349
1350  // Clip based on user supplied data rate variability limit.
1351  if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
1352    total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
1353
1354  return total_group_bits;
1355}
1356
1357// Calculate the number bits extra to assign to boosted frames in a group.
1358static int calculate_boost_bits(int frame_count,
1359                                int boost, int64_t total_group_bits) {
1360  int allocation_chunks;
1361
1362  // return 0 for invalid inputs (could arise e.g. through rounding errors)
1363  if (!boost || (total_group_bits <= 0) || (frame_count <= 0) )
1364    return 0;
1365
1366  allocation_chunks = (frame_count * 100) + boost;
1367
1368  // Prevent overflow.
1369  if (boost > 1023) {
1370    int divisor = boost >> 10;
1371    boost /= divisor;
1372    allocation_chunks /= divisor;
1373  }
1374
1375  // Calculate the number of extra bits for use in the boosted frame or frames.
1376  return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0);
1377}
1378
1379// Current limit on maximum number of active arfs in a GF/ARF group.
1380#define MAX_ACTIVE_ARFS 2
1381#define ARF_SLOT1 2
1382#define ARF_SLOT2 3
1383// This function indirects the choice of buffers for arfs.
1384// At the moment the values are fixed but this may change as part of
1385// the integration process with other codec features that swap buffers around.
1386static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) {
1387  arf_buffer_indices[0] = ARF_SLOT1;
1388  arf_buffer_indices[1] = ARF_SLOT2;
1389}
1390
1391static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits,
1392                                   double group_error, int gf_arf_bits) {
1393  RATE_CONTROL *const rc = &cpi->rc;
1394  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1395  TWO_PASS *const twopass = &cpi->twopass;
1396  GF_GROUP *const gf_group = &twopass->gf_group;
1397  FIRSTPASS_STATS frame_stats;
1398  int i;
1399  int frame_index = 1;
1400  int target_frame_size;
1401  int key_frame;
1402  const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf);
1403  int64_t total_group_bits = gf_group_bits;
1404  double modified_err = 0.0;
1405  double err_fraction;
1406  int mid_boost_bits = 0;
1407  int mid_frame_idx;
1408  unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS];
1409  int alt_frame_index = frame_index;
1410  int has_temporal_layers = is_two_pass_svc(cpi) &&
1411                            cpi->svc.number_temporal_layers > 1;
1412
1413  // Only encode alt reference frame in temporal base layer.
1414  if (has_temporal_layers)
1415    alt_frame_index = cpi->svc.number_temporal_layers;
1416
1417  key_frame = cpi->common.frame_type == KEY_FRAME ||
1418              vp9_is_upper_layer_key_frame(cpi);
1419
1420  get_arf_buffer_indices(arf_buffer_indices);
1421
1422  // For key frames the frame target rate is already set and it
1423  // is also the golden frame.
1424  if (!key_frame) {
1425    if (rc->source_alt_ref_active) {
1426      gf_group->update_type[0] = OVERLAY_UPDATE;
1427      gf_group->rf_level[0] = INTER_NORMAL;
1428      gf_group->bit_allocation[0] = 0;
1429      gf_group->arf_update_idx[0] = arf_buffer_indices[0];
1430      gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
1431    } else {
1432      gf_group->update_type[0] = GF_UPDATE;
1433      gf_group->rf_level[0] = GF_ARF_STD;
1434      gf_group->bit_allocation[0] = gf_arf_bits;
1435      gf_group->arf_update_idx[0] = arf_buffer_indices[0];
1436      gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
1437    }
1438
1439    // Step over the golden frame / overlay frame
1440    if (EOF == input_stats(twopass, &frame_stats))
1441      return;
1442  }
1443
1444  // Deduct the boost bits for arf (or gf if it is not a key frame)
1445  // from the group total.
1446  if (rc->source_alt_ref_pending || !key_frame)
1447    total_group_bits -= gf_arf_bits;
1448
1449  // Store the bits to spend on the ARF if there is one.
1450  if (rc->source_alt_ref_pending) {
1451    gf_group->update_type[alt_frame_index] = ARF_UPDATE;
1452    gf_group->rf_level[alt_frame_index] = GF_ARF_STD;
1453    gf_group->bit_allocation[alt_frame_index] = gf_arf_bits;
1454
1455    if (has_temporal_layers)
1456      gf_group->arf_src_offset[alt_frame_index] =
1457          (unsigned char)(rc->baseline_gf_interval -
1458                          cpi->svc.number_temporal_layers);
1459    else
1460      gf_group->arf_src_offset[alt_frame_index] =
1461          (unsigned char)(rc->baseline_gf_interval - 1);
1462
1463    gf_group->arf_update_idx[alt_frame_index] = arf_buffer_indices[0];
1464    gf_group->arf_ref_idx[alt_frame_index] =
1465      arf_buffer_indices[cpi->multi_arf_last_grp_enabled &&
1466                         rc->source_alt_ref_active];
1467    if (!has_temporal_layers)
1468      ++frame_index;
1469
1470    if (cpi->multi_arf_enabled) {
1471      // Set aside a slot for a level 1 arf.
1472      gf_group->update_type[frame_index] = ARF_UPDATE;
1473      gf_group->rf_level[frame_index] = GF_ARF_LOW;
1474      gf_group->arf_src_offset[frame_index] =
1475        (unsigned char)((rc->baseline_gf_interval >> 1) - 1);
1476      gf_group->arf_update_idx[frame_index] = arf_buffer_indices[1];
1477      gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
1478      ++frame_index;
1479    }
1480  }
1481
1482  // Define middle frame
1483  mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
1484
1485  // Allocate bits to the other frames in the group.
1486  for (i = 0; i < rc->baseline_gf_interval - 1; ++i) {
1487    int arf_idx = 0;
1488    if (EOF == input_stats(twopass, &frame_stats))
1489      break;
1490
1491    if (has_temporal_layers && frame_index == alt_frame_index) {
1492      ++frame_index;
1493    }
1494
1495    modified_err = calculate_modified_err(twopass, oxcf, &frame_stats);
1496
1497    if (group_error > 0)
1498      err_fraction = modified_err / DOUBLE_DIVIDE_CHECK(group_error);
1499    else
1500      err_fraction = 0.0;
1501
1502    target_frame_size = (int)((double)total_group_bits * err_fraction);
1503
1504    if (rc->source_alt_ref_pending && cpi->multi_arf_enabled) {
1505      mid_boost_bits += (target_frame_size >> 4);
1506      target_frame_size -= (target_frame_size >> 4);
1507
1508      if (frame_index <= mid_frame_idx)
1509        arf_idx = 1;
1510    }
1511    gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx];
1512    gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx];
1513
1514    target_frame_size = clamp(target_frame_size, 0,
1515                              MIN(max_bits, (int)total_group_bits));
1516
1517    gf_group->update_type[frame_index] = LF_UPDATE;
1518    gf_group->rf_level[frame_index] = INTER_NORMAL;
1519
1520    gf_group->bit_allocation[frame_index] = target_frame_size;
1521    ++frame_index;
1522  }
1523
1524  // Note:
1525  // We need to configure the frame at the end of the sequence + 1 that will be
1526  // the start frame for the next group. Otherwise prior to the call to
1527  // vp9_rc_get_second_pass_params() the data will be undefined.
1528  gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
1529  gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
1530
1531  if (rc->source_alt_ref_pending) {
1532    gf_group->update_type[frame_index] = OVERLAY_UPDATE;
1533    gf_group->rf_level[frame_index] = INTER_NORMAL;
1534
1535    // Final setup for second arf and its overlay.
1536    if (cpi->multi_arf_enabled) {
1537      gf_group->bit_allocation[2] =
1538          gf_group->bit_allocation[mid_frame_idx] + mid_boost_bits;
1539      gf_group->update_type[mid_frame_idx] = OVERLAY_UPDATE;
1540      gf_group->bit_allocation[mid_frame_idx] = 0;
1541    }
1542  } else {
1543    gf_group->update_type[frame_index] = GF_UPDATE;
1544    gf_group->rf_level[frame_index] = GF_ARF_STD;
1545  }
1546
1547  // Note whether multi-arf was enabled this group for next time.
1548  cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled;
1549}
1550
1551// Analyse and define a gf/arf group.
1552static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1553  RATE_CONTROL *const rc = &cpi->rc;
1554  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1555  TWO_PASS *const twopass = &cpi->twopass;
1556  FIRSTPASS_STATS next_frame;
1557  const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
1558  int i;
1559
1560  double boost_score = 0.0;
1561  double old_boost_score = 0.0;
1562  double gf_group_err = 0.0;
1563  double gf_first_frame_err = 0.0;
1564  double mod_frame_err = 0.0;
1565
1566  double mv_ratio_accumulator = 0.0;
1567  double decay_accumulator = 1.0;
1568  double zero_motion_accumulator = 1.0;
1569
1570  double loop_decay_rate = 1.00;
1571  double last_loop_decay_rate = 1.00;
1572
1573  double this_frame_mv_in_out = 0.0;
1574  double mv_in_out_accumulator = 0.0;
1575  double abs_mv_in_out_accumulator = 0.0;
1576  double mv_ratio_accumulator_thresh;
1577  unsigned int allow_alt_ref = is_altref_enabled(cpi);
1578
1579  int f_boost = 0;
1580  int b_boost = 0;
1581  int flash_detected;
1582  int active_max_gf_interval;
1583  int64_t gf_group_bits;
1584  double gf_group_error_left;
1585  int gf_arf_bits;
1586
1587  // Reset the GF group data structures unless this is a key
1588  // frame in which case it will already have been done.
1589  if (cpi->common.frame_type != KEY_FRAME) {
1590    vp9_zero(twopass->gf_group);
1591  }
1592
1593  vp9_clear_system_state();
1594  vp9_zero(next_frame);
1595
1596  // Load stats for the current frame.
1597  mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame);
1598
1599  // Note the error of the frame at the start of the group. This will be
1600  // the GF frame error if we code a normal gf.
1601  gf_first_frame_err = mod_frame_err;
1602
1603  // If this is a key frame or the overlay from a previous arf then
1604  // the error score / cost of this frame has already been accounted for.
1605  if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1606    gf_group_err -= gf_first_frame_err;
1607
1608  // Motion breakout threshold for loop below depends on image size.
1609  mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 4.0;
1610
1611  // Work out a maximum interval for the GF group.
1612  // If the image appears almost completely static we can extend beyond this.
1613  if (cpi->multi_arf_allowed) {
1614    active_max_gf_interval = rc->max_gf_interval;
1615  } else {
1616   // The value chosen depends on the active Q range. At low Q we have
1617   // bits to spare and are better with a smaller interval and smaller boost.
1618   // At high Q when there are few bits to spare we are better with a longer
1619   // interval to spread the cost of the GF.
1620   active_max_gf_interval =
1621     12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME],
1622                                        cpi->common.bit_depth) >> 5);
1623
1624   if (active_max_gf_interval > rc->max_gf_interval)
1625     active_max_gf_interval = rc->max_gf_interval;
1626  }
1627
1628  i = 0;
1629  while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
1630    ++i;
1631
1632    // Accumulate error score of frames in this gf group.
1633    mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame);
1634    gf_group_err += mod_frame_err;
1635
1636    if (EOF == input_stats(twopass, &next_frame))
1637      break;
1638
1639    // Test for the case where there is a brief flash but the prediction
1640    // quality back to an earlier frame is then restored.
1641    flash_detected = detect_flash(twopass, 0);
1642
1643    // Update the motion related elements to the boost calculation.
1644    accumulate_frame_motion_stats(&next_frame,
1645                                  &this_frame_mv_in_out, &mv_in_out_accumulator,
1646                                  &abs_mv_in_out_accumulator,
1647                                  &mv_ratio_accumulator);
1648
1649    // Accumulate the effect of prediction quality decay.
1650    if (!flash_detected) {
1651      last_loop_decay_rate = loop_decay_rate;
1652      loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
1653
1654      decay_accumulator = decay_accumulator * loop_decay_rate;
1655
1656      // Monitor for static sections.
1657      zero_motion_accumulator =
1658        MIN(zero_motion_accumulator,
1659            get_zero_motion_factor(&cpi->common, &next_frame));
1660
1661      // Break clause to detect very still sections after motion. For example,
1662      // a static image after a fade or other transition.
1663      if (detect_transition_to_still(twopass, i, 5, loop_decay_rate,
1664                                     last_loop_decay_rate)) {
1665        allow_alt_ref = 0;
1666        break;
1667      }
1668    }
1669
1670    // Calculate a boost number for this frame.
1671    boost_score += decay_accumulator * calc_frame_boost(cpi, &next_frame,
1672                                                        this_frame_mv_in_out,
1673                                                        GF_MAX_BOOST);
1674
1675    // Break out conditions.
1676    if (
1677      // Break at active_max_gf_interval unless almost totally static.
1678      (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
1679      (
1680        // Don't break out with a very short interval.
1681        (i > MIN_GF_INTERVAL) &&
1682        (!flash_detected) &&
1683        ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
1684         (abs_mv_in_out_accumulator > 3.0) ||
1685         (mv_in_out_accumulator < -2.0) ||
1686         ((boost_score - old_boost_score) < BOOST_FACTOR)))) {
1687      boost_score = old_boost_score;
1688      break;
1689    }
1690
1691    *this_frame = next_frame;
1692    old_boost_score = boost_score;
1693  }
1694
1695  twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
1696
1697  // Set the interval until the next gf.
1698  if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1699    rc->baseline_gf_interval = i - 1;
1700  else
1701    rc->baseline_gf_interval = i;
1702
1703  // Only encode alt reference frame in temporal base layer. So
1704  // baseline_gf_interval should be multiple of a temporal layer group
1705  // (typically the frame distance between two base layer frames)
1706  if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
1707    int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
1708    int new_gf_interval = (rc->baseline_gf_interval + count) & (~count);
1709    int j;
1710    for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) {
1711      if (EOF == input_stats(twopass, this_frame))
1712        break;
1713      gf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
1714    }
1715    rc->baseline_gf_interval = new_gf_interval;
1716  }
1717
1718  rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1719
1720  // Should we use the alternate reference frame.
1721  if (allow_alt_ref &&
1722      (i < cpi->oxcf.lag_in_frames) &&
1723      (i >= MIN_GF_INTERVAL)) {
1724    // Calculate the boost for alt ref.
1725    rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
1726                                   &b_boost);
1727    rc->source_alt_ref_pending = 1;
1728
1729    // Test to see if multi arf is appropriate.
1730    cpi->multi_arf_enabled =
1731      (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) &&
1732      (zero_motion_accumulator < 0.995)) ? 1 : 0;
1733  } else {
1734    rc->gfu_boost = MAX((int)boost_score, 125);
1735    rc->source_alt_ref_pending = 0;
1736  }
1737
1738  // Reset the file position.
1739  reset_fpf_position(twopass, start_pos);
1740
1741  // Calculate the bits to be allocated to the gf/arf group as a whole
1742  gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
1743
1744  // Calculate the extra bits to be used for boosted frame(s)
1745  {
1746    int q = rc->last_q[INTER_FRAME];
1747    int boost =
1748        (rc->gfu_boost * gfboost_qadjust(q, cpi->common.bit_depth)) / 100;
1749
1750    // Set max and minimum boost and hence minimum allocation.
1751    boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200);
1752
1753    // Calculate the extra bits to be used for boosted frame(s)
1754    gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval,
1755                                       boost, gf_group_bits);
1756  }
1757
1758  // Adjust KF group bits and error remaining.
1759  twopass->kf_group_error_left -= (int64_t)gf_group_err;
1760
1761  // If this is an arf update we want to remove the score for the overlay
1762  // frame at the end which will usually be very cheap to code.
1763  // The overlay frame has already, in effect, been coded so we want to spread
1764  // the remaining bits among the other frames.
1765  // For normal GFs remove the score for the GF itself unless this is
1766  // also a key frame in which case it has already been accounted for.
1767  if (rc->source_alt_ref_pending) {
1768    gf_group_error_left = gf_group_err - mod_frame_err;
1769  } else if (cpi->common.frame_type != KEY_FRAME) {
1770    gf_group_error_left = gf_group_err - gf_first_frame_err;
1771  } else {
1772    gf_group_error_left = gf_group_err;
1773  }
1774
1775  // Allocate bits to each of the frames in the GF group.
1776  allocate_gf_group_bits(cpi, gf_group_bits, gf_group_error_left, gf_arf_bits);
1777
1778  // Reset the file position.
1779  reset_fpf_position(twopass, start_pos);
1780
1781  // Calculate a section intra ratio used in setting max loop filter.
1782  if (cpi->common.frame_type != KEY_FRAME) {
1783    twopass->section_intra_rating =
1784        calculate_section_intra_ratio(start_pos, twopass->stats_in_end,
1785                                      rc->baseline_gf_interval);
1786  }
1787}
1788
1789// TODO(PGW) Re-examine the use of II ration in this code in the light of#
1790// changes elsewhere
1791#define KF_II_MAX 128.0
1792static int test_candidate_kf(TWO_PASS *twopass,
1793                             const FIRSTPASS_STATS *last_frame,
1794                             const FIRSTPASS_STATS *this_frame,
1795                             const FIRSTPASS_STATS *next_frame) {
1796  int is_viable_kf = 0;
1797
1798  // Does the frame satisfy the primary criteria of a key frame?
1799  // If so, then examine how well it predicts subsequent frames.
1800  if ((this_frame->pcnt_second_ref < 0.10) &&
1801      (next_frame->pcnt_second_ref < 0.10) &&
1802      ((this_frame->pcnt_inter < 0.05) ||
1803       (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) &&
1804        ((this_frame->intra_error /
1805          DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
1806        ((fabs(last_frame->coded_error - this_frame->coded_error) /
1807              DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) ||
1808         (fabs(last_frame->intra_error - this_frame->intra_error) /
1809              DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) ||
1810         ((next_frame->intra_error /
1811           DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
1812    int i;
1813    const FIRSTPASS_STATS *start_pos = twopass->stats_in;
1814    FIRSTPASS_STATS local_next_frame = *next_frame;
1815    double boost_score = 0.0;
1816    double old_boost_score = 0.0;
1817    double decay_accumulator = 1.0;
1818
1819    // Examine how well the key frame predicts subsequent frames.
1820    for (i = 0; i < 16; ++i) {
1821      double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error /
1822                             DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
1823
1824      if (next_iiratio > KF_II_MAX)
1825        next_iiratio = KF_II_MAX;
1826
1827      // Cumulative effect of decay in prediction quality.
1828      if (local_next_frame.pcnt_inter > 0.85)
1829        decay_accumulator *= local_next_frame.pcnt_inter;
1830      else
1831        decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
1832
1833      // Keep a running total.
1834      boost_score += (decay_accumulator * next_iiratio);
1835
1836      // Test various breakout clauses.
1837      if ((local_next_frame.pcnt_inter < 0.05) ||
1838          (next_iiratio < 1.5) ||
1839          (((local_next_frame.pcnt_inter -
1840             local_next_frame.pcnt_neutral) < 0.20) &&
1841           (next_iiratio < 3.0)) ||
1842          ((boost_score - old_boost_score) < 3.0) ||
1843          (local_next_frame.intra_error < 200)) {
1844        break;
1845      }
1846
1847      old_boost_score = boost_score;
1848
1849      // Get the next frame details
1850      if (EOF == input_stats(twopass, &local_next_frame))
1851        break;
1852    }
1853
1854    // If there is tolerable prediction for at least the next 3 frames then
1855    // break out else discard this potential key frame and move on
1856    if (boost_score > 30.0 && (i > 3)) {
1857      is_viable_kf = 1;
1858    } else {
1859      // Reset the file position
1860      reset_fpf_position(twopass, start_pos);
1861
1862      is_viable_kf = 0;
1863    }
1864  }
1865
1866  return is_viable_kf;
1867}
1868
1869static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1870  int i, j;
1871  RATE_CONTROL *const rc = &cpi->rc;
1872  TWO_PASS *const twopass = &cpi->twopass;
1873  GF_GROUP *const gf_group = &twopass->gf_group;
1874  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1875  const FIRSTPASS_STATS first_frame = *this_frame;
1876  const FIRSTPASS_STATS *const start_position = twopass->stats_in;
1877  FIRSTPASS_STATS next_frame;
1878  FIRSTPASS_STATS last_frame;
1879  int kf_bits = 0;
1880  int loop_decay_counter = 0;
1881  double decay_accumulator = 1.0;
1882  double av_decay_accumulator = 0.0;
1883  double zero_motion_accumulator = 1.0;
1884  double boost_score = 0.0;
1885  double kf_mod_err = 0.0;
1886  double kf_group_err = 0.0;
1887  double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
1888
1889  vp9_zero(next_frame);
1890
1891  cpi->common.frame_type = KEY_FRAME;
1892
1893  // Reset the GF group data structures.
1894  vp9_zero(*gf_group);
1895
1896  // Is this a forced key frame by interval.
1897  rc->this_key_frame_forced = rc->next_key_frame_forced;
1898
1899  // Clear the alt ref active flag and last group multi arf flags as they
1900  // can never be set for a key frame.
1901  rc->source_alt_ref_active = 0;
1902  cpi->multi_arf_last_grp_enabled = 0;
1903
1904  // KF is always a GF so clear frames till next gf counter.
1905  rc->frames_till_gf_update_due = 0;
1906
1907  rc->frames_to_key = 1;
1908
1909  twopass->kf_group_bits = 0;        // Total bits available to kf group
1910  twopass->kf_group_error_left = 0;  // Group modified error score.
1911
1912  kf_mod_err = calculate_modified_err(twopass, oxcf, this_frame);
1913
1914  // Find the next keyframe.
1915  i = 0;
1916  while (twopass->stats_in < twopass->stats_in_end &&
1917         rc->frames_to_key < cpi->oxcf.key_freq) {
1918    // Accumulate kf group error.
1919    kf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
1920
1921    // Load the next frame's stats.
1922    last_frame = *this_frame;
1923    input_stats(twopass, this_frame);
1924
1925    // Provided that we are not at the end of the file...
1926    if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) {
1927      double loop_decay_rate;
1928
1929      // Check for a scene cut.
1930      if (test_candidate_kf(twopass, &last_frame, this_frame,
1931                            twopass->stats_in))
1932        break;
1933
1934      // How fast is the prediction quality decaying?
1935      loop_decay_rate = get_prediction_decay_rate(&cpi->common,
1936                                                  twopass->stats_in);
1937
1938      // We want to know something about the recent past... rather than
1939      // as used elsewhere where we are concerned with decay in prediction
1940      // quality since the last GF or KF.
1941      recent_loop_decay[i % 8] = loop_decay_rate;
1942      decay_accumulator = 1.0;
1943      for (j = 0; j < 8; ++j)
1944        decay_accumulator *= recent_loop_decay[j];
1945
1946      // Special check for transition or high motion followed by a
1947      // static scene.
1948      if (detect_transition_to_still(twopass, i, cpi->oxcf.key_freq - i,
1949                                     loop_decay_rate, decay_accumulator))
1950        break;
1951
1952      // Step on to the next frame.
1953      ++rc->frames_to_key;
1954
1955      // If we don't have a real key frame within the next two
1956      // key_freq intervals then break out of the loop.
1957      if (rc->frames_to_key >= 2 * cpi->oxcf.key_freq)
1958        break;
1959    } else {
1960      ++rc->frames_to_key;
1961    }
1962    ++i;
1963  }
1964
1965  // If there is a max kf interval set by the user we must obey it.
1966  // We already breakout of the loop above at 2x max.
1967  // This code centers the extra kf if the actual natural interval
1968  // is between 1x and 2x.
1969  if (cpi->oxcf.auto_key &&
1970      rc->frames_to_key > cpi->oxcf.key_freq) {
1971    FIRSTPASS_STATS tmp_frame = first_frame;
1972
1973    rc->frames_to_key /= 2;
1974
1975    // Reset to the start of the group.
1976    reset_fpf_position(twopass, start_position);
1977
1978    kf_group_err = 0;
1979
1980    // Rescan to get the correct error data for the forced kf group.
1981    for (i = 0; i < rc->frames_to_key; ++i) {
1982      kf_group_err += calculate_modified_err(twopass, oxcf, &tmp_frame);
1983      input_stats(twopass, &tmp_frame);
1984    }
1985    rc->next_key_frame_forced = 1;
1986  } else if (twopass->stats_in == twopass->stats_in_end ||
1987             rc->frames_to_key >= cpi->oxcf.key_freq) {
1988    rc->next_key_frame_forced = 1;
1989  } else {
1990    rc->next_key_frame_forced = 0;
1991  }
1992
1993  if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
1994    int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
1995    int new_frame_to_key = (rc->frames_to_key + count) & (~count);
1996    int j;
1997    for (j = 0; j < new_frame_to_key - rc->frames_to_key; ++j) {
1998      if (EOF == input_stats(twopass, this_frame))
1999        break;
2000      kf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
2001    }
2002    rc->frames_to_key = new_frame_to_key;
2003  }
2004
2005  // Special case for the last key frame of the file.
2006  if (twopass->stats_in >= twopass->stats_in_end) {
2007    // Accumulate kf group error.
2008    kf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
2009  }
2010
2011  // Calculate the number of bits that should be assigned to the kf group.
2012  if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) {
2013    // Maximum number of bits for a single normal frame (not key frame).
2014    const int max_bits = frame_max_bits(rc, &cpi->oxcf);
2015
2016    // Maximum number of bits allocated to the key frame group.
2017    int64_t max_grp_bits;
2018
2019    // Default allocation based on bits left and relative
2020    // complexity of the section.
2021    twopass->kf_group_bits = (int64_t)(twopass->bits_left *
2022       (kf_group_err / twopass->modified_error_left));
2023
2024    // Clip based on maximum per frame rate defined by the user.
2025    max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
2026    if (twopass->kf_group_bits > max_grp_bits)
2027      twopass->kf_group_bits = max_grp_bits;
2028  } else {
2029    twopass->kf_group_bits = 0;
2030  }
2031  twopass->kf_group_bits = MAX(0, twopass->kf_group_bits);
2032
2033  // Reset the first pass file position.
2034  reset_fpf_position(twopass, start_position);
2035
2036  // Scan through the kf group collating various stats used to determine
2037  // how many bits to spend on it.
2038  decay_accumulator = 1.0;
2039  boost_score = 0.0;
2040  for (i = 0; i < (rc->frames_to_key - 1); ++i) {
2041    if (EOF == input_stats(twopass, &next_frame))
2042      break;
2043
2044    // Monitor for static sections.
2045    zero_motion_accumulator =
2046      MIN(zero_motion_accumulator,
2047          get_zero_motion_factor(&cpi->common, &next_frame));
2048
2049    // Not all frames in the group are necessarily used in calculating boost.
2050    if ((i <= rc->max_gf_interval) ||
2051        ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) {
2052      const double frame_boost =
2053        calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST);
2054
2055      // How fast is prediction quality decaying.
2056      if (!detect_flash(twopass, 0)) {
2057        const double loop_decay_rate =
2058          get_prediction_decay_rate(&cpi->common, &next_frame);
2059        decay_accumulator *= loop_decay_rate;
2060        decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR);
2061        av_decay_accumulator += decay_accumulator;
2062        ++loop_decay_counter;
2063      }
2064      boost_score += (decay_accumulator * frame_boost);
2065    }
2066  }
2067  av_decay_accumulator /= (double)loop_decay_counter;
2068
2069  reset_fpf_position(twopass, start_position);
2070
2071  // Store the zero motion percentage
2072  twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2073
2074  // Calculate a section intra ratio used in setting max loop filter.
2075  twopass->section_intra_rating =
2076      calculate_section_intra_ratio(start_position, twopass->stats_in_end,
2077                                    rc->frames_to_key);
2078
2079  // Apply various clamps for min and max boost
2080  rc->kf_boost = (int)(av_decay_accumulator * boost_score);
2081  rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3));
2082  rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST);
2083
2084  // Work out how many bits to allocate for the key frame itself.
2085  kf_bits = calculate_boost_bits((rc->frames_to_key - 1),
2086                                  rc->kf_boost, twopass->kf_group_bits);
2087
2088  twopass->kf_group_bits -= kf_bits;
2089
2090  // Save the bits to spend on the key frame.
2091  gf_group->bit_allocation[0] = kf_bits;
2092  gf_group->update_type[0] = KF_UPDATE;
2093  gf_group->rf_level[0] = KF_STD;
2094
2095  // Note the total error score of the kf group minus the key frame itself.
2096  twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2097
2098  // Adjust the count of total modified error left.
2099  // The count of bits left is adjusted elsewhere based on real coded frame
2100  // sizes.
2101  twopass->modified_error_left -= kf_group_err;
2102}
2103
2104// For VBR...adjustment to the frame target based on error from previous frames
2105void vbr_rate_correction(int * this_frame_target,
2106                         const int64_t vbr_bits_off_target) {
2107  int max_delta = (*this_frame_target * 15) / 100;
2108
2109  // vbr_bits_off_target > 0 means we have extra bits to spend
2110  if (vbr_bits_off_target > 0) {
2111    *this_frame_target +=
2112      (vbr_bits_off_target > max_delta) ? max_delta
2113                                        : (int)vbr_bits_off_target;
2114  } else {
2115    *this_frame_target -=
2116      (vbr_bits_off_target < -max_delta) ? max_delta
2117                                         : (int)-vbr_bits_off_target;
2118  }
2119}
2120
2121// Define the reference buffers that will be updated post encode.
2122void configure_buffer_updates(VP9_COMP *cpi) {
2123  TWO_PASS *const twopass = &cpi->twopass;
2124
2125  cpi->rc.is_src_frame_alt_ref = 0;
2126  switch (twopass->gf_group.update_type[twopass->gf_group.index]) {
2127    case KF_UPDATE:
2128      cpi->refresh_last_frame = 1;
2129      cpi->refresh_golden_frame = 1;
2130      cpi->refresh_alt_ref_frame = 1;
2131      break;
2132    case LF_UPDATE:
2133      cpi->refresh_last_frame = 1;
2134      cpi->refresh_golden_frame = 0;
2135      cpi->refresh_alt_ref_frame = 0;
2136      break;
2137    case GF_UPDATE:
2138      cpi->refresh_last_frame = 1;
2139      cpi->refresh_golden_frame = 1;
2140      cpi->refresh_alt_ref_frame = 0;
2141      break;
2142    case OVERLAY_UPDATE:
2143      cpi->refresh_last_frame = 0;
2144      cpi->refresh_golden_frame = 1;
2145      cpi->refresh_alt_ref_frame = 0;
2146      cpi->rc.is_src_frame_alt_ref = 1;
2147      break;
2148    case ARF_UPDATE:
2149      cpi->refresh_last_frame = 0;
2150      cpi->refresh_golden_frame = 0;
2151      cpi->refresh_alt_ref_frame = 1;
2152      break;
2153    default:
2154      assert(0);
2155      break;
2156  }
2157  if (is_two_pass_svc(cpi)) {
2158    if (cpi->svc.temporal_layer_id > 0) {
2159      cpi->refresh_last_frame = 0;
2160      cpi->refresh_golden_frame = 0;
2161    }
2162    if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0)
2163      cpi->refresh_golden_frame = 0;
2164    if (cpi->alt_ref_source == NULL)
2165      cpi->refresh_alt_ref_frame = 0;
2166  }
2167}
2168
2169
2170void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2171  VP9_COMMON *const cm = &cpi->common;
2172  RATE_CONTROL *const rc = &cpi->rc;
2173  TWO_PASS *const twopass = &cpi->twopass;
2174  GF_GROUP *const gf_group = &twopass->gf_group;
2175  int frames_left;
2176  FIRSTPASS_STATS this_frame;
2177  FIRSTPASS_STATS this_frame_copy;
2178
2179  int target_rate;
2180  LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
2181        &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0;
2182
2183  if (lc != NULL) {
2184    frames_left = (int)(twopass->total_stats.count -
2185                  lc->current_video_frame_in_layer);
2186  } else {
2187    frames_left = (int)(twopass->total_stats.count -
2188                  cm->current_video_frame);
2189  }
2190
2191  if (!twopass->stats_in)
2192    return;
2193
2194  // If this is an arf frame then we dont want to read the stats file or
2195  // advance the input pointer as we already have what we need.
2196  if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2197    int target_rate;
2198    configure_buffer_updates(cpi);
2199    target_rate = gf_group->bit_allocation[gf_group->index];
2200    target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2201    rc->base_frame_target = target_rate;
2202
2203    // Correction to rate target based on prior over or under shoot.
2204    if (cpi->oxcf.rc_mode == VPX_VBR)
2205      vbr_rate_correction(&target_rate, rc->vbr_bits_off_target);
2206
2207    vp9_rc_set_frame_target(cpi, target_rate);
2208    cm->frame_type = INTER_FRAME;
2209
2210    if (lc != NULL) {
2211      if (cpi->svc.spatial_layer_id == 0) {
2212        lc->is_key_frame = 0;
2213      } else {
2214        lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2215
2216        if (lc->is_key_frame)
2217          cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2218      }
2219    }
2220
2221    return;
2222  }
2223
2224  vp9_clear_system_state();
2225
2226  if (cpi->oxcf.rc_mode == VPX_Q) {
2227    twopass->active_worst_quality = cpi->oxcf.cq_level;
2228  } else if (cm->current_video_frame == 0 ||
2229             (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2230    // Special case code for first frame.
2231    const int section_target_bandwidth = (int)(twopass->bits_left /
2232                                               frames_left);
2233    const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats,
2234                                                section_target_bandwidth);
2235    twopass->active_worst_quality = tmp_q;
2236    rc->ni_av_qi = tmp_q;
2237    rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
2238  }
2239  vp9_zero(this_frame);
2240  if (EOF == input_stats(twopass, &this_frame))
2241    return;
2242
2243  // Local copy of the current frame's first pass stats.
2244  this_frame_copy = this_frame;
2245
2246  // Keyframe and section processing.
2247  if (rc->frames_to_key == 0 ||
2248      (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2249    // Define next KF group and assign bits to it.
2250    find_next_key_frame(cpi, &this_frame_copy);
2251  } else {
2252    cm->frame_type = INTER_FRAME;
2253  }
2254
2255  if (lc != NULL) {
2256    if (cpi->svc.spatial_layer_id == 0) {
2257      lc->is_key_frame = (cm->frame_type == KEY_FRAME);
2258      if (lc->is_key_frame) {
2259        cpi->ref_frame_flags &=
2260            (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2261        lc->frames_from_key_frame = 0;
2262      }
2263    } else {
2264      cm->frame_type = INTER_FRAME;
2265      lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2266
2267      if (lc->is_key_frame) {
2268        cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2269        lc->frames_from_key_frame = 0;
2270      }
2271    }
2272  }
2273
2274  // Define a new GF/ARF group. (Should always enter here for key frames).
2275  if (rc->frames_till_gf_update_due == 0) {
2276    define_gf_group(cpi, &this_frame_copy);
2277
2278    if (twopass->gf_zeromotion_pct > 995) {
2279      // As long as max_thresh for encode breakout is small enough, it is ok
2280      // to enable it for show frame, i.e. set allow_encode_breakout to
2281      // ENCODE_BREAKOUT_LIMITED.
2282      if (!cm->show_frame)
2283        cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED;
2284      else
2285        cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED;
2286    }
2287
2288    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2289    if (lc != NULL)
2290      cpi->refresh_golden_frame = 1;
2291
2292#if ARF_STATS_OUTPUT
2293    {
2294      FILE *fpfile;
2295      fpfile = fopen("arf.stt", "a");
2296      ++arf_count;
2297      fprintf(fpfile, "%10d %10d %10d %10ld\n",
2298              cm->current_video_frame, rc->kf_boost, arf_count, rc->gfu_boost);
2299
2300      fclose(fpfile);
2301    }
2302#endif
2303  }
2304
2305  configure_buffer_updates(cpi);
2306
2307  target_rate = gf_group->bit_allocation[gf_group->index];
2308  if (cpi->common.frame_type == KEY_FRAME)
2309    target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2310  else
2311    target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2312
2313  rc->base_frame_target = target_rate;
2314
2315  // Correction to rate target based on prior over or under shoot.
2316  if (cpi->oxcf.rc_mode == VPX_VBR)
2317    vbr_rate_correction(&target_rate, rc->vbr_bits_off_target);
2318
2319  vp9_rc_set_frame_target(cpi, target_rate);
2320
2321  // Update the total stats remaining structure.
2322  subtract_stats(&twopass->total_left_stats, &this_frame);
2323}
2324
2325void vp9_twopass_postencode_update(VP9_COMP *cpi) {
2326  TWO_PASS *const twopass = &cpi->twopass;
2327  RATE_CONTROL *const rc = &cpi->rc;
2328
2329  // VBR correction is done through rc->vbr_bits_off_target. Based on the
2330  // sign of this value, a limited % adjustment is made to the target rate
2331  // of subsequent frames, to try and push it back towards 0. This method
2332  // is designed to prevent extreme behaviour at the end of a clip
2333  // or group of frames.
2334  const int bits_used = rc->base_frame_target;
2335  rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
2336
2337  twopass->bits_left = MAX(twopass->bits_left - bits_used, 0);
2338
2339  if (cpi->common.frame_type != KEY_FRAME &&
2340      !vp9_is_upper_layer_key_frame(cpi)) {
2341    twopass->kf_group_bits -= bits_used;
2342    twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
2343  }
2344  twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0);
2345
2346  // Increment the gf group index ready for the next frame.
2347  ++twopass->gf_group.index;
2348}
2349