decodeframe.c 85.4 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11 12 13 14
 */

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

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

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

Yaowu Xu's avatar
Yaowu Xu committed
30
#include "av1/common/alloccommon.h"
Yaowu Xu's avatar
Yaowu Xu committed
31
#if CONFIG_CLPF
Yaowu Xu's avatar
Yaowu Xu committed
32
#include "av1/common/clpf.h"
Yaowu Xu's avatar
Yaowu Xu committed
33
#endif
Yaowu Xu's avatar
Yaowu Xu committed
34
#include "av1/common/common.h"
35
#if CONFIG_DERING
Yaowu Xu's avatar
Yaowu Xu committed
36
#include "av1/common/dering.h"
37
#endif  // CONFIG_DERING
Yaowu Xu's avatar
Yaowu Xu committed
38 39 40 41 42 43
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/idct.h"
#include "av1/common/pred_common.h"
#include "av1/common/quant_common.h"
#include "av1/common/reconinter.h"
Jingning Han's avatar
Jingning Han committed
44
#include "av1/common/reconintra.h"
Yaowu Xu's avatar
Yaowu Xu committed
45
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
46
#include "av1/common/thread_common.h"
Yaowu Xu's avatar
Yaowu Xu committed
47
#include "av1/common/tile_common.h"
48

Yaowu Xu's avatar
Yaowu Xu committed
49 50 51
#include "av1/decoder/decodeframe.h"
#include "av1/decoder/decodemv.h"
#include "av1/decoder/decoder.h"
Jingning Han's avatar
Jingning Han committed
52
#include "av1/decoder/detokenize.h"
Yaowu Xu's avatar
Yaowu Xu committed
53
#include "av1/decoder/dsubexp.h"
Jingning Han's avatar
Jingning Han committed
54

55
#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita's avatar
Michael Bebenita committed
56
#define ACCT_STR __func__
Jingning Han's avatar
Jingning Han committed
57

58
static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Jingning Han's avatar
Jingning Han committed
59
  int i;
clang-format's avatar
clang-format committed
60
  if (frame_is_intra_only(cm)) return 0;
Jingning Han's avatar
Jingning Han committed
61
  for (i = 1; i < REFS_PER_FRAME; ++i)
clang-format's avatar
clang-format committed
62
    if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
Jingning Han's avatar
Jingning Han committed
63 64 65 66

  return 0;
}

67
static void setup_compound_reference_mode(AV1_COMMON *cm) {
68 69 70 71 72 73 74 75
#if CONFIG_EXT_REFS
  cm->comp_fwd_ref[0] = LAST_FRAME;
  cm->comp_fwd_ref[1] = LAST2_FRAME;
  cm->comp_fwd_ref[2] = LAST3_FRAME;
  cm->comp_fwd_ref[3] = GOLDEN_FRAME;
  cm->comp_bwd_ref[0] = BWDREF_FRAME;
  cm->comp_bwd_ref[1] = ALTREF_FRAME;
#else
Jingning Han's avatar
Jingning Han committed
76
  if (cm->ref_frame_sign_bias[LAST_FRAME] ==
clang-format's avatar
clang-format committed
77
      cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
Jingning Han's avatar
Jingning Han committed
78 79 80 81
    cm->comp_fixed_ref = ALTREF_FRAME;
    cm->comp_var_ref[0] = LAST_FRAME;
    cm->comp_var_ref[1] = GOLDEN_FRAME;
  } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
clang-format's avatar
clang-format committed
82
             cm->ref_frame_sign_bias[ALTREF_FRAME]) {
Jingning Han's avatar
Jingning Han committed
83 84 85 86 87 88 89 90
    cm->comp_fixed_ref = GOLDEN_FRAME;
    cm->comp_var_ref[0] = LAST_FRAME;
    cm->comp_var_ref[1] = ALTREF_FRAME;
  } else {
    cm->comp_fixed_ref = LAST_FRAME;
    cm->comp_var_ref[0] = GOLDEN_FRAME;
    cm->comp_var_ref[1] = ALTREF_FRAME;
  }
91
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
92 93 94 95 96 97
}

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

Adrian Grange's avatar
Adrian Grange committed
98 99
static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
  const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
Jingning Han's avatar
Jingning Han committed
100 101 102
  return data > max ? max : data;
}

