decodetxb.c 22.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (c) 2017, Alliance for Open Media. All rights reserved
 *
 * 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.
 */

#include "av1/common/scan.h"
Angie Chiang's avatar
Angie Chiang committed
13
#include "av1/common/idct.h"
14
#include "av1/common/txb_common.h"
15
#include "av1/decoder/decodemv.h"
16
#include "av1/decoder/decodetxb.h"
17
#include "av1/decoder/dsubexp.h"
18
#include "av1/decoder/symbolrate.h"
19 20 21

#define ACCT_STR __func__

22 23 24 25
static int read_golomb(MACROBLOCKD *xd, aom_reader *r, FRAME_COUNTS *counts) {
#if !CONFIG_SYMBOLRATE
  (void)counts;
#endif
26 27 28 29 30
  int x = 1;
  int length = 0;
  int i = 0;

  while (!i) {
31
    i = av1_read_record_bit(counts, r, ACCT_STR);
32
    ++length;
33 34 35 36 37
    if (length >= 32) {
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
                         "Invalid length in read_golomb");
      break;
    }
38 39 40 41
  }

  for (i = 0; i < length - 1; ++i) {
    x <<= 1;
42
    x += av1_read_record_bit(counts, r, ACCT_STR);
43 44 45 46 47
  }

  return x - 1;
}

Linfeng Zhang's avatar
Linfeng Zhang committed
48 49 50 51 52 53
static INLINE int read_nz_map(aom_reader *const r, uint8_t *const levels,
                              const int plane, const int16_t *const scan,
                              const TX_SIZE tx_size, const TX_TYPE tx_type,
                              FRAME_CONTEXT *const fc,
                              FRAME_COUNTS *const counts) {
  const TX_SIZE txs_ctx = get_txsize_context(tx_size);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
  const int height = tx_size_high[tx_size];
#if CONFIG_CTX1D
  const int width = tx_size_wide[tx_size];
  const int eob_offset = width + height;
  const TX_CLASS tx_class = get_tx_class(tx_type);
  const int seg_eob =
      (tx_class == TX_CLASS_2D) ? tx_size_2d[tx_size] : eob_offset;
#else
  const int seg_eob = tx_size_2d[tx_size];
#endif
  const PLANE_TYPE plane_type = get_plane_type(plane);
  unsigned int(*nz_map_count)[SIG_COEF_CONTEXTS][2] =
      (counts) ? &counts->nz_map[txs_ctx][plane_type] : NULL;
#if !LV_MAP_PROB
  aom_prob *nz_map = fc->nz_map[txs_ctx][plane_type];
  aom_prob *eob_flag = fc->eob_flag[txs_ctx][plane_type];
#endif
  int c;
  for (c = 0; c < seg_eob; ++c) {
    int is_nz;
Linfeng Zhang's avatar
Linfeng Zhang committed
75
    int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, 1);
Linfeng Zhang's avatar
Linfeng Zhang committed
76
    int eob_ctx = get_eob_ctx(scan[c], txs_ctx, tx_type);
77 78 79

    if (c < seg_eob - 1) {
#if LV_MAP_PROB
80
      is_nz = av1_read_record_bin(
81 82
          counts, r, fc->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2,
          ACCT_STR);
83 84 85 86 87 88 89 90
#else
      is_nz = aom_read(r, nz_map[coeff_ctx], ACCT_STR);
#endif
    } else {
      is_nz = 1;
    }

    // set non-zero coefficient map.
Linfeng Zhang's avatar
Linfeng Zhang committed
91
    levels[scan[c]] = is_nz;
92 93 94 95 96 97 98 99 100 101

    if (c == seg_eob - 1) {
      ++c;
      break;
    }

    if (counts) ++(*nz_map_count)[coeff_ctx][is_nz];

    if (is_nz) {
#if LV_MAP_PROB
102
      int is_eob = av1_read_record_bin(
103 104
          counts, r, fc->eob_flag_cdf[txs_ctx][plane_type][eob_ctx], 2,
          ACCT_STR);
105 106 107 108 109 110 111 112 113 114 115
#else
      int is_eob = aom_read(r, eob_flag[eob_ctx], ACCT_STR);
#endif
      if (counts) ++counts->eob_flag[txs_ctx][plane_type][eob_ctx][is_eob];
      if (is_eob) break;
    }
  }
  return AOMMIN(seg_eob, c + 1);
}

