/src/openh264/codec/processing/src/scrolldetection/ScrollDetectionFuncs.cpp
Line | Count | Source |
1 | | /*! |
2 | | * \copy |
3 | | * Copyright (c) 2009-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 "ScrollDetection.h" |
34 | | #include "ScrollDetectionFuncs.h" |
35 | | #include "ls_defines.h" |
36 | | |
37 | | WELSVP_NAMESPACE_BEGIN |
38 | | |
39 | 0 | int32_t CheckLine (uint8_t* pData, int32_t iWidth) { |
40 | 0 | int32_t iQualified = 0; |
41 | 0 | int32_t iColorMap[8] = {0}; |
42 | 0 | int32_t iChangedTimes = 0; |
43 | 0 | int32_t iColorCounts = 0; |
44 | |
|
45 | 0 | RECORD_COLOR (pData[0], iColorMap); |
46 | |
|
47 | 0 | for (int32_t i = 1; i < iWidth; i++) { |
48 | 0 | RECORD_COLOR (pData[i], iColorMap); |
49 | 0 | iChangedTimes += (pData[i] != pData[i - 1]); |
50 | 0 | } |
51 | 0 | for (int32_t i = 0; i < 8; i++) |
52 | 0 | for (int32_t j = 0; j < 32; j++) |
53 | 0 | iColorCounts += ((iColorMap[i] >> j) & 1); |
54 | |
|
55 | 0 | switch (iColorCounts) { |
56 | 0 | case 1: |
57 | 0 | iQualified = 0; |
58 | 0 | break; |
59 | 0 | case 2: |
60 | 0 | case 3: |
61 | 0 | iQualified = (iChangedTimes > 3); |
62 | 0 | break; |
63 | 0 | default: |
64 | 0 | iQualified = 1; |
65 | 0 | break; |
66 | 0 | } |
67 | 0 | return iQualified; |
68 | 0 | } |
69 | | |
70 | | int32_t SelectTestLine (uint8_t* pY, int32_t iWidth, int32_t iHeight, int32_t iPicHeight, |
71 | 0 | int32_t iStride, int32_t iOffsetX, int32_t iOffsetY) { |
72 | 0 | const int32_t kiHalfHeight = iHeight >> 1; |
73 | 0 | const int32_t kiMidPos = iOffsetY + kiHalfHeight; |
74 | 0 | int32_t TestPos = kiMidPos; |
75 | 0 | int32_t iOffsetAbs; |
76 | 0 | uint8_t* pTmp; |
77 | |
|
78 | 0 | for (iOffsetAbs = 0; iOffsetAbs < kiHalfHeight; iOffsetAbs++) { |
79 | 0 | TestPos = kiMidPos + iOffsetAbs; |
80 | 0 | if (TestPos < iPicHeight) { |
81 | 0 | pTmp = pY + TestPos * iStride + iOffsetX; |
82 | 0 | if (CheckLine (pTmp, iWidth)) break; |
83 | 0 | } |
84 | 0 | TestPos = kiMidPos - iOffsetAbs; |
85 | 0 | if (TestPos >= 0) { |
86 | 0 | pTmp = pY + TestPos * iStride + iOffsetX; |
87 | 0 | if (CheckLine (pTmp, iWidth)) break; |
88 | 0 | } |
89 | 0 | } |
90 | 0 | if (iOffsetAbs == kiHalfHeight) |
91 | 0 | TestPos = -1; |
92 | 0 | return TestPos; |
93 | 0 | } |
94 | | |
95 | | /* |
96 | | * compare pixel line between previous and current one |
97 | | * return: 0 for totally equal, otherwise 1 |
98 | | */ |
99 | 0 | int32_t CompareLine (uint8_t* pYSrc, uint8_t* pYRef, const int32_t kiWidth) { |
100 | 0 | int32_t iCmp = 1; |
101 | |
|
102 | 0 | if (LD32 (pYSrc) != LD32 (pYRef)) return 1; |
103 | 0 | if (LD32 (pYSrc + 4) != LD32 (pYRef + 4)) return 1; |
104 | 0 | if (LD32 (pYSrc + 8) != LD32 (pYRef + 8)) return 1; |
105 | 0 | if (kiWidth > 12) |
106 | 0 | iCmp = WelsMemcmp (pYSrc + 12, pYRef + 12, kiWidth - 12); |
107 | 0 | return iCmp; |
108 | 0 | } |
109 | | |
110 | | void ScrollDetectionCore (SPixMap* pSrcPixMap, SPixMap* pRefPixMap, int32_t iWidth, int32_t iHeight, |
111 | 0 | int32_t iOffsetX, int32_t iOffsetY, SScrollDetectionParam& sScrollDetectionParam) { |
112 | 0 | bool bScrollDetected = 0; |
113 | 0 | uint8_t* pYLine; |
114 | 0 | uint8_t* pYTmp; |
115 | 0 | int32_t iTestPos, iSearchPos = 0, iOffsetAbs, iMaxAbs; |
116 | 0 | int32_t iPicHeight = pRefPixMap->sRect.iRectHeight; |
117 | 0 | int32_t iMinHeight = WELS_MAX (iOffsetY, 0); |
118 | 0 | int32_t iMaxHeight = WELS_MIN (iOffsetY + iHeight - 1, iPicHeight - 1) ; //offset_y + height - 1;// |
119 | 0 | uint8_t* pYRef, *pYSrc; |
120 | 0 | int32_t iYStride; |
121 | |
|
122 | 0 | pYRef = (uint8_t*)pRefPixMap->pPixel[0]; |
123 | 0 | pYSrc = (uint8_t*)pSrcPixMap->pPixel[0]; |
124 | 0 | iYStride = pRefPixMap->iStride[0]; |
125 | |
|
126 | 0 | iTestPos = SelectTestLine (pYSrc, iWidth, iHeight, iPicHeight, iYStride, iOffsetX, iOffsetY); |
127 | |
|
128 | 0 | if (iTestPos == -1) { |
129 | 0 | sScrollDetectionParam.bScrollDetectFlag = 0; |
130 | 0 | return; |
131 | 0 | } |
132 | 0 | pYLine = pYSrc + iYStride * iTestPos + iOffsetX; |
133 | 0 | iMaxAbs = WELS_MIN (WELS_MAX (iTestPos - iMinHeight - 1, iMaxHeight - iTestPos), MAX_SCROLL_MV_Y); |
134 | 0 | iSearchPos = iTestPos; |
135 | 0 | for (iOffsetAbs = 0; iOffsetAbs <= iMaxAbs; iOffsetAbs++) { |
136 | 0 | iSearchPos = iTestPos + iOffsetAbs; |
137 | 0 | if (iSearchPos <= iMaxHeight) { |
138 | 0 | pYTmp = pYRef + iSearchPos * iYStride + iOffsetX; |
139 | 0 | if (!CompareLine (pYLine, pYTmp, iWidth)) { |
140 | 0 | uint8_t* pYUpper, *pYLineUpper; |
141 | 0 | int32_t iCheckedLines; |
142 | 0 | int32_t iLowOffset = WELS_MIN (iMaxHeight - iSearchPos, CHECK_OFFSET); |
143 | 0 | int32_t i; |
144 | |
|
145 | 0 | iCheckedLines = WELS_MIN (iTestPos - iMinHeight + iLowOffset, 2 * CHECK_OFFSET); |
146 | 0 | pYUpper = pYTmp - (iCheckedLines - iLowOffset) * iYStride; |
147 | 0 | pYLineUpper = pYLine - (iCheckedLines - iLowOffset) * iYStride; |
148 | |
|
149 | 0 | for (i = 0; i < iCheckedLines; i ++) { |
150 | 0 | if (CompareLine (pYLineUpper, pYUpper, iWidth)) { |
151 | 0 | break; |
152 | 0 | } |
153 | 0 | pYUpper += iYStride; |
154 | 0 | pYLineUpper += iYStride; |
155 | 0 | } |
156 | 0 | if (i == iCheckedLines) { |
157 | 0 | bScrollDetected = 1; |
158 | 0 | break; |
159 | 0 | } |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | 0 | iSearchPos = iTestPos - iOffsetAbs - 1; |
164 | 0 | if (iSearchPos >= iMinHeight) { |
165 | 0 | pYTmp = pYRef + iSearchPos * iYStride + iOffsetX; |
166 | 0 | if (!CompareLine (pYLine, pYTmp, iWidth)) { |
167 | 0 | uint8_t* pYUpper, *pYLineUpper; |
168 | 0 | int32_t iCheckedLines; |
169 | 0 | int32_t iUpOffset = WELS_MIN (iSearchPos - iMinHeight, CHECK_OFFSET); |
170 | 0 | int32_t i; |
171 | |
|
172 | 0 | pYUpper = pYTmp - iUpOffset * iYStride; |
173 | 0 | pYLineUpper = pYLine - iUpOffset * iYStride; |
174 | 0 | iCheckedLines = WELS_MIN (iMaxHeight - iTestPos + iUpOffset, 2 * CHECK_OFFSET); |
175 | |
|
176 | 0 | for (i = 0; i < iCheckedLines; i ++) { |
177 | 0 | if (CompareLine (pYLineUpper, pYUpper, iWidth)) { |
178 | 0 | break; |
179 | 0 | } |
180 | 0 | pYUpper += iYStride; |
181 | 0 | pYLineUpper += iYStride; |
182 | 0 | } |
183 | 0 | if (i == iCheckedLines) { |
184 | 0 | bScrollDetected = 1; |
185 | 0 | break; |
186 | 0 | } |
187 | 0 | } |
188 | 0 | } |
189 | 0 | } |
190 | |
|
191 | 0 | if (!bScrollDetected) { |
192 | 0 | sScrollDetectionParam.bScrollDetectFlag = 0; |
193 | 0 | } else { |
194 | 0 | sScrollDetectionParam.bScrollDetectFlag = 1; |
195 | 0 | sScrollDetectionParam.iScrollMvY = iSearchPos - iTestPos; // pre_pos - cur_pos, change to mv |
196 | 0 | sScrollDetectionParam.iScrollMvX = 0; |
197 | 0 | } |
198 | 0 | } |
199 | | |
200 | | WELSVP_NAMESPACE_END |