decodeframe.c 141 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 13 14
 */

#include <assert.h>
#include <stdlib.h>  // qsort()

15 16 17
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
#include "./aom_scale_rtcd.h"
Jingning Han's avatar
Jingning Han committed
18
#include "./av1_rtcd.h"
Jingning Han's avatar
Jingning Han committed
19

20
#include "aom/aom_codec.h"
21
#include "aom_dsp/aom_dsp_common.h"
22
#include "aom_dsp/binary_codes_reader.h"
Jingning Han's avatar
Jingning Han committed
23 24
#include "aom_dsp/bitreader.h"
#include "aom_dsp/bitreader_buffer.h"
25
#include "aom_mem/aom_mem.h"
26
#include "aom_ports/aom_timer.h"
27 28
#include "aom_ports/mem.h"
#include "aom_ports/mem_ops.h"
29 30
#include "aom_scale/aom_scale.h"
#include "aom_util/aom_thread.h"
Jingning Han's avatar
Jingning Han committed
31

32 33 34 35
#if CONFIG_BITSTREAM_DEBUG
#include "aom_util/debug_util.h"
#endif  // CONFIG_BITSTREAM_DEBUG

36
#include "av1/common/alloccommon.h"
37
#include "av1/common/cdef.h"
38 39 40
#if CONFIG_INSPECTION
#include "av1/decoder/inspection.h"
#endif
41 42 43
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
44
#include "av1/common/entropymv.h"
45
#include "av1/common/idct.h"
46
#include "av1/common/mvref_common.h"
47 48 49
#include "av1/common/pred_common.h"
#include "av1/common/quant_common.h"
#include "av1/common/reconinter.h"
Jingning Han's avatar
Jingning Han committed
50
#include "av1/common/reconintra.h"
51 52 53
#if CONFIG_FRAME_SUPERRES
#include "av1/common/resize.h"
#endif  // CONFIG_FRAME_SUPERRES
54
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
55
#include "av1/common/thread_common.h"
56
#include "av1/common/tile_common.h"
57

58 59 60
#include "av1/decoder/decodeframe.h"
#include "av1/decoder/decodemv.h"
#include "av1/decoder/decoder.h"
61 62 63
#if CONFIG_LV_MAP
#include "av1/decoder/decodetxb.h"
#endif
Jingning Han's avatar
Jingning Han committed
64
#include "av1/decoder/detokenize.h"
65
#include "av1/decoder/dsubexp.h"
66
#include "av1/decoder/symbolrate.h"
Jingning Han's avatar
Jingning Han committed
67

Yue Chen's avatar
Yue Chen committed
68 69
#include "av1/common/warped_motion.h"

70
#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita's avatar
Michael Bebenita committed
71
#define ACCT_STR __func__
Jingning Han's avatar
Jingning Han committed
72

73 74 75 76
#if CONFIG_CFL
#include "av1/common/cfl.h"
#endif

77 78 79 80
#if CONFIG_STRIPED_LOOP_RESTORATION && !CONFIG_LOOP_RESTORATION
#error "striped_loop_restoration requires loop_restoration"
#endif

81 82 83 84 85 86 87
#if CONFIG_LOOP_RESTORATION
static void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
                                            MACROBLOCKD *xd,
                                            aom_reader *const r, int plane,
                                            int rtile_idx);
#endif

88 89 90 91 92 93 94 95
static struct aom_read_bit_buffer *init_read_bit_buffer(
    AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
    const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
                                  size_t partition_size);
static size_t read_uncompressed_header(AV1Decoder *pbi,
                                       struct aom_read_bit_buffer *rb);

96
static int is_compound_reference_allowed(const AV1_COMMON *cm) {
97
  return !frame_is_intra_only(cm);
Jingning Han's avatar
Jingning Han committed
98 99
}

100
static void setup_compound_reference_mode(AV1_COMMON *cm) {
101
  cm->comp_fwd_ref[0] = LAST_FRAME;
102 103 104 105
  cm->comp_fwd_ref[1] = LAST2_FRAME;
  cm->comp_fwd_ref[2] = LAST3_FRAME;
  cm->comp_fwd_ref[3] = GOLDEN_FRAME;

106
  cm->comp_bwd_ref[0] = BWDREF_FRAME;
Zoe Liu's avatar
Zoe Liu committed
107 108
  cm->comp_bwd_ref[1] = ALTREF2_FRAME;
  cm->comp_bwd_ref[2] = ALTREF_FRAME;
Jingning Han's avatar
Jingning Han committed
109 110 111 112 113 114
}

static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
  return len != 0 && len <= (size_t)(end - start);
}

115 116
static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
  const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Jingning Han's avatar
Jingning Han committed
117 118 119
  return data > max ? max : data;
}

120 121 122 123 124 125 126 127 128 129 130
#if CONFIG_SIMPLIFY_TX_MODE
static TX_MODE read_tx_mode(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
  if (cm->all_lossless) return ONLY_4X4;
#if CONFIG_VAR_TX_NO_TX_MODE
  (void)rb;
  return TX_MODE_SELECT;
#else
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
#endif  // CONFIG_VAR_TX_NO_TX_MODE
}
#else
131
static TX_MODE read_tx_mode(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
132
#if CONFIG_TX64X64
133 134
  TX_MODE tx_mode;
#endif
135
  if (cm->all_lossless) return ONLY_4X4;
136 137 138 139
#if CONFIG_VAR_TX_NO_TX_MODE
  (void)rb;
  return TX_MODE_SELECT;
#else
140 141
#if CONFIG_TX64X64
  tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
142 143 144
  if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
  return tx_mode;
#else
145
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
146
#endif  // CONFIG_TX64X64
147
#endif  // CONFIG_VAR_TX_NO_TX_MODE
148
}
149
#endif  // CONFIG_SIMPLIFY_TX_MODE
Jingning Han's avatar
Jingning Han committed
150

