encoder.c 214 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11
 */

12
#include <limits.h>
Jingning Han's avatar
Jingning Han committed
13 14 15
#include <math.h>
#include <stdio.h>

Yaowu Xu's avatar
Yaowu Xu committed
16
#include "./aom_config.h"
Jingning Han's avatar
Jingning Han committed
17

18
#include "av1/common/alloccommon.h"
19
#if CONFIG_CDEF
20
#include "av1/common/cdef.h"
21
#include "av1/common/clpf.h"
22
#endif  // CONFIG_CDEF
23 24 25 26
#include "av1/common/filter.h"
#include "av1/common/idct.h"
#include "av1/common/reconinter.h"
#include "av1/common/reconintra.h"
27
#include "av1/common/resize.h"
28
#include "av1/common/tile_common.h"
Jingning Han's avatar
Jingning Han committed
29

30 31 32 33
#include "av1/encoder/aq_complexity.h"
#include "av1/encoder/aq_cyclicrefresh.h"
#include "av1/encoder/aq_variance.h"
#include "av1/encoder/bitstream.h"
Todd Nguyen's avatar
Todd Nguyen committed
34 35 36
#if CONFIG_BGSPRITE
#include "av1/encoder/bgsprite.h"
#endif  // CONFIG_BGSPRITE
37
#if CONFIG_ANS
Alex Converse's avatar
Alex Converse committed
38
#include "aom_dsp/buf_ans.h"
39
#endif
40 41 42 43
#include "av1/encoder/context_tree.h"
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/encoder.h"
44 45 46
#if CONFIG_LV_MAP
#include "av1/encoder/encodetxb.h"
#endif
47 48 49 50
#include "av1/encoder/ethread.h"
#include "av1/encoder/firstpass.h"
#include "av1/encoder/mbgraph.h"
#include "av1/encoder/picklpf.h"
51
#if CONFIG_LOOP_RESTORATION
52
#include "av1/encoder/pickrst.h"
53
#endif  // CONFIG_LOOP_RESTORATION
54 55 56 57 58
#include "av1/encoder/ratectrl.h"
#include "av1/encoder/rd.h"
#include "av1/encoder/segmentation.h"
#include "av1/encoder/speed_features.h"
#include "av1/encoder/temporal_filter.h"
Jingning Han's avatar
Jingning Han committed
59

Yaowu Xu's avatar
Yaowu Xu committed
60 61 62
#include "./av1_rtcd.h"
#include "./aom_dsp_rtcd.h"
#include "./aom_scale_rtcd.h"
63
#include "aom_dsp/psnr.h"
64
#if CONFIG_INTERNAL_STATS
65
#include "aom_dsp/ssim.h"
66
#endif
Yaowu Xu's avatar
Yaowu Xu committed
67 68
#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/aom_filter.h"
Jingning Han's avatar
Jingning Han committed
69
#include "aom_ports/aom_timer.h"
70 71
#include "aom_ports/mem.h"
#include "aom_ports/system_state.h"
Yaowu Xu's avatar
Yaowu Xu committed
72
#include "aom_scale/aom_scale.h"
73
#if CONFIG_BITSTREAM_DEBUG
74
#include "aom_util/debug_util.h"
75
#endif  // CONFIG_BITSTREAM_DEBUG
76

77 78
#if CONFIG_ENTROPY_STATS
FRAME_COUNTS aggregate_fc;
79 80
// Aggregate frame counts per frame context type
FRAME_COUNTS aggregate_fc_per_type[FRAME_CONTEXTS];
81 82
#endif  // CONFIG_ENTROPY_STATS

Jingning Han's avatar
Jingning Han committed
83 84 85
#define AM_SEGMENT_ID_INACTIVE 7
#define AM_SEGMENT_ID_ACTIVE 0

86 87 88 89 90 91 92 93
#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */

#define ALTREF_HIGH_PRECISION_MV 1     // Whether to use high precision mv
                                       //  for altref computation.
#define HIGH_PRECISION_MV_QTHRESH 200  // Q threshold for high precision
                                       // mv. Choose a very high value for
                                       // now so that HIGH_PRECISION is always
                                       // chosen.
Jingning Han's avatar
Jingning Han committed
94 95 96 97 98 99 100 101 102
// #define OUTPUT_YUV_REC
#ifdef OUTPUT_YUV_DENOISED
FILE *yuv_denoised_file = NULL;
#endif
#ifdef OUTPUT_YUV_SKINMAP
FILE *yuv_skinmap_file = NULL;
#endif
#ifdef OUTPUT_YUV_REC
FILE *yuv_rec_file;
103
#define FILE_NAME_LEN 100
Jingning Han's avatar
Jingning Han committed
104 105 106 107 108 109 110 111
#endif

#if 0
FILE *framepsnr;
FILE *kf_list;
FILE *keyfile;
#endif

Luc Trudeau's avatar
Luc Trudeau committed
112 113 114 115
#if CONFIG_CFL
CFL_CTX NULL_CFL;
#endif

116 117 118 119
#if CONFIG_INTERNAL_STATS
typedef enum { Y, U, V, ALL } STAT_TYPE;
#endif  // CONFIG_INTERNAL_STATS