#if CONFIG_CTX1D
Linfeng Zhang's avatar
Linfeng Zhang committed
116 117 118 119
static INLINE int read_nz_map_vert(aom_reader *r, uint8_t *levels, int plane,
                                   const int16_t *scan, const int16_t *iscan,
                                   TX_SIZE tx_size, TX_TYPE tx_type,
                                   FRAME_CONTEXT *fc, FRAME_COUNTS *counts) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133
  const TX_SIZE txs_ctx = get_txsize_context(tx_size);
  const PLANE_TYPE plane_type = get_plane_type(plane);
  const TX_CLASS tx_class = get_tx_class(tx_type);
  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
  const int width = tx_size_wide[tx_size];
  const int height = tx_size_high[tx_size];
  int16_t eob_ls[MAX_HVTX_SIZE];
  int eob = 0;
#if !LV_MAP_PROB
  aom_prob *nz_map = fc->nz_map[txs_ctx][plane_type];
#endif
  for (int col = 0; col < width; ++col) {
    int el_ctx = get_empty_line_ctx(col, eob_ls);
#if LV_MAP_PROB
134
    int empty_line = av1_read_record_bin(
135
        counts, r, fc->empty_line_cdf[txs_ctx][plane_type][tx_class][el_ctx], 2,
136 137 138 139 140 141 142 143 144 145 146
        ACCT_STR);
#else
    int empty_line = aom_read(
        r, fc->empty_line[txs_ctx][plane_type][tx_class][el_ctx], ACCT_STR);
#endif
    if (counts)
      ++counts->empty_line[txs_ctx][plane_type][tx_class][el_ctx][empty_line];
    if (!empty_line) {
      int row;
      for (row = 0; row < height; ++row) {
        if (row + 1 != height) {
147 148 149
          int coeff_idx = row * width + col;
          int scan_idx = iscan[coeff_idx];
          int coeff_ctx =
Linfeng Zhang's avatar
Linfeng Zhang committed
150
              get_nz_map_ctx(levels, scan_idx, scan, bwl, height, tx_type, 1);
151
#if LV_MAP_PROB
152
          int is_nz = av1_read_record_bin(
153 154
              counts, r, fc->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2,
              ACCT_STR);
155 156 157 158
#else
          int is_nz = aom_read(r, nz_map[coeff_ctx], ACCT_STR);
#endif
          if (counts) ++counts->nz_map[txs_ctx][plane_type][coeff_ctx][is_nz];
Linfeng Zhang's avatar
Linfeng Zhang committed
159
          levels[coeff_idx] = is_nz;
160 161 162 163 164
          if (is_nz) {
            eob = AOMMAX(eob, iscan[coeff_idx] + 1);
            if (row + 1 != height) {
              int eob_ctx = get_hv_eob_ctx(col, row, eob_ls);
#if LV_MAP_PROB
165
              int is_eob = av1_read_record_bin(
166 167
                  counts, r,
                  fc->hv_eob_cdf[txs_ctx][plane_type][tx_class][eob_ctx], 2,
168 169 170 171 172 173 174 175 176 177 178 179 180
                  ACCT_STR);
#else
              int is_eob = aom_read(
                  r, fc->hv_eob[txs_ctx][plane_type][tx_class][eob_ctx],
                  ACCT_STR);
#endif
              if (counts)
                ++counts
                      ->hv_eob[txs_ctx][plane_type][tx_class][eob_ctx][is_eob];
              if (is_eob) break;
            }
          }
        } else {
181
          int coeff_idx = row * width + col;
Linfeng Zhang's avatar
Linfeng Zhang committed
182
          levels[coeff_idx] = 1;
183 184 185 186 187 188 189 190 191 192 193
          eob = AOMMAX(eob, iscan[coeff_idx] + 1);
        }
      }
      eob_ls[col] = AOMMIN(height, row + 1);
    } else {
      eob_ls[col] = 0;
    }
  }
  return eob;
}

