#ifndef X265_QUANT_H
#define X265_QUANT_H
#include "common.h"
#include "scalinglist.h"
#include "contexts.h"
namespace X265_NS {
class CUData;
class Entropy;
struct TUEntropyCodingParameters;
struct QpParam
{
int rem;
int per;
int qp;
int64_t lambda2;
int32_t lambda;
QpParam() : qp(MAX_INT) {}
void setQpParam(int qpScaled)
{
if (qp != qpScaled)
{
rem = qpScaled % 6;
per = qpScaled / 6;
qp = qpScaled;
lambda2 = (int64_t)(x265_lambda2_tab[qp - QP_BD_OFFSET] * 256. + 0.5);
lambda = (int32_t)(x265_lambda_tab[qp - QP_BD_OFFSET] * 256. + 0.5);
X265_CHECK((x265_lambda_tab[qp - QP_BD_OFFSET] * 256. + 0.5) < (double)MAX_INT, "x265_lambda_tab[] value too large\n");
}
}
};
struct NoiseReduction
{
ALIGN_VAR_16(uint32_t, nrResidualSum[MAX_NUM_TR_CATEGORIES][MAX_NUM_TR_COEFFS]);
uint32_t nrCount[MAX_NUM_TR_CATEGORIES];
uint16_t nrOffsetDenoise[MAX_NUM_TR_CATEGORIES][MAX_NUM_TR_COEFFS];
uint16_t (*offset)[MAX_NUM_TR_COEFFS];
uint32_t (*residualSum)[MAX_NUM_TR_COEFFS];
uint32_t *count;
};
class Quant
{
protected:
const ScalingList* m_scalingList;
Entropy* m_entropyCoder;
QpParam m_qpParam[3];
int m_rdoqLevel;
int32_t m_psyRdoqScale;
int16_t* m_resiDctCoeff;
int16_t* m_fencDctCoeff;
int16_t* m_fencShortBuf;
enum { IEP_RATE = 32768 };
public:
NoiseReduction* m_nr;
NoiseReduction* m_frameNr;
Quant();
~Quant();
bool init(double psyScale, const ScalingList& scalingList, Entropy& entropy);
bool allocNoiseReduction(const x265_param& param);
void setQPforQuant(const CUData& ctu, int qp);
uint32_t transformNxN(const CUData& cu, const pixel* fenc, uint32_t fencStride, const int16_t* residual, uint32_t resiStride, coeff_t* coeff,
uint32_t log2TrSize, TextType ttype, uint32_t absPartIdx, bool useTransformSkip);
void invtransformNxN(const CUData& cu, int16_t* residual, uint32_t resiStride, const coeff_t* coeff,
uint32_t log2TrSize, TextType ttype, bool bIntra, bool useTransformSkip, uint32_t numSig);
uint64_t ssimDistortion(const CUData& cu, const pixel* fenc, uint32_t fStride, const pixel* recon, intptr_t rstride,
uint32_t log2TrSize, TextType ttype, uint32_t absPartIdx);
static uint32_t calcPatternSigCtx(uint64_t sigCoeffGroupFlag64, uint32_t cgPosX, uint32_t cgPosY, uint32_t cgBlkPos, uint32_t trSizeCG)
{
if (trSizeCG == 1)
return 0;
X265_CHECK(trSizeCG <= 8, "transform CG is too large\n");
X265_CHECK(cgBlkPos < 64, "cgBlkPos is too large\n");
const uint32_t sigPos = (uint32_t)(sigCoeffGroupFlag64 >> (cgBlkPos + 1));
const uint32_t sigRight = (cgPosX != (trSizeCG - 1)) & sigPos;
const uint32_t sigLower = (cgPosY != (trSizeCG - 1)) & (sigPos >> (trSizeCG - 1));
return sigRight + sigLower * 2;
}
static uint32_t getSigCoeffGroupCtxInc(uint64_t cgGroupMask, uint32_t cgPosX, uint32_t cgPosY, uint32_t cgBlkPos, uint32_t trSizeCG)
{
X265_CHECK(cgBlkPos < 64, "cgBlkPos is too large\n");
const uint32_t sigPos = (uint32_t)(cgGroupMask >> (cgBlkPos + 1));
const uint32_t sigRight = (cgPosX != (trSizeCG - 1)) & sigPos;
const uint32_t sigLower = (cgPosY != (trSizeCG - 1)) & (sigPos >> (trSizeCG - 1));
return (sigRight | sigLower);
}
static uint32_t getSigCtxInc(uint32_t patternSigCtx, uint32_t log2TrSize, uint32_t trSize, uint32_t blkPos, bool bIsLuma, uint32_t firstSignificanceMapContext);
protected:
void setChromaQP(int qpin, TextType ttype, int chFmt);
uint32_t signBitHidingHDQ(int16_t* qcoeff, int32_t* deltaU, uint32_t numSig, const TUEntropyCodingParameters &codingParameters, uint32_t log2TrSize);
template<uint32_t log2TrSize>
uint32_t rdoQuant(const CUData& cu, int16_t* dstCoeff, TextType ttype, uint32_t absPartIdx, bool usePsy);
public:
typedef uint32_t (Quant::*rdoQuant_t)(const CUData& cu, int16_t* dstCoeff, TextType ttype, uint32_t absPartIdx, bool usePsy);
private:
static rdoQuant_t rdoQuant_func[NUM_CU_DEPTH];
};
}
#endif