decodeframe.c 142 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
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.
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"
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"
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"
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__
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);
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;
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));
117 118 119
  return data > max ? max : data;
}

120 121 122 123 124 125
#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;
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
}
#else
126 127
static TX_MODE read_tx_mode(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
  if (cm->all_lossless) return ONLY_4X4;
128
#if CONFIG_TX64X64
129 130
  TX_MODE tx_mode =
      aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
131 132 133
  if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
  return tx_mode;
#else
134
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
135
#endif  // CONFIG_TX64X64
136
}
137
#endif  // CONFIG_SIMPLIFY_TX_MODE
138

139
static REFERENCE_MODE read_frame_reference_mode(
140
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
141
  if (is_compound_reference_allowed(cm)) {
142 143 144
#if CONFIG_REF_ADAPT
    return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
#else
145
    return aom_rb_read_bit(rb)
146
               ? REFERENCE_MODE_SELECT
147
               : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
148
#endif  // CONFIG_REF_ADAPT
149 150 151 152
  } else {
    return SINGLE_REFERENCE;
  }
}
153

154
static void inverse_transform_block(MACROBLOCKD *xd, int plane,
155
                                    const TX_TYPE tx_type,
156
                                    const TX_SIZE tx_size, uint8_t *dst,
157 158
                                    int stride, int16_t scan_line, int eob,
                                    int reduced_tx_set) {
159
  struct macroblockd_plane *const pd = &xd->plane[plane];
160
  tran_low_t *const dqcoeff = pd->dqcoeff;
161 162
  av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst, stride,
                              eob, reduced_tx_set);
163
  memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
164 165
}

166 167 168 169 170 171
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);
172
  const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
173 174 175 176
  const uint8_t txh_unit = tx_size_high_unit[tx_size];
  return row * max_blocks_wide + col * txh_unit;
}

177 178 179
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) {
180
  PLANE_TYPE plane_type = get_plane_type(plane);
181
  const int block_idx = get_block_idx(xd, plane, row, col);
182
  av1_predict_intra_block_facade(cm, xd, plane, block_idx, col, row, tx_size);
183 184

  if (!mbmi->skip) {
185
    struct macroblockd_plane *const pd = &xd->plane[plane];
186 187 188 189
#if TXCOEFF_TIMER
    struct aom_usec_timer timer;
    aom_usec_timer_start(&timer);
#endif
190 191
#if CONFIG_LV_MAP
    int16_t max_scan_line = 0;
192 193
    int eob;
    av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
194
                               pd->dqcoeff, tx_size, &max_scan_line, &eob);
195
    // tx_type will be read out in av1_read_coeffs_txb_facade
196 197
    const TX_TYPE tx_type =
        av1_get_tx_type(plane_type, xd, row, col, block_idx, tx_size);
198
#else   // CONFIG_LV_MAP
199 200
    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
201
    const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, mbmi);
202 203
    int16_t max_scan_line = 0;
    const int eob =
204
        av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
205
                                tx_type, &max_scan_line, r, mbmi->segment_id);
206
#endif  // CONFIG_LV_MAP
207 208 209 210 211 212 213

#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
214 215 216
    if (eob) {
      uint8_t *dst =
          &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
217
      inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
218
                              max_scan_line, eob, cm->reduced_tx_set_used);
219
    }
220
  }
221
#if CONFIG_CFL
222
  if (plane == AOM_PLANE_Y && xd->cfl.store_y) {
223
    cfl_store_tx(xd, row, col, tx_size, mbmi->sb_type);
224
  }
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
225
#endif  // CONFIG_CFL
226 227
}

228 229
static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
                                  aom_reader *r, MB_MODE_INFO *const mbmi,
230
                                  int plane, BLOCK_SIZE plane_bsize,
