decodeframe.c 85.9 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;
156
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Jingning Han's avatar
Jingning Han committed
157
    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 160 161 162 163
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_inter_mode_tree, fc->inter_mode_probs[i],
                    fc->inter_mode_cdf[i]);
#endif
  }
164
#endif
Jingning Han's avatar
Jingning Han committed
165 166
}

167
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
168
static REFERENCE_MODE read_frame_reference_mode(
169
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
170
  if (is_compound_reference_allowed(cm)) {
Adrian Grange's avatar
Adrian Grange committed
171
    return aom_rb_read_bit(rb)
clang-format's avatar
clang-format committed
172
               ? REFERENCE_MODE_SELECT
Adrian Grange's avatar
Adrian Grange committed
173
               : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
174 175 176 177 178
  } else {
    return SINGLE_REFERENCE;
  }
}
#else
179
static REFERENCE_MODE read_frame_reference_mode(const AV1_COMMON *cm,
Adrian Grange's avatar
Adrian Grange committed
180
                                                aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
181
  if (is_compound_reference_allowed(cm)) {
Michael Bebenita's avatar
Michael Bebenita committed
182 183 184
    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
185
               : SINGLE_REFERENCE;
Jingning Han's avatar
Jingning Han committed
186 187 188 189
  } else {
    return SINGLE_REFERENCE;
  }
}
190
#endif
Jingning Han's avatar
Jingning Han committed
191

192
static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
193
  FRAME_CONTEXT *const fc = cm->fc;
194
  int i, j;
Jingning Han's avatar
Jingning Han committed
195 196 197

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

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

  if (cm->reference_mode != SINGLE_REFERENCE)
206 207 208
#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
209 210
        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]);
211 212
    }
#else
Jingning Han's avatar
Jingning Han committed
213
    for (i = 0; i < REF_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
214
      av1_diff_update_prob(r, &fc->comp_ref_prob[i], ACCT_STR);
215
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
216 217
}

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

Adrian Grange's avatar
Adrian Grange committed
229
static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
230 231 232
  int i, j;

  update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
233 234 235
#if CONFIG_DAALA_EC
  av1_tree_to_cdf(av1_mv_joint_tree, ctx->joints, ctx->joint_cdf);
#endif
Jingning Han's avatar
Jingning Han committed
236 237 238 239 240 241 242

  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);
243 244 245
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_mv_class_tree, comp_ctx->classes, comp_ctx->class_cdf);
#endif
Jingning Han's avatar
Jingning Han committed
246 247 248 249 250 251
  }

  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);
252
    update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
Jingning Han's avatar
Jingning Han committed
253 254 255 256 257 258 259 260 261 262 263
  }

  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
264 265 266
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
267
  struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
268
  TX_TYPE tx_type = get_tx_type(pd->plane_type, xd, block);
269
  const int seg_id = xd->mi[0]->mbmi.segment_id;
Jingning Han's avatar
Jingning Han committed
270 271
  if (eob > 0) {
    tran_low_t *const dqcoeff = pd->dqcoeff;
272
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
273
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
hui su's avatar
hui su committed
274 275
      switch (tx_size) {
        case TX_4X4:
276
          av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
277
                                      tx_type, xd->lossless[seg_id]);
hui su's avatar
hui su committed
278 279
          break;
        case TX_8X8:
280
          av1_highbd_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
281
                                      tx_type);
hui su's avatar
hui su committed
282 283
          break;
        case TX_16X16:
284
          av1_highbd_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
285
                                        tx_type);
hui su's avatar
hui su committed
286 287
          break;
        case TX_32X32:
288
          av1_highbd_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
289
                                        tx_type);
hui su's avatar
hui su committed
290
          break;
clang-format's avatar
clang-format committed
291
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
292 293
      }
    } else {
294
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
295 296
      switch (tx_size) {
        case TX_4X4:
297
          av1_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, tx_type,
clang-format's avatar
clang-format committed
298
                               xd->lossless[seg_id]);
Jingning Han's avatar
Jingning Han committed
299 300
          break;
        case TX_8X8:
301
          av1_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
302 303
          break;
        case TX_16X16:
304
          av1_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
305 306
          break;
        case TX_32X32:
307
          av1_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
308
          break;
clang-format's avatar
clang-format committed
309
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
310
      }
311
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
312
    }
