decodeframe.c 89.6 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"
Angie Chiang's avatar
Angie Chiang committed
29
30
31
#if CONFIG_BITSTREAM_DEBUG
#include "aom_util/debug_util.h"
#endif  // CONFIG_BITSTREAM_DEBUG
Jingning Han's avatar
Jingning Han committed
32

Yaowu Xu's avatar
Yaowu Xu committed
33
#include "av1/common/alloccommon.h"
Yaowu Xu's avatar
Yaowu Xu committed
34
#if CONFIG_CLPF
Steinar Midtskogen's avatar
Steinar Midtskogen committed
35
#include "aom/aom_image.h"
Yaowu Xu's avatar
Yaowu Xu committed
36
#include "av1/common/clpf.h"
Yaowu Xu's avatar
Yaowu Xu committed
37
#endif
Yaowu Xu's avatar
Yaowu Xu committed
38
#include "av1/common/common.h"
39
#if CONFIG_DERING
Yaowu Xu's avatar
Yaowu Xu committed
40
#include "av1/common/dering.h"
41
#endif  // CONFIG_DERING
Yaowu Xu's avatar
Yaowu Xu committed
42
43
44
45
46
47
#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
48
#include "av1/common/reconintra.h"
Yaowu Xu's avatar
Yaowu Xu committed
49
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
50
#include "av1/common/thread_common.h"
Yaowu Xu's avatar
Yaowu Xu committed
51
#include "av1/common/tile_common.h"
52

Yaowu Xu's avatar
Yaowu Xu committed
53
54
55
#include "av1/decoder/decodeframe.h"
#include "av1/decoder/decodemv.h"
#include "av1/decoder/decoder.h"
Jingning Han's avatar
Jingning Han committed
56
#include "av1/decoder/detokenize.h"
Yaowu Xu's avatar
Yaowu Xu committed
57
#include "av1/decoder/dsubexp.h"
Jingning Han's avatar
Jingning Han committed
58

59
#define MAX_AV1_HEADER_SIZE 80
Michael Bebenita's avatar
Michael Bebenita committed
60
#define ACCT_STR __func__
Jingning Han's avatar
Jingning Han committed
61

62
63
64
65
66
67
68
69
static struct aom_read_bit_buffer *init_read_bit_buffer(
    AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
    const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
                                  size_t partition_size);
static size_t read_uncompressed_header(AV1Decoder *pbi,
                                       struct aom_read_bit_buffer *rb);

70
static int is_compound_reference_allowed(const AV1_COMMON *cm) {
Jingning Han's avatar
Jingning Han committed
71
  int i;
clang-format's avatar
clang-format committed
72
  if (frame_is_intra_only(cm)) return 0;
Jingning Han's avatar
Jingning Han committed
73
  for (i = 1; i < REFS_PER_FRAME; ++i)
clang-format's avatar
clang-format committed
74
    if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
Jingning Han's avatar
Jingning Han committed
75
76
77
78

  return 0;
}

79
static void setup_compound_reference_mode(AV1_COMMON *cm) {
80
81
82
83
84
85
86
87
#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
88
  if (cm->ref_frame_sign_bias[LAST_FRAME] ==
clang-format's avatar
clang-format committed
89
      cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
Jingning Han's avatar
Jingning Han committed
90
91
92
93
    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
94
             cm->ref_frame_sign_bias[ALTREF_FRAME]) {
Jingning Han's avatar
Jingning Han committed
95
96
97
98
99
100
101
102
    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;
  }
103
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
104
105
106
107
108
109
}

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
110
111
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
112
113
114
  return data > max ? max : data;
}

115
#if CONFIG_MISC_FIXES
Adrian Grange's avatar
Adrian Grange committed
116
117
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);
118
119
}
#else
Adrian Grange's avatar
Adrian Grange committed
120
static TX_MODE read_tx_mode(aom_reader *r) {
Michael Bebenita's avatar
Michael Bebenita committed
121
122
  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
123
124
  return tx_mode;
}
125
#endif
Jingning Han's avatar
Jingning Han committed
126

