decodeframe.c 144 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
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()

Yaowu Xu's avatar
Yaowu Xu committed
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"
Yaowu Xu's avatar
Yaowu Xu committed
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"
Yaowu Xu's avatar
Yaowu Xu committed
25
#include "aom_mem/aom_mem.h"
26 27
#include "aom_ports/mem.h"
#include "aom_ports/mem_ops.h"
Yaowu Xu's avatar
Yaowu Xu committed
28 29
#include "aom_scale/aom_scale.h"
#include "aom_util/aom_thread.h"
Jingning Han's avatar
Jingning Han committed
30

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

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

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

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

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

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

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

82 83 84 85 86 87 88
#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

89 90 91 92 93 94 95 96
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);

Yaowu Xu's avatar
Yaowu Xu committed
97
static int is_compound_reference_allowed(const AV1_COMMON *cm) {
98
#if CONFIG_ONE_SIDED_COMPOUND  // Normative in decoder
99 100
  return !frame_is_intra_only(cm);
#else
Jingning Han's avatar
Jingning Han committed
101
  int i;
102
  if (frame_is_intra_only(cm)) return 0;
103
  for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
104
    if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
Jingning Han's avatar
Jingning Han committed
105 106

  return 0;
107
#endif  // CONFIG_ONE_SIDED_COMPOUND
Jingning Han's avatar
Jingning Han committed
108 109
}

Yaowu Xu's avatar
Yaowu Xu committed
110
static void setup_compound_reference_mode(AV1_COMMON *cm) {
111
  cm->comp_fwd_ref[0] = LAST_FRAME;
112 113 114 115
  cm->comp_fwd_ref[1] = LAST2_FRAME;
  cm->comp_fwd_ref[2] = LAST3_FRAME;
  cm->comp_fwd_ref[3] = GOLDEN_FRAME;

116
  cm->comp_bwd_ref[0] = BWDREF_FRAME;
Zoe Liu's avatar
Zoe Liu committed
117 118
  cm->comp_bwd_ref[1] = ALTREF2_FRAME;
  cm->comp_bwd_ref[2] = ALTREF_FRAME;
Jingning Han's avatar
Jingning Han committed
119 120 121 122 123 124
}

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

Yaowu Xu's avatar
Yaowu Xu committed
125 126
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
127 128 129
  return data > max ? max : data;
}

130 131 132 133 134 135 136 137 138 139 140
#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
141
static TX_MODE read_tx_mode(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
142
#if CONFIG_TX64X64
143 144
  TX_MODE tx_mode;
#endif
145
  if (cm->all_lossless) return ONLY_4X4;
146 147 148 149
#if CONFIG_VAR_TX_NO_TX_MODE
  (void)rb;
  return TX_MODE_SELECT;
#else
150 151
#if CONFIG_TX64X64
  tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
152 153 154
  if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
  return tx_mode;
#else
Yaowu Xu's avatar
Yaowu Xu committed
155
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
156
#endif  // CONFIG_TX64X64
157
#endif  // CONFIG_VAR_TX_NO_TX_MODE
158
}
159
#endif  // CONFIG_SIMPLIFY_TX_MODE
Jingning Han's avatar
Jingning Han committed
160

Thomas Davies's avatar
Thomas Davies committed
161
#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xu's avatar
Yaowu Xu committed
162
static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
163
  int i;
164
  for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
165
    av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
Sarah Parker's avatar
Sarah Parker committed
166
  for (i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
167
    av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
168
  for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
169
    av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
170
  for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
171
    av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
172
}
173
#endif
Jingning Han's avatar
Jingning Han committed
174