Linfeng Zhang's avatar
Linfeng Zhang committed
194 195 196 197
static INLINE int read_nz_map_horiz(aom_reader *r, uint8_t *levels, int plane,
                                    const int16_t *scan, const int16_t *iscan,
                                    TX_SIZE tx_size, TX_TYPE tx_type,
                                    FRAME_CONTEXT *fc, FRAME_COUNTS *counts) {
198 199 200 201 202 203 204 205 206 207 208 209 210 211
  const TX_SIZE txs_ctx = get_txsize_context(tx_size);
  const PLANE_TYPE plane_type = get_plane_type(plane);
  const TX_CLASS tx_class = get_tx_class(tx_type);
  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
  const int width = tx_size_wide[tx_size];
  const int height = tx_size_high[tx_size];
  int16_t eob_ls[MAX_HVTX_SIZE];
  int eob = 0;
#if !LV_MAP_PROB
  aom_prob *nz_map = fc->nz_map[txs_ctx][plane_type];
#endif
  for (int row = 0; row < height; ++row) {
    int el_ctx = get_empty_line_ctx(row, eob_ls);
#if LV_MAP_PROB
212
    int empty_line = av1_read_record_bin(
213
        counts, r, fc->empty_line_cdf[txs_ctx][plane_type][tx_class][el_ctx], 2,
214 215 216 217 218 219 220 221 222 223 224
        ACCT_STR);
#else
    int empty_line = aom_read(
        r, fc->empty_line[txs_ctx][plane_type][tx_class][el_ctx], ACCT_STR);
#endif
    if (counts)
      ++counts->empty_line[txs_ctx][plane_type][tx_class][el_ctx][empty_line];
    if (!empty_line) {
      int col;
      for (col = 0; col < width; ++col) {
        if (col + 1 != width) {
225 226 227
          int coeff_idx = row * width + col;
          int scan_idx = iscan[coeff_idx];
          int coeff_ctx =
Linfeng Zhang's avatar
Linfeng Zhang committed
228
              get_nz_map_ctx(levels, scan_idx, scan, bwl, height, tx_type, 1);
229
#if LV_MAP_PROB
230
          int is_nz = av1_read_record_bin(
231 232
              counts, r, fc->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2,
              ACCT_STR);
233 234 235 236
#else
          int is_nz = aom_read(r, nz_map[coeff_ctx], ACCT_STR);
#endif
          if (counts) ++counts->nz_map[txs_ctx][plane_type][coeff_ctx][is_nz];
Linfeng Zhang's avatar
Linfeng Zhang committed
237
          levels[coeff_idx] = is_nz;
238 239 240 241
          if (is_nz) {
            eob = AOMMAX(eob, iscan[coeff_idx] + 1);
            int eob_ctx = get_hv_eob_ctx(row, col, eob_ls);
#if LV_MAP_PROB
242
            int is_eob = av1_read_record_bin(
243 244
                counts, r,
                fc->hv_eob_cdf[txs_ctx][plane_type][tx_class][eob_ctx], 2,
245 246 247 248 249 250 251 252 253 254 255
                ACCT_STR);
#else
            int is_eob =
                aom_read(r, fc->hv_eob[txs_ctx][plane_type][tx_class][eob_ctx],
                         ACCT_STR);
#endif
            if (counts)
              ++counts->hv_eob[txs_ctx][plane_type][tx_class][eob_ctx][is_eob];
            if (is_eob) break;
          }
        } else {
256
          int coeff_idx = row * width + col;
Linfeng Zhang's avatar
Linfeng Zhang committed
257
          levels[coeff_idx] = 1;
258 259 260 261 262 263 264 265 266 267 268 269
          eob = AOMMAX(eob, iscan[coeff_idx] + 1);
        }
      }
      eob_ls[row] = AOMMIN(width, col + 1);
    } else {
      eob_ls[row] = 0;
    }
  }
  return eob;
}
#endif