151
static REFERENCE_MODE read_frame_reference_mode(
152
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
153
  if (is_compound_reference_allowed(cm)) {
Zoe Liu's avatar
Zoe Liu committed
154 155 156
#if CONFIG_REF_ADAPT
    return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
#else
157
    return aom_rb_read_bit(rb)
158
               ? REFERENCE_MODE_SELECT
159
               : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liu's avatar
Zoe Liu committed
160
#endif  // CONFIG_REF_ADAPT
161 162 163 164
  } else {
    return SINGLE_REFERENCE;
  }
}
Jingning Han's avatar
Jingning Han committed
165

166
static void inverse_transform_block(MACROBLOCKD *xd, int plane,
167
                                    const TX_TYPE tx_type,
168
                                    const TX_SIZE tx_size, uint8_t *dst,
169
                                    int stride, int16_t scan_line, int eob) {
Jingning Han's avatar
Jingning Han committed
170
  struct macroblockd_plane *const pd = &xd->plane[plane];
171
  tran_low_t *const dqcoeff = pd->dqcoeff;
Lester Lu's avatar
Lester Lu committed
172
  av1_inverse_transform_block(xd, dqcoeff,
Sarah Parker's avatar
Sarah Parker committed
173 174 175
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
                              xd->mrc_mask,
#endif  // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
176
                              plane, tx_type, tx_size, dst, stride, eob);
177
  memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
178 179
}

180 181 182 183 184 185
static int get_block_idx(const MACROBLOCKD *xd, int plane, int row, int col) {
  const int bsize = xd->mi[0]->mbmi.sb_type;
  const struct macroblockd_plane *pd = &xd->plane[plane];
  const BLOCK_SIZE plane_bsize =
      AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
186
  const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
187 188 189 190
  const uint8_t txh_unit = tx_size_high_unit[tx_size];
  return row * max_blocks_wide + col * txh_unit;
}

191 192 193
static void predict_and_reconstruct_intra_block(
    AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
    MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
194
  PLANE_TYPE plane_type = get_plane_type(plane);
195
  const int block_idx = get_block_idx(xd, plane, row, col);
196
  av1_predict_intra_block_facade(cm, xd, plane, block_idx, col, row, tx_size);
Jingning Han's avatar
Jingning Han committed
197 198

  if (!mbmi->skip) {
199
    struct macroblockd_plane *const pd = &xd->plane[plane];
200 201 202 203
#if TXCOEFF_TIMER
    struct aom_usec_timer timer;
    aom_usec_timer_start(&timer);
#endif
204 205
#if CONFIG_LV_MAP
    int16_t max_scan_line = 0;
206 207
    int eob;
    av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
208
                               pd->dqcoeff, tx_size, &max_scan_line, &eob);
209
    // tx_type will be read out in av1_read_coeffs_txb_facade
210 211
    const TX_TYPE tx_type =
        av1_get_tx_type(plane_type, xd, row, col, block_idx, tx_size);
212
#else   // CONFIG_LV_MAP
213 214
    const TX_TYPE tx_type =
        av1_get_tx_type(plane_type, xd, row, col, block_idx, tx_size);
Angie Chiang's avatar
Angie Chiang committed
215
    const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, mbmi);
216 217
    int16_t max_scan_line = 0;
    const int eob =
218
        av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
219
                                tx_type, &max_scan_line, r, mbmi->segment_id);
220
#endif  // CONFIG_LV_MAP
221 222 223 224 225 226 227

#if TXCOEFF_TIMER
    aom_usec_timer_mark(&timer);
    const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
    cm->txcoeff_timer += elapsed_time;
    ++cm->txb_count;
#endif
228 229 230
    if (eob) {
      uint8_t *dst =
          &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
231
      inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
Hui Su's avatar
Hui Su committed
232
                              max_scan_line, eob);
233
    }
Jingning Han's avatar
Jingning Han committed
234
  }
235
#if CONFIG_CFL
236
  if (plane == AOM_PLANE_Y && xd->cfl->store_y) {
237
    cfl_store_tx(xd, row, col, tx_size, mbmi->sb_type);
238
  }
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
239
#endif  // CONFIG_CFL
Jingning Han's avatar
Jingning Han committed
240 241
}

Angie Chiang's avatar
Angie Chiang committed
242 243
static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
                                  aom_reader *r, MB_MODE_INFO *const mbmi,
244
                                  int plane, BLOCK_SIZE plane_bsize,