313
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
314 315 316 317

    if (eob == 1) {
      dqcoeff[0] = 0;
    } else {
318
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
319
        memset(dqcoeff, 0, 4 * tx_size_1d[tx_size] * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
320 321 322
      else if (tx_size == TX_32X32 && eob <= 34)
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
      else
323 324
        memset(dqcoeff, 0,
               (1 << (tx_size_1d_log2[tx_size] * 2)) * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
325 326 327 328
    }
  }
}

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

    if (eob == 1) {
      dqcoeff[0] = 0;
    } else {
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
384
        memset(dqcoeff, 0, 4 * tx_size_1d[tx_size] * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
385 386 387
      else if (tx_size == TX_32X32 && eob <= 34)
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
      else
388 389
        memset(dqcoeff, 0,
               (1 << (tx_size_1d_log2[tx_size] * 2)) * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
390 391 392 393 394
    }
  }
}

static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
395
                                                aom_reader *r,
Jingning Han's avatar
Jingning Han committed
396
                                                MB_MODE_INFO *const mbmi,
clang-format's avatar
clang-format committed
397
                                                int plane, int row, int col,
Jingning Han's avatar
Jingning Han committed
398 399 400
                                                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
401
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han's avatar
Jingning Han committed
402
  uint8_t *dst;
hui su's avatar
hui su committed
403
  int block_idx = (row << 1) + col;
Jingning Han's avatar
Jingning Han committed
404 405 406
  dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];

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

409
  av1_predict_intra_block(xd, pd->n4_wl, pd->n4_hl, tx_size, mode, dst,
clang-format's avatar
clang-format committed
410
                          pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Jingning Han's avatar
Jingning Han committed
411 412

  if (!mbmi->skip) {
hui su's avatar
hui su committed
413
    TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx);
414 415 416
    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
417 418
    inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
                                  pd->dst.stride, eob);
Jingning Han's avatar
Jingning Han committed
419 420 421
  }
}

Adrian Grange's avatar
Adrian Grange committed
422
static int reconstruct_inter_block(MACROBLOCKD *const xd, aom_reader *r,
clang-format's avatar
clang-format committed
423 424
                                   MB_MODE_INFO *const mbmi, int plane, int row,
                                   int col, TX_SIZE tx_size) {
Jingning Han's avatar
Jingning Han committed
425
  struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
426 427 428
  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);
429 430 431
  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
432

clang-format's avatar
clang-format committed
433 434 435
  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
436 437 438 439 440 441 442 443 444 445 446 447
  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);
  }
}

448
static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Jingning Han's avatar
Jingning Han committed
449
                                 BLOCK_SIZE bsize, int mi_row, int mi_col,
clang-format's avatar
clang-format committed
450 451
                                 int bw, int bh, int x_mis, int y_mis, int bwl,
                                 int bhl) {
Jingning Han's avatar
Jingning Han committed
452 453 454 455 456 457 458 459 460 461
  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
462
    for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Jingning Han's avatar
Jingning Han committed
463 464 465 466 467 468 469 470

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

471
  av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
472 473 474
  return &xd->mi[0]->mbmi;
}

475
static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
476
                         int mi_row, int mi_col, aom_reader *r,
clang-format's avatar
clang-format committed
477
                         BLOCK_SIZE bsize, int bwl, int bhl) {
478
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
479 480 481
  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
482 483
  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
484

Michael Bebenita's avatar
Michael Bebenita committed
485 486 487
#if CONFIG_ACCOUNTING
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
#endif
clang-format's avatar
clang-format committed
488 489
  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
490 491 492 493 494

  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
495
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
clang-format's avatar
clang-format committed
496
                         "Invalid block size.");
Jingning Han's avatar
Jingning Han committed
497 498
  }

499
  av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Jingning Han's avatar
Jingning Han committed
500 501 502 503 504 505 506

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

  if (!is_inter_block(mbmi)) {
    int plane;
507 508 509 510 511 512
#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
513 514
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
      const struct macroblockd_plane *const pd = &xd->plane[plane];
515
      const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
516 517
      const int num_4x4_w = pd->n4_w;
      const int num_4x4_h = pd->n4_h;
518
      const int step = tx_size_1d_in_unit[tx_size];
Jingning Han's avatar
Jingning Han committed
519
      int row, col;
clang-format's avatar
clang-format committed
520 521 522 523 524 525 526 527
      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
528 529 530

      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
531 532
          predict_and_reconstruct_intra_block(xd, r, mbmi, plane, row, col,
                                              tx_size);
Jingning Han's avatar
Jingning Han committed
533 534 535
    }
  } else {
    // Prediction
536
    av1_build_inter_predictors_sb(xd, mi_row, mi_col, AOMMAX(bsize, BLOCK_8X8));
537 538 539 540
#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
541 542 543 544 545 546 547 548
    // 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 =
549
            plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
550 551
        const int num_4x4_w = pd->n4_w;
        const int num_4x4_h = pd->n4_h;
552
        const int step = tx_size_1d_in_unit[tx_size];
Jingning Han's avatar
Jingning Han committed
553
        int row, col;
clang-format's avatar
clang-format committed
554 555 556 557 558 559 560 561
        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
562 563 564

        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
565 566
            eobtotal +=
                reconstruct_inter_block(xd, r, mbmi, plane, row, col, tx_size);
Jingning Han's avatar
Jingning Han committed
567 568 569
      }

      if (!less8x8 && eobtotal == 0)
