decodetxb.c 20 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;
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static INLINE int read_nz_map(aom_reader *r, tran_low_t *tcoeffs, int plane,
                              const int16_t *scan, TX_SIZE tx_size,
                              TX_TYPE tx_type, FRAME_CONTEXT *fc,
                              FRAME_COUNTS *counts) {
  TX_SIZE txs_ctx = get_txsize_context(tx_size);
  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;
74
    int coeff_ctx = get_nz_map_ctx(tcoeffs, c, scan, bwl, height, tx_type);
75 76 77 78
    int eob_ctx = get_eob_ctx(tcoeffs, scan[c], txs_ctx, tx_type);

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

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

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

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

    if (is_nz) {
#if LV_MAP_PROB
101
      int is_eob = av1_read_record_bin(
102 103
          counts, r, fc->eob_flag_cdf[txs_ctx][plane_type][eob_ctx], 2,
          ACCT_STR);
104 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
static INLINE int read_nz_map_vert(aom_reader *r, tran_low_t *tcoeffs,
116 117 118 119
                                   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 150
          int coeff_idx = row * width + col;
          int scan_idx = iscan[coeff_idx];
          int coeff_ctx =
              get_nz_map_ctx(tcoeffs, scan_idx, scan, bwl, height, tx_type);
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 159 160 161 162 163 164
#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];
          tcoeffs[coeff_idx] = is_nz;
          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;
182 183 184 185 186 187 188 189 190 191 192 193 194
          tcoeffs[coeff_idx] = 1;
          eob = AOMMAX(eob, iscan[coeff_idx] + 1);
        }
      }
      eob_ls[col] = AOMMIN(height, row + 1);
    } else {
      eob_ls[col] = 0;
    }
  }
  return eob;
}

static INLINE int read_nz_map_horiz(aom_reader *r, tran_low_t *tcoeffs,
195 196 197 198
                                    int plane, const int16_t *scan,
                                    const int16_t *iscan, TX_SIZE tx_size,
                                    TX_TYPE tx_type, FRAME_CONTEXT *fc,
                                    FRAME_COUNTS *counts) {
199 200 201 202 203 204 205 206 207 208 209 210 211 212
  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
213
    int empty_line = av1_read_record_bin(
214
        counts, r, fc->empty_line_cdf[txs_ctx][plane_type][tx_class][el_ctx], 2,
215 216 217 218 219 220 221 222 223 224 225
        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) {
226 227 228 229
          int coeff_idx = row * width + col;
          int scan_idx = iscan[coeff_idx];
          int coeff_ctx =
              get_nz_map_ctx(tcoeffs, scan_idx, scan, bwl, height, tx_type);
230
#if LV_MAP_PROB
231
          int is_nz = av1_read_record_bin(
232 233
              counts, r, fc->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2,
              ACCT_STR);
234 235 236 237 238 239 240 241 242
#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];
          tcoeffs[coeff_idx] = is_nz;
          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
243
            int is_eob = av1_read_record_bin(
244 245
                counts, r,
                fc->hv_eob_cdf[txs_ctx][plane_type][tx_class][eob_ctx], 2,
246 247 248 249 250 251 252 253 254 255 256
                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 {
257
          int coeff_idx = row * width + col;
258 259 260 261 262 263 264 265 266 267 268 269 270
          tcoeffs[coeff_idx] = 1;
          eob = AOMMAX(eob, iscan[coeff_idx] + 1);
        }
      }
      eob_ls[row] = AOMMIN(width, col + 1);
    } else {
      eob_ls[row] = 0;
    }
  }
  return eob;
}
#endif

271
uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
272 273
                            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
