decodemv.c 81.7 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
 */

#include <assert.h>

14
15
16
17
18
19
20
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/entropymv.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconinter.h"
hui su's avatar
hui su committed
21
22
23
#if CONFIG_EXT_INTRA
#include "av1/common/reconintra.h"
#endif  // CONFIG_EXT_INTRA
24
#include "av1/common/seg_common.h"
Yue Chen's avatar
Yue Chen committed
25
26
27
#if CONFIG_WARPED_MOTION
#include "av1/common/warped_motion.h"
#endif  // CONFIG_WARPED_MOTION
28
29

#include "av1/decoder/decodeframe.h"
Jingning Han's avatar
Jingning Han committed
30
#include "av1/decoder/decodemv.h"
31

Yaowu Xu's avatar
Yaowu Xu committed
32
#include "aom_dsp/aom_dsp_common.h"
33

Michael Bebenita's avatar
Michael Bebenita committed
34
#define ACCT_STR __func__
35
#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
36
static INLINE int read_uniform(aom_reader *r, int n) {
37
38
39
  const int l = get_unsigned_bits(n);
  const int m = (1 << l) - n;
  const int v = aom_read_literal(r, l - 1, ACCT_STR);
hui su's avatar
hui su committed
40
41
42
43
  assert(l != 0);
  if (v < m)
    return v;
  else
Michael Bebenita's avatar
Michael Bebenita committed
44
    return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
hui su's avatar
hui su committed
45
}
46
#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
hui su's avatar
hui su committed
47

48
static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
49
50
51
  return (PREDICTION_MODE)
      av1_intra_mode_inv[aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR)];
}
Jingning Han's avatar
Jingning Han committed
52

53
54
55
56
57
58
59
60
61
#if CONFIG_DELTA_Q
static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
                             MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
  FRAME_COUNTS *counts = xd->counts;
  int sign, abs, reduced_delta_qindex = 0;
  BLOCK_SIZE bsize = mbmi->sb_type;
  const int b_col = mi_col & MAX_MIB_MASK;
  const int b_row = mi_row & MAX_MIB_MASK;
  const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Davies's avatar
Thomas Davies committed
62
63
64
65
66
67
68
69
  int rem_bits, thr;
  int i, smallval;
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif
70

71
  if ((bsize != BLOCK_LARGEST || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Davies's avatar
Thomas Davies committed
72
73
74
75
76
77
78
79
    abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
    smallval = (abs < DELTA_Q_SMALL);
    if (counts) {
      for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
      if (smallval) counts->delta_q[abs][0]++;
    }

    if (!smallval) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
      rem_bits = aom_read_literal(r, 3, ACCT_STR);
      thr = (1 << rem_bits) + 1;
      abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
    }

    if (abs) {
      sign = aom_read_bit(r, ACCT_STR);
    } else {
      sign = 1;
    }

    reduced_delta_qindex = sign ? -abs : abs;
  }
  return reduced_delta_qindex;
}
Fangwen Fu's avatar
Fangwen Fu committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#if CONFIG_EXT_DELTA_Q
static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
                              MB_MODE_INFO *const mbmi, int mi_col,
                              int mi_row) {
  FRAME_COUNTS *counts = xd->counts;
  int sign, abs, reduced_delta_lflevel = 0;
  BLOCK_SIZE bsize = mbmi->sb_type;
  const int b_col = mi_col & MAX_MIB_MASK;
  const int b_row = mi_row & MAX_MIB_MASK;
  const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
  int rem_bits, thr;
  int i, smallval;
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

  if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_lf_flag) {
    abs =
        aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
    smallval = (abs < DELTA_LF_SMALL);
    if (counts) {
      for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
      if (smallval) counts->delta_lf[abs][0]++;
    }
    if (!smallval) {
      rem_bits = aom_read_literal(r, 3, ACCT_STR);
      thr = (1 << rem_bits) + 1;
      abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
    }

    if (abs) {
      sign = aom_read_bit(r, ACCT_STR);
    } else {
      sign = 1;
    }

    reduced_delta_lflevel = sign ? -abs : abs;
  }
  return reduced_delta_lflevel;
}
#endif
139
140
#endif