231 232
                                  int blk_row, int blk_col, int block,
                                  TX_SIZE tx_size, int *eob_total) {
233
  const struct macroblockd_plane *const pd = &xd->plane[plane];
234
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
235 236
  const int tx_row = blk_row >> (1 - pd->subsampling_y);
  const int tx_col = blk_col >> (1 - pd->subsampling_x);
237
  const TX_SIZE plane_tx_size =
238
      plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
239
            : mbmi->inter_tx_size[tx_row][tx_col];
240
  // Scale to match transform block unit.
241 242
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
243

244
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
245

246 247 248 249 250
  if (tx_size == plane_tx_size
#if DISABLE_VARTX_FOR_CHROMA
      || pd->subsampling_x || pd->subsampling_y
#endif  // DISABLE_VARTX_FOR_CHROMA
      ) {
251
    PLANE_TYPE plane_type = get_plane_type(plane);
252 253 254 255
#if TXCOEFF_TIMER
    struct aom_usec_timer timer;
    aom_usec_timer_start(&timer);
#endif
256 257
#if CONFIG_LV_MAP
    int16_t max_scan_line = 0;
258
    int eob;
259
    av1_read_coeffs_txb_facade(cm, xd, r, blk_row, blk_col, block, plane,
260
                               pd->dqcoeff, tx_size, &max_scan_line, &eob);
261
    // tx_type will be read out in av1_read_coeffs_txb_facade
hui su's avatar
hui su committed
262
    const TX_TYPE tx_type =
263
        av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
264
#else   // CONFIG_LV_MAP
hui su's avatar
hui su committed
265
    const TX_TYPE tx_type =
266 267
        av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
    const SCAN_ORDER *sc = get_scan(cm, tx_size, tx_type, mbmi);
268
    int16_t max_scan_line = 0;
269 270 271
    const int eob =
        av1_decode_block_tokens(cm, xd, plane, sc, blk_col, blk_row, tx_size,
                                tx_type, &max_scan_line, r, mbmi->segment_id);
272
#endif  // CONFIG_LV_MAP
273 274 275 276 277 278 279 280

#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

281
    inverse_transform_block(
282
        xd, plane, tx_type, tx_size,
283 284 285
        &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]],
        pd->dst.stride, max_scan_line, eob, cm->reduced_tx_set_used);
286 287
    *eob_total += eob;
  } else {
288
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
289 290
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
291 292 293
    const int bsw = tx_size_wide_unit[sub_txs];
    const int bsh = tx_size_high_unit[sub_txs];
    const int sub_step = bsw * bsh;
294

295
    assert(bsw > 0 && bsh > 0);
296

297 298 299 300
    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;
301

302
        if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
303

304 305 306 307
        decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
                              offsetc, block, sub_txs, eob_total);
        block += sub_step;
      }
308 309 310
    }
  }
}
311

312 313 314
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) {
315 316 317 318 319 320 321 322 323
  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;
324 325 326
#if CONFIG_RD_DEBUG
  xd->mi[0]->mbmi.mi_row = mi_row;
  xd->mi[0]->mbmi.mi_col = mi_col;
327 328
#endif
#if CONFIG_CFL
329 330
  xd->cfl.mi_row = mi_row;
  xd->cfl.mi_col = mi_col;
331
#endif
332 333 334 335 336 337 338 339

  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;
  }
340

Jingning Han's avatar
Jingning Han committed
341
  set_plane_n4(xd, bw, bh);
342 343
  set_skip_context(xd, mi_row, mi_col);

344 345 346
  // 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,
347
#if CONFIG_DEPENDENT_HORZTILES
348 349 350
                 cm->dependent_horz_tiles,
#endif  // CONFIG_DEPENDENT_HORZTILES
                 cm->mi_rows, cm->mi_cols);
351

352 353
  av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
                       mi_col);
354 355
}

356 357
static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                              int mi_row, int mi_col, aom_reader *r,
358
#if CONFIG_EXT_PARTITION_TYPES
359
                              PARTITION_TYPE partition,
360
#endif  // CONFIG_EXT_PARTITION_TYPES
361
                              BLOCK_SIZE bsize) {
362
  AV1_COMMON *const cm = &pbi->common;
363 364
  const int bw = mi_size_wide[bsize];
  const int bh = mi_size_high[bsize];
365 366
  const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
  const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
367

Michael Bebenita's avatar
Michael Bebenita committed
368 369 370
#if CONFIG_ACCOUNTING
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
#endif
371
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
372 373 374
#if CONFIG_EXT_PARTITION_TYPES
  xd->mi[0]->mbmi.partition = partition;
#endif
375
  av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
376 377 378 379
  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)