Adrian Grange's avatar
Adrian Grange committed
127
static void read_tx_mode_probs(struct tx_probs *tx_probs, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
128
129
130
  int i, j;

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

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

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

Adrian Grange's avatar
Adrian Grange committed
143
static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
144
  int i, j;
145
  for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Jingning Han's avatar
Jingning Han committed
146
    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
147
      av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
148
149
150
151
152
#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
153
154
}

Adrian Grange's avatar
Adrian Grange committed
155
static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
156
157
158
  int i;
#if CONFIG_REF_MV
  for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
159
    av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
160
  for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
161
    av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
162
  for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
163
    av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
164
  for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
165
    av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
166
167
#else
  int j;
168
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
Jingning Han's avatar
Jingning Han committed
169
    for (j = 0; j < INTER_MODES - 1; ++j)
Michael Bebenita's avatar
Michael Bebenita committed
170
      av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
171
172
173
174
175
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_inter_mode_tree, fc->inter_mode_probs[i],
                    fc->inter_mode_cdf[i]);
#endif
  }
176
#endif
Jingning Han's avatar
Jingning Han committed
177
178
}

179
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
180
static REFERENCE_MODE read_frame_reference_mode(
181
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
182
  if (is_compound_reference_allowed(cm)) {
Adrian Grange's avatar
Adrian Grange committed
183
    return aom_rb_read_bit(rb)
clang-format's avatar
clang-format committed
184
               ? REFERENCE_MODE_SELECT
Adrian Grange's avatar
Adrian Grange committed
185
               : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
186
187
188
189
190
  } else {
    return SINGLE_REFERENCE;
  }
}
#else
191
static REFERENCE_MODE read_frame_reference_mode(const AV1_COMMON *cm,
Adrian Grange's avatar
Adrian Grange committed
192
                                                aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
193
  if (is_compound_reference_allowed(cm)) {
Michael Bebenita's avatar
Michael Bebenita committed
194
195
196
    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
197
               : SINGLE_REFERENCE;
Jingning Han's avatar
Jingning Han committed
198
199
200
201
  } else {
    return SINGLE_REFERENCE;
  }
}
202
#endif
Jingning Han's avatar
Jingning Han committed
203

204
static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
205
  FRAME_CONTEXT *const fc = cm->fc;
206
  int i, j;
Jingning Han's avatar
Jingning Han committed
207
208
209

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

  if (cm->reference_mode != COMPOUND_REFERENCE)
213
214
    for (i = 0; i < REF_CONTEXTS; ++i)
      for (j = 0; j < (SINGLE_REFS - 1); ++j)
Michael Bebenita's avatar
Michael Bebenita committed
215
        av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
216
217

  if (cm->reference_mode != SINGLE_REFERENCE)
218
219
220
#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
221
222
        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]);
223
224
    }
#else
Jingning Han's avatar
Jingning Han committed
225
    for (i = 0; i < REF_CONTEXTS; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
226
      av1_diff_update_prob(r, &fc->comp_ref_prob[i], ACCT_STR);
227
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
228
229
}

Adrian Grange's avatar
Adrian Grange committed
230
static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
231
232
  int i;
  for (i = 0; i < n; ++i)
233
#if CONFIG_MISC_FIXES
Michael Bebenita's avatar
Michael Bebenita committed
234
    av1_diff_update_prob(r, &p[i], ACCT_STR);
235
#else
Michael Bebenita's avatar
Michael Bebenita committed
236
237
    if (aom_read(r, MV_UPDATE_PROB, ACCT_STR))
      p[i] = (aom_read_literal(r, 7, ACCT_STR) << 1) | 1;
238
#endif
Jingning Han's avatar
Jingning Han committed
239
240
}

Adrian Grange's avatar
Adrian Grange committed
241
static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
242
243
244
  int i, j;

  update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
245
246
247
#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
248
249
250
251
252
253
254

  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);