141
static PREDICTION_MODE read_intra_mode_y(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
142
                                         aom_reader *r, int size_group) {
Jingning Han's avatar
Jingning Han committed
143
  const PREDICTION_MODE y_mode =
144
      read_intra_mode(r, ec_ctx->y_mode_cdf[size_group]);
Jingning Han's avatar
Jingning Han committed
145
  FRAME_COUNTS *counts = xd->counts;
146
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
147
148
149
  return y_mode;
}

150
151
static PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
                                          MACROBLOCKD *xd, aom_reader *r,
Jingning Han's avatar
Jingning Han committed
152
                                          PREDICTION_MODE y_mode) {
153
  const PREDICTION_MODE uv_mode =
154
      read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Jingning Han's avatar
Jingning Han committed
155
  FRAME_COUNTS *counts = xd->counts;
156
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
157
158
159
  return uv_mode;
}

Luc Trudeau's avatar
Luc Trudeau committed
160
#if CONFIG_CFL
161
162
163
164
165
166
167
168
169
170
171
172
static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r, int skip,
                           CFL_SIGN_TYPE signs_out[CFL_PRED_PLANES]) {
  if (skip) {
    signs_out[CFL_PRED_U] = CFL_SIGN_POS;
    signs_out[CFL_PRED_V] = CFL_SIGN_POS;
    return 0;
  } else {
    const int ind = aom_read_symbol(r, ec_ctx->cfl_alpha_cdf, CFL_ALPHABET_SIZE,
                                    "cfl:alpha");
    // Signs are only coded for nonzero values
    // sign == 0 implies negative alpha
    // sign == 1 implies positive alpha
173
    signs_out[CFL_PRED_U] = cfl_alpha_codes[ind][CFL_PRED_U]
174
175
                                ? aom_read_bit(r, "cfl:sign")
                                : CFL_SIGN_POS;
176
    signs_out[CFL_PRED_V] = cfl_alpha_codes[ind][CFL_PRED_V]
177
178
179
180
181
                                ? aom_read_bit(r, "cfl:sign")
                                : CFL_SIGN_POS;

    return ind;
  }
Luc Trudeau's avatar
Luc Trudeau committed
182
183
184
}
#endif

185
#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xu's avatar
Yaowu Xu committed
186
187
188
static INTERINTRA_MODE read_interintra_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
                                            aom_reader *r, int size_group) {
  const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_tree(
Michael Bebenita's avatar
Michael Bebenita committed
189
190
      r, av1_interintra_mode_tree, cm->fc->interintra_mode_prob[size_group],
      ACCT_STR);
191
  FRAME_COUNTS *counts = xd->counts;
192
  if (counts) ++counts->interintra_mode[size_group][ii_mode];
193
194
  return ii_mode;
}
195
#endif  // CONFIG_EXT_INTER && CONFIG_INTERINTRA
196

197
static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
198
                                       aom_reader *r, int16_t ctx) {
199
  FRAME_COUNTS *counts = xd->counts;
200
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
201
  aom_prob mode_prob = ec_ctx->newmv_prob[mode_ctx];
202

Michael Bebenita's avatar
Michael Bebenita committed
203
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
204
    if (counts) ++counts->newmv_mode[mode_ctx][0];
205
    return NEWMV;
206
  }
207
  if (counts) ++counts->newmv_mode[mode_ctx][1];
208

209
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
210

211
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
212

213
  mode_prob = ec_ctx->zeromv_prob[mode_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
214
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
215
    if (counts) ++counts->zeromv_mode[mode_ctx][0];
216
217
    return ZEROMV;
  }
218
  if (counts) ++counts->zeromv_mode[mode_ctx][1];
219

220
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
221

222
223
224
  if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
  if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
  if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
225

226
  mode_prob = ec_ctx->refmv_prob[mode_ctx];
227

Michael Bebenita's avatar
Michael Bebenita committed
228
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
229
    if (counts) ++counts->refmv_mode[mode_ctx][0];