103
#if CONFIG_MISC_FIXES
Adrian Grange's avatar
Adrian Grange committed
104 105
static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb) {
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
106 107
}
#else
Adrian Grange's avatar
Adrian Grange committed
108
static TX_MODE read_tx_mode(aom_reader *r) {
Michael Bebenita's avatar
Michael Bebenita committed
109 110
  TX_MODE tx_mode = aom_read_literal(r, 2, ACCT_STR);
  if (tx_mode == ALLOW_32X32) tx_mode += aom_read_bit(r, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
111 112
  return tx_mode;
}
113
#endif
Jingning Han's avatar
Jingning Han committed
114

Adrian Grange's avatar
Adrian Grange committed
115
static void read_tx_mode_probs(struct tx_probs *tx_probs, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
116 117 118
  int i, j;

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
Jingning Han's avatar
Jingning Han committed
119
    for (j = TX_4X4; j < TX_SIZES - 3; ++j)
Michael Bebenita's avatar
Michael Bebenita committed
120
      av1_diff_update_prob(r, &tx_probs->p8x8[i][j], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
121 122

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
Jingning Han's avatar
Jingning Han committed
123
    for (j = TX_4X4; j < TX_SIZES - 2; ++j)
Michael Bebenita's avatar
Michael Bebenita committed
124
      av1_diff_update_prob(r, &tx_probs->p16x16[i][j], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
125 126

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
Jingning Han's avatar
Jingning Han committed
127
    for (j = TX_4X4; j < TX_SIZES - 1; ++j)
Michael Bebenita's avatar
Michael Bebenita committed
128
      av1_diff_update_prob(r, &tx_probs->p32x32[i][j], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
129 130
}

Adrian Grange's avatar
Adrian Grange committed
131
static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
132
  int i, j;
133
  for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Jingning Han's avatar
Jingning Han committed
134
    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
135
      av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
136 137 138 139 140
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_switchable_interp_tree, fc->switchable_interp_prob[j],
                    fc->switchable_interp_cdf[j]);
#endif
  }
Jingning Han's avatar
Jingning Han committed
141 142
}

Adrian Grange's avatar
Adrian Grange committed
143
static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
144 145 146
  int i;
#if CONFIG_REF_MV
  for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
147
    av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
148
  for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
149
    av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
150
  for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
151
    av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
152
  for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
153
    av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
154 155
#else
  int j;
Jingning Han's avatar
Jingning Han committed
156 157
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
    for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita's avatar
Michael Bebenita committed
158
      av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
159
#endif
Jingning Han's avatar
Jingning Han committed
160 161
}

162
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
163
static REFERENCE_MODE read_frame_reference_mode(
164
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
165
  if (is_compound_reference_allowed(cm)) {
Adrian Grange's avatar
Adrian Grange committed
166
    return aom_rb_read_bit(rb)
clang-format's avatar
clang-format committed
167
               ? REFERENCE_MODE_SELECT
Adrian Grange's avatar
Adrian Grange committed
168
               : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
169 170 171 172 173
  } else {
    return SINGLE_REFERENCE;
  }
}
#else
174
static REFERENCE_MODE read_frame_reference_mode(const AV1_COMMON *cm,
Adrian Grange's avatar
Adrian Grange committed
175
                                                aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
176
  if (is_compound_reference_allowed(cm)) {
Michael Bebenita's avatar
Michael Bebenita committed
177 178 179
    return aom_read_bit(r, ACCT_STR)
               ? (aom_read_bit(r, ACCT_STR) ? REFERENCE_MODE_SELECT
                                            : COMPOUND_REFERENCE)
clang-format's avatar
clang-format committed
180
               : SINGLE_REFERENCE;
Jingning Han's avatar
Jingning Han committed
181 182 183 184
  } else {
    return SINGLE_REFERENCE;
  }
}
185
#endif
Jingning Han's avatar
Jingning Han committed
186

187
static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
188
  FRAME_CONTEXT *const fc = cm->fc;
189
  int i, j;
Jingning Han's avatar
Jingning Han committed
190 191 192

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

  if (cm->reference_mode != COMPOUND_REFERENCE)
196 197
    for (i = 0; i < REF_CONTEXTS; ++i)
      for (j = 0; j < (SINGLE_REFS - 1); ++j)
Michael Bebenita's avatar
Michael Bebenita committed
198
        av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
199 200

  if (cm->reference_mode != SINGLE_REFERENCE)
