encodetxb.c 78.2 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/blockd.h"
14
#include "av1/common/idct.h"
Angie Chiang's avatar
Angie Chiang committed
15
#include "av1/common/pred_common.h"
16 17
#include "av1/encoder/bitstream.h"
#include "av1/encoder/encodeframe.h"
18
#include "av1/encoder/cost.h"
19
#include "av1/encoder/encodetxb.h"
Angie Chiang's avatar
Angie Chiang committed
20
#include "av1/encoder/rdopt.h"
21
#include "av1/encoder/subexp.h"
Angie Chiang's avatar
Angie Chiang committed
22
#include "av1/encoder/tokenize.h"
23

Angie Chiang's avatar
Angie Chiang committed
24 25
#define TEST_OPTIMIZE_TXB 0

Dake He's avatar
Dake He committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
typedef struct LevelDownStats {
  int update;
  tran_low_t low_qc;
  tran_low_t low_dqc;
  int64_t dist0;
  int rate;
  int rate_low;
  int64_t dist;
  int64_t dist_low;
  int64_t rd;
  int64_t rd_low;
  int nz_rate;  // for eob
  int64_t rd_diff;
  int cost_diff;
  int64_t dist_diff;
  int new_eob;
} LevelDownStats;

44
void av1_alloc_txb_buf(AV1_COMP *cpi) {
45
#if 0
46 47 48 49 50 51 52 53 54 55 56 57
  AV1_COMMON *cm = &cpi->common;
  int mi_block_size = 1 << MI_SIZE_LOG2;
  // TODO(angiebird): Make sure cm->subsampling_x/y is set correctly, and then
  // use precise buffer size according to cm->subsampling_x/y
  int pixel_stride = mi_block_size * cm->mi_cols;
  int pixel_height = mi_block_size * cm->mi_rows;
  int i;
  for (i = 0; i < MAX_MB_PLANE; ++i) {
    CHECK_MEM_ERROR(
        cm, cpi->tcoeff_buf[i],
        aom_malloc(sizeof(*cpi->tcoeff_buf[i]) * pixel_stride * pixel_height));
  }
58
#else
59 60 61 62
  AV1_COMMON *cm = &cpi->common;
  int size = ((cm->mi_rows >> MAX_MIB_SIZE_LOG2) + 1) *
             ((cm->mi_cols >> MAX_MIB_SIZE_LOG2) + 1);

63
  av1_free_txb_buf(cpi);
64 65 66
  // TODO(jingning): This should be further reduced.
  CHECK_MEM_ERROR(cm, cpi->coeff_buffer_base,
                  aom_malloc(sizeof(*cpi->coeff_buffer_base) * size));
67
#endif
68 69 70
}

void av1_free_txb_buf(AV1_COMP *cpi) {
71
#if 0
72 73 74 75
  int i;
  for (i = 0; i < MAX_MB_PLANE; ++i) {
    aom_free(cpi->tcoeff_buf[i]);
  }
76
#else
77
  aom_free(cpi->coeff_buffer_base);
78
#endif
79 80
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
void av1_set_coeff_buffer(const AV1_COMP *const cpi, MACROBLOCK *const x,
                          int mi_row, int mi_col) {
  int stride = (cpi->common.mi_cols >> MAX_MIB_SIZE_LOG2) + 1;
  int offset =
      (mi_row >> MAX_MIB_SIZE_LOG2) * stride + (mi_col >> MAX_MIB_SIZE_LOG2);
  CB_COEFF_BUFFER *coeff_buf = &cpi->coeff_buffer_base[offset];
  const int txb_offset = x->cb_offset / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
  for (int plane = 0; plane < MAX_MB_PLANE; ++plane) {
    x->mbmi_ext->tcoeff[plane] = coeff_buf->tcoeff[plane] + x->cb_offset;
    x->mbmi_ext->eobs[plane] = coeff_buf->eobs[plane] + txb_offset;
    x->mbmi_ext->txb_skip_ctx[plane] =
        coeff_buf->txb_skip_ctx[plane] + txb_offset;
    x->mbmi_ext->dc_sign_ctx[plane] =
        coeff_buf->dc_sign_ctx[plane] + txb_offset;
  }
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static void write_golomb(aom_writer *w, int level) {
  int x = level + 1;
  int i = x;
  int length = 0;

  while (i) {
    i >>= 1;
    ++length;
  }
  assert(length > 0);

  for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0);

  for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01);
}

Dake He's avatar
Dake He committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static INLINE tran_low_t get_lower_coeff(tran_low_t qc) {
  if (qc == 0) {
    return 0;
  }
  return qc > 0 ? qc - 1 : qc + 1;
}

static INLINE tran_low_t qcoeff_to_dqcoeff(tran_low_t qc, int dqv, int shift) {
  int sgn = qc < 0 ? -1 : 1;
  return sgn * ((abs(qc) * dqv) >> shift);
}

static INLINE int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff,
                                     int shift) {
  const int64_t diff = (tcoeff - dqcoeff) * (1 << shift);
  const int64_t error = diff * diff;
  return error;
}

Jingning Han's avatar
Jingning Han committed
133 134
void av1_update_eob_context(int eob, int seg_eob, TX_SIZE tx_size,
                            TX_TYPE tx_type, PLANE_TYPE plane,
135 136
                            FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts,
                            uint8_t allow_update_cdf) {
Dake He's avatar
Dake He committed
137 138 139 140
  int16_t eob_extra;
  int16_t eob_pt = get_eob_pos_token(eob, &eob_extra);
  int16_t dummy;
  int16_t max_eob_pt = get_eob_pos_token(seg_eob, &dummy);
Jingning Han's avatar
Jingning Han committed
141
  TX_SIZE txs_ctx = get_txsize_context(tx_size);
Dake He's avatar
Dake He committed
142 143

  for (int i = 1; i < max_eob_pt; i++) {
Jingning Han's avatar
Jingning Han committed
144 145
    int eob_pos_ctx = av1_get_eob_pos_ctx(tx_type, i);
    counts->eob_flag[txs_ctx][plane][eob_pos_ctx][eob_pt == i]++;
146 147 148
    if (allow_update_cdf)
      update_cdf(ec_ctx->eob_flag_cdf[txs_ctx][plane][eob_pos_ctx], eob_pt == i,
                 2);
Dake He's avatar
Dake He committed
149 150 151 152
    if (eob_pt == i) {
      break;
    }
  }
Jingning Han's avatar
Jingning Han committed
153

Angie Chiang's avatar
Angie Chiang committed
154 155 156
  if (k_eob_offset_bits[eob_pt] > 0) {
    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Jingning Han's avatar
Jingning Han committed
157
    counts->eob_extra[txs_ctx][plane][eob_pt][bit]++;
158 159
    if (allow_update_cdf)
      update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_pt], bit, 2);