570 571 572
#if CONFIG_MISC_FIXES
        mbmi->has_no_coeffs = 1;  // skip loopfilter
#else
Jingning Han's avatar
Jingning Han committed
573
        mbmi->skip = 1;  // skip loopfilter
574
#endif
Jingning Han's avatar
Jingning Han committed
575 576 577
    }
  }

Adrian Grange's avatar
Adrian Grange committed
578
  xd->corrupted |= aom_reader_has_error(r);
Jingning Han's avatar
Jingning Han committed
579 580
}

clang-format's avatar
clang-format committed
581 582
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
583
  const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
584 585
  const PARTITION_CONTEXT *left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
clang-format's avatar
clang-format committed
586
  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
Jingning Han's avatar
Jingning Han committed
587

clang-format's avatar
clang-format committed
588
  //  assert(bsl >= 0);
Jingning Han's avatar
Jingning Han committed
589 590 591 592

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

clang-format's avatar
clang-format committed
593 594
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
595 596
                                                int bw) {
  PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
597 598
  PARTITION_CONTEXT *const left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
Jingning Han's avatar
Jingning Han committed
599 600 601 602 603 604 605 606

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

607
static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
608
                                     int mi_row, int mi_col, aom_reader *r,
Jingning Han's avatar
Jingning Han committed
609 610
                                     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
611
  const aom_prob *const probs = cm->fc->partition_prob[ctx];
Jingning Han's avatar
Jingning Han committed
612 613 614 615
  FRAME_COUNTS *counts = xd->counts;
  PARTITION_TYPE p;

  if (has_rows && has_cols)
616
#if CONFIG_DAALA_EC
617
    p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
Michael Bebenita's avatar
Michael Bebenita committed
618
                                        PARTITION_TYPES, ACCT_STR);
619
#else
Michael Bebenita's avatar
Michael Bebenita committed
620
    p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
621
#endif
Jingning Han's avatar
Jingning Han committed
622
  else if (!has_rows && has_cols)
Michael Bebenita's avatar
Michael Bebenita committed
623
    p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Jingning Han's avatar
Jingning Han committed
624
  else if (has_rows && !has_cols)
Michael Bebenita's avatar
Michael Bebenita committed
625
    p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Jingning Han's avatar
Jingning Han committed
626 627 628
  else
    p = PARTITION_SPLIT;

clang-format's avatar
clang-format committed
629
  if (counts) ++counts->partition[ctx][p];
Jingning Han's avatar
Jingning Han committed
630 631 632 633 634

  return p;
}

// TODO(slavarnway): eliminate bsize and subsize in future commits
635
static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
636
                             int mi_row, int mi_col, aom_reader *r,
clang-format's avatar
clang-format committed
637
                             BLOCK_SIZE bsize, int n4x4_l2) {
638
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
639 640 641 642 643 644 645 646
  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
647
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
Jingning Han's avatar
Jingning Han committed
648

clang-format's avatar
clang-format committed
649 650
  partition =
      read_partition(cm, xd, mi_row, mi_col, r, has_rows, has_cols, n8x8_l2);
Jingning Han's avatar
Jingning Han committed
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
  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
681
      default: assert(0 && "Invalid partition type");
Jingning Han's avatar
Jingning Han committed
682 683 684 685 686 687 688
    }
  }

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

690
#if CONFIG_DERING
691 692
  if (bsize == BLOCK_64X64) {
    if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
clang-format's avatar
clang-format committed
693
      cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Michael Bebenita's avatar
Michael Bebenita committed
694
          aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
695
    } else {
clang-format's avatar
clang-format committed
696 697
      cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
          0;
698 699 700
    }
  }
#endif
Jingning Han's avatar
Jingning Han committed
701 702
}

clang-format's avatar
clang-format committed
703
static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
Jingning Han's avatar
Jingning Han committed
704
                                size_t read_size,
Adrian Grange's avatar
Adrian Grange committed
705 706
                                struct aom_internal_error_info *error_info,
                                aom_reader *r, aom_decrypt_cb decrypt_cb,
Jingning Han's avatar
Jingning Han committed
707 708 709 710 711
                                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
712
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Jingning Han's avatar
Jingning Han committed
713 714
                       "Truncated packet or corrupt tile length");

Adrian Grange's avatar
Adrian Grange committed
715
  if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Adrian Grange's avatar
Adrian Grange committed
716
    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
717 718 719
                       "Failed to allocate bool decoder %d", 1);
}

