encoder.c 246 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
#endif  // CONFIG_CDEF
22 23 24 25
#include "av1/common/filter.h"
#include "av1/common/idct.h"
#include "av1/common/reconinter.h"
#include "av1/common/reconintra.h"
26
#include "av1/common/resize.h"
27
#include "av1/common/tile_common.h"
Jingning Han's avatar
Jingning Han committed
28

29 30 31 32
#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
33 34 35
#if CONFIG_BGSPRITE
#include "av1/encoder/bgsprite.h"
#endif  // CONFIG_BGSPRITE
36
#if CONFIG_ANS
Alex Converse's avatar
Alex Converse committed
37
#include "aom_dsp/buf_ans.h"
38
#endif
39 40 41 42
#include "av1/encoder/context_tree.h"
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/encoder.h"
43 44 45
#if CONFIG_LV_MAP
#include "av1/encoder/encodetxb.h"
#endif
46 47
#include "av1/encoder/ethread.h"
#include "av1/encoder/firstpass.h"
48 49 50
#if CONFIG_HASH_ME
#include "av1/encoder/hash_motion.h"
#endif
51
#include "av1/encoder/mbgraph.h"
52 53 54
#if CONFIG_NCOBMC_ADAPT_WEIGHT
#include "av1/common/ncobmc_kernels.h"
#endif  // CONFIG_NCOBMC_ADAPT_WEIGHT
55
#include "av1/encoder/picklpf.h"
56
#if CONFIG_LOOP_RESTORATION
57
#include "av1/encoder/pickrst.h"
58
#endif  // CONFIG_LOOP_RESTORATION
59
#include "av1/encoder/random.h"
60 61 62 63 64
#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
65

Yaowu Xu's avatar
Yaowu Xu committed
66 67 68
#include "./av1_rtcd.h"
#include "./aom_dsp_rtcd.h"
#include "./aom_scale_rtcd.h"
69
#include "aom_dsp/psnr.h"
70
#if CONFIG_INTERNAL_STATS
71
#include "aom_dsp/ssim.h"
72
#endif
Yaowu Xu's avatar
Yaowu Xu committed
73 74
#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/aom_filter.h"
Jingning Han's avatar
Jingning Han committed
75
#include "aom_ports/aom_timer.h"
76 77
#include "aom_ports/mem.h"
#include "aom_ports/system_state.h"
Yaowu Xu's avatar
Yaowu Xu committed
78
#include "aom_scale/aom_scale.h"
79
#if CONFIG_BITSTREAM_DEBUG
80
#include "aom_util/debug_util.h"
81
#endif  // CONFIG_BITSTREAM_DEBUG
82

83 84
#if CONFIG_ENTROPY_STATS
FRAME_COUNTS aggregate_fc;
85 86
// Aggregate frame counts per frame context type
FRAME_COUNTS aggregate_fc_per_type[FRAME_CONTEXTS];
87 88
#endif  // CONFIG_ENTROPY_STATS

Jingning Han's avatar
Jingning Han committed
89 90 91
#define AM_SEGMENT_ID_INACTIVE 7
#define AM_SEGMENT_ID_ACTIVE 0

92 93 94 95 96 97 98 99
#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.
100

Jingning Han's avatar
Jingning Han committed
101 102 103 104 105 106 107 108 109
// #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;
110
#define FILE_NAME_LEN 100
Jingning Han's avatar
Jingning Han committed
111 112 113 114 115 116 117 118
#endif

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

Luc Trudeau's avatar
Luc Trudeau committed
119 120 121 122
#if CONFIG_CFL
CFL_CTX NULL_CFL;
#endif

123 124 125 126
#if CONFIG_INTERNAL_STATS
typedef enum { Y, U, V, ALL } STAT_TYPE;
#endif  // CONFIG_INTERNAL_STATS