230

231
232
    return NEARESTMV;
  } else {
233
    if (counts) ++counts->refmv_mode[mode_ctx][1];
234
235
236
237
238
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
Jingning Han's avatar
Jingning Han committed
239
240
}

Yaowu Xu's avatar
Yaowu Xu committed
241
242
243
static void read_drl_idx(const AV1_COMMON *cm, MACROBLOCKD *xd,
                         MB_MODE_INFO *mbmi, aom_reader *r) {
  uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
244
245
  mbmi->ref_mv_idx = 0;

246
247
248
#if CONFIG_EXT_INTER
  if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
#else
249
  if (mbmi->mode == NEWMV) {
250
#endif
251
252
253
    int idx;
    for (idx = 0; idx < 2; ++idx) {
      if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xu's avatar
Yaowu Xu committed
254
255
        uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
        aom_prob drl_prob = cm->fc->drl_prob[drl_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
256
        if (!aom_read(r, drl_prob, ACCT_STR)) {
257
          mbmi->ref_mv_idx = idx;
258
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
259
260
261
          return;
        }
        mbmi->ref_mv_idx = idx + 1;
262
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
263
264
265
266
      }
    }
  }

David Barker's avatar
David Barker committed
267
  if (have_nearmv_in_inter_mode(mbmi->mode)) {
268
269
270
271
272
273
    int idx;
    // Offset the NEARESTMV mode.
    // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
    // mode is factored in.
    for (idx = 1; idx < 3; ++idx) {
      if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xu's avatar
Yaowu Xu committed
274
275
        uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
        aom_prob drl_prob = cm->fc->drl_prob[drl_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
276
        if (!aom_read(r, drl_prob, ACCT_STR)) {
277
          mbmi->ref_mv_idx = idx - 1;
278
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
279
280
          return;
        }
281
        mbmi->ref_mv_idx = idx;
282
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
283
284
285
286
287
      }
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
288
289
#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
290
291
292
293
294
295
296
                                    MODE_INFO *mi, aom_reader *r) {
  MB_MODE_INFO *mbmi = &mi->mbmi;
  const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
      0, xd->global_motion,
#endif  // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
      mi);
Yue Chen's avatar
Yue Chen committed
297
298
  int motion_mode;
  FRAME_COUNTS *counts = xd->counts;
Yaowu Xu's avatar
Yaowu Xu committed
299

Yue Chen's avatar
Yue Chen committed
300
301
302
303
304
305
306
307
  if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
  if (last_motion_mode_allowed == OBMC_CAUSAL) {
    motion_mode = aom_read(r, cm->fc->obmc_prob[mbmi->sb_type], ACCT_STR);
    if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
  } else {
#endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
308
309
310
311
312
    motion_mode =
        aom_read_tree(r, av1_motion_mode_tree,
                      cm->fc->motion_mode_prob[mbmi->sb_type], ACCT_STR);
    if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yue Chen's avatar
Yue Chen committed
313
#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
314
  }
Yue Chen's avatar
Yue Chen committed
315
#endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
316
317
318
}
#endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION

319
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
320
321
static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
                                                aom_reader *r, int16_t ctx) {
Michael Bebenita's avatar
Michael Bebenita committed
322
323
324
  const int mode =
      aom_read_tree(r, av1_inter_compound_mode_tree,
                    cm->fc->inter_compound_mode_probs[ctx], ACCT_STR);
325
326
  FRAME_COUNTS *counts = xd->counts;

327
  if (counts) ++counts->inter_compound_mode[ctx][mode];
328
329
330
331
332
333

  assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
  return NEAREST_NEARESTMV + mode;
}
#endif  // CONFIG_EXT_INTER

334
static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita's avatar
Michael Bebenita committed
335
  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
336
337
}

338
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
339
static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
340
                               MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
