Showing the changes to codec/processing/src/backgrounddetection/BackgroundDetection.cpp (1 of 5 files). Show all
Unified · Split
| @@ -0,0 +1,388 @@ | |||
| 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 "BackgroundDetection.h" | ||
| 34 | |||
| 35 | WELSVP_NAMESPACE_BEGIN | ||
| 36 | |||
| 37 | #define LOG2_BGD_OU_SIZE (4) | ||
| 38 | #define LOG2_BGD_OU_SIZE_UV (LOG2_BGD_OU_SIZE-1) | ||
| 39 | #define BGD_OU_SIZE (1<<LOG2_BGD_OU_SIZE) | ||
| 40 | #define BGD_OU_SIZE_UV (BGD_OU_SIZE>>1) | ||
| 41 | #define BGD_THD_SAD (2*BGD_OU_SIZE*BGD_OU_SIZE) | ||
| 42 | #define BGD_THD_ASD_UV (4*BGD_OU_SIZE_UV) | ||
| 43 | #define LOG2_MB_SIZE (4) | ||
| 44 | #define OU_SIZE_IN_MB (BGD_OU_SIZE >> 4) | ||
| 45 | #define Q_FACTOR (8) | ||
| 46 | #define BGD_DELTA_QP_THD (3) | ||
| 47 | |||
| 48 | #define OU_LEFT (0x01) | ||
| 49 | #define OU_RIGHT (0x02) | ||
| 50 | #define OU_TOP (0x04) | ||
| 51 | #define OU_BOTTOM (0x08) | ||
| 52 | |||
| 53 | CBackgroundDetection::CBackgroundDetection (int32_t iCpuFlag) { | ||
| 54 | m_eMethod = METHOD_BACKGROUND_DETECTION; | ||
| 55 | WelsMemset (&m_BgdParam, 0, sizeof (m_BgdParam)); | ||
| 56 | m_iLargestFrameSize = 0; | ||
| 57 | } | ||
| 58 | |||
| 59 | CBackgroundDetection::~CBackgroundDetection() { | ||
| 60 | FreeOUArrayMemory(); | ||
| 61 | } | ||
| 62 | |||
| 63 | EResult CBackgroundDetection::Process (int32_t iType, SPixMap* pSrcPixMap, SPixMap* pRefPixMap) { | ||
| 64 | EResult eReturn = RET_INVALIDPARAM; | ||
| 65 | |||
| 66 | if (pSrcPixMap == NULL || pRefPixMap == NULL) | ||
| 67 | return eReturn; | ||
| 68 | |||
| 69 | m_BgdParam.pCur[0] = (uint8_t*)pSrcPixMap->pPixel[0]; | ||
| 70 | m_BgdParam.pCur[1] = (uint8_t*)pSrcPixMap->pPixel[1]; | ||
| 71 | m_BgdParam.pCur[2] = (uint8_t*)pSrcPixMap->pPixel[2]; | ||
| 72 | m_BgdParam.pRef[0] = (uint8_t*)pRefPixMap->pPixel[0]; | ||
| 73 | m_BgdParam.pRef[1] = (uint8_t*)pRefPixMap->pPixel[1]; | ||
| 74 | m_BgdParam.pRef[2] = (uint8_t*)pRefPixMap->pPixel[2]; | ||
| 75 | m_BgdParam.iBgdWidth = pSrcPixMap->sRect.iRectWidth; | ||
| 76 | m_BgdParam.iBgdHeight = pSrcPixMap->sRect.iRectHeight; | ||
| 77 | m_BgdParam.iStride[0] = pSrcPixMap->iStride[0]; | ||
| 78 | m_BgdParam.iStride[1] = pSrcPixMap->iStride[1]; | ||
| 79 | m_BgdParam.iStride[2] = pSrcPixMap->iStride[2]; | ||
| 80 | |||
| 81 | int32_t iCurFrameSize = m_BgdParam.iBgdWidth * m_BgdParam.iBgdHeight; | ||
| 82 | if (m_BgdParam.pOU_array == NULL || iCurFrameSize > m_iLargestFrameSize) { | ||
| 83 | FreeOUArrayMemory(); | ||
| 84 | m_BgdParam.pOU_array = AllocateOUArrayMemory (m_BgdParam.iBgdWidth, m_BgdParam.iBgdHeight); | ||
| 85 | m_iLargestFrameSize = iCurFrameSize; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (m_BgdParam.pOU_array == NULL) | ||
| 89 | return eReturn; | ||
| 90 | |||
| 91 | BackgroundDetection (&m_BgdParam); | ||
| 92 | |||
| 93 | return RET_SUCCESS; | ||
| 94 | } | ||
| 95 | |||
| 96 | EResult CBackgroundDetection::Set (int32_t iType, void* pParam) { | ||
| 97 | if (pParam == NULL) { | ||
| 98 | return RET_INVALIDPARAM; | ||
| 99 | } | ||
| 100 | |||
| 101 | SBGDInterface* pInterface = (SBGDInterface*)pParam; | ||
| 102 | |||
| 103 | m_BgdParam.pBackgroundMbFlag = (int8_t*)pInterface->pBackgroundMbFlag; | ||
| 104 | m_BgdParam.pCalcRes = pInterface->pCalcRes; | ||
| 105 | |||
| 106 | return RET_SUCCESS; | ||
| 107 | } | ||
| 108 | |||
| 109 | inline SBackgroundOU* CBackgroundDetection::AllocateOUArrayMemory (int32_t iWidth, int32_t iHeight) { | ||
| 110 | int32_t iMaxOUWidth = (BGD_OU_SIZE - 1 + iWidth) >> LOG2_BGD_OU_SIZE; | ||
| 111 | int32_t iMaxOUHeight = (BGD_OU_SIZE - 1 + iHeight) >> LOG2_BGD_OU_SIZE; | ||
| 112 | return (SBackgroundOU*)WelsMalloc (iMaxOUWidth * iMaxOUHeight * sizeof (SBackgroundOU)); | ||
| 113 | } | ||
| 114 | |||
| 115 | inline void CBackgroundDetection::FreeOUArrayMemory() { | ||
| 116 | _SafeFree (m_BgdParam.pOU_array); | ||
| 117 | } | ||
| 118 | |||
| 119 | void CBackgroundDetection::GetOUParameters (SVAACalcResult* sVaaCalcInfo, int32_t iMbIndex, int32_t iMbWidth, | ||
| 120 | SBackgroundOU* pBgdOU) { | ||
| 121 | int32_t iSubSD[4]; | ||
| 122 | uint8_t iSubMAD[4]; | ||
| 123 | int32_t iSubSAD[4]; | ||
| 124 | |||
| 125 | uint8_t (*pMad8x8)[4]; | ||
| 126 | int32_t (*pSad8x8)[4]; | ||
| 127 | int32_t (*pSd8x8)[4]; | ||
| 128 | |||
| 129 | pSad8x8 = sVaaCalcInfo->pSad8x8; | ||
| 130 | pMad8x8 = sVaaCalcInfo->pMad8x8; | ||
| 131 | pSd8x8 = sVaaCalcInfo->pSumOfDiff8x8; | ||
| 132 | |||
| 133 | iSubSAD[0] = pSad8x8[iMbIndex][0]; | ||
| 134 | iSubSAD[1] = pSad8x8[iMbIndex][1]; | ||
| 135 | iSubSAD[2] = pSad8x8[iMbIndex][2]; | ||
| 136 | iSubSAD[3] = pSad8x8[iMbIndex][3]; | ||
| 137 | |||
| 138 | iSubSD[0] = pSd8x8[iMbIndex][0]; | ||
| 139 | iSubSD[1] = pSd8x8[iMbIndex][1]; | ||
| 140 | iSubSD[2] = pSd8x8[iMbIndex][2]; | ||
| 141 | iSubSD[3] = pSd8x8[iMbIndex][3]; | ||
| 142 | |||
| 143 | iSubMAD[0] = pMad8x8[iMbIndex][0]; | ||
| 144 | iSubMAD[1] = pMad8x8[iMbIndex][1]; | ||
| 145 | iSubMAD[2] = pMad8x8[iMbIndex][2]; | ||
| 146 | iSubMAD[3] = pMad8x8[iMbIndex][3]; | ||
| 147 | |||
| 148 | pBgdOU->iSD = iSubSD[0] + iSubSD[1] + iSubSD[2] + iSubSD[3]; | ||
| 149 | pBgdOU->iSAD = iSubSAD[0] + iSubSAD[1] + iSubSAD[2] + iSubSAD[3]; | ||
| 150 | pBgdOU->iSD = WELS_ABS (pBgdOU->iSD); | ||
| 151 | |||
| 152 | // get the max absolute difference (MAD) of OU and min value of the MAD of sub-blocks of OU | ||
| 153 | pBgdOU->iMAD = WELS_MAX (WELS_MAX (iSubMAD[0], iSubMAD[1]), WELS_MAX (iSubMAD[2], iSubMAD[3])); | ||
| 154 | pBgdOU->iMinSubMad = WELS_MIN (WELS_MIN (iSubMAD[0], iSubMAD[1]), WELS_MIN (iSubMAD[2], iSubMAD[3])); | ||
| 155 | |||
| 156 | // get difference between the max and min SD of the SDs of sub-blocks of OU | ||
| 157 | pBgdOU->iMaxDiffSubSd = WELS_MAX (WELS_MAX (iSubSD[0], iSubSD[1]), WELS_MAX (iSubSD[2], iSubSD[3])) - | ||
| 158 | WELS_MIN (WELS_MIN (iSubSD[0], iSubSD[1]), WELS_MIN (iSubSD[2], iSubSD[3])); | ||
| 159 | } | ||
| 160 | |||
| 161 | void CBackgroundDetection::ForegroundBackgroundDivision (vBGDParam* pBgdParam) { | ||
| 162 | int32_t iPicWidthInOU = pBgdParam->iBgdWidth >> LOG2_BGD_OU_SIZE; | ||
| 163 | int32_t iPicHeightInOU = pBgdParam->iBgdHeight >> LOG2_BGD_OU_SIZE; | ||
| 164 | int32_t iPicWidthInMb = (15 + pBgdParam->iBgdWidth) >> 4; | ||
| 165 | |||
| 166 | SBackgroundOU* pBackgroundOU = pBgdParam->pOU_array; | ||
| 167 | |||
| 168 | for (int32_t j = 0; j < iPicHeightInOU; j ++) { | ||
| 169 | for (int32_t i = 0; i < iPicWidthInOU; i++) { | ||
| 170 | GetOUParameters (pBgdParam->pCalcRes, (j * iPicWidthInMb + i) << (LOG2_BGD_OU_SIZE - LOG2_MB_SIZE), iPicWidthInMb, | ||
| 171 | pBackgroundOU); | ||
| 172 | |||
| 173 | pBackgroundOU->iBackgroundFlag = 0; | ||
| 174 | if (pBackgroundOU->iMAD > 63) { | ||
| 175 | pBackgroundOU++; | ||
| 176 | continue; | ||
| 177 | } | ||
| 178 | if ((pBackgroundOU->iMaxDiffSubSd <= pBackgroundOU->iSAD >> 3 | ||
| 179 | || pBackgroundOU->iMaxDiffSubSd <= (BGD_OU_SIZE * Q_FACTOR)) | ||
| 180 | && pBackgroundOU->iSAD < (BGD_THD_SAD << 1)) { //BGD_OU_SIZE*BGD_OU_SIZE>>2 | ||
| 181 | if (pBackgroundOU->iSAD <= BGD_OU_SIZE * Q_FACTOR) { | ||
| 182 | pBackgroundOU->iBackgroundFlag = 1; | ||
| 183 | } else { | ||
| 184 | pBackgroundOU->iBackgroundFlag = pBackgroundOU->iSAD < BGD_THD_SAD ? | ||
| 185 | (pBackgroundOU->iSD < (pBackgroundOU->iSAD * 3) >> 2) : | ||
| 186 | (pBackgroundOU->iSD << 1 < pBackgroundOU->iSAD); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | pBackgroundOU++; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | inline int32_t CBackgroundDetection::CalculateAsdChromaEdge (uint8_t* pOriRef, uint8_t* pOriCur, int32_t iStride) { | ||
| 194 | int32_t ASD = 0; | ||
| 195 | int32_t idx; | ||
| 196 | for (idx = 0; idx < BGD_OU_SIZE_UV; idx++) { | ||
| 197 | ASD += *pOriCur - *pOriRef; | ||
| 198 | pOriRef += iStride; | ||
| 199 | pOriCur += iStride; | ||
| 200 | } | ||
| 201 | return WELS_ABS (ASD); | ||
| 202 | } | ||
| 203 | |||
| 204 | inline bool_t CBackgroundDetection::ForegroundDilation23Luma (SBackgroundOU* pBackgroundOU, | ||
| 205 | SBackgroundOU* pOUNeighbours[]) { | ||
| 206 | SBackgroundOU* pOU_L = pOUNeighbours[0]; | ||
| 207 | SBackgroundOU* pOU_R = pOUNeighbours[1]; | ||
| 208 | SBackgroundOU* pOU_U = pOUNeighbours[2]; | ||
| 209 | SBackgroundOU* pOU_D = pOUNeighbours[3]; | ||
| 210 | |||
| 211 | if (pBackgroundOU->iMAD > pBackgroundOU->iMinSubMad << 1) { | ||
| 212 | int32_t iMaxNbrForegroundMad; | ||
| 213 | int32_t iMaxNbrBackgroundMad; | ||
| 214 | int32_t aBackgroundMad[4]; | ||
| 215 | int32_t aForegroundMad[4]; | ||
| 216 | |||
| 217 | aForegroundMad[0] = (pOU_L->iBackgroundFlag - 1) & pOU_L->iMAD; | ||
| 218 | aForegroundMad[1] = (pOU_R->iBackgroundFlag - 1) & pOU_R->iMAD; | ||
| 219 | aForegroundMad[2] = (pOU_U->iBackgroundFlag - 1) & pOU_U->iMAD; | ||
| 220 | aForegroundMad[3] = (pOU_D->iBackgroundFlag - 1) & pOU_D->iMAD; | ||
| 221 | iMaxNbrForegroundMad = WELS_MAX (WELS_MAX (aForegroundMad[0], aForegroundMad[1]), WELS_MAX (aForegroundMad[2], | ||
| 222 | aForegroundMad[3])); | ||
| 223 | |||
| 224 | aBackgroundMad[0] = ((!pOU_L->iBackgroundFlag) - 1) & pOU_L->iMAD; | ||
| 225 | aBackgroundMad[1] = ((!pOU_R->iBackgroundFlag) - 1) & pOU_R->iMAD; | ||
| 226 | aBackgroundMad[2] = ((!pOU_U->iBackgroundFlag) - 1) & pOU_U->iMAD; | ||
| 227 | aBackgroundMad[3] = ((!pOU_D->iBackgroundFlag) - 1) & pOU_D->iMAD; | ||
| 228 | iMaxNbrBackgroundMad = WELS_MAX (WELS_MAX (aBackgroundMad[0], aBackgroundMad[1]), WELS_MAX (aBackgroundMad[2], | ||
| 229 | aBackgroundMad[3])); | ||
| 230 | |||
| 231 | return ((iMaxNbrForegroundMad > pBackgroundOU->iMinSubMad << 2) || (pBackgroundOU->iMAD > iMaxNbrBackgroundMad << 1 | ||
| 232 | && pBackgroundOU->iMAD <= (iMaxNbrForegroundMad * 3) >> 1)); | ||
| 233 | } | ||
| 234 | return 0; | ||
| 235 | } | ||
| 236 | |||
| 237 | inline bool_t CBackgroundDetection::ForegroundDilation23Chroma (int8_t iNeighbourForegroundFlags, | ||
| 238 | int32_t iStartSamplePos, int32_t iPicStrideUV, vBGDParam* pBgdParam) { | ||
| 239 | static const int8_t kaOUPos[4] = {OU_LEFT, OU_RIGHT, OU_TOP, OU_BOTTOM}; | ||
| 240 | int32_t aEdgeOffset[4] = {0, BGD_OU_SIZE_UV - 1, 0, iPicStrideUV* (BGD_OU_SIZE_UV - 1)}; | ||
| 241 | int32_t iStride[4] = {iPicStrideUV, iPicStrideUV, 1, 1}; | ||
| 242 | |||
| 243 | // V component first, high probability because V stands for red color and human skin colors have more weight on this component | ||
| 244 | for (int32_t i = 0; i < 4; i++) { | ||
| 245 | if (iNeighbourForegroundFlags & kaOUPos[i]) { | ||
| 246 | uint8_t* pRefC = pBgdParam->pRef[2] + iStartSamplePos + aEdgeOffset[i]; | ||
| 247 | uint8_t* pCurC = pBgdParam->pCur[2] + iStartSamplePos + aEdgeOffset[i]; | ||
| 248 | if (CalculateAsdChromaEdge (pRefC, pCurC, iStride[i]) > BGD_THD_ASD_UV) { | ||
| 249 | return 1; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | // U component, which stands for blue color, low probability | ||
| 254 | for (int32_t i = 0; i < 4; i++) { | ||
| 255 | if (iNeighbourForegroundFlags & kaOUPos[i]) { | ||
| 256 | uint8_t* pRefC = pBgdParam->pRef[1] + iStartSamplePos + aEdgeOffset[i]; | ||
| 257 | uint8_t* pCurC = pBgdParam->pCur[1] + iStartSamplePos + aEdgeOffset[i]; | ||
| 258 | if (CalculateAsdChromaEdge (pRefC, pCurC, iStride[i]) > BGD_THD_ASD_UV) { | ||
| 259 | return 1; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | return 0; | ||
| 265 | } | ||
| 266 | |||
| 267 | inline void CBackgroundDetection::ForegroundDilation (SBackgroundOU* pBackgroundOU, SBackgroundOU* pOUNeighbours[], | ||
| 268 | vBGDParam* pBgdParam, int32_t iChromaSampleStartPos) { | ||
| 269 | int32_t iPicStrideUV = pBgdParam->iStride[1]; | ||
| 270 | int32_t iSumNeighBackgroundFlags = pOUNeighbours[0]->iBackgroundFlag + pOUNeighbours[1]->iBackgroundFlag + | ||
| 271 | pOUNeighbours[2]->iBackgroundFlag + pOUNeighbours[3]->iBackgroundFlag; | ||
| 272 | |||
| 273 | if (pBackgroundOU->iSAD > BGD_OU_SIZE * Q_FACTOR) { | ||
| 274 | switch (iSumNeighBackgroundFlags) { | ||
| 275 | case 0: | ||
| 276 | case 1: | ||
| 277 | pBackgroundOU->iBackgroundFlag = 0; | ||
| 278 | break; | ||
| 279 | case 2: | ||
| 280 | case 3: | ||
| 281 | pBackgroundOU->iBackgroundFlag = !ForegroundDilation23Luma (pBackgroundOU, pOUNeighbours); | ||
| 282 | |||
| 283 | // chroma component check | ||
| 284 | if (pBackgroundOU->iBackgroundFlag == 1) { | ||
| 285 | int8_t iNeighbourForegroundFlags = !pOUNeighbours[0]->iBackgroundFlag | ((!pOUNeighbours[1]->iBackgroundFlag) << 1) | ||
| 286 | | ((!pOUNeighbours[2]->iBackgroundFlag) << 2) | ((!pOUNeighbours[3]->iBackgroundFlag) << 3); | ||
| 287 | pBackgroundOU->iBackgroundFlag = !ForegroundDilation23Chroma (iNeighbourForegroundFlags, iChromaSampleStartPos, | ||
| 288 | iPicStrideUV, pBgdParam); | ||
| 289 | } | ||
| 290 | break; | ||
| 291 | default: | ||
| 292 | break; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | } | ||
| 296 | inline void CBackgroundDetection::BackgroundErosion (SBackgroundOU* pBackgroundOU, SBackgroundOU* pOUNeighbours[]) { | ||
| 297 | if (pBackgroundOU->iMaxDiffSubSd <= (BGD_OU_SIZE * Q_FACTOR)) { //BGD_OU_SIZE*BGD_OU_SIZE>>2 | ||
| 298 | int32_t iSumNeighBackgroundFlags = pOUNeighbours[0]->iBackgroundFlag + pOUNeighbours[1]->iBackgroundFlag + | ||
| 299 | pOUNeighbours[2]->iBackgroundFlag + pOUNeighbours[3]->iBackgroundFlag; | ||
| 300 | int32_t sumNbrBGsad = (pOUNeighbours[0]->iSAD & (-pOUNeighbours[0]->iBackgroundFlag)) + (pOUNeighbours[2]->iSAD & | ||
| 301 | (-pOUNeighbours[2]->iBackgroundFlag)) | ||
| 302 | + (pOUNeighbours[1]->iSAD & (-pOUNeighbours[1]->iBackgroundFlag)) + (pOUNeighbours[3]->iSAD & | ||
| 303 | (-pOUNeighbours[3]->iBackgroundFlag)); | ||
| 304 | if (pBackgroundOU->iSAD * iSumNeighBackgroundFlags <= (3 * sumNbrBGsad) >> 1) { | ||
| 305 | if (iSumNeighBackgroundFlags == 4) { | ||
| 306 | pBackgroundOU->iBackgroundFlag = 1; | ||
| 307 | } else { | ||
| 308 | if ((pOUNeighbours[0]->iBackgroundFlag & pOUNeighbours[1]->iBackgroundFlag) | ||
| 309 | || (pOUNeighbours[2]->iBackgroundFlag & pOUNeighbours[3]->iBackgroundFlag)) { | ||
| 310 | pBackgroundOU->iBackgroundFlag = !ForegroundDilation23Luma (pBackgroundOU, pOUNeighbours); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | inline void CBackgroundDetection::SetBackgroundMbFlag (int8_t* pBackgroundMbFlag, int32_t iPicWidthInMb, | ||
| 318 | int32_t iBackgroundMbFlag) { | ||
| 319 | *pBackgroundMbFlag = iBackgroundMbFlag; | ||
| 320 | } | ||
| 321 | |||
| 322 | inline void CBackgroundDetection::UpperOUForegroundCheck (SBackgroundOU* pCurOU, int8_t* pBackgroundMbFlag, | ||
| 323 | int32_t iPicWidthInOU, int32_t iPicWidthInMb) { | ||
| 324 | if (pCurOU->iSAD > BGD_OU_SIZE * Q_FACTOR) { | ||
| 325 | SBackgroundOU* pOU_L = pCurOU - 1; | ||
| 326 | SBackgroundOU* pOU_R = pCurOU + 1; | ||
| 327 | SBackgroundOU* pOU_U = pCurOU - iPicWidthInOU; | ||
| 328 | SBackgroundOU* pOU_D = pCurOU + iPicWidthInOU; | ||
| 329 | if (pOU_L->iBackgroundFlag + pOU_R->iBackgroundFlag + pOU_U->iBackgroundFlag + pOU_D->iBackgroundFlag <= 1) { | ||
| 330 | SetBackgroundMbFlag (pBackgroundMbFlag, iPicWidthInMb, 0); | ||
| 331 | pCurOU->iBackgroundFlag = 0; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | void CBackgroundDetection::ForegroundDilationAndBackgroundErosion (vBGDParam* pBgdParam) { | ||
| 337 | int32_t iPicStrideUV = pBgdParam->iStride[1]; | ||
| 338 | int32_t iPicWidthInOU = pBgdParam->iBgdWidth >> LOG2_BGD_OU_SIZE; | ||
| 339 | int32_t iPicHeightInOU = pBgdParam->iBgdHeight >> LOG2_BGD_OU_SIZE; | ||
| 340 | int32_t iOUStrideUV = iPicStrideUV << (LOG2_BGD_OU_SIZE - 1); | ||
| 341 | int32_t iPicWidthInMb = (15 + pBgdParam->iBgdWidth) >> 4; | ||
| 342 | |||
| 343 | SBackgroundOU* pBackgroundOU = pBgdParam->pOU_array; | ||
| 344 | int8_t* pVaaBackgroundMbFlag = (int8_t*)pBgdParam->pBackgroundMbFlag; | ||
| 345 | SBackgroundOU* pOUNeighbours[4];//0: left; 1: right; 2: top; 3: bottom | ||
| 346 | |||
| 347 | pBackgroundOU = pBgdParam->pOU_array; | ||
| 348 | pOUNeighbours[2] = pBackgroundOU;//top OU | ||
| 349 | for (int32_t j = 0; j < iPicHeightInOU; j ++) { | ||
| 350 | int8_t* pRowSkipFlag = pVaaBackgroundMbFlag; | ||
| 351 | pOUNeighbours[0] = pBackgroundOU;//left OU | ||
| 352 | pOUNeighbours[3] = pBackgroundOU + (iPicWidthInOU & ((j == iPicHeightInOU - 1) - 1)); //bottom OU | ||
| 353 | for (int32_t i = 0; i < iPicWidthInOU; i++) { | ||
| 354 | pOUNeighbours[1] = pBackgroundOU + (i < iPicWidthInOU - 1); //right OU | ||
| 355 | |||
| 356 | if (pBackgroundOU->iBackgroundFlag) | ||
| 357 | ForegroundDilation (pBackgroundOU, pOUNeighbours, pBgdParam, j * iOUStrideUV + (i << LOG2_BGD_OU_SIZE_UV)); | ||
| 358 | else | ||
| 359 | BackgroundErosion (pBackgroundOU, pOUNeighbours); | ||
| 360 | |||
| 361 | // check the up OU | ||
| 362 | if (j > 1 && i > 0 && i < iPicWidthInOU - 1 && pOUNeighbours[2]->iBackgroundFlag == 1) { | ||
| 363 | UpperOUForegroundCheck (pOUNeighbours[2], pRowSkipFlag - OU_SIZE_IN_MB * iPicWidthInMb, iPicWidthInOU, iPicWidthInMb); | ||
| 364 | } | ||
| 365 | |||
| 366 | SetBackgroundMbFlag (pRowSkipFlag, iPicWidthInMb, pBackgroundOU->iBackgroundFlag); | ||
| 367 | |||
| 368 | // preparation for the next OU | ||
| 369 | pRowSkipFlag += OU_SIZE_IN_MB; | ||
| 370 | pOUNeighbours[0] = pBackgroundOU; | ||
| 371 | pOUNeighbours[2]++; | ||
| 372 | pOUNeighbours[3]++; | ||
| 373 | pBackgroundOU++; | ||
| 374 | } | ||
| 375 | pOUNeighbours[2] = pBackgroundOU - iPicWidthInOU; | ||
| 376 | pVaaBackgroundMbFlag += OU_SIZE_IN_MB * iPicWidthInMb; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | void CBackgroundDetection::BackgroundDetection (vBGDParam* pBgdParam) { | ||
| 381 | // 1st step: foreground/background coarse division | ||
| 382 | ForegroundBackgroundDivision (pBgdParam); | ||
| 383 | |||
| 384 | // 2nd step: foreground dilation and background erosion | ||
| 385 | ForegroundDilationAndBackgroundErosion (pBgdParam); | ||
| 386 | } | ||
| 387 | |||
| 388 | WELSVP_NAMESPACE_END | ||