Yaowu Xu's avatar
Yaowu Xu committed
127
static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Jingning Han's avatar
Jingning Han committed
128 129 130 131 132 133 134 135 136 137 138 139
  switch (mode) {
    case NORMAL:
      *hr = 1;
      *hs = 1;
      break;
    case FOURFIVE:
      *hr = 4;
      *hs = 5;
      break;
    case THREEFIVE:
      *hr = 3;
      *hs = 5;
140
      break;
Jingning Han's avatar
Jingning Han committed
141 142 143
    case ONETWO:
      *hr = 1;
      *hs = 2;
144
      break;
Jingning Han's avatar
Jingning Han committed
145 146 147
    default:
      *hr = 1;
      *hs = 1;
148
      assert(0);
Jingning Han's avatar
Jingning Han committed
149 150 151 152 153 154
      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
155
static void suppress_active_map(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
156 157 158 159 160 161 162 163
  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
164
static void apply_active_map(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  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
181 182
      av1_enable_segmentation(seg);
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
#if CONFIG_LOOPFILTER_LEVEL
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);

      av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
                      -MAX_LOOP_FILTER);
      av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
                      -MAX_LOOP_FILTER);
      av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
                      -MAX_LOOP_FILTER);
      av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
                      -MAX_LOOP_FILTER);
#else
Yaowu Xu's avatar
Yaowu Xu committed
198
      av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Jingning Han's avatar
Jingning Han committed
199 200
      // 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
201 202
      av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
                      -MAX_LOOP_FILTER);
203
#endif  // CONFIG_LOOPFILTER_LEVEL
Jingning Han's avatar
Jingning Han committed
204
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
205
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
206 207 208 209 210 211
#if CONFIG_LOOPFILTER_LEVEL
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
#else
Yaowu Xu's avatar
Yaowu Xu committed
212
      av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
213
#endif  // CONFIG_LOOPFILTER_LEVEL
Jingning Han's avatar
Jingning Han committed
214 215 216 217 218 219 220 221 222
      if (seg->enabled) {
        seg->update_data = 1;
        seg->update_map = 1;
      }
    }
    cpi->active_map.update = 0;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
223 224
int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
                       int cols) {
Jingning Han's avatar
Jingning Han committed
225 226 227 228
  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;
229 230
    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
231 232 233 234 235 236
    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] =
237
              new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Jingning Han's avatar
Jingning Han committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251
                  ? 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
252 253
int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
                       int cols) {
Jingning Han's avatar
Jingning Han committed
254 255
  if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
      new_map_16x16) {
256
    unsigned char *const seg_map_8x8 = cpi->segmentation_map;
Jingning Han's avatar
Jingning Han committed
257 258
    const int mi_rows = cpi->common.mi_rows;
    const int mi_cols = cpi->common.mi_cols;
259 260 261
    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
262 263 264 265 266 267 268
    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
269
          new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Jingning Han's avatar
Jingning Han committed
270 271 272 273 274 275 276 277 278 279
              seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
        }
      }
    }
    return 0;
  } else {
    return -1;
  }
}

RogerZhou's avatar
RogerZhou committed
280 281 282
static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv
#if CONFIG_AMVR
                                  ,
283
                                  int cur_frame_force_integer_mv