341
342
                               TX_SIZE tx_size, int depth, int blk_row,
                               int blk_col, aom_reader *r) {
343
  int is_split = 0;
344
345
  const int tx_row = blk_row >> 1;
  const int tx_col = blk_col >> 1;
346
347
  const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
  const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
348
  int ctx = txfm_partition_context(xd->above_txfm_context + tx_col,
349
350
                                   xd->left_txfm_context + tx_row,
                                   mbmi->sb_type, tx_size);
clang-format's avatar
clang-format committed
351
  TX_SIZE(*const inter_tx_size)
352
353
354
  [MAX_MIB_SIZE] =
      (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
355

Jingning Han's avatar
Jingning Han committed
356
  if (depth == MAX_VARTX_DEPTH) {
357
358
    int idx, idy;
    inter_tx_size[0][0] = tx_size;
359
360
    for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
      for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
361
362
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
363
    mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
364
365
    if (counts) ++counts->txfm_partition[ctx][0];
    txfm_partition_update(xd->above_txfm_context + tx_col,
366
                          xd->left_txfm_context + tx_row, tx_size, tx_size);
367
368
369
    return;
  }

Michael Bebenita's avatar
Michael Bebenita committed
370
  is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
371
372

  if (is_split) {
373
374
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
    const int bsl = tx_size_wide_unit[sub_txs];
375
    int i;
376

377
    if (counts) ++counts->txfm_partition[ctx][1];
378

379
    if (tx_size == TX_8X8) {
380
      int idx, idy;
381
      inter_tx_size[0][0] = sub_txs;
382
383
      for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
        for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
384
          inter_tx_size[idy][idx] = inter_tx_size[0][0];
385
      mbmi->tx_size = sub_txs;
386
      mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
387
      txfm_partition_update(xd->above_txfm_context + tx_col,
388
                            xd->left_txfm_context + tx_row, sub_txs, tx_size);
389
390
391
392
393
      return;
    }

    assert(bsl > 0);
    for (i = 0; i < 4; ++i) {
394
395
396
      int offsetr = blk_row + (i >> 1) * bsl;
      int offsetc = blk_col + (i & 0x01) * bsl;
      read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
397
                         offsetc, r);
398
399
    }
  } else {
400
    int idx, idy;
401
    inter_tx_size[0][0] = tx_size;
402
403
    for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
      for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
404
405
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
406
    mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
407
    if (counts) ++counts->txfm_partition[ctx][0];
408
    txfm_partition_update(xd->above_txfm_context + tx_col,
409
                          xd->left_txfm_context + tx_row, tx_size, tx_size);
410
411
412
413
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
414
415
static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
                                     int tx_size_cat, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
416
417
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
418
419
420
421
422
423
424
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

425
426
  const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
                                    tx_size_cat + 2, ACCT_STR);
427
428
429
430
  const TX_SIZE tx_size = depth_to_tx_size(depth);
#if CONFIG_RECT_TX
  assert(!is_rect_tx(tx_size));
#endif  // CONFIG_RECT_TX
431
  if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
432
  return tx_size;
Jingning Han's avatar
Jingning Han committed
433
434
}

435
436
437
438
static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
                            int allow_select_inter, aom_reader *r) {
  const TX_MODE tx_mode = cm->tx_mode;
  const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
439
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
440
#if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX
441
  if (bsize > BLOCK_4X4) {
442
#else
443
  if (bsize >= BLOCK_8X8) {
444
445
446
447
#endif  // CONFIG_CB4X4 && CONFIG_VAR_TX
    if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
      const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
                                           : intra_tx_size_cat_lookup[bsize];
448
      const TX_SIZE coded_tx_size =
449
          read_selected_tx_size(cm, xd, tx_size_cat, r);
450
#if CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
451
452
      if (coded_tx_size > max_txsize_lookup[bsize]) {
        assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
453
#if CONFIG_EXT_TX && CONFIG_RECT_TX_EXT
454
455
456
457
458
459
460
461
        if (is_quarter_tx_allowed(xd, &xd->mi[0]->mbmi, is_inter)) {
          int quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
          FRAME_COUNTS *counts = xd->counts;

          if (counts) ++counts->quarter_tx_size[quarter_tx];
          return quarter_tx ? quarter_txsize_lookup[bsize]
                            : max_txsize_rect_lookup[bsize];
        }
462
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX_EXT
463

464
465
        return max_txsize_rect_lookup[bsize];
      }
466
467
#else
      assert(coded_tx_size <= max_txsize_lookup[bsize]);
468
#endif  // CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
469
      return coded_tx_size;
470
    } else {
471
      return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
472
473
    }
  } else {
474
#if CONFIG_EXT_TX && CONFIG_RECT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
475
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
476
477
478
    return max_txsize_rect_lookup[bsize];
#else
    return TX_4X4;
479
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
480
  }
Jingning Han's avatar
Jingning Han committed
481
482
}