Angie Chiang's avatar
Angie Chiang committed
160
  }
Dake He's avatar
Dake He committed
161 162 163
}

static int get_eob_cost(int eob, int seg_eob,
Jingning Han's avatar
Jingning Han committed
164
                        const LV_MAP_COEFF_COST *txb_costs, TX_TYPE tx_type) {
Dake He's avatar
Dake He committed
165 166 167 168 169 170 171 172
  int16_t eob_extra;
  int16_t eob_pt = get_eob_pos_token(eob, &eob_extra);
  int16_t dummy;
  int16_t max_eob_pt = get_eob_pos_token(seg_eob, &dummy);
  int eob_cost = 0;

  // printf("Enc: [%d, %d], (%d, %d) ", seg_eob, eob, eob_pt, eob_extra);
  for (int i = 1; i < max_eob_pt; i++) {
Jingning Han's avatar
Jingning Han committed
173
    int eob_pos_ctx = av1_get_eob_pos_ctx(tx_type, i);
Dake He's avatar
Dake He committed
174 175 176 177 178 179
    eob_cost += txb_costs->eob_cost[eob_pos_ctx][eob_pt == i];
    if (eob_pt == i) {
      break;
    }
  }
  if (k_eob_offset_bits[eob_pt] > 0) {
Angie Chiang's avatar
Angie Chiang committed
180 181 182 183 184 185
    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
    eob_cost += txb_costs->eob_extra_cost[eob_pt][bit];
    for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
      eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
      bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Dake He's avatar
Dake He committed
186 187 188 189 190 191
      eob_cost += av1_cost_bit(128, bit);
    }
  }
  return eob_cost;
}

192 193 194
static int get_coeff_cost(const tran_low_t qc, const int scan_idx,
                          const TxbInfo *const txb_info,
                          const LV_MAP_COEFF_COST *const txb_costs);
Dake He's avatar
Dake He committed
195 196 197 198 199 200 201

void get_dist_cost_stats(LevelDownStats *stats, int scan_idx,
                         const LV_MAP_COEFF_COST *txb_costs,
                         TxbInfo *txb_info) {
  const int16_t *scan = txb_info->scan_order->scan;
  const int coeff_idx = scan[scan_idx];
  const tran_low_t qc = txb_info->qcoeff[coeff_idx];
202
  const uint8_t *const levels = txb_info->levels;
Dake He's avatar
Dake He committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  stats->new_eob = -1;
  stats->update = 0;

  const tran_low_t tqc = txb_info->tcoeff[coeff_idx];
  const int dqv = txb_info->dequant[coeff_idx != 0];

  const tran_low_t dqc = qcoeff_to_dqcoeff(qc, dqv, txb_info->shift);
  const int64_t dqc_dist = get_coeff_dist(tqc, dqc, txb_info->shift);
  const int qc_cost = get_coeff_cost(qc, scan_idx, txb_info, txb_costs);

  // distortion difference when coefficient is quantized to 0
  const tran_low_t dqc0 = qcoeff_to_dqcoeff(0, dqv, txb_info->shift);
  stats->dist0 = get_coeff_dist(tqc, dqc0, txb_info->shift);
  stats->dist = dqc_dist - stats->dist0;
  stats->rate = qc_cost;

  if (qc == 0) {
    return;
  }
  stats->rd = RDCOST(txb_info->rdmult, stats->rate, stats->dist);

  stats->low_qc = get_lower_coeff(qc);
  stats->low_dqc = qcoeff_to_dqcoeff(stats->low_qc, dqv, txb_info->shift);
  const int64_t low_dqc_dist =
      get_coeff_dist(tqc, stats->low_dqc, txb_info->shift);
  const int low_qc_cost =
      get_coeff_cost(stats->low_qc, scan_idx, txb_info, txb_costs);

  stats->dist_low = low_dqc_dist - stats->dist0;
  stats->rate_low = low_qc_cost;
  stats->rd_low = RDCOST(txb_info->rdmult, stats->rate_low, stats->dist_low);

235 236
  int coeff_ctx = get_nz_map_ctx(levels, scan_idx, scan, txb_info->bwl,
                                 txb_info->height, txb_info->tx_type, 1);
Dake He's avatar
Dake He committed
237 238 239 240 241 242 243 244

  if ((stats->rd_low < stats->rd) && (stats->low_qc == 0)) {
    stats->nz_rate = txb_costs->nz_map_cost[coeff_ctx][0];
  } else {
    stats->nz_rate = txb_costs->nz_map_cost[coeff_ctx][1];
  }
}

245 246
static INLINE void update_qcoeff(const int coeff_idx, const tran_low_t qc,
                                 const TxbInfo *const txb_info) {
Dake He's avatar
Dake He committed
247
  txb_info->qcoeff[coeff_idx] = qc;
248 249 250 251 252 253
  txb_info->levels[coeff_idx] = (uint8_t)clamp(abs(qc), 0, UINT8_MAX);
}

static INLINE void update_coeff(const int coeff_idx, const tran_low_t qc,
                                const TxbInfo *const txb_info) {
  update_qcoeff(coeff_idx, qc, txb_info);
Dake He's avatar
Dake He committed
254 255 256 257
  const int dqv = txb_info->dequant[coeff_idx != 0];
  txb_info->dqcoeff[coeff_idx] = qcoeff_to_dqcoeff(qc, dqv, txb_info->shift);
}

