blob: bdf8f6b55097ec651c5259fd297d36dca7e5a545 [file]
/*
* Copyright (c) 2021, Alliance for Open Media. All rights reserved.
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include "av1/encoder/txb_rdopt.h"
#include "av1/encoder/txb_rdopt_utils.h"
#include "av1/encoder/rdopt_utils.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_ports/mem.h"
#include "av1/common/idct.h"
// Specialized trellis update for the last non-zero coefficient (si == eob - 1)
// when its magnitude is >= 2.
static AOM_FORCE_INLINE tran_low_t update_coeff_last(
int *accu_rate, int64_t *accu_dist, int si, int ci, tran_low_t abs_qc,
int sign, TX_CLASS tx_class, int bhl, int width, int64_t rdmult, int shift,
const int16_t *dequant, const LV_MAP_COEFF_COST *txb_costs,
const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
const qm_val_t *iqmatrix, const qm_val_t *qmatrix, int need_accu_dist) {
assert(si > 0 && ci > 0);
assert(abs_qc >= 2);
const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
const tran_low_t tqc = tcoeff[ci];
const tran_low_t dqc = dqcoeff[ci];
const int *const base_eob = txb_costs->base_eob_cost[coeff_ctx];
const tran_low_t abs_qc_low = abs_qc - 1;
int rate, rate_low;
if (abs_qc == 2) {
rate = base_eob[1] + av1_cost_literal(1);
rate_low = base_eob[0] + av1_cost_literal(1);
} else {
const int br_ctx = get_br_ctx_eob(ci, bhl, tx_class);
const int *const lps_cost = txb_costs->lps_cost[br_ctx];
const int base_rate = base_eob[2] + av1_cost_literal(1);
if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
rate_low = (abs_qc == 3)
? (base_eob[1] + av1_cost_literal(1))
: (base_rate + lps_cost[abs_qc - (2 + NUM_BASE_LEVELS)]);
} else {
rate = base_rate + get_br_cost(abs_qc, lps_cost);
rate_low = base_rate + get_br_cost(abs_qc_low, lps_cost);
}
}
int dqv = dequant[1];
if (iqmatrix != NULL) {
dqv = ((iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
}
tran_low_t qc_low, dqc_low;
get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
int64_t dist_diff_0, dist_diff_low_0;
int64_t scaled_qm = 0;
if (qmatrix == NULL) {
const int64_t tqc2 = (int64_t)tqc * 2;
dist_diff_0 = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
dist_diff_low_0 =
((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
} else {
scaled_qm = (int64_t)qmatrix[ci] << shift;
dist_diff_0 = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm);
dist_diff_low_0 = get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm);
}
const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
int64_t chosen_dist;
tran_low_t final_abs = abs_qc;
if (rd_low < rd) {
qcoeff[ci] = qc_low;
dqcoeff[ci] = dqc_low;
*accu_rate += rate_low;
chosen_dist = dist_diff_low_0;
final_abs = abs_qc_low;
} else {
*accu_rate += rate;
chosen_dist = dist_diff_0;
}
if (need_accu_dist) {
if (qmatrix != NULL) {
chosen_dist -= get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
}
*accu_dist += chosen_dist;
}
return final_abs;
}
// Cold [0, 1 << RDDIV_BITS] tie-breaker helper kept out of line to keep
// update_coeff_dc() compact.
static AOM_NOINLINE int eval_better_qm_exact(tran_low_t tqc, tran_low_t dqc,
tran_low_t dqc_low,
int64_t scaled_qm, int rate,
int rate_low, int64_t rdmult) {
const int64_t dist_diff = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) -
get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm);
const int64_t rd_thresh =
RDCOST(rdmult, rate_low, 0) - RDCOST(rdmult, rate, 0);
return dist_diff * (1 << RDDIV_BITS) > rd_thresh;
}
// Specialized trellis update for the DC coefficient (si == 0) when it is not
// the last coefficient in the block.
static AOM_FORCE_INLINE void update_coeff_dc(
int *accu_rate, TX_SIZE tx_size, TX_CLASS tx_class, int bhl,
intptr_t stride, int64_t rdmult, int shift, int dc_sign_ctx,
const int16_t *dequant, 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, const qm_val_t *qmatrix) {
const tran_low_t qc = qcoeff[0];
const int coeff_ctx =
(tx_class == TX_CLASS_2D)
? 0
: get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
if (qc == 0) {
*accu_rate += txb_costs->base_cost[coeff_ctx][0];
} else {
const int sign = (qc < 0) ? 1 : 0;
const tran_low_t abs_qc = abs(qc);
const tran_low_t tqc = tcoeff[0];
const tran_low_t dqc = dqcoeff[0];
const int dc_sign_cost = txb_costs->dc_sign_cost[dc_sign_ctx][sign];
const int *const base_cost = txb_costs->base_cost[coeff_ctx];
int rate = base_cost[AOMMIN(abs_qc, 3)] + dc_sign_cost;
const int *lps_cost = txb_costs->lps_cost[0];
if (abs_qc > NUM_BASE_LEVELS) {
const int br_ctx = get_br_ctx_stride(levels, 0, bhl, stride, tx_class);
lps_cost = txb_costs->lps_cost[br_ctx];
rate += get_br_cost(abs_qc, lps_cost);
}
tran_low_t qc_low = 0, dqc_low = 0;
tran_low_t abs_qc_low = 0;
int rate_low;
if (abs_qc == 1) {
rate_low = base_cost[0];
} else {
const int dqv = get_dqv(dequant, 0, iqmatrix);
get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
abs_qc_low = abs_qc - 1;
rate_low = base_cost[AOMMIN(abs_qc_low, 3)] + dc_sign_cost;
if (abs_qc_low > NUM_BASE_LEVELS) {
rate_low += get_br_cost(abs_qc_low, lps_cost);
}
}
const int64_t approx_rd =
((int64_t)(rate - rate_low) * rdmult) >> AV1_PROB_COST_SHIFT;
const int64_t sum_term = (int64_t)tqc * 2 - (int64_t)dqc - (int64_t)dqc_low;
const int64_t dist_term = ((int64_t)dqc - (int64_t)dqc_low) * sum_term;
int better;
if (qmatrix == NULL) {
const int64_t dd_pos = dist_term * (1LL << (2 * shift + RDDIV_BITS));
if (approx_rd >= dd_pos) {
if (approx_rd > dd_pos) {
better = 1;
} else {
better =
RDCOST(rdmult, rate, 0) - RDCOST(rdmult, rate_low, 0) > dd_pos;
}
} else {
better = 0;
}
} else {
const int scaled_qm = qmatrix[0] << shift;
const int sq_qm = scaled_qm * scaled_qm;
const int64_t approx_dd = (dist_term * sq_qm) >> (2 * AOM_QM_BITS);
// Floor-shifted diffs underestimate rounded values by 0 or 1: both
// R1 - R0 - approx_rd and D0 - D1 - approx_dd lie in {0, 1}. For exact
// rd - rd_low = margin + e_R - e_D * (1 << RDDIV_BITS), margin < 0
// guarantees exact <= 0 and margin > (1 << RDDIV_BITS) guarantees > 0.
const int64_t margin = approx_rd - (approx_dd * (1 << RDDIV_BITS));
if (margin >= 0) {
if (margin > (1 << RDDIV_BITS)) {
better = 1;
} else {
better = eval_better_qm_exact(tqc, dqc, dqc_low, scaled_qm, rate,
rate_low, rdmult);
}
} else {
better = 0;
}
}
if (better) {
qcoeff[0] = qc_low;
dqcoeff[0] = dqc_low;
levels[0] = AOMMIN(abs_qc_low, INT8_MAX);
*accu_rate += rate_low;
} else {
*accu_rate += rate;
}
}
}
/*!\cond */
typedef struct {
int64_t rdmult;
int64_t accu_dist;
const LV_MAP_EOB_COST *txb_eob_costs;
const LV_MAP_COEFF_COST *txb_costs;
const int16_t *dequant;
const tran_low_t *tcoeff;
tran_low_t *qcoeff;
tran_low_t *dqcoeff;
uint8_t *levels;
const qm_val_t *iqmatrix;
const qm_val_t *qmatrix;
int *nz_ci;
intptr_t stride;
int eob;
int nz_num;
int accu_rate;
int bhl;
int width;
int dc_sign_ctx;
int shift;
int dequant_ac;
int16_t sharpness;
TX_CLASS tx_class;
int min_eob_cutoff;
} TxbOptCtx;
/*!\endcond */
enum { kMaxNzNum = 2 };
// Keep trellis reduction helpers out-of-line to limit register pressure in
// the hot interior scan loops.
static AOM_NOINLINE int update_coeff_simple_gt1_try_lower(
intptr_t ci, tran_low_t abs_qc, intptr_t coeff_ctx, tran_low_t abs_dqc,
const TxbOptCtx *restrict ctx, tran_low_t abs_tqc) {
assert(ci > 0);
assert(abs_qc >= 2);
const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
const int *const base_cost = txb_costs->base_cost[coeff_ctx];
// update_coeff_simple_gt1_try_lower is only invoked for AC coefficients
// (si > 0), so dequant_ac (dequant[1]) applies without a (ci > 0) branch.
if (abs_qc == 2) {
int dqv = ctx->dequant_ac;
if (ctx->iqmatrix != NULL) {
dqv =
((ctx->iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
}
const int shift = ctx->shift;
const tran_low_t abs_dqc_low = dqv >> shift;
const qm_val_t *restrict qmatrix = ctx->qmatrix;
int64_t dist_diff_0, dist_diff_low_0;
if (qmatrix == NULL) {
const int64_t abs_tqc2 = (int64_t)abs_tqc << 1;
dist_diff_0 =
((int64_t)abs_dqc * (abs_dqc - abs_tqc2)) * (1LL << (2 * shift));
dist_diff_low_0 = ((int64_t)abs_dqc_low * (abs_dqc_low - abs_tqc2)) *
(1LL << (2 * shift));
} else {
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
dist_diff_0 = get_coeff_dist_scaled_qm(abs_tqc, abs_dqc, scaled_qm);
dist_diff_low_0 =
get_coeff_dist_scaled_qm(abs_tqc, abs_dqc_low, scaled_qm);
}
const int rate = base_cost[6];
const int rate_low = base_cost[5];
const int64_t rdmult = ctx->rdmult;
const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
if (rd_low < rd) {
tran_low_t *const qcoeff = ctx->qcoeff;
const int sign = (qcoeff[ci] < 0) ? 1 : 0;
qcoeff[ci] = (-sign ^ 1) + sign;
ctx->dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
ctx->levels[get_padded_idx((int)ci, ctx->bhl)] = 1;
return rate_low;
}
return rate;
}
const int br_ctx = get_br_ctx_simple_stride(ctx->levels, (int)ci, ctx->bhl,
ctx->stride, ctx->tx_class);
const int *const lps_cost = txb_costs->lps_cost[br_ctx];
const int base_rate = base_cost[7];
int rate, rate_low;
if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
rate_low = (abs_qc == 3)
? base_cost[6]
: (base_rate + lps_cost[abs_qc - (2 + NUM_BASE_LEVELS)]);
} else {
rate = base_rate + get_br_cost(abs_qc, lps_cost);
rate_low = base_rate + get_br_cost(abs_qc - 1, lps_cost);
}
int dqv = ctx->dequant_ac;
if (ctx->iqmatrix != NULL) {
dqv = ((ctx->iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
}
const int shift = ctx->shift;
const tran_low_t abs_qc_low = abs_qc - 1;
const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
const qm_val_t *restrict qmatrix = ctx->qmatrix;
int64_t dist_diff_0, dist_diff_low_0;
if (qmatrix == NULL) {
const int64_t abs_tqc2 = (int64_t)abs_tqc << 1;
dist_diff_0 =
((int64_t)abs_dqc * (abs_dqc - abs_tqc2)) * (1LL << (2 * shift));
dist_diff_low_0 = ((int64_t)abs_dqc_low * (abs_dqc_low - abs_tqc2)) *
(1LL << (2 * shift));
} else {
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
dist_diff_0 = get_coeff_dist_scaled_qm(abs_tqc, abs_dqc, scaled_qm);
dist_diff_low_0 = get_coeff_dist_scaled_qm(abs_tqc, abs_dqc_low, scaled_qm);
}
const int64_t rdmult = ctx->rdmult;
const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
if (rd_low < rd) {
tran_low_t *const qcoeff = ctx->qcoeff;
const int sign = (qcoeff[ci] < 0) ? 1 : 0;
qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
ctx->dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
ctx->levels[get_padded_idx((int)ci, ctx->bhl)] =
AOMMIN(abs_qc_low, INT8_MAX);
return rate_low;
}
return rate;
}
static AOM_NOINLINE int update_coeff_simple_gt1(intptr_t ci, tran_low_t qc,
intptr_t coeff_ctx,
TX_CLASS tx_class,
const TxbOptCtx *restrict ctx) {
assert(ci > 0);
const tran_low_t abs_tqc = abs(ctx->tcoeff[ci]);
const tran_low_t abs_dqc = abs(ctx->dqcoeff[ci]);
const tran_low_t abs_qc = abs(qc);
if (abs_dqc >= abs_tqc) {
return update_coeff_simple_gt1_try_lower(ci, abs_qc, coeff_ctx, abs_dqc,
ctx, abs_tqc);
}
const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
const int *const base_cost = txb_costs->base_cost[coeff_ctx];
if (abs_qc == 2) return base_cost[6];
const int br_ctx = get_br_ctx_simple_stride(ctx->levels, (int)ci, ctx->bhl,
ctx->stride, tx_class);
const int *const lps_cost = txb_costs->lps_cost[br_ctx];
const int base_rate = base_cost[7];
if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
return base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
}
return base_rate + get_br_cost(abs_qc, lps_cost);
}
static AOM_NOINLINE int update_coeff_simple_nz(intptr_t ci, tran_low_t qc,
intptr_t coeff_ctx,
TX_CLASS tx_class,
const TxbOptCtx *restrict ctx) {
assert(ci > 0);
if ((unsigned int)(qc + 1) > 2) {
return update_coeff_simple_gt1(ci, qc, coeff_ctx, tx_class, ctx);
}
const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
const int *const base_cost = txb_costs->base_cost[coeff_ctx];
const int rate = base_cost[5];
const tran_low_t abs_tqc = abs(ctx->tcoeff[ci]);
const tran_low_t abs_dqc = abs(ctx->dqcoeff[ci]);
if (abs_dqc < abs_tqc) {
return rate;
}
const qm_val_t *restrict qmatrix = ctx->qmatrix;
const int shift = ctx->shift;
int64_t dist_diff_0;
if (qmatrix == NULL) {
dist_diff_0 = ((int64_t)abs_dqc * (abs_dqc - ((int64_t)abs_tqc * 2))) *
(1LL << (2 * shift));
} else {
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
const int64_t dist0 = get_coeff_dist_scaled_qm(abs_tqc, 0, scaled_qm);
dist_diff_0 = get_coeff_dist_scaled_qm(abs_tqc, abs_dqc, scaled_qm) - dist0;
}
const int rate_low = base_cost[0];
const int64_t rdmult = ctx->rdmult;
const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
const int64_t rd_low = RDCOST(rdmult, rate_low, 0);
if (rd_low < rd) {
ctx->qcoeff[ci] = 0;
ctx->dqcoeff[ci] = 0;
ctx->levels[get_padded_idx((int)ci, ctx->bhl)] = 0;
return rate_low;
}
return rate;
}
static AOM_FORCE_INLINE int64_t
calc_coeff_dist(tran_low_t tqc, tran_low_t dqc, int shift,
const qm_val_t *restrict qmatrix, int ci) {
if (qmatrix == NULL) {
const int64_t tqc2 = (int64_t)tqc * 2;
return ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
}
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
const int64_t dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
return get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
}
static AOM_NOINLINE int update_coeff_eob_gt1(int si, int ci, tran_low_t qc,
int coeff_ctx, TX_CLASS tx_class,
TxbOptCtx *restrict ctx) {
const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
const tran_low_t abs_qc = abs(qc);
assert(abs_qc >= 2);
const tran_low_t abs_qc_low = abs_qc - 1;
const int bhl = ctx->bhl;
const intptr_t stride = ctx->stride;
uint8_t *restrict levels = ctx->levels;
const int sign = (qc < 0) ? 1 : 0;
const int sign_cost = (ci == 0)
? txb_costs->dc_sign_cost[ctx->dc_sign_ctx][sign]
: av1_cost_literal(1);
const int *const base_cost = txb_costs->base_cost[coeff_ctx];
int rate, rate_low;
if (abs_qc == 2) {
rate = base_cost[2] + sign_cost;
rate_low = base_cost[1] + sign_cost;
} else {
const int br_ctx = get_br_ctx_stride(levels, ci, bhl, stride, tx_class);
const int *const lps_cost = txb_costs->lps_cost[br_ctx];
const int base_rate = base_cost[3] + sign_cost;
if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
rate_low = (abs_qc == 3)
? (base_cost[2] + sign_cost)
: (base_rate + lps_cost[abs_qc - (2 + NUM_BASE_LEVELS)]);
} else {
rate = base_rate + get_br_cost(abs_qc, lps_cost);
rate_low = base_rate + get_br_cost(abs_qc_low, lps_cost);
}
}
const tran_low_t tqc = ctx->tcoeff[ci];
const tran_low_t dqc = ctx->dqcoeff[ci];
const int shift = ctx->shift;
const qm_val_t *restrict qmatrix = ctx->qmatrix;
int dqv = ctx->dequant_ac;
if (ci == 0) dqv = ctx->dequant[0];
if (ctx->iqmatrix != NULL) {
dqv = ((ctx->iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
}
tran_low_t qc_low, dqc_low;
get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
int64_t dist, dist_low;
if (qmatrix == NULL) {
const int64_t tqc2 = (int64_t)tqc * 2;
dist = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
dist_low = ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
} else {
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
const int64_t dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
dist = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
dist_low = get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm) - dist0;
}
const int64_t dist_orig = dist;
const int64_t rdmult = ctx->rdmult;
int accu_rate = ctx->accu_rate;
const int64_t accu_dist = ctx->accu_dist;
int64_t rd = RDCOST(rdmult, accu_rate + rate, accu_dist + dist);
int lower_level = 0;
const int min_eob_cutoff = ctx->min_eob_cutoff;
const int sharpness = ctx->sharpness;
const int qc_threshold = (si <= min_eob_cutoff) ? 2 : 1;
const int allow_lower_qc = sharpness ? (abs_qc > qc_threshold) : 1;
if (allow_lower_qc) {
const int64_t rd_low =
RDCOST(rdmult, accu_rate + rate_low, accu_dist + dist_low);
if (rd_low < rd) {
lower_level = 1;
rd = rd_low;
rate = rate_low;
dist = dist_low;
}
}
const int new_eob = si + 1;
if (sharpness == 0 || new_eob >= min_eob_cutoff) {
const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, ctx->width, si);
const int new_eob_cost =
get_eob_cost(new_eob, ctx->txb_eob_costs, txb_costs, tx_class);
const int *const base_eob_cost =
txb_costs->base_eob_cost[coeff_ctx_new_eob];
int rate_coeff_eob, rate_coeff_eob_low;
if (abs_qc == 2) {
rate_coeff_eob = new_eob_cost + base_eob_cost[1] + sign_cost;
rate_coeff_eob_low = new_eob_cost + base_eob_cost[0] + sign_cost;
} else {
const int br_ctx_eob = get_br_ctx_eob(ci, bhl, tx_class);
const int *const lps_cost_eob = txb_costs->lps_cost[br_ctx_eob];
const int base_eob_rate = new_eob_cost + base_eob_cost[2] + sign_cost;
if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
rate_coeff_eob =
base_eob_rate + lps_cost_eob[abs_qc - (1 + NUM_BASE_LEVELS)];
rate_coeff_eob_low =
(abs_qc == 3) ? (new_eob_cost + base_eob_cost[1] + sign_cost)
: (base_eob_rate +
lps_cost_eob[abs_qc - (2 + NUM_BASE_LEVELS)]);
} else {
rate_coeff_eob = base_eob_rate + get_br_cost(abs_qc, lps_cost_eob);
rate_coeff_eob_low =
base_eob_rate + get_br_cost(abs_qc_low, lps_cost_eob);
}
}
int lower_level_new_eob = 0;
int64_t dist_new_eob = dist_orig;
int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
const int64_t rd_new_eob_low = RDCOST(rdmult, rate_coeff_eob_low, dist_low);
if (rd_new_eob_low < rd_new_eob) {
lower_level_new_eob = 1;
rd_new_eob = rd_new_eob_low;
rate_coeff_eob = rate_coeff_eob_low;
dist_new_eob = dist_low;
}
if (rd_new_eob < rd) {
tran_low_t *restrict qcoeff = ctx->qcoeff;
tran_low_t *restrict dqcoeff = ctx->dqcoeff;
const int *restrict nz_ci = ctx->nz_ci;
const int nz_num = ctx->nz_num;
for (int ni = 0; ni < nz_num; ++ni) {
int last_ci = nz_ci[ni];
levels[get_padded_idx(last_ci, bhl)] = 0;
qcoeff[last_ci] = 0;
dqcoeff[last_ci] = 0;
}
ctx->eob = new_eob;
ctx->nz_num = 0;
accu_rate = rate_coeff_eob;
ctx->accu_dist = dist_new_eob;
lower_level = lower_level_new_eob;
} else {
accu_rate += rate;
ctx->accu_dist = accu_dist + dist;
}
} else {
accu_rate += rate;
ctx->accu_dist = accu_dist + dist;
}
ctx->nz_ci[ctx->nz_num] = ci;
++ctx->nz_num;
if (lower_level) {
ctx->qcoeff[ci] = qc_low;
ctx->dqcoeff[ci] = dqc_low;
levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
}
return accu_rate;
}
static AOM_NOINLINE int update_coeff_eob_eq1(int si, int ci, tran_low_t qc,
int coeff_ctx, TX_CLASS tx_class,
TxbOptCtx *restrict ctx) {
assert(abs(qc) == 1);
const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
const int sign_cost = (ci == 0)
? txb_costs->dc_sign_cost[ctx->dc_sign_ctx][qc < 0]
: av1_cost_literal(1);
const int *const base_cost = txb_costs->base_cost[coeff_ctx];
int rate = base_cost[1] + sign_cost;
const int bhl = ctx->bhl;
const int new_eob = si + 1;
const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, ctx->width, si);
const int new_eob_cost =
get_eob_cost(new_eob, ctx->txb_eob_costs, txb_costs, tx_class);
const int rate_coeff_eob =
new_eob_cost + txb_costs->base_eob_cost[coeff_ctx_new_eob][0] + sign_cost;
const int64_t rdmult = ctx->rdmult;
int accu_rate = ctx->accu_rate;
const int64_t accu_dist = ctx->accu_dist;
int new_eob_wins = 0;
if (ctx->sharpness != 0) {
// RDCOST() is linear in distortion, so dist cancels between rd_new_eob
// and rd; compare directly without computing dist first.
if (RDCOST(rdmult, rate_coeff_eob, 0) >=
RDCOST(rdmult, accu_rate + rate, accu_dist)) {
const int nz_num = ctx->nz_num;
// Once nz_num exceeds kMaxNzNum, update_coeff_eob_facade() exits to
// update_coeff_simple_facade() / update_coeff_dc(), which never read
// accu_dist.
if (nz_num < kMaxNzNum) {
ctx->accu_dist =
accu_dist + calc_coeff_dist(ctx->tcoeff[ci], ctx->dqcoeff[ci],
ctx->shift, ctx->qmatrix, ci);
}
ctx->nz_ci[nz_num] = ci;
ctx->nz_num = nz_num + 1;
return accu_rate + rate;
}
new_eob_wins = 1;
}
const int64_t dist_orig = calc_coeff_dist(ctx->tcoeff[ci], ctx->dqcoeff[ci],
ctx->shift, ctx->qmatrix, ci);
int64_t dist = dist_orig;
int lower_level = 0;
if (!new_eob_wins) {
assert(ctx->sharpness == 0);
int64_t rd = RDCOST(rdmult, accu_rate + rate, accu_dist + dist);
const int rate_low = base_cost[0];
const int64_t rd_low = RDCOST(rdmult, accu_rate + rate_low, accu_dist);
if (rd_low < rd) {
lower_level = 1;
rd = rd_low;
rate = rate_low;
dist = 0;
}
new_eob_wins = RDCOST(rdmult, rate_coeff_eob, dist_orig) < rd;
}
uint8_t *restrict levels = ctx->levels;
if (new_eob_wins) {
tran_low_t *restrict qcoeff = ctx->qcoeff;
tran_low_t *restrict dqcoeff = ctx->dqcoeff;
int *restrict nz_ci = ctx->nz_ci;
const int nz_num = ctx->nz_num;
for (int ni = 0; ni < nz_num; ++ni) {
int last_ci = nz_ci[ni];
levels[get_padded_idx(last_ci, bhl)] = 0;
qcoeff[last_ci] = 0;
dqcoeff[last_ci] = 0;
}
ctx->eob = new_eob;
ctx->nz_num = 1;
nz_ci[0] = ci;
ctx->accu_dist = dist_orig;
return rate_coeff_eob;
}
accu_rate += rate;
ctx->accu_dist = accu_dist + dist;
if (lower_level) {
ctx->qcoeff[ci] = 0;
ctx->dqcoeff[ci] = 0;
levels[get_padded_idx(ci, bhl)] = 0;
return accu_rate;
}
ctx->nz_ci[ctx->nz_num] = ci;
++ctx->nz_num;
return accu_rate;
}
static AOM_NOINLINE int update_coeff_eob_nz(int si, int ci, tran_low_t qc,
int coeff_ctx, TX_CLASS tx_class,
TxbOptCtx *restrict ctx) {
const tran_low_t abs_qc = abs(qc);
assert(abs_qc >= 1);
// Skip trellis lowering once si + 1 < min_eob_cutoff with abs_qc <= 2 when
// sharpness > 0 (since allow_lower_qc == 0 and no future new_eob can fire).
if (ctx->sharpness != 0 && si + 1 < ctx->min_eob_cutoff && abs_qc <= 2) {
const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
const int rate = (ci == 0)
? (txb_costs->base_cost[coeff_ctx][abs_qc] +
txb_costs->dc_sign_cost[ctx->dc_sign_ctx][qc < 0])
: txb_costs->base_cost[coeff_ctx][4 + abs_qc];
ctx->nz_ci[ctx->nz_num] = ci;
++ctx->nz_num;
return ctx->accu_rate + rate;
}
if (abs_qc > 1) {
return update_coeff_eob_gt1(si, ci, qc, coeff_ctx, tx_class, ctx);
}
return update_coeff_eob_eq1(si, ci, qc, coeff_ctx, tx_class, ctx);
}
static inline void update_skip(int *accu_rate, int64_t accu_dist, int *eob,
int nz_num, int *nz_ci, int64_t rdmult,
int skip_cost, int non_skip_cost,
tran_low_t *qcoeff, tran_low_t *dqcoeff) {
const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
if (rd_new_eob < rd) {
for (int i = 0; i < nz_num; ++i) {
const int ci = nz_ci[i];
qcoeff[ci] = 0;
dqcoeff[ci] = 0;
// no need to set up levels because this is the last step
// levels[get_padded_idx(ci, bhl)] = 0;
}
*accu_rate = 0;
*eob = 0;
}
}
// TODO(angiebird): use this function whenever it's possible
static inline 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) {
if (plane > 0) return 0;
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
const MB_MODE_INFO *mbmi = xd->mi[0];
const int is_inter = is_inter_block(mbmi);
const TxSetType set_type =
av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
if (av1_num_ext_tx_set[set_type] > 1 &&
!xd->lossless[xd->mi[0]->segment_id]) {
const int ext_tx_set = ext_tx_set_index[is_inter][set_type];
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;
return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size]
[intra_dir][tx_type];
}
}
}
return 0;
}
static AOM_FORCE_INLINE void update_coeff_eob_facade(
int *si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, intptr_t stride,
const int16_t *restrict scan, const LV_MAP_COEFF_COST *restrict txb_costs,
const tran_low_t *qcoeff, const uint8_t *levels, int max_nz_num,
TxbOptCtx *ctx) {
int s = *si;
int local_rate = ctx->accu_rate;
const int8_t *const nz_map_ctx =
(tx_class == TX_CLASS_2D) ? av1_nz_map_ctx_offset[tx_size] : NULL;
const int (*base_cost)[8] = txb_costs->base_cost;
while (s >= 1 && ctx->nz_num <= max_nz_num) {
int ci;
tran_low_t qc;
int coeff_ctx;
do {
ci = scan[s];
coeff_ctx = get_lower_levels_ctx_simple_fast(levels, ci, bhl, stride,
nz_map_ctx, tx_class);
qc = qcoeff[ci];
if (qc != 0) break;
local_rate += base_cost[coeff_ctx][0];
} while (--s >= 1);
if (s < 1) break;
ctx->accu_rate = local_rate;
local_rate = update_coeff_eob_nz(s, ci, qc, coeff_ctx, tx_class, ctx);
--s;
}
ctx->accu_rate = local_rate;
*si = s;
}
static AOM_FORCE_INLINE void update_coeff_simple_facade(
int si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, intptr_t stride,
const int16_t *restrict scan, const LV_MAP_COEFF_COST *restrict txb_costs,
const tran_low_t *qcoeff, const uint8_t *levels, int sharpness,
TxbOptCtx *ctx) {
assert(si >= 1);
const int16_t *scan_p = scan + si;
const int16_t *const scan_end = scan + 1;
int local_rate = ctx->accu_rate;
const int8_t *const nz_map_ctx =
(tx_class == TX_CLASS_2D) ? av1_nz_map_ctx_offset[tx_size] : NULL;
const int (*const local_base_cost)[8] = txb_costs->base_cost;
if (sharpness != 0) {
for (;;) {
intptr_t ci;
tran_low_t qc;
int coeff_ctx;
for (;;) {
ci = *scan_p;
assert(ci > 0);
coeff_ctx = get_lower_levels_ctx_simple_fast(
levels, (int)ci, bhl, stride, nz_map_ctx, tx_class);
qc = qcoeff[ci];
if ((unsigned int)(qc + 1) > 2) break;
local_rate += local_base_cost[coeff_ctx][4 + (qc & 1)];
if (--scan_p < scan_end) goto simple_done;
}
local_rate += update_coeff_simple_gt1(ci, qc, coeff_ctx, tx_class, ctx);
if (--scan_p < scan_end) break;
}
} else {
for (;;) {
intptr_t ci;
tran_low_t qc;
int coeff_ctx;
for (;;) {
ci = *scan_p;
assert(ci > 0);
coeff_ctx = get_lower_levels_ctx_simple_fast(
levels, (int)ci, bhl, stride, nz_map_ctx, tx_class);
qc = qcoeff[ci];
if (qc != 0) break;
local_rate += local_base_cost[coeff_ctx][0];
if (--scan_p < scan_end) goto simple_done;
}
local_rate += update_coeff_simple_nz(ci, qc, coeff_ctx, tx_class, ctx);
if (--scan_p < scan_end) break;
}
}
simple_done:
ctx->accu_rate = local_rate;
}
static inline int64_t get_txb_rdmult(const struct AV1_COMP *cpi,
const MACROBLOCK *x, const MACROBLOCKD *xd,
int plane, PLANE_TYPE plane_type,
int sharpness, int *is_noise_pattern_out) {
const MB_MODE_INFO *mbmi = xd->mi[0];
const int is_inter = is_inter_block(mbmi);
int is_noise_pattern = 0;
#if CONFIG_AV1_HIGHBITDEPTH
if (xd->bd > 8 && sharpness == 3 && plane == AOM_PLANE_Y) {
int64_t src_var, rec_var;
av1_get_variance_stats(x, &src_var, &rec_var);
if (src_var > rec_var) {
const int num_pixels =
block_size_wide[mbmi->bsize] * block_size_high[mbmi->bsize];
const int64_t src_var_per_px = src_var / num_pixels;
if (src_var_per_px < 64) {
is_noise_pattern = 1;
}
}
}
#else
(void)plane;
#endif
if (is_noise_pattern_out != NULL) *is_noise_pattern_out = is_noise_pattern;
const int rshift = (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_IQ ||
cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIMULACRA2)
? 7
: (is_noise_pattern ? 6 : 5);
const int (*trellis_rd_mult)[2] = cpi->sf.tx_sf.use_chroma_trellis_rd_mult
? plane_rd_mult_chroma
: plane_rd_mult;
return ROUND_POWER_OF_TWO(
(int64_t)x->rdmult * (8 - sharpness) *
(trellis_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))),
rshift);
}
// 2D scans are diagonal: the first 10 (36) positions stay in columns 0..3
// (0..7 when height >= 8). Neighbor reads reach col + 2, which falls inside
// the TX_PAD_BOTTOM (4) zero-padded trailing columns past the initialized
// width.
static AOM_FORCE_INLINE int get_init_levels_width_2d(int width, int bhl,
int eob) {
static_assert(TX_PAD_BOTTOM == 4, "");
assert(width >= 4 && (width & 3) == 0);
assert(bhl >= MIN_TX_SIZE_LOG2 && bhl <= MAX_TX_SIZE_LOG2 - 1);
assert(eob > 1);
if (eob <= 10) return 4;
if (eob <= 36 && bhl >= 3) return AOMMIN(width, 8);
return width;
}
// Fast path for 2D blocks with si <= 3 (eob <= 5, sharpness != 0) when
// qcoeff[scan[0..si]] are in [-2, 2] and no remaining coeff can be lowered
// (qc_dc == 0 whenever nz_num exceeds max_nz_num, plus |qc1| <= 1 if si == 3).
// Relies on 2D scan geometry: nz_off[scan[1]] == nz_off[scan[2]], and scan[3]
// is either 2 * scan[2] or 2 * scan[1].
static AOM_FORCE_INLINE int optimize_txb_2d_fast(
const MACROBLOCK *x, const MACROBLOCKD *xd, const AV1_COMMON *cm,
const struct macroblock_plane *p, int plane, int block, TX_SIZE tx_size,
TX_TYPE tx_type, const TXB_CTX *txb_ctx, const LV_MAP_COEFF_COST *txb_costs,
const int16_t *scan, const tran_low_t *qcoeff, int si, int ci,
tran_low_t last_abs_qc, int accu_rate, int *rate_cost) {
assert(si >= 0 && si <= 3);
assert(tx_type_to_class[tx_type] == TX_CLASS_2D);
assert(scan[0] == 0 && last_abs_qc >= 1);
static_assert(kMaxNzNum == 2, "");
const tran_low_t qc_dc = qcoeff[0];
if ((unsigned int)(qc_dc + 2) > 4) return 0;
unsigned int ac_cul = (unsigned int)last_abs_qc;
if (si != 0) {
const int ci1 = scan[1];
const tran_low_t qc1 = qcoeff[ci1];
if ((unsigned int)(qc1 + 2) > 4) return 0;
const unsigned int abs_qc1 = (unsigned int)abs(qc1);
const int8_t *const nz_off = av1_nz_map_ctx_offset[tx_size];
const int off1 = (uint8_t)nz_off[ci1];
if (si == 1) {
accu_rate += txb_costs->base_cost[off1][4 + abs_qc1];
ac_cul += abs_qc1;
} else {
const int ci2 = scan[2];
const tran_low_t qc2 = qcoeff[ci2];
if ((unsigned int)(qc2 + 2) > 4) return 0;
const unsigned int abs_qc2 = (unsigned int)abs(qc2);
assert(off1 == (uint8_t)nz_off[ci2]);
if (si == 2) {
if (qc_dc != 0 && qc1 != 0 && qc2 != 0) return 0;
const int ctx3 = 1 + (last_abs_qc >= 3);
const int is_sq3 = (ci == 2 * ci2);
assert(ci == 2 * ci2 || ci == 2 * ci1);
accu_rate +=
txb_costs->base_cost[off1 + (is_sq3 ? ctx3 : 0)][4 + abs_qc2] +
txb_costs->base_cost[off1 + (is_sq3 ? 0 : ctx3)][4 + abs_qc1];
ac_cul += abs_qc2 + abs_qc1;
} else {
const int ci3 = scan[3];
const tran_low_t qc3 = qcoeff[ci3];
if ((unsigned int)(qc3 + 2) > 4 ||
!((qc3 == 0 || qc2 == 0)
? (qc_dc == 0 || (qc3 == 0 && qc2 == 0) || qc1 == 0)
: (qc_dc == 0 && (unsigned int)(qc1 + 1) <= 2))) {
return 0;
}
const int last_mag = (int)AOMMIN(last_abs_qc, 3);
const unsigned int abs_qc3 = (unsigned int)abs(qc3);
const int is_sq3 = (ci3 == 2 * ci2);
assert(ci3 == 2 * ci2 || ci3 == 2 * ci1);
assert(ci == ci1 + ci2);
const int mag2 = last_mag + (is_sq3 ? (int)abs_qc3 : 0);
const int mag1 = last_mag + (is_sq3 ? 0 : (int)abs_qc3);
accu_rate +=
txb_costs->base_cost[(uint8_t)nz_off[ci3]][4 + abs_qc3] +
txb_costs->base_cost[off1 + ((mag2 + 1) >> 1)][4 + abs_qc2] +
txb_costs->base_cost[off1 + ((mag1 + 1) >> 1)][4 + abs_qc1];
ac_cul += abs_qc3 + abs_qc2 + abs_qc1;
}
}
}
const unsigned int abs_dc = (unsigned int)abs(qc_dc);
if (qc_dc == 0) {
accu_rate += txb_costs->base_cost[0][0];
} else {
accu_rate += txb_costs->base_cost[0][abs_dc] +
txb_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][qc_dc < 0];
}
int cul = (int)AOMMIN(abs_dc + ac_cul, (unsigned int)COEFF_CONTEXT_MASK);
set_dc_sign(&cul, qc_dc);
p->txb_entropy_ctx[block] = (uint8_t)cul;
const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
cm->features.reduced_tx_set_used);
*rate_cost = accu_rate + txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0] +
tx_type_cost;
return 1;
}
// Keep av1_optimize_txb_multi() out-of-line so the eob == 1 fast path in
// av1_optimize_txb() stays small and spill-free.
AOM_NOINLINE
int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
int block, TX_SIZE tx_size, TX_TYPE tx_type,
const TXB_CTX *const txb_ctx, int *rate_cost,
int sharpness) {
MACROBLOCKD *xd = &x->e_mbd;
const struct macroblock_plane *p = &x->plane[plane];
int eob = p->eobs[block];
assert(eob > 1);
const PLANE_TYPE plane_type = get_plane_type(plane);
const int seg_id = xd->mi[0]->segment_id;
// Resolve qmatrix pointers before get_txb_rdmult() so xd->mi[0] is loaded
// once alongside use_intrabc before av1_get_variance_stats().
const qm_val_t *const iqmatrix =
tx_type < IDTX ? xd->plane[plane].seg_iqmatrix[seg_id][tx_size] : NULL;
const qm_val_t *const qmatrix =
(cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR &&
tx_type < IDTX)
? xd->plane[plane].seg_qmatrix[seg_id][tx_size]
: NULL;
int is_noise_pattern = 0;
const int64_t rdmult = get_txb_rdmult(cpi, x, xd, plane, plane_type,
sharpness, &is_noise_pattern);
const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
const int16_t *scan = scan_order->scan;
const int shift = av1_get_tx_scale(tx_size);
const int16_t *dequant = p->dequant_QTX;
const int block_offset = BLOCK_OFFSET(block);
tran_low_t *qcoeff = p->qcoeff + block_offset;
tran_low_t *dqcoeff = p->dqcoeff + block_offset;
const tran_low_t *tcoeff = p->coeff + block_offset;
const CoeffCosts *coeff_costs = &x->coeff_costs;
const AV1_COMMON *cm = &cpi->common;
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
const TX_CLASS tx_class = tx_type_to_class[tx_type];
const int bhl = get_txb_bhl(tx_size);
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
assert(height == (1 << bhl));
const LV_MAP_COEFF_COST *txb_costs =
&coeff_costs->coeff_costs[txs_ctx][plane_type];
const int eob_multi_size = txsize_log2_minus4[tx_size];
const LV_MAP_EOB_COST *txb_eob_costs =
&coeff_costs->eob_costs[eob_multi_size][plane_type];
const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
int accu_rate = eob_cost;
int64_t accu_dist = 0;
int si = eob - 1;
const int ci = scan[si];
const tran_low_t qc = qcoeff[ci];
tran_low_t last_abs_qc = abs(qc);
const int sign = qc < 0;
const int min_eob_cutoff = is_noise_pattern ? 8 : 5;
// accu_dist is only read by new_eob truncation (>= min_eob_cutoff when
// sharpness != 0) and update_skip() (sharpness == 0), so it can be skipped
// when sharpness != 0 && eob <= min_eob_cutoff (dist cancels in rd_low < rd).
const int need_accu_dist = sharpness == 0 || eob > min_eob_cutoff;
const int max_nz_num = kMaxNzNum;
int nz_ci[kMaxNzNum + 1] = { ci, 0, 0 };
if (last_abs_qc >= 2) {
last_abs_qc = update_coeff_last(&accu_rate, &accu_dist, si, ci, last_abs_qc,
sign, tx_class, bhl, width, rdmult, shift,
dequant, txb_costs, tcoeff, qcoeff, dqcoeff,
iqmatrix, qmatrix, need_accu_dist);
} else {
assert(last_abs_qc == 1);
const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
accu_rate += txb_costs->base_eob_cost[coeff_ctx][0] + av1_cost_literal(1);
if (need_accu_dist) {
const tran_low_t tqc = tcoeff[ci];
const tran_low_t dqc = dqcoeff[ci];
if (qmatrix == NULL) {
accu_dist +=
((int64_t)dqc * (dqc - (int64_t)tqc * 2)) * (1LL << (2 * shift));
} else {
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
accu_dist += get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) -
get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
}
}
}
--si;
if (sharpness != 0 && si <= 3 && tx_class == TX_CLASS_2D) {
if (optimize_txb_2d_fast(x, xd, cm, p, plane, block, tx_size, tx_type,
txb_ctx, txb_costs, scan, qcoeff, si, ci,
last_abs_qc, accu_rate, rate_cost)) {
return eob;
}
}
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, height);
if (si >= 1 || tx_class != TX_CLASS_2D || (unsigned int)(qcoeff[0] + 2) > 4) {
const int init_w = (tx_class == TX_CLASS_2D)
? get_init_levels_width_2d(width, bhl, eob)
: width;
av1_txb_init_levels(qcoeff, init_w, height, levels);
}
const intptr_t stride = (1 << bhl) + TX_PAD_HOR;
TxbOptCtx ctx = { rdmult,
accu_dist,
txb_eob_costs,
txb_costs,
dequant,
tcoeff,
qcoeff,
dqcoeff,
levels,
iqmatrix,
qmatrix,
nz_ci,
stride,
eob,
1,
accu_rate,
bhl,
width,
txb_ctx->dc_sign_ctx,
shift,
dequant[1],
(int16_t)sharpness,
tx_class,
min_eob_cutoff };
TxbOptCtx *ctx_ptr = &ctx;
if (si >= 1) {
#define UPDATE_COEFF_PASSES_CASE(tx_class_literal) \
case tx_class_literal: \
update_coeff_eob_facade(&si, tx_size, tx_class_literal, bhl, stride, scan, \
txb_costs, qcoeff, levels, max_nz_num, ctx_ptr); \
if (si >= 1) { \
update_coeff_simple_facade(si, tx_size, tx_class_literal, bhl, stride, \
scan, txb_costs, qcoeff, levels, sharpness, \
ctx_ptr); \
si = 0; \
} \
break
switch (tx_class) {
UPDATE_COEFF_PASSES_CASE(TX_CLASS_2D);
UPDATE_COEFF_PASSES_CASE(TX_CLASS_HORIZ);
UPDATE_COEFF_PASSES_CASE(TX_CLASS_VERT);
#undef UPDATE_COEFF_PASSES_CASE
default: assert(false);
}
}
if (ctx_ptr->nz_num <= max_nz_num) {
const int coeff_ctx =
(tx_class == TX_CLASS_2D)
? 0
: get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
const tran_low_t qc_dc = qcoeff[0];
if (qc_dc == 0) {
ctx_ptr->accu_rate += txb_costs->base_cost[coeff_ctx][0];
} else {
ctx_ptr->accu_rate =
update_coeff_eob_nz(0, 0, qc_dc, coeff_ctx, tx_class, ctx_ptr);
}
if (ctx_ptr->nz_num <= max_nz_num && sharpness == 0) {
const int *const txb_skip_cost =
txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx];
const int non_skip_cost = txb_skip_cost[0];
const int skip_cost = txb_skip_cost[1];
update_skip(&ctx_ptr->accu_rate, ctx_ptr->accu_dist, &ctx_ptr->eob,
ctx_ptr->nz_num, nz_ci, rdmult, skip_cost, non_skip_cost,
qcoeff, dqcoeff);
if (ctx_ptr->eob == 0) {
p->eobs[block] = 0;
p->txb_entropy_ctx[block] = 0;
*rate_cost = ctx_ptr->accu_rate + skip_cost;
return 0;
}
}
} else {
assert(si == 0);
// DC position (si == 0)
// no need to update accu_dist because it's not used after this point
update_coeff_dc(&ctx_ptr->accu_rate, tx_size, tx_class, bhl, stride, rdmult,
shift, txb_ctx->dc_sign_ctx, dequant, txb_costs, tcoeff,
qcoeff, dqcoeff, levels, iqmatrix, qmatrix);
}
accu_rate = ctx_ptr->accu_rate;
eob = ctx_ptr->eob;
p->eobs[block] = eob;
const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
cm->features.reduced_tx_set_used);
accu_rate +=
txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0] + tx_type_cost;
p->txb_entropy_ctx[block] =
av1_get_txb_entropy_context(qcoeff, scan_order, eob);
*rate_cost = accu_rate;
return eob;
}
#if CONFIG_QUANT_MATRIX
// Pack 32-bit rate and COEFF_CONTEXT_MASK-clamped level contribution for
// cul_level accumulation into a single 64-bit return value.
static inline uint64_t pack_rate_cul_qm(int rate, unsigned int abs_val) {
const unsigned int capped = AOMMIN(abs_val, (unsigned int)COEFF_CONTEXT_MASK);
return (uint64_t)(uint32_t)rate | ((uint64_t)capped << 32);
}
static inline int64_t calc_coeff_dist0_qm(intptr_t ci, const tran_low_t *tcoeff,
const tran_low_t *dqcoeff,
const qm_val_t *qmatrix, int shift) {
const tran_low_t tqc = tcoeff[ci];
const tran_low_t dqc = dqcoeff[ci];
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
const int64_t dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
return get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
}
static AOM_FORCE_INLINE void update_coeff_last_qm(
int *accu_rate, int si, int ci, tran_low_t abs_qc, int sign, int bhl,
int width, int64_t rdmult, int shift, const int16_t *dequant,
const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
tran_low_t *qcoeff, tran_low_t *dqcoeff, const qm_val_t *iqmatrix,
const qm_val_t *qmatrix) {
assert(si > 0 && ci > 0 && abs_qc >= 2);
const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
const tran_low_t tqc = tcoeff[ci];
const tran_low_t dqc = dqcoeff[ci];
const int *const base_eob = txb_costs->base_eob_cost[coeff_ctx];
const tran_low_t abs_qc_low = abs_qc - 1;
int rate, rate_low;
if (abs_qc == 2) {
rate = base_eob[1] + av1_cost_literal(1);
rate_low = base_eob[0] + av1_cost_literal(1);
} else {
const int br_ctx = get_br_ctx_eob(ci, bhl, TX_CLASS_2D);
const int *const lps_cost = txb_costs->lps_cost[br_ctx];
const int base_rate = base_eob[2] + av1_cost_literal(1);
if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
rate_low =
(abs_qc == 3)
? (base_eob[1] + av1_cost_literal(1))
: (base_rate + lps_cost[abs_qc_low - (1 + NUM_BASE_LEVELS)]);
} else {
rate = base_rate + get_br_cost(abs_qc, lps_cost);
rate_low = base_rate + get_br_cost(abs_qc_low, lps_cost);
}
}
const int dqv =
((iqmatrix[ci] * dequant[1]) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
tran_low_t qc_low, dqc_low;
get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
const int64_t rd_rate = RDCOST(rdmult, rate, 0);
const int64_t rd_low_rate = RDCOST(rdmult, rate_low, 0);
const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
// dist(tqc, 0) cancels between rd and rd_low, and phase 1 recalculates
// ci_last's dist0 lazily if needed (phase 2/DC never lower EOB).
const int64_t dist_diff_0 = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm);
const int64_t dist_diff_low_0 =
get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm);
const int64_t rd = rd_rate + dist_diff_0 * (1LL << RDDIV_BITS);
const int64_t rd_low = rd_low_rate + dist_diff_low_0 * (1LL << RDDIV_BITS);
if (rd_low < rd) {
qcoeff[ci] = qc_low;
dqcoeff[ci] = dqc_low;
*accu_rate += rate_low;
} else {
*accu_rate += rate;
}
}
// Cold borderline tie-check and |qcoeff| == 2 -> 1 update; kept out of line
// to avoid spilling registers in the fast simple scan loop.
static AOM_NOINLINE uint64_t
finish_try_lower_qm(intptr_t ci, int rate, int rate_low, tran_low_t dqc_low,
int64_t margin, const TxbOptCtx *restrict ctx) {
assert(ci > 0 && ctx->qmatrix != NULL && abs(ctx->qcoeff[ci]) == 2 &&
margin >= 0);
const tran_low_t tqc = ctx->tcoeff[ci];
const tran_low_t dqc = ctx->dqcoeff[ci];
assert(tqc != 0 && (tqc < 0) == (ctx->qcoeff[ci] < 0));
const int sign_mask = AOMSIGN(tqc);
assert((dqc ^ sign_mask) >= (tqc ^ sign_mask));
const int64_t scaled_qm = (int64_t)ctx->qmatrix[ci] << ctx->shift;
// approx_rd and approx_dd are floor shifts that under-count the exact
// rounded terms by at most 1, so the exact margin lies in
// [margin - (1 << RDDIV_BITS), margin + 1]. Only [0, 1 << RDDIV_BITS]
// needs the exact check.
const int better = (margin > (1LL << RDDIV_BITS)) ||
eval_better_qm_exact(tqc, dqc, dqc_low, scaled_qm, rate,
rate_low, ctx->rdmult);
if (better) {
ctx->qcoeff[ci] = ((tran_low_t)1 ^ sign_mask) - sign_mask;
ctx->dqcoeff[ci] = dqc_low;
ctx->levels[get_padded_idx((int)ci, ctx->bhl)] = 1;
return (uint64_t)(uint32_t)rate_low | (1ULL << 32);
}
return (uint64_t)(uint32_t)rate | (2ULL << 32);
}
// Cold handler for |qcoeff| >= 3 in the QM 2D simple pass; kept out of line
// so the |qcoeff| <= 2 loop stays compact without register spills.
// bc4 points to &base_cost[coeff_ctx][4], where bc4[0..3] store level 0..3+
// non-zero base costs including sign bit (rd.c:av1_fill_coeff_costs).
static AOM_NOINLINE uint64_t
update_coeff_simple_gt1_qm(intptr_t ci, unsigned int abs_qc, const int *bc4,
const TxbOptCtx *restrict ctx) {
assert(ci > 0 && abs_qc >= 3 && ctx->iqmatrix != NULL &&
ctx->qmatrix != NULL);
const tran_low_t tqc = ctx->tcoeff[ci];
const tran_low_t dqc = ctx->dqcoeff[ci];
assert(tqc != 0 && (tqc < 0) == (ctx->qcoeff[ci] < 0));
const int sign_mask = AOMSIGN(tqc);
const int br_ctx = get_br_ctx_simple_stride(ctx->levels, (int)ci, ctx->bhl,
ctx->stride, TX_CLASS_2D);
const int *const lps_flat = ctx->txb_costs->lps_cost[br_ctx];
const int base_rate = bc4[3];
int rate, rate_low;
if (LIKELY(abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE)) {
rate = base_rate + lps_flat[abs_qc - (1 + NUM_BASE_LEVELS)];
if ((dqc ^ sign_mask) < (tqc ^ sign_mask)) {
return pack_rate_cul_qm(rate, abs_qc);
}
rate_low = (abs_qc == 3)
? bc4[2]
: (base_rate + lps_flat[abs_qc - (2 + NUM_BASE_LEVELS)]);
} else {
rate = base_rate + get_br_cost((tran_low_t)abs_qc, lps_flat);
if ((dqc ^ sign_mask) < (tqc ^ sign_mask)) {
return pack_rate_cul_qm(rate, abs_qc);
}
rate_low = base_rate + get_br_cost((tran_low_t)(abs_qc - 1), lps_flat);
}
const int dqv =
((ctx->iqmatrix[ci] * ctx->dequant_ac) + (1 << (AOM_QM_BITS - 1))) >>
AOM_QM_BITS;
const int shift = ctx->shift;
const unsigned int abs_qc_low = abs_qc - 1;
const tran_low_t abs_dqc_low =
(tran_low_t)(((tran_low_t)abs_qc_low * dqv) >> shift);
const tran_low_t dqc_low = (abs_dqc_low ^ sign_mask) - sign_mask;
const int64_t sum_term = (int64_t)tqc * 2 - (int64_t)dqc - (int64_t)dqc_low;
const int64_t rdmult = ctx->rdmult;
const int64_t approx_rd =
((int64_t)(rate - rate_low) * rdmult) >> AV1_PROB_COST_SHIFT;
const int64_t scaled_qm = (int64_t)ctx->qmatrix[ci] << shift;
const int64_t sq_qm = scaled_qm * scaled_qm;
const int64_t approx_dd =
((((int64_t)dqc - dqc_low) * sum_term) * sq_qm) >> (2 * AOM_QM_BITS);
const int64_t margin = approx_rd - approx_dd * (1LL << RDDIV_BITS);
const int better =
(margin >= 0) && (margin > (1LL << RDDIV_BITS) ||
eval_better_qm_exact(tqc, dqc, dqc_low, scaled_qm, rate,
rate_low, rdmult));
if (better) {
ctx->qcoeff[ci] = ((tran_low_t)abs_qc_low ^ sign_mask) - sign_mask;
ctx->dqcoeff[ci] = dqc_low;
ctx->levels[get_padded_idx((int)ci, ctx->bhl)] =
(uint8_t)AOMMIN(abs_qc_low, (unsigned int)INT8_MAX);
return pack_rate_cul_qm(rate_low, abs_qc_low);
}
return pack_rate_cul_qm(rate, abs_qc);
}
static inline uint64_t update_coeff_simple_eq2_qm(
intptr_t ci, const int *bc4, const TxbOptCtx *restrict ctx) {
assert(ci > 0 && ctx->iqmatrix != NULL && ctx->qmatrix != NULL);
const tran_low_t tqc = ctx->tcoeff[ci];
const tran_low_t dqc = ctx->dqcoeff[ci];
assert(tqc != 0 && (tqc < 0) == (ctx->qcoeff[ci] < 0));
const int sign_mask = AOMSIGN(tqc);
const int rate = bc4[2];
if ((dqc ^ sign_mask) < (tqc ^ sign_mask)) {
return (uint64_t)(uint32_t)rate | (2ULL << 32);
}
const int raw_dqv =
(ctx->iqmatrix[ci] * ctx->dequant_ac) + (1 << (AOM_QM_BITS - 1));
const int shift = ctx->shift;
const int64_t scaled_qm = (int64_t)ctx->qmatrix[ci] << shift;
const int64_t sq_qm = scaled_qm * scaled_qm;
const tran_low_t dqc_low =
((raw_dqv >> (AOM_QM_BITS + shift)) ^ sign_mask) - sign_mask;
const int64_t sum_term = (int64_t)tqc * 2 - (int64_t)dqc - (int64_t)dqc_low;
const int64_t approx_dd =
((((int64_t)dqc - dqc_low) * sum_term) * sq_qm) >> (2 * AOM_QM_BITS);
const int rate_low = bc4[1];
const int64_t approx_rd =
((int64_t)(rate - rate_low) * ctx->rdmult) >> AV1_PROB_COST_SHIFT;
const int64_t margin = approx_rd - approx_dd * (1LL << RDDIV_BITS);
if (margin >= 0) {
return finish_try_lower_qm(ci, rate, rate_low, dqc_low, margin, ctx);
}
return (uint64_t)(uint32_t)rate | (2ULL << 32);
}
// Keep the specialized QM-PSNR 2D optimizer out of line from av1_optimize_txb
// and av1_optimize_b so non-QM builds retain untouched av1_optimize_txb_multi
// codegen while keeping hot scan-loop scalars in registers.
AOM_NOINLINE
int av1_optimize_txb_multi_qm(const struct AV1_COMP *cpi, MACROBLOCK *x,
int plane, int block, TX_SIZE tx_size,
TX_TYPE tx_type, const TXB_CTX *const txb_ctx,
int *rate_cost, int sharpness) {
const MACROBLOCKD *const xd = &x->e_mbd;
const struct macroblock_plane *p = &x->plane[plane];
int eob = p->eobs[block];
assert(eob > 1 && sharpness != 0 && tx_type < IDTX);
assert(cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR);
const PLANE_TYPE plane_type = get_plane_type(plane);
const int seg_id = xd->mi[0]->segment_id;
const qm_val_t *const iqmatrix =
xd->plane[plane].seg_iqmatrix[seg_id][tx_size];
const qm_val_t *const qmatrix = xd->plane[plane].seg_qmatrix[seg_id][tx_size];
assert(iqmatrix != NULL && qmatrix != NULL);
int is_noise_pattern = 0;
const int64_t rdmult = get_txb_rdmult(cpi, x, xd, plane, plane_type,
sharpness, &is_noise_pattern);
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
const int shift = av1_get_tx_scale(tx_size);
const int16_t *dequant = p->dequant_QTX;
const int block_offset = BLOCK_OFFSET(block);
tran_low_t *qcoeff = p->qcoeff + block_offset;
tran_low_t *dqcoeff = p->dqcoeff + block_offset;
const tran_low_t *tcoeff = p->coeff + block_offset;
const CoeffCosts *coeff_costs = &x->coeff_costs;
const AV1_COMMON *cm = &cpi->common;
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
assert(tx_type_to_class[tx_type] == TX_CLASS_2D);
const int bhl = get_txb_bhl(tx_size);
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
const LV_MAP_COEFF_COST *txb_costs =
&coeff_costs->coeff_costs[txs_ctx][plane_type];
const int eob_multi_size = txsize_log2_minus4[tx_size];
const LV_MAP_EOB_COST *txb_eob_costs =
&coeff_costs->eob_costs[eob_multi_size][plane_type];
const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, TX_CLASS_2D);
int accu_rate = eob_cost;
int si = eob - 1;
const int ci_last = scan[si];
const tran_low_t qc_last = qcoeff[ci_last];
const tran_low_t abs_qc_last = abs(qc_last);
const int sign_last = qc_last < 0;
const int max_nz_num = kMaxNzNum;
int nz_ci[kMaxNzNum + 1] = { ci_last, 0, 0 };
const int min_eob_cutoff = is_noise_pattern ? 8 : 5;
if (abs_qc_last >= 2) {
update_coeff_last_qm(&accu_rate, si, ci_last, abs_qc_last, sign_last, bhl,
width, rdmult, shift, dequant, txb_costs, tcoeff,
qcoeff, dqcoeff, iqmatrix, qmatrix);
--si;
} else {
assert(abs_qc_last == 1);
const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
accu_rate += txb_costs->base_eob_cost[coeff_ctx][0] + av1_cost_literal(1);
--si;
}
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, height);
if (si >= 1 || (unsigned int)(qcoeff[0] + 2) > 4) {
av1_txb_init_levels(qcoeff, width, height, levels);
}
const intptr_t stride = (1 << bhl) + TX_PAD_HOR;
TxbOptCtx ctx = {
.rdmult = rdmult,
.accu_dist = 0,
.txb_eob_costs = txb_eob_costs,
.txb_costs = txb_costs,
.dequant = dequant,
.tcoeff = tcoeff,
.qcoeff = qcoeff,
.dqcoeff = dqcoeff,
.levels = levels,
.iqmatrix = iqmatrix,
.qmatrix = qmatrix,
.nz_ci = nz_ci,
.stride = stride,
.eob = eob,
.nz_num = 1,
.accu_rate = accu_rate,
.bhl = bhl,
.width = width,
.dc_sign_ctx = txb_ctx->dc_sign_ctx,
.shift = shift,
.dequant_ac = dequant[1],
.sharpness = (int16_t)sharpness,
.tx_class = TX_CLASS_2D,
.min_eob_cutoff = min_eob_cutoff,
};
int ac_nz_num = 1;
unsigned int simple_cul = 0;
if (si >= 1) {
int local_rate = ctx.accu_rate;
int nz_num = ctx.nz_num;
const int8_t *const nz_map_ctx = av1_nz_map_ctx_offset[tx_size];
const int (*base_cost)[8] = txb_costs->base_cost;
const int min_s_phase1 = min_eob_cutoff - 1;
const int16_t *scan_p = scan + si;
const int16_t *const p1_end = scan + min_s_phase1;
if (scan_p >= p1_end) {
const int *const eob_cost_tab = txb_eob_costs->eob_cost[0];
int64_t local_dist = 0;
int pending_dist_ci = nz_ci[0];
do {
int ci;
tran_low_t qc;
int coeff_ctx;
for (;;) {
ci = *scan_p;
coeff_ctx = get_lower_levels_ctx_simple_fast(levels, ci, bhl, stride,
nz_map_ctx, TX_CLASS_2D);
qc = qcoeff[ci];
if (qc != 0) break;
local_rate += base_cost[coeff_ctx][0];
if (--scan_p < p1_end) goto phase1_done;
}
if (pending_dist_ci >= 0) {
local_dist += calc_coeff_dist0_qm(pending_dist_ci, tcoeff, dqcoeff,
qmatrix, shift);
pending_dist_ci = -1;
}
const int cur_s = (int)(scan_p - scan);
if ((unsigned int)(qc + 1) <= 2) {
const int coeff_ctx_new_eob =
get_lower_levels_ctx_eob(bhl, width, cur_s);
assert(cur_s >= 4);
const int k = get_msb(cur_s);
const int bit = (cur_s >> (k - 1)) & 1;
const int rate_coeff_eob =
eob_cost_tab[k + 1] + txb_costs->eob_extra_cost[k - 1][bit] +
txb_costs->base_eob_cost[coeff_ctx_new_eob][0] +
av1_cost_literal(k);
const int rate_keep = local_rate + base_cost[coeff_ctx][5];
// Check exact RDCOST when rounding error around d_eob == 0 (or -1
// under arithmetic/truncating right shift) could flip the choice.
const int64_t approx_dr =
((int64_t)(rate_coeff_eob - rate_keep) * rdmult) >>
AV1_PROB_COST_SHIFT;
const int64_t d_eob = approx_dr - local_dist * (1LL << RDDIV_BITS);
if ((d_eob > 0) ||
(d_eob >= -1 && RDCOST(rdmult, rate_coeff_eob, 0) >=
RDCOST(rdmult, rate_keep, local_dist))) {
if (nz_num < max_nz_num) {
pending_dist_ci = ci;
}
nz_ci[nz_num] = ci;
++nz_num;
local_rate = rate_keep;
--scan_p;
continue;
}
}
ctx.accu_rate = local_rate;
ctx.accu_dist = local_dist;
ctx.nz_num = nz_num;
local_rate =
update_coeff_eob_nz(cur_s, ci, qc, coeff_ctx, TX_CLASS_2D, &ctx);
local_dist = ctx.accu_dist;
nz_num = ctx.nz_num;
--scan_p;
} while (scan_p >= p1_end && nz_num <= max_nz_num);
}
phase1_done:;
const int16_t *const p2_end = scan + 1;
while (scan_p >= p2_end && nz_num <= max_nz_num) {
const int ci = *scan_p;
const tran_low_t qc = qcoeff[ci];
const unsigned int abs_qc = (unsigned int)abs(qc);
const int coeff_ctx = get_lower_levels_ctx_simple_fast(
levels, ci, bhl, stride, nz_map_ctx, TX_CLASS_2D);
if (abs_qc > 2) {
ctx.accu_rate = local_rate;
ctx.nz_num = nz_num;
local_rate = update_coeff_eob_nz((int)(scan_p - scan), ci, qc,
coeff_ctx, TX_CLASS_2D, &ctx);
nz_num = ctx.nz_num;
} else {
local_rate += base_cost[coeff_ctx][4 + abs_qc];
assert(nz_num <= max_nz_num);
nz_ci[nz_num] = ci;
nz_num += (abs_qc != 0);
}
--scan_p;
}
si = (int)(scan_p - scan);
ctx.nz_num = nz_num;
ac_nz_num = nz_num;
if (si >= 1) {
const int16_t *const scan_end = scan + 1;
for (;;) {
intptr_t ci;
tran_low_t qc;
int coeff_ctx;
for (;;) {
ci = *scan_p;
assert(ci > 0);
coeff_ctx = get_lower_levels_ctx_simple_fast(
levels, (int)ci, bhl, stride, nz_map_ctx, TX_CLASS_2D);
qc = qcoeff[ci];
if (UNLIKELY((unsigned int)(qc + 1) > 2)) break;
const unsigned int is_nz = (unsigned int)qc & 1u;
simple_cul += is_nz;
local_rate += base_cost[coeff_ctx][4 + is_nz];
if (--scan_p < scan_end) goto simple_done;
}
const int *const bc4 = &base_cost[coeff_ctx][4];
const unsigned int abs_qc = (unsigned int)abs(qc);
const uint64_t packed =
(abs_qc == 2) ? update_coeff_simple_eq2_qm(ci, bc4, &ctx)
: update_coeff_simple_gt1_qm(ci, abs_qc, bc4, &ctx);
local_rate += (int)(uint32_t)packed;
simple_cul += (unsigned int)(packed >> 32);
if (--scan_p < scan_end) break;
}
simple_done:
si = 0;
}
ctx.accu_rate = local_rate;
}
if (ac_nz_num <= max_nz_num) {
const tran_low_t qc_dc = qcoeff[0];
if (qc_dc == 0) {
ctx.accu_rate += txb_costs->base_cost[0][0];
} else {
ctx.accu_rate = update_coeff_eob_nz(0, 0, qc_dc, 0, TX_CLASS_2D, &ctx);
}
} else {
assert(si == 0);
update_coeff_dc(&ctx.accu_rate, tx_size, TX_CLASS_2D, bhl, stride, rdmult,
shift, ctx.dc_sign_ctx, dequant, txb_costs, tcoeff, qcoeff,
dqcoeff, levels, iqmatrix, qmatrix);
}
accu_rate = ctx.accu_rate;
eob = ctx.eob;
p->eobs[block] = eob;
assert(eob > 1);
assert(ac_nz_num >= 1 && ac_nz_num <= kMaxNzNum + 1);
const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
cm->features.reduced_tx_set_used);
accu_rate += non_skip_cost + tx_type_cost;
const tran_low_t dc_val = qcoeff[0];
const int dc_sign = ((dc_val < 0) + ((dc_val > 0) << 1))
<< COEFF_CONTEXT_BITS;
// Every non-zero AC coeff is counted once either in simple_cul (Phase 3)
// or in nz_ci[0..ac_nz_num) (initial EOB / Phase 1 / Phase 2).
unsigned int cul_level = (unsigned int)abs(dc_val) + simple_cul;
if (cul_level < (unsigned int)COEFF_CONTEXT_MASK) {
for (int i = 0; i < ac_nz_num; ++i) {
cul_level += (unsigned int)abs(qcoeff[nz_ci[i]]);
}
}
p->txb_entropy_ctx[block] =
(uint8_t)(AOMMIN(cul_level, (unsigned int)COEFF_CONTEXT_MASK) | dc_sign);
assert(p->txb_entropy_ctx[block] ==
av1_get_txb_entropy_context(qcoeff, scan_order, eob));
*rate_cost = accu_rate;
return eob;
}
#endif // CONFIG_QUANT_MATRIX
int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
int block, TX_SIZE tx_size, TX_TYPE tx_type,
const TXB_CTX *const txb_ctx, int *rate_cost,
int sharpness) {
const struct macroblock_plane *p = &x->plane[plane];
const int eob = p->eobs[block];
assert(eob > 0);
if (eob > 1) {
#if CONFIG_QUANT_MATRIX
if (av1_use_qm_txb_opt_2d(cpi, &x->e_mbd, plane, tx_size, tx_type,
sharpness)) {
return av1_optimize_txb_multi_qm(cpi, x, plane, block, tx_size, tx_type,
txb_ctx, rate_cost, sharpness);
}
#endif // CONFIG_QUANT_MATRIX
return av1_optimize_txb_multi(cpi, x, plane, block, tx_size, tx_type,
txb_ctx, rate_cost, sharpness);
}
// Fast path for DC-only blocks (eob == 1): evaluate DC level reduction
// without initializing the 2D padded levels buffer.
MACROBLOCKD *xd = &x->e_mbd;
const PLANE_TYPE plane_type = get_plane_type(plane);
const MB_MODE_INFO *mbmi = xd->mi[0];
const int block_offset = BLOCK_OFFSET(block);
tran_low_t *qcoeff = p->qcoeff + block_offset;
tran_low_t *dqcoeff = p->dqcoeff + block_offset;
const CoeffCosts *coeff_costs = &x->coeff_costs;
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
const TX_CLASS tx_class = tx_type_to_class[tx_type];
const LV_MAP_COEFF_COST *txb_costs =
&coeff_costs->coeff_costs[txs_ctx][plane_type];
const int eob_multi_size = txsize_log2_minus4[tx_size];
const LV_MAP_EOB_COST *txb_eob_costs =
&coeff_costs->eob_costs[eob_multi_size][plane_type];
const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
const int eob_cost = txb_eob_costs->eob_cost[tx_class != TX_CLASS_2D][0];
const tran_low_t qc = qcoeff[0];
const tran_low_t abs_qc = abs(qc);
const int sign = qc < 0;
int accu_rate = eob_cost;
if (abs_qc == 1) {
accu_rate += txb_costs->base_eob_cost[0][0] +
txb_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign];
if (sharpness == 0) {
const int64_t rdmult =
get_txb_rdmult(cpi, x, xd, plane, plane_type, sharpness, NULL);
const tran_low_t tqc = p->coeff[block_offset];
const tran_low_t dqc = dqcoeff[0];
const int shift = av1_get_tx_scale(tx_size);
const qm_val_t *qmatrix =
(cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR &&
tx_type < IDTX)
? xd->plane[plane].seg_qmatrix[mbmi->segment_id][tx_size]
: NULL;
const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, 0);
const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, 0);
const int64_t accu_dist = dist - dist0;
if (RDCOST(rdmult, skip_cost, 0) <
RDCOST(rdmult, accu_rate + non_skip_cost, accu_dist)) {
qcoeff[0] = 0;
dqcoeff[0] = 0;
p->eobs[block] = 0;
p->txb_entropy_ctx[block] = 0;
*rate_cost = skip_cost;
return 0;
}
}
} else {
const int64_t rdmult =
get_txb_rdmult(cpi, x, xd, plane, plane_type, sharpness, NULL);
const tran_low_t tqc = p->coeff[block_offset];
const tran_low_t dqc = dqcoeff[0];
const int shift = av1_get_tx_scale(tx_size);
const qm_val_t *iqmatrix =
tx_type < IDTX
? xd->plane[plane].seg_iqmatrix[mbmi->segment_id][tx_size]
: NULL;
const qm_val_t *qmatrix =
(cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR &&
tx_type < IDTX)
? xd->plane[plane].seg_qmatrix[mbmi->segment_id][tx_size]
: NULL;
const int dc_sign_cost =
txb_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign];
int rate =
txb_costs->base_eob_cost[0][AOMMIN(abs_qc, 3) - 1] + dc_sign_cost;
if (abs_qc > NUM_BASE_LEVELS) {
rate += get_br_cost(abs_qc, txb_costs->lps_cost[0]);
}
const int dqv = get_dqv(p->dequant_QTX, 0, iqmatrix);
tran_low_t qc_low, dqc_low;
get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
const tran_low_t abs_qc_low = abs_qc - 1;
int rate_low =
txb_costs->base_eob_cost[0][AOMMIN(abs_qc_low, 3) - 1] + dc_sign_cost;
if (abs_qc_low > NUM_BASE_LEVELS) {
rate_low += get_br_cost(abs_qc_low, txb_costs->lps_cost[0]);
}
int64_t dist_diff_0, dist_diff_low_0;
if (qmatrix == NULL) {
const int64_t tqc2 = (int64_t)tqc * 2;
dist_diff_0 = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
dist_diff_low_0 =
((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
} else {
const int64_t scaled_qm = (int64_t)qmatrix[0] << shift;
const int64_t dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
dist_diff_0 = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
dist_diff_low_0 =
get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm) - dist0;
}
const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
int64_t accu_dist;
if (rd_low < rd) {
qcoeff[0] = qc_low;
dqcoeff[0] = dqc_low;
accu_rate += rate_low;
accu_dist = dist_diff_low_0;
} else {
accu_rate += rate;
accu_dist = dist_diff_0;
}
if (sharpness == 0) {
if (RDCOST(rdmult, skip_cost, 0) <
RDCOST(rdmult, accu_rate + non_skip_cost, accu_dist)) {
qcoeff[0] = 0;
dqcoeff[0] = 0;
p->eobs[block] = 0;
p->txb_entropy_ctx[block] = 0;
*rate_cost = skip_cost;
return 0;
}
}
}
const int tx_type_cost = get_tx_type_cost(
x, xd, plane, tx_size, tx_type, cpi->common.features.reduced_tx_set_used);
*rate_cost = accu_rate + non_skip_cost + tx_type_cost;
p->eobs[block] = 1;
p->txb_entropy_ctx[block] = av1_get_txb_entropy_context(qcoeff, NULL, 1);
return 1;
}
static AOM_FORCE_INLINE int warehouse_efficients_txb_loop(
const tran_low_t *const __restrict qcoeff,
const int16_t *const __restrict scan, const int eob,
const uint8_t *const __restrict levels, const TX_SIZE tx_size,
const int bhl, const int width, const intptr_t stride,
const TXB_CTX *const txb_ctx, const LV_MAP_COEFF_COST *const coeff_costs,
const TX_CLASS tx_class, int cost) {
const int (*lps_cost)[16] = coeff_costs->lps_cost;
const int (*base_cost)[8] = coeff_costs->base_cost;
int c = eob - 1;
{
const int pos = scan[c];
const tran_low_t v = qcoeff[pos];
if (v) {
const unsigned int level = (unsigned int)abs(v);
const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, c);
const uintptr_t min_lvl = level < 3 ? level : 3;
cost += coeff_costs->base_eob_cost[coeff_ctx][min_lvl - 1] +
av1_cost_literal(1);
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx = get_br_ctx_eob(pos, bhl, tx_class);
cost += get_br_cost(level, lps_cost[ctx]);
}
}
}
if (eob <= ((width << bhl) >> 4) + 3) {
const int8_t *const nz_map_ctx =
(tx_class == TX_CLASS_2D) ? av1_nz_map_ctx_offset[tx_size] : NULL;
for (c = eob - 2; c >= 1; --c) {
const int pos = scan[c];
const int coeff_ctx = get_lower_levels_ctx_simple_fast(
levels, pos, bhl, stride, nz_map_ctx, tx_class);
const int level = abs(qcoeff[pos]);
cost += base_cost[coeff_ctx][4 + AOMMIN(level, 3)];
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx =
get_br_ctx_simple_stride(levels, pos, bhl, stride, tx_class);
cost += get_br_cost(level, lps_cost[ctx]);
}
}
const tran_low_t v = qcoeff[0];
const int coeff_ctx =
(tx_class == TX_CLASS_2D)
? 0
: get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
if (!v) {
cost += base_cost[coeff_ctx][0];
} else {
const int level = abs(v);
const int sign01 = (v < 0) ? 1 : 0;
const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
cost += base_cost[coeff_ctx][AOMMIN(level, 3)] +
coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx = get_br_ctx_stride(levels, 0, bhl, stride, tx_class);
cost += get_br_cost(level, lps_cost[ctx]);
}
}
return cost;
}
DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
for (c = eob - 2; c >= 1; --c) {
const int pos = scan[c];
const int coeff_ctx = coeff_contexts[pos];
const unsigned int level = (unsigned int)abs(qcoeff[pos]);
const uintptr_t min_lvl = level < 3 ? level : 3;
cost += base_cost[coeff_ctx][4 + min_lvl];
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx =
get_br_ctx_simple_stride(levels, pos, bhl, stride, tx_class);
cost += get_br_cost(level, lps_cost[ctx]);
}
}
// c == 0 after previous loop
{
const tran_low_t v = qcoeff[0];
const int coeff_ctx = coeff_contexts[0];
if (!v) {
cost += base_cost[coeff_ctx][0];
} else {
const unsigned int level = (unsigned int)abs(v);
const int sign01 = (v < 0) ? 1 : 0;
const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
const uintptr_t min_lvl = level < 3 ? level : 3;
cost += base_cost[coeff_ctx][min_lvl] +
coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx = get_br_ctx_stride(levels, 0, bhl, stride, tx_class);
cost += get_br_cost(level, lps_cost[ctx]);
}
}
}
return cost;
}
static AOM_FORCE_INLINE int warehouse_efficients_2d_fast(
const tran_low_t *const qcoeff, const int16_t *const scan,
const TX_SIZE tx_size, const int bhl, const int eob,
const TXB_CTX *const txb_ctx, const LV_MAP_COEFF_COST *const coeff_costs,
int cost, int *out_cost) {
assert(eob == 3 || eob == 4);
assert(scan[0] == 0);
const int pos1 = scan[1];
const tran_low_t v1 = qcoeff[pos1];
const tran_low_t v0 = qcoeff[0];
if ((unsigned int)(v1 + 2) > 4 || (unsigned int)(v0 + 2) > 4) {
return 0;
}
const int off1 = (uint8_t)av1_nz_map_ctx_offset[tx_size][pos1];
const unsigned int abs_v1 = (unsigned int)abs(v1);
if (eob == 3) {
const int pos2 = scan[2];
const tran_low_t v2 = qcoeff[pos2];
assert(get_lower_levels_ctx_eob(bhl, get_txb_wide(tx_size), 2) == 1);
if (v2 != 0) {
const unsigned int level2 = (unsigned int)abs(v2);
if (level2 <= NUM_BASE_LEVELS) {
cost += coeff_costs->base_eob_cost[1][level2 - 1] + av1_cost_literal(1);
} else {
const int br2 = get_br_ctx_eob(pos2, bhl, TX_CLASS_2D);
cost += coeff_costs->base_eob_cost[1][2] + av1_cost_literal(1) +
get_br_cost((tran_low_t)level2, coeff_costs->lps_cost[br2]);
}
}
cost += coeff_costs->base_cost[off1][4 + abs_v1];
} else {
const int pos2 = scan[2];
const tran_low_t v2 = qcoeff[pos2];
if ((unsigned int)(v2 + 2) > 4) return 0;
assert(off1 == (uint8_t)av1_nz_map_ctx_offset[tx_size][pos2]);
const int pos3 = scan[3];
const tran_low_t v3 = qcoeff[pos3];
int ctx_val2 = 0;
int ctx_val1 = 0;
const int eob_ctx3 = (tx_size == TX_4X4) ? 2 : 1;
assert(pos3 == 2 * pos2 || pos3 == 2 * pos1);
assert(get_lower_levels_ctx_eob(bhl, get_txb_wide(tx_size), 3) == eob_ctx3);
if (v3 != 0) {
const unsigned int level3 = (unsigned int)abs(v3);
if (level3 <= NUM_BASE_LEVELS) {
cost += coeff_costs->base_eob_cost[eob_ctx3][level3 - 1] +
av1_cost_literal(1);
} else {
const int br3 = get_br_ctx_eob(pos3, bhl, TX_CLASS_2D);
cost += coeff_costs->base_eob_cost[eob_ctx3][2] + av1_cost_literal(1) +
get_br_cost((tran_low_t)level3, coeff_costs->lps_cost[br3]);
}
const int c3_val = 1 + (level3 >= 3);
if (pos3 == 2 * pos2) {
ctx_val2 = c3_val;
} else {
ctx_val1 = c3_val;
}
}
cost += coeff_costs->base_cost[off1 + ctx_val2][4 + (unsigned int)abs(v2)] +
coeff_costs->base_cost[off1 + ctx_val1][4 + abs_v1];
}
if (!v0) {
cost += coeff_costs->base_cost[0][0];
} else {
cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][v0 < 0] +
coeff_costs->base_cost[0][(unsigned int)abs(v0)];
}
*out_cost = cost;
return 1;
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((noinline))
#endif
static int warehouse_efficients_txb_multi(
const tran_low_t *const qcoeff, const TX_SIZE tx_size,
const TXB_CTX *const txb_ctx, const int eob,
const LV_MAP_COEFF_COST *const coeff_costs, const TX_TYPE tx_type,
const TX_CLASS tx_class, int cost) {
const int bhl = get_txb_bhl(tx_size);
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
if (eob <= 4 && tx_class == TX_CLASS_2D) {
int fast_cost;
if (warehouse_efficients_2d_fast(qcoeff, scan, tx_size, bhl, eob, txb_ctx,
coeff_costs, cost, &fast_cost)) {
return fast_cost;
}
}
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, height);
// Only narrow init_w when staying on the scalar loop; av1_get_nz_map_contexts
// scans all width columns up to width + 1.
const int init_w =
(tx_class == TX_CLASS_2D && eob <= ((width << bhl) >> 4) + 3)
? get_init_levels_width_2d(width, bhl, eob)
: width;
av1_txb_init_levels(qcoeff, init_w, height, levels);
const intptr_t stride = height + TX_PAD_HOR;
switch (tx_class) {
case TX_CLASS_2D:
return warehouse_efficients_txb_loop(qcoeff, scan, eob, levels, tx_size,
bhl, width, stride, txb_ctx,
coeff_costs, TX_CLASS_2D, cost);
case TX_CLASS_VERT:
return warehouse_efficients_txb_loop(qcoeff, scan, eob, levels, tx_size,
bhl, width, stride, txb_ctx,
coeff_costs, TX_CLASS_VERT, cost);
case TX_CLASS_HORIZ:
default:
return warehouse_efficients_txb_loop(qcoeff, scan, eob, levels, tx_size,
bhl, width, stride, txb_ctx,
coeff_costs, TX_CLASS_HORIZ, cost);
}
}
/*!\brief Estimate the entropy cost of transform coefficients using Laplacian
* distribution.
*
* \ingroup coefficient_coding
*
* This function assumes each transform coefficient is of its own Laplacian
* distribution and the coefficient is the only observation of the Laplacian
* distribution.
*
* Based on that, each coefficient's coding cost can be estimated by computing
* the entropy of the corresponding Laplacian distribution.
*
* This function then return the sum of the estimated entropy cost for all
* coefficients in the transform block.
*
* Note that the entropy cost of end of block (eob) and transform type (tx_type)
* are not included.
*
* \param[in] x Pointer to structure holding the data for the
current encoding macroblock
* \param[in] plane The index of the current plane
* \param[in] block The index of the current transform block in the
* macroblock. It's defined by number of 4x4 units that have been coded before
* the currernt transform block
* \param[in] tx_size The transform size
* \param[in] tx_type The transform type
* \return int Estimated entropy cost of coefficients in the
* transform block.
*/
static 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;
}
static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian(
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, const TX_CLASS tx_class,
int reduced_tx_set_used) {
const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
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];
int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
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;
}
int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
const TX_SIZE tx_size, const TX_TYPE tx_type,
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];
if (eob == 0) {
return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
}
const MACROBLOCKD *const xd = &x->e_mbd;
const TX_CLASS tx_class = tx_type_to_class[tx_type];
const tran_low_t *const __restrict qcoeff = p->qcoeff + BLOCK_OFFSET(block);
const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
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];
int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
if (eob == 1) {
const tran_low_t v = qcoeff[0];
if (v) {
const int sign = AOMSIGN(v);
const int level = (v ^ sign) - sign;
cost += coeff_costs->base_eob_cost[0][AOMMIN(level, 3) - 1];
if (level > NUM_BASE_LEVELS) {
cost += get_br_cost(level, coeff_costs->lps_cost[0]);
}
const int sign01 = -sign;
cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
}
return cost;
}
if (eob == 2 && tx_class == TX_CLASS_2D) {
const int16_t *const scan = get_scan(tx_size, tx_type)->scan;
const int pos1 = scan[1];
assert(pos1 == 1 || pos1 == (1 << get_txb_bhl(tx_size)));
assert(get_lower_levels_ctx_eob(get_txb_bhl(tx_size), get_txb_wide(tx_size),
1) == 1);
const tran_low_t v1 = qcoeff[pos1];
unsigned int level1 = 0;
if (v1) {
level1 = (unsigned int)abs(v1);
const uintptr_t min_lvl1 = level1 < 3 ? level1 : 3;
cost += coeff_costs->base_eob_cost[1][min_lvl1 - 1] + av1_cost_literal(1);
if (UNLIKELY(level1 > NUM_BASE_LEVELS)) {
cost += get_br_cost(level1, coeff_costs->lps_cost[7]);
}
}
const tran_low_t v0 = qcoeff[0];
if (!v0) {
cost += coeff_costs->base_cost[0][0];
} else {
const unsigned int level0 = (unsigned int)abs(v0);
const int sign01 = (v0 < 0) ? 1 : 0;
const uintptr_t min_lvl0 = level0 < 3 ? level0 : 3;
cost += coeff_costs->base_cost[0][min_lvl0] +
coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
if (UNLIKELY(level0 > NUM_BASE_LEVELS)) {
const unsigned int ctx0 = AOMMIN((level1 + 1) >> 1, 6);
cost += get_br_cost(level0, coeff_costs->lps_cost[ctx0]);
}
}
return cost;
}
return warehouse_efficients_txb_multi(qcoeff, tx_size, txb_ctx, eob,
coeff_costs, tx_type, tx_class, cost);
}
int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane,
const int block, const TX_SIZE tx_size,
const TX_TYPE tx_type,
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];
if (eob == 0) {
return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
}
const MACROBLOCKD *const xd = &x->e_mbd;
const TX_CLASS tx_class = tx_type_to_class[tx_type];
return warehouse_efficients_txb_laplacian(
x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd,
tx_type, tx_class, reduced_tx_set_used);
}