Yaowu Xu's avatar
Yaowu Xu committed
483
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
484
485
486
487
488
                              int mi_offset, int x_mis, int y_mis) {
  int x, y, segment_id = INT_MAX;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
489
      segment_id =
Yaowu Xu's avatar
Yaowu Xu committed
490
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
491
492
493
494
495

  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
496
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
497
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
498
499
500
501
502
503
504
505
506
  int x, y;

  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
      cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
507
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
508
                                 int mi_offset, int x_mis, int y_mis,
Yaowu Xu's avatar
Yaowu Xu committed
509
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
510
  struct segmentation *const seg = &cm->seg;
511
512
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
513
514
  int segment_id;

515
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
516

517
  assert(seg->update_map && !seg->temporal_update);
Jingning Han's avatar
Jingning Han committed
518

519
  segment_id = read_segment_id(r, segp);
520
  if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
521
522
523
524
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
525
static void copy_segment_id(const AV1_COMMON *cm,
526
527
528
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
529
530
531
532
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
533
534
535
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
536
537
}

Yaowu Xu's avatar
Yaowu Xu committed
538
539
static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                 int mi_row, int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
540
  struct segmentation *const seg = &cm->seg;
541
542
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
543
544
545
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  int predicted_segment_id, segment_id;
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
546
547
  const int bw = mi_size_wide[mbmi->sb_type];
  const int bh = mi_size_high[mbmi->sb_type];
Jingning Han's avatar
Jingning Han committed
548
549

  // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xu's avatar
Yaowu Xu committed
550
551
  const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
  const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Jingning Han's avatar
Jingning Han committed
552

553
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
554

555
556
557
558
  predicted_segment_id = cm->last_frame_seg_map
                             ? dec_get_segment_id(cm, cm->last_frame_seg_map,
                                                  mi_offset, x_mis, y_mis)
                             : 0;
Jingning Han's avatar
Jingning Han committed
559
560
561
562
563
564
565
566

  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
                    mi_offset, x_mis, y_mis);
    return predicted_segment_id;
  }

  if (seg->temporal_update) {
Yaowu Xu's avatar
Yaowu Xu committed
567
568
    const int ctx = av1_get_pred_context_seg_id(xd);
    const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita's avatar
Michael Bebenita committed
569
    mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
570
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
571
572
573
574
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
575
      if (counts) ++counts->seg.tree_mispred[segment_id];
576
    }
Jingning Han's avatar
Jingning Han committed
577
  } else {
578
    segment_id = read_segment_id(r, segp);
579
    if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
580
581
582
583
584
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
585
586
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
587
588
589
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
590
    const int ctx = av1_get_skip_context(xd);
Michael Bebenita's avatar
Michael Bebenita committed
591
    const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
592
    FRAME_COUNTS *counts = xd->counts;
593
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
594
595
596
597
    return skip;
  }
}

598
#if CONFIG_PALETTE
599
600
#if CONFIG_PALETTE_DELTA_ENCODING
static int uint16_compare(const void *a, const void *b) {
601
602
  const uint16_t va = *(const uint16_t *)a;
  const uint16_t vb = *(const uint16_t *)b;
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
  return va - vb;
}