Dake He's avatar
Dake He committed
270 271 272 273 274 275 276 277 278 279
#if CONFIG_EOB_FIRST
static INLINE int rec_eob_pos(int16_t eob_token, int16_t extra) {
  int eob = k_eob_group_start[eob_token];
  if (eob > 2) {
    eob += extra;
  }
  return eob;
}
#endif

280
uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
281 282
                            aom_reader *r, int blk_row, int blk_col, int block,
                            int plane, tran_low_t *tcoeffs, TXB_CTX *txb_ctx,
Jingning Han's avatar
Jingning Han committed
283
                            TX_SIZE tx_size, int16_t *max_scan_line, int *eob) {
284
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
285
  FRAME_COUNTS *counts = xd->counts;
286
  TX_SIZE txs_ctx = get_txsize_context(tx_size);
287
  PLANE_TYPE plane_type = get_plane_type(plane);
288
#if !LV_MAP_PROB
289 290
  aom_prob *nz_map = ec_ctx->nz_map[txs_ctx][plane_type];
  aom_prob *eob_flag = ec_ctx->eob_flag[txs_ctx][plane_type];
291
#endif
292
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
293
  const int seg_eob = tx_size_2d[tx_size];
294
  int c = 0;
295
  int update_eob = -1;
296
  const int16_t *const dequant = xd->plane[plane].seg_dequant[mbmi->segment_id];
297
  const int shift = av1_get_tx_scale(tx_size);
298
  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
299
  const int height = tx_size_high[tx_size];
300
  int cul_level = 0;
301 302 303
  uint8_t levels[64 * 64];
  int8_t signs[64 * 64];

304 305
  memset(tcoeffs, 0, sizeof(*tcoeffs) * seg_eob);

306
#if LV_MAP_PROB
307
  int all_zero = av1_read_record_bin(
308 309
      counts, r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2,
      ACCT_STR);
310
#else
Angie Chiang's avatar
Angie Chiang committed
311
  int all_zero =
312
      aom_read(r, ec_ctx->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx], ACCT_STR);
313
#endif
Angie Chiang's avatar
Angie Chiang committed
314
  if (xd->counts)
315
    ++xd->counts->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx][all_zero];
Angie Chiang's avatar
Angie Chiang committed
316

317
  *eob = 0;
Angie Chiang's avatar
Angie Chiang committed
318 319
  if (all_zero) {
    *max_scan_line = 0;
320 321 322
#if CONFIG_TXK_SEL
    if (plane == 0) mbmi->txk_type[(blk_row << 4) + blk_col] = DCT_DCT;
#endif
Angie Chiang's avatar
Angie Chiang committed
323 324 325
    return 0;
  }

Linfeng Zhang's avatar
Linfeng Zhang committed
326
  memset(levels, 0, sizeof(levels[0]) * seg_eob);
327 328
  memset(signs, 0, sizeof(signs[0]) * seg_eob);

Jingning Han's avatar
Jingning Han committed
329 330
  (void)blk_row;
  (void)blk_col;
Angie Chiang's avatar
Angie Chiang committed
331
#if CONFIG_TXK_SEL
332 333
  av1_read_tx_type(cm, xd, blk_row, blk_col, block, plane,
                   get_min_tx_size(tx_size), r);
Angie Chiang's avatar
Angie Chiang committed
334
#endif
335 336
  const TX_TYPE tx_type =
      av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