380
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
381
                         "Invalid block size.");
382 383
  }

384 385
  int reader_corrupted_flag = aom_reader_has_error(r);
  aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
386 387 388 389 390 391 392 393 394 395 396 397
}

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

398 399
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
400
#if CONFIG_CFL
401
  CFL_CTX *const cfl = &xd->cfl;
402 403
  cfl->is_chroma_reference = is_chroma_reference(
      mi_row, mi_col, bsize, cfl->subsampling_x, cfl->subsampling_y);
404
#endif  // CONFIG_CFL
405

406 407 408
  if (cm->delta_q_present_flag) {
    int i;
    for (i = 0; i < MAX_SEGMENTS; i++) {
409
#if CONFIG_EXT_DELTA_Q
410
      const int current_qindex =
411 412 413
#if CONFIG_Q_SEGMENTATION
          av1_get_qindex(&cm->seg, i, i, xd->current_qindex);
#else
414
          av1_get_qindex(&cm->seg, i, xd->current_qindex);
415
#endif
416
#else
417 418
      const int current_qindex = xd->current_qindex;
#endif  // CONFIG_EXT_DELTA_Q
419
      for (int j = 0; j < av1_num_planes(cm); ++j) {
420 421 422 423 424
        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);
425 426 427 428
        xd->plane[j].seg_dequant_QTX[i][0] =
            av1_dc_quant_QTX(current_qindex, dc_delta_q, cm->bit_depth);
        xd->plane[j].seg_dequant_QTX[i][1] =
            av1_ac_quant_QTX(current_qindex, ac_delta_q, cm->bit_depth);
429
      }
430 431
    }
  }
432
  if (mbmi->skip) av1_reset_skip_context(xd, mi_row, mi_col, bsize);
433

434
  if (!is_inter_block(mbmi)) {
435 436
    const int num_planes = av1_num_planes(cm);
    for (int plane = 0; plane < AOMMIN(2, num_planes); ++plane) {
437
      if (mbmi->palette_mode_info.palette_size[plane])
438
        av1_decode_palette_tokens(xd, plane, r);
439
    }
440

441
    for (int plane = 0; plane < num_planes; ++plane) {
442
      const struct macroblockd_plane *const pd = &xd->plane[plane];
443
      const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
444 445
      const int stepr = tx_size_high_unit[tx_size];
      const int stepc = tx_size_wide_unit[tx_size];
446 447
      const BLOCK_SIZE plane_bsize =
          AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
448
      int row, col;
449 450
      const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
      const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
451 452
      if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
                               pd->subsampling_y))
453
        continue;
454 455 456 457 458 459 460 461 462 463
      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) {
464
        const int unit_height = AOMMIN(mu_blocks_high + row, max_blocks_high);
465 466 467 468 469 470 471 472 473
        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);
        }
      }
474 475
    }
  } else {
476 477
    int ref;

478 479
    for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
      const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
      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);
      }
498
    }
499

500
    av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
501

Yue Chen's avatar
Yue Chen committed
502
    if (mbmi->motion_mode == OBMC_CAUSAL) {
Yue Chen's avatar
Yue Chen committed
503 504 505
#if CONFIG_NCOBMC
      av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
#else
506
      av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Yue Chen's avatar
Yue Chen committed
507
#endif
508 509 510 511
    }
    // Reconstruction
    if (!mbmi->skip) {
      int eobtotal = 0;
512
      for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
513
        const struct macroblockd_plane *const pd = &xd->plane[plane];
514 515 516 517 518
        if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
                                 pd->subsampling_y))
          continue;
        const BLOCK_SIZE bsizec =
            scale_chroma_bsize(bsize, pd->subsampling_x, pd->subsampling_y);
519
        const BLOCK_SIZE plane_bsize =
520
            AOMMAX(BLOCK_4X4, get_plane_block_size(bsizec, pd));
521 522
        const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
        const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
523
        int row, col;
524

525 526 527 528 529 530 531 532 533
        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);

534
        TX_SIZE max_tx_size = get_vartx_max_txsize(
535
            xd, plane_bsize, pd->subsampling_x || pd->subsampling_y);