258 259 260 261 262 263 264
static INLINE void av1_txb_init_levels(const tran_low_t *const coeff,
                                       const int size, uint8_t *const levels) {
  for (int i = 0; i < size; i++) {
    levels[i] = (uint8_t)clamp(abs(coeff[i]), 0, UINT8_MAX);
  }
}

265
void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
266 267 268
                          aom_writer *w, int blk_row, int blk_col, int block,
                          int plane, TX_SIZE tx_size, const tran_low_t *tcoeff,
                          uint16_t eob, TXB_CTX *txb_ctx) {
269
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
270
  const PLANE_TYPE plane_type = get_plane_type(plane);
271
  const TX_SIZE txs_ctx = get_txsize_context(tx_size);
272 273
  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
274
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
275
  const int16_t *scan = scan_order->scan;
276
  const int seg_eob = tx_size_2d[tx_size];
277 278
  int c;
  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
279
  const int height = tx_size_high[tx_size];
280
  uint16_t update_eob = 0;
281
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
282
  uint8_t levels[MAX_TX_SQUARE];
283

Jingning Han's avatar
Jingning Han committed
284 285 286
  (void)blk_row;
  (void)blk_col;

287 288
  aom_write_bin(w, eob == 0,
                ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2);
289 290

  if (eob == 0) return;
291

292
  av1_txb_init_levels(tcoeff, seg_eob, levels);
293

Angie Chiang's avatar
Angie Chiang committed
294
#if CONFIG_TXK_SEL
295 296
  av1_write_tx_type(cm, xd, blk_row, blk_col, block, plane,
                    get_min_tx_size(tx_size), w);
Angie Chiang's avatar
Angie Chiang committed
297
#endif
298

Dake He's avatar
Dake He committed
299 300 301 302 303 304 305
  int16_t eob_extra;
  int16_t eob_pt = get_eob_pos_token(eob, &eob_extra);
  int16_t dummy;
  int16_t max_eob_pt = get_eob_pos_token(seg_eob, &dummy);

  // printf("Enc: [%d, %d], (%d, %d) ", seg_eob, eob, eob_pt, eob_extra);
  for (int i = 1; i < max_eob_pt; i++) {
Jingning Han's avatar
Jingning Han committed
306
    int eob_pos_ctx = av1_get_eob_pos_ctx(tx_type, i);
Dake He's avatar
Dake He committed
307 308 309 310 311 312 313 314 315 316 317

    aom_write_bin(w, eob_pt == i,
                  ec_ctx->eob_flag_cdf[txs_ctx][plane_type][eob_pos_ctx], 2);
    // aom_write_symbol(w, eob_pt == i,
    // ec_ctx->eob_flag_cdf[AOMMIN(txs_ctx,3)][plane_type][eob_pos_ctx], 2);
    if (eob_pt == i) {
      break;
    }
  }

  if (k_eob_offset_bits[eob_pt] > 0) {
Angie Chiang's avatar
Angie Chiang committed
318 319 320 321 322 323 324
    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
    aom_write_bin(w, bit, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_pt],
                  2);
    for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
      eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
      bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Dake He's avatar
Dake He committed
325 326 327 328
      aom_write_bit(w, bit);
      // printf("%d ", bit);
    }
  }
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

#if USE_CAUSAL_BASE_CTX
  int coeff_ctx = 0;
  for (int i = 0; i < eob; ++i) {
    c = eob - 1 - i;
    coeff_ctx = get_nz_map_ctx(tcoeff, c, scan, bwl, height, tx_type, 0);

    tran_low_t v = tcoeff[scan[c]];
    int is_nz = (v != 0);

    if (c < eob - 1) {
      aom_write_bin(w, is_nz,
                    ec_ctx->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2);
    }
    if (is_nz) {
      const int level = abs(v);
      int k;
      for (k = 0; k < NUM_BASE_LEVELS; ++k) {
        int is_k = (level > (k + 1));
        int ctx = coeff_ctx;
        aom_write_bin(w, is_k,
                      ec_ctx->coeff_base_cdf[txs_ctx][plane_type][k][ctx], 2);
        if (is_k == 0) break;
      }
    }
  }
  update_eob = eob - 1;
#else
Dake He's avatar
Dake He committed
357 358
  for (int i = 1; i < eob; ++i) {
    c = eob - 1 - i;
359
    int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, 1);
Dake He's avatar
Dake He committed
360 361 362 363 364 365 366 367 368

    tran_low_t v = tcoeff[scan[c]];
    int is_nz = (v != 0);

    aom_write_bin(w, is_nz, ec_ctx->nz_map_cdf[txs_ctx][plane_type][coeff_ctx],
                  2);
  }

  for (int i = 0; i < NUM_BASE_LEVELS; ++i) {
369 370
    update_eob = 0;
    for (c = eob - 1; c >= 0; --c) {
371
      const tran_low_t level = abs(tcoeff[scan[c]]);
372 373 374 375
      int ctx;

      if (level <= i) continue;

376
      ctx = get_base_ctx(levels, scan[c], bwl, height, i + 1);
377 378

      if (level == i + 1) {
379 380
        aom_write_bin(w, 1, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][i][ctx],
                      2);
381 382
        continue;
      }
383

384 385
      aom_write_bin(w, 0, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][i][ctx],
                    2);
386 387 388
      update_eob = AOMMAX(update_eob, c);
    }
  }
389
#endif
390