Angie Chiang's avatar
Angie Chiang committed
337
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
338
  const int16_t *scan = scan_order->scan;
Angie Chiang's avatar
Angie Chiang committed
339

Dake He's avatar
Dake He committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
#if CONFIG_EOB_FIRST

  unsigned int(*nz_map_count)[SIG_COEF_CONTEXTS][2] =
      (counts) ? &counts->nz_map[txs_ctx][plane_type] : NULL;
  int16_t dummy;
  int16_t max_eob_pt = get_eob_pos_token(seg_eob, &dummy);

  int16_t eob_extra = 0;
  int16_t eob_pt = 0;
  int is_equal = 0;

  for (int i = 1; i < max_eob_pt; i++) {
    int eob_pos_ctx = get_eob_pos_ctx(i);
    is_equal = av1_read_record_bin(
        counts, r, ec_ctx->eob_flag_cdf[txs_ctx][plane_type][eob_pos_ctx], 2,
        ACCT_STR);

    // aom_read_symbol(r,
    // ec_ctx->eob_flag_cdf[AOMMIN(txs_ctx,3)][plane_type][eob_pos_ctx], 2,
    // ACCT_STR);
    if (counts) ++counts->eob_flag[txs_ctx][plane_type][eob_pos_ctx][is_equal];

    if (is_equal) {
      eob_pt = i;
      break;
    }
  }
  if (is_equal == 0) {
    eob_pt = max_eob_pt;
  }

  // printf("Dec: ");
  if (k_eob_offset_bits[eob_pt] > 0) {
Angie Chiang's avatar
Angie Chiang committed
373 374 375 376 377 378 379 380 381 382 383
    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
    int bit = av1_read_record_bin(
        counts, r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_pt], 2,
        ACCT_STR);
    if (bit) {
      eob_extra += (1 << eob_shift);
    }

    for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
      eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
      bit = av1_read_record_bit(counts, r, ACCT_STR);
Dake He's avatar
Dake He committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      if (bit) {
        eob_extra += (1 << eob_shift);
      }
      //  printf("%d ", bit);
    }
  }
  *eob = rec_eob_pos(eob_pt, eob_extra);
  // printf("=>[%d, %d], (%d, %d)\n", seg_eob, *eob, eob_pt, eob_extra);

  for (int i = 0; i < *eob; ++i) {
    c = *eob - 1 - i;
    int is_nz;
    int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, 1);
    // int eob_ctx = get_eob_ctx(tcoeffs, scan[c], txs_ctx, tx_type);

    if (c < *eob - 1) {
#if LV_MAP_PROB
      is_nz = av1_read_record_bin(
          counts, r, ec_ctx->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2,
          ACCT_STR);
#else
      is_nz = aom_read(r, nz_map[coeff_ctx], ACCT_STR);
#endif
    } else {
      is_nz = 1;
    }

    // set non-zero coefficient map.
    levels[scan[c]] = is_nz;

    if (counts) ++(*nz_map_count)[coeff_ctx][is_nz];
  }
#else

