blob: 35386888a9d06907addf2311e3bed4eb00d0f4c0 [file] [log] [blame]
/*
* 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/decoder/decodetxb.h"
#include "aom_ports/mem.h"
#include "av1/common/idct.h"
#include "av1/common/pred_common.h"
#include "av1/common/scan.h"
#include "av1/common/txb_common.h"
#include "av1/common/reconintra.h"
#include "av1/decoder/decodemv.h"
static int read_golomb(MACROBLOCKD *xd, aom_reader *r) {
int x = 1;
int length = 0;
#if CONFIG_BYPASS_IMPROVEMENT
length = aom_read_unary(r, 21, ACCT_INFO("length"));
if (length > 20) {
aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
"Invalid length in read_golomb");
}
x = 1 << length;
x += aom_read_literal(r, length, ACCT_INFO());
#else
int i = 0;
while (!i) {
i = aom_read_bit(r, ACCT_INFO());
++length;
if (length > 20) {
aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
"Invalid length in read_golomb");
break;
}
}
for (i = 0; i < length - 1; ++i) {
x <<= 1;
x += aom_read_bit(r, ACCT_INFO());
}
#endif // CONFIG_BYPASS_IMPROVEMENT
return x - 1;
}
static INLINE int read_high_range(MACROBLOCKD *xd, aom_reader *r, int tcq_mode,
int level, int lf) {
int max_br = lf ? LF_MAX_BASE_BR_RANGE : MAX_BASE_BR_RANGE;
#if NEWHR
int use_tcq_hr = tcq_mode && (level >= max_br - 1);
#else
(void)tcq_mode;
int use_tcq_hr = 0;
#endif
if (use_tcq_hr) {
level += 2 * read_golomb(xd, r);
} else if (level >= max_br) {
level += read_golomb(xd, r);
}
return level;
}
static INLINE int rec_eob_pos(const int eob_token, const int extra) {
int eob = av1_eob_group_start[eob_token];
if (eob > 2) {
eob += extra;
}
return eob;
}
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;
}
static int read_low_range(aom_reader *r, aom_cdf_prob *cdf
#if CONFIG_COEFF_LOGS
,
qcoeff_bit_cost *qcoeff_cost, uint64_t *prev_bits
#endif
) {
int br_level = 0;
for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
const int k =
aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_INFO("k", "br_lf_uv_cdf"));
br_level += k;
#if CONFIG_COEFF_LOGS
if (qcoeff_cost) {
int lr = idx / (BR_CDF_SIZE - 1);
qcoeff_cost->lr_cost[lr] = aom_reader_tell_frac(r) - *prev_bits;
*prev_bits += qcoeff_cost->lr_cost[lr];
}
#endif // CONFIG_COEFF_LOGS
if (k < BR_CDF_SIZE - 1) break;
}
return br_level;
}
static INLINE void read_coeffs_reverse_2d(
aom_reader *r, int start_si, int end_si, const int16_t *scan, int bwl,
uint8_t *levels, base_lf_cdf_arr base_lf_cdf, br_cdf_arr br_lf_cdf,
int plane, base_cdf_arr base_cdf, br_cdf_arr br_cdf
#if CONFIG_LCCHROMA
,
base_lf_cdf_arr base_lf_uv_cdf, br_cdf_arr br_lf_uv_cdf,
base_cdf_arr base_uv_cdf, br_cdf_arr br_uv_cdf
#endif // CONFIG_LCCHROMA
#if CONFIG_DQ
#if !CONFIG_LCCHROMA
,
base_lf_cdf_arr base_lf_cdf_tcq, base_cdf_arr base_cdf_tcq
#endif
,
int *state
#if DQENABLE
,
const TX_SIZE tx_size
#endif
#endif
#if CONFIG_COEFF_LOGS
,
qcoeff_bit_cost *qcoeff_cost
#endif // CONFIG_COEFF_LOGS
) {
#if CONFIG_COEFF_LOGS
uint64_t prev_bits = aom_reader_tell_frac(r);
#endif // CONFIG_COEFF_LOGS
for (int c = end_si; c >= start_si; --c) {
const int pos = scan[c];
int level = 0;
const int row = pos >> bwl;
const int col = pos - (row << bwl);
int limits = get_lf_limits(row, col, 0, plane);
#if CONFIG_DQ
int dq = tcq_quant(*state);
#endif
#if CONFIG_LCCHROMA
if (plane > 0) {
if (limits) {
const int coeff_ctx =
get_lower_levels_ctx_lf_2d_chroma(levels, pos, bwl, plane);
level += aom_read_symbol(r,
base_lf_uv_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_uv_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_lf_ctx_2d_chroma(levels, pos, bwl);
aom_cdf_prob *cdf = br_lf_uv_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
} else {
const int coeff_ctx =
get_lower_levels_ctx_2d_chroma(levels, pos, bwl, plane);
level += aom_read_symbol(r,
base_uv_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
4, ACCT_INFO("level", "base_uv_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_2d_chroma(levels, pos, bwl);
aom_cdf_prob *cdf = br_uv_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
}
} else {
if (limits) {
const int coeff_ctx = get_lower_levels_ctx_lf_2d(levels, pos, bwl);
level +=
aom_read_symbol(r,
base_lf_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
LF_BASE_SYMBOLS, ACCT_INFO("level", "base_lf_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_lf_ctx_2d(levels, pos, bwl);
aom_cdf_prob *cdf = br_lf_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
} else {
const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bwl
#if CONFIG_CHROMA_TX_COEFF_CODING
,
plane
#endif // CONFIG_CHROMA_TX_COEFF_CODING
);
level += aom_read_symbol(r,
base_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
4, ACCT_INFO("level", "base_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_2d(levels, pos, bwl);
aom_cdf_prob *cdf = br_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
}
}
#else
if (limits) {
const int coeff_ctx = get_lower_levels_ctx_lf_2d(levels, pos, bwl);
#if CONFIG_DQ
if (tcq_quant(*state)) // q1
level += aom_read_symbol(r, base_lf_cdf_tcq[coeff_ctx], LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_cdf_tcq"));
else
level += aom_read_symbol(r, base_lf_cdf[coeff_ctx], LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_cdf"));
#else
level += aom_read_symbol(r, base_lf_cdf[coeff_ctx], LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_cdf"));
#endif
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_lf_ctx_2d(levels, pos, bwl);
aom_cdf_prob *cdf = br_lf_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
} else {
const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bwl
#if CONFIG_CHROMA_TX_COEFF_CODING
,
plane
#endif // CONFIG_CHROMA_TX_COEFF_CODING
);
#if CONFIG_DQ
if (tcq_quant(*state)) // q1
level += aom_read_symbol(r, base_cdf_tcq[coeff_ctx], 4,
ACCT_INFO("level", "base_cdf_tcq"));
else
level += aom_read_symbol(r, base_cdf[coeff_ctx], 4,
ACCT_INFO("level", "base_cdf"));
#else
level += aom_read_symbol(r, base_cdf[coeff_ctx], 4,
ACCT_INFO("level", "base_cdf"));
#endif
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_2d(levels, pos, bwl);
aom_cdf_prob *cdf = br_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
}
#endif // CONFIG_LCCHROMA
levels[get_padded_idx(pos, bwl)] = level;
#if CONFIG_DQ
*state = tcq_next_state(*state, level, limits);
#endif
}
}
static INLINE void read_coeffs_reverse(
aom_reader *r, TX_CLASS tx_class, int start_si, int end_si,
const int16_t *scan, int bwl, uint8_t *levels, base_lf_cdf_arr base_lf_cdf,
br_cdf_arr br_lf_cdf, int plane, base_cdf_arr base_cdf, br_cdf_arr br_cdf
#if CONFIG_LCCHROMA
,
base_lf_cdf_arr base_lf_uv_cdf, br_cdf_arr br_lf_uv_cdf,
base_cdf_arr base_uv_cdf, br_cdf_arr br_uv_cdf
#endif // CONFIG_LCCHROMA
#if CONFIG_DQ
#if !CONFIG_LCCHROMA
,
base_lf_cdf_arr base_lf_cdf_tcq, base_cdf_arr base_cdf_tcq
#endif
,
int *state
#if DQENABLE
,
const TX_SIZE tx_size
#endif
#endif
#if CONFIG_COEFF_LOGS
,
qcoeff_bit_cost *qcoeff_cost
#endif // CONFIG_COEFF_LOGS
) {
#if CONFIG_COEFF_LOGS
uint64_t prev_bits = aom_reader_tell_frac(r);
#endif // CONFIG_COEFF_LOGS
for (int c = end_si; c >= start_si; --c) {
const int pos = scan[c];
int level = 0;
#if CONFIG_DQ
int dq = tcq_quant(*state);
#elif CONFIG_DQ
int dq = 0;
#endif
const int row = pos >> bwl;
const int col = pos - (row << bwl);
int limits = get_lf_limits(row, col, tx_class, plane);
#if CONFIG_LCCHROMA
if (plane > 0) {
if (limits) {
const int coeff_ctx =
get_lower_levels_lf_ctx_chroma(levels, pos, bwl, tx_class, plane);
level += aom_read_symbol(r,
base_lf_uv_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_uv_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_lf_ctx_chroma(levels, pos, bwl, tx_class);
aom_cdf_prob *cdf = br_lf_uv_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
} else {
const int coeff_ctx =
get_lower_levels_ctx_chroma(levels, pos, bwl, tx_class, plane);
level += aom_read_symbol(r,
base_uv_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
4, ACCT_INFO("level", "base_uv_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_chroma(levels, pos, bwl, tx_class);
aom_cdf_prob *cdf = br_uv_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
}
} else {
if (limits) {
const int coeff_ctx =
get_lower_levels_lf_ctx(levels, pos, bwl, tx_class);
level +=
aom_read_symbol(r,
base_lf_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
LF_BASE_SYMBOLS, ACCT_INFO("level", "base_lf_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_lf_ctx(levels, pos, bwl, tx_class);
aom_cdf_prob *cdf = br_lf_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
} else {
const int coeff_ctx = get_lower_levels_ctx(levels, pos, bwl, tx_class
#if CONFIG_CHROMA_TX_COEFF_CODING
,
plane
#endif // CONFIG_CHROMA_TX_COEFF_CODING
);
level += aom_read_symbol(r,
base_cdf[coeff_ctx]
#if CONFIG_DQ
[dq]
#endif
,
4, ACCT_INFO("level", "base_cdf"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
aom_cdf_prob *cdf = br_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
}
}
#else
if (limits) {
const int coeff_ctx = get_lower_levels_lf_ctx(levels, pos, bwl, tx_class);
#if CONFIG_DQ
if (tcq_quant(*state)) // q1
level += aom_read_symbol(r, base_lf_cdf_tcq[coeff_ctx], LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_cdf_tcq"));
else
level += aom_read_symbol(r, base_lf_cdf[coeff_ctx], LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_cdf"));
#else
level += aom_read_symbol(r, base_lf_cdf[coeff_ctx], LF_BASE_SYMBOLS,
ACCT_INFO("level", "base_lf_cdf"));
#endif
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_lf_ctx(levels, pos, bwl, tx_class);
aom_cdf_prob *cdf = br_lf_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
} else {
const int coeff_ctx = get_lower_levels_ctx(levels, pos, bwl, tx_class
#if CONFIG_CHROMA_TX_COEFF_CODING
,
plane
#endif // CONFIG_CHROMA_TX_COEFF_CODING
);
#if CONFIG_DQ
if (tcq_quant(*state)) // q1
level += aom_read_symbol(r, base_cdf_tcq[coeff_ctx], 4,
ACCT_INFO("level", "base_cdf_tcq"));
else
level += aom_read_symbol(r, base_cdf[coeff_ctx], 4,
ACCT_INFO("level", "base_cdf"));
#else
level += aom_read_symbol(r, base_cdf[coeff_ctx], 4,
ACCT_INFO("level", "base_cdf"));
#endif
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
aom_cdf_prob *cdf = br_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
}
#endif // CONFIG_LCCHROMA
levels[get_padded_idx(pos, bwl)] = level;
#if CONFIG_DQ
*state = tcq_next_state(*state, level, limits);
#endif
}
}
static INLINE void read_coeffs_forward_2d(aom_reader *r, int start_si,
int end_si, const int16_t *scan,
int bwl, uint8_t *levels,
base_fsc_cdf_arr base_cdf,
br_cdf_arr br_cdf) {
for (int c = start_si; c <= end_si; c++) {
const int pos = scan[c];
const int coeff_ctx = get_upper_levels_ctx_2d(levels, pos, bwl);
const int nsymbs = 4;
int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs,
ACCT_INFO("level", "base_cdf"));
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_skip(levels, pos, bwl);
aom_cdf_prob *cdf = br_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
levels[get_padded_idx_left(pos, bwl)] = level;
}
}
// Decode the end-of-block syntax.
static INLINE void decode_eob(DecoderCodingBlock *dcb, aom_reader *const r,
const int plane, const TX_SIZE tx_size
#if CONFIG_COEFF_LOGS
,
uint64_t *eob_pt_cost, uint64_t *eob_extra_cost,
uint64_t *eob_offset_cost
#endif // CONFIG_COEFF_LOGS
) {
MACROBLOCKD *const xd = &dcb->xd;
const PLANE_TYPE plane_type = get_plane_type(plane);
FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
#if CONFIG_EOB_POS_LUMA
const int is_inter = is_inter_block(xd->mi[0], xd->tree_type);
const int pl_ctx = get_eob_plane_ctx(plane, is_inter);
#else
const int pl_ctx = plane_type;
#endif // CONFIG_EOB_POS_LUMA
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
uint16_t *const eob = &(eob_data->eob);
eob_info *bob_data = dcb->bob_data[plane] + dcb->txb_offset[plane];
uint16_t *const bob = &(bob_data->eob);
#if CONFIG_COEFF_LOGS
uint64_t prev_cost = aom_reader_tell_frac(r);
#endif // CONFIG_COEFF_LOGS
int eob_extra = 0;
int eob_pt = 1;
const int eob_multi_size = txsize_log2_minus4[tx_size];
switch (eob_multi_size) {
case 0:
eob_pt =
aom_read_symbol(r, ec_ctx->eob_flag_cdf16[pl_ctx], EOB_MAX_SYMS - 6,
ACCT_INFO("eob_pt", "eob_multi_size:0")) +
1;
break;
case 1:
eob_pt =
aom_read_symbol(r, ec_ctx->eob_flag_cdf32[pl_ctx], EOB_MAX_SYMS - 5,
ACCT_INFO("eob_pt", "eob_multi_size:1")) +
1;
break;
case 2:
eob_pt =
aom_read_symbol(r, ec_ctx->eob_flag_cdf64[pl_ctx], EOB_MAX_SYMS - 4,
ACCT_INFO("eob_pt", "eob_multi_size:2")) +
1;
break;
case 3:
eob_pt =
aom_read_symbol(r, ec_ctx->eob_flag_cdf128[pl_ctx], EOB_MAX_SYMS - 3,
ACCT_INFO("eob_pt", "eob_multi_size:3")) +
1;
break;
case 4:
eob_pt =
aom_read_symbol(r, ec_ctx->eob_flag_cdf256[pl_ctx], EOB_MAX_SYMS - 2,
ACCT_INFO("eob_pt", "eob_multi_size:4")) +
1;
break;
case 5:
eob_pt =
aom_read_symbol(r, ec_ctx->eob_flag_cdf512[pl_ctx], EOB_MAX_SYMS - 1,
ACCT_INFO("eob_pt", "eob_multi_size:5")) +
1;
break;
case 6:
default:
eob_pt =
aom_read_symbol(r, ec_ctx->eob_flag_cdf1024[pl_ctx], EOB_MAX_SYMS,
ACCT_INFO("eob_pt", "eob_multi_size:6")) +
1;
break;
}
#if CONFIG_COEFF_LOGS
*eob_pt_cost = aom_reader_tell_frac(r) - prev_cost;
prev_cost += *eob_pt_cost;
#endif // CONFIG_COEFF_LOGS
const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
if (eob_offset_bits > 0) {
const int eob_ctx = eob_pt - 3;
int bit =
aom_read_symbol(r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx],
2, ACCT_INFO("eob_extra_cdf"));
if (bit) {
eob_extra += (1 << (eob_offset_bits - 1));
}
#if CONFIG_COEFF_LOGS
*eob_extra_cost = aom_reader_tell_frac(r) - prev_cost;
prev_cost += *eob_extra_cost;
#endif // CONFIG_COEFF_LOGS
#if CONFIG_BYPASS_IMPROVEMENT
eob_extra +=
aom_read_literal(r, eob_offset_bits - 1, ACCT_INFO("eob_extra"));
#else
for (int i = 1; i < eob_offset_bits; i++) {
bit = aom_read_bit(r, ACCT_INFO("eob_offset_bits"));
if (bit) {
eob_extra += (1 << (eob_offset_bits - 1 - i));
}
}
#endif // CONFIG_BYPASS_IMPROVEMENT
#if CONFIG_COEFF_LOGS
*eob_offset_cost = aom_reader_tell_frac(r) - prev_cost;
#endif // CONFIG_COEFF_LOGS
}
*eob = rec_eob_pos(eob_pt, eob_extra);
*bob = *eob; // escape character
#if CONFIG_CONTEXT_DERIVATION
if (plane == AOM_PLANE_U) {
xd->eob_u = *eob;
}
#endif // CONFIG_CONTEXT_DERIVATION
}
uint8_t av1_read_sig_txtype(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
aom_reader *const r, const int blk_row,
const int blk_col, const int plane,
const TXB_CTX *const txb_ctx, const TX_SIZE tx_size
#if CONFIG_COEFF_LOGS
,
uint64_t *eob_pt_bits, uint64_t *eob_extra_bits,
uint64_t *eob_offset_bits
#endif // CONFIG_COEFF_LOGS
) {
MACROBLOCKD *const xd = &dcb->xd;
FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
const int is_inter = is_inter_block(xd->mi[0], xd->tree_type);
eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
uint16_t *const eob = &(eob_data->eob);
uint16_t *const max_scan_line = &(eob_data->max_scan_line);
*max_scan_line = 0;
*eob = 0;
#if 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 // CCTX_C2_DROPPED
#if CONFIG_CONTEXT_DERIVATION
if (plane == AOM_PLANE_U) {
xd->eob_u = 0;
}
int txb_skip_ctx = txb_ctx->txb_skip_ctx;
int all_zero;
if (plane == AOM_PLANE_Y || plane == AOM_PLANE_U) {
#if CONFIG_TX_SKIP_FLAG_MODE_DEP_CTX
MB_MODE_INFO *const mbmi = xd->mi[0];
const int pred_mode_ctx =
(is_inter || mbmi->fsc_mode[xd->tree_type == CHROMA_PART]) ? 1 : 0;
all_zero = aom_read_symbol(
r, ec_ctx->txb_skip_cdf[pred_mode_ctx][txs_ctx][txb_skip_ctx], 2,
ACCT_INFO("all_zero", "plane_y_or_u"));
#else
all_zero = aom_read_symbol(r, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx],
2, ACCT_INFO("all_zero", "plane_y_or_u"));
#endif // CONFIG_TX_SKIP_FLAG_MODE_DEP_CTX
} else {
txb_skip_ctx += (xd->eob_u_flag ? V_TXB_SKIP_CONTEXT_OFFSET : 0);
all_zero = aom_read_symbol(r, ec_ctx->v_txb_skip_cdf[txb_skip_ctx], 2,
ACCT_INFO("all_zero", "plane_v"));
}
#else
const int all_zero =
aom_read_symbol(r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx],
2, ACCT_INFO("all_zero"));
#endif // CONFIG_CONTEXT_DERIVATION
#if CONFIG_INSPECTION
MB_MODE_INFO *const mbmi = xd->mi[0];
if (plane == 0) {
const int txk_type_idx =
av1_get_txk_type_index(mbmi->sb_type[0], blk_row, blk_col);
mbmi->tx_skip[txk_type_idx] = all_zero;
}
#endif // CONFIG_INSPECTION
#if CONFIG_CONTEXT_DERIVATION
if (plane == AOM_PLANE_U) {
xd->eob_u_flag = all_zero ? 0 : 1;
}
#endif // CONFIG_CONTEXT_DERIVATION
#if CONFIG_WAIP
#if !CONFIG_INSPECTION
MB_MODE_INFO *mbmi = xd->mi[0];
#endif // !CONFIG_INSPECTION
if (is_inter_block(mbmi, xd->tree_type)) {
#if CONFIG_TX_PARTITION_TYPE_EXT
mbmi->is_wide_angle[plane > 0][mbmi->txb_idx] = 0;
mbmi->mapped_intra_mode[plane > 0][mbmi->txb_idx] = DC_PRED;
#else
mbmi->is_wide_angle[plane > 0] = 0;
mbmi->mapped_intra_mode[plane > 0] = DC_PRED;
#endif // CONFIG_TX_PARTITION_TYPE_EXT
} else {
PREDICTION_MODE mode = (plane == PLANE_TYPE_Y ? mbmi->mode : mbmi->uv_mode);
const int angle_delta =
mbmi->angle_delta[plane != AOM_PLANE_Y] * ANGLE_STEP;
wide_angle_mapping(mbmi, angle_delta, tx_size, mode, plane);
}
#endif // CONFIG_WAIP
if (all_zero) {
*max_scan_line = 0;
if (plane == 0) {
xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col] = DCT_DCT;
}
#if CONFIG_TXFMBLK_LOGS
fprintf(
cm->fDecTxfmLog,
"[AV2DEC-TXFMBLK-INFO] %03d %4d %03d %03d %3d %3d %04d %04d %6d %02d\n",
cm->current_frame.absolute_poc, // 1: POC (picture number)
plane, // 2: plane ID (Y/U/V)
xd->mi_row, // 3: macroblock row index
xd->mi_col, // 4: macroblock coloumn index
blk_row, // 5: transform block row index
blk_col, // 6: transform block column index
tx_size, // 7: transform size
0, // 8: transform type (filler for skipped blocks)
0, // 9: eob (# of coded coefficients)
0); // 10: coded flag: 0 for skipped blocks, 1 for coded blocks
#endif // CONFIG_TXFMBLK_LOGS
return 0;
}
decode_eob(dcb, r, plane, tx_size
#if CONFIG_COEFF_LOGS
,
eob_pt_bits, eob_extra_bits, eob_offset_bits
#endif // CONFIG_COEFF_LOGS
);
av1_read_tx_type(cm, xd, blk_row, blk_col, tx_size, r, plane, *eob,
is_inter ? 0 : *eob);
if (plane == AOM_PLANE_U && is_cctx_allowed(cm, xd)) {
const int skip_cctx = is_inter ? 0 : (*eob == 1);
if (!all_zero && !skip_cctx) {
av1_read_cctx_type(cm, xd, blk_row, blk_col, tx_size, r);
} else {
int row_offset, col_offset;
#if CONFIG_EXT_RECUR_PARTITIONS
get_chroma_mi_offsets(xd, &row_offset, &col_offset);
#else
get_chroma_mi_offsets(xd, tx_size, &row_offset, &col_offset);
#endif // CONFIG_EXT_RECUR_PARTITIONS
update_cctx_array(xd, blk_row, blk_col, row_offset, col_offset, tx_size,
CCTX_NONE);
}
}
return 1;
}
uint8_t av1_read_coeffs_txb_skip(const AV1_COMMON *const cm,
DecoderCodingBlock *dcb, aom_reader *const r,
const int blk_row, const int blk_col,
const int plane, const TX_SIZE tx_size) {
MACROBLOCKD *const xd = &dcb->xd;
MB_MODE_INFO *const mbmi = xd->mi[0];
FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
struct macroblockd_plane *const pd = &xd->plane[plane];
const PLANE_TYPE plane_type = get_plane_type(plane);
const int32_t max_value = (1 << (7 + xd->bd)) - 1;
const int32_t min_value = -(1 << (7 + xd->bd));
const int32_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id];
tran_low_t *const tcoeffs = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
const int shift = av1_get_tx_scale(tx_size);
const int bwl = get_txb_bwl(tx_size);
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
#if CONFIG_INSPECTION
tran_low_t *const tcoeffs_copy =
dcb->dqcoeff_block_copy[plane] + dcb->cb_offset[plane];
tran_low_t *const quant_coeffs =
dcb->qcoeff_block[plane] + dcb->cb_offset[plane];
tran_low_t *const dequant_values =
dcb->dequant_values[plane] + dcb->cb_offset[plane];
// For TX sizes > 32x32, all coeffs are zero except for top-left 32x32.
const int coeff_width = AOMMIN(width, 32);
const int coeff_height = AOMMIN(height, 32);
memset(tcoeffs_copy, 0, sizeof(tran_low_t) * coeff_width * coeff_height);
memset(quant_coeffs, 0, sizeof(tran_low_t) * coeff_width * coeff_height);
memset(dequant_values, 0, sizeof(tran_low_t) * coeff_width * coeff_height);
#endif // CONFIG_INSPECTION
int cul_level = 0;
int dc_val = 0;
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, width);
int8_t signs_buf[TX_PAD_2D];
int8_t *const signs = set_signs(signs_buf, width);
eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
eob_data->max_scan_line = 0;
eob_data->eob = av1_get_max_eob(tx_size);
eob_info *bob_data = dcb->bob_data[plane] + dcb->txb_offset[plane];
bob_data->max_scan_line = 0;
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 qm_val_t *iqmatrix =
av1_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type);
#if CONFIG_INSPECTION
for (int c = 0; c < width * height; c++) {
dequant_values[c] = get_dqv(dequant, c, iqmatrix);
}
#endif // CONFIG_INSPECTION
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
#if CONFIG_IMPROVEIDTX_CTXS
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
const int size_ctx = AOMMIN(txs_ctx, TX_16X16);
#endif // CONFIG_IMPROVEIDTX_CTXS
if (eob_data->eob > 1) {
memset(levels_buf, 0, sizeof(*levels_buf) * TX_PAD_2D);
memset(signs_buf, 0, sizeof(*signs_buf) * TX_PAD_2D);
#if CONFIG_IMPROVEIDTX_CTXS
base_fsc_cdf_arr base_cdf = ec_ctx->coeff_base_cdf_idtx[size_ctx];
br_cdf_arr br_cdf = ec_ctx->coeff_br_cdf_idtx[size_ctx];
#else
base_fsc_cdf_arr base_cdf = ec_ctx->coeff_base_cdf_idtx;
br_cdf_arr br_cdf = ec_ctx->coeff_br_cdf_idtx;
#endif // CONFIG_IMPROVEIDTX_CTXS
const int bob = av1_get_max_eob(tx_size) - bob_data->eob;
{
const int pos = scan[bob];
const int coeff_ctx_bob = get_lower_levels_ctx_bob(bwl, height, bob);
const int nsymbs_bob = 3;
#if CONFIG_IMPROVEIDTX_CTXS
aom_cdf_prob *cdf_bob =
ec_ctx->coeff_base_bob_cdf[size_ctx][coeff_ctx_bob];
#else
aom_cdf_prob *cdf_bob = ec_ctx->coeff_base_bob_cdf[coeff_ctx_bob];
#endif // CONFIG_IMPROVEIDTX_CTXS
int level = aom_read_symbol(r, cdf_bob, nsymbs_bob,
ACCT_INFO("level", "cdf_bob")) +
1;
if (level > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_skip(levels, pos, bwl);
aom_cdf_prob *cdf = br_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
levels[get_padded_idx_left(pos, bwl)] = level;
}
read_coeffs_forward_2d(r, bob + 1, eob_data->eob - 1, scan, bwl, levels,
base_cdf, br_cdf);
}
const int bob = av1_get_max_eob(tx_size) - bob_data->eob;
#if CONFIG_IMPROVEIDTX_RDPH
for (int c = bob; c < eob_data->eob; c++) {
#else
for (int c = eob_data->eob - 1; c >= 0; --c) {
#endif // CONFIG_IMPROVEIDTX_RDPH
const int pos = scan[c];
#if CONFIG_IMPROVEIDTX_RDPH
const int sign_idx = get_padded_idx_left(pos, bwl);
#else
const int sign_idx = get_padded_idx(pos, bwl);
#endif // CONFIG_IMPROVEIDTX_RDPH
uint8_t sign;
tran_low_t level = levels[get_padded_idx_left(pos, bwl)];
if (level) {
eob_data->max_scan_line = AOMMAX(eob_data->max_scan_line, pos);
int idtx_sign_ctx = get_sign_ctx_skip(signs, levels, pos, bwl);
#if CONFIG_IMPROVEIDTX_CTXS
sign = aom_read_symbol(r, ec_ctx->idtx_sign_cdf[size_ctx][idtx_sign_ctx],
2, ACCT_INFO("sign"));
#else
sign = aom_read_symbol(r, ec_ctx->idtx_sign_cdf[idtx_sign_ctx], 2,
ACCT_INFO("sign"));
#endif // CONFIG_IMPROVEIDTX_CTXS
signs[sign_idx] = sign > 0 ? -1 : 1;
level = read_high_range(xd, r, 0, level, 0);
if (c == 0) dc_val = sign ? -level : level;
// Bitmasking to clamp level to valid range:
// The valid range for 8/10/12 bit video is at most 14/16/18 bit
level &= 0xfffff;
cul_level += level;
tran_low_t dq_coeff;
// Bitmasking to clamp dq_coeff to valid range:
// The valid range for 8/10/12 bit video is at most 17/19/21 bits
const int64_t dq_coeff_hp =
(int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff;
dq_coeff =
(tran_low_t)(ROUND_POWER_OF_TWO_64(dq_coeff_hp, QUANT_TABLE_BITS));
dq_coeff = dq_coeff >> shift;
if (sign) {
dq_coeff = -dq_coeff;
}
tcoeffs[pos] = clamp(dq_coeff, min_value, max_value);
#if CONFIG_INSPECTION
tcoeffs_copy[pos] = tcoeffs[pos];
quant_coeffs[pos] = sign ? -level : level;
#endif // CONFIG_INSPECTION
}
}
cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
set_dc_sign(&cul_level, dc_val);
return cul_level;
}
// This function returns the partial absolute level of the coefficient
// with hidden parity.
static INLINE tran_low_t read_coeff_hidden(aom_reader *r, TX_CLASS tx_class,
const int16_t *scan, int bwl,
uint8_t *levels, int parity,
base_ph_cdf_arr base_cdf_ph,
br_cdf_arr br_cdf_ph
#if CONFIG_COEFF_LOGS
,
qcoeff_bit_cost *qcoeff_cost
#endif // CONFIG_COEFF_LOGS
) {
#if CONFIG_COEFF_LOGS
uint64_t prev_bits = aom_reader_tell_frac(r);
#endif // CONFIG_COEFF_LOGS
int q_index;
const int pos = scan[0];
int ctx_idx = get_base_ctx_ph(levels, pos, bwl, tx_class);
q_index = aom_read_symbol(r, base_cdf_ph[ctx_idx], 4, ACCT_INFO("q_index"));
#if CONFIG_COEFF_LOGS
qcoeff_cost[0].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[0].br_cost;
#endif // CONFIG_COEFF_LOGS
if (q_index > NUM_BASE_LEVELS) {
ctx_idx = get_par_br_ctx(levels, pos, bwl, tx_class);
aom_cdf_prob *cdf_br = br_cdf_ph[ctx_idx];
q_index += read_low_range(r, cdf_br
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
assert(q_index <= MAX_BASE_BR_RANGE);
uint8_t level = (q_index << 1) + parity;
levels[get_padded_idx(pos, bwl)] = level;
return level;
}
uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb,
aom_reader *const r, const int blk_row,
const int blk_col, const int plane,
const TXB_CTX *const txb_ctx, const TX_SIZE tx_size
#if CONFIG_COEFF_LOGS
,
uint64_t *eob_pt_bits, uint64_t *eob_extra_bits,
uint64_t *eob_offset_bits
#endif // CONFIG_COEFF_LOGS
) {
MACROBLOCKD *const xd = &dcb->xd;
FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
const int32_t max_value = (1 << (7 + xd->bd)) - 1;
const int32_t min_value = -(1 << (7 + xd->bd));
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
const PLANE_TYPE plane_type = get_plane_type(plane);
MB_MODE_INFO *const mbmi = xd->mi[0];
struct macroblockd_plane *const pd = &xd->plane[plane];
const int32_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id];
tran_low_t *const tcoeffs = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
const int shift = av1_get_tx_scale(tx_size);
const int bwl = get_txb_bwl(tx_size);
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
#if CONFIG_INSPECTION
tran_low_t *const tcoeffs_copy =
dcb->dqcoeff_block_copy[plane] + dcb->cb_offset[plane];
tran_low_t *const quant_coeffs =
dcb->qcoeff_block[plane] + dcb->cb_offset[plane];
tran_low_t *const dequant_values =
dcb->dequant_values[plane] + dcb->cb_offset[plane];
// For TX sizes > 32x32, all coeffs are zero except for top-left 32x32.
const int coeff_width = AOMMIN(width, 32);
const int coeff_height = AOMMIN(height, 32);
memset(tcoeffs_copy, 0, sizeof(tran_low_t) * coeff_width * coeff_height);
memset(quant_coeffs, 0, sizeof(tran_low_t) * coeff_width * coeff_height);
memset(dequant_values, 0, sizeof(tran_low_t) * coeff_width * coeff_height);
#endif // CONFIG_INSPECTION
int cul_level = 0;
int dc_val = 0;
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, width);
eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
uint16_t *const eob = &(eob_data->eob);
uint16_t *const max_scan_line = &(eob_data->max_scan_line);
#if CONFIG_DQ
int tcq_mode = cm->features.tcq_mode;
#else
int tcq_mode = 0;
#endif
#if DEBUG_EXTQUANT
fprintf(cm->fDecCoeffLog,
"\nmi_row = %d, mi_col = %d, blk_row = %d,"
" blk_col = %d, plane = %d, tx_size = %d ",
xd->mi_row, xd->mi_col, blk_row, blk_col, plane, tx_size);
#endif
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 TX_CLASS tx_class = tx_type_to_class[get_primary_tx_type(tx_type)];
const qm_val_t *iqmatrix =
av1_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type);
#if CONFIG_INSPECTION
for (int c = 0; c < width * height; c++) {
dequant_values[c] = get_dqv(dequant, c, iqmatrix);
}
#endif // CONFIG_INSPECTION
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
// read sec_tx_type here
// Only y plane's sec_tx_type is transmitted
#if CONFIG_INTER_IST
if ((plane == AOM_PLANE_Y) &&
(is_inter_block(mbmi, xd->tree_type)
? (*eob > 3 && cm->seq_params.enable_inter_ist)
: (*eob != 1 && cm->seq_params.enable_ist))) {
#else
if ((plane == AOM_PLANE_Y) && (cm->seq_params.enable_ist) && (*eob != 1)) {
#endif // // CONFIG_INTER_IST
av1_read_sec_tx_type(cm, xd, blk_row, blk_col, tx_size, eob, r);
}
//
if (*eob > 1) {
memset(levels_buf, 0, sizeof(*levels_buf) * TX_PAD_2D);
}
#if DEBUG_EXTQUANT
fprintf(cm->fDecCoeffLog, "tx_type = %d, eob = %d\n", tx_type, *eob);
#endif
#if CONFIG_TXFMBLK_LOGS || CONFIG_COEFF_LOGS
const int neob = *eob;
#endif // CONFIG_TXFMBLK_LOGS || CONFIG_COEFF_LOGS
#if CONFIG_TXFMBLK_LOGS
fprintf(
cm->fDecTxfmLog,
"[AV2DEC-TXFMBLK-INFO] %03d %4d %03d %03d %3d %3d %04d %04d %6d %02d\n",
cm->current_frame.absolute_poc, // 1: POC (picture number)
plane, // 2: plane ID (Y/U/V)
xd->mi_row, // 3: macroblock row index
xd->mi_col, // 4: macroblock coloumn index
blk_row, // 5: transform block row index
blk_col, // 6: transform block column index
tx_size, // 7: transform size
tx_type, // 8: transform type
neob, // 9: eob (# of coded coefficients)
1); // 10: coded flag: 0 for skipped blocks, 1 for coded blocks
#endif // CONFIG_TXFMBLK_LOGS
#if CONFIG_COEFF_LOGS
const int n_coeffs = av1_get_max_eob(tx_size);
qcoeff_bit_cost *qcoeff_cost = malloc(sizeof(qcoeff_bit_cost) * n_coeffs);
memset(qcoeff_cost, 0, sizeof(qcoeff_bit_cost) * n_coeffs);
int *qcoeff = malloc(sizeof(int) * n_coeffs);
memset(qcoeff, 0, sizeof(int) * n_coeffs);
uint64_t prev_bits = aom_reader_tell_frac(r);
#endif // CONFIG_COEFF_LOGS
#if CONFIG_DQ
int state = tcq_init_state(cm->features.tcq_mode);
#endif
{
// Read the non-zero coefficient with scan index eob-1
// TODO(angiebird): Put this into a function
const int c = *eob - 1;
const int pos = scan[c];
const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, c);
int level = 0;
const int row = pos >> bwl;
const int col = pos - (row << bwl);
int limits = get_lf_limits(row, col, tx_class, plane);
#if CONFIG_LCCHROMA
if (plane > 0) {
if (limits) {
aom_cdf_prob *cdf = ec_ctx->coeff_base_lf_eob_uv_cdf[coeff_ctx];
level +=
aom_read_symbol(r, cdf, LF_BASE_SYMBOLS - 1,
ACCT_INFO("level", "coeff_base_lf_eob_uv_cdf")) +
1;
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_lf_eob_chroma(pos, tx_class);
cdf = ec_ctx->coeff_br_lf_uv_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
} else {
aom_cdf_prob *cdf = ec_ctx->coeff_base_eob_uv_cdf[coeff_ctx];
level += aom_read_symbol(r, cdf, 3,
ACCT_INFO("level", "coeff_base_eob_uv_cdf")) +
1;
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > NUM_BASE_LEVELS) {
const int br_ctx = 0; /* get_lf_ctx_eob */
cdf = ec_ctx->coeff_br_uv_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
}
} else {
if (limits) {
aom_cdf_prob *cdf = ec_ctx->coeff_base_lf_eob_cdf[txs_ctx][coeff_ctx];
level += aom_read_symbol(r, cdf, LF_BASE_SYMBOLS - 1,
ACCT_INFO("level", "coeff_base_lf_eob_cdf")) +
1;
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_lf_eob(pos, tx_class);
cdf = ec_ctx->coeff_br_lf_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
} else {
aom_cdf_prob *cdf = ec_ctx->coeff_base_eob_cdf[txs_ctx][coeff_ctx];
level += aom_read_symbol(r, cdf, 3,
ACCT_INFO("level", "coeff_base_eob_cdf")) +
1;
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].br_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].br_cost;
#endif // CONFIG_COEFF_LOGS
if (level > NUM_BASE_LEVELS) {
const int br_ctx = 0; /* get_lf_ctx_eob */
cdf = ec_ctx->coeff_br_cdf[br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
&qcoeff_cost[c], &prev_bits
#endif
);
}
}
}
#else
if (limits) {
aom_cdf_prob *cdf =
ec_ctx->coeff_base_lf_eob_cdf[txs_ctx][plane_type][coeff_ctx];
level += aom_read_symbol(r, cdf, LF_BASE_SYMBOLS - 1,
ACCT_INFO("level", "coeff_base_lf_eob_cdf")) +
1;
if (level > LF_NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_lf_eob(pos, tx_class);
cdf = ec_ctx->coeff_br_lf_cdf[plane_type][br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
} else {
aom_cdf_prob *cdf =
ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx];
level +=
aom_read_symbol(r, cdf, 3, ACCT_INFO("level", "coeff_base_eob_cdf")) +
1;
if (level > NUM_BASE_LEVELS) {
const int br_ctx = 0; /* get_lf_ctx_eob */
cdf = ec_ctx->coeff_br_cdf[plane_type][br_ctx];
level += read_low_range(r, cdf
#if CONFIG_COEFF_LOGS
,
0, 0
#endif
);
}
}
#endif // CONFIG_LCCHROMA
levels[get_padded_idx(pos, bwl)] = level;
#if CONFIG_DQ
state = tcq_next_state(state, level, limits);
#endif
}
bool enable_parity_hiding =
cm->features.allow_parity_hiding && !xd->lossless[mbmi->segment_id] &&
plane == PLANE_TYPE_Y &&
#if CONFIG_IMPROVEIDTX_RDPH
ph_allowed_tx_types[get_primary_tx_type(tx_type)] && (*eob > PHTHRESH);
#else
get_primary_tx_type(tx_type) < IDTX;
#endif // CONFIG_IMPROVEIDTX_RDPH
int num_nz = 0, sum_abs1 = 0;
bool is_hidden = false;
if (*eob > 1) {
#if CONFIG_LCCHROMA
base_lf_cdf_arr base_lf_cdf = ec_ctx->coeff_base_lf_cdf[txs_ctx];
br_cdf_arr br_lf_cdf = ec_ctx->coeff_br_lf_cdf;
base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx];
br_cdf_arr br_cdf = ec_ctx->coeff_br_cdf;
base_lf_cdf_arr base_lf_uv_cdf = ec_ctx->coeff_base_lf_uv_cdf;
br_cdf_arr br_lf_uv_cdf = ec_ctx->coeff_br_lf_uv_cdf;
base_cdf_arr base_uv_cdf = ec_ctx->coeff_base_uv_cdf;
br_cdf_arr br_uv_cdf = ec_ctx->coeff_br_uv_cdf;
#else
base_lf_cdf_arr base_lf_cdf =
ec_ctx->coeff_base_lf_cdf[txs_ctx][plane_type];
#if CONFIG_DQ && !CONFIG_LCCHROMA
base_lf_cdf_arr base_lf_cdf_tcq =
ec_ctx->coeff_base_lf_cdf_tcq[txs_ctx][plane_type];
#endif
br_cdf_arr br_lf_cdf = ec_ctx->coeff_br_lf_cdf[plane_type];
base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx][plane_type];
#if CONFIG_DQ && !CONFIG_LCCHROMA
base_cdf_arr base_cdf_tcq = ec_ctx->coeff_base_cdf_tcq[txs_ctx][plane_type];
#endif
br_cdf_arr br_cdf = ec_ctx->coeff_br_cdf[plane_type];
#endif // CONFIG_LCCHROMA
if (tx_class == TX_CLASS_2D) {
read_coeffs_reverse_2d(r, 1, *eob - 2, scan, bwl, levels, base_lf_cdf,
br_lf_cdf, plane, base_cdf, br_cdf
#if CONFIG_LCCHROMA
,
base_lf_uv_cdf, br_lf_uv_cdf, base_uv_cdf,
br_uv_cdf
#endif // CONFIG_LCCHROMA
#if CONFIG_DQ
#if !CONFIG_LCCHROMA
,
base_lf_cdf_tcq, base_cdf_tcq
#endif
,
&state
#if DQENABLE
,
tx_size
#endif
#endif
#if CONFIG_COEFF_LOGS
,
qcoeff_cost
#endif // CONFIG_COEFF_LOGS
);
if (enable_parity_hiding) {
for (int si = *eob - 1; si > 0; --si) {
int pos = scan[si];
int level =
AOMMIN(levels[get_padded_idx(pos, bwl)], MAX_BASE_BR_RANGE);
if (level) {
++num_nz;
sum_abs1 += level;
}
}
is_hidden = num_nz >= PHTHRESH;
}
if (is_hidden) {
read_coeff_hidden(r, tx_class, scan, bwl, levels, (sum_abs1 & 1),
ec_ctx->coeff_base_ph_cdf, ec_ctx->coeff_br_ph_cdf
#if CONFIG_COEFF_LOGS
,
qcoeff_cost
#endif // CONFIG_COEFF_LOGS
);
} else {
read_coeffs_reverse(r, tx_class, 0, 0, scan, bwl, levels, base_lf_cdf,
br_lf_cdf, plane, base_cdf, br_cdf
#if CONFIG_LCCHROMA
,
base_lf_uv_cdf, br_lf_uv_cdf, base_uv_cdf, br_uv_cdf
#endif // CONFIG_LCCHROMA
#if CONFIG_DQ
#if !CONFIG_LCCHROMA
,
base_lf_cdf_tcq, base_cdf_tcq
#endif
,
&state
#if DQENABLE
,
tx_size
#endif
#endif
#if CONFIG_COEFF_LOGS
,
qcoeff_cost
#endif // CONFIG_COEFF_LOGS
);
}
} else {
read_coeffs_reverse(r, tx_class, 1, *eob - 2, scan, bwl, levels,
base_lf_cdf, br_lf_cdf, plane, base_cdf, br_cdf
#if CONFIG_LCCHROMA
,
base_lf_uv_cdf, br_lf_uv_cdf, base_uv_cdf, br_uv_cdf
#endif // CONFIG_LCCHROMA
#if CONFIG_DQ
#if !CONFIG_LCCHROMA
,
base_lf_cdf_tcq, base_cdf_tcq
#endif
,
&state
#if DQENABLE
,
tx_size
#endif
#endif
#if CONFIG_COEFF_LOGS
,
qcoeff_cost
#endif // CONFIG_COEFF_LOGS
);
if (enable_parity_hiding) {
for (int si = *eob - 1; si > 0; --si) {
int pos = scan[si];
int level =
AOMMIN(levels[get_padded_idx(pos, bwl)], MAX_BASE_BR_RANGE);
if (level) {
++num_nz;
sum_abs1 += level;
}
}
is_hidden = num_nz >= PHTHRESH;
}
if (is_hidden) {
read_coeff_hidden(r, tx_class, scan, bwl, levels, (sum_abs1 & 1),
ec_ctx->coeff_base_ph_cdf, ec_ctx->coeff_br_ph_cdf
#if CONFIG_COEFF_LOGS
,
qcoeff_cost
#endif // CONFIG_COEFF_LOGS
);
} else {
read_coeffs_reverse(r, tx_class, 0, 0, scan, bwl, levels, base_lf_cdf,
br_lf_cdf, plane, base_cdf, br_cdf
#if CONFIG_LCCHROMA
,
base_lf_uv_cdf, br_lf_uv_cdf, base_uv_cdf, br_uv_cdf
#endif // CONFIG_LCCHROMA
#if CONFIG_DQ
#if !CONFIG_LCCHROMA
,
base_lf_cdf_tcq, base_cdf_tcq
#endif
,
&state
#if DQENABLE
,
tx_size
#endif
#endif
#if CONFIG_COEFF_LOGS
,
qcoeff_cost
#endif // CONFIG_COEFF_LOGS
);
}
}
}
#if CONFIG_COEFF_LOGS
// reset bitstream position pointer
prev_bits = aom_reader_tell_frac(r);
#endif // CONFIG_COEFF_LOGS
#if CONFIG_IMPROVEIDTX_RDPH
for (int c = *eob - 1; c >= 0; --c) {
#else
for (int c = 0; c < *eob; ++c) {
#endif // CONFIG_IMPROVEIDTX_RDPH
const int pos = scan[c];
uint8_t sign;
tran_low_t level = levels[get_padded_idx(pos, bwl)];
#if CONFIG_IMPROVEIDTX_RDPH
const int tmp_sign_idx = pos;
#else
const int tmp_sign_idx = 0;
#endif // CONFIG_IMPROVEIDTX_RDPH
#if CONFIG_CONTEXT_DERIVATION
if (plane == AOM_PLANE_U) {
xd->tmp_sign[pos] = 0;
}
#endif // CONFIG_CONTEXT_DERIVATION
if (level) {
*max_scan_line = AOMMAX(*max_scan_line, pos);
#if CONFIG_IMPROVEIDTX_CTXS
const int row = pos >> bwl;
const int col = pos - (row << bwl);
const bool dc_2dtx = (c == 0);
const bool dc_hor = (col == 0) && tx_class == TX_CLASS_HORIZ;
const bool dc_ver = (row == 0) && tx_class == TX_CLASS_VERT;
if (dc_2dtx || dc_hor || dc_ver) {
const int dc_sign_ctx = dc_2dtx ? txb_ctx->dc_sign_ctx : 0;
#else
if (c == 0) {
const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
#endif // CONFIG_IMPROVEIDTX_CTXS
#if CONFIG_CONTEXT_DERIVATION
if (plane == AOM_PLANE_Y || plane == AOM_PLANE_U) {
#if CONFIG_IMPROVEIDTX_CTXS
sign = aom_read_symbol(
r,
ec_ctx->dc_sign_cdf[plane_type][is_hidden ? 1 : 0][dc_sign_ctx],
2, ACCT_INFO("sign", "dc_sign_cdf", "plane_y_or_u"));
#else
sign = aom_read_symbol(
r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2,
ACCT_INFO("sign", "dc_sign_cdf", "plane_y_or_u"));
#endif // CONFIG_IMPROVEIDTX_CTXS
} else {
int32_t tmp_sign = 0;
if (c < xd->eob_u) tmp_sign = xd->tmp_sign[tmp_sign_idx];
sign =
aom_read_symbol(r, ec_ctx->v_dc_sign_cdf[tmp_sign][dc_sign_ctx],
2, ACCT_INFO("sign", "v_dc_sign_cdf", "plane_v"));
}
if (plane == AOM_PLANE_U) xd->tmp_sign[tmp_sign_idx] = (sign ? 2 : 1);
#else
sign = aom_read_symbol(r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
2, ACCT_INFO("sign", "dc_sign_cdf"));
#endif // CONFIG_CONTEXT_DERIVATION
} else {
#if CONFIG_CONTEXT_DERIVATION
if (plane == AOM_PLANE_Y || plane == AOM_PLANE_U)
sign = aom_read_bit(r, ACCT_INFO("sign", "plane_y_or_u"));
else {
int32_t tmp_sign = 0;
if (c < xd->eob_u) tmp_sign = xd->tmp_sign[pos];
sign = aom_read_symbol(r, ec_ctx->v_ac_sign_cdf[tmp_sign], 2,
ACCT_INFO("sign", "v_ac_sign_cdf", "plane_v"));
}
if (plane == AOM_PLANE_U) xd->tmp_sign[pos] = (sign ? 2 : 1);
#else
sign = aom_read_bit(r, ACCT_INFO("sign"));
#endif // CONFIG_CONTEXT_DERIVATION
}
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].sign_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].sign_cost;
#endif // CONFIG_COEFF_LOGS
if (is_hidden && c == 0) {
if (level >= (MAX_BASE_BR_RANGE << 1)) {
level += (read_golomb(xd, r) << 1);
}
} else {
#if !CONFIG_IMPROVEIDTX_CTXS
const int row = pos >> bwl;
const int col = pos - (row << bwl);
#endif // !CONFIG_IMPROVEIDTX_CTXS
int limits = get_lf_limits(row, col, tx_class, plane);
level = read_high_range(xd, r, tcq_mode, level, limits);
}
if (c == 0) dc_val = sign ? -level : level;
#if CONFIG_COEFF_LOGS
qcoeff_cost[c].hr_cost = aom_reader_tell_frac(r) - prev_bits;
prev_bits += qcoeff_cost[c].hr_cost;
#endif // CONFIG_COEFF_LOGS
// Bitmasking to clamp level to valid range:
// The valid range for 8/10/12 bit vdieo is at most 14/16/18 bit
level &= 0xfffff;
cul_level += level;
#if CONFIG_DQ
if (!xd->lossless[mbmi->segment_id] &&
dq_enable(tcq_mode, tx_size, plane)) {
tcoeffs[pos] = sign ? -level : level;
} else {
tran_low_t dq_coeff;
// Bitmasking to clamp dq_coeff to valid range:
// The valid range for 8/10/12 bit video is at most 17/19/21 bit
const int64_t dq_coeff_hp =
(int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff;
dq_coeff =
(tran_low_t)(ROUND_POWER_OF_TWO_64(dq_coeff_hp, QUANT_TABLE_BITS));
dq_coeff = dq_coeff >> shift;
if (sign) {
dq_coeff = -dq_coeff;
}
tcoeffs[pos] = clamp(dq_coeff, min_value, max_value);
#if CONFIG_INSPECTION
tcoeffs_copy[pos] = tcoeffs[pos];
quant_coeffs[pos] = sign ? -level : level;
#endif // CONFIG_INSPECTION
}
#else
tran_low_t dq_coeff;
// Bitmasking to clamp dq_coeff to valid range:
// The valid range for 8/10/12 bit video is at most 17/19/21 bit
const int64_t dq_coeff_hp =
(int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff;
dq_coeff =
(tran_low_t)(ROUND_POWER_OF_TWO_64(dq_coeff_hp, QUANT_TABLE_BITS));
dq_coeff = dq_coeff >> shift;
if (sign) {
dq_coeff = -dq_coeff;
}
tcoeffs[pos] = clamp(dq_coeff, min_value, max_value);
#if CONFIG_INSPECTION
tcoeffs_copy[pos] = tcoeffs[pos];
quant_coeffs[pos] = sign ? -level : level;
#endif // CONFIG_INSPECTION
#endif
#if CONFIG_COEFF_LOGS
// record coefficient index at position [c]
qcoeff[pos] = sign ? -level : level;
#endif // CONFIG_COEFF_LOGS
}
}
#if CONFIG_DQ
if (!xd->lossless[mbmi->segment_id] && dq_enable(tcq_mode, tx_size, plane)) {
state = tcq_init_state(tcq_mode);
for (int c = *eob - 1; c >= 0; --c) {
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);
int Qx = tcq_quant(state);
int level = abs(tcoeffs[pos]);
if (abs(tcoeffs[pos])) {
tran_low_t dq_coeff;
tran_low_t qIdx = AOMMAX(0, (abs(tcoeffs[pos]) << 1) - Qx);
// Bitmasking to clamp dq_coeff to valid range:
// The valid range for 8/10/12 bit video is at most 17/19/21 bit
int tempdqv = get_dqv(dequant, scan[c], iqmatrix);
const int64_t dq_coeff_hp = (int64_t)qIdx * tempdqv & 0xffffff;
dq_coeff =
(tran_low_t)(ROUND_POWER_OF_TWO_64(dq_coeff_hp, QUANT_TABLE_BITS));
dq_coeff = dq_coeff >> (shift + 1);
// dq_coeff = dq_coeff >> (shift);
// dq_coeff = (tran_low_t)ROUND_POWER_OF_TWO_64((tran_high_t)qIdx *
// get_dqv(dequant, scan[c], iqmatrix), QUANT_TABLE_BITS) >> (shift +
// 1);
if (tcoeffs[pos] < 0) {
dq_coeff = -dq_coeff;
}
#if CONFIG_COEFF_LOGS
qcoeff[pos] = tcoeffs[pos];
#endif // CONFIG_COEFF_LOGS
tcoeffs[pos] = clamp(dq_coeff, min_value, max_value);
}
// state transition
state = tcq_next_state(state, level, limits);
}
}
#endif
#if DEBUG_EXTQUANT
for (int c = 0; c < tx_size_wide[tx_size] * tx_size_high[tx_size]; c++) {
fprintf(cm->fDecCoeffLog, "%d ", tcoeffs[c]);
}
fprintf(cm->fDecCoeffLog, "\n\n");
#endif
#if CONFIG_COEFF_LOGS
const int sq_qstep_dc =
ROUND_POWER_OF_TWO(dequant[0], QUANT_TABLE_BITS) >> shift;
const int sq_qstep_ac =
ROUND_POWER_OF_TWO(dequant[1], QUANT_TABLE_BITS) >> shift;
const int vq_qstep_dc = sq_qstep_dc >> 1;
const int vq_qstep_ac = sq_qstep_ac >> 1;
const int is_inter = is_inter_block(mbmi, xd->tree_type);
// log per-coefficient stats
av2_tcq_declog_percoeff(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, sq_qstep_dc, sq_qstep_ac, vq_qstep_dc,
vq_qstep_ac, neob, n_coeffs, qcoeff, "TXDEC");
av2_tcq_declog_percoeff(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, sq_qstep_dc, sq_qstep_ac, vq_qstep_dc,
vq_qstep_ac, neob, n_coeffs, tcoeffs, "TXREC");
av2_tcq_declog_percoeff(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, sq_qstep_dc, sq_qstep_ac, vq_qstep_dc,
vq_qstep_ac, neob, n_coeffs, tcoeffs, "TCOEFF");
av2_tcq_declog_eobrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, *eob_pt_bits, *eob_extra_bits,
*eob_offset_bits);
// log per-coefficient rate cost
av2_tcq_declog_decrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, qcoeff_cost,
QCOEFF_RCOST_TYPE_SGNRATE);
av2_tcq_declog_decrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, qcoeff_cost, QCOEFF_RCOST_TYPE_BRRATE);
av2_tcq_declog_decrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, qcoeff_cost,
QCOEFF_RCOST_TYPE_LR1RATE);
av2_tcq_declog_decrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, qcoeff_cost,
QCOEFF_RCOST_TYPE_LR2RATE);
av2_tcq_declog_decrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, qcoeff_cost,
QCOEFF_RCOST_TYPE_LR3RATE);
av2_tcq_declog_decrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, qcoeff_cost,
QCOEFF_RCOST_TYPE_LR4RATE);
av2_tcq_declog_decrate(cm, dcb, plane, blk_row, blk_col, tx_size, tx_type,
is_inter, neob, qcoeff_cost, QCOEFF_RCOST_TYPE_HRRATE);
free(qcoeff);
free(qcoeff_cost);
#endif // CONFIG_COEFF_LOGS
#if CONFIG_TXFMBLK_LOGS || CONFIG_COEFF_LOGS
fflush(cm->fDecTxfmLog);
#endif // CONFIG_TXFMBLK_LOGS || CONFIG_COEFF_LOGS
cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
// DC value
set_dc_sign(&cul_level, dc_val);
return cul_level;
}
void av1_read_coeffs_txb_facade(const AV1_COMMON *const cm,
DecoderCodingBlock *dcb, aom_reader *const r,
const int plane, const int row, const int col,
const TX_SIZE tx_size) {
#if TXCOEFF_TIMER
struct aom_usec_timer timer;
aom_usec_timer_start(&timer);
#endif
MACROBLOCKD *const xd = &dcb->xd;
MB_MODE_INFO *const mbmi = xd->mi[0];
struct macroblockd_plane *const pd = &xd->plane[plane];
const BLOCK_SIZE plane_bsize = get_mb_plane_block_size(
xd, mbmi, plane, pd->subsampling_x, pd->subsampling_y);
#if !CONFIG_EXT_RECUR_PARTITIONS
assert(plane_bsize == get_plane_block_size(mbmi->sb_type[plane > 0],
pd->subsampling_x,
pd->subsampling_y));
#endif // !CONFIG_EXT_RECUR_PARTITIONS
TXB_CTX txb_ctx;
get_txb_ctx(plane_bsize, tx_size, plane, pd->above_entropy_context + col,
pd->left_entropy_context + row, &txb_ctx,
mbmi->fsc_mode[xd->tree_type == CHROMA_PART]);
#if CONFIG_COEFF_LOGS
uint64_t eob_pt_bits = 0, eob_extra_bits = 0, eob_offset_bits = 0;
const uint8_t decode_rest =
av1_read_sig_txtype(cm, dcb, r, row, col, plane, &txb_ctx, tx_size,
&eob_pt_bits, &eob_extra_bits, &eob_offset_bits);
#else
const uint8_t decode_rest =
av1_read_sig_txtype(cm, dcb, r, row, col, plane, &txb_ctx, tx_size);
#endif // CONFIG_COEFF_LOGS
const PLANE_TYPE plane_type = get_plane_type(plane);
const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
cm->features.reduced_tx_set_used);
const int is_inter = is_inter_block(mbmi, xd->tree_type);
uint8_t cul_level = 0;
if (decode_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)) {
cul_level =
av1_read_coeffs_txb_skip(cm, dcb, r, row, col, plane, tx_size);
} else {
cul_level =
av1_read_coeffs_txb(cm, dcb, r, row, col, plane, &txb_ctx, tx_size
#if CONFIG_COEFF_LOGS
,
&eob_pt_bits, &eob_extra_bits, &eob_offset_bits
#endif // CONFIG_COEFF_LOGS
);
}
}
#if CONFIG_LR_IMPROVEMENTS
else {
av1_update_txk_skip_array(cm, xd->mi_row, xd->mi_col, xd->tree_type,
&mbmi->chroma_ref_info, plane, row, col, tx_size);
}
#endif // CONFIG_LR_IMPROVEMENTS
av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, col,
row);
if (is_inter_block(mbmi, xd->tree_type)) {
#if CONFIG_INTER_IST
const TX_TYPE tx_type1 = av1_get_tx_type(xd, plane_type, row, col, tx_size,
cm->features.reduced_tx_set_used);
#endif // CONFIG_INTER_IST
if (plane == 0) {
const int txw = tx_size_wide_unit[tx_size];
const int txh = tx_size_high_unit[tx_size];
// The 16x16 unit is due to the constraint from tx_64x64 which sets the
// maximum tx size for chroma as 32x32. Coupled with 4x1 transform block
// size, the constraint takes effect in 32x16 / 16x32 size too. To solve
// the intricacy, cover all the 16x16 units inside a 64 level transform.
if (txw == tx_size_wide_unit[TX_64X64] ||
txh == tx_size_high_unit[TX_64X64]) {
const int tx_unit = tx_size_wide_unit[TX_16X16];
const int stride = xd->tx_type_map_stride;
for (int idy = 0; idy < txh; idy += tx_unit) {
for (int idx = 0; idx < txw; idx += tx_unit) {
#if CONFIG_INTER_IST
xd->tx_type_map[(row + idy) * stride + col + idx] = tx_type1;
#else
xd->tx_type_map[(row + idy) * stride + col + idx] = tx_type;
#endif // CONFIG_INTER_IST
}
}
}
}
}
#if TXCOEFF_TIMER
aom_usec_timer_mark(&timer);
const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
cm->txcoeff_timer += elapsed_time;
++cm->txb_count;
#endif
}
#if CONFIG_COEFF_LOGS
////////////////////////////////////////
/// Decoder-side Logging for AV2 TCQ experiments
////////////////////////////////////////
void av2_tcq_declog_eobrate(const struct AV1Common *const cm,
struct DecoderCodingBlock *dcb, const int plane,
const int blk_row, const int blk_col,
TX_SIZE tx_size, TX_TYPE tx_type,
const int is_inter, const int neob,
const uint64_t eob_pt_bits,
const uint64_t eob_extra_bits,
const uint64_t eob_offset_bits) {
FILE *fp = cm->fDecTxfmLog ? cm->fDecTxfmLog : stdout;
MACROBLOCKD *const xd = &dcb->xd;
fprintf(
fp,
"[AV2DEC-TXFMBLK-EOBRATE] %03d %4d %03d %03d %3d %3d %04d %04d %4d %4d",
cm->current_frame.absolute_poc, // 1: POC (picture number)
plane, // 2: plain ID (Y/U/V)
xd->mi_row, // 3: macroblock row index
xd->mi_col, // 4: macroblock coloumn index
blk_row, // 5: transform block row index
blk_col, // 6: transform block column index
tx_size, // 7: transform size
tx_type, // 8: transform type
is_inter, // 9: inter/intra mode
neob); // 10: neob
fprintf(fp, " %2d %2d %2d\n", (int)eob_pt_bits, (int)eob_extra_bits,
(int)eob_offset_bits);
}
void av2_tcq_declog_decrate(const struct AV1Common *const cm,
struct DecoderCodingBlock *dcb, const int plane,
const int blk_row, const int blk_col,
TX_SIZE tx_size, TX_TYPE tx_type,
const int is_inter, const int neob,
qcoeff_bit_cost *qcoeff_cost,
const QCOEFF_RCOST_TYPE qcoeff_rcost_type) {
FILE *fp = cm->fDecTxfmLog ? cm->fDecTxfmLog : stdout;
MACROBLOCKD *const xd = &dcb->xd;
switch (qcoeff_rcost_type) {
case QCOEFF_RCOST_TYPE_SGNRATE:
fprintf(fp, "[AV2DEC-TXFMBLK-SGNRATE] ");
break;
case QCOEFF_RCOST_TYPE_BRRATE:
fprintf(fp, "[AV2DEC-TXFMBLK-BRRATE] ");
break;
case QCOEFF_RCOST_TYPE_LR1RATE:
fprintf(fp, "[AV2DEC-TXFMBLK-LR1RATE] ");
break;
case QCOEFF_RCOST_TYPE_LR2RATE:
fprintf(fp, "[AV2DEC-TXFMBLK-LR2RATE] ");
break;
case QCOEFF_RCOST_TYPE_LR3RATE:
fprintf(fp, "[AV2DEC-TXFMBLK-LR3RATE] ");
break;
case QCOEFF_RCOST_TYPE_LR4RATE:
fprintf(fp, "[AV2DEC-TXFMBLK-LR4RATE] ");
break;
case QCOEFF_RCOST_TYPE_HRRATE:
fprintf(fp, "[AV2DEC-TXFMBLK-HRRATE] ");
break;
default:
fprintf(stderr, "WARNING: unsupported rate cost type: %d\n",
qcoeff_rcost_type);
assert(0);
}
fprintf(fp, "%03d %4d %03d %03d %3d %3d %04d %04d %4d %4d",
cm->current_frame.absolute_poc, // 1: POC (picture number)
plane, // 2: plain ID (Y/U/V)
xd->mi_row, // 3: macroblock row index
xd->mi_col, // 4: macroblock coloumn index
blk_row, // 5: transform block row index
blk_col, // 6: transform block column index
tx_size, // 7: transform size
tx_type, // 8: transform type
is_inter, // 9: inter/intra mode
neob); // 10: neob
switch (qcoeff_rcost_type) {
case QCOEFF_RCOST_TYPE_SGNRATE:
for (int c = 0; c < neob; ++c) {
fprintf(fp, " %2d", (int)qcoeff_cost[c].sign_cost);
}
break;
case QCOEFF_RCOST_TYPE_BRRATE:
for (int c = 0; c < neob; ++c) {
fprintf(fp, " %2d", (int)qcoeff_cost[c].br_cost);
}
break;
case QCOEFF_RCOST_TYPE_LR1RATE:
for (int c = 0; c < neob; ++c) {
fprintf(fp, " %2d", (int)qcoeff_cost[c].lr_cost[0]);
}
break;
case QCOEFF_RCOST_TYPE_LR2RATE:
for (int c = 0; c < neob; ++c) {
fprintf(fp, " %2d", (int)qcoeff_cost[c].lr_cost[1]);
}
break;
case QCOEFF_RCOST_TYPE_LR3RATE:
for (int c = 0; c < neob; ++c) {
fprintf(fp, " %2d", (int)qcoeff_cost[c].lr_cost[2]);
}
break;
case QCOEFF_RCOST_TYPE_LR4RATE:
for (int c = 0; c < neob; ++c) {
fprintf(fp, " %2d", (int)qcoeff_cost[c].lr_cost[3]);
}
break;
case QCOEFF_RCOST_TYPE_HRRATE:
for (int c = 0; c < neob; ++c) {
fprintf(fp, " %2d", (int)qcoeff_cost[c].hr_cost);
}
break;
default:
fprintf(stderr, "WARNING: unsupported qcoeff_rcost_type: %d\n",
qcoeff_rcost_type);
assert(0);
}
fprintf(fp, "\n");
}
void av2_tcq_declog_percoeff(
const struct AV1Common *const cm, struct DecoderCodingBlock *dcb,
const int plane, const int blk_row, const int blk_col, TX_SIZE tx_size,
TX_TYPE tx_type, const int is_inter, const int sq_step_dc,
const int sq_step_ac, const int vq_step_dc, const int vq_step_ac,
const int neob, const int n_coeffs, const int *vec, const char *tag) {
FILE *fp = cm->fDecTxfmLog ? cm->fDecTxfmLog : stdout;
MACROBLOCKD *const xd = &dcb->xd;
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
// record per-txfm-block reconstructed txform coefficients
fprintf(fp,
"[AV2DEC-TXFMBLK-%s] %03d %4d %03d %03d %3d %3d %04d %04d %4d %8d "
"%8d %8d %8d %4d %4d",
tag,
cm->current_frame.absolute_poc, // 1: POC (picture number)
plane, // 2: plain ID (Y/U/V)
xd->mi_row, // 3: macroblock row index
xd->mi_col, // 4: macroblock coloumn index
blk_row, // 5: transform block row index
blk_col, // 6: transform block column index
tx_size, // 7: transform size
tx_type, // 8: transform type
is_inter, // 9: inter/intra mode
sq_step_dc, // 10: SQ step size for DC coefficients
sq_step_ac, // 11: SQ step size for AC coefficients
vq_step_dc, // 12: TCQ step size for DC coefficients
vq_step_ac, // 13: TCQ step size for AC coefficients
n_coeffs, // 14: total number of coefficients in block
neob); // 15: neob
for (int c = 0; c < neob; ++c) {
const int val = vec[scan[c]];
fprintf(fp, " %5d", val);
}
fprintf(fp, "\n");
}
#endif // CONFIG_COEFF_LOGS