Yaowu Xu's avatar
Yaowu Xu committed
120
static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Jingning Han's avatar
Jingning Han committed
121 122 123 124 125 126 127 128 129 130 131 132
  switch (mode) {
    case NORMAL:
      *hr = 1;
      *hs = 1;
      break;
    case FOURFIVE:
      *hr = 4;
      *hs = 5;
      break;
    case THREEFIVE:
      *hr = 3;
      *hs = 5;
133
      break;
Jingning Han's avatar
Jingning Han committed
134 135 136
    case ONETWO:
      *hr = 1;
      *hs = 2;
137
      break;
Jingning Han's avatar
Jingning Han committed
138 139 140
    default:
      *hr = 1;
      *hs = 1;
141
      assert(0);
Jingning Han's avatar
Jingning Han committed
142 143 144 145 146 147
      break;
  }
}

// Mark all inactive blocks as active. Other segmentation features may be set
// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xu's avatar
Yaowu Xu committed
148
static void suppress_active_map(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
149 150 151 152 153 154 155 156
  unsigned char *const seg_map = cpi->segmentation_map;
  int i;
  if (cpi->active_map.enabled || cpi->active_map.update)
    for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
      if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
        seg_map[i] = AM_SEGMENT_ID_ACTIVE;
}

Yaowu Xu's avatar
Yaowu Xu committed
157
static void apply_active_map(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  struct segmentation *const seg = &cpi->common.seg;
  unsigned char *const seg_map = cpi->segmentation_map;
  const unsigned char *const active_map = cpi->active_map.map;
  int i;

  assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);

  if (frame_is_intra_only(&cpi->common)) {
    cpi->active_map.enabled = 0;
    cpi->active_map.update = 1;
  }

  if (cpi->active_map.update) {
    if (cpi->active_map.enabled) {
      for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
        if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xu's avatar
Yaowu Xu committed
174 175 176
      av1_enable_segmentation(seg);
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Jingning Han's avatar
Jingning Han committed
177 178
      // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
      // filter level being zero regardless of the value of seg->abs_delta.
Yaowu Xu's avatar
Yaowu Xu committed
179 180
      av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
                      -MAX_LOOP_FILTER);
Jingning Han's avatar
Jingning Han committed
181
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
182 183
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Jingning Han's avatar
Jingning Han committed
184 185 186 187 188 189 190 191 192
      if (seg->enabled) {
        seg->update_data = 1;
        seg->update_map = 1;
      }
    }
    cpi->active_map.update = 0;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
193 194
int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
                       int cols) {
Jingning Han's avatar
Jingning Han committed
195 196 197 198
  if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
    unsigned char *const active_map_8x8 = cpi->active_map.map;
    const int mi_rows = cpi->common.mi_rows;
    const int mi_cols = cpi->common.mi_cols;
199 200
    const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
    const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Jingning Han's avatar
Jingning Han committed
201 202 203 204 205 206
    cpi->active_map.update = 1;
    if (new_map_16x16) {
      int r, c;
      for (r = 0; r < mi_rows; ++r) {
        for (c = 0; c < mi_cols; ++c) {
          active_map_8x8[r * mi_cols + c] =
207
              new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Jingning Han's avatar
Jingning Han committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221
                  ? AM_SEGMENT_ID_ACTIVE
                  : AM_SEGMENT_ID_INACTIVE;
        }
      }
      cpi->active_map.enabled = 1;
    } else {
      cpi->active_map.enabled = 0;
    }
    return 0;
  } else {
    return -1;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
222 223
int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
                       int cols) {
Jingning Han's avatar
Jingning Han committed
224 225
  if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
      new_map_16x16) {
226
    unsigned char *const seg_map_8x8 = cpi->segmentation_map;
Jingning Han's avatar
Jingning Han committed
227 228
    const int mi_rows = cpi->common.mi_rows;
    const int mi_cols = cpi->common.mi_cols;
229 230 231
    const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
    const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;

Jingning Han's avatar
Jingning Han committed
232 233 234 235 236 237 238
    memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
    if (cpi->active_map.enabled) {
      int r, c;
      for (r = 0; r < mi_rows; ++r) {
        for (c = 0; c < mi_cols; ++c) {
          // Cyclic refresh segments are considered active despite not having
          // AM_SEGMENT_ID_ACTIVE
239
          new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Jingning Han's avatar
Jingning Han committed
240 241 242 243 244 245 246 247 248 249
              seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
        }
      }
    }
    return 0;
  } else {
    return -1;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
250
void av1_set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv) {
Jingning Han's avatar
Jingning Han committed
251 252
  MACROBLOCK *const mb = &cpi->td.mb;
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
253 254 255 256 257 258 259 260 261 262 263 264

  if (cpi->common.allow_high_precision_mv) {
    int i;
    for (i = 0; i < NMV_CONTEXTS; ++i) {
      mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
    }
  } else {
    int i;
    for (i = 0; i < NMV_CONTEXTS; ++i) {
      mb->mv_cost_stack[i] = mb->nmvcost[i];
    }
  }
Jingning Han's avatar
Jingning Han committed
265 266
}

