decodeframe.c 83.7 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

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

Yaowu Xu's avatar
Yaowu Xu committed
29
#include "av1/common/alloccommon.h"
Yaowu Xu's avatar
Yaowu Xu committed
30
#if CONFIG_CLPF
Yaowu Xu's avatar
Yaowu Xu committed
31
#include "av1/common/clpf.h"
Yaowu Xu's avatar
Yaowu Xu committed
32
#endif
Yaowu Xu's avatar
Yaowu Xu committed
33
#include "av1/common/common.h"
34
#if CONFIG_DERING
Yaowu Xu's avatar
Yaowu Xu committed
35
#include "av1/common/dering.h"
36
#endif  // CONFIG_DERING
Yaowu Xu's avatar
Yaowu Xu committed
37 38 39 40 41 42
#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
43
#include "av1/common/reconintra.h"
Yaowu Xu's avatar
Yaowu Xu committed
44
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
45
#include "av1/common/thread_common.h"
Yaowu Xu's avatar
Yaowu Xu committed
46
#include "av1/common/tile_common.h"
47

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

54
#define MAX_AV1_HEADER_SIZE 80
Jingning Han's avatar
Jingning Han committed
55

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

  return 0;
}

65
static void setup_compound_reference_mode(AV1_COMMON *cm) {
66 67 68 69 70 71 72 73
#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
74
  if (cm->ref_frame_sign_bias[LAST_FRAME] ==
clang-format's avatar
clang-format committed
75
      cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
Jingning Han's avatar
Jingning Han committed
76 77 78 79
    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
80
             cm->ref_frame_sign_bias[ALTREF_FRAME]) {
Jingning Han's avatar
Jingning Han committed
81 82 83 84 85 86 87 88
    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;
  }
89
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
90 91 92 93 94 95
}

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
96 97
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
98 99 100
  return data > max ? max : data;
}

101
#if CONFIG_MISC_FIXES
Adrian Grange's avatar
Adrian Grange committed
102 103
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);
104 105
}
#else
Adrian Grange's avatar
Adrian Grange committed
106 107 108
static TX_MODE read_tx_mode(aom_reader *r) {
  TX_MODE tx_mode = aom_read_literal(r, 2);
  if (tx_mode == ALLOW_32X32) tx_mode += aom_read_bit(r);
Jingning Han's avatar
Jingning Han committed
109 110
  return tx_mode;
}
111
#endif
Jingning Han's avatar
Jingning Han committed
112

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

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

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

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

Adrian Grange's avatar
Adrian Grange committed
129
static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
130
  int i, j;
131
  for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
Jingning Han's avatar
Jingning Han committed
132
    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
133
      av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
134 135 136 137 138
#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
139 140
}

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

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

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

  if (cm->reference_mode == REFERENCE_MODE_SELECT)
    for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
190
      av1_diff_update_prob(r, &fc->comp_inter_prob[i]);
Jingning Han's avatar
Jingning Han committed
191 192

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

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

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

Adrian Grange's avatar
Adrian Grange committed
221
static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  int i, j;

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

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

  for (i = 0; i < 2; ++i) {
    nmv_component *const comp_ctx = &ctx->comps[i];
    for (j = 0; j < CLASS0_SIZE; ++j)
      update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
    update_mv_probs(comp_ctx->fp, 3, r);
  }

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

    if (eob == 1) {
      dqcoeff[0] = 0;
    } else {
304
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
Jingning Han's avatar
Jingning Han committed
305 306 307 308 309 310 311 312 313
        memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
      else if (tx_size == TX_32X32 && eob <= 34)
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
      else
        memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
    }
  }
}

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

    if (eob == 1) {
      dqcoeff[0] = 0;
    } else {
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
        memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
      else if (tx_size == TX_32X32 && eob <= 34)
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
      else
        memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
    }
  }
}

static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
379
                                                aom_reader *r,
Jingning Han's avatar
Jingning Han committed
380
                                                MB_MODE_INFO *const mbmi,
clang-format's avatar
clang-format committed
381
                                                int plane, int row, int col,
Jingning Han's avatar
Jingning Han committed
382 383 384
                                                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
385
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
Jingning Han's avatar
Jingning Han committed
386
  uint8_t *dst;
hui su's avatar
hui su committed
387
  int block_idx = (row << 1) + col;
Jingning Han's avatar
Jingning Han committed
388 389 390
  dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];

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