201 202 203
#if CONFIG_EXT_REFS
    for (i = 0; i < REF_CONTEXTS; ++i) {
      for (j = 0; j < (FWD_REFS - 1); ++j)
Michael Bebenita's avatar
Michael Bebenita committed
204 205
        av1_diff_update_prob(r, &fc->comp_fwdref_prob[i][j], ACCT_STR);
      for (j = 0; j < (BWD_REFS - 1); ++j) (r, &fc->comp_bwdref_prob[i][j]);
206 207
    }
#else
Jingning Han's avatar
Jingning Han committed
208
    for (i = 0; i < REF_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
209
      av1_diff_update_prob(r, &fc->comp_ref_prob[i], ACCT_STR);
210
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
211 212
}

Adrian Grange's avatar
Adrian Grange committed
213
static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
214 215
  int i;
  for (i = 0; i < n; ++i)
216
#if CONFIG_MISC_FIXES
Michael Bebenita's avatar
Michael Bebenita committed
217
    av1_diff_update_prob(r, &p[i], ACCT_STR);
218
#else
Michael Bebenita's avatar
Michael Bebenita committed
219 220
    if (aom_read(r, MV_UPDATE_PROB, ACCT_STR))
      p[i] = (aom_read_literal(r, 7, ACCT_STR) << 1) | 1;
221
#endif
Jingning Han's avatar
Jingning Han committed
222 223
}

Adrian Grange's avatar
Adrian Grange committed
224
static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  int i, j;

  update_mv_probs(ctx->joints, MV_JOINTS - 1, r);

  for (i = 0; i < 2; ++i) {
    nmv_component *const comp_ctx = &ctx->comps[i];
    update_mv_probs(&comp_ctx->sign, 1, r);
    update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
    update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
    update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
  }

  for (i = 0; i < 2; ++i) {
    nmv_component *const comp_ctx = &ctx->comps[i];
    for (j = 0; j < CLASS0_SIZE; ++j)
      update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
241
    update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Jingning Han's avatar
Jingning Han committed
242 243 244 245 246 247 248 249 250 251 252
  }

  if (allow_hp) {
    for (i = 0; i < 2; ++i) {
      nmv_component *const comp_ctx = &ctx->comps[i];
      update_mv_probs(&comp_ctx->class0_hp, 1, r);
      update_mv_probs(&comp_ctx->hp, 1, r);
    }
  }
}

clang-format's avatar
clang-format committed
253 254 255
static void inverse_transform_block_inter(MACROBLOCKD *xd, int plane,
                                          const TX_SIZE tx_size, uint8_t *dst,
                                          int stride, int eob, int block) {
Jingning Han's avatar
Jingning Han committed
256
  struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
257
  TX_TYPE tx_type = get_tx_type(pd->plane_type, xd, block);
258
  const int seg_id = xd->mi[0]->mbmi.segment_id;
Jingning Han's avatar
Jingning Han committed
259 260
  if (eob > 0) {
    tran_low_t *const dqcoeff = pd->dqcoeff;
261
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
262
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
hui su's avatar
hui su committed
263 264
      switch (tx_size) {
        case TX_4X4:
265
          av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
266
                                      tx_type, xd->lossless[seg_id]);
hui su's avatar
hui su committed
267 268
          break;
        case TX_8X8:
269
          av1_highbd_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
270
                                      tx_type);
hui su's avatar
hui su committed
271 272
          break;
        case TX_16X16:
273
          av1_highbd_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
274
                                        tx_type);
hui su's avatar
hui su committed
275 276
          break;
        case TX_32X32:
277
          av1_highbd_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
278
                                        tx_type);
hui su's avatar
hui su committed
279
          break;
clang-format's avatar
clang-format committed
280
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
281 282
      }
    } else {
283
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
284 285
      switch (tx_size) {
        case TX_4X4:
286
          av1_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, tx_type,
clang-format's avatar
clang-format committed
287
                               xd->lossless[seg_id]);
Jingning Han's avatar
Jingning Han committed
288 289
          break;
        case TX_8X8:
290
          av1_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
291 292
          break;
        case TX_16X16:
293
          av1_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
294 295
          break;
        case TX_32X32:
296
          av1_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
297
          break;
clang-format's avatar
clang-format committed
298
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
299
      }
300
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
301
    }