Yaowu Xu's avatar
Yaowu Xu committed
267
static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
268
#if CONFIG_EXT_PARTITION
Yaowu Xu's avatar
Yaowu Xu committed
269
  if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
270 271
    return BLOCK_64X64;

Yaowu Xu's avatar
Yaowu Xu committed
272
  if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
273 274
    return BLOCK_128X128;

Yaowu Xu's avatar
Yaowu Xu committed
275
  assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
276 277 278 279 280 281 282 283 284 285 286 287 288 289

  assert(IMPLIES(cpi->common.tile_cols > 1,
                 cpi->common.tile_width % MAX_MIB_SIZE == 0));
  assert(IMPLIES(cpi->common.tile_rows > 1,
                 cpi->common.tile_height % MAX_MIB_SIZE == 0));

  // TODO(any): Possibly could improve this with a heuristic.
  return BLOCK_128X128;
#else
  (void)cpi;
  return BLOCK_64X64;
#endif  //  CONFIG_EXT_PARTITION
}

Yaowu Xu's avatar
Yaowu Xu committed
290 291
static void setup_frame(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
292 293 294 295 296 297
  // Set up entropy context depending on frame type. The decoder mandates
  // the use of the default context, index 0, for keyframes and inter
  // frames where the error_resilient_mode or intra_only flag is set. For
  // other inter-frames the encoder currently uses only two contexts;
  // context 1 for ALTREF frames and context 0 for the others.
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xu's avatar
Yaowu Xu committed
298
    av1_setup_past_independence(cm);
Jingning Han's avatar
Jingning Han committed
299
  } else {
300 301 302 303 304 305 306
#if CONFIG_EXT_REFS
    const GF_GROUP *gf_group = &cpi->twopass.gf_group;
    if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
      cm->frame_context_idx = EXT_ARF_FRAME;
    else if (cpi->refresh_alt_ref_frame)
      cm->frame_context_idx = ARF_FRAME;
#else
307
    if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
Zoe Liu's avatar
Zoe Liu committed
308
#endif  // CONFIG_EXT_REFS
309 310 311 312 313 314 315
    else if (cpi->rc.is_src_frame_alt_ref)
      cm->frame_context_idx = OVERLAY_FRAME;
    else if (cpi->refresh_golden_frame)
      cm->frame_context_idx = GLD_FRAME;
#if CONFIG_EXT_REFS
    else if (cpi->refresh_bwd_ref_frame)
      cm->frame_context_idx = BRF_FRAME;
Zoe Liu's avatar
Zoe Liu committed
316
#endif  // CONFIG_EXT_REFS
317 318
    else
      cm->frame_context_idx = REGULAR_FRAME;
Jingning Han's avatar
Jingning Han committed
319 320 321
  }

  if (cm->frame_type == KEY_FRAME) {
Yunqing Wang's avatar
Yunqing Wang committed
322
    cpi->refresh_golden_frame = 1;
Jingning Han's avatar
Jingning Han committed
323
    cpi->refresh_alt_ref_frame = 1;
Yaowu Xu's avatar
Yaowu Xu committed
324
    av1_zero(cpi->interp_filter_selected);
Jingning Han's avatar
Jingning Han committed
325 326
  } else {
    *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Yaowu Xu's avatar
Yaowu Xu committed
327
    av1_zero(cpi->interp_filter_selected[0]);
Jingning Han's avatar
Jingning Han committed
328
  }
329
#if CONFIG_EXT_REFS
330
#if CONFIG_ONE_SIDED_COMPOUND  // No change to bitstream
331 332 333 334 335 336
  if (cpi->sf.recode_loop == DISALLOW_RECODE) {
    cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
    cpi->rc.is_bipred_frame = 1;
  }
#endif
#endif
337
  cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
338

Geza Lore's avatar
Geza Lore committed
339 340
  cpi->vaq_refresh = 0;

341
  set_sb_size(cm, select_sb_size(cpi));
Jingning Han's avatar
Jingning Han committed
342 343
}

Yaowu Xu's avatar
Yaowu Xu committed
344
static void av1_enc_setup_mi(AV1_COMMON *cm) {
Jingning Han's avatar
Jingning Han committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  int i;
  cm->mi = cm->mip + cm->mi_stride + 1;
  memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
  cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
  // Clear top border row
  memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
  // Clear left border column
  for (i = 1; i < cm->mi_rows + 1; ++i)
    memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));

  cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;

  memset(cm->mi_grid_base, 0,
         cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
}

Yaowu Xu's avatar
Yaowu Xu committed
362 363
static int av1_enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
  cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
364
  if (!cm->mip) return 1;
Yaowu Xu's avatar
Yaowu Xu committed
365
  cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
366
  if (!cm->prev_mip) return 1;
Jingning Han's avatar
Jingning Han committed
367 368
  cm->mi_alloc_size = mi_size;

Yaowu Xu's avatar
Yaowu Xu committed
369
  cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
370 371
  if (!cm->mi_grid_base) return 1;
  cm->prev_mi_grid_base =
