LCOV - code coverage report
Current view: top level - third_party/aom/av1/encoder - firstpass.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 1383 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 47 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13