RogerZhou's avatar
RogerZhou committed
284 285
#endif
                                  ) {
286 287 288
  MACROBLOCK *const mb = &cpi->td.mb;
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;

RogerZhou's avatar
RogerZhou committed
289
#if CONFIG_AMVR
290
  if (cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0) {
RogerZhou's avatar
RogerZhou committed
291
#else
292
  if (cpi->common.allow_high_precision_mv) {
RogerZhou's avatar
RogerZhou committed
293
#endif
294 295 296 297 298 299 300 301 302 303 304 305
    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];
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
306
static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
307
#if CONFIG_EXT_PARTITION
Yaowu Xu's avatar
Yaowu Xu committed
308
  if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
309 310
    return BLOCK_64X64;

Yaowu Xu's avatar
Yaowu Xu committed
311
  if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
312 313
    return BLOCK_128X128;

Yaowu Xu's avatar
Yaowu Xu committed
314
  assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
315 316 317 318 319 320 321 322 323 324 325 326 327 328

  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
329 330
static void setup_frame(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
331 332 333 334 335 336
  // 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
337
    av1_setup_past_independence(cm);
Jingning Han's avatar
Jingning Han committed
338
  } else {
339 340 341 342
#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
// Just use frame context from first signaled reference frame.
// This will always be LAST_FRAME for now.
#else
343
    const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liu's avatar
Zoe Liu committed
344
    if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
345 346 347
      cm->frame_context_idx = EXT_ARF_FRAME;
    else if (cpi->refresh_alt_ref_frame)
      cm->frame_context_idx = ARF_FRAME;
348 349 350 351 352 353 354 355
    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;
    else if (cpi->refresh_bwd_ref_frame)
      cm->frame_context_idx = BRF_FRAME;
    else
      cm->frame_context_idx = REGULAR_FRAME;
356
#endif  // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Jingning Han's avatar
Jingning Han committed
357 358 359
  }

  if (cm->frame_type == KEY_FRAME) {
Yunqing Wang's avatar
Yunqing Wang committed
360
    cpi->refresh_golden_frame = 1;
Jingning Han's avatar
Jingning Han committed
361
    cpi->refresh_alt_ref_frame = 1;
Yaowu Xu's avatar
Yaowu Xu committed
362
    av1_zero(cpi->interp_filter_selected);
363
    set_sb_size(cm, select_sb_size(cpi));
364 365 366
#if CONFIG_REFERENCE_BUFFER
    set_use_reference_buffer(cm, 0);
#endif  // CONFIG_REFERENCE_BUFFER
Jingning Han's avatar
Jingning Han committed
367
  } else {
368 369 370 371 372 373 374 375
#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
    if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
        cm->frame_refs[0].idx < 0) {
      *cm->fc = cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
    } else {
      *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
    }
#else
Jingning Han's avatar
Jingning Han committed
376
    *cm->fc = cm->frame_contexts[cm->frame_context_idx];
377
#endif  // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xu's avatar
Yaowu Xu committed
378
    av1_zero(cpi->interp_filter_selected[0]);
Jingning Han's avatar
Jingning Han committed
379
  }
380 381
#if CONFIG_ONE_SIDED_COMPOUND && \
    !CONFIG_EXT_COMP_REFS  // No change to bitstream
382 383 384 385
  if (cpi->sf.recode_loop == DISALLOW_RECODE) {
    cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
    cpi->rc.is_bipred_frame = 1;
  }
386
#endif  // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
387
#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
388 389
  if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
      cm->frame_refs[0].idx < 0) {
390 391 392
    // use default frame context values
    cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
  } else {
393
    *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
394 395 396
    cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
  }
#else
397
  cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
398
#endif  // CONFIG_NO_FRAME_CONTEXT_SIGNALING
399

Geza Lore's avatar
Geza Lore committed
400
  cpi->vaq_refresh = 0;
Jingning Han's avatar
Jingning Han committed
401 402
}

403
static void enc_setup_mi(AV1_COMMON *cm) {
Jingning Han's avatar
Jingning Han committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  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));
}

420
static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xu's avatar
Yaowu Xu committed
421
  cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
422
  if (!cm->mip) return 1;
Yaowu Xu's avatar
Yaowu Xu committed
423
  cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
424
  if (!cm->prev_mip) return 1;
Jingning Han's avatar
Jingning Han committed
425 426
  cm->mi_alloc_size = mi_size;

Yaowu Xu's avatar
Yaowu Xu committed
427
  cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
428 429
  if (!cm->mi_grid_base) return 1;
  cm->prev_mi_grid_base =