245 246
                                  int blk_row, int blk_col, int block,
                                  TX_SIZE tx_size, int *eob_total) {
247
  const struct macroblockd_plane *const pd = &xd->plane[plane];
248
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
249 250
  const int tx_row = blk_row >> (1 - pd->subsampling_y);
  const int tx_col = blk_col >> (1 - pd->subsampling_x);
251
  const TX_SIZE plane_tx_size =
252
      plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
253
            : mbmi->inter_tx_size[tx_row][tx_col];
254
  // Scale to match transform block unit.
255 256
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
257

258
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
259

260
  if (tx_size == plane_tx_size) {
261
    PLANE_TYPE plane_type = get_plane_type(plane);
262 263 264 265
#if TXCOEFF_TIMER
    struct aom_usec_timer timer;
    aom_usec_timer_start(&timer);
#endif
266 267
#if CONFIG_LV_MAP
    int16_t max_scan_line = 0;
268
    int eob;
269
    av1_read_coeffs_txb_facade(cm, xd, r, blk_row, blk_col, block, plane,
270
                               pd->dqcoeff, tx_size, &max_scan_line, &eob);
271
    // tx_type will be read out in av1_read_coeffs_txb_facade
hui su's avatar
hui su committed
272
    const TX_TYPE tx_type =
273
        av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, plane_tx_size);
274
#else   // CONFIG_LV_MAP
hui su's avatar
hui su committed
275
    const TX_TYPE tx_type =
276
        av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, plane_tx_size);
Angie Chiang's avatar
Angie Chiang committed
277
    const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, mbmi);
278
    int16_t max_scan_line = 0;
279 280 281
    const int eob = av1_decode_block_tokens(
        cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
        &max_scan_line, r, mbmi->segment_id);
282
#endif  // CONFIG_LV_MAP
283 284 285 286 287 288 289 290

#if TXCOEFF_TIMER
    aom_usec_timer_mark(&timer);
    const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
    cm->txcoeff_timer += elapsed_time;
    ++cm->txb_count;
#endif

291
    inverse_transform_block(xd, plane, tx_type, plane_tx_size,
292 293 294
                            &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
                                         << tx_size_wide_log2[0]],
                            pd->dst.stride, max_scan_line, eob);
295 296
    *eob_total += eob;
  } else {
297
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
298 299
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
300 301 302
    const int bsw = tx_size_wide_unit[sub_txs];
    const int bsh = tx_size_high_unit[sub_txs];
    const int sub_step = bsw * bsh;
303

304
    assert(bsw > 0 && bsh > 0);
305

306 307 308 309
    for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
      for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
        const int offsetr = blk_row + row;
        const int offsetc = blk_col + col;
310

311
        if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
312

313 314 315 316
        decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
                              offsetc, block, sub_txs, eob_total);
        block += sub_step;
      }
317 318 319
    }
  }
}
Jingning Han's avatar
Jingning Han committed
320

321 322 323
static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                        BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
                        int bh, int x_mis, int y_mis) {
Jingning Han's avatar
Jingning Han committed
324 325 326 327 328 329 330 331 332
  const int offset = mi_row * cm->mi_stride + mi_col;
  int x, y;
  const TileInfo *const tile = &xd->tile;

  xd->mi = cm->mi_grid_visible + offset;
  xd->mi[0] = &cm->mi[offset];
  // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
  // passing bsize from decode_partition().
  xd->mi[0]->mbmi.sb_type = bsize;
333 334 335
#if CONFIG_RD_DEBUG
  xd->mi[0]->mbmi.mi_row = mi_row;
  xd->mi[0]->mbmi.mi_col = mi_col;
336 337 338 339
#endif
#if CONFIG_CFL
  xd->cfl->mi_row = mi_row;
  xd->cfl->mi_col = mi_col;
340
#endif
341 342 343 344 345 346 347 348

  assert(x_mis && y_mis);
  for (x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
  int idx = cm->mi_stride;
  for (y = 1; y < y_mis; ++y) {
    memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
    idx += cm->mi_stride;
  }
Jingning Han's avatar
Jingning Han committed
349

Jingning Han's avatar
Jingning Han committed
350
  set_plane_n4(xd, bw, bh);
Jingning Han's avatar
Jingning Han committed
351 352
  set_skip_context(xd, mi_row, mi_col);

353 354 355
  // Distance of Mb to the various image edges. These are specified to 8th pel
  // as they are always compared to values that are in 1/8th pel units
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
356
#if CONFIG_DEPENDENT_HORZTILES
357 358 359
                 cm->dependent_horz_tiles,
#endif  // CONFIG_DEPENDENT_HORZTILES
                 cm->mi_rows, cm->mi_cols);
Jingning Han's avatar
Jingning Han committed
360

361 362
  av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
                       mi_col);
Jingning Han's avatar
Jingning Han committed
363 364
}

365 366
static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                              int mi_row, int mi_col, aom_reader *r,
367
#if CONFIG_EXT_PARTITION_TYPES
368
                              PARTITION_TYPE partition,
369
#endif  // CONFIG_EXT_PARTITION_TYPES
370
                              BLOCK_SIZE bsize) {
371
  AV1_COMMON *const cm = &pbi->common;
372 373
  const int bw = mi_size_wide[bsize];
  const int bh = mi_size_high[bsize];
374 375
  const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
  const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
376

Michael Bebenita's avatar
Michael Bebenita committed
377 378 379
#if CONFIG_ACCOUNTING
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
#endif
380
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
381 382 383
#if CONFIG_EXT_PARTITION_TYPES
  xd->mi[0]->mbmi.partition = partition;
#endif
384
  av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Jingning Han's avatar
Jingning Han committed
385 386 387 388
  if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
    const BLOCK_SIZE uv_subsize =
        ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
    if (uv_subsize == BLOCK_INVALID)
389
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
390
                         "Invalid block size.");