536 537 538 539 540 541 542 543 544 545 546 547
#if DISABLE_VARTX_FOR_CHROMA == 2
        // If the luma transform size is split at least one level, split the
        // chroma by one level. Otherwise use the  largest possible trasnform
        // size for chroma.
        if (plane && (pd->subsampling_x || pd->subsampling_y)) {
          const TX_SIZE l_max_tx_size = get_vartx_max_txsize(xd, bsizec, 0);
          const int is_split =
              (l_max_tx_size != mbmi->inter_tx_size[0][0] && bsize == bsizec &&
               txsize_to_bsize[l_max_tx_size] == bsizec);
          if (is_split) max_tx_size = sub_tx_size_map[max_tx_size];
        }
#endif  // DISABLE_VARTX_FOR_CHROMA == 2
548 549
        const int bh_var_tx = tx_size_high_unit[max_tx_size];
        const int bw_var_tx = tx_size_wide_unit[max_tx_size];
550 551 552
        int block = 0;
        int step =
            tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

        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;
              }
            }
569 570
          }
        }
571
      }
572 573
    }
  }
574
#if CONFIG_CFL
575 576 577 578 579
  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);
    }
  }
580
#endif  // CONFIG_CFL
581

582 583
  int reader_corrupted_flag = aom_reader_has_error(r);
  aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
584 585
}

586
#if NC_MODE_INFO
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
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];

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  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;
625
#if CONFIG_EXT_PARTITION_TYPES
626 627 628
#if CONFIG_EXT_PARTITION_TYPES_AB
#error NC_MODE_INFO+MOTION_VAR not yet supported for new HORZ/VERT_AB partitions
#endif
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    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;
651
#endif
652
    default: assert(0 && "Invalid partition type");
653 654 655 656
  }
}
#endif

657 658 659 660 661 662
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) {
663
  decode_mbmi_block(pbi, xd, mi_row, mi_col, r,
664 665 666 667
#if CONFIG_EXT_PARTITION_TYPES
                    partition,
#endif
                    bsize);
668

669
#if !(NC_MODE_INFO)
670
  decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
671
#endif
672 673
}

674 675
static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
                                     aom_reader *r, int has_rows, int has_cols,
676 677
                                     BLOCK_SIZE bsize) {
  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
678
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
679

680
  if (!has_rows && !has_cols) return PARTITION_SPLIT;
Jingning Han's avatar
Jingning Han committed
681

682 683
  assert(ctx >= 0);
  aom_cdf_prob *partition_cdf = ec_ctx->partition_cdf[ctx];
684
  if (has_rows && has_cols) {
685 686
    return (PARTITION_TYPE)aom_read_symbol(
        r, partition_cdf, partition_cdf_length(bsize), ACCT_STR);
687
  } else if (!has_rows && has_cols) {
688 689
    assert(bsize > BLOCK_8X8);
    aom_cdf_prob cdf[2];
690
    partition_gather_vert_alike(cdf, partition_cdf, bsize);
691
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
692 693 694
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
  } else {
    assert(has_rows && !has_cols);
695 696
    assert(bsize > BLOCK_8X8);
    aom_cdf_prob cdf[2];
697
    partition_gather_horz_alike(cdf, partition_cdf, bsize);
698
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
699
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
700
  }
701 702 703
}

// TODO(slavarnway): eliminate bsize and subsize in future commits
704 705
static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
                             int mi_row, int mi_col, aom_reader *r,
706
                             BLOCK_SIZE bsize) {
707
  AV1_COMMON *const cm = &pbi->common;
708
  const int num_8x8_wh = mi_size_wide[bsize];
709
  const int hbs = num_8x8_wh >> 1;
710 711 712
#if CONFIG_EXT_PARTITION_TYPES && CONFIG_EXT_PARTITION_TYPES_AB
  const int qbs = num_8x8_wh >> 2;
#endif
713 714
  PARTITION_TYPE partition;
  BLOCK_SIZE subsize;
715
#if CONFIG_EXT_PARTITION_TYPES
716 717
  const int quarter_step = num_8x8_wh / 4;
  int i;
718 719 720
#if !CONFIG_EXT_PARTITION_TYPES_AB
  BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
#endif
721
#endif
722 723 724
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
  const int has_cols = (mi_col + hbs) < cm->mi_cols;

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

727
  partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
728
                                  : read_partition(xd, mi_row, mi_col, r,
729
                                                   has_rows, has_cols, bsize);
730
  subsize = subsize_lookup[partition][bsize];  // get_subsize(bsize, partition);
731

732 733 734 735 736 737 738 739 740
  // 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]);
  }