Yaowu Xu's avatar
Yaowu Xu committed
430
      (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
431
  if (!cm->prev_mi_grid_base) return 1;
Jingning Han's avatar
Jingning Han committed
432 433 434 435

  return 0;
}

436
static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xu's avatar
Yaowu Xu committed
437
  aom_free(cm->mip);
Jingning Han's avatar
Jingning Han committed
438
  cm->mip = NULL;
Yaowu Xu's avatar
Yaowu Xu committed
439
  aom_free(cm->prev_mip);
Jingning Han's avatar
Jingning Han committed
440
  cm->prev_mip = NULL;
Yaowu Xu's avatar
Yaowu Xu committed
441
  aom_free(cm->mi_grid_base);
Jingning Han's avatar
Jingning Han committed
442
  cm->mi_grid_base = NULL;
Yaowu Xu's avatar
Yaowu Xu committed
443
  aom_free(cm->prev_mi_grid_base);
Jingning Han's avatar
Jingning Han committed
444
  cm->prev_mi_grid_base = NULL;
445
  cm->mi_alloc_size = 0;
Jingning Han's avatar
Jingning Han committed
446 447
}

448
static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Jingning Han's avatar
Jingning Han committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  // 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
465
void av1_initialize_enc(void) {
Jingning Han's avatar
Jingning Han committed
466 467 468
  static volatile int init_done = 0;

  if (!init_done) {
Yaowu Xu's avatar
Yaowu Xu committed
469 470 471 472 473
    av1_rtcd();
    aom_dsp_rtcd();
    aom_scale_rtcd();
    av1_init_intra_predictors();
    av1_init_me_luts();
474
#if !CONFIG_XIPHRC
Yaowu Xu's avatar
Yaowu Xu committed
475
    av1_rc_init_minq_luts();
476
#endif
Yaowu Xu's avatar
Yaowu Xu committed
477 478 479
    av1_entropy_mv_init();
    av1_encode_token_init();
    av1_init_wedge_masks();
Jingning Han's avatar
Jingning Han committed
480 481 482 483
    init_done = 1;
  }
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
  if (cpi->mbmi_ext_base) {
    aom_free(cpi->mbmi_ext_base);
    cpi->mbmi_ext_base = NULL;
  }
}

static void alloc_context_buffers_ext(AV1_COMP *cpi) {
  AV1_COMMON *cm = &cpi->common;
  int mi_size = cm->mi_cols * cm->mi_rows;

  dealloc_context_buffers_ext(cpi);
  CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
                  aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
}

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

503
  dealloc_context_buffers_ext(cpi);
Jingning Han's avatar
Jingning Han committed
504

Yaowu Xu's avatar
Yaowu Xu committed
505
  aom_free(cpi->tile_data);
Jingning Han's avatar
Jingning Han committed
506 507 508
  cpi->tile_data = NULL;

  // Delete sementation map
Yaowu Xu's avatar
Yaowu Xu committed
509
  aom_free(cpi->segmentation_map);
Jingning Han's avatar
Jingning Han committed
510 511
  cpi->segmentation_map = NULL;

Yaowu Xu's avatar
Yaowu Xu committed
512
  av1_cyclic_refresh_free(cpi->cyclic_refresh);
Jingning Han's avatar
Jingning Han committed
513 514
  cpi->cyclic_refresh = NULL;

Yaowu Xu's avatar
Yaowu Xu committed
515
  aom_free(cpi->active_map.map);
Jingning Han's avatar
Jingning Han committed
516 517
  cpi->active_map.map = NULL;

518 519 520 521 522 523 524 525 526 527 528 529
  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;

Jingning Han's avatar
Jingning Han committed
530 531 532 533 534
#if CONFIG_MFMV
  aom_free(cm->tpl_mvs);
  cm->tpl_mvs = NULL;
#endif

Yaowu Xu's avatar
Yaowu Xu committed
535
  av1_free_ref_frame_buffers(cm->buffer_pool);