Jingning Han's avatar
Jingning Han committed
391 392
  }

393 394
  int reader_corrupted_flag = aom_reader_has_error(r);
  aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
395 396
}

397 398 399 400 401 402 403 404
// Converts a Q3 quantizer lookup from static configuration to the
// actual TX scaling in use
static int dequant_Q3_to_QTX(int q3, int bd) {
  // Right now, TX scale in use is still Q3
  (void)bd;
  return q3;
}

405 406 407 408 409 410 411 412 413 414
static void decode_token_and_recon_block(AV1Decoder *const pbi,
                                         MACROBLOCKD *const xd, int mi_row,
                                         int mi_col, aom_reader *r,
                                         BLOCK_SIZE bsize) {
  AV1_COMMON *const cm = &pbi->common;
  const int bw = mi_size_wide[bsize];
  const int bh = mi_size_high[bsize];
  const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
  const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);

415 416
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
417
#if CONFIG_CFL
418 419 420
  CFL_CTX *const cfl = xd->cfl;
  cfl->is_chroma_reference = is_chroma_reference(
      mi_row, mi_col, bsize, cfl->subsampling_x, cfl->subsampling_y);
421
#endif  // CONFIG_CFL
422

423 424 425
  if (cm->delta_q_present_flag) {
    int i;
    for (i = 0; i < MAX_SEGMENTS; i++) {
Fangwen Fu's avatar
Fangwen Fu committed
426
#if CONFIG_EXT_DELTA_Q
427
      const int current_qindex =
428 429 430
#if CONFIG_Q_SEGMENTATION
          av1_get_qindex(&cm->seg, i, i, xd->current_qindex);
#else
431
          av1_get_qindex(&cm->seg, i, xd->current_qindex);
432
#endif
Fangwen Fu's avatar
Fangwen Fu committed
433
#else
434 435
      const int current_qindex = xd->current_qindex;
#endif  // CONFIG_EXT_DELTA_Q
436
      for (int j = 0; j < av1_num_planes(cm); ++j) {
437 438 439 440 441
        const int dc_delta_q =
            j == 0 ? cm->y_dc_delta_q
                   : (j == 1 ? cm->u_dc_delta_q : cm->v_dc_delta_q);
        const int ac_delta_q =
            j == 0 ? 0 : (j == 1 ? cm->u_ac_delta_q : cm->v_ac_delta_q);
442 443 444 445 446 447
        xd->plane[j].seg_dequant_QTX[i][0] = dequant_Q3_to_QTX(
            av1_dc_quant(current_qindex, dc_delta_q, cm->bit_depth),
            cm->bit_depth);
        xd->plane[j].seg_dequant_QTX[i][1] = dequant_Q3_to_QTX(
            av1_ac_quant(current_qindex, ac_delta_q, cm->bit_depth),
            cm->bit_depth);
448
      }
449 450
    }
  }
451
  if (mbmi->skip) av1_reset_skip_context(xd, mi_row, mi_col, bsize);
452

453
  if (!is_inter_block(mbmi)) {
454 455
    const int num_planes = av1_num_planes(cm);
    for (int plane = 0; plane < AOMMIN(2, num_planes); ++plane) {
456
      if (mbmi->palette_mode_info.palette_size[plane])
457
        av1_decode_palette_tokens(xd, plane, r);
Jingning Han's avatar
Jingning Han committed
458
    }
459

460
    for (int plane = 0; plane < num_planes; ++plane) {
461
      const struct macroblockd_plane *const pd = &xd->plane[plane];
462
      const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
463 464
      const int stepr = tx_size_high_unit[tx_size];
      const int stepc = tx_size_wide_unit[tx_size];
465 466
      const BLOCK_SIZE plane_bsize =
          AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
467
      int row, col;
468 469
      const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
      const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
470 471
      if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
                               pd->subsampling_y))
472
        continue;
473 474 475 476 477 478 479 480 481 482
      int blk_row, blk_col;
      const BLOCK_SIZE max_unit_bsize = get_plane_block_size(BLOCK_64X64, pd);
      int mu_blocks_wide =
          block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
      int mu_blocks_high =
          block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
      mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
      mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);

      for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
483
        const int unit_height = AOMMIN(mu_blocks_high + row, max_blocks_high);
484 485 486 487 488 489 490 491 492
        for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
          const int unit_width = AOMMIN(mu_blocks_wide + col, max_blocks_wide);

          for (blk_row = row; blk_row < unit_height; blk_row += stepr)
            for (blk_col = col; blk_col < unit_width; blk_col += stepc)
              predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane,
                                                  blk_row, blk_col, tx_size);
        }
      }
493 494
    }
  } else {
495 496
    int ref;

497 498
    for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
      const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
      if (frame < LAST_FRAME) {
#if CONFIG_INTRABC
        assert(is_intrabc_block(mbmi));
        assert(frame == INTRA_FRAME);
        assert(ref == 0);
#else
        assert(0);
#endif  // CONFIG_INTRABC
      } else {
        RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];

        xd->block_refs[ref] = ref_buf;
        if ((!av1_is_valid_scale(&ref_buf->sf)))
          aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
                             "Reference frame has invalid dimensions");
        av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
                             &ref_buf->sf);
      }
517
    }
518

519
    av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
520

Yue Chen's avatar
Yue Chen committed
521
    if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen's avatar
Yue Chen committed
522 523 524
#if CONFIG_NCOBMC
      av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