static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
                                  PALETTE_MODE_INFO *const pmi, aom_reader *r) {
  uint16_t color_cache[2 * PALETTE_MAX_SIZE];
  const MODE_INFO *const above_mi = xd->above_mi;
  const MODE_INFO *const left_mi = xd->left_mi;
  const int n_cache = av1_get_palette_cache(above_mi, left_mi, 0, color_cache);
  const int n = pmi->palette_size[0];
  int idx = 0;
  for (int i = 0; i < n_cache && idx < n; ++i)
    if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
  if (idx < n) {
    pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
    if (idx < n) {
      const int min_bits = bit_depth - 3;
      int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
      int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
      for (; idx < n; ++idx) {
        const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
        pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
        range -= delta;
        bits = AOMMIN(bits, av1_ceil_log2(range));
      }
    }
  }
  qsort(pmi->palette_colors, n, sizeof(pmi->palette_colors[0]), uint16_compare);
}

static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
                                   PALETTE_MODE_INFO *const pmi,
                                   aom_reader *r) {
  const int n = pmi->palette_size[1];
  // U channel colors.
  uint16_t color_cache[2 * PALETTE_MAX_SIZE];
  const MODE_INFO *const above_mi = xd->above_mi;
  const MODE_INFO *const left_mi = xd->left_mi;
  const int n_cache = av1_get_palette_cache(above_mi, left_mi, 1, color_cache);
  int idx = PALETTE_MAX_SIZE;
  for (int i = 0; i < n_cache && idx < PALETTE_MAX_SIZE + n; ++i)
    if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
  if (idx < PALETTE_MAX_SIZE + n) {
    pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
    if (idx < PALETTE_MAX_SIZE + n) {
      const int min_bits = bit_depth - 3;
      int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
      int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
      for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
        const int delta = aom_read_literal(r, bits, ACCT_STR);
        pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
        range -= delta;
        bits = AOMMIN(bits, av1_ceil_log2(range));
      }
    }
  }
  qsort(pmi->palette_colors + PALETTE_MAX_SIZE, n,
        sizeof(pmi->palette_colors[0]), uint16_compare);

  // V channel colors.
  if (aom_read_bit(r, ACCT_STR)) {  // Delta encoding.
    const int min_bits_v = bit_depth - 4;
    const int max_val = 1 << bit_depth;
    int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
    pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
        aom_read_literal(r, bit_depth, ACCT_STR);
    for (int i = 1; i < n; ++i) {
      int delta = aom_read_literal(r, bits, ACCT_STR);
      if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
      int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
      if (val < 0) val += max_val;
      if (val >= max_val) val -= max_val;
      pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
    }
  } else {
    for (int i = 0; i < n; ++i) {
      pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
          aom_read_literal(r, bit_depth, ACCT_STR);
    }
  }
}
#endif  // CONFIG_PALETTE_DELTA_ENCODING

Yaowu Xu's avatar
Yaowu Xu committed
686
687
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
hui su's avatar
hui su committed
688
689
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
690
  const MODE_INFO *const above_mi = xd->above_mi;
691
  const MODE_INFO *const left_mi = xd->left_mi;
hui su's avatar
hui su committed
692
  const BLOCK_SIZE bsize = mbmi->sb_type;
693
  int n;