255
256
257
#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
258
259
260
261
  }

  for (i = 0; i < 2; ++i) {
    nmv_component *const comp_ctx = &ctx->comps[i];
262
    for (j = 0; j < CLASS0_SIZE; ++j) {
Jingning Han's avatar
Jingning Han committed
263
      update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
264
265
266
267
268
#if CONFIG_DAALA_EC
      av1_tree_to_cdf(av1_mv_fp_tree, comp_ctx->class0_fp[j],
                      comp_ctx->class0_fp_cdf[j]);
#endif
    }
269
    update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
270
271
272
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_mv_fp_tree, comp_ctx->fp, comp_ctx->fp_cdf);
#endif
Jingning Han's avatar
Jingning Han committed
273
274
275
276
277
278
279
280
281
282
283
  }

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

284
285
286
287
static void inverse_transform_block(MACROBLOCKD *xd, int plane,
                                    const TX_TYPE tx_type,
                                    const TX_SIZE tx_size, uint8_t *dst,
                                    int stride, int eob) {
Jingning Han's avatar
Jingning Han committed
288
  struct macroblockd_plane *const pd = &xd->plane[plane];
289
  const int seg_id = xd->mi[0]->mbmi.segment_id;
Jingning Han's avatar
Jingning Han committed
290
291
  if (eob > 0) {
    tran_low_t *const dqcoeff = pd->dqcoeff;
292
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
293
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
hui su's avatar
hui su committed
294
295
      switch (tx_size) {
        case TX_4X4:
296
          av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
297
                                      tx_type, xd->lossless[seg_id]);
hui su's avatar
hui su committed
298
299
          break;
        case TX_8X8:
300
          av1_highbd_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
301
                                      tx_type);
hui su's avatar
hui su committed
302
303
          break;
        case TX_16X16:
304
          av1_highbd_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
305
                                        tx_type);
hui su's avatar
hui su committed
306
307
          break;
        case TX_32X32:
308
          av1_highbd_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, xd->bd,
clang-format's avatar
clang-format committed
309
                                        tx_type);
hui su's avatar
hui su committed
310
          break;
clang-format's avatar
clang-format committed
311
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
312
313
      }
    } else {
314
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
315
316
      switch (tx_size) {
        case TX_4X4:
317
          av1_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, tx_type,
clang-format's avatar
clang-format committed
318
                               xd->lossless[seg_id]);
Jingning Han's avatar
Jingning Han committed
319
320
          break;
        case TX_8X8:
321
          av1_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
322
323
          break;
        case TX_16X16:
324
          av1_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
325
326
          break;
        case TX_32X32:
327
          av1_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, tx_type);
Jingning Han's avatar
Jingning Han committed
328
          break;
clang-format's avatar
clang-format committed
329
        default: assert(0 && "Invalid transform size"); return;
Jingning Han's avatar
Jingning Han committed
330
      }
331
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
332
    }
333
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
334
335
336
337

    if (eob == 1) {
      dqcoeff[0] = 0;
    } else {
338
339
340
#if CONFIG_ADAPT_SCAN
      memset(dqcoeff, 0, tx_size_2d[tx_size] * sizeof(dqcoeff[0]));
#else   // CONFIG_ADAPT_SCAN
Jingning Han's avatar
Jingning Han committed
341
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
342
        memset(dqcoeff, 0, 4 * tx_size_1d[tx_size] * sizeof(dqcoeff[0]));
Jingning Han's avatar
Jingning Han committed
343
344
345
      else if (tx_size == TX_32X32 && eob <= 34)
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
      else
346
347
        memset(dqcoeff, 0, tx_size_2d[tx_size] * sizeof(dqcoeff[0]));
#endif  // CONFIG_ADAPT_SCAN
Jingning Han's avatar
Jingning Han committed
348
349
350
351
    }
  }
}

Angie Chiang's avatar
Angie Chiang committed
352
353
354
static void predict_and_reconstruct_intra_block(
    AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *r,
    MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
Jingning Han's avatar
Jingning Han committed
355
356
  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
357
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han's avatar
Jingning Han committed
358
  uint8_t *dst;
hui su's avatar
hui su committed
359
  int block_idx = (row << 1) + col;
Jingning Han's avatar
Jingning Han committed
360
361
362
  dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];

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