Yaowu Xu's avatar
Yaowu Xu committed
372
      (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
373
  if (!cm->prev_mi_grid_base) return 1;
Jingning Han's avatar
Jingning Han committed
374 375 376 377

  return 0;
}

Yaowu Xu's avatar
Yaowu Xu committed
378 379
static void av1_enc_free_mi(AV1_COMMON *cm) {
  aom_free(cm->mip);
Jingning Han's avatar
Jingning Han committed
380
  cm->mip = NULL;
Yaowu Xu's avatar
Yaowu Xu committed
381
  aom_free(cm->prev_mip);
Jingning Han's avatar
Jingning Han committed
382
  cm->prev_mip = NULL;
Yaowu Xu's avatar
Yaowu Xu committed
383
  aom_free(cm->mi_grid_base);
Jingning Han's avatar
Jingning Han committed
384
  cm->mi_grid_base = NULL;
Yaowu Xu's avatar
Yaowu Xu committed
385
  aom_free(cm->prev_mi_grid_base);
Jingning Han's avatar
Jingning Han committed
386 387 388
  cm->prev_mi_grid_base = NULL;
}

Yaowu Xu's avatar
Yaowu Xu committed
389
static void av1_swap_mi_and_prev_mi(AV1_COMMON *cm) {
Jingning Han's avatar
Jingning Han committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  // Current mip will be the prev_mip for the next frame.
  MODE_INFO **temp_base = cm->prev_mi_grid_base;
  MODE_INFO *temp = cm->prev_mip;
  cm->prev_mip = cm->mip;
  cm->mip = temp;

  // Update the upper left visible macroblock ptrs.
  cm->mi = cm->mip + cm->mi_stride + 1;
  cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;

  cm->prev_mi_grid_base = cm->mi_grid_base;
  cm->mi_grid_base = temp_base;
  cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
  cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
}

Yaowu Xu's avatar
Yaowu Xu committed
406
void av1_initialize_enc(void) {
Jingning Han's avatar
Jingning Han committed
407 408 409
  static volatile int init_done = 0;

  if (!init_done) {
Yaowu Xu's avatar
Yaowu Xu committed
410 411 412 413 414
    av1_rtcd();
    aom_dsp_rtcd();
    aom_scale_rtcd();
    av1_init_intra_predictors();
    av1_init_me_luts();
415
#if !CONFIG_XIPHRC
Yaowu Xu's avatar
Yaowu Xu committed
416
    av1_rc_init_minq_luts();
417
#endif
Yaowu Xu's avatar
Yaowu Xu committed
418 419
    av1_entropy_mv_init();
    av1_encode_token_init();
420
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
421
    av1_init_wedge_masks();
422
#endif
Jingning Han's avatar
Jingning Han committed
423 424 425 426
    init_done = 1;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
427 428
static void dealloc_compressor_data(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
429

Yaowu Xu's avatar
Yaowu Xu committed
430
  aom_free(cpi->mbmi_ext_base);
Jingning Han's avatar
Jingning Han committed
431 432
  cpi->mbmi_ext_base = NULL;

433 434
#if CONFIG_PVQ
  if (cpi->oxcf.pass != 1) {
Yushin Cho's avatar
Yushin Cho committed
435 436
    const int tile_cols = cm->tile_cols;
    const int tile_rows = cm->tile_rows;
437 438 439 440 441 442 443 444 445 446
    int tile_col, tile_row;

    for (tile_row = 0; tile_row < tile_rows; ++tile_row)
      for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
        TileDataEnc *tile_data =
            &cpi->tile_data[tile_row * tile_cols + tile_col];
        aom_free(tile_data->pvq_q.buf);
      }
  }
#endif
Yaowu Xu's avatar
Yaowu Xu committed
447
  aom_free(cpi->tile_data);
Jingning Han's avatar
Jingning Han committed
448 449 450
  cpi->tile_data = NULL;

  // Delete sementation map
Yaowu Xu's avatar
Yaowu Xu committed
451
  aom_free(cpi->segmentation_map);
Jingning Han's avatar
Jingning Han committed
452 453
  cpi->segmentation_map = NULL;

Yaowu Xu's avatar
Yaowu Xu committed
454
  av1_cyclic_refresh_free(cpi->cyclic_refresh);
Jingning Han's avatar
Jingning Han committed
455 456
  cpi->cyclic_refresh = NULL;

Yaowu Xu's avatar
Yaowu Xu committed
457
  aom_free(cpi->active_map.map);
Jingning Han's avatar
Jingning Han committed
458 459
  cpi->active_map.map = NULL;

460 461 462 463 464 465 466 467 468 469 470 471 472 473
#if CONFIG_MOTION_VAR
  aom_free(cpi->td.mb.above_pred_buf);
  cpi->td.mb.above_pred_buf = NULL;

  aom_free(cpi->td.mb.left_pred_buf);
  cpi->td.mb.left_pred_buf = NULL;

  aom_free(cpi->td.mb.wsrc_buf);
  cpi->td.mb.wsrc_buf = NULL;

  aom_free(cpi->td.mb.mask_buf);
  cpi->td.mb.mask_buf = NULL;
#endif

Yaowu Xu's avatar
Yaowu Xu committed
474
  av1_free_ref_frame_buffers(cm->buffer_pool);
475 476 477
#if CONFIG_LV_MAP
  av1_free_txb_buf(cpi);
#endif
Yaowu Xu's avatar
Yaowu Xu committed
478
  av1_free_context_buffers(cm);
Jingning Han's avatar
Jingning Han committed
479

Yaowu Xu's avatar
Yaowu Xu committed
480
  aom_free_frame_buffer(&cpi->last_frame_uf);
Debargha Mukherjee's avatar
Debargha Mukherjee committed
481
#if CONFIG_LOOP_RESTORATION
Yaowu Xu's avatar
Yaowu Xu committed
482
  av1_free_restoration_buffers(cm);
483 484 485
  aom_free_frame_buffer(&cpi->last_frame_db);
  aom_free_frame_buffer(&cpi->trial_frame_rst);
  aom_free(cpi->extra_rstbuf);
486 487 488 489 490
  {
    int i;
    for (i = 0; i < MAX_MB_PLANE; ++i)
      av1_free_restoration_struct(&cpi->rst_search[i]);
  }
Debargha Mukherjee's avatar
Debargha Mukherjee committed
491
#endif  // CONFIG_LOOP_RESTORATION
Yaowu Xu's avatar
Yaowu Xu committed
492 493 494 495
  aom_free_frame_buffer(&cpi->scaled_source);
  aom_free_frame_buffer(&cpi->scaled_last_source);
  aom_free_frame_buffer(&cpi->alt_ref_buffer);
  av1_lookahead_destroy(cpi->lookahead);
Jingning Han's avatar
Jingning Han committed
496

Yaowu Xu's avatar
Yaowu Xu committed
497
  aom_free(cpi->tile_tok[0][0]);
Jingning Han's avatar
Jingning Han committed
498 499
  cpi->tile_tok[0][0] = 0;

Yaowu Xu's avatar
Yaowu Xu committed
500
  av1_free_pc_tree(&cpi->td);
501

502
#if CONFIG_PALETTE
503
  aom_free(cpi->td.mb.palette_buffer);
504
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
505

506
#if CONFIG_ANS
Alex Converse's avatar
Alex Converse committed
507
  aom_buf_ans_free(&cpi->buf_ans);
508
#endif  // CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
509 510
}

Yaowu Xu's avatar
Yaowu Xu committed
511
static void save_coding_context(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
512
  CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xu's avatar
Yaowu Xu committed
513
  AV1_COMMON *cm = &cpi->common;
514
  int i;
Jingning Han's avatar
Jingning Han committed
515

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
516 517 518 519
  // Stores a snapshot of key state variables which can subsequently be
  // restored with a call to av1_restore_coding_context. These functions are
  // intended for use in a re-code loop in av1_compress_frame where the
  // quantizer value is adjusted between loop iterations.
520
  for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
521
    av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
522 523
    av1_copy(cc->nmv_costs, cpi->nmv_costs);
    av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
524
  }
Jingning Han's avatar
Jingning Han committed
525

Yaowu Xu's avatar
Yaowu Xu committed
526 527
  av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
  av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Jingning Han's avatar
Jingning Han committed
528 529 530 531

  cc->fc = *cm->fc;
}