694
695
696
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;

  if (mbmi->mode == DC_PRED) {
697
    int palette_y_mode_ctx = 0;
698
    if (above_mi)
699
700
      palette_y_mode_ctx +=
          (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
701
    if (left_mi)
702
703
704
705
706
      palette_y_mode_ctx +=
          (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
    if (aom_read(r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
                                                   [palette_y_mode_ctx],
                 ACCT_STR)) {
707
      pmi->palette_size[0] =
Yaowu Xu's avatar
Yaowu Xu committed
708
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
709
710
                        av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
711
          2;
712
      n = pmi->palette_size[0];
hui su's avatar
hui su committed
713
#if CONFIG_PALETTE_DELTA_ENCODING
714
      read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui su's avatar
hui su committed
715
#else
716
      int i;
717
      for (i = 0; i < n; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
718
        pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui su's avatar
hui su committed
719
#endif  // CONFIG_PALETTE_DELTA_ENCODING
720
721
722
723
      xd->plane[0].color_index_map[0] = read_uniform(r, n);
      assert(xd->plane[0].color_index_map[0] < n);
    }
  }
hui su's avatar
hui su committed
724

725
  if (mbmi->uv_mode == DC_PRED) {
726
727
    const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
    if (aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
Michael Bebenita's avatar
Michael Bebenita committed
728
                 ACCT_STR)) {
729
      pmi->palette_size[1] =
Yaowu Xu's avatar
Yaowu Xu committed
730
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
731
732
                        av1_default_palette_uv_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
733
          2;
734
      n = pmi->palette_size[1];
hui su's avatar
hui su committed
735
#if CONFIG_PALETTE_DELTA_ENCODING
736
      read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui su's avatar
hui su committed
737
#else
738
      int i;
739
740
      for (i = 0; i < n; ++i) {
        pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
741
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
742
        pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
743
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
744
      }
hui su's avatar
hui su committed
745
#endif  // CONFIG_PALETTE_DELTA_ENCODING
746
747
748
      xd->plane[1].color_index_map[0] = read_uniform(r, n);
      assert(xd->plane[1].color_index_map[0] < n);
    }
hui su's avatar
hui su committed
749
750
  }
}
751
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
752

753
754
755
#if CONFIG_FILTER_INTRA
static void read_filter_intra_mode_info(AV1_COMMON *const cm,
                                        MACROBLOCKD *const xd, aom_reader *r) {
hui su's avatar
hui su committed
756
757
758
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  FRAME_COUNTS *counts = xd->counts;
759
760
  FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
      &mbmi->filter_intra_mode_info;
hui su's avatar
hui su committed
761

762
763
764
765
766
  if (mbmi->mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[0] == 0
#endif  // CONFIG_PALETTE
      ) {
767
768
769
770
    filter_intra_mode_info->use_filter_intra_mode[0] =
        aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
    if (filter_intra_mode_info->use_filter_intra_mode[0]) {
      filter_intra_mode_info->filter_intra_mode[0] =
hui su's avatar
hui su committed
771
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
772
    }
773
    if (counts) {
clang-format's avatar
clang-format committed
774
775
      ++counts
            ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
776
    }
hui su's avatar
hui su committed
777
  }
778
779
780
781
782
  if (mbmi->uv_mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[1] == 0
#endif  // CONFIG_PALETTE
      ) {
783
784
785
786
    filter_intra_mode_info->use_filter_intra_mode[1] =
        aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
    if (filter_intra_mode_info->use_filter_intra_mode[1]) {
      filter_intra_mode_info->filter_intra_mode[1] =
hui su's avatar
hui su committed
787
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
788
    }
789
    if (counts) {
clang-format's avatar
clang-format committed
790
791
      ++counts
            ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
792
    }
hui su's avatar
hui su committed
793
794
  }
}
795
#endif  // CONFIG_FILTER_INTRA
796

797
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
798
799
static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                  aom_reader *r) {
800
801
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
hui su's avatar
hui su committed
802
#if CONFIG_INTRA_INTERP
803
804
805
806
807
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
#else
  FRAME_CONTEXT *const ec_ctx = cm->fc;
#endif  // CONFIG_EC_ADAPT
Yaowu Xu's avatar
Yaowu Xu committed
808
  const int ctx = av1_get_pred_context_intra_interp(xd);
809
  int p_angle;
hui su's avatar
hui su committed
810
#endif  // CONFIG_INTRA_INTERP
811

hui su's avatar
hui su committed
812
  (void)cm;
813
  if (bsize < BLOCK_8X8) return;
814

hui su's avatar
hui su committed
815
  if (av1_is_directional_mode(mbmi->mode, bsize)) {
816
    mbmi->angle_delta[0] =
hui su's avatar
hui su committed
817
        read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
hui su's avatar
hui su committed
818
#if CONFIG_INTRA_INTERP
hui su's avatar
hui su committed
819
    p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xu's avatar
Yaowu Xu committed
820
    if (av1_is_intra_filter_switchable(p_angle)) {
821
      FRAME_COUNTS *counts = xd->counts;
822
823
      mbmi->intra_filter = aom_read_symbol(r, ec_ctx->intra_filter_cdf[ctx],
                                           INTRA_FILTERS, ACCT_STR);
824
      if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
825
826
827
    } else {
      mbmi->intra_filter = INTRA_FILTER_LINEAR;
    }
hui su's avatar
hui su committed
828
#endif  // CONFIG_INTRA_INTERP
829
830
  }

hui su's avatar
hui su committed
831
  if (av1_is_directional_mode(mbmi->uv_mode, bsize)) {
832
    mbmi->angle_delta[1] =
hui su's avatar
hui su committed
833
        read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
834
835
  }
}
hui su's avatar
hui su committed
836
837
#endif  // CONFIG_EXT_INTRA