274
                            TX_SIZE tx_size, int16_t *max_scan_line, int *eob) {
275
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
276
  FRAME_COUNTS *counts = xd->counts;
277
  TX_SIZE txs_ctx = get_txsize_context(tx_size);
278
  PLANE_TYPE plane_type = get_plane_type(plane);
279
#if !LV_MAP_PROB
280 281
  aom_prob *nz_map = ec_ctx->nz_map[txs_ctx][plane_type];
  aom_prob *eob_flag = ec_ctx->eob_flag[txs_ctx][plane_type];
282
#endif
283
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
284
  const int seg_eob = tx_size_2d[tx_size];
285
  int c = 0;
286
  int update_eob = -1;
287
  const int16_t *const dequant = xd->plane[plane].seg_dequant[mbmi->segment_id];
288
  const int shift = av1_get_tx_scale(tx_size);
289
  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
290
  const int height = tx_size_high[tx_size];
291 292 293
  int cul_level = 0;
  memset(tcoeffs, 0, sizeof(*tcoeffs) * seg_eob);

294
#if LV_MAP_PROB
295
  int all_zero = av1_read_record_bin(
296 297
      counts, r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2,
      ACCT_STR);
298
#else
Angie Chiang's avatar
Angie Chiang committed
299
  int all_zero =
300
      aom_read(r, ec_ctx->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx], ACCT_STR);
301
#endif
Angie Chiang's avatar
Angie Chiang committed
302
  if (xd->counts)
303
    ++xd->counts->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx][all_zero];
Angie Chiang's avatar
Angie Chiang committed
304

305
  *eob = 0;
Angie Chiang's avatar
Angie Chiang committed
306 307
  if (all_zero) {
    *max_scan_line = 0;
308 309 310
#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
311 312 313
    return 0;
  }

Jingning Han's avatar
Jingning Han committed
314 315
  (void)blk_row;
  (void)blk_col;
Angie Chiang's avatar
Angie Chiang committed
316
#if CONFIG_TXK_SEL
317 318
  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
319
#endif
320 321
  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
322
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
323
  const int16_t *scan = scan_order->scan;
Angie Chiang's avatar
Angie Chiang committed
324

325 326 327 328 329 330 331
#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 =
        read_nz_map(r, tcoeffs, plane, scan, tx_size, tx_type, ec_ctx, counts);
  } else {
332
#if LV_MAP_PROB
333
    const int eob_mode = av1_read_record_bin(
334 335
        counts, r, ec_ctx->eob_mode_cdf[txs_ctx][plane_type][tx_class], 2,
        ACCT_STR);
336
#else
337 338
    const int eob_mode =
        aom_read(r, ec_ctx->eob_mode[txs_ctx][plane_type][tx_class], ACCT_STR);
339
#endif
340 341 342 343
    if (counts) ++counts->eob_mode[txs_ctx][plane_type][tx_class][eob_mode];
    if (eob_mode == 0) {
      *eob = read_nz_map(r, tcoeffs, plane, scan, tx_size, tx_type, ec_ctx,
                         counts);
344
    } else {
345 346
      assert(tx_class == TX_CLASS_VERT || tx_class == TX_CLASS_HORIZ);
      if (tx_class == TX_CLASS_VERT)
347 348
        *eob = read_nz_map_vert(r, tcoeffs, plane, scan, iscan, tx_size,
                                tx_type, ec_ctx, counts);
349
      else
350 351
        *eob = read_nz_map_horiz(r, tcoeffs, plane, scan, iscan, tx_size,
                                 tx_type, ec_ctx, counts);
352
    }
353
  }
354
#else
355
  *eob = read_nz_map(r, tcoeffs, plane, scan, tx_size, tx_type, ec_ctx, counts);
356
#endif
357
  *max_scan_line = *eob;
