文件位置
● openh264/codec/processing/denoise/denoise.cpp
● openh264/codec/processing/denoise/denoise_filter.cpp
代碼流程
- 說明:從代碼流程可以看到,實現降噪的核心功能主要就是
BilateralDenoiseLuma
、WaverageDenoiseChroma
兩個函數。
原理
- 參數開關控制:(bool) bEnableDenoise
BilateralDenoiseLuma函數
- 功能:實現圖像亮度分量的雙邊濾波降噪
- 過程:
- 將亮度指針pSrcY向前移動m_uiSpaceRadius行,跳過圖像邊緣的噪聲處理區域;
- 外層循環遍歷圖像的高度,從m_uiSpaceRadius開始,到iHeight - m_uiSpaceRadius,跳過邊緣區域;
- 內層循環遍歷圖像的寬度,從m_uiSpaceRadius開始,每次增加8(為了利用SIMD指令集進行優化),直到iWidth - m_uiSpaceRadius - TAIL_OF_LINE8;
- 對當前8個像素塊使用雙邊濾波器
pfBilateralLumaFilter8
函數(指向具體的BilateralLumaFilter8_c
函數)進行去噪;- for 循環處理剩余的像素,這些像素不能被8整除,因此需要單獨處理;
- 調用
Gauss3x3Filter
函數對單個像素使用高斯濾波器進行去噪;- 將指針pSrcY向下移動一行,準備處理下一行的數據;
- 原理圖
- 相關源碼:
BilateralDenoiseLuma
函數
void CDenoiser::BilateralDenoiseLuma (uint8_t* pSrcY, int32_t iWidth, int32_t iHeight, int32_t iStride) {int32_t w;pSrcY = pSrcY + m_uiSpaceRadius * iStride;for (int32_t h = m_uiSpaceRadius; h < iHeight - m_uiSpaceRadius; h++) {for (w = m_uiSpaceRadius; w < iWidth - m_uiSpaceRadius - TAIL_OF_LINE8; w += 8) {m_pfDenoise.pfBilateralLumaFilter8 (pSrcY + w, iStride);}for (; w < iWidth - m_uiSpaceRadius; w++) {Gauss3x3Filter (pSrcY + w, iStride);}pSrcY += iStride;}
}
pfBilateralLumaFilter8
函數(指向具體的BilateralLumaFilter8_c
函數)
void BilateralLumaFilter8_c (uint8_t* pSample, int32_t iStride) {int32_t nSum = 0, nTotWeight = 0;int32_t iCenterSample = *pSample;uint8_t* pCurLine = pSample - iStride - DENOISE_GRAY_RADIUS;int32_t x, y;int32_t iCurSample, iCurWeight, iGreyDiff;uint8_t aSample[8];for (int32_t i = 0; i < 8; i++) {nSum = 0;nTotWeight = 0;iCenterSample = *pSample;pCurLine = pSample - iStride - DENOISE_GRAY_RADIUS;for (y = 0; y < 3; y++) {for (x = 0; x < 3; x++) {if (x == 1 && y == 1) continue; // except center pointiCurSample = pCurLine[x];iCurWeight = WELS_ABS (iCurSample - iCenterSample);iGreyDiff = 32 - iCurWeight;if (iGreyDiff < 0) continue;else iCurWeight = (iGreyDiff * iGreyDiff) >> 5;nSum += iCurSample * iCurWeight;nTotWeight += iCurWeight;}pCurLine += iStride;}nTotWeight = 256 - nTotWeight;nSum += iCenterSample * nTotWeight;aSample[i] = nSum >> 8;pSample++;}WelsMemcpy (pSample - 8, aSample, 8);
}
Gauss3x3Filter
函數
/***************************************************************************
edge of y/uv use a 3x3 Gauss filter, radius = 1:
1 2 1
2 4 2
1 2 1
***************************************************************************/
void Gauss3x3Filter (uint8_t* pSrc, int32_t iStride) {int32_t nSum = 0;uint8_t* pCurLine1 = pSrc - iStride - 1;uint8_t* pCurLine2 = pCurLine1 + iStride;uint8_t* pCurLine3 = pCurLine2 + iStride;nSum = pCurLine1[0] + (pCurLine1[1] << 1) + pCurLine1[2] +(pCurLine2[0] << 1) + (pCurLine2[1] << 2) + (pCurLine2[2] << 1) +pCurLine3[0] + (pCurLine3[1] << 1) + pCurLine3[2];*pSrc = nSum >> 4;
}
WaverageDenoiseChroma函數
- 功能:實現圖像色度分量的降噪功能
- 過程:
- 將指針pSrcUV向前移動UV_WINDOWS_RADIUS行,跳過圖像邊緣的噪聲處理區域;
- 外層循環遍歷圖像的高度,從UV_WINDOWS_RADIUS開始,到iHeight - UV_WINDOWS_RADIUS,跳過邊緣區域;
- 內層循環遍歷圖像的寬度,從UV_WINDOWS_RADIUS開始,每次增加8(為了利用SIMD指令集進行優化),直到iWidth - UV_WINDOWS_RADIUS - TAIL_OF_LINE8,用于確保在處理行尾時不會超出邊界;
- 對當前8個像素塊使用加權平均濾波器
pfWaverageChromaFilter8
函數(指向WaverageChromaFilter8_c
函數)進行去噪;- 調用
Gauss3x3Filter
函數處理剩余的像素,這些像素不能被8整除,因此需要單獨處理;- 將指針pSrcUV向下移動一行,準備處理下一行的數據。
- 原理圖:
- 相關源碼:
WaverageDenoiseChroma
函數
void CDenoiser::WaverageDenoiseChroma (uint8_t* pSrcUV, int32_t iWidth, int32_t iHeight, int32_t iStride) {int32_t w;pSrcUV = pSrcUV + UV_WINDOWS_RADIUS * iStride;for (int32_t h = UV_WINDOWS_RADIUS; h < iHeight - UV_WINDOWS_RADIUS; h++) {for (w = UV_WINDOWS_RADIUS; w < iWidth - UV_WINDOWS_RADIUS - TAIL_OF_LINE8; w += 8) {m_pfDenoise.pfWaverageChromaFilter8 (pSrcUV + w, iStride);}for (; w < iWidth - UV_WINDOWS_RADIUS; w++) {Gauss3x3Filter (pSrcUV + w, iStride);}pSrcUV += iStride;}
}
WaverageChromaFilter8_c
函數
/***************************************************************************
5x5 filter:
1 1 2 1 1
1 2 4 2 1
2 4 20 4 2
1 2 4 2 1
1 1 2 1 1
***************************************************************************/
#define SUM_LINE1(pSample) (pSample[0] +(pSample[1]) +(pSample[2]<<1) + pSample[3] + pSample[4])
#define SUM_LINE2(pSample) (pSample[0] +(pSample[1]<<1) +(pSample[2]<<2) +(pSample[3]<<1) + pSample[4])
#define SUM_LINE3(pSample) ((pSample[0]<<1) +(pSample[1]<<2) +(pSample[2]*20) +(pSample[3]<<2) +(pSample[4]<<1))
void WaverageChromaFilter8_c (uint8_t* pSample, int32_t iStride) {int32_t sum;uint8_t* pStartPixels = pSample - UV_WINDOWS_RADIUS * iStride - UV_WINDOWS_RADIUS;uint8_t* pCurLine1 = pStartPixels;uint8_t* pCurLine2 = pCurLine1 + iStride;uint8_t* pCurLine3 = pCurLine2 + iStride;uint8_t* pCurLine4 = pCurLine3 + iStride;uint8_t* pCurLine5 = pCurLine4 + iStride;uint8_t aSample[8];for (int32_t i = 0; i < 8; i++) {sum = SUM_LINE1 ((pCurLine1 + i)) + SUM_LINE2 ((pCurLine2 + i)) + SUM_LINE3 ((pCurLine3 + i))+ SUM_LINE2 ((pCurLine4 + i)) + SUM_LINE1 ((pCurLine5 + i));aSample[i] = (sum >> 6);pSample++;}WelsMemcpy (pSample - 8, aSample, 8);
}
Gauss3x3Filter
函數
/***************************************************************************
edge of y/uv use a 3x3 Gauss filter, radius = 1:
1 2 1
2 4 2
1 2 1
***************************************************************************/
void Gauss3x3Filter (uint8_t* pSrc, int32_t iStride) {int32_t nSum = 0;uint8_t* pCurLine1 = pSrc - iStride - 1;uint8_t* pCurLine2 = pCurLine1 + iStride;uint8_t* pCurLine3 = pCurLine2 + iStride;nSum = pCurLine1[0] + (pCurLine1[1] << 1) + pCurLine1[2] +(pCurLine2[0] << 1) + (pCurLine2[1] << 2) + (pCurLine2[2] << 1) +pCurLine3[0] + (pCurLine3[1] << 1) + pCurLine3[2];*pSrc = nSum >> 4;
}