418 419 420 421 422
#if CONFIG_CTX1D
  const int16_t *iscan = scan_order->iscan;
  TX_CLASS tx_class = get_tx_class(tx_type);
  if (tx_class == TX_CLASS_2D) {
    *eob =
Linfeng Zhang's avatar
Linfeng Zhang committed
423
        read_nz_map(r, levels, plane, scan, tx_size, tx_type, ec_ctx, counts);
424
  } else {
425
#if LV_MAP_PROB
426
    const int eob_mode = av1_read_record_bin(
427 428
        counts, r, ec_ctx->eob_mode_cdf[txs_ctx][plane_type][tx_class], 2,
        ACCT_STR);
429
#else
430 431
    const int eob_mode =
        aom_read(r, ec_ctx->eob_mode[txs_ctx][plane_type][tx_class], ACCT_STR);
432
#endif
433 434
    if (counts) ++counts->eob_mode[txs_ctx][plane_type][tx_class][eob_mode];
    if (eob_mode == 0) {
Linfeng Zhang's avatar
Linfeng Zhang committed
435 436
      *eob =
          read_nz_map(r, levels, plane, scan, tx_size, tx_type, ec_ctx, counts);
437
    } else {
438 439
      assert(tx_class == TX_CLASS_VERT || tx_class == TX_CLASS_HORIZ);
      if (tx_class == TX_CLASS_VERT)
Linfeng Zhang's avatar
Linfeng Zhang committed
440 441
        *eob = read_nz_map_vert(r, levels, plane, scan, iscan, tx_size, tx_type,
                                ec_ctx, counts);
442
      else
Linfeng Zhang's avatar
Linfeng Zhang committed
443
        *eob = read_nz_map_horiz(r, levels, plane, scan, iscan, tx_size,
444
                                 tx_type, ec_ctx, counts);
445
    }
446
  }
447
#else
Linfeng Zhang's avatar
Linfeng Zhang committed
448
  *eob = read_nz_map(r, levels, plane, scan, tx_size, tx_type, ec_ctx, counts);
Dake He's avatar
Dake He committed
449 450
#endif

451
#endif
452
  *max_scan_line = *eob;
453 454 455

  int i;
  for (i = 0; i < NUM_BASE_LEVELS; ++i) {
456
#if !LV_MAP_PROB
457
    aom_prob *coeff_base = ec_ctx->coeff_base[txs_ctx][plane_type][i];
458
#endif
459
    update_eob = 0;
460
    for (c = *eob - 1; c >= 0; --c) {
461 462
      uint8_t *const level = &levels[scan[c]];
      int8_t *const sign = &signs[scan[c]];
463 464
      int ctx;

465
      if (*level <= i) continue;
466

467
      ctx = get_base_ctx(levels, scan[c], bwl, height, i + 1);
468

469
#if LV_MAP_PROB
470
      if (av1_read_record_bin(
471 472
              counts, r, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][i][ctx], 2,
              ACCT_STR))
473 474 475 476
#else
      if (aom_read(r, coeff_base[ctx], ACCT_STR))
#endif
      {
477
        assert(*level == i + 1);
478 479
        cul_level += i + 1;

480
        if (counts) ++counts->coeff_base[txs_ctx][plane_type][i][ctx][1];
481 482 483

        if (c == 0) {
          int dc_sign_ctx = txb_ctx->dc_sign_ctx;
484
#if LV_MAP_PROB
485
          *sign = av1_read_record_bin(
486 487
              counts, r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2,
              ACCT_STR);
488
#else
489
          *sign =
490
              aom_read(r, ec_ctx->dc_sign[plane_type][dc_sign_ctx], ACCT_STR);
491
#endif
492
          if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][*sign];
493
        } else {
494
          *sign = av1_read_record_bit(counts, r, ACCT_STR);
495 496 497
        }
        continue;
      }
498
      *level = i + 2;
499
      if (counts) ++counts->coeff_base[txs_ctx][plane_type][i][ctx][0];
500 501 502 503 504 505 506

      // update the eob flag for coefficients with magnitude above 1.
      update_eob = AOMMAX(update_eob, c);
    }
  }

  for (c = update_eob; c >= 0; --c) {
507 508
    uint8_t *const level = &levels[scan[c]];
    int8_t *const sign = &signs[scan[c]];
509 510 511
    int idx;
    int ctx;

512
    if (*level <= NUM_BASE_LEVELS) continue;
513 514 515

    if (c == 0) {
      int dc_sign_ctx = txb_ctx->dc_sign_ctx;
516
#if LV_MAP_PROB
517
      *sign = av1_read_record_bin(
518
          counts, r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2, ACCT_STR);
519
#else
520
      *sign = aom_read(r, ec_ctx->dc_sign[plane_type][dc_sign_ctx], ACCT_STR);
521
#endif
522
      if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][*sign];
523
    } else {
524
      *sign = av1_read_record_bit(counts, r, ACCT_STR);
525 526
    }

