[dep_quant.c] Small refactor

This commit is contained in:
Joose Sainio 2023-04-07 08:14:06 +03:00
parent 64d34f8559
commit 2912db5fca

View file

@ -547,7 +547,11 @@ static void depquant_state_init(depquant_state* state, uint32_t sig_frac_bits[2]
state->m_sbbFracBits[1] = 0;
}
static INLINE void checkRdCostSkipSbbZeroOut(Decision * decision, const all_depquant_states * const state, int decision_id, int skip_offset) {
static INLINE void checkRdCostSkipSbbZeroOut(
Decision* decision,
const all_depquant_states* const state,
int decision_id,
int skip_offset) {
int64_t rdCost = state->m_rdCost[decision_id + skip_offset] + state->m_sbbFracBits[decision_id + skip_offset][0];
decision->rdCost[decision_id] = rdCost;
decision->absLevel[decision_id] = 0;
@ -569,77 +573,64 @@ static void checkRdCosts(
int64_t rdCostA = state->m_rdCost[state_offset] + pqDataA->deltaDist[pqA];
int64_t rdCostB = state->m_rdCost[state_offset] + pqDataA->deltaDist[pqB];
int64_t rdCostZ = state->m_rdCost[state_offset];
if (state->m_remRegBins[state_offset] >= 4)
{
if (pqDataA->absLevel[pqA] < 4)
{
if (state->m_remRegBins[state_offset] >= 4) {
if (pqDataA->absLevel[pqA] < 4) {
rdCostA += state->m_coeffFracBits[state_offset][pqDataA->absLevel[pqA]];
}
else
{
else {
const coeff_t value = (pqDataA->absLevel[pqA] - 4) >> 1;
rdCostA +=
state->m_coeffFracBits[state_offset][pqDataA->absLevel[pqA] - (value << 1)] + goRiceTab[value < RICEMAX ? value : RICEMAX - 1];
state->m_coeffFracBits[state_offset][pqDataA->absLevel[pqA] - (value << 1)] + goRiceTab[
value < RICEMAX ? value : RICEMAX - 1];
}
if (pqDataA->absLevel[pqB] < 4)
{
if (pqDataA->absLevel[pqB] < 4) {
rdCostB += state->m_coeffFracBits[state_offset][pqDataA->absLevel[pqB]];
}
else
{
else {
const coeff_t value = (pqDataA->absLevel[pqB] - 4) >> 1;
rdCostB +=
state->m_coeffFracBits[state_offset][pqDataA->absLevel[pqB] - (value << 1)] + goRiceTab[value < RICEMAX ? value : RICEMAX - 1];
state->m_coeffFracBits[state_offset][pqDataA->absLevel[pqB] - (value << 1)] + goRiceTab[
value < RICEMAX ? value : RICEMAX - 1];
}
if (spt == SCAN_ISCSBB)
{
if (spt == SCAN_ISCSBB) {
rdCostA += state->m_sigFracBits[state_offset][1];
rdCostB += state->m_sigFracBits[state_offset][1];
rdCostZ += state->m_sigFracBits[state_offset][0];
}
else if (spt == SCAN_SOCSBB)
{
else if (spt == SCAN_SOCSBB) {
rdCostA += state->m_sbbFracBits[state_offset][1] + state->m_sigFracBits[state_offset][1];
rdCostB += state->m_sbbFracBits[state_offset][1] + state->m_sigFracBits[state_offset][1];
rdCostZ += state->m_sbbFracBits[state_offset][1] + state->m_sigFracBits[state_offset][0];
}
else if (state->m_numSigSbb[state_offset])
{
else if (state->m_numSigSbb[state_offset]) {
rdCostA += state->m_sigFracBits[state_offset][1];
rdCostB += state->m_sigFracBits[state_offset][1];
rdCostZ += state->m_sigFracBits[state_offset][0];
}
else
{
else {
rdCostZ = decisions->rdCost[decisionA];
}
}
else
{
rdCostA +=
(1 << SCALE_BITS) + goRiceTab[pqDataA->absLevel[pqA] <= state->m_goRiceZero[state_offset]
else {
rdCostA += (1 << SCALE_BITS) + goRiceTab[pqDataA->absLevel[pqA] <= state->m_goRiceZero[state_offset]
? pqDataA->absLevel[pqA] - 1
: (pqDataA->absLevel[pqA] < RICEMAX ? pqDataA->absLevel[pqA] : RICEMAX - 1)];
rdCostB +=
(1 << SCALE_BITS) + goRiceTab[pqDataA->absLevel[pqB] <= state->m_goRiceZero[state_offset]
rdCostB += (1 << SCALE_BITS) + goRiceTab[pqDataA->absLevel[pqB] <= state->m_goRiceZero[state_offset]
? pqDataA->absLevel[pqB] - 1
: (pqDataA->absLevel[pqB] < RICEMAX ? pqDataA->absLevel[pqB] : RICEMAX - 1)];
rdCostZ += goRiceTab[state->m_goRiceZero[state_offset]];
}
if (rdCostA < decisions->rdCost[decisionA])
{
if (rdCostA < decisions->rdCost[decisionA]) {
decisions->rdCost[decisionA] = rdCostA;
decisions->absLevel[decisionA] = pqDataA->absLevel[pqA];
decisions->prevId[decisionA] = state->m_stateId[state_offset];
}
if (rdCostZ < decisions->rdCost[decisionA])
{
if (rdCostZ < decisions->rdCost[decisionA]) {
decisions->rdCost[decisionA] = rdCostZ;
decisions->absLevel[decisionA] = 0;
decisions->prevId[decisionA] = state->m_stateId[state_offset];
}
if (rdCostB < decisions->rdCost[decisionB])
{
if (rdCostB < decisions->rdCost[decisionB]) {
decisions->rdCost[decisionB] = rdCostB;
decisions->absLevel[decisionB] = pqDataA->absLevel[pqB];
decisions->prevId[decisionB] = state->m_stateId[state_offset];
@ -661,17 +652,15 @@ static INLINE void checkRdCostStart(const depquant_state* const state, int32_t l
decision_id)
{
int64_t rdCost = pqData->deltaDist[decision_id] + lastOffset;
if (pqData->absLevel[decision_id] < 4)
{
if (pqData->absLevel[decision_id] < 4) {
rdCost += state->m_coeffFracBits[pqData->absLevel[decision_id]];
}
else
{
else {
const coeff_t value = (pqData->absLevel[decision_id] - 4) >> 1;
rdCost += state->m_coeffFracBits[pqData->absLevel[decision_id] - (value << 1)] + g_goRiceBits[state->m_goRicePar][value < RICEMAX ? value : RICEMAX - 1];
rdCost += state->m_coeffFracBits[pqData->absLevel[decision_id] - (value << 1)]
+ g_goRiceBits[state->m_goRicePar][value < RICEMAX ? value : RICEMAX - 1];
}
if (rdCost < decisions->rdCost[decision_id])
{
if (rdCost < decisions->rdCost[decision_id]) {
decisions->rdCost[decision_id] = rdCost;
decisions->absLevel[decision_id] = pqData->absLevel[decision_id];
decisions->prevId[decision_id] = -1;
@ -721,10 +710,8 @@ static void xDecide(
{
memcpy(decisions, &startDec, sizeof(Decision));
if (zeroOut)
{
if (spt == SCAN_EOCSBB)
{
if (zeroOut) {
if (spt == SCAN_EOCSBB) {
checkRdCostSkipSbbZeroOut(decisions, all_states, 0, skip_offset);
checkRdCostSkipSbbZeroOut(decisions, all_states, 1, skip_offset);
checkRdCostSkipSbbZeroOut(decisions, all_states, 2, skip_offset);
@ -739,8 +726,7 @@ static void xDecide(
checkRdCosts(all_states, spt, &pqData, decisions, 2, 0, prev_offset + 1);
checkRdCosts(all_states, spt, &pqData, decisions, 1, 3, prev_offset + 2);
checkRdCosts(all_states, spt, &pqData, decisions, 3, 1, prev_offset + 3);
if (spt == SCAN_EOCSBB)
{
if (spt == SCAN_EOCSBB) {
checkRdCostSkipSbb(all_states, decisions, 0, skip_offset);
checkRdCostSkipSbb(all_states, decisions, 1, skip_offset);
checkRdCostSkipSbb(all_states, decisions, 2, skip_offset);
@ -755,24 +741,19 @@ static void xDecide(
static INLINE unsigned templateAbsCompare(coeff_t sum)
{
int rangeIdx = 0;
if (sum < g_riceT[0])
{
if (sum < g_riceT[0]) {
rangeIdx = 0;
}
else if (sum < g_riceT[1])
{
else if (sum < g_riceT[1]) {
rangeIdx = 1;
}
else if (sum < g_riceT[2])
{
else if (sum < g_riceT[2]) {
rangeIdx = 2;
}
else if (sum < g_riceT[3])
{
else if (sum < g_riceT[3]) {
rangeIdx = 3;
}
else
{
else {
rangeIdx = 4;
}
return g_riceShift[rangeIdx];
@ -869,30 +850,27 @@ static INLINE void updateStateEOS(
all_depquant_states* state = &ctxs->m_allStates;
int curr_state_offset = ctxs->m_curr_state_offset + decision_id;
state->m_rdCost[curr_state_offset] = decisions->rdCost[decision_id];
if (decisions->prevId[decision_id] > -2)
{
if (decisions->prevId[decision_id] > -2) {
int prvState = -1;
if (decisions->prevId[decision_id] >= 4)
{
if (decisions->prevId[decision_id] >= 4) {
prvState = ctxs->m_skip_state_offset + (decisions->prevId[decision_id] - 4);
state->m_numSigSbb[curr_state_offset] = 0;
memset(state->m_absLevelsAndCtxInit[curr_state_offset], 0, 16 * sizeof(uint8_t));
}
else if (decisions->prevId[decision_id] >= 0)
{
else if (decisions->prevId[decision_id] >= 0) {
prvState = ctxs->m_prev_state_offset + decisions->prevId[decision_id];
state->m_numSigSbb[curr_state_offset] = state->m_numSigSbb[prvState] + !!decisions->absLevel[decision_id];
memcpy(state->m_absLevelsAndCtxInit[curr_state_offset], state->m_absLevelsAndCtxInit[prvState], 16 * sizeof(uint8_t));
}
else
{
else {
state->m_numSigSbb[curr_state_offset] = 1;
memset(state->m_absLevelsAndCtxInit[curr_state_offset], 0, 16 * sizeof(uint8_t));
}
uint8_t* temp = (uint8_t*)(&state->m_absLevelsAndCtxInit[curr_state_offset][scan_pos & 15]);
*temp = (uint8_t)MIN(255, decisions->absLevel[decision_id]);
update_common_context(ctxs, state->m_commonCtx, scan_pos, cg_pos, width_in_sbb, height_in_sbb, next_sbb_right,next_sbb_below, prvState, ctxs->m_curr_state_offset + decision_id);
update_common_context(ctxs, state->m_commonCtx, scan_pos, cg_pos, width_in_sbb, height_in_sbb, next_sbb_right,
next_sbb_below, prvState, ctxs->m_curr_state_offset + decision_id);
coeff_t tinit = state->m_absLevelsAndCtxInit[curr_state_offset][8 + ((scan_pos - 1) & 15)];
coeff_t sumNum = tinit & 7;
@ -901,7 +879,8 @@ static INLINE void updateStateEOS(
state->m_sigFracBits[curr_state_offset][0] = state->m_sigFracBitsArray[curr_state_offset][sigCtxOffsetNext + MIN((sumAbs1 + 1) >> 1, 3)][0];
state->m_sigFracBits[curr_state_offset][1] = state->m_sigFracBitsArray[curr_state_offset][sigCtxOffsetNext + MIN((sumAbs1 + 1) >> 1, 3)][1];
memcpy(state->m_coeffFracBits[curr_state_offset], state->m_gtxFracBitsArray[gtxCtxOffsetNext + (sumGt1 < 4 ? sumGt1 : 4)], sizeof(state->m_coeffFracBits[0]));
memcpy(state->m_coeffFracBits[curr_state_offset],
state->m_gtxFracBitsArray[gtxCtxOffsetNext + (sumGt1 < 4 ? sumGt1 : 4)], sizeof(state->m_coeffFracBits[0]));
}
}
@ -919,10 +898,8 @@ static INLINE void updateState(
all_depquant_states* state = &ctxs->m_allStates;
int state_id = ctxs->m_curr_state_offset + decision_id;
state->m_rdCost[state_id] = decisions->rdCost[decision_id];
if (decisions->prevId[decision_id] > -2)
{
if (decisions->prevId[decision_id] >= 0)
{
if (decisions->prevId[decision_id] > -2) {
if (decisions->prevId[decision_id] >= 0) {
const int prvState = ctxs->m_prev_state_offset + decisions->prevId[decision_id];
state->m_numSigSbb[state_id] = (state->m_numSigSbb[prvState]) + !!decisions->absLevel[decision_id];
state->m_refSbbCtxId[state_id] = state->m_refSbbCtxId[prvState];
@ -930,54 +907,50 @@ static INLINE void updateState(
state->m_sbbFracBits[state_id][1] = state->m_sbbFracBits[prvState][1];
state->m_remRegBins[state_id] = state->m_remRegBins[prvState] - 1;
state->m_goRicePar[state_id] = state->m_goRicePar[prvState];
if (state->m_remRegBins[state_id] >= 4)
{
state->m_remRegBins[state_id] -= (decisions->absLevel[decision_id] < 2 ? (unsigned)decisions->absLevel[decision_id] : 3);
if (state->m_remRegBins[state_id] >= 4) {
state->m_remRegBins[state_id] -= (decisions->absLevel[decision_id] < 2
? (unsigned)decisions->absLevel[decision_id]
: 3);
}
memcpy(state->m_absLevelsAndCtxInit[state_id], state->m_absLevelsAndCtxInit[prvState], 48 * sizeof(uint8_t));
}
else
{
else {
state->m_numSigSbb[state_id] = 1;
state->m_refSbbCtxId[state_id] = -1;
int ctxBinSampleRatio = 28; //(scanInfo.chType == CHANNEL_TYPE_LUMA) ? MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT_LUMA : MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT_CHROMA;
state->m_remRegBins[state_id] = (state->effWidth * state->effHeight * ctxBinSampleRatio) / 16 - (decisions->absLevel[decision_id] < 2 ? (unsigned)decisions->absLevel[decision_id] : 3);
int ctxBinSampleRatio = 28;
//(scanInfo.chType == CHANNEL_TYPE_LUMA) ? MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT_LUMA : MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT_CHROMA;
state->m_remRegBins[state_id] = (state->effWidth * state->effHeight * ctxBinSampleRatio) / 16 - (
decisions->absLevel[decision_id] < 2 ? (unsigned)decisions->absLevel[decision_id] : 3);
memset(state->m_absLevelsAndCtxInit[state_id], 0, 48 * sizeof(uint8_t));
}
uint8_t* levels = (uint8_t*)(state->m_absLevelsAndCtxInit[state_id]);
levels[scan_pos & 15] = (uint8_t)MIN(255, decisions->absLevel[decision_id]);
if (state->m_remRegBins[state_id] >= 4)
{
if (state->m_remRegBins[state_id] >= 4) {
coeff_t tinit = state->m_absLevelsAndCtxInit[state_id][8 + ((scan_pos - 1) & 15)];
coeff_t sumAbs1 = (tinit >> 3) & 31;
coeff_t sumNum = tinit & 7;
#define UPDATE(k) {coeff_t t=levels[next_nb_info_ssb.inPos[k]]; sumAbs1+=MIN(4+(t&1),t); sumNum+=!!t; }
if (numIPos == 1)
{
if (numIPos == 1) {
UPDATE(0);
}
else if (numIPos == 2)
{
else if (numIPos == 2) {
UPDATE(0);
UPDATE(1);
}
else if (numIPos == 3)
{
else if (numIPos == 3) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
}
else if (numIPos == 4)
{
else if (numIPos == 4) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
UPDATE(3);
}
else if (numIPos == 5)
{
else if (numIPos == 5) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
@ -986,37 +959,35 @@ static INLINE void updateState(
}
#undef UPDATE
coeff_t sumGt1 = sumAbs1 - sumNum;
state->m_sigFracBits[state_id][0] = state->m_sigFracBitsArray[state_id][sigCtxOffsetNext + MIN((sumAbs1 + 1) >> 1, 3)][0];
state->m_sigFracBits[state_id][1] = state->m_sigFracBitsArray[state_id][sigCtxOffsetNext + MIN((sumAbs1 + 1) >> 1, 3)][1];
memcpy(state->m_coeffFracBits[state_id], state->m_gtxFracBitsArray[gtxCtxOffsetNext + (sumGt1 < 4 ? sumGt1 : 4)], sizeof(state->m_coeffFracBits[0]));
state->m_sigFracBits[state_id][0] = state->m_sigFracBitsArray[state_id][sigCtxOffsetNext + MIN(
(sumAbs1 + 1) >> 1, 3)][0];
state->m_sigFracBits[state_id][1] = state->m_sigFracBitsArray[state_id][sigCtxOffsetNext + MIN(
(sumAbs1 + 1) >> 1, 3)][1];
memcpy(state->m_coeffFracBits[state_id], state->m_gtxFracBitsArray[gtxCtxOffsetNext + (sumGt1 < 4 ? sumGt1 : 4)],
sizeof(state->m_coeffFracBits[0]));
coeff_t sumAbs = state->m_absLevelsAndCtxInit[state_id][8 + ((scan_pos - 1) & 15)] >> 8;
#define UPDATE(k) {coeff_t t=levels[next_nb_info_ssb.inPos[k]]; sumAbs+=t; }
if (numIPos == 1)
{
if (numIPos == 1) {
UPDATE(0);
}
else if (numIPos == 2)
{
else if (numIPos == 2) {
UPDATE(0);
UPDATE(1);
}
else if (numIPos == 3)
{
else if (numIPos == 3) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
}
else if (numIPos == 4)
{
else if (numIPos == 4) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
UPDATE(3);
}
else if (numIPos == 5)
{
else if (numIPos == 5) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
@ -1024,48 +995,40 @@ static INLINE void updateState(
UPDATE(4);
}
#undef UPDATE
if (extRiceFlag)
{
if (extRiceFlag) {
unsigned currentShift = templateAbsCompare(sumAbs);
sumAbs = sumAbs >> currentShift;
int sumAll = MAX(MIN(31, (int)sumAbs - (int)baseLevel), 0);
state->m_goRicePar[state_id] = g_goRiceParsCoeff[sumAll];
state->m_goRicePar[state_id] += currentShift;
}
else
{
else {
int sumAll = MAX(MIN(31, (int)sumAbs - 4 * 5), 0);
state->m_goRicePar[state_id] = g_goRiceParsCoeff[sumAll];
}
}
else
{
else {
coeff_t sumAbs = (state->m_absLevelsAndCtxInit[state_id][8 + ((scan_pos - 1) & 15)]) >> 8;
#define UPDATE(k) {coeff_t t=levels[next_nb_info_ssb.inPos[k]]; sumAbs+=t; }
if (numIPos == 1)
{
if (numIPos == 1) {
UPDATE(0);
}
else if (numIPos == 2)
{
else if (numIPos == 2) {
UPDATE(0);
UPDATE(1);
}
else if (numIPos == 3)
{
else if (numIPos == 3) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
}
else if (numIPos == 4)
{
else if (numIPos == 4) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
UPDATE(3);
}
else if (numIPos == 5)
{
else if (numIPos == 5) {
UPDATE(0);
UPDATE(1);
UPDATE(2);
@ -1073,16 +1036,14 @@ static INLINE void updateState(
UPDATE(4);
}
#undef UPDATE
if (extRiceFlag)
{
if (extRiceFlag) {
unsigned currentShift = templateAbsCompare(sumAbs);
sumAbs = sumAbs >> currentShift;
sumAbs = MIN(31, sumAbs);
state->m_goRicePar[state_id] = g_goRiceParsCoeff[sumAbs];
state->m_goRicePar[state_id] += currentShift;
}
else
{
else {
sumAbs = MIN(31, sumAbs);
state->m_goRicePar[state_id] = g_goRiceParsCoeff[sumAbs];
}