diff --git a/source/Lib/CommonLib/AdaptiveLoopFilter.cpp b/source/Lib/CommonLib/AdaptiveLoopFilter.cpp index 768b71ab2cf7a0a825d08840175ec3df96492f7d..679c8ce3458c13dc4e69285d44fe30373025f0e1 100644 --- a/source/Lib/CommonLib/AdaptiveLoopFilter.cpp +++ b/source/Lib/CommonLib/AdaptiveLoopFilter.cpp @@ -209,72 +209,39 @@ bool AdaptiveLoopFilter::isCrossedByVirtualBoundaries( const CodingStructure& cs return numHorVirBndry > 0 || numVerVirBndry > 0 || clipTop || clipBottom || clipLeft || clipRight || rasterSliceAlfPad; } -const int AdaptiveLoopFilter::m_fixedFilterSetCoeff[ALF_FIXED_FILTER_NUM][MAX_NUM_ALF_LUMA_COEFF] = -{ - { 0, 0, 2, -3, 1, -4, 1, 7, -1, 1, -1, 5, 0 }, - { 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, -1, 2, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0 }, - { 2, 2, -7, -3, 0, -5, 13, 22, 12, -3, -3, 17, 0 }, - { -1, 0, 6, -8, 1, -5, 1, 23, 0, 2, -5, 10, 0 }, - { 0, 0, -1, -1, 0, -1, 2, 1, 0, 0, -1, 4, 0 }, - { 0, 0, 3, -11, 1, 0, -1, 35, 5, 2, -9, 9, 0 }, - { 0, 0, 8, -8, -2, -7, 4, 4, 2, 1, -1, 25, 0 }, - { 0, 0, 1, -1, 0, -3, 1, 3, -1, 1, -1, 3, 0 }, - { 0, 0, 3, -3, 0, -6, 5, -1, 2, 1, -4, 21, 0 }, - { -7, 1, 5, 4, -3, 5, 11, 13, 12, -8, 11, 12, 0 }, - { -5, -3, 6, -2, -3, 8, 14, 15, 2, -7, 11, 16, 0 }, - { 2, -1, -6, -5, -2, -2, 20, 14, -4, 0, -3, 25, 0 }, - { 3, 1, -8, -4, 0, -8, 22, 5, -3, 2, -10, 29, 0 }, - { 2, 1, -7, -1, 2, -11, 23, -5, 0, 2, -10, 29, 0 }, - { -6, -3, 8, 9, -4, 8, 9, 7, 14, -2, 8, 9, 0 }, - { 2, 1, -4, -7, 0, -8, 17, 22, 1, -1, -4, 23, 0 }, - { 3, 0, -5, -7, 0, -7, 15, 18, -5, 0, -5, 27, 0 }, - { 2, 0, 0, -7, 1, -10, 13, 13, -4, 2, -7, 24, 0 }, - { 3, 3, -13, 4, -2, -5, 9, 21, 25, -2, -3, 12, 0 }, - { -5, -2, 7, -3, -7, 9, 8, 9, 16, -2, 15, 12, 0 }, - { 0, -1, 0, -7, -5, 4, 11, 11, 8, -6, 12, 21, 0 }, - { 3, -2, -3, -8, -4, -1, 16, 15, -2, -3, 3, 26, 0 }, - { 2, 1, -5, -4, -1, -8, 16, 4, -2, 1, -7, 33, 0 }, - { 2, 1, -4, -2, 1, -10, 17, -2, 0, 2, -11, 33, 0 }, - { 1, -2, 7, -15, -16, 10, 8, 8, 20, 11, 14, 11, 0 }, - { 2, 2, 3, -13, -13, 4, 8, 12, 2, -3, 16, 24, 0 }, - { 1, 4, 0, -7, -8, -4, 9, 9, -2, -2, 8, 29, 0 }, - { 1, 1, 2, -4, -1, -6, 6, 3, -1, -1, -3, 30, 0 }, - { -7, 3, 2, 10, -2, 3, 7, 11, 19, -7, 8, 10, 0 }, - { 0, -2, -5, -3, -2, 4, 20, 15, -1, -3, -1, 22, 0 }, - { 3, -1, -8, -4, -1, -4, 22, 8, -4, 2, -8, 28, 0 }, - { 0, 3, -14, 3, 0, 1, 19, 17, 8, -3, -7, 20, 0 }, - { 0, 2, -1, -8, 3, -6, 5, 21, 1, 1, -9, 13, 0 }, - { -4, -2, 8, 20, -2, 2, 3, 5, 21, 4, 6, 1, 0 }, - { 2, -2, -3, -9, -4, 2, 14, 16, 3, -6, 8, 24, 0 }, - { 2, 1, 5, -16, -7, 2, 3, 11, 15, -3, 11, 22, 0 }, - { 1, 2, 3, -11, -2, -5, 4, 8, 9, -3, -2, 26, 0 }, - { 0, -1, 10, -9, -1, -8, 2, 3, 4, 0, 0, 29, 0 }, - { 1, 2, 0, -5, 1, -9, 9, 3, 0, 1, -7, 20, 0 }, - { -2, 8, -6, -4, 3, -9, -8, 45, 14, 2, -13, 7, 0 }, - { 1, -1, 16, -19, -8, -4, -3, 2, 19, 0, 4, 30, 0 }, - { 1, 1, -3, 0, 2, -11, 15, -5, 1, 2, -9, 24, 0 }, - { 0, 1, -2, 0, 1, -4, 4, 0, 0, 1, -4, 7, 0 }, - { 0, 1, 2, -5, 1, -6, 4, 10, -2, 1, -4, 10, 0 }, - { 3, 0, -3, -6, -2, -6, 14, 8, -1, -1, -3, 31, 0 }, - { 0, 1, 0, -2, 1, -6, 5, 1, 0, 1, -5, 13, 0 }, - { 3, 1, 9, -19, -21, 9, 7, 6, 13, 5, 15, 21, 0 }, - { 2, 4, 3, -12, -13, 1, 7, 8, 3, 0, 12, 26, 0 }, - { 3, 1, -8, -2, 0, -6, 18, 2, -2, 3, -10, 23, 0 }, - { 1, 1, -4, -1, 1, -5, 8, 1, -1, 2, -5, 10, 0 }, - { 0, 1, -1, 0, 0, -2, 2, 0, 0, 1, -2, 3, 0 }, - { 1, 1, -2, -7, 1, -7, 14, 18, 0, 0, -7, 21, 0 }, - { 0, 1, 0, -2, 0, -7, 8, 1, -2, 0, -3, 24, 0 }, - { 0, 1, 1, -2, 2, -10, 10, 0, -2, 1, -7, 23, 0 }, - { 0, 2, 2, -11, 2, -4, -3, 39, 7, 1, -10, 9, 0 }, - { 1, 0, 13, -16, -5, -6, -1, 8, 6, 0, 6, 29, 0 }, - { 1, 3, 1, -6, -4, -7, 9, 6, -3, -2, 3, 33, 0 }, - { 4, 0, -17, -1, -1, 5, 26, 8, -2, 3, -15, 30, 0 }, - { 0, 1, -2, 0, 2, -8, 12, -6, 1, 1, -6, 16, 0 }, - { 0, 0, 0, -1, 1, -4, 4, 0, 0, 0, -3, 11, 0 }, - { 0, 1, 2, -8, 2, -6, 5, 15, 0, 2, -7, 9, 0 }, - { 1, -1, 12, -15, -7, -2, 3, 6, 6, -1, 7, 30, 0 }, +const AlfCoeff AdaptiveLoopFilter::m_fixedFilterSetCoeff[ALF_FIXED_FILTER_NUM][MAX_NUM_ALF_LUMA_COEFF] = { + { 0, 0, 2, -3, 1, -4, 1, 7, -1, 1, -1, 5, 0 }, { 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, -1, 2, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0 }, + { 2, 2, -7, -3, 0, -5, 13, 22, 12, -3, -3, 17, 0 }, { -1, 0, 6, -8, 1, -5, 1, 23, 0, 2, -5, 10, 0 }, + { 0, 0, -1, -1, 0, -1, 2, 1, 0, 0, -1, 4, 0 }, { 0, 0, 3, -11, 1, 0, -1, 35, 5, 2, -9, 9, 0 }, + { 0, 0, 8, -8, -2, -7, 4, 4, 2, 1, -1, 25, 0 }, { 0, 0, 1, -1, 0, -3, 1, 3, -1, 1, -1, 3, 0 }, + { 0, 0, 3, -3, 0, -6, 5, -1, 2, 1, -4, 21, 0 }, { -7, 1, 5, 4, -3, 5, 11, 13, 12, -8, 11, 12, 0 }, + { -5, -3, 6, -2, -3, 8, 14, 15, 2, -7, 11, 16, 0 }, { 2, -1, -6, -5, -2, -2, 20, 14, -4, 0, -3, 25, 0 }, + { 3, 1, -8, -4, 0, -8, 22, 5, -3, 2, -10, 29, 0 }, { 2, 1, -7, -1, 2, -11, 23, -5, 0, 2, -10, 29, 0 }, + { -6, -3, 8, 9, -4, 8, 9, 7, 14, -2, 8, 9, 0 }, { 2, 1, -4, -7, 0, -8, 17, 22, 1, -1, -4, 23, 0 }, + { 3, 0, -5, -7, 0, -7, 15, 18, -5, 0, -5, 27, 0 }, { 2, 0, 0, -7, 1, -10, 13, 13, -4, 2, -7, 24, 0 }, + { 3, 3, -13, 4, -2, -5, 9, 21, 25, -2, -3, 12, 0 }, { -5, -2, 7, -3, -7, 9, 8, 9, 16, -2, 15, 12, 0 }, + { 0, -1, 0, -7, -5, 4, 11, 11, 8, -6, 12, 21, 0 }, { 3, -2, -3, -8, -4, -1, 16, 15, -2, -3, 3, 26, 0 }, + { 2, 1, -5, -4, -1, -8, 16, 4, -2, 1, -7, 33, 0 }, { 2, 1, -4, -2, 1, -10, 17, -2, 0, 2, -11, 33, 0 }, + { 1, -2, 7, -15, -16, 10, 8, 8, 20, 11, 14, 11, 0 }, { 2, 2, 3, -13, -13, 4, 8, 12, 2, -3, 16, 24, 0 }, + { 1, 4, 0, -7, -8, -4, 9, 9, -2, -2, 8, 29, 0 }, { 1, 1, 2, -4, -1, -6, 6, 3, -1, -1, -3, 30, 0 }, + { -7, 3, 2, 10, -2, 3, 7, 11, 19, -7, 8, 10, 0 }, { 0, -2, -5, -3, -2, 4, 20, 15, -1, -3, -1, 22, 0 }, + { 3, -1, -8, -4, -1, -4, 22, 8, -4, 2, -8, 28, 0 }, { 0, 3, -14, 3, 0, 1, 19, 17, 8, -3, -7, 20, 0 }, + { 0, 2, -1, -8, 3, -6, 5, 21, 1, 1, -9, 13, 0 }, { -4, -2, 8, 20, -2, 2, 3, 5, 21, 4, 6, 1, 0 }, + { 2, -2, -3, -9, -4, 2, 14, 16, 3, -6, 8, 24, 0 }, { 2, 1, 5, -16, -7, 2, 3, 11, 15, -3, 11, 22, 0 }, + { 1, 2, 3, -11, -2, -5, 4, 8, 9, -3, -2, 26, 0 }, { 0, -1, 10, -9, -1, -8, 2, 3, 4, 0, 0, 29, 0 }, + { 1, 2, 0, -5, 1, -9, 9, 3, 0, 1, -7, 20, 0 }, { -2, 8, -6, -4, 3, -9, -8, 45, 14, 2, -13, 7, 0 }, + { 1, -1, 16, -19, -8, -4, -3, 2, 19, 0, 4, 30, 0 }, { 1, 1, -3, 0, 2, -11, 15, -5, 1, 2, -9, 24, 0 }, + { 0, 1, -2, 0, 1, -4, 4, 0, 0, 1, -4, 7, 0 }, { 0, 1, 2, -5, 1, -6, 4, 10, -2, 1, -4, 10, 0 }, + { 3, 0, -3, -6, -2, -6, 14, 8, -1, -1, -3, 31, 0 }, { 0, 1, 0, -2, 1, -6, 5, 1, 0, 1, -5, 13, 0 }, + { 3, 1, 9, -19, -21, 9, 7, 6, 13, 5, 15, 21, 0 }, { 2, 4, 3, -12, -13, 1, 7, 8, 3, 0, 12, 26, 0 }, + { 3, 1, -8, -2, 0, -6, 18, 2, -2, 3, -10, 23, 0 }, { 1, 1, -4, -1, 1, -5, 8, 1, -1, 2, -5, 10, 0 }, + { 0, 1, -1, 0, 0, -2, 2, 0, 0, 1, -2, 3, 0 }, { 1, 1, -2, -7, 1, -7, 14, 18, 0, 0, -7, 21, 0 }, + { 0, 1, 0, -2, 0, -7, 8, 1, -2, 0, -3, 24, 0 }, { 0, 1, 1, -2, 2, -10, 10, 0, -2, 1, -7, 23, 0 }, + { 0, 2, 2, -11, 2, -4, -3, 39, 7, 1, -10, 9, 0 }, { 1, 0, 13, -16, -5, -6, -1, 8, 6, 0, 6, 29, 0 }, + { 1, 3, 1, -6, -4, -7, 9, 6, -3, -2, 3, 33, 0 }, { 4, 0, -17, -1, -1, 5, 26, 8, -2, 3, -15, 30, 0 }, + { 0, 1, -2, 0, 2, -8, 12, -6, 1, 1, -6, 16, 0 }, { 0, 0, 0, -1, 1, -4, 4, 0, 0, 0, -3, 11, 0 }, + { 0, 1, 2, -8, 2, -6, 5, 15, 0, 2, -7, 9, 0 }, { 1, -1, 12, -15, -7, -2, 3, 6, 6, -1, 7, 30, 0 }, }; // clang-format off @@ -299,10 +266,10 @@ const int AdaptiveLoopFilter::m_classToFilterMapping[ALF_NUM_FIXED_FILTER_SETS][ }; // clang-format on -void AdaptiveLoopFilter::applyCcAlfFilter(CodingStructure &cs, ComponentID compID, const PelBuf &dstBuf, - const PelUnitBuf &recYuvExt, uint8_t *filterControl, - const short filterSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], - const int selectedFilterIdx) +void AdaptiveLoopFilter::applyCcAlfFilter(CodingStructure& cs, ComponentID compID, const PelBuf& dstBuf, + const PelUnitBuf& recYuvExt, uint8_t* filterControl, + const AlfCoeff filterSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], + const int selectedFilterIdx) { bool clipTop = false, clipBottom = false, clipLeft = false, clipRight = false; int numHorVirBndry = 0, numVerVirBndry = 0; @@ -509,7 +476,7 @@ void AdaptiveLoopFilter::ALFProcess(CodingStructure& cs) const Area blkDst( xStart, yStart, w, h ); deriveClassification( m_classifier, buf.get(COMPONENT_Y), blkDst, blkSrc ); const AlfMode m = lumaModes[ctuIdx]; - const short* coeff = getCoeffVals(m); + const AlfCoeff* coeff = getCoeffVals(m); const Pel* clip = getClipVals(m); #if GREEN_METADATA_SEI_ENABLED cs.m_featureCounter.alfLumaType7+= (width * height / 16) ; @@ -533,7 +500,7 @@ void AdaptiveLoopFilter::ALFProcess(CodingStructure& cs) const Area blkDst( xStart >> chromaScaleX, yStart >> chromaScaleY, w >> chromaScaleX, h >> chromaScaleY ); const int altNum = m_modes[compIdx][ctuIdx] - AlfMode::CHROMA0; m_filter5x5Blk(m_classifier, recYuv, buf, blkDst, blkSrc, compID, m_chromaCoeffFinal[altNum], - m_chromaClippFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, + m_chromaClipValsFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, m_alfVBChmaPos); #if GREEN_METADATA_SEI_ENABLED cs.m_featureCounter.alfChromaType5+= ((width >> chromaScaleX) * (height >> chromaScaleY) / 16) ; @@ -549,7 +516,7 @@ void AdaptiveLoopFilter::ALFProcess(CodingStructure& cs) const Area blkSrc(0, 0, w, h); Area blkDst(xStart >> chromaScaleX, yStart >> chromaScaleY, w >> chromaScaleX, h >> chromaScaleY); - const int16_t *filterCoeff = m_ccAlfFilterParam.ccAlfCoeff[compIdx - 1][filterIdx - 1]; + const AlfCoeff* filterCoeff = m_ccAlfFilterParam.ccAlfCoeff[compIdx - 1][filterIdx - 1]; #if GREEN_METADATA_SEI_ENABLED cs.m_featureCounter.alfLumaType7+= (width * height / 16) ; cs.m_featureCounter.alfLumaPels += (width * height); @@ -574,7 +541,7 @@ void AdaptiveLoopFilter::ALFProcess(CodingStructure& cs) Area blk( xPos, yPos, width, height ); deriveClassification( m_classifier, tmpYuv.get( COMPONENT_Y ), blk, blk ); const AlfMode m = lumaModes[ctuIdx]; - const short* coeff = getCoeffVals(m); + const AlfCoeff* coeff = getCoeffVals(m); const Pel* clip = getClipVals(m); #if GREEN_METADATA_SEI_ENABLED cs.m_featureCounter.alfLumaType7+= (width * height / 16) ; @@ -599,7 +566,7 @@ void AdaptiveLoopFilter::ALFProcess(CodingStructure& cs) cs.m_featureCounter.alfChromaPels += ((width >> chromaScaleX) * (height >> chromaScaleY)) ; #endif m_filter5x5Blk(m_classifier, recYuv, tmpYuv, blk, blk, compID, m_chromaCoeffFinal[altNum], - m_chromaClippFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, + m_chromaClipValsFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, m_alfVBChmaPos); } if (cu->slice->m_ccAlfFilterParam.ccAlfFilterEnabled[compIdx - 1]) @@ -643,6 +610,7 @@ void AdaptiveLoopFilter::reconstructCoeffAPSs(CodingStructure& cs, bool luma, bo reconstructCoeff(alfParamTmp, ChannelType::LUMA, isRdo, true); memcpy(m_coeffApsLuma[i], m_coeffFinal, sizeof(m_coeffFinal)); memcpy(m_clippApsLuma[i], m_clippFinal, sizeof(m_clippFinal)); + memcpy(m_clipValsApsLuma[i], m_clipValsFinal, sizeof(m_clipValsFinal)); } } @@ -671,8 +639,8 @@ void AdaptiveLoopFilter::reconstructCoeff(AlfParam &alfParam, const ChannelType for( int altIdx = 0; altIdx < numAlts; ++ altIdx ) { int numFilters = isLuma( channel ) ? alfParam.numLumaFilters : 1; - short* coeff = isLuma( channel ) ? alfParam.lumaCoeff : alfParam.chromaCoeff[altIdx]; - Pel* clipp = isLuma( channel ) ? alfParam.lumaClipp : alfParam.chromaClipp[altIdx]; + AlfCoeff* coeff = isLuma(channel) ? alfParam.lumaCoeff : alfParam.chromaCoeff[altIdx]; + AlfClipIdx* clipp = isLuma(channel) ? alfParam.lumaClipp : alfParam.chromaClipp[altIdx]; for( int filterIdx = 0; filterIdx < numFilters; filterIdx++ ) { @@ -685,15 +653,17 @@ void AdaptiveLoopFilter::reconstructCoeff(AlfParam &alfParam, const ChannelType { m_chromaCoeffFinal[altIdx][coeffIdx] = coeff[coeffIdx]; const int clipIdx = alfParam.nonLinearFlag[channel] ? clipp[coeffIdx] : 0; - m_chromaClippFinal[altIdx][coeffIdx] = isRdo ? clipIdx : m_alfClippingValues[channel][clipIdx]; + m_chromaClippFinal[altIdx][coeffIdx] = clipIdx; + m_chromaClipValsFinal[altIdx][coeffIdx] = m_alfClippingValues[channel][clipIdx]; } m_chromaCoeffFinal[altIdx][numCoeffMinus1] = factor; - m_chromaClippFinal[altIdx][numCoeffMinus1] = isRdo ? 0 : m_alfClippingValues[channel][0]; + m_chromaClippFinal[altIdx][numCoeffMinus1] = 0; + m_chromaClipValsFinal[altIdx][numCoeffMinus1] = m_alfClippingValues[channel][0]; continue; } for( int classIdx = 0; classIdx < numClasses; classIdx++ ) { - int filterIdx = alfParam.filterCoeffDeltaIdx[classIdx]; + AlfBankIdx filterIdx = alfParam.filterCoeffDeltaIdx[classIdx]; CHECK(!(filterIdx >= 0 && filterIdx < alfParam.numLumaFilters), "Bad coeff delta idx in ALF"); for (int coeffIdx = 0; coeffIdx < numCoeffMinus1; ++coeffIdx) @@ -701,15 +671,17 @@ void AdaptiveLoopFilter::reconstructCoeff(AlfParam &alfParam, const ChannelType m_coeffFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + coeffIdx] = coeff[filterIdx * MAX_NUM_ALF_LUMA_COEFF + coeffIdx]; } m_coeffFinal[classIdx* MAX_NUM_ALF_LUMA_COEFF + numCoeffMinus1] = factor; - m_clippFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + numCoeffMinus1] = isRdo ? 0 : m_alfClippingValues[channel][0]; + m_clippFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + numCoeffMinus1] = 0; + m_clipValsFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + numCoeffMinus1] = m_alfClippingValues[channel][0]; for( int coeffIdx = 0; coeffIdx < numCoeffMinus1; ++coeffIdx ) { int clipIdx = alfParam.nonLinearFlag[channel] ? clipp[filterIdx * MAX_NUM_ALF_LUMA_COEFF + coeffIdx] : 0; CHECK(!(clipIdx >= 0 && clipIdx < MAX_ALF_NUM_CLIP_VALS), "Bad clip idx in ALF"); - m_clippFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + coeffIdx] = - isRdo ? clipIdx : m_alfClippingValues[channel][clipIdx]; + m_clippFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + coeffIdx] = clipIdx; + m_clipValsFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + coeffIdx] = m_alfClippingValues[channel][clipIdx]; } - m_clippFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + numCoeffMinus1] = isRdo ? 0 : m_alfClippingValues[channel][0]; + m_clippFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + numCoeffMinus1] = 0; + m_clipValsFinal[classIdx * MAX_NUM_ALF_LUMA_COEFF + numCoeffMinus1] = m_alfClippingValues[channel][0]; } } } @@ -1075,10 +1047,10 @@ void AdaptiveLoopFilter::deriveClassificationBlk(AlfClassifier **classifier, int } template<AlfFilterType filtType> -void AdaptiveLoopFilter::filterBlk(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, - const short *filterSet, const Pel *fClipSet, const ClpRng &clpRng, - CodingStructure &cs, const int vbCTUHeight, int vbPos) +void AdaptiveLoopFilter::filterBlk(AlfClassifier** classifier, const PelUnitBuf& recDst, const CPelUnitBuf& recSrc, + const Area& blkDst, const Area& blk, const ComponentID compId, + const AlfCoeff* filterSet, const Pel* fClipSet, const ClpRng& clpRng, + CodingStructure& cs, const int vbCTUHeight, int vbPos) { CHECK((vbCTUHeight & (vbCTUHeight - 1)) != 0, "vbCTUHeight must be a power of 2"); @@ -1105,7 +1077,7 @@ void AdaptiveLoopFilter::filterBlk(AlfClassifier **classifier, const PelUnitBuf const Pel *pImgYPad0, *pImgYPad1, *pImgYPad2, *pImgYPad3, *pImgYPad4, *pImgYPad5, *pImgYPad6; const Pel *pImg0, *pImg1, *pImg2, *pImg3, *pImg4, *pImg5, *pImg6; - const short *coef = filterSet; + const AlfCoeff* coef = filterSet; const Pel *clip = fClipSet; const int shift = COEFF_SCALE_BITS; @@ -1307,9 +1279,9 @@ void AdaptiveLoopFilter::filterBlk(AlfClassifier **classifier, const PelUnitBuf } template<AlfFilterType filtTypeCcAlf> -void AdaptiveLoopFilter::filterBlkCcAlf(const PelBuf &dstBuf, const CPelUnitBuf &recSrc, const Area &blkDst, - const Area &blkSrc, const ComponentID compId, const int16_t *filterCoeff, - const ClpRngs &clpRngs, CodingStructure &cs, int vbCTUHeight, int vbPos) +void AdaptiveLoopFilter::filterBlkCcAlf(const PelBuf& dstBuf, const CPelUnitBuf& recSrc, const Area& blkDst, + const Area& blkSrc, const ComponentID compId, const AlfCoeff* filterCoeff, + const ClpRngs& clpRngs, CodingStructure& cs, int vbCTUHeight, int vbPos) { CHECK(1 << floorLog2(vbCTUHeight) != vbCTUHeight, "Not a power of 2"); diff --git a/source/Lib/CommonLib/AdaptiveLoopFilter.h b/source/Lib/CommonLib/AdaptiveLoopFilter.h index eb4e7ef8d01ce1c2bc4ec8c582063f0243d0901e..fea596370f686d97a2ddac4a1af2f76d79ca773f 100644 --- a/source/Lib/CommonLib/AdaptiveLoopFilter.h +++ b/source/Lib/CommonLib/AdaptiveLoopFilter.h @@ -92,35 +92,35 @@ public: const int vbCTUHeight, int vbPos); void deriveClassification( AlfClassifier** classifier, const CPelBuf& srcLuma, const Area& blkDst, const Area& blk ); template<AlfFilterType filtTypeCcAlf> - static void filterBlkCcAlf(const PelBuf &dstBuf, const CPelUnitBuf &recSrc, const Area &blkDst, const Area &blkSrc, - const ComponentID compId, const int16_t *filterCoeff, const ClpRngs &clpRngs, - CodingStructure &cs, int vbCTUHeight, int vbPos); - - template<AlfFilterType filtType> - static void filterBlk(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, - int vbPos); + static void filterBlkCcAlf(const PelBuf& dstBuf, const CPelUnitBuf& recSrc, const Area& blkDst, const Area& blkSrc, + const ComponentID compId, const AlfCoeff* filterCoeff, const ClpRngs& clpRngs, + CodingStructure& cs, int vbCTUHeight, int vbPos); + + template<AlfFilterType filtType> static void filterBlk(AlfClassifier** classifier, const PelUnitBuf& recDst, + const CPelUnitBuf& recSrc, const Area& blkDst, const Area& blk, + const ComponentID compId, const AlfCoeff* filterSet, + const Pel* fClipSet, const ClpRng& clpRng, CodingStructure& cs, + const int vbCTUHeight, int vbPos); void (*m_deriveClassificationBlk)(AlfClassifier **classifier, int **laplacian[NUM_DIRECTIONS], const CPelBuf &srcLuma, const Area &blkDst, const Area &blk, const int shift, const int vbCTUHeight, int vbPos); - void (*m_filterCcAlf)(const PelBuf &dstBuf, const CPelUnitBuf &recSrc, const Area &blkDst, const Area &blkSrc, - const ComponentID compId, const int16_t *filterCoeff, const ClpRngs &clpRngs, - CodingStructure &cs, int vbCTUHeight, int vbPos); - void applyCcAlfFilter(CodingStructure &cs, ComponentID compID, const PelBuf &dstBuf, const PelUnitBuf &recYuvExt, - uint8_t * filterControl, - const short filterSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], - const int selectedFilterIdx); + void (*m_filterCcAlf)(const PelBuf& dstBuf, const CPelUnitBuf& recSrc, const Area& blkDst, const Area& blkSrc, + const ComponentID compId, const AlfCoeff* filterCoeff, const ClpRngs& clpRngs, + CodingStructure& cs, int vbCTUHeight, int vbPos); + void applyCcAlfFilter(CodingStructure& cs, ComponentID compID, const PelBuf& dstBuf, const PelUnitBuf& recYuvExt, + uint8_t* filterControl, + const AlfCoeff filterSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], + const int selectedFilterIdx); CcAlfFilterParam &getCcAlfFilterParam() { return m_ccAlfFilterParam; } uint8_t* getCcAlfControlIdc(const ComponentID compID) { return m_ccAlfFilterControl[compID-1]; } - void (*m_filter5x5Blk)(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, + void (*m_filter5x5Blk)(AlfClassifier** classifier, const PelUnitBuf& recDst, const CPelUnitBuf& recSrc, + const Area& blkDst, const Area& blk, const ComponentID compId, const AlfCoeff* filterSet, + const Pel* fClipSet, const ClpRng& clpRng, CodingStructure& cs, const int vbCTUHeight, int vbPos); - void (*m_filter7x7Blk)(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, + void (*m_filter7x7Blk)(AlfClassifier** classifier, const PelUnitBuf& recDst, const CPelUnitBuf& recSrc, + const Area& blkDst, const Area& blk, const ComponentID compId, const AlfCoeff* filterSet, + const Pel* fClipSet, const ClpRng& clpRng, CodingStructure& cs, const int vbCTUHeight, int vbPos); #ifdef TARGET_SIMD_X86 @@ -135,21 +135,24 @@ protected: CcAlfFilterParam m_ccAlfFilterParam; uint8_t* m_ccAlfFilterControl[2] = { nullptr }; static const int m_classToFilterMapping[ALF_NUM_FIXED_FILTER_SETS][MAX_NUM_ALF_CLASSES]; - static const int m_fixedFilterSetCoeff[ALF_FIXED_FILTER_NUM][MAX_NUM_ALF_LUMA_COEFF]; - short m_fixedFilterSetCoeffDec[ALF_NUM_FIXED_FILTER_SETS][MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; - short m_coeffApsLuma[ALF_CTB_MAX_NUM_APS][MAX_NUM_ALF_LUMA_COEFF * MAX_NUM_ALF_CLASSES]; - Pel m_clippApsLuma[ALF_CTB_MAX_NUM_APS][MAX_NUM_ALF_LUMA_COEFF * MAX_NUM_ALF_CLASSES]; + static const AlfCoeff m_fixedFilterSetCoeff[ALF_FIXED_FILTER_NUM][MAX_NUM_ALF_LUMA_COEFF]; + AlfCoeff m_fixedFilterSetCoeffDec[ALF_NUM_FIXED_FILTER_SETS][MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; + AlfCoeff m_coeffApsLuma[ALF_CTB_MAX_NUM_APS][MAX_NUM_ALF_LUMA_COEFF * MAX_NUM_ALF_CLASSES]; + AlfClipIdx m_clippApsLuma[ALF_CTB_MAX_NUM_APS][MAX_NUM_ALF_LUMA_COEFF * MAX_NUM_ALF_CLASSES]; + Pel m_clipValsApsLuma[ALF_CTB_MAX_NUM_APS][MAX_NUM_ALF_LUMA_COEFF * MAX_NUM_ALF_CLASSES]; Pel m_clipDefault[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; bool m_created = false; - short m_chromaCoeffFinal[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; AlfParam* m_alfParamChroma; EnumArray<std::array<Pel, MAX_ALF_NUM_CLIP_VALS>, ChannelType> m_alfClippingValues; std::vector<AlfFilterShape> m_filterShapesCcAlf[2]; EnumArray<std::vector<AlfFilterShape>, ChannelType> m_filterShapes; AlfClassifier** m_classifier; - short m_coeffFinal[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; - Pel m_clippFinal[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; - Pel m_chromaClippFinal[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; + AlfCoeff m_coeffFinal[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; + AlfClipIdx m_clippFinal[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; + Pel m_clipValsFinal[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; + AlfCoeff m_chromaCoeffFinal[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; + AlfClipIdx m_chromaClippFinal[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; + Pel m_chromaClipValsFinal[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; int** m_laplacian[NUM_DIRECTIONS]; int * m_laplacianPtr[NUM_DIRECTIONS][m_CLASSIFICATION_BLK_SIZE + 5]; int m_laplacianData[NUM_DIRECTIONS][m_CLASSIFICATION_BLK_SIZE + 5][m_CLASSIFICATION_BLK_SIZE + 5]; @@ -174,11 +177,14 @@ protected: ChromaFormat m_chromaFormat; ClpRngs m_clpRngs; - short* getCoeffVals(AlfMode m) + const AlfCoeff* getCoeffVals(AlfMode m) const { return isAlfLumaFixed(m) ? m_fixedFilterSetCoeffDec[m - AlfMode::LUMA_FIXED0] : m_coeffApsLuma[m - AlfMode::LUMA0]; } - Pel* getClipVals(AlfMode m) { return isAlfLumaFixed(m) ? m_clipDefault : m_clippApsLuma[m - AlfMode::LUMA0]; } + const Pel* getClipVals(AlfMode m) const + { + return isAlfLumaFixed(m) ? m_clipDefault : m_clipValsApsLuma[m - AlfMode::LUMA0]; + } }; #endif diff --git a/source/Lib/CommonLib/AlfParameters.h b/source/Lib/CommonLib/AlfParameters.h index 5f6cd13daad9b44d1937841c563cecd775922e1b..f4da653105911956cb22cf7e1e4be84bae8f931d 100644 --- a/source/Lib/CommonLib/AlfParameters.h +++ b/source/Lib/CommonLib/AlfParameters.h @@ -111,12 +111,12 @@ struct AlfParam { bool enabledFlag[MAX_NUM_COMPONENT]; // alf_slice_enable_flag, alf_chroma_idc EnumArray<bool, ChannelType> nonLinearFlag; - short lumaCoeff[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; // alf_coeff_luma_delta[i][j] - Pel lumaClipp[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; // alf_clipp_luma_[i][j] + AlfCoeff lumaCoeff[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; // alf_coeff_luma_delta[i][j] + AlfClipIdx lumaClipp[MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; // alf_clipp_luma_[i][j] int numAlternativesChroma; // alf_chroma_num_alts_minus_one + 1 - short chromaCoeff[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; // alf_coeff_chroma[i] - Pel chromaClipp[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; // alf_clipp_chroma[i] - short filterCoeffDeltaIdx[MAX_NUM_ALF_CLASSES]; // filter_coeff_delta[i] + AlfCoeff chromaCoeff[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; // alf_coeff_chroma[i] + AlfClipIdx chromaClipp[ALF_MAX_NUM_ALTERNATIVES_CHROMA][MAX_NUM_ALF_CHROMA_COEFF]; // alf_clipp_chroma[i] + AlfBankIdx filterCoeffDeltaIdx[MAX_NUM_ALF_CLASSES]; // filter_coeff_delta[i] bool alfLumaCoeffFlag[MAX_NUM_ALF_CLASSES]; // alf_luma_coeff_flag[i] int numLumaFilters; // number_of_filters_minus1 + 1 bool alfLumaCoeffDeltaFlag; // alf_luma_coeff_delta_flag @@ -227,7 +227,7 @@ struct CcAlfFilterParam bool ccAlfFilterEnabled[2]; bool ccAlfFilterIdxEnabled[2][MAX_NUM_CC_ALF_FILTERS]; uint8_t ccAlfFilterCount[2]; - short ccAlfCoeff[2][MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; + AlfCoeff ccAlfCoeff[2][MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; int newCcAlfFilter[2]; int numberValidComponents; CcAlfFilterParam() diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h index bfb0f40bfc28577366bf39f39a5d56d34e8029df..9c7b28305120cff3f8323bd2ee438c5ff94f0452 100644 --- a/source/Lib/CommonLib/TypeDef.h +++ b/source/Lib/CommonLib/TypeDef.h @@ -988,6 +988,10 @@ inline int operator-(AlfMode i, AlfMode j) { return to_underlying(i) - to_un inline bool isAlfLumaFixed(AlfMode m) { return m >= AlfMode::LUMA_FIXED0 && m < AlfMode::LUMA0; } +using AlfCoeff = int16_t; +using AlfBankIdx = uint8_t; +using AlfClipIdx = uint8_t; + ////////////////////////////////////////////////////////////////////////// // Encoder modes to try out ////////////////////////////////////////////////////////////////////////// diff --git a/source/Lib/CommonLib/x86/AdaptiveLoopFilterX86.h b/source/Lib/CommonLib/x86/AdaptiveLoopFilterX86.h index c628bfadb4be337012de868ab3d291926e1b363b..e7ef6b7f5e75f7f6cc55e85a3c5e6837d352a9be 100644 --- a/source/Lib/CommonLib/x86/AdaptiveLoopFilterX86.h +++ b/source/Lib/CommonLib/x86/AdaptiveLoopFilterX86.h @@ -41,10 +41,10 @@ #include <x86intrin.h> #endif #if RExt__HIGH_BIT_DEPTH_SUPPORT -static void simdFilter5x5Blk_HBD(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, - int vbPos) +static void simdFilter5x5Blk_HBD(AlfClassifier** classifier, const PelUnitBuf& recDst, const CPelUnitBuf& recSrc, + const Area& blkDst, const Area& blk, const ComponentID compId, + const AlfCoeff* filterSet, const Pel* fClipSet, const ClpRng& clpRng, + CodingStructure& cs, const int vbCTUHeight, int vbPos) { CHECK((vbCTUHeight & (vbCTUHeight - 1)) != 0, "vbCTUHeight must be a power of 2"); @@ -412,10 +412,10 @@ static void simdDeriveClassificationBlk_HBD(AlfClassifier **classifier, int **la } #ifdef USE_AVX2 -static void simdFilter5x5Blk_HBD_AVX2(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, - int vbPos) +static void simdFilter5x5Blk_HBD_AVX2(AlfClassifier** classifier, const PelUnitBuf& recDst, const CPelUnitBuf& recSrc, + const Area& blkDst, const Area& blk, const ComponentID compId, + const AlfCoeff* filterSet, const Pel* fClipSet, const ClpRng& clpRng, + CodingStructure& cs, const int vbCTUHeight, int vbPos) { CHECK((vbCTUHeight & (vbCTUHeight - 1)) != 0, "vbCTUHeight must be a power of 2"); @@ -816,11 +816,11 @@ static void simdDeriveClassificationBlk(AlfClassifier **classifier, int **laplac } } -template<X86_VEXT vext> -static void simdFilter5x5Blk(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, - int vbPos) +template<X86_VEXT vext> static void simdFilter5x5Blk(AlfClassifier** classifier, const PelUnitBuf& recDst, + const CPelUnitBuf& recSrc, const Area& blkDst, const Area& blk, + const ComponentID compId, const AlfCoeff* filterSet, + const Pel* fClipSet, const ClpRng& clpRng, CodingStructure& cs, + const int vbCTUHeight, int vbPos) { CHECK((vbCTUHeight & (vbCTUHeight - 1)) != 0, "vbCTUHeight must be a power of 2"); @@ -1024,10 +1024,10 @@ static const uint32_t shuffleTab32[4][3][4] = { }, }; -static void simdFilter7x7Blk_HBD(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, - int vbPos) +static void simdFilter7x7Blk_HBD(AlfClassifier** classifier, const PelUnitBuf& recDst, const CPelUnitBuf& recSrc, + const Area& blkDst, const Area& blk, const ComponentID compId, + const AlfCoeff* filterSet, const Pel* fClipSet, const ClpRng& clpRng, + CodingStructure& cs, const int vbCTUHeight, int vbPos) { CHECK((vbCTUHeight & (vbCTUHeight - 1)) != 0, "vbCTUHeight must be a power of 2"); CHECK(isChroma(compId), "7x7 ALF filter is meant for luma only"); @@ -1250,10 +1250,10 @@ static void simdFilter7x7Blk_HBD(AlfClassifier **classifier, const PelUnitBuf &r } #ifdef USE_AVX2 -static void simdFilter7x7Blk_HBD_AVX2(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, - int vbPos) +static void simdFilter7x7Blk_HBD_AVX2(AlfClassifier** classifier, const PelUnitBuf& recDst, const CPelUnitBuf& recSrc, + const Area& blkDst, const Area& blk, const ComponentID compId, + const AlfCoeff* filterSet, const Pel* fClipSet, const ClpRng& clpRng, + CodingStructure& cs, const int vbCTUHeight, int vbPos) { CHECK((vbCTUHeight & (vbCTUHeight - 1)) != 0, "vbCTUHeight must be a power of 2"); CHECK(isChroma(compId), "7x7 ALF filter is meant for luma only"); @@ -1477,11 +1477,11 @@ static void simdFilter7x7Blk_HBD_AVX2(AlfClassifier **classifier, const PelUnitB } #endif #else -template<X86_VEXT vext> -static void simdFilter7x7Blk(AlfClassifier **classifier, const PelUnitBuf &recDst, const CPelUnitBuf &recSrc, - const Area &blkDst, const Area &blk, const ComponentID compId, const short *filterSet, - const Pel *fClipSet, const ClpRng &clpRng, CodingStructure &cs, const int vbCTUHeight, - int vbPos) +template<X86_VEXT vext> static void simdFilter7x7Blk(AlfClassifier** classifier, const PelUnitBuf& recDst, + const CPelUnitBuf& recSrc, const Area& blkDst, const Area& blk, + const ComponentID compId, const AlfCoeff* filterSet, + const Pel* fClipSet, const ClpRng& clpRng, CodingStructure& cs, + const int vbCTUHeight, int vbPos) { CHECK((vbCTUHeight & (vbCTUHeight - 1)) != 0, "vbCTUHeight must be a power of 2"); CHECK(isChroma(compId), "7x7 ALF filter is meant for luma only"); diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp index 64ef8e0038a4c675fcc8109775383173552e3072..3fa76b4bcf00f262e1887302c1f79810c1073f69 100644 --- a/source/Lib/DecoderLib/VLCReader.cpp +++ b/source/Lib/DecoderLib/VLCReader.cpp @@ -1032,7 +1032,7 @@ void HLSyntaxReader::parseAlfAps( APS* aps ) ccAlfParam.ccAlfFilterIdxEnabled[ccIdx][filterIdx] = true; AlfFilterShape alfShape(size_CC_ALF); - short *coeff = ccAlfParam.ccAlfCoeff[ccIdx][filterIdx]; + AlfCoeff* coeff = ccAlfParam.ccAlfCoeff[ccIdx][filterIdx]; // Filter coefficients for (int i = 0; i < alfShape.numCoeff - 1; i++) { @@ -5193,8 +5193,8 @@ void HLSyntaxReader::alfFilter( AlfParam& alfParam, const bool isChroma, const i // derive maxGolombIdx AlfFilterShape alfShape( isChroma ? 5 : 7 ); const int numFilters = isChroma ? 1 : alfParam.numLumaFilters; - short* coeff = isChroma ? alfParam.chromaCoeff[altIdx] : alfParam.lumaCoeff; - Pel* clipp = isChroma ? alfParam.chromaClipp[altIdx] : alfParam.lumaClipp; + AlfCoeff* coeff = isChroma ? alfParam.chromaCoeff[altIdx] : alfParam.lumaCoeff; + AlfClipIdx* clipp = isChroma ? alfParam.chromaClipp[altIdx] : alfParam.lumaClipp; // Filter coefficients for( int ind = 0; ind < numFilters; ++ind ) diff --git a/source/Lib/EncoderLib/EncAdaptiveLoopFilter.cpp b/source/Lib/EncoderLib/EncAdaptiveLoopFilter.cpp index 0461ff0afb53bd9010cd9ff308d01f5547e9d2ea..941c1a176a11dce28bc7a72029d379c690cf873d 100644 --- a/source/Lib/EncoderLib/EncAdaptiveLoopFilter.cpp +++ b/source/Lib/EncoderLib/EncAdaptiveLoopFilter.cpp @@ -53,7 +53,7 @@ struct FilterIdxCount bool compareCounts(FilterIdxCount a, FilterIdxCount b) { return a.count > b.count; } #endif -void AlfCovariance::getClipMax(const AlfFilterShape& alfShape, int *clip_max) const +void AlfCovariance::getClipMax(const AlfFilterShape& alfShape, AlfClipIdx* clip_max) const { for( int k = 0; k < numCoeff-1; ++k ) { @@ -78,7 +78,7 @@ void AlfCovariance::getClipMax(const AlfFilterShape& alfShape, int *clip_max) co clip_max[numCoeff-1] = 0; } -void AlfCovariance::reduceClipCost(const AlfFilterShape& alfShape, int *clip) const +void AlfCovariance::reduceClipCost(const AlfFilterShape& alfShape, AlfClipIdx* clip) const { for( int k = 0; k < numCoeff-1; ++k ) { @@ -100,10 +100,11 @@ void AlfCovariance::reduceClipCost(const AlfFilterShape& alfShape, int *clip) co } } -double AlfCovariance::optimizeFilter(const AlfFilterShape &alfShape, int *clip, double *f, bool optimizeClip) const +double AlfCovariance::optimizeFilter(const AlfFilterShape& alfShape, AlfClipIdx* clip, double* f, + bool optimizeClip) const { const int size = alfShape.numCoeff; - int clip_max[MAX_NUM_ALF_LUMA_COEFF]; + AlfClipIdx clip_max[MAX_NUM_ALF_LUMA_COEFF]; double err_best, err_last; @@ -117,7 +118,7 @@ double AlfCovariance::optimizeFilter(const AlfFilterShape &alfShape, int *clip, for (int k=0; k<size; ++k) { clip[k] = std::max(clip_max[k], clip[k]); - clip[k] = std::min(clip[k], numBins-1); + clip[k] = std::min(clip[k], AlfClipIdx(numBins - 1)); } } @@ -238,7 +239,7 @@ double AlfCovariance::optimizeFilter(const AlfFilterShape &alfShape, int *clip, return err_best; } -double AlfCovariance::calcErrorForCoeffs(const int *clip, const int *coeff, const int numCoeff, +double AlfCovariance::calcErrorForCoeffs(const AlfClipIdx* clip, const AlfCoeff* coeff, const int numCoeff, const int fractionalBits) const { const double factor = 1 << fractionalBits; @@ -278,7 +279,7 @@ double AlfCovariance::calcErrorForCcAlfCoeffs(const int16_t *coeff, const int nu return error / factor; } -double AlfCovariance::calculateError( const int *clip, const double *coeff, const int numCoeff ) const +double AlfCovariance::calculateError(const AlfClipIdx* clip, const double* coeff, const int numCoeff) const { double sum = 0; for( int i = 0; i < numCoeff; i++ ) @@ -289,7 +290,7 @@ double AlfCovariance::calculateError( const int *clip, const double *coeff, cons return pixAcc - sum; } -double AlfCovariance::calculateError( const int *clip ) const +double AlfCovariance::calculateError(const AlfClipIdx* clip) const { Ty c; @@ -380,7 +381,7 @@ void AlfCovariance::gnsBacksubstitution( TE R, double* z, int size, double* A ) } } -int AlfCovariance::gnsSolveByChol( const int *clip, double *x, int numEq ) const +int AlfCovariance::gnsSolveByChol(const AlfClipIdx* clip, double* x, int numEq) const { TE LHS; Ty rhs; @@ -449,7 +450,6 @@ EncAdaptiveLoopFilter::EncAdaptiveLoopFilter() m_alfCovarianceFrame.fill(nullptr); m_filterCoeffSet = nullptr; m_filterClippSet = nullptr; - m_diffFilterCoeff = nullptr; m_alfCovarianceCcAlf[0] = nullptr; m_alfCovarianceCcAlf[1] = nullptr; @@ -510,15 +510,13 @@ void EncAdaptiveLoopFilter::create(const EncCfg* encCfg, const int picWidth, con } } - m_filterCoeffSet = new int*[std::max(MAX_NUM_ALF_CLASSES, ALF_MAX_NUM_ALTERNATIVES_CHROMA)]; - m_filterClippSet = new int*[std::max(MAX_NUM_ALF_CLASSES, ALF_MAX_NUM_ALTERNATIVES_CHROMA)]; - m_diffFilterCoeff = new int*[MAX_NUM_ALF_CLASSES]; + m_filterCoeffSet = new AlfCoeff*[std::max(MAX_NUM_ALF_CLASSES, ALF_MAX_NUM_ALTERNATIVES_CHROMA)]; + m_filterClippSet = new AlfClipIdx*[std::max(MAX_NUM_ALF_CLASSES, ALF_MAX_NUM_ALTERNATIVES_CHROMA)]; for( int i = 0; i < MAX_NUM_ALF_CLASSES; i++ ) { - m_filterCoeffSet[i] = new int[MAX_NUM_ALF_LUMA_COEFF]; - m_filterClippSet[i] = new int[MAX_NUM_ALF_LUMA_COEFF]; - m_diffFilterCoeff[i] = new int[MAX_NUM_ALF_LUMA_COEFF]; + m_filterCoeffSet[i] = new AlfCoeff[MAX_NUM_ALF_LUMA_COEFF]; + m_filterClippSet[i] = new AlfClipIdx[MAX_NUM_ALF_LUMA_COEFF]; } @@ -657,18 +655,6 @@ void EncAdaptiveLoopFilter::destroy() m_filterClippSet = nullptr; } - if( m_diffFilterCoeff ) - { - for( int i = 0; i < MAX_NUM_ALF_CLASSES; i++ ) - { - delete[] m_diffFilterCoeff[i]; - m_diffFilterCoeff[i] = nullptr; - } - delete[] m_diffFilterCoeff; - m_diffFilterCoeff = nullptr; - } - - delete[] m_ctbDistortionFixedFilter; m_ctbDistortionFixedFilter = nullptr; for (int comp = 0; comp < MAX_NUM_COMPONENT; comp++) @@ -1516,8 +1502,8 @@ double EncAdaptiveLoopFilter::getFilteredDistortion( AlfCovariance* cov, const i } double EncAdaptiveLoopFilter::mergeFiltersAndCost( - AlfParam &alfParam, AlfFilterShape &alfShape, AlfCovariance *covFrame, AlfCovariance *covMerged, - int clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], int &coeffBitsFinal) + AlfParam& alfParam, AlfFilterShape& alfShape, AlfCovariance* covFrame, AlfCovariance* covMerged, + AlfClipIdx clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], int& coeffBitsFinal) { int numFiltersBest = 0; int numFilters = MAX_NUM_ALF_CLASSES; @@ -1534,7 +1520,7 @@ double EncAdaptiveLoopFilter::mergeFiltersAndCost( dist = deriveFilterCoeffs(covFrame, covMerged, clipMerged, alfShape, m_filterIndices[numFilters - 1], numFilters, errorForce0CoeffTab, alfParam); // filter coeffs are stored in m_filterCoeffSet distForce0 = getDistForce0( alfShape, numFilters, errorForce0CoeffTab, codedVarBins ); - coeffBits = deriveFilterCoefficientsPredictionMode( alfShape, m_filterCoeffSet, m_diffFilterCoeff, numFilters ); + coeffBits = deriveFilterCoefficientsPredictionMode(alfShape, m_filterCoeffSet, numFilters); coeffBitsForce0 = getCostFilterCoeffForce0( alfShape, m_filterCoeffSet, numFilters, codedVarBins ); cost = dist + m_lambda[COMPONENT_Y] * coeffBits; @@ -1554,7 +1540,7 @@ double EncAdaptiveLoopFilter::mergeFiltersAndCost( } dist = deriveFilterCoeffs( covFrame, covMerged, clipMerged, alfShape, m_filterIndices[numFiltersBest - 1], numFiltersBest, errorForce0CoeffTab, alfParam ); - coeffBits = deriveFilterCoefficientsPredictionMode( alfShape, m_filterCoeffSet, m_diffFilterCoeff, numFiltersBest ); + coeffBits = deriveFilterCoefficientsPredictionMode(alfShape, m_filterCoeffSet, numFiltersBest); distForce0 = getDistForce0( alfShape, numFiltersBest, errorForce0CoeffTab, codedVarBins ); coeffBitsForce0 = getCostFilterCoeffForce0( alfShape, m_filterCoeffSet, numFiltersBest, codedVarBins ); @@ -1582,8 +1568,8 @@ double EncAdaptiveLoopFilter::mergeFiltersAndCost( { if( codedVarBins[varInd] == 0 ) { - memset( m_filterCoeffSet[varInd], 0, sizeof( int )*MAX_NUM_ALF_LUMA_COEFF ); - memset( m_filterClippSet[varInd], 0, sizeof( int )*MAX_NUM_ALF_LUMA_COEFF ); + std::fill_n(m_filterCoeffSet[varInd], MAX_NUM_ALF_LUMA_COEFF, 0); + std::fill_n(m_filterClippSet[varInd], MAX_NUM_ALF_LUMA_COEFF, 0); } } } @@ -1597,7 +1583,7 @@ double EncAdaptiveLoopFilter::mergeFiltersAndCost( } } - memcpy( alfParam.filterCoeffDeltaIdx, m_filterIndices[numFiltersBest - 1], sizeof( short ) * MAX_NUM_ALF_CLASSES ); + memcpy(alfParam.filterCoeffDeltaIdx, m_filterIndices[numFiltersBest - 1], sizeof(AlfBankIdx) * MAX_NUM_ALF_CLASSES); coeffBitsFinal += getNonFilterCoeffRate(alfParam); return distReturn; } @@ -1619,8 +1605,8 @@ int EncAdaptiveLoopFilter::getNonFilterCoeffRate( AlfParam& alfParam ) return len; } - -int EncAdaptiveLoopFilter::getCostFilterCoeffForce0( AlfFilterShape& alfShape, int **pDiffQFilterCoeffIntPP, const int numFilters, bool* codedVarBins ) +int EncAdaptiveLoopFilter::getCostFilterCoeffForce0(AlfFilterShape& alfShape, AlfCoeff** pDiffQFilterCoeffIntPP, + const int numFilters, bool* codedVarBins) { int len = 0; // Filter coefficients @@ -1661,18 +1647,21 @@ int EncAdaptiveLoopFilter::getCostFilterCoeffForce0( AlfFilterShape& alfShape, i return len; } -int EncAdaptiveLoopFilter::deriveFilterCoefficientsPredictionMode( AlfFilterShape& alfShape, int **filterSet, int** filterCoeffDiff, const int numFilters ) +int EncAdaptiveLoopFilter::deriveFilterCoefficientsPredictionMode(AlfFilterShape& alfShape, AlfCoeff** filterSet, + const int numFilters) { return (m_alfParamTemp.nonLinearFlag[ChannelType::LUMA] ? getCostFilterClipp(alfShape, filterSet, numFilters) : 0) + getCostFilterCoeff(alfShape, filterSet, numFilters); } -int EncAdaptiveLoopFilter::getCostFilterCoeff( AlfFilterShape& alfShape, int **pDiffQFilterCoeffIntPP, const int numFilters ) +int EncAdaptiveLoopFilter::getCostFilterCoeff(AlfFilterShape& alfShape, AlfCoeff** pDiffQFilterCoeffIntPP, + const int numFilters) { return lengthFilterCoeffs( alfShape, numFilters, pDiffQFilterCoeffIntPP ); // alf_coeff_luma_delta[i][j]; } -int EncAdaptiveLoopFilter::getCostFilterClipp( AlfFilterShape& alfShape, int **pDiffQFilterCoeffIntPP, const int numFilters ) +int EncAdaptiveLoopFilter::getCostFilterClipp(AlfFilterShape& alfShape, AlfCoeff** pDiffQFilterCoeffIntPP, + const int numFilters) { for (int filterIdx = 0; filterIdx < numFilters; ++filterIdx) { @@ -1687,7 +1676,7 @@ int EncAdaptiveLoopFilter::getCostFilterClipp( AlfFilterShape& alfShape, int **p return (numFilters * (alfShape.numCoeff - 1)) << 1; } -int EncAdaptiveLoopFilter::lengthFilterCoeffs( AlfFilterShape& alfShape, const int numFilters, int **FilterCoeff ) +int EncAdaptiveLoopFilter::lengthFilterCoeffs(AlfFilterShape& alfShape, const int numFilters, AlfCoeff** FilterCoeff) { int bitCnt = 0; @@ -1779,7 +1768,10 @@ int EncAdaptiveLoopFilter::lengthUvlc(int code) return (length >> 1) + ((length + 1) >> 1); } -double EncAdaptiveLoopFilter::deriveFilterCoeffs( AlfCovariance* cov, AlfCovariance* covMerged, int clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], AlfFilterShape& alfShape, short* filterIndices, int numFilters, double errorTabForce0Coeff[MAX_NUM_ALF_CLASSES][2], AlfParam& alfParam ) +double EncAdaptiveLoopFilter::deriveFilterCoeffs( + AlfCovariance* cov, AlfCovariance* covMerged, + AlfClipIdx clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], AlfFilterShape& alfShape, + AlfBankIdx* filterIndices, int numFilters, double errorTabForce0Coeff[MAX_NUM_ALF_CLASSES][2], AlfParam& alfParam) { double error = 0.0; AlfCovariance& tmpCov = covMerged[MAX_NUM_ALF_CLASSES]; @@ -1796,7 +1788,7 @@ double EncAdaptiveLoopFilter::deriveFilterCoeffs( AlfCovariance* cov, AlfCovaria if( !found_clip ) { found_clip = true; // clip should be at the adress of shortest one - memcpy(m_filterClippSet[filtIdx], clipMerged[numFilters-1][classIdx], sizeof(int[MAX_NUM_ALF_LUMA_COEFF])); + std::copy_n(clipMerged[numFilters - 1][classIdx], MAX_NUM_ALF_LUMA_COEFF, m_filterClippSet[filtIdx]); } } } @@ -1812,13 +1804,13 @@ double EncAdaptiveLoopFilter::deriveFilterCoeffs( AlfCovariance* cov, AlfCovaria return error; } -double EncAdaptiveLoopFilter::deriveCoeffQuant(int *filterClipp, int *filterCoeffQuant, const AlfCovariance &cov, - const AlfFilterShape &shape, const int fractionalBits, - const bool optimizeClip) +double EncAdaptiveLoopFilter::deriveCoeffQuant(AlfClipIdx* filterClipp, AlfCoeff* filterCoeffQuant, + const AlfCovariance& cov, const AlfFilterShape& shape, + const int fractionalBits, const bool optimizeClip) { - const int factor = 1 << fractionalBits; - const int maxValue = factor - 1; - const int minValue = -factor + 1; + const AlfCoeff factor = 1 << fractionalBits; + const AlfCoeff maxValue = factor - 1; + const AlfCoeff minValue = -factor + 1; const int numCoeff = shape.numCoeff; double filterCoeff[MAX_NUM_ALF_LUMA_COEFF]; @@ -1876,19 +1868,21 @@ double EncAdaptiveLoopFilter::deriveCoeffQuant(int *filterClipp, int *filterCoef return errRef; } -void EncAdaptiveLoopFilter::roundFiltCoeff( int *filterCoeffQuant, double *filterCoeff, const int numCoeff, const int factor ) +void EncAdaptiveLoopFilter::roundFiltCoeff(AlfCoeff* filterCoeffQuant, double* filterCoeff, const int numCoeff, + const AlfCoeff factor) { bool isLumaFilter = numCoeff > 7 ? 1 : 0; double alfStrength = isLumaFilter ? m_encCfg->getALFStrengthLuma() : m_encCfg->getALFStrengthChroma(); for( int i = 0; i < numCoeff; i++ ) { const int sign = sgn2(filterCoeff[i]); - filterCoeffQuant[i] = int((filterCoeff[i] * alfStrength) * sign * factor + 0.5) * sign; + const double absVal = filterCoeff[i] * alfStrength * sign; + filterCoeffQuant[i] = int(std::min(absVal, 2.0) * factor + 0.5) * sign; } } -void EncAdaptiveLoopFilter::roundFiltCoeffCCALF(int16_t *filterCoeffQuant, double *filterCoeff, const int numCoeff, - const int factor) +void EncAdaptiveLoopFilter::roundFiltCoeffCCALF(AlfCoeff* filterCoeffQuant, double* filterCoeff, const int numCoeff, + const AlfCoeff factor) { for (int i = 0; i < numCoeff; i++) { @@ -1915,10 +1909,13 @@ void EncAdaptiveLoopFilter::roundFiltCoeffCCALF(int16_t *filterCoeffQuant, doubl } } -void EncAdaptiveLoopFilter::mergeClasses( const AlfFilterShape& alfShape, AlfCovariance* cov, AlfCovariance* covMerged, int clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], const int numClasses, short filterIndices[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES] ) +void EncAdaptiveLoopFilter::mergeClasses( + const AlfFilterShape& alfShape, AlfCovariance* cov, AlfCovariance* covMerged, + AlfClipIdx clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], const int numClasses, + AlfBankIdx filterIndices[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES]) { - int tmpClip[MAX_NUM_ALF_LUMA_COEFF]; - int bestMergeClip[MAX_NUM_ALF_LUMA_COEFF]; + AlfClipIdx tmpClip[MAX_NUM_ALF_LUMA_COEFF]; + AlfClipIdx bestMergeClip[MAX_NUM_ALF_LUMA_COEFF]; double err[MAX_NUM_ALF_CLASSES]; double bestMergeErr = std::numeric_limits<double>::max(); bool availableClass[MAX_NUM_ALF_CLASSES]; @@ -1926,7 +1923,7 @@ void EncAdaptiveLoopFilter::mergeClasses( const AlfFilterShape& alfShape, AlfCov uint8_t indexListTemp[MAX_NUM_ALF_CLASSES]; int numRemaining = numClasses; - memset( filterIndices, 0, sizeof( short ) * MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_CLASSES ); + std::fill_n(filterIndices[0], MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_CLASSES, 0); for( int i = 0; i < numClasses; i++ ) { @@ -1946,15 +1943,16 @@ void EncAdaptiveLoopFilter::mergeClasses( const AlfFilterShape& alfShape, AlfCov // init Clip for( int i = 0; i < numClasses; i++ ) { - std::fill_n(clipMerged[numRemaining - 1][i], MAX_NUM_ALF_LUMA_COEFF, + AlfClipIdx* clipIdxs = clipMerged[numRemaining - 1][i]; + std::fill_n(clipIdxs, MAX_NUM_ALF_LUMA_COEFF, m_alfParamTemp.nonLinearFlag[ChannelType::LUMA] ? ALF_NUM_CLIP_VALS[ChannelType::LUMA] / 2 : 0); if (m_alfParamTemp.nonLinearFlag[ChannelType::LUMA]) { - err[i] = covMerged[i].optimizeFilterClip( alfShape, clipMerged[numRemaining-1][i] ); + err[i] = covMerged[i].optimizeFilterClip(alfShape, clipIdxs); } else { - err[i] = covMerged[i].calculateError( clipMerged[numRemaining-1][i] ); + err[i] = covMerged[i].calculateError(clipIdxs); } } @@ -1998,8 +1996,9 @@ void EncAdaptiveLoopFilter::mergeClasses( const AlfFilterShape& alfShape, AlfCov } covMerged[bestToMergeIdx1] += covMerged[bestToMergeIdx2]; - memcpy(clipMerged[numRemaining-2], clipMerged[numRemaining-1], sizeof(int[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF])); - memcpy(clipMerged[numRemaining-2][bestToMergeIdx1], bestMergeClip, sizeof(bestMergeClip)); + std::copy_n(clipMerged[numRemaining - 1][0], MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF, + clipMerged[numRemaining - 2][0]); + std::copy_n(bestMergeClip, MAX_NUM_ALF_LUMA_COEFF, clipMerged[numRemaining - 2][bestToMergeIdx1]); err[bestToMergeIdx1] = bestMergeErr; availableClass[bestToMergeIdx2] = false; @@ -2704,8 +2703,8 @@ void EncAdaptiveLoopFilter::alfEncoderCtb(CodingStructure& cs, AlfParam& alfPar dDistOrgNewFilter += m_ctbDistortionUnfilter[COMPONENT_Y][ctbIdx]; for (int classIdx = 0; classIdx < MAX_NUM_ALF_CLASSES; classIdx++) { - short* pCoeff = m_coeffFinal; - Pel* pClipp = m_clippFinal; + AlfCoeff* pCoeff = m_coeffFinal; + AlfClipIdx* pClipp = m_clippFinal; for (int i = 0; i < MAX_NUM_ALF_LUMA_COEFF; i++) { m_filterTmp[i] = pCoeff[classIdx * MAX_NUM_ALF_LUMA_COEFF + i]; @@ -2799,8 +2798,8 @@ void EncAdaptiveLoopFilter::alfEncoderCtb(CodingStructure& cs, AlfParam& alfPar } else { - short *pCoeff; - Pel *pClipp; + AlfCoeff* pCoeff; + AlfClipIdx* pClipp; if (useNewFilter && filterSetIdx == ALF_NUM_FIXED_FILTER_SETS) { pCoeff = m_coeffFinal; @@ -3203,7 +3202,7 @@ void EncAdaptiveLoopFilter::alfReconstructor(CodingStructure& cs, const PelUnitB const Area blkSrc(0, 0, w, h); const Area blkDst(xStart, yStart, w, h); const AlfMode m = m_modes[COMPONENT_Y][ctuIdx]; - const short* coeff = getCoeffVals(m); + const AlfCoeff* coeff = getCoeffVals(m); const Pel* clip = getClipVals(m); #if GREEN_METADATA_SEI_ENABLED cs.m_featureCounter.alfLumaType7+= (width * height / 16) ; @@ -3224,7 +3223,7 @@ void EncAdaptiveLoopFilter::alfReconstructor(CodingStructure& cs, const PelUnitB const Area blkDst(xStart >> chromaScaleX, yStart >> chromaScaleY, w >> chromaScaleX, h >> chromaScaleY); const int altNum = m_modes[compID][ctuIdx] - AlfMode::CHROMA0; m_filter5x5Blk(m_classifier, recBuf, buf, blkDst, blkSrc, compID, m_chromaCoeffFinal[altNum], - m_chromaClippFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, + m_chromaClipValsFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, m_alfVBChmaPos); #if GREEN_METADATA_SEI_ENABLED cs.m_featureCounter.alfChromaType5+= ((width >> chromaScaleX) * (height >> chromaScaleY) / 16); @@ -3246,7 +3245,7 @@ void EncAdaptiveLoopFilter::alfReconstructor(CodingStructure& cs, const PelUnitB { Area blk(xPos, yPos, width, height); const AlfMode m = m_modes[COMPONENT_Y][ctuIdx]; - const short* coeff = getCoeffVals(m); + const AlfCoeff* coeff = getCoeffVals(m); const Pel* clip = getClipVals(m); m_filter7x7Blk(m_classifier, recBuf, recExtBuf, blk, blk, COMPONENT_Y, coeff, clip, m_clpRngs.comp[COMPONENT_Y], cs, m_alfVBLumaCTUHeight, m_alfVBLumaPos); @@ -3266,7 +3265,7 @@ void EncAdaptiveLoopFilter::alfReconstructor(CodingStructure& cs, const PelUnitB Area blk(xPos >> chromaScaleX, yPos >> chromaScaleY, width >> chromaScaleX, height >> chromaScaleY); const int altNum = m_modes[compID][ctuIdx] - AlfMode::CHROMA0; m_filter5x5Blk(m_classifier, recBuf, recExtBuf, blk, blk, compID, m_chromaCoeffFinal[altNum], - m_chromaClippFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, + m_chromaClipValsFinal[altNum], m_clpRngs.comp[compIdx], cs, m_alfVBChmaCTUHeight, m_alfVBChmaPos); #if GREEN_METADATA_SEI_ENABLED cs.m_featureCounter.alfChromaType5+= ((width >> chromaScaleX) * (height >> chromaScaleY) / 16) ; @@ -3299,7 +3298,9 @@ int EncAdaptiveLoopFilter::getMaxNumAlternativesChroma( ) return std::min<int>( m_numCTUsInPic * 2, m_encCfg->getMaxNumAlfAlternativesChroma() ); } -int EncAdaptiveLoopFilter::getCoeffRateCcAlf(short chromaCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], bool filterEnabled[MAX_NUM_CC_ALF_FILTERS], uint8_t filterCount, ComponentID compID) +int EncAdaptiveLoopFilter::getCoeffRateCcAlf(AlfCoeff chromaCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], + bool filterEnabled[MAX_NUM_CC_ALF_FILTERS], uint8_t filterCount, + ComponentID compID) { int bits = 0; @@ -3327,7 +3328,9 @@ int EncAdaptiveLoopFilter::getCoeffRateCcAlf(short chromaCoeff[MAX_NUM_CC_ALF_FI return bits; } -void EncAdaptiveLoopFilter::deriveCcAlfFilterCoeff( ComponentID compID, const PelUnitBuf& recYuv, const PelUnitBuf& recYuvExt, short filterCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], const uint8_t filterIdx ) +void EncAdaptiveLoopFilter::deriveCcAlfFilterCoeff( + ComponentID compID, const PelUnitBuf& recYuv, const PelUnitBuf& recYuvExt, + AlfCoeff filterCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], const uint8_t filterIdx) { int forward_tab[CCALF_CANDS_COEFF_NR * 2 - 1] = {0}; for (int i = 0; i < CCALF_CANDS_COEFF_NR; i++) @@ -3711,7 +3714,7 @@ void EncAdaptiveLoopFilter::deriveCcAlfFilter( CodingStructure& cs, ComponentID double bestUnfilteredTotalCost = 1 * m_lambda[compID] + unfilteredDistortion; // 1 bit is for gating flag bool ccAlfFilterIdxEnabled[MAX_NUM_CC_ALF_FILTERS]; - short ccAlfFilterCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; + AlfCoeff ccAlfFilterCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; uint8_t ccAlfFilterCount = MAX_NUM_CC_ALF_FILTERS; double bestFilteredTotalCost = MAX_DOUBLE; bool bestreuseTemporalFilterCoeff = false; @@ -3895,7 +3898,7 @@ void EncAdaptiveLoopFilter::deriveCcAlfFilter( CodingStructure& cs, ComponentID // update the filter control indicators if (bestreuseTemporalFilterCoeff!=1) { - short storedBestFilterCoeffSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; + AlfCoeff storedBestFilterCoeffSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; for (int filterIdx=0; filterIdx<MAX_NUM_CC_ALF_FILTERS; filterIdx++) { memcpy(storedBestFilterCoeffSet[filterIdx], m_bestFilterCoeffSet[filterIdx], sizeof(m_bestFilterCoeffSet[filterIdx])); diff --git a/source/Lib/EncoderLib/EncAdaptiveLoopFilter.h b/source/Lib/EncoderLib/EncAdaptiveLoopFilter.h index 009502f3e0a1492738f6efb1a17f2a44bdad823f..11a28ab1db9a728e97473e658ce3016da0b26e0a 100644 --- a/source/Lib/EncoderLib/EncAdaptiveLoopFilter.h +++ b/source/Lib/EncoderLib/EncAdaptiveLoopFilter.h @@ -176,7 +176,7 @@ struct AlfCovariance return *this; } - void setEyFromClip(const int* clip, TE _E, Ty _y, int size) const + void setEyFromClip(const AlfClipIdx* clip, TE _E, Ty _y, int size) const { for (int k=0; k<size; k++) { @@ -188,34 +188,38 @@ struct AlfCovariance } } - double optimizeFilter(const int* clip, double *f, int size) const + double optimizeFilter(const AlfClipIdx* clip, double* f, int size) const { gnsSolveByChol( clip, f, size ); return calculateError( clip, f ); } - double optimizeFilter(const AlfFilterShape &alfShape, int *clip, double *f, bool optimizeClip) const; - double optimizeFilterClip(const AlfFilterShape& alfShape, int* clip) const + double optimizeFilter(const AlfFilterShape& alfShape, AlfClipIdx* clip, double* f, bool optimizeClip) const; + double optimizeFilterClip(const AlfFilterShape& alfShape, AlfClipIdx* clip) const { Ty f; return optimizeFilter(alfShape, clip, f, true); } - double calculateError( const int *clip ) const; - double calculateError( const int *clip, const double *coeff ) const { return calculateError(clip, coeff, numCoeff); } - double calculateError( const int *clip, const double *coeff, const int numCoeff ) const; - double calcErrorForCoeffs(const int *clip, const int *coeff, const int numCoeff, const int fractionalBits) const; + double calculateError(const AlfClipIdx* clip) const; + double calculateError(const AlfClipIdx* clip, const double* coeff) const + { + return calculateError(clip, coeff, numCoeff); + } + double calculateError(const AlfClipIdx* clip, const double* coeff, const int numCoeff) const; + double calcErrorForCoeffs(const AlfClipIdx* clip, const AlfCoeff* coeff, const int numCoeff, + const int fractionalBits) const; double calcErrorForCcAlfCoeffs(const int16_t *coeff, const int numCoeff, const int bitDepth) const; - void getClipMax(const AlfFilterShape& alfShape, int *clip_max) const; - void reduceClipCost(const AlfFilterShape& alfShape, int *clip) const; + void getClipMax(const AlfFilterShape& alfShape, AlfClipIdx* clip_max) const; + void reduceClipCost(const AlfFilterShape& alfShape, AlfClipIdx* clip) const; int gnsSolveByChol( TE LHS, double* rhs, double *x, int numEq ) const; private: // Cholesky decomposition - int gnsSolveByChol( const int *clip, double *x, int numEq ) const; + int gnsSolveByChol(const AlfClipIdx* clip, double* x, int numEq) const; void gnsBacksubstitution( TE R, double* z, int size, double* A ) const; void gnsTransposeBacksubstitution( TE U, double* rhs, double* x, int order ) const; int gnsCholeskyDec( TE inpMatr, TE outMatr, int numEq ) const; @@ -241,15 +245,14 @@ private: AlfParam m_alfParamTemp; ParameterSetMap<APS>* m_apsMap; AlfCovariance m_alfCovarianceMerged[ALF_NUM_OF_FILTER_TYPES][MAX_NUM_ALF_CLASSES + 2]; - int m_alfClipMerged[ALF_NUM_OF_FILTER_TYPES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF]; + AlfClipIdx m_alfClipMerged[ALF_NUM_OF_FILTER_TYPES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF]; CABACWriter* m_CABACEstimator; CtxPool *m_ctxPool; double m_lambda[MAX_NUM_COMPONENT]; - int** m_filterCoeffSet; // [lumaClassIdx/chromaAltIdx][coeffIdx] - int** m_filterClippSet; // [lumaClassIdx/chromaAltIdx][coeffIdx] - int** m_diffFilterCoeff; - short m_filterIndices[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES]; + AlfCoeff** m_filterCoeffSet; // [lumaClassIdx/chromaAltIdx][coeffIdx] + AlfClipIdx** m_filterClippSet; // [lumaClassIdx/chromaAltIdx][coeffIdx] + AlfBankIdx m_filterIndices[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES]; EnumArray<unsigned, ChannelType> m_bitsNewFilter; @@ -259,13 +262,13 @@ private: std::vector<AlfMode> m_indexTmpVec[MAX_NUM_COMPONENT]; AlfMode *m_indexTmp[MAX_NUM_COMPONENT]; AlfParam m_alfParamTempNL; - int m_clipDefaultEnc[MAX_NUM_ALF_LUMA_COEFF]; - int m_filterTmp[MAX_NUM_ALF_LUMA_COEFF]; - int m_clipTmp[MAX_NUM_ALF_LUMA_COEFF]; + AlfClipIdx m_clipDefaultEnc[MAX_NUM_ALF_LUMA_COEFF]; + AlfCoeff m_filterTmp[MAX_NUM_ALF_LUMA_COEFF]; + AlfClipIdx m_clipTmp[MAX_NUM_ALF_LUMA_COEFF]; int m_apsIdCcAlfStart[2]; - short m_bestFilterCoeffSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; + AlfCoeff m_bestFilterCoeffSet[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF]; bool m_bestFilterIdxEnabled[MAX_NUM_CC_ALF_FILTERS]; uint8_t m_bestFilterCount; uint8_t* m_trainingCovControl; @@ -314,10 +317,10 @@ private: ); void copyAlfParam( AlfParam& alfParamDst, AlfParam& alfParamSrc, ChannelType channel ); - double mergeFiltersAndCost(AlfParam &alfParam, AlfFilterShape &alfShape, AlfCovariance *covFrame, - AlfCovariance *covMerged, - int clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], - int &coeffBitsFinal); + double mergeFiltersAndCost(AlfParam& alfParam, AlfFilterShape& alfShape, AlfCovariance* covFrame, + AlfCovariance* covMerged, + AlfClipIdx clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], + int& coeffBitsFinal); void getFrameStats(ChannelType channel, int shapeIdx); void getFrameStat(AlfCovariance *frameCov, AlfCovariance **ctbCov, AlfMode *ctbEnableFlags, const int numClasses, @@ -336,30 +339,36 @@ private: const ComponentID compID, const int yPos); void calcCovarianceCcAlf(Pel ELocal[MAX_NUM_CC_ALF_CHROMA_COEFF][1], const Pel *rec, const ptrdiff_t stride, const AlfFilterShape &shape, int vbDistance); - void mergeClasses(const AlfFilterShape& alfShape, AlfCovariance* cov, AlfCovariance* covMerged, int clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], const int numClasses, short filterIndices[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES]); + void mergeClasses(const AlfFilterShape& alfShape, AlfCovariance* cov, AlfCovariance* covMerged, + AlfClipIdx clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], + const int numClasses, AlfBankIdx filterIndices[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES]); double getFilterCoeffAndCost(CodingStructure &cs, double distUnfilter, ChannelType channel, bool bReCollectStat, int shapeIdx, int &coeffBits, bool onlyFilterCost = false); - double deriveFilterCoeffs(AlfCovariance* cov, AlfCovariance* covMerged, int clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], AlfFilterShape& alfShape, short* filterIndices, int numFilters, double errorTabForce0Coeff[MAX_NUM_ALF_CLASSES][2], AlfParam& alfParam); - int deriveFilterCoefficientsPredictionMode( AlfFilterShape& alfShape, int **filterSet, int** filterCoeffDiff, const int numFilters ); - double deriveCoeffQuant(int *filterClipp, int *filterCoeffQuant, const AlfCovariance &cov, - const AlfFilterShape &shape, const int fractionalBits, const bool optimizeClip); + double deriveFilterCoeffs(AlfCovariance* cov, AlfCovariance* covMerged, + AlfClipIdx clipMerged[MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_CLASSES][MAX_NUM_ALF_LUMA_COEFF], + AlfFilterShape& alfShape, AlfBankIdx* filterIndices, int numFilters, + double errorTabForce0Coeff[MAX_NUM_ALF_CLASSES][2], AlfParam& alfParam); + int deriveFilterCoefficientsPredictionMode(AlfFilterShape& alfShape, AlfCoeff** filterSet, const int numFilters); + double deriveCoeffQuant(AlfClipIdx* filterClipp, AlfCoeff* filterCoeffQuant, const AlfCovariance& cov, + const AlfFilterShape& shape, const int fractionalBits, const bool optimizeClip); double deriveCtbAlfEnableFlags(CodingStructure &cs, const int shapeIdx, ChannelType channel, #if ENABLE_QPA const double chromaWeight, #endif const int numClasses, const int numCoeff, double &distUnfilter); - void roundFiltCoeff( int *filterCoeffQuant, double *filterCoeff, const int numCoeff, const int factor ); - void roundFiltCoeffCCALF(int16_t *filterCoeffQuant, double *filterCoeff, const int numCoeff, int factor); + void roundFiltCoeff(AlfCoeff* filterCoeffQuant, double* filterCoeff, const int numCoeff, const AlfCoeff factor); + void roundFiltCoeffCCALF(AlfCoeff* filterCoeffQuant, double* filterCoeff, const int numCoeff, AlfCoeff factor); double getDistCoeffForce0( bool* codedVarBins, double errorForce0CoeffTab[MAX_NUM_ALF_CLASSES][2], int* bitsVarBin, int zeroBitsVarBin, const int numFilters); int lengthUvlc( int uiCode ); int getNonFilterCoeffRate( AlfParam& alfParam ); - int getCostFilterCoeffForce0( AlfFilterShape& alfShape, int **pDiffQFilterCoeffIntPP, const int numFilters, bool* codedVarBins ); - int getCostFilterCoeff( AlfFilterShape& alfShape, int **pDiffQFilterCoeffIntPP, const int numFilters ); - int getCostFilterClipp( AlfFilterShape& alfShape, int **pDiffQFilterCoeffIntPP, const int numFilters ); - int lengthFilterCoeffs( AlfFilterShape& alfShape, const int numFilters, int **FilterCoeff ); + int getCostFilterCoeffForce0(AlfFilterShape& alfShape, AlfCoeff** pDiffQFilterCoeffIntPP, const int numFilters, + bool* codedVarBins); + int getCostFilterCoeff(AlfFilterShape& alfShape, AlfCoeff** pDiffQFilterCoeffIntPP, const int numFilters); + int getCostFilterClipp(AlfFilterShape& alfShape, AlfCoeff** pDiffQFilterCoeffIntPP, const int numFilters); + int lengthFilterCoeffs(AlfFilterShape& alfShape, const int numFilters, AlfCoeff** FilterCoeff); double getDistForce0( AlfFilterShape& alfShape, const int numFilters, double errorTabForce0Coeff[MAX_NUM_ALF_CLASSES][2], bool* codedVarBins ); int getChromaCoeffRate( AlfParam& alfParam, int altIdx ); @@ -374,8 +383,11 @@ private: ChannelType channel); void initCtuAlternativeChroma(AlfMode *ctuAlts[MAX_NUM_COMPONENT]); int getMaxNumAlternativesChroma( ); - int getCoeffRateCcAlf(short chromaCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], bool filterEnabled[MAX_NUM_CC_ALF_FILTERS], uint8_t filterCount, ComponentID compID); - void deriveCcAlfFilterCoeff( ComponentID compID, const PelUnitBuf& recYuv, const PelUnitBuf& recYuvExt, short filterCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], const uint8_t filterIdx ); + int getCoeffRateCcAlf(AlfCoeff chromaCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], + bool filterEnabled[MAX_NUM_CC_ALF_FILTERS], uint8_t filterCount, ComponentID compID); + void deriveCcAlfFilterCoeff(ComponentID compID, const PelUnitBuf& recYuv, const PelUnitBuf& recYuvExt, + AlfCoeff filterCoeff[MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF], + const uint8_t filterIdx); void determineControlIdcValues(CodingStructure &cs, const ComponentID compID, const PelBuf *buf, const int ctuWidthC, const int ctuHeightC, const int picWidthC, const int picHeightC, double **unfilteredDistortion, uint64_t *trainingDistortion[MAX_NUM_CC_ALF_FILTERS], diff --git a/source/Lib/EncoderLib/VLCWriter.cpp b/source/Lib/EncoderLib/VLCWriter.cpp index b1569cc8b5c5234a79c20b620cc953ace0a63362..b6cae7598b852a5d259ef77cb0dda0812d4803c9 100644 --- a/source/Lib/EncoderLib/VLCWriter.cpp +++ b/source/Lib/EncoderLib/VLCWriter.cpp @@ -619,7 +619,7 @@ void HLSWriter::codeAlfAps( APS* pcAPS ) { AlfFilterShape alfShape(size_CC_ALF); - const short *coeff = paramCcAlf.ccAlfCoeff[ccIdx][filterIdx]; + const AlfCoeff* coeff = paramCcAlf.ccAlfCoeff[ccIdx][filterIdx]; // Filter coefficients for (int i = 0; i < alfShape.numCoeff - 1; i++) { @@ -3139,8 +3139,8 @@ bool HLSWriter::xFindMatchingLTRP(Slice* pcSlice, uint32_t *ltrpsIndex, int ltrp void HLSWriter::alfFilter( const AlfParam& alfParam, const bool isChroma, const int altIdx ) { AlfFilterShape alfShape(isChroma ? 5 : 7); - const short* coeff = isChroma ? alfParam.chromaCoeff[altIdx] : alfParam.lumaCoeff; - const Pel* clipp = isChroma ? alfParam.chromaClipp[altIdx] : alfParam.lumaClipp; + const AlfCoeff* coeff = isChroma ? alfParam.chromaCoeff[altIdx] : alfParam.lumaCoeff; + const AlfClipIdx* clipp = isChroma ? alfParam.chromaClipp[altIdx] : alfParam.lumaClipp; const int numFilters = isChroma ? 1 : alfParam.numLumaFilters; // vlc for all