527
    ctx = get_br_ctx(levels, scan[c], bwl, height);
528

529 530 531
#if BR_NODE
    for (idx = 0; idx < BASE_RANGE_SETS; ++idx) {
#if LV_MAP_PROB
532
      if (av1_read_record_bin(
533 534
              counts, r, ec_ctx->coeff_br_cdf[txs_ctx][plane_type][idx][ctx], 2,
              ACCT_STR))
535
#else   // LV_MAP_PROB
536
      if (aom_read(r, ec_ctx->coeff_br[txs_ctx][plane_type][idx][ctx],
537 538 539 540 541 542 543
                   ACCT_STR))
#endif  // LV_MAP_PROB
      {
        int extra_bits = (1 << br_extra_bits[idx]) - 1;
        //        int br_offset = aom_read_literal(r, extra_bits, ACCT_STR);
        int br_offset = 0;
        int tok;
544
        if (counts) ++counts->coeff_br[txs_ctx][plane_type][idx][ctx][1];
545 546
        for (tok = 0; tok < extra_bits; ++tok) {
#if LV_MAP_PROB
547
          if (av1_read_record_bin(
548 549
                  counts, r, ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx], 2,
                  ACCT_STR))
550
#else
551
          if (aom_read(r, ec_ctx->coeff_lps[txs_ctx][plane_type][ctx],
552 553 554 555 556 557 558 559 560 561 562 563 564
                       ACCT_STR))
#endif
          {
            br_offset = tok;
            if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][1];
            break;
          }
          if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][0];
        }
        if (tok == extra_bits) br_offset = extra_bits;

        int br_base = br_index_to_coeff[idx];

565 566
        *level = NUM_BASE_LEVELS + 1 + br_base + br_offset;
        cul_level += *level;
567 568
        break;
      }
569
      if (counts) ++counts->coeff_br[txs_ctx][plane_type][idx][ctx][0];
570 571 572 573
    }

    if (idx < BASE_RANGE_SETS) continue;
#else
574
    for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
575
#if LV_MAP_PROB
576 577 578
      if (av1_read_record_bin(counts, r,
                              ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx],
                              2, ACCT_STR))
579
#else
580
      if (aom_read(r, ec_ctx->coeff_lps[txs_ctx][plane_type][ctx], ACCT_STR))
581 582
#endif
      {
583 584
        *level = idx + 1 + NUM_BASE_LEVELS;
        cul_level += *level;
585

586
        if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][1];
587 588
        break;
      }
589
      if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][0];
590 591
    }
    if (idx < COEFF_BASE_RANGE) continue;
592
#endif
593 594

    // decode 0-th order Golomb code
595 596 597 598
    *level = COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS;
    // Save golomb in tcoeffs because adding it to level may incur overflow
    tcoeffs[scan[c]] = read_golomb(xd, r, counts);
    cul_level += *level + tcoeffs[scan[c]];
599 600
  }

601
  for (c = 0; c < *eob; ++c) {
602 603
    const int16_t dqv = (c == 0) ? dequant[0] : dequant[1];
    const int level = levels[scan[c]];
604
    const int16_t t = ((level + tcoeffs[scan[c]]) * dqv) >> shift;
605
#if CONFIG_SYMBOLRATE
606
    av1_record_coeff(counts, level);
607
#endif
608
    tcoeffs[scan[c]] = signs[scan[c]] ? -t : t;
609 610 611 612 613 614 615 616 617
  }

  cul_level = AOMMIN(63, cul_level);

  // DC value
  set_dc_sign(&cul_level, tcoeffs[0]);

  return cul_level;
}
Angie Chiang's avatar
Angie Chiang committed
618