741 742 743 744 745 746 747 748 749 750 751 752
#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))

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  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;
769
#if CONFIG_EXT_PARTITION_TYPES
770
#if CONFIG_EXT_PARTITION_TYPES_AB
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    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;
795
#else
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
    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;
816
#endif
817 818 819 820 821 822 823 824 825 826 827 828 829 830
    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;
831
#endif  // CONFIG_EXT_PARTITION_TYPES
832
    default: assert(0 && "Invalid partition type");
833 834
  }

835 836 837 838 839
#undef DEC_PARTITION
#undef DEC_BLOCK
#undef DEC_BLOCK_EPT_ARG
#undef DEC_BLOCK_STX_ARG

840
#if CONFIG_EXT_PARTITION_TYPES
841
  update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
842
#else
843 844 845
  // update partition context
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
846
    update_partition_context(xd, mi_row, mi_col, subsize, bsize);
847
#endif  // CONFIG_EXT_PARTITION_TYPES
848

849 850
#if CONFIG_LPF_SB
  if (bsize == cm->sb_size) {
851 852 853
    int filt_lvl;
    if (mi_row == 0 && mi_col == 0) {
      filt_lvl = aom_read_literal(r, 6, ACCT_STR);
854 855 856
      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;
857 858 859 860 861 862 863 864 865
    } 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;
      }
866

867 868 869 870 871 872 873 874 875 876
      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;
877

878
      if (reuse_prev_lvl) {
879
        filt_lvl = prev_lvl;
880 881
        curr_mbmi->delta = 0;
        curr_mbmi->sign = 0;
882
      } else {
883 884 885 886
        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;
887
        delta *= LPF_STEP;
888 889

        if (delta) {
890 891 892 893
          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;
894 895 896
          filt_lvl = sign ? prev_lvl + delta : prev_lvl - delta;
        } else {
          filt_lvl = prev_lvl;
897
          curr_mbmi->sign = 0;
898
        }
899 900
      }
    }
901 902

    av1_loop_filter_sb_level_init(cm, mi_row, mi_col, filt_lvl);
903 904 905
  }
#endif

906
  if (bsize == cm->sb_size) {
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    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;
      }
922 923
    }
  }
924
#if CONFIG_LOOP_RESTORATION
925
  for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
926
    int rcol0, rcol1, rrow0, rrow1, tile_tl_idx;
927 928
    if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
                                           &rcol0, &rcol1, &rrow0, &rrow1,
929 930
                                           &tile_tl_idx)) {
      const int rstride = cm->rst_info[plane].horz_units_per_tile;
931 932
      for (int rrow = rrow0; rrow < rrow1; ++rrow) {
        for (int rcol = rcol0; rcol < rcol1; ++rcol) {
933
          const int rtile_idx = tile_tl_idx + rcol + rrow * rstride;
934 935 936 937 938 939
          loop_restoration_read_sb_coeffs(cm, xd, r, plane, rtile_idx);
        }
      }
    }
  }
#endif
940 941
}

942
static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
Alex Converse's avatar
Alex Converse committed
943
                               const size_t read_size,
944
                               struct aom_internal_error_info *error_info,
945
                               aom_reader *r, uint8_t allow_update_cdf,
946 947 948 949
#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
950 951 952 953
  // 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))
954
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Alex Converse's avatar
Alex Converse committed
955 956
                       "Truncated packet or corrupt tile length");

957
#if CONFIG_ANS && ANS_MAX_SYMBOLS
958
  r->window_size = window_size;
959
#endif
960
  if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
961
    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Alex Converse's avatar
Alex Converse committed
962
                       "Failed to allocate bool decoder %d", 1);