365
  av1_predict_intra_block(xd, pd->n4_wl, pd->n4_hl, tx_size, mode, dst,
clang-format's avatar
clang-format committed
366
                          pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Jingning Han's avatar
Jingning Han committed
367
368

  if (!mbmi->skip) {
hui su's avatar
hui su committed
369
    TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx);
Angie Chiang's avatar
Angie Chiang committed
370
    const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type);
371
372
    const int eob = av1_decode_block_tokens(xd, plane, scan_order, col, row,
                                            tx_size, r, mbmi->segment_id);
373
374
375
#if CONFIG_ADAPT_SCAN
    av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
#endif
376
377
    inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
                            eob);
Jingning Han's avatar
Jingning Han committed
378
379
380
  }
}

Angie Chiang's avatar
Angie Chiang committed
381
382
383
384
static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
                                   aom_reader *r, MB_MODE_INFO *const mbmi,
                                   int plane, int row, int col,
                                   TX_SIZE tx_size) {
Jingning Han's avatar
Jingning Han committed
385
  struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
386
387
388
  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);
Angie Chiang's avatar
Angie Chiang committed
389
  const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type);
390
391
  const int eob = av1_decode_block_tokens(xd, plane, scan_order, col, row,
                                          tx_size, r, mbmi->segment_id);
392
393
394
#if CONFIG_ADAPT_SCAN
  av1_update_scan_count_facade(cm, tx_size, tx_type, pd->dqcoeff, eob);
#endif
395
396
397
  inverse_transform_block(xd, plane, tx_type, tx_size,
                          &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
                          pd->dst.stride, eob);
Jingning Han's avatar
Jingning Han committed
398
399
400
401
402
403
404
405
406
407
408
409
  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);
  }
}

410
static MB_MODE_INFO *set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Jingning Han's avatar
Jingning Han committed
411
                                 BLOCK_SIZE bsize, int mi_row, int mi_col,
clang-format's avatar
clang-format committed
412
413
                                 int bw, int bh, int x_mis, int y_mis, int bwl,
                                 int bhl) {
Jingning Han's avatar
Jingning Han committed
414
415
416
417
418
419
420
421
422
423
  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
424
    for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
Jingning Han's avatar
Jingning Han committed
425
426
427
428
429
430
431
432

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

433
  av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
Jingning Han's avatar
Jingning Han committed
434
435
436
  return &xd->mi[0]->mbmi;
}

437
static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
438
                         int mi_row, int mi_col, aom_reader *r,
clang-format's avatar
clang-format committed
439
                         BLOCK_SIZE bsize, int bwl, int bhl) {
440
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
441
442
443
  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
444
445
  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
446

Michael Bebenita's avatar
Michael Bebenita committed
447
448
449
#if CONFIG_ACCOUNTING
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
#endif
clang-format's avatar
clang-format committed
450
451
  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
452
453
454
455
456

  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
457
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
clang-format's avatar
clang-format committed
458
                         "Invalid block size.");
Jingning Han's avatar
Jingning Han committed
459
460
  }

461
  av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
Jingning Han's avatar
Jingning Han committed
462

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#if CONFIG_DELTA_Q
  if (cm->delta_q_present_flag) {
    int i;
    for (i = 0; i < MAX_SEGMENTS; i++) {
      xd->plane[0].seg_dequant[i][0] =
          av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
      xd->plane[0].seg_dequant[i][1] =
          av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
      xd->plane[1].seg_dequant[i][0] =
          av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
      xd->plane[1].seg_dequant[i][1] =
          av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
      xd->plane[2].seg_dequant[i][0] =
          av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
      xd->plane[2].seg_dequant[i][1] =
          av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
    }
  }
#endif