Yaowu Xu's avatar
Yaowu Xu committed
532
static void restore_coding_context(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
533
  CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xu's avatar
Yaowu Xu committed
534
  AV1_COMMON *cm = &cpi->common;
535
  int i;
Jingning Han's avatar
Jingning Han committed
536

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
537 538
  // Restore key state variables to the snapshot state stored in the
  // previous call to av1_save_coding_context.
539
  for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
540
    av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
541 542
    av1_copy(cpi->nmv_costs, cc->nmv_costs);
    av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
543
  }
Jingning Han's avatar
Jingning Han committed
544

Yaowu Xu's avatar
Yaowu Xu committed
545 546
  av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
  av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Jingning Han's avatar
Jingning Han committed
547 548 549 550

  *cm->fc = cc->fc;
}

Yaowu Xu's avatar
Yaowu Xu committed
551 552
static void configure_static_seg_features(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
  const RATE_CONTROL *const rc = &cpi->rc;
  struct segmentation *const seg = &cm->seg;

  int high_q = (int)(rc->avg_q > 48.0);
  int qi_delta;

  // Disable and clear down for KF
  if (cm->frame_type == KEY_FRAME) {
    // Clear down the global segmentation map
    memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    seg->update_map = 0;
    seg->update_data = 0;
    cpi->static_mb_pct = 0;

    // Disable segmentation
Yaowu Xu's avatar
Yaowu Xu committed
568
    av1_disable_segmentation(seg);
Jingning Han's avatar
Jingning Han committed
569 570

    // Clear down the segment features.
Yaowu Xu's avatar
Yaowu Xu committed
571
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
572 573 574 575 576 577 578 579 580
  } else if (cpi->refresh_alt_ref_frame) {
    // If this is an alt ref frame
    // Clear down the global segmentation map
    memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
    seg->update_map = 0;
    seg->update_data = 0;
    cpi->static_mb_pct = 0;

    // Disable segmentation and individual segment features by default
Yaowu Xu's avatar
Yaowu Xu committed
581 582
    av1_disable_segmentation(seg);
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
583 584 585

    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
Yaowu Xu's avatar
Yaowu Xu committed
586
    av1_update_mbgraph_stats(cpi);
Jingning Han's avatar
Jingning Han committed
587 588 589 590 591 592 593

    // If segmentation was enabled set those features needed for the
    // arf itself.
    if (seg->enabled) {
      seg->update_map = 1;
      seg->update_data = 1;

594
      qi_delta =
Yaowu Xu's avatar
Yaowu Xu committed
595 596 597
          av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
      av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
      av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Jingning Han's avatar
Jingning Han committed
598

Yaowu Xu's avatar
Yaowu Xu committed
599 600
      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Jingning Han's avatar
Jingning Han committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

      // Where relevant assume segment data is delta data
      seg->abs_delta = SEGMENT_DELTADATA;
    }
  } else if (seg->enabled) {
    // All other frames if segmentation has been enabled

    // First normal frame in a valid gf or alt ref group
    if (rc->frames_since_golden == 0) {
      // Set up segment features for normal frames in an arf group
      if (rc->source_alt_ref_active) {
        seg->update_map = 0;
        seg->update_data = 1;
        seg->abs_delta = SEGMENT_DELTADATA;

Yaowu Xu's avatar
Yaowu Xu committed
616 617 618 619
        qi_delta =
            av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
        av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
        av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Jingning Han's avatar
Jingning Han committed
620

Yaowu Xu's avatar
Yaowu Xu committed
621 622
        av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
        av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Jingning Han's avatar
Jingning Han committed
623 624 625

        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xu's avatar
Yaowu Xu committed
626 627 628
          av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
          av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
          av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Jingning Han's avatar
Jingning Han committed
629 630 631 632 633
        }
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

Yaowu Xu's avatar
Yaowu Xu committed
634
        av1_disable_segmentation(seg);
Jingning Han's avatar
Jingning Han committed
635 636 637 638 639 640

        memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);

        seg->update_map = 0;
        seg->update_data = 0;

Yaowu Xu's avatar
Yaowu Xu committed
641
        av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
642 643 644 645 646 647 648
      }
    } else if (rc->is_src_frame_alt_ref) {
      // Special case where we are coding over the top of a previous
      // alt ref frame.
      // Segment coding disabled for compred testing

      // Enable ref frame features for segment 0 as well
Yaowu Xu's avatar
Yaowu Xu committed
649 650
      av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
      av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Jingning Han's avatar
Jingning Han committed
651 652

      // All mbs should use ALTREF_FRAME
Yaowu Xu's avatar
Yaowu Xu committed
653 654 655 656
      av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
      av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
      av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
      av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Jingning Han's avatar
Jingning Han committed
657 658 659

      // Skip all MBs if high Q (0,0 mv and skip coeffs)
      if (high_q) {
Yaowu Xu's avatar
Yaowu Xu committed
660 661
        av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Jingning Han's avatar
Jingning Han committed
662 663 664 665 666 667 668 669 670 671 672 673 674
      }
      // Enable data update
      seg->update_data = 1;
    } else {
      // All other frames.

      // No updates.. leave things as they are.
      seg->update_map = 0;
      seg->update_data = 0;
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
675 676
static void update_reference_segmentation_map(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690
  MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
  uint8_t *cache_ptr = cm->last_frame_seg_map;
  int row, col;

  for (row = 0; row < cm->mi_rows; row++) {
    MODE_INFO **mi_8x8 = mi_8x8_ptr;
    uint8_t *cache = cache_ptr;
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
      cache[0] = mi_8x8[0]->mbmi.segment_id;
    mi_8x8_ptr += cm->mi_stride;
    cache_ptr += cm->mi_cols;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
691 692 693
static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
  AV1_COMMON *cm = &cpi->common;
  const AV1EncoderConfig *oxcf = &cpi->oxcf;
Jingning Han's avatar
Jingning Han committed
694 695

  if (!cpi->lookahead)
Yaowu Xu's avatar
Yaowu Xu committed
696 697
    cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
                                        cm->subsampling_x, cm->subsampling_y,
698
#if CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
699
                                        cm->use_highbitdepth,
Jingning Han's avatar
Jingning Han committed
700
#endif
Yaowu Xu's avatar
Yaowu Xu committed
701
                                        oxcf->lag_in_frames);
Jingning Han's avatar
Jingning Han committed
702
  if (!cpi->lookahead)
Yaowu Xu's avatar
Yaowu Xu committed
703
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
704 705 706
                       "Failed to allocate lag buffers");

  // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xu's avatar
Yaowu Xu committed
707
  if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Jingning Han's avatar
Jingning Han committed
708
                               cm->subsampling_x, cm->subsampling_y,
709
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
710 711
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
712 713
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
714
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
715 716 717
                       "Failed to allocate altref buffer");
}