963 964

  r->allow_update_cdf = allow_update_cdf;
Alex Converse's avatar
Alex Converse committed
965
}
966

967 968
static void setup_segmentation(AV1_COMMON *const cm,
                               struct aom_read_bit_buffer *rb) {
969
  struct segmentation *const seg = &cm->seg;
970 971 972 973
  int i, j;

  seg->update_map = 0;
  seg->update_data = 0;
974
  seg->temporal_update = 0;
975

976
  seg->enabled = aom_rb_read_bit(rb);
977
  if (!seg->enabled) return;
978 979

  // Segmentation map update
980 981 982
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    seg->update_map = 1;
  } else {
983
    seg->update_map = aom_rb_read_bit(rb);
984
  }
985
  if (seg->update_map) {
986 987 988
    if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
      seg->temporal_update = 0;
    } else {
989
      seg->temporal_update = aom_rb_read_bit(rb);
990
    }
991 992 993
  }

  // Segmentation data update
994
  seg->update_data = aom_rb_read_bit(rb);
995
  if (seg->update_data) {
996
    av1_clearall_segfeatures(seg);
997 998 999 1000

    for (i = 0; i < MAX_SEGMENTS; i++) {
      for (j = 0; j < SEG_LVL_MAX; j++) {
        int data = 0;
1001
        const int feature_enabled = aom_rb_read_bit(rb);
1002
        if (feature_enabled) {
1003 1004 1005 1006
          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;
1007
        }
1008
        av1_set_segdata(seg, i, j, data);
1009 1010 1011 1012 1013
      }
    }
  }
}

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
#if CONFIG_Q_SEGMENTATION
static void setup_q_segmentation(AV1_COMMON *const cm,
                                 struct aom_read_bit_buffer *rb) {
  int i;
  struct segmentation *const seg = &cm->seg;

  for (i = 0; i < MAX_SEGMENTS; i++) {
    if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
      seg->q_lvls = 0;
      return;
    }
  }

1027 1028 1029 1030
  if (!aom_rb_read_bit(rb)) {
    seg->q_lvls = 0;
    return;
  }
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

  seg->q_lvls = decode_unsigned_max(rb, MAX_SEGMENTS);

  for (i = 0; i < seg->q_lvls; i++) {
    int val = decode_unsigned_max(rb, MAXQ);
    val *= 1 - 2 * aom_rb_read_bit(rb);
    seg->q_delta[i] = val;
  }
}
#endif

1042
#if CONFIG_LOOP_RESTORATION
1043 1044
static void decode_restoration_mode(AV1_COMMON *cm,
                                    struct aom_read_bit_buffer *rb) {
1045 1046 1047
#if CONFIG_INTRABC
  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) return;
#endif  // CONFIG_INTRABC
1048 1049
  int all_none = 1, chroma_none = 1;
  for (int p = 0; p < av1_num_planes(cm); ++p) {
1050
    RestorationInfo *rsi = &cm->rst_info[p];
1051 1052 1053 1054
    if (aom_rb_read_bit(rb)) {
      rsi->frame_restoration_type =
          aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
    } else {
1055 1056
      rsi->frame_restoration_type =
          aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
1057
    }
1058 1059 1060 1061
    if (rsi->frame_restoration_type != RESTORE_NONE) {
      all_none = 0;
      chroma_none &= p == 0;
    }
1062
  }
1063
  if (!all_none) {
1064 1065 1066 1067 1068
    const int qsize = RESTORATION_TILESIZE_MAX >> 2;
    for (int p = 0; p < MAX_MB_PLANE; ++p)
      cm->rst_info[p].restoration_unit_size = qsize;

    RestorationInfo *rsi = &cm->rst_info[0];
1069
    rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1070
    if (rsi->restoration_unit_size != qsize) {
1071
      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1072
    }
1073 1074 1075 1076
  } else {
    const int size = RESTORATION_TILESIZE_MAX;
    for (int p = 0; p < MAX_MB_PLANE; ++p)
      cm->rst_info[p].restoration_unit_size = size;
1077
  }