302
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
303 304 305 306

    if (eob == 1) {
      dqcoeff[0] = 0;
    } else {
307
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
308
        memset(dqcoeff, 0, 4 * tx_size_1d[tx_size] * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
309 310 311
      else if (tx_size == TX_32X32 && eob <= 34)
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
      else
312 313
        memset(dqcoeff, 0,
               (1 << (tx_size_1d_log2[tx_size] * 2)) * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
314 315 316 317
    }
  }
}

clang-format's avatar
clang-format committed
318
static void inverse_transform_block_intra(MACROBLOCKD *xd, int plane,
Jingning Han's avatar
Jingning Han committed
319
                                          const TX_TYPE tx_type,
clang-format's avatar
clang-format committed
320 321
                                          const TX_SIZE tx_size, uint8_t *dst,
                                          int stride, int eob) {
Jingning Han's avatar
Jingning Han committed
322
  struct macroblockd_plane *const pd = &xd->plane[plane];
323
  const int seg_id = xd->mi[0]->mbmi.segment_id;
Jingning Han's avatar
Jingning Han committed
324 325
  if (eob > 0) {
    tran_low_t *const dqcoeff = pd->dqcoeff;
326
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
327
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
hui su's avatar
hui su committed
328 329
      switch (tx_size) {
        case TX_4X4:
330
          av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
331
                                      tx_type, xd->lossless[seg_id]);
hui su's avatar
hui su committed
332 333
          break;
        case TX_8X8:
334
          av1_highbd_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
335
                                      tx_type);
hui su's avatar
hui su committed
336 337
          break;
        case TX_16X16:
338
          av1_highbd_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
339
                                        tx_type);
hui su's avatar
hui su committed
340 341
          break;
        case TX_32X32:
342
          av1_highbd_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
343
                                        tx_type);
hui su's avatar
hui su committed
344
          break;
clang-format's avatar
clang-format committed
345
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
346 347
      }
    } else {
348
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
349 350
      switch (tx_size) {
        case TX_4X4:
351
          av1_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, tx_type,
clang-format's avatar
clang-format committed
352
                               xd->lossless[seg_id]);
Jingning Han's avatar
Jingning Han committed
353 354
          break;
        case TX_8X8:
355
          av1_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
356 357
          break;
        case TX_16X16:
358
          av1_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
359 360
          break;
        case TX_32X32:
361
          av1_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
362
          break;
clang-format's avatar
clang-format committed
363
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
364
      }
365
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
366
    }
367
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
368 369 370 371 372

    if (eob == 1) {
      dqcoeff[0] = 0;
    } else {
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
373
        memset(dqcoeff, 0, 4 * tx_size_1d[tx_size] * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
374 375 376
      else if (tx_size == TX_32X32 && eob <= 34)
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
      else
377 378
        memset(dqcoeff, 0,
               (1 << (tx_size_1d_log2[tx_size] * 2)) * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
379 380 381 382 383
    }
  }
}

static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
384
                                                aom_reader *r,
Jingning Han's avatar
Jingning Han committed
385
                                                MB_MODE_INFO *const mbmi,
clang-format's avatar
clang-format committed
386
                                                int plane, int row, int col,
Jingning Han's avatar
Jingning Han committed
387 388 389
                                                TX_SIZE tx_size) {
  struct macroblockd_plane *const pd = &xd->plane[plane];
  PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
hui su's avatar
hui su committed
390
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han's avatar
Jingning Han committed
391
  uint8_t *dst;
hui su's avatar
hui su committed
392
  int block_idx = (row << 1) + col;
Jingning Han's avatar
Jingning Han committed
393 394 395
  dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];

  if (mbmi->sb_type < BLOCK_8X8)
clang-format's avatar
clang-format committed
396
    if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
Jingning Han's avatar
Jingning Han committed
397

398
  av1_predict_intra_block(xd, pd->n4_wl, pd->n4_hl, tx_size, mode, dst,
clang-format's avatar
clang-format committed
399
                          pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Jingning Han's avatar
Jingning Han committed
400 401

  if (!mbmi->skip) {
hui su's avatar
hui su committed
402
    TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx);
403 404 405
    const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
    const int eob = av1_decode_block_tokens(xd, plane, scan_order, col, row,
                                            tx_size, r, mbmi->segment_id);
clang-format's avatar
clang-format committed
406 407
    inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
                                  pd->dst.stride, eob);
Jingning Han's avatar
Jingning Han committed
408 409 410
  }
}