Yaowu Xu's avatar
Yaowu Xu committed
718 719 720
static void alloc_util_frame_buffers(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
  if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Jingning Han's avatar
Jingning Han committed
721
                               cm->subsampling_x, cm->subsampling_y,
722
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
723 724
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
725 726
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
727
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
728 729
                       "Failed to allocate last frame buffer");

Debargha Mukherjee's avatar
Debargha Mukherjee committed
730
#if CONFIG_LOOP_RESTORATION
Yaowu Xu's avatar
Yaowu Xu committed
731
  if (aom_realloc_frame_buffer(&cpi->last_frame_db, cm->width, cm->height,
Debargha Mukherjee's avatar
Debargha Mukherjee committed
732
                               cm->subsampling_x, cm->subsampling_y,
733
#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee's avatar
Debargha Mukherjee committed
734 735
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
736 737
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
738
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee's avatar
Debargha Mukherjee committed
739
                       "Failed to allocate last frame deblocked buffer");
740 741 742 743 744 745 746 747
  if (aom_realloc_frame_buffer(
          &cpi->trial_frame_rst,
#if CONFIG_FRAME_SUPERRES
          cm->superres_upscaled_width, cm->superres_upscaled_height,
#else
          cm->width, cm->height,
#endif  // CONFIG_FRAME_SUPERRES
          cm->subsampling_x, cm->subsampling_y,
748
#if CONFIG_HIGHBITDEPTH
749
          cm->use_highbitdepth,
750
#endif
751
          AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
752 753
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
                       "Failed to allocate trial restored frame buffer");
754
  int extra_rstbuf_sz = RESTORATION_EXTBUF_SIZE;
755
  if (extra_rstbuf_sz > 0) {
Alex Converse's avatar
Alex Converse committed
756
    aom_free(cpi->extra_rstbuf);
757
    CHECK_MEM_ERROR(cm, cpi->extra_rstbuf,
Alex Converse's avatar
Alex Converse committed
758
                    (uint8_t *)aom_malloc(extra_rstbuf_sz));
759 760 761
  } else {
    cpi->extra_rstbuf = NULL;
  }
Debargha Mukherjee's avatar
Debargha Mukherjee committed
762 763
#endif  // CONFIG_LOOP_RESTORATION

Yaowu Xu's avatar
Yaowu Xu committed
764
  if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Jingning Han's avatar
Jingning Han committed
765
                               cm->subsampling_x, cm->subsampling_y,
766
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
767 768
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
769 770
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
771
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
772 773
                       "Failed to allocate scaled source buffer");

Yaowu Xu's avatar
Yaowu Xu committed
774
  if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Jingning Han's avatar
Jingning Han committed
775
                               cm->subsampling_x, cm->subsampling_y,
776
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
777 778
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
779 780
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
781
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
782 783 784
                       "Failed to allocate scaled last source buffer");
}

