decodetxb.c 10.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 19 20

#define ACCT_STR __func__

21
static int read_golomb(MACROBLOCKD *xd, aom_reader *r) {
22 23 24 25 26 27 28
  int x = 1;
  int length = 0;
  int i = 0;

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

  for (i = 0; i < length - 1; ++i) {
    x <<= 1;
    x += aom_read_bit(r, ACCT_STR);
  }

  return x - 1;
}

uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
45 46
                            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
47
                            TX_SIZE tx_size, int16_t *max_scan_line, int *eob) {
48
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
49
  FRAME_COUNTS *counts = xd->counts;
50
  TX_SIZE txs_ctx = get_txsize_context(tx_size);
51
  PLANE_TYPE plane_type = get_plane_type(plane);
52
#if !LV_MAP_PROB
53 54
  aom_prob *nz_map = ec_ctx->nz_map[txs_ctx][plane_type];
  aom_prob *eob_flag = ec_ctx->eob_flag[txs_ctx][plane_type];
55
#endif
56
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
57
  const int seg_eob = tx_size_2d[tx_size];
58
  int c = 0;
59
  int update_eob = -1;
60
  const int16_t *const dequant = xd->plane[plane].seg_dequant[mbmi->segment_id];
61
  const int shift = av1_get_tx_scale(tx_size);
62
  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
63
  const int height = tx_size_high[tx_size];
64 65 66
  int cul_level = 0;
  unsigned int(*nz_map_count)[SIG_COEF_CONTEXTS][2];

67
  nz_map_count = (counts) ? &counts->nz_map[txs_ctx][plane_type] : NULL;
68 69 70

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

71 72
#if LV_MAP_PROB
  int all_zero = aom_read_symbol(
73
      r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2, ACCT_STR);
74
#else
Angie Chiang's avatar
Angie Chiang committed
75
  int all_zero =
76
      aom_read(r, ec_ctx->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx], ACCT_STR);
77
#endif
Angie Chiang's avatar
Angie Chiang committed
78
  if (xd->counts)
79
    ++xd->counts->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx][all_zero];
Angie Chiang's avatar
Angie Chiang committed
80

81
  *eob = 0;
Angie Chiang's avatar
Angie Chiang committed
82 83
  if (all_zero) {
    *max_scan_line = 0;
84 85 86
#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
87 88 89
    return 0;
  }

Jingning Han's avatar
Jingning Han committed
90 91
  (void)blk_row;
  (void)blk_col;
Angie Chiang's avatar
Angie Chiang committed
92
#if CONFIG_TXK_SEL
93 94
  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
95
#endif
96 97
  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
98
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
99
  const int16_t *scan = scan_order->scan;
Angie Chiang's avatar
Angie Chiang committed
100

101 102
  for (c = 0; c < seg_eob; ++c) {
    int is_nz;
Angie Chiang's avatar
Angie Chiang committed
103
    int coeff_ctx = get_nz_map_ctx(tcoeffs, scan[c], bwl, height, tx_type);
104
    int eob_ctx = get_eob_ctx(tcoeffs, scan[c], txs_ctx);
105

106 107 108
    if (c < seg_eob - 1) {
#if LV_MAP_PROB
      is_nz = aom_read_symbol(
109
          r, ec_ctx->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2, ACCT_STR);
110
#else
111
      is_nz = aom_read(r, nz_map[coeff_ctx], ACCT_STR);
112 113
#endif
    } else {
114
      is_nz = 1;
115
    }
116 117 118 119 120 121 122 123 124 125 126 127

    // 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) {
128 129
#if LV_MAP_PROB
      int is_eob = aom_read_symbol(
130
          r, ec_ctx->eob_flag_cdf[txs_ctx][plane_type][eob_ctx], 2, ACCT_STR);
131
#else
132
      int is_eob = aom_read(r, eob_flag[eob_ctx], ACCT_STR);
133
#endif
134
      if (counts) ++counts->eob_flag[txs_ctx][plane_type][eob_ctx][is_eob];
135 136 137 138
      if (is_eob) break;
    }
  }

139 140
  *eob = AOMMIN(seg_eob, c + 1);
  *max_scan_line = *eob;