175
static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xu's avatar
Yaowu Xu committed
176
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
177
  if (is_compound_reference_allowed(cm)) {
Zoe Liu's avatar
Zoe Liu committed
178 179 180
#if CONFIG_REF_ADAPT
    return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
#else
Yaowu Xu's avatar
Yaowu Xu committed
181
    return aom_rb_read_bit(rb)
182
               ? REFERENCE_MODE_SELECT
Yaowu Xu's avatar
Yaowu Xu committed
183
               : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
Zoe Liu's avatar
Zoe Liu committed
184
#endif  // CONFIG_REF_ADAPT
185 186 187 188
  } else {
    return SINGLE_REFERENCE;
  }
}
Jingning Han's avatar
Jingning Han committed
189

Thomas Davies's avatar
Thomas Davies committed
190
#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xu's avatar
Yaowu Xu committed
191
static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
192
  FRAME_CONTEXT *const fc = cm->fc;
193
  int i;
Jingning Han's avatar
Jingning Han committed
194 195 196

  if (cm->reference_mode == REFERENCE_MODE_SELECT)
    for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
197
      av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
198

199
  if (cm->reference_mode != COMPOUND_REFERENCE) {
Jingning Han's avatar
Jingning Han committed
200
    for (i = 0; i < REF_CONTEXTS; ++i) {
201
      int j;
202
      for (j = 0; j < (SINGLE_REFS - 1); ++j) {
Michael Bebenita's avatar
Michael Bebenita committed
203
        av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
204
      }
Jingning Han's avatar
Jingning Han committed
205
    }
206
  }
Jingning Han's avatar
Jingning Han committed
207

208
  if (cm->reference_mode != SINGLE_REFERENCE) {
Zoe Liu's avatar
Zoe Liu committed
209 210 211 212
#if CONFIG_EXT_COMP_REFS
    for (i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i)
      av1_diff_update_prob(r, &fc->comp_ref_type_prob[i], ACCT_STR);

213 214
    for (i = 0; i < UNI_COMP_REF_CONTEXTS; ++i) {
      int j;
Zoe Liu's avatar
Zoe Liu committed
215 216
      for (j = 0; j < (UNIDIR_COMP_REFS - 1); ++j)
        av1_diff_update_prob(r, &fc->uni_comp_ref_prob[i][j], ACCT_STR);
217
    }
Zoe Liu's avatar
Zoe Liu committed
218 219
#endif  // CONFIG_EXT_COMP_REFS

220
    for (i = 0; i < REF_CONTEXTS; ++i) {
221
      int j;
222
      for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita's avatar
Michael Bebenita committed
223
        av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
224
      for (j = 0; j < (BWD_REFS - 1); ++j)
Michael Bebenita's avatar
Michael Bebenita committed
225
        av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
226 227
    }
  }
Jingning Han's avatar
Jingning Han committed
228 229
}

Yaowu Xu's avatar
Yaowu Xu committed
230
static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
231
  int i;
Michael Bebenita's avatar
Michael Bebenita committed
232
  for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
233 234
}

Yaowu Xu's avatar
Yaowu Xu committed
235
static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
236
  int i;
Jingning Han's avatar
Jingning Han committed
237 238 239 240 241 242 243 244
  if (allow_hp) {
    for (i = 0; i < 2; ++i) {
      nmv_component *const comp_ctx = &ctx->comps[i];
      update_mv_probs(&comp_ctx->class0_hp, 1, r);
      update_mv_probs(&comp_ctx->hp, 1, r);
    }
  }
}
245
#endif
Jingning Han's avatar
Jingning Han committed
246

247
static void inverse_transform_block(MACROBLOCKD *xd, int plane,
248
                                    const TX_TYPE tx_type,
249
                                    const TX_SIZE tx_size, uint8_t *dst,
250
                                    int stride, int16_t scan_line, int eob) {
Jingning Han's avatar
Jingning Han committed
251
  struct macroblockd_plane *const pd = &xd->plane[plane];
252
  tran_low_t *const dqcoeff = pd->dqcoeff;
Lester Lu's avatar
Lester Lu committed
253
  av1_inverse_transform_block(xd, dqcoeff,
Sarah Parker's avatar
Sarah Parker committed
254 255 256
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
                              xd->mrc_mask,
#endif  // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
257
                              plane, tx_type, tx_size, dst, stride, eob);
258
  memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
259 260
}