#else
525
      av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chen's avatar
Yue Chen committed
526
#endif
527 528 529 530
    }
    // Reconstruction
    if (!mbmi->skip) {
      int eobtotal = 0;
531
      for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
Jingning Han's avatar
Jingning Han committed
532
        const struct macroblockd_plane *const pd = &xd->plane[plane];
533 534
        const BLOCK_SIZE plane_bsize =
            AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
535 536
        const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
        const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
537
        int row, col;
538

539 540
        if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
                                 pd->subsampling_y))
541 542
          continue;

543 544 545 546 547 548 549 550 551
        const BLOCK_SIZE max_unit_bsize = get_plane_block_size(BLOCK_64X64, pd);
        int mu_blocks_wide =
            block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
        int mu_blocks_high =
            block_size_high[max_unit_bsize] >> tx_size_high_log2[0];

        mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
        mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);

552 553
        const TX_SIZE max_tx_size = get_vartx_max_txsize(
            mbmi, plane_bsize, pd->subsampling_x || pd->subsampling_y);
554 555
        const int bh_var_tx = tx_size_high_unit[max_tx_size];
        const int bw_var_tx = tx_size_wide_unit[max_tx_size];
556 557 558
        int block = 0;
        int step =
            tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

        for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
          for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
            int blk_row, blk_col;
            const int unit_height =
                AOMMIN(mu_blocks_high + row, max_blocks_high);
            const int unit_width =
                AOMMIN(mu_blocks_wide + col, max_blocks_wide);
            for (blk_row = row; blk_row < unit_height; blk_row += bh_var_tx) {
              for (blk_col = col; blk_col < unit_width; blk_col += bw_var_tx) {
                decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize,
                                      blk_row, blk_col, block, max_tx_size,
                                      &eobtotal);
                block += step;
              }
            }
575 576
          }
        }
577
      }
Jingning Han's avatar
Jingning Han committed
578 579
    }
  }
580
#if CONFIG_CFL
581 582 583 584 585
  if (mbmi->uv_mode != UV_CFL_PRED) {
    if (!cfl->is_chroma_reference && is_inter_block(mbmi)) {
      cfl_store_block(xd, mbmi->sb_type, mbmi->tx_size);
    }
  }
586
#endif  // CONFIG_CFL
Jingning Han's avatar
Jingning Han committed
587

588 589
  int reader_corrupted_flag = aom_reader_has_error(r);
  aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
Jingning Han's avatar
Jingning Han committed
590 591
}

592
#if NC_MODE_INFO
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                                 int mi_row, int mi_col, aom_reader *r,
                                 BLOCK_SIZE bsize) {
  AV1_COMMON *const cm = &pbi->common;
  const int hbs = mi_size_wide[bsize] >> 1;
#if CONFIG_EXT_PARTITION_TYPES
  BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
#endif
  PARTITION_TYPE partition;
  BLOCK_SIZE subsize;
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
  const int has_cols = (mi_col + hbs) < cm->mi_cols;

  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;

  partition = get_partition(cm, mi_row, mi_col, bsize);
  subsize = subsize_lookup[partition][bsize];

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
  switch (partition) {
    case PARTITION_NONE:
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
      break;
    case PARTITION_HORZ:
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
      if (has_rows)
        decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
      break;
    case PARTITION_VERT:
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
      if (has_cols)
        decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
      break;
    case PARTITION_SPLIT:
      detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
      detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
      detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
      detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
      break;
631
#if CONFIG_EXT_PARTITION_TYPES
632 633 634
#if CONFIG_EXT_PARTITION_TYPES_AB
#error NC_MODE_INFO+MOTION_VAR not yet supported for new HORZ/VERT_AB partitions
#endif
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    case PARTITION_HORZ_A:
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
      decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
      break;
    case PARTITION_HORZ_B:
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
      decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
      decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
                                   bsize2);
      break;
    case PARTITION_VERT_A:
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
      decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
      break;
    case PARTITION_VERT_B:
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
      decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
      decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
                                   bsize2);
      break;
657
#endif
658
    default: assert(0 && "Invalid partition type");
659 660 661 662
  }
}
#endif

663 664 665 666 667 668
static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                         int mi_row, int mi_col, aom_reader *r,
#if CONFIG_EXT_PARTITION_TYPES
                         PARTITION_TYPE partition,
#endif  // CONFIG_EXT_PARTITION_TYPES
                         BLOCK_SIZE bsize) {
669
  decode_mbmi_block(pbi, xd, mi_row, mi_col, r,
670 671 672 673
#if CONFIG_EXT_PARTITION_TYPES
                    partition,
#endif
                    bsize);
674

675
#if !(NC_MODE_INFO)
676
  decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
677
#endif
678 679
}

680 681
static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
                                     int mi_row, int mi_col, aom_reader *r,
682
                                     int has_rows, int has_cols,