619
uint8_t av1_read_coeffs_txb_facade(AV1_COMMON *cm, MACROBLOCKD *xd,
Angie Chiang's avatar
Angie Chiang committed
620 621
                                   aom_reader *r, int row, int col, int block,
                                   int plane, tran_low_t *tcoeffs,
Jingning Han's avatar
Jingning Han committed
622 623
                                   TX_SIZE tx_size, int16_t *max_scan_line,
                                   int *eob) {
Angie Chiang's avatar
Angie Chiang committed
624
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
625
  struct macroblockd_plane *pd = &xd->plane[plane];
Angie Chiang's avatar
Angie Chiang committed
626 627

  const BLOCK_SIZE bsize = mbmi->sb_type;
628
#if CONFIG_CHROMA_SUB8X8
Angie Chiang's avatar
Angie Chiang committed
629 630
  const BLOCK_SIZE plane_bsize =
      AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
631
#else
632
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
633
#endif  // CONFIG_CHROMA_SUB8X8
Angie Chiang's avatar
Angie Chiang committed
634 635 636 637

  TXB_CTX txb_ctx;
  get_txb_ctx(plane_bsize, tx_size, plane, pd->above_context + col,
              pd->left_context + row, &txb_ctx);
Jingning Han's avatar
Jingning Han committed
638 639 640
  uint8_t cul_level =
      av1_read_coeffs_txb(cm, xd, r, row, col, block, plane, tcoeffs, &txb_ctx,
                          tx_size, max_scan_line, eob);
641 642
#if CONFIG_ADAPT_SCAN
  PLANE_TYPE plane_type = get_plane_type(plane);
643
  TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, row, col, block, tx_size);
644 645 646 647
  if (xd->counts && *eob > 0)
    av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type, pd->dqcoeff,
                                 *eob);
#endif
648 649
  av1_set_contexts(xd, pd, plane, tx_size, cul_level, col, row);
  return cul_level;
Angie Chiang's avatar
Angie Chiang committed
650
}
651

652
#if !LV_MAP_PROB
653
static void read_txb_probs(FRAME_CONTEXT *fc, const TX_SIZE tx_size,
654 655 656 657
                           aom_reader *r, FRAME_COUNTS *counts) {
#if !CONFIG_SYMBOLRATE
  (void)counts;
#endif
658 659
  int plane, ctx, level;

660
  if (av1_read_record_bit(counts, r, ACCT_STR) == 0) return;
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

  for (ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
    av1_diff_update_prob(r, &fc->txb_skip[tx_size][ctx], ACCT_STR);

  for (plane = 0; plane < PLANE_TYPES; ++plane)
    for (ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx)
      av1_diff_update_prob(r, &fc->nz_map[tx_size][plane][ctx], ACCT_STR);

  for (plane = 0; plane < PLANE_TYPES; ++plane)
    for (ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx)
      av1_diff_update_prob(r, &fc->eob_flag[tx_size][plane][ctx], ACCT_STR);

  for (level = 0; level < NUM_BASE_LEVELS; ++level)
    for (plane = 0; plane < PLANE_TYPES; ++plane)
      for (ctx = 0; ctx < COEFF_BASE_CONTEXTS; ++ctx)
        av1_diff_update_prob(r, &fc->coeff_base[tx_size][plane][level][ctx],
                             ACCT_STR);

  for (plane = 0; plane < PLANE_TYPES; ++plane)
    for (ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
      av1_diff_update_prob(r, &fc->coeff_lps[tx_size][plane][ctx], ACCT_STR);
}

684 685
void av1_read_txb_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r,
                        FRAME_COUNTS *counts) {
686 687 688
  const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  TX_SIZE tx_size;
  int ctx, plane;
689

690 691 692 693 694
  for (plane = 0; plane < PLANE_TYPES; ++plane)
    for (ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
      av1_diff_update_prob(r, &fc->dc_sign[plane][ctx], ACCT_STR);

  for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
695
    read_txb_probs(fc, tx_size, r, counts);
696
}
697
#endif  // !LV_MAP_PROB