261 262 263 264 265 266
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);
267
  const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
268 269 270 271
  const uint8_t txh_unit = tx_size_high_unit[tx_size];
  return row * max_blocks_wide + col * txh_unit;
}

272 273 274
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) {
275
  PLANE_TYPE plane_type = get_plane_type(plane);
276
  const int block_idx = get_block_idx(xd, plane, row, col);
277
  av1_predict_intra_block_facade(cm, xd, plane, block_idx, col, row, tx_size);
Jingning Han's avatar
Jingning Han committed
278 279

  if (!mbmi->skip) {
280
    struct macroblockd_plane *const pd = &xd->plane[plane];
Angie Chiang's avatar
Angie Chiang committed
281 282
#if CONFIG_LV_MAP
    int16_t max_scan_line = 0;
283 284
    int eob;
    av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
Jingning Han's avatar
Jingning Han committed
285
                               pd->dqcoeff, tx_size, &max_scan_line, &eob);
286
    // tx_type will be read out in av1_read_coeffs_txb_facade
287 288
    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
289
#else   // CONFIG_LV_MAP
290 291
    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
292
    const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, mbmi);
293 294
    int16_t max_scan_line = 0;
    const int eob =
295
        av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
296
                                tx_type, &max_scan_line, r, mbmi->segment_id);
Angie Chiang's avatar
Angie Chiang committed
297
#endif  // CONFIG_LV_MAP
298 299 300
    if (eob) {
      uint8_t *dst =
          &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
301
      inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
Hui Su's avatar
Hui Su committed
302
                              max_scan_line, eob);
303
    }
Jingning Han's avatar
Jingning Han committed
304
  }
305
#if CONFIG_CFL
306
  if (plane == AOM_PLANE_Y && xd->cfl->store_y) {
307
    cfl_store_tx(xd, row, col, tx_size, mbmi->sb_type);
308
  }
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
309
#endif  // CONFIG_CFL
Jingning Han's avatar
Jingning Han committed
310 311
}

Angie Chiang's avatar
Angie Chiang committed
312 313
static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
                                  aom_reader *r, MB_MODE_INFO *const mbmi,
314
                                  int plane, BLOCK_SIZE plane_bsize,
315 316
                                  int blk_row, int blk_col, int block,
                                  TX_SIZE tx_size, int *eob_total) {
317
  const struct macroblockd_plane *const pd = &xd->plane[plane];
318
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
319 320
  const int tx_row = blk_row >> (1 - pd->subsampling_y);
  const int tx_col = blk_col >> (1 - pd->subsampling_x);
321
  const TX_SIZE plane_tx_size =
322
      plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
323
            : mbmi->inter_tx_size[tx_row][tx_col];
324
  // Scale to match transform block unit.
325 326
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
327

328
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
329

330
  if (tx_size == plane_tx_size) {
331
    PLANE_TYPE plane_type = get_plane_type(plane);
Angie Chiang's avatar
Angie Chiang committed
332 333
#if CONFIG_LV_MAP
    int16_t max_scan_line = 0;
334
    int eob;
335
    av1_read_coeffs_txb_facade(cm, xd, r, blk_row, blk_col, block, plane,
Jingning Han's avatar
Jingning Han committed
336
                               pd->dqcoeff, tx_size, &max_scan_line, &eob);
337
    // tx_type will be read out in av1_read_coeffs_txb_facade
hui su's avatar
hui su committed
338
    const TX_TYPE tx_type =
339
        av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, plane_tx_size);
Angie Chiang's avatar
Angie Chiang committed
340
#else   // CONFIG_LV_MAP
hui su's avatar
hui su committed
341
    const TX_TYPE tx_type =
342
        av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, plane_tx_size);
Angie Chiang's avatar
Angie Chiang committed
343
    const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, mbmi);