391 392 393
  // Loop to code all signs in the transform block,
  // starting with the sign of DC (if applicable)
  for (c = 0; c < eob; ++c) {
394 395 396
    const tran_low_t v = tcoeff[scan[c]];
    const tran_low_t level = abs(v);
    const int sign = (v < 0) ? 1 : 0;
397
    if (level == 0) continue;
398 399

    if (c == 0) {
400
#if LV_MAP_PROB
401 402
      aom_write_bin(w, sign,
                    ec_ctx->dc_sign_cdf[plane_type][txb_ctx->dc_sign_ctx], 2);
403 404 405
#else
      aom_write(w, sign, ec_ctx->dc_sign[plane_type][txb_ctx->dc_sign_ctx]);
#endif
406 407 408
    } else {
      aom_write_bit(w, sign);
    }
409 410 411
  }

  for (c = update_eob; c >= 0; --c) {
412
    const tran_low_t level = abs(tcoeff[scan[c]]);
413 414 415 416
    int idx;
    int ctx;

    if (level <= NUM_BASE_LEVELS) continue;
417 418

    // level is above 1.
419
    ctx = get_br_ctx(levels, scan[c], bwl, height);
420 421 422 423 424 425 426 427 428 429 430 431

    int base_range = level - 1 - NUM_BASE_LEVELS;
    int br_set_idx = 0;
    int br_base = 0;
    int br_offset = 0;

    if (base_range >= COEFF_BASE_RANGE)
      br_set_idx = BASE_RANGE_SETS;
    else
      br_set_idx = coeff_to_br_index[base_range];

    for (idx = 0; idx < BASE_RANGE_SETS; ++idx) {
432 433
      aom_write_bin(w, idx == br_set_idx,
                    ec_ctx->coeff_br_cdf[txs_ctx][plane_type][idx][ctx], 2);
434 435 436 437 438 439
      if (idx == br_set_idx) {
        br_base = br_index_to_coeff[br_set_idx];
        br_offset = base_range - br_base;
        int extra_bits = (1 << br_extra_bits[idx]) - 1;
        for (int tok = 0; tok < extra_bits; ++tok) {
          if (tok == br_offset) {
440 441
            aom_write_bin(w, 1, ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx],
                          2);
442 443
            break;
          }
444 445
          aom_write_bin(w, 0, ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx],
                        2);
446 447 448 449 450 451 452
        }
        //        aom_write_literal(w, br_offset, br_extra_bits[idx]);
        break;
      }
    }

    if (br_set_idx < BASE_RANGE_SETS) continue;
453 454

    // use 0-th order Golomb code to handle the residual level.
455 456
    write_golomb(w,
                 abs(tcoeff[scan[c]]) - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
457 458
  }
}
459

Angie Chiang's avatar
Angie Chiang committed
460 461 462 463 464 465
void av1_write_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x,
                         aom_writer *w, int plane) {
  MACROBLOCKD *xd = &x->e_mbd;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
  BLOCK_SIZE bsize = mbmi->sb_type;
  struct macroblockd_plane *pd = &xd->plane[plane];
466 467
  const BLOCK_SIZE plane_bsize =
      AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
468 469
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
470
  const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
Angie Chiang's avatar
Angie Chiang committed
471 472 473 474 475
  const int bkw = tx_size_wide_unit[tx_size];
  const int bkh = tx_size_high_unit[tx_size];
  const int step = tx_size_wide_unit[tx_size] * tx_size_high_unit[tx_size];
  int row, col;
  int block = 0;
476 477
  for (row = 0; row < max_blocks_high; row += bkh) {
    for (col = 0; col < max_blocks_wide; col += bkw) {
Angie Chiang's avatar
Angie Chiang committed
478 479 480 481
      tran_low_t *tcoeff = BLOCK_OFFSET(x->mbmi_ext->tcoeff[plane], block);
      uint16_t eob = x->mbmi_ext->eobs[plane][block];
      TXB_CTX txb_ctx = { x->mbmi_ext->txb_skip_ctx[plane][block],
                          x->mbmi_ext->dc_sign_ctx[plane][block] };
Jingning Han's avatar
Jingning Han committed
482 483
      av1_write_coeffs_txb(cm, xd, w, row, col, block, plane, tx_size, tcoeff,
                           eob, &txb_ctx);
Angie Chiang's avatar
Angie Chiang committed
484 485 486 487 488
      block += step;
    }
  }
}

489 490
static INLINE void get_base_ctx_set(const tran_low_t *tcoeffs,
                                    int c,  // raster order
491
                                    const int bwl, const int height,
492 493 494 495
                                    int ctx_set[NUM_BASE_LEVELS]) {
  const int row = c >> bwl;
  const int col = c - (row << bwl);
  const int stride = 1 << bwl;
496 497
  int mag_count[NUM_BASE_LEVELS] = { 0 };
  int nb_mag[NUM_BASE_LEVELS][3] = { { 0 } };
498 499 500 501 502 503 504 505 506
  int idx;
  tran_low_t abs_coeff;
  int i;

  for (idx = 0; idx < BASE_CONTEXT_POSITION_NUM; ++idx) {
    int ref_row = row + base_ref_offset[idx][0];
    int ref_col = col + base_ref_offset[idx][1];
    int pos = (ref_row << bwl) + ref_col;

507
    if (ref_row < 0 || ref_col < 0 || ref_row >= height || ref_col >= stride)
508 509 510 511 512 513
      continue;

    abs_coeff = abs(tcoeffs[pos]);

    for (i = 0; i < NUM_BASE_LEVELS; ++i) {
      ctx_set[i] += abs_coeff > i;
514 515 516 517 518 519
      if (base_ref_offset[idx][0] == 0 && base_ref_offset[idx][1] == 1)
        nb_mag[i][0] = abs_coeff;
      if (base_ref_offset[idx][0] == 1 && base_ref_offset[idx][1] == 0)
        nb_mag[i][1] = abs_coeff;
      if (base_ref_offset[idx][0] == 1 && base_ref_offset[idx][1] == 1)
        nb_mag[i][2] = abs_coeff;
520 521 522 523
    }
  }

  for (i = 0; i < NUM_BASE_LEVELS; ++i) {
524 525 526
    for (idx = 0; idx < 3; ++idx) mag_count[i] += nb_mag[i][idx] > i + 1;
    ctx_set[i] = get_base_ctx_from_count_mag(row, col, ctx_set[i],
                                             AOMMIN(2, mag_count[i]));
527 528 529 530
  }
  return;
}

531
static INLINE int get_br_cost(tran_low_t abs_qc, int ctx,
Angie Chiang's avatar
Angie Chiang committed
532
                              const int *coeff_lps) {
533 534
  const tran_low_t min_level = 1 + NUM_BASE_LEVELS;
  const tran_low_t max_level = 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE;
535
  (void)ctx;
536 537
  if (abs_qc >= min_level) {
    if (abs_qc >= max_level)
538
      return coeff_lps[COEFF_BASE_RANGE];  // COEFF_BASE_RANGE * cost0;
539
    else
540
      return coeff_lps[(abs_qc - min_level)];  //  * cost0 + cost1;
541 542 543 544 545 546
  } else {
    return 0;
  }
}

