inline int32_t roundIBDI(int32_t num, int32_t den)
{
return num >= 0 ? ((num * 2 + den)/(den * 2)) : -((-num * 2 + den)/(den * 2));
}
inline int8_t signOf(int x)
{
return (x >> 31) | ((int)((((uint32_t)-x)) >> 31));
}
inline int signOf2(const int a, const int b)
{
int r = 0;
if (a < b) r = -1;
if (a > b) r = 1;
return r;
}
/**
* @brief 计算D_post和 D_pre的差值,其中D_pre和D_post分别表示原始像素与重构像素(SAO补偿前、补偿后)之间的失真。
* @param count : 一个CTB内某个特定SAO类型样本的个数
* @param offset : 一个CTB内某个特定SAO类型样本的补偿值
* @param offsetOrg : 原始像素与重构像素(SAO补偿前)之间的差值之和
*/
inline int64_t estSaoDist(int32_t count, int32_t offset, int32_t offsetOrg)
{
return (count * offset - offsetOrg * 2) * offset;
}
/**
* @brief 边界补偿模式下像素的5种分类 :
* 第1类谷点和第2类凹拐点,需要加上一个正补偿值;
* 第4类峰点和第3类凸拐点,需要加上一个负补偿值;
* 第0类像素不进行补偿。
*/
const uint32_t SAO::s_eoTable[NUM_EDGETYPE] =
{
1,
2,
0,
3,
4
};
/**
* @brief 创建SAO的部分参数
*/
bool SAO::create(x265_param* param, int initCommon)
{
m_param = param;
m_chromaFormat = param->internalCsp;
m_hChromaShift = CHROMA_H_SHIFT(param->internalCsp);
m_vChromaShift = CHROMA_V_SHIFT(param->internalCsp);
m_numCuInWidth = (m_param->sourceWidth + g_maxCUSize - 1) / g_maxCUSize;
m_numCuInHeight = (m_param->sourceHeight + g_maxCUSize - 1) / g_maxCUSize;
const pixel maxY = (1 << X265_DEPTH) - 1;
const pixel rangeExt = maxY >> 1;
int numCtu = m_numCuInWidth * m_numCuInHeight;
for (int i = 0; i < (param->internalCsp != X265_CSP_I400 ? 3 : 1); i++)
{
CHECKED_MALLOC(m_tmpL1[i], pixel, g_maxCUSize + 1);
CHECKED_MALLOC(m_tmpL2[i], pixel, g_maxCUSize + 1);
CHECKED_MALLOC(m_tmpU[i], pixel, m_numCuInWidth * g_maxCUSize + 2 + 32);
m_tmpU[i] += 1;
}
if (initCommon)
{
if (m_param->bSaoNonDeblocked)
{
CHECKED_MALLOC(m_countPreDblk, PerPlane, numCtu);
CHECKED_MALLOC(m_offsetOrgPreDblk, PerPlane, numCtu);
}
CHECKED_MALLOC(m_depthSaoRate, double, 2 * SAO_DEPTHRATE_SIZE);
m_depthSaoRate[0 * SAO_DEPTHRATE_SIZE + 0] = 0;
m_depthSaoRate[0 * SAO_DEPTHRATE_SIZE + 1] = 0;
m_depthSaoRate[0 * SAO_DEPTHRATE_SIZE + 2] = 0;
m_depthSaoRate[0 * SAO_DEPTHRATE_SIZE + 3] = 0;
m_depthSaoRate[1 * SAO_DEPTHRATE_SIZE + 0] = 0;
m_depthSaoRate[1 * SAO_DEPTHRATE_SIZE + 1] = 0;
m_depthSaoRate[1 * SAO_DEPTHRATE_SIZE + 2] = 0;
m_depthSaoRate[1 * SAO_DEPTHRATE_SIZE + 3] = 0;
CHECKED_MALLOC(m_clipTableBase, pixel, maxY + 2 * rangeExt);
m_clipTable = &(m_clipTableBase[rangeExt]);
for (int i = 0; i < rangeExt; i++)
m_clipTableBase[i] = 0;
for (int i = 0; i < maxY; i++)
m_clipTable[i] = (pixel)i;
for (int i = maxY; i < maxY + rangeExt; i++)
m_clipTable[i] = maxY;
}
else
{
m_countPreDblk = NULL;
m_offsetOrgPreDblk = NULL;
m_clipTableBase = NULL;
m_clipTable = NULL;
}
return true;
fail:
return false;
}
void SAO::allocSaoParam(SAOParam* saoParam) const
{
int planes = (m_param->internalCsp != X265_CSP_I400) ? 3 : 1;
saoParam->numCuInWidth = m_numCuInWidth;
for (int i = 0; i < planes; i++)
saoParam->ctuParam[i] = new SaoCtuParam[m_numCuInHeight * m_numCuInWidth];
}
/**
* @brief 根据SAO补偿模式对重构像素值进行补偿.
* @param addr : 从上到下、从左到右,当前CTU的序号
* @param typeIdx : SAO补偿模式,取值SAO_EO_X 或 SAO_BO
* @param plane : 颜色空间平面的序号,亮度平面为0,两个色度平面分别为1和2.
*/
void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
{
PicYuv* reconPic = m_frame->m_reconPic;
pixel* rec = reconPic->getPlaneAddr(plane, addr);
intptr_t stride = plane ? reconPic->m_strideC : reconPic->m_stride;
uint32_t picWidth = m_param->sourceWidth;
uint32_t picHeight = m_param->sourceHeight;
const CUData* cu = m_frame->m_encData->getPicCTU(addr);
int ctuWidth = g_maxCUSize;
int ctuHeight = g_maxCUSize;
uint32_t lpelx = cu->m_cuPelX;
uint32_t tpely = cu->m_cuPelY;
if (plane)
{
picWidth >>= m_hChromaShift;
picHeight >>= m_vChromaShift;
ctuWidth >>= m_hChromaShift;
ctuHeight >>= m_vChromaShift;
lpelx >>= m_hChromaShift;
tpely >>= m_vChromaShift;
}
uint32_t rpelx = x265_min(lpelx + ctuWidth, picWidth);
uint32_t bpely = x265_min(tpely + ctuHeight, picHeight);
ctuWidth = rpelx - lpelx;
ctuHeight = bpely - tpely;
int8_t _upBuff1[MAX_CU_SIZE + 2], *upBuff1 = _upBuff1 + 1, signLeft1[2];
int8_t _upBufft[MAX_CU_SIZE + 2], *upBufft = _upBufft + 1;
memset(_upBuff1 + MAX_CU_SIZE, 0, 2 * sizeof(int8_t));
pixel* tmpL = m_tmpL1[plane];
pixel* tmpU = &(m_tmpU[plane][lpelx]);
int8_t* offsetEo = m_offsetEo[plane];
switch (typeIdx)
{
case SAO_EO_0:
{... ...}
case SAO_EO_1:
{... ...}
case SAO_EO_2:
{... ...}
case SAO_EO_3:
{... ...}
case SAO_BO:
{... ...}
default: break;
}
}
void SAO::generateLumaOffsets(SaoCtuParam* ctuParam, int idxY, int idxX)
{
PicYuv* reconPic = m_frame->m_reconPic;
intptr_t stride = reconPic->m_stride;
int ctuWidth = g_maxCUSize;
int ctuHeight = g_maxCUSize;
int addr = idxY * m_numCuInWidth + idxX;
pixel* rec = reconPic->getLumaAddr(addr);
if (idxX == 0)
{
for (int i = 0; i < ctuHeight + 1; i++)
{
m_tmpL1[0][i] = rec[0];
rec += stride;
}
}
bool mergeLeftFlag = (ctuParam[addr].mergeMode == SAO_MERGE_LEFT);
int typeIdx = ctuParam[addr].typeIdx;
if (idxX != (m_numCuInWidth - 1))
{
rec = reconPic->getLumaAddr(addr);
for (int i = 0; i < ctuHeight + 1; i++)
{
m_tmpL2[0][i] = rec[ctuWidth - 1];
rec += stride;
}
}
if (typeIdx >= 0)
{
if (!mergeLeftFlag)
{
if (typeIdx == SAO_BO)
{
memset(m_offsetBo[0], 0, sizeof(m_offsetBo[0]));
for (int i = 0; i < SAO_NUM_OFFSET; i++)
m_offsetBo[0][((ctuParam[addr].bandPos + i) & (MAX_NUM_SAO_CLASS - 1))] =
(int8_t)(ctuParam[addr].offset[i] << SAO_BIT_INC);
}
else
{
int offset[NUM_EDGETYPE];
offset[0] = 0;
for (int i = 0; i < SAO_NUM_OFFSET; i++)
offset[i + 1] = ctuParam[addr].offset[i] << SAO_BIT_INC;
for (int edgeType = 0; edgeType < NUM_EDGETYPE; edgeType++)
m_offsetEo[0][edgeType] = (int8_t)offset[s_eoTable[edgeType]];
}
}
applyPixelOffsets(addr, typeIdx, 0);
}
std::swap(m_tmpL1[0], m_tmpL2[0]);
}
void SAO::generateChromaOffsets(SaoCtuParam* ctuParam[3], int idxY, int idxX);
void SAO::calcSaoStatsCTU(int addr, int plane)
{
const PicYuv* reconPic = m_frame->m_reconPic;
const CUData* cu = m_frame->m_encData->getPicCTU(addr);
const pixel* fenc0 = m_frame->m_fencPic->getPlaneAddr(plane, addr);
const pixel* rec0 = reconPic->getPlaneAddr(plane, addr);
const pixel* fenc;
const pixel* rec;
intptr_t stride = plane ? reconPic->m_strideC : reconPic->m_stride;
uint32_t picWidth = m_param->sourceWidth;
uint32_t picHeight = m_param->sourceHeight;
int ctuWidth = g_maxCUSize;
int ctuHeight = g_maxCUSize;
uint32_t lpelx = cu->m_cuPelX;
uint32_t tpely = cu->m_cuPelY;
if (plane)
{
picWidth >>= m_hChromaShift;
picHeight >>= m_vChromaShift;
ctuWidth >>= m_hChromaShift;
ctuHeight >>= m_vChromaShift;
lpelx >>= m_hChromaShift;
tpely >>= m_vChromaShift;
}
uint32_t rpelx = x265_min(lpelx + ctuWidth, picWidth);
uint32_t bpely = x265_min(tpely + ctuHeight, picHeight);
ctuWidth = rpelx - lpelx;
ctuHeight = bpely - tpely;
int startX, startY, endX, endY;
const int plane_offset = plane ? 2 : 0;
int skipB = 4, skipR = 5;
int8_t _upBuff[2 * (MAX_CU_SIZE + 16 + 16)], *upBuff1 = _upBuff + 16,
*upBufft = upBuff1 +(MAX_CU_SIZE + 16 + 16);
ALIGN_VAR_32(int16_t, diff[MAX_CU_SIZE * MAX_CU_SIZE]);
if ((lpelx + ctuWidth < picWidth) & (tpely + ctuHeight < picHeight))
{
X265_CHECK((ctuWidth == ctuHeight) || (m_chromaFormat != X265_CSP_I420),
"video size check failure\n");
if (plane)
primitives.chroma[m_chromaFormat].cu[g_maxLog2CUSize - 2].
sub_ps(diff, MAX_CU_SIZE, fenc0, rec0, stride, stride);
else
primitives.cu[g_maxLog2CUSize - 2].sub_ps(diff, MAX_CU_SIZE, fenc0, rec0, stride, stride);
}
else
{
for(int y = 0; y < ctuHeight; y++)
{
for(int x = 0; x < ctuWidth; x++)
{
diff[y * MAX_CU_SIZE + x] = (fenc0[y * stride + x] – rec0[y * stride + x]);
}
}
}
{
if (m_param->bSaoNonDeblocked)
{
skipB = 3;
skipR = 4;
}
endX = (rpelx == picWidth) ? ctuWidth : ctuWidth - skipR + plane_offset;
endY = (bpely == picHeight) ? ctuHeight : ctuHeight - skipB + plane_offset;
primitives.saoCuStatsBO(diff, rec0, stride, endX, endY,
m_offsetOrg[plane][SAO_BO], m_count[plane][SAO_BO]);
}
{
if (m_param->bSaoNonDeblocked)
{
skipB = 3;
skipR = 5;
}
startX = !lpelx;
endX = (rpelx == picWidth) ? ctuWidth - 1 : ctuWidth - skipR + plane_offset;
primitives.saoCuStatsE0(diff + startX, rec0 + startX, stride, endX - startX, ctuHeight - skipB +
plane_offset, m_offsetOrg[plane][SAO_EO_0], m_count[plane][SAO_EO_0]);
}
{
if (m_param->bSaoNonDeblocked)
{
skipB = 4;
skipR = 4;
}
rec = rec0;
startY = !tpely;
endX = (rpelx == picWidth) ? ctuWidth : ctuWidth - skipR + plane_offset;
endY = (bpely == picHeight) ? ctuHeight - 1 : ctuHeight - skipB + plane_offset;
if (!tpely)
rec += stride;
primitives.sign(upBuff1, rec, &rec[- stride], ctuWidth);
primitives.saoCuStatsE1(diff + startY * MAX_CU_SIZE, rec0 + startY * stride, stride, upBuff1,
endX, endY - startY, m_offsetOrg[plane][SAO_EO_1], m_count[plane][SAO_EO_1]);
}
{
if (m_param->bSaoNonDeblocked)
{
skipB = 4;
skipR = 5;
}
fenc = fenc0;
rec = rec0;
startX = !lpelx;
endX = (rpelx == picWidth) ? ctuWidth - 1 : ctuWidth - skipR + plane_offset;
startY = !tpely;
endY = (bpely == picHeight) ? ctuHeight - 1 : ctuHeight - skipB + plane_offset;
if (!tpely)
{
fenc += stride;
rec += stride;
}
primitives.sign(upBuff1, &rec[startX], &rec[startX - stride - 1], (endX - startX));
primitives.saoCuStatsE2(diff + startX + startY * MAX_CU_SIZE, rec0 + startX + startY * stride,
stride, upBuff1, upBufft, endX - startX, endY - startY,
m_offsetOrg[plane][SAO_EO_2], m_count[plane][SAO_EO_2]);
}
{
if (m_param->bSaoNonDeblocked)
{
skipB = 4;
skipR = 5;
}
fenc = fenc0;
rec = rec0;
startX = !lpelx;
endX = (rpelx == picWidth) ? ctuWidth - 1 : ctuWidth - skipR + plane_offset;
startY = !tpely;
endY = (bpely == picHeight) ? ctuHeight - 1 : ctuHeight - skipB + plane_offset;
if (!tpely)
{
fenc += stride;
rec += stride;
}
primitives.sign(upBuff1, &rec[startX - 1], &rec[startX - 1 - stride + 1],(endX - startX + 1));
primitives.saoCuStatsE3(diff + startX + startY * MAX_CU_SIZE, rec0 + startX + startY * stride,
stride, upBuff1 + 1, endX - startX, endY - startY,
m_offsetOrg[plane][SAO_EO_3], m_count[plane][SAO_EO_3]);
}
}
void SAO::calcSaoStatsCu_BeforeDblk(Frame* frame, int idxX, int idxY);
void SAO::rdoSaoUnitCu(SAOParam* saoParam, int rowBaseAddr, int idxX, int addr)
{
Slice* slice = m_frame->m_encData->m_slice;
const CUData* cu = m_frame->m_encData->getPicCTU(addr);
int qp = cu->m_qp[0];
int64_t lambda[2] = { 0 };
int qpCb = qp;
if (m_param->internalCsp == X265_CSP_I420)
qpCb = x265_clip3(QP_MIN, QP_MAX_MAX, (int)g_chromaScale[qp + slice->m_pps->chromaQpOffset[0]]);
else
qpCb = X265_MIN(qp + slice->m_pps->chromaQpOffset[0], QP_MAX_SPEC);
lambda[0] = (int64_t)floor(256.0 * x265_lambda2_tab[qp]);
lambda[1] = (int64_t)floor(256.0 * x265_lambda2_tab[qpCb]);
const bool allowMerge[2] = {(idxX != 0), (rowBaseAddr != 0)};
const int addrMerge[2] = {(idxX ? addr - 1 : -1), (rowBaseAddr ? addr - m_numCuInWidth : -1)};
bool chroma = m_param->internalCsp != X265_CSP_I400 &&
m_frame->m_fencPic->m_picCsp != X265_CSP_I400;
int planes = chroma ? 3 : 1;
if (m_param->bSaoNonDeblocked)
{
memcpy(m_count, m_countPreDblk[addr], sizeof(m_count));
memcpy(m_offsetOrg, m_offsetOrgPreDblk[addr], sizeof(m_offsetOrg));
}
else
{
memset(m_count, 0, sizeof(m_count));
memset(m_offsetOrg, 0, sizeof(m_offsetOrg));
}
for (int i = 0; i < planes; i++)
saoParam->ctuParam[i][addr].reset();
if (saoParam->bSaoFlag[0])
calcSaoStatsCTU(addr, 0);
if (saoParam->bSaoFlag[1])
{
calcSaoStatsCTU(addr, 1);
calcSaoStatsCTU(addr, 2);
}
saoStatsInitialOffset(planes);
m_entropyCoder.load(m_rdContexts.cur);
m_entropyCoder.resetBits();
if (allowMerge[0])
m_entropyCoder.codeSaoMerge(0);
if (allowMerge[1])
m_entropyCoder.codeSaoMerge(0);
m_entropyCoder.store(m_rdContexts.temp);
int64_t bestCost = 0;
int64_t rateDist = 0;
saoLumaComponentParamDist(saoParam, addr, rateDist, lambda, bestCost);
if (chroma)
saoChromaComponentParamDist(saoParam, addr, rateDist, lambda, bestCost);
if (saoParam->bSaoFlag[0] || saoParam->bSaoFlag[1])
{
for (int mergeIdx = 0; mergeIdx < 2; ++mergeIdx)
{
if (!allowMerge[mergeIdx])
continue;
int64_t mergeDist = 0;
for (int plane = 0; plane < planes; plane++)
{
int64_t estDist = 0;
SaoCtuParam* mergeSrcParam = &(saoParam->ctuParam[plane][addrMerge[mergeIdx]]);
int typeIdx = mergeSrcParam->typeIdx;
if (typeIdx >= 0)
{
int bandPos = (typeIdx == SAO_BO) ? mergeSrcParam->bandPos : 1;
for (int classIdx = 0; classIdx < SAO_NUM_OFFSET; classIdx++)
{
int mergeOffset = mergeSrcParam->offset[classIdx];
estDist += estSaoDist(m_count[plane][typeIdx][classIdx + bandPos], mergeOffset,
m_offsetOrg[plane][typeIdx][classIdx + bandPos]);
}
}
mergeDist += (estDist << 8) / lambda[!!plane];
}
m_entropyCoder.load(m_rdContexts.cur);
m_entropyCoder.resetBits();
if (allowMerge[0])
m_entropyCoder.codeSaoMerge(1 - mergeIdx);
if (allowMerge[1] && (mergeIdx == 1))
m_entropyCoder.codeSaoMerge(1);
uint32_t estRate = m_entropyCoder.getNumberOfWrittenBits();
int64_t mergeCost = mergeDist + estRate;
if (mergeCost < bestCost)
{
SaoMergeMode mergeMode = mergeIdx ? SAO_MERGE_UP : SAO_MERGE_LEFT;
bestCost = mergeCost;
m_entropyCoder.store(m_rdContexts.temp);
for (int plane = 0; plane < planes; plane++)
{
if (saoParam->bSaoFlag[plane > 0])
{
SaoCtuParam* dstCtuParam = &saoParam->ctuParam[plane][addr];
SaoCtuParam* mergeSrcParam = &(saoParam->ctuParam[plane][addrMerge[mergeIdx]]);
dstCtuParam->mergeMode = mergeMode;
dstCtuParam->typeIdx = mergeSrcParam->typeIdx;
dstCtuParam->bandPos = mergeSrcParam->bandPos;
for (int i = 0; i < SAO_NUM_OFFSET; i++)
dstCtuParam->offset[i] = mergeSrcParam->offset[i];
}
}
}
}
if (saoParam->ctuParam[0][addr].typeIdx < 0)
m_numNoSao[0]++;
if (chroma && saoParam->ctuParam[1][addr].typeIdx < 0)
m_numNoSao[1]++;
m_entropyCoder.load(m_rdContexts.temp);
m_entropyCoder.store(m_rdContexts.cur);
}
}
void SAO::saoStatsInitialOffset(int planes)
{
memset(m_offset, 0, sizeof(m_offset));
for (int plane = 0; plane < planes; plane++)
{
for (int typeIdx = 0; typeIdx < MAX_NUM_SAO_TYPE - 1; typeIdx++)
{
for (int classIdx = 1; classIdx < SAO_NUM_OFFSET + 1; classIdx++)
{
int32_t& count = m_count[plane][typeIdx][classIdx];
int32_t& offsetOrg = m_offsetOrg[plane][typeIdx][classIdx];
int32_t& offsetOut = m_offset[plane][typeIdx][classIdx];
if (count)
{
offsetOut = roundIBDI(offsetOrg, count << SAO_BIT_INC);
offsetOut = x265_clip3(-OFFSET_THRESH + 1, OFFSET_THRESH - 1, offsetOut);
if (classIdx < 3)
offsetOut = X265_MAX(offsetOut, 0);
else
offsetOut = X265_MIN(offsetOut, 0);
}
}
}
}
for (int plane = 0; plane < planes; plane++)
{
for (int classIdx = 0; classIdx < MAX_NUM_SAO_CLASS; classIdx++)
{
int32_t& count = m_count[plane][SAO_BO][classIdx];
int32_t& offsetOrg = m_offsetOrg[plane][SAO_BO][classIdx];
int32_t& offsetOut = m_offset[plane][SAO_BO][classIdx];
if (count)
{
offsetOut = roundIBDI(offsetOrg, count << SAO_BIT_INC);
offsetOut = x265_clip3(-OFFSET_THRESH + 1, OFFSET_THRESH - 1, offsetOut);
}
}
}
}
inline int64_t SAO::calcSaoRdoCost(int64_t distortion, uint32_t bits, int64_t lambda)
{
return distortion + ((bits * lambda + 128) >> 8);
}
/**
* @brief 找到最优率失真代价及对应的补偿值和失真值.
* @param typeIdx : SAO模式,即 SAO_EO_X 和 SAO_BO
* @param lambda : 拉格朗日乘子,取值依赖QP,即 256.0 * x265_lambda2_tab[qp]
* @param count : typeIdx模式下,某classIdx的点的数目
* @param offsetOrg : 原始像素与重构像素(SAO补偿前)之间的差值之和
* @param offset[输出] : 最优率失真代价对应的补偿值
* @param distClasses[输出] : 最优率失真代价对应的失真
* @param costClasses[输出] : 最优率失真代价
*/
void SAO::estIterOffset(int typeIdx, int64_t lambda, int32_t count, int32_t offsetOrg,
int32_t& offset, int32_t& distClasses, int64_t& costClasses)
{
int bestOffset = 0;
distClasses = 0;
int64_t bestCost = calcSaoRdoCost(0, 1, lambda);
while (offset != 0)
{
uint32_t rate = (typeIdx == SAO_BO) ? (abs(offset) + 2) : (abs(offset) + 1);
if (abs(offset) == OFFSET_THRESH - 1)
rate--;
int64_t dist = estSaoDist(count, offset << SAO_BIT_INC, offsetOrg);
int64_t cost = calcSaoRdoCost(dist, rate, lambda);
if (cost < bestCost)
{
bestCost = cost;
bestOffset = offset;
distClasses = (int)dist;
}
offset = (offset > 0) ? (offset - 1) : (offset + 1);
}
costClasses = bestCost;
offset = bestOffset;
}
void SAO::saoLumaComponentParamDist(SAOParam* saoParam, int32_t addr, int64_t& rateDist,
int64_t* lambda, int64_t &bestCost)
{
int64_t bestDist = 0;
int bestTypeIdx = -1;
SaoCtuParam* lclCtuParam = &saoParam->ctuParam[0][addr];
int32_t distClasses[MAX_NUM_SAO_CLASS];
int64_t costClasses[MAX_NUM_SAO_CLASS];
m_entropyCoder.load(m_rdContexts.temp);
m_entropyCoder.resetBits();
m_entropyCoder.codeSaoType(0);
int64_t costPartBest = calcSaoRdoCost(0, m_entropyCoder.getNumberOfWrittenBits(), lambda[0]);
for (int typeIdx = 0; typeIdx < MAX_NUM_SAO_TYPE - 1; typeIdx++)
{
int64_t estDist = 0;
for (int classIdx = 1; classIdx < SAO_NUM_OFFSET + 1; classIdx++)
{
int32_t& count = m_count[0][typeIdx][classIdx];
int32_t& offsetOrg = m_offsetOrg[0][typeIdx][classIdx];
int32_t& offsetOut = m_offset[0][typeIdx][classIdx];
estIterOffset(typeIdx, lambda[0], count, offsetOrg, offsetOut,
distClasses[classIdx], costClasses[classIdx]);
estDist += distClasses[classIdx];
}
m_entropyCoder.load(m_rdContexts.temp);
m_entropyCoder.resetBits();
m_entropyCoder.codeSaoOffsetEO(m_offset[0][typeIdx] + 1, typeIdx, 0);
int64_t cost = calcSaoRdoCost(estDist, m_entropyCoder.getNumberOfWrittenBits(), lambda[0]);
if (cost < costPartBest)
{
costPartBest = cost;
bestDist = estDist;
bestTypeIdx = typeIdx;
}
}
if (bestTypeIdx != -1)
{
lclCtuParam->mergeMode = SAO_MERGE_NONE;
lclCtuParam->typeIdx = bestTypeIdx;
lclCtuParam->bandPos = 0;
for (int classIdx = 0; classIdx < SAO_NUM_OFFSET; classIdx++)
lclCtuParam->offset[classIdx] = m_offset[0][bestTypeIdx][classIdx + 1];
}
int64_t estDist = 0;
for (int classIdx = 0; classIdx < MAX_NUM_SAO_CLASS; classIdx++)
{
int32_t& count = m_count[0][SAO_BO][classIdx];
int32_t& offsetOrg = m_offsetOrg[0][SAO_BO][classIdx];
int32_t& offsetOut = m_offset[0][SAO_BO][classIdx];
estIterOffset(SAO_BO, lambda[0], count, offsetOrg, offsetOut,
distClasses[classIdx], costClasses[classIdx]);
}
int64_t bestRDCostBO = MAX_INT64;
int32_t bestClassBO = 0;
for (int i = 0; i < MAX_NUM_SAO_CLASS - SAO_NUM_OFFSET + 1; i++)
{
int64_t currentRDCost = 0;
for (int j = i; j < i + SAO_NUM_OFFSET; j++)
currentRDCost += costClasses[j];
if (currentRDCost < bestRDCostBO)
{
bestRDCostBO = currentRDCost;
bestClassBO = i;
}
}
estDist = 0;
for (int classIdx = bestClassBO; classIdx < bestClassBO + SAO_NUM_OFFSET; classIdx++)
estDist += distClasses[classIdx];
m_entropyCoder.load(m_rdContexts.temp);
m_entropyCoder.resetBits();
m_entropyCoder.codeSaoOffsetBO(m_offset[0][SAO_BO] + bestClassBO, bestClassBO, 0);
int64_t cost = calcSaoRdoCost(estDist, m_entropyCoder.getNumberOfWrittenBits(), lambda[0]);
if (cost < costPartBest)
{
costPartBest = cost;
bestDist = estDist;
lclCtuParam->mergeMode = SAO_MERGE_NONE;
lclCtuParam->typeIdx = SAO_BO;
lclCtuParam->bandPos = bestClassBO;
for (int classIdx = 0; classIdx < SAO_NUM_OFFSET; classIdx++)
lclCtuParam->offset[classIdx] = m_offset[0][SAO_BO][classIdx + bestClassBO];
}
rateDist = (bestDist << 8) / lambda[0];
m_entropyCoder.load(m_rdContexts.temp);
m_entropyCoder.codeSaoOffset(*lclCtuParam, 0);
m_entropyCoder.store(m_rdContexts.temp);
}
void SAO::saoChromaComponentParamDist(SAOParam* saoParam, int32_t addr,
int64_t& rateDist, int64_t* lambda, int64_t &bestCost);
void saoCuStatsBO_c(const int16_t *diff, const pixel *rec, intptr_t stride,
int endX, int endY, int32_t *stats, int32_t *count)
{
const int boShift = X265_DEPTH - SAO_BO_BITS;
for (int y = 0; y < endY; y++)
{
for (int x = 0; x < endX; x++)
{
int classIdx = rec[x] >> boShift;
stats[classIdx] += diff[x];
count[classIdx]++;
}
diff += MAX_CU_SIZE;
rec += stride;
}
}
void saoCuStatsE0_c(const int16_t *diff, const pixel *rec, intptr_t stride,
int endX, int endY, int32_t *stats, int32_t *count)
{
int32_t tmp_stats[SAO::NUM_EDGETYPE];
int32_t tmp_count[SAO::NUM_EDGETYPE];
memset(tmp_stats, 0, sizeof(tmp_stats));
memset(tmp_count, 0, sizeof(tmp_count));
for (int y = 0; y < endY; y++)
{
int signLeft = signOf(rec[0] - rec[-1]);
for (int x = 0; x < endX; x++)
{
int signRight = signOf2(rec[x], rec[x + 1]);
uint32_t edgeType = signRight + signLeft + 2;
signLeft = -signRight;
X265_CHECK(edgeType <= 4, "edgeType check failure\n");
tmp_stats[edgeType] += diff[x];
tmp_count[edgeType]++;
}
diff += MAX_CU_SIZE;
rec += stride;
}
for (int x = 0; x < SAO::NUM_EDGETYPE; x++)
{
stats[SAO::s_eoTable[x]] += tmp_stats[x];
count[SAO::s_eoTable[x]] += tmp_count[x];
}
}
void saoCuStatsE1_c(const int16_t *diff, const pixel *rec, intptr_t stride,
int8_t *upBuff1, int endX, int endY, int32_t *stats, int32_t *count);
void saoCuStatsE2_c(const int16_t *diff, const pixel *rec, intptr_t stride, int8_t
*upBuff1, int8_t *upBufft, int endX, int endY, int32_t *stats, int32_t *count);
void saoCuStatsE3_c(const int16_t *diff, const pixel *rec, intptr_t stride,
int8_t *upBuff1, int endX, int endY, int32_t *stats, int32_t *count);