Jingning Han's avatar
Jingning Han committed
483
484
485
486
487
488
  if (mbmi->skip) {
    dec_reset_skip_context(xd);
  }

  if (!is_inter_block(mbmi)) {
    int plane;
489
490
491
492
493
494
#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
495
496
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
      const struct macroblockd_plane *const pd = &xd->plane[plane];
497
      const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
498
499
      const int num_4x4_w = pd->n4_w;
      const int num_4x4_h = pd->n4_h;
500
      const int step = tx_size_1d_in_unit[tx_size];
Jingning Han's avatar
Jingning Han committed
501
      int row, col;
clang-format's avatar
clang-format committed
502
503
504
505
506
507
508
509
      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
510
511
512

      for (row = 0; row < max_blocks_high; row += step)
        for (col = 0; col < max_blocks_wide; col += step)
Angie Chiang's avatar
Angie Chiang committed
513
          predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
clang-format's avatar
clang-format committed
514
                                              tx_size);
Jingning Han's avatar
Jingning Han committed
515
516
517
    }
  } else {
    // Prediction
518
    av1_build_inter_predictors_sb(xd, mi_row, mi_col, AOMMAX(bsize, BLOCK_8X8));
519
520
521
522
#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
523
524
525
526
527
528
529
530
    // 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 =
531
            plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
532
533
        const int num_4x4_w = pd->n4_w;
        const int num_4x4_h = pd->n4_h;
534
        const int step = tx_size_1d_in_unit[tx_size];
Jingning Han's avatar
Jingning Han committed
535
        int row, col;
clang-format's avatar
clang-format committed
536
537
538
539
540
541
542
543
        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
544
545
546

        for (row = 0; row < max_blocks_high; row += step)
          for (col = 0; col < max_blocks_wide; col += step)
Angie Chiang's avatar
Angie Chiang committed
547
548
            eobtotal += reconstruct_inter_block(cm, xd, r, mbmi, plane, row,
                                                col, tx_size);
Jingning Han's avatar
Jingning Han committed
549
550
551
      }

      if (!less8x8 && eobtotal == 0)
552
        mbmi->has_no_coeffs = 1;  // skip loopfilter
Jingning Han's avatar
Jingning Han committed
553
554
555
    }
  }

Adrian Grange's avatar
Adrian Grange committed
556
  xd->corrupted |= aom_reader_has_error(r);
Jingning Han's avatar
Jingning Han committed
557
558
}

clang-format's avatar
clang-format committed
559
560
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
561
  const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
562
563
  const PARTITION_CONTEXT *left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
clang-format's avatar
clang-format committed
564
  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
Jingning Han's avatar
Jingning Han committed
565

clang-format's avatar
clang-format committed
566
  //  assert(bsl >= 0);
Jingning Han's avatar
Jingning Han committed
567
568
569
570

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

clang-format's avatar
clang-format committed
571
572
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
573
574
                                                int bw) {
  PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
575
576
  PARTITION_CONTEXT *const left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
Jingning Han's avatar
Jingning Han committed
577
578
579
580
581
582
583
584

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

585
static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
586
                                     int mi_row, int mi_col, aom_reader *r,
Jingning Han's avatar
Jingning Han committed
587
588
                                     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
589
  const aom_prob *const probs = cm->fc->partition_prob[ctx];
Jingning Han's avatar
Jingning Han committed
590
591
592
593
  FRAME_COUNTS *counts = xd->counts;
  PARTITION_TYPE p;

  if (has_rows && has_cols)
594
#if CONFIG_DAALA_EC
595
    p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
Michael Bebenita's avatar
Michael Bebenita committed
596
                                        PARTITION_TYPES, ACCT_STR);
597
#else
Michael Bebenita's avatar
Michael Bebenita committed
598
    p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
599
#endif
Jingning Han's avatar
Jingning Han committed
600
  else if (!has_rows && has_cols)
Michael Bebenita's avatar
Michael Bebenita committed
601
    p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
Jingning Han's avatar
Jingning Han committed
602
  else if (has_rows && !has_cols)
Michael Bebenita's avatar
Michael Bebenita committed
603
    p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Jingning Han's avatar
Jingning Han committed
604
605
606
  else
    p = PARTITION_SPLIT;

