1 /*!
2 * \copy
3 * Copyright (c) 2013, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33 #include "denoise.h"
34 #include "cpu.h"
35
36 WELSVP_NAMESPACE_BEGIN
37
38 #define CALC_BI_STRIDE(iWidth, iBitcount) ((((iWidth) * (iBitcount) + 31) & ~31) >> 3)
39
40 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
41
CDenoiser(int32_t iCpuFlag)42 CDenoiser::CDenoiser (int32_t iCpuFlag) {
43 m_CPUFlag = iCpuFlag;
44 m_eMethod = METHOD_DENOISE;
45 WelsMemset (&m_pfDenoise, 0, sizeof (m_pfDenoise));
46
47 m_uiSpaceRadius = DENOISE_GRAY_RADIUS;
48 m_fSigmaGrey = DENOISE_GRAY_SIGMA;
49 m_uiType = DENOISE_ALL_COMPONENT;
50 InitDenoiseFunc (m_pfDenoise, m_CPUFlag);
51 }
52
~CDenoiser()53 CDenoiser::~CDenoiser() {
54 }
55
InitDenoiseFunc(SDenoiseFuncs & denoiser,int32_t iCpuFlag)56 void CDenoiser::InitDenoiseFunc (SDenoiseFuncs& denoiser, int32_t iCpuFlag) {
57 denoiser.pfBilateralLumaFilter8 = BilateralLumaFilter8_c;
58 denoiser.pfWaverageChromaFilter8 = WaverageChromaFilter8_c;
59 #if defined(X86_ASM)
60 if (iCpuFlag & WELS_CPU_SSE2) {
61 denoiser.pfBilateralLumaFilter8 = BilateralLumaFilter8_sse2;
62 denoiser.pfWaverageChromaFilter8 = WaverageChromaFilter8_sse2;
63 }
64 #endif
65 }
66
Process(int32_t iType,SPixMap * pSrc,SPixMap * dst)67 EResult CDenoiser::Process (int32_t iType, SPixMap* pSrc, SPixMap* dst) {
68 uint8_t* pSrcY = (uint8_t*)pSrc->pPixel[0];
69 uint8_t* pSrcU = (uint8_t*)pSrc->pPixel[1];
70 uint8_t* pSrcV = (uint8_t*)pSrc->pPixel[2];
71 if (pSrcY == NULL || pSrcU == NULL || pSrcV == NULL) {
72 return RET_INVALIDPARAM;
73 }
74
75 int32_t iWidthY = pSrc->sRect.iRectWidth;
76 int32_t iHeightY = pSrc->sRect.iRectHeight;
77 int32_t iWidthUV = iWidthY >> 1;
78 int32_t iHeightUV = iHeightY >> 1;
79
80 if (m_uiType & DENOISE_Y_COMPONENT)
81 BilateralDenoiseLuma (pSrcY, iWidthY, iHeightY, pSrc->iStride[0]);
82
83 if (m_uiType & DENOISE_U_COMPONENT)
84 WaverageDenoiseChroma (pSrcU, iWidthUV, iHeightUV, pSrc->iStride[1]);
85
86 if (m_uiType & DENOISE_V_COMPONENT)
87 WaverageDenoiseChroma (pSrcV, iWidthUV, iHeightUV, pSrc->iStride[2]);
88
89 return RET_SUCCESS;
90 }
91
BilateralDenoiseLuma(uint8_t * pSrcY,int32_t iWidth,int32_t iHeight,int32_t iStride)92 void CDenoiser::BilateralDenoiseLuma (uint8_t* pSrcY, int32_t iWidth, int32_t iHeight, int32_t iStride) {
93 int32_t w;
94
95 pSrcY = pSrcY + m_uiSpaceRadius * iStride;
96 for (int32_t h = m_uiSpaceRadius; h < iHeight - m_uiSpaceRadius; h++) {
97 for (w = m_uiSpaceRadius; w < iWidth - m_uiSpaceRadius - TAIL_OF_LINE8; w += 8) {
98 m_pfDenoise.pfBilateralLumaFilter8 (pSrcY + w, iStride);
99 }
100 for (; w < iWidth - m_uiSpaceRadius; w++) {
101 Gauss3x3Filter (pSrcY + w, iStride);
102 }
103 pSrcY += iStride;
104 }
105 }
106
WaverageDenoiseChroma(uint8_t * pSrcUV,int32_t iWidth,int32_t iHeight,int32_t iStride)107 void CDenoiser::WaverageDenoiseChroma (uint8_t* pSrcUV, int32_t iWidth, int32_t iHeight, int32_t iStride) {
108 int32_t w;
109
110 pSrcUV = pSrcUV + UV_WINDOWS_RADIUS * iStride;
111 for (int32_t h = UV_WINDOWS_RADIUS; h < iHeight - UV_WINDOWS_RADIUS; h++) {
112 for (w = UV_WINDOWS_RADIUS; w < iWidth - UV_WINDOWS_RADIUS - TAIL_OF_LINE8; w += 8) {
113 m_pfDenoise.pfWaverageChromaFilter8 (pSrcUV + w, iStride);
114 }
115
116 for (; w < iWidth - UV_WINDOWS_RADIUS; w++) {
117 Gauss3x3Filter (pSrcUV + w, iStride);
118 }
119 pSrcUV += iStride;
120 }
121 }
122
123
124 WELSVP_NAMESPACE_END
125