| /* |
| * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
| * |
| * This source code is subject to the terms of the BSD 3-Clause Clear License |
| * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
| * License was not distributed with this source code in the LICENSE file, you |
| * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. 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 |
| * aomedia.org/license/patent-license/. |
| */ |
| |
| #include "av1/encoder/encodetxb.h" |
| |
| #include "aom_ports/mem.h" |
| #include "av1/common/blockd.h" |
| #include "av1/common/idct.h" |
| #include "av1/common/pred_common.h" |
| #include "av1/common/scan.h" |
| #include "av1/common/reconintra.h" |
| #include "av1/encoder/bitstream.h" |
| #include "av1/encoder/cost.h" |
| #include "av1/encoder/encodeframe.h" |
| #include "av1/encoder/hash.h" |
| #include "av1/encoder/rdopt.h" |
| #include "av1/encoder/tokenize.h" |
| |
| #if CONFIG_PAR_HIDING |
| typedef struct { |
| tran_low_t qc; |
| tran_low_t dqc; // dequantized qc |
| int64_t delta_cost; // cost change between between coding up and low level |
| int delta_rate; // rate change between coding up and low level |
| bool upround; // is quantized into up level |
| bool tunable; // tunable mark |
| } coeff_info; |
| |
| // set rd related information for the coefficient at current position. |
| void set_coeff_info(tran_low_t qc_low, tran_low_t dqc_low, tran_low_t qc_up, |
| tran_low_t dqc_up, int64_t cost_low, int64_t cost_up, |
| int rate_low, int rate_up, bool upround, |
| coeff_info *coef_info, const int scan_idx) { |
| if (!scan_idx) { |
| return; |
| } |
| coef_info[scan_idx].upround = upround; |
| if (upround) { |
| coef_info[scan_idx].qc = qc_low; |
| coef_info[scan_idx].dqc = dqc_low; |
| } else { |
| coef_info[scan_idx].qc = qc_up; |
| coef_info[scan_idx].dqc = dqc_up; |
| } |
| |
| coef_info[scan_idx].tunable = |
| (abs(coef_info[scan_idx].qc) < MAX_BASE_BR_RANGE) || |
| ((abs(qc_up) == MAX_BASE_BR_RANGE) && upround); |
| if (coef_info[scan_idx].tunable) { |
| if (upround) { |
| coef_info[scan_idx].delta_cost = (cost_low - cost_up); |
| coef_info[scan_idx].delta_rate = (rate_low - rate_up); |
| } else { |
| coef_info[scan_idx].delta_cost = (cost_up - cost_low); |
| coef_info[scan_idx].delta_rate = (rate_up - rate_low); |
| } |
| } |
| } |
| #endif // CONFIG_PAR_HIDING |
| |
| 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; |
| int64_t nz_rd; |
| int64_t rd_diff; |
| int cost_diff; |
| int64_t dist_diff; |
| int new_eob; |
| } LevelDownStats; |
| |
| static INLINE int get_dqv(const int32_t *dequant, int coeff_idx, |
| const qm_val_t *iqmatrix) { |
| int dqv = dequant[!!coeff_idx]; |
| if (iqmatrix != NULL) |
| dqv = |
| ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| return dqv; |
| } |
| |
| void av1_alloc_txb_buf(AV1_COMP *cpi) { |
| AV1_COMMON *cm = &cpi->common; |
| int size = ((cm->mi_params.mi_rows >> cm->seq_params.mib_size_log2) + 1) * |
| ((cm->mi_params.mi_cols >> cm->seq_params.mib_size_log2) + 1); |
| |
| av1_free_txb_buf(cpi); |
| // TODO(jingning): This should be further reduced. |
| CHECK_MEM_ERROR(cm, cpi->coeff_buffer_base, |
| aom_memalign(32, sizeof(*cpi->coeff_buffer_base) * size)); |
| } |
| |
| void av1_free_txb_buf(AV1_COMP *cpi) { aom_free(cpi->coeff_buffer_base); } |
| |
| static void write_golomb(aom_writer *w, int level) { |
| int x = level + 1; |
| int length = 0; |
| |
| #if CONFIG_BYPASS_IMPROVEMENT |
| length = get_msb(x) + 1; |
| assert(length > 0); |
| aom_write_literal(w, 0, length - 1); |
| aom_write_literal(w, x, length); |
| #else |
| int i = x; |
| 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); |
| #endif |
| } |
| |
| 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; |
| } |
| |
| static const int8_t eob_to_pos_small[33] = { |
| 0, 1, 2, // 0-2 |
| 3, 3, // 3-4 |
| 4, 4, 4, 4, // 5-8 |
| 5, 5, 5, 5, 5, 5, 5, 5, // 9-16 |
| 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 // 17-32 |
| }; |
| |
| static const int8_t eob_to_pos_large[17] = { |
| 6, // place holder |
| 7, // 33-64 |
| 8, 8, // 65-128 |
| 9, 9, 9, 9, // 129-256 |
| 10, 10, 10, 10, 10, 10, 10, 10, // 257-512 |
| 11 // 513- |
| }; |
| |
| static INLINE int get_eob_pos_token(const int eob, int *const extra) { |
| int t; |
| |
| if (eob < 33) { |
| t = eob_to_pos_small[eob]; |
| } else { |
| const int e = AOMMIN((eob - 1) >> 5, 16); |
| t = eob_to_pos_large[e]; |
| } |
| |
| *extra = eob - av1_eob_group_start[t]; |
| |
| return t; |
| } |
| |
| #if CONFIG_ENTROPY_STATS |
| void av1_update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size, |
| TX_CLASS tx_class, PLANE_TYPE plane, |
| FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts, |
| uint8_t allow_update_cdf) { |
| #else |
| void av1_update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class, |
| PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx, |
| uint8_t allow_update_cdf) { |
| #endif |
| int eob_extra; |
| const int eob_pt = get_eob_pos_token(eob, &eob_extra); |
| TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
| |
| const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
| |
| switch (eob_multi_size) { |
| case 0: |
| #if CONFIG_ENTROPY_STATS |
| ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| #endif |
| if (allow_update_cdf) |
| update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5); |
| break; |
| case 1: |
| #if CONFIG_ENTROPY_STATS |
| ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| #endif |
| if (allow_update_cdf) |
| update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6); |
| break; |
| case 2: |
| #if CONFIG_ENTROPY_STATS |
| ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| #endif |
| if (allow_update_cdf) |
| update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7); |
| break; |
| case 3: |
| #if CONFIG_ENTROPY_STATS |
| ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| #endif |
| if (allow_update_cdf) { |
| update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1, |
| 8); |
| } |
| break; |
| case 4: |
| #if CONFIG_ENTROPY_STATS |
| ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| #endif |
| if (allow_update_cdf) { |
| update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1, |
| 9); |
| } |
| break; |
| case 5: |
| #if CONFIG_ENTROPY_STATS |
| ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| #endif |
| if (allow_update_cdf) { |
| update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1, |
| 10); |
| } |
| break; |
| case 6: |
| default: |
| #if CONFIG_ENTROPY_STATS |
| ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| #endif |
| if (allow_update_cdf) { |
| update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1, |
| 11); |
| } |
| break; |
| } |
| |
| if (av1_eob_offset_bits[eob_pt] > 0) { |
| int eob_ctx = eob_pt - 3; |
| int eob_shift = av1_eob_offset_bits[eob_pt] - 1; |
| int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
| #if CONFIG_ENTROPY_STATS |
| counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++; |
| #endif // CONFIG_ENTROPY_STATS |
| if (allow_update_cdf) |
| update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2); |
| } |
| } |
| |
| static int get_eob_cost(int eob, const LV_MAP_EOB_COST *txb_eob_costs, |
| const LV_MAP_COEFF_COST *txb_costs, TX_CLASS tx_class) { |
| int eob_extra; |
| const int eob_pt = get_eob_pos_token(eob, &eob_extra); |
| int eob_cost = 0; |
| const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
| eob_cost = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1]; |
| |
| if (av1_eob_offset_bits[eob_pt] > 0) { |
| const int eob_ctx = eob_pt - 3; |
| const int eob_shift = av1_eob_offset_bits[eob_pt] - 1; |
| const int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
| eob_cost += txb_costs->eob_extra_cost[eob_ctx][bit]; |
| const int offset_bits = av1_eob_offset_bits[eob_pt]; |
| if (offset_bits > 1) eob_cost += av1_cost_literal(offset_bits - 1); |
| } |
| return eob_cost; |
| } |
| |
| static const int golomb_bits_cost[32] = { |
| 0, 512, 512 * 3, 512 * 3, 512 * 5, 512 * 5, 512 * 5, 512 * 5, |
| 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, |
| 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, |
| 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9 |
| }; |
| static const int golomb_cost_diff[32] = { |
| 0, 512, 512 * 2, 0, 512 * 2, 0, 0, 0, 512 * 2, 0, 0, 0, 0, 0, 0, 0, |
| 512 * 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| }; |
| |
| static INLINE int get_golomb_cost(int abs_qc) { |
| if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) { |
| const int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS; |
| const int length = get_msb(r) + 1; |
| return av1_cost_literal(2 * length - 1); |
| } |
| return 0; |
| } |
| |
| #if CONFIG_ATC_COEFCODING |
| // Golomb cost of coding bypass coded level values in the |
| // low-frequency region. |
| static INLINE int get_golomb_cost_lf(int abs_qc) { |
| if (abs_qc >= 1 + LF_NUM_BASE_LEVELS + COEFF_BASE_RANGE) { |
| const int r = abs_qc - COEFF_BASE_RANGE - LF_NUM_BASE_LEVELS; |
| const int length = get_msb(r) + 1; |
| return av1_cost_literal(2 * length - 1); |
| } |
| return 0; |
| } |
| |
| // Base range cost of coding level values in the |
| // low-frequency region, includes the bypass cost. |
| static INLINE int get_br_lf_cost(tran_low_t level, const int *coeff_lps) { |
| const int base_range = |
| AOMMIN(level - 1 - LF_NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
| return coeff_lps[base_range] + get_golomb_cost_lf(level); |
| } |
| |
| // Calculates differential cost for base range coding in the low-frequency |
| // region for encoder coefficient level optimization. |
| static INLINE int get_br_lf_cost_with_diff(tran_low_t level, |
| const int *coeff_lps, int *diff) { |
| const int base_range = |
| AOMMIN(level - 1 - LF_NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
| int golomb_bits = 0; |
| if (level <= COEFF_BASE_RANGE + 1 + LF_NUM_BASE_LEVELS) |
| *diff += coeff_lps[base_range + COEFF_BASE_RANGE + 1]; |
| if (level >= COEFF_BASE_RANGE + 1 + LF_NUM_BASE_LEVELS) { |
| int r = level - COEFF_BASE_RANGE - LF_NUM_BASE_LEVELS; |
| if (r < 32) { |
| golomb_bits = golomb_bits_cost[r]; |
| *diff += golomb_cost_diff[r]; |
| } else { |
| golomb_bits = get_golomb_cost_lf(level); |
| *diff += (r & (r - 1)) == 0 ? 1024 : 0; |
| } |
| } |
| return coeff_lps[base_range] + golomb_bits; |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| |
| static INLINE int get_br_cost_with_diff(tran_low_t level, const int *coeff_lps, |
| int *diff) { |
| const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
| int golomb_bits = 0; |
| if (level <= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) |
| *diff += coeff_lps[base_range + COEFF_BASE_RANGE + 1]; |
| |
| if (level >= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) { |
| int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS; |
| if (r < 32) { |
| golomb_bits = golomb_bits_cost[r]; |
| *diff += golomb_cost_diff[r]; |
| } else { |
| golomb_bits = get_golomb_cost(level); |
| *diff += (r & (r - 1)) == 0 ? 1024 : 0; |
| } |
| } |
| |
| return coeff_lps[base_range] + golomb_bits; |
| } |
| |
| static INLINE int get_br_cost(tran_low_t level, const int *coeff_lps) { |
| const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
| return coeff_lps[base_range] + get_golomb_cost(level); |
| } |
| |
| static INLINE int get_nz_map_ctx(const uint8_t *const levels, |
| const int coeff_idx, const int bwl, |
| const int height, const int scan_idx, |
| const int is_eob, |
| #if !CONFIG_ATC_COEFCODING |
| const TX_SIZE tx_size, |
| #endif // !CONFIG_ATC_COEFCODING |
| const TX_CLASS tx_class |
| #if CONFIG_ATC_COEFCODING |
| , |
| const int plane) { |
| #else |
| ) { |
| #endif // CONFIG_ATC_COEFCODING |
| if (is_eob) { |
| if (scan_idx == 0) return 0; |
| if (scan_idx <= (height << bwl) / 8) return 1; |
| if (scan_idx <= (height << bwl) / 4) return 2; |
| return 3; |
| } |
| #if CONFIG_ATC_COEFCODING |
| int stats = 0; |
| const int row = coeff_idx >> bwl; |
| const int col = coeff_idx - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| stats = |
| get_nz_mag_lf(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class); |
| return get_nz_map_ctx_from_stats_lf(stats, coeff_idx, bwl, tx_class); |
| } else { |
| stats = get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class); |
| return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_class); |
| } |
| #else |
| const int stats = |
| get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class); |
| return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class); |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| |
| static INLINE int get_nz_map_ctx_skip(const uint8_t *const levels, |
| const int coeff_idx, const int bwl) { |
| const int stats = |
| get_nz_mag_skip(levels + get_padded_idx_left(coeff_idx, bwl), bwl); |
| return get_nz_map_ctx_from_stats_skip(stats, coeff_idx, bwl); |
| } |
| |
| void av1_txb_init_levels_signs_c(const tran_low_t *const coeff, const int width, |
| const int height, uint8_t *const levels, |
| int8_t *const signs) { |
| const int stride = width + TX_PAD_LEFT; |
| int8_t *si = signs; |
| uint8_t *ls = levels; |
| // bottom 4 pad |
| memset(levels + stride * (height + TX_PAD_TOP), 0, |
| sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END)); |
| memset(signs + stride * (height + TX_PAD_TOP), 0, |
| sizeof(*signs) * (TX_PAD_BOTTOM * stride + TX_PAD_END)); |
| // top 4 pad |
| memset(levels, 0, sizeof(*levels) * (TX_PAD_TOP * stride)); |
| ls += TX_PAD_TOP * stride; |
| memset(signs, 0, sizeof(*signs) * (TX_PAD_TOP * stride)); |
| si += TX_PAD_TOP * stride; |
| for (int i = 0; i < height; i++) { |
| // left 4 pad |
| for (int j = 0; j < TX_PAD_LEFT; j++) { |
| *ls++ = 0; |
| } |
| for (int j = 0; j < width; j++) { |
| *si++ = (int8_t)(coeff[i * width + j] > 0) ? 1 : -1; |
| *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX); |
| } |
| // right 4 pad |
| for (int j = 0; j < TX_PAD_RIGHT; j++) { |
| *si++ = 0; |
| } |
| } |
| } |
| |
| void av1_txb_init_levels_skip_c(const tran_low_t *const coeff, const int width, |
| const int height, uint8_t *const levels) { |
| const int stride = width + TX_PAD_LEFT; |
| uint8_t *ls = levels; |
| // bottom 4 padded region |
| memset(levels + stride * (height + TX_PAD_TOP), 0, |
| sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END)); |
| // top 4 padded region |
| memset(levels, 0, sizeof(*levels) * (TX_PAD_TOP * stride)); |
| ls += TX_PAD_TOP * stride; |
| for (int i = 0; i < height; i++) { |
| // left 4 padded region for each row |
| for (int j = 0; j < TX_PAD_LEFT; j++) { |
| *ls++ = 0; |
| } |
| for (int j = 0; j < width; j++) { |
| *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX); |
| } |
| } |
| } |
| |
| void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width, |
| const int height, uint8_t *const levels) { |
| const int stride = width + TX_PAD_HOR; |
| uint8_t *ls = levels; |
| |
| memset(levels + stride * height, 0, |
| sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END)); |
| |
| for (int i = 0; i < height; i++) { |
| for (int j = 0; j < width; j++) { |
| *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX); |
| } |
| for (int j = 0; j < TX_PAD_HOR; j++) { |
| *ls++ = 0; |
| } |
| } |
| } |
| |
| void av1_get_nz_map_contexts_c(const uint8_t *const levels, |
| const int16_t *const scan, const uint16_t eob, |
| const TX_SIZE tx_size, const TX_CLASS tx_class, |
| int8_t *const coeff_contexts |
| #if CONFIG_ATC_COEFCODING |
| , |
| const int plane) { |
| #else |
| ) { |
| #endif // CONFIG_ATC_COEFCODING |
| const int bwl = get_txb_bwl(tx_size); |
| const int height = get_txb_high(tx_size); |
| for (int i = 0; i < eob; ++i) { |
| const int pos = scan[i]; |
| coeff_contexts[pos] = |
| get_nz_map_ctx(levels, pos, bwl, height, i, i == eob - 1, |
| #if !CONFIG_ATC_COEFCODING |
| tx_size, |
| #endif // !CONFIG_ATC_COEFCODING |
| tx_class |
| #if CONFIG_ATC_COEFCODING |
| , |
| plane); |
| #else |
| ); |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| } |
| |
| void av1_get_nz_map_contexts_skip_c(const uint8_t *const levels, |
| const int16_t *const scan, |
| const uint16_t eob, const TX_SIZE tx_size, |
| int8_t *const coeff_contexts) { |
| const int bwl = get_txb_bwl(tx_size); |
| for (int i = 0; i < eob; ++i) { |
| const int pos = scan[i]; |
| coeff_contexts[pos] = get_nz_map_ctx_skip(levels, pos, bwl); |
| } |
| } |
| |
| int av1_write_sig_txtype(const AV1_COMMON *const cm, MACROBLOCK *const x, |
| aom_writer *w, int blk_row, int blk_col, int plane, |
| int block, TX_SIZE tx_size) { |
| MACROBLOCKD *xd = &x->e_mbd; |
| const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
| const int txb_offset = |
| x->mbmi_ext_frame->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN); |
| |
| #if CONFIG_CONTEXT_DERIVATION |
| const int width = get_txb_wide(tx_size); |
| const int height = get_txb_high(tx_size); |
| if (plane == AOM_PLANE_U) |
| memset(xd->tmp_sign, 0, width * height * sizeof(int32_t)); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| |
| const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset; |
| const uint16_t eob = eob_txb[block]; |
| const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset; |
| |
| #if CONFIG_CONTEXT_DERIVATION |
| int txb_skip_ctx = (entropy_ctx[block] & TXB_SKIP_CTX_MASK); |
| if (plane == AOM_PLANE_V) { |
| txb_skip_ctx += (xd->eob_u_flag ? V_TXB_SKIP_CONTEXT_OFFSET : 0); |
| } |
| #else |
| const int txb_skip_ctx = entropy_ctx[block] & TXB_SKIP_CTX_MASK; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| |
| const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
| FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| |
| const PLANE_TYPE plane_type = get_plane_type(plane); |
| const TX_TYPE tx_type = |
| av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
| cm->features.reduced_tx_set_used); |
| |
| #if CONFIG_CROSS_CHROMA_TX && CCTX_C2_DROPPED |
| if (plane == AOM_PLANE_V && is_cctx_allowed(cm, xd)) { |
| CctxType cctx_type = av1_get_cctx_type(xd, blk_row, blk_col); |
| if (!keep_chroma_c2(cctx_type)) return 0; |
| } |
| #endif // CONFIG_CROSS_CHROMA_TX && CCTX_C2_DROPPED |
| |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_U) { |
| xd->eob_u_flag = eob ? 1 : 0; |
| } |
| if (plane == AOM_PLANE_Y || plane == AOM_PLANE_U) { |
| aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], |
| 2); |
| } else { |
| aom_write_symbol(w, eob == 0, ec_ctx->v_txb_skip_cdf[txb_skip_ctx], 2); |
| } |
| #else |
| aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 2); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| |
| #if CONFIG_CROSS_CHROMA_TX |
| if (plane == AOM_PLANE_U && is_cctx_allowed(cm, xd)) { |
| CctxType cctx_type = av1_get_cctx_type(xd, blk_row, blk_col); |
| if (eob > 0) av1_write_cctx_type(cm, xd, cctx_type, tx_size, w); |
| } |
| #endif // CONFIG_CROSS_CHROMA_TX |
| |
| if (eob == 0) return 0; |
| if (plane == 0) { // Only y plane's tx_type is transmitted |
| av1_write_tx_type(cm, xd, tx_type, tx_size, w); |
| } |
| return 1; |
| } |
| |
| void av1_write_coeffs_txb_skip(const AV1_COMMON *const cm, MACROBLOCK *const x, |
| aom_writer *w, int blk_row, int blk_col, |
| int plane, int block, TX_SIZE tx_size) { |
| MACROBLOCKD *xd = &x->e_mbd; |
| const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
| const uint16_t eob = av1_get_max_eob(tx_size); |
| FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| const PLANE_TYPE plane_type = get_plane_type(plane); |
| const TX_TYPE tx_type = |
| av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
| cm->features.reduced_tx_set_used); |
| const int width = get_txb_wide(tx_size); |
| const int height = get_txb_high(tx_size); |
| uint8_t levels_buf[TX_PAD_2D]; |
| int8_t signs_buf[TX_PAD_2D]; |
| const tran_low_t *tcoeff_txb = |
| cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane]; |
| const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block); |
| av1_txb_init_levels_signs(tcoeff, width, height, levels_buf, signs_buf); |
| uint8_t *const levels = set_levels(levels_buf, width); |
| int8_t *const signs = set_signs(signs_buf, width); |
| const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
| const int16_t *const scan = scan_order->scan; |
| const int bwl = get_txb_bwl(tx_size); |
| |
| DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
| av1_get_nz_map_contexts_skip(levels, scan, eob, tx_size, coeff_contexts); |
| |
| for (int c = 0; c < eob; ++c) { |
| const int pos = scan[c]; |
| const int coeff_ctx = coeff_contexts[pos]; |
| const tran_low_t v = tcoeff[pos]; |
| const tran_low_t level = abs(v); |
| aom_write_symbol(w, AOMMIN(level, 3), |
| ec_ctx->coeff_base_cdf_idtx[coeff_ctx], 4); |
| if (level > NUM_BASE_LEVELS) { |
| // level is above 1. |
| const int base_range = level - 1 - NUM_BASE_LEVELS; |
| const int br_ctx = get_br_ctx_skip(levels, pos, bwl); |
| aom_cdf_prob *cdf = ec_ctx->coeff_br_cdf_idtx[br_ctx]; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| } |
| // Loop to code all signs, bypass levels in the transform block |
| for (int c = eob - 1; c >= 0; --c) { |
| const int pos = scan[c]; |
| const tran_low_t v = tcoeff[pos]; |
| const tran_low_t level = abs(v); |
| const int sign = (v < 0) ? 1 : 0; |
| if (level) { |
| int idtx_sign_ctx = get_sign_ctx_skip(signs, levels, pos, bwl); |
| aom_write_symbol(w, sign, ec_ctx->idtx_sign_cdf[idtx_sign_ctx], 2); |
| if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
| } |
| } |
| } |
| |
| #if CONFIG_PAR_HIDING |
| static INLINE void write_coeff_hidden(aom_writer *w, TX_CLASS tx_class, |
| const int16_t *scan, int bwl, |
| uint8_t *levels, const int level, |
| base_cdf_arr base_cdf_ph, |
| br_cdf_arr br_cdf_ph) { |
| const int q_index = (level >> 1); |
| const int pos = scan[0]; |
| |
| int ctx_id = get_base_ctx_ph(levels, pos, bwl, tx_class); |
| aom_write_symbol(w, AOMMIN(q_index, 3), base_cdf_ph[ctx_id], 4); |
| |
| if (q_index > NUM_BASE_LEVELS) { |
| ctx_id = get_par_br_ctx(levels, pos, bwl, tx_class); |
| aom_cdf_prob *cdf_br = br_cdf_ph[ctx_id]; |
| const int base_range = q_index - 1 - NUM_BASE_LEVELS; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf_br, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| } |
| #endif // CONFIG_PAR_HIDING |
| |
| void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x, |
| aom_writer *w, int blk_row, int blk_col, int plane, |
| int block, TX_SIZE tx_size) { |
| MACROBLOCKD *xd = &x->e_mbd; |
| const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
| #if CONFIG_CONTEXT_DERIVATION |
| const int width = get_txb_wide(tx_size); |
| const int height = get_txb_high(tx_size); |
| if (plane == AOM_PLANE_U) |
| memset(xd->tmp_sign, 0, width * height * sizeof(int32_t)); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| const int txb_offset = |
| x->mbmi_ext_frame->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN); |
| const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset; |
| const uint16_t eob = eob_txb[block]; |
| const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset; |
| const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
| FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| #if CONFIG_PC_WIENER |
| assert((eob == 0) == |
| av1_get_txk_skip(cm, xd->mi_row, xd->mi_col, plane, blk_row, blk_col)); |
| #endif // CONFIG_PC_WIENER |
| if (eob == 0) return; |
| |
| const PLANE_TYPE plane_type = get_plane_type(plane); |
| const TX_TYPE tx_type = |
| av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
| cm->features.reduced_tx_set_used); |
| |
| #if DEBUG_EXTQUANT |
| fprintf(cm->fEncCoeffLog, "\nblk_row=%d,blk_col=%d,plane=%d,tx_size=%d", |
| blk_row, blk_col, plane, tx_size); |
| #endif |
| |
| int eob_extra; |
| const int eob_pt = get_eob_pos_token(eob, &eob_extra); |
| const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| const TX_CLASS tx_class = tx_type_to_class[get_primary_tx_type(tx_type)]; |
| const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
| switch (eob_multi_size) { |
| case 0: |
| aom_write_symbol(w, eob_pt - 1, |
| ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5); |
| break; |
| case 1: |
| aom_write_symbol(w, eob_pt - 1, |
| ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6); |
| break; |
| case 2: |
| aom_write_symbol(w, eob_pt - 1, |
| ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7); |
| break; |
| case 3: |
| aom_write_symbol(w, eob_pt - 1, |
| ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8); |
| break; |
| case 4: |
| aom_write_symbol(w, eob_pt - 1, |
| ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9); |
| break; |
| case 5: |
| aom_write_symbol(w, eob_pt - 1, |
| ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10); |
| break; |
| default: |
| aom_write_symbol(w, eob_pt - 1, |
| ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11); |
| break; |
| } |
| |
| const int eob_offset_bits = av1_eob_offset_bits[eob_pt]; |
| if (eob_offset_bits > 0) { |
| const int eob_ctx = eob_pt - 3; |
| int eob_shift = eob_offset_bits - 1; |
| int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
| aom_write_symbol(w, bit, |
| ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2); |
| #if CONFIG_BYPASS_IMPROVEMENT |
| // Zero out top bit; write (eob_offset_bits - 1) lsb bits. |
| eob_extra &= (1 << (eob_offset_bits - 1)) - 1; |
| aom_write_literal(w, eob_extra, eob_offset_bits - 1); |
| #else |
| for (int i = 1; i < eob_offset_bits; i++) { |
| eob_shift = eob_offset_bits - 1 - i; |
| bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
| aom_write_bit(w, bit); |
| } |
| #endif |
| } |
| |
| // write sec_tx_type here |
| // Only y plane's sec_tx_type is transmitted |
| if ((plane == AOM_PLANE_Y) && (cm->seq_params.enable_ist)) { |
| av1_write_sec_tx_type(cm, xd, tx_type, tx_size, eob, w); |
| } |
| |
| #if DEBUG_EXTQUANT |
| fprintf(cm->fEncCoeffLog, "tx_type=%d, eob=%d\n", tx_type, eob); |
| #endif |
| |
| #if !CONFIG_CONTEXT_DERIVATION |
| const int width = get_txb_wide(tx_size); |
| const int height = get_txb_high(tx_size); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| uint8_t levels_buf[TX_PAD_2D]; |
| uint8_t *const levels = set_levels(levels_buf, width); |
| const tran_low_t *tcoeff_txb = |
| cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane]; |
| const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block); |
| av1_txb_init_levels(tcoeff, width, height, levels); |
| const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
| const int16_t *const scan = scan_order->scan; |
| DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
| av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts |
| #if CONFIG_ATC_COEFCODING |
| , |
| plane |
| #endif // CONFIG_ATC_COEFCODING |
| ); |
| |
| const int bwl = get_txb_bwl(tx_size); |
| |
| #if CONFIG_PAR_HIDING |
| bool enable_parity_hiding = cm->features.allow_parity_hiding && |
| !xd->lossless[xd->mi[0]->segment_id] && |
| plane == PLANE_TYPE_Y && |
| get_primary_tx_type(tx_type) < IDTX; |
| for (int c = eob - 1; c > 0; --c) { |
| #else |
| for (int c = eob - 1; c >= 0; --c) { |
| #endif // CONFIG_PAR_HIDING |
| const int pos = scan[c]; |
| const int coeff_ctx = coeff_contexts[pos]; |
| const tran_low_t v = tcoeff[pos]; |
| const tran_low_t level = abs(v); |
| |
| #if CONFIG_ATC_COEFCODING |
| if (c == eob - 1) { |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| aom_write_symbol( |
| w, AOMMIN(level, LF_BASE_SYMBOLS - 1) - 1, |
| ec_ctx->coeff_base_lf_eob_cdf[txs_ctx][plane_type][coeff_ctx], |
| LF_BASE_SYMBOLS - 1); |
| } else { |
| aom_write_symbol( |
| w, AOMMIN(level, 3) - 1, |
| ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3); |
| } |
| } else { |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| aom_write_symbol( |
| w, AOMMIN(level, LF_BASE_SYMBOLS - 1), |
| ec_ctx->coeff_base_lf_cdf[txs_ctx][plane_type][coeff_ctx], |
| LF_BASE_SYMBOLS); |
| } else { |
| aom_write_symbol(w, AOMMIN(level, 3), |
| ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], |
| 4); |
| } |
| #else |
| if (c == eob - 1) { |
| aom_write_symbol( |
| w, AOMMIN(level, 3) - 1, |
| ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3); |
| } else { |
| aom_write_symbol(w, AOMMIN(level, 3), |
| ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], |
| 4); |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| #if CONFIG_ATC_COEFCODING |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| if (level > LF_NUM_BASE_LEVELS) { |
| const int base_range = |
| level - 1 - LF_NUM_BASE_LEVELS; // level is above 1. |
| const int br_ctx = get_br_lf_ctx(levels, pos, bwl, tx_class); |
| aom_cdf_prob *cdf = ec_ctx->coeff_br_lf_cdf[plane_type][br_ctx]; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| } else { |
| if (level > NUM_BASE_LEVELS) { |
| const int base_range = level - 1 - NUM_BASE_LEVELS; |
| const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| aom_cdf_prob *cdf = ec_ctx->coeff_br_cdf[plane_type][br_ctx]; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| } |
| #else |
| if (level > NUM_BASE_LEVELS) { |
| // level is above 1. |
| const int base_range = level - 1 - NUM_BASE_LEVELS; |
| const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| aom_cdf_prob *cdf = |
| ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx]; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| |
| #if CONFIG_PAR_HIDING |
| int num_nz = 0; |
| bool is_hidden = false; |
| if (enable_parity_hiding) { |
| for (int c = eob - 1; c > 0; --c) { |
| const int pos = scan[c]; |
| num_nz += !!tcoeff[pos]; |
| } |
| is_hidden = num_nz >= PHTHRESH; |
| } |
| if (is_hidden) { |
| const int pos = scan[0]; |
| const tran_low_t v = tcoeff[pos]; |
| const tran_low_t level = abs(v); |
| write_coeff_hidden(w, tx_class, scan, bwl, levels, level, |
| ec_ctx->coeff_base_ph_cdf, ec_ctx->coeff_br_ph_cdf); |
| } else { |
| const int c = 0; |
| const int pos = scan[c]; |
| const int coeff_ctx = coeff_contexts[pos]; |
| const tran_low_t v = tcoeff[pos]; |
| const tran_low_t level = abs(v); |
| |
| #if CONFIG_ATC_COEFCODING |
| if (c == eob - 1) { |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| aom_write_symbol( |
| w, AOMMIN(level, LF_BASE_SYMBOLS - 1) - 1, |
| ec_ctx->coeff_base_lf_eob_cdf[txs_ctx][plane_type][coeff_ctx], |
| LF_BASE_SYMBOLS - 1); |
| } else { |
| aom_write_symbol( |
| w, AOMMIN(level, 3) - 1, |
| ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3); |
| } |
| } else { |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| aom_write_symbol( |
| w, AOMMIN(level, LF_BASE_SYMBOLS - 1), |
| ec_ctx->coeff_base_lf_cdf[txs_ctx][plane_type][coeff_ctx], |
| LF_BASE_SYMBOLS); |
| } else { |
| aom_write_symbol(w, AOMMIN(level, 3), |
| ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], |
| 4); |
| } |
| #else |
| if (c == eob - 1) { |
| aom_write_symbol( |
| w, AOMMIN(level, 3) - 1, |
| ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3); |
| } else { |
| aom_write_symbol(w, AOMMIN(level, 3), |
| ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], |
| 4); |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| #if CONFIG_ATC_COEFCODING |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| if (level > LF_NUM_BASE_LEVELS) { |
| const int base_range = |
| level - 1 - LF_NUM_BASE_LEVELS; // level is above 1. |
| const int br_ctx = get_br_lf_ctx(levels, pos, bwl, tx_class); |
| aom_cdf_prob *cdf = ec_ctx->coeff_br_lf_cdf[plane_type][br_ctx]; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| } else { |
| if (level > NUM_BASE_LEVELS) { |
| const int base_range = level - 1 - NUM_BASE_LEVELS; |
| const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| aom_cdf_prob *cdf = ec_ctx->coeff_br_cdf[plane_type][br_ctx]; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| } |
| #else |
| if (level > NUM_BASE_LEVELS) { |
| // level is above 1. |
| const int base_range = level - 1 - NUM_BASE_LEVELS; |
| const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| aom_cdf_prob *cdf = |
| ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx]; |
| for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
| if (k < BR_CDF_SIZE - 1) break; |
| } |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| #endif // CONFIG_PAR_HIDING |
| |
| #if DEBUG_EXTQUANT |
| for (int c = 0; c < eob; ++c) { |
| const tran_low_t v = tcoeff[scan[c]]; |
| const tran_low_t level = abs(v); |
| fprintf(cm->fEncCoeffLog, "c=%d,pos=%d,level=%d,dq_coeff=%d\n", c, scan[c], |
| level, v); |
| } |
| #endif |
| |
| // Loop to code all signs in the transform block, |
| // starting with the sign of DC (if applicable) |
| for (int c = 0; c < eob; ++c) { |
| const tran_low_t v = tcoeff[scan[c]]; |
| const tran_low_t level = abs(v); |
| const int sign = (v < 0) ? 1 : 0; |
| if (level) { |
| if (c == 0) { |
| const int dc_sign_ctx = |
| (entropy_ctx[block] >> DC_SIGN_CTX_SHIFT) & DC_SIGN_CTX_MASK; |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_U) xd->tmp_sign[0] = (sign ? 2 : 1); |
| if (plane == AOM_PLANE_V) { |
| aom_write_symbol( |
| w, sign, ec_ctx->v_dc_sign_cdf[xd->tmp_sign[0]][dc_sign_ctx], 2); |
| } else { |
| aom_write_symbol(w, sign, |
| ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2); |
| } |
| #else |
| aom_write_symbol(w, sign, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], |
| 2); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| } else { |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_U) xd->tmp_sign[scan[c]] = (sign ? 2 : 1); |
| if (plane == AOM_PLANE_Y || plane == AOM_PLANE_U) |
| aom_write_bit(w, sign); |
| else |
| aom_write_symbol(w, sign, |
| ec_ctx->v_ac_sign_cdf[xd->tmp_sign[scan[c]]], 2); |
| #else |
| aom_write_bit(w, sign); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| } |
| #if CONFIG_PAR_HIDING |
| #if CONFIG_ATC_COEFCODING |
| if (is_hidden && c == 0) { |
| int q_index = level >> 1; |
| if (q_index > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| write_golomb(w, q_index - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
| } else { |
| const int pos = scan[c]; |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| if (level > COEFF_BASE_RANGE + LF_NUM_BASE_LEVELS) |
| write_golomb(w, level - COEFF_BASE_RANGE - 1 - LF_NUM_BASE_LEVELS); |
| } else { |
| if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
| } |
| } |
| #else |
| if (is_hidden && c == 0) { |
| int q_index = level >> 1; |
| if (q_index > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| write_golomb(w, q_index - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
| } else { |
| if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| #else |
| #if CONFIG_ATC_COEFCODING |
| const int pos = scan[c]; |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| if (level > COEFF_BASE_RANGE + LF_NUM_BASE_LEVELS) |
| write_golomb(w, level - COEFF_BASE_RANGE - 1 - LF_NUM_BASE_LEVELS); |
| } else { |
| if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
| } |
| #else |
| if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
| #endif // CONFIG_ATC_COEFCODING |
| #endif // CONFIG_PAR_HIDING |
| } |
| } |
| } |
| |
| typedef struct encode_txb_args { |
| const AV1_COMMON *cm; |
| MACROBLOCK *x; |
| aom_writer *w; |
| } ENCODE_TXB_ARGS; |
| |
| void av1_write_intra_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x, |
| aom_writer *w, BLOCK_SIZE bsize) { |
| MACROBLOCKD *xd = &x->e_mbd; |
| const MB_MODE_INFO *const mbmi = xd->mi[0]; |
| const int is_inter = is_inter_block(mbmi, xd->tree_type); |
| int block[MAX_MB_PLANE] = { 0 }; |
| int row, col; |
| assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x, |
| xd->plane[0].subsampling_y)); |
| const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
| const int max_blocks_high = max_block_high(xd, bsize, 0); |
| const BLOCK_SIZE max_unit_bsize = BLOCK_64X64; |
| int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
| int mu_blocks_high = mi_size_high[max_unit_bsize]; |
| mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide); |
| mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high); |
| |
| for (row = 0; row < max_blocks_high; row += mu_blocks_high) { |
| for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) { |
| const int plane_start = get_partition_plane_start(xd->tree_type); |
| const int plane_end = |
| get_partition_plane_end(xd->tree_type, av1_num_planes(cm)); |
| for (int plane = plane_start; plane < plane_end; ++plane) { |
| if (plane && !xd->is_chroma_ref) break; |
| #if CONFIG_CROSS_CHROMA_TX |
| if (plane == AOM_PLANE_U && is_cctx_allowed(cm, xd)) continue; |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const TX_SIZE tx_size = av1_get_tx_size(plane, xd); |
| const int stepr = tx_size_high_unit[tx_size]; |
| const int stepc = tx_size_wide_unit[tx_size]; |
| const int step = stepr * stepc; |
| const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| const int ss_x = pd->subsampling_x; |
| const int ss_y = pd->subsampling_y; |
| const BLOCK_SIZE plane_bsize = |
| get_mb_plane_block_size(xd, mbmi, plane, ss_x, ss_y); |
| const int plane_unit_height = |
| get_plane_tx_unit_height(xd, plane_bsize, plane, row, ss_y); |
| const int plane_unit_width = |
| get_plane_tx_unit_width(xd, plane_bsize, plane, col, ss_x); |
| for (int blk_row = row >> ss_y; blk_row < plane_unit_height; |
| blk_row += stepr) { |
| for (int blk_col = col >> ss_x; blk_col < plane_unit_width; |
| blk_col += stepc) { |
| #if CONFIG_CROSS_CHROMA_TX |
| // Loop order for the two chroma planes is changed for CCTX |
| // because the transform information for both planes are needed at |
| // once at the decoder side. |
| if (plane == AOM_PLANE_V && is_cctx_allowed(cm, xd)) { |
| const int code_rest = |
| av1_write_sig_txtype(cm, x, w, blk_row, blk_col, AOM_PLANE_U, |
| block[AOM_PLANE_U], tx_size); |
| if (code_rest) |
| av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, AOM_PLANE_U, |
| block[AOM_PLANE_U], tx_size); |
| block[AOM_PLANE_U] += step; |
| } |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const int code_rest = av1_write_sig_txtype( |
| cm, x, w, blk_row, blk_col, plane, block[plane], tx_size); |
| const TX_TYPE tx_type = |
| av1_get_tx_type(xd, get_plane_type(plane), blk_row, blk_col, |
| tx_size, cm->features.reduced_tx_set_used); |
| if (code_rest) { |
| if ((mbmi->fsc_mode[xd->tree_type == CHROMA_PART] && |
| get_primary_tx_type(tx_type) == IDTX && |
| plane == PLANE_TYPE_Y) || |
| use_inter_fsc(cm, plane, tx_type, is_inter)) { |
| av1_write_coeffs_txb_skip(cm, x, w, blk_row, blk_col, plane, |
| block[plane], tx_size); |
| } else { |
| av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane, |
| block[plane], tx_size); |
| } |
| } |
| block[plane] += step; |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| #if CONFIG_CROSS_CHROMA_TX |
| int get_cctx_type_cost(const AV1_COMMON *cm, const MACROBLOCK *x, |
| const MACROBLOCKD *xd, int plane, TX_SIZE tx_size, |
| int block, CctxType cctx_type) { |
| if (plane == AOM_PLANE_U && x->plane[plane].eobs[block] && |
| is_cctx_allowed(cm, xd)) { |
| const TX_SIZE square_tx_size = txsize_sqr_map[tx_size]; |
| int above_cctx, left_cctx; |
| #if CONFIG_EXT_RECUR_PARTITIONS |
| get_above_and_left_cctx_type(cm, xd, &above_cctx, &left_cctx); |
| #else |
| get_above_and_left_cctx_type(cm, xd, tx_size, &above_cctx, &left_cctx); |
| #endif // CONFIG_EXT_RECUR_PARTITIONS |
| const int cctx_ctx = get_cctx_context(xd, &above_cctx, &left_cctx); |
| return x->mode_costs.cctx_type_cost[square_tx_size][cctx_ctx][cctx_type]; |
| } else { |
| return 0; |
| } |
| } |
| #endif // CONFIG_CROSS_CHROMA_TX |
| |
| // TODO(angiebird): use this function whenever it's possible |
| static int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd, |
| int plane, TX_SIZE tx_size, TX_TYPE tx_type, |
| int reduced_tx_set_used, int eob) { |
| if (plane > 0) return 0; |
| |
| const TX_SIZE square_tx_size = txsize_sqr_map[tx_size]; |
| |
| const MB_MODE_INFO *mbmi = xd->mi[0]; |
| if (mbmi->fsc_mode[xd->tree_type == CHROMA_PART] && |
| !is_inter_block(mbmi, xd->tree_type) && plane == PLANE_TYPE_Y) { |
| return 0; |
| } |
| const int is_inter = is_inter_block(mbmi, xd->tree_type); |
| if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 && |
| !xd->lossless[xd->mi[0]->segment_id]) { |
| const int ext_tx_set = |
| get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used); |
| if (is_inter) { |
| if (ext_tx_set > 0) |
| return x->mode_costs |
| .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type]; |
| } else { |
| if (ext_tx_set > 0) { |
| PREDICTION_MODE intra_dir; |
| if (mbmi->filter_intra_mode_info.use_filter_intra) |
| intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info |
| .filter_intra_mode]; |
| else |
| intra_dir = mbmi->mode; |
| TX_TYPE primary_tx_type = get_primary_tx_type(tx_type); |
| int tx_type_cost = |
| x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size] |
| [intra_dir][primary_tx_type]; |
| if (block_signals_sec_tx_type(xd, tx_size, tx_type, eob) && |
| xd->enable_ist) { |
| tx_type_cost += |
| x->mode_costs.stx_flag_cost[square_tx_size] |
| [get_secondary_tx_type(tx_type)]; |
| } |
| return tx_type_cost; |
| } |
| } |
| } else if (!is_inter && !xd->lossless[xd->mi[0]->segment_id]) { |
| if (block_signals_sec_tx_type(xd, tx_size, tx_type, eob) && |
| xd->enable_ist) { |
| return x->mode_costs |
| .stx_flag_cost[square_tx_size][get_secondary_tx_type(tx_type)]; |
| } |
| } |
| return 0; |
| } |
| |
| static INLINE void update_coeff_eob_fast(int *eob, int shift, |
| const int32_t *dequant_ptr, |
| const int16_t *scan, |
| const tran_low_t *coeff_ptr, |
| tran_low_t *qcoeff_ptr, |
| tran_low_t *dqcoeff_ptr) { |
| // TODO(sarahparker) make this work for aomqm |
| int eob_out = *eob; |
| int zbin[2] = { dequant_ptr[0] + ROUND_POWER_OF_TWO(dequant_ptr[0] * 70, |
| 7 + QUANT_TABLE_BITS), |
| dequant_ptr[1] + ROUND_POWER_OF_TWO(dequant_ptr[1] * 70, |
| 7 + QUANT_TABLE_BITS) }; |
| for (int i = *eob - 1; i >= 0; i--) { |
| const int rc = scan[i]; |
| const int qcoeff = qcoeff_ptr[rc]; |
| const int coeff = coeff_ptr[rc]; |
| const int coeff_sign = AOMSIGN(coeff); |
| int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| |
| if (((abs_coeff << (1 + shift)) < zbin[rc != 0]) || (qcoeff == 0)) { |
| eob_out--; |
| qcoeff_ptr[rc] = 0; |
| dqcoeff_ptr[rc] = 0; |
| } else { |
| break; |
| } |
| } |
| |
| *eob = eob_out; |
| } |
| |
| static AOM_FORCE_INLINE int warehouse_efficients_txb_skip( |
| #if CONFIG_CROSS_CHROMA_TX |
| const AV1_COMMON *cm, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const MACROBLOCK *x, const int plane, const int block, |
| const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, |
| const struct macroblock_plane *p, const int eob, |
| const LV_MAP_COEFF_COST *const coeff_costs, const MACROBLOCKD *const xd, |
| const TX_TYPE tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| const CctxType cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| int reduced_tx_set_used) { |
| const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
| const int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| const int bwl = get_txb_bwl(tx_size); |
| const int width = get_txb_wide(tx_size); |
| const int height = get_txb_high(tx_size); |
| const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
| const int16_t *const scan = scan_order->scan; |
| uint8_t levels_buf[TX_PAD_2D]; |
| uint8_t *const levels = set_levels(levels_buf, width); |
| int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
| int8_t signs_buf[TX_PAD_2D]; |
| int8_t *const signs = set_signs(signs_buf, width); |
| av1_txb_init_levels_signs(qcoeff, width, height, levels_buf, signs_buf); |
| cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used, |
| eob); |
| #if CONFIG_CROSS_CHROMA_TX |
| cost += get_cctx_type_cost(cm, x, xd, plane, tx_size, block, cctx_type); |
| #endif // CONFIG_CROSS_CHROMA_TX |
| DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
| av1_get_nz_map_contexts_skip(levels, scan, eob, tx_size, coeff_contexts); |
| const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] = |
| coeff_costs->lps_cost_skip; |
| const int(*base_cost)[8] = coeff_costs->idtx_base_cost; |
| for (int c = 0; c < eob; c++) { |
| const int pos = scan[c]; |
| const int coeff_ctx = coeff_contexts[pos]; |
| const tran_low_t v = qcoeff[pos]; |
| const int level = abs(v); |
| cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
| if (v) { |
| if (level > NUM_BASE_LEVELS) { |
| const int ctx = get_br_ctx_skip(levels, pos, bwl); |
| cost += get_br_cost(level, lps_cost[ctx]); |
| } |
| } |
| } |
| for (int c = eob - 1; c >= 0; --c) { |
| const int pos = scan[c]; |
| const tran_low_t v = qcoeff[pos]; |
| const tran_low_t level = abs(v); |
| const int sign = (v < 0) ? 1 : 0; |
| if (level) { |
| int idtx_sign_ctx = get_sign_ctx_skip(signs, levels, pos, bwl); |
| cost += coeff_costs->idtx_sign_cost[idtx_sign_ctx][sign]; |
| } |
| } |
| return cost; |
| } |
| |
| static AOM_FORCE_INLINE int warehouse_efficients_txb( |
| #if CONFIG_CROSS_CHROMA_TX |
| const AV1_COMMON *cm, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const MACROBLOCK *x, const int plane, const int block, |
| const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, |
| const struct macroblock_plane *p, const int eob, |
| const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs, |
| const MACROBLOCKD *const xd, const TX_TYPE tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| const CctxType cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const TX_CLASS tx_class, int reduced_tx_set_used |
| #if CONFIG_PAR_HIDING |
| , |
| bool enable_parity_hiding, const LV_MAP_COEFF_COST *const coeff_costs_ph |
| #endif |
| ) { |
| const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
| #if CONFIG_CONTEXT_DERIVATION |
| const struct macroblock_plane *pu = &x->plane[AOM_PLANE_U]; |
| int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| if (plane == AOM_PLANE_V) { |
| txb_skip_ctx += (pu->eobs[block] ? V_TXB_SKIP_CONTEXT_OFFSET : 0); |
| } |
| #else |
| const int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| const int bwl = get_txb_bwl(tx_size); |
| const int width = get_txb_wide(tx_size); |
| const int height = get_txb_high(tx_size); |
| const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
| const int16_t *const scan = scan_order->scan; |
| uint8_t levels_buf[TX_PAD_2D]; |
| uint8_t *const levels = set_levels(levels_buf, width); |
| DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
| const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| const LV_MAP_EOB_COST *const eob_costs = |
| &x->coeff_costs.eob_costs[eob_multi_size][plane_type]; |
| #if CONFIG_CONTEXT_DERIVATION |
| int cost; |
| if (plane == AOM_PLANE_V) { |
| cost = coeff_costs->v_txb_skip_cost[txb_skip_ctx][0]; |
| } else { |
| cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
| } |
| #else |
| int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| |
| av1_txb_init_levels(qcoeff, width, height, levels); |
| |
| cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used, |
| eob); |
| #if CONFIG_CROSS_CHROMA_TX |
| cost += get_cctx_type_cost(cm, x, xd, plane, tx_size, block, cctx_type); |
| #endif // CONFIG_CROSS_CHROMA_TX |
| |
| cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class); |
| |
| av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts |
| #if CONFIG_ATC_COEFCODING |
| , |
| plane |
| #endif // CONFIG_ATC_COEFCODING |
| ); |
| |
| const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] = |
| coeff_costs->lps_cost; |
| #if CONFIG_ATC_COEFCODING |
| const int(*lps_lf_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] = |
| coeff_costs->lps_lf_cost; |
| #endif // CONFIG_ATC_COEFCODING |
| int c = eob - 1; |
| { |
| const int pos = scan[c]; |
| const tran_low_t v = qcoeff[pos]; |
| const int sign = AOMSIGN(v); |
| const int level = (v ^ sign) - sign; |
| const int coeff_ctx = coeff_contexts[pos]; |
| #if CONFIG_ATC_COEFCODING |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| cost += |
| coeff_costs->base_lf_eob_cost[coeff_ctx] |
| [AOMMIN(level, LF_BASE_SYMBOLS - 1) - 1]; |
| } else { |
| cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1]; |
| } |
| #else |
| cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1]; |
| #endif // CONFIG_ATC_COEFCODING |
| |
| if (v) { |
| // sign bit cost |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| if (level > LF_NUM_BASE_LEVELS) { |
| const int ctx = get_br_ctx_lf_eob(pos, tx_class); |
| cost += get_br_lf_cost(level, lps_lf_cost[ctx]); |
| } |
| } else { |
| if (level > NUM_BASE_LEVELS) { |
| const int ctx = 7; /* get_lf_ctx_eob */ |
| cost += get_br_cost(level, lps_cost[ctx]); |
| } |
| } |
| #else |
| if (level > NUM_BASE_LEVELS) { |
| const int ctx = get_br_ctx_eob(pos, bwl, tx_class); |
| cost += get_br_cost(level, lps_cost[ctx]); |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| if (c) { |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) { |
| const int sign01 = (sign ^ sign) - sign; |
| cost += coeff_costs->v_ac_sign_cost[xd->tmp_sign[pos]][sign01]; |
| } else { |
| cost += av1_cost_literal(1); |
| } |
| #else |
| cost += av1_cost_literal(1); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| } else { |
| const int sign01 = (sign ^ sign) - sign; |
| const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) { |
| cost += |
| coeff_costs->v_dc_sign_cost[xd->tmp_sign[0]][dc_sign_ctx][sign01]; |
| } else { |
| cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
| } |
| #else |
| cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| return cost; |
| } |
| } |
| } |
| #if CONFIG_ATC_COEFCODING |
| const int(*base_lf_cost)[LF_BASE_SYMBOLS * 2] = coeff_costs->base_lf_cost; |
| #endif // CONFIG_ATC_COEFCODING |
| const int(*base_cost)[8] = coeff_costs->base_cost; |
| for (c = eob - 2; c >= 1; --c) { |
| const int pos = scan[c]; |
| const int coeff_ctx = coeff_contexts[pos]; |
| const tran_low_t v = qcoeff[pos]; |
| const int level = abs(v); |
| #if CONFIG_ATC_COEFCODING |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| cost += base_lf_cost[coeff_ctx][AOMMIN(level, LF_BASE_SYMBOLS - 1)]; |
| } else { |
| cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
| } |
| #else |
| cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
| #endif // CONFIG_ATC_COEFCODING |
| if (v) { |
| // sign bit cost |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) { |
| const int sign = AOMSIGN(v); |
| const int sign01 = (sign ^ sign) - sign; |
| cost += coeff_costs->v_ac_sign_cost[xd->tmp_sign[pos]][sign01]; |
| } else { |
| cost += av1_cost_literal(1); |
| } |
| #else |
| cost += av1_cost_literal(1); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| if (level > LF_NUM_BASE_LEVELS) { |
| const int ctx = get_br_lf_ctx(levels, pos, bwl, tx_class); |
| cost += get_br_lf_cost(level, lps_lf_cost[ctx]); |
| } |
| } else { |
| if (level > NUM_BASE_LEVELS) { |
| const int ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| cost += get_br_cost(level, lps_cost[ctx]); |
| } |
| } |
| #else |
| if (level > NUM_BASE_LEVELS) { |
| const int ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| cost += get_br_cost(level, lps_cost[ctx]); |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| } |
| // c == 0 after previous loop |
| #if CONFIG_PAR_HIDING |
| int num_nz = 0; |
| for (c = eob - 1; c > 0; --c) { |
| const int pos = scan[c]; |
| num_nz += !!qcoeff[pos]; |
| } |
| c = 0; |
| if (num_nz >= PHTHRESH && enable_parity_hiding) { |
| const int pos = scan[c]; |
| const tran_low_t v = qcoeff[pos]; |
| const int level = abs(v); |
| const int q_index = level >> 1; |
| cost += coeff_costs_ph->base_ph_cost[get_base_ctx_ph( |
| levels, pos, bwl, tx_class)][AOMMIN(q_index, 3)]; |
| if (v) { |
| const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
| cost += coeff_costs->dc_sign_cost[dc_sign_ctx][v < 0]; |
| |
| if (q_index > NUM_BASE_LEVELS) { |
| const int ctx = get_par_br_ctx(levels, pos, bwl, tx_class); |
| cost += get_br_cost(q_index, coeff_costs_ph->lps_ph_cost[ctx]); |
| } |
| } |
| return cost; |
| } |
| #endif |
| { |
| const int pos = scan[c]; |
| const tran_low_t v = qcoeff[pos]; |
| const int coeff_ctx = coeff_contexts[pos]; |
| const int sign = AOMSIGN(v); |
| const int level = (v ^ sign) - sign; |
| #if CONFIG_ATC_COEFCODING |
| const int row = pos >> bwl; |
| const int col = pos - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| cost += base_lf_cost[coeff_ctx][AOMMIN(level, LF_BASE_SYMBOLS - 1)]; |
| } else { |
| cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
| } |
| #else |
| cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
| #endif // CONFIG_ATC_COEFCODING |
| |
| if (v) { |
| // sign bit cost |
| const int sign01 = (sign ^ sign) - sign; |
| const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) { |
| cost += |
| coeff_costs->v_dc_sign_cost[xd->tmp_sign[0]][dc_sign_ctx][sign01]; |
| } else { |
| cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
| } |
| #else |
| cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| if (level > LF_NUM_BASE_LEVELS) { |
| const int ctx = get_br_lf_ctx(levels, pos, bwl, tx_class); |
| cost += get_br_lf_cost(level, lps_lf_cost[ctx]); |
| } |
| } else { |
| if (level > NUM_BASE_LEVELS) { |
| const int ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| cost += get_br_cost(level, lps_cost[ctx]); |
| } |
| } |
| #else |
| if (level > NUM_BASE_LEVELS) { |
| const int ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| cost += get_br_cost(level, lps_cost[ctx]); |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| } |
| return cost; |
| } |
| |
| static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian( |
| const AV1_COMMON *cm, const MACROBLOCK *x, const int plane, const int block, |
| const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob, |
| const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs, |
| const MACROBLOCKD *const xd, const TX_TYPE tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| const CctxType cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const TX_CLASS tx_class, int reduced_tx_set_used) { |
| #if CONFIG_CONTEXT_DERIVATION |
| int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| if (plane == AOM_PLANE_V) { |
| txb_skip_ctx += |
| (x->plane[AOM_PLANE_U].eobs[block] ? V_TXB_SKIP_CONTEXT_OFFSET : 0); |
| } |
| #else |
| const int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| |
| const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| const LV_MAP_EOB_COST *const eob_costs = |
| &x->coeff_costs.eob_costs[eob_multi_size][plane_type]; |
| #if CONFIG_CONTEXT_DERIVATION |
| int cost; |
| if (plane == AOM_PLANE_V) { |
| cost = coeff_costs->v_txb_skip_cost[txb_skip_ctx][0]; |
| } else { |
| cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
| } |
| #else |
| int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| |
| cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used, |
| eob); |
| #if CONFIG_CROSS_CHROMA_TX |
| cost += get_cctx_type_cost(cm, x, xd, plane, tx_size, block, cctx_type); |
| #endif // CONFIG_CROSS_CHROMA_TX |
| |
| const MB_MODE_INFO *mbmi = xd->mi[0]; |
| if ((mbmi->fsc_mode[xd->tree_type == CHROMA_PART] && |
| get_primary_tx_type(tx_type) == IDTX && plane == PLANE_TYPE_Y) || |
| use_inter_fsc(cm, plane, tx_type, is_inter_block(mbmi, xd->tree_type))) { |
| cost += |
| av1_cost_coeffs_txb_skip_estimate(x, plane, block, tx_size, tx_type); |
| } else { |
| cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class); |
| cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type); |
| } |
| return cost; |
| } |
| |
| // Look up table of individual cost of coefficient by its quantization level. |
| // determined based on Laplacian distribution conditioned on estimated context |
| static const int costLUT[15] = { -1143, 53, 545, 825, 1031, |
| 1209, 1393, 1577, 1763, 1947, |
| 2132, 2317, 2501, 2686, 2871 }; |
| static const int const_term = (1 << AV1_PROB_COST_SHIFT); |
| static const int loge_par = ((14427 << AV1_PROB_COST_SHIFT) + 5000) / 10000; |
| int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane, |
| const int block, const TX_SIZE tx_size, |
| const TX_TYPE tx_type) { |
| assert(plane == 0); |
| |
| int cost = 0; |
| const struct macroblock_plane *p = &x->plane[plane]; |
| const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
| const int16_t *scan = scan_order->scan; |
| tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
| |
| int eob = p->eobs[block]; |
| |
| // coeffs |
| int c = eob - 1; |
| // eob |
| { |
| const int pos = scan[c]; |
| const tran_low_t v = abs(qcoeff[pos]) - 1; |
| cost += (v << (AV1_PROB_COST_SHIFT + 2)); |
| } |
| // other coeffs |
| for (c = eob - 2; c >= 0; c--) { |
| const int pos = scan[c]; |
| const tran_low_t v = abs(qcoeff[pos]); |
| const int idx = AOMMIN(v, 14); |
| |
| cost += costLUT[idx]; |
| } |
| |
| // const_term does not contain DC, and log(e) does not contain eob, so both |
| // (eob-1) |
| cost += (const_term + loge_par) * (eob - 1); |
| |
| return cost; |
| } |
| |
| int av1_cost_coeffs_txb_skip_estimate(const MACROBLOCK *x, const int plane, |
| const int block, const TX_SIZE tx_size, |
| const TX_TYPE tx_type) { |
| assert(plane == PLANE_TYPE_Y); |
| int cost = 0; |
| const struct macroblock_plane *p = &x->plane[plane]; |
| const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
| const int16_t *scan = scan_order->scan; |
| tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
| int eob = p->eobs[block]; |
| assert(eob == av1_get_max_eob(tx_size)); |
| // coeffs |
| for (int c = 0; c < eob; c++) { |
| const int pos = scan[c]; |
| const tran_low_t v = abs(qcoeff[pos]); |
| const int idx = AOMMIN(v, 14); |
| cost += costLUT[idx]; |
| } |
| cost += (const_term + loge_par) * (eob - 1); |
| return cost; |
| } |
| |
| int av1_cost_coeffs_txb(const AV1_COMMON *cm, const MACROBLOCK *x, |
| const int plane, const int block, const TX_SIZE tx_size, |
| const TX_TYPE tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| const CctxType cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const TXB_CTX *const txb_ctx, int reduced_tx_set_used) { |
| const struct macroblock_plane *p = &x->plane[plane]; |
| const int eob = p->eobs[block]; |
| const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
| const PLANE_TYPE plane_type = get_plane_type(plane); |
| const LV_MAP_COEFF_COST *const coeff_costs = |
| &x->coeff_costs.coeff_costs[txs_ctx][plane_type]; |
| const MACROBLOCKD *const xd = &x->e_mbd; |
| if (eob == 0) { |
| int skip_cost = 0; |
| #if CONFIG_CROSS_CHROMA_TX && CCTX_C2_DROPPED |
| if (plane == AOM_PLANE_V && !keep_chroma_c2(cctx_type) && |
| is_cctx_allowed(cm, xd)) |
| return 0; |
| #endif // CONFIG_CROSS_CHROMA_TX && CCTX_C2_DROPPED |
| #if CONFIG_CONTEXT_DERIVATION |
| int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| if (plane == AOM_PLANE_Y || plane == AOM_PLANE_U) { |
| skip_cost += coeff_costs->txb_skip_cost[txb_skip_ctx][1]; |
| } else { |
| txb_skip_ctx += |
| (x->plane[AOM_PLANE_U].eobs[block] ? V_TXB_SKIP_CONTEXT_OFFSET : 0); |
| skip_cost += coeff_costs->v_txb_skip_cost[txb_skip_ctx][1]; |
| } |
| #else |
| skip_cost += coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CROSS_CHROMA_TX |
| skip_cost += |
| get_cctx_type_cost(cm, x, xd, plane, tx_size, block, cctx_type); |
| #endif // CONFIG_CROSS_CHROMA_TX |
| return skip_cost; |
| } |
| |
| const TX_CLASS tx_class = tx_type_to_class[get_primary_tx_type(tx_type)]; |
| #if CONFIG_PAR_HIDING |
| bool enable_parity_hiding = cm->features.allow_parity_hiding && |
| !xd->lossless[xd->mi[0]->segment_id] && |
| plane == PLANE_TYPE_Y && |
| get_primary_tx_type(tx_type) < IDTX; |
| #endif // CONFIG_PAR_HIDING |
| |
| const MB_MODE_INFO *mbmi = xd->mi[0]; |
| if ((mbmi->fsc_mode[xd->tree_type == CHROMA_PART] && |
| get_primary_tx_type(tx_type) == IDTX && plane == PLANE_TYPE_Y) || |
| use_inter_fsc(cm, plane, tx_type, is_inter_block(mbmi, xd->tree_type))) { |
| return warehouse_efficients_txb_skip( |
| #if CONFIG_CROSS_CHROMA_TX |
| cm, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| x, plane, block, tx_size, txb_ctx, p, eob, coeff_costs, xd, tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| reduced_tx_set_used); |
| } else { |
| return warehouse_efficients_txb( |
| #if CONFIG_CROSS_CHROMA_TX |
| cm, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| x, plane, block, tx_size, txb_ctx, p, eob, plane_type, coeff_costs, xd, |
| tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| tx_class, reduced_tx_set_used |
| #if CONFIG_PAR_HIDING |
| , |
| enable_parity_hiding, &x->coeff_costs.coeff_costs[0][0] |
| #endif // CONFIG_PAR_HIDING |
| ); |
| } |
| } |
| |
| int av1_cost_coeffs_txb_laplacian(const AV1_COMMON *cm, const MACROBLOCK *x, |
| const int plane, const int block, |
| const TX_SIZE tx_size, const TX_TYPE tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| const CctxType cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| const TXB_CTX *const txb_ctx, |
| const int reduced_tx_set_used, |
| const int adjust_eob) { |
| const struct macroblock_plane *p = &x->plane[plane]; |
| int eob = p->eobs[block]; |
| |
| if (adjust_eob) { |
| const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
| const int16_t *scan = scan_order->scan; |
| tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block); |
| tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
| tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block); |
| update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan, |
| tcoeff, qcoeff, dqcoeff); |
| p->eobs[block] = eob; |
| } |
| |
| const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
| const PLANE_TYPE plane_type = get_plane_type(plane); |
| const LV_MAP_COEFF_COST *const coeff_costs = |
| &x->coeff_costs.coeff_costs[txs_ctx][plane_type]; |
| const MACROBLOCKD *const xd = &x->e_mbd; |
| if (eob == 0) { |
| int skip_cost = 0; |
| #if CONFIG_CONTEXT_DERIVATION |
| int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| if (plane == AOM_PLANE_Y || plane == AOM_PLANE_U) { |
| skip_cost += coeff_costs->txb_skip_cost[txb_skip_ctx][1]; |
| } else { |
| txb_skip_ctx += |
| (x->plane[AOM_PLANE_U].eobs[block] ? V_TXB_SKIP_CONTEXT_OFFSET : 0); |
| skip_cost += coeff_costs->v_txb_skip_cost[txb_skip_ctx][1]; |
| } |
| #else |
| skip_cost += coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CROSS_CHROMA_TX |
| skip_cost += |
| get_cctx_type_cost(cm, x, xd, plane, tx_size, block, cctx_type); |
| #endif // CONFIG_CROSS_CHROMA_TX |
| return skip_cost; |
| } |
| |
| const TX_CLASS tx_class = tx_type_to_class[get_primary_tx_type(tx_type)]; |
| |
| return warehouse_efficients_txb_laplacian(cm, x, plane, block, tx_size, |
| txb_ctx, eob, plane_type, |
| coeff_costs, xd, tx_type, |
| #if CONFIG_CROSS_CHROMA_TX |
| cctx_type, |
| #endif // CONFIG_CROSS_CHROMA_TX |
| tx_class, reduced_tx_set_used); |
| } |
| |
| static AOM_FORCE_INLINE int get_two_coeff_cost_simple( |
| #if CONFIG_ATC_COEFCODING |
| int plane, |
| #endif // CONFIG_ATC_COEFCODING |
| int ci, tran_low_t abs_qc, int coeff_ctx, |
| const LV_MAP_COEFF_COST *txb_costs, int bwl, TX_CLASS tx_class, |
| const uint8_t *levels, int *cost_low) { |
| // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0) |
| // and not the last (scan_idx != eob - 1) |
| assert(ci > 0); |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int cost = 0; |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| cost += |
| txb_costs->base_lf_cost[coeff_ctx][AOMMIN(abs_qc, LF_BASE_SYMBOLS - 1)]; |
| } else { |
| cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
| } |
| #else |
| int cost = txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
| #endif // CONFIG_ATC_COEFCODING |
| int diff = 0; |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| if (abs_qc <= (LF_BASE_SYMBOLS - 1)) { |
| if (abs_qc == 0) { |
| diff = 0; |
| } else if (abs_qc == 1) { |
| diff = txb_costs->base_lf_cost[coeff_ctx][1] + av1_cost_literal(1) - |
| txb_costs->base_lf_cost[coeff_ctx][0]; |
| } else if (abs_qc == 2) { |
| diff = txb_costs->base_lf_cost[coeff_ctx][2] - |
| txb_costs->base_lf_cost[coeff_ctx][1]; |
| } else if (abs_qc == 3) { |
| diff = txb_costs->base_lf_cost[coeff_ctx][3] - |
| txb_costs->base_lf_cost[coeff_ctx][2]; |
| } else if (abs_qc == 4) { |
| diff = txb_costs->base_lf_cost[coeff_ctx][4] - |
| txb_costs->base_lf_cost[coeff_ctx][3]; |
| } else { |
| diff = txb_costs->base_lf_cost[coeff_ctx][5] - |
| txb_costs->base_lf_cost[coeff_ctx][4]; |
| } |
| } |
| } else { |
| if (abs_qc <= 3) { |
| if (abs_qc == 0) { |
| diff = 0; |
| } else if (abs_qc == 1) { |
| diff = txb_costs->base_cost[coeff_ctx][1] + av1_cost_literal(1) - |
| txb_costs->base_cost[coeff_ctx][0]; |
| } else if (abs_qc == 2) { |
| diff = txb_costs->base_cost[coeff_ctx][2] - |
| txb_costs->base_cost[coeff_ctx][1]; |
| } else { |
| diff = txb_costs->base_cost[coeff_ctx][3] - |
| txb_costs->base_cost[coeff_ctx][2]; |
| } |
| } |
| } |
| #else |
| if (abs_qc <= 3) diff = txb_costs->base_cost[coeff_ctx][abs_qc + 4]; |
| #endif // CONFIG_ATC_COEFCODING |
| if (abs_qc) { |
| cost += av1_cost_literal(1); |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| if (abs_qc > LF_NUM_BASE_LEVELS) { |
| const int br_ctx = get_br_lf_ctx(levels, ci, bwl, tx_class); |
| int brcost_diff = 0; |
| cost += get_br_lf_cost_with_diff(abs_qc, txb_costs->lps_lf_cost[br_ctx], |
| &brcost_diff); |
| diff += brcost_diff; |
| } |
| } else { |
| if (abs_qc > NUM_BASE_LEVELS) { |
| const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
| int brcost_diff = 0; |
| cost += get_br_cost_with_diff(abs_qc, txb_costs->lps_cost[br_ctx], |
| &brcost_diff); |
| diff += brcost_diff; |
| } |
| } |
| #else |
| if (abs_qc > NUM_BASE_LEVELS) { |
| const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
| int brcost_diff = 0; |
| cost += get_br_cost_with_diff(abs_qc, txb_costs->lps_cost[br_ctx], |
| &brcost_diff); |
| diff += brcost_diff; |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| *cost_low = cost - diff; |
| |
| return cost; |
| } |
| |
| static INLINE int get_coeff_cost_eob(int ci, tran_low_t abs_qc, int sign, |
| int coeff_ctx, int dc_sign_ctx, |
| const LV_MAP_COEFF_COST *txb_costs, |
| int bwl, TX_CLASS tx_class |
| #if CONFIG_CONTEXT_DERIVATION |
| , |
| int32_t *tmp_sign |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| , |
| int plane |
| #endif // CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| ) { |
| int cost = 0; |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| cost += |
| txb_costs->base_lf_eob_cost[coeff_ctx] |
| [AOMMIN(abs_qc, LF_BASE_SYMBOLS - 1) - 1]; |
| } else { |
| cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
| } |
| #else |
| cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
| #endif // CONFIG_ATC_COEFCODING |
| if (abs_qc != 0) { |
| if (ci == 0) { |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) |
| cost += txb_costs->v_dc_sign_cost[tmp_sign[0]][dc_sign_ctx][sign]; |
| else |
| cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
| #else |
| cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| } else { |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) |
| cost += txb_costs->v_ac_sign_cost[tmp_sign[ci]][sign]; |
| else |
| cost += av1_cost_literal(1); |
| #else |
| cost += av1_cost_literal(1); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| } |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| if (abs_qc > LF_NUM_BASE_LEVELS) { |
| int br_ctx; |
| br_ctx = get_br_ctx_lf_eob(ci, tx_class); |
| cost += get_br_lf_cost(abs_qc, txb_costs->lps_lf_cost[br_ctx]); |
| } |
| } else { |
| if (abs_qc > NUM_BASE_LEVELS) { |
| int br_ctx = 7; /* get_br_ctx_eob */ |
| cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
| } |
| } |
| #else |
| if (abs_qc > NUM_BASE_LEVELS) { |
| int br_ctx; |
| br_ctx = get_br_ctx_eob(ci, bwl, tx_class); |
| cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| return cost; |
| } |
| |
| static INLINE int get_coeff_cost_general(int is_last, int ci, tran_low_t abs_qc, |
| int sign, int coeff_ctx, |
| int dc_sign_ctx, |
| const LV_MAP_COEFF_COST *txb_costs, |
| int bwl, TX_CLASS tx_class, |
| const uint8_t *levels |
| #if CONFIG_CONTEXT_DERIVATION |
| , |
| int32_t *tmp_sign |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| , |
| int plane |
| #endif // CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| ) { |
| int cost = 0; |
| if (is_last) { |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| cost += |
| txb_costs->base_lf_eob_cost[coeff_ctx] |
| [AOMMIN(abs_qc, LF_BASE_SYMBOLS - 1) - 1]; |
| } else { |
| cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
| } |
| #else |
| cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
| #endif // CONFIG_ATC_COEFCODING |
| } else { |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| cost += |
| txb_costs |
| ->base_lf_cost[coeff_ctx][AOMMIN(abs_qc, LF_BASE_SYMBOLS - 1)]; |
| } else { |
| cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
| } |
| #else |
| cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| if (abs_qc != 0) { |
| if (ci == 0) { |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) |
| cost += txb_costs->v_dc_sign_cost[tmp_sign[0]][dc_sign_ctx][sign]; |
| else |
| cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
| #else |
| cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
| #endif // CONFIG_CONTEXT_DERIVATION |
| } else { |
| #if CONFIG_CONTEXT_DERIVATION |
| if (plane == AOM_PLANE_V) |
| cost += txb_costs->v_ac_sign_cost[tmp_sign[ci]][sign]; |
| else |
| cost += av1_cost_literal(1); |
| #else |
| cost += av1_cost_literal(1); |
| #endif // CONFIG_CONTEXT_DERIVATION |
| } |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| if (abs_qc > LF_NUM_BASE_LEVELS) { |
| int br_ctx; |
| if (is_last) |
| br_ctx = get_br_ctx_lf_eob(ci, tx_class); |
| else |
| br_ctx = get_br_lf_ctx(levels, ci, bwl, tx_class); |
| cost += get_br_lf_cost(abs_qc, txb_costs->lps_lf_cost[br_ctx]); |
| } |
| } else { |
| if (abs_qc > NUM_BASE_LEVELS) { |
| int br_ctx; |
| if (is_last) |
| br_ctx = 0; /*get_br_ctx_eob*/ |
| else |
| br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
| cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
| } |
| } |
| #else |
| if (abs_qc > NUM_BASE_LEVELS) { |
| int br_ctx; |
| if (is_last) |
| br_ctx = get_br_ctx_eob(ci, bwl, tx_class); |
| else |
| br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
| cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
| } |
| #endif // CONFIG_ATC_COEFCODING |
| } |
| return cost; |
| } |
| |
| static INLINE void get_qc_dqc_low(tran_low_t abs_qc, int sign, int dqv, |
| int shift, tran_low_t *qc_low, |
| tran_low_t *dqc_low) { |
| tran_low_t abs_qc_low = abs_qc - 1; |
| *qc_low = (-sign ^ abs_qc_low) + sign; |
| assert((sign ? -abs_qc_low : abs_qc_low) == *qc_low); |
| |
| tran_low_t abs_dqc_low = |
| (tran_low_t)(ROUND_POWER_OF_TWO_64((tran_high_t)abs_qc_low * dqv, |
| QUANT_TABLE_BITS) >> |
| shift); |
| |
| *dqc_low = (-sign ^ abs_dqc_low) + sign; |
| assert((sign ? -abs_dqc_low : abs_dqc_low) == *dqc_low); |
| } |
| |
| static INLINE void update_coeff_general( |
| int *accu_rate, int64_t *accu_dist, int si, int eob, |
| #if !CONFIG_ATC_COEFCODING |
| TX_SIZE tx_size, |
| #endif // !CONFIG_ATC_COEFCODING |
| TX_CLASS tx_class, int bwl, int height, int64_t rdmult, int shift, |
| int dc_sign_ctx, const int32_t *dequant, const int16_t *scan, |
| const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff, |
| tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, |
| const qm_val_t *iqmatrix |
| #if CONFIG_CONTEXT_DERIVATION |
| , |
| int32_t *tmp_sign |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| , |
| int plane |
| #endif // CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| #if CONFIG_PAR_HIDING |
| , |
| coeff_info *coef_info, bool enable_parity_hiding |
| #endif // CONFIG_PAR_HIDING |
| ) { |
| const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
| const int ci = scan[si]; |
| const tran_low_t qc = qcoeff[ci]; |
| const int is_last = si == (eob - 1); |
| const int coeff_ctx = |
| get_lower_levels_ctx_general(is_last, si, bwl, height, levels, ci, |
| #if !CONFIG_ATC_COEFCODING |
| tx_size, |
| #endif // !CONFIG_ATC_COEFCODING |
| tx_class |
| #if CONFIG_ATC_COEFCODING |
| , |
| plane |
| #endif // CONFIG_ATC_COEFCODING |
| ); |
| if (qc == 0) { |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| *accu_rate += txb_costs->base_lf_cost[coeff_ctx][0]; |
| } else { |
| *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| } |
| #else |
| *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| #endif // CONFIG_ATC_COEFCODING |
| } else { |
| const int sign = (qc < 0) ? 1 : 0; |
| const tran_low_t abs_qc = abs(qc); |
| const tran_low_t tqc = tcoeff[ci]; |
| const tran_low_t dqc = dqcoeff[ci]; |
| const int64_t dist = get_coeff_dist(tqc, dqc, shift); |
| const int64_t dist0 = get_coeff_dist(tqc, 0, shift); |
| const int rate = |
| get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx, |
| dc_sign_ctx, txb_costs, bwl, tx_class, levels |
| #if CONFIG_CONTEXT_DERIVATION |
| , |
| tmp_sign |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| , |
| plane |
| #endif // CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| ); |
| const int64_t rd = RDCOST(rdmult, rate, dist); |
| |
| tran_low_t qc_low, dqc_low; |
| tran_low_t abs_qc_low; |
| int64_t dist_low, rd_low; |
| int rate_low; |
| if (abs_qc == 1) { |
| abs_qc_low = qc_low = dqc_low = 0; |
| dist_low = dist0; |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| if (limits) { |
| rate_low = txb_costs->base_lf_cost[coeff_ctx][0]; |
| } else { |
| rate_low = txb_costs->base_cost[coeff_ctx][0]; |
| } |
| #else |
| rate_low = txb_costs->base_cost[coeff_ctx][0]; |
| #endif // CONFIG_ATC_COEFCODING |
| } else { |
| get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low); |
| abs_qc_low = abs_qc - 1; |
| dist_low = get_coeff_dist(tqc, dqc_low, shift); |
| rate_low = |
| get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx, |
| dc_sign_ctx, txb_costs, bwl, tx_class, levels |
| #if CONFIG_CONTEXT_DERIVATION |
| , |
| tmp_sign |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| , |
| plane |
| #endif // CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| ); |
| } |
| |
| rd_low = RDCOST(rdmult, rate_low, dist_low); |
| if (rd_low < rd) { |
| qcoeff[ci] = qc_low; |
| dqcoeff[ci] = dqc_low; |
| levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX); |
| *accu_rate += rate_low; |
| *accu_dist += dist_low - dist0; |
| #if CONFIG_PAR_HIDING |
| if (enable_parity_hiding) |
| set_coeff_info(qc_low, dqc_low, qc, dqc, rd_low, rd, rate_low, rate, |
| false, coef_info, si); |
| #endif // CONFIG_PAR_HIDING |
| } else { |
| *accu_rate += rate; |
| *accu_dist += dist - dist0; |
| #if CONFIG_PAR_HIDING |
| if (enable_parity_hiding) |
| set_coeff_info(qc_low, dqc_low, qc, dqc, rd_low, rd, rate_low, rate, |
| true, coef_info, si); |
| #endif // CONFIG_PAR_HIDING |
| } |
| } |
| } |
| |
| static AOM_FORCE_INLINE void update_coeff_simple( |
| int *accu_rate, int si, int eob, |
| #if !CONFIG_ATC_COEFCODING |
| TX_SIZE tx_size, |
| #endif // !CONFIG_ATC_COEFCODING |
| TX_CLASS tx_class, int bwl, int64_t rdmult, int shift, |
| const int32_t *dequant, const int16_t *scan, |
| const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff, |
| tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, |
| const qm_val_t *iqmatrix |
| #if CONFIG_PAR_HIDING |
| , |
| coeff_info *coef_info, bool enable_parity_hiding |
| #endif // CONFIG_PAR_HIDING |
| #if CONFIG_ATC_COEFCODING |
| , |
| int plane) { |
| #else |
| ) { |
| #endif // CONFIG_ATC_COEFCODING |
| const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
| (void)eob; |
| // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0) |
| // and not the last (scan_idx != eob - 1) |
| assert(si != eob - 1); |
| assert(si > 0); |
| const int ci = scan[si]; |
| const tran_low_t qc = qcoeff[ci]; |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| int coeff_ctx = 0; |
| if (limits) { |
| coeff_ctx = get_lower_levels_lf_ctx(levels, ci, bwl, tx_class); |
| } else { |
| coeff_ctx = get_lower_levels_ctx(levels, ci, bwl, tx_class); |
| } |
| #else |
| const int coeff_ctx = |
| get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class); |
| #endif // CONFIG_ATC_COEFCODING |
| if (qc == 0) { |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| *accu_rate += txb_costs->base_lf_cost[coeff_ctx][0]; |
| } else { |
| *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| } |
| #else |
| *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| #endif // CONFIG_ATC_COEFCODING |
| } else { |
| const tran_low_t abs_qc = abs(qc); |
| const tran_low_t abs_tqc = abs(tcoeff[ci]); |
| const tran_low_t abs_dqc = abs(dqcoeff[ci]); |
| int rate_low = 0; |
| const int rate = get_two_coeff_cost_simple( |
| #if CONFIG_ATC_COEFCODING |
| plane, |
| #endif // CONFIG_ATC_COEFCODING |
| ci, abs_qc, coeff_ctx, txb_costs, bwl, tx_class, levels, &rate_low); |
| if (abs_dqc < abs_tqc) { |
| *accu_rate += rate; |
| return; |
| } |
| |
| const int64_t dist = get_coeff_dist(abs_tqc, abs_dqc, shift); |
| const int64_t rd = RDCOST(rdmult, rate, dist); |
| |
| const tran_low_t abs_qc_low = abs_qc - 1; |
| const tran_low_t abs_dqc_low = |
| (tran_low_t)ROUND_POWER_OF_TWO_64((tran_high_t)abs_qc_low * dqv, |
| QUANT_TABLE_BITS) >> |
| shift; |
| const int64_t dist_low = get_coeff_dist(abs_tqc, abs_dqc_low, shift); |
| const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low); |
| |
| if (rd_low < rd) { |
| #if CONFIG_PAR_HIDING |
| tran_low_t qc_low = qc < 0 ? -abs_qc_low : abs_qc_low; |
| tran_low_t dqc_low = qc < 0 ? -abs_dqc_low : abs_dqc_low; |
| if (enable_parity_hiding) |
| set_coeff_info(qc_low, dqc_low, qc, dqcoeff[ci], rd_low, rd, rate_low, |
| rate, false, coef_info, si); |
| qcoeff[ci] = qc_low; |
| dqcoeff[ci] = dqc_low; |
| #else |
| const int sign = (qc < 0) ? 1 : 0; |
| qcoeff[ci] = (-sign ^ abs_qc_low) + sign; |
| dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign; |
| #endif // CONFIG_PAR_HIDING |
| levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX); |
| *accu_rate += rate_low; |
| } else { |
| *accu_rate += rate; |
| #if CONFIG_PAR_HIDING |
| tran_low_t qc_low = qc < 0 ? -abs_qc_low : abs_qc_low; |
| tran_low_t dqc_low = qc < 0 ? -abs_dqc_low : abs_dqc_low; |
| if (enable_parity_hiding) |
| set_coeff_info(qc_low, dqc_low, qc, dqcoeff[ci], rd_low, rd, rate_low, |
| rate, true, coef_info, si); |
| #endif // CONFIG_PAR_HIDING |
| } |
| } |
| } |
| |
| static AOM_FORCE_INLINE void update_coeff_eob( |
| int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci, |
| int si, |
| #if !CONFIG_ATC_COEFCODING |
| TX_SIZE tx_size, |
| #endif // !CONFIG_ATC_COEFCODING |
| TX_CLASS tx_class, int bwl, int height, int dc_sign_ctx, int64_t rdmult, |
| int shift, const int32_t *dequant, const int16_t *scan, |
| const LV_MAP_EOB_COST *txb_eob_costs, const LV_MAP_COEFF_COST *txb_costs, |
| const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff, |
| uint8_t *levels, int sharpness, const qm_val_t *iqmatrix |
| #if CONFIG_CONTEXT_DERIVATION |
| , |
| int32_t *tmp_sign |
| #endif // CONFIG_CONTEXT_DERIVATION |
| #if CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| , |
| int plane |
| #endif // CONFIG_CONTEXT_DERIVATION || CONFIG_ATC_COEFCODING |
| #if CONFIG_PAR_HIDING |
| , |
| coeff_info *coef_info, bool enable_parity_hiding |
| #endif // CONFIG_PAR_HIDING |
| ) { |
| const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
| assert(si != *eob - 1); |
| const int ci = scan[si]; |
| const tran_low_t qc = qcoeff[ci]; |
| #if CONFIG_ATC_COEFCODING |
| const int row = ci >> bwl; |
| const int col = ci - (row << bwl); |
| int limits = get_lf_limits(row, col, tx_class, plane); |
| int coeff_ctx = 0; |
| if (limits) { |
| coeff_ctx = get_lower_levels_lf_ctx(levels, ci, bwl, tx_class); |
| } else { |
| coeff_ctx = get_lower_levels_ctx(levels, ci, bwl, tx_class); |
| } |
| #else |
| const int coeff_ctx = |
| get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class); |
| #endif // CONFIG_ATC_COEFCODING |
| if (qc == 0) { |
| #if CONFIG_ATC_COEFCODING |
| if (limits) { |
| *accu_rate += txb_costs->base_lf_cost[coeff_ctx][0]; |
| } else { |
| *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| } |
| #else |
| *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| #endif // CONFIG_ATC_COEFCODING |
| } else { |
| #if CONFIG_PAR_HIDING |
| int64_t rd_eob_low = INT64_MAX >> 1; |
| int rate_eob_low = INT32_MAX >> 1; |
| #endif // CONFIG_PAR_HIDING |
| int lower_level = 0; |
| const tran_low_t abs_qc = abs(qc); |
| const tran_low_t tqc = tcoeff[ci]; |
| const tran_low_t dqc = dqcoeff[ci]; |
| const int sign = (qc < 0) ? 1 : 0; |
| const int64_t dist0 = get_coeff_dist(tqc, 0, shift); |
| int64_t dist = get_coeff_dist(tqc, dqc, shift) - dist0; |
| int rate = |
| get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx, |
| txb_cos
|