683 684
                                     BLOCK_SIZE bsize) {
  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Jingning Han's avatar
Jingning Han committed
685
  PARTITION_TYPE p;
686 687
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
Jingning Han's avatar
Jingning Han committed
688

Jingning Han's avatar
Jingning Han committed
689 690
  aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;

691
  if (has_rows && has_cols) {
692
#if CONFIG_EXT_PARTITION_TYPES
693 694 695 696
    const int num_partition_types =
        (mi_width_log2_lookup[bsize] > mi_width_log2_lookup[BLOCK_8X8])
            ? EXT_PARTITION_TYPES
            : PARTITION_TYPES;
697
#else
698
    const int num_partition_types = PARTITION_TYPES;
699
#endif  // CONFIG_EXT_PARTITION_TYPES
700 701
    p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, num_partition_types,
                                        ACCT_STR);
702
  } else if (!has_rows && has_cols) {
703 704 705 706 707 708
    assert(bsize > BLOCK_8X8);
    aom_cdf_prob cdf[2];
    partition_gather_vert_alike(cdf, partition_cdf);
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
    p = aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
    // gather cols
709
  } else if (has_rows && !has_cols) {
710 711 712 713 714
    assert(bsize > BLOCK_8X8);
    aom_cdf_prob cdf[2];
    partition_gather_horz_alike(cdf, partition_cdf);
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
    p = aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
715
  } else {
Jingning Han's avatar
Jingning Han committed
716
    p = PARTITION_SPLIT;
717
  }
Jingning Han's avatar
Jingning Han committed
718 719 720 721 722

  return p;
}

// TODO(slavarnway): eliminate bsize and subsize in future commits
723 724
static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                             int mi_row, int mi_col, aom_reader *r,
725
                             BLOCK_SIZE bsize) {
726
  AV1_COMMON *const cm = &pbi->common;
727
  const int num_8x8_wh = mi_size_wide[bsize];
Jingning Han's avatar
Jingning Han committed
728
  const int hbs = num_8x8_wh >> 1;
729 730 731
#if CONFIG_EXT_PARTITION_TYPES && CONFIG_EXT_PARTITION_TYPES_AB
  const int qbs = num_8x8_wh >> 2;
#endif
Jingning Han's avatar
Jingning Han committed
732 733
  PARTITION_TYPE partition;
  BLOCK_SIZE subsize;
734
#if CONFIG_EXT_PARTITION_TYPES
735 736
  const int quarter_step = num_8x8_wh / 4;
  int i;
737 738 739
#if !CONFIG_EXT_PARTITION_TYPES_AB
  BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
#endif
740
#endif
Jingning Han's avatar
Jingning Han committed
741 742 743
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
  const int has_cols = (mi_col + hbs) < cm->mi_cols;

744
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
Jingning Han's avatar
Jingning Han committed
745

746 747 748
  partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
                                  : read_partition(cm, xd, mi_row, mi_col, r,
                                                   has_rows, has_cols, bsize);
Jingning Han's avatar
Jingning Han committed
749
  subsize = subsize_lookup[partition][bsize];  // get_subsize(bsize, partition);
750

751 752 753 754 755 756 757 758 759
  // Check the bitstream is conformant: if there is subsampling on the
  // chroma planes, subsize must subsample to a valid block size.
  const struct macroblockd_plane *const pd_u = &xd->plane[1];
  if (get_plane_block_size(subsize, pd_u) == BLOCK_INVALID) {
    aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
                       "Block size %dx%d invalid with this subsampling mode",
                       block_size_wide[subsize], block_size_high[subsize]);
  }

760 761 762 763 764 765 766 767 768 769 770 771
#define DEC_BLOCK_STX_ARG
#if CONFIG_EXT_PARTITION_TYPES
#define DEC_BLOCK_EPT_ARG partition,
#else
#define DEC_BLOCK_EPT_ARG
#endif
#define DEC_BLOCK(db_r, db_c, db_subsize)                   \
  decode_block(pbi, xd, DEC_BLOCK_STX_ARG(db_r), (db_c), r, \
               DEC_BLOCK_EPT_ARG(db_subsize))