344
    int16_t max_scan_line = 0;
345 346 347
    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);
Angie Chiang's avatar
Angie Chiang committed
348
#endif  // CONFIG_LV_MAP
349
    inverse_transform_block(xd, plane, tx_type, plane_tx_size,
350 351 352
                            &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
                                         << tx_size_wide_log2[0]],
                            pd->dst.stride, max_scan_line, eob);
353 354
    *eob_total += eob;
  } else {
Yue Chen's avatar
Yue Chen committed
355 356 357 358 359
#if CONFIG_RECT_TX_EXT
    int is_qttx = plane_tx_size == quarter_txsize_lookup[plane_bsize];
    const TX_SIZE sub_txs = is_qttx ? plane_tx_size : sub_tx_size_map[tx_size];
    if (is_qttx) assert(blk_row == 0 && blk_col == 0 && block == 0);
#else
360
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
361 362
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
Yue Chen's avatar
Yue Chen committed
363
#endif
364
    const int bsl = tx_size_wide_unit[sub_txs];
365
    int sub_step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
366 367 368 369 370
    int i;

    assert(bsl > 0);

    for (i = 0; i < 4; ++i) {
Yue Chen's avatar
Yue Chen committed
371 372 373 374 375 376 377 378 379
#if CONFIG_RECT_TX_EXT
      int is_wide_tx = tx_size_wide_unit[sub_txs] > tx_size_high_unit[sub_txs];
      const int offsetr =
          is_qttx ? (is_wide_tx ? i * tx_size_high_unit[sub_txs] : 0)
                  : blk_row + ((i >> 1) * bsl);
      const int offsetc =
          is_qttx ? (is_wide_tx ? 0 : i * tx_size_wide_unit[sub_txs])
                  : blk_col + (i & 0x01) * bsl;
#else
380 381
      const int offsetr = blk_row + (i >> 1) * bsl;
      const int offsetc = blk_col + (i & 0x01) * bsl;
Yue Chen's avatar
Yue Chen committed
382
#endif
383

384
      if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
385

386
      decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
387 388
                            offsetc, block, sub_txs, eob_total);
      block += sub_step;
389 390 391
    }
  }
}
Jingning Han's avatar
Jingning Han committed
392

393 394 395
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
396 397 398 399 400 401 402 403 404
  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;
405 406 407
#if CONFIG_RD_DEBUG
  xd->mi[0]->mbmi.mi_row = mi_row;
  xd->mi[0]->mbmi.mi_col = mi_col;
408 409 410 411
#endif
#if CONFIG_CFL
  xd->cfl->mi_row = mi_row;
  xd->cfl->mi_col = mi_col;
412
#endif
413 414 415 416 417 418 419 420

  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
421

Jingning Han's avatar
Jingning Han committed
422
  set_plane_n4(xd, bw, bh);
Jingning Han's avatar
Jingning Han committed
423 424
  set_skip_context(xd, mi_row, mi_col);

425 426 427
  // 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,
428
#if CONFIG_DEPENDENT_HORZTILES
429 430 431
                 cm->dependent_horz_tiles,
#endif  // CONFIG_DEPENDENT_HORZTILES
                 cm->mi_rows, cm->mi_cols);
Jingning Han's avatar
Jingning Han committed
432

433 434
  av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
                       mi_col);
Jingning Han's avatar
Jingning Han committed
435 436
}

437 438
static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                              int mi_row, int mi_col, aom_reader *r,
439
#if CONFIG_EXT_PARTITION_TYPES
440
                              PARTITION_TYPE partition,
