[313] | 1 | /* The copyright in this software is being made available under the BSD |
---|
| 2 | * License, included below. This software may be subject to other third party |
---|
| 3 | * and contributor rights, including patent rights, and no such rights are |
---|
[1029] | 4 | * granted under this license. |
---|
[313] | 5 | * |
---|
[1259] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[313] | 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright notice, |
---|
| 13 | * this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 15 | * this list of conditions and the following disclaimer in the documentation |
---|
| 16 | * and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
| 18 | * be used to endorse or promote products derived from this software without |
---|
| 19 | * specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
| 31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** \file TComSampleAdaptiveOffset.cpp |
---|
| 35 | \brief sample adaptive offset class |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TComSampleAdaptiveOffset.h" |
---|
| 39 | #include <string.h> |
---|
| 40 | #include <stdlib.h> |
---|
| 41 | #include <stdio.h> |
---|
| 42 | #include <math.h> |
---|
| 43 | |
---|
| 44 | //! \ingroup TLibCommon |
---|
| 45 | //! \{ |
---|
| 46 | |
---|
[540] | 47 | SAOOffset::SAOOffset() |
---|
[1029] | 48 | { |
---|
[540] | 49 | reset(); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | SAOOffset::~SAOOffset() |
---|
| 53 | { |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | Void SAOOffset::reset() |
---|
| 58 | { |
---|
| 59 | modeIdc = SAO_MODE_OFF; |
---|
| 60 | typeIdc = -1; |
---|
| 61 | typeAuxInfo = -1; |
---|
| 62 | ::memset(offset, 0, sizeof(Int)* MAX_NUM_SAO_CLASSES); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | const SAOOffset& SAOOffset::operator= (const SAOOffset& src) |
---|
| 66 | { |
---|
| 67 | modeIdc = src.modeIdc; |
---|
| 68 | typeIdc = src.typeIdc; |
---|
| 69 | typeAuxInfo = src.typeAuxInfo; |
---|
| 70 | ::memcpy(offset, src.offset, sizeof(Int)* MAX_NUM_SAO_CLASSES); |
---|
| 71 | |
---|
| 72 | return *this; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | SAOBlkParam::SAOBlkParam() |
---|
| 77 | { |
---|
| 78 | reset(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | SAOBlkParam::~SAOBlkParam() |
---|
| 82 | { |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | Void SAOBlkParam::reset() |
---|
| 87 | { |
---|
[1029] | 88 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 89 | { |
---|
| 90 | offsetParam[compIdx].reset(); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | const SAOBlkParam& SAOBlkParam::operator= (const SAOBlkParam& src) |
---|
| 95 | { |
---|
[1029] | 96 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 97 | { |
---|
| 98 | offsetParam[compIdx] = src.offsetParam[compIdx]; |
---|
| 99 | } |
---|
| 100 | return *this; |
---|
| 101 | |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | TComSampleAdaptiveOffset::TComSampleAdaptiveOffset() |
---|
| 105 | { |
---|
| 106 | m_tempPicYuv = NULL; |
---|
| 107 | m_lineBufWidth = 0; |
---|
| 108 | m_signLineBuf1 = NULL; |
---|
| 109 | m_signLineBuf2 = NULL; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | TComSampleAdaptiveOffset::~TComSampleAdaptiveOffset() |
---|
| 114 | { |
---|
| 115 | destroy(); |
---|
[1029] | 116 | |
---|
[1246] | 117 | if (m_signLineBuf1) |
---|
| 118 | { |
---|
| 119 | delete[] m_signLineBuf1; |
---|
| 120 | m_signLineBuf1 = NULL; |
---|
| 121 | } |
---|
| 122 | if (m_signLineBuf2) |
---|
| 123 | { |
---|
| 124 | delete[] m_signLineBuf2; |
---|
| 125 | m_signLineBuf2 = NULL; |
---|
| 126 | } |
---|
[540] | 127 | } |
---|
| 128 | |
---|
[1029] | 129 | Void TComSampleAdaptiveOffset::create( Int picWidth, Int picHeight, ChromaFormat format, UInt maxCUWidth, UInt maxCUHeight, UInt maxCUDepth, UInt lumaBitShift, UInt chromaBitShift ) |
---|
[540] | 130 | { |
---|
| 131 | destroy(); |
---|
| 132 | |
---|
[1029] | 133 | m_picWidth = picWidth; |
---|
| 134 | m_picHeight = picHeight; |
---|
| 135 | m_chromaFormatIDC = format; |
---|
| 136 | m_maxCUWidth = maxCUWidth; |
---|
| 137 | m_maxCUHeight = maxCUHeight; |
---|
[540] | 138 | |
---|
[1029] | 139 | m_numCTUInWidth = (m_picWidth/m_maxCUWidth) + ((m_picWidth % m_maxCUWidth)?1:0); |
---|
| 140 | m_numCTUInHeight = (m_picHeight/m_maxCUHeight) + ((m_picHeight % m_maxCUHeight)?1:0); |
---|
| 141 | m_numCTUsPic = m_numCTUInHeight*m_numCTUInWidth; |
---|
[540] | 142 | |
---|
| 143 | //temporary picture buffer |
---|
| 144 | if ( !m_tempPicYuv ) |
---|
| 145 | { |
---|
| 146 | m_tempPicYuv = new TComPicYuv; |
---|
[1289] | 147 | m_tempPicYuv->create( m_picWidth, m_picHeight, m_chromaFormatIDC, m_maxCUWidth, m_maxCUHeight, maxCUDepth, true ); |
---|
[540] | 148 | } |
---|
| 149 | |
---|
| 150 | //bit-depth related |
---|
[1029] | 151 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 152 | { |
---|
[1029] | 153 | m_offsetStepLog2 [compIdx] = isLuma(ComponentID(compIdx))? lumaBitShift : chromaBitShift; |
---|
[540] | 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | Void TComSampleAdaptiveOffset::destroy() |
---|
| 158 | { |
---|
| 159 | if ( m_tempPicYuv ) |
---|
| 160 | { |
---|
| 161 | m_tempPicYuv->destroy(); |
---|
| 162 | delete m_tempPicYuv; |
---|
| 163 | m_tempPicYuv = NULL; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
[1029] | 167 | Void TComSampleAdaptiveOffset::invertQuantOffsets(ComponentID compIdx, Int typeIdc, Int typeAuxInfo, Int* dstOffsets, Int* srcOffsets) |
---|
[540] | 168 | { |
---|
| 169 | Int codedOffset[MAX_NUM_SAO_CLASSES]; |
---|
| 170 | |
---|
| 171 | ::memcpy(codedOffset, srcOffsets, sizeof(Int)*MAX_NUM_SAO_CLASSES); |
---|
| 172 | ::memset(dstOffsets, 0, sizeof(Int)*MAX_NUM_SAO_CLASSES); |
---|
| 173 | |
---|
| 174 | if(typeIdc == SAO_TYPE_START_BO) |
---|
| 175 | { |
---|
| 176 | for(Int i=0; i< 4; i++) |
---|
| 177 | { |
---|
| 178 | dstOffsets[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES] = codedOffset[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES]*(1<<m_offsetStepLog2[compIdx]); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | else //EO |
---|
| 182 | { |
---|
| 183 | for(Int i=0; i< NUM_SAO_EO_CLASSES; i++) |
---|
| 184 | { |
---|
| 185 | dstOffsets[i] = codedOffset[i] *(1<<m_offsetStepLog2[compIdx]); |
---|
| 186 | } |
---|
| 187 | assert(dstOffsets[SAO_CLASS_EO_PLAIN] == 0); //keep EO plain offset as zero |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | |
---|
[1029] | 192 | Int TComSampleAdaptiveOffset::getMergeList(TComPic* pic, Int ctuRsAddr, SAOBlkParam* blkParams, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES]) |
---|
[540] | 193 | { |
---|
[1029] | 194 | Int ctuX = ctuRsAddr % m_numCTUInWidth; |
---|
| 195 | Int ctuY = ctuRsAddr / m_numCTUInWidth; |
---|
[540] | 196 | Int mergedCTUPos; |
---|
| 197 | Int numValidMergeCandidates = 0; |
---|
| 198 | |
---|
| 199 | for(Int mergeType=0; mergeType< NUM_SAO_MERGE_TYPES; mergeType++) |
---|
| 200 | { |
---|
| 201 | SAOBlkParam* mergeCandidate = NULL; |
---|
| 202 | |
---|
| 203 | switch(mergeType) |
---|
| 204 | { |
---|
| 205 | case SAO_MERGE_ABOVE: |
---|
| 206 | { |
---|
| 207 | if(ctuY > 0) |
---|
| 208 | { |
---|
[1029] | 209 | mergedCTUPos = ctuRsAddr- m_numCTUInWidth; |
---|
| 210 | if( pic->getSAOMergeAvailability(ctuRsAddr, mergedCTUPos) ) |
---|
[540] | 211 | { |
---|
| 212 | mergeCandidate = &(blkParams[mergedCTUPos]); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | break; |
---|
| 217 | case SAO_MERGE_LEFT: |
---|
| 218 | { |
---|
| 219 | if(ctuX > 0) |
---|
| 220 | { |
---|
[1029] | 221 | mergedCTUPos = ctuRsAddr- 1; |
---|
| 222 | if( pic->getSAOMergeAvailability(ctuRsAddr, mergedCTUPos) ) |
---|
[540] | 223 | { |
---|
| 224 | mergeCandidate = &(blkParams[mergedCTUPos]); |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | break; |
---|
| 229 | default: |
---|
| 230 | { |
---|
| 231 | printf("not a supported merge type"); |
---|
| 232 | assert(0); |
---|
| 233 | exit(-1); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[1029] | 237 | mergeList[mergeType]=mergeCandidate; |
---|
[540] | 238 | if (mergeCandidate != NULL) |
---|
| 239 | { |
---|
| 240 | numValidMergeCandidates++; |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | return numValidMergeCandidates; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
[1029] | 248 | Void TComSampleAdaptiveOffset::reconstructBlkSAOParam(SAOBlkParam& recParam, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES]) |
---|
[540] | 249 | { |
---|
[1029] | 250 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 251 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 252 | { |
---|
[1029] | 253 | const ComponentID component = ComponentID(compIdx); |
---|
| 254 | SAOOffset& offsetParam = recParam[component]; |
---|
[540] | 255 | |
---|
| 256 | if(offsetParam.modeIdc == SAO_MODE_OFF) |
---|
| 257 | { |
---|
| 258 | continue; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | switch(offsetParam.modeIdc) |
---|
| 262 | { |
---|
| 263 | case SAO_MODE_NEW: |
---|
| 264 | { |
---|
[1029] | 265 | invertQuantOffsets(component, offsetParam.typeIdc, offsetParam.typeAuxInfo, offsetParam.offset, offsetParam.offset); |
---|
[540] | 266 | } |
---|
| 267 | break; |
---|
| 268 | case SAO_MODE_MERGE: |
---|
| 269 | { |
---|
| 270 | SAOBlkParam* mergeTarget = mergeList[offsetParam.typeIdc]; |
---|
| 271 | assert(mergeTarget != NULL); |
---|
| 272 | |
---|
[1029] | 273 | offsetParam = (*mergeTarget)[component]; |
---|
[540] | 274 | } |
---|
| 275 | break; |
---|
| 276 | default: |
---|
| 277 | { |
---|
| 278 | printf("Not a supported mode"); |
---|
| 279 | assert(0); |
---|
| 280 | exit(-1); |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | Void TComSampleAdaptiveOffset::reconstructBlkSAOParams(TComPic* pic, SAOBlkParam* saoBlkParams) |
---|
| 287 | { |
---|
[1029] | 288 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
| 289 | { |
---|
| 290 | m_picSAOEnabled[compIdx] = false; |
---|
| 291 | } |
---|
[540] | 292 | |
---|
[1029] | 293 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 294 | |
---|
| 295 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
---|
[540] | 296 | { |
---|
[1029] | 297 | SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES] = { NULL }; |
---|
| 298 | getMergeList(pic, ctuRsAddr, saoBlkParams, mergeList); |
---|
[540] | 299 | |
---|
[1029] | 300 | reconstructBlkSAOParam(saoBlkParams[ctuRsAddr], mergeList); |
---|
[540] | 301 | |
---|
[1029] | 302 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 303 | { |
---|
[1029] | 304 | if(saoBlkParams[ctuRsAddr][compIdx].modeIdc != SAO_MODE_OFF) |
---|
[540] | 305 | { |
---|
| 306 | m_picSAOEnabled[compIdx] = true; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | |
---|
[1287] | 313 | Void TComSampleAdaptiveOffset::offsetBlock(const Int channelBitDepth, Int typeIdx, Int* offset |
---|
[540] | 314 | , Pel* srcBlk, Pel* resBlk, Int srcStride, Int resStride, Int width, Int height |
---|
| 315 | , Bool isLeftAvail, Bool isRightAvail, Bool isAboveAvail, Bool isBelowAvail, Bool isAboveLeftAvail, Bool isAboveRightAvail, Bool isBelowLeftAvail, Bool isBelowRightAvail) |
---|
| 316 | { |
---|
| 317 | if(m_lineBufWidth != m_maxCUWidth) |
---|
| 318 | { |
---|
| 319 | m_lineBufWidth = m_maxCUWidth; |
---|
[1029] | 320 | |
---|
[1246] | 321 | if (m_signLineBuf1) |
---|
| 322 | { |
---|
| 323 | delete[] m_signLineBuf1; |
---|
| 324 | m_signLineBuf1 = NULL; |
---|
| 325 | } |
---|
[540] | 326 | m_signLineBuf1 = new Char[m_lineBufWidth+1]; |
---|
[1029] | 327 | |
---|
[1246] | 328 | if (m_signLineBuf2) |
---|
| 329 | { |
---|
| 330 | delete[] m_signLineBuf2; |
---|
| 331 | m_signLineBuf2 = NULL; |
---|
| 332 | } |
---|
[540] | 333 | m_signLineBuf2 = new Char[m_lineBufWidth+1]; |
---|
| 334 | } |
---|
| 335 | |
---|
[1287] | 336 | const Int maxSampleValueIncl = (1<< channelBitDepth )-1; |
---|
[540] | 337 | |
---|
| 338 | Int x,y, startX, startY, endX, endY, edgeType; |
---|
| 339 | Int firstLineStartX, firstLineEndX, lastLineStartX, lastLineEndX; |
---|
| 340 | Char signLeft, signRight, signDown; |
---|
| 341 | |
---|
| 342 | Pel* srcLine = srcBlk; |
---|
| 343 | Pel* resLine = resBlk; |
---|
| 344 | |
---|
| 345 | switch(typeIdx) |
---|
| 346 | { |
---|
| 347 | case SAO_TYPE_EO_0: |
---|
| 348 | { |
---|
| 349 | offset += 2; |
---|
| 350 | startX = isLeftAvail ? 0 : 1; |
---|
| 351 | endX = isRightAvail ? width : (width -1); |
---|
| 352 | for (y=0; y< height; y++) |
---|
| 353 | { |
---|
[713] | 354 | signLeft = (Char)sgn(srcLine[startX] - srcLine[startX-1]); |
---|
[540] | 355 | for (x=startX; x< endX; x++) |
---|
| 356 | { |
---|
[713] | 357 | signRight = (Char)sgn(srcLine[x] - srcLine[x+1]); |
---|
[540] | 358 | edgeType = signRight + signLeft; |
---|
| 359 | signLeft = -signRight; |
---|
| 360 | |
---|
[1029] | 361 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
[540] | 362 | } |
---|
| 363 | srcLine += srcStride; |
---|
| 364 | resLine += resStride; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | } |
---|
| 368 | break; |
---|
| 369 | case SAO_TYPE_EO_90: |
---|
| 370 | { |
---|
| 371 | offset += 2; |
---|
| 372 | Char *signUpLine = m_signLineBuf1; |
---|
| 373 | |
---|
| 374 | startY = isAboveAvail ? 0 : 1; |
---|
| 375 | endY = isBelowAvail ? height : height-1; |
---|
| 376 | if (!isAboveAvail) |
---|
| 377 | { |
---|
| 378 | srcLine += srcStride; |
---|
| 379 | resLine += resStride; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | Pel* srcLineAbove= srcLine- srcStride; |
---|
| 383 | for (x=0; x< width; x++) |
---|
| 384 | { |
---|
[713] | 385 | signUpLine[x] = (Char)sgn(srcLine[x] - srcLineAbove[x]); |
---|
[540] | 386 | } |
---|
| 387 | |
---|
| 388 | Pel* srcLineBelow; |
---|
| 389 | for (y=startY; y<endY; y++) |
---|
| 390 | { |
---|
| 391 | srcLineBelow= srcLine+ srcStride; |
---|
| 392 | |
---|
| 393 | for (x=0; x< width; x++) |
---|
| 394 | { |
---|
[713] | 395 | signDown = (Char)sgn(srcLine[x] - srcLineBelow[x]); |
---|
[540] | 396 | edgeType = signDown + signUpLine[x]; |
---|
| 397 | signUpLine[x]= -signDown; |
---|
| 398 | |
---|
[1029] | 399 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
[540] | 400 | } |
---|
| 401 | srcLine += srcStride; |
---|
| 402 | resLine += resStride; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | } |
---|
| 406 | break; |
---|
| 407 | case SAO_TYPE_EO_135: |
---|
| 408 | { |
---|
| 409 | offset += 2; |
---|
| 410 | Char *signUpLine, *signDownLine, *signTmpLine; |
---|
| 411 | |
---|
| 412 | signUpLine = m_signLineBuf1; |
---|
| 413 | signDownLine= m_signLineBuf2; |
---|
| 414 | |
---|
| 415 | startX = isLeftAvail ? 0 : 1 ; |
---|
| 416 | endX = isRightAvail ? width : (width-1); |
---|
| 417 | |
---|
| 418 | //prepare 2nd line's upper sign |
---|
| 419 | Pel* srcLineBelow= srcLine+ srcStride; |
---|
| 420 | for (x=startX; x< endX+1; x++) |
---|
| 421 | { |
---|
[713] | 422 | signUpLine[x] = (Char)sgn(srcLineBelow[x] - srcLine[x- 1]); |
---|
[540] | 423 | } |
---|
| 424 | |
---|
| 425 | //1st line |
---|
| 426 | Pel* srcLineAbove= srcLine- srcStride; |
---|
| 427 | firstLineStartX = isAboveLeftAvail ? 0 : 1; |
---|
| 428 | firstLineEndX = isAboveAvail? endX: 1; |
---|
| 429 | for(x= firstLineStartX; x< firstLineEndX; x++) |
---|
| 430 | { |
---|
[713] | 431 | edgeType = sgn(srcLine[x] - srcLineAbove[x- 1]) - signUpLine[x+1]; |
---|
[1029] | 432 | |
---|
| 433 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
[540] | 434 | } |
---|
| 435 | srcLine += srcStride; |
---|
| 436 | resLine += resStride; |
---|
| 437 | |
---|
| 438 | |
---|
| 439 | //middle lines |
---|
| 440 | for (y= 1; y< height-1; y++) |
---|
| 441 | { |
---|
| 442 | srcLineBelow= srcLine+ srcStride; |
---|
| 443 | |
---|
| 444 | for (x=startX; x<endX; x++) |
---|
| 445 | { |
---|
[713] | 446 | signDown = (Char)sgn(srcLine[x] - srcLineBelow[x+ 1]); |
---|
[540] | 447 | edgeType = signDown + signUpLine[x]; |
---|
[1029] | 448 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
[540] | 449 | |
---|
[1029] | 450 | signDownLine[x+1] = -signDown; |
---|
[540] | 451 | } |
---|
[713] | 452 | signDownLine[startX] = (Char)sgn(srcLineBelow[startX] - srcLine[startX-1]); |
---|
[540] | 453 | |
---|
| 454 | signTmpLine = signUpLine; |
---|
| 455 | signUpLine = signDownLine; |
---|
| 456 | signDownLine = signTmpLine; |
---|
| 457 | |
---|
| 458 | srcLine += srcStride; |
---|
| 459 | resLine += resStride; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | //last line |
---|
| 463 | srcLineBelow= srcLine+ srcStride; |
---|
| 464 | lastLineStartX = isBelowAvail ? startX : (width -1); |
---|
| 465 | lastLineEndX = isBelowRightAvail ? width : (width -1); |
---|
| 466 | for(x= lastLineStartX; x< lastLineEndX; x++) |
---|
| 467 | { |
---|
[713] | 468 | edgeType = sgn(srcLine[x] - srcLineBelow[x+ 1]) + signUpLine[x]; |
---|
[1029] | 469 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
[540] | 470 | |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | break; |
---|
| 474 | case SAO_TYPE_EO_45: |
---|
| 475 | { |
---|
| 476 | offset += 2; |
---|
| 477 | Char *signUpLine = m_signLineBuf1+1; |
---|
| 478 | |
---|
| 479 | startX = isLeftAvail ? 0 : 1; |
---|
| 480 | endX = isRightAvail ? width : (width -1); |
---|
| 481 | |
---|
| 482 | //prepare 2nd line upper sign |
---|
| 483 | Pel* srcLineBelow= srcLine+ srcStride; |
---|
| 484 | for (x=startX-1; x< endX; x++) |
---|
| 485 | { |
---|
[713] | 486 | signUpLine[x] = (Char)sgn(srcLineBelow[x] - srcLine[x+1]); |
---|
[540] | 487 | } |
---|
| 488 | |
---|
| 489 | |
---|
| 490 | //first line |
---|
| 491 | Pel* srcLineAbove= srcLine- srcStride; |
---|
| 492 | firstLineStartX = isAboveAvail ? startX : (width -1 ); |
---|
| 493 | firstLineEndX = isAboveRightAvail ? width : (width-1); |
---|
| 494 | for(x= firstLineStartX; x< firstLineEndX; x++) |
---|
| 495 | { |
---|
[713] | 496 | edgeType = sgn(srcLine[x] - srcLineAbove[x+1]) -signUpLine[x-1]; |
---|
[1029] | 497 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
[540] | 498 | } |
---|
| 499 | srcLine += srcStride; |
---|
| 500 | resLine += resStride; |
---|
| 501 | |
---|
| 502 | //middle lines |
---|
| 503 | for (y= 1; y< height-1; y++) |
---|
| 504 | { |
---|
| 505 | srcLineBelow= srcLine+ srcStride; |
---|
| 506 | |
---|
| 507 | for(x= startX; x< endX; x++) |
---|
| 508 | { |
---|
[713] | 509 | signDown = (Char)sgn(srcLine[x] - srcLineBelow[x-1]); |
---|
[540] | 510 | edgeType = signDown + signUpLine[x]; |
---|
[1029] | 511 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
| 512 | signUpLine[x-1] = -signDown; |
---|
[540] | 513 | } |
---|
[713] | 514 | signUpLine[endX-1] = (Char)sgn(srcLineBelow[endX-1] - srcLine[endX]); |
---|
[540] | 515 | srcLine += srcStride; |
---|
| 516 | resLine += resStride; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | //last line |
---|
| 520 | srcLineBelow= srcLine+ srcStride; |
---|
| 521 | lastLineStartX = isBelowLeftAvail ? 0 : 1; |
---|
| 522 | lastLineEndX = isBelowAvail ? endX : 1; |
---|
| 523 | for(x= lastLineStartX; x< lastLineEndX; x++) |
---|
| 524 | { |
---|
[713] | 525 | edgeType = sgn(srcLine[x] - srcLineBelow[x-1]) + signUpLine[x]; |
---|
[1029] | 526 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
[540] | 527 | |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | break; |
---|
| 531 | case SAO_TYPE_BO: |
---|
| 532 | { |
---|
[1287] | 533 | const Int shiftBits = channelBitDepth - NUM_SAO_BO_CLASSES_LOG2; |
---|
[540] | 534 | for (y=0; y< height; y++) |
---|
| 535 | { |
---|
| 536 | for (x=0; x< width; x++) |
---|
| 537 | { |
---|
[1029] | 538 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[srcLine[x] >> shiftBits] ); |
---|
[540] | 539 | } |
---|
| 540 | srcLine += srcStride; |
---|
| 541 | resLine += resStride; |
---|
| 542 | } |
---|
| 543 | } |
---|
| 544 | break; |
---|
| 545 | default: |
---|
| 546 | { |
---|
| 547 | printf("Not a supported SAO types\n"); |
---|
| 548 | assert(0); |
---|
| 549 | exit(-1); |
---|
| 550 | } |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | |
---|
[1029] | 554 | Void TComSampleAdaptiveOffset::offsetCTU(Int ctuRsAddr, TComPicYuv* srcYuv, TComPicYuv* resYuv, SAOBlkParam& saoblkParam, TComPic* pPic) |
---|
[540] | 555 | { |
---|
| 556 | Bool isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail; |
---|
| 557 | |
---|
[1029] | 558 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 559 | Bool bAllOff=true; |
---|
| 560 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 561 | { |
---|
[1246] | 562 | if (saoblkParam[compIdx].modeIdc != SAO_MODE_OFF) |
---|
| 563 | { |
---|
| 564 | bAllOff=false; |
---|
| 565 | } |
---|
[540] | 566 | } |
---|
[1246] | 567 | if (bAllOff) |
---|
| 568 | { |
---|
| 569 | return; |
---|
| 570 | } |
---|
[540] | 571 | |
---|
| 572 | //block boundary availability |
---|
[1029] | 573 | pPic->getPicSym()->deriveLoopFilterBoundaryAvailibility(ctuRsAddr, isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail); |
---|
[540] | 574 | |
---|
[1029] | 575 | Int yPos = (ctuRsAddr / m_numCTUInWidth)*m_maxCUHeight; |
---|
| 576 | Int xPos = (ctuRsAddr % m_numCTUInWidth)*m_maxCUWidth; |
---|
[540] | 577 | Int height = (yPos + m_maxCUHeight > m_picHeight)?(m_picHeight- yPos):m_maxCUHeight; |
---|
| 578 | Int width = (xPos + m_maxCUWidth > m_picWidth )?(m_picWidth - xPos):m_maxCUWidth; |
---|
| 579 | |
---|
[1029] | 580 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 581 | { |
---|
[1029] | 582 | const ComponentID component = ComponentID(compIdx); |
---|
[540] | 583 | SAOOffset& ctbOffset = saoblkParam[compIdx]; |
---|
| 584 | |
---|
| 585 | if(ctbOffset.modeIdc != SAO_MODE_OFF) |
---|
| 586 | { |
---|
[1029] | 587 | const UInt componentScaleX = getComponentScaleX(component, pPic->getChromaFormat()); |
---|
| 588 | const UInt componentScaleY = getComponentScaleY(component, pPic->getChromaFormat()); |
---|
[540] | 589 | |
---|
[1029] | 590 | Int blkWidth = (width >> componentScaleX); |
---|
| 591 | Int blkHeight = (height >> componentScaleY); |
---|
| 592 | Int blkXPos = (xPos >> componentScaleX); |
---|
| 593 | Int blkYPos = (yPos >> componentScaleY); |
---|
[540] | 594 | |
---|
[1029] | 595 | Int srcStride = srcYuv->getStride(component); |
---|
| 596 | Pel* srcBlk = srcYuv->getAddr(component) + blkYPos*srcStride + blkXPos; |
---|
[540] | 597 | |
---|
[1029] | 598 | Int resStride = resYuv->getStride(component); |
---|
| 599 | Pel* resBlk = resYuv->getAddr(component) + blkYPos*resStride + blkXPos; |
---|
[540] | 600 | |
---|
[1287] | 601 | #if SVC_EXTENSION |
---|
| 602 | offsetBlock( pPic->getSlice(0)->getBitDepth(toChannelType(component)), ctbOffset.typeIdc, ctbOffset.offset |
---|
| 603 | #else |
---|
| 604 | offsetBlock( pPic->getPicSym()->getSPS().getBitDepth(toChannelType(component)), ctbOffset.typeIdc, ctbOffset.offset |
---|
| 605 | #endif |
---|
[540] | 606 | , srcBlk, resBlk, srcStride, resStride, blkWidth, blkHeight |
---|
| 607 | , isLeftAvail, isRightAvail |
---|
| 608 | , isAboveAvail, isBelowAvail |
---|
| 609 | , isAboveLeftAvail, isAboveRightAvail |
---|
| 610 | , isBelowLeftAvail, isBelowRightAvail |
---|
| 611 | ); |
---|
| 612 | } |
---|
| 613 | } //compIdx |
---|
| 614 | |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | |
---|
| 618 | Void TComSampleAdaptiveOffset::SAOProcess(TComPic* pDecPic) |
---|
| 619 | { |
---|
[1029] | 620 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 621 | Bool bAllDisabled=true; |
---|
| 622 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 623 | { |
---|
[1246] | 624 | if (m_picSAOEnabled[compIdx]) |
---|
| 625 | { |
---|
| 626 | bAllDisabled=false; |
---|
| 627 | } |
---|
[540] | 628 | } |
---|
[1246] | 629 | if (bAllDisabled) |
---|
| 630 | { |
---|
| 631 | return; |
---|
| 632 | } |
---|
[1029] | 633 | |
---|
[540] | 634 | TComPicYuv* resYuv = pDecPic->getPicYuvRec(); |
---|
| 635 | TComPicYuv* srcYuv = m_tempPicYuv; |
---|
| 636 | resYuv->copyToPic(srcYuv); |
---|
[1029] | 637 | for(Int ctuRsAddr= 0; ctuRsAddr < m_numCTUsPic; ctuRsAddr++) |
---|
[540] | 638 | { |
---|
[1029] | 639 | offsetCTU(ctuRsAddr, srcYuv, resYuv, (pDecPic->getPicSym()->getSAOBlkParam())[ctuRsAddr], pDecPic); |
---|
[540] | 640 | } //ctu |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | |
---|
[1029] | 644 | /** PCM LF disable process. |
---|
[313] | 645 | * \param pcPic picture (TComPic) pointer |
---|
| 646 | * |
---|
| 647 | * \note Replace filtered sample values of PCM mode blocks with the transmitted and reconstructed ones. |
---|
| 648 | */ |
---|
| 649 | Void TComSampleAdaptiveOffset::PCMLFDisableProcess (TComPic* pcPic) |
---|
| 650 | { |
---|
| 651 | xPCMRestoration(pcPic); |
---|
| 652 | } |
---|
| 653 | |
---|
[1029] | 654 | /** Picture-level PCM restoration. |
---|
[313] | 655 | * \param pcPic picture (TComPic) pointer |
---|
| 656 | */ |
---|
| 657 | Void TComSampleAdaptiveOffset::xPCMRestoration(TComPic* pcPic) |
---|
| 658 | { |
---|
| 659 | Bool bPCMFilter = (pcPic->getSlice(0)->getSPS()->getUsePCM() && pcPic->getSlice(0)->getSPS()->getPCMFilterDisableFlag())? true : false; |
---|
| 660 | |
---|
| 661 | if(bPCMFilter || pcPic->getSlice(0)->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 662 | { |
---|
[1029] | 663 | for( UInt ctuRsAddr = 0; ctuRsAddr < pcPic->getNumberOfCtusInFrame() ; ctuRsAddr++ ) |
---|
[313] | 664 | { |
---|
[1029] | 665 | TComDataCU* pcCU = pcPic->getCtu(ctuRsAddr); |
---|
[313] | 666 | |
---|
[1029] | 667 | xPCMCURestoration(pcCU, 0, 0); |
---|
| 668 | } |
---|
[313] | 669 | } |
---|
| 670 | } |
---|
| 671 | |
---|
[1029] | 672 | /** PCM CU restoration. |
---|
[1260] | 673 | * \param pcCU pointer to current CU |
---|
| 674 | * \param uiAbsZorderIdx part index |
---|
| 675 | * \param uiDepth CU depth |
---|
[313] | 676 | */ |
---|
| 677 | Void TComSampleAdaptiveOffset::xPCMCURestoration ( TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth ) |
---|
| 678 | { |
---|
| 679 | TComPic* pcPic = pcCU->getPic(); |
---|
[1029] | 680 | UInt uiCurNumParts = pcPic->getNumPartitionsInCtu() >> (uiDepth<<1); |
---|
[313] | 681 | UInt uiQNumParts = uiCurNumParts>>2; |
---|
| 682 | |
---|
| 683 | // go to sub-CU |
---|
| 684 | if( pcCU->getDepth(uiAbsZorderIdx) > uiDepth ) |
---|
| 685 | { |
---|
| 686 | for ( UInt uiPartIdx = 0; uiPartIdx < 4; uiPartIdx++, uiAbsZorderIdx+=uiQNumParts ) |
---|
| 687 | { |
---|
| 688 | UInt uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsZorderIdx] ]; |
---|
| 689 | UInt uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsZorderIdx] ]; |
---|
[1203] | 690 | #if SVC_EXTENSION |
---|
[442] | 691 | if( ( uiLPelX < pcCU->getSlice()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getPicHeightInLumaSamples() ) ) |
---|
| 692 | #else |
---|
[313] | 693 | if( ( uiLPelX < pcCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
[442] | 694 | #endif |
---|
[1246] | 695 | { |
---|
[313] | 696 | xPCMCURestoration( pcCU, uiAbsZorderIdx, uiDepth+1 ); |
---|
[1246] | 697 | } |
---|
[313] | 698 | } |
---|
| 699 | return; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | // restore PCM samples |
---|
| 703 | if ((pcCU->getIPCMFlag(uiAbsZorderIdx)&& pcPic->getSlice(0)->getSPS()->getPCMFilterDisableFlag()) || pcCU->isLosslessCoded( uiAbsZorderIdx)) |
---|
| 704 | { |
---|
[1029] | 705 | const UInt numComponents=pcPic->getNumberValidComponents(); |
---|
| 706 | for(UInt comp=0; comp<numComponents; comp++) |
---|
| 707 | { |
---|
| 708 | xPCMSampleRestoration (pcCU, uiAbsZorderIdx, uiDepth, ComponentID(comp)); |
---|
| 709 | } |
---|
[313] | 710 | } |
---|
| 711 | } |
---|
| 712 | |
---|
[1029] | 713 | /** PCM sample restoration. |
---|
[1260] | 714 | * \param pcCU pointer to current CU |
---|
| 715 | * \param uiAbsZorderIdx part index |
---|
| 716 | * \param uiDepth CU depth |
---|
| 717 | * \param compID texture component type |
---|
[313] | 718 | */ |
---|
[1029] | 719 | Void TComSampleAdaptiveOffset::xPCMSampleRestoration (TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth, const ComponentID compID) |
---|
[313] | 720 | { |
---|
[1029] | 721 | TComPicYuv* pcPicYuvRec = pcCU->getPic()->getPicYuvRec(); |
---|
| 722 | UInt uiPcmLeftShiftBit; |
---|
| 723 | const UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight(); |
---|
| 724 | const UInt csx=pcPicYuvRec->getComponentScaleX(compID); |
---|
| 725 | const UInt csy=pcPicYuvRec->getComponentScaleY(compID); |
---|
| 726 | const UInt uiOffset = (uiMinCoeffSize*uiAbsZorderIdx)>>(csx+csy); |
---|
[313] | 727 | |
---|
[1029] | 728 | Pel *piSrc = pcPicYuvRec->getAddr(compID, pcCU->getCtuRsAddr(), uiAbsZorderIdx); |
---|
| 729 | const Pel *piPcm = pcCU->getPCMSample(compID) + uiOffset; |
---|
| 730 | const UInt uiStride = pcPicYuvRec->getStride(compID); |
---|
[1289] | 731 | const TComSPS &sps = *(pcCU->getSlice()->getSPS()); |
---|
| 732 | const UInt uiWidth = ((sps.getMaxCUWidth() >> uiDepth) >> csx); |
---|
| 733 | const UInt uiHeight = ((sps.getMaxCUHeight() >> uiDepth) >> csy); |
---|
[1029] | 734 | |
---|
| 735 | if ( pcCU->isLosslessCoded(uiAbsZorderIdx) && !pcCU->getIPCMFlag(uiAbsZorderIdx) ) |
---|
[313] | 736 | { |
---|
[1029] | 737 | uiPcmLeftShiftBit = 0; |
---|
[313] | 738 | } |
---|
| 739 | else |
---|
| 740 | { |
---|
[1287] | 741 | #if SVC_EXTENSION |
---|
| 742 | uiPcmLeftShiftBit = pcCU->getSlice()->getBitDepth(toChannelType(compID)) - pcCU->getSlice()->getSPS()->getPCMBitDepth(toChannelType(compID)); |
---|
| 743 | #else |
---|
[1289] | 744 | uiPcmLeftShiftBit = sps.getBitDepth(toChannelType(compID)) - sps.getPCMBitDepth(toChannelType(compID)); |
---|
[1287] | 745 | #endif |
---|
[313] | 746 | } |
---|
| 747 | |
---|
[1029] | 748 | for(UInt uiY = 0; uiY < uiHeight; uiY++ ) |
---|
[313] | 749 | { |
---|
[1029] | 750 | for(UInt uiX = 0; uiX < uiWidth; uiX++ ) |
---|
[313] | 751 | { |
---|
| 752 | piSrc[uiX] = (piPcm[uiX] << uiPcmLeftShiftBit); |
---|
| 753 | } |
---|
| 754 | piPcm += uiWidth; |
---|
| 755 | piSrc += uiStride; |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | //! \} |
---|