#define DEC_PARTITION(db_r, db_c, db_subsize) \
  decode_partition(pbi, xd, DEC_BLOCK_STX_ARG(db_r), (db_c), r, (db_subsize))

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
  switch (partition) {
    case PARTITION_NONE: DEC_BLOCK(mi_row, mi_col, subsize); break;
    case PARTITION_HORZ:
      DEC_BLOCK(mi_row, mi_col, subsize);
      if (has_rows) DEC_BLOCK(mi_row + hbs, mi_col, subsize);
      break;
    case PARTITION_VERT:
      DEC_BLOCK(mi_row, mi_col, subsize);
      if (has_cols) DEC_BLOCK(mi_row, mi_col + hbs, subsize);
      break;
    case PARTITION_SPLIT:
      DEC_PARTITION(mi_row, mi_col, subsize);
      DEC_PARTITION(mi_row, mi_col + hbs, subsize);
      DEC_PARTITION(mi_row + hbs, mi_col, subsize);
      DEC_PARTITION(mi_row + hbs, mi_col + hbs, subsize);
      break;
788
#if CONFIG_EXT_PARTITION_TYPES
789
#if CONFIG_EXT_PARTITION_TYPES_AB
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    case PARTITION_HORZ_A:
      DEC_BLOCK(mi_row, mi_col, get_subsize(bsize, PARTITION_HORZ_4));
      DEC_BLOCK(mi_row + qbs, mi_col, get_subsize(bsize, PARTITION_HORZ_4));
      DEC_BLOCK(mi_row + hbs, mi_col, subsize);
      break;
    case PARTITION_HORZ_B:
      DEC_BLOCK(mi_row, mi_col, subsize);
      DEC_BLOCK(mi_row + hbs, mi_col, get_subsize(bsize, PARTITION_HORZ_4));
      if (mi_row + 3 * qbs < cm->mi_rows)
        DEC_BLOCK(mi_row + 3 * qbs, mi_col,
                  get_subsize(bsize, PARTITION_HORZ_4));
      break;
    case PARTITION_VERT_A:
      DEC_BLOCK(mi_row, mi_col, get_subsize(bsize, PARTITION_VERT_4));
      DEC_BLOCK(mi_row, mi_col + qbs, get_subsize(bsize, PARTITION_VERT_4));
      DEC_BLOCK(mi_row, mi_col + hbs, subsize);
      break;
    case PARTITION_VERT_B:
      DEC_BLOCK(mi_row, mi_col, subsize);
      DEC_BLOCK(mi_row, mi_col + hbs, get_subsize(bsize, PARTITION_VERT_4));
      if (mi_col + 3 * qbs < cm->mi_cols)
        DEC_BLOCK(mi_row, mi_col + 3 * qbs,
                  get_subsize(bsize, PARTITION_VERT_4));
      break;
814
#else
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    case PARTITION_HORZ_A:
      DEC_BLOCK(mi_row, mi_col, bsize2);
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
      DEC_BLOCK(mi_row + hbs, mi_col, subsize);
      break;
    case PARTITION_HORZ_B:
      DEC_BLOCK(mi_row, mi_col, subsize);
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
      break;
    case PARTITION_VERT_A:
      DEC_BLOCK(mi_row, mi_col, bsize2);
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
      DEC_BLOCK(mi_row, mi_col + hbs, subsize);
      break;
    case PARTITION_VERT_B:
      DEC_BLOCK(mi_row, mi_col, subsize);
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
      break;
835
#endif
836 837 838 839 840 841 842 843 844 845 846 847 848 849
    case PARTITION_HORZ_4:
      for (i = 0; i < 4; ++i) {
        int this_mi_row = mi_row + i * quarter_step;
        if (i > 0 && this_mi_row >= cm->mi_rows) break;
        DEC_BLOCK(this_mi_row, mi_col, subsize);
      }
      break;
    case PARTITION_VERT_4:
      for (i = 0; i < 4; ++i) {
        int this_mi_col = mi_col + i * quarter_step;
        if (i > 0 && this_mi_col >= cm->mi_cols) break;
        DEC_BLOCK(mi_row, this_mi_col, subsize);
      }
      break;
850
#endif  // CONFIG_EXT_PARTITION_TYPES
851
    default: assert(0 && "Invalid partition type");
Jingning Han's avatar
Jingning Han committed
852 853
  }

854 855 856 857 858
#undef DEC_PARTITION
#undef DEC_BLOCK
#undef DEC_BLOCK_EPT_ARG
#undef DEC_BLOCK_STX_ARG

859
#if CONFIG_EXT_PARTITION_TYPES
860
  update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
861
#else
Jingning Han's avatar
Jingning Han committed
862 863 864
  // update partition context
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
865
    update_partition_context(xd, mi_row, mi_col, subsize, bsize);
866
#endif  // CONFIG_EXT_PARTITION_TYPES
867

868 869
#if CONFIG_LPF_SB
  if (bsize == cm->sb_size) {
870 871 872
    int filt_lvl;
    if (mi_row == 0 && mi_col == 0) {
      filt_lvl = aom_read_literal(r, 6, ACCT_STR);
873 874 875
      cm->mi_grid_visible[0]->mbmi.reuse_sb_lvl = 0;
      cm->mi_grid_visible[0]->mbmi.delta = 0;
      cm->mi_grid_visible[0]->mbmi.sign = 0;
876 877 878 879 880 881 882 883 884
    } else {
      int prev_mi_row, prev_mi_col;
      if (mi_col - MAX_MIB_SIZE < 0) {
        prev_mi_row = mi_row - MAX_MIB_SIZE;
        prev_mi_col = mi_col;
      } else {
        prev_mi_row = mi_row;
        prev_mi_col = mi_col - MAX_MIB_SIZE;
      }
885

886 887 888 889 890 891 892 893 894 895
      MB_MODE_INFO *curr_mbmi =
          &cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi;
      MB_MODE_INFO *prev_mbmi =
          &cm->mi_grid_visible[prev_mi_row * cm->mi_stride + prev_mi_col]->mbmi;
      const uint8_t prev_lvl = prev_mbmi->filt_lvl;

      const int reuse_ctx = prev_mbmi->reuse_sb_lvl;
      const int reuse_prev_lvl = aom_read_symbol(
          r, xd->tile_ctx->lpf_reuse_cdf[reuse_ctx], 2, ACCT_STR);
      curr_mbmi->reuse_sb_lvl = reuse_prev_lvl;
896

897
      if (reuse_prev_lvl) {
898
        filt_lvl = prev_lvl;
899 900
        curr_mbmi->delta = 0;
        curr_mbmi->sign = 0;
901
      } else {
902 903 904 905
        const int delta_ctx = prev_mbmi->delta;
        unsigned int delta = aom_read_symbol(
            r, xd->tile_ctx->lpf_delta_cdf[delta_ctx], DELTA_RANGE, ACCT_STR);
        curr_mbmi->delta = delta;
906
        delta *= LPF_STEP;
907 908

        if (delta) {
909 910 911 912
          const int sign_ctx = prev_mbmi->sign;
          const int sign = aom_read_symbol(
              r, xd->tile_ctx->lpf_sign_cdf[reuse_ctx][sign_ctx], 2, ACCT_STR);
          curr_mbmi->sign = sign;
913 914 915
          filt_lvl = sign ? prev_lvl + delta : prev_lvl - delta;
        } else {
          filt_lvl = prev_lvl;
916
          curr_mbmi->sign = 0;
917
        }
918 919
      }
    }
920 921

    av1_loop_filter_sb_level_init(cm, mi_row, mi_col, filt_lvl);
922 923 924
  }