358 359 360

  int i;
  for (i = 0; i < NUM_BASE_LEVELS; ++i) {
361
#if !LV_MAP_PROB
362
    aom_prob *coeff_base = ec_ctx->coeff_base[txs_ctx][plane_type][i];
363
#endif
364
    update_eob = 0;
365
    for (c = *eob - 1; c >= 0; --c) {
366 367 368 369 370 371
      tran_low_t *v = &tcoeffs[scan[c]];
      int sign;
      int ctx;

      if (*v <= i) continue;

372
      ctx = get_base_ctx(tcoeffs, scan[c], bwl, height, i + 1);
373

374
#if LV_MAP_PROB
375
      if (av1_read_record_bin(
376 377
              counts, r, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][i][ctx], 2,
              ACCT_STR))
378 379 380 381
#else
      if (aom_read(r, coeff_base[ctx], ACCT_STR))
#endif
      {
382 383 384
        *v = i + 1;
        cul_level += i + 1;

385
        if (counts) ++counts->coeff_base[txs_ctx][plane_type][i][ctx][1];
386 387 388

        if (c == 0) {
          int dc_sign_ctx = txb_ctx->dc_sign_ctx;
389
#if LV_MAP_PROB
390
          sign = av1_read_record_bin(
391 392
              counts, r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2,
              ACCT_STR);
393
#else
394
          sign =
395
              aom_read(r, ec_ctx->dc_sign[plane_type][dc_sign_ctx], ACCT_STR);
396
#endif
397 398
          if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][sign];
        } else {
399
          sign = av1_read_record_bit(counts, r, ACCT_STR);
400 401 402 403 404
        }
        if (sign) *v = -(*v);
        continue;
      }
      *v = i + 2;
405
      if (counts) ++counts->coeff_base[txs_ctx][plane_type][i][ctx][0];
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

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

  for (c = update_eob; c >= 0; --c) {
    tran_low_t *v = &tcoeffs[scan[c]];
    int sign;
    int idx;
    int ctx;

    if (*v <= NUM_BASE_LEVELS) continue;

    if (c == 0) {
      int dc_sign_ctx = txb_ctx->dc_sign_ctx;
422
#if LV_MAP_PROB
423
      sign = av1_read_record_bin(
424
          counts, r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2, ACCT_STR);
425
#else
426
      sign = aom_read(r, ec_ctx->dc_sign[plane_type][dc_sign_ctx], ACCT_STR);
427
#endif
428 429
      if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][sign];
    } else {
430
      sign = av1_read_record_bit(counts, r, ACCT_STR);
431 432
    }

433
    ctx = get_br_ctx(tcoeffs, scan[c], bwl, height);
434

435 436 437
#if BR_NODE
    for (idx = 0; idx < BASE_RANGE_SETS; ++idx) {
#if LV_MAP_PROB
438
      if (av1_read_record_bin(
439 440
              counts, r, ec_ctx->coeff_br_cdf[txs_ctx][plane_type][idx][ctx], 2,
              ACCT_STR))
441
#else   // LV_MAP_PROB
442
      if (aom_read(r, ec_ctx->coeff_br[txs_ctx][plane_type][idx][ctx],
443 444 445 446 447 448 449
                   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;
450
        if (counts) ++counts->coeff_br[txs_ctx][plane_type][idx][ctx][1];
451 452
        for (tok = 0; tok < extra_bits; ++tok) {
#if LV_MAP_PROB
453
          if (av1_read_record_bin(
454 455
                  counts, r, ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx], 2,
                  ACCT_STR))
456
#else
457
          if (aom_read(r, ec_ctx->coeff_lps[txs_ctx][plane_type][ctx],
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
                       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];

        *v = NUM_BASE_LEVELS + 1 + br_base + br_offset;
        cul_level += *v;
        if (sign) *v = -(*v);
        break;
      }
476
      if (counts) ++counts->coeff_br[txs_ctx][plane_type][idx][ctx][0];
477 478 479 480
    }

    if (idx < BASE_RANGE_SETS) continue;
#else
481
    for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
482
#if LV_MAP_PROB
483 484 485
      if (av1_read_record_bin(counts, r,
                              ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx],
                              2, ACCT_STR))
486
#else
487
      if (aom_read(r, ec_ctx->coeff_lps[txs_ctx][plane_type][ctx], ACCT_STR))
488 489
#endif
      {
490 491 492 493
        *v = (idx + 1 + NUM_BASE_LEVELS);
        if (sign) *v = -(*v);
        cul_level += abs(*v);

494
        if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][1];
495 496
        break;
      }