536 537 538
#if CONFIG_LV_MAP
  av1_free_txb_buf(cpi);
#endif
Yaowu Xu's avatar
Yaowu Xu committed
539
  av1_free_context_buffers(cm);
Jingning Han's avatar
Jingning Han committed
540

Yaowu Xu's avatar
Yaowu Xu committed
541
  aom_free_frame_buffer(&cpi->last_frame_uf);
Debargha Mukherjee's avatar
Debargha Mukherjee committed
542
#if CONFIG_LOOP_RESTORATION
Yaowu Xu's avatar
Yaowu Xu committed
543
  av1_free_restoration_buffers(cm);
544
  aom_free_frame_buffer(&cpi->trial_frame_rst);
Debargha Mukherjee's avatar
Debargha Mukherjee committed
545
#endif  // CONFIG_LOOP_RESTORATION
Yaowu Xu's avatar
Yaowu Xu committed
546 547 548 549
  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
550

Yaowu Xu's avatar
Yaowu Xu committed
551
  aom_free(cpi->tile_tok[0][0]);
Jingning Han's avatar
Jingning Han committed
552 553
  cpi->tile_tok[0][0] = 0;

Yaowu Xu's avatar
Yaowu Xu committed
554
  av1_free_pc_tree(&cpi->td);
555

556
  aom_free(cpi->td.mb.palette_buffer);
hui su's avatar
hui su committed
557

558
#if CONFIG_ANS
Alex Converse's avatar
Alex Converse committed
559
  aom_buf_ans_free(&cpi->buf_ans);
560
#endif  // CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
561 562
}

Yaowu Xu's avatar
Yaowu Xu committed
563
static void save_coding_context(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
564
  CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xu's avatar
Yaowu Xu committed
565
  AV1_COMMON *cm = &cpi->common;
566
  int i;
Jingning Han's avatar
Jingning Han committed
567

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
568 569 570 571
  // 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.
572 573 574 575 576 577
  for (i = 0; i < NMV_CONTEXTS; ++i) {
    av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
    av1_copy(cc->nmv_costs, cpi->nmv_costs);
    av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
  }

Yaowu Xu's avatar
Yaowu Xu committed
578 579
  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
580 581 582 583

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

Yaowu Xu's avatar
Yaowu Xu committed
584
static void restore_coding_context(AV1_COMP *cpi) {
Jingning Han's avatar
Jingning Han committed
585
  CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xu's avatar
Yaowu Xu committed
586
  AV1_COMMON *cm = &cpi->common;
587
  int i;
Jingning Han's avatar
Jingning Han committed
588

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
589 590
  // Restore key state variables to the snapshot state stored in the
  // previous call to av1_save_coding_context.
591 592 593 594 595 596
  for (i = 0; i < NMV_CONTEXTS; ++i) {
    av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
    av1_copy(cpi->nmv_costs, cc->nmv_costs);
    av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
  }

Yaowu Xu's avatar
Yaowu Xu committed
597 598
  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
599 600 601 602

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

Yaowu Xu's avatar
Yaowu Xu committed
603 604
static void configure_static_seg_features(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  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
620
    av1_disable_segmentation(seg);
Jingning Han's avatar
Jingning Han committed
621 622

    // Clear down the segment features.
Yaowu Xu's avatar
Yaowu Xu committed
623
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
624 625 626 627 628 629 630 631 632
  } 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
633 634
    av1_disable_segmentation(seg);
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
635 636 637

    // Scan frames from current to arf frame.
    // This function re-enables segmentation if appropriate.
Yaowu Xu's avatar
Yaowu Xu committed
638
    av1_update_mbgraph_stats(cpi);
Jingning Han's avatar
Jingning Han committed
639 640 641 642 643 644 645

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

646
      qi_delta =
Yaowu Xu's avatar
Yaowu Xu committed
647 648
          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);
649 650 651 652 653 654 655 656 657 658 659
#if CONFIG_LOOPFILTER_LEVEL
      av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
      av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
      av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
      av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);

      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