Adrian Grange's avatar
Adrian Grange committed
411
static int reconstruct_inter_block(MACROBLOCKD *const xd, aom_reader *r,
clang-format's avatar
clang-format committed
412 413
                                   MB_MODE_INFO *const mbmi, int plane, int row,
                                   int col, TX_SIZE tx_size) {
Jingning Han's avatar
Jingning Han committed
414
  struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
415 416 417
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  int block_idx = (row << 1) + col;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx);
418 419 420
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
  const int eob = av1_decode_block_tokens(xd, plane, scan_order, col, row,
                                          tx_size, r, mbmi->segment_id);
Jingning Han's avatar
Jingning Han committed
421

clang-format's avatar
clang-format committed
422 423 424
  inverse_transform_block_inter(
      xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
      pd->dst.stride, eob, block_idx);
Jingning Han's avatar
Jingning Han committed
425 426 427 428 429 430 431 432 433 434 435 436
  return eob;
}

static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
  int i;
  for (i = 0; i < MAX_MB_PLANE; i++) {
    struct macroblockd_plane *const pd = &xd->plane[i];
    memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
    memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
  }
}

437
static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Jingning Han's avatar
Jingning Han committed
438
                                 BLOCK_SIZE bsize, int mi_row, int mi_col,
clang-format's avatar
clang-format committed
439 440
                                 int bw, int bh, int x_mis, int y_mis, int bwl,
                                 int bhl) {
Jingning Han's avatar
Jingning Han committed
441 442 443 444 445 446 447 448 449 450
  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;
  for (y = 0; y < y_mis; ++y)
Jingning Han's avatar
Jingning Han committed
451
    for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Jingning Han's avatar
Jingning Han committed
452 453 454 455 456 457 458 459

  set_plane_n4(xd, bw, bh, bwl, bhl);
  set_skip_context(xd, mi_row, mi_col);

  // 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, cm->mi_rows, cm->mi_cols);

460
  av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
461 462 463
  return &xd->mi[0]->mbmi;
}

464
static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
465
                         int mi_row, int mi_col, aom_reader *r,
clang-format's avatar
clang-format committed
466
                         BLOCK_SIZE bsize, int bwl, int bhl) {
467
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
468 469 470
  const int less8x8 = bsize < BLOCK_8X8;
  const int bw = 1 << (bwl - 1);
  const int bh = 1 << (bhl - 1);
Adrian Grange's avatar
Adrian Grange committed
471 472
  const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
  const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
Jingning Han's avatar
Jingning Han committed
473

Michael Bebenita's avatar
Michael Bebenita committed
474 475 476
#if CONFIG_ACCOUNTING
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
#endif
clang-format's avatar
clang-format committed
477 478
  MB_MODE_INFO *mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
                                   y_mis, bwl, bhl);
Jingning Han's avatar
Jingning Han committed
479 480 481 482 483

  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)
Adrian Grange's avatar
Adrian Grange committed
484
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
clang-format's avatar
clang-format committed
485
                         "Invalid block size.");
Jingning Han's avatar
Jingning Han committed
486 487
  }

488
  av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Jingning Han's avatar
