LCOV - code coverage report
Current view: top level - media/libvpx/libvpx/vp9/encoder - vp9_firstpass.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 1451 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 53 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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
      68             : unsigned 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.
      73           0 : static void reset_fpf_position(TWO_PASS *p, const FIRSTPASS_STATS *position) {
      74           0 :   p->stats_in = position;
      75           0 : }
      76             : 
      77             : // Read frame stats at an offset from the current position.
      78           0 : static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) {
      79           0 :   if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) ||
      80           0 :       (offset < 0 && p->stats_in + offset < p->stats_in_start)) {
      81           0 :     return NULL;
      82             :   }
      83             : 
      84           0 :   return &p->stats_in[offset];
      85             : }
      86             : 
      87           0 : static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) {
      88           0 :   if (p->stats_in >= p->stats_in_end) return EOF;
      89             : 
      90           0 :   *fps = *p->stats_in;
      91           0 :   ++p->stats_in;
      92           0 :   return 1;
      93             : }
      94             : 
      95           0 : static void output_stats(FIRSTPASS_STATS *stats,
      96             :                          struct vpx_codec_pkt_list *pktlist) {
      97             :   struct vpx_codec_cx_pkt pkt;
      98           0 :   pkt.kind = VPX_CODEC_STATS_PKT;
      99           0 :   pkt.data.twopass_stats.buf = stats;
     100           0 :   pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
     101           0 :   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           0 : }
     125             : 
     126             : #if CONFIG_FP_MB_STATS
     127             : static 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             : 
     137           0 : static void zero_stats(FIRSTPASS_STATS *section) {
     138           0 :   section->frame = 0.0;
     139           0 :   section->weight = 0.0;
     140           0 :   section->intra_error = 0.0;
     141           0 :   section->coded_error = 0.0;
     142           0 :   section->sr_coded_error = 0.0;
     143           0 :   section->frame_noise_energy = 0.0;
     144           0 :   section->pcnt_inter = 0.0;
     145           0 :   section->pcnt_motion = 0.0;
     146           0 :   section->pcnt_second_ref = 0.0;
     147           0 :   section->pcnt_neutral = 0.0;
     148           0 :   section->intra_skip_pct = 0.0;
     149           0 :   section->intra_smooth_pct = 0.0;
     150           0 :   section->inactive_zone_rows = 0.0;
     151           0 :   section->inactive_zone_cols = 0.0;
     152           0 :   section->MVr = 0.0;
     153           0 :   section->mvr_abs = 0.0;
     154           0 :   section->MVc = 0.0;
     155           0 :   section->mvc_abs = 0.0;
     156           0 :   section->MVrv = 0.0;
     157           0 :   section->MVcv = 0.0;
     158           0 :   section->mv_in_out_count = 0.0;
     159           0 :   section->count = 0.0;
     160           0 :   section->duration = 1.0;
     161           0 :   section->spatial_layer_id = 0;
     162           0 : }
     163             : 
     164           0 : static void accumulate_stats(FIRSTPASS_STATS *section,
     165             :                              const FIRSTPASS_STATS *frame) {
     166           0 :   section->frame += frame->frame;
     167           0 :   section->weight += frame->weight;
     168           0 :   section->spatial_layer_id = frame->spatial_layer_id;
     169           0 :   section->intra_error += frame->intra_error;
     170           0 :   section->coded_error += frame->coded_error;
     171           0 :   section->sr_coded_error += frame->sr_coded_error;
     172           0 :   section->frame_noise_energy += frame->frame_noise_energy;
     173           0 :   section->pcnt_inter += frame->pcnt_inter;
     174           0 :   section->pcnt_motion += frame->pcnt_motion;
     175           0 :   section->pcnt_second_ref += frame->pcnt_second_ref;
     176           0 :   section->pcnt_neutral += frame->pcnt_neutral;
     177           0 :   section->intra_skip_pct += frame->intra_skip_pct;
     178           0 :   section->intra_smooth_pct += frame->intra_smooth_pct;
     179           0 :   section->inactive_zone_rows += frame->inactive_zone_rows;
     180           0 :   section->inactive_zone_cols += frame->inactive_zone_cols;
     181           0 :   section->MVr += frame->MVr;
     182           0 :   section->mvr_abs += frame->mvr_abs;
     183           0 :   section->MVc += frame->MVc;
     184           0 :   section->mvc_abs += frame->mvc_abs;
     185           0 :   section->MVrv += frame->MVrv;
     186           0 :   section->MVcv += frame->MVcv;
     187           0 :   section->mv_in_out_count += frame->mv_in_out_count;
     188           0 :   section->count += frame->count;
     189           0 :   section->duration += frame->duration;
     190           0 : }
     191             : 
     192           0 : static void subtract_stats(FIRSTPASS_STATS *section,
     193             :                            const FIRSTPASS_STATS *frame) {
     194           0 :   section->frame -= frame->frame;
     195           0 :   section->weight -= frame->weight;
     196           0 :   section->intra_error -= frame->intra_error;
     197           0 :   section->coded_error -= frame->coded_error;
     198           0 :   section->sr_coded_error -= frame->sr_coded_error;
     199           0 :   section->frame_noise_energy -= frame->frame_noise_energy;
     200           0 :   section->pcnt_inter -= frame->pcnt_inter;
     201           0 :   section->pcnt_motion -= frame->pcnt_motion;
     202           0 :   section->pcnt_second_ref -= frame->pcnt_second_ref;
     203           0 :   section->pcnt_neutral -= frame->pcnt_neutral;
     204           0 :   section->intra_skip_pct -= frame->intra_skip_pct;
     205           0 :   section->intra_smooth_pct -= frame->intra_smooth_pct;
     206           0 :   section->inactive_zone_rows -= frame->inactive_zone_rows;
     207           0 :   section->inactive_zone_cols -= frame->inactive_zone_cols;
     208           0 :   section->MVr -= frame->MVr;
     209           0 :   section->mvr_abs -= frame->mvr_abs;
     210           0 :   section->MVc -= frame->MVc;
     211           0 :   section->mvc_abs -= frame->mvc_abs;
     212           0 :   section->MVrv -= frame->MVrv;
     213           0 :   section->MVcv -= frame->MVcv;
     214           0 :   section->mv_in_out_count -= frame->mv_in_out_count;
     215           0 :   section->count -= frame->count;
     216           0 :   section->duration -= frame->duration;
     217           0 : }
     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
     223           0 : static double calculate_active_area(const VP9_COMP *cpi,
     224             :                                     const FIRSTPASS_STATS *this_frame) {
     225             :   double active_pct;
     226             : 
     227           0 :   active_pct =
     228             :       1.0 -
     229           0 :       ((this_frame->intra_skip_pct / 2) +
     230           0 :        ((this_frame->inactive_zone_rows * 2) / (double)cpi->common.mb_rows));
     231           0 :   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
     237           0 : static double calculate_modified_err(const VP9_COMP *cpi,
     238             :                                      const TWO_PASS *twopass,
     239             :                                      const VP9EncoderConfig *oxcf,
     240             :                                      const FIRSTPASS_STATS *this_frame) {
     241           0 :   const FIRSTPASS_STATS *const stats = &twopass->total_stats;
     242           0 :   const double av_weight = stats->weight / stats->count;
     243           0 :   const double av_err = (stats->coded_error * av_weight) / stats->count;
     244           0 :   double modified_error =
     245           0 :       av_err * pow(this_frame->coded_error * this_frame->weight /
     246           0 :                        DOUBLE_DIVIDE_CHECK(av_err),
     247           0 :                    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           0 :   modified_error *=
     255           0 :       pow(calculate_active_area(cpi, this_frame), ACT_AREA_CORRECTION);
     256             : 
     257           0 :   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.
     262           0 : static int frame_max_bits(const RATE_CONTROL *rc,
     263             :                           const VP9EncoderConfig *oxcf) {
     264           0 :   int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
     265           0 :                       (int64_t)oxcf->two_pass_vbrmax_section) /
     266             :                      100;
     267           0 :   if (max_bits < 0)
     268           0 :     max_bits = 0;
     269           0 :   else if (max_bits > rc->max_frame_bandwidth)
     270           0 :     max_bits = rc->max_frame_bandwidth;
     271             : 
     272           0 :   return (int)max_bits;
     273             : }
     274             : 
     275           0 : void vp9_init_first_pass(VP9_COMP *cpi) {
     276           0 :   zero_stats(&cpi->twopass.total_stats);
     277           0 : }
     278             : 
     279           0 : void vp9_end_first_pass(VP9_COMP *cpi) {
     280           0 :   if (is_two_pass_svc(cpi)) {
     281             :     int i;
     282           0 :     for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
     283           0 :       output_stats(&cpi->svc.layer_context[i].twopass.total_stats,
     284             :                    cpi->output_pkt_list);
     285             :     }
     286             :   } else {
     287           0 :     output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
     288             :   }
     289           0 : }
     290             : 
     291           0 : static vpx_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
     292           0 :   switch (bsize) {
     293           0 :     case BLOCK_8X8: return vpx_mse8x8;
     294           0 :     case BLOCK_16X8: return vpx_mse16x8;
     295           0 :     case BLOCK_8X16: return vpx_mse8x16;
     296           0 :     default: return vpx_mse16x16;
     297             :   }
     298             : }
     299             : 
     300           0 : static 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           0 :   const vpx_variance_fn_t fn = get_block_variance_fn(bsize);
     305           0 :   fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
     306           0 :   return sse;
     307             : }
     308             : 
     309             : #if CONFIG_VP9_HIGHBITDEPTH
     310             : static 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             : 
     340             : static 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.
     353           0 : static int get_search_range(const VP9_COMP *cpi) {
     354           0 :   int sr = 0;
     355           0 :   const int dim = VPXMIN(cpi->initial_width, cpi->initial_height);
     356             : 
     357           0 :   while ((dim << sr) < MAX_FULL_PEL_VAL) ++sr;
     358           0 :   return sr;
     359             : }
     360             : 
     361           0 : static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
     362             :                                      const MV *ref_mv, MV *best_mv,
     363             :                                      int *best_motion_err) {
     364           0 :   MACROBLOCKD *const xd = &x->e_mbd;
     365           0 :   MV tmp_mv = { 0, 0 };
     366           0 :   MV ref_mv_full = { ref_mv->row >> 3, ref_mv->col >> 3 };
     367             :   int num00, tmp_err, n;
     368           0 :   const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
     369           0 :   vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
     370           0 :   const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
     371             : 
     372           0 :   int step_param = 3;
     373           0 :   int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
     374           0 :   const int sr = get_search_range(cpi);
     375           0 :   step_param += sr;
     376           0 :   further_steps -= sr;
     377             : 
     378             :   // Override the default variance function to use MSE.
     379           0 :   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           0 :   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           0 :   if (tmp_err < INT_MAX)
     391           0 :     tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
     392           0 :   if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
     393             : 
     394           0 :   if (tmp_err < *best_motion_err) {
     395           0 :     *best_motion_err = tmp_err;
     396           0 :     *best_mv = tmp_mv;
     397             :   }
     398             : 
     399             :   // Carry out further step/diamond searches as necessary.
     400           0 :   n = num00;
     401           0 :   num00 = 0;
     402             : 
     403           0 :   while (n < further_steps) {
     404           0 :     ++n;
     405             : 
     406           0 :     if (num00) {
     407           0 :       --num00;
     408             :     } else {
     409           0 :       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           0 :       if (tmp_err < INT_MAX)
     413           0 :         tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
     414           0 :       if (tmp_err < INT_MAX - new_mv_mode_penalty)
     415           0 :         tmp_err += new_mv_mode_penalty;
     416             : 
     417           0 :       if (tmp_err < *best_motion_err) {
     418           0 :         *best_motion_err = tmp_err;
     419           0 :         *best_mv = tmp_mv;
     420             :       }
     421             :     }
     422             :   }
     423           0 : }
     424             : 
     425           0 : static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
     426           0 :   if (2 * mb_col + 1 < cm->mi_cols) {
     427           0 :     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 : BLOCK_16X8;
     428             :   } else {
     429           0 :     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 : BLOCK_8X8;
     430             :   }
     431             : }
     432             : 
     433           0 : static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
     434             :   int i;
     435             : 
     436           0 :   for (i = 0; i < QINDEX_RANGE; ++i)
     437           0 :     if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q) break;
     438             : 
     439           0 :   if (i == QINDEX_RANGE) i--;
     440             : 
     441           0 :   return i;
     442             : }
     443             : 
     444           0 : static void set_first_pass_params(VP9_COMP *cpi) {
     445           0 :   VP9_COMMON *const cm = &cpi->common;
     446           0 :   if (!cpi->refresh_alt_ref_frame &&
     447           0 :       (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY))) {
     448           0 :     cm->frame_type = KEY_FRAME;
     449             :   } else {
     450           0 :     cm->frame_type = INTER_FRAME;
     451             :   }
     452             :   // Do not use periodic key frames.
     453           0 :   cpi->rc.frames_to_key = INT_MAX;
     454           0 : }
     455             : 
     456             : // Scale an sse threshold to account for 8/10/12 bit.
     457           0 : static int scale_sse_threshold(VP9_COMMON *cm, int thresh) {
     458           0 :   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           0 :   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
     483           0 : static int get_ul_intra_threshold(VP9_COMMON *cm) {
     484           0 :   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           0 :   return ret_val;
     501             : }
     502             : 
     503             : #define SMOOTH_INTRA_THRESH 4000
     504           0 : static int get_smooth_intra_threshold(VP9_COMMON *cm) {
     505           0 :   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           0 :   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
     529             : static 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
     534           0 : static int fp_estimate_point_noise(uint8_t *src_ptr, const int stride) {
     535           0 :   int sum_weight = 0;
     536           0 :   int sum_val = 0;
     537             :   int i, j;
     538           0 :   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           0 :   uint8_t centre_val = *src_ptr;
     545             : 
     546           0 :   kernal_ptr = fp_dn_kernal_3;
     547             : 
     548             :   // Apply the kernal
     549           0 :   tmp_ptr = src_ptr - stride - 1;
     550           0 :   for (i = 0; i < KERNEL_SIZE; ++i) {
     551           0 :     for (j = 0; j < KERNEL_SIZE; ++j) {
     552           0 :       diff = abs((int)centre_val - (int)tmp_ptr[j]);
     553           0 :       max_diff = VPXMAX(max_diff, diff);
     554           0 :       if (diff <= FP_DN_THRESH) {
     555           0 :         sum_weight += *kernal_ptr;
     556           0 :         sum_val += (int)tmp_ptr[j] * (int)*kernal_ptr;
     557             :       }
     558           0 :       ++kernal_ptr;
     559             :     }
     560           0 :     tmp_ptr += stride;
     561             :   }
     562             : 
     563           0 :   if (max_diff < FP_MAX_DN_THRESH)
     564             :     // Update the source value with the new filtered value
     565           0 :     dn_val = (sum_val + (sum_weight >> 1)) / sum_weight;
     566             :   else
     567           0 :     dn_val = *src_ptr;
     568             : 
     569             :   // return the noise energy as the square of the difference between the
     570             :   // denoised and raw value.
     571           0 :   dn_diff = (int)*src_ptr - (int)dn_val;
     572           0 :   return dn_diff * dn_diff;
     573             : }
     574             : #if CONFIG_VP9_HIGHBITDEPTH
     575             : static 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.
     620           0 : static int fp_estimate_block_noise(MACROBLOCK *x, BLOCK_SIZE bsize) {
     621             : #if CONFIG_VP9_HIGHBITDEPTH
     622             :   MACROBLOCKD *xd = &x->e_mbd;
     623             : #endif
     624           0 :   uint8_t *src_ptr = &x->plane[0].src.buf[0];
     625           0 :   const int width = num_4x4_blocks_wide_lookup[bsize] * 4;
     626           0 :   const int height = num_4x4_blocks_high_lookup[bsize] * 4;
     627             :   int w, h;
     628           0 :   int stride = x->plane[0].src.stride;
     629           0 :   int block_noise = 0;
     630             : 
     631             :   // Sampled points to reduce cost overhead.
     632           0 :   for (h = 0; h < height; h += 2) {
     633           0 :     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           0 :       block_noise += fp_estimate_point_noise(src_ptr, stride);
     641             : #endif
     642           0 :       ++src_ptr;
     643             :     }
     644           0 :     src_ptr += (stride - width);
     645             :   }
     646           0 :   return block_noise << 2;  // Scale << 2 to account for sampling.
     647             : }
     648             : 
     649             : #define INVALID_ROW -1
     650           0 : void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
     651             :   int mb_row, mb_col;
     652           0 :   MACROBLOCK *const x = &cpi->td.mb;
     653           0 :   VP9_COMMON *const cm = &cpi->common;
     654           0 :   MACROBLOCKD *const xd = &x->e_mbd;
     655             :   TileInfo tile;
     656           0 :   struct macroblock_plane *const p = x->plane;
     657           0 :   struct macroblockd_plane *const pd = xd->plane;
     658           0 :   const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
     659             :   int i;
     660             : 
     661             :   int recon_yoffset, recon_uvoffset;
     662           0 :   int64_t intra_error = 0;
     663           0 :   int64_t coded_error = 0;
     664           0 :   int64_t sr_coded_error = 0;
     665           0 :   int64_t frame_noise_energy = 0;
     666             : 
     667           0 :   int sum_mvr = 0, sum_mvc = 0;
     668           0 :   int sum_mvr_abs = 0, sum_mvc_abs = 0;
     669           0 :   int64_t sum_mvrs = 0, sum_mvcs = 0;
     670           0 :   int mvcount = 0;
     671           0 :   int intercount = 0;
     672           0 :   int second_ref_count = 0;
     673           0 :   const int intrapenalty = INTRA_MODE_PENALTY;
     674             :   double neutral_count;
     675           0 :   int intra_skip_count = 0;
     676           0 :   int intra_smooth_count = 0;
     677           0 :   int image_data_start_row = INVALID_ROW;
     678           0 :   int sum_in_vectors = 0;
     679           0 :   TWO_PASS *twopass = &cpi->twopass;
     680           0 :   const MV zero_mv = { 0, 0 };
     681             :   int recon_y_stride, recon_uv_stride, uv_mb_height;
     682             : 
     683           0 :   YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
     684           0 :   YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
     685           0 :   YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
     686           0 :   const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
     687             : 
     688           0 :   LAYER_CONTEXT *const lc =
     689           0 :       is_two_pass_svc(cpi) ? &cpi->svc.layer_context[cpi->svc.spatial_layer_id]
     690           0 :                            : NULL;
     691             :   double intra_factor;
     692             :   double brightness_factor;
     693           0 :   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           0 :   assert(new_yv12 != NULL);
     698           0 :   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           0 :   vpx_clear_system_state();
     707             : 
     708           0 :   intra_factor = 0.0;
     709           0 :   brightness_factor = 0.0;
     710           0 :   neutral_count = 0.0;
     711             : 
     712           0 :   set_first_pass_params(cpi);
     713           0 :   vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
     714             : 
     715           0 :   if (lc != NULL) {
     716           0 :     twopass = &lc->twopass;
     717             : 
     718           0 :     cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
     719           0 :     cpi->ref_frame_flags = VP9_LAST_FLAG;
     720             : 
     721           0 :     if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id <
     722             :         REF_FRAMES) {
     723           0 :       cpi->gld_fb_idx =
     724           0 :           cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id;
     725           0 :       cpi->ref_frame_flags |= VP9_GOLD_FLAG;
     726           0 :       cpi->refresh_golden_frame = (lc->current_video_frame_in_layer == 0);
     727             :     } else {
     728           0 :       cpi->refresh_golden_frame = 0;
     729             :     }
     730             : 
     731           0 :     if (lc->current_video_frame_in_layer == 0) cpi->ref_frame_flags = 0;
     732             : 
     733           0 :     vp9_scale_references(cpi);
     734             : 
     735             :     // Use either last frame or alt frame for motion search.
     736           0 :     if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
     737           0 :       first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
     738           0 :       if (first_ref_buf == NULL)
     739           0 :         first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME);
     740             :     }
     741             : 
     742           0 :     if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
     743           0 :       gld_yv12 = vp9_get_scaled_ref_frame(cpi, GOLDEN_FRAME);
     744           0 :       if (gld_yv12 == NULL) {
     745           0 :         gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
     746             :       }
     747             :     } else {
     748           0 :       gld_yv12 = NULL;
     749             :     }
     750             : 
     751           0 :     set_ref_ptrs(cm, xd,
     752           0 :                  (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME : NONE,
     753           0 :                  (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE);
     754             : 
     755           0 :     cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
     756             :                                         &cpi->scaled_source, 0);
     757             :   }
     758             : 
     759           0 :   vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
     760             : 
     761           0 :   vp9_setup_src_planes(x, cpi->Source, 0, 0);
     762           0 :   vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
     763             : 
     764           0 :   if (!frame_is_intra_only(cm)) {
     765           0 :     vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
     766             :   }
     767             : 
     768           0 :   xd->mi = cm->mi_grid_visible;
     769           0 :   xd->mi[0] = cm->mi;
     770             : 
     771           0 :   vp9_frame_init_quantizer(cpi);
     772             : 
     773           0 :   for (i = 0; i < MAX_MB_PLANE; ++i) {
     774           0 :     p[i].coeff = ctx->coeff_pbuf[i][1];
     775           0 :     p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
     776           0 :     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
     777           0 :     p[i].eobs = ctx->eobs_pbuf[i][1];
     778             :   }
     779           0 :   x->skip_recode = 0;
     780             : 
     781           0 :   vp9_init_mv_probs(cm);
     782           0 :   vp9_initialize_rd_consts(cpi);
     783             : 
     784             :   // Tiling is ignored in the first pass.
     785           0 :   vp9_tile_init(&tile, cm, 0, 0);
     786             : 
     787           0 :   recon_y_stride = new_yv12->y_stride;
     788           0 :   recon_uv_stride = new_yv12->uv_stride;
     789           0 :   uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
     790             : 
     791           0 :   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
     792           0 :     MV best_ref_mv = { 0, 0 };
     793             : 
     794             :     // Reset above block coeffs.
     795           0 :     recon_yoffset = (mb_row * recon_y_stride * 16);
     796           0 :     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           0 :     x->mv_limits.row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
     801           0 :     x->mv_limits.row_max =
     802           0 :         ((cm->mb_rows - 1 - mb_row) * 16) + BORDER_MV_PIXELS_B16;
     803             : 
     804           0 :     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
     805             :       int this_error;
     806             :       int this_intra_error;
     807           0 :       const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
     808           0 :       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           0 :       vpx_clear_system_state();
     817             : 
     818           0 :       xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
     819           0 :       xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
     820           0 :       xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
     821           0 :       xd->mi[0]->sb_type = bsize;
     822           0 :       xd->mi[0]->ref_frame[0] = INTRA_FRAME;
     823           0 :       set_mi_row_col(xd, &tile, mb_row << 1, num_8x8_blocks_high_lookup[bsize],
     824           0 :                      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           0 :       xd->above_mi = (mb_row != 0) ? &mi_above : NULL;
     831           0 :       xd->left_mi = (mb_col > tile.mi_col_start) ? &mi_left : NULL;
     832             : 
     833             :       // Do intra 16x16 prediction.
     834           0 :       x->skip_encode = 0;
     835           0 :       xd->mi[0]->mode = DC_PRED;
     836           0 :       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           0 :       vp9_zero_array(x->plane[0].src_diff, 256);
     842           0 :       vp9_encode_intra_block_plane(x, bsize, 0, 0);
     843           0 :       this_error = vpx_get_mb_ss(x->plane[0].src_diff);
     844           0 :       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           0 :       if (this_error < get_ul_intra_threshold(cm)) {
     852           0 :         ++intra_skip_count;
     853           0 :       } else if ((mb_col > 0) && (image_data_start_row == INVALID_ROW)) {
     854           0 :         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           0 :       if (this_error < get_smooth_intra_threshold(cm)) {
     861           0 :         ++intra_smooth_count;
     862             :       }
     863             : 
     864             :       // Special case noise measurement for first frame.
     865           0 :       if (cm->current_video_frame == 0) {
     866           0 :         if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH)) {
     867           0 :           frame_noise_energy += fp_estimate_block_noise(x, bsize);
     868             :         } else {
     869           0 :           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           0 :       vpx_clear_system_state();
     889           0 :       log_intra = log(this_error + 1.0);
     890           0 :       if (log_intra < 10.0)
     891           0 :         intra_factor += 1.0 + ((10.0 - log_intra) * 0.05);
     892             :       else
     893           0 :         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           0 :       level_sample = x->plane[0].src.buf[0];
     902             : #endif
     903           0 :       if ((level_sample < DARK_THRESH) && (log_intra < 9.0))
     904           0 :         brightness_factor += 1.0 + (0.01 * (DARK_THRESH - level_sample));
     905             :       else
     906           0 :         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           0 :       this_error += intrapenalty;
     916             : 
     917             :       // Accumulate the intra error.
     918           0 :       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           0 :       x->mv_limits.col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
     930           0 :       x->mv_limits.col_max =
     931           0 :           ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
     932             : 
     933             :       // Other than for the first frame do a motion search.
     934           0 :       if ((lc == NULL && cm->current_video_frame > 0) ||
     935           0 :           (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           0 :         MV mv = { 0, 0 }, tmp_mv = { 0, 0 };
     939             :         struct buf_2d unscaled_last_source_buf_2d;
     940             : 
     941           0 :         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           0 :         motion_error =
     952           0 :             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           0 :         unscaled_last_source_buf_2d.buf =
     959           0 :             cpi->unscaled_last_source->y_buffer + recon_yoffset;
     960           0 :         unscaled_last_source_buf_2d.stride =
     961           0 :             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           0 :         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           0 :         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           0 :           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           0 :           if (!is_zero_mv(&best_ref_mv)) {
     984           0 :             tmp_err = INT_MAX;
     985           0 :             first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
     986             : 
     987           0 :             if (tmp_err < motion_error) {
     988           0 :               motion_error = tmp_err;
     989           0 :               mv = tmp_mv;
     990             :             }
     991             :           }
     992             : 
     993             :           // Search in an older reference frame.
     994           0 :           if (((lc == NULL && cm->current_video_frame > 1) ||
     995           0 :                (lc != NULL && lc->current_video_frame_in_layer > 1)) &&
     996           0 :               gld_yv12 != NULL) {
     997             :             // Assume 0,0 motion with no mv overhead.
     998             :             int gf_motion_error;
     999             : 
    1000           0 :             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           0 :             gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
    1011           0 :                                                    &xd->plane[0].pre[0]);
    1012             : #endif  // CONFIG_VP9_HIGHBITDEPTH
    1013             : 
    1014           0 :             first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv,
    1015             :                                      &gf_motion_error);
    1016             : 
    1017           0 :             if (gf_motion_error < motion_error && gf_motion_error < this_error)
    1018           0 :               ++second_ref_count;
    1019             : 
    1020             :             // Reset to last frame as reference buffer.
    1021           0 :             xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
    1022           0 :             xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
    1023           0 :             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           0 :             if (gf_motion_error < this_error)
    1030           0 :               sr_coded_error += gf_motion_error;
    1031             :             else
    1032           0 :               sr_coded_error += this_error;
    1033             :           } else {
    1034           0 :             sr_coded_error += motion_error;
    1035             :           }
    1036             :         } else {
    1037           0 :           sr_coded_error += motion_error;
    1038             :         }
    1039             : 
    1040             :         // Start by assuming that intra mode is best.
    1041           0 :         best_ref_mv.row = 0;
    1042           0 :         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           0 :         if (motion_error <= this_error) {
    1059           0 :           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           0 :           if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
    1065           0 :               (this_error < (2 * intrapenalty))) {
    1066           0 :             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           0 :           } else if ((this_error > NCOUNT_INTRA_THRESH) &&
    1070           0 :                      (this_error < (NCOUNT_INTRA_FACTOR * motion_error))) {
    1071           0 :             neutral_count +=
    1072           0 :                 (double)motion_error / DOUBLE_DIVIDE_CHECK((double)this_error);
    1073             :           }
    1074             : 
    1075           0 :           mv.row *= 8;
    1076           0 :           mv.col *= 8;
    1077           0 :           this_error = motion_error;
    1078           0 :           xd->mi[0]->mode = NEWMV;
    1079           0 :           xd->mi[0]->mv[0].as_mv = mv;
    1080           0 :           xd->mi[0]->tx_size = TX_4X4;
    1081           0 :           xd->mi[0]->ref_frame[0] = LAST_FRAME;
    1082           0 :           xd->mi[0]->ref_frame[1] = NONE;
    1083           0 :           vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
    1084           0 :           vp9_encode_sby_pass1(x, bsize);
    1085           0 :           sum_mvr += mv.row;
    1086           0 :           sum_mvr_abs += abs(mv.row);
    1087           0 :           sum_mvc += mv.col;
    1088           0 :           sum_mvc_abs += abs(mv.col);
    1089           0 :           sum_mvrs += mv.row * mv.row;
    1090           0 :           sum_mvcs += mv.col * mv.col;
    1091           0 :           ++intercount;
    1092             : 
    1093           0 :           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           0 :           if (!is_zero_mv(&mv)) {
    1112           0 :             ++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           0 :             if (mb_row < cm->mb_rows / 2) {
    1143           0 :               if (mv.row > 0)
    1144           0 :                 --sum_in_vectors;
    1145           0 :               else if (mv.row < 0)
    1146           0 :                 ++sum_in_vectors;
    1147           0 :             } else if (mb_row > cm->mb_rows / 2) {
    1148           0 :               if (mv.row > 0)
    1149           0 :                 ++sum_in_vectors;
    1150           0 :               else if (mv.row < 0)
    1151           0 :                 --sum_in_vectors;
    1152             :             }
    1153             : 
    1154             :             // Does the col vector point inwards or outwards?
    1155           0 :             if (mb_col < cm->mb_cols / 2) {
    1156           0 :               if (mv.col > 0)
    1157           0 :                 --sum_in_vectors;
    1158           0 :               else if (mv.col < 0)
    1159           0 :                 ++sum_in_vectors;
    1160           0 :             } else if (mb_col > cm->mb_cols / 2) {
    1161           0 :               if (mv.col > 0)
    1162           0 :                 ++sum_in_vectors;
    1163           0 :               else if (mv.col < 0)
    1164           0 :                 --sum_in_vectors;
    1165             :             }
    1166           0 :             frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
    1167           0 :           } else if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH)) {
    1168           0 :             frame_noise_energy += fp_estimate_block_noise(x, bsize);
    1169             :           } else {  // 0,0 mv but high error
    1170           0 :             frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
    1171             :           }
    1172             :         } else {  // Intra < inter error
    1173           0 :           if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH))
    1174           0 :             frame_noise_energy += fp_estimate_block_noise(x, bsize);
    1175             :           else
    1176           0 :             frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
    1177             :         }
    1178             :       } else {
    1179           0 :         sr_coded_error += (int64_t)this_error;
    1180             :       }
    1181           0 :       coded_error += (int64_t)this_error;
    1182             : 
    1183             :       // Adjust to the next column of MBs.
    1184           0 :       x->plane[0].src.buf += 16;
    1185           0 :       x->plane[1].src.buf += uv_mb_height;
    1186           0 :       x->plane[2].src.buf += uv_mb_height;
    1187             : 
    1188           0 :       recon_yoffset += 16;
    1189           0 :       recon_uvoffset += uv_mb_height;
    1190             :     }
    1191             : 
    1192             :     // Adjust to the next row of MBs.
    1193           0 :     x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
    1194           0 :     x->plane[1].src.buf +=
    1195           0 :         uv_mb_height * x->plane[1].src.stride - uv_mb_height * cm->mb_cols;
    1196           0 :     x->plane[2].src.buf +=
    1197           0 :         uv_mb_height * x->plane[1].src.stride - uv_mb_height * cm->mb_cols;
    1198             : 
    1199           0 :     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           0 :   if ((image_data_start_row > cm->mb_rows / 2) ||
    1205             :       (image_data_start_row == INVALID_ROW)) {
    1206           0 :     image_data_start_row = cm->mb_rows / 2;
    1207             :   }
    1208             :   // Exclude any image dead zone
    1209           0 :   if (image_data_start_row > 0) {
    1210           0 :     intra_skip_count =
    1211           0 :         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           0 :     const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
    1222             :                             ? cpi->initial_mbs
    1223           0 :                             : cpi->common.MBs;
    1224           0 :     const double min_err = 200 * sqrt(num_mbs);
    1225             : 
    1226           0 :     intra_factor = intra_factor / (double)num_mbs;
    1227           0 :     brightness_factor = brightness_factor / (double)num_mbs;
    1228           0 :     fps.weight = intra_factor * brightness_factor;
    1229             : 
    1230           0 :     fps.frame = cm->current_video_frame;
    1231           0 :     fps.spatial_layer_id = cpi->svc.spatial_layer_id;
    1232           0 :     fps.coded_error = (double)(coded_error >> 8) + min_err;
    1233           0 :     fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
    1234           0 :     fps.intra_error = (double)(intra_error >> 8) + min_err;
    1235           0 :     fps.frame_noise_energy = (double)frame_noise_energy / (double)num_mbs;
    1236           0 :     fps.count = 1.0;
    1237           0 :     fps.pcnt_inter = (double)intercount / num_mbs;
    1238           0 :     fps.pcnt_second_ref = (double)second_ref_count / num_mbs;
    1239           0 :     fps.pcnt_neutral = (double)neutral_count / num_mbs;
    1240           0 :     fps.intra_skip_pct = (double)intra_skip_count / num_mbs;
    1241           0 :     fps.intra_smooth_pct = (double)intra_smooth_count / num_mbs;
    1242           0 :     fps.inactive_zone_rows = (double)image_data_start_row;
    1243             :     // Currently set to 0 as most issues relate to letter boxing.
    1244           0 :     fps.inactive_zone_cols = (double)0;
    1245             : 
    1246           0 :     if (mvcount > 0) {
    1247           0 :       fps.MVr = (double)sum_mvr / mvcount;
    1248           0 :       fps.mvr_abs = (double)sum_mvr_abs / mvcount;
    1249           0 :       fps.MVc = (double)sum_mvc / mvcount;
    1250           0 :       fps.mvc_abs = (double)sum_mvc_abs / mvcount;
    1251           0 :       fps.MVrv =
    1252           0 :           ((double)sum_mvrs - ((double)sum_mvr * sum_mvr / mvcount)) / mvcount;
    1253           0 :       fps.MVcv =
    1254           0 :           ((double)sum_mvcs - ((double)sum_mvc * sum_mvc / mvcount)) / mvcount;
    1255           0 :       fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
    1256           0 :       fps.pcnt_motion = (double)mvcount / num_mbs;
    1257             :     } else {
    1258           0 :       fps.MVr = 0.0;
    1259           0 :       fps.mvr_abs = 0.0;
    1260           0 :       fps.MVc = 0.0;
    1261           0 :       fps.mvc_abs = 0.0;
    1262           0 :       fps.MVrv = 0.0;
    1263           0 :       fps.MVcv = 0.0;
    1264           0 :       fps.mv_in_out_count = 0.0;
    1265           0 :       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           0 :     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           0 :     twopass->this_frame_stats = fps;
    1274           0 :     output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
    1275           0 :     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           0 :   if ((twopass->sr_update_lag > 3) ||
    1287           0 :       ((cm->current_video_frame > 0) &&
    1288           0 :        (twopass->this_frame_stats.pcnt_inter > 0.20) &&
    1289           0 :        ((twopass->this_frame_stats.intra_error /
    1290           0 :          DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
    1291           0 :     if (gld_yv12 != NULL) {
    1292           0 :       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
    1293           0 :                  cm->ref_frame_map[cpi->lst_fb_idx]);
    1294             :     }
    1295           0 :     twopass->sr_update_lag = 1;
    1296             :   } else {
    1297           0 :     ++twopass->sr_update_lag;
    1298             :   }
    1299             : 
    1300           0 :   vpx_extend_frame_borders(new_yv12);
    1301             : 
    1302           0 :   if (lc != NULL) {
    1303           0 :     vp9_update_reference_frames(cpi);
    1304             :   } else {
    1305             :     // The frame we just compressed now becomes the last frame.
    1306           0 :     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           0 :   if (cm->current_video_frame == 0 && cpi->gld_fb_idx != INVALID_IDX &&
    1313             :       lc == NULL) {
    1314           0 :     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
    1315           0 :                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           0 :   ++cm->current_video_frame;
    1335           0 :   if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
    1336           0 : }
    1337             : 
    1338           0 : static 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           0 :   const double error_term = err_per_mb / err_divisor;
    1342             : 
    1343             :   // Adjustment based on actual quantizer to power term.
    1344           0 :   const double power_term =
    1345           0 :       VPXMIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high);
    1346             : 
    1347             :   // Calculate correction factor.
    1348           0 :   if (power_term < 1.0) assert(error_term >= 0.0);
    1349             : 
    1350           0 :   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
    1356           0 : static 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           0 :   const RATE_CONTROL *const rc = &cpi->rc;
    1360           0 :   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    1361           0 :   TWO_PASS *const twopass = &cpi->twopass;
    1362             : 
    1363             :   // Clamp the target rate to VBR min / max limts.
    1364           0 :   const int target_rate =
    1365             :       vp9_rc_clamp_pframe_target_size(cpi, section_target_bandwidth);
    1366           0 :   double noise_factor = pow((section_noise / SECTION_NOISE_DEF), 0.5);
    1367           0 :   noise_factor = fclamp(noise_factor, NOISE_FACTOR_MIN, NOISE_FACTOR_MAX);
    1368           0 :   inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
    1369             : 
    1370           0 :   if (target_rate <= 0) {
    1371           0 :     return rc->worst_quality;  // Highest value allowed
    1372             :   } else {
    1373           0 :     const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
    1374             :                             ? cpi->initial_mbs
    1375           0 :                             : cpi->common.MBs;
    1376           0 :     const int active_mbs = VPXMAX(1, num_mbs - (int)(num_mbs * inactive_zone));
    1377           0 :     const double av_err_per_mb = section_err / active_mbs;
    1378           0 :     const double speed_term = 1.0 + 0.04 * oxcf->speed;
    1379             :     double last_group_rate_err;
    1380           0 :     const int target_norm_bits_per_mb =
    1381           0 :         (int)(((uint64_t)target_rate << BPER_MB_NORMBITS) / active_mbs);
    1382             :     int q;
    1383           0 :     int is_svc_upper_layer = 0;
    1384             : 
    1385           0 :     if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
    1386           0 :       is_svc_upper_layer = 1;
    1387             : 
    1388             :     // based on recent history adjust expectations of bits per macroblock.
    1389           0 :     last_group_rate_err =
    1390           0 :         (double)twopass->rolling_arf_group_actual_bits /
    1391           0 :         DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
    1392           0 :     last_group_rate_err = VPXMAX(0.25, VPXMIN(4.0, last_group_rate_err));
    1393           0 :     twopass->bpm_factor *= (3.0 + last_group_rate_err) / 4.0;
    1394           0 :     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           0 :     for (q = rc->best_quality; q < rc->worst_quality; ++q) {
    1399           0 :       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           0 :       const int bits_per_mb = vp9_rc_bits_per_mb(
    1404             :           INTER_FRAME, q,
    1405           0 :           factor * speed_term * cpi->twopass.bpm_factor * noise_factor,
    1406             :           cpi->common.bit_depth);
    1407           0 :       if (bits_per_mb <= target_norm_bits_per_mb) break;
    1408             :     }
    1409             : 
    1410             :     // Restriction on active max q for constrained quality mode.
    1411           0 :     if (cpi->oxcf.rc_mode == VPX_CQ) q = VPXMAX(q, oxcf->cq_level);
    1412           0 :     return q;
    1413             :   }
    1414             : }
    1415             : 
    1416           0 : static void setup_rf_level_maxq(VP9_COMP *cpi) {
    1417             :   int i;
    1418           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1419           0 :   for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) {
    1420           0 :     int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality);
    1421           0 :     rc->rf_level_maxq[i] = VPXMAX(rc->worst_quality + qdelta, rc->best_quality);
    1422             :   }
    1423           0 : }
    1424             : 
    1425           0 : static void init_subsampling(VP9_COMP *cpi) {
    1426           0 :   const VP9_COMMON *const cm = &cpi->common;
    1427           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1428           0 :   const int w = cm->width;
    1429           0 :   const int h = cm->height;
    1430             :   int i;
    1431             : 
    1432           0 :   for (i = 0; i < FRAME_SCALE_STEPS; ++i) {
    1433             :     // Note: Frames with odd-sized dimensions may result from this scaling.
    1434           0 :     rc->frame_width[i] = (w * 16) / frame_scale_factor[i];
    1435           0 :     rc->frame_height[i] = (h * 16) / frame_scale_factor[i];
    1436             :   }
    1437             : 
    1438           0 :   setup_rf_level_maxq(cpi);
    1439           0 : }
    1440             : 
    1441           0 : void calculate_coded_size(VP9_COMP *cpi, int *scaled_frame_width,
    1442             :                           int *scaled_frame_height) {
    1443           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1444           0 :   *scaled_frame_width = rc->frame_width[rc->frame_size_selector];
    1445           0 :   *scaled_frame_height = rc->frame_height[rc->frame_size_selector];
    1446           0 : }
    1447             : 
    1448           0 : void vp9_init_second_pass(VP9_COMP *cpi) {
    1449           0 :   SVC *const svc = &cpi->svc;
    1450           0 :   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    1451           0 :   const int is_two_pass_svc =
    1452           0 :       (svc->number_spatial_layers > 1) || (svc->number_temporal_layers > 1);
    1453           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1454           0 :   TWO_PASS *const twopass =
    1455           0 :       is_two_pass_svc ? &svc->layer_context[svc->spatial_layer_id].twopass
    1456           0 :                       : &cpi->twopass;
    1457             :   double frame_rate;
    1458             :   FIRSTPASS_STATS *stats;
    1459             : 
    1460           0 :   zero_stats(&twopass->total_stats);
    1461           0 :   zero_stats(&twopass->total_left_stats);
    1462             : 
    1463           0 :   if (!twopass->stats_in_end) return;
    1464             : 
    1465           0 :   stats = &twopass->total_stats;
    1466             : 
    1467           0 :   *stats = *twopass->stats_in_end;
    1468           0 :   twopass->total_left_stats = *stats;
    1469             : 
    1470           0 :   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           0 :   if (is_two_pass_svc) {
    1478           0 :     vp9_update_spatial_layer_framerate(cpi, frame_rate);
    1479           0 :     twopass->bits_left =
    1480           0 :         (int64_t)(stats->duration *
    1481           0 :                   svc->layer_context[svc->spatial_layer_id].target_bandwidth /
    1482             :                   10000000.0);
    1483             :   } else {
    1484           0 :     vp9_new_framerate(cpi, frame_rate);
    1485           0 :     twopass->bits_left =
    1486           0 :         (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           0 :   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           0 :     const double avg_error =
    1496           0 :         stats->coded_error / DOUBLE_DIVIDE_CHECK(stats->count);
    1497           0 :     const FIRSTPASS_STATS *s = twopass->stats_in;
    1498           0 :     double modified_error_total = 0.0;
    1499           0 :     twopass->modified_error_min =
    1500           0 :         (avg_error * oxcf->two_pass_vbrmin_section) / 100;
    1501           0 :     twopass->modified_error_max =
    1502           0 :         (avg_error * oxcf->two_pass_vbrmax_section) / 100;
    1503           0 :     while (s < twopass->stats_in_end) {
    1504           0 :       modified_error_total += calculate_modified_err(cpi, twopass, oxcf, s);
    1505           0 :       ++s;
    1506             :     }
    1507           0 :     twopass->modified_error_left = modified_error_total;
    1508             :   }
    1509             : 
    1510             :   // Reset the vbr bits off target counters
    1511           0 :   rc->vbr_bits_off_target = 0;
    1512           0 :   rc->vbr_bits_off_target_fast = 0;
    1513           0 :   rc->rate_error_estimate = 0;
    1514             : 
    1515             :   // Static sequence monitor variables.
    1516           0 :   twopass->kf_zeromotion_pct = 100;
    1517           0 :   twopass->last_kfgroup_zeromotion_pct = 100;
    1518             : 
    1519             :   // Initialize bits per macro_block estimate correction factor.
    1520           0 :   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           0 :   twopass->rolling_arf_group_target_bits = 1;
    1524           0 :   twopass->rolling_arf_group_actual_bits = 1;
    1525             : 
    1526           0 :   if (oxcf->resize_mode != RESIZE_NONE) {
    1527           0 :     init_subsampling(cpi);
    1528             :   }
    1529             : 
    1530             :   // Initialize the arnr strangth adjustment to 0
    1531           0 :   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             : 
    1542           0 : static double get_sr_decay_rate(const VP9_COMP *cpi,
    1543             :                                 const FIRSTPASS_STATS *frame) {
    1544           0 :   const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
    1545           0 :                                                              : cpi->common.MBs;
    1546           0 :   double sr_diff = (frame->sr_coded_error - frame->coded_error) / num_mbs;
    1547           0 :   double sr_decay = 1.0;
    1548             :   double modified_pct_inter;
    1549             :   double modified_pcnt_intra;
    1550           0 :   const double motion_amplitude_part =
    1551           0 :       frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) /
    1552           0 :                             (cpi->initial_height + cpi->initial_width));
    1553             : 
    1554           0 :   modified_pct_inter = frame->pcnt_inter;
    1555           0 :   if (((frame->coded_error / num_mbs) > LOW_CODED_ERR_PER_MB) &&
    1556           0 :       ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) <
    1557             :        (double)NCOUNT_FRAME_II_THRESH)) {
    1558           0 :     modified_pct_inter = frame->pcnt_inter - frame->pcnt_neutral;
    1559             :   }
    1560           0 :   modified_pcnt_intra = 100 * (1.0 - modified_pct_inter);
    1561             : 
    1562           0 :   if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
    1563           0 :     sr_diff = VPXMIN(sr_diff, SR_DIFF_MAX);
    1564           0 :     sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) - motion_amplitude_part -
    1565           0 :                (INTRA_PART * modified_pcnt_intra);
    1566             :   }
    1567           0 :   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.
    1572           0 : static double get_zero_motion_factor(const VP9_COMP *cpi,
    1573             :                                      const FIRSTPASS_STATS *frame) {
    1574           0 :   const double zero_motion_pct = frame->pcnt_inter - frame->pcnt_motion;
    1575           0 :   double sr_decay = get_sr_decay_rate(cpi, frame);
    1576           0 :   return VPXMIN(sr_decay, zero_motion_pct);
    1577             : }
    1578             : 
    1579             : #define ZM_POWER_FACTOR 0.75
    1580             : 
    1581           0 : static double get_prediction_decay_rate(const VP9_COMP *cpi,
    1582             :                                         const FIRSTPASS_STATS *next_frame) {
    1583           0 :   const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame);
    1584           0 :   const double zero_motion_factor =
    1585           0 :       (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
    1586             :                   ZM_POWER_FACTOR));
    1587             : 
    1588           0 :   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.
    1595           0 : static 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           0 :   TWO_PASS *const twopass = &cpi->twopass;
    1600           0 :   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           0 :   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           0 :     for (j = 0; j < still_interval; ++j) {
    1611           0 :       const FIRSTPASS_STATS *stats = &twopass->stats_in[j];
    1612           0 :       if (stats >= twopass->stats_in_end) break;
    1613             : 
    1614           0 :       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           0 :     return j == still_interval;
    1619             :   }
    1620             : 
    1621           0 :   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.
    1627           0 : static int detect_flash(const TWO_PASS *twopass, int offset) {
    1628           0 :   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           0 :   return next_frame != NULL &&
    1636           0 :          next_frame->pcnt_second_ref > next_frame->pcnt_inter &&
    1637           0 :          next_frame->pcnt_second_ref >= 0.5;
    1638             : }
    1639             : 
    1640             : // Update the motion related elements to the GF arf boost calculation.
    1641           0 : static 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           0 :   const double pct = stats->pcnt_motion;
    1647             : 
    1648             :   // Accumulate Motion In/Out of frame stats.
    1649           0 :   *mv_in_out = stats->mv_in_out_count * pct;
    1650           0 :   *mv_in_out_accumulator += *mv_in_out;
    1651           0 :   *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           0 :   if (pct > 0.05) {
    1656           0 :     const double mvr_ratio =
    1657           0 :         fabs(stats->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVr));
    1658           0 :     const double mvc_ratio =
    1659           0 :         fabs(stats->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
    1660             : 
    1661           0 :     *mv_ratio_accumulator +=
    1662           0 :         pct * (mvr_ratio < stats->mvr_abs ? mvr_ratio : stats->mvr_abs);
    1663           0 :     *mv_ratio_accumulator +=
    1664           0 :         pct * (mvc_ratio < stats->mvc_abs ? mvc_ratio : stats->mvc_abs);
    1665             :   }
    1666           0 : }
    1667             : 
    1668             : #define BASELINE_ERR_PER_MB 1000.0
    1669           0 : static 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           0 :   const double lq = vp9_convert_qindex_to_q(
    1674             :       cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth);
    1675           0 :   const double boost_q_correction = VPXMIN((0.5 + (lq * 0.015)), 1.5);
    1676           0 :   int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
    1677           0 :                                                        : cpi->common.MBs;
    1678             : 
    1679             :   // Correct for any inactive region in the image
    1680           0 :   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           0 :   frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
    1684           0 :                 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           0 :   *sr_accumulator += (this_frame->sr_coded_error - this_frame->coded_error) / 1;
    1690           0 :   *sr_accumulator = VPXMAX(0.0, *sr_accumulator);
    1691             : 
    1692             :   // Small adjustment for cases where there is a zoom out
    1693           0 :   if (this_frame_mv_in_out > 0.0)
    1694           0 :     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
    1695             : 
    1696             :   // Q correction and scalling
    1697           0 :   frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction;
    1698             : 
    1699           0 :   return VPXMIN(frame_boost, max_boost * boost_q_correction);
    1700             : }
    1701             : 
    1702             : #define KF_BOOST_FACTOR 12.5
    1703           0 : static 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           0 :   const double lq = vp9_convert_qindex_to_q(
    1710             :       cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth);
    1711           0 :   const double boost_q_correction = VPXMIN((0.50 + (lq * 0.015)), 2.00);
    1712           0 :   int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
    1713           0 :                                                        : cpi->common.MBs;
    1714             : 
    1715             :   // Correct for any inactive region in the image
    1716           0 :   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           0 :   frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
    1720           0 :                 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           0 :   *sr_accumulator += (this_frame->sr_coded_error - this_frame->coded_error) / 1;
    1726           0 :   *sr_accumulator = VPXMAX(0.0, *sr_accumulator);
    1727             : 
    1728             :   // Small adjustment for cases where there is a zoom out
    1729           0 :   if (this_frame_mv_in_out > 0.0)
    1730           0 :     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
    1731             : 
    1732             :   // Q correction and scalling
    1733           0 :   frame_boost = frame_boost * KF_BOOST_FACTOR * boost_q_correction;
    1734             : 
    1735           0 :   return VPXMIN(frame_boost, max_boost * boost_q_correction);
    1736             : }
    1737             : 
    1738           0 : static int calc_arf_boost(VP9_COMP *cpi, int offset, int f_frames, int b_frames,
    1739             :                           int *f_boost, int *b_boost) {
    1740           0 :   TWO_PASS *const twopass = &cpi->twopass;
    1741             :   int i;
    1742           0 :   double boost_score = 0.0;
    1743           0 :   double mv_ratio_accumulator = 0.0;
    1744           0 :   double decay_accumulator = 1.0;
    1745           0 :   double this_frame_mv_in_out = 0.0;
    1746           0 :   double mv_in_out_accumulator = 0.0;
    1747           0 :   double abs_mv_in_out_accumulator = 0.0;
    1748           0 :   double sr_accumulator = 0.0;
    1749             :   int arf_boost;
    1750           0 :   int flash_detected = 0;
    1751             : 
    1752             :   // Search forward from the proposed arf/next gf position.
    1753           0 :   for (i = 0; i < f_frames; ++i) {
    1754           0 :     const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
    1755           0 :     if (this_frame == NULL) break;
    1756             : 
    1757             :     // Update the motion related elements to the boost calculation.
    1758           0 :     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           0 :     flash_detected = detect_flash(twopass, i + offset) ||
    1765           0 :                      detect_flash(twopass, i + offset + 1);
    1766             : 
    1767             :     // Accumulate the effect of prediction quality decay.
    1768           0 :     if (!flash_detected) {
    1769           0 :       decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
    1770           0 :       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
    1771             :                               ? MIN_DECAY_FACTOR
    1772           0 :                               : decay_accumulator;
    1773             :     }
    1774             : 
    1775           0 :     sr_accumulator = 0.0;
    1776           0 :     boost_score += decay_accumulator *
    1777           0 :                    calc_frame_boost(cpi, this_frame, &sr_accumulator,
    1778             :                                     this_frame_mv_in_out, GF_MAX_BOOST);
    1779             :   }
    1780             : 
    1781           0 :   *f_boost = (int)boost_score;
    1782             : 
    1783             :   // Reset for backward looking loop.
    1784           0 :   boost_score = 0.0;
    1785           0 :   mv_ratio_accumulator = 0.0;
    1786           0 :   decay_accumulator = 1.0;
    1787           0 :   this_frame_mv_in_out = 0.0;
    1788           0 :   mv_in_out_accumulator = 0.0;
    1789           0 :   abs_mv_in_out_accumulator = 0.0;
    1790           0 :   sr_accumulator = 0.0;
    1791             : 
    1792             :   // Search backward towards last gf position.
    1793           0 :   for (i = -1; i >= -b_frames; --i) {
    1794           0 :     const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
    1795           0 :     if (this_frame == NULL) break;
    1796             : 
    1797             :     // Update the motion related elements to the boost calculation.
    1798           0 :     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           0 :     flash_detected = detect_flash(twopass, i + offset) ||
    1805           0 :                      detect_flash(twopass, i + offset + 1);
    1806             : 
    1807             :     // Cumulative effect of prediction quality decay.
    1808           0 :     if (!flash_detected) {
    1809           0 :       decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
    1810           0 :       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
    1811             :                               ? MIN_DECAY_FACTOR
    1812           0 :                               : decay_accumulator;
    1813             :     }
    1814             : 
    1815           0 :     sr_accumulator = 0.0;
    1816           0 :     boost_score += decay_accumulator *
    1817           0 :                    calc_frame_boost(cpi, this_frame, &sr_accumulator,
    1818             :                                     this_frame_mv_in_out, GF_MAX_BOOST);
    1819             :   }
    1820           0 :   *b_boost = (int)boost_score;
    1821             : 
    1822           0 :   arf_boost = (*f_boost + *b_boost);
    1823           0 :   if (arf_boost < ((b_frames + f_frames) * 20))
    1824           0 :     arf_boost = ((b_frames + f_frames) * 20);
    1825           0 :   arf_boost = VPXMAX(arf_boost, MIN_ARF_GF_BOOST);
    1826             : 
    1827           0 :   return arf_boost;
    1828             : }
    1829             : 
    1830             : // Calculate a section intra ratio used in setting max loop filter.
    1831           0 : static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
    1832             :                                          const FIRSTPASS_STATS *end,
    1833             :                                          int section_length) {
    1834           0 :   const FIRSTPASS_STATS *s = begin;
    1835           0 :   double intra_error = 0.0;
    1836           0 :   double coded_error = 0.0;
    1837           0 :   int i = 0;
    1838             : 
    1839           0 :   while (s < end && i < section_length) {
    1840           0 :     intra_error += s->intra_error;
    1841           0 :     coded_error += s->coded_error;
    1842           0 :     ++s;
    1843           0 :     ++i;
    1844             :   }
    1845             : 
    1846           0 :   return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error));
    1847             : }
    1848             : 
    1849             : // Calculate the total bits to allocate in this GF/ARF group.
    1850           0 : static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi,
    1851             :                                              double gf_group_err) {
    1852           0 :   const RATE_CONTROL *const rc = &cpi->rc;
    1853           0 :   const TWO_PASS *const twopass = &cpi->twopass;
    1854           0 :   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           0 :   if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) {
    1859           0 :     total_group_bits = (int64_t)(twopass->kf_group_bits *
    1860           0 :                                  (gf_group_err / twopass->kf_group_error_left));
    1861             :   } else {
    1862           0 :     total_group_bits = 0;
    1863             :   }
    1864             : 
    1865             :   // Clamp odd edge cases.
    1866           0 :   total_group_bits =
    1867           0 :       (total_group_bits < 0) ? 0 : (total_group_bits > twopass->kf_group_bits)
    1868           0 :                                        ? twopass->kf_group_bits
    1869             :                                        : total_group_bits;
    1870             : 
    1871             :   // Clip based on user supplied data rate variability limit.
    1872           0 :   if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
    1873           0 :     total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
    1874             : 
    1875           0 :   return total_group_bits;
    1876             : }
    1877             : 
    1878             : // Calculate the number bits extra to assign to boosted frames in a group.
    1879           0 : static 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           0 :   if (!boost || (total_group_bits <= 0) || (frame_count <= 0)) return 0;
    1885             : 
    1886           0 :   allocation_chunks = (frame_count * 100) + boost;
    1887             : 
    1888             :   // Prevent overflow.
    1889           0 :   if (boost > 1023) {
    1890           0 :     int divisor = boost >> 10;
    1891           0 :     boost /= divisor;
    1892           0 :     allocation_chunks /= divisor;
    1893             :   }
    1894             : 
    1895             :   // Calculate the number of extra bits for use in the boosted frame or frames.
    1896           0 :   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.
    1907           0 : static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) {
    1908           0 :   arf_buffer_indices[0] = ARF_SLOT1;
    1909           0 :   arf_buffer_indices[1] = ARF_SLOT2;
    1910           0 : }
    1911             : 
    1912           0 : static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits,
    1913             :                                    int gf_arf_bits) {
    1914           0 :   RATE_CONTROL *const rc = &cpi->rc;
    1915           0 :   TWO_PASS *const twopass = &cpi->twopass;
    1916           0 :   GF_GROUP *const gf_group = &twopass->gf_group;
    1917             :   FIRSTPASS_STATS frame_stats;
    1918             :   int i;
    1919           0 :   int frame_index = 1;
    1920             :   int target_frame_size;
    1921             :   int key_frame;
    1922           0 :   const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf);
    1923           0 :   int64_t total_group_bits = gf_group_bits;
    1924           0 :   int mid_boost_bits = 0;
    1925             :   int mid_frame_idx;
    1926             :   unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS];
    1927           0 :   int alt_frame_index = frame_index;
    1928           0 :   int has_temporal_layers =
    1929           0 :       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           0 :   if (has_temporal_layers) alt_frame_index = cpi->svc.number_temporal_layers;
    1937             : 
    1938           0 :   key_frame =
    1939           0 :       cpi->common.frame_type == KEY_FRAME || vp9_is_upper_layer_key_frame(cpi);
    1940             : 
    1941           0 :   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           0 :   if (!key_frame) {
    1946           0 :     if (rc->source_alt_ref_active) {
    1947           0 :       gf_group->update_type[0] = OVERLAY_UPDATE;
    1948           0 :       gf_group->rf_level[0] = INTER_NORMAL;
    1949           0 :       gf_group->bit_allocation[0] = 0;
    1950             :     } else {
    1951           0 :       gf_group->update_type[0] = GF_UPDATE;
    1952           0 :       gf_group->rf_level[0] = GF_ARF_STD;
    1953           0 :       gf_group->bit_allocation[0] = gf_arf_bits;
    1954             :     }
    1955           0 :     gf_group->arf_update_idx[0] = arf_buffer_indices[0];
    1956           0 :     gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
    1957             : 
    1958             :     // Step over the golden frame / overlay frame
    1959           0 :     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           0 :   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           0 :   if (rc->source_alt_ref_pending) {
    1968           0 :     gf_group->update_type[alt_frame_index] = ARF_UPDATE;
    1969           0 :     gf_group->rf_level[alt_frame_index] = GF_ARF_STD;
    1970           0 :     gf_group->bit_allocation[alt_frame_index] = gf_arf_bits;
    1971             : 
    1972           0 :     if (has_temporal_layers)
    1973           0 :       gf_group->arf_src_offset[alt_frame_index] =
    1974           0 :           (unsigned char)(rc->baseline_gf_interval -
    1975           0 :                           cpi->svc.number_temporal_layers);
    1976             :     else
    1977           0 :       gf_group->arf_src_offset[alt_frame_index] =
    1978           0 :           (unsigned char)(rc->baseline_gf_interval - 1);
    1979             : 
    1980           0 :     gf_group->arf_update_idx[alt_frame_index] = arf_buffer_indices[0];
    1981           0 :     gf_group->arf_ref_idx[alt_frame_index] =
    1982           0 :         arf_buffer_indices[cpi->multi_arf_last_grp_enabled &&
    1983           0 :                            rc->source_alt_ref_active];
    1984           0 :     if (!has_temporal_layers) ++frame_index;
    1985             : 
    1986           0 :     if (cpi->multi_arf_enabled) {
    1987             :       // Set aside a slot for a level 1 arf.
    1988           0 :       gf_group->update_type[frame_index] = ARF_UPDATE;
    1989           0 :       gf_group->rf_level[frame_index] = GF_ARF_LOW;
    1990           0 :       gf_group->arf_src_offset[frame_index] =
    1991           0 :           (unsigned char)((rc->baseline_gf_interval >> 1) - 1);
    1992           0 :       gf_group->arf_update_idx[frame_index] = arf_buffer_indices[1];
    1993           0 :       gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
    1994           0 :       ++frame_index;
    1995             :     }
    1996             :   }
    1997             : 
    1998             :   // Note index of the first normal inter frame int eh group (not gf kf arf)
    1999           0 :   gf_group->first_inter_index = frame_index;
    2000             : 
    2001             :   // Define middle frame
    2002           0 :   mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
    2003             : 
    2004           0 :   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           0 :   if (normal_frames > 1) {
    2009           0 :     normal_frame_bits = (int)(total_group_bits / normal_frames);
    2010           0 :     last_frame_reduction = normal_frame_bits / 16;
    2011           0 :     last_frame_bits = normal_frame_bits - last_frame_reduction;
    2012             :   } else {
    2013           0 :     normal_frame_bits = (int)total_group_bits;
    2014           0 :     last_frame_bits = normal_frame_bits;
    2015           0 :     last_frame_reduction = 0;
    2016             :   }
    2017             : 
    2018             :   // Allocate bits to the other frames in the group.
    2019           0 :   for (i = 0; i < normal_frames; ++i) {
    2020           0 :     int arf_idx = 0;
    2021           0 :     if (EOF == input_stats(twopass, &frame_stats)) break;
    2022             : 
    2023           0 :     if (has_temporal_layers && frame_index == alt_frame_index) {
    2024           0 :       ++frame_index;
    2025             :     }
    2026             : 
    2027           0 :     target_frame_size = (i == (normal_frames - 1))
    2028             :                             ? last_frame_bits
    2029           0 :                             : (i == mid_frame_idx)
    2030             :                                   ? normal_frame_bits + last_frame_reduction
    2031           0 :                                   : normal_frame_bits;
    2032             : 
    2033           0 :     if (rc->source_alt_ref_pending && cpi->multi_arf_enabled) {
    2034           0 :       mid_boost_bits += (target_frame_size >> 4);
    2035           0 :       target_frame_size -= (target_frame_size >> 4);
    2036             : 
    2037           0 :       if (frame_index <= mid_frame_idx) arf_idx = 1;
    2038             :     }
    2039           0 :     gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx];
    2040           0 :     gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx];
    2041             : 
    2042           0 :     target_frame_size =
    2043           0 :         clamp(target_frame_size, 0, VPXMIN(max_bits, (int)total_group_bits));
    2044             : 
    2045           0 :     gf_group->update_type[frame_index] = LF_UPDATE;
    2046           0 :     gf_group->rf_level[frame_index] = INTER_NORMAL;
    2047             : 
    2048           0 :     gf_group->bit_allocation[frame_index] = target_frame_size;
    2049           0 :     ++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           0 :   gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
    2057           0 :   gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
    2058             : 
    2059           0 :   if (rc->source_alt_ref_pending) {
    2060           0 :     gf_group->update_type[frame_index] = OVERLAY_UPDATE;
    2061           0 :     gf_group->rf_level[frame_index] = INTER_NORMAL;
    2062             : 
    2063             :     // Final setup for second arf and its overlay.
    2064           0 :     if (cpi->multi_arf_enabled) {
    2065           0 :       gf_group->bit_allocation[2] =
    2066           0 :           gf_group->bit_allocation[mid_frame_idx] + mid_boost_bits;
    2067           0 :       gf_group->update_type[mid_frame_idx] = OVERLAY_UPDATE;
    2068           0 :       gf_group->bit_allocation[mid_frame_idx] = 0;
    2069             :     }
    2070             :   } else {
    2071           0 :     gf_group->update_type[frame_index] = GF_UPDATE;
    2072           0 :     gf_group->rf_level[frame_index] = GF_ARF_STD;
    2073             :   }
    2074             : 
    2075             :   // Note whether multi-arf was enabled this group for next time.
    2076           0 :   cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled;
    2077             : }
    2078             : 
    2079             : // Adjusts the ARNF filter for a GF group.
    2080           0 : static void adjust_group_arnr_filter(VP9_COMP *cpi, double section_noise,
    2081             :                                      double section_inter,
    2082             :                                      double section_motion) {
    2083           0 :   TWO_PASS *const twopass = &cpi->twopass;
    2084           0 :   double section_zeromv = section_inter - section_motion;
    2085             : 
    2086           0 :   twopass->arnr_strength_adjustment = 0;
    2087             : 
    2088           0 :   if ((section_zeromv < 0.10) || (section_noise <= (SECTION_NOISE_DEF * 0.75)))
    2089           0 :     twopass->arnr_strength_adjustment -= 1;
    2090           0 :   if (section_zeromv > 0.50) twopass->arnr_strength_adjustment += 1;
    2091           0 : }
    2092             : 
    2093             : // Analyse and define a gf/arf group.
    2094           0 : static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
    2095           0 :   VP9_COMMON *const cm = &cpi->common;
    2096           0 :   RATE_CONTROL *const rc = &cpi->rc;
    2097           0 :   VP9EncoderConfig *const oxcf = &cpi->oxcf;
    2098           0 :   TWO_PASS *const twopass = &cpi->twopass;
    2099             :   FIRSTPASS_STATS next_frame;
    2100           0 :   const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
    2101             :   int i;
    2102             : 
    2103           0 :   double boost_score = 0.0;
    2104           0 :   double old_boost_score = 0.0;
    2105           0 :   double gf_group_err = 0.0;
    2106           0 :   double gf_group_raw_error = 0.0;
    2107           0 :   double gf_group_noise = 0.0;
    2108           0 :   double gf_group_skip_pct = 0.0;
    2109           0 :   double gf_group_inactive_zone_rows = 0.0;
    2110           0 :   double gf_group_inter = 0.0;
    2111           0 :   double gf_group_motion = 0.0;
    2112           0 :   double gf_first_frame_err = 0.0;
    2113           0 :   double mod_frame_err = 0.0;
    2114             : 
    2115           0 :   double mv_ratio_accumulator = 0.0;
    2116           0 :   double decay_accumulator = 1.0;
    2117           0 :   double zero_motion_accumulator = 1.0;
    2118           0 :   double loop_decay_rate = 1.00;
    2119           0 :   double last_loop_decay_rate = 1.00;
    2120             : 
    2121           0 :   double this_frame_mv_in_out = 0.0;
    2122           0 :   double mv_in_out_accumulator = 0.0;
    2123           0 :   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           0 :   double sr_accumulator = 0.0;
    2128           0 :   unsigned int allow_alt_ref = is_altref_enabled(cpi);
    2129             : 
    2130           0 :   int f_boost = 0;
    2131           0 :   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           0 :   const int is_key_frame = frame_is_intra_only(cm);
    2138           0 :   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           0 :   if (is_key_frame == 0) {
    2143           0 :     vp9_zero(twopass->gf_group);
    2144             :   }
    2145             : 
    2146           0 :   vpx_clear_system_state();
    2147           0 :   vp9_zero(next_frame);
    2148             : 
    2149             :   // Load stats for the current frame.
    2150           0 :   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           0 :   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           0 :   if (arf_active_or_kf) {
    2159           0 :     gf_group_err -= gf_first_frame_err;
    2160           0 :     gf_group_raw_error -= this_frame->coded_error;
    2161           0 :     gf_group_noise -= this_frame->frame_noise_energy;
    2162           0 :     gf_group_skip_pct -= this_frame->intra_skip_pct;
    2163           0 :     gf_group_inactive_zone_rows -= this_frame->inactive_zone_rows;
    2164           0 :     gf_group_inter -= this_frame->pcnt_inter;
    2165           0 :     gf_group_motion -= this_frame->pcnt_motion;
    2166             :   }
    2167             : 
    2168             :   // Motion breakout threshold for loop below depends on image size.
    2169           0 :   mv_ratio_accumulator_thresh =
    2170           0 :       (cpi->initial_height + cpi->initial_width) / 4.0;
    2171           0 :   mv_in_out_thresh = (cpi->initial_height + cpi->initial_width) / 300.0;
    2172           0 :   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           0 :     int int_max_q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
    2178             :                                                   cpi->common.bit_depth));
    2179           0 :     int int_lbq = (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex,
    2180             :                                                 cpi->common.bit_depth));
    2181           0 :     active_min_gf_interval =
    2182           0 :         rc->min_gf_interval + arf_active_or_kf + VPXMIN(2, int_max_q / 200);
    2183           0 :     if (active_min_gf_interval > rc->max_gf_interval)
    2184           0 :       active_min_gf_interval = rc->max_gf_interval;
    2185             : 
    2186           0 :     if (cpi->multi_arf_allowed) {
    2187           0 :       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           0 :       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           0 :       if (active_max_gf_interval < active_min_gf_interval)
    2197           0 :         active_max_gf_interval = active_min_gf_interval;
    2198           0 :       else if (active_max_gf_interval > rc->max_gf_interval)
    2199           0 :         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           0 :       if ((active_max_gf_interval <= rc->frames_to_key) &&
    2203           0 :           (active_max_gf_interval >= (rc->frames_to_key - rc->min_gf_interval)))
    2204           0 :         active_max_gf_interval = rc->frames_to_key / 2;
    2205             :     }
    2206             :   }
    2207             : 
    2208           0 :   i = 0;
    2209           0 :   while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
    2210           0 :     ++i;
    2211             : 
    2212             :     // Accumulate error score of frames in this gf group.
    2213           0 :     mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
    2214           0 :     gf_group_err += mod_frame_err;
    2215           0 :     gf_group_raw_error += this_frame->coded_error;
    2216           0 :     gf_group_noise += this_frame->frame_noise_energy;
    2217           0 :     gf_group_skip_pct += this_frame->intra_skip_pct;
    2218           0 :     gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
    2219           0 :     gf_group_inter += this_frame->pcnt_inter;
    2220           0 :     gf_group_motion += this_frame->pcnt_motion;
    2221             : 
    2222           0 :     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           0 :     flash_detected = detect_flash(twopass, 0);
    2227             : 
    2228             :     // Update the motion related elements to the boost calculation.
    2229           0 :     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           0 :     if (!flash_detected) {
    2235           0 :       last_loop_decay_rate = loop_decay_rate;
    2236           0 :       loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
    2237             : 
    2238           0 :       decay_accumulator = decay_accumulator * loop_decay_rate;
    2239             : 
    2240             :       // Monitor for static sections.
    2241           0 :       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           0 :       if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
    2247             :                                      last_loop_decay_rate)) {
    2248           0 :         allow_alt_ref = 0;
    2249           0 :         break;
    2250             :       }
    2251             :     }
    2252             : 
    2253             :     // Calculate a boost number for this frame.
    2254           0 :     sr_accumulator = 0.0;
    2255           0 :     boost_score += decay_accumulator *
    2256           0 :                    calc_frame_boost(cpi, &next_frame, &sr_accumulator,
    2257             :                                     this_frame_mv_in_out, GF_MAX_BOOST);
    2258             : 
    2259             :     // Break out conditions.
    2260           0 :     if (
    2261             :         // Break at active_max_gf_interval unless almost totally static.
    2262           0 :         ((i >= active_max_gf_interval) && (zero_motion_accumulator < 0.995)) ||
    2263             :         (
    2264             :             // Don't break out with a very short interval.
    2265           0 :             (i >= active_min_gf_interval) &&
    2266             :             // If possible dont break very close to a kf
    2267           0 :             ((rc->frames_to_key - i) >= rc->min_gf_interval) &&
    2268           0 :             (!flash_detected) &&
    2269           0 :             ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
    2270           0 :              (abs_mv_in_out_accumulator > abs_mv_in_out_thresh) ||
    2271           0 :              (mv_in_out_accumulator < -mv_in_out_thresh) ||
    2272           0 :              ((boost_score - old_boost_score) < BOOST_BREAKOUT)))) {
    2273           0 :       boost_score = old_boost_score;
    2274           0 :       break;
    2275             :     }
    2276             : 
    2277           0 :     *this_frame = next_frame;
    2278           0 :     old_boost_score = boost_score;
    2279             :   }
    2280             : 
    2281             :   // Was the group length constrained by the requirement for a new KF?
    2282           0 :   rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0;
    2283             : 
    2284             :   // Should we use the alternate reference frame.
    2285           0 :   if (allow_alt_ref && (i < cpi->oxcf.lag_in_frames) &&
    2286           0 :       (i >= rc->min_gf_interval)) {
    2287             :     // Calculate the boost for alt ref.
    2288           0 :     rc->gfu_boost =
    2289           0 :         calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
    2290           0 :     rc->source_alt_ref_pending = 1;
    2291             : 
    2292             :     // Test to see if multi arf is appropriate.
    2293           0 :     cpi->multi_arf_enabled =
    2294           0 :         (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) &&
    2295             :          (zero_motion_accumulator < 0.995))
    2296             :             ? 1
    2297           0 :             : 0;
    2298             :   } else {
    2299           0 :     rc->gfu_boost = VPXMAX((int)boost_score, MIN_ARF_GF_BOOST);
    2300           0 :     rc->source_alt_ref_pending = 0;
    2301             :   }
    2302             : 
    2303             :   // Limit maximum boost based on interval length.
    2304           0 :   rc->gfu_boost = VPXMIN((int)rc->gfu_boost, i * 200);
    2305             : 
    2306             :   // Set the interval until the next gf.
    2307           0 :   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           0 :   if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
    2313           0 :     int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
    2314           0 :     int new_gf_interval = (rc->baseline_gf_interval + count) & (~count);
    2315             :     int j;
    2316           0 :     for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) {
    2317           0 :       if (EOF == input_stats(twopass, this_frame)) break;
    2318           0 :       gf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
    2319           0 :       gf_group_raw_error += this_frame->coded_error;
    2320           0 :       gf_group_noise += this_frame->frame_noise_energy;
    2321           0 :       gf_group_skip_pct += this_frame->intra_skip_pct;
    2322           0 :       gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
    2323           0 :       gf_group_inter += this_frame->pcnt_inter;
    2324           0 :       gf_group_motion += this_frame->pcnt_motion;
    2325             :     }
    2326           0 :     rc->baseline_gf_interval = new_gf_interval;
    2327             :   }
    2328             : 
    2329           0 :   rc->frames_till_gf_update_due = rc->baseline_gf_interval;
    2330             : 
    2331             :   // Reset the file position.
    2332           0 :   reset_fpf_position(twopass, start_pos);
    2333             : 
    2334             :   // Calculate the bits to be allocated to the gf/arf group as a whole
    2335           0 :   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           0 :   if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) {
    2343           0 :     const int vbr_group_bits_per_frame =
    2344           0 :         (int)(gf_group_bits / rc->baseline_gf_interval);
    2345           0 :     const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval;
    2346           0 :     const double group_av_noise = gf_group_noise / rc->baseline_gf_interval;
    2347           0 :     const double group_av_skip_pct =
    2348           0 :         gf_group_skip_pct / rc->baseline_gf_interval;
    2349           0 :     const double group_av_inactive_zone =
    2350           0 :         ((gf_group_inactive_zone_rows * 2) /
    2351           0 :          (rc->baseline_gf_interval * (double)cm->mb_rows));
    2352           0 :     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           0 :     twopass->active_worst_quality =
    2356           0 :         (tmp_q + (twopass->active_worst_quality * 3)) >> 2;
    2357             :   }
    2358             : 
    2359             :   // Context Adjustment of ARNR filter strength
    2360           0 :   if (rc->baseline_gf_interval > 1) {
    2361           0 :     adjust_group_arnr_filter(cpi, (gf_group_noise / rc->baseline_gf_interval),
    2362           0 :                              (gf_group_inter / rc->baseline_gf_interval),
    2363           0 :                              (gf_group_motion / rc->baseline_gf_interval));
    2364             :   } else {
    2365           0 :     twopass->arnr_strength_adjustment = 0;
    2366             :   }
    2367             : 
    2368             :   // Calculate the extra bits to be used for boosted frame(s)
    2369           0 :   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           0 :   twopass->kf_group_error_left -= (int64_t)gf_group_err;
    2374             : 
    2375             :   // Allocate bits to each of the frames in the GF group.
    2376           0 :   allocate_gf_group_bits(cpi, gf_group_bits, gf_arf_bits);
    2377             : 
    2378             :   // Reset the file position.
    2379           0 :   reset_fpf_position(twopass, start_pos);
    2380             : 
    2381             :   // Calculate a section intra ratio used in setting max loop filter.
    2382           0 :   if (cpi->common.frame_type != KEY_FRAME) {
    2383           0 :     twopass->section_intra_rating = calculate_section_intra_ratio(
    2384             :         start_pos, twopass->stats_in_end, rc->baseline_gf_interval);
    2385             :   }
    2386             : 
    2387           0 :   if (oxcf->resize_mode == RESIZE_DYNAMIC) {
    2388             :     // Default to starting GF groups at normal frame size.
    2389           0 :     cpi->rc.next_frame_size_selector = UNSCALED;
    2390             :   }
    2391             : 
    2392             :   // Reset rolling actual and target bits counters for ARF groups.
    2393           0 :   twopass->rolling_arf_group_target_bits = 0;
    2394           0 :   twopass->rolling_arf_group_actual_bits = 0;
    2395           0 : }
    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             : 
    2423           0 : static 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           0 :   int is_viable_kf = 0;
    2428           0 :   double pcnt_intra = 1.0 - this_frame->pcnt_inter;
    2429           0 :   double modified_pcnt_inter =
    2430           0 :       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           0 :   if ((this_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
    2436           0 :       (next_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
    2437           0 :       ((this_frame->pcnt_inter < VERY_LOW_INTER_THRESH) ||
    2438           0 :        ((pcnt_intra > MIN_INTRA_LEVEL) &&
    2439           0 :         (pcnt_intra > (INTRA_VS_INTER_THRESH * modified_pcnt_inter)) &&
    2440           0 :         ((this_frame->intra_error /
    2441           0 :           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) <
    2442           0 :          KF_II_ERR_THRESHOLD) &&
    2443           0 :         ((fabs(last_frame->coded_error - this_frame->coded_error) /
    2444           0 :               DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
    2445           0 :           ERR_CHANGE_THRESHOLD) ||
    2446           0 :          (fabs(last_frame->intra_error - this_frame->intra_error) /
    2447           0 :               DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
    2448           0 :           ERR_CHANGE_THRESHOLD) ||
    2449           0 :          ((next_frame->intra_error /
    2450           0 :            DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) >
    2451             :           II_IMPROVEMENT_THRESHOLD))))) {
    2452             :     int i;
    2453           0 :     const FIRSTPASS_STATS *start_pos = twopass->stats_in;
    2454           0 :     FIRSTPASS_STATS local_next_frame = *next_frame;
    2455           0 :     double boost_score = 0.0;
    2456           0 :     double old_boost_score = 0.0;
    2457           0 :     double decay_accumulator = 1.0;
    2458             : 
    2459             :     // Examine how well the key frame predicts subsequent frames.
    2460           0 :     for (i = 0; i < 16; ++i) {
    2461           0 :       double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error /
    2462           0 :                              DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
    2463             : 
    2464           0 :       if (next_iiratio > KF_II_MAX) next_iiratio = KF_II_MAX;
    2465             : 
    2466             :       // Cumulative effect of decay in prediction quality.
    2467           0 :       if (local_next_frame.pcnt_inter > 0.85)
    2468           0 :         decay_accumulator *= local_next_frame.pcnt_inter;
    2469             :       else
    2470           0 :         decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
    2471             : 
    2472             :       // Keep a running total.
    2473           0 :       boost_score += (decay_accumulator * next_iiratio);
    2474             : 
    2475             :       // Test various breakout clauses.
    2476           0 :       if ((local_next_frame.pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
    2477           0 :           (((local_next_frame.pcnt_inter - local_next_frame.pcnt_neutral) <
    2478           0 :             0.20) &&
    2479           0 :            (next_iiratio < 3.0)) ||
    2480           0 :           ((boost_score - old_boost_score) < 3.0) ||
    2481           0 :           (local_next_frame.intra_error < 200)) {
    2482             :         break;
    2483             :       }
    2484             : 
    2485           0 :       old_boost_score = boost_score;
    2486             : 
    2487             :       // Get the next frame details
    2488           0 :       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           0 :     if (boost_score > 30.0 && (i > 3)) {
    2494           0 :       is_viable_kf = 1;
    2495             :     } else {
    2496             :       // Reset the file position
    2497           0 :       reset_fpf_position(twopass, start_pos);
    2498             : 
    2499           0 :       is_viable_kf = 0;
    2500             :     }
    2501             :   }
    2502             : 
    2503           0 :   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             : 
    2512           0 : static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
    2513             :   int i, j;
    2514           0 :   RATE_CONTROL *const rc = &cpi->rc;
    2515           0 :   TWO_PASS *const twopass = &cpi->twopass;
    2516           0 :   GF_GROUP *const gf_group = &twopass->gf_group;
    2517           0 :   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    2518           0 :   const FIRSTPASS_STATS first_frame = *this_frame;
    2519           0 :   const FIRSTPASS_STATS *const start_position = twopass->stats_in;
    2520             :   FIRSTPASS_STATS next_frame;
    2521             :   FIRSTPASS_STATS last_frame;
    2522           0 :   int kf_bits = 0;
    2523           0 :   double decay_accumulator = 1.0;
    2524           0 :   double zero_motion_accumulator = 1.0;
    2525           0 :   double boost_score = 0.0;
    2526           0 :   double kf_mod_err = 0.0;
    2527           0 :   double kf_group_err = 0.0;
    2528             :   double recent_loop_decay[FRAMES_TO_CHECK_DECAY];
    2529           0 :   double sr_accumulator = 0.0;
    2530             : 
    2531           0 :   vp9_zero(next_frame);
    2532             : 
    2533           0 :   cpi->common.frame_type = KEY_FRAME;
    2534             : 
    2535             :   // Reset the GF group data structures.
    2536           0 :   vp9_zero(*gf_group);
    2537             : 
    2538             :   // Is this a forced key frame by interval.
    2539           0 :   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           0 :   rc->source_alt_ref_active = 0;
    2544           0 :   cpi->multi_arf_last_grp_enabled = 0;
    2545             : 
    2546             :   // KF is always a GF so clear frames till next gf counter.
    2547           0 :   rc->frames_till_gf_update_due = 0;
    2548             : 
    2549           0 :   rc->frames_to_key = 1;
    2550             : 
    2551           0 :   twopass->kf_group_bits = 0;        // Total bits available to kf group
    2552           0 :   twopass->kf_group_error_left = 0;  // Group modified error score.
    2553             : 
    2554           0 :   kf_mod_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
    2555             : 
    2556             :   // Initialize the decay rates for the recent frames to check
    2557           0 :   for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j) recent_loop_decay[j] = 1.0;
    2558             : 
    2559             :   // Find the next keyframe.
    2560           0 :   i = 0;
    2561           0 :   while (twopass->stats_in < twopass->stats_in_end &&
    2562           0 :          rc->frames_to_key < cpi->oxcf.key_freq) {
    2563             :     // Accumulate kf group error.
    2564           0 :     kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
    2565             : 
    2566             :     // Load the next frame's stats.
    2567           0 :     last_frame = *this_frame;
    2568           0 :     input_stats(twopass, this_frame);
    2569             : 
    2570             :     // Provided that we are not at the end of the file...
    2571           0 :     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           0 :       if (test_candidate_kf(twopass, &last_frame, this_frame,
    2576             :                             twopass->stats_in))
    2577           0 :         break;
    2578             : 
    2579             :       // How fast is the prediction quality decaying?
    2580           0 :       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           0 :       recent_loop_decay[i % FRAMES_TO_CHECK_DECAY] = loop_decay_rate;
    2586           0 :       decay_accumulator = 1.0;
    2587           0 :       for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
    2588           0 :         decay_accumulator *= recent_loop_decay[j];
    2589             : 
    2590             :       // Special check for transition or high motion followed by a
    2591             :       // static scene.
    2592           0 :       if (detect_transition_to_still(cpi, i, cpi->oxcf.key_freq - i,
    2593             :                                      loop_decay_rate, decay_accumulator))
    2594           0 :         break;
    2595             : 
    2596             :       // Step on to the next frame.
    2597           0 :       ++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           0 :       if (rc->frames_to_key >= 2 * cpi->oxcf.key_freq) break;
    2602             :     } else {
    2603           0 :       ++rc->frames_to_key;
    2604             :     }
    2605           0 :     ++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           0 :   if (cpi->oxcf.auto_key && rc->frames_to_key > cpi->oxcf.key_freq) {
    2613           0 :     FIRSTPASS_STATS tmp_frame = first_frame;
    2614             : 
    2615           0 :     rc->frames_to_key /= 2;
    2616             : 
    2617             :     // Reset to the start of the group.
    2618           0 :     reset_fpf_position(twopass, start_position);
    2619             : 
    2620           0 :     kf_group_err = 0.0;
    2621             : 
    2622             :     // Rescan to get the correct error data for the forced kf group.
    2623           0 :     for (i = 0; i < rc->frames_to_key; ++i) {
    2624           0 :       kf_group_err += calculate_modified_err(cpi, twopass, oxcf, &tmp_frame);
    2625           0 :       input_stats(twopass, &tmp_frame);
    2626             :     }
    2627           0 :     rc->next_key_frame_forced = 1;
    2628           0 :   } else if (twopass->stats_in == twopass->stats_in_end ||
    2629           0 :              rc->frames_to_key >= cpi->oxcf.key_freq) {
    2630           0 :     rc->next_key_frame_forced = 1;
    2631             :   } else {
    2632           0 :     rc->next_key_frame_forced = 0;
    2633             :   }
    2634             : 
    2635           0 :   if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
    2636           0 :     int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
    2637           0 :     int new_frame_to_key = (rc->frames_to_key + count) & (~count);
    2638             :     int j;
    2639           0 :     for (j = 0; j < new_frame_to_key - rc->frames_to_key; ++j) {
    2640           0 :       if (EOF == input_stats(twopass, this_frame)) break;
    2641           0 :       kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
    2642             :     }
    2643           0 :     rc->frames_to_key = new_frame_to_key;
    2644             :   }
    2645             : 
    2646             :   // Special case for the last key frame of the file.
    2647           0 :   if (twopass->stats_in >= twopass->stats_in_end) {
    2648             :     // Accumulate kf group error.
    2649           0 :     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           0 :   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           0 :     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           0 :     twopass->kf_group_bits = (int64_t)(
    2663           0 :         twopass->bits_left * (kf_group_err / twopass->modified_error_left));
    2664             : 
    2665             :     // Clip based on maximum per frame rate defined by the user.
    2666           0 :     max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
    2667           0 :     if (twopass->kf_group_bits > max_grp_bits)
    2668           0 :       twopass->kf_group_bits = max_grp_bits;
    2669             :   } else {
    2670           0 :     twopass->kf_group_bits = 0;
    2671             :   }
    2672           0 :   twopass->kf_group_bits = VPXMAX(0, twopass->kf_group_bits);
    2673             : 
    2674             :   // Reset the first pass file position.
    2675           0 :   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           0 :   boost_score = 0.0;
    2680             : 
    2681           0 :   for (i = 0; i < (rc->frames_to_key - 1); ++i) {
    2682           0 :     if (EOF == input_stats(twopass, &next_frame)) break;
    2683             : 
    2684           0 :     if (i <= KF_BOOST_SCAN_MAX_FRAMES) {
    2685             :       double frame_boost;
    2686             :       double zm_factor;
    2687             : 
    2688             :       // Monitor for static sections.
    2689           0 :       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           0 :       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           0 :       if (i < 2) sr_accumulator = 0.0;
    2700           0 :       frame_boost = calc_kf_frame_boost(cpi, &next_frame, &sr_accumulator, 0,
    2701             :                                         KF_MAX_FRAME_BOOST * zm_factor);
    2702             : 
    2703           0 :       boost_score += frame_boost;
    2704           0 :       if (frame_boost < 25.00) break;
    2705             :     } else {
    2706           0 :       break;
    2707             :     }
    2708             :   }
    2709             : 
    2710           0 :   reset_fpf_position(twopass, start_position);
    2711             : 
    2712             :   // Store the zero motion percentage
    2713           0 :   twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
    2714             : 
    2715             :   // Calculate a section intra ratio used in setting max loop filter.
    2716           0 :   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           0 :   rc->kf_boost = VPXMAX((int)boost_score, (rc->frames_to_key * 3));
    2721           0 :   rc->kf_boost = VPXMAX(rc->kf_boost, MIN_KF_TOT_BOOST);
    2722           0 :   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           0 :   kf_bits = calculate_boost_bits((rc->frames_to_key - 1), rc->kf_boost,
    2726             :                                  twopass->kf_group_bits);
    2727             : 
    2728           0 :   twopass->kf_group_bits -= kf_bits;
    2729             : 
    2730             :   // Save the bits to spend on the key frame.
    2731           0 :   gf_group->bit_allocation[0] = kf_bits;
    2732           0 :   gf_group->update_type[0] = KF_UPDATE;
    2733           0 :   gf_group->rf_level[0] = KF_STD;
    2734             : 
    2735             :   // Note the total error score of the kf group minus the key frame itself.
    2736           0 :   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           0 :   twopass->modified_error_left -= kf_group_err;
    2742             : 
    2743           0 :   if (oxcf->resize_mode == RESIZE_DYNAMIC) {
    2744             :     // Default to normal-sized frame on keyframes.
    2745           0 :     cpi->rc.next_frame_size_selector = UNSCALED;
    2746             :   }
    2747           0 : }
    2748             : 
    2749             : // Define the reference buffers that will be updated post encode.
    2750           0 : static void configure_buffer_updates(VP9_COMP *cpi) {
    2751           0 :   TWO_PASS *const twopass = &cpi->twopass;
    2752             : 
    2753           0 :   cpi->rc.is_src_frame_alt_ref = 0;
    2754           0 :   switch (twopass->gf_group.update_type[twopass->gf_group.index]) {
    2755             :     case KF_UPDATE:
    2756           0 :       cpi->refresh_last_frame = 1;
    2757           0 :       cpi->refresh_golden_frame = 1;
    2758           0 :       cpi->refresh_alt_ref_frame = 1;
    2759           0 :       break;
    2760             :     case LF_UPDATE:
    2761           0 :       cpi->refresh_last_frame = 1;
    2762           0 :       cpi->refresh_golden_frame = 0;
    2763           0 :       cpi->refresh_alt_ref_frame = 0;
    2764           0 :       break;
    2765             :     case GF_UPDATE:
    2766           0 :       cpi->refresh_last_frame = 1;
    2767           0 :       cpi->refresh_golden_frame = 1;
    2768           0 :       cpi->refresh_alt_ref_frame = 0;
    2769           0 :       break;
    2770             :     case OVERLAY_UPDATE:
    2771           0 :       cpi->refresh_last_frame = 0;
    2772           0 :       cpi->refresh_golden_frame = 1;
    2773           0 :       cpi->refresh_alt_ref_frame = 0;
    2774           0 :       cpi->rc.is_src_frame_alt_ref = 1;
    2775           0 :       break;
    2776             :     case ARF_UPDATE:
    2777           0 :       cpi->refresh_last_frame = 0;
    2778           0 :       cpi->refresh_golden_frame = 0;
    2779           0 :       cpi->refresh_alt_ref_frame = 1;
    2780           0 :       break;
    2781           0 :     default: assert(0); break;
    2782             :   }
    2783           0 :   if (is_two_pass_svc(cpi)) {
    2784           0 :     if (cpi->svc.temporal_layer_id > 0) {
    2785           0 :       cpi->refresh_last_frame = 0;
    2786           0 :       cpi->refresh_golden_frame = 0;
    2787             :     }
    2788           0 :     if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0)
    2789           0 :       cpi->refresh_golden_frame = 0;
    2790           0 :     if (cpi->alt_ref_source == NULL) cpi->refresh_alt_ref_frame = 0;
    2791             :   }
    2792           0 : }
    2793             : 
    2794           0 : static 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           0 :   const SVC *const svc = &cpi->svc;
    2800           0 :   const TWO_PASS *const twopass =
    2801           0 :       is_two_pass_svc(cpi) ? &svc->layer_context[svc->spatial_layer_id].twopass
    2802           0 :                            : &cpi->twopass;
    2803             : 
    2804           0 :   return (!frame_is_intra_only(&cpi->common) &&
    2805           0 :           twopass->stats_in - 2 > twopass->stats_in_start &&
    2806           0 :           twopass->stats_in < twopass->stats_in_end &&
    2807           0 :           (twopass->stats_in - 1)->pcnt_inter -
    2808           0 :                   (twopass->stats_in - 1)->pcnt_motion ==
    2809           0 :               1 &&
    2810           0 :           (twopass->stats_in - 2)->pcnt_inter -
    2811           0 :                   (twopass->stats_in - 2)->pcnt_motion ==
    2812           0 :               1 &&
    2813           0 :           twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
    2814             : }
    2815             : 
    2816           0 : void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
    2817           0 :   VP9_COMMON *const cm = &cpi->common;
    2818           0 :   RATE_CONTROL *const rc = &cpi->rc;
    2819           0 :   TWO_PASS *const twopass = &cpi->twopass;
    2820           0 :   GF_GROUP *const gf_group = &twopass->gf_group;
    2821             :   FIRSTPASS_STATS this_frame;
    2822             : 
    2823             :   int target_rate;
    2824           0 :   LAYER_CONTEXT *const lc =
    2825           0 :       is_two_pass_svc(cpi) ? &cpi->svc.layer_context[cpi->svc.spatial_layer_id]
    2826           0 :                            : 0;
    2827             : 
    2828           0 :   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           0 :   if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
    2833             :     int target_rate;
    2834           0 :     configure_buffer_updates(cpi);
    2835           0 :     target_rate = gf_group->bit_allocation[gf_group->index];
    2836           0 :     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
    2837           0 :     rc->base_frame_target = target_rate;
    2838             : 
    2839           0 :     cm->frame_type = INTER_FRAME;
    2840             : 
    2841           0 :     if (lc != NULL) {
    2842           0 :       if (cpi->svc.spatial_layer_id == 0) {
    2843           0 :         lc->is_key_frame = 0;
    2844             :       } else {
    2845           0 :         lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
    2846             : 
    2847           0 :         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           0 :     if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
    2854           0 :         (!cpi->use_svc || is_two_pass_svc(cpi))) {
    2855           0 :       cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
    2856             :     }
    2857             : 
    2858           0 :     return;
    2859             :   }
    2860             : 
    2861           0 :   vpx_clear_system_state();
    2862             : 
    2863           0 :   if (cpi->oxcf.rc_mode == VPX_Q) {
    2864           0 :     twopass->active_worst_quality = cpi->oxcf.cq_level;
    2865           0 :   } else if (cm->current_video_frame == 0 ||
    2866           0 :              (lc != NULL && lc->current_video_frame_in_layer == 0)) {
    2867           0 :     const int frames_left =
    2868           0 :         (int)(twopass->total_stats.count -
    2869           0 :               ((lc != NULL) ? lc->current_video_frame_in_layer
    2870           0 :                             : cm->current_video_frame));
    2871             :     // Special case code for first frame.
    2872           0 :     const int section_target_bandwidth =
    2873           0 :         (int)(twopass->bits_left / frames_left);
    2874           0 :     const double section_length = twopass->total_left_stats.count;
    2875           0 :     const double section_error =
    2876           0 :         twopass->total_left_stats.coded_error / section_length;
    2877           0 :     const double section_intra_skip =
    2878           0 :         twopass->total_left_stats.intra_skip_pct / section_length;
    2879           0 :     const double section_inactive_zone =
    2880           0 :         (twopass->total_left_stats.inactive_zone_rows * 2) /
    2881           0 :         ((double)cm->mb_rows * section_length);
    2882           0 :     const double section_noise =
    2883           0 :         twopass->total_left_stats.frame_noise_energy / section_length;
    2884             :     int tmp_q;
    2885             : 
    2886           0 :     tmp_q = get_twopass_worst_quality(
    2887             :         cpi, section_error, section_intra_skip + section_inactive_zone,
    2888             :         section_noise, section_target_bandwidth);
    2889             : 
    2890           0 :     twopass->active_worst_quality = tmp_q;
    2891           0 :     twopass->baseline_active_worst_quality = tmp_q;
    2892           0 :     rc->ni_av_qi = tmp_q;
    2893           0 :     rc->last_q[INTER_FRAME] = tmp_q;
    2894           0 :     rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
    2895           0 :     rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
    2896           0 :     rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2;
    2897           0 :     rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
    2898             :   }
    2899           0 :   vp9_zero(this_frame);
    2900           0 :   if (EOF == input_stats(twopass, &this_frame)) return;
    2901             : 
    2902             :   // Set the frame content type flag.
    2903           0 :   if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH)
    2904           0 :     twopass->fr_content_type = FC_GRAPHICS_ANIMATION;
    2905             :   else
    2906           0 :     twopass->fr_content_type = FC_NORMAL;
    2907             : 
    2908             :   // Keyframe and section processing.
    2909           0 :   if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) {
    2910             :     FIRSTPASS_STATS this_frame_copy;
    2911           0 :     this_frame_copy = this_frame;
    2912             :     // Define next KF group and assign bits to it.
    2913           0 :     find_next_key_frame(cpi, &this_frame);
    2914           0 :     this_frame = this_frame_copy;
    2915             :   } else {
    2916           0 :     cm->frame_type = INTER_FRAME;
    2917             :   }
    2918             : 
    2919           0 :   if (lc != NULL) {
    2920           0 :     if (cpi->svc.spatial_layer_id == 0) {
    2921           0 :       lc->is_key_frame = (cm->frame_type == KEY_FRAME);
    2922           0 :       if (lc->is_key_frame) {
    2923           0 :         cpi->ref_frame_flags &=
    2924             :             (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
    2925           0 :         lc->frames_from_key_frame = 0;
    2926             :         // Encode an intra only empty frame since we have a key frame.
    2927           0 :         cpi->svc.encode_intra_empty_frame = 1;
    2928             :       }
    2929             :     } else {
    2930           0 :       cm->frame_type = INTER_FRAME;
    2931           0 :       lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
    2932             : 
    2933           0 :       if (lc->is_key_frame) {
    2934           0 :         cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
    2935           0 :         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           0 :   if (rc->frames_till_gf_update_due == 0) {
    2942           0 :     define_gf_group(cpi, &this_frame);
    2943             : 
    2944           0 :     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
    2945           0 :     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           0 :   configure_buffer_updates(cpi);
    2962             : 
    2963             :   // Do the firstpass stats indicate that this frame is skippable for the
    2964             :   // partition search?
    2965           0 :   if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
    2966           0 :       (!cpi->use_svc || is_two_pass_svc(cpi))) {
    2967           0 :     cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
    2968             :   }
    2969             : 
    2970           0 :   target_rate = gf_group->bit_allocation[gf_group->index];
    2971           0 :   rc->base_frame_target = target_rate;
    2972             : 
    2973             :   {
    2974           0 :     const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
    2975             :                             ? cpi->initial_mbs
    2976           0 :                             : 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           0 :     twopass->mb_av_energy =
    2980           0 :         log(((this_frame.intra_error * 256.0) / num_mbs) + 1.0);
    2981           0 :     twopass->mb_smooth_pct = this_frame.intra_smooth_pct;
    2982             :   }
    2983             : 
    2984             :   // Update the total stats remaining structure.
    2985           0 :   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
    2991           0 : void vp9_twopass_postencode_update(VP9_COMP *cpi) {
    2992           0 :   TWO_PASS *const twopass = &cpi->twopass;
    2993           0 :   RATE_CONTROL *const rc = &cpi->rc;
    2994           0 :   VP9_COMMON *const cm = &cpi->common;
    2995           0 :   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           0 :   rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
    3003           0 :   twopass->bits_left = VPXMAX(twopass->bits_left - bits_used, 0);
    3004             : 
    3005             :   // Target vs actual bits for this arf group.
    3006           0 :   twopass->rolling_arf_group_target_bits += rc->this_frame_target;
    3007           0 :   twopass->rolling_arf_group_actual_bits += rc->projected_frame_size;
    3008             : 
    3009             :   // Calculate the pct rc error.
    3010           0 :   if (rc->total_actual_bits) {
    3011           0 :     rc->rate_error_estimate =
    3012           0 :         (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
    3013           0 :     rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
    3014             :   } else {
    3015           0 :     rc->rate_error_estimate = 0;
    3016             :   }
    3017             : 
    3018           0 :   if (cpi->common.frame_type != KEY_FRAME &&
    3019           0 :       !vp9_is_upper_layer_key_frame(cpi)) {
    3020           0 :     twopass->kf_group_bits -= bits_used;
    3021           0 :     twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
    3022             :   }
    3023           0 :   twopass->kf_group_bits = VPXMAX(twopass->kf_group_bits, 0);
    3024             : 
    3025             :   // Increment the gf group index ready for the next frame.
    3026           0 :   ++twopass->gf_group.index;
    3027             : 
    3028             :   // If the rate control is drifting consider adjustment to min or maxq.
    3029           0 :   if ((cpi->oxcf.rc_mode != VPX_Q) && !cpi->rc.is_src_frame_alt_ref) {
    3030           0 :     const int maxq_adj_limit =
    3031           0 :         rc->worst_quality - twopass->active_worst_quality;
    3032           0 :     const int minq_adj_limit =
    3033           0 :         (cpi->oxcf.rc_mode == VPX_CQ ? MINQ_ADJ_LIMIT_CQ : MINQ_ADJ_LIMIT);
    3034           0 :     int aq_extend_min = 0;
    3035           0 :     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           0 :     if (cpi->oxcf.aq_mode != NO_AQ) {
    3040           0 :       if (cm->seg.aq_av_offset < 0) {
    3041             :         // The balance of the AQ map tends towarda lowering the average Q.
    3042           0 :         aq_extend_min = 0;
    3043           0 :         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           0 :         aq_extend_min = VPXMIN(minq_adj_limit, cm->seg.aq_av_offset);
    3047           0 :         aq_extend_max = 0;
    3048             :       }
    3049             :     }
    3050             : 
    3051             :     // Undershoot.
    3052           0 :     if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) {
    3053           0 :       --twopass->extend_maxq;
    3054           0 :       if (rc->rolling_target_bits >= rc->rolling_actual_bits)
    3055           0 :         ++twopass->extend_minq;
    3056             :       // Overshoot.
    3057           0 :     } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) {
    3058           0 :       --twopass->extend_minq;
    3059           0 :       if (rc->rolling_target_bits < rc->rolling_actual_bits)
    3060           0 :         ++twopass->extend_maxq;
    3061             :     } else {
    3062             :       // Adjustment for extreme local overshoot.
    3063           0 :       if (rc->projected_frame_size > (2 * rc->base_frame_target) &&
    3064           0 :           rc->projected_frame_size > (2 * rc->avg_frame_bandwidth))
    3065           0 :         ++twopass->extend_maxq;
    3066             : 
    3067             :       // Unwind undershoot or overshoot adjustment.
    3068           0 :       if (rc->rolling_target_bits < rc->rolling_actual_bits)
    3069           0 :         --twopass->extend_minq;
    3070           0 :       else if (rc->rolling_target_bits > rc->rolling_actual_bits)
    3071           0 :         --twopass->extend_maxq;
    3072             :     }
    3073             : 
    3074           0 :     twopass->extend_minq =
    3075           0 :         clamp(twopass->extend_minq, aq_extend_min, minq_adj_limit);
    3076           0 :     twopass->extend_maxq =
    3077           0 :         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           0 :     if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) {
    3084           0 :       int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO;
    3085           0 :       if (rc->projected_frame_size < fast_extra_thresh) {
    3086           0 :         rc->vbr_bits_off_target_fast +=
    3087           0 :             fast_extra_thresh - rc->projected_frame_size;
    3088           0 :         rc->vbr_bits_off_target_fast =
    3089           0 :             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           0 :         if (rc->avg_frame_bandwidth) {
    3093           0 :           twopass->extend_minq_fast =
    3094           0 :               (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth);
    3095             :         }
    3096           0 :         twopass->extend_minq_fast = VPXMIN(
    3097             :             twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
    3098           0 :       } else if (rc->vbr_bits_off_target_fast) {
    3099           0 :         twopass->extend_minq_fast = VPXMIN(
    3100             :             twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
    3101             :       } else {
    3102           0 :         twopass->extend_minq_fast = 0;
    3103             :       }
    3104             :     }
    3105             :   }
    3106           0 : }

Generated by: LCOV version 1.13