clang-format's avatar
clang-format committed
607
  if (counts) ++counts->partition[ctx][p];
Jingning Han's avatar
Jingning Han committed
608
609
610
611
612

  return p;
}

// TODO(slavarnway): eliminate bsize and subsize in future commits
613
static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
614
                             int mi_row, int mi_col, aom_reader *r,
clang-format's avatar
clang-format committed
615
                             BLOCK_SIZE bsize, int n4x4_l2) {
616
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
617
618
619
620
621
622
623
624
  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
625
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
Jingning Han's avatar
Jingning Han committed
626

clang-format's avatar
clang-format committed
627
628
  partition =
      read_partition(cm, xd, mi_row, mi_col, r, has_rows, has_cols, n8x8_l2);
Jingning Han's avatar
Jingning Han committed
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
  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
659
      default: assert(0 && "Invalid partition type");
Jingning Han's avatar
Jingning Han committed
660
661
662
663
664
665
666
    }
  }

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

668
#if CONFIG_DERING
669
670
  if (bsize == BLOCK_64X64) {
    if (cm->dering_level != 0 && !sb_all_skip(cm, mi_row, mi_col)) {
clang-format's avatar
clang-format committed
671
      cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
Michael Bebenita's avatar
Michael Bebenita committed
672
          aom_read_literal(r, DERING_REFINEMENT_BITS, ACCT_STR);
673
    } else {
clang-format's avatar
clang-format committed
674
675
      cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.dering_gain =
          0;
676
677
678
    }
  }
#endif
Jingning Han's avatar
Jingning Han committed
679
680
}

clang-format's avatar
clang-format committed
681
static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
Jingning Han's avatar
Jingning Han committed
682
                                size_t read_size,
Adrian Grange's avatar
Adrian Grange committed
683
684
                                struct aom_internal_error_info *error_info,
                                aom_reader *r, aom_decrypt_cb decrypt_cb,
Jingning Han's avatar
Jingning Han committed
685
686
687
688
689
                                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
690
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Jingning Han's avatar
Jingning Han committed
691
692
                       "Truncated packet or corrupt tile length");

Adrian Grange's avatar
Adrian Grange committed
693
  if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
Adrian Grange's avatar
Adrian Grange committed
694
    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
695
696
697
                       "Failed to allocate bool decoder %d", 1);
}

698
static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
Adrian Grange's avatar
Adrian Grange committed
699
                                   aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
700
701
  int i, j, k, l, m;

Michael Bebenita's avatar
Michael Bebenita committed
702
  if (aom_read_bit(r, ACCT_STR))
Jingning Han's avatar
Jingning Han committed
703
704
705
706
707
    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
708
              av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
709
710
}

Adrian Grange's avatar
Adrian Grange committed
711
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
clang-format's avatar
clang-format committed
712
713
  const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  TX_SIZE tx_size;
714
  for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
clang-format's avatar
clang-format committed
715
    read_coef_probs_common(fc->coef_probs[tx_size], r);
716
#if CONFIG_RANS || CONFIG_DAALA_EC
717
718
  av1_coef_pareto_cdfs(fc);
#endif  // CONFIG_RANS
Jingning Han's avatar
Jingning Han committed
719
720
}

721
static void setup_segmentation(AV1_COMMON *const cm,
Adrian Grange's avatar
Adrian Grange committed
722
                               struct aom_read_bit_buffer *rb) {
723
  struct segmentation *const seg = &cm->seg;
724
725
726
#if !CONFIG_MISC_FIXES
  struct segmentation_probs *const segp = &cm->segp;
#endif
Jingning Han's avatar
Jingning Han committed
727
728
729
730
731
  int i, j;

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

Adrian Grange's avatar
Adrian Grange committed
732
  seg->enabled = aom_rb_read_bit(rb);
clang-format's avatar
clang-format committed
733
  if (!seg->enabled) return;
Jingning Han's avatar
Jingning Han committed
734
735

  // Segmentation map update
736
737
738
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    seg->update_map = 1;
  } else {
Adrian Grange's avatar
Adrian Grange committed
739
    seg->update_map = aom_rb_read_bit(rb);
740
  }