static INLINE int get_base_cost(tran_low_t abs_qc, int ctx,
547
                                const int coeff_base[2], int base_idx) {
548
  const int level = base_idx + 1;
549
  (void)ctx;
550 551 552
  if (abs_qc < level)
    return 0;
  else
553
    return coeff_base[abs_qc == level];
554 555
}

556
int av1_cost_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *x, int plane,
Jingning Han's avatar
Jingning Han committed
557 558
                        int blk_row, int blk_col, int block, TX_SIZE tx_size,
                        TXB_CTX *txb_ctx) {
559
  MACROBLOCKD *const xd = &x->e_mbd;
560
  TX_SIZE txs_ctx = get_txsize_context(tx_size);
561
  const PLANE_TYPE plane_type = get_plane_type(plane);
562 563
  const TX_TYPE tx_type =
      av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
564 565 566 567 568 569 570 571
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
  const struct macroblock_plane *p = &x->plane[plane];
  const int eob = p->eobs[block];
  const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  int c, cost;
  int txb_skip_ctx = txb_ctx->txb_skip_ctx;

  const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2;
572 573
  const int height = tx_size_high[tx_size];

Angie Chiang's avatar
Angie Chiang committed
574
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
575
  const int16_t *scan = scan_order->scan;
576
  uint8_t levels[MAX_TX_SQUARE];
577

578 579
  LV_MAP_COEFF_COST *coeff_costs = &x->coeff_costs[txs_ctx][plane_type];

580 581 582
  cost = 0;

  if (eob == 0) {
583
    cost = coeff_costs->txb_skip_cost[txb_skip_ctx][1];
584 585
    return cost;
  }
586
  cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
587

588 589
  av1_txb_init_levels(qcoeff, tx_size_2d[tx_size], levels);

Angie Chiang's avatar
Angie Chiang committed
590
#if CONFIG_TXK_SEL
591
  cost += av1_tx_type_cost(cm, x, xd, mbmi->sb_type, plane, tx_size, tx_type);
Angie Chiang's avatar
Angie Chiang committed
592
#endif
593

Dake He's avatar
Dake He committed
594
  const int seg_eob = tx_size_2d[tx_size];
Jingning Han's avatar
Jingning Han committed
595
  int eob_cost = get_eob_cost(eob, seg_eob, coeff_costs, tx_type);
Dake He's avatar
Dake He committed
596 597

  cost += eob_cost;
598 599 600
#if USE_CAUSAL_BASE_CTX
  int coeff_ctx = 0;
#endif
Dake He's avatar
Dake He committed
601 602 603 604
  for (c = eob - 1; c >= 0; --c) {
    tran_low_t v = qcoeff[scan[c]];
    int is_nz = (v != 0);
    int level = abs(v);
605 606 607
#if USE_CAUSAL_BASE_CTX
    coeff_ctx = get_nz_map_ctx(qcoeff, c, scan, bwl, height, tx_type, 0);
#endif
Dake He's avatar
Dake He committed
608 609

    if (c < eob - 1) {
610
#if !USE_CAUSAL_BASE_CTX
611
      int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type, 1);
612
#endif
Dake He's avatar
Dake He committed
613 614 615 616
      cost += coeff_costs->nz_map_cost[coeff_ctx][is_nz];
    }

    if (is_nz) {
617
#if !USE_CAUSAL_BASE_CTX
Dake He's avatar
Dake He committed
618
      int ctx_ls[NUM_BASE_LEVELS] = { 0 };
619
#endif
Dake He's avatar
Dake He committed
620 621 622 623 624 625 626 627 628
      int sign = (v < 0) ? 1 : 0;

      // sign bit cost
      if (c == 0) {
        int dc_sign_ctx = txb_ctx->dc_sign_ctx;
        cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign];
      } else {
        cost += av1_cost_bit(128, sign);
      }
629 630 631 632 633 634 635 636 637 638
#if USE_CAUSAL_BASE_CTX
      int k;
      for (k = 0; k < NUM_BASE_LEVELS; ++k) {
        int is_k = (level > (k + 1));
        int ctx = coeff_ctx;
        // get_base_ctx_from_b(c, k, b0, b1, b2);
        cost += coeff_costs->base_cost[k][ctx][is_k];
        if (is_k == 0) break;
      }
#else
Dake He's avatar
Dake He committed
639 640 641 642 643 644 645 646 647 648 649 650
      get_base_ctx_set(qcoeff, scan[c], bwl, height, ctx_ls);

      int i;
      for (i = 0; i < NUM_BASE_LEVELS; ++i) {
        if (level <= i) continue;

        if (level == i + 1) {
          cost += coeff_costs->base_cost[i][ctx_ls[i]][1];
          continue;
        }
        cost += coeff_costs->base_cost[i][ctx_ls[i]][0];
      }
651
#endif
Dake He's avatar
Dake He committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

      if (level > NUM_BASE_LEVELS) {
        int ctx;
        ctx = get_br_ctx_coeff(qcoeff, scan[c], bwl, height);
        int base_range = level - 1 - NUM_BASE_LEVELS;
        if (base_range < COEFF_BASE_RANGE) {
          cost += coeff_costs->lps_cost[ctx][base_range];
        } else {
          cost += coeff_costs->lps_cost[ctx][COEFF_BASE_RANGE];
        }

        if (level >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
          // residual cost
          int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
          int ri = r;
          int length = 0;

          while (ri) {
            ri >>= 1;
            ++length;
          }

          for (ri = 0; ri < length - 1; ++ri) cost += av1_cost_bit(128, 0);

          for (ri = length - 1; ri >= 0; --ri)
            cost += av1_cost_bit(128, (r >> ri) & 0x01);
        }
      }
    }
  }
682 683
  return cost;
}
Angie Chiang's avatar
Angie Chiang committed
684

