/src/openh264/codec/processing/src/denoise/denoise.cpp
Line | Count | Source |
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 | | |
42 | 0 | CDenoiser::CDenoiser (int32_t iCpuFlag) { |
43 | 0 | m_CPUFlag = iCpuFlag; |
44 | 0 | m_eMethod = METHOD_DENOISE; |
45 | 0 | WelsMemset (&m_pfDenoise, 0, sizeof (m_pfDenoise)); |
46 | |
|
47 | 0 | m_uiSpaceRadius = DENOISE_GRAY_RADIUS; |
48 | 0 | m_fSigmaGrey = DENOISE_GRAY_SIGMA; |
49 | 0 | m_uiType = DENOISE_ALL_COMPONENT; |
50 | 0 | InitDenoiseFunc (m_pfDenoise, m_CPUFlag); |
51 | 0 | } |
52 | | |
53 | 0 | CDenoiser::~CDenoiser() { |
54 | 0 | } |
55 | | |
56 | 0 | void CDenoiser::InitDenoiseFunc (SDenoiseFuncs& denoiser, int32_t iCpuFlag) { |
57 | 0 | denoiser.pfBilateralLumaFilter8 = BilateralLumaFilter8_c; |
58 | 0 | denoiser.pfWaverageChromaFilter8 = WaverageChromaFilter8_c; |
59 | 0 | #if defined(X86_ASM) |
60 | 0 | if (iCpuFlag & WELS_CPU_SSE2) { |
61 | 0 | denoiser.pfBilateralLumaFilter8 = BilateralLumaFilter8_sse2; |
62 | 0 | denoiser.pfWaverageChromaFilter8 = WaverageChromaFilter8_sse2; |
63 | 0 | } |
64 | 0 | #endif |
65 | 0 | } |
66 | | |
67 | 0 | EResult CDenoiser::Process (int32_t iType, SPixMap* pSrc, SPixMap* dst) { |
68 | 0 | uint8_t* pSrcY = (uint8_t*)pSrc->pPixel[0]; |
69 | 0 | uint8_t* pSrcU = (uint8_t*)pSrc->pPixel[1]; |
70 | 0 | uint8_t* pSrcV = (uint8_t*)pSrc->pPixel[2]; |
71 | 0 | if (pSrcY == NULL || pSrcU == NULL || pSrcV == NULL) { |
72 | 0 | return RET_INVALIDPARAM; |
73 | 0 | } |
74 | | |
75 | 0 | int32_t iWidthY = pSrc->sRect.iRectWidth; |
76 | 0 | int32_t iHeightY = pSrc->sRect.iRectHeight; |
77 | 0 | int32_t iWidthUV = iWidthY >> 1; |
78 | 0 | int32_t iHeightUV = iHeightY >> 1; |
79 | |
|
80 | 0 | if (m_uiType & DENOISE_Y_COMPONENT) |
81 | 0 | BilateralDenoiseLuma (pSrcY, iWidthY, iHeightY, pSrc->iStride[0]); |
82 | |
|
83 | 0 | if (m_uiType & DENOISE_U_COMPONENT) |
84 | 0 | WaverageDenoiseChroma (pSrcU, iWidthUV, iHeightUV, pSrc->iStride[1]); |
85 | |
|
86 | 0 | if (m_uiType & DENOISE_V_COMPONENT) |
87 | 0 | WaverageDenoiseChroma (pSrcV, iWidthUV, iHeightUV, pSrc->iStride[2]); |
88 | |
|
89 | 0 | return RET_SUCCESS; |
90 | 0 | } |
91 | | |
92 | 0 | void CDenoiser::BilateralDenoiseLuma (uint8_t* pSrcY, int32_t iWidth, int32_t iHeight, int32_t iStride) { |
93 | 0 | int32_t w; |
94 | |
|
95 | 0 | pSrcY = pSrcY + m_uiSpaceRadius * iStride; |
96 | 0 | for (int32_t h = m_uiSpaceRadius; h < iHeight - m_uiSpaceRadius; h++) { |
97 | 0 | for (w = m_uiSpaceRadius; w < iWidth - m_uiSpaceRadius - TAIL_OF_LINE8; w += 8) { |
98 | 0 | m_pfDenoise.pfBilateralLumaFilter8 (pSrcY + w, iStride); |
99 | 0 | } |
100 | 0 | for (; w < iWidth - m_uiSpaceRadius; w++) { |
101 | 0 | Gauss3x3Filter (pSrcY + w, iStride); |
102 | 0 | } |
103 | 0 | pSrcY += iStride; |
104 | 0 | } |
105 | 0 | } |
106 | | |
107 | 0 | void CDenoiser::WaverageDenoiseChroma (uint8_t* pSrcUV, int32_t iWidth, int32_t iHeight, int32_t iStride) { |
108 | 0 | int32_t w; |
109 | |
|
110 | 0 | pSrcUV = pSrcUV + UV_WINDOWS_RADIUS * iStride; |
111 | 0 | for (int32_t h = UV_WINDOWS_RADIUS; h < iHeight - UV_WINDOWS_RADIUS; h++) { |
112 | 0 | for (w = UV_WINDOWS_RADIUS; w < iWidth - UV_WINDOWS_RADIUS - TAIL_OF_LINE8; w += 8) { |
113 | 0 | m_pfDenoise.pfWaverageChromaFilter8 (pSrcUV + w, iStride); |
114 | 0 | } |
115 | |
|
116 | 0 | for (; w < iWidth - UV_WINDOWS_RADIUS; w++) { |
117 | 0 | Gauss3x3Filter (pSrcUV + w, iStride); |
118 | 0 | } |
119 | 0 | pSrcUV += iStride; |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | |
124 | | WELSVP_NAMESPACE_END |