tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

firstpass.c (64145B)


      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 "config/aom_dsp_rtcd.h"
     17 #include "config/aom_scale_rtcd.h"
     18 
     19 #include "aom_dsp/aom_dsp_common.h"
     20 #include "aom_dsp/variance.h"
     21 #include "aom_mem/aom_mem.h"
     22 #include "aom_ports/mem.h"
     23 #include "aom_scale/yv12config.h"
     24 #include "aom_util/aom_pthread.h"
     25 
     26 #include "av1/common/entropymv.h"
     27 #include "av1/common/quant_common.h"
     28 #include "av1/common/reconinter.h"  // av1_setup_dst_planes()
     29 #include "av1/common/reconintra.h"
     30 #include "av1/common/txb_common.h"
     31 #include "av1/encoder/aq_variance.h"
     32 #include "av1/encoder/av1_quantize.h"
     33 #include "av1/encoder/block.h"
     34 #include "av1/encoder/dwt.h"
     35 #include "av1/encoder/encodeframe.h"
     36 #include "av1/encoder/encodeframe_utils.h"
     37 #include "av1/encoder/encodemb.h"
     38 #include "av1/encoder/encodemv.h"
     39 #include "av1/encoder/encoder.h"
     40 #include "av1/encoder/encoder_utils.h"
     41 #include "av1/encoder/encode_strategy.h"
     42 #include "av1/encoder/ethread.h"
     43 #include "av1/encoder/extend.h"
     44 #include "av1/encoder/firstpass.h"
     45 #include "av1/encoder/mcomp.h"
     46 #include "av1/encoder/rd.h"
     47 #include "av1/encoder/reconinter_enc.h"
     48 
     49 #define OUTPUT_FPF 0
     50 
     51 #define FIRST_PASS_Q 10.0
     52 #define INTRA_MODE_PENALTY 1024
     53 #define NEW_MV_MODE_PENALTY 32
     54 #define DARK_THRESH 64
     55 
     56 #define NCOUNT_INTRA_THRESH 8192
     57 #define NCOUNT_INTRA_FACTOR 3
     58 
     59 #define INVALID_FP_STATS_TO_PREDICT_FLAT_GOP -1
     60 
     61 static inline void output_stats(FIRSTPASS_STATS *stats,
     62                                struct aom_codec_pkt_list *pktlist) {
     63  struct aom_codec_cx_pkt pkt;
     64  pkt.kind = AOM_CODEC_STATS_PKT;
     65  pkt.data.twopass_stats.buf = stats;
     66  pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
     67  if (pktlist != NULL) aom_codec_pkt_list_add(pktlist, &pkt);
     68 
     69 // TEMP debug code
     70 #if OUTPUT_FPF
     71  {
     72    FILE *fpfile;
     73    fpfile = fopen("firstpass.stt", "a");
     74 
     75    fprintf(fpfile,
     76            "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf"
     77            "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf"
     78            "%12.4lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf\n",
     79            stats->frame, stats->weight, stats->intra_error, stats->coded_error,
     80            stats->sr_coded_error, stats->pcnt_inter, stats->pcnt_motion,
     81            stats->pcnt_second_ref, stats->pcnt_neutral, stats->intra_skip_pct,
     82            stats->inactive_zone_rows, stats->inactive_zone_cols, stats->MVr,
     83            stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv,
     84            stats->MVcv, stats->mv_in_out_count, stats->new_mv_count,
     85            stats->count, stats->duration);
     86    fclose(fpfile);
     87  }
     88 #endif
     89 }
     90 
     91 void av1_twopass_zero_stats(FIRSTPASS_STATS *section) {
     92  section->frame = 0.0;
     93  section->weight = 0.0;
     94  section->intra_error = 0.0;
     95  section->frame_avg_wavelet_energy = 0.0;
     96  section->coded_error = 0.0;
     97  section->log_intra_error = 0.0;
     98  section->log_coded_error = 0.0;
     99  section->sr_coded_error = 0.0;
    100  section->pcnt_inter = 0.0;
    101  section->pcnt_motion = 0.0;
    102  section->pcnt_second_ref = 0.0;
    103  section->pcnt_neutral = 0.0;
    104  section->intra_skip_pct = 0.0;
    105  section->inactive_zone_rows = 0.0;
    106  section->inactive_zone_cols = 0.0;
    107  section->MVr = 0.0;
    108  section->mvr_abs = 0.0;
    109  section->MVc = 0.0;
    110  section->mvc_abs = 0.0;
    111  section->MVrv = 0.0;
    112  section->MVcv = 0.0;
    113  section->mv_in_out_count = 0.0;
    114  section->new_mv_count = 0.0;
    115  section->count = 0.0;
    116  section->duration = 1.0;
    117  section->is_flash = 0;
    118  section->noise_var = 0;
    119  section->cor_coeff = 1.0;
    120 }
    121 
    122 void av1_accumulate_stats(FIRSTPASS_STATS *section,
    123                          const FIRSTPASS_STATS *frame) {
    124  section->frame += frame->frame;
    125  section->weight += frame->weight;
    126  section->intra_error += frame->intra_error;
    127  section->log_intra_error += log1p(frame->intra_error);
    128  section->log_coded_error += log1p(frame->coded_error);
    129  section->frame_avg_wavelet_energy += frame->frame_avg_wavelet_energy;
    130  section->coded_error += frame->coded_error;
    131  section->sr_coded_error += frame->sr_coded_error;
    132  section->pcnt_inter += frame->pcnt_inter;
    133  section->pcnt_motion += frame->pcnt_motion;
    134  section->pcnt_second_ref += frame->pcnt_second_ref;
    135  section->pcnt_neutral += frame->pcnt_neutral;
    136  section->intra_skip_pct += frame->intra_skip_pct;
    137  section->inactive_zone_rows += frame->inactive_zone_rows;
    138  section->inactive_zone_cols += frame->inactive_zone_cols;
    139  section->MVr += frame->MVr;
    140  section->mvr_abs += frame->mvr_abs;
    141  section->MVc += frame->MVc;
    142  section->mvc_abs += frame->mvc_abs;
    143  section->MVrv += frame->MVrv;
    144  section->MVcv += frame->MVcv;
    145  section->mv_in_out_count += frame->mv_in_out_count;
    146  section->new_mv_count += frame->new_mv_count;
    147  section->count += frame->count;
    148  section->duration += frame->duration;
    149 }
    150 
    151 static int get_unit_rows(const BLOCK_SIZE fp_block_size, const int mb_rows) {
    152  const int height_mi_log2 = mi_size_high_log2[fp_block_size];
    153  const int mb_height_mi_log2 = mi_size_high_log2[BLOCK_16X16];
    154  if (height_mi_log2 > mb_height_mi_log2) {
    155    return mb_rows >> (height_mi_log2 - mb_height_mi_log2);
    156  }
    157 
    158  return mb_rows << (mb_height_mi_log2 - height_mi_log2);
    159 }
    160 
    161 static int get_unit_cols(const BLOCK_SIZE fp_block_size, const int mb_cols) {
    162  const int width_mi_log2 = mi_size_wide_log2[fp_block_size];
    163  const int mb_width_mi_log2 = mi_size_wide_log2[BLOCK_16X16];
    164  if (width_mi_log2 > mb_width_mi_log2) {
    165    return mb_cols >> (width_mi_log2 - mb_width_mi_log2);
    166  }
    167 
    168  return mb_cols << (mb_width_mi_log2 - width_mi_log2);
    169 }
    170 
    171 // TODO(chengchen): can we simplify it even if resize has to be considered?
    172 static int get_num_mbs(const BLOCK_SIZE fp_block_size,
    173                       const int num_mbs_16X16) {
    174  const int width_mi_log2 = mi_size_wide_log2[fp_block_size];
    175  const int height_mi_log2 = mi_size_high_log2[fp_block_size];
    176  const int mb_width_mi_log2 = mi_size_wide_log2[BLOCK_16X16];
    177  const int mb_height_mi_log2 = mi_size_high_log2[BLOCK_16X16];
    178  // TODO(chengchen): Now this function assumes a square block is used.
    179  // It does not support rectangular block sizes.
    180  assert(width_mi_log2 == height_mi_log2);
    181  if (width_mi_log2 > mb_width_mi_log2) {
    182    return num_mbs_16X16 >> ((width_mi_log2 - mb_width_mi_log2) +
    183                             (height_mi_log2 - mb_height_mi_log2));
    184  }
    185 
    186  return num_mbs_16X16 << ((mb_width_mi_log2 - width_mi_log2) +
    187                           (mb_height_mi_log2 - height_mi_log2));
    188 }
    189 
    190 void av1_end_first_pass(AV1_COMP *cpi) {
    191  if (cpi->ppi->twopass.stats_buf_ctx->total_stats && !cpi->ppi->lap_enabled)
    192    output_stats(cpi->ppi->twopass.stats_buf_ctx->total_stats,
    193                 cpi->ppi->output_pkt_list);
    194 }
    195 
    196 static aom_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
    197  switch (bsize) {
    198    case BLOCK_8X8: return aom_mse8x8;
    199    case BLOCK_16X8: return aom_mse16x8;
    200    case BLOCK_8X16: return aom_mse8x16;
    201    default: return aom_mse16x16;
    202  }
    203 }
    204 
    205 static unsigned int get_prediction_error(BLOCK_SIZE bsize,
    206                                         const struct buf_2d *src,
    207                                         const struct buf_2d *ref) {
    208  unsigned int sse;
    209  const aom_variance_fn_t fn = get_block_variance_fn(bsize);
    210  fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
    211  return sse;
    212 }
    213 
    214 #if CONFIG_AV1_HIGHBITDEPTH
    215 static aom_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize,
    216                                                      int bd) {
    217  switch (bd) {
    218    default:
    219      switch (bsize) {
    220        case BLOCK_8X8: return aom_highbd_8_mse8x8;
    221        case BLOCK_16X8: return aom_highbd_8_mse16x8;
    222        case BLOCK_8X16: return aom_highbd_8_mse8x16;
    223        default: return aom_highbd_8_mse16x16;
    224      }
    225    case 10:
    226      switch (bsize) {
    227        case BLOCK_8X8: return aom_highbd_10_mse8x8;
    228        case BLOCK_16X8: return aom_highbd_10_mse16x8;
    229        case BLOCK_8X16: return aom_highbd_10_mse8x16;
    230        default: return aom_highbd_10_mse16x16;
    231      }
    232    case 12:
    233      switch (bsize) {
    234        case BLOCK_8X8: return aom_highbd_12_mse8x8;
    235        case BLOCK_16X8: return aom_highbd_12_mse16x8;
    236        case BLOCK_8X16: return aom_highbd_12_mse8x16;
    237        default: return aom_highbd_12_mse16x16;
    238      }
    239  }
    240 }
    241 
    242 static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize,
    243                                                const struct buf_2d *src,
    244                                                const struct buf_2d *ref,
    245                                                int bd) {
    246  unsigned int sse;
    247  const aom_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
    248  fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
    249  return sse;
    250 }
    251 #endif  // CONFIG_AV1_HIGHBITDEPTH
    252 
    253 // Refine the motion search range according to the frame dimension
    254 // for first pass test.
    255 static int get_search_range(int width, int height) {
    256  int sr = 0;
    257  const int dim = AOMMIN(width, height);
    258 
    259  while ((dim << sr) < MAX_FULL_PEL_VAL) ++sr;
    260  return sr;
    261 }
    262 
    263 static inline const search_site_config *av1_get_first_pass_search_site_config(
    264    const AV1_COMP *cpi, MACROBLOCK *x, SEARCH_METHODS search_method) {
    265  const int ref_stride = x->e_mbd.plane[0].pre[0].stride;
    266 
    267  // For AVIF applications, even the source frames can have changing resolution,
    268  // so we need to manually check for the strides :(
    269  // AV1_COMP::mv_search_params.search_site_config is a compressor level cache
    270  // that's shared by multiple threads. In most cases where all frames have the
    271  // same resolution, the cache contains the search site config that we need.
    272  const MotionVectorSearchParams *mv_search_params = &cpi->mv_search_params;
    273  if (ref_stride == mv_search_params->search_site_cfg[SS_CFG_FPF]->stride) {
    274    return mv_search_params->search_site_cfg[SS_CFG_FPF];
    275  }
    276 
    277  // If the cache does not contain the correct stride, then we will need to rely
    278  // on the thread level config MACROBLOCK::search_site_cfg_buf. If even the
    279  // thread level config doesn't match, then we need to update it.
    280  search_method = search_method_lookup[search_method];
    281  assert(search_method_lookup[search_method] == search_method &&
    282         "The search_method_lookup table should be idempotent.");
    283  if (ref_stride != x->search_site_cfg_buf[search_method].stride) {
    284    av1_refresh_search_site_config(x->search_site_cfg_buf, search_method,
    285                                   ref_stride);
    286  }
    287 
    288  return x->search_site_cfg_buf;
    289 }
    290 
    291 static inline void first_pass_motion_search(AV1_COMP *cpi, MACROBLOCK *x,
    292                                            const MV *ref_mv,
    293                                            FULLPEL_MV *best_mv,
    294                                            int *best_motion_err) {
    295  AV1_COMMON *const cm = &cpi->common;
    296  MACROBLOCKD *const xd = &x->e_mbd;
    297  FULLPEL_MV start_mv = get_fullmv_from_mv(ref_mv);
    298  int tmp_err;
    299  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
    300  const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
    301  const int sr = get_search_range(cm->width, cm->height);
    302  const int step_param = cpi->sf.fp_sf.reduce_mv_step_param + sr;
    303 
    304  const search_site_config *first_pass_search_sites =
    305      av1_get_first_pass_search_site_config(cpi, x, NSTEP);
    306  const int fine_search_interval =
    307      cpi->is_screen_content_type && cm->features.allow_intrabc;
    308  FULLPEL_MOTION_SEARCH_PARAMS ms_params;
    309  av1_make_default_fullpel_ms_params(&ms_params, cpi, x, bsize, ref_mv,
    310                                     start_mv, first_pass_search_sites, NSTEP,
    311                                     fine_search_interval);
    312 
    313  FULLPEL_MV this_best_mv;
    314  FULLPEL_MV_STATS best_mv_stats;
    315  tmp_err = av1_full_pixel_search(start_mv, &ms_params, step_param, NULL,
    316                                  &this_best_mv, &best_mv_stats, NULL);
    317 
    318  if (tmp_err < INT_MAX) {
    319    aom_variance_fn_ptr_t v_fn_ptr = cpi->ppi->fn_ptr[bsize];
    320    const MSBuffers *ms_buffers = &ms_params.ms_buffers;
    321    tmp_err = av1_get_mvpred_sse(&ms_params.mv_cost_params, this_best_mv,
    322                                 &v_fn_ptr, ms_buffers->src, ms_buffers->ref) +
    323              new_mv_mode_penalty;
    324  }
    325 
    326  if (tmp_err < *best_motion_err) {
    327    *best_motion_err = tmp_err;
    328    *best_mv = this_best_mv;
    329  }
    330 }
    331 
    332 static BLOCK_SIZE get_bsize(const CommonModeInfoParams *const mi_params,
    333                            const BLOCK_SIZE fp_block_size, const int unit_row,
    334                            const int unit_col) {
    335  const int unit_width = mi_size_wide[fp_block_size];
    336  const int unit_height = mi_size_high[fp_block_size];
    337  const int is_half_width =
    338      unit_width * unit_col + unit_width / 2 >= mi_params->mi_cols;
    339  const int is_half_height =
    340      unit_height * unit_row + unit_height / 2 >= mi_params->mi_rows;
    341  const int max_dimension =
    342      AOMMAX(block_size_wide[fp_block_size], block_size_high[fp_block_size]);
    343  int square_block_size = 0;
    344  // 4X4, 8X8, 16X16, 32X32, 64X64, 128X128
    345  switch (max_dimension) {
    346    case 4: square_block_size = 0; break;
    347    case 8: square_block_size = 1; break;
    348    case 16: square_block_size = 2; break;
    349    case 32: square_block_size = 3; break;
    350    case 64: square_block_size = 4; break;
    351    case 128: square_block_size = 5; break;
    352    default: assert(0 && "First pass block size is not supported!"); break;
    353  }
    354  if (is_half_width && is_half_height) {
    355    return subsize_lookup[PARTITION_SPLIT][square_block_size];
    356  } else if (is_half_width) {
    357    return subsize_lookup[PARTITION_VERT][square_block_size];
    358  } else if (is_half_height) {
    359    return subsize_lookup[PARTITION_HORZ][square_block_size];
    360  } else {
    361    return fp_block_size;
    362  }
    363 }
    364 
    365 static int find_fp_qindex(aom_bit_depth_t bit_depth) {
    366  return av1_find_qindex(FIRST_PASS_Q, bit_depth, 0, QINDEX_RANGE - 1);
    367 }
    368 
    369 static double raw_motion_error_stdev(int *raw_motion_err_list,
    370                                     int raw_motion_err_counts) {
    371  int64_t sum_raw_err = 0;
    372  double raw_err_avg = 0;
    373  double raw_err_stdev = 0;
    374  if (raw_motion_err_counts == 0) return 0;
    375 
    376  int i;
    377  for (i = 0; i < raw_motion_err_counts; i++) {
    378    sum_raw_err += raw_motion_err_list[i];
    379  }
    380  raw_err_avg = (double)sum_raw_err / raw_motion_err_counts;
    381  for (i = 0; i < raw_motion_err_counts; i++) {
    382    raw_err_stdev += (raw_motion_err_list[i] - raw_err_avg) *
    383                     (raw_motion_err_list[i] - raw_err_avg);
    384  }
    385  // Calculate the standard deviation for the motion error of all the inter
    386  // blocks of the 0,0 motion using the last source
    387  // frame as the reference.
    388  raw_err_stdev = sqrt(raw_err_stdev / raw_motion_err_counts);
    389  return raw_err_stdev;
    390 }
    391 
    392 static inline int calc_wavelet_energy(const AV1EncoderConfig *oxcf) {
    393  return oxcf->q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL;
    394 }
    395 typedef struct intra_pred_block_pass1_args {
    396  const SequenceHeader *seq_params;
    397  MACROBLOCK *x;
    398 } intra_pred_block_pass1_args;
    399 
    400 static inline void copy_rect(uint8_t *dst, int dstride, const uint8_t *src,
    401                             int sstride, int width, int height, int use_hbd) {
    402 #if CONFIG_AV1_HIGHBITDEPTH
    403  if (use_hbd) {
    404    aom_highbd_convolve_copy(CONVERT_TO_SHORTPTR(src), sstride,
    405                             CONVERT_TO_SHORTPTR(dst), dstride, width, height);
    406  } else {
    407    aom_convolve_copy(src, sstride, dst, dstride, width, height);
    408  }
    409 #else
    410  (void)use_hbd;
    411  aom_convolve_copy(src, sstride, dst, dstride, width, height);
    412 #endif
    413 }
    414 
    415 static void first_pass_intra_pred_and_calc_diff(int plane, int block,
    416                                                int blk_row, int blk_col,
    417                                                BLOCK_SIZE plane_bsize,
    418                                                TX_SIZE tx_size, void *arg) {
    419  (void)block;
    420  struct intra_pred_block_pass1_args *const args = arg;
    421  MACROBLOCK *const x = args->x;
    422  MACROBLOCKD *const xd = &x->e_mbd;
    423  MACROBLOCKD_PLANE *const pd = &xd->plane[plane];
    424  MACROBLOCK_PLANE *const p = &x->plane[plane];
    425  const int dst_stride = pd->dst.stride;
    426  uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
    427  const MB_MODE_INFO *const mbmi = xd->mi[0];
    428  const SequenceHeader *seq_params = args->seq_params;
    429  const int src_stride = p->src.stride;
    430  uint8_t *src = &p->src.buf[(blk_row * src_stride + blk_col) << MI_SIZE_LOG2];
    431 
    432  av1_predict_intra_block(
    433      xd, seq_params->sb_size, seq_params->enable_intra_edge_filter, pd->width,
    434      pd->height, tx_size, mbmi->mode, 0, 0, FILTER_INTRA_MODES, src,
    435      src_stride, dst, dst_stride, blk_col, blk_row, plane);
    436 
    437  av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
    438 }
    439 
    440 static void first_pass_predict_intra_block_for_luma_plane(
    441    const SequenceHeader *seq_params, MACROBLOCK *x, BLOCK_SIZE bsize) {
    442  assert(bsize < BLOCK_SIZES_ALL);
    443  const MACROBLOCKD *const xd = &x->e_mbd;
    444  const int plane = AOM_PLANE_Y;
    445  const MACROBLOCKD_PLANE *const pd = &xd->plane[plane];
    446  const int ss_x = pd->subsampling_x;
    447  const int ss_y = pd->subsampling_y;
    448  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
    449  const int dst_stride = pd->dst.stride;
    450  uint8_t *dst = pd->dst.buf;
    451  const MACROBLOCK_PLANE *const p = &x->plane[plane];
    452  const int src_stride = p->src.stride;
    453  const uint8_t *src = p->src.buf;
    454 
    455  intra_pred_block_pass1_args args = { seq_params, x };
    456  av1_foreach_transformed_block_in_plane(
    457      xd, plane_bsize, plane, first_pass_intra_pred_and_calc_diff, &args);
    458 
    459  // copy source data to recon buffer, as the recon buffer will be used as a
    460  // reference frame subsequently.
    461  copy_rect(dst, dst_stride, src, src_stride, block_size_wide[bsize],
    462            block_size_high[bsize], seq_params->use_highbitdepth);
    463 }
    464 
    465 #define UL_INTRA_THRESH 50
    466 #define INVALID_ROW -1
    467 // Computes and returns the intra pred error of a block.
    468 // intra pred error: sum of squared error of the intra predicted residual.
    469 // Inputs:
    470 //   cpi: the encoder setting. Only a few params in it will be used.
    471 //   this_frame: the current frame buffer.
    472 //   tile: tile information (not used in first pass, already init to zero)
    473 //   unit_row: row index in the unit of first pass block size.
    474 //   unit_col: column index in the unit of first pass block size.
    475 //   y_offset: the offset of y frame buffer, indicating the starting point of
    476 //             the current block.
    477 //   uv_offset: the offset of u and v frame buffer, indicating the starting
    478 //              point of the current block.
    479 //   fp_block_size: first pass block size.
    480 //   qindex: quantization step size to encode the frame.
    481 //   stats: frame encoding stats.
    482 // Modifies:
    483 //   stats->intra_skip_count
    484 //   stats->image_data_start_row
    485 //   stats->intra_factor
    486 //   stats->brightness_factor
    487 //   stats->intra_error
    488 //   stats->frame_avg_wavelet_energy
    489 // Returns:
    490 //   this_intra_error.
    491 static int firstpass_intra_prediction(
    492    AV1_COMP *cpi, ThreadData *td, YV12_BUFFER_CONFIG *const this_frame,
    493    const TileInfo *const tile, const int unit_row, const int unit_col,
    494    const int y_offset, const int uv_offset, const BLOCK_SIZE fp_block_size,
    495    const int qindex, FRAME_STATS *const stats) {
    496  const AV1_COMMON *const cm = &cpi->common;
    497  const CommonModeInfoParams *const mi_params = &cm->mi_params;
    498  const SequenceHeader *const seq_params = cm->seq_params;
    499  MACROBLOCK *const x = &td->mb;
    500  MACROBLOCKD *const xd = &x->e_mbd;
    501  const int unit_scale = mi_size_wide[fp_block_size];
    502  const int num_planes = av1_num_planes(cm);
    503  const BLOCK_SIZE bsize =
    504      get_bsize(mi_params, fp_block_size, unit_row, unit_col);
    505 
    506  set_mi_offsets(mi_params, xd, unit_row * unit_scale, unit_col * unit_scale);
    507  xd->plane[0].dst.buf = this_frame->y_buffer + y_offset;
    508  if (num_planes > 1) {
    509    xd->plane[1].dst.buf = this_frame->u_buffer + uv_offset;
    510    xd->plane[2].dst.buf = this_frame->v_buffer + uv_offset;
    511  }
    512  xd->left_available = (unit_col != 0);
    513  xd->mi[0]->bsize = bsize;
    514  xd->mi[0]->ref_frame[0] = INTRA_FRAME;
    515  set_mi_row_col(xd, tile, unit_row * unit_scale, mi_size_high[bsize],
    516                 unit_col * unit_scale, mi_size_wide[bsize], mi_params->mi_rows,
    517                 mi_params->mi_cols);
    518  set_plane_n4(xd, mi_size_wide[bsize], mi_size_high[bsize], num_planes);
    519  xd->mi[0]->segment_id = 0;
    520  xd->lossless[xd->mi[0]->segment_id] = (qindex == 0);
    521  xd->mi[0]->mode = DC_PRED;
    522  xd->mi[0]->tx_size = TX_4X4;
    523 
    524  if (cpi->sf.fp_sf.disable_recon)
    525    first_pass_predict_intra_block_for_luma_plane(seq_params, x, bsize);
    526  else
    527    av1_encode_intra_block_plane(cpi, x, bsize, 0, DRY_RUN_NORMAL, 0);
    528  int this_intra_error = aom_get_mb_ss(x->plane[0].src_diff);
    529  if (seq_params->use_highbitdepth) {
    530    switch (seq_params->bit_depth) {
    531      case AOM_BITS_8: break;
    532      case AOM_BITS_10: this_intra_error >>= 4; break;
    533      case AOM_BITS_12: this_intra_error >>= 8; break;
    534      default:
    535        assert(0 &&
    536               "seq_params->bit_depth should be AOM_BITS_8, "
    537               "AOM_BITS_10 or AOM_BITS_12");
    538        return -1;
    539    }
    540  }
    541 
    542  if (this_intra_error < UL_INTRA_THRESH) {
    543    ++stats->intra_skip_count;
    544  } else if ((unit_col > 0) && (stats->image_data_start_row == INVALID_ROW)) {
    545    stats->image_data_start_row = unit_row;
    546  }
    547 
    548  double log_intra = log1p(this_intra_error);
    549  if (log_intra < 10.0) {
    550    stats->intra_factor += 1.0 + ((10.0 - log_intra) * 0.05);
    551  } else {
    552    stats->intra_factor += 1.0;
    553  }
    554 
    555  int level_sample;
    556  if (seq_params->use_highbitdepth) {
    557    level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0];
    558  } else {
    559    level_sample = x->plane[0].src.buf[0];
    560  }
    561 
    562  if (seq_params->use_highbitdepth) {
    563    switch (seq_params->bit_depth) {
    564      case AOM_BITS_8: break;
    565      case AOM_BITS_10: level_sample >>= 2; break;
    566      case AOM_BITS_12: level_sample >>= 4; break;
    567      default:
    568        assert(0 &&
    569               "seq_params->bit_depth should be AOM_BITS_8, "
    570               "AOM_BITS_10 or AOM_BITS_12");
    571        return -1;
    572    }
    573  }
    574  if ((level_sample < DARK_THRESH) && (log_intra < 9.0)) {
    575    stats->brightness_factor += 1.0 + (0.01 * (DARK_THRESH - level_sample));
    576  } else {
    577    stats->brightness_factor += 1.0;
    578  }
    579 
    580  // Intrapenalty below deals with situations where the intra and inter
    581  // error scores are very low (e.g. a plain black frame).
    582  // We do not have special cases in first pass for 0,0 and nearest etc so
    583  // all inter modes carry an overhead cost estimate for the mv.
    584  // When the error score is very low this causes us to pick all or lots of
    585  // INTRA modes and throw lots of key frames.
    586  // This penalty adds a cost matching that of a 0,0 mv to the intra case.
    587  this_intra_error += INTRA_MODE_PENALTY;
    588 
    589  // Accumulate the intra error.
    590  stats->intra_error += (int64_t)this_intra_error;
    591 
    592  // Stats based on wavelet energy is used in the following cases :
    593  // 1. ML model which predicts if a flat structure (golden-frame only structure
    594  // without ALT-REF and Internal-ARFs) is better. This ML model is enabled in
    595  // constant quality mode under certain conditions.
    596  // 2. Delta qindex mode is set as DELTA_Q_PERCEPTUAL.
    597  // Thus, wavelet energy calculation is enabled for the above cases.
    598  if (calc_wavelet_energy(&cpi->oxcf)) {
    599    const int hbd = is_cur_buf_hbd(xd);
    600    const int stride = x->plane[0].src.stride;
    601    const int num_8x8_rows = block_size_high[fp_block_size] / 8;
    602    const int num_8x8_cols = block_size_wide[fp_block_size] / 8;
    603    const uint8_t *buf = x->plane[0].src.buf;
    604    stats->frame_avg_wavelet_energy += av1_haar_ac_sad_mxn_uint8_input(
    605        buf, stride, hbd, num_8x8_rows, num_8x8_cols);
    606  } else {
    607    stats->frame_avg_wavelet_energy = INVALID_FP_STATS_TO_PREDICT_FLAT_GOP;
    608  }
    609 
    610  return this_intra_error;
    611 }
    612 
    613 // Returns the sum of square error between source and reference blocks.
    614 static int get_prediction_error_bitdepth(const int is_high_bitdepth,
    615                                         const int bitdepth,
    616                                         const BLOCK_SIZE block_size,
    617                                         const struct buf_2d *src,
    618                                         const struct buf_2d *ref) {
    619  (void)is_high_bitdepth;
    620  (void)bitdepth;
    621 #if CONFIG_AV1_HIGHBITDEPTH
    622  if (is_high_bitdepth) {
    623    return highbd_get_prediction_error(block_size, src, ref, bitdepth);
    624  }
    625 #endif  // CONFIG_AV1_HIGHBITDEPTH
    626  return get_prediction_error(block_size, src, ref);
    627 }
    628 
    629 // Accumulates motion vector stats.
    630 // Modifies member variables of "stats".
    631 static void accumulate_mv_stats(const MV best_mv, const FULLPEL_MV mv,
    632                                const int mb_row, const int mb_col,
    633                                const int mb_rows, const int mb_cols,
    634                                MV *last_non_zero_mv, FRAME_STATS *stats) {
    635  if (is_zero_mv(&best_mv)) return;
    636 
    637  ++stats->mv_count;
    638  // Non-zero vector, was it different from the last non zero vector?
    639  if (!is_equal_mv(&best_mv, last_non_zero_mv)) ++stats->new_mv_count;
    640  *last_non_zero_mv = best_mv;
    641 
    642  // Does the row vector point inwards or outwards?
    643  if (mb_row < mb_rows / 2) {
    644    if (mv.row > 0) {
    645      --stats->sum_in_vectors;
    646    } else if (mv.row < 0) {
    647      ++stats->sum_in_vectors;
    648    }
    649  } else if (mb_row > mb_rows / 2) {
    650    if (mv.row > 0) {
    651      ++stats->sum_in_vectors;
    652    } else if (mv.row < 0) {
    653      --stats->sum_in_vectors;
    654    }
    655  }
    656 
    657  // Does the col vector point inwards or outwards?
    658  if (mb_col < mb_cols / 2) {
    659    if (mv.col > 0) {
    660      --stats->sum_in_vectors;
    661    } else if (mv.col < 0) {
    662      ++stats->sum_in_vectors;
    663    }
    664  } else if (mb_col > mb_cols / 2) {
    665    if (mv.col > 0) {
    666      ++stats->sum_in_vectors;
    667    } else if (mv.col < 0) {
    668      --stats->sum_in_vectors;
    669    }
    670  }
    671 }
    672 
    673 // Computes and returns the inter prediction error from the last frame.
    674 // Computes inter prediction errors from the golden and alt ref frams and
    675 // Updates stats accordingly.
    676 // Inputs:
    677 //   cpi: the encoder setting. Only a few params in it will be used.
    678 //   last_frame: the frame buffer of the last frame.
    679 //   golden_frame: the frame buffer of the golden frame.
    680 //   unit_row: row index in the unit of first pass block size.
    681 //   unit_col: column index in the unit of first pass block size.
    682 //   recon_yoffset: the y offset of the reconstructed  frame buffer,
    683 //                  indicating the starting point of the current block.
    684 //   recont_uvoffset: the u/v offset of the reconstructed frame buffer,
    685 //                    indicating the starting point of the current block.
    686 //   src_yoffset: the y offset of the source frame buffer.
    687 //   fp_block_size: first pass block size.
    688 //   this_intra_error: the intra prediction error of this block.
    689 //   raw_motion_err_counts: the count of raw motion vectors.
    690 //   raw_motion_err_list: the array that records the raw motion error.
    691 //   ref_mv: the reference used to start the motion search
    692 //   best_mv: the best mv found
    693 //   last_non_zero_mv: the last non zero mv found in this tile row.
    694 //   stats: frame encoding stats.
    695 //  Modifies:
    696 //    raw_motion_err_list
    697 //    best_ref_mv
    698 //    last_mv
    699 //    stats: many member params in it.
    700 //  Returns:
    701 //    this_inter_error
    702 static int firstpass_inter_prediction(
    703    AV1_COMP *cpi, ThreadData *td, const YV12_BUFFER_CONFIG *const last_frame,
    704    const YV12_BUFFER_CONFIG *const golden_frame, const int unit_row,
    705    const int unit_col, const int recon_yoffset, const int recon_uvoffset,
    706    const int src_yoffset, const BLOCK_SIZE fp_block_size,
    707    const int this_intra_error, const int raw_motion_err_counts,
    708    int *raw_motion_err_list, const MV ref_mv, MV *best_mv,
    709    MV *last_non_zero_mv, FRAME_STATS *stats) {
    710  int this_inter_error = this_intra_error;
    711  AV1_COMMON *const cm = &cpi->common;
    712  const CommonModeInfoParams *const mi_params = &cm->mi_params;
    713  CurrentFrame *const current_frame = &cm->current_frame;
    714  MACROBLOCK *const x = &td->mb;
    715  MACROBLOCKD *const xd = &x->e_mbd;
    716  const int is_high_bitdepth = is_cur_buf_hbd(xd);
    717  const int bitdepth = xd->bd;
    718  const int unit_scale = mi_size_wide[fp_block_size];
    719  const BLOCK_SIZE bsize =
    720      get_bsize(mi_params, fp_block_size, unit_row, unit_col);
    721  const int fp_block_size_height = block_size_wide[fp_block_size];
    722  const int unit_width = mi_size_wide[fp_block_size];
    723  const int unit_rows = get_unit_rows(fp_block_size, mi_params->mb_rows);
    724  const int unit_cols = get_unit_cols(fp_block_size, mi_params->mb_cols);
    725  // Assume 0,0 motion with no mv overhead.
    726  FULLPEL_MV mv = kZeroFullMv;
    727  xd->plane[0].pre[0].buf = last_frame->y_buffer + recon_yoffset;
    728  // Set up limit values for motion vectors to prevent them extending
    729  // outside the UMV borders.
    730  av1_set_mv_col_limits(mi_params, &x->mv_limits, unit_col * unit_width,
    731                        fp_block_size_height >> MI_SIZE_LOG2,
    732                        cpi->oxcf.border_in_pixels);
    733 
    734  int motion_error =
    735      get_prediction_error_bitdepth(is_high_bitdepth, bitdepth, bsize,
    736                                    &x->plane[0].src, &xd->plane[0].pre[0]);
    737 
    738  // Compute the motion error of the 0,0 motion using the last source
    739  // frame as the reference. Skip the further motion search on
    740  // reconstructed frame if this error is small.
    741  // TODO(chiyotsai): The unscaled last source might be different dimension
    742  // as the current source. See BUG=aomedia:3413
    743  struct buf_2d unscaled_last_source_buf_2d;
    744  unscaled_last_source_buf_2d.buf =
    745      cpi->unscaled_last_source->y_buffer + src_yoffset;
    746  unscaled_last_source_buf_2d.stride = cpi->unscaled_last_source->y_stride;
    747  const int raw_motion_error = get_prediction_error_bitdepth(
    748      is_high_bitdepth, bitdepth, bsize, &x->plane[0].src,
    749      &unscaled_last_source_buf_2d);
    750  raw_motion_err_list[raw_motion_err_counts] = raw_motion_error;
    751  const FIRST_PASS_SPEED_FEATURES *const fp_sf = &cpi->sf.fp_sf;
    752 
    753  if (raw_motion_error > fp_sf->skip_motion_search_threshold) {
    754    // Test last reference frame using the previous best mv as the
    755    // starting point (best reference) for the search.
    756    first_pass_motion_search(cpi, x, &ref_mv, &mv, &motion_error);
    757 
    758    // If the current best reference mv is not centered on 0,0 then do a
    759    // 0,0 based search as well.
    760    if ((fp_sf->skip_zeromv_motion_search == 0) && !is_zero_mv(&ref_mv)) {
    761      FULLPEL_MV tmp_mv = kZeroFullMv;
    762      int tmp_err = INT_MAX;
    763      first_pass_motion_search(cpi, x, &kZeroMv, &tmp_mv, &tmp_err);
    764 
    765      if (tmp_err < motion_error) {
    766        motion_error = tmp_err;
    767        mv = tmp_mv;
    768      }
    769    }
    770  }
    771 
    772  // Motion search in 2nd reference frame.
    773  int gf_motion_error = motion_error;
    774  if ((current_frame->frame_number > 1) && golden_frame != NULL) {
    775    FULLPEL_MV tmp_mv = kZeroFullMv;
    776    // Assume 0,0 motion with no mv overhead.
    777    av1_setup_pre_planes(xd, 0, golden_frame, 0, 0, NULL, 1);
    778    xd->plane[0].pre[0].buf += recon_yoffset;
    779    gf_motion_error =
    780        get_prediction_error_bitdepth(is_high_bitdepth, bitdepth, bsize,
    781                                      &x->plane[0].src, &xd->plane[0].pre[0]);
    782    first_pass_motion_search(cpi, x, &kZeroMv, &tmp_mv, &gf_motion_error);
    783  }
    784  if (gf_motion_error < motion_error && gf_motion_error < this_intra_error) {
    785    ++stats->second_ref_count;
    786  }
    787  // In accumulating a score for the 2nd reference frame take the
    788  // best of the motion predicted score and the intra coded error
    789  // (just as will be done for) accumulation of "coded_error" for
    790  // the last frame.
    791  if ((current_frame->frame_number > 1) && golden_frame != NULL) {
    792    stats->sr_coded_error += AOMMIN(gf_motion_error, this_intra_error);
    793  } else {
    794    // TODO(chengchen): I believe logically this should also be changed to
    795    // stats->sr_coded_error += AOMMIN(gf_motion_error, this_intra_error).
    796    stats->sr_coded_error += motion_error;
    797  }
    798 
    799  // Reset to last frame as reference buffer.
    800  xd->plane[0].pre[0].buf = last_frame->y_buffer + recon_yoffset;
    801  if (av1_num_planes(&cpi->common) > 1) {
    802    xd->plane[1].pre[0].buf = last_frame->u_buffer + recon_uvoffset;
    803    xd->plane[2].pre[0].buf = last_frame->v_buffer + recon_uvoffset;
    804  }
    805 
    806  // Start by assuming that intra mode is best.
    807  *best_mv = kZeroMv;
    808 
    809  if (motion_error <= this_intra_error) {
    810    // Keep a count of cases where the inter and intra were very close
    811    // and very low. This helps with scene cut detection for example in
    812    // cropped clips with black bars at the sides or top and bottom.
    813    if (((this_intra_error - INTRA_MODE_PENALTY) * 9 <= motion_error * 10) &&
    814        (this_intra_error < (2 * INTRA_MODE_PENALTY))) {
    815      stats->neutral_count += 1.0;
    816      // Also track cases where the intra is not much worse than the inter
    817      // and use this in limiting the GF/arf group length.
    818    } else if ((this_intra_error > NCOUNT_INTRA_THRESH) &&
    819               (this_intra_error < (NCOUNT_INTRA_FACTOR * motion_error))) {
    820      stats->neutral_count +=
    821          (double)motion_error / DOUBLE_DIVIDE_CHECK((double)this_intra_error);
    822    }
    823 
    824    *best_mv = get_mv_from_fullmv(&mv);
    825    this_inter_error = motion_error;
    826    xd->mi[0]->mode = NEWMV;
    827    xd->mi[0]->mv[0].as_mv = *best_mv;
    828    xd->mi[0]->tx_size = TX_4X4;
    829    xd->mi[0]->ref_frame[0] = LAST_FRAME;
    830    xd->mi[0]->ref_frame[1] = NONE_FRAME;
    831 
    832    if (fp_sf->disable_recon == 0) {
    833      av1_enc_build_inter_predictor(cm, xd, unit_row * unit_scale,
    834                                    unit_col * unit_scale, NULL, bsize,
    835                                    AOM_PLANE_Y, AOM_PLANE_Y);
    836      av1_encode_sby_pass1(cpi, x, bsize);
    837    }
    838    stats->sum_mvr += best_mv->row;
    839    stats->sum_mvr_abs += abs(best_mv->row);
    840    stats->sum_mvc += best_mv->col;
    841    stats->sum_mvc_abs += abs(best_mv->col);
    842    stats->sum_mvrs += best_mv->row * best_mv->row;
    843    stats->sum_mvcs += best_mv->col * best_mv->col;
    844    ++stats->inter_count;
    845 
    846    accumulate_mv_stats(*best_mv, mv, unit_row, unit_col, unit_rows, unit_cols,
    847                        last_non_zero_mv, stats);
    848  }
    849 
    850  return this_inter_error;
    851 }
    852 
    853 // Normalize the first pass stats.
    854 // Error / counters are normalized to each MB.
    855 // MVs are normalized to the width/height of the frame.
    856 static void normalize_firstpass_stats(FIRSTPASS_STATS *fps,
    857                                      double num_mbs_16x16, double f_w,
    858                                      double f_h) {
    859  fps->coded_error /= num_mbs_16x16;
    860  fps->sr_coded_error /= num_mbs_16x16;
    861  fps->intra_error /= num_mbs_16x16;
    862  fps->frame_avg_wavelet_energy /= num_mbs_16x16;
    863  fps->log_coded_error = log1p(fps->coded_error);
    864  fps->log_intra_error = log1p(fps->intra_error);
    865  fps->MVr /= f_h;
    866  fps->mvr_abs /= f_h;
    867  fps->MVc /= f_w;
    868  fps->mvc_abs /= f_w;
    869  fps->MVrv /= (f_h * f_h);
    870  fps->MVcv /= (f_w * f_w);
    871  fps->new_mv_count /= num_mbs_16x16;
    872 }
    873 
    874 // Updates the first pass stats of this frame.
    875 // Input:
    876 //   cpi: the encoder setting. Only a few params in it will be used.
    877 //   stats: stats accumulated for this frame.
    878 //   raw_err_stdev: the statndard deviation for the motion error of all the
    879 //                  inter blocks of the (0,0) motion using the last source
    880 //                  frame as the reference.
    881 //   frame_number: current frame number.
    882 //   ts_duration: Duration of the frame / collection of frames.
    883 // Updates:
    884 //   twopass->total_stats: the accumulated stats.
    885 //   twopass->stats_buf_ctx->stats_in_end: the pointer to the current stats,
    886 //                                         update its value and its position
    887 //                                         in the buffer.
    888 static void update_firstpass_stats(AV1_COMP *cpi,
    889                                   const FRAME_STATS *const stats,
    890                                   const double raw_err_stdev,
    891                                   const int frame_number,
    892                                   const int64_t ts_duration,
    893                                   const BLOCK_SIZE fp_block_size) {
    894  TWO_PASS *twopass = &cpi->ppi->twopass;
    895  AV1_COMMON *const cm = &cpi->common;
    896  const CommonModeInfoParams *const mi_params = &cm->mi_params;
    897  FIRSTPASS_STATS *this_frame_stats = twopass->stats_buf_ctx->stats_in_end;
    898  FIRSTPASS_STATS fps;
    899  // The minimum error here insures some bit allocation to frames even
    900  // in static regions. The allocation per MB declines for larger formats
    901  // where the typical "real" energy per MB also falls.
    902  // Initial estimate here uses sqrt(mbs) to define the min_err, where the
    903  // number of mbs is proportional to the image area.
    904  const int num_mbs_16X16 = (cpi->oxcf.resize_cfg.resize_mode != RESIZE_NONE)
    905                                ? cpi->initial_mbs
    906                                : mi_params->MBs;
    907  // Number of actual units used in the first pass, it can be other square
    908  // block sizes than 16X16.
    909  const int num_mbs = get_num_mbs(fp_block_size, num_mbs_16X16);
    910  const double min_err = 200 * sqrt(num_mbs);
    911 
    912  fps.weight = stats->intra_factor * stats->brightness_factor;
    913  fps.frame = frame_number;
    914  fps.coded_error = (double)(stats->coded_error >> 8) + min_err;
    915  fps.sr_coded_error = (double)(stats->sr_coded_error >> 8) + min_err;
    916  fps.intra_error = (double)(stats->intra_error >> 8) + min_err;
    917  fps.frame_avg_wavelet_energy = (double)stats->frame_avg_wavelet_energy;
    918  fps.count = 1.0;
    919  fps.pcnt_inter = (double)stats->inter_count / num_mbs;
    920  fps.pcnt_second_ref = (double)stats->second_ref_count / num_mbs;
    921  fps.pcnt_neutral = (double)stats->neutral_count / num_mbs;
    922  fps.intra_skip_pct = (double)stats->intra_skip_count / num_mbs;
    923  fps.inactive_zone_rows = (double)stats->image_data_start_row;
    924  fps.inactive_zone_cols = 0.0;  // Placeholder: not currently supported.
    925  fps.raw_error_stdev = raw_err_stdev;
    926  fps.is_flash = 0;
    927  fps.noise_var = 0.0;
    928  fps.cor_coeff = 1.0;
    929  fps.log_coded_error = 0.0;
    930  fps.log_intra_error = 0.0;
    931 
    932  if (stats->mv_count > 0) {
    933    fps.MVr = (double)stats->sum_mvr / stats->mv_count;
    934    fps.mvr_abs = (double)stats->sum_mvr_abs / stats->mv_count;
    935    fps.MVc = (double)stats->sum_mvc / stats->mv_count;
    936    fps.mvc_abs = (double)stats->sum_mvc_abs / stats->mv_count;
    937    fps.MVrv = ((double)stats->sum_mvrs -
    938                ((double)stats->sum_mvr * stats->sum_mvr / stats->mv_count)) /
    939               stats->mv_count;
    940    fps.MVcv = ((double)stats->sum_mvcs -
    941                ((double)stats->sum_mvc * stats->sum_mvc / stats->mv_count)) /
    942               stats->mv_count;
    943    fps.mv_in_out_count = (double)stats->sum_in_vectors / (stats->mv_count * 2);
    944    fps.new_mv_count = stats->new_mv_count;
    945    fps.pcnt_motion = (double)stats->mv_count / num_mbs;
    946  } else {
    947    fps.MVr = 0.0;
    948    fps.mvr_abs = 0.0;
    949    fps.MVc = 0.0;
    950    fps.mvc_abs = 0.0;
    951    fps.MVrv = 0.0;
    952    fps.MVcv = 0.0;
    953    fps.mv_in_out_count = 0.0;
    954    fps.new_mv_count = 0.0;
    955    fps.pcnt_motion = 0.0;
    956  }
    957 
    958  // TODO(paulwilkins):  Handle the case when duration is set to 0, or
    959  // something less than the full time between subsequent values of
    960  // cpi->source_time_stamp.
    961  fps.duration = (double)ts_duration;
    962 
    963  normalize_firstpass_stats(&fps, num_mbs_16X16, cm->width, cm->height);
    964 
    965  // We will store the stats inside the persistent twopass struct (and NOT the
    966  // local variable 'fps'), and then cpi->output_pkt_list will point to it.
    967  *this_frame_stats = fps;
    968  if (!cpi->ppi->lap_enabled) {
    969    output_stats(this_frame_stats, cpi->ppi->output_pkt_list);
    970  } else {
    971    av1_firstpass_info_push(&twopass->firstpass_info, this_frame_stats);
    972  }
    973  if (cpi->ppi->twopass.stats_buf_ctx->total_stats != NULL) {
    974    av1_accumulate_stats(cpi->ppi->twopass.stats_buf_ctx->total_stats, &fps);
    975  }
    976  twopass->stats_buf_ctx->stats_in_end++;
    977  // When ducky encode is on, we always use linear buffer for stats_buf_ctx.
    978  if (cpi->use_ducky_encode == 0) {
    979    // TODO(angiebird): Figure out why first pass uses circular buffer.
    980    /* In the case of two pass, first pass uses it as a circular buffer,
    981     * when LAP is enabled it is used as a linear buffer*/
    982    if ((cpi->oxcf.pass == AOM_RC_FIRST_PASS) &&
    983        (twopass->stats_buf_ctx->stats_in_end >=
    984         twopass->stats_buf_ctx->stats_in_buf_end)) {
    985      twopass->stats_buf_ctx->stats_in_end =
    986          twopass->stats_buf_ctx->stats_in_start;
    987    }
    988  }
    989 }
    990 
    991 static void print_reconstruction_frame(
    992    const YV12_BUFFER_CONFIG *const last_frame, int frame_number,
    993    int do_print) {
    994  if (!do_print) return;
    995 
    996  char filename[512];
    997  FILE *recon_file;
    998  snprintf(filename, sizeof(filename), "enc%04d.yuv", frame_number);
    999 
   1000  if (frame_number == 0) {
   1001    recon_file = fopen(filename, "wb");
   1002  } else {
   1003    recon_file = fopen(filename, "ab");
   1004  }
   1005 
   1006  fwrite(last_frame->buffer_alloc, last_frame->frame_size, 1, recon_file);
   1007  fclose(recon_file);
   1008 }
   1009 
   1010 static FRAME_STATS accumulate_frame_stats(FRAME_STATS *mb_stats, int mb_rows,
   1011                                          int mb_cols) {
   1012  FRAME_STATS stats = { 0 };
   1013  int i, j;
   1014 
   1015  stats.image_data_start_row = INVALID_ROW;
   1016  for (j = 0; j < mb_rows; j++) {
   1017    for (i = 0; i < mb_cols; i++) {
   1018      FRAME_STATS mb_stat = mb_stats[j * mb_cols + i];
   1019      stats.brightness_factor += mb_stat.brightness_factor;
   1020      stats.coded_error += mb_stat.coded_error;
   1021      stats.frame_avg_wavelet_energy += mb_stat.frame_avg_wavelet_energy;
   1022      if (stats.image_data_start_row == INVALID_ROW &&
   1023          mb_stat.image_data_start_row != INVALID_ROW) {
   1024        stats.image_data_start_row = mb_stat.image_data_start_row;
   1025      }
   1026      stats.inter_count += mb_stat.inter_count;
   1027      stats.intra_error += mb_stat.intra_error;
   1028      stats.intra_factor += mb_stat.intra_factor;
   1029      stats.intra_skip_count += mb_stat.intra_skip_count;
   1030      stats.mv_count += mb_stat.mv_count;
   1031      stats.neutral_count += mb_stat.neutral_count;
   1032      stats.new_mv_count += mb_stat.new_mv_count;
   1033      stats.second_ref_count += mb_stat.second_ref_count;
   1034      stats.sr_coded_error += mb_stat.sr_coded_error;
   1035      stats.sum_in_vectors += mb_stat.sum_in_vectors;
   1036      stats.sum_mvc += mb_stat.sum_mvc;
   1037      stats.sum_mvc_abs += mb_stat.sum_mvc_abs;
   1038      stats.sum_mvcs += mb_stat.sum_mvcs;
   1039      stats.sum_mvr += mb_stat.sum_mvr;
   1040      stats.sum_mvr_abs += mb_stat.sum_mvr_abs;
   1041      stats.sum_mvrs += mb_stat.sum_mvrs;
   1042    }
   1043  }
   1044  return stats;
   1045 }
   1046 
   1047 static void setup_firstpass_data(AV1_COMMON *const cm,
   1048                                 FirstPassData *firstpass_data,
   1049                                 const int unit_rows, const int unit_cols) {
   1050  CHECK_MEM_ERROR(cm, firstpass_data->raw_motion_err_list,
   1051                  aom_calloc(unit_rows * unit_cols,
   1052                             sizeof(*firstpass_data->raw_motion_err_list)));
   1053  CHECK_MEM_ERROR(
   1054      cm, firstpass_data->mb_stats,
   1055      aom_calloc(unit_rows * unit_cols, sizeof(*firstpass_data->mb_stats)));
   1056  for (int j = 0; j < unit_rows; j++) {
   1057    for (int i = 0; i < unit_cols; i++) {
   1058      firstpass_data->mb_stats[j * unit_cols + i].image_data_start_row =
   1059          INVALID_ROW;
   1060    }
   1061  }
   1062 }
   1063 
   1064 void av1_free_firstpass_data(FirstPassData *firstpass_data) {
   1065  aom_free(firstpass_data->raw_motion_err_list);
   1066  firstpass_data->raw_motion_err_list = NULL;
   1067  aom_free(firstpass_data->mb_stats);
   1068  firstpass_data->mb_stats = NULL;
   1069 }
   1070 
   1071 int av1_get_unit_rows_in_tile(const TileInfo *tile,
   1072                              const BLOCK_SIZE fp_block_size) {
   1073  const int unit_height_log2 = mi_size_high_log2[fp_block_size];
   1074  const int mi_rows = tile->mi_row_end - tile->mi_row_start;
   1075  const int unit_rows = CEIL_POWER_OF_TWO(mi_rows, unit_height_log2);
   1076 
   1077  return unit_rows;
   1078 }
   1079 
   1080 int av1_get_unit_cols_in_tile(const TileInfo *tile,
   1081                              const BLOCK_SIZE fp_block_size) {
   1082  const int unit_width_log2 = mi_size_wide_log2[fp_block_size];
   1083  const int mi_cols = tile->mi_col_end - tile->mi_col_start;
   1084  const int unit_cols = CEIL_POWER_OF_TWO(mi_cols, unit_width_log2);
   1085 
   1086  return unit_cols;
   1087 }
   1088 
   1089 #define FIRST_PASS_ALT_REF_DISTANCE 16
   1090 static void first_pass_tile(AV1_COMP *cpi, ThreadData *td,
   1091                            TileDataEnc *tile_data,
   1092                            const BLOCK_SIZE fp_block_size) {
   1093  TileInfo *tile = &tile_data->tile_info;
   1094  const int unit_height = mi_size_high[fp_block_size];
   1095  const int unit_height_log2 = mi_size_high_log2[fp_block_size];
   1096  for (int mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
   1097       mi_row += unit_height) {
   1098    av1_first_pass_row(cpi, td, tile_data, mi_row >> unit_height_log2,
   1099                       fp_block_size);
   1100  }
   1101 }
   1102 
   1103 static void first_pass_tiles(AV1_COMP *cpi, const BLOCK_SIZE fp_block_size) {
   1104  AV1_COMMON *const cm = &cpi->common;
   1105  const int tile_cols = cm->tiles.cols;
   1106  const int tile_rows = cm->tiles.rows;
   1107 
   1108  av1_alloc_src_diff_buf(cm, &cpi->td.mb);
   1109  for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
   1110    for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
   1111      TileDataEnc *const tile_data =
   1112          &cpi->tile_data[tile_row * tile_cols + tile_col];
   1113      first_pass_tile(cpi, &cpi->td, tile_data, fp_block_size);
   1114    }
   1115  }
   1116 }
   1117 
   1118 void av1_first_pass_row(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
   1119                        const int unit_row, const BLOCK_SIZE fp_block_size) {
   1120  MACROBLOCK *const x = &td->mb;
   1121  AV1_COMMON *const cm = &cpi->common;
   1122  const CommonModeInfoParams *const mi_params = &cm->mi_params;
   1123  const SequenceHeader *const seq_params = cm->seq_params;
   1124  const int num_planes = av1_num_planes(cm);
   1125  MACROBLOCKD *const xd = &x->e_mbd;
   1126  TileInfo *tile = &tile_data->tile_info;
   1127  const int qindex = find_fp_qindex(seq_params->bit_depth);
   1128  const int fp_block_size_width = block_size_high[fp_block_size];
   1129  const int fp_block_size_height = block_size_wide[fp_block_size];
   1130  const int unit_width = mi_size_wide[fp_block_size];
   1131  const int unit_width_log2 = mi_size_wide_log2[fp_block_size];
   1132  const int unit_height_log2 = mi_size_high_log2[fp_block_size];
   1133  const int unit_cols = mi_params->mb_cols * 4 / unit_width;
   1134  int raw_motion_err_counts = 0;
   1135  int unit_row_in_tile = unit_row - (tile->mi_row_start >> unit_height_log2);
   1136  int unit_col_start = tile->mi_col_start >> unit_width_log2;
   1137  int unit_cols_in_tile = av1_get_unit_cols_in_tile(tile, fp_block_size);
   1138  MultiThreadInfo *const mt_info = &cpi->mt_info;
   1139  AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
   1140  AV1EncRowMultiThreadSync *const row_mt_sync = &tile_data->row_mt_sync;
   1141 
   1142  const YV12_BUFFER_CONFIG *last_frame =
   1143      av1_get_scaled_ref_frame(cpi, LAST_FRAME);
   1144  if (!last_frame) {
   1145    last_frame = get_ref_frame_yv12_buf(cm, LAST_FRAME);
   1146  }
   1147  const YV12_BUFFER_CONFIG *golden_frame =
   1148      av1_get_scaled_ref_frame(cpi, GOLDEN_FRAME);
   1149  if (!golden_frame) {
   1150    golden_frame = get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
   1151  }
   1152  YV12_BUFFER_CONFIG *const this_frame = &cm->cur_frame->buf;
   1153 
   1154  PICK_MODE_CONTEXT *ctx = td->firstpass_ctx;
   1155  FRAME_STATS *mb_stats =
   1156      cpi->firstpass_data.mb_stats + unit_row * unit_cols + unit_col_start;
   1157  int *raw_motion_err_list = cpi->firstpass_data.raw_motion_err_list +
   1158                             unit_row * unit_cols + unit_col_start;
   1159  MV *first_top_mv = &tile_data->firstpass_top_mv;
   1160 
   1161  for (int i = 0; i < num_planes; ++i) {
   1162    x->plane[i].coeff = ctx->coeff[i];
   1163    x->plane[i].qcoeff = ctx->qcoeff[i];
   1164    x->plane[i].eobs = ctx->eobs[i];
   1165    x->plane[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
   1166    x->plane[i].dqcoeff = ctx->dqcoeff[i];
   1167  }
   1168 
   1169  const int src_y_stride = cpi->source->y_stride;
   1170  const int recon_y_stride = this_frame->y_stride;
   1171  const int recon_uv_stride = this_frame->uv_stride;
   1172  const int uv_mb_height =
   1173      fp_block_size_height >> (this_frame->y_height > this_frame->uv_height);
   1174 
   1175  MV best_ref_mv = kZeroMv;
   1176  MV last_mv;
   1177 
   1178  // Reset above block coeffs.
   1179  xd->up_available = (unit_row_in_tile != 0);
   1180  int recon_yoffset = (unit_row * recon_y_stride * fp_block_size_height) +
   1181                      (unit_col_start * fp_block_size_width);
   1182  int src_yoffset = (unit_row * src_y_stride * fp_block_size_height) +
   1183                    (unit_col_start * fp_block_size_width);
   1184  int recon_uvoffset = (unit_row * recon_uv_stride * uv_mb_height) +
   1185                       (unit_col_start * uv_mb_height);
   1186 
   1187  // Set up limit values for motion vectors to prevent them extending
   1188  // outside the UMV borders.
   1189  av1_set_mv_row_limits(
   1190      mi_params, &x->mv_limits, (unit_row << unit_height_log2),
   1191      (fp_block_size_height >> MI_SIZE_LOG2), cpi->oxcf.border_in_pixels);
   1192 
   1193  av1_setup_src_planes(x, cpi->source, unit_row << unit_height_log2,
   1194                       tile->mi_col_start, num_planes, fp_block_size);
   1195 
   1196  // Fix - zero the 16x16 block first. This ensures correct this_intra_error for
   1197  // block sizes smaller than 16x16.
   1198  av1_zero_array(x->plane[0].src_diff, 256);
   1199 
   1200  for (int unit_col_in_tile = 0; unit_col_in_tile < unit_cols_in_tile;
   1201       unit_col_in_tile++) {
   1202    const int unit_col = unit_col_start + unit_col_in_tile;
   1203 
   1204    enc_row_mt->sync_read_ptr(row_mt_sync, unit_row_in_tile, unit_col_in_tile);
   1205 
   1206 #if CONFIG_MULTITHREAD
   1207    if (cpi->ppi->p_mt_info.num_workers > 1) {
   1208      pthread_mutex_lock(enc_row_mt->mutex_);
   1209      bool firstpass_mt_exit = enc_row_mt->firstpass_mt_exit;
   1210      pthread_mutex_unlock(enc_row_mt->mutex_);
   1211      // Exit in case any worker has encountered an error.
   1212      if (firstpass_mt_exit) return;
   1213    }
   1214 #endif
   1215 
   1216    if (unit_col_in_tile == 0) {
   1217      last_mv = *first_top_mv;
   1218    }
   1219    int this_intra_error = firstpass_intra_prediction(
   1220        cpi, td, this_frame, tile, unit_row, unit_col, recon_yoffset,
   1221        recon_uvoffset, fp_block_size, qindex, mb_stats);
   1222 
   1223    if (!frame_is_intra_only(cm)) {
   1224      const int this_inter_error = firstpass_inter_prediction(
   1225          cpi, td, last_frame, golden_frame, unit_row, unit_col, recon_yoffset,
   1226          recon_uvoffset, src_yoffset, fp_block_size, this_intra_error,
   1227          raw_motion_err_counts, raw_motion_err_list, best_ref_mv, &best_ref_mv,
   1228          &last_mv, mb_stats);
   1229      if (unit_col_in_tile == 0) {
   1230        *first_top_mv = last_mv;
   1231      }
   1232      mb_stats->coded_error += this_inter_error;
   1233      ++raw_motion_err_counts;
   1234    } else {
   1235      mb_stats->sr_coded_error += this_intra_error;
   1236      mb_stats->coded_error += this_intra_error;
   1237    }
   1238 
   1239    // Adjust to the next column of MBs.
   1240    x->plane[0].src.buf += fp_block_size_width;
   1241    if (num_planes > 1) {
   1242      x->plane[1].src.buf += uv_mb_height;
   1243      x->plane[2].src.buf += uv_mb_height;
   1244    }
   1245 
   1246    recon_yoffset += fp_block_size_width;
   1247    src_yoffset += fp_block_size_width;
   1248    recon_uvoffset += uv_mb_height;
   1249    mb_stats++;
   1250 
   1251    enc_row_mt->sync_write_ptr(row_mt_sync, unit_row_in_tile, unit_col_in_tile,
   1252                               unit_cols_in_tile);
   1253  }
   1254 }
   1255 
   1256 void av1_noop_first_pass_frame(AV1_COMP *cpi, const int64_t ts_duration) {
   1257  AV1_COMMON *const cm = &cpi->common;
   1258  CurrentFrame *const current_frame = &cm->current_frame;
   1259  const CommonModeInfoParams *const mi_params = &cm->mi_params;
   1260  int max_mb_rows = mi_params->mb_rows;
   1261  int max_mb_cols = mi_params->mb_cols;
   1262  if (cpi->oxcf.frm_dim_cfg.forced_max_frame_width) {
   1263    int max_mi_cols = size_in_mi(cpi->oxcf.frm_dim_cfg.forced_max_frame_width);
   1264    max_mb_cols = ROUND_POWER_OF_TWO(max_mi_cols, 2);
   1265  }
   1266  if (cpi->oxcf.frm_dim_cfg.forced_max_frame_height) {
   1267    int max_mi_rows = size_in_mi(cpi->oxcf.frm_dim_cfg.forced_max_frame_height);
   1268    max_mb_rows = ROUND_POWER_OF_TWO(max_mi_rows, 2);
   1269  }
   1270  const int unit_rows = get_unit_rows(BLOCK_16X16, max_mb_rows);
   1271  const int unit_cols = get_unit_cols(BLOCK_16X16, max_mb_cols);
   1272  setup_firstpass_data(cm, &cpi->firstpass_data, unit_rows, unit_cols);
   1273  FRAME_STATS *mb_stats = cpi->firstpass_data.mb_stats;
   1274  FRAME_STATS stats = accumulate_frame_stats(mb_stats, unit_rows, unit_cols);
   1275  av1_free_firstpass_data(&cpi->firstpass_data);
   1276  update_firstpass_stats(cpi, &stats, 1.0, current_frame->frame_number,
   1277                         ts_duration, BLOCK_16X16);
   1278 }
   1279 
   1280 void av1_first_pass(AV1_COMP *cpi, const int64_t ts_duration) {
   1281  MACROBLOCK *const x = &cpi->td.mb;
   1282  AV1_COMMON *const cm = &cpi->common;
   1283  const CommonModeInfoParams *const mi_params = &cm->mi_params;
   1284  CurrentFrame *const current_frame = &cm->current_frame;
   1285  const SequenceHeader *const seq_params = cm->seq_params;
   1286  const int num_planes = av1_num_planes(cm);
   1287  MACROBLOCKD *const xd = &x->e_mbd;
   1288  const int qindex = find_fp_qindex(seq_params->bit_depth);
   1289  const int ref_frame_flags_backup = cpi->ref_frame_flags;
   1290  cpi->ref_frame_flags = av1_ref_frame_flag_list[LAST_FRAME] |
   1291                         av1_ref_frame_flag_list[GOLDEN_FRAME];
   1292 
   1293  // Detect if the key frame is screen content type.
   1294  if (frame_is_intra_only(cm)) {
   1295    FeatureFlags *const features = &cm->features;
   1296    assert(cpi->source != NULL);
   1297    xd->cur_buf = cpi->source;
   1298    av1_set_screen_content_options(cpi, features);
   1299  }
   1300 
   1301  // Prepare the speed features
   1302  av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
   1303 
   1304  // Unit size for the first pass encoding.
   1305  const BLOCK_SIZE fp_block_size =
   1306      get_fp_block_size(cpi->is_screen_content_type);
   1307 
   1308  int max_mb_rows = mi_params->mb_rows;
   1309  int max_mb_cols = mi_params->mb_cols;
   1310  if (cpi->oxcf.frm_dim_cfg.forced_max_frame_width) {
   1311    int max_mi_cols = size_in_mi(cpi->oxcf.frm_dim_cfg.forced_max_frame_width);
   1312    max_mb_cols = ROUND_POWER_OF_TWO(max_mi_cols, 2);
   1313  }
   1314  if (cpi->oxcf.frm_dim_cfg.forced_max_frame_height) {
   1315    int max_mi_rows = size_in_mi(cpi->oxcf.frm_dim_cfg.forced_max_frame_height);
   1316    max_mb_rows = ROUND_POWER_OF_TWO(max_mi_rows, 2);
   1317  }
   1318 
   1319  // Number of rows in the unit size.
   1320  // Note max_mb_rows and max_mb_cols are in the unit of 16x16.
   1321  const int unit_rows = get_unit_rows(fp_block_size, max_mb_rows);
   1322  const int unit_cols = get_unit_cols(fp_block_size, max_mb_cols);
   1323 
   1324  // Set fp_block_size, for the convenience of multi-thread usage.
   1325  cpi->fp_block_size = fp_block_size;
   1326 
   1327  setup_firstpass_data(cm, &cpi->firstpass_data, unit_rows, unit_cols);
   1328  int *raw_motion_err_list = cpi->firstpass_data.raw_motion_err_list;
   1329  FRAME_STATS *mb_stats = cpi->firstpass_data.mb_stats;
   1330 
   1331  // multi threading info
   1332  MultiThreadInfo *const mt_info = &cpi->mt_info;
   1333  AV1EncRowMultiThreadInfo *const enc_row_mt = &mt_info->enc_row_mt;
   1334 
   1335  const int tile_cols = cm->tiles.cols;
   1336  const int tile_rows = cm->tiles.rows;
   1337  if (cpi->allocated_tiles < tile_cols * tile_rows) {
   1338    av1_alloc_tile_data(cpi);
   1339  }
   1340 
   1341  av1_init_tile_data(cpi);
   1342 
   1343  const YV12_BUFFER_CONFIG *last_frame = NULL;
   1344  const YV12_BUFFER_CONFIG *golden_frame = NULL;
   1345  if (!frame_is_intra_only(cm)) {
   1346    av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0);
   1347    last_frame = av1_is_scaled(get_ref_scale_factors_const(cm, LAST_FRAME))
   1348                     ? av1_get_scaled_ref_frame(cpi, LAST_FRAME)
   1349                     : get_ref_frame_yv12_buf(cm, LAST_FRAME);
   1350    golden_frame = av1_is_scaled(get_ref_scale_factors_const(cm, GOLDEN_FRAME))
   1351                       ? av1_get_scaled_ref_frame(cpi, GOLDEN_FRAME)
   1352                       : get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
   1353  }
   1354 
   1355  YV12_BUFFER_CONFIG *const this_frame = &cm->cur_frame->buf;
   1356  // First pass code requires valid last and new frame buffers.
   1357  assert(this_frame != NULL);
   1358  assert(frame_is_intra_only(cm) || (last_frame != NULL));
   1359 
   1360  av1_setup_frame_size(cpi);
   1361  av1_set_mv_search_params(cpi);
   1362 
   1363  set_mi_offsets(mi_params, xd, 0, 0);
   1364  xd->mi[0]->bsize = fp_block_size;
   1365 
   1366  // Do not use periodic key frames.
   1367  cpi->rc.frames_to_key = INT_MAX;
   1368 
   1369  av1_set_quantizer(
   1370      cm, cpi->oxcf.q_cfg.qm_minlevel, cpi->oxcf.q_cfg.qm_maxlevel, qindex,
   1371      cpi->oxcf.q_cfg.enable_chroma_deltaq, cpi->oxcf.q_cfg.enable_hdr_deltaq,
   1372      cpi->oxcf.mode == ALLINTRA, cpi->oxcf.tune_cfg.tuning);
   1373 
   1374  av1_setup_block_planes(xd, seq_params->subsampling_x,
   1375                         seq_params->subsampling_y, num_planes);
   1376 
   1377  av1_setup_src_planes(x, cpi->source, 0, 0, num_planes, fp_block_size);
   1378  av1_setup_dst_planes(xd->plane, seq_params->sb_size, this_frame, 0, 0, 0,
   1379                       num_planes);
   1380 
   1381  if (!frame_is_intra_only(cm)) {
   1382    av1_setup_pre_planes(xd, 0, last_frame, 0, 0, NULL, num_planes);
   1383  }
   1384 
   1385  set_mi_offsets(mi_params, xd, 0, 0);
   1386 
   1387  // Don't store luma on the fist pass since chroma is not computed
   1388  xd->cfl.store_y = 0;
   1389  av1_frame_init_quantizer(cpi);
   1390 
   1391  av1_default_coef_probs(cm);
   1392  av1_init_mode_probs(cm->fc);
   1393  av1_init_mv_probs(cm);
   1394  av1_initialize_rd_consts(cpi);
   1395 
   1396  enc_row_mt->sync_read_ptr = av1_row_mt_sync_read_dummy;
   1397  enc_row_mt->sync_write_ptr = av1_row_mt_sync_write_dummy;
   1398 
   1399  if (mt_info->num_workers > 1) {
   1400    enc_row_mt->sync_read_ptr = av1_row_mt_sync_read;
   1401    enc_row_mt->sync_write_ptr = av1_row_mt_sync_write;
   1402    av1_fp_encode_tiles_row_mt(cpi);
   1403  } else {
   1404    first_pass_tiles(cpi, fp_block_size);
   1405  }
   1406 
   1407  FRAME_STATS stats = accumulate_frame_stats(mb_stats, unit_rows, unit_cols);
   1408  int total_raw_motion_err_count =
   1409      frame_is_intra_only(cm) ? 0 : unit_rows * unit_cols;
   1410  const double raw_err_stdev =
   1411      raw_motion_error_stdev(raw_motion_err_list, total_raw_motion_err_count);
   1412  av1_free_firstpass_data(&cpi->firstpass_data);
   1413  av1_dealloc_src_diff_buf(&cpi->td.mb, av1_num_planes(cm));
   1414 
   1415  // Clamp the image start to rows/2. This number of rows is discarded top
   1416  // and bottom as dead data so rows / 2 means the frame is blank.
   1417  if ((stats.image_data_start_row > unit_rows / 2) ||
   1418      (stats.image_data_start_row == INVALID_ROW)) {
   1419    stats.image_data_start_row = unit_rows / 2;
   1420  }
   1421  // Exclude any image dead zone
   1422  if (stats.image_data_start_row > 0) {
   1423    stats.intra_skip_count =
   1424        AOMMAX(0, stats.intra_skip_count -
   1425                      (stats.image_data_start_row * unit_cols * 2));
   1426  }
   1427 
   1428  TWO_PASS *twopass = &cpi->ppi->twopass;
   1429  const int num_mbs_16X16 = (cpi->oxcf.resize_cfg.resize_mode != RESIZE_NONE)
   1430                                ? cpi->initial_mbs
   1431                                : mi_params->MBs;
   1432  // Number of actual units used in the first pass, it can be other square
   1433  // block sizes than 16X16.
   1434  const int num_mbs = get_num_mbs(fp_block_size, num_mbs_16X16);
   1435  stats.intra_factor = stats.intra_factor / (double)num_mbs;
   1436  stats.brightness_factor = stats.brightness_factor / (double)num_mbs;
   1437  FIRSTPASS_STATS *this_frame_stats = twopass->stats_buf_ctx->stats_in_end;
   1438  update_firstpass_stats(cpi, &stats, raw_err_stdev,
   1439                         current_frame->frame_number, ts_duration,
   1440                         fp_block_size);
   1441 
   1442  // Copy the previous Last Frame back into gf buffer if the prediction is good
   1443  // enough... but also don't allow it to lag too far.
   1444  if ((twopass->sr_update_lag > 3) ||
   1445      ((current_frame->frame_number > 0) &&
   1446       (this_frame_stats->pcnt_inter > 0.20) &&
   1447       ((this_frame_stats->intra_error /
   1448         DOUBLE_DIVIDE_CHECK(this_frame_stats->coded_error)) > 2.0))) {
   1449    if (golden_frame != NULL) {
   1450      assign_frame_buffer_p(
   1451          &cm->ref_frame_map[get_ref_frame_map_idx(cm, GOLDEN_FRAME)],
   1452          cm->ref_frame_map[get_ref_frame_map_idx(cm, LAST_FRAME)]);
   1453    }
   1454    twopass->sr_update_lag = 1;
   1455  } else {
   1456    ++twopass->sr_update_lag;
   1457  }
   1458 
   1459  aom_extend_frame_borders(this_frame, num_planes);
   1460 
   1461  // The frame we just compressed now becomes the last frame.
   1462  assign_frame_buffer_p(
   1463      &cm->ref_frame_map[get_ref_frame_map_idx(cm, LAST_FRAME)], cm->cur_frame);
   1464 
   1465  // Special case for the first frame. Copy into the GF buffer as a second
   1466  // reference.
   1467  if (current_frame->frame_number == 0 &&
   1468      get_ref_frame_map_idx(cm, GOLDEN_FRAME) != INVALID_IDX) {
   1469    assign_frame_buffer_p(
   1470        &cm->ref_frame_map[get_ref_frame_map_idx(cm, GOLDEN_FRAME)],
   1471        cm->ref_frame_map[get_ref_frame_map_idx(cm, LAST_FRAME)]);
   1472  }
   1473 
   1474  print_reconstruction_frame(last_frame, current_frame->frame_number,
   1475                             /*do_print=*/0);
   1476 
   1477  ++current_frame->frame_number;
   1478  cpi->ref_frame_flags = ref_frame_flags_backup;
   1479  if (!frame_is_intra_only(cm)) {
   1480    release_scaled_references(cpi);
   1481  }
   1482 }
   1483 
   1484 aom_codec_err_t av1_firstpass_info_init(FIRSTPASS_INFO *firstpass_info,
   1485                                        FIRSTPASS_STATS *ext_stats_buf,
   1486                                        int ext_stats_buf_size) {
   1487  assert(IMPLIES(ext_stats_buf == NULL, ext_stats_buf_size == 0));
   1488  if (ext_stats_buf == NULL) {
   1489    firstpass_info->stats_buf = firstpass_info->static_stats_buf;
   1490    firstpass_info->stats_buf_size =
   1491        sizeof(firstpass_info->static_stats_buf) /
   1492        sizeof(firstpass_info->static_stats_buf[0]);
   1493    firstpass_info->start_index = 0;
   1494    firstpass_info->cur_index = 0;
   1495    firstpass_info->stats_count = 0;
   1496    firstpass_info->future_stats_count = 0;
   1497    firstpass_info->past_stats_count = 0;
   1498    av1_zero(firstpass_info->total_stats);
   1499    if (ext_stats_buf_size == 0) {
   1500      return AOM_CODEC_OK;
   1501    } else {
   1502      return AOM_CODEC_ERROR;
   1503    }
   1504  } else {
   1505    firstpass_info->stats_buf = ext_stats_buf;
   1506    firstpass_info->stats_buf_size = ext_stats_buf_size;
   1507    firstpass_info->start_index = 0;
   1508    firstpass_info->cur_index = 0;
   1509    firstpass_info->stats_count = firstpass_info->stats_buf_size;
   1510    firstpass_info->future_stats_count = firstpass_info->stats_count;
   1511    firstpass_info->past_stats_count = 0;
   1512    av1_zero(firstpass_info->total_stats);
   1513    for (int i = 0; i < firstpass_info->stats_count; ++i) {
   1514      av1_accumulate_stats(&firstpass_info->total_stats,
   1515                           &firstpass_info->stats_buf[i]);
   1516    }
   1517  }
   1518  return AOM_CODEC_OK;
   1519 }
   1520 
   1521 aom_codec_err_t av1_firstpass_info_move_cur_index(
   1522    FIRSTPASS_INFO *firstpass_info) {
   1523  assert(firstpass_info->future_stats_count +
   1524             firstpass_info->past_stats_count ==
   1525         firstpass_info->stats_count);
   1526  if (firstpass_info->future_stats_count > 1) {
   1527    firstpass_info->cur_index =
   1528        (firstpass_info->cur_index + 1) % firstpass_info->stats_buf_size;
   1529    --firstpass_info->future_stats_count;
   1530    ++firstpass_info->past_stats_count;
   1531    return AOM_CODEC_OK;
   1532  } else {
   1533    return AOM_CODEC_ERROR;
   1534  }
   1535 }
   1536 
   1537 aom_codec_err_t av1_firstpass_info_pop(FIRSTPASS_INFO *firstpass_info) {
   1538  if (firstpass_info->stats_count > 0 && firstpass_info->past_stats_count > 0) {
   1539    const int next_start =
   1540        (firstpass_info->start_index + 1) % firstpass_info->stats_buf_size;
   1541    firstpass_info->start_index = next_start;
   1542    --firstpass_info->stats_count;
   1543    --firstpass_info->past_stats_count;
   1544    return AOM_CODEC_OK;
   1545  } else {
   1546    return AOM_CODEC_ERROR;
   1547  }
   1548 }
   1549 
   1550 aom_codec_err_t av1_firstpass_info_move_cur_index_and_pop(
   1551    FIRSTPASS_INFO *firstpass_info) {
   1552  aom_codec_err_t ret = av1_firstpass_info_move_cur_index(firstpass_info);
   1553  if (ret != AOM_CODEC_OK) return ret;
   1554  ret = av1_firstpass_info_pop(firstpass_info);
   1555  return ret;
   1556 }
   1557 
   1558 aom_codec_err_t av1_firstpass_info_push(FIRSTPASS_INFO *firstpass_info,
   1559                                        const FIRSTPASS_STATS *input_stats) {
   1560  if (firstpass_info->stats_count < firstpass_info->stats_buf_size) {
   1561    const int next_index =
   1562        (firstpass_info->start_index + firstpass_info->stats_count) %
   1563        firstpass_info->stats_buf_size;
   1564    firstpass_info->stats_buf[next_index] = *input_stats;
   1565    ++firstpass_info->stats_count;
   1566    ++firstpass_info->future_stats_count;
   1567    av1_accumulate_stats(&firstpass_info->total_stats, input_stats);
   1568    return AOM_CODEC_OK;
   1569  } else {
   1570    return AOM_CODEC_ERROR;
   1571  }
   1572 }
   1573 
   1574 const FIRSTPASS_STATS *av1_firstpass_info_peek(
   1575    const FIRSTPASS_INFO *firstpass_info, int offset_from_cur) {
   1576  if (offset_from_cur >= -firstpass_info->past_stats_count &&
   1577      offset_from_cur < firstpass_info->future_stats_count) {
   1578    const int index = (firstpass_info->cur_index + offset_from_cur) %
   1579                      firstpass_info->stats_buf_size;
   1580    return &firstpass_info->stats_buf[index];
   1581  } else {
   1582    return NULL;
   1583  }
   1584 }
   1585 
   1586 int av1_firstpass_info_future_count(const FIRSTPASS_INFO *firstpass_info,
   1587                                    int offset_from_cur) {
   1588  if (offset_from_cur < firstpass_info->future_stats_count) {
   1589    return firstpass_info->future_stats_count - offset_from_cur;
   1590  }
   1591  return 0;
   1592 }