#else
Yaowu Xu's avatar
Yaowu Xu committed
660
      av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
661 662
      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
#endif  // CONFIG_LOOPFILTER_LEVEL
Jingning Han's avatar
Jingning Han committed
663

Yaowu Xu's avatar
Yaowu Xu committed
664
      av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Jingning Han's avatar
Jingning Han committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

      // 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
680 681 682 683
        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
684

685 686 687 688 689 690 691 692 693 694 695
#if CONFIG_LOOPFILTER_LEVEL
        av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
        av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
        av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
        av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);

        av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
        av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
        av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
        av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
#else
Yaowu Xu's avatar
Yaowu Xu committed
696 697
        av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
        av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
698
#endif  // CONFIG_LOOPFILTER_LEVEL
Jingning Han's avatar
Jingning Han committed
699 700 701

        // Segment coding disabled for compred testing
        if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xu's avatar
Yaowu Xu committed
702 703 704
          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
705 706 707 708 709
        }
      } else {
        // Disable segmentation and clear down features if alt ref
        // is not active for this group

Yaowu Xu's avatar
Yaowu Xu committed
710
        av1_disable_segmentation(seg);
Jingning Han's avatar
Jingning Han committed
711 712 713 714 715 716

        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
717
        av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
718 719 720 721 722 723 724
      }
    } 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
725 726
      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
727 728

      // All mbs should use ALTREF_FRAME
Yaowu Xu's avatar
Yaowu Xu committed
729 730 731 732
      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
733 734 735

      // Skip all MBs if high Q (0,0 mv and skip coeffs)
      if (high_q) {
Yaowu Xu's avatar
Yaowu Xu committed
736 737
        av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
        av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Jingning Han's avatar
Jingning Han committed
738 739 740 741 742 743 744 745 746 747 748 749 750
      }
      // 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
751 752
static void update_reference_segmentation_map(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766
  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
767 768 769
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
770 771

  if (!cpi->lookahead)
Yaowu Xu's avatar
Yaowu Xu committed
772 773
    cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
                                        cm->subsampling_x, cm->subsampling_y,
774
#if CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
775
                                        cm->use_highbitdepth,
Jingning Han's avatar
Jingning Han committed
776
#endif
Yaowu Xu's avatar
Yaowu Xu committed
777
                                        oxcf->lag_in_frames);
Jingning Han's avatar
Jingning Han committed
778
  if (!cpi->lookahead)
Yaowu Xu's avatar
Yaowu Xu committed
779
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
780 781 782
                       "Failed to allocate lag buffers");

  // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xu's avatar
Yaowu Xu committed
783
  if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Jingning Han's avatar
Jingning Han committed
784
                               cm->subsampling_x, cm->subsampling_y,
785
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
786 787
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
788 789
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
790
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
791 792 793
                       "Failed to allocate altref buffer");
}

Yaowu Xu's avatar
Yaowu Xu committed
794 795 796
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
797
                               cm->subsampling_x, cm->subsampling_y,
798
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
799 800
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
801 802
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
803
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
804 805
                       "Failed to allocate last frame buffer");

Debargha Mukherjee's avatar
Debargha Mukherjee committed
806
#if CONFIG_LOOP_RESTORATION
807 808 809 810 811 812 813 814
  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,
815
#if CONFIG_HIGHBITDEPTH
816
          cm->use_highbitdepth,
817
#endif
818
          AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
819 820
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
                       "Failed to allocate trial restored frame buffer");
Debargha Mukherjee's avatar
Debargha Mukherjee committed
821 822
#endif  // CONFIG_LOOP_RESTORATION

Yaowu Xu's avatar
Yaowu Xu committed
823
  if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Jingning Han's avatar
Jingning Han committed
824
                               cm->subsampling_x, cm->subsampling_y,