441
#endif  // CONFIG_EXT_PARTITION_TYPES
442
                              BLOCK_SIZE bsize) {
Yaowu Xu's avatar
Yaowu Xu committed
443
  AV1_COMMON *const cm = &pbi->common;
444 445
  const int bw = mi_size_wide[bsize];
  const int bh = mi_size_high[bsize];
Yaowu Xu's avatar
Yaowu Xu committed
446 447
  const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
  const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
448

Michael Bebenita's avatar
Michael Bebenita committed
449 450 451
#if CONFIG_ACCOUNTING
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
#endif
452
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
453 454 455
#if CONFIG_EXT_PARTITION_TYPES
  xd->mi[0]->mbmi.partition = partition;
#endif
Yaowu Xu's avatar
Yaowu Xu committed
456
  av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Jingning Han's avatar
Jingning Han committed
457 458 459 460
  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)
Yaowu Xu's avatar
Yaowu Xu committed
461
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
462
                         "Invalid block size.");
Jingning Han's avatar
Jingning Han committed
463 464
  }

465 466
  int reader_corrupted_flag = aom_reader_has_error(r);
  aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
467 468 469 470 471 472 473 474 475 476 477 478
}

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);

479 480
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
481
#if CONFIG_CFL
482 483 484
  CFL_CTX *const cfl = xd->cfl;
  cfl->is_chroma_reference = is_chroma_reference(
      mi_row, mi_col, bsize, cfl->subsampling_x, cfl->subsampling_y);
485
#endif  // CONFIG_CFL
486

487 488 489
  if (cm->delta_q_present_flag) {
    int i;
    for (i = 0; i < MAX_SEGMENTS; i++) {
Fangwen Fu's avatar
Fangwen Fu committed
490
#if CONFIG_EXT_DELTA_Q
491 492
      const int current_qindex =
          av1_get_qindex(&cm->seg, i, xd->current_qindex);
Fangwen Fu's avatar
Fangwen Fu committed
493
#else
494 495 496 497 498 499 500 501 502 503 504 505
      const int current_qindex = xd->current_qindex;
#endif  // CONFIG_EXT_DELTA_Q
      int j;
      for (j = 0; j < MAX_MB_PLANE; ++j) {
        const int dc_delta_q = j == 0 ? cm->y_dc_delta_q : cm->uv_dc_delta_q;
        const int ac_delta_q = j == 0 ? 0 : cm->uv_ac_delta_q;

        xd->plane[j].seg_dequant[i][0] =
            av1_dc_quant(current_qindex, dc_delta_q, cm->bit_depth);
        xd->plane[j].seg_dequant[i][1] =
            av1_ac_quant(current_qindex, ac_delta_q, cm->bit_depth);
      }
506 507
    }
  }
508
  if (mbmi->skip) av1_reset_skip_context(xd, mi_row, mi_col, bsize);
509

510 511
  if (!is_inter_block(mbmi)) {
    int plane;
512

513 514
    for (plane = 0; plane <= 1; ++plane) {
      if (mbmi->palette_mode_info.palette_size[plane])
Yaowu Xu's avatar
Yaowu Xu committed
515
        av1_decode_palette_tokens(xd, plane, r);
Jingning Han's avatar
Jingning Han committed
516
    }
517

518 519
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
      const struct macroblockd_plane *const pd = &xd->plane[plane];
520
      const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
521 522
      const int stepr = tx_size_high_unit[tx_size];
      const int stepc = tx_size_wide_unit[tx_size];
523 524
      const BLOCK_SIZE plane_bsize =
          AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
525
      int row, col;
526 527
      const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
      const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
528 529
      if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
                               pd->subsampling_y))
530
        continue;
531 532 533 534 535 536 537 538 539 540
      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) {
541
        const int unit_height = AOMMIN(mu_blocks_high + row, max_blocks_high);
542 543 544 545 546 547 548 549 550
        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);
        }
      }