685 686 687 688 689 690 691 692 693
static INLINE int has_base(tran_low_t qc, int base_idx) {
  const int level = base_idx + 1;
  return abs(qc) >= level;
}

static INLINE int has_br(tran_low_t qc) {
  return abs(qc) >= 1 + NUM_BASE_LEVELS;
}

694
static INLINE int get_sign_bit_cost(tran_low_t qc, int coeff_idx,
695
                                    const int (*dc_sign_cost)[2],
696 697 698 699
                                    int dc_sign_ctx) {
  const int sign = (qc < 0) ? 1 : 0;
  // sign bit cost
  if (coeff_idx == 0) {
700
    return dc_sign_cost[dc_sign_ctx][sign];
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  } else {
    return av1_cost_bit(128, sign);
  }
}
static INLINE int get_golomb_cost(int abs_qc) {
  if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
    // residual cost
    int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
    int ri = r;
    int length = 0;

    while (ri) {
      ri >>= 1;
      ++length;
    }

    return av1_cost_literal(2 * length - 1);
  } else {
    return 0;
  }
}

723
void gen_txb_cache(TxbCache *txb_cache, TxbInfo *txb_info) {
Angie Chiang's avatar
Angie Chiang committed
724
  // gen_nz_count_arr
725
  const int16_t *scan = txb_info->scan_order->scan;
Angie Chiang's avatar
Angie Chiang committed
726 727
  const int bwl = txb_info->bwl;
  const int height = txb_info->height;
Linfeng Zhang's avatar
Linfeng Zhang committed
728
  const tran_low_t *const qcoeff = txb_info->qcoeff;
729 730
  const BASE_CTX_TABLE *base_ctx_table =
      txb_info->coeff_ctx_table->base_ctx_table;
Angie Chiang's avatar
Angie Chiang committed
731 732 733 734
  for (int c = 0; c < txb_info->eob; ++c) {
    const int coeff_idx = scan[c];  // raster order
    const int row = coeff_idx >> bwl;
    const int col = coeff_idx - (row << bwl);
Jingning Han's avatar
Jingning Han committed
735 736 737

    txb_cache->nz_count_arr[coeff_idx] = get_nz_count(
        qcoeff, bwl, height, row, col, get_tx_class(txb_info->tx_type), 0);
Dake He's avatar
Dake He committed
738

Angie Chiang's avatar
Angie Chiang committed
739
    const int nz_count = txb_cache->nz_count_arr[coeff_idx];
740 741 742 743 744 745
    txb_cache->nz_ctx_arr[coeff_idx] =
        get_nz_map_ctx_from_count(nz_count, coeff_idx, bwl, height,
#if USE_CAUSAL_BASE_CTX
                                  0,
#endif
                                  txb_info->tx_type);
Angie Chiang's avatar
Angie Chiang committed
746 747 748 749

    // gen_base_count_mag_arr
    if (!has_base(qcoeff[coeff_idx], 0)) continue;
    int *base_mag = txb_cache->base_mag_arr[coeff_idx];
Angie Chiang's avatar
Angie Chiang committed
750 751
    int count[NUM_BASE_LEVELS];
    get_base_count_mag(base_mag, count, qcoeff, bwl, height, row, col);
Angie Chiang's avatar
Angie Chiang committed
752 753

    for (int i = 0; i < NUM_BASE_LEVELS; ++i) {
Angie Chiang's avatar
Angie Chiang committed
754 755
      if (!has_base(qcoeff[coeff_idx], i)) break;
      txb_cache->base_count_arr[i][coeff_idx] = count[i];
Angie Chiang's avatar
Angie Chiang committed
756
      const int level = i + 1;
Angie Chiang's avatar
Angie Chiang committed
757
      txb_cache->base_ctx_arr[i][coeff_idx] =
758
          base_ctx_table[row != 0][col != 0][base_mag[0] > level][count[i]];
Angie Chiang's avatar
Angie Chiang committed
759 760 761 762 763 764
    }

    // gen_br_count_mag_arr
    if (!has_br(qcoeff[coeff_idx])) continue;
    int *br_count = txb_cache->br_count_arr + coeff_idx;
    int *br_mag = txb_cache->br_mag_arr[coeff_idx];
Angie Chiang's avatar
Angie Chiang committed
765 766
    *br_count = get_br_count_mag(br_mag, qcoeff, bwl, height, row, col,
                                 NUM_BASE_LEVELS);
Angie Chiang's avatar
Angie Chiang committed
767 768 769
    txb_cache->br_ctx_arr[coeff_idx] =
        get_br_ctx_from_count_mag(row, col, *br_count, br_mag[0]);
  }
770 771
}

772 773 774
static INLINE const int *get_level_prob(int level, int coeff_idx,
                                        const TxbCache *txb_cache,
                                        const LV_MAP_COEFF_COST *txb_costs) {
775
  if (level == 0) {
776
    const int ctx = txb_cache->nz_ctx_arr[coeff_idx];
777
    return txb_costs->nz_map_cost[ctx];
778 779
  } else if (level >= 1 && level < 1 + NUM_BASE_LEVELS) {
    const int idx = level - 1;
780
    const int ctx = txb_cache->base_ctx_arr[idx][coeff_idx];
781
    return txb_costs->base_cost[idx][ctx];
782 783
  } else if (level >= 1 + NUM_BASE_LEVELS &&
             level < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
784
    const int ctx = txb_cache->br_ctx_arr[coeff_idx];
785
    return txb_costs->lps_cost[ctx];
786 787 788 789 790 791 792 793 794 795
  } else if (level >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
    printf("get_level_prob does not support golomb\n");
    assert(0);
    return 0;
  } else {
    assert(0);
    return 0;
  }
}

Angie Chiang's avatar
Angie Chiang committed
796 797 798 799 800 801 802
static INLINE void update_mag_arr(int *mag_arr, int abs_qc) {
  if (mag_arr[0] == abs_qc) {
    mag_arr[1] -= 1;
    assert(mag_arr[1] >= 0);
  }
}