825
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
826 827
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
828 829
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
830
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
831 832
                       "Failed to allocate scaled source buffer");

Yaowu Xu's avatar
Yaowu Xu committed
833
  if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Jingning Han's avatar
Jingning Han committed
834
                               cm->subsampling_x, cm->subsampling_y,
835
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
836 837
                               cm->use_highbitdepth,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
838 839
                               AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
                               NULL, NULL))
Yaowu Xu's avatar
Yaowu Xu committed
840
    aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
841 842 843
                       "Failed to allocate scaled last source buffer");
}

844
static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xu's avatar
Yaowu Xu committed
845
  AV1_COMMON *cm = &cpi->common;
Jingning Han's avatar
Jingning Han committed
846

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

849 850 851 852
#if CONFIG_LV_MAP
  av1_alloc_txb_buf(cpi);
#endif

Jingning Han's avatar
Jingning Han committed
853 854
  alloc_context_buffers_ext(cpi);

Yaowu Xu's avatar
Yaowu Xu committed
855
  aom_free(cpi->tile_tok[0][0]);
Jingning Han's avatar
Jingning Han committed
856 857

  {
858 859
    unsigned int tokens =
        get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2);
Jingning Han's avatar
Jingning Han committed
860
    CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xu's avatar
Yaowu Xu committed
861
                    aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Jingning Han's avatar
Jingning Han committed
862 863
  }

Yaowu Xu's avatar
Yaowu Xu committed
864
  av1_setup_pc_tree(&cpi->common, &cpi->td);
Jingning Han's avatar
Jingning Han committed
865 866
}

Yaowu Xu's avatar
Yaowu Xu committed
867
void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Jingning Han's avatar
Jingning Han committed
868
  cpi->framerate = framerate < 0.1 ? 30 : framerate;
869 870 871 872 873
#if CONFIG_XIPHRC
  if (!cpi->od_rc.cur_frame) return;
  cpi->od_rc.framerate = cpi->framerate;
  od_enc_rc_resize(&cpi->od_rc);
#else
874
  av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
875
#endif
Jingning Han's avatar
Jingning Han committed
876 877
}

878 879 880 881
#if CONFIG_MAX_TILE

static void set_tile_info_max_tile(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;
882
  int i, start_sb;
883 884 885 886

  av1_get_tile_limits(cm);

  // configure tile columns
887
  if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
888
    cm->uniform_tile_spacing_flag = 1;
889 890
    cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
    cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
891 892 893
  } else {
    int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
    int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
894
    int size_sb, j = 0;
895 896 897
    cm->uniform_tile_spacing_flag = 0;
    for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
      cm->tile_col_start_sb[i] = start_sb;
898 899 900
      size_sb = cpi->oxcf.tile_widths[j++];
      if (j >= cpi->oxcf.tile_width_count) j = 0;
      start_sb += AOMMIN(size_sb, MAX_TILE_WIDTH_SB);
901 902 903
    }
    cm->tile_cols = i;
    cm->tile_col_start_sb[i] = sb_cols;
904 905 906 907 908 909 910
  }
  av1_calculate_tile_cols(cm);

  // configure tile rows
  if (cm->uniform_tile_spacing_flag) {
    cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
    cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
911 912 913
  } else {
    int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
    int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
914
    int size_sb, j = 0;
915 916
    for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
      cm->tile_row_start_sb[i] = start_sb;
917 918 919
      size_sb = cpi->oxcf.tile_heights[j++];
      if (j >= cpi->oxcf.tile_height_count) j = 0;
      start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
920 921 922
    }
    cm->tile_rows = i;
    cm->tile_row_start_sb[i] = sb_rows;
923 924 925 926 927 928
  }
  av1_calculate_tile_rows(cm);
}

#endif

Yaowu Xu's avatar
Yaowu Xu committed
929 930
static void set_tile_info(AV1_COMP *cpi) {
  AV1_COMMON *const cm = &cpi->common;