551 552
    }
  } else {
553 554
    int ref;

555
#if CONFIG_COMPOUND_SINGLEREF
556 557 558
    for (ref = 0; ref < 1 + is_inter_anyref_comp_mode(mbmi->mode); ++ref)
#else
    for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
559
#endif  // CONFIG_COMPOUND_SINGLEREF
560
    {
561
      const MV_REFERENCE_FRAME frame =
562
#if CONFIG_COMPOUND_SINGLEREF
563 564
          has_second_ref(mbmi) ? mbmi->ref_frame[ref] : mbmi->ref_frame[0];
#else
565
          mbmi->ref_frame[ref];
566
#endif  // CONFIG_COMPOUND_SINGLEREF
Alex Converse's avatar
Alex Converse committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
      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);
      }
585
    }
586

587
    av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
588

Yue Chen's avatar
Yue Chen committed
589
    if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen's avatar
Yue Chen committed
590 591 592
#if CONFIG_NCOBMC
      av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
#else
593
      av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chen's avatar
Yue Chen committed
594
#endif
595 596 597 598 599 600
    }
    // Reconstruction
    if (!mbmi->skip) {
      int eobtotal = 0;
      int plane;

Jingning Han's avatar
Jingning Han committed
601 602
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
        const struct macroblockd_plane *const pd = &xd->plane[plane];
603 604
        const BLOCK_SIZE plane_bsize =
            AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
605 606
        const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
        const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
607
        int row, col;
608

609 610
        if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
                                 pd->subsampling_y))
611 612
          continue;

613 614 615 616 617 618 619 620 621
        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);

622 623
        const TX_SIZE max_tx_size = get_vartx_max_txsize(
            mbmi, plane_bsize, pd->subsampling_x || pd->subsampling_y);
624 625
        const int bh_var_tx = tx_size_high_unit[max_tx_size];
        const int bw_var_tx = tx_size_wide_unit[max_tx_size];
626 627 628
        int block = 0;
        int step =
            tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

        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;
              }
            }
645 646
          }
        }
647
      }
Jingning Han's avatar
Jingning Han committed
648 649
    }
  }
650
#if CONFIG_CFL
651 652 653 654 655
  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);
    }
  }
656
#endif  // CONFIG_CFL
Jingning Han's avatar
Jingning Han committed
657

658 659
  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
660 661
}

662
#if NC_MODE_INFO
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
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];

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
  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;
701
#if CONFIG_EXT_PARTITION_TYPES
702 703 704
#if CONFIG_EXT_PARTITION_TYPES_AB
#error NC_MODE_INFO+MOTION_VAR not yet supported for new HORZ/VERT_AB partitions
#endif
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
    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;
727
#endif
728
    default: assert(0 && "Invalid partition type");
729 730 731 732
  }
}
#endif

733 734 735 736 737 738
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) {
739
  decode_mbmi_block(pbi, xd, mi_row, mi_col, r,
740 741 742 743
#if CONFIG_EXT_PARTITION_TYPES
                    partition,
#endif
                    bsize);
744

745
#if !(NC_MODE_INFO)
746
  decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
747
#endif
748 749
}

Yaowu Xu's avatar
Yaowu Xu committed
750 751
static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
                                     int mi_row, int mi_col, aom_reader *r,
752
                                     int has_rows, int has_cols,
753
                                     BLOCK_SIZE bsize) {
754 755 756 757
#if CONFIG_UNPOISON_PARTITION_CTX
  const int ctx =
      partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
#else
758
  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
759
#endif
Jingning Han's avatar
Jingning Han committed
760
  PARTITION_TYPE p;
761 762
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
Jingning Han's avatar
Jingning Han committed
763

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

766
  if (has_rows && has_cols) {
767
#if CONFIG_EXT_PARTITION_TYPES
768 769 770 771
    const int num_partition_types =
        (mi_width_log2_lookup[bsize] > mi_width_log2_lookup[BLOCK_8X8])
            ? EXT_PARTITION_TYPES
            : PARTITION_TYPES;
772
#else
773
    const int num_partition_types = PARTITION_TYPES;
774
#endif  // CONFIG_EXT_PARTITION_TYPES
775 776
    p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, num_partition_types,
                                        ACCT_STR);
777
  } else if (!has_rows && has_cols) {
778 779 780 781 782 783
    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
784
  } else if (has_rows && !has_cols) {
785 786 787 788 789
    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;
790
  } else {
Jingning Han's avatar
Jingning Han committed
791
    p = PARTITION_SPLIT;
792
  }