#endif

925
  if (bsize == cm->sb_size) {
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
    int width_step = mi_size_wide[BLOCK_64X64];
    int height_step = mi_size_wide[BLOCK_64X64];
    int w, h;
    for (h = 0; (h < mi_size_high[cm->sb_size]) && (mi_row + h < cm->mi_rows);
         h += height_step) {
      for (w = 0; (w < mi_size_wide[cm->sb_size]) && (mi_col + w < cm->mi_cols);
           w += width_step) {
        if (!cm->all_lossless && !sb_all_skip(cm, mi_row + h, mi_col + w))
          cm->mi_grid_visible[(mi_row + h) * cm->mi_stride + (mi_col + w)]
              ->mbmi.cdef_strength =
              aom_read_literal(r, cm->cdef_bits, ACCT_STR);
        else
          cm->mi_grid_visible[(mi_row + h) * cm->mi_stride + (mi_col + w)]
              ->mbmi.cdef_strength = -1;
      }
941 942
    }
  }
943
#if CONFIG_LOOP_RESTORATION
944
  for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
945
    int rcol0, rcol1, rrow0, rrow1, tile_tl_idx;
946 947
    if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
                                           &rcol0, &rcol1, &rrow0, &rrow1,
948 949
                                           &tile_tl_idx)) {
      const int rstride = cm->rst_info[plane].horz_units_per_tile;
950 951
      for (int rrow = rrow0; rrow < rrow1; ++rrow) {
        for (int rcol = rcol0; rcol < rcol1; ++rcol) {
952
          const int rtile_idx = tile_tl_idx + rcol + rrow * rstride;
953 954 955 956 957 958
          loop_restoration_read_sb_coeffs(cm, xd, r, plane, rtile_idx);
        }
      }
    }
  }
#endif
Jingning Han's avatar
Jingning Han committed
959 960
}

961
static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
Alex Converse's avatar
Alex Converse committed
962
                               const size_t read_size,
963
                               struct aom_internal_error_info *error_info,
964
                               aom_reader *r, uint8_t allow_update_cdf,
965 966 967 968
#if CONFIG_ANS && ANS_MAX_SYMBOLS
                               int window_size,
#endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
                               aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Alex Converse's avatar
Alex Converse committed
969 970 971 972
  // Validate the calculated partition length. If the buffer
  // described by the partition can't be fully read, then restrict
  // it to the portion that can be (for EC mode) or throw an error.
  if (!read_is_valid(data, read_size, data_end))
973
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Alex Converse's avatar
Alex Converse committed
974 975
                       "Truncated packet or corrupt tile length");

976
#if CONFIG_ANS && ANS_MAX_SYMBOLS
977
  r->window_size = window_size;
978
#endif
979
  if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
980
    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Alex Converse's avatar
Alex Converse committed
981
                       "Failed to allocate bool decoder %d", 1);
982 983

  r->allow_update_cdf = allow_update_cdf;
Alex Converse's avatar
Alex Converse committed
984
}
Jingning Han's avatar
Jingning Han committed
985

986 987
static void setup_segmentation(AV1_COMMON *const cm,
                               struct aom_read_bit_buffer *rb) {
988
  struct segmentation *const seg = &cm->seg;
Jingning Han's avatar
Jingning Han committed
989 990 991 992
  int i, j;

  seg->update_map = 0;
  seg->update_data = 0;
993
  seg->temporal_update = 0;
Jingning Han's avatar
Jingning Han committed
994

995
  seg->enabled = aom_rb_read_bit(rb);
996
  if (!seg->enabled) return;
Jingning Han's avatar
Jingning Han committed
997 998

  // Segmentation map update
999 1000 1001
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    seg->update_map = 1;
  } else {
1002
    seg->update_map = aom_rb_read_bit(rb);
1003
  }
Jingning Han's avatar
Jingning Han committed
1004
  if (seg->update_map) {
1005 1006 1007
    if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
      seg->temporal_update = 0;
    } else {
1008
      seg->temporal_update = aom_rb_read_bit(rb);
1009
    }
Jingning Han's avatar
Jingning Han committed
1010 1011 1012
  }

  // Segmentation data update
1013
  seg->update_data = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
1014
  if (seg->update_data) {
1015
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
1016 1017 1018 1019

    for (i = 0; i < MAX_SEGMENTS; i++) {
      for (j = 0; j < SEG_LVL_MAX; j++) {
        int data = 0;
1020
        const int feature_enabled = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
1021
        if (feature_enabled) {
1022 1023 1024 1025
          av1_enable_segfeature(seg, i, j);
          data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
          if (av1_is_segfeature_signed(j))
            data = aom_rb_read_bit(rb) ? -data : data;
Jingning Han's avatar
Jingning Han committed
1026
        }
1027
        av1_set_segdata(seg, i, j, data);
Jingning Han's avatar
Jingning Han committed
1028 1029 1030 1031 1032
      }
    }
  }
}

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
#if CONFIG_Q_SEGMENTATION