141 142 143

  int i;
  for (i = 0; i < NUM_BASE_LEVELS; ++i) {
144
#if !LV_MAP_PROB
145
    aom_prob *coeff_base = ec_ctx->coeff_base[txs_ctx][plane_type][i];
146
#endif
147
    update_eob = 0;
148
    for (c = *eob - 1; c >= 0; --c) {
149 150 151 152 153 154
      tran_low_t *v = &tcoeffs[scan[c]];
      int sign;
      int ctx;

      if (*v <= i) continue;

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

157 158
#if LV_MAP_PROB
      if (aom_read_symbol(r,
159
                          ec_ctx->coeff_base_cdf[txs_ctx][plane_type][i][ctx],
160 161 162 163 164
                          2, ACCT_STR))
#else
      if (aom_read(r, coeff_base[ctx], ACCT_STR))
#endif
      {
165 166 167
        *v = i + 1;
        cul_level += i + 1;

168
        if (counts) ++counts->coeff_base[txs_ctx][plane_type][i][ctx][1];
169 170 171

        if (c == 0) {
          int dc_sign_ctx = txb_ctx->dc_sign_ctx;
172 173
#if LV_MAP_PROB
          sign = aom_read_symbol(
174
              r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2, ACCT_STR);
175
#else
176
          sign =
177
              aom_read(r, ec_ctx->dc_sign[plane_type][dc_sign_ctx], ACCT_STR);
178
#endif
179 180 181 182 183 184 185 186
          if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][sign];
        } else {
          sign = aom_read_bit(r, ACCT_STR);
        }
        if (sign) *v = -(*v);
        continue;
      }
      *v = i + 2;
187
      if (counts) ++counts->coeff_base[txs_ctx][plane_type][i][ctx][0];
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

      // 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;
204
#if LV_MAP_PROB
205
      sign = aom_read_symbol(r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2,
206 207
                             ACCT_STR);
#else
208
      sign = aom_read(r, ec_ctx->dc_sign[plane_type][dc_sign_ctx], ACCT_STR);
209
#endif
210 211 212 213 214
      if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][sign];
    } else {
      sign = aom_read_bit(r, ACCT_STR);
    }

215
    ctx = get_br_ctx(tcoeffs, scan[c], bwl, height);
216

217
    if (ec_ctx->coeff_lps[txs_ctx][plane_type][ctx] == 0) exit(0);
218 219

    for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
220
#if LV_MAP_PROB
221
      if (aom_read_symbol(r, ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx], 2,
222 223
                          ACCT_STR))
#else
224
      if (aom_read(r, ec_ctx->coeff_lps[txs_ctx][plane_type][ctx], ACCT_STR))
225 226
#endif
      {
227 228 229 230
        *v = (idx + 1 + NUM_BASE_LEVELS);
        if (sign) *v = -(*v);
        cul_level += abs(*v);

231
        if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][1];
232 233
        break;
      }
234
      if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][0];
235 236 237 238
    }
    if (idx < COEFF_BASE_RANGE) continue;

    // decode 0-th order Golomb code
239
    *v = read_golomb(xd, r) + COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS;
240 241 242 243
    if (sign) *v = -(*v);
    cul_level += abs(*v);
  }

244
  for (c = 0; c < *eob; ++c) {
245 246 247 248 249 250 251 252 253 254 255 256 257 258
    int16_t dqv = (c == 0) ? dequant[0] : dequant[1];
    tran_low_t *v = &tcoeffs[scan[c]];
    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
259

260
uint8_t av1_read_coeffs_txb_facade(AV1_COMMON *cm, MACROBLOCKD *xd,
Angie Chiang's avatar
Angie Chiang committed
261 262
                                   aom_reader *r, int row, int col, int block,
                                   int plane, tran_low_t *tcoeffs,
Jingning Han's avatar
Jingning Han committed
263 264
                                   TX_SIZE tx_size, int16_t *max_scan_line,
                                   int *eob) {
Angie Chiang's avatar
Angie Chiang committed
265
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
266
  struct macroblockd_plane *pd = &xd->plane[plane];
Angie Chiang's avatar
Angie Chiang committed
267 268

  const BLOCK_SIZE bsize = mbmi->sb_type;
269
#if CONFIG_CHROMA_SUB8X8
Angie Chiang's avatar
Angie Chiang committed
270 271
  const BLOCK_SIZE plane_bsize =
      AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
272 273
#elif CONFIG_CB4X4
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Angie Chiang's avatar
Angie Chiang committed
274 275 276 277 278 279 280 281
#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
282 283 284
  uint8_t cul_level =
      av1_read_coeffs_txb(cm, xd, r, row, col, block, plane, tcoeffs, &txb_ctx,
                          tx_size, max_scan_line, eob);
285 286
#if CONFIG_ADAPT_SCAN
  PLANE_TYPE plane_type = get_plane_type(plane);
287
  TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, row, col, block, tx_size);
288 289 290 291
  if (xd->counts && *eob > 0)
    av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type, pd->dqcoeff,
                                 *eob);
#endif
292 293
  av1_set_contexts(xd, pd, plane, tx_size, cul_level, col, row);
  return cul_level;
Angie Chiang's avatar
Angie Chiang committed
294
}
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

static void read_txb_probs(FRAME_CONTEXT *fc, const TX_SIZE tx_size,
                           aom_reader *r) {
  int plane, ctx, level;

  if (aom_read_bit(r, ACCT_STR) == 0) return;

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

void av1_read_txb_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
  const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  TX_SIZE tx_size;
  int ctx, plane;
328 329 330 331 332

#if LV_MAP_PROB
  return;
#endif

333 334 335 336 337 338 339
  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)
    read_txb_probs(fc, tx_size, r);
}