785
static void alloc_context_buffers_ext(AV1_COMP *cpi) {
Yaowu Xu's avatar
Yaowu Xu committed
786
  AV1_COMMON *cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
787 788
  int mi_size = cm->mi_cols * cm->mi_rows;

789 790
  CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
                  aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
Jingning Han's avatar
Jingning Han committed
791 792
}

Yaowu Xu's avatar
Yaowu Xu committed
793 794
void av1_alloc_compressor_data(AV1_COMP *cpi) {
  AV1_COMMON *cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
795

Yaowu Xu's avatar
Yaowu Xu committed
796
  av1_alloc_context_buffers(cm, cm->width, cm->height);
Jingning Han's avatar
Jingning Han committed
797

798 799 800 801
#if CONFIG_LV_MAP
  av1_alloc_txb_buf(cpi);
#endif

Jingning Han's avatar
Jingning Han committed
802 803
  alloc_context_buffers_ext(cpi);

Yaowu Xu's avatar
Yaowu Xu committed
804
  aom_free(cpi->tile_tok[0][0]);
Jingning Han's avatar
Jingning Han committed
805 806 807 808

  {
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
    CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xu's avatar
Yaowu Xu committed
809
                    aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
810 811
#if CONFIG_ANS && !ANS_MAX_SYMBOLS
    aom_buf_ans_alloc(&cpi->buf_ans, &cm->error, (int)tokens);
812
#endif  // CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
813 814
  }

Yaowu Xu's avatar
Yaowu Xu committed
815
  av1_setup_pc_tree(&cpi->common, &cpi->td);
Jingning Han's avatar
Jingning Han committed
816 817
}

Yaowu Xu's avatar
Yaowu Xu committed
818
void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Jingning Han's avatar
Jingning Han committed
819
  cpi->framerate = framerate < 0.1 ? 30 : framerate;
820 821 822 823 824
#if CONFIG_XIPHRC
  if (!cpi->od_rc.cur_frame) return;
  cpi->od_rc.framerate = cpi->framerate;
  od_enc_rc_resize(&cpi->od_rc);
#else
Yaowu Xu's avatar
Yaowu Xu committed
825
  av1_rc_update_framerate(cpi);
826
#endif
Jingning Han's avatar
Jingning Han committed
827 828
}