1078

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
  if (av1_num_planes(cm) > 1) {
    int s = AOMMIN(cm->subsampling_x, cm->subsampling_y);
    if (s && !chroma_none) {
      cm->rst_info[1].restoration_unit_size =
          cm->rst_info[0].restoration_unit_size >> (aom_rb_read_bit(rb) * s);
    } else {
      cm->rst_info[1].restoration_unit_size =
          cm->rst_info[0].restoration_unit_size;
    }
    cm->rst_info[2].restoration_unit_size =
        cm->rst_info[1].restoration_unit_size;
1090
  }
1091 1092
}

1093
static void read_wiener_filter(int wiener_win, WienerInfo *wiener_info,
1094
                               WienerInfo *ref_wiener_info, aom_reader *rb) {
1095 1096 1097
  memset(wiener_info->vfilter, 0, sizeof(wiener_info->vfilter));
  memset(wiener_info->hfilter, 0, sizeof(wiener_info->hfilter));

1098 1099 1100 1101 1102 1103 1104 1105 1106
  if (wiener_win == WIENER_WIN)
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
        aom_read_primitive_refsubexpfin(
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
            WIENER_FILT_TAP0_SUBEXP_K,
            ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
        WIENER_FILT_TAP0_MINV;
  else
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] = 0;
1107
  wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
1108 1109 1110
      aom_read_primitive_refsubexpfin(
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
          WIENER_FILT_TAP1_SUBEXP_K,
1111
          ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1112
      WIENER_FILT_TAP1_MINV;
1113
  wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
1114 1115 1116
      aom_read_primitive_refsubexpfin(
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
          WIENER_FILT_TAP2_SUBEXP_K,
1117
          ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1118
      WIENER_FILT_TAP2_MINV;
1119
  // The central element has an implicit +WIENER_FILT_STEP
1120
  wiener_info->vfilter[WIENER_HALFWIN] =
1121 1122 1123
      -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
            wiener_info->vfilter[2]);

1124 1125 1126 1127 1128 1129 1130 1131 1132
  if (wiener_win == WIENER_WIN)
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
        aom_read_primitive_refsubexpfin(
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
            WIENER_FILT_TAP0_SUBEXP_K,
            ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
        WIENER_FILT_TAP0_MINV;
  else
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] = 0;
1133
  wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
1134 1135 1136
      aom_read_primitive_refsubexpfin(
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
          WIENER_FILT_TAP1_SUBEXP_K,
1137
          ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1138
      WIENER_FILT_TAP1_MINV;
1139
  wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
1140 1141 1142
      aom_read_primitive_refsubexpfin(
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
          WIENER_FILT_TAP2_SUBEXP_K,
1143
          ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1144
      WIENER_FILT_TAP2_MINV;
1145
  // The central element has an implicit +WIENER_FILT_STEP
1146
  wiener_info->hfilter[WIENER_HALFWIN] =
1147 1148
      -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
            wiener_info->hfilter[2]);
1149
  memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
1150 1151
}

1152 1153
static void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
                                SgrprojInfo *ref_sgrproj_info, aom_reader *rb) {
1154 1155
  sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
  sgrproj_info->xqd[0] =
1156 1157
      aom_read_primitive_refsubexpfin(
          rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1158
          ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1159
      SGRPROJ_PRJ_MIN0;
1160
  sgrproj_info->xqd[1] =
1161 1162
      aom_read_primitive_refsubexpfin(
          rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1163
          ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1164 1165
      SGRPROJ_PRJ_MIN1;
  memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
1166 1167
}

1168 1169 1170 1171
static void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
                                            MACROBLOCKD *xd,
                                            aom_reader *const r, int plane,
                                            int rtile_idx) {
1172 1173
  const RestorationInfo *rsi = &cm->rst_info[plane];
  RestorationUnitInfo *rui = &rsi->unit_info[rtile_idx];
1174 1175 1176 1177 1178 1179 1180
  if (rsi->frame_restoration_type == RESTORE_NONE) return;

  const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
  WienerInfo *wiener_info = xd->wiener_info + plane;
  SgrprojInfo *sgrproj_info = xd->sgrproj_info + plane;

  if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
1181
    rui->restoration_type =
1182 1183
        aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
                        RESTORE_SWITCHABLE_TYPES, ACCT_STR);