Jingning Han's avatar
Jingning Han committed
793 794 795 796 797

  return p;
}

// TODO(slavarnway): eliminate bsize and subsize in future commits
Yaowu Xu's avatar
Yaowu Xu committed
798 799
static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                             int mi_row, int mi_col, aom_reader *r,
800
                             BLOCK_SIZE bsize) {
Yaowu Xu's avatar
Yaowu Xu committed
801
  AV1_COMMON *const cm = &pbi->common;
802
  const int num_8x8_wh = mi_size_wide[bsize];
Jingning Han's avatar
Jingning Han committed
803
  const int hbs = num_8x8_wh >> 1;
804 805 806
#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
807 808
  PARTITION_TYPE partition;
  BLOCK_SIZE subsize;
809
#if CONFIG_EXT_PARTITION_TYPES
810 811
  const int quarter_step = num_8x8_wh / 4;
  int i;
812 813 814
#if !CONFIG_EXT_PARTITION_TYPES_AB
  BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
#endif
815
#endif
Jingning Han's avatar
Jingning Han committed
816 817 818
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
  const int has_cols = (mi_col + hbs) < cm->mi_cols;

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

821 822 823
  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
824
  subsize = subsize_lookup[partition][bsize];  // get_subsize(bsize, partition);
825

826 827 828 829 830 831 832 833 834
  // 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]);
  }

835 836 837 838 839 840 841 842 843 844 845 846
#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))

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
  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;
863
#if CONFIG_EXT_PARTITION_TYPES
864
#if CONFIG_EXT_PARTITION_TYPES_AB
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    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;
889
#else
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
    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;
910
#endif
911 912 913 914 915 916 917 918 919 920 921 922 923 924
    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;
925
#endif  // CONFIG_EXT_PARTITION_TYPES
926
    default: assert(0 && "Invalid partition type");
Jingning Han's avatar
Jingning Han committed
927 928
  }

929 930 931 932 933
#undef DEC_PARTITION
#undef DEC_BLOCK
#undef DEC_BLOCK_EPT_ARG
#undef DEC_BLOCK_STX_ARG

934
#if CONFIG_EXT_PARTITION_TYPES
935
  update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
936
#else
Jingning Han's avatar
Jingning Han committed
937 938 939
  // update partition context
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
940
    update_partition_context(xd, mi_row, mi_col, subsize, bsize);
941
#endif  // CONFIG_EXT_PARTITION_TYPES
942

943 944
#if CONFIG_LPF_SB
  if (bsize == cm->sb_size) {
945 946 947
    int filt_lvl;
    if (mi_row == 0 && mi_col == 0) {
      filt_lvl = aom_read_literal(r, 6, ACCT_STR);
948 949 950
      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;
951 952 953 954 955 956 957 958 959
    } 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;
      }
960

961 962 963 964 965 966 967 968 969 970
      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;
971

972
      if (reuse_prev_lvl) {
973
        filt_lvl = prev_lvl;
974 975
        curr_mbmi->delta = 0;
        curr_mbmi->sign = 0;
976
      } else {
977 978 979 980
        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;
981
        delta *= LPF_STEP;
982 983

        if (delta) {
984 985 986 987
          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;
988 989 990
          filt_lvl = sign ? prev_lvl + delta : prev_lvl - delta;
        } else {
          filt_lvl = prev_lvl;
991
          curr_mbmi->sign = 0;
992
        }
993 994
      }
    }
995 996

    av1_loop_filter_sb_level_init(cm, mi_row, mi_col, filt_lvl);
997 998 999
  }
#endif

1000
#if CONFIG_CDEF
1001
  if (bsize == cm->sb_size) {