838
void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
839
#if CONFIG_SUPERTX
840
                      int supertx_enabled,
Jingning Han's avatar
Jingning Han committed
841
#endif
Angie Chiang's avatar
Angie Chiang committed
842
#if CONFIG_TXK_SEL
843
                      int block, int plane,
844
845
846
#endif
                      aom_reader *r) {
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han's avatar
Jingning Han committed
847
  const int inter_block = is_inter_block(mbmi);
848
849
850
#if CONFIG_VAR_TX
  const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
#else
Jingning Han's avatar
Jingning Han committed
851
  const TX_SIZE tx_size = mbmi->tx_size;
852
#endif
853
854
855
856
857
858
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

Angie Chiang's avatar
Angie Chiang committed
859
#if !CONFIG_TXK_SEL
860
861
  TX_TYPE *tx_type = &mbmi->tx_type;
#else
862
863
  // only y plane's tx_type is transmitted
  if (plane > 0) return;
864
865
866
  TX_TYPE *tx_type = &mbmi->txk_type[block];
#endif

Jingning Han's avatar
Jingning Han committed
867
868
  if (!FIXED_TX_TYPE) {
#if CONFIG_EXT_TX
869
    const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
870
871
    if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
                         cm->reduced_tx_set_used) > 1 &&
872
873
874
        ((!cm->seg.enabled && cm->base_qindex > 0) ||
         (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
        !mbmi->skip &&
Jingning Han's avatar
Jingning Han committed
875
876
877
878
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
879
880
      const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
                                      cm->reduced_tx_set_used);
Jingning Han's avatar
Jingning Han committed
881
882
883
884
      FRAME_COUNTS *counts = xd->counts;

      if (inter_block) {
        if (eset > 0) {
885
          *tx_type = av1_ext_tx_inter_inv[eset][aom_read_symbol(
886
887
              r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
              ext_tx_cnt_inter[eset], ACCT_STR)];
888
          if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
Jingning Han's avatar
Jingning Han committed
889
890
891
        }
      } else if (ALLOW_INTRA_EXT_TX) {
        if (eset > 0) {
892
          *tx_type = av1_ext_tx_intra_inv[eset][aom_read_symbol(
893
894
              r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
              ext_tx_cnt_intra[eset], ACCT_STR)];
Jingning Han's avatar
Jingning Han committed
895
          if (counts)
896
            ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
Jingning Han's avatar
Jingning Han committed
897
898
899
        }
      }
    } else {
900
      *tx_type = DCT_DCT;
Jingning Han's avatar
Jingning Han committed
901
902
    }
#else
903
904
905
906
907

    if (tx_size < TX_32X32 &&
        ((!cm->seg.enabled && cm->base_qindex > 0) ||
         (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
        !mbmi->skip &&
Jingning Han's avatar
Jingning Han committed
908
909
910
911
912
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
      FRAME_COUNTS *counts = xd->counts;
913

Jingning Han's avatar
Jingning Han committed
914
      if (inter_block) {
915
        *tx_type = av1_ext_tx_inv[aom_read_symbol(
916
            r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
917
        if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Jingning Han's avatar
Jingning Han committed
918
919
      } else {
        const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
920
        *tx_type = av1_ext_tx_inv[aom_read_symbol(