Jingning Han committed
489 490 491 492 493 494 495

  if (mbmi->skip) {
    dec_reset_skip_context(xd);
  }

  if (!is_inter_block(mbmi)) {
    int plane;
496 497 498 499 500 501
#if CONFIG_PALETTE
    for (plane = 0; plane <= 1; ++plane) {
      if (mbmi->palette_mode_info.palette_size[plane])
        av1_decode_palette_tokens(xd, plane, r);
    }
#endif  // CONFIG_PALETTE
Jingning Han's avatar
Jingning Han committed
502 503
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
      const struct macroblockd_plane *const pd = &xd->plane[plane];
504
      const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
505 506
      const int num_4x4_w = pd->n4_w;
      const int num_4x4_h = pd->n4_h;
507
      const int step = tx_size_1d_in_unit[tx_size];
Jingning Han's avatar
Jingning Han committed
508
      int row, col;
clang-format's avatar
clang-format committed
509 510 511 512 513 514 515 516
      const int max_blocks_wide =
          num_4x4_w + (xd->mb_to_right_edge >= 0
                           ? 0
                           : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
      const int max_blocks_high =
          num_4x4_h + (xd->mb_to_bottom_edge >= 0
                           ? 0
                           : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
Jingning Han's avatar
Jingning Han committed
517 518 519

      for (row = 0; row < max_blocks_high; row += step)
        for (col = 0; col < max_blocks_wide; col += step)
clang-format's avatar
clang-format committed
520 521
          predict_and_reconstruct_intra_block(xd, r, mbmi, plane, row, col,
                                              tx_size);
Jingning Han's avatar
Jingning Han committed
522 523 524
    }
  } else {
    // Prediction
525
    av1_build_inter_predictors_sb(xd, mi_row, mi_col, AOMMAX(bsize, BLOCK_8X8));
526 527 528 529
#if CONFIG_MOTION_VAR
    if (mbmi->motion_mode == OBMC_CAUSAL)
      av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
#endif  // CONFIG_MOTION_VAR
Jingning Han's avatar
Jingning Han committed
530 531 532 533 534 535 536 537
    // Reconstruction
    if (!mbmi->skip) {
      int eobtotal = 0;
      int plane;

      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
        const struct macroblockd_plane *const pd = &xd->plane[plane];
        const TX_SIZE tx_size =
538
            plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
539 540
        const int num_4x4_w = pd->n4_w;
        const int num_4x4_h = pd->n4_h;
541
        const int step = tx_size_1d_in_unit[tx_size];
Jingning Han's avatar
Jingning Han committed
542
        int row, col;
clang-format's avatar
clang-format committed
543 544 545 546 547 548 549 550
        const int max_blocks_wide =
            num_4x4_w + (xd->mb_to_right_edge >= 0
                             ? 0
                             : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
        const int max_blocks_high =
            num_4x4_h +
            (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
                                                  (5 + pd->subsampling_y));
Jingning Han's avatar
Jingning Han committed
551 552 553

        for (row = 0; row < max_blocks_high; row += step)
          for (col = 0; col < max_blocks_wide; col += step)
clang-format's avatar
clang-format committed
554 555
            eobtotal +=
                reconstruct_inter_block(xd, r, mbmi, plane, row, col, tx_size);
Jingning Han's avatar
Jingning Han committed
556 557 558
      }

      if (!less8x8 && eobtotal == 0)
559 560 561
#if CONFIG_MISC_FIXES
        mbmi->has_no_coeffs = 1;  // skip loopfilter
#else
Jingning Han's avatar
Jingning Han committed
562
        mbmi->skip = 1;  // skip loopfilter
563
#endif
Jingning Han's avatar
Jingning Han committed
564 565 566
    }
  }

Adrian Grange's avatar
Adrian Grange committed
567
  xd->corrupted |= aom_reader_has_error(r);
Jingning Han's avatar
Jingning Han committed
568 569
}

clang-format's avatar
clang-format committed
570 571
static INLINE int dec_partition_plane_context(const MACROBLOCKD *xd, int mi_row,
                                              int mi_col, int bsl) {
Jingning Han's avatar
Jingning Han committed
572
  const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
573 574
  const PARTITION_CONTEXT *left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
clang-format's avatar
clang-format committed
575
  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
Jingning Han's avatar
Jingning Han committed
576

clang-format's avatar
clang-format committed
577
  //  assert(bsl >= 0);
Jingning Han's avatar
Jingning Han committed
578 579 580 581

  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
}

clang-format's avatar
clang-format committed
582 583
static INLINE void dec_update_partition_context(MACROBLOCKD *xd, int mi_row,
                                                int mi_col, BLOCK_SIZE subsize,
Jingning Han's avatar
Jingning Han committed
584 585
                                                int bw) {
  PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
586 587
  PARTITION_CONTEXT *const left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
Jingning Han's avatar
Jingning Han committed
588 589 590 591 592 593 594 595

  // update the partition context at the end notes. set partition bits
  // of block sizes larger than the current one to be one, and partition
  // bits of smaller block sizes to be zero.
  memset(above_ctx, partition_context_lookup[subsize].above, bw);
  memset(left_ctx, partition_context_lookup[subsize].left, bw);
}

596
static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
597
                                     int mi_row, int mi_col, aom_reader *r,
Jingning Han's avatar
Jingning Han committed
598 599
                                     int has_rows, int has_cols, int bsl) {
  const int ctx = dec_partition_plane_context(xd, mi_row, mi_col, bsl);
Adrian Grange's avatar
Adrian Grange committed
600
  const aom_prob *const probs = cm->fc->partition_prob[ctx];
Jingning Han's avatar
Jingning Han committed
601 602 603 604
  FRAME_COUNTS *counts = xd->counts;
  PARTITION_TYPE p;

  if (has_rows && has_cols)
605
#if CONFIG_DAALA_EC
606
    p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
Michael Bebenita's avatar
Michael Bebenita committed
607
                                        PARTITION_TYPES, ACCT_STR);