Jingning Han's avatar
Jingning Han committed
741
  if (seg->update_map) {
742
#if !CONFIG_MISC_FIXES
743
    for (i = 0; i < SEG_TREE_PROBS; i++) {
clang-format's avatar
clang-format committed
744
      segp->tree_probs[i] =
Adrian Grange's avatar
Adrian Grange committed
745
          aom_rb_read_bit(rb) ? aom_rb_read_literal(rb, 8) : MAX_PROB;
746
747
748
749
    }
#if CONFIG_DAALA_EC
    av1_tree_to_cdf(av1_segment_tree, segp->tree_probs, segp->tree_cdf);
#endif
750
#endif
751
752
753
    if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
      seg->temporal_update = 0;
    } else {
Adrian Grange's avatar
Adrian Grange committed
754
      seg->temporal_update = aom_rb_read_bit(rb);
755
    }
756
#if !CONFIG_MISC_FIXES
Jingning Han's avatar
Jingning Han committed
757
758
    if (seg->temporal_update) {
      for (i = 0; i < PREDICTION_PROBS; i++)
clang-format's avatar
clang-format committed
759
        segp->pred_probs[i] =
Adrian Grange's avatar
Adrian Grange committed
760
            aom_rb_read_bit(rb) ? aom_rb_read_literal(rb, 8) : MAX_PROB;
Jingning Han's avatar
Jingning Han committed
761
    } else {
clang-format's avatar
clang-format committed
762
      for (i = 0; i < PREDICTION_PROBS; i++) segp->pred_probs[i] = MAX_PROB;
Jingning Han's avatar
Jingning Han committed
763
    }
764
#endif
Jingning Han's avatar
Jingning Han committed
765
766
767
  }

  // Segmentation data update
Adrian Grange's avatar
Adrian Grange committed
768
  seg->update_data = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
769
  if (seg->update_data) {
Adrian Grange's avatar
Adrian Grange committed
770
    seg->abs_delta = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
771

772
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
773
774
775
776

    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
777
        const int feature_enabled = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
778
        if (feature_enabled) {
779
780
781
          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
782
            data = aom_rb_read_bit(rb) ? -data : data;
Jingning Han's avatar
Jingning Han committed
783
        }
784
        av1_set_segdata(seg, i, j, data);
Jingning Han's avatar
Jingning Han committed
785
786
787
788
789
790
      }
    }
  }
}

static void setup_loopfilter(struct loopfilter *lf,
Adrian Grange's avatar
Adrian Grange committed
791
792
793
                             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
794
795
796
797
798

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

Adrian Grange's avatar
Adrian Grange committed
799
  lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
800
  if (lf->mode_ref_delta_enabled) {
Adrian Grange's avatar
Adrian Grange committed
801
    lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
802
803
804
    if (lf->mode_ref_delta_update) {
      int i;

805
      for (i = 0; i < MAX_REF_FRAMES; i++)
Adrian Grange's avatar
Adrian Grange committed
806
807
        if (aom_rb_read_bit(rb))
          lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Jingning Han's avatar
Jingning Han committed
808
809

      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Adrian Grange's avatar
Adrian Grange committed
810
811
        if (aom_rb_read_bit(rb))
          lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Jingning Han's avatar
Jingning Han committed
812
813
814
815
    }
  }
}

Steinar Midtskogen's avatar
Steinar Midtskogen committed
816
#if CONFIG_CLPF
817
static void setup_clpf(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
818
  cm->clpf_blocks = 0;
Steinar Midtskogen's avatar
Steinar Midtskogen committed
819
820
821
822
  cm->clpf_strength_y = aom_rb_read_literal(rb, 2);
  cm->clpf_strength_u = aom_rb_read_literal(rb, 2);
  cm->clpf_strength_v = aom_rb_read_literal(rb, 2);
  if (cm->clpf_strength_y) {
823
824
825
826
827
828
829
830
831
832
833
834
    cm->clpf_size = aom_rb_read_literal(rb, 2);
    if (cm->clpf_size) {
      int i;
      cm->clpf_numblocks = aom_rb_read_literal(rb, av1_clpf_maxbits(cm));
      CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(cm->clpf_numblocks));
      for (i = 0; i < cm->clpf_numblocks; i++) {
        cm->clpf_blocks[i] = aom_rb_read_literal(rb, 1);
      }
    }
  }
}

