vp9_firstpass.c revision 7bc9febe8749e98a3812a0dc4380ceae75c29450
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_dsp_rtcd.h"
16#include "./vpx_scale_rtcd.h"
17
18#include "vpx_dsp/vpx_dsp_common.h"
19#include "vpx_mem/vpx_mem.h"
20#include "vpx_ports/mem.h"
21#include "vpx_ports/system_state.h"
22#include "vpx_scale/vpx_scale.h"
23#include "vpx_scale/yv12config.h"
24
25#include "vp9/common/vp9_entropymv.h"
26#include "vp9/common/vp9_quant_common.h"
27#include "vp9/common/vp9_reconinter.h"  // vp9_setup_dst_planes()
28#include "vp9/encoder/vp9_aq_variance.h"
29#include "vp9/encoder/vp9_block.h"
30#include "vp9/encoder/vp9_encodeframe.h"
31#include "vp9/encoder/vp9_encodemb.h"
32#include "vp9/encoder/vp9_encodemv.h"
33#include "vp9/encoder/vp9_encoder.h"
34#include "vp9/encoder/vp9_extend.h"
35#include "vp9/encoder/vp9_firstpass.h"
36#include "vp9/encoder/vp9_mcomp.h"
37#include "vp9/encoder/vp9_quantize.h"
38#include "vp9/encoder/vp9_rd.h"
39#include "vpx_dsp/variance.h"
40
41#define OUTPUT_FPF 0
42#define ARF_STATS_OUTPUT 0
43
44#define BOOST_BREAKOUT 12.5
45#define BOOST_FACTOR 12.5
46#define FACTOR_PT_LOW 0.70
47#define FACTOR_PT_HIGH 0.90
48#define FIRST_PASS_Q 10.0
49#define GF_MAX_BOOST 96.0
50#define INTRA_MODE_PENALTY 1024
51#define MIN_ARF_GF_BOOST 240
52#define MIN_DECAY_FACTOR 0.01
53#define NEW_MV_MODE_PENALTY 32
54#define SVC_FACTOR_PT_LOW 0.45
55#define DARK_THRESH 64
56#define DEFAULT_GRP_WEIGHT 1.0
57#define RC_FACTOR_MIN 0.75
58#define RC_FACTOR_MAX 1.75
59#define SECTION_NOISE_DEF 250.0
60#define LOW_I_THRESH 24000
61
62#define NCOUNT_INTRA_THRESH 8192
63#define NCOUNT_INTRA_FACTOR 3
64
65#define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x)-0.000001 : (x) + 0.000001)
66
67#if ARF_STATS_OUTPUT
68unsigned int arf_count = 0;
69#endif
70
71// Resets the first pass file to the given position using a relative seek from
72// the current position.
73static void reset_fpf_position(TWO_PASS *p, const FIRSTPASS_STATS *position) {
74  p->stats_in = position;
75}
76
77// Read frame stats at an offset from the current position.
78static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) {
79  if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) ||
80      (offset < 0 && p->stats_in + offset < p->stats_in_start)) {
81    return NULL;
82  }
83
84  return &p->stats_in[offset];
85}
86
87static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) {
88  if (p->stats_in >= p->stats_in_end) return EOF;
89
90  *fps = *p->stats_in;
91  ++p->stats_in;
92  return 1;
93}
94
95static void output_stats(FIRSTPASS_STATS *stats,
96                         struct vpx_codec_pkt_list *pktlist) {
97  struct vpx_codec_cx_pkt pkt;
98  pkt.kind = VPX_CODEC_STATS_PKT;
99  pkt.data.twopass_stats.buf = stats;
100  pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
101  vpx_codec_pkt_list_add(pktlist, &pkt);
102
103// TEMP debug code
104#if OUTPUT_FPF
105  {
106    FILE *fpfile;
107    fpfile = fopen("firstpass.stt", "a");
108
109    fprintf(fpfile,
110            "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.0lf %12.4lf"
111            "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf"
112            "%12.4lf %12.4lf %12.4lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf"
113            "\n",
114            stats->frame, stats->weight, stats->intra_error, stats->coded_error,
115            stats->sr_coded_error, stats->frame_noise_energy, stats->pcnt_inter,
116            stats->pcnt_motion, stats->pcnt_second_ref, stats->pcnt_neutral,
117            stats->intra_skip_pct, stats->intra_smooth_pct,
118            stats->inactive_zone_rows, stats->inactive_zone_cols, stats->MVr,
119            stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv,
120            stats->MVcv, stats->mv_in_out_count, stats->count, stats->duration);
121    fclose(fpfile);
122  }
123#endif
124}
125
126#if CONFIG_FP_MB_STATS
127static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm,
128                              struct vpx_codec_pkt_list *pktlist) {
129  struct vpx_codec_cx_pkt pkt;
130  pkt.kind = VPX_CODEC_FPMB_STATS_PKT;
131  pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats;
132  pkt.data.firstpass_mb_stats.sz = cm->initial_mbs * sizeof(uint8_t);
133  vpx_codec_pkt_list_add(pktlist, &pkt);
134}
135#endif
136
137static void zero_stats(FIRSTPASS_STATS *section) {
138  section->frame = 0.0;
139  section->weight = 0.0;
140  section->intra_error = 0.0;
141  section->coded_error = 0.0;
142  section->sr_coded_error = 0.0;
143  section->frame_noise_energy = 0.0;
144  section->pcnt_inter = 0.0;
145  section->pcnt_motion = 0.0;
146  section->pcnt_second_ref = 0.0;
147  section->pcnt_neutral = 0.0;
148  section->intra_skip_pct = 0.0;
149  section->intra_smooth_pct = 0.0;
150  section->inactive_zone_rows = 0.0;
151  section->inactive_zone_cols = 0.0;
152  section->MVr = 0.0;
153  section->mvr_abs = 0.0;
154  section->MVc = 0.0;
155  section->mvc_abs = 0.0;
156  section->MVrv = 0.0;
157  section->MVcv = 0.0;
158  section->mv_in_out_count = 0.0;
159  section->count = 0.0;
160  section->duration = 1.0;
161  section->spatial_layer_id = 0;
162}
163
164static void accumulate_stats(FIRSTPASS_STATS *section,
165                             const FIRSTPASS_STATS *frame) {
166  section->frame += frame->frame;
167  section->weight += frame->weight;
168  section->spatial_layer_id = frame->spatial_layer_id;
169  section->intra_error += frame->intra_error;
170  section->coded_error += frame->coded_error;
171  section->sr_coded_error += frame->sr_coded_error;
172  section->frame_noise_energy += frame->frame_noise_energy;
173  section->pcnt_inter += frame->pcnt_inter;
174  section->pcnt_motion += frame->pcnt_motion;
175  section->pcnt_second_ref += frame->pcnt_second_ref;
176  section->pcnt_neutral += frame->pcnt_neutral;
177  section->intra_skip_pct += frame->intra_skip_pct;
178  section->intra_smooth_pct += frame->intra_smooth_pct;
179  section->inactive_zone_rows += frame->inactive_zone_rows;
180  section->inactive_zone_cols += frame->inactive_zone_cols;
181  section->MVr += frame->MVr;
182  section->mvr_abs += frame->mvr_abs;
183  section->MVc += frame->MVc;
184  section->mvc_abs += frame->mvc_abs;
185  section->MVrv += frame->MVrv;
186  section->MVcv += frame->MVcv;
187  section->mv_in_out_count += frame->mv_in_out_count;
188  section->count += frame->count;
189  section->duration += frame->duration;
190}
191
192static void subtract_stats(FIRSTPASS_STATS *section,
193                           const FIRSTPASS_STATS *frame) {
194  section->frame -= frame->frame;
195  section->weight -= frame->weight;
196  section->intra_error -= frame->intra_error;
197  section->coded_error -= frame->coded_error;
198  section->sr_coded_error -= frame->sr_coded_error;
199  section->frame_noise_energy -= frame->frame_noise_energy;
200  section->pcnt_inter -= frame->pcnt_inter;
201  section->pcnt_motion -= frame->pcnt_motion;
202  section->pcnt_second_ref -= frame->pcnt_second_ref;
203  section->pcnt_neutral -= frame->pcnt_neutral;
204  section->intra_skip_pct -= frame->intra_skip_pct;
205  section->intra_smooth_pct -= frame->intra_smooth_pct;
206  section->inactive_zone_rows -= frame->inactive_zone_rows;
207  section->inactive_zone_cols -= frame->inactive_zone_cols;
208  section->MVr -= frame->MVr;
209  section->mvr_abs -= frame->mvr_abs;
210  section->MVc -= frame->MVc;
211  section->mvc_abs -= frame->mvc_abs;
212  section->MVrv -= frame->MVrv;
213  section->MVcv -= frame->MVcv;
214  section->mv_in_out_count -= frame->mv_in_out_count;
215  section->count -= frame->count;
216  section->duration -= frame->duration;
217}
218
219// Calculate an active area of the image that discounts formatting
220// bars and partially discounts other 0 energy areas.
221#define MIN_ACTIVE_AREA 0.5
222#define MAX_ACTIVE_AREA 1.0
223static double calculate_active_area(const VP9_COMP *cpi,
224                                    const FIRSTPASS_STATS *this_frame) {
225  double active_pct;
226
227  active_pct =
228      1.0 -
229      ((this_frame->intra_skip_pct / 2) +
230       ((this_frame->inactive_zone_rows * 2) / (double)cpi->common.mb_rows));
231  return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA);
232}
233
234// Calculate a modified Error used in distributing bits between easier and
235// harder frames.
236#define ACT_AREA_CORRECTION 0.5
237static double calculate_modified_err(const VP9_COMP *cpi,
238                                     const TWO_PASS *twopass,
239                                     const VP9EncoderConfig *oxcf,
240                                     const FIRSTPASS_STATS *this_frame) {
241  const FIRSTPASS_STATS *const stats = &twopass->total_stats;
242  const double av_weight = stats->weight / stats->count;
243  const double av_err = (stats->coded_error * av_weight) / stats->count;
244  double modified_error =
245      av_err * pow(this_frame->coded_error * this_frame->weight /
246                       DOUBLE_DIVIDE_CHECK(av_err),
247                   oxcf->two_pass_vbrbias / 100.0);
248
249  // Correction for active area. Frames with a reduced active area
250  // (eg due to formatting bars) have a higher error per mb for the
251  // remaining active MBs. The correction here assumes that coding
252  // 0.5N blocks of complexity 2X is a little easier than coding N
253  // blocks of complexity X.
254  modified_error *=
255      pow(calculate_active_area(cpi, this_frame), ACT_AREA_CORRECTION);
256
257  return fclamp(modified_error, twopass->modified_error_min,
258                twopass->modified_error_max);
259}
260
261// This function returns the maximum target rate per frame.
262static int frame_max_bits(const RATE_CONTROL *rc,
263                          const VP9EncoderConfig *oxcf) {
264  int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
265                      (int64_t)oxcf->two_pass_vbrmax_section) /
266                     100;
267  if (max_bits < 0)
268    max_bits = 0;
269  else if (max_bits > rc->max_frame_bandwidth)
270    max_bits = rc->max_frame_bandwidth;
271
272  return (int)max_bits;
273}
274
275void vp9_init_first_pass(VP9_COMP *cpi) {
276  zero_stats(&cpi->twopass.total_stats);
277}
278
279void vp9_end_first_pass(VP9_COMP *cpi) {
280  if (is_two_pass_svc(cpi)) {
281    int i;
282    for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
283      output_stats(&cpi->svc.layer_context[i].twopass.total_stats,
284                   cpi->output_pkt_list);
285    }
286  } else {
287    output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
288  }
289}
290
291static vpx_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
292  switch (bsize) {
293    case BLOCK_8X8: return vpx_mse8x8;
294    case BLOCK_16X8: return vpx_mse16x8;
295    case BLOCK_8X16: return vpx_mse8x16;
296    default: return vpx_mse16x16;
297  }
298}
299
300static unsigned int get_prediction_error(BLOCK_SIZE bsize,
301                                         const struct buf_2d *src,
302                                         const struct buf_2d *ref) {
303  unsigned int sse;
304  const vpx_variance_fn_t fn = get_block_variance_fn(bsize);
305  fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
306  return sse;
307}
308
309#if CONFIG_VP9_HIGHBITDEPTH
310static vpx_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize,
311                                                      int bd) {
312  switch (bd) {
313    default:
314      switch (bsize) {
315        case BLOCK_8X8: return vpx_highbd_8_mse8x8;
316        case BLOCK_16X8: return vpx_highbd_8_mse16x8;
317        case BLOCK_8X16: return vpx_highbd_8_mse8x16;
318        default: return vpx_highbd_8_mse16x16;
319      }
320      break;
321    case 10:
322      switch (bsize) {
323        case BLOCK_8X8: return vpx_highbd_10_mse8x8;
324        case BLOCK_16X8: return vpx_highbd_10_mse16x8;
325        case BLOCK_8X16: return vpx_highbd_10_mse8x16;
326        default: return vpx_highbd_10_mse16x16;
327      }
328      break;
329    case 12:
330      switch (bsize) {
331        case BLOCK_8X8: return vpx_highbd_12_mse8x8;
332        case BLOCK_16X8: return vpx_highbd_12_mse16x8;
333        case BLOCK_8X16: return vpx_highbd_12_mse8x16;
334        default: return vpx_highbd_12_mse16x16;
335      }
336      break;
337  }
338}
339
340static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize,
341                                                const struct buf_2d *src,
342                                                const struct buf_2d *ref,
343                                                int bd) {
344  unsigned int sse;
345  const vpx_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
346  fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
347  return sse;
348}
349#endif  // CONFIG_VP9_HIGHBITDEPTH
350
351// Refine the motion search range according to the frame dimension
352// for first pass test.
353static int get_search_range(const VP9_COMP *cpi) {
354  int sr = 0;
355  const int dim = VPXMIN(cpi->initial_width, cpi->initial_height);
356
357  while ((dim << sr) < MAX_FULL_PEL_VAL) ++sr;
358  return sr;
359}
360
361static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
362                                     const MV *ref_mv, MV *best_mv,
363                                     int *best_motion_err) {
364  MACROBLOCKD *const xd = &x->e_mbd;
365  MV tmp_mv = { 0, 0 };
366  MV ref_mv_full = { ref_mv->row >> 3, ref_mv->col >> 3 };
367  int num00, tmp_err, n;
368  const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
369  vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
370  const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
371
372  int step_param = 3;
373  int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
374  const int sr = get_search_range(cpi);
375  step_param += sr;
376  further_steps -= sr;
377
378  // Override the default variance function to use MSE.
379  v_fn_ptr.vf = get_block_variance_fn(bsize);
380#if CONFIG_VP9_HIGHBITDEPTH
381  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
382    v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd);
383  }
384#endif  // CONFIG_VP9_HIGHBITDEPTH
385
386  // Center the initial step/diamond search on best mv.
387  tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
388                                    step_param, x->sadperbit16, &num00,
389                                    &v_fn_ptr, ref_mv);
390  if (tmp_err < INT_MAX)
391    tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
392  if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
393
394  if (tmp_err < *best_motion_err) {
395    *best_motion_err = tmp_err;
396    *best_mv = tmp_mv;
397  }
398
399  // Carry out further step/diamond searches as necessary.
400  n = num00;
401  num00 = 0;
402
403  while (n < further_steps) {
404    ++n;
405
406    if (num00) {
407      --num00;
408    } else {
409      tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
410                                        step_param + n, x->sadperbit16, &num00,
411                                        &v_fn_ptr, ref_mv);
412      if (tmp_err < INT_MAX)
413        tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
414      if (tmp_err < INT_MAX - new_mv_mode_penalty)
415        tmp_err += new_mv_mode_penalty;
416
417      if (tmp_err < *best_motion_err) {
418        *best_motion_err = tmp_err;
419        *best_mv = tmp_mv;
420      }
421    }
422  }
423}
424
425static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
426  if (2 * mb_col + 1 < cm->mi_cols) {
427    return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 : BLOCK_16X8;
428  } else {
429    return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 : BLOCK_8X8;
430  }
431}
432
433static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
434  int i;
435
436  for (i = 0; i < QINDEX_RANGE; ++i)
437    if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q) break;
438
439  if (i == QINDEX_RANGE) i--;
440
441  return i;
442}
443
444static void set_first_pass_params(VP9_COMP *cpi) {
445  VP9_COMMON *const cm = &cpi->common;
446  if (!cpi->refresh_alt_ref_frame &&
447      (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY))) {
448    cm->frame_type = KEY_FRAME;
449  } else {
450    cm->frame_type = INTER_FRAME;
451  }
452  // Do not use periodic key frames.
453  cpi->rc.frames_to_key = INT_MAX;
454}
455
456// Scale an sse threshold to account for 8/10/12 bit.
457static int scale_sse_threshold(VP9_COMMON *cm, int thresh) {
458  int ret_val = thresh;
459#if CONFIG_VP9_HIGHBITDEPTH
460  if (cm->use_highbitdepth) {
461    switch (cm->bit_depth) {
462      case VPX_BITS_8: ret_val = thresh; break;
463      case VPX_BITS_10: ret_val = thresh >> 4; break;
464      case VPX_BITS_12: ret_val = thresh >> 8; break;
465      default:
466        assert(0 &&
467               "cm->bit_depth should be VPX_BITS_8, "
468               "VPX_BITS_10 or VPX_BITS_12");
469    }
470  }
471#else
472  (void)cm;
473#endif  // CONFIG_VP9_HIGHBITDEPTH
474  return ret_val;
475}
476
477// This threshold is used to track blocks where to all intents and purposes
478// the intra prediction error 0. Though the metric we test against
479// is technically a sse we are mainly interested in blocks where all the pixels
480// in the 8 bit domain have an error of <= 1 (where error = sse) so a
481// linear scaling for 10 and 12 bit gives similar results.
482#define UL_INTRA_THRESH 50
483static int get_ul_intra_threshold(VP9_COMMON *cm) {
484  int ret_val = UL_INTRA_THRESH;
485#if CONFIG_VP9_HIGHBITDEPTH
486  if (cm->use_highbitdepth) {
487    switch (cm->bit_depth) {
488      case VPX_BITS_8: ret_val = UL_INTRA_THRESH; break;
489      case VPX_BITS_10: ret_val = UL_INTRA_THRESH << 2; break;
490      case VPX_BITS_12: ret_val = UL_INTRA_THRESH << 4; break;
491      default:
492        assert(0 &&
493               "cm->bit_depth should be VPX_BITS_8, "
494               "VPX_BITS_10 or VPX_BITS_12");
495    }
496  }
497#else
498  (void)cm;
499#endif  // CONFIG_VP9_HIGHBITDEPTH
500  return ret_val;
501}
502
503#define SMOOTH_INTRA_THRESH 4000
504static int get_smooth_intra_threshold(VP9_COMMON *cm) {
505  int ret_val = SMOOTH_INTRA_THRESH;
506#if CONFIG_VP9_HIGHBITDEPTH
507  if (cm->use_highbitdepth) {
508    switch (cm->bit_depth) {
509      case VPX_BITS_8: ret_val = SMOOTH_INTRA_THRESH; break;
510      case VPX_BITS_10: ret_val = SMOOTH_INTRA_THRESH << 4; break;
511      case VPX_BITS_12: ret_val = SMOOTH_INTRA_THRESH << 8; break;
512      default:
513        assert(0 &&
514               "cm->bit_depth should be VPX_BITS_8, "
515               "VPX_BITS_10 or VPX_BITS_12");
516    }
517  }
518#else
519  (void)cm;
520#endif  // CONFIG_VP9_HIGHBITDEPTH
521  return ret_val;
522}
523
524#define FP_DN_THRESH 8
525#define FP_MAX_DN_THRESH 16
526#define KERNEL_SIZE 3
527
528// Baseline Kernal weights for first pass noise metric
529static uint8_t fp_dn_kernal_3[KERNEL_SIZE * KERNEL_SIZE] = { 1, 2, 1, 2, 4,
530                                                             2, 1, 2, 1 };
531
532// Estimate noise at a single point based on the impace of a spatial kernal
533// on the point value
534static int fp_estimate_point_noise(uint8_t *src_ptr, const int stride) {
535  int sum_weight = 0;
536  int sum_val = 0;
537  int i, j;
538  int max_diff = 0;
539  int diff;
540  int dn_diff;
541  uint8_t *tmp_ptr;
542  uint8_t *kernal_ptr;
543  uint8_t dn_val;
544  uint8_t centre_val = *src_ptr;
545
546  kernal_ptr = fp_dn_kernal_3;
547
548  // Apply the kernal
549  tmp_ptr = src_ptr - stride - 1;
550  for (i = 0; i < KERNEL_SIZE; ++i) {
551    for (j = 0; j < KERNEL_SIZE; ++j) {
552      diff = abs((int)centre_val - (int)tmp_ptr[j]);
553      max_diff = VPXMAX(max_diff, diff);
554      if (diff <= FP_DN_THRESH) {
555        sum_weight += *kernal_ptr;
556        sum_val += (int)tmp_ptr[j] * (int)*kernal_ptr;
557      }
558      ++kernal_ptr;
559    }
560    tmp_ptr += stride;
561  }
562
563  if (max_diff < FP_MAX_DN_THRESH)
564    // Update the source value with the new filtered value
565    dn_val = (sum_val + (sum_weight >> 1)) / sum_weight;
566  else
567    dn_val = *src_ptr;
568
569  // return the noise energy as the square of the difference between the
570  // denoised and raw value.
571  dn_diff = (int)*src_ptr - (int)dn_val;
572  return dn_diff * dn_diff;
573}
574#if CONFIG_VP9_HIGHBITDEPTH
575static int fp_highbd_estimate_point_noise(uint8_t *src_ptr, const int stride) {
576  int sum_weight = 0;
577  int sum_val = 0;
578  int i, j;
579  int max_diff = 0;
580  int diff;
581  int dn_diff;
582  uint8_t *tmp_ptr;
583  uint16_t *tmp_ptr16;
584  uint8_t *kernal_ptr;
585  uint16_t dn_val;
586  uint16_t centre_val = *CONVERT_TO_SHORTPTR(src_ptr);
587
588  kernal_ptr = fp_dn_kernal_3;
589
590  // Apply the kernal
591  tmp_ptr = src_ptr - stride - 1;
592  for (i = 0; i < KERNEL_SIZE; ++i) {
593    tmp_ptr16 = CONVERT_TO_SHORTPTR(tmp_ptr);
594    for (j = 0; j < KERNEL_SIZE; ++j) {
595      diff = abs((int)centre_val - (int)tmp_ptr16[j]);
596      max_diff = VPXMAX(max_diff, diff);
597      if (diff <= FP_DN_THRESH) {
598        sum_weight += *kernal_ptr;
599        sum_val += (int)tmp_ptr16[j] * (int)*kernal_ptr;
600      }
601      ++kernal_ptr;
602    }
603    tmp_ptr += stride;
604  }
605
606  if (max_diff < FP_MAX_DN_THRESH)
607    // Update the source value with the new filtered value
608    dn_val = (sum_val + (sum_weight >> 1)) / sum_weight;
609  else
610    dn_val = *CONVERT_TO_SHORTPTR(src_ptr);
611
612  // return the noise energy as the square of the difference between the
613  // denoised and raw value.
614  dn_diff = (int)(*CONVERT_TO_SHORTPTR(src_ptr)) - (int)dn_val;
615  return dn_diff * dn_diff;
616}
617#endif
618
619// Estimate noise for a block.
620static int fp_estimate_block_noise(MACROBLOCK *x, BLOCK_SIZE bsize) {
621#if CONFIG_VP9_HIGHBITDEPTH
622  MACROBLOCKD *xd = &x->e_mbd;
623#endif
624  uint8_t *src_ptr = &x->plane[0].src.buf[0];
625  const int width = num_4x4_blocks_wide_lookup[bsize] * 4;
626  const int height = num_4x4_blocks_high_lookup[bsize] * 4;
627  int w, h;
628  int stride = x->plane[0].src.stride;
629  int block_noise = 0;
630
631  // Sampled points to reduce cost overhead.
632  for (h = 0; h < height; h += 2) {
633    for (w = 0; w < width; w += 2) {
634#if CONFIG_VP9_HIGHBITDEPTH
635      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
636        block_noise += fp_highbd_estimate_point_noise(src_ptr, stride);
637      else
638        block_noise += fp_estimate_point_noise(src_ptr, stride);
639#else
640      block_noise += fp_estimate_point_noise(src_ptr, stride);
641#endif
642      ++src_ptr;
643    }
644    src_ptr += (stride - width);
645  }
646  return block_noise << 2;  // Scale << 2 to account for sampling.
647}
648
649#define INVALID_ROW -1
650void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
651  int mb_row, mb_col;
652  MACROBLOCK *const x = &cpi->td.mb;
653  VP9_COMMON *const cm = &cpi->common;
654  MACROBLOCKD *const xd = &x->e_mbd;
655  TileInfo tile;
656  struct macroblock_plane *const p = x->plane;
657  struct macroblockd_plane *const pd = xd->plane;
658  const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
659  int i;
660
661  int recon_yoffset, recon_uvoffset;
662  int64_t intra_error = 0;
663  int64_t coded_error = 0;
664  int64_t sr_coded_error = 0;
665  int64_t frame_noise_energy = 0;
666
667  int sum_mvr = 0, sum_mvc = 0;
668  int sum_mvr_abs = 0, sum_mvc_abs = 0;
669  int64_t sum_mvrs = 0, sum_mvcs = 0;
670  int mvcount = 0;
671  int intercount = 0;
672  int second_ref_count = 0;
673  const int intrapenalty = INTRA_MODE_PENALTY;
674  double neutral_count;
675  int intra_skip_count = 0;
676  int intra_smooth_count = 0;
677  int image_data_start_row = INVALID_ROW;
678  int sum_in_vectors = 0;
679  TWO_PASS *twopass = &cpi->twopass;
680  const MV zero_mv = { 0, 0 };
681  int recon_y_stride, recon_uv_stride, uv_mb_height;
682
683  YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
684  YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
685  YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
686  const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
687
688  LAYER_CONTEXT *const lc =
689      is_two_pass_svc(cpi) ? &cpi->svc.layer_context[cpi->svc.spatial_layer_id]
690                           : NULL;
691  double intra_factor;
692  double brightness_factor;
693  BufferPool *const pool = cm->buffer_pool;
694  MODE_INFO mi_above, mi_left;
695
696  // First pass code requires valid last and new frame buffers.
697  assert(new_yv12 != NULL);
698  assert((lc != NULL) || frame_is_intra_only(cm) || (lst_yv12 != NULL));
699
700#if CONFIG_FP_MB_STATS
701  if (cpi->use_fp_mb_stats) {
702    vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs);
703  }
704#endif
705
706  vpx_clear_system_state();
707
708  intra_factor = 0.0;
709  brightness_factor = 0.0;
710  neutral_count = 0.0;
711
712  set_first_pass_params(cpi);
713  vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
714
715  if (lc != NULL) {
716    twopass = &lc->twopass;
717
718    cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
719    cpi->ref_frame_flags = VP9_LAST_FLAG;
720
721    if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id <
722        REF_FRAMES) {
723      cpi->gld_fb_idx =
724          cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id;
725      cpi->ref_frame_flags |= VP9_GOLD_FLAG;
726      cpi->refresh_golden_frame = (lc->current_video_frame_in_layer == 0);
727    } else {
728      cpi->refresh_golden_frame = 0;
729    }
730
731    if (lc->current_video_frame_in_layer == 0) cpi->ref_frame_flags = 0;
732
733    vp9_scale_references(cpi);
734
735    // Use either last frame or alt frame for motion search.
736    if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
737      first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
738      if (first_ref_buf == NULL)
739        first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME);
740    }
741
742    if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
743      gld_yv12 = vp9_get_scaled_ref_frame(cpi, GOLDEN_FRAME);
744      if (gld_yv12 == NULL) {
745        gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
746      }
747    } else {
748      gld_yv12 = NULL;
749    }
750
751    set_ref_ptrs(cm, xd,
752                 (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME : NONE,
753                 (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE);
754
755    cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
756                                        &cpi->scaled_source, 0);
757  }
758
759  vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
760
761  vp9_setup_src_planes(x, cpi->Source, 0, 0);
762  vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
763
764  if (!frame_is_intra_only(cm)) {
765    vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
766  }
767
768  xd->mi = cm->mi_grid_visible;
769  xd->mi[0] = cm->mi;
770
771  vp9_frame_init_quantizer(cpi);
772
773  for (i = 0; i < MAX_MB_PLANE; ++i) {
774    p[i].coeff = ctx->coeff_pbuf[i][1];
775    p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
776    pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
777    p[i].eobs = ctx->eobs_pbuf[i][1];
778  }
779  x->skip_recode = 0;
780
781  vp9_init_mv_probs(cm);
782  vp9_initialize_rd_consts(cpi);
783
784  // Tiling is ignored in the first pass.
785  vp9_tile_init(&tile, cm, 0, 0);
786
787  recon_y_stride = new_yv12->y_stride;
788  recon_uv_stride = new_yv12->uv_stride;
789  uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
790
791  for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
792    MV best_ref_mv = { 0, 0 };
793
794    // Reset above block coeffs.
795    recon_yoffset = (mb_row * recon_y_stride * 16);
796    recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height);
797
798    // Set up limit values for motion vectors to prevent them extending
799    // outside the UMV borders.
800    x->mv_limits.row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
801    x->mv_limits.row_max =
802        ((cm->mb_rows - 1 - mb_row) * 16) + BORDER_MV_PIXELS_B16;
803
804    for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
805      int this_error;
806      int this_intra_error;
807      const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
808      const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
809      double log_intra;
810      int level_sample;
811
812#if CONFIG_FP_MB_STATS
813      const int mb_index = mb_row * cm->mb_cols + mb_col;
814#endif
815
816      vpx_clear_system_state();
817
818      xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
819      xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
820      xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
821      xd->mi[0]->sb_type = bsize;
822      xd->mi[0]->ref_frame[0] = INTRA_FRAME;
823      set_mi_row_col(xd, &tile, mb_row << 1, num_8x8_blocks_high_lookup[bsize],
824                     mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
825                     cm->mi_rows, cm->mi_cols);
826      // Are edges available for intra prediction?
827      // Since the firstpass does not populate the mi_grid_visible,
828      // above_mi/left_mi must be overwritten with a nonzero value when edges
829      // are available.  Required by vp9_predict_intra_block().
830      xd->above_mi = (mb_row != 0) ? &mi_above : NULL;
831      xd->left_mi = (mb_col > tile.mi_col_start) ? &mi_left : NULL;
832
833      // Do intra 16x16 prediction.
834      x->skip_encode = 0;
835      xd->mi[0]->mode = DC_PRED;
836      xd->mi[0]->tx_size =
837          use_dc_pred ? (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
838
839      // Set the 16x16 src_diff block to zero, which ensures correct this_error
840      // calculation for block sizes smaller than 16x16.
841      vp9_zero_array(x->plane[0].src_diff, 256);
842      vp9_encode_intra_block_plane(x, bsize, 0, 0);
843      this_error = vpx_get_mb_ss(x->plane[0].src_diff);
844      this_intra_error = this_error;
845
846      // Keep a record of blocks that have very low intra error residual
847      // (i.e. are in effect completely flat and untextured in the intra
848      // domain). In natural videos this is uncommon, but it is much more
849      // common in animations, graphics and screen content, so may be used
850      // as a signal to detect these types of content.
851      if (this_error < get_ul_intra_threshold(cm)) {
852        ++intra_skip_count;
853      } else if ((mb_col > 0) && (image_data_start_row == INVALID_ROW)) {
854        image_data_start_row = mb_row;
855      }
856
857      // Blocks that are mainly smooth in the intra domain.
858      // Some special accounting for CQ but also these are better for testing
859      // noise levels.
860      if (this_error < get_smooth_intra_threshold(cm)) {
861        ++intra_smooth_count;
862      }
863
864      // Special case noise measurement for first frame.
865      if (cm->current_video_frame == 0) {
866        if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH)) {
867          frame_noise_energy += fp_estimate_block_noise(x, bsize);
868        } else {
869          frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
870        }
871      }
872
873#if CONFIG_VP9_HIGHBITDEPTH
874      if (cm->use_highbitdepth) {
875        switch (cm->bit_depth) {
876          case VPX_BITS_8: break;
877          case VPX_BITS_10: this_error >>= 4; break;
878          case VPX_BITS_12: this_error >>= 8; break;
879          default:
880            assert(0 &&
881                   "cm->bit_depth should be VPX_BITS_8, "
882                   "VPX_BITS_10 or VPX_BITS_12");
883            return;
884        }
885      }
886#endif  // CONFIG_VP9_HIGHBITDEPTH
887
888      vpx_clear_system_state();
889      log_intra = log(this_error + 1.0);
890      if (log_intra < 10.0)
891        intra_factor += 1.0 + ((10.0 - log_intra) * 0.05);
892      else
893        intra_factor += 1.0;
894
895#if CONFIG_VP9_HIGHBITDEPTH
896      if (cm->use_highbitdepth)
897        level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0];
898      else
899        level_sample = x->plane[0].src.buf[0];
900#else
901      level_sample = x->plane[0].src.buf[0];
902#endif
903      if ((level_sample < DARK_THRESH) && (log_intra < 9.0))
904        brightness_factor += 1.0 + (0.01 * (DARK_THRESH - level_sample));
905      else
906        brightness_factor += 1.0;
907
908      // Intrapenalty below deals with situations where the intra and inter
909      // error scores are very low (e.g. a plain black frame).
910      // We do not have special cases in first pass for 0,0 and nearest etc so
911      // all inter modes carry an overhead cost estimate for the mv.
912      // When the error score is very low this causes us to pick all or lots of
913      // INTRA modes and throw lots of key frames.
914      // This penalty adds a cost matching that of a 0,0 mv to the intra case.
915      this_error += intrapenalty;
916
917      // Accumulate the intra error.
918      intra_error += (int64_t)this_error;
919
920#if CONFIG_FP_MB_STATS
921      if (cpi->use_fp_mb_stats) {
922        // initialization
923        cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
924      }
925#endif
926
927      // Set up limit values for motion vectors to prevent them extending
928      // outside the UMV borders.
929      x->mv_limits.col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
930      x->mv_limits.col_max =
931          ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
932
933      // Other than for the first frame do a motion search.
934      if ((lc == NULL && cm->current_video_frame > 0) ||
935          (lc != NULL && lc->current_video_frame_in_layer > 0)) {
936        int tmp_err, motion_error, raw_motion_error;
937        // Assume 0,0 motion with no mv overhead.
938        MV mv = { 0, 0 }, tmp_mv = { 0, 0 };
939        struct buf_2d unscaled_last_source_buf_2d;
940
941        xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
942#if CONFIG_VP9_HIGHBITDEPTH
943        if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
944          motion_error = highbd_get_prediction_error(
945              bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
946        } else {
947          motion_error = get_prediction_error(bsize, &x->plane[0].src,
948                                              &xd->plane[0].pre[0]);
949        }
950#else
951        motion_error =
952            get_prediction_error(bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
953#endif  // CONFIG_VP9_HIGHBITDEPTH
954
955        // Compute the motion error of the 0,0 motion using the last source
956        // frame as the reference. Skip the further motion search on
957        // reconstructed frame if this error is small.
958        unscaled_last_source_buf_2d.buf =
959            cpi->unscaled_last_source->y_buffer + recon_yoffset;
960        unscaled_last_source_buf_2d.stride =
961            cpi->unscaled_last_source->y_stride;
962#if CONFIG_VP9_HIGHBITDEPTH
963        if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
964          raw_motion_error = highbd_get_prediction_error(
965              bsize, &x->plane[0].src, &unscaled_last_source_buf_2d, xd->bd);
966        } else {
967          raw_motion_error = get_prediction_error(bsize, &x->plane[0].src,
968                                                  &unscaled_last_source_buf_2d);
969        }
970#else
971        raw_motion_error = get_prediction_error(bsize, &x->plane[0].src,
972                                                &unscaled_last_source_buf_2d);
973#endif  // CONFIG_VP9_HIGHBITDEPTH
974
975        // TODO(pengchong): Replace the hard-coded threshold
976        if (raw_motion_error > 25 || lc != NULL) {
977          // Test last reference frame using the previous best mv as the
978          // starting point (best reference) for the search.
979          first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error);
980
981          // If the current best reference mv is not centered on 0,0 then do a
982          // 0,0 based search as well.
983          if (!is_zero_mv(&best_ref_mv)) {
984            tmp_err = INT_MAX;
985            first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
986
987            if (tmp_err < motion_error) {
988              motion_error = tmp_err;
989              mv = tmp_mv;
990            }
991          }
992
993          // Search in an older reference frame.
994          if (((lc == NULL && cm->current_video_frame > 1) ||
995               (lc != NULL && lc->current_video_frame_in_layer > 1)) &&
996              gld_yv12 != NULL) {
997            // Assume 0,0 motion with no mv overhead.
998            int gf_motion_error;
999
1000            xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
1001#if CONFIG_VP9_HIGHBITDEPTH
1002            if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1003              gf_motion_error = highbd_get_prediction_error(
1004                  bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
1005            } else {
1006              gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
1007                                                     &xd->plane[0].pre[0]);
1008            }
1009#else
1010            gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
1011                                                   &xd->plane[0].pre[0]);
1012#endif  // CONFIG_VP9_HIGHBITDEPTH
1013
1014            first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv,
1015                                     &gf_motion_error);
1016
1017            if (gf_motion_error < motion_error && gf_motion_error < this_error)
1018              ++second_ref_count;
1019
1020            // Reset to last frame as reference buffer.
1021            xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
1022            xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
1023            xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
1024
1025            // In accumulating a score for the older reference frame take the
1026            // best of the motion predicted score and the intra coded error
1027            // (just as will be done for) accumulation of "coded_error" for
1028            // the last frame.
1029            if (gf_motion_error < this_error)
1030              sr_coded_error += gf_motion_error;
1031            else
1032              sr_coded_error += this_error;
1033          } else {
1034            sr_coded_error += motion_error;
1035          }
1036        } else {
1037          sr_coded_error += motion_error;
1038        }
1039
1040        // Start by assuming that intra mode is best.
1041        best_ref_mv.row = 0;
1042        best_ref_mv.col = 0;
1043
1044#if CONFIG_FP_MB_STATS
1045        if (cpi->use_fp_mb_stats) {
1046          // intra prediction statistics
1047          cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
1048          cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK;
1049          cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
1050          if (this_error > FPMB_ERROR_LARGE_TH) {
1051            cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
1052          } else if (this_error < FPMB_ERROR_SMALL_TH) {
1053            cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
1054          }
1055        }
1056#endif
1057
1058        if (motion_error <= this_error) {
1059          vpx_clear_system_state();
1060
1061          // Keep a count of cases where the inter and intra were very close
1062          // and very low. This helps with scene cut detection for example in
1063          // cropped clips with black bars at the sides or top and bottom.
1064          if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
1065              (this_error < (2 * intrapenalty))) {
1066            neutral_count += 1.0;
1067            // Also track cases where the intra is not much worse than the inter
1068            // and use this in limiting the GF/arf group length.
1069          } else if ((this_error > NCOUNT_INTRA_THRESH) &&
1070                     (this_error < (NCOUNT_INTRA_FACTOR * motion_error))) {
1071            neutral_count +=
1072                (double)motion_error / DOUBLE_DIVIDE_CHECK((double)this_error);
1073          }
1074
1075          mv.row *= 8;
1076          mv.col *= 8;
1077          this_error = motion_error;
1078          xd->mi[0]->mode = NEWMV;
1079          xd->mi[0]->mv[0].as_mv = mv;
1080          xd->mi[0]->tx_size = TX_4X4;
1081          xd->mi[0]->ref_frame[0] = LAST_FRAME;
1082          xd->mi[0]->ref_frame[1] = NONE;
1083          vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
1084          vp9_encode_sby_pass1(x, bsize);
1085          sum_mvr += mv.row;
1086          sum_mvr_abs += abs(mv.row);
1087          sum_mvc += mv.col;
1088          sum_mvc_abs += abs(mv.col);
1089          sum_mvrs += mv.row * mv.row;
1090          sum_mvcs += mv.col * mv.col;
1091          ++intercount;
1092
1093          best_ref_mv = mv;
1094
1095#if CONFIG_FP_MB_STATS
1096          if (cpi->use_fp_mb_stats) {
1097            // inter prediction statistics
1098            cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
1099            cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK;
1100            cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
1101            if (this_error > FPMB_ERROR_LARGE_TH) {
1102              cpi->twopass.frame_mb_stats_buf[mb_index] |=
1103                  FPMB_ERROR_LARGE_MASK;
1104            } else if (this_error < FPMB_ERROR_SMALL_TH) {
1105              cpi->twopass.frame_mb_stats_buf[mb_index] |=
1106                  FPMB_ERROR_SMALL_MASK;
1107            }
1108          }
1109#endif
1110
1111          if (!is_zero_mv(&mv)) {
1112            ++mvcount;
1113
1114#if CONFIG_FP_MB_STATS
1115            if (cpi->use_fp_mb_stats) {
1116              cpi->twopass.frame_mb_stats_buf[mb_index] &=
1117                  ~FPMB_MOTION_ZERO_MASK;
1118              // check estimated motion direction
1119              if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) {
1120                // right direction
1121                cpi->twopass.frame_mb_stats_buf[mb_index] |=
1122                    FPMB_MOTION_RIGHT_MASK;
1123              } else if (mv.as_mv.row < 0 &&
1124                         abs(mv.as_mv.row) >= abs(mv.as_mv.col)) {
1125                // up direction
1126                cpi->twopass.frame_mb_stats_buf[mb_index] |=
1127                    FPMB_MOTION_UP_MASK;
1128              } else if (mv.as_mv.col < 0 &&
1129                         abs(mv.as_mv.col) >= abs(mv.as_mv.row)) {
1130                // left direction
1131                cpi->twopass.frame_mb_stats_buf[mb_index] |=
1132                    FPMB_MOTION_LEFT_MASK;
1133              } else {
1134                // down direction
1135                cpi->twopass.frame_mb_stats_buf[mb_index] |=
1136                    FPMB_MOTION_DOWN_MASK;
1137              }
1138            }
1139#endif
1140
1141            // Does the row vector point inwards or outwards?
1142            if (mb_row < cm->mb_rows / 2) {
1143              if (mv.row > 0)
1144                --sum_in_vectors;
1145              else if (mv.row < 0)
1146                ++sum_in_vectors;
1147            } else if (mb_row > cm->mb_rows / 2) {
1148              if (mv.row > 0)
1149                ++sum_in_vectors;
1150              else if (mv.row < 0)
1151                --sum_in_vectors;
1152            }
1153
1154            // Does the col vector point inwards or outwards?
1155            if (mb_col < cm->mb_cols / 2) {
1156              if (mv.col > 0)
1157                --sum_in_vectors;
1158              else if (mv.col < 0)
1159                ++sum_in_vectors;
1160            } else if (mb_col > cm->mb_cols / 2) {
1161              if (mv.col > 0)
1162                ++sum_in_vectors;
1163              else if (mv.col < 0)
1164                --sum_in_vectors;
1165            }
1166            frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
1167          } else if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH)) {
1168            frame_noise_energy += fp_estimate_block_noise(x, bsize);
1169          } else {  // 0,0 mv but high error
1170            frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
1171          }
1172        } else {  // Intra < inter error
1173          if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH))
1174            frame_noise_energy += fp_estimate_block_noise(x, bsize);
1175          else
1176            frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
1177        }
1178      } else {
1179        sr_coded_error += (int64_t)this_error;
1180      }
1181      coded_error += (int64_t)this_error;
1182
1183      // Adjust to the next column of MBs.
1184      x->plane[0].src.buf += 16;
1185      x->plane[1].src.buf += uv_mb_height;
1186      x->plane[2].src.buf += uv_mb_height;
1187
1188      recon_yoffset += 16;
1189      recon_uvoffset += uv_mb_height;
1190    }
1191
1192    // Adjust to the next row of MBs.
1193    x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
1194    x->plane[1].src.buf +=
1195        uv_mb_height * x->plane[1].src.stride - uv_mb_height * cm->mb_cols;
1196    x->plane[2].src.buf +=
1197        uv_mb_height * x->plane[1].src.stride - uv_mb_height * cm->mb_cols;
1198
1199    vpx_clear_system_state();
1200  }
1201
1202  // Clamp the image start to rows/2. This number of rows is discarded top
1203  // and bottom as dead data so rows / 2 means the frame is blank.
1204  if ((image_data_start_row > cm->mb_rows / 2) ||
1205      (image_data_start_row == INVALID_ROW)) {
1206    image_data_start_row = cm->mb_rows / 2;
1207  }
1208  // Exclude any image dead zone
1209  if (image_data_start_row > 0) {
1210    intra_skip_count =
1211        VPXMAX(0, intra_skip_count - (image_data_start_row * cm->mb_cols * 2));
1212  }
1213
1214  {
1215    FIRSTPASS_STATS fps;
1216    // The minimum error here insures some bit allocation to frames even
1217    // in static regions. The allocation per MB declines for larger formats
1218    // where the typical "real" energy per MB also falls.
1219    // Initial estimate here uses sqrt(mbs) to define the min_err, where the
1220    // number of mbs is proportional to the image area.
1221    const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1222                            ? cpi->initial_mbs
1223                            : cpi->common.MBs;
1224    const double min_err = 200 * sqrt(num_mbs);
1225
1226    intra_factor = intra_factor / (double)num_mbs;
1227    brightness_factor = brightness_factor / (double)num_mbs;
1228    fps.weight = intra_factor * brightness_factor;
1229
1230    fps.frame = cm->current_video_frame;
1231    fps.spatial_layer_id = cpi->svc.spatial_layer_id;
1232    fps.coded_error = (double)(coded_error >> 8) + min_err;
1233    fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
1234    fps.intra_error = (double)(intra_error >> 8) + min_err;
1235    fps.frame_noise_energy = (double)frame_noise_energy / (double)num_mbs;
1236    fps.count = 1.0;
1237    fps.pcnt_inter = (double)intercount / num_mbs;
1238    fps.pcnt_second_ref = (double)second_ref_count / num_mbs;
1239    fps.pcnt_neutral = (double)neutral_count / num_mbs;
1240    fps.intra_skip_pct = (double)intra_skip_count / num_mbs;
1241    fps.intra_smooth_pct = (double)intra_smooth_count / num_mbs;
1242    fps.inactive_zone_rows = (double)image_data_start_row;
1243    // Currently set to 0 as most issues relate to letter boxing.
1244    fps.inactive_zone_cols = (double)0;
1245
1246    if (mvcount > 0) {
1247      fps.MVr = (double)sum_mvr / mvcount;
1248      fps.mvr_abs = (double)sum_mvr_abs / mvcount;
1249      fps.MVc = (double)sum_mvc / mvcount;
1250      fps.mvc_abs = (double)sum_mvc_abs / mvcount;
1251      fps.MVrv =
1252          ((double)sum_mvrs - ((double)sum_mvr * sum_mvr / mvcount)) / mvcount;
1253      fps.MVcv =
1254          ((double)sum_mvcs - ((double)sum_mvc * sum_mvc / mvcount)) / mvcount;
1255      fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
1256      fps.pcnt_motion = (double)mvcount / num_mbs;
1257    } else {
1258      fps.MVr = 0.0;
1259      fps.mvr_abs = 0.0;
1260      fps.MVc = 0.0;
1261      fps.mvc_abs = 0.0;
1262      fps.MVrv = 0.0;
1263      fps.MVcv = 0.0;
1264      fps.mv_in_out_count = 0.0;
1265      fps.pcnt_motion = 0.0;
1266    }
1267
1268    // Dont allow a value of 0 for duration.
1269    // (Section duration is also defaulted to minimum of 1.0).
1270    fps.duration = VPXMAX(1.0, (double)(source->ts_end - source->ts_start));
1271
1272    // Don't want to do output stats with a stack variable!
1273    twopass->this_frame_stats = fps;
1274    output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
1275    accumulate_stats(&twopass->total_stats, &fps);
1276
1277#if CONFIG_FP_MB_STATS
1278    if (cpi->use_fp_mb_stats) {
1279      output_fpmb_stats(twopass->frame_mb_stats_buf, cm, cpi->output_pkt_list);
1280    }
1281#endif
1282  }
1283
1284  // Copy the previous Last Frame back into gf and and arf buffers if
1285  // the prediction is good enough... but also don't allow it to lag too far.
1286  if ((twopass->sr_update_lag > 3) ||
1287      ((cm->current_video_frame > 0) &&
1288       (twopass->this_frame_stats.pcnt_inter > 0.20) &&
1289       ((twopass->this_frame_stats.intra_error /
1290         DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
1291    if (gld_yv12 != NULL) {
1292      ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
1293                 cm->ref_frame_map[cpi->lst_fb_idx]);
1294    }
1295    twopass->sr_update_lag = 1;
1296  } else {
1297    ++twopass->sr_update_lag;
1298  }
1299
1300  vpx_extend_frame_borders(new_yv12);
1301
1302  if (lc != NULL) {
1303    vp9_update_reference_frames(cpi);
1304  } else {
1305    // The frame we just compressed now becomes the last frame.
1306    ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
1307               cm->new_fb_idx);
1308  }
1309
1310  // Special case for the first frame. Copy into the GF buffer as a second
1311  // reference.
1312  if (cm->current_video_frame == 0 && cpi->gld_fb_idx != INVALID_IDX &&
1313      lc == NULL) {
1314    ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
1315               cm->ref_frame_map[cpi->lst_fb_idx]);
1316  }
1317
1318  // Use this to see what the first pass reconstruction looks like.
1319  if (0) {
1320    char filename[512];
1321    FILE *recon_file;
1322    snprintf(filename, sizeof(filename), "enc%04d.yuv",
1323             (int)cm->current_video_frame);
1324
1325    if (cm->current_video_frame == 0)
1326      recon_file = fopen(filename, "wb");
1327    else
1328      recon_file = fopen(filename, "ab");
1329
1330    (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
1331    fclose(recon_file);
1332  }
1333
1334  ++cm->current_video_frame;
1335  if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
1336}
1337
1338static double calc_correction_factor(double err_per_mb, double err_divisor,
1339                                     double pt_low, double pt_high, int q,
1340                                     vpx_bit_depth_t bit_depth) {
1341  const double error_term = err_per_mb / err_divisor;
1342
1343  // Adjustment based on actual quantizer to power term.
1344  const double power_term =
1345      VPXMIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high);
1346
1347  // Calculate correction factor.
1348  if (power_term < 1.0) assert(error_term >= 0.0);
1349
1350  return fclamp(pow(error_term, power_term), 0.05, 5.0);
1351}
1352
1353#define ERR_DIVISOR 115.0
1354#define NOISE_FACTOR_MIN 0.9
1355#define NOISE_FACTOR_MAX 1.1
1356static int get_twopass_worst_quality(VP9_COMP *cpi, const double section_err,
1357                                     double inactive_zone, double section_noise,
1358                                     int section_target_bandwidth) {
1359  const RATE_CONTROL *const rc = &cpi->rc;
1360  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1361  TWO_PASS *const twopass = &cpi->twopass;
1362
1363  // Clamp the target rate to VBR min / max limts.
1364  const int target_rate =
1365      vp9_rc_clamp_pframe_target_size(cpi, section_target_bandwidth);
1366  double noise_factor = pow((section_noise / SECTION_NOISE_DEF), 0.5);
1367  noise_factor = fclamp(noise_factor, NOISE_FACTOR_MIN, NOISE_FACTOR_MAX);
1368  inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
1369
1370  if (target_rate <= 0) {
1371    return rc->worst_quality;  // Highest value allowed
1372  } else {
1373    const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1374                            ? cpi->initial_mbs
1375                            : cpi->common.MBs;
1376    const int active_mbs = VPXMAX(1, num_mbs - (int)(num_mbs * inactive_zone));
1377    const double av_err_per_mb = section_err / active_mbs;
1378    const double speed_term = 1.0 + 0.04 * oxcf->speed;
1379    double last_group_rate_err;
1380    const int target_norm_bits_per_mb =
1381        (int)(((uint64_t)target_rate << BPER_MB_NORMBITS) / active_mbs);
1382    int q;
1383    int is_svc_upper_layer = 0;
1384
1385    if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
1386      is_svc_upper_layer = 1;
1387
1388    // based on recent history adjust expectations of bits per macroblock.
1389    last_group_rate_err =
1390        (double)twopass->rolling_arf_group_actual_bits /
1391        DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
1392    last_group_rate_err = VPXMAX(0.25, VPXMIN(4.0, last_group_rate_err));
1393    twopass->bpm_factor *= (3.0 + last_group_rate_err) / 4.0;
1394    twopass->bpm_factor = VPXMAX(0.25, VPXMIN(4.0, twopass->bpm_factor));
1395
1396    // Try and pick a max Q that will be high enough to encode the
1397    // content at the given rate.
1398    for (q = rc->best_quality; q < rc->worst_quality; ++q) {
1399      const double factor = calc_correction_factor(
1400          av_err_per_mb, ERR_DIVISOR,
1401          is_svc_upper_layer ? SVC_FACTOR_PT_LOW : FACTOR_PT_LOW,
1402          FACTOR_PT_HIGH, q, cpi->common.bit_depth);
1403      const int bits_per_mb = vp9_rc_bits_per_mb(
1404          INTER_FRAME, q,
1405          factor * speed_term * cpi->twopass.bpm_factor * noise_factor,
1406          cpi->common.bit_depth);
1407      if (bits_per_mb <= target_norm_bits_per_mb) break;
1408    }
1409
1410    // Restriction on active max q for constrained quality mode.
1411    if (cpi->oxcf.rc_mode == VPX_CQ) q = VPXMAX(q, oxcf->cq_level);
1412    return q;
1413  }
1414}
1415
1416static void setup_rf_level_maxq(VP9_COMP *cpi) {
1417  int i;
1418  RATE_CONTROL *const rc = &cpi->rc;
1419  for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) {
1420    int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality);
1421    rc->rf_level_maxq[i] = VPXMAX(rc->worst_quality + qdelta, rc->best_quality);
1422  }
1423}
1424
1425static void init_subsampling(VP9_COMP *cpi) {
1426  const VP9_COMMON *const cm = &cpi->common;
1427  RATE_CONTROL *const rc = &cpi->rc;
1428  const int w = cm->width;
1429  const int h = cm->height;
1430  int i;
1431
1432  for (i = 0; i < FRAME_SCALE_STEPS; ++i) {
1433    // Note: Frames with odd-sized dimensions may result from this scaling.
1434    rc->frame_width[i] = (w * 16) / frame_scale_factor[i];
1435    rc->frame_height[i] = (h * 16) / frame_scale_factor[i];
1436  }
1437
1438  setup_rf_level_maxq(cpi);
1439}
1440
1441void calculate_coded_size(VP9_COMP *cpi, int *scaled_frame_width,
1442                          int *scaled_frame_height) {
1443  RATE_CONTROL *const rc = &cpi->rc;
1444  *scaled_frame_width = rc->frame_width[rc->frame_size_selector];
1445  *scaled_frame_height = rc->frame_height[rc->frame_size_selector];
1446}
1447
1448void vp9_init_second_pass(VP9_COMP *cpi) {
1449  SVC *const svc = &cpi->svc;
1450  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1451  const int is_two_pass_svc =
1452      (svc->number_spatial_layers > 1) || (svc->number_temporal_layers > 1);
1453  RATE_CONTROL *const rc = &cpi->rc;
1454  TWO_PASS *const twopass =
1455      is_two_pass_svc ? &svc->layer_context[svc->spatial_layer_id].twopass
1456                      : &cpi->twopass;
1457  double frame_rate;
1458  FIRSTPASS_STATS *stats;
1459
1460  zero_stats(&twopass->total_stats);
1461  zero_stats(&twopass->total_left_stats);
1462
1463  if (!twopass->stats_in_end) return;
1464
1465  stats = &twopass->total_stats;
1466
1467  *stats = *twopass->stats_in_end;
1468  twopass->total_left_stats = *stats;
1469
1470  frame_rate = 10000000.0 * stats->count / stats->duration;
1471  // Each frame can have a different duration, as the frame rate in the source
1472  // isn't guaranteed to be constant. The frame rate prior to the first frame
1473  // encoded in the second pass is a guess. However, the sum duration is not.
1474  // It is calculated based on the actual durations of all frames from the
1475  // first pass.
1476
1477  if (is_two_pass_svc) {
1478    vp9_update_spatial_layer_framerate(cpi, frame_rate);
1479    twopass->bits_left =
1480        (int64_t)(stats->duration *
1481                  svc->layer_context[svc->spatial_layer_id].target_bandwidth /
1482                  10000000.0);
1483  } else {
1484    vp9_new_framerate(cpi, frame_rate);
1485    twopass->bits_left =
1486        (int64_t)(stats->duration * oxcf->target_bandwidth / 10000000.0);
1487  }
1488
1489  // This variable monitors how far behind the second ref update is lagging.
1490  twopass->sr_update_lag = 1;
1491
1492  // Scan the first pass file and calculate a modified total error based upon
1493  // the bias/power function used to allocate bits.
1494  {
1495    const double avg_error =
1496        stats->coded_error / DOUBLE_DIVIDE_CHECK(stats->count);
1497    const FIRSTPASS_STATS *s = twopass->stats_in;
1498    double modified_error_total = 0.0;
1499    twopass->modified_error_min =
1500        (avg_error * oxcf->two_pass_vbrmin_section) / 100;
1501    twopass->modified_error_max =
1502        (avg_error * oxcf->two_pass_vbrmax_section) / 100;
1503    while (s < twopass->stats_in_end) {
1504      modified_error_total += calculate_modified_err(cpi, twopass, oxcf, s);
1505      ++s;
1506    }
1507    twopass->modified_error_left = modified_error_total;
1508  }
1509
1510  // Reset the vbr bits off target counters
1511  rc->vbr_bits_off_target = 0;
1512  rc->vbr_bits_off_target_fast = 0;
1513  rc->rate_error_estimate = 0;
1514
1515  // Static sequence monitor variables.
1516  twopass->kf_zeromotion_pct = 100;
1517  twopass->last_kfgroup_zeromotion_pct = 100;
1518
1519  // Initialize bits per macro_block estimate correction factor.
1520  twopass->bpm_factor = 1.0;
1521  // Initialize actual and target bits counters for ARF groups so that
1522  // at the start we have a neutral bpm adjustment.
1523  twopass->rolling_arf_group_target_bits = 1;
1524  twopass->rolling_arf_group_actual_bits = 1;
1525
1526  if (oxcf->resize_mode != RESIZE_NONE) {
1527    init_subsampling(cpi);
1528  }
1529
1530  // Initialize the arnr strangth adjustment to 0
1531  twopass->arnr_strength_adjustment = 0;
1532}
1533
1534#define SR_DIFF_PART 0.0015
1535#define INTRA_PART 0.005
1536#define DEFAULT_DECAY_LIMIT 0.75
1537#define LOW_SR_DIFF_TRHESH 0.1
1538#define SR_DIFF_MAX 128.0
1539#define LOW_CODED_ERR_PER_MB 10.0
1540#define NCOUNT_FRAME_II_THRESH 6.0
1541
1542static double get_sr_decay_rate(const VP9_COMP *cpi,
1543                                const FIRSTPASS_STATS *frame) {
1544  const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
1545                                                             : cpi->common.MBs;
1546  double sr_diff = (frame->sr_coded_error - frame->coded_error) / num_mbs;
1547  double sr_decay = 1.0;
1548  double modified_pct_inter;
1549  double modified_pcnt_intra;
1550  const double motion_amplitude_part =
1551      frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) /
1552                            (cpi->initial_height + cpi->initial_width));
1553
1554  modified_pct_inter = frame->pcnt_inter;
1555  if (((frame->coded_error / num_mbs) > LOW_CODED_ERR_PER_MB) &&
1556      ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) <
1557       (double)NCOUNT_FRAME_II_THRESH)) {
1558    modified_pct_inter = frame->pcnt_inter - frame->pcnt_neutral;
1559  }
1560  modified_pcnt_intra = 100 * (1.0 - modified_pct_inter);
1561
1562  if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1563    sr_diff = VPXMIN(sr_diff, SR_DIFF_MAX);
1564    sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) - motion_amplitude_part -
1565               (INTRA_PART * modified_pcnt_intra);
1566  }
1567  return VPXMAX(sr_decay, DEFAULT_DECAY_LIMIT);
1568}
1569
1570// This function gives an estimate of how badly we believe the prediction
1571// quality is decaying from frame to frame.
1572static double get_zero_motion_factor(const VP9_COMP *cpi,
1573                                     const FIRSTPASS_STATS *frame) {
1574  const double zero_motion_pct = frame->pcnt_inter - frame->pcnt_motion;
1575  double sr_decay = get_sr_decay_rate(cpi, frame);
1576  return VPXMIN(sr_decay, zero_motion_pct);
1577}
1578
1579#define ZM_POWER_FACTOR 0.75
1580
1581static double get_prediction_decay_rate(const VP9_COMP *cpi,
1582                                        const FIRSTPASS_STATS *next_frame) {
1583  const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame);
1584  const double zero_motion_factor =
1585      (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
1586                  ZM_POWER_FACTOR));
1587
1588  return VPXMAX(zero_motion_factor,
1589                (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1590}
1591
1592// Function to test for a condition where a complex transition is followed
1593// by a static section. For example in slide shows where there is a fade
1594// between slides. This is to help with more optimal kf and gf positioning.
1595static int detect_transition_to_still(VP9_COMP *cpi, int frame_interval,
1596                                      int still_interval,
1597                                      double loop_decay_rate,
1598                                      double last_decay_rate) {
1599  TWO_PASS *const twopass = &cpi->twopass;
1600  RATE_CONTROL *const rc = &cpi->rc;
1601
1602  // Break clause to detect very still sections after motion
1603  // For example a static image after a fade or other transition
1604  // instead of a clean scene cut.
1605  if (frame_interval > rc->min_gf_interval && loop_decay_rate >= 0.999 &&
1606      last_decay_rate < 0.9) {
1607    int j;
1608
1609    // Look ahead a few frames to see if static condition persists...
1610    for (j = 0; j < still_interval; ++j) {
1611      const FIRSTPASS_STATS *stats = &twopass->stats_in[j];
1612      if (stats >= twopass->stats_in_end) break;
1613
1614      if (stats->pcnt_inter - stats->pcnt_motion < 0.999) break;
1615    }
1616
1617    // Only if it does do we signal a transition to still.
1618    return j == still_interval;
1619  }
1620
1621  return 0;
1622}
1623
1624// This function detects a flash through the high relative pcnt_second_ref
1625// score in the frame following a flash frame. The offset passed in should
1626// reflect this.
1627static int detect_flash(const TWO_PASS *twopass, int offset) {
1628  const FIRSTPASS_STATS *const next_frame = read_frame_stats(twopass, offset);
1629
1630  // What we are looking for here is a situation where there is a
1631  // brief break in prediction (such as a flash) but subsequent frames
1632  // are reasonably well predicted by an earlier (pre flash) frame.
1633  // The recovery after a flash is indicated by a high pcnt_second_ref
1634  // compared to pcnt_inter.
1635  return next_frame != NULL &&
1636         next_frame->pcnt_second_ref > next_frame->pcnt_inter &&
1637         next_frame->pcnt_second_ref >= 0.5;
1638}
1639
1640// Update the motion related elements to the GF arf boost calculation.
1641static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats,
1642                                          double *mv_in_out,
1643                                          double *mv_in_out_accumulator,
1644                                          double *abs_mv_in_out_accumulator,
1645                                          double *mv_ratio_accumulator) {
1646  const double pct = stats->pcnt_motion;
1647
1648  // Accumulate Motion In/Out of frame stats.
1649  *mv_in_out = stats->mv_in_out_count * pct;
1650  *mv_in_out_accumulator += *mv_in_out;
1651  *abs_mv_in_out_accumulator += fabs(*mv_in_out);
1652
1653  // Accumulate a measure of how uniform (or conversely how random) the motion
1654  // field is (a ratio of abs(mv) / mv).
1655  if (pct > 0.05) {
1656    const double mvr_ratio =
1657        fabs(stats->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVr));
1658    const double mvc_ratio =
1659        fabs(stats->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
1660
1661    *mv_ratio_accumulator +=
1662        pct * (mvr_ratio < stats->mvr_abs ? mvr_ratio : stats->mvr_abs);
1663    *mv_ratio_accumulator +=
1664        pct * (mvc_ratio < stats->mvc_abs ? mvc_ratio : stats->mvc_abs);
1665  }
1666}
1667
1668#define BASELINE_ERR_PER_MB 1000.0
1669static double calc_frame_boost(VP9_COMP *cpi, const FIRSTPASS_STATS *this_frame,
1670                               double *sr_accumulator,
1671                               double this_frame_mv_in_out, double max_boost) {
1672  double frame_boost;
1673  const double lq = vp9_convert_qindex_to_q(
1674      cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth);
1675  const double boost_q_correction = VPXMIN((0.5 + (lq * 0.015)), 1.5);
1676  int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
1677                                                       : cpi->common.MBs;
1678
1679  // Correct for any inactive region in the image
1680  num_mbs = (int)VPXMAX(1, num_mbs * calculate_active_area(cpi, this_frame));
1681
1682  // Underlying boost factor is based on inter error ratio.
1683  frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
1684                DOUBLE_DIVIDE_CHECK(this_frame->coded_error + *sr_accumulator);
1685
1686  // Update the accumulator for second ref error difference.
1687  // This is intended to give an indication of how much the coded error is
1688  // increasing over time.
1689  *sr_accumulator += (this_frame->sr_coded_error - this_frame->coded_error) / 1;
1690  *sr_accumulator = VPXMAX(0.0, *sr_accumulator);
1691
1692  // Small adjustment for cases where there is a zoom out
1693  if (this_frame_mv_in_out > 0.0)
1694    frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1695
1696  // Q correction and scalling
1697  frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction;
1698
1699  return VPXMIN(frame_boost, max_boost * boost_q_correction);
1700}
1701
1702#define KF_BOOST_FACTOR 12.5
1703static double calc_kf_frame_boost(VP9_COMP *cpi,
1704                                  const FIRSTPASS_STATS *this_frame,
1705                                  double *sr_accumulator,
1706                                  double this_frame_mv_in_out,
1707                                  double max_boost) {
1708  double frame_boost;
1709  const double lq = vp9_convert_qindex_to_q(
1710      cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth);
1711  const double boost_q_correction = VPXMIN((0.50 + (lq * 0.015)), 2.00);
1712  int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
1713                                                       : cpi->common.MBs;
1714
1715  // Correct for any inactive region in the image
1716  num_mbs = (int)VPXMAX(1, num_mbs * calculate_active_area(cpi, this_frame));
1717
1718  // Underlying boost factor is based on inter error ratio.
1719  frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
1720                DOUBLE_DIVIDE_CHECK(this_frame->coded_error + *sr_accumulator);
1721
1722  // Update the accumulator for second ref error difference.
1723  // This is intended to give an indication of how much the coded error is
1724  // increasing over time.
1725  *sr_accumulator += (this_frame->sr_coded_error - this_frame->coded_error) / 1;
1726  *sr_accumulator = VPXMAX(0.0, *sr_accumulator);
1727
1728  // Small adjustment for cases where there is a zoom out
1729  if (this_frame_mv_in_out > 0.0)
1730    frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1731
1732  // Q correction and scalling
1733  frame_boost = frame_boost * KF_BOOST_FACTOR * boost_q_correction;
1734
1735  return VPXMIN(frame_boost, max_boost * boost_q_correction);
1736}
1737
1738static int calc_arf_boost(VP9_COMP *cpi, int offset, int f_frames, int b_frames,
1739                          int *f_boost, int *b_boost) {
1740  TWO_PASS *const twopass = &cpi->twopass;
1741  int i;
1742  double boost_score = 0.0;
1743  double mv_ratio_accumulator = 0.0;
1744  double decay_accumulator = 1.0;
1745  double this_frame_mv_in_out = 0.0;
1746  double mv_in_out_accumulator = 0.0;
1747  double abs_mv_in_out_accumulator = 0.0;
1748  double sr_accumulator = 0.0;
1749  int arf_boost;
1750  int flash_detected = 0;
1751
1752  // Search forward from the proposed arf/next gf position.
1753  for (i = 0; i < f_frames; ++i) {
1754    const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1755    if (this_frame == NULL) break;
1756
1757    // Update the motion related elements to the boost calculation.
1758    accumulate_frame_motion_stats(
1759        this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1760        &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1761
1762    // We want to discount the flash frame itself and the recovery
1763    // frame that follows as both will have poor scores.
1764    flash_detected = detect_flash(twopass, i + offset) ||
1765                     detect_flash(twopass, i + offset + 1);
1766
1767    // Accumulate the effect of prediction quality decay.
1768    if (!flash_detected) {
1769      decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
1770      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1771                              ? MIN_DECAY_FACTOR
1772                              : decay_accumulator;
1773    }
1774
1775    sr_accumulator = 0.0;
1776    boost_score += decay_accumulator *
1777                   calc_frame_boost(cpi, this_frame, &sr_accumulator,
1778                                    this_frame_mv_in_out, GF_MAX_BOOST);
1779  }
1780
1781  *f_boost = (int)boost_score;
1782
1783  // Reset for backward looking loop.
1784  boost_score = 0.0;
1785  mv_ratio_accumulator = 0.0;
1786  decay_accumulator = 1.0;
1787  this_frame_mv_in_out = 0.0;
1788  mv_in_out_accumulator = 0.0;
1789  abs_mv_in_out_accumulator = 0.0;
1790  sr_accumulator = 0.0;
1791
1792  // Search backward towards last gf position.
1793  for (i = -1; i >= -b_frames; --i) {
1794    const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1795    if (this_frame == NULL) break;
1796
1797    // Update the motion related elements to the boost calculation.
1798    accumulate_frame_motion_stats(
1799        this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1800        &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1801
1802    // We want to discount the the flash frame itself and the recovery
1803    // frame that follows as both will have poor scores.
1804    flash_detected = detect_flash(twopass, i + offset) ||
1805                     detect_flash(twopass, i + offset + 1);
1806
1807    // Cumulative effect of prediction quality decay.
1808    if (!flash_detected) {
1809      decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
1810      decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1811                              ? MIN_DECAY_FACTOR
1812                              : decay_accumulator;
1813    }
1814
1815    sr_accumulator = 0.0;
1816    boost_score += decay_accumulator *
1817                   calc_frame_boost(cpi, this_frame, &sr_accumulator,
1818                                    this_frame_mv_in_out, GF_MAX_BOOST);
1819  }
1820  *b_boost = (int)boost_score;
1821
1822  arf_boost = (*f_boost + *b_boost);
1823  if (arf_boost < ((b_frames + f_frames) * 20))
1824    arf_boost = ((b_frames + f_frames) * 20);
1825  arf_boost = VPXMAX(arf_boost, MIN_ARF_GF_BOOST);
1826
1827  return arf_boost;
1828}
1829
1830// Calculate a section intra ratio used in setting max loop filter.
1831static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
1832                                         const FIRSTPASS_STATS *end,
1833                                         int section_length) {
1834  const FIRSTPASS_STATS *s = begin;
1835  double intra_error = 0.0;
1836  double coded_error = 0.0;
1837  int i = 0;
1838
1839  while (s < end && i < section_length) {
1840    intra_error += s->intra_error;
1841    coded_error += s->coded_error;
1842    ++s;
1843    ++i;
1844  }
1845
1846  return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error));
1847}
1848
1849// Calculate the total bits to allocate in this GF/ARF group.
1850static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi,
1851                                             double gf_group_err) {
1852  const RATE_CONTROL *const rc = &cpi->rc;
1853  const TWO_PASS *const twopass = &cpi->twopass;
1854  const int max_bits = frame_max_bits(rc, &cpi->oxcf);
1855  int64_t total_group_bits;
1856
1857  // Calculate the bits to be allocated to the group as a whole.
1858  if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) {
1859    total_group_bits = (int64_t)(twopass->kf_group_bits *
1860                                 (gf_group_err / twopass->kf_group_error_left));
1861  } else {
1862    total_group_bits = 0;
1863  }
1864
1865  // Clamp odd edge cases.
1866  total_group_bits =
1867      (total_group_bits < 0) ? 0 : (total_group_bits > twopass->kf_group_bits)
1868                                       ? twopass->kf_group_bits
1869                                       : total_group_bits;
1870
1871  // Clip based on user supplied data rate variability limit.
1872  if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
1873    total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
1874
1875  return total_group_bits;
1876}
1877
1878// Calculate the number bits extra to assign to boosted frames in a group.
1879static int calculate_boost_bits(int frame_count, int boost,
1880                                int64_t total_group_bits) {
1881  int allocation_chunks;
1882
1883  // return 0 for invalid inputs (could arise e.g. through rounding errors)
1884  if (!boost || (total_group_bits <= 0) || (frame_count <= 0)) return 0;
1885
1886  allocation_chunks = (frame_count * 100) + boost;
1887
1888  // Prevent overflow.
1889  if (boost > 1023) {
1890    int divisor = boost >> 10;
1891    boost /= divisor;
1892    allocation_chunks /= divisor;
1893  }
1894
1895  // Calculate the number of extra bits for use in the boosted frame or frames.
1896  return VPXMAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks),
1897                0);
1898}
1899
1900// Current limit on maximum number of active arfs in a GF/ARF group.
1901#define MAX_ACTIVE_ARFS 2
1902#define ARF_SLOT1 2
1903#define ARF_SLOT2 3
1904// This function indirects the choice of buffers for arfs.
1905// At the moment the values are fixed but this may change as part of
1906// the integration process with other codec features that swap buffers around.
1907static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) {
1908  arf_buffer_indices[0] = ARF_SLOT1;
1909  arf_buffer_indices[1] = ARF_SLOT2;
1910}
1911
1912static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits,
1913                                   int gf_arf_bits) {
1914  RATE_CONTROL *const rc = &cpi->rc;
1915  TWO_PASS *const twopass = &cpi->twopass;
1916  GF_GROUP *const gf_group = &twopass->gf_group;
1917  FIRSTPASS_STATS frame_stats;
1918  int i;
1919  int frame_index = 1;
1920  int target_frame_size;
1921  int key_frame;
1922  const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf);
1923  int64_t total_group_bits = gf_group_bits;
1924  int mid_boost_bits = 0;
1925  int mid_frame_idx;
1926  unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS];
1927  int alt_frame_index = frame_index;
1928  int has_temporal_layers =
1929      is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1;
1930  int normal_frames;
1931  int normal_frame_bits;
1932  int last_frame_bits;
1933  int last_frame_reduction;
1934
1935  // Only encode alt reference frame in temporal base layer.
1936  if (has_temporal_layers) alt_frame_index = cpi->svc.number_temporal_layers;
1937
1938  key_frame =
1939      cpi->common.frame_type == KEY_FRAME || vp9_is_upper_layer_key_frame(cpi);
1940
1941  get_arf_buffer_indices(arf_buffer_indices);
1942
1943  // For key frames the frame target rate is already set and it
1944  // is also the golden frame.
1945  if (!key_frame) {
1946    if (rc->source_alt_ref_active) {
1947      gf_group->update_type[0] = OVERLAY_UPDATE;
1948      gf_group->rf_level[0] = INTER_NORMAL;
1949      gf_group->bit_allocation[0] = 0;
1950    } else {
1951      gf_group->update_type[0] = GF_UPDATE;
1952      gf_group->rf_level[0] = GF_ARF_STD;
1953      gf_group->bit_allocation[0] = gf_arf_bits;
1954    }
1955    gf_group->arf_update_idx[0] = arf_buffer_indices[0];
1956    gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
1957
1958    // Step over the golden frame / overlay frame
1959    if (EOF == input_stats(twopass, &frame_stats)) return;
1960  }
1961
1962  // Deduct the boost bits for arf (or gf if it is not a key frame)
1963  // from the group total.
1964  if (rc->source_alt_ref_pending || !key_frame) total_group_bits -= gf_arf_bits;
1965
1966  // Store the bits to spend on the ARF if there is one.
1967  if (rc->source_alt_ref_pending) {
1968    gf_group->update_type[alt_frame_index] = ARF_UPDATE;
1969    gf_group->rf_level[alt_frame_index] = GF_ARF_STD;
1970    gf_group->bit_allocation[alt_frame_index] = gf_arf_bits;
1971
1972    if (has_temporal_layers)
1973      gf_group->arf_src_offset[alt_frame_index] =
1974          (unsigned char)(rc->baseline_gf_interval -
1975                          cpi->svc.number_temporal_layers);
1976    else
1977      gf_group->arf_src_offset[alt_frame_index] =
1978          (unsigned char)(rc->baseline_gf_interval - 1);
1979
1980    gf_group->arf_update_idx[alt_frame_index] = arf_buffer_indices[0];
1981    gf_group->arf_ref_idx[alt_frame_index] =
1982        arf_buffer_indices[cpi->multi_arf_last_grp_enabled &&
1983                           rc->source_alt_ref_active];
1984    if (!has_temporal_layers) ++frame_index;
1985
1986    if (cpi->multi_arf_enabled) {
1987      // Set aside a slot for a level 1 arf.
1988      gf_group->update_type[frame_index] = ARF_UPDATE;
1989      gf_group->rf_level[frame_index] = GF_ARF_LOW;
1990      gf_group->arf_src_offset[frame_index] =
1991          (unsigned char)((rc->baseline_gf_interval >> 1) - 1);
1992      gf_group->arf_update_idx[frame_index] = arf_buffer_indices[1];
1993      gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
1994      ++frame_index;
1995    }
1996  }
1997
1998  // Note index of the first normal inter frame int eh group (not gf kf arf)
1999  gf_group->first_inter_index = frame_index;
2000
2001  // Define middle frame
2002  mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
2003
2004  normal_frames = (rc->baseline_gf_interval - rc->source_alt_ref_pending);
2005
2006  // The last frame in the group is used less as a predictor so reduce
2007  // its allocation a little.
2008  if (normal_frames > 1) {
2009    normal_frame_bits = (int)(total_group_bits / normal_frames);
2010    last_frame_reduction = normal_frame_bits / 16;
2011    last_frame_bits = normal_frame_bits - last_frame_reduction;
2012  } else {
2013    normal_frame_bits = (int)total_group_bits;
2014    last_frame_bits = normal_frame_bits;
2015    last_frame_reduction = 0;
2016  }
2017
2018  // Allocate bits to the other frames in the group.
2019  for (i = 0; i < normal_frames; ++i) {
2020    int arf_idx = 0;
2021    if (EOF == input_stats(twopass, &frame_stats)) break;
2022
2023    if (has_temporal_layers && frame_index == alt_frame_index) {
2024      ++frame_index;
2025    }
2026
2027    target_frame_size = (i == (normal_frames - 1))
2028                            ? last_frame_bits
2029                            : (i == mid_frame_idx)
2030                                  ? normal_frame_bits + last_frame_reduction
2031                                  : normal_frame_bits;
2032
2033    if (rc->source_alt_ref_pending && cpi->multi_arf_enabled) {
2034      mid_boost_bits += (target_frame_size >> 4);
2035      target_frame_size -= (target_frame_size >> 4);
2036
2037      if (frame_index <= mid_frame_idx) arf_idx = 1;
2038    }
2039    gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx];
2040    gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx];
2041
2042    target_frame_size =
2043        clamp(target_frame_size, 0, VPXMIN(max_bits, (int)total_group_bits));
2044
2045    gf_group->update_type[frame_index] = LF_UPDATE;
2046    gf_group->rf_level[frame_index] = INTER_NORMAL;
2047
2048    gf_group->bit_allocation[frame_index] = target_frame_size;
2049    ++frame_index;
2050  }
2051
2052  // Note:
2053  // We need to configure the frame at the end of the sequence + 1 that will be
2054  // the start frame for the next group. Otherwise prior to the call to
2055  // vp9_rc_get_second_pass_params() the data will be undefined.
2056  gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
2057  gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
2058
2059  if (rc->source_alt_ref_pending) {
2060    gf_group->update_type[frame_index] = OVERLAY_UPDATE;
2061    gf_group->rf_level[frame_index] = INTER_NORMAL;
2062
2063    // Final setup for second arf and its overlay.
2064    if (cpi->multi_arf_enabled) {
2065      gf_group->bit_allocation[2] =
2066          gf_group->bit_allocation[mid_frame_idx] + mid_boost_bits;
2067      gf_group->update_type[mid_frame_idx] = OVERLAY_UPDATE;
2068      gf_group->bit_allocation[mid_frame_idx] = 0;
2069    }
2070  } else {
2071    gf_group->update_type[frame_index] = GF_UPDATE;
2072    gf_group->rf_level[frame_index] = GF_ARF_STD;
2073  }
2074
2075  // Note whether multi-arf was enabled this group for next time.
2076  cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled;
2077}
2078
2079// Adjusts the ARNF filter for a GF group.
2080static void adjust_group_arnr_filter(VP9_COMP *cpi, double section_noise,
2081                                     double section_inter,
2082                                     double section_motion) {
2083  TWO_PASS *const twopass = &cpi->twopass;
2084  double section_zeromv = section_inter - section_motion;
2085
2086  twopass->arnr_strength_adjustment = 0;
2087
2088  if ((section_zeromv < 0.10) || (section_noise <= (SECTION_NOISE_DEF * 0.75)))
2089    twopass->arnr_strength_adjustment -= 1;
2090  if (section_zeromv > 0.50) twopass->arnr_strength_adjustment += 1;
2091}
2092
2093// Analyse and define a gf/arf group.
2094static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2095  VP9_COMMON *const cm = &cpi->common;
2096  RATE_CONTROL *const rc = &cpi->rc;
2097  VP9EncoderConfig *const oxcf = &cpi->oxcf;
2098  TWO_PASS *const twopass = &cpi->twopass;
2099  FIRSTPASS_STATS next_frame;
2100  const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
2101  int i;
2102
2103  double boost_score = 0.0;
2104  double old_boost_score = 0.0;
2105  double gf_group_err = 0.0;
2106  double gf_group_raw_error = 0.0;
2107  double gf_group_noise = 0.0;
2108  double gf_group_skip_pct = 0.0;
2109  double gf_group_inactive_zone_rows = 0.0;
2110  double gf_group_inter = 0.0;
2111  double gf_group_motion = 0.0;
2112  double gf_first_frame_err = 0.0;
2113  double mod_frame_err = 0.0;
2114
2115  double mv_ratio_accumulator = 0.0;
2116  double decay_accumulator = 1.0;
2117  double zero_motion_accumulator = 1.0;
2118  double loop_decay_rate = 1.00;
2119  double last_loop_decay_rate = 1.00;
2120
2121  double this_frame_mv_in_out = 0.0;
2122  double mv_in_out_accumulator = 0.0;
2123  double abs_mv_in_out_accumulator = 0.0;
2124  double mv_ratio_accumulator_thresh;
2125  double mv_in_out_thresh;
2126  double abs_mv_in_out_thresh;
2127  double sr_accumulator = 0.0;
2128  unsigned int allow_alt_ref = is_altref_enabled(cpi);
2129
2130  int f_boost = 0;
2131  int b_boost = 0;
2132  int flash_detected;
2133  int active_max_gf_interval;
2134  int active_min_gf_interval;
2135  int64_t gf_group_bits;
2136  int gf_arf_bits;
2137  const int is_key_frame = frame_is_intra_only(cm);
2138  const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active;
2139
2140  // Reset the GF group data structures unless this is a key
2141  // frame in which case it will already have been done.
2142  if (is_key_frame == 0) {
2143    vp9_zero(twopass->gf_group);
2144  }
2145
2146  vpx_clear_system_state();
2147  vp9_zero(next_frame);
2148
2149  // Load stats for the current frame.
2150  mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
2151
2152  // Note the error of the frame at the start of the group. This will be
2153  // the GF frame error if we code a normal gf.
2154  gf_first_frame_err = mod_frame_err;
2155
2156  // If this is a key frame or the overlay from a previous arf then
2157  // the error score / cost of this frame has already been accounted for.
2158  if (arf_active_or_kf) {
2159    gf_group_err -= gf_first_frame_err;
2160    gf_group_raw_error -= this_frame->coded_error;
2161    gf_group_noise -= this_frame->frame_noise_energy;
2162    gf_group_skip_pct -= this_frame->intra_skip_pct;
2163    gf_group_inactive_zone_rows -= this_frame->inactive_zone_rows;
2164    gf_group_inter -= this_frame->pcnt_inter;
2165    gf_group_motion -= this_frame->pcnt_motion;
2166  }
2167
2168  // Motion breakout threshold for loop below depends on image size.
2169  mv_ratio_accumulator_thresh =
2170      (cpi->initial_height + cpi->initial_width) / 4.0;
2171  mv_in_out_thresh = (cpi->initial_height + cpi->initial_width) / 300.0;
2172  abs_mv_in_out_thresh = (cpi->initial_height + cpi->initial_width) / 200.0;
2173
2174  // Set a maximum and minimum interval for the GF group.
2175  // If the image appears almost completely static we can extend beyond this.
2176  {
2177    int int_max_q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
2178                                                  cpi->common.bit_depth));
2179    int int_lbq = (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex,
2180                                                cpi->common.bit_depth));
2181    active_min_gf_interval =
2182        rc->min_gf_interval + arf_active_or_kf + VPXMIN(2, int_max_q / 200);
2183    if (active_min_gf_interval > rc->max_gf_interval)
2184      active_min_gf_interval = rc->max_gf_interval;
2185
2186    if (cpi->multi_arf_allowed) {
2187      active_max_gf_interval = rc->max_gf_interval;
2188    } else {
2189      // The value chosen depends on the active Q range. At low Q we have
2190      // bits to spare and are better with a smaller interval and smaller boost.
2191      // At high Q when there are few bits to spare we are better with a longer
2192      // interval to spread the cost of the GF.
2193      active_max_gf_interval = 12 + arf_active_or_kf + VPXMIN(4, (int_lbq / 6));
2194
2195      // We have: active_min_gf_interval <= rc->max_gf_interval
2196      if (active_max_gf_interval < active_min_gf_interval)
2197        active_max_gf_interval = active_min_gf_interval;
2198      else if (active_max_gf_interval > rc->max_gf_interval)
2199        active_max_gf_interval = rc->max_gf_interval;
2200
2201      // Would the active max drop us out just before the near the next kf?
2202      if ((active_max_gf_interval <= rc->frames_to_key) &&
2203          (active_max_gf_interval >= (rc->frames_to_key - rc->min_gf_interval)))
2204        active_max_gf_interval = rc->frames_to_key / 2;
2205    }
2206  }
2207
2208  i = 0;
2209  while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
2210    ++i;
2211
2212    // Accumulate error score of frames in this gf group.
2213    mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
2214    gf_group_err += mod_frame_err;
2215    gf_group_raw_error += this_frame->coded_error;
2216    gf_group_noise += this_frame->frame_noise_energy;
2217    gf_group_skip_pct += this_frame->intra_skip_pct;
2218    gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
2219    gf_group_inter += this_frame->pcnt_inter;
2220    gf_group_motion += this_frame->pcnt_motion;
2221
2222    if (EOF == input_stats(twopass, &next_frame)) break;
2223
2224    // Test for the case where there is a brief flash but the prediction
2225    // quality back to an earlier frame is then restored.
2226    flash_detected = detect_flash(twopass, 0);
2227
2228    // Update the motion related elements to the boost calculation.
2229    accumulate_frame_motion_stats(
2230        &next_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
2231        &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
2232
2233    // Accumulate the effect of prediction quality decay.
2234    if (!flash_detected) {
2235      last_loop_decay_rate = loop_decay_rate;
2236      loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2237
2238      decay_accumulator = decay_accumulator * loop_decay_rate;
2239
2240      // Monitor for static sections.
2241      zero_motion_accumulator = VPXMIN(
2242          zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
2243
2244      // Break clause to detect very still sections after motion. For example,
2245      // a static image after a fade or other transition.
2246      if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
2247                                     last_loop_decay_rate)) {
2248        allow_alt_ref = 0;
2249        break;
2250      }
2251    }
2252
2253    // Calculate a boost number for this frame.
2254    sr_accumulator = 0.0;
2255    boost_score += decay_accumulator *
2256                   calc_frame_boost(cpi, &next_frame, &sr_accumulator,
2257                                    this_frame_mv_in_out, GF_MAX_BOOST);
2258
2259    // Break out conditions.
2260    if (
2261        // Break at active_max_gf_interval unless almost totally static.
2262        ((i >= active_max_gf_interval) && (zero_motion_accumulator < 0.995)) ||
2263        (
2264            // Don't break out with a very short interval.
2265            (i >= active_min_gf_interval) &&
2266            // If possible dont break very close to a kf
2267            ((rc->frames_to_key - i) >= rc->min_gf_interval) &&
2268            (!flash_detected) &&
2269            ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
2270             (abs_mv_in_out_accumulator > abs_mv_in_out_thresh) ||
2271             (mv_in_out_accumulator < -mv_in_out_thresh) ||
2272             ((boost_score - old_boost_score) < BOOST_BREAKOUT)))) {
2273      boost_score = old_boost_score;
2274      break;
2275    }
2276
2277    *this_frame = next_frame;
2278    old_boost_score = boost_score;
2279  }
2280
2281  // Was the group length constrained by the requirement for a new KF?
2282  rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0;
2283
2284  // Should we use the alternate reference frame.
2285  if (allow_alt_ref && (i < cpi->oxcf.lag_in_frames) &&
2286      (i >= rc->min_gf_interval)) {
2287    // Calculate the boost for alt ref.
2288    rc->gfu_boost =
2289        calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
2290    rc->source_alt_ref_pending = 1;
2291
2292    // Test to see if multi arf is appropriate.
2293    cpi->multi_arf_enabled =
2294        (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) &&
2295         (zero_motion_accumulator < 0.995))
2296            ? 1
2297            : 0;
2298  } else {
2299    rc->gfu_boost = VPXMAX((int)boost_score, MIN_ARF_GF_BOOST);
2300    rc->source_alt_ref_pending = 0;
2301  }
2302
2303  // Limit maximum boost based on interval length.
2304  rc->gfu_boost = VPXMIN((int)rc->gfu_boost, i * 200);
2305
2306  // Set the interval until the next gf.
2307  rc->baseline_gf_interval = i - (is_key_frame || rc->source_alt_ref_pending);
2308
2309  // Only encode alt reference frame in temporal base layer. So
2310  // baseline_gf_interval should be multiple of a temporal layer group
2311  // (typically the frame distance between two base layer frames)
2312  if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
2313    int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
2314    int new_gf_interval = (rc->baseline_gf_interval + count) & (~count);
2315    int j;
2316    for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) {
2317      if (EOF == input_stats(twopass, this_frame)) break;
2318      gf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2319      gf_group_raw_error += this_frame->coded_error;
2320      gf_group_noise += this_frame->frame_noise_energy;
2321      gf_group_skip_pct += this_frame->intra_skip_pct;
2322      gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
2323      gf_group_inter += this_frame->pcnt_inter;
2324      gf_group_motion += this_frame->pcnt_motion;
2325    }
2326    rc->baseline_gf_interval = new_gf_interval;
2327  }
2328
2329  rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2330
2331  // Reset the file position.
2332  reset_fpf_position(twopass, start_pos);
2333
2334  // Calculate the bits to be allocated to the gf/arf group as a whole
2335  gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
2336
2337  // Calculate an estimate of the maxq needed for the group.
2338  // We are more agressive about correcting for sections
2339  // where there could be significant overshoot than for easier
2340  // sections where we do not wish to risk creating an overshoot
2341  // of the allocated bit budget.
2342  if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) {
2343    const int vbr_group_bits_per_frame =
2344        (int)(gf_group_bits / rc->baseline_gf_interval);
2345    const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval;
2346    const double group_av_noise = gf_group_noise / rc->baseline_gf_interval;
2347    const double group_av_skip_pct =
2348        gf_group_skip_pct / rc->baseline_gf_interval;
2349    const double group_av_inactive_zone =
2350        ((gf_group_inactive_zone_rows * 2) /
2351         (rc->baseline_gf_interval * (double)cm->mb_rows));
2352    int tmp_q = get_twopass_worst_quality(
2353        cpi, group_av_err, (group_av_skip_pct + group_av_inactive_zone),
2354        group_av_noise, vbr_group_bits_per_frame);
2355    twopass->active_worst_quality =
2356        (tmp_q + (twopass->active_worst_quality * 3)) >> 2;
2357  }
2358
2359  // Context Adjustment of ARNR filter strength
2360  if (rc->baseline_gf_interval > 1) {
2361    adjust_group_arnr_filter(cpi, (gf_group_noise / rc->baseline_gf_interval),
2362                             (gf_group_inter / rc->baseline_gf_interval),
2363                             (gf_group_motion / rc->baseline_gf_interval));
2364  } else {
2365    twopass->arnr_strength_adjustment = 0;
2366  }
2367
2368  // Calculate the extra bits to be used for boosted frame(s)
2369  gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, rc->gfu_boost,
2370                                     gf_group_bits);
2371
2372  // Adjust KF group bits and error remaining.
2373  twopass->kf_group_error_left -= (int64_t)gf_group_err;
2374
2375  // Allocate bits to each of the frames in the GF group.
2376  allocate_gf_group_bits(cpi, gf_group_bits, gf_arf_bits);
2377
2378  // Reset the file position.
2379  reset_fpf_position(twopass, start_pos);
2380
2381  // Calculate a section intra ratio used in setting max loop filter.
2382  if (cpi->common.frame_type != KEY_FRAME) {
2383    twopass->section_intra_rating = calculate_section_intra_ratio(
2384        start_pos, twopass->stats_in_end, rc->baseline_gf_interval);
2385  }
2386
2387  if (oxcf->resize_mode == RESIZE_DYNAMIC) {
2388    // Default to starting GF groups at normal frame size.
2389    cpi->rc.next_frame_size_selector = UNSCALED;
2390  }
2391
2392  // Reset rolling actual and target bits counters for ARF groups.
2393  twopass->rolling_arf_group_target_bits = 0;
2394  twopass->rolling_arf_group_actual_bits = 0;
2395}
2396
2397// Threshold for use of the lagging second reference frame. High second ref
2398// usage may point to a transient event like a flash or occlusion rather than
2399// a real scene cut.
2400#define SECOND_REF_USEAGE_THRESH 0.1
2401// Minimum % intra coding observed in first pass (1.0 = 100%)
2402#define MIN_INTRA_LEVEL 0.25
2403// Minimum ratio between the % of intra coding and inter coding in the first
2404// pass after discounting neutral blocks (discounting neutral blocks in this
2405// way helps catch scene cuts in clips with very flat areas or letter box
2406// format clips with image padding.
2407#define INTRA_VS_INTER_THRESH 2.0
2408// Hard threshold where the first pass chooses intra for almost all blocks.
2409// In such a case even if the frame is not a scene cut coding a key frame
2410// may be a good option.
2411#define VERY_LOW_INTER_THRESH 0.05
2412// Maximum threshold for the relative ratio of intra error score vs best
2413// inter error score.
2414#define KF_II_ERR_THRESHOLD 2.5
2415// In real scene cuts there is almost always a sharp change in the intra
2416// or inter error score.
2417#define ERR_CHANGE_THRESHOLD 0.4
2418// For real scene cuts we expect an improvment in the intra inter error
2419// ratio in the next frame.
2420#define II_IMPROVEMENT_THRESHOLD 3.5
2421#define KF_II_MAX 128.0
2422
2423static int test_candidate_kf(TWO_PASS *twopass,
2424                             const FIRSTPASS_STATS *last_frame,
2425                             const FIRSTPASS_STATS *this_frame,
2426                             const FIRSTPASS_STATS *next_frame) {
2427  int is_viable_kf = 0;
2428  double pcnt_intra = 1.0 - this_frame->pcnt_inter;
2429  double modified_pcnt_inter =
2430      this_frame->pcnt_inter - this_frame->pcnt_neutral;
2431
2432  // Does the frame satisfy the primary criteria of a key frame?
2433  // See above for an explanation of the test criteria.
2434  // If so, then examine how well it predicts subsequent frames.
2435  if ((this_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
2436      (next_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
2437      ((this_frame->pcnt_inter < VERY_LOW_INTER_THRESH) ||
2438       ((pcnt_intra > MIN_INTRA_LEVEL) &&
2439        (pcnt_intra > (INTRA_VS_INTER_THRESH * modified_pcnt_inter)) &&
2440        ((this_frame->intra_error /
2441          DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) <
2442         KF_II_ERR_THRESHOLD) &&
2443        ((fabs(last_frame->coded_error - this_frame->coded_error) /
2444              DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
2445          ERR_CHANGE_THRESHOLD) ||
2446         (fabs(last_frame->intra_error - this_frame->intra_error) /
2447              DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
2448          ERR_CHANGE_THRESHOLD) ||
2449         ((next_frame->intra_error /
2450           DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) >
2451          II_IMPROVEMENT_THRESHOLD))))) {
2452    int i;
2453    const FIRSTPASS_STATS *start_pos = twopass->stats_in;
2454    FIRSTPASS_STATS local_next_frame = *next_frame;
2455    double boost_score = 0.0;
2456    double old_boost_score = 0.0;
2457    double decay_accumulator = 1.0;
2458
2459    // Examine how well the key frame predicts subsequent frames.
2460    for (i = 0; i < 16; ++i) {
2461      double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error /
2462                             DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
2463
2464      if (next_iiratio > KF_II_MAX) next_iiratio = KF_II_MAX;
2465
2466      // Cumulative effect of decay in prediction quality.
2467      if (local_next_frame.pcnt_inter > 0.85)
2468        decay_accumulator *= local_next_frame.pcnt_inter;
2469      else
2470        decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
2471
2472      // Keep a running total.
2473      boost_score += (decay_accumulator * next_iiratio);
2474
2475      // Test various breakout clauses.
2476      if ((local_next_frame.pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
2477          (((local_next_frame.pcnt_inter - local_next_frame.pcnt_neutral) <
2478            0.20) &&
2479           (next_iiratio < 3.0)) ||
2480          ((boost_score - old_boost_score) < 3.0) ||
2481          (local_next_frame.intra_error < 200)) {
2482        break;
2483      }
2484
2485      old_boost_score = boost_score;
2486
2487      // Get the next frame details
2488      if (EOF == input_stats(twopass, &local_next_frame)) break;
2489    }
2490
2491    // If there is tolerable prediction for at least the next 3 frames then
2492    // break out else discard this potential key frame and move on
2493    if (boost_score > 30.0 && (i > 3)) {
2494      is_viable_kf = 1;
2495    } else {
2496      // Reset the file position
2497      reset_fpf_position(twopass, start_pos);
2498
2499      is_viable_kf = 0;
2500    }
2501  }
2502
2503  return is_viable_kf;
2504}
2505
2506#define FRAMES_TO_CHECK_DECAY 8
2507#define KF_MAX_FRAME_BOOST 96.0
2508#define MIN_KF_TOT_BOOST 300
2509#define MAX_KF_TOT_BOOST 5400
2510#define KF_BOOST_SCAN_MAX_FRAMES 32
2511
2512static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2513  int i, j;
2514  RATE_CONTROL *const rc = &cpi->rc;
2515  TWO_PASS *const twopass = &cpi->twopass;
2516  GF_GROUP *const gf_group = &twopass->gf_group;
2517  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2518  const FIRSTPASS_STATS first_frame = *this_frame;
2519  const FIRSTPASS_STATS *const start_position = twopass->stats_in;
2520  FIRSTPASS_STATS next_frame;
2521  FIRSTPASS_STATS last_frame;
2522  int kf_bits = 0;
2523  double decay_accumulator = 1.0;
2524  double zero_motion_accumulator = 1.0;
2525  double boost_score = 0.0;
2526  double kf_mod_err = 0.0;
2527  double kf_group_err = 0.0;
2528  double recent_loop_decay[FRAMES_TO_CHECK_DECAY];
2529  double sr_accumulator = 0.0;
2530
2531  vp9_zero(next_frame);
2532
2533  cpi->common.frame_type = KEY_FRAME;
2534
2535  // Reset the GF group data structures.
2536  vp9_zero(*gf_group);
2537
2538  // Is this a forced key frame by interval.
2539  rc->this_key_frame_forced = rc->next_key_frame_forced;
2540
2541  // Clear the alt ref active flag and last group multi arf flags as they
2542  // can never be set for a key frame.
2543  rc->source_alt_ref_active = 0;
2544  cpi->multi_arf_last_grp_enabled = 0;
2545
2546  // KF is always a GF so clear frames till next gf counter.
2547  rc->frames_till_gf_update_due = 0;
2548
2549  rc->frames_to_key = 1;
2550
2551  twopass->kf_group_bits = 0;        // Total bits available to kf group
2552  twopass->kf_group_error_left = 0;  // Group modified error score.
2553
2554  kf_mod_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
2555
2556  // Initialize the decay rates for the recent frames to check
2557  for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j) recent_loop_decay[j] = 1.0;
2558
2559  // Find the next keyframe.
2560  i = 0;
2561  while (twopass->stats_in < twopass->stats_in_end &&
2562         rc->frames_to_key < cpi->oxcf.key_freq) {
2563    // Accumulate kf group error.
2564    kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2565
2566    // Load the next frame's stats.
2567    last_frame = *this_frame;
2568    input_stats(twopass, this_frame);
2569
2570    // Provided that we are not at the end of the file...
2571    if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) {
2572      double loop_decay_rate;
2573
2574      // Check for a scene cut.
2575      if (test_candidate_kf(twopass, &last_frame, this_frame,
2576                            twopass->stats_in))
2577        break;
2578
2579      // How fast is the prediction quality decaying?
2580      loop_decay_rate = get_prediction_decay_rate(cpi, twopass->stats_in);
2581
2582      // We want to know something about the recent past... rather than
2583      // as used elsewhere where we are concerned with decay in prediction
2584      // quality since the last GF or KF.
2585      recent_loop_decay[i % FRAMES_TO_CHECK_DECAY] = loop_decay_rate;
2586      decay_accumulator = 1.0;
2587      for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
2588        decay_accumulator *= recent_loop_decay[j];
2589
2590      // Special check for transition or high motion followed by a
2591      // static scene.
2592      if (detect_transition_to_still(cpi, i, cpi->oxcf.key_freq - i,
2593                                     loop_decay_rate, decay_accumulator))
2594        break;
2595
2596      // Step on to the next frame.
2597      ++rc->frames_to_key;
2598
2599      // If we don't have a real key frame within the next two
2600      // key_freq intervals then break out of the loop.
2601      if (rc->frames_to_key >= 2 * cpi->oxcf.key_freq) break;
2602    } else {
2603      ++rc->frames_to_key;
2604    }
2605    ++i;
2606  }
2607
2608  // If there is a max kf interval set by the user we must obey it.
2609  // We already breakout of the loop above at 2x max.
2610  // This code centers the extra kf if the actual natural interval
2611  // is between 1x and 2x.
2612  if (cpi->oxcf.auto_key && rc->frames_to_key > cpi->oxcf.key_freq) {
2613    FIRSTPASS_STATS tmp_frame = first_frame;
2614
2615    rc->frames_to_key /= 2;
2616
2617    // Reset to the start of the group.
2618    reset_fpf_position(twopass, start_position);
2619
2620    kf_group_err = 0.0;
2621
2622    // Rescan to get the correct error data for the forced kf group.
2623    for (i = 0; i < rc->frames_to_key; ++i) {
2624      kf_group_err += calculate_modified_err(cpi, twopass, oxcf, &tmp_frame);
2625      input_stats(twopass, &tmp_frame);
2626    }
2627    rc->next_key_frame_forced = 1;
2628  } else if (twopass->stats_in == twopass->stats_in_end ||
2629             rc->frames_to_key >= cpi->oxcf.key_freq) {
2630    rc->next_key_frame_forced = 1;
2631  } else {
2632    rc->next_key_frame_forced = 0;
2633  }
2634
2635  if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
2636    int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
2637    int new_frame_to_key = (rc->frames_to_key + count) & (~count);
2638    int j;
2639    for (j = 0; j < new_frame_to_key - rc->frames_to_key; ++j) {
2640      if (EOF == input_stats(twopass, this_frame)) break;
2641      kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2642    }
2643    rc->frames_to_key = new_frame_to_key;
2644  }
2645
2646  // Special case for the last key frame of the file.
2647  if (twopass->stats_in >= twopass->stats_in_end) {
2648    // Accumulate kf group error.
2649    kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2650  }
2651
2652  // Calculate the number of bits that should be assigned to the kf group.
2653  if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) {
2654    // Maximum number of bits for a single normal frame (not key frame).
2655    const int max_bits = frame_max_bits(rc, &cpi->oxcf);
2656
2657    // Maximum number of bits allocated to the key frame group.
2658    int64_t max_grp_bits;
2659
2660    // Default allocation based on bits left and relative
2661    // complexity of the section.
2662    twopass->kf_group_bits = (int64_t)(
2663        twopass->bits_left * (kf_group_err / twopass->modified_error_left));
2664
2665    // Clip based on maximum per frame rate defined by the user.
2666    max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
2667    if (twopass->kf_group_bits > max_grp_bits)
2668      twopass->kf_group_bits = max_grp_bits;
2669  } else {
2670    twopass->kf_group_bits = 0;
2671  }
2672  twopass->kf_group_bits = VPXMAX(0, twopass->kf_group_bits);
2673
2674  // Reset the first pass file position.
2675  reset_fpf_position(twopass, start_position);
2676
2677  // Scan through the kf group collating various stats used to determine
2678  // how many bits to spend on it.
2679  boost_score = 0.0;
2680
2681  for (i = 0; i < (rc->frames_to_key - 1); ++i) {
2682    if (EOF == input_stats(twopass, &next_frame)) break;
2683
2684    if (i <= KF_BOOST_SCAN_MAX_FRAMES) {
2685      double frame_boost;
2686      double zm_factor;
2687
2688      // Monitor for static sections.
2689      zero_motion_accumulator = VPXMIN(
2690          zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
2691
2692      // Factor 0.75-1.25 based on how much of frame is static.
2693      zm_factor = (0.75 + (zero_motion_accumulator / 2.0));
2694
2695      // The second (lagging) ref error is not valid immediately after
2696      // a key frame because either the lag has not built up (in the case of
2697      // the first key frame or it points to a refernce before the new key
2698      // frame.
2699      if (i < 2) sr_accumulator = 0.0;
2700      frame_boost = calc_kf_frame_boost(cpi, &next_frame, &sr_accumulator, 0,
2701                                        KF_MAX_FRAME_BOOST * zm_factor);
2702
2703      boost_score += frame_boost;
2704      if (frame_boost < 25.00) break;
2705    } else {
2706      break;
2707    }
2708  }
2709
2710  reset_fpf_position(twopass, start_position);
2711
2712  // Store the zero motion percentage
2713  twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2714
2715  // Calculate a section intra ratio used in setting max loop filter.
2716  twopass->section_intra_rating = calculate_section_intra_ratio(
2717      start_position, twopass->stats_in_end, rc->frames_to_key);
2718
2719  // Apply various clamps for min and max boost
2720  rc->kf_boost = VPXMAX((int)boost_score, (rc->frames_to_key * 3));
2721  rc->kf_boost = VPXMAX(rc->kf_boost, MIN_KF_TOT_BOOST);
2722  rc->kf_boost = VPXMIN(rc->kf_boost, MAX_KF_TOT_BOOST);
2723
2724  // Work out how many bits to allocate for the key frame itself.
2725  kf_bits = calculate_boost_bits((rc->frames_to_key - 1), rc->kf_boost,
2726                                 twopass->kf_group_bits);
2727
2728  twopass->kf_group_bits -= kf_bits;
2729
2730  // Save the bits to spend on the key frame.
2731  gf_group->bit_allocation[0] = kf_bits;
2732  gf_group->update_type[0] = KF_UPDATE;
2733  gf_group->rf_level[0] = KF_STD;
2734
2735  // Note the total error score of the kf group minus the key frame itself.
2736  twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2737
2738  // Adjust the count of total modified error left.
2739  // The count of bits left is adjusted elsewhere based on real coded frame
2740  // sizes.
2741  twopass->modified_error_left -= kf_group_err;
2742
2743  if (oxcf->resize_mode == RESIZE_DYNAMIC) {
2744    // Default to normal-sized frame on keyframes.
2745    cpi->rc.next_frame_size_selector = UNSCALED;
2746  }
2747}
2748
2749// Define the reference buffers that will be updated post encode.
2750static void configure_buffer_updates(VP9_COMP *cpi) {
2751  TWO_PASS *const twopass = &cpi->twopass;
2752
2753  cpi->rc.is_src_frame_alt_ref = 0;
2754  switch (twopass->gf_group.update_type[twopass->gf_group.index]) {
2755    case KF_UPDATE:
2756      cpi->refresh_last_frame = 1;
2757      cpi->refresh_golden_frame = 1;
2758      cpi->refresh_alt_ref_frame = 1;
2759      break;
2760    case LF_UPDATE:
2761      cpi->refresh_last_frame = 1;
2762      cpi->refresh_golden_frame = 0;
2763      cpi->refresh_alt_ref_frame = 0;
2764      break;
2765    case GF_UPDATE:
2766      cpi->refresh_last_frame = 1;
2767      cpi->refresh_golden_frame = 1;
2768      cpi->refresh_alt_ref_frame = 0;
2769      break;
2770    case OVERLAY_UPDATE:
2771      cpi->refresh_last_frame = 0;
2772      cpi->refresh_golden_frame = 1;
2773      cpi->refresh_alt_ref_frame = 0;
2774      cpi->rc.is_src_frame_alt_ref = 1;
2775      break;
2776    case ARF_UPDATE:
2777      cpi->refresh_last_frame = 0;
2778      cpi->refresh_golden_frame = 0;
2779      cpi->refresh_alt_ref_frame = 1;
2780      break;
2781    default: assert(0); break;
2782  }
2783  if (is_two_pass_svc(cpi)) {
2784    if (cpi->svc.temporal_layer_id > 0) {
2785      cpi->refresh_last_frame = 0;
2786      cpi->refresh_golden_frame = 0;
2787    }
2788    if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0)
2789      cpi->refresh_golden_frame = 0;
2790    if (cpi->alt_ref_source == NULL) cpi->refresh_alt_ref_frame = 0;
2791  }
2792}
2793
2794static int is_skippable_frame(const VP9_COMP *cpi) {
2795  // If the current frame does not have non-zero motion vector detected in the
2796  // first  pass, and so do its previous and forward frames, then this frame
2797  // can be skipped for partition check, and the partition size is assigned
2798  // according to the variance
2799  const SVC *const svc = &cpi->svc;
2800  const TWO_PASS *const twopass =
2801      is_two_pass_svc(cpi) ? &svc->layer_context[svc->spatial_layer_id].twopass
2802                           : &cpi->twopass;
2803
2804  return (!frame_is_intra_only(&cpi->common) &&
2805          twopass->stats_in - 2 > twopass->stats_in_start &&
2806          twopass->stats_in < twopass->stats_in_end &&
2807          (twopass->stats_in - 1)->pcnt_inter -
2808                  (twopass->stats_in - 1)->pcnt_motion ==
2809              1 &&
2810          (twopass->stats_in - 2)->pcnt_inter -
2811                  (twopass->stats_in - 2)->pcnt_motion ==
2812              1 &&
2813          twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
2814}
2815
2816void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2817  VP9_COMMON *const cm = &cpi->common;
2818  RATE_CONTROL *const rc = &cpi->rc;
2819  TWO_PASS *const twopass = &cpi->twopass;
2820  GF_GROUP *const gf_group = &twopass->gf_group;
2821  FIRSTPASS_STATS this_frame;
2822
2823  int target_rate;
2824  LAYER_CONTEXT *const lc =
2825      is_two_pass_svc(cpi) ? &cpi->svc.layer_context[cpi->svc.spatial_layer_id]
2826                           : 0;
2827
2828  if (!twopass->stats_in) return;
2829
2830  // If this is an arf frame then we dont want to read the stats file or
2831  // advance the input pointer as we already have what we need.
2832  if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2833    int target_rate;
2834    configure_buffer_updates(cpi);
2835    target_rate = gf_group->bit_allocation[gf_group->index];
2836    target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2837    rc->base_frame_target = target_rate;
2838
2839    cm->frame_type = INTER_FRAME;
2840
2841    if (lc != NULL) {
2842      if (cpi->svc.spatial_layer_id == 0) {
2843        lc->is_key_frame = 0;
2844      } else {
2845        lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2846
2847        if (lc->is_key_frame) cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2848      }
2849    }
2850
2851    // Do the firstpass stats indicate that this frame is skippable for the
2852    // partition search?
2853    if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
2854        (!cpi->use_svc || is_two_pass_svc(cpi))) {
2855      cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
2856    }
2857
2858    return;
2859  }
2860
2861  vpx_clear_system_state();
2862
2863  if (cpi->oxcf.rc_mode == VPX_Q) {
2864    twopass->active_worst_quality = cpi->oxcf.cq_level;
2865  } else if (cm->current_video_frame == 0 ||
2866             (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2867    const int frames_left =
2868        (int)(twopass->total_stats.count -
2869              ((lc != NULL) ? lc->current_video_frame_in_layer
2870                            : cm->current_video_frame));
2871    // Special case code for first frame.
2872    const int section_target_bandwidth =
2873        (int)(twopass->bits_left / frames_left);
2874    const double section_length = twopass->total_left_stats.count;
2875    const double section_error =
2876        twopass->total_left_stats.coded_error / section_length;
2877    const double section_intra_skip =
2878        twopass->total_left_stats.intra_skip_pct / section_length;
2879    const double section_inactive_zone =
2880        (twopass->total_left_stats.inactive_zone_rows * 2) /
2881        ((double)cm->mb_rows * section_length);
2882    const double section_noise =
2883        twopass->total_left_stats.frame_noise_energy / section_length;
2884    int tmp_q;
2885
2886    tmp_q = get_twopass_worst_quality(
2887        cpi, section_error, section_intra_skip + section_inactive_zone,
2888        section_noise, section_target_bandwidth);
2889
2890    twopass->active_worst_quality = tmp_q;
2891    twopass->baseline_active_worst_quality = tmp_q;
2892    rc->ni_av_qi = tmp_q;
2893    rc->last_q[INTER_FRAME] = tmp_q;
2894    rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
2895    rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
2896    rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2;
2897    rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
2898  }
2899  vp9_zero(this_frame);
2900  if (EOF == input_stats(twopass, &this_frame)) return;
2901
2902  // Set the frame content type flag.
2903  if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH)
2904    twopass->fr_content_type = FC_GRAPHICS_ANIMATION;
2905  else
2906    twopass->fr_content_type = FC_NORMAL;
2907
2908  // Keyframe and section processing.
2909  if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2910    FIRSTPASS_STATS this_frame_copy;
2911    this_frame_copy = this_frame;
2912    // Define next KF group and assign bits to it.
2913    find_next_key_frame(cpi, &this_frame);
2914    this_frame = this_frame_copy;
2915  } else {
2916    cm->frame_type = INTER_FRAME;
2917  }
2918
2919  if (lc != NULL) {
2920    if (cpi->svc.spatial_layer_id == 0) {
2921      lc->is_key_frame = (cm->frame_type == KEY_FRAME);
2922      if (lc->is_key_frame) {
2923        cpi->ref_frame_flags &=
2924            (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2925        lc->frames_from_key_frame = 0;
2926        // Encode an intra only empty frame since we have a key frame.
2927        cpi->svc.encode_intra_empty_frame = 1;
2928      }
2929    } else {
2930      cm->frame_type = INTER_FRAME;
2931      lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2932
2933      if (lc->is_key_frame) {
2934        cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2935        lc->frames_from_key_frame = 0;
2936      }
2937    }
2938  }
2939
2940  // Define a new GF/ARF group. (Should always enter here for key frames).
2941  if (rc->frames_till_gf_update_due == 0) {
2942    define_gf_group(cpi, &this_frame);
2943
2944    rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2945    if (lc != NULL) cpi->refresh_golden_frame = 1;
2946
2947#if ARF_STATS_OUTPUT
2948    {
2949      FILE *fpfile;
2950      fpfile = fopen("arf.stt", "a");
2951      ++arf_count;
2952      fprintf(fpfile, "%10d %10ld %10d %10d %10ld\n", cm->current_video_frame,
2953              rc->frames_till_gf_update_due, rc->kf_boost, arf_count,
2954              rc->gfu_boost);
2955
2956      fclose(fpfile);
2957    }
2958#endif
2959  }
2960
2961  configure_buffer_updates(cpi);
2962
2963  // Do the firstpass stats indicate that this frame is skippable for the
2964  // partition search?
2965  if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
2966      (!cpi->use_svc || is_two_pass_svc(cpi))) {
2967    cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
2968  }
2969
2970  target_rate = gf_group->bit_allocation[gf_group->index];
2971  rc->base_frame_target = target_rate;
2972
2973  {
2974    const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
2975                            ? cpi->initial_mbs
2976                            : cpi->common.MBs;
2977    // The multiplication by 256 reverses a scaling factor of (>> 8)
2978    // applied when combining MB error values for the frame.
2979    twopass->mb_av_energy =
2980        log(((this_frame.intra_error * 256.0) / num_mbs) + 1.0);
2981    twopass->mb_smooth_pct = this_frame.intra_smooth_pct;
2982  }
2983
2984  // Update the total stats remaining structure.
2985  subtract_stats(&twopass->total_left_stats, &this_frame);
2986}
2987
2988#define MINQ_ADJ_LIMIT 48
2989#define MINQ_ADJ_LIMIT_CQ 20
2990#define HIGH_UNDERSHOOT_RATIO 2
2991void vp9_twopass_postencode_update(VP9_COMP *cpi) {
2992  TWO_PASS *const twopass = &cpi->twopass;
2993  RATE_CONTROL *const rc = &cpi->rc;
2994  VP9_COMMON *const cm = &cpi->common;
2995  const int bits_used = rc->base_frame_target;
2996
2997  // VBR correction is done through rc->vbr_bits_off_target. Based on the
2998  // sign of this value, a limited % adjustment is made to the target rate
2999  // of subsequent frames, to try and push it back towards 0. This method
3000  // is designed to prevent extreme behaviour at the end of a clip
3001  // or group of frames.
3002  rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
3003  twopass->bits_left = VPXMAX(twopass->bits_left - bits_used, 0);
3004
3005  // Target vs actual bits for this arf group.
3006  twopass->rolling_arf_group_target_bits += rc->this_frame_target;
3007  twopass->rolling_arf_group_actual_bits += rc->projected_frame_size;
3008
3009  // Calculate the pct rc error.
3010  if (rc->total_actual_bits) {
3011    rc->rate_error_estimate =
3012        (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
3013    rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
3014  } else {
3015    rc->rate_error_estimate = 0;
3016  }
3017
3018  if (cpi->common.frame_type != KEY_FRAME &&
3019      !vp9_is_upper_layer_key_frame(cpi)) {
3020    twopass->kf_group_bits -= bits_used;
3021    twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
3022  }
3023  twopass->kf_group_bits = VPXMAX(twopass->kf_group_bits, 0);
3024
3025  // Increment the gf group index ready for the next frame.
3026  ++twopass->gf_group.index;
3027
3028  // If the rate control is drifting consider adjustment to min or maxq.
3029  if ((cpi->oxcf.rc_mode != VPX_Q) && !cpi->rc.is_src_frame_alt_ref) {
3030    const int maxq_adj_limit =
3031        rc->worst_quality - twopass->active_worst_quality;
3032    const int minq_adj_limit =
3033        (cpi->oxcf.rc_mode == VPX_CQ ? MINQ_ADJ_LIMIT_CQ : MINQ_ADJ_LIMIT);
3034    int aq_extend_min = 0;
3035    int aq_extend_max = 0;
3036
3037    // Extend min or Max Q range to account for imbalance from the base
3038    // value when using AQ.
3039    if (cpi->oxcf.aq_mode != NO_AQ) {
3040      if (cm->seg.aq_av_offset < 0) {
3041        // The balance of the AQ map tends towarda lowering the average Q.
3042        aq_extend_min = 0;
3043        aq_extend_max = VPXMIN(maxq_adj_limit, -cm->seg.aq_av_offset);
3044      } else {
3045        // The balance of the AQ map tends towards raising the average Q.
3046        aq_extend_min = VPXMIN(minq_adj_limit, cm->seg.aq_av_offset);
3047        aq_extend_max = 0;
3048      }
3049    }
3050
3051    // Undershoot.
3052    if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) {
3053      --twopass->extend_maxq;
3054      if (rc->rolling_target_bits >= rc->rolling_actual_bits)
3055        ++twopass->extend_minq;
3056      // Overshoot.
3057    } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) {
3058      --twopass->extend_minq;
3059      if (rc->rolling_target_bits < rc->rolling_actual_bits)
3060        ++twopass->extend_maxq;
3061    } else {
3062      // Adjustment for extreme local overshoot.
3063      if (rc->projected_frame_size > (2 * rc->base_frame_target) &&
3064          rc->projected_frame_size > (2 * rc->avg_frame_bandwidth))
3065        ++twopass->extend_maxq;
3066
3067      // Unwind undershoot or overshoot adjustment.
3068      if (rc->rolling_target_bits < rc->rolling_actual_bits)
3069        --twopass->extend_minq;
3070      else if (rc->rolling_target_bits > rc->rolling_actual_bits)
3071        --twopass->extend_maxq;
3072    }
3073
3074    twopass->extend_minq =
3075        clamp(twopass->extend_minq, aq_extend_min, minq_adj_limit);
3076    twopass->extend_maxq =
3077        clamp(twopass->extend_maxq, aq_extend_max, maxq_adj_limit);
3078
3079    // If there is a big and undexpected undershoot then feed the extra
3080    // bits back in quickly. One situation where this may happen is if a
3081    // frame is unexpectedly almost perfectly predicted by the ARF or GF
3082    // but not very well predcited by the previous frame.
3083    if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) {
3084      int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO;
3085      if (rc->projected_frame_size < fast_extra_thresh) {
3086        rc->vbr_bits_off_target_fast +=
3087            fast_extra_thresh - rc->projected_frame_size;
3088        rc->vbr_bits_off_target_fast =
3089            VPXMIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth));
3090
3091        // Fast adaptation of minQ if necessary to use up the extra bits.
3092        if (rc->avg_frame_bandwidth) {
3093          twopass->extend_minq_fast =
3094              (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth);
3095        }
3096        twopass->extend_minq_fast = VPXMIN(
3097            twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
3098      } else if (rc->vbr_bits_off_target_fast) {
3099        twopass->extend_minq_fast = VPXMIN(
3100            twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
3101      } else {
3102        twopass->extend_minq_fast = 0;
3103      }
3104    }
3105  }
3106}
3107