Angie Chiang's avatar
Angie Chiang committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
static INLINE int get_mag_from_mag_arr(const int *mag_arr) {
  int mag;
  if (mag_arr[1] > 0) {
    mag = mag_arr[0];
  } else if (mag_arr[0] > 0) {
    mag = mag_arr[0] - 1;
  } else {
    // no neighbor
    assert(mag_arr[0] == 0 && mag_arr[1] == 0);
    mag = 0;
  }
  return mag;
}

static int neighbor_level_down_update(int *new_count, int *new_mag, int count,
                                      const int *mag, int coeff_idx,
                                      tran_low_t abs_nb_coeff, int nb_coeff_idx,
                                      int level, const TxbInfo *txb_info) {
  *new_count = count;
  *new_mag = get_mag_from_mag_arr(mag);

  int update = 0;
  // check if br_count changes
  if (abs_nb_coeff == level) {
    update = 1;
    *new_count -= 1;
    assert(*new_count >= 0);
  }
  const int row = coeff_idx >> txb_info->bwl;
  const int col = coeff_idx - (row << txb_info->bwl);
  const int nb_row = nb_coeff_idx >> txb_info->bwl;
  const int nb_col = nb_coeff_idx - (nb_row << txb_info->bwl);

  // check if mag changes
  if (nb_row >= row && nb_col >= col) {
    if (abs_nb_coeff == mag[0]) {
      assert(mag[1] > 0);
      if (mag[1] == 1) {
        // the nb is the only qc with max mag
        *new_mag -= 1;
        assert(*new_mag >= 0);
        update = 1;
      }
    }
  }
  return update;
}

static int try_neighbor_level_down_br(int coeff_idx, int nb_coeff_idx,
                                      const TxbCache *txb_cache,
853
                                      const LV_MAP_COEFF_COST *txb_costs,
Angie Chiang's avatar
Angie Chiang committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
                                      const TxbInfo *txb_info) {
  const tran_low_t qc = txb_info->qcoeff[coeff_idx];
  const tran_low_t abs_qc = abs(qc);
  const int level = NUM_BASE_LEVELS + 1;
  if (abs_qc < level) return 0;

  const tran_low_t nb_coeff = txb_info->qcoeff[nb_coeff_idx];
  const tran_low_t abs_nb_coeff = abs(nb_coeff);
  const int count = txb_cache->br_count_arr[coeff_idx];
  const int *mag = txb_cache->br_mag_arr[coeff_idx];
  int new_count;
  int new_mag;
  const int update =
      neighbor_level_down_update(&new_count, &new_mag, count, mag, coeff_idx,
                                 abs_nb_coeff, nb_coeff_idx, level, txb_info);
  if (update) {
    const int row = coeff_idx >> txb_info->bwl;
    const int col = coeff_idx - (row << txb_info->bwl);
872
    const int ctx = txb_cache->br_ctx_arr[coeff_idx];
873
    const int org_cost = get_br_cost(abs_qc, ctx, txb_costs->lps_cost[ctx]);
Angie Chiang's avatar
Angie Chiang committed
874 875

    const int new_ctx = get_br_ctx_from_count_mag(row, col, new_count, new_mag);
876 877
    const int new_cost =
        get_br_cost(abs_qc, new_ctx, txb_costs->lps_cost[new_ctx]);
Angie Chiang's avatar
Angie Chiang committed
878 879 880 881 882 883 884 885 886
    const int cost_diff = -org_cost + new_cost;
    return cost_diff;
  } else {
    return 0;
  }
}

static int try_neighbor_level_down_base(int coeff_idx, int nb_coeff_idx,
                                        const TxbCache *txb_cache,
887
                                        const LV_MAP_COEFF_COST *txb_costs,
Angie Chiang's avatar
Angie Chiang committed
888 889 890
                                        const TxbInfo *txb_info) {
  const tran_low_t qc = txb_info->qcoeff[coeff_idx];
  const tran_low_t abs_qc = abs(qc);
891 892
  const BASE_CTX_TABLE *base_ctx_table =
      txb_info->coeff_ctx_table->base_ctx_table;
Angie Chiang's avatar
Angie Chiang committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

  int cost_diff = 0;
  for (int base_idx = 0; base_idx < NUM_BASE_LEVELS; ++base_idx) {
    const int level = base_idx + 1;
    if (abs_qc < level) continue;

    const tran_low_t nb_coeff = txb_info->qcoeff[nb_coeff_idx];
    const tran_low_t abs_nb_coeff = abs(nb_coeff);

    const int count = txb_cache->base_count_arr[base_idx][coeff_idx];
    const int *mag = txb_cache->base_mag_arr[coeff_idx];
    int new_count;
    int new_mag;
    const int update =
        neighbor_level_down_update(&new_count, &new_mag, count, mag, coeff_idx,
                                   abs_nb_coeff, nb_coeff_idx, level, txb_info);
    if (update) {
      const int row = coeff_idx >> txb_info->bwl;
      const int col = coeff_idx - (row << txb_info->bwl);
912
      const int ctx = txb_cache->base_ctx_arr[base_idx][coeff_idx];
913 914
      const int org_cost = get_base_cost(
          abs_qc, ctx, txb_costs->base_cost[base_idx][ctx], base_idx);
Angie Chiang's avatar
Angie Chiang committed
915 916

      const int new_ctx =
917
          base_ctx_table[row != 0][col != 0][new_mag > level][new_count];
918 919
      const int new_cost = get_base_cost(
          abs_qc, new_ctx, txb_costs->base_cost[base_idx][new_ctx], base_idx);
Angie Chiang's avatar
Angie Chiang committed
920 921 922 923 924 925 926 927
      cost_diff += -org_cost + new_cost;
    }
  }
  return cost_diff;
}