835
836
837
838
839
static int clpf_bit(UNUSED int k, UNUSED int l,
                    UNUSED const YV12_BUFFER_CONFIG *rec,
                    UNUSED const YV12_BUFFER_CONFIG *org,
                    UNUSED const AV1_COMMON *cm, UNUSED int block_size,
                    UNUSED int w, UNUSED int h, UNUSED unsigned int strength,
840
                    UNUSED unsigned int fb_size_log2, uint8_t *bit) {
841
  return *bit;
Steinar Midtskogen's avatar
Steinar Midtskogen committed
842
843
844
}
#endif

845
#if CONFIG_DERING
846
static void setup_dering(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
clang-format's avatar
clang-format committed
847
  cm->dering_level = aom_rb_read_literal(rb, DERING_LEVEL_BITS);
848
849
850
}
#endif  // CONFIG_DERING

Adrian Grange's avatar
Adrian Grange committed
851
852
853
static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
  return aom_rb_read_bit(rb)
             ? aom_rb_read_inv_signed_literal(rb, CONFIG_MISC_FIXES ? 6 : 4)
clang-format's avatar
clang-format committed
854
             : 0;
Jingning Han's avatar
Jingning Han committed
855
856
}

857
static void setup_quantization(AV1_COMMON *const cm,
Adrian Grange's avatar
Adrian Grange committed
858
859
                               struct aom_read_bit_buffer *rb) {
  cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Jingning Han's avatar
Jingning Han committed
860
861
862
863
  cm->y_dc_delta_q = read_delta_q(rb);
  cm->uv_dc_delta_q = read_delta_q(rb);
  cm->uv_ac_delta_q = read_delta_q(rb);
  cm->dequant_bit_depth = cm->bit_depth;
864
#if CONFIG_AOM_QM
Adrian Grange's avatar
Adrian Grange committed
865
  cm->using_qmatrix = aom_rb_read_bit(rb);
866
  if (cm->using_qmatrix) {
Adrian Grange's avatar
Adrian Grange committed
867
868
    cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
    cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
869
870
871
872
  } else {
    cm->min_qmlevel = 0;
    cm->max_qmlevel = 0;
  }
873
#endif
Jingning Han's avatar
Jingning Han committed
874
875
}

876
static void setup_segmentation_dequant(AV1_COMMON *const cm) {
Jingning Han's avatar
Jingning Han committed
877
  // Build y/uv dequant values based on segmentation.
878
879
880
881
  int i = 0;
#if CONFIG_AOM_QM
  int lossless;
  int j = 0;
882
  int qmlevel;
883
  int using_qm = cm->using_qmatrix;
884
885
  int minqm = cm->min_qmlevel;
  int maxqm = cm->max_qmlevel;
886
#endif
Jingning Han's avatar
Jingning Han committed
887
888
  if (cm->seg.enabled) {
    for (i = 0; i < MAX_SEGMENTS; ++i) {
889
      const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
clang-format's avatar
clang-format committed
890
      cm->y_dequant[i][0] =
891
892
          av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
      cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
clang-format's avatar
clang-format committed
893
      cm->uv_dequant[i][0] =
894
          av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
clang-format's avatar
clang-format committed
895
      cm->uv_dequant[i][1] =
896
          av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
897
898
899
900
#if CONFIG_AOM_QM
      lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
                 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
      // NB: depends on base index so there is only 1 set per frame
901
      // No quant weighting when lossless or signalled not using QM
902
903
904
      qmlevel = (lossless || using_qm == 0)
                    ? NUM_QM_LEVELS - 1
                    : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
905
      for (j = 0; j < TX_SIZES; ++j) {