608
#else
Michael Bebenita's avatar
Michael Bebenita committed
609
    p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
610
#endif
Jingning Han's avatar
Jingning Han committed
611
  else if (!has_rows && has_cols)
Michael Bebenita's avatar
Michael Bebenita committed
612
    p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Jingning Han's avatar
Jingning Han committed
613
  else if (has_rows && !has_cols)
Michael Bebenita's avatar
Michael Bebenita committed
614
    p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Jingning Han's avatar
Jingning Han committed
615 616 617
  else
    p = PARTITION_SPLIT;

clang-format's avatar
clang-format committed
618
  if (counts) ++counts->partition[ctx][p];
Jingning Han's avatar
Jingning Han committed
619 620 621 622 623

  return p;
}

// TODO(slavarnway): eliminate bsize and subsize in future commits
624
static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
625
                             int mi_row, int mi_col, aom_reader *r,
clang-format's avatar
clang-format committed
626
                             BLOCK_SIZE bsize, int n4x4_l2) {
627
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
628 629 630 631 632 633 634 635
  const int n8x8_l2 = n4x4_l2 - 1;
  const int num_8x8_wh = 1 << n8x8_l2;
  const int hbs = num_8x8_wh >> 1;
  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;

clang-format's avatar
clang-format committed
636
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
Jingning Han's avatar
Jingning Han committed
637

clang-format's avatar
clang-format committed
638 639
  partition =
      read_partition(cm, xd, mi_row, mi_col, r, has_rows, has_cols, n8x8_l2);
Jingning Han's avatar
Jingning Han committed
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
  subsize = subsize_lookup[partition][bsize];  // get_subsize(bsize, partition);
  if (!hbs) {
    // calculate bmode block dimensions (log 2)
    xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
    xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
    decode_block(pbi, xd, mi_row, mi_col, r, subsize, 1, 1);
  } else {
    switch (partition) {
      case PARTITION_NONE:
        decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n4x4_l2);
        break;
      case PARTITION_HORZ:
        decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n8x8_l2);
        if (has_rows)
          decode_block(pbi, xd, mi_row + hbs, mi_col, r, subsize, n4x4_l2,
                       n8x8_l2);
        break;
      case PARTITION_VERT:
        decode_block(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2, n4x4_l2);
        if (has_cols)
          decode_block(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2,
                       n4x4_l2);
        break;
      case PARTITION_SPLIT:
        decode_partition(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2);
        decode_partition(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2);
        decode_partition(pbi, xd, mi_row + hbs, mi_col, r, subsize, n8x8_l2);
        decode_partition(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize,
                         n8x8_l2);
        break;
clang-format's avatar
clang-format committed
670
      default: assert(0 && "Invalid partition type");
Jingning Han's avatar
Jingning Han committed
671 672 673 674 675 676 677
    }
  }

  // update partition context
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
    dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh);
678

679
#if CONFIG_DERING
680 681
  if (bsize == BLOCK_64X64) {
    if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
clang-format's avatar
clang-format committed
682
      cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Michael Bebenita's avatar
Michael Bebenita committed
683
          aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
684
    } else {
clang-format's avatar
clang-format committed
685 686
      cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
          0;
687 688 689
    }
  }
#endif
Jingning Han's avatar
Jingning Han committed
690 691
}

clang-format's avatar
clang-format committed
692
static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
Jingning Han's avatar
Jingning Han committed
693
                                size_t read_size,
Adrian Grange's avatar
Adrian Grange committed
694 695
                                struct aom_internal_error_info *error_info,
                                aom_reader *r, aom_decrypt_cb decrypt_cb,
Jingning Han's avatar
Jingning Han committed
696 697 698 699 700
                                void *decrypt_state) {
  // 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))
Adrian Grange's avatar
Adrian Grange committed
701
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Jingning Han's avatar
Jingning Han committed
702 703
                       "Truncated packet or corrupt tile length");

Adrian Grange's avatar
Adrian Grange committed
704
  if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Adrian Grange's avatar
Adrian Grange committed
705
    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
706 707 708
                       "Failed to allocate bool decoder %d", 1);
}