720
static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
Adrian Grange's avatar
Adrian Grange committed
721
                                   aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
722 723
  int i, j, k, l, m;

Michael Bebenita's avatar
Michael Bebenita committed
724
  if (aom_read_bit(r, ACCT_STR))
Jingning Han's avatar
Jingning Han committed
725 726 727 728 729
    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
730
              av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
731 732
}

Adrian Grange's avatar
Adrian Grange committed
733
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
clang-format's avatar
clang-format committed
734 735
  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
736
  for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
clang-format's avatar
clang-format committed
737
    read_coef_probs_common(fc->coef_probs[tx_size], r);
738
#if CONFIG_RANS || CONFIG_DAALA_EC
739 740
  av1_coef_pareto_cdfs(fc);
#endif  // CONFIG_RANS
Jingning Han's avatar
Jingning Han committed
741 742
}

743
static void setup_segmentation(AV1_COMMON *const cm,
Adrian Grange's avatar
Adrian Grange committed
744
                               struct aom_read_bit_buffer *rb) {
745
  struct segmentation *const seg = &cm->seg;
746 747 748
#if !CONFIG_MISC_FIXES
  struct segmentation_probs *const segp = &cm->segp;
#endif
Jingning Han's avatar
Jingning Han committed
749 750 751 752 753
  int i, j;

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

Adrian Grange's avatar
Adrian Grange committed
754
  seg->enabled = aom_rb_read_bit(rb);
clang-format's avatar
clang-format committed
755
  if (!seg->enabled) return;
Jingning Han's avatar
Jingning Han committed
756 757

  // Segmentation map update
758 759 760
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    seg->update_map = 1;
  } else {
Adrian Grange's avatar
Adrian Grange committed
761
    seg->update_map = aom_rb_read_bit(rb);
762
  }
Jingning Han's avatar
Jingning Han committed
763
  if (seg->update_map) {
764
#if !CONFIG_MISC_FIXES
765
    for (i = 0; i < SEG_TREE_PROBS; i++) {
clang-format's avatar
clang-format committed
766
      segp->tree_probs[i] =
Adrian Grange's avatar
Adrian Grange committed
767
          aom_rb_read_bit(rb) ? aom_rb_read_literal(rb, 8) : MAX_PROB;
768 769 770 771
    }
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_segment_tree, segp->tree_probs, segp->tree_cdf);
#endif
772
#endif
773 774 775
    if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
      seg->temporal_update = 0;
    } else {
Adrian Grange's avatar
Adrian Grange committed
776
      seg->temporal_update = aom_rb_read_bit(rb);
777
    }
778
#if !CONFIG_MISC_FIXES
Jingning Han's avatar
Jingning Han committed
779 780
    if (seg->temporal_update) {
      for (i = 0; i < PREDICTION_PROBS; i++)
clang-format's avatar
clang-format committed
781
        segp->pred_probs[i] =
Adrian Grange's avatar
Adrian Grange committed
782
            aom_rb_read_bit(rb) ? aom_rb_read_literal(rb, 8) : MAX_PROB;
Jingning Han's avatar
Jingning Han committed
783
    } else {
clang-format's avatar
clang-format committed
784
      for (i = 0; i < PREDICTION_PROBS; i++) segp->pred_probs[i] = MAX_PROB;
Jingning Han's avatar
Jingning Han committed
785
    }
786
#endif
Jingning Han's avatar
Jingning Han committed
787 788 789
  }

  // Segmentation data update
Adrian Grange's avatar
Adrian Grange committed
790
  seg->update_data = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
791
  if (seg->update_data) {
Adrian Grange's avatar
Adrian Grange committed
792
    seg->abs_delta = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
793

794
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
795 796 797 798

    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
799
        const int feature_enabled = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
800
        if (feature_enabled) {
801 802 803
          av1_enable_segfeature(seg, i, j);
          data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
          if (av1_is_segfeature_signed(j))
Adrian Grange's avatar
Adrian Grange committed
804
            data = aom_rb_read_bit(rb) ? -data : data;
Jingning Han's avatar
Jingning Han committed
805
        }
806
        av1_set_segdata(seg, i, j, data);
Jingning Han's avatar
Jingning Han committed
807 808 809 810 811 812
      }
    }
  }
}

static void setup_loopfilter(struct loopfilter *lf,
Adrian Grange's avatar
Adrian Grange committed
813 814 815
                             struct aom_read_bit_buffer *rb) {
  lf->filter_level = aom_rb_read_literal(rb, 6);
  lf->sharpness_level = aom_rb_read_literal(rb, 3);
Jingning Han's avatar
Jingning Han committed
816 817 818 819 820

  // Read in loop filter deltas applied at the MB level based on mode or ref
  // frame.
  lf->mode_ref_delta_update = 0;