393
  av1_predict_intra_block(xd, pd->n4_wl, pd->n4_hl, tx_size, mode, dst,
clang-format's avatar
clang-format committed
394
                          pd->dst.stride, dst, pd->dst.stride, col, row, plane);
Jingning Han's avatar
Jingning Han committed
395 396

  if (!mbmi->skip) {
hui su's avatar
hui su committed
397
    TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx);
398 399 400
    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
401 402
    inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
                                  pd->dst.stride, eob);
Jingning Han's avatar
Jingning Han committed
403 404 405
  }
}

Adrian Grange's avatar
Adrian Grange committed
406
static int reconstruct_inter_block(MACROBLOCKD *const xd, aom_reader *r,
clang-format's avatar
clang-format committed
407 408
                                   MB_MODE_INFO *const mbmi, int plane, int row,
                                   int col, TX_SIZE tx_size) {
Jingning Han's avatar
Jingning Han committed
409
  struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
410 411 412
  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);
413 414 415
  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
416

clang-format's avatar
clang-format committed
417 418 419
  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
420 421 422
  return eob;
}

clang-format's avatar
clang-format committed
423 424
static INLINE TX_SIZE dec_get_uv_tx_size(const MB_MODE_INFO *mbmi, int n4_wl,
                                         int n4_hl) {
Jingning Han's avatar
Jingning Han committed
425
  // get minimum log2 num4x4s dimension
Adrian Grange's avatar
Adrian Grange committed
426 427
  const int x = AOMMIN(n4_wl, n4_hl);
  return AOMMIN(mbmi->tx_size, x);
Jingning Han's avatar
Jingning Han committed
428 429 430 431 432 433 434 435 436 437 438
}

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

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

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

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

466
static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
467
                         int mi_row, int mi_col, aom_reader *r,
clang-format's avatar
clang-format committed
468
                         BLOCK_SIZE bsize, int bwl, int bhl) {
469
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
470 471 472
  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
473 474
  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
475

clang-format's avatar
clang-format committed
476 477
  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
478 479 480 481 482

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

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

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

  if (!is_inter_block(mbmi)) {
    int plane;
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
      const struct macroblockd_plane *const pd = &xd->plane[plane];
      const TX_SIZE tx_size =
          plane ? dec_get_uv_tx_size(mbmi, pd->n4_wl, pd->n4_hl)
clang-format's avatar
clang-format committed
499
                : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
500 501 502 503
      const int num_4x4_w = pd->n4_w;
      const int num_4x4_h = pd->n4_h;
      const int step = (1 << tx_size);
      int row, col;
clang-format's avatar
clang-format committed
504 505 506 507 508 509 510 511
      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
512 513 514

      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
515 516
          predict_and_reconstruct_intra_block(xd, r, mbmi, plane, row, col,
                                              tx_size);
Jingning Han's avatar
Jingning Han committed
517 518 519
    }
  } else {
    // Prediction
520
    av1_build_inter_predictors_sb(xd, mi_row, mi_col, AOMMAX(bsize, BLOCK_8X8));
521 522 523 524
#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
525 526 527 528 529 530 531 532 533
    // 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 =
            plane ? dec_get_uv_tx_size(mbmi, pd->n4_wl, pd->n4_hl)
clang-format's avatar
clang-format committed
534
                  : mbmi->tx_size;
Jingning Han's avatar
Jingning Han committed
535 536 537 538
        const int num_4x4_w = pd->n4_w;
        const int num_4x4_h = pd->n4_h;
        const int step = (1 << tx_size);
        int row, col;
clang-format's avatar
clang-format committed
539 540 541 542 543 544 545 546
        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
547 548 549

        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
550 551
            eobtotal +=
                reconstruct_inter_block(xd, r, mbmi, plane, row, col, tx_size);
Jingning Han's avatar
Jingning Han committed
552 553 554
      }

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

Adrian Grange's avatar
Adrian Grange committed
563
  xd->corrupted |= aom_reader_has_error(r);
Jingning Han's avatar
Jingning Han committed
564 565
}

clang-format's avatar
clang-format committed
566 567
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
568
  const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
569 570
  const PARTITION_CONTEXT *left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
clang-format's avatar
clang-format committed
571
  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
Jingning Han's avatar
Jingning Han committed
572

clang-format's avatar
clang-format committed
573
  //  assert(bsl >= 0);
Jingning Han's avatar
Jingning Han committed
574 575 576 577

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