static int try_neighbor_level_down_nz(int coeff_idx, int nb_coeff_idx,
                                      const TxbCache *txb_cache,
928
                                      const LV_MAP_COEFF_COST *txb_costs,
Angie Chiang's avatar
Angie Chiang committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942
                                      TxbInfo *txb_info) {
  // assume eob doesn't change
  const tran_low_t qc = txb_info->qcoeff[coeff_idx];
  const tran_low_t abs_qc = abs(qc);
  const tran_low_t nb_coeff = txb_info->qcoeff[nb_coeff_idx];
  const tran_low_t abs_nb_coeff = abs(nb_coeff);
  if (abs_nb_coeff != 1) return 0;
  const int16_t *iscan = txb_info->scan_order->iscan;
  const int scan_idx = iscan[coeff_idx];
  if (scan_idx == txb_info->seg_eob) return 0;
  const int nb_scan_idx = iscan[nb_coeff_idx];
  if (nb_scan_idx < scan_idx) {
    const int count = txb_cache->nz_count_arr[coeff_idx];
    assert(count > 0);
943
    update_qcoeff(nb_coeff_idx, get_lower_coeff(nb_coeff), txb_info);
944 945 946 947 948 949
    const int new_ctx = get_nz_map_ctx_from_count(
        count - 1, coeff_idx, txb_info->bwl, txb_info->height,
#if USE_CAUSAL_BASE_CTX
        0,
#endif
        txb_info->tx_type);
950
    update_qcoeff(nb_coeff_idx, nb_coeff, txb_info);
951
    const int ctx = txb_cache->nz_ctx_arr[coeff_idx];
Angie Chiang's avatar
Angie Chiang committed
952
    const int is_nz = abs_qc > 0;
953 954
    const int org_cost = txb_costs->nz_map_cost[ctx][is_nz];
    const int new_cost = txb_costs->nz_map_cost[new_ctx][is_nz];
Angie Chiang's avatar
Angie Chiang committed
955 956 957 958 959 960 961 962 963
    const int cost_diff = new_cost - org_cost;
    return cost_diff;
  } else {
    return 0;
  }
}

static int try_self_level_down(tran_low_t *low_coeff, int coeff_idx,
                               const TxbCache *txb_cache,
964 965
                               const LV_MAP_COEFF_COST *txb_costs,
                               TxbInfo *txb_info) {
966 967 968 969 970 971 972 973 974 975
  const tran_low_t qc = txb_info->qcoeff[coeff_idx];
  if (qc == 0) {
    *low_coeff = 0;
    return 0;
  }
  const tran_low_t abs_qc = abs(qc);
  *low_coeff = get_lower_coeff(qc);
  int cost_diff;
  if (*low_coeff == 0) {
    const int scan_idx = txb_info->scan_order->iscan[coeff_idx];
976 977 978 979
    const int *level_cost =
        get_level_prob(abs_qc, coeff_idx, txb_cache, txb_costs);
    const int *low_level_cost =
        get_level_prob(abs(*low_coeff), coeff_idx, txb_cache, txb_costs);
Jingning Han's avatar
Jingning Han committed
980

Dake He's avatar
Dake He committed
981
    if (scan_idx < txb_info->eob - 1) {
982 983 984 985
      // When level-0, we code the binary of abs_qc > level
      // but when level-k k > 0 we code the binary of abs_qc == level
      // That's why wee need this special treatment for level-0 map
      // TODO(angiebird): make leve-0 consistent to other levels
986
      cost_diff = -level_cost[1] + low_level_cost[0] - low_level_cost[1];
987
    } else {
988
      cost_diff = -level_cost[1];
989 990 991
    }

    const int sign_cost = get_sign_bit_cost(
992
        qc, coeff_idx, txb_costs->dc_sign_cost, txb_info->txb_ctx->dc_sign_ctx);
993
    cost_diff -= sign_cost;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  } else if (abs_qc <= NUM_BASE_LEVELS) {
    const int *level_cost =
        get_level_prob(abs_qc, coeff_idx, txb_cache, txb_costs);
    const int *low_level_cost =
        get_level_prob(abs(*low_coeff), coeff_idx, txb_cache, txb_costs);
    cost_diff = -level_cost[1] + low_level_cost[1] - low_level_cost[0];
  } else if (abs_qc == NUM_BASE_LEVELS + 1) {
    const int *level_cost =
        get_level_prob(abs_qc, coeff_idx, txb_cache, txb_costs);
    const int *low_level_cost =
        get_level_prob(abs(*low_coeff), coeff_idx, txb_cache, txb_costs);
    cost_diff = -level_cost[0] + low_level_cost[1] - low_level_cost[0];
1006
  } else if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
1007 1008 1009 1010 1011
    const int *level_cost =
        get_level_prob(abs_qc, coeff_idx, txb_cache, txb_costs);
    const int *low_level_cost =
        get_level_prob(abs(*low_coeff), coeff_idx, txb_cache, txb_costs);

1012 1013
    cost_diff = -level_cost[abs_qc - 1 - NUM_BASE_LEVELS] +
                low_level_cost[abs(*low_coeff) - 1 - NUM_BASE_LEVELS];
1014
  } else if (abs_qc == 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
1015 1016
    const int *low_level_cost =
        get_level_prob(abs(*low_coeff), coeff_idx, txb_cache, txb_costs);
1017 1018
    cost_diff = -get_golomb_cost(abs_qc) - low_level_cost[COEFF_BASE_RANGE] +
                low_level_cost[COEFF_BASE_RANGE - 1];
1019 1020 1021 1022 1023 1024 1025 1026
  } else {
    assert(abs_qc > 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE);
    const tran_low_t abs_low_coeff = abs(*low_coeff);
    cost_diff = -get_golomb_cost(abs_qc) + get_golomb_cost(abs_low_coeff);
  }
  return cost_diff;
}

Angie Chiang's avatar
Angie Chiang committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
#define COST_MAP_SIZE 5
#define COST_MAP_OFFSET 2

static INLINE int check_nz_neighbor(tran_low_t qc) { return abs(qc) == 1; }

static INLINE int check_base_neighbor(tran_low_t qc) {
  return abs(qc) <= 1 + NUM_BASE_LEVELS;
}

static INLINE int check_br_neighbor(tran_low_t qc) {
  return abs(qc) > BR_MAG_OFFSET;
}

1040
#define FAST_OPTIMIZE_TXB 1
1041 1042 1043

#if FAST_OPTIMIZE_TXB
#define ALNB_REF_OFFSET_NUM 2
1044
static const int alnb_ref_offset[ALNB_REF_OFFSET_NUM][2] = {