497
      if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][0];
498 499
    }
    if (idx < COEFF_BASE_RANGE) continue;
500
#endif
501 502

    // decode 0-th order Golomb code
503
    *v = read_golomb(xd, r, counts) + COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS;
504 505 506 507
    if (sign) *v = -(*v);
    cul_level += abs(*v);
  }

508
  for (c = 0; c < *eob; ++c) {
509 510
    int16_t dqv = (c == 0) ? dequant[0] : dequant[1];
    tran_low_t *v = &tcoeffs[scan[c]];
511
#if CONFIG_SYMBOLRATE
512
    av1_record_coeff(counts, abs(*v));
513
#endif
514 515 516 517 518 519 520 521 522 523 524 525
    int sign = (*v) < 0;
    *v = (abs(*v) * dqv) >> shift;
    if (sign) *v = -(*v);
  }

  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
526

527
uint8_t av1_read_coeffs_txb_facade(AV1_COMMON *cm, MACROBLOCKD *xd,
Angie Chiang's avatar
Angie Chiang committed
528 529
                                   aom_reader *r, int row, int col, int block,
                                   int plane, tran_low_t *tcoeffs,
Jingning Han's avatar
Jingning Han committed
530 531
                                   TX_SIZE tx_size, int16_t *max_scan_line,
                                   int *eob) {
Angie Chiang's avatar
Angie Chiang committed
532
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
533
  struct macroblockd_plane *pd = &xd->plane[plane];
Angie Chiang's avatar
Angie Chiang committed
534 535

  const BLOCK_SIZE bsize = mbmi->sb_type;
536
#if CONFIG_CHROMA_SUB8X8
Angie Chiang's avatar
Angie Chiang committed
537 538
  const BLOCK_SIZE plane_bsize =
      AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
539 540
#elif CONFIG_CB4X4
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Angie Chiang's avatar
Angie Chiang committed
541 542 543 544 545 546 547 548
#else   // CONFIG_CB4X4
  const BLOCK_SIZE plane_bsize =
      get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
#endif  // CONFIG_CB4X4

  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
549 550 551
  uint8_t cul_level =
      av1_read_coeffs_txb(cm, xd, r, row, col, block, plane, tcoeffs, &txb_ctx,
                          tx_size, max_scan_line, eob);
552 553
#if CONFIG_ADAPT_SCAN
  PLANE_TYPE plane_type = get_plane_type(plane);
554
  TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, row, col, block, tx_size);
555 556 557 558
  if (xd->counts && *eob > 0)
    av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type, pd->dqcoeff,
                                 *eob);
#endif
559 560
  av1_set_contexts(xd, pd, plane, tx_size, cul_level, col, row);
  return cul_level;
Angie Chiang's avatar
Angie Chiang committed
561
}
562

563
#if !LV_MAP_PROB
564
static void read_txb_probs(FRAME_CONTEXT *fc, const TX_SIZE tx_size,
565 566 567 568
                           aom_reader *r, FRAME_COUNTS *counts) {
#if !CONFIG_SYMBOLRATE
  (void)counts;
#endif
569 570
  int plane, ctx, level;

571
  if (av1_read_record_bit(counts, r, ACCT_STR) == 0) return;
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

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

595 596
void av1_read_txb_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r,
                        FRAME_COUNTS *counts) {
597 598 599
  const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  TX_SIZE tx_size;
  int ctx, plane;
600

601 602 603 604 605
  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)
606
    read_txb_probs(fc, tx_size, r, counts);
607
}
608
#endif  // !LV_MAP_PROB