clang-format's avatar
clang-format committed
578 579
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
580 581
                                                int bw) {
  PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
582 583
  PARTITION_CONTEXT *const left_ctx =
      xd->left_seg_context + (mi_row & MAX_MIB_MASK);
Jingning Han's avatar
Jingning Han committed
584 585 586 587 588 589 590 591

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

592
static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
593
                                     int mi_row, int mi_col, aom_reader *r,
Jingning Han's avatar
Jingning Han committed
594 595
                                     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
596
  const aom_prob *const probs = cm->fc->partition_prob[ctx];
Jingning Han's avatar
Jingning Han committed
597 598 599 600
  FRAME_COUNTS *counts = xd->counts;
  PARTITION_TYPE p;

  if (has_rows && has_cols)
601 602 603 604
#if CONFIG_DAALA_EC
    p = (PARTITION_TYPE)aom_read_tree_cdf(r, cm->fc->partition_cdf[ctx],
                                          PARTITION_TYPES);
#else
605
    p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs);
606
#endif
Jingning Han's avatar
Jingning Han committed
607
  else if (!has_rows && has_cols)
Adrian Grange's avatar
Adrian Grange committed
608
    p = aom_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
Jingning Han's avatar
Jingning Han committed
609
  else if (has_rows && !has_cols)
Adrian Grange's avatar
Adrian Grange committed
610
    p = aom_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
Jingning Han's avatar
Jingning Han committed
611 612 613
  else
    p = PARTITION_SPLIT;

clang-format's avatar
clang-format committed
614
  if (counts) ++counts->partition[ctx][p];
Jingning Han's avatar
Jingning Han committed
615 616 617 618 619

  return p;
}

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

clang-format's avatar
clang-format committed
634 635
  partition =
      read_partition(cm, xd, mi_row, mi_col, r, has_rows, has_cols, n8x8_l2);
Jingning Han's avatar
Jingning Han committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
  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
666
      default: assert(0 && "Invalid partition type");
Jingning Han's avatar
Jingning Han committed
667 668 669 670 671 672 673
    }
  }

  // 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);
674 675 676 677

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

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

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

705
static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
Adrian Grange's avatar
Adrian Grange committed
706
                                   aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
707 708
  int i, j, k, l, m;

Adrian Grange's avatar
Adrian Grange committed
709
  if (aom_read_bit(r))
Jingning Han's avatar
Jingning Han committed
710 711 712 713 714
    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)
715
              av1_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
Jingning Han's avatar
Jingning Han committed
716 717
}

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

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

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

Adrian Grange's avatar
Adrian Grange committed
739
  seg->enabled = aom_rb_read_bit(rb);
clang-format's avatar
clang-format committed
740
  if (!seg->enabled) return;
Jingning Han's avatar
Jingning Han committed
741 742

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

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

775
    av1_clearall_segfeatures(seg);
Jingning Han's avatar
Jingning Han committed
776 777 778 779

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

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

  // 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
802
  lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
803
  if (lf->mode_ref_delta_enabled) {
Adrian Grange's avatar
Adrian Grange committed
804
    lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Jingning Han's avatar
Jingning Han committed
805 806 807
    if (lf->mode_ref_delta_update) {
      int i;

808
      for (i = 0; i < MAX_REF_FRAMES; i++)
Adrian Grange's avatar
Adrian Grange committed
809 810
        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
811 812

      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
Adrian Grange's avatar
Adrian Grange committed
813 814
        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
815 816 817 818
    }
  }
}

Steinar Midtskogen's avatar
Steinar Midtskogen committed
819
#if CONFIG_CLPF
820
static void setup_clpf(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
  cm->clpf_blocks = 0;
  cm->clpf_strength = aom_rb_read_literal(rb, 2);
  if (cm->clpf_strength) {
    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);
      }
    }
  }
}

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

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

Adrian Grange's avatar
Adrian Grange committed
850 851 852
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
853
             : 0;
Jingning Han's avatar
Jingning Han committed
854 855
}

856
static void setup_quantization(AV1_COMMON *const cm,
Adrian Grange's avatar
Adrian Grange committed
857 858
                               struct aom_read_bit_buffer *rb) {
  cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
Jingning Han's avatar
Jingning Han committed
859 860 861 862
  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;
863
#if CONFIG_AOM_QM
Adrian Grange's avatar
Adrian Grange committed
864
  cm->using_qmatrix = aom_rb_read_bit(rb);
865
  if (cm->using_qmatrix) {