Yaowu Xu's avatar
Yaowu Xu committed
829 830
static void set_tile_info(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
831 832 833 834
#if CONFIG_TILE_GROUPS && CONFIG_DEPENDENT_HORZTILES
  int tile_row, tile_col, num_tiles_in_tg;
  int tg_row_start, tg_col_start;
#endif
835
#if CONFIG_EXT_TILE
836
  if (cpi->oxcf.large_scale_tile) {
837
#if CONFIG_EXT_PARTITION
838 839 840 841 842 843 844 845 846 847 848 849
    if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
      cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
      cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
      cm->tile_width <<= MAX_MIB_SIZE_LOG2;
      cm->tile_height <<= MAX_MIB_SIZE_LOG2;
    } else {
      cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
      cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
      cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
      cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
    }
#else
850
    cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
851
    cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
852 853
    cm->tile_width <<= MAX_MIB_SIZE_LOG2;
    cm->tile_height <<= MAX_MIB_SIZE_LOG2;
854
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
855

856 857
    cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
    cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
858

859 860
    assert(cm->tile_width >> MAX_MIB_SIZE <= 32);
    assert(cm->tile_height >> MAX_MIB_SIZE <= 32);
861

862 863 864
    // Get the number of tiles
    cm->tile_cols = 1;
    while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
865

866 867 868 869 870 871
    cm->tile_rows = 1;
    while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
  } else {
#endif  // CONFIG_EXT_TILE
    int min_log2_tile_cols, max_log2_tile_cols;
    av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Jingning Han's avatar
Jingning Han committed
872

873 874 875
    cm->log2_tile_cols =
        clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
    cm->log2_tile_rows = cpi->oxcf.tile_rows;
876

877 878
    cm->tile_cols = 1 << cm->log2_tile_cols;
    cm->tile_rows = 1 << cm->log2_tile_rows;
879

880 881 882 883
    cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
    cm->tile_width >>= cm->log2_tile_cols;
    cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
    cm->tile_height >>= cm->log2_tile_rows;
884

885 886 887 888 889
    // round to integer multiples of max superblock size
    cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
    cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
#if CONFIG_EXT_TILE
  }
890
#endif  // CONFIG_EXT_TILE
891

892 893
#if CONFIG_DEPENDENT_HORZTILES
  cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
894
#if CONFIG_EXT_TILE
895 896 897
  if (cm->large_scale_tile) {
    // May not needed since cpi->oxcf.dependent_horz_tiles is already adjusted.
    cm->dependent_horz_tiles = 0;
898
  } else {
899 900 901
#endif  // CONFIG_EXT_TILE
    if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
#if CONFIG_EXT_TILE
902
  }
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
#endif  // CONFIG_EXT_TILE

#if CONFIG_TILE_GROUPS
#if CONFIG_EXT_TILE
  if (!cm->large_scale_tile) {
#endif  // CONFIG_EXT_TILE
    if (cpi->oxcf.mtu == 0) {
      cm->num_tg = cpi->oxcf.num_tile_groups;
    } else {
      // Use a default value for the purposes of weighting costs in probability
      // updates
      cm->num_tg = DEFAULT_MAX_NUM_TG;
    }
    num_tiles_in_tg =
        (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
    tg_row_start = 0;
    tg_col_start = 0;
    for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
      for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
        if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
          tg_row_start = tile_row;
          tg_col_start = tile_col;
        }
        cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
        cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
928 929
      }
    }
930
#if CONFIG_EXT_TILE
931
  }
932
#endif  // CONFIG_EXT_TILE
933
#endif
934 935
#endif

936
#if CONFIG_LOOPFILTERING_ACROSS_TILES
937 938
  cm->loop_filter_across_tiles_enabled =
      cpi->oxcf.loop_filter_across_tiles_enabled;
939
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
Jingning Han's avatar
Jingning Han committed
940 941
}

Yaowu Xu's avatar
Yaowu Xu committed
942 943
static void update_frame_size(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
944 945
  MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;

Yaowu Xu's avatar
Yaowu Xu committed
946 947
  av1_set_mb_mi(cm, cm->width, cm->height);
  av1_init_context_buffers(cm);
948 949 950
  av1_init_macroblockd(cm, xd,
#if CONFIG_PVQ
                       NULL,
Luc Trudeau's avatar
Luc Trudeau committed
951 952 953
#endif
#if CONFIG_CFL
                       &NULL_CFL,
954 955
#endif
                       NULL);
Jingning Han's avatar
Jingning Han committed
956 957 958
  memset(cpi->mbmi_ext_base, 0,
         cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));

959
  set_tile_info(cpi);
Jingning Han's avatar
Jingning Han committed
960 961
}

Yaowu Xu's avatar
Yaowu Xu committed
962
static void init_buffer_indices(AV1_COMP *cpi) {
963
#if CONFIG_EXT_REFS
964 965 966 967
  int fb_idx;
  for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
    cpi->lst_fb_idxes[fb_idx] = fb_idx;
  cpi->gld_fb_idx = LAST_REF_FRAMES;
968 969
  cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
  cpi->alt_fb_idx = LAST_REF_FRAMES + 2;
970 971
  for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
    cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
972
#else
973
  cpi->lst_fb_idx = 0;