709
static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
Adrian Grange's avatar
Adrian Grange committed
710
                                   aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
711 712
  int i, j, k, l, m;

Michael Bebenita's avatar
Michael Bebenita committed
713
  if (aom_read_bit(r, ACCT_STR))
Jingning Han's avatar
Jingning Han committed
714 715 716 717 718
    for (i = 0; i < PLANE_TYPES; ++i)
      for (j = 0; j < REF_TYPES; ++j)
        for (k = 0; k < COEF_BANDS; ++k)
          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
            for (m = 0; m < UNCONSTRAINED_NODES; ++m)
Michael Bebenita's avatar
Michael Bebenita committed
719
              av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
720 721
}

Adrian Grange's avatar
Adrian Grange committed
722
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
clang-format's avatar
clang-format committed
723 724
  const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  TX_SIZE tx_size;
Jingning Han's avatar
Jingning Han committed
725
  for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
clang-format's avatar
clang-format committed
726
    read_coef_probs_common(fc->coef_probs[tx_size], r);
727
#if CONFIG_RANS || CONFIG_DAALA_EC
728 729
  av1_coef_pareto_cdfs(fc);
#endif  // CONFIG_RANS
Jingning Han's avatar
Jingning Han committed
730 731
}

732
static void setup_segmentation(AV1_COMMON *const cm,
Adrian Grange's avatar
Adrian Grange committed
733
                               struct aom_read_bit_buffer *rb) {
734
  struct segmentation *const seg = &cm->seg;
735 736 737
#if !CONFIG_MISC_FIXES
  struct segmentation_probs *const segp = &cm->segp;
#endif
Jingning Han's avatar
Jingning Han committed
738 739 740 741 742
  int i, j;

  seg->update_map = 0;
  seg->update_data = 0;

Adrian Grange's avatar
Adrian Grange committed
743
  seg->enabled = aom_rb_read_bit(rb);
clang-format's avatar
clang-format committed
744
  if (!seg->enabled) return;
Jingning Han's avatar
Jingning Han committed
745 746

  // Segmentation map update
747 748 749
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    seg->update_map = 1;
  } else {
Adrian Grange's avatar
Adrian Grange committed
750
    seg->update_map = aom_rb_read_bit(rb);
751
  }
Jingning Han's avatar
Jingning Han committed
752
  if (seg->update_map) {
753
#if !CONFIG_MISC_FIXES
754
    for (i = 0; i < SEG_TREE_PROBS; i++) {
clang-format's avatar
clang-format committed
755
      segp->tree_probs[i] =
Adrian Grange's avatar
Adrian Grange committed
756
          aom_rb_read_bit(rb) ? aom_rb_read_literal(rb, 8) : MAX_PROB;
757 758 759 760
    }
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_segment_tree, segp->tree_probs, segp->tree_cdf);
#endif
761
#endif
762 763 764
    if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
      seg->temporal_update = 0;
    } else {
Adrian Grange's avatar
Adrian Grange committed
765
      seg->temporal_update = aom_rb_read_bit(rb);
766
    }
767
#if !CONFIG_MISC_FIXES
Jingning Han's avatar
Jingning Han committed
768 769
    if (seg->temporal_update) {
      for (i = 0; i < PREDICTION_PROBS; i++)
clang-format's avatar
clang-format committed
770
        segp->pred_probs[i] =
Adrian Grange's avatar
Adrian Grange committed
771
            aom_rb_read_bit(rb) ? aom_rb_read_literal(rb, 8) : MAX_PROB;
Jingning Han's avatar
Jingning Han committed
772
    } else {
clang-format's avatar
clang-format committed
773
      for (i = 0; i < PREDICTION_PROBS; i++) segp->pred_probs[i] = MAX_PROB;
Jingning Han's avatar
Jingning Han committed
774
    }
775
#endif
Jingning Han's avatar
Jingning Han committed
776 777 778
  }

  // Segmentation data update
Adrian Grange's avatar
Adrian Grange committed
779
  seg->update_data = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
780
  if (seg->update_data) {
Adrian Grange's avatar
Adrian Grange committed
781
    seg->abs_delta = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
782

783
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
784 785 786 787

    for (i = 0; i < MAX_SEGMENTS; i++) {
      for (j = 0; j < SEG_LVL_MAX; j++) {
        int data = 0;
Adrian Grange's avatar
Adrian Grange committed
788
        const int feature_enabled = aom_rb_read_bit(rb);