Showing the changes to codec/processing/src/adaptivequantization/AdaptiveQuantization.cpp (1 of 12 files). Show all
Unified · Split
| @@ -30,16 +30,16 @@ · 29 unmodified lines | |||
| 30 | * | 30 | * |
| 31 | */ | 31 | */ |
| 32 | #include "AdaptiveQuantization.h" | 32 | #include "AdaptiveQuantization.h" |
| 33 | 33 | #include "macros.h" | |
| 34 | WELSVP_NAMESPACE_BEGIN | 34 | WELSVP_NAMESPACE_BEGIN |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | #define AVERAGE_TIME_MOTION (0.3) //0.3046875 // 1/4 + 1/16 - 1/128 ~ 0.3 | 38 | #define AVERAGE_TIME_MOTION (3000) //0.3046875 // 1/4 + 1/16 - 1/128 ~ 0.3 *AQ_TIME_INT_MULTIPLY |
| 39 | #define AVERAGE_TIME_TEXTURE_QUALITYMODE (1.0) //0.5 // 1/2 | 39 | #define AVERAGE_TIME_TEXTURE_QUALITYMODE (10000) //0.5 // 1/2 *AQ_TIME_INT_MULTIPLY |
| 40 | #define AVERAGE_TIME_TEXTURE_BITRATEMODE (0.875) //0.5 // 1/2 | 40 | #define AVERAGE_TIME_TEXTURE_BITRATEMODE (8750) //0.5 // 1/2 *AQ_TIME_INT_MULTIPLY |
| 41 | #define MODEL_ALPHA (0.9910) //1.5 //1.1102 | 41 | #define MODEL_ALPHA (9910) //1.5 //1.1102 *AQ_TIME_INT_MULTIPLY |
| 42 | #define MODEL_TIME (5.8185) //9.0 //5.9842 | 42 | #define MODEL_TIME (58185) //9.0 //5.9842 *AQ_TIME_INT_MULTIPLY |
| 43 | 43 | ||
| 44 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// | 44 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 45 | 45 | ||
| @@ -65,14 +65,14 @@ EResult CAdaptiveQuantization::Process (int32_t iType, SPixMap* pSrcPixMap, SPix · 19 unmodified lines | |||
| 65 | 65 | ||
| 66 | SMotionTextureUnit* pMotionTexture = NULL; | 66 | SMotionTextureUnit* pMotionTexture = NULL; |
| 67 | SVAACalcResult* pVaaCalcResults = NULL; | 67 | SVAACalcResult* pVaaCalcResults = NULL; |
| 68 | int8_t iMotionTextureIndexToDeltaQp = 0; | 68 | int32_t iMotionTextureIndexToDeltaQp = 0; |
| 69 | int32_t iAverMotionTextureIndexToDeltaQp = 0; // double to uint32 | 69 | int32_t iAverMotionTextureIndexToDeltaQp = 0; // double to uint32 |
| 70 | double dAverageMotionIndex = 0.0; // double to float | 70 | int64_t iAverageMotionIndex = 0; // double to float |
| 71 | double dAverageTextureIndex = 0.0; | 71 | int64_t iAverageTextureIndex = 0; |
| 72 | 72 | ||
| 73 | double dQStep = 0.0; | 73 | int64_t iQStep = 0; |
| 74 | double dLumaMotionDeltaQp = 0; | 74 | int64_t iLumaMotionDeltaQp = 0; |
| 75 | double dLumaTextureDeltaQp = 0; | 75 | int64_t iLumaTextureDeltaQp = 0; |
| 76 | 76 | ||
| 77 | uint8_t* pRefFrameY = NULL, *pCurFrameY = NULL; | 77 | uint8_t* pRefFrameY = NULL, *pCurFrameY = NULL; |
| 78 | int32_t iRefStride = 0, iCurStride = 0; | 78 | int32_t iRefStride = 0, iCurStride = 0; |
| @@ -88,8 +88,8 @@ EResult CAdaptiveQuantization::Process (int32_t iType, SPixMap* pSrcPixMap, SPix · 9 unmodified lines | |||
| 88 | 88 | ||
| 89 | /////////////////////////////////////// motion ////////////////////////////////// | 89 | /////////////////////////////////////// motion ////////////////////////////////// |
| 90 | // motion MB residual variance | 90 | // motion MB residual variance |
| 91 | dAverageMotionIndex = 0.0; | 91 | iAverageMotionIndex = 0; |
| 92 | dAverageTextureIndex = 0.0; | 92 | iAverageTextureIndex = 0; |
| 93 | pMotionTexture = m_sAdaptiveQuantParam.pMotionTextureUnit; | 93 | pMotionTexture = m_sAdaptiveQuantParam.pMotionTextureUnit; |
| 94 | pVaaCalcResults = m_sAdaptiveQuantParam.pCalcResult; | 94 | pVaaCalcResults = m_sAdaptiveQuantParam.pCalcResult; |
| 95 | 95 | ||
| @@ -115,8 +115,8 @@ EResult CAdaptiveQuantization::Process (int32_t iType, SPixMap* pSrcPixMap, SPix · 19 unmodified lines | |||
| 115 | uiSum = uiSum >> 8; | 115 | uiSum = uiSum >> 8; |
| 116 | pMotionTexture->uiTextureIndex = (iSQSum >> 8) - (uiSum * uiSum); | 116 | pMotionTexture->uiTextureIndex = (iSQSum >> 8) - (uiSum * uiSum); |
| 117 | 117 | ||
| 118 | dAverageMotionIndex += pMotionTexture->uiMotionIndex; | 118 | iAverageMotionIndex += pMotionTexture->uiMotionIndex; |
| 119 | dAverageTextureIndex += pMotionTexture->uiTextureIndex; | 119 | iAverageTextureIndex += pMotionTexture->uiTextureIndex; |
| 120 | pMotionTexture++; | 120 | pMotionTexture++; |
| 121 | ++iMbIndex; | 121 | ++iMbIndex; |
| 122 | pRefFrameTmp += MB_WIDTH_LUMA; | 122 | pRefFrameTmp += MB_WIDTH_LUMA; |
| @@ -131,8 +131,8 @@ EResult CAdaptiveQuantization::Process (int32_t iType, SPixMap* pSrcPixMap, SPix · 8 unmodified lines | |||
| 131 | pCurFrameTmp = pCurFrameY; | 131 | pCurFrameTmp = pCurFrameY; |
| 132 | for (i = 0; i < iMbWidth; i++) { | 132 | for (i = 0; i < iMbWidth; i++) { |
| 133 | m_pfVar (pRefFrameTmp, iRefStride, pCurFrameTmp, iCurStride, pMotionTexture); | 133 | m_pfVar (pRefFrameTmp, iRefStride, pCurFrameTmp, iCurStride, pMotionTexture); |
| 134 | dAverageMotionIndex += pMotionTexture->uiMotionIndex; | 134 | iAverageMotionIndex += pMotionTexture->uiMotionIndex; |
| 135 | dAverageTextureIndex += pMotionTexture->uiTextureIndex; | 135 | iAverageTextureIndex += pMotionTexture->uiTextureIndex; |
| 136 | pMotionTexture++; | 136 | pMotionTexture++; |
| 137 | pRefFrameTmp += MB_WIDTH_LUMA; | 137 | pRefFrameTmp += MB_WIDTH_LUMA; |
| 138 | pCurFrameTmp += MB_WIDTH_LUMA; | 138 | pCurFrameTmp += MB_WIDTH_LUMA; |
| @@ -142,49 +142,51 @@ EResult CAdaptiveQuantization::Process (int32_t iType, SPixMap* pSrcPixMap, SPix · 3 unmodified lines | |||
| 142 | pCurFrameY += (iCurStride) << 4; | 142 | pCurFrameY += (iCurStride) << 4; |
| 143 | } | 143 | } |
| 144 | } | 144 | } |
| 145 | dAverageMotionIndex = dAverageMotionIndex / iMbTotalNum; | 145 | iAverageMotionIndex = WELS_DIV_ROUND64(iAverageMotionIndex * AQ_INT_MULTIPLY,iMbTotalNum); |
| 146 | dAverageTextureIndex = dAverageTextureIndex / iMbTotalNum; | 146 | iAverageTextureIndex = WELS_DIV_ROUND64(iAverageTextureIndex * AQ_INT_MULTIPLY, iMbTotalNum); |
| 147 | if ((dAverageMotionIndex <= PESN) && (dAverageMotionIndex >= -PESN)) { | 147 | if ((iAverageMotionIndex <= AQ_PESN) && (iAverageMotionIndex >= -AQ_PESN)) { |
| 148 | dAverageMotionIndex = 1.0; | 148 | iAverageMotionIndex = AQ_INT_MULTIPLY; |
| 149 | } | 149 | } |
| 150 | if ((dAverageTextureIndex <= PESN) && (dAverageTextureIndex >= -PESN)) { | 150 | if ((iAverageTextureIndex <= AQ_PESN) && (iAverageTextureIndex >= -AQ_PESN)) { |
| 151 | dAverageTextureIndex = 1.0; | 151 | iAverageTextureIndex = AQ_INT_MULTIPLY; |
| 152 | } | 152 | } |
| 153 | // motion mb residual map to QP | 153 | // motion mb residual map to QP |
| 154 | // texture mb original map to QP | 154 | // texture mb original map to QP |
| 155 | iAverMotionTextureIndexToDeltaQp = 0; | 155 | iAverMotionTextureIndexToDeltaQp = 0; |
| 156 | dAverageMotionIndex = AVERAGE_TIME_MOTION * dAverageMotionIndex; | 156 | iAverageMotionIndex = WELS_DIV_ROUND64(AVERAGE_TIME_MOTION * iAverageMotionIndex, AQ_TIME_INT_MULTIPLY); |
| 157 | 157 | ||
| 158 | if (m_sAdaptiveQuantParam.iAdaptiveQuantMode == AQ_QUALITY_MODE) { | 158 | if (m_sAdaptiveQuantParam.iAdaptiveQuantMode == AQ_QUALITY_MODE) { |
| 159 | dAverageTextureIndex = AVERAGE_TIME_TEXTURE_QUALITYMODE * dAverageTextureIndex; | 159 | iAverageTextureIndex = WELS_DIV_ROUND64(AVERAGE_TIME_TEXTURE_QUALITYMODE * iAverageTextureIndex, AQ_TIME_INT_MULTIPLY); |
| 160 | } else { | 160 | } else { |
| 161 | dAverageTextureIndex = AVERAGE_TIME_TEXTURE_BITRATEMODE * dAverageTextureIndex; | 161 | iAverageTextureIndex = WELS_DIV_ROUND64(AVERAGE_TIME_TEXTURE_BITRATEMODE * iAverageTextureIndex, AQ_TIME_INT_MULTIPLY); |
| 162 | } | 162 | } |
| 163 | 163 | ||
| 164 | int64_t iAQ_EPSN = -((int64_t)AQ_PESN*AQ_TIME_INT_MULTIPLY*AQ_QSTEP_INT_MULTIPLY/AQ_INT_MULTIPLY); | ||
| 164 | pMotionTexture = m_sAdaptiveQuantParam.pMotionTextureUnit; | 165 | pMotionTexture = m_sAdaptiveQuantParam.pMotionTextureUnit; |
| 165 | for (j = 0; j < iMbHeight; j ++) { | 166 | for (j = 0; j < iMbHeight; j ++) { |
| 166 | for (i = 0; i < iMbWidth; i++) { | 167 | for (i = 0; i < iMbWidth; i++) { |
| 167 | double a = pMotionTexture->uiTextureIndex / dAverageTextureIndex; | 168 | int64_t a = WELS_DIV_ROUND64((int64_t)(pMotionTexture->uiTextureIndex) *AQ_INT_MULTIPLY * AQ_TIME_INT_MULTIPLY, iAverageTextureIndex); |
| 168 | dQStep = (a - 1) / (a + MODEL_ALPHA); | 169 | iQStep = WELS_DIV_ROUND64((a - AQ_TIME_INT_MULTIPLY) * AQ_QSTEP_INT_MULTIPLY, (a + MODEL_ALPHA)); |
| 169 | dLumaTextureDeltaQp = MODEL_TIME * dQStep;// range +- 6 | 170 | iLumaTextureDeltaQp = MODEL_TIME * iQStep;// range +- 6 |
| 170 | 171 | ||
| 171 | iMotionTextureIndexToDeltaQp = (int8_t)dLumaTextureDeltaQp; | 172 | iMotionTextureIndexToDeltaQp = ((int32_t)(iLumaTextureDeltaQp/(AQ_TIME_INT_MULTIPLY))); |
| 172 | 173 | ||
| 173 | a = pMotionTexture->uiMotionIndex / dAverageMotionIndex; | 174 | a = WELS_DIV_ROUND64(((int64_t)pMotionTexture->uiMotionIndex)*AQ_INT_MULTIPLY * AQ_TIME_INT_MULTIPLY, iAverageMotionIndex); |
| 174 | dQStep = (a - 1) / (a + MODEL_ALPHA); | 175 | iQStep = WELS_DIV_ROUND64((a - AQ_TIME_INT_MULTIPLY) * AQ_QSTEP_INT_MULTIPLY, (a + MODEL_ALPHA)); |
| 175 | dLumaMotionDeltaQp = MODEL_TIME * dQStep;// range +- 6 | 176 | iLumaMotionDeltaQp = MODEL_TIME * iQStep;// range +- 6 |
| 176 | 177 | ||
| 177 | if ((m_sAdaptiveQuantParam.iAdaptiveQuantMode == AQ_QUALITY_MODE && dLumaMotionDeltaQp < -PESN) | 178 | if ((m_sAdaptiveQuantParam.iAdaptiveQuantMode == AQ_QUALITY_MODE && iLumaMotionDeltaQp < iAQ_EPSN) |
| 178 | || (m_sAdaptiveQuantParam.iAdaptiveQuantMode == AQ_BITRATE_MODE)) { | 179 | || (m_sAdaptiveQuantParam.iAdaptiveQuantMode == AQ_BITRATE_MODE)) { |
| 179 | iMotionTextureIndexToDeltaQp += (int8_t)dLumaMotionDeltaQp; | 180 | iMotionTextureIndexToDeltaQp += ((int32_t)(iLumaMotionDeltaQp/(AQ_TIME_INT_MULTIPLY))); |
| 180 | } | 181 | } |
| 181 | 182 | ||
| 182 | m_sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp[j * iMbWidth + i] = iMotionTextureIndexToDeltaQp; | 183 | m_sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp[j * iMbWidth + i] = (int8_t)(iMotionTextureIndexToDeltaQp/AQ_QSTEP_INT_MULTIPLY); |
| 183 | iAverMotionTextureIndexToDeltaQp += iMotionTextureIndexToDeltaQp; | 184 | iAverMotionTextureIndexToDeltaQp += iMotionTextureIndexToDeltaQp; |
| 184 | pMotionTexture++; | 185 | pMotionTexture++; |
| 185 | } | 186 | } |
| 186 | } | 187 | } |
| 187 | m_sAdaptiveQuantParam.dAverMotionTextureIndexToDeltaQp = (1.0 * iAverMotionTextureIndexToDeltaQp) / iMbTotalNum; | 188 | |
| 189 | m_sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp = iAverMotionTextureIndexToDeltaQp / iMbTotalNum; | ||
| 188 | 190 | ||
| 189 | eReturn = RET_SUCCESS; | 191 | eReturn = RET_SUCCESS; |
| 190 | 192 | ||
| @@ -210,7 +212,7 @@ EResult CAdaptiveQuantization::Get (int32_t iType, void* pParam) { · 19 unmodified lines | |||
| 210 | 212 | ||
| 211 | SAdaptiveQuantizationParam* sAdaptiveQuantParam = (SAdaptiveQuantizationParam*)pParam; | 213 | SAdaptiveQuantizationParam* sAdaptiveQuantParam = (SAdaptiveQuantizationParam*)pParam; |
| 212 | 214 | ||
| 213 | sAdaptiveQuantParam->dAverMotionTextureIndexToDeltaQp = m_sAdaptiveQuantParam.dAverMotionTextureIndexToDeltaQp; | 215 | sAdaptiveQuantParam->iAverMotionTextureIndexToDeltaQp = m_sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp; |
| 214 | 216 | ||
| 215 | return RET_SUCCESS; | 217 | return RET_SUCCESS; |
| 216 | } | 218 | } |