[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 | |
---|
[1029] | 34 | /** |
---|
[313] | 35 | \file TEncSampleAdaptiveOffset.cpp |
---|
| 36 | \brief estimation part of sample adaptive offset class |
---|
| 37 | */ |
---|
| 38 | #include "TEncSampleAdaptiveOffset.h" |
---|
| 39 | #include <string.h> |
---|
| 40 | #include <stdlib.h> |
---|
| 41 | #include <stdio.h> |
---|
| 42 | #include <math.h> |
---|
| 43 | |
---|
| 44 | //! \ingroup TLibEncoder |
---|
| 45 | //! \{ |
---|
| 46 | |
---|
[540] | 47 | |
---|
[1260] | 48 | //! rounding with IBDI |
---|
[540] | 49 | inline Double xRoundIbdi2(Int bitDepth, Double x) |
---|
| 50 | { |
---|
| 51 | return ((x)>0) ? (Int)(((Int)(x)+(1<<(bitDepth-8-1)))/(1<<(bitDepth-8))) : ((Int)(((Int)(x)-(1<<(bitDepth-8-1)))/(1<<(bitDepth-8)))); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | inline Double xRoundIbdi(Int bitDepth, Double x) |
---|
| 55 | { |
---|
| 56 | return (bitDepth > 8 ? xRoundIbdi2(bitDepth, (x)) : ((x)>=0 ? ((Int)((x)+0.5)) : ((Int)((x)-0.5)))) ; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
[313] | 60 | TEncSampleAdaptiveOffset::TEncSampleAdaptiveOffset() |
---|
| 61 | { |
---|
[1029] | 62 | m_pppcRDSbacCoder = NULL; |
---|
[540] | 63 | m_pcRDGoOnSbacCoder = NULL; |
---|
[1029] | 64 | m_pppcBinCoderCABAC = NULL; |
---|
[540] | 65 | m_statData = NULL; |
---|
| 66 | m_preDBFstatData = NULL; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | TEncSampleAdaptiveOffset::~TEncSampleAdaptiveOffset() |
---|
| 70 | { |
---|
| 71 | destroyEncData(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | Void TEncSampleAdaptiveOffset::createEncData(Bool isPreDBFSamplesUsed) |
---|
| 75 | { |
---|
| 76 | |
---|
| 77 | //cabac coder for RDO |
---|
| 78 | m_pppcRDSbacCoder = new TEncSbac* [NUM_SAO_CABACSTATE_LABELS]; |
---|
[1029] | 79 | #if FAST_BIT_EST |
---|
[540] | 80 | m_pppcBinCoderCABAC = new TEncBinCABACCounter* [NUM_SAO_CABACSTATE_LABELS]; |
---|
[1029] | 81 | #else |
---|
| 82 | m_pppcBinCoderCABAC = new TEncBinCABAC* [NUM_SAO_CABACSTATE_LABELS]; |
---|
| 83 | #endif |
---|
[540] | 84 | |
---|
| 85 | for(Int cs=0; cs < NUM_SAO_CABACSTATE_LABELS; cs++) |
---|
| 86 | { |
---|
| 87 | m_pppcRDSbacCoder[cs] = new TEncSbac; |
---|
[1029] | 88 | #if FAST_BIT_EST |
---|
[540] | 89 | m_pppcBinCoderCABAC[cs] = new TEncBinCABACCounter; |
---|
[1029] | 90 | #else |
---|
| 91 | m_pppcBinCoderCABAC[cs] = new TEncBinCABAC; |
---|
| 92 | #endif |
---|
[540] | 93 | m_pppcRDSbacCoder [cs]->init( m_pppcBinCoderCABAC [cs] ); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | //statistics |
---|
| 98 | m_statData = new SAOStatData**[m_numCTUsPic]; |
---|
| 99 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
| 100 | { |
---|
[1029] | 101 | m_statData[i] = new SAOStatData*[MAX_NUM_COMPONENT]; |
---|
| 102 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 103 | { |
---|
| 104 | m_statData[i][compIdx] = new SAOStatData[NUM_SAO_NEW_TYPES]; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | if(isPreDBFSamplesUsed) |
---|
| 108 | { |
---|
| 109 | m_preDBFstatData = new SAOStatData**[m_numCTUsPic]; |
---|
| 110 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
| 111 | { |
---|
[1029] | 112 | m_preDBFstatData[i] = new SAOStatData*[MAX_NUM_COMPONENT]; |
---|
| 113 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 114 | { |
---|
| 115 | m_preDBFstatData[i][compIdx] = new SAOStatData[NUM_SAO_NEW_TYPES]; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | ::memset(m_saoDisabledRate, 0, sizeof(m_saoDisabledRate)); |
---|
| 122 | |
---|
| 123 | for(Int typeIdc=0; typeIdc < NUM_SAO_NEW_TYPES; typeIdc++) |
---|
| 124 | { |
---|
[1029] | 125 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
---|
| 126 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
---|
[540] | 127 | |
---|
[1029] | 128 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
---|
| 129 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
---|
[540] | 130 | |
---|
| 131 | if(isPreDBFSamplesUsed) |
---|
| 132 | { |
---|
| 133 | switch(typeIdc) |
---|
| 134 | { |
---|
| 135 | case SAO_TYPE_EO_0: |
---|
| 136 | { |
---|
[1029] | 137 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
---|
| 138 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
---|
[540] | 139 | |
---|
[1029] | 140 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 3; |
---|
| 141 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 1; |
---|
[540] | 142 | } |
---|
| 143 | break; |
---|
| 144 | case SAO_TYPE_EO_90: |
---|
| 145 | { |
---|
[1029] | 146 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 4; |
---|
| 147 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 2; |
---|
[540] | 148 | |
---|
[1029] | 149 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
---|
| 150 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
---|
[540] | 151 | } |
---|
| 152 | break; |
---|
| 153 | case SAO_TYPE_EO_135: |
---|
| 154 | case SAO_TYPE_EO_45: |
---|
| 155 | { |
---|
[1029] | 156 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
---|
| 157 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
---|
[540] | 158 | |
---|
[1029] | 159 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
---|
| 160 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
---|
[540] | 161 | } |
---|
| 162 | break; |
---|
| 163 | case SAO_TYPE_BO: |
---|
| 164 | { |
---|
[1029] | 165 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 4; |
---|
| 166 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 2; |
---|
[540] | 167 | |
---|
[1029] | 168 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 3; |
---|
| 169 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 1; |
---|
[540] | 170 | } |
---|
| 171 | break; |
---|
| 172 | default: |
---|
| 173 | { |
---|
| 174 | printf("Not a supported type"); |
---|
| 175 | assert(0); |
---|
| 176 | exit(-1); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | Void TEncSampleAdaptiveOffset::destroyEncData() |
---|
| 185 | { |
---|
| 186 | if(m_pppcRDSbacCoder != NULL) |
---|
| 187 | { |
---|
| 188 | for (Int cs = 0; cs < NUM_SAO_CABACSTATE_LABELS; cs ++ ) |
---|
| 189 | { |
---|
| 190 | delete m_pppcRDSbacCoder[cs]; |
---|
| 191 | } |
---|
| 192 | delete[] m_pppcRDSbacCoder; m_pppcRDSbacCoder = NULL; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | if(m_pppcBinCoderCABAC != NULL) |
---|
| 196 | { |
---|
| 197 | for (Int cs = 0; cs < NUM_SAO_CABACSTATE_LABELS; cs ++ ) |
---|
| 198 | { |
---|
| 199 | delete m_pppcBinCoderCABAC[cs]; |
---|
| 200 | } |
---|
| 201 | delete[] m_pppcBinCoderCABAC; m_pppcBinCoderCABAC = NULL; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if(m_statData != NULL) |
---|
| 205 | { |
---|
| 206 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
| 207 | { |
---|
[1029] | 208 | for(Int compIdx=0; compIdx< MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 209 | { |
---|
| 210 | delete[] m_statData[i][compIdx]; |
---|
| 211 | } |
---|
| 212 | delete[] m_statData[i]; |
---|
| 213 | } |
---|
| 214 | delete[] m_statData; m_statData = NULL; |
---|
| 215 | } |
---|
| 216 | if(m_preDBFstatData != NULL) |
---|
| 217 | { |
---|
| 218 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
| 219 | { |
---|
[1029] | 220 | for(Int compIdx=0; compIdx< MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 221 | { |
---|
| 222 | delete[] m_preDBFstatData[i][compIdx]; |
---|
| 223 | } |
---|
| 224 | delete[] m_preDBFstatData[i]; |
---|
| 225 | } |
---|
| 226 | delete[] m_preDBFstatData; m_preDBFstatData = NULL; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
[1029] | 230 | Void TEncSampleAdaptiveOffset::initRDOCabacCoder(TEncSbac* pcRDGoOnSbacCoder, TComSlice* pcSlice) |
---|
[540] | 231 | { |
---|
| 232 | m_pcRDGoOnSbacCoder = pcRDGoOnSbacCoder; |
---|
[1291] | 233 | m_pcRDGoOnSbacCoder->resetEntropy(pcSlice); |
---|
[540] | 234 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
| 235 | |
---|
| 236 | m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[SAO_CABACSTATE_PIC_INIT]); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | |
---|
[1344] | 241 | Void TEncSampleAdaptiveOffset::SAOProcess(TComPic* pPic, Bool* sliceEnabled, const Double *lambdas, const Bool bTestSAODisableAtPictureLevel, const Double saoEncodingRate, const Double saoEncodingRateChroma, Bool isPreDBFSamplesUsed ) |
---|
[540] | 242 | { |
---|
| 243 | TComPicYuv* orgYuv= pPic->getPicYuvOrg(); |
---|
| 244 | TComPicYuv* resYuv= pPic->getPicYuvRec(); |
---|
[1029] | 245 | memcpy(m_lambda, lambdas, sizeof(m_lambda)); |
---|
[540] | 246 | TComPicYuv* srcYuv = m_tempPicYuv; |
---|
| 247 | resYuv->copyToPic(srcYuv); |
---|
| 248 | srcYuv->setBorderExtension(false); |
---|
| 249 | srcYuv->extendPicBorder(); |
---|
| 250 | |
---|
| 251 | //collect statistics |
---|
| 252 | getStatistics(m_statData, orgYuv, srcYuv, pPic); |
---|
| 253 | if(isPreDBFSamplesUsed) |
---|
| 254 | { |
---|
| 255 | addPreDBFStatistics(m_statData); |
---|
| 256 | } |
---|
[1029] | 257 | //slice on/off |
---|
[1332] | 258 | decidePicParams(sliceEnabled, pPic->getSlice(0)->getDepth(), saoEncodingRate, saoEncodingRateChroma); |
---|
[540] | 259 | |
---|
[1029] | 260 | //block on/off |
---|
[540] | 261 | SAOBlkParam* reconParams = new SAOBlkParam[m_numCTUsPic]; //temporary parameter buffer for storing reconstructed SAO parameters |
---|
[1332] | 262 | decideBlkParams(pPic, sliceEnabled, m_statData, srcYuv, resYuv, reconParams, pPic->getPicSym()->getSAOBlkParam(), bTestSAODisableAtPictureLevel, saoEncodingRate, saoEncodingRateChroma); |
---|
[540] | 263 | delete[] reconParams; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | Void TEncSampleAdaptiveOffset::getPreDBFStatistics(TComPic* pPic) |
---|
| 267 | { |
---|
| 268 | getStatistics(m_preDBFstatData, pPic->getPicYuvOrg(), pPic->getPicYuvRec(), pPic, true); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | Void TEncSampleAdaptiveOffset::addPreDBFStatistics(SAOStatData*** blkStats) |
---|
| 272 | { |
---|
| 273 | for(Int n=0; n< m_numCTUsPic; n++) |
---|
| 274 | { |
---|
[1029] | 275 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 276 | { |
---|
| 277 | for(Int typeIdc=0; typeIdc < NUM_SAO_NEW_TYPES; typeIdc++) |
---|
| 278 | { |
---|
| 279 | blkStats[n][compIdx][typeIdc] += m_preDBFstatData[n][compIdx][typeIdc]; |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
[1344] | 285 | Void TEncSampleAdaptiveOffset::getStatistics(SAOStatData*** blkStats, TComPicYuv* orgYuv, TComPicYuv* srcYuv, TComPic* pPic, Bool isCalculatePreDeblockSamples) |
---|
[540] | 286 | { |
---|
| 287 | Bool isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail; |
---|
| 288 | |
---|
[1029] | 289 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 290 | |
---|
| 291 | for(Int ctuRsAddr= 0; ctuRsAddr < m_numCTUsPic; ctuRsAddr++) |
---|
[540] | 292 | { |
---|
[1029] | 293 | Int yPos = (ctuRsAddr / m_numCTUInWidth)*m_maxCUHeight; |
---|
| 294 | Int xPos = (ctuRsAddr % m_numCTUInWidth)*m_maxCUWidth; |
---|
[540] | 295 | Int height = (yPos + m_maxCUHeight > m_picHeight)?(m_picHeight- yPos):m_maxCUHeight; |
---|
| 296 | Int width = (xPos + m_maxCUWidth > m_picWidth )?(m_picWidth - xPos):m_maxCUWidth; |
---|
| 297 | |
---|
[1029] | 298 | pPic->getPicSym()->deriveLoopFilterBoundaryAvailibility(ctuRsAddr, isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail); |
---|
[540] | 299 | |
---|
| 300 | //NOTE: The number of skipped lines during gathering CTU statistics depends on the slice boundary availabilities. |
---|
| 301 | //For simplicity, here only picture boundaries are considered. |
---|
| 302 | |
---|
| 303 | isRightAvail = (xPos + m_maxCUWidth < m_picWidth ); |
---|
| 304 | isBelowAvail = (yPos + m_maxCUHeight < m_picHeight); |
---|
| 305 | isBelowRightAvail = (isRightAvail && isBelowAvail); |
---|
| 306 | isBelowLeftAvail = ((xPos > 0) && (isBelowAvail)); |
---|
| 307 | isAboveRightAvail = ((yPos > 0) && (isRightAvail)); |
---|
| 308 | |
---|
[1029] | 309 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 310 | { |
---|
[1029] | 311 | const ComponentID component = ComponentID(compIdx); |
---|
[540] | 312 | |
---|
[1029] | 313 | const UInt componentScaleX = getComponentScaleX(component, pPic->getChromaFormat()); |
---|
| 314 | const UInt componentScaleY = getComponentScaleY(component, pPic->getChromaFormat()); |
---|
[540] | 315 | |
---|
[1029] | 316 | Int srcStride = srcYuv->getStride(component); |
---|
| 317 | Pel* srcBlk = srcYuv->getAddr(component) + ((yPos >> componentScaleY) * srcStride) + (xPos >> componentScaleX); |
---|
[540] | 318 | |
---|
[1029] | 319 | Int orgStride = orgYuv->getStride(component); |
---|
| 320 | Pel* orgBlk = orgYuv->getAddr(component) + ((yPos >> componentScaleY) * orgStride) + (xPos >> componentScaleX); |
---|
| 321 | |
---|
[1287] | 322 | getBlkStats(component, pPic->getPicSym()->getSPS().getBitDepth(toChannelType(component)), blkStats[ctuRsAddr][component] |
---|
[1029] | 323 | , srcBlk, orgBlk, srcStride, orgStride, (width >> componentScaleX), (height >> componentScaleY) |
---|
[1307] | 324 | , isLeftAvail, isRightAvail, isAboveAvail, isBelowAvail, isAboveLeftAvail, isAboveRightAvail |
---|
[540] | 325 | , isCalculatePreDeblockSamples |
---|
| 326 | ); |
---|
| 327 | |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
[1332] | 332 | Void TEncSampleAdaptiveOffset::decidePicParams(Bool* sliceEnabled, Int picTempLayer, const Double saoEncodingRate, const Double saoEncodingRateChroma) |
---|
[540] | 333 | { |
---|
| 334 | //decide sliceEnabled[compIdx] |
---|
[1029] | 335 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 336 | for (Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
[540] | 337 | { |
---|
[1029] | 338 | sliceEnabled[compIdx] = false; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
| 342 | { |
---|
[540] | 343 | // reset flags & counters |
---|
| 344 | sliceEnabled[compIdx] = true; |
---|
| 345 | |
---|
[1332] | 346 | if (saoEncodingRate>0.0) |
---|
[540] | 347 | { |
---|
[1332] | 348 | if (saoEncodingRateChroma>0.0) |
---|
| 349 | { |
---|
| 350 | // decide slice-level on/off based on previous results |
---|
| 351 | if( (picTempLayer > 0) |
---|
| 352 | && (m_saoDisabledRate[compIdx][picTempLayer-1] > ((compIdx==COMPONENT_Y) ? saoEncodingRate : saoEncodingRateChroma)) ) |
---|
| 353 | { |
---|
| 354 | sliceEnabled[compIdx] = false; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | else |
---|
| 358 | { |
---|
| 359 | // decide slice-level on/off based on previous results |
---|
| 360 | if( (picTempLayer > 0) |
---|
| 361 | && (m_saoDisabledRate[COMPONENT_Y][0] > saoEncodingRate) ) |
---|
| 362 | { |
---|
| 363 | sliceEnabled[compIdx] = false; |
---|
| 364 | } |
---|
| 365 | } |
---|
[540] | 366 | } |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | |
---|
[1287] | 370 | Int64 TEncSampleAdaptiveOffset::getDistortion(const Int channelBitDepth, Int typeIdc, Int typeAuxInfo, Int* invQuantOffset, SAOStatData& statData) |
---|
[540] | 371 | { |
---|
[1029] | 372 | Int64 dist = 0; |
---|
[1287] | 373 | Int shift = 2 * DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth - 8); |
---|
[540] | 374 | |
---|
| 375 | switch(typeIdc) |
---|
| 376 | { |
---|
| 377 | case SAO_TYPE_EO_0: |
---|
| 378 | case SAO_TYPE_EO_90: |
---|
| 379 | case SAO_TYPE_EO_135: |
---|
| 380 | case SAO_TYPE_EO_45: |
---|
| 381 | { |
---|
| 382 | for (Int offsetIdx=0; offsetIdx<NUM_SAO_EO_CLASSES; offsetIdx++) |
---|
| 383 | { |
---|
| 384 | dist += estSaoDist( statData.count[offsetIdx], invQuantOffset[offsetIdx], statData.diff[offsetIdx], shift); |
---|
[1029] | 385 | } |
---|
[540] | 386 | } |
---|
| 387 | break; |
---|
| 388 | case SAO_TYPE_BO: |
---|
| 389 | { |
---|
| 390 | for (Int offsetIdx=typeAuxInfo; offsetIdx<typeAuxInfo+4; offsetIdx++) |
---|
| 391 | { |
---|
[1029] | 392 | Int bandIdx = offsetIdx % NUM_SAO_BO_CLASSES ; |
---|
[540] | 393 | dist += estSaoDist( statData.count[bandIdx], invQuantOffset[bandIdx], statData.diff[bandIdx], shift); |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | break; |
---|
| 397 | default: |
---|
| 398 | { |
---|
| 399 | printf("Not a supported type"); |
---|
| 400 | assert(0); |
---|
| 401 | exit(-1); |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | return dist; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | inline Int64 TEncSampleAdaptiveOffset::estSaoDist(Int64 count, Int64 offset, Int64 diffSum, Int shift) |
---|
| 409 | { |
---|
| 410 | return (( count*offset*offset-diffSum*offset*2 ) >> shift); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | |
---|
[1307] | 414 | inline Int TEncSampleAdaptiveOffset::estIterOffset(Int typeIdx, Double lambda, Int offsetInput, Int64 count, Int64 diffSum, Int shift, Int bitIncrease, Int64& bestDist, Double& bestCost, Int offsetTh ) |
---|
[540] | 415 | { |
---|
| 416 | Int iterOffset, tempOffset; |
---|
| 417 | Int64 tempDist, tempRate; |
---|
| 418 | Double tempCost, tempMinCost; |
---|
| 419 | Int offsetOutput = 0; |
---|
| 420 | iterOffset = offsetInput; |
---|
[1029] | 421 | // Assuming sending quantized value 0 results in zero offset and sending the value zero needs 1 bit. entropy coder can be used to measure the exact rate here. |
---|
| 422 | tempMinCost = lambda; |
---|
[540] | 423 | while (iterOffset != 0) |
---|
| 424 | { |
---|
| 425 | // Calculate the bits required for signaling the offset |
---|
[1029] | 426 | tempRate = (typeIdx == SAO_TYPE_BO) ? (abs((Int)iterOffset)+2) : (abs((Int)iterOffset)+1); |
---|
| 427 | if (abs((Int)iterOffset)==offsetTh) //inclusive |
---|
| 428 | { |
---|
[540] | 429 | tempRate --; |
---|
| 430 | } |
---|
| 431 | // Do the dequantization before distortion calculation |
---|
| 432 | tempOffset = iterOffset << bitIncrease; |
---|
| 433 | tempDist = estSaoDist( count, tempOffset, diffSum, shift); |
---|
| 434 | tempCost = ((Double)tempDist + lambda * (Double) tempRate); |
---|
| 435 | if(tempCost < tempMinCost) |
---|
| 436 | { |
---|
| 437 | tempMinCost = tempCost; |
---|
| 438 | offsetOutput = iterOffset; |
---|
| 439 | bestDist = tempDist; |
---|
| 440 | bestCost = tempCost; |
---|
| 441 | } |
---|
| 442 | iterOffset = (iterOffset > 0) ? (iterOffset-1):(iterOffset+1); |
---|
| 443 | } |
---|
| 444 | return offsetOutput; |
---|
| 445 | } |
---|
| 446 | |
---|
[1287] | 447 | Void TEncSampleAdaptiveOffset::deriveOffsets(ComponentID compIdx, const Int channelBitDepth, Int typeIdc, SAOStatData& statData, Int* quantOffsets, Int& typeAuxInfo) |
---|
[540] | 448 | { |
---|
[1287] | 449 | Int bitDepth = channelBitDepth; |
---|
[1029] | 450 | Int shift = 2 * DISTORTION_PRECISION_ADJUSTMENT(bitDepth-8); |
---|
[1287] | 451 | Int offsetTh = TComSampleAdaptiveOffset::getMaxOffsetQVal(channelBitDepth); //inclusive |
---|
[540] | 452 | |
---|
| 453 | ::memset(quantOffsets, 0, sizeof(Int)*MAX_NUM_SAO_CLASSES); |
---|
| 454 | |
---|
[1029] | 455 | //derive initial offsets |
---|
[540] | 456 | Int numClasses = (typeIdc == SAO_TYPE_BO)?((Int)NUM_SAO_BO_CLASSES):((Int)NUM_SAO_EO_CLASSES); |
---|
| 457 | for(Int classIdx=0; classIdx< numClasses; classIdx++) |
---|
| 458 | { |
---|
[1029] | 459 | if( (typeIdc != SAO_TYPE_BO) && (classIdx==SAO_CLASS_EO_PLAIN) ) |
---|
[540] | 460 | { |
---|
| 461 | continue; //offset will be zero |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | if(statData.count[classIdx] == 0) |
---|
| 465 | { |
---|
| 466 | continue; //offset will be zero |
---|
| 467 | } |
---|
| 468 | |
---|
[1029] | 469 | quantOffsets[classIdx] = (Int) xRoundIbdi(bitDepth, (Double)( statData.diff[classIdx]<<(bitDepth-8)) |
---|
| 470 | / |
---|
[540] | 471 | (Double)( statData.count[classIdx]<< m_offsetStepLog2[compIdx]) |
---|
| 472 | ); |
---|
| 473 | quantOffsets[classIdx] = Clip3(-offsetTh, offsetTh, quantOffsets[classIdx]); |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | // adjust offsets |
---|
| 477 | switch(typeIdc) |
---|
| 478 | { |
---|
| 479 | case SAO_TYPE_EO_0: |
---|
| 480 | case SAO_TYPE_EO_90: |
---|
| 481 | case SAO_TYPE_EO_135: |
---|
| 482 | case SAO_TYPE_EO_45: |
---|
| 483 | { |
---|
| 484 | Int64 classDist; |
---|
| 485 | Double classCost; |
---|
[1029] | 486 | for(Int classIdx=0; classIdx<NUM_SAO_EO_CLASSES; classIdx++) |
---|
| 487 | { |
---|
[1246] | 488 | if(classIdx==SAO_CLASS_EO_FULL_VALLEY && quantOffsets[classIdx] < 0) |
---|
| 489 | { |
---|
| 490 | quantOffsets[classIdx] =0; |
---|
| 491 | } |
---|
| 492 | if(classIdx==SAO_CLASS_EO_HALF_VALLEY && quantOffsets[classIdx] < 0) |
---|
| 493 | { |
---|
| 494 | quantOffsets[classIdx] =0; |
---|
| 495 | } |
---|
| 496 | if(classIdx==SAO_CLASS_EO_HALF_PEAK && quantOffsets[classIdx] > 0) |
---|
| 497 | { |
---|
| 498 | quantOffsets[classIdx] =0; |
---|
| 499 | } |
---|
| 500 | if(classIdx==SAO_CLASS_EO_FULL_PEAK && quantOffsets[classIdx] > 0) |
---|
| 501 | { |
---|
| 502 | quantOffsets[classIdx] =0; |
---|
| 503 | } |
---|
[540] | 504 | |
---|
| 505 | if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero |
---|
| 506 | { |
---|
[1307] | 507 | quantOffsets[classIdx] = estIterOffset( typeIdc, m_lambda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], classDist , classCost , offsetTh ); |
---|
[540] | 508 | } |
---|
| 509 | } |
---|
[1029] | 510 | |
---|
[540] | 511 | typeAuxInfo =0; |
---|
| 512 | } |
---|
| 513 | break; |
---|
| 514 | case SAO_TYPE_BO: |
---|
| 515 | { |
---|
| 516 | Int64 distBOClasses[NUM_SAO_BO_CLASSES]; |
---|
| 517 | Double costBOClasses[NUM_SAO_BO_CLASSES]; |
---|
| 518 | ::memset(distBOClasses, 0, sizeof(Int64)*NUM_SAO_BO_CLASSES); |
---|
| 519 | for(Int classIdx=0; classIdx< NUM_SAO_BO_CLASSES; classIdx++) |
---|
[1029] | 520 | { |
---|
[595] | 521 | costBOClasses[classIdx]= m_lambda[compIdx]; |
---|
[540] | 522 | if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero |
---|
| 523 | { |
---|
[1307] | 524 | quantOffsets[classIdx] = estIterOffset( typeIdc, m_lambda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], distBOClasses[classIdx], costBOClasses[classIdx], offsetTh ); |
---|
[540] | 525 | } |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | //decide the starting band index |
---|
| 529 | Double minCost = MAX_DOUBLE, cost; |
---|
[1029] | 530 | for(Int band=0; band< NUM_SAO_BO_CLASSES- 4+ 1; band++) |
---|
[540] | 531 | { |
---|
| 532 | cost = costBOClasses[band ]; |
---|
| 533 | cost += costBOClasses[band+1]; |
---|
| 534 | cost += costBOClasses[band+2]; |
---|
| 535 | cost += costBOClasses[band+3]; |
---|
| 536 | |
---|
| 537 | if(cost < minCost) |
---|
| 538 | { |
---|
| 539 | minCost = cost; |
---|
| 540 | typeAuxInfo = band; |
---|
| 541 | } |
---|
| 542 | } |
---|
| 543 | //clear those unused classes |
---|
| 544 | Int clearQuantOffset[NUM_SAO_BO_CLASSES]; |
---|
| 545 | ::memset(clearQuantOffset, 0, sizeof(Int)*NUM_SAO_BO_CLASSES); |
---|
[1029] | 546 | for(Int i=0; i< 4; i++) |
---|
[540] | 547 | { |
---|
| 548 | Int band = (typeAuxInfo+i)%NUM_SAO_BO_CLASSES; |
---|
| 549 | clearQuantOffset[band] = quantOffsets[band]; |
---|
| 550 | } |
---|
[1029] | 551 | ::memcpy(quantOffsets, clearQuantOffset, sizeof(Int)*NUM_SAO_BO_CLASSES); |
---|
[540] | 552 | } |
---|
| 553 | break; |
---|
| 554 | default: |
---|
| 555 | { |
---|
| 556 | printf("Not a supported type"); |
---|
| 557 | assert(0); |
---|
| 558 | exit(-1); |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | |
---|
| 564 | } |
---|
| 565 | |
---|
[1287] | 566 | Void TEncSampleAdaptiveOffset::deriveModeNewRDO(const BitDepths &bitDepths, Int ctuRsAddr, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES], Bool* sliceEnabled, SAOStatData*** blkStats, SAOBlkParam& modeParam, Double& modeNormCost, TEncSbac** cabacCoderRDO, Int inCabacLabel) |
---|
[540] | 567 | { |
---|
| 568 | Double minCost, cost; |
---|
[595] | 569 | UInt previousWrittenBits; |
---|
[1029] | 570 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 571 | |
---|
| 572 | Int64 dist[MAX_NUM_COMPONENT], modeDist[MAX_NUM_COMPONENT]; |
---|
| 573 | SAOOffset testOffset[MAX_NUM_COMPONENT]; |
---|
[540] | 574 | Int invQuantOffset[MAX_NUM_SAO_CLASSES]; |
---|
[1029] | 575 | for(Int comp=0; comp < MAX_NUM_COMPONENT; comp++) |
---|
| 576 | { |
---|
| 577 | modeDist[comp] = 0; |
---|
| 578 | } |
---|
[540] | 579 | |
---|
| 580 | //pre-encode merge flags |
---|
[1029] | 581 | modeParam[COMPONENT_Y].modeIdc = SAO_MODE_OFF; |
---|
[540] | 582 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
---|
[1287] | 583 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(modeParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), true); |
---|
[540] | 584 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
| 585 | |
---|
[1029] | 586 | //------ luma --------// |
---|
| 587 | { |
---|
[1287] | 588 | const ComponentID compIdx = COMPONENT_Y; |
---|
[1029] | 589 | //"off" case as initial cost |
---|
| 590 | modeParam[compIdx].modeIdc = SAO_MODE_OFF; |
---|
| 591 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
[1287] | 592 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, modeParam[compIdx], sliceEnabled[compIdx], bitDepths.recon[CHANNEL_TYPE_LUMA]); |
---|
[1029] | 593 | modeDist[compIdx] = 0; |
---|
| 594 | minCost= m_lambda[compIdx]*((Double)m_pcRDGoOnSbacCoder->getNumberOfWrittenBits()); |
---|
| 595 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
| 596 | if(sliceEnabled[compIdx]) |
---|
[540] | 597 | { |
---|
[1029] | 598 | for(Int typeIdc=0; typeIdc< NUM_SAO_NEW_TYPES; typeIdc++) |
---|
| 599 | { |
---|
| 600 | testOffset[compIdx].modeIdc = SAO_MODE_NEW; |
---|
| 601 | testOffset[compIdx].typeIdc = typeIdc; |
---|
[540] | 602 | |
---|
[1029] | 603 | //derive coded offset |
---|
[1287] | 604 | deriveOffsets(compIdx, bitDepths.recon[CHANNEL_TYPE_LUMA], typeIdc, blkStats[ctuRsAddr][compIdx][typeIdc], testOffset[compIdx].offset, testOffset[compIdx].typeAuxInfo); |
---|
[540] | 605 | |
---|
[1029] | 606 | //inversed quantized offsets |
---|
| 607 | invertQuantOffsets(compIdx, typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, testOffset[compIdx].offset); |
---|
[540] | 608 | |
---|
[1029] | 609 | //get distortion |
---|
[1287] | 610 | dist[compIdx] = getDistortion(bitDepths.recon[CHANNEL_TYPE_LUMA], testOffset[compIdx].typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, blkStats[ctuRsAddr][compIdx][typeIdc]); |
---|
[540] | 611 | |
---|
[1029] | 612 | //get rate |
---|
| 613 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
| 614 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
[1287] | 615 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, testOffset[compIdx], sliceEnabled[compIdx], bitDepths.recon[CHANNEL_TYPE_LUMA]); |
---|
[1029] | 616 | Int rate = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
| 617 | cost = (Double)dist[compIdx] + m_lambda[compIdx]*((Double)rate); |
---|
| 618 | if(cost < minCost) |
---|
| 619 | { |
---|
| 620 | minCost = cost; |
---|
| 621 | modeDist[compIdx] = dist[compIdx]; |
---|
| 622 | modeParam[compIdx]= testOffset[compIdx]; |
---|
| 623 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
| 624 | } |
---|
[540] | 625 | } |
---|
| 626 | } |
---|
[1029] | 627 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
| 628 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
[540] | 629 | } |
---|
| 630 | |
---|
| 631 | //------ chroma --------// |
---|
[1029] | 632 | //"off" case as initial cost |
---|
[595] | 633 | cost = 0; |
---|
| 634 | previousWrittenBits = 0; |
---|
[540] | 635 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
[1029] | 636 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
---|
[595] | 637 | { |
---|
[1029] | 638 | const ComponentID component = ComponentID(componentIndex); |
---|
[540] | 639 | |
---|
[1029] | 640 | modeParam[component].modeIdc = SAO_MODE_OFF; |
---|
| 641 | modeDist [component] = 0; |
---|
[1287] | 642 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(component, modeParam[component], sliceEnabled[component], bitDepths.recon[CHANNEL_TYPE_CHROMA]); |
---|
| 643 | |
---|
[595] | 644 | const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
| 645 | cost += m_lambda[component] * (currentWrittenBits - previousWrittenBits); |
---|
| 646 | previousWrittenBits = currentWrittenBits; |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | minCost = cost; |
---|
| 650 | |
---|
[540] | 651 | //doesn't need to store cabac status here since the whole CTU parameters will be re-encoded at the end of this function |
---|
| 652 | |
---|
| 653 | for(Int typeIdc=0; typeIdc< NUM_SAO_NEW_TYPES; typeIdc++) |
---|
| 654 | { |
---|
[595] | 655 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
| 656 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
| 657 | previousWrittenBits = 0; |
---|
| 658 | cost = 0; |
---|
| 659 | |
---|
[1029] | 660 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
---|
[540] | 661 | { |
---|
[1029] | 662 | const ComponentID component = ComponentID(componentIndex); |
---|
| 663 | if(!sliceEnabled[component]) |
---|
[540] | 664 | { |
---|
[1029] | 665 | testOffset[component].modeIdc = SAO_MODE_OFF; |
---|
| 666 | dist[component]= 0; |
---|
[540] | 667 | continue; |
---|
| 668 | } |
---|
[1029] | 669 | testOffset[component].modeIdc = SAO_MODE_NEW; |
---|
| 670 | testOffset[component].typeIdc = typeIdc; |
---|
[540] | 671 | |
---|
| 672 | //derive offset & get distortion |
---|
[1287] | 673 | deriveOffsets(component, bitDepths.recon[CHANNEL_TYPE_CHROMA], typeIdc, blkStats[ctuRsAddr][component][typeIdc], testOffset[component].offset, testOffset[component].typeAuxInfo); |
---|
[1029] | 674 | invertQuantOffsets(component, typeIdc, testOffset[component].typeAuxInfo, invQuantOffset, testOffset[component].offset); |
---|
[1287] | 675 | dist[component] = getDistortion(bitDepths.recon[CHANNEL_TYPE_CHROMA], typeIdc, testOffset[component].typeAuxInfo, invQuantOffset, blkStats[ctuRsAddr][component][typeIdc]); |
---|
[595] | 676 | |
---|
[1287] | 677 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(component, testOffset[component], sliceEnabled[component], bitDepths.recon[CHANNEL_TYPE_CHROMA]); |
---|
[595] | 678 | |
---|
| 679 | const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
[1029] | 680 | cost += dist[component] + (m_lambda[component] * (currentWrittenBits - previousWrittenBits)); |
---|
[595] | 681 | previousWrittenBits = currentWrittenBits; |
---|
[540] | 682 | } |
---|
| 683 | |
---|
| 684 | if(cost < minCost) |
---|
| 685 | { |
---|
| 686 | minCost = cost; |
---|
[1029] | 687 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
---|
[595] | 688 | { |
---|
[1029] | 689 | modeDist[componentIndex] = dist[componentIndex]; |
---|
| 690 | modeParam[componentIndex] = testOffset[componentIndex]; |
---|
[595] | 691 | } |
---|
[540] | 692 | } |
---|
| 693 | |
---|
[1029] | 694 | } // SAO_TYPE loop |
---|
[540] | 695 | |
---|
| 696 | //----- re-gen rate & normalized cost----// |
---|
[595] | 697 | modeNormCost = 0; |
---|
[1029] | 698 | for(UInt componentIndex = COMPONENT_Y; componentIndex < numberOfComponents; componentIndex++) |
---|
[595] | 699 | { |
---|
[1029] | 700 | modeNormCost += (Double)modeDist[componentIndex] / m_lambda[componentIndex]; |
---|
[595] | 701 | } |
---|
[1029] | 702 | |
---|
[540] | 703 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
---|
| 704 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
[1287] | 705 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(modeParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), false); |
---|
[540] | 706 | modeNormCost += (Double)m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
| 707 | } |
---|
| 708 | |
---|
[1287] | 709 | Void TEncSampleAdaptiveOffset::deriveModeMergeRDO(const BitDepths &bitDepths, Int ctuRsAddr, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES], Bool* sliceEnabled, SAOStatData*** blkStats, SAOBlkParam& modeParam, Double& modeNormCost, TEncSbac** cabacCoderRDO, Int inCabacLabel) |
---|
[540] | 710 | { |
---|
| 711 | modeNormCost = MAX_DOUBLE; |
---|
| 712 | |
---|
| 713 | Double cost; |
---|
| 714 | SAOBlkParam testBlkParam; |
---|
[1029] | 715 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
[540] | 716 | |
---|
[1029] | 717 | for(Int mergeType=0; mergeType< NUM_SAO_MERGE_TYPES; mergeType++) |
---|
[540] | 718 | { |
---|
| 719 | if(mergeList[mergeType] == NULL) |
---|
| 720 | { |
---|
| 721 | continue; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | testBlkParam = *(mergeList[mergeType]); |
---|
| 725 | //normalized distortion |
---|
| 726 | Double normDist=0; |
---|
[1029] | 727 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 728 | { |
---|
| 729 | testBlkParam[compIdx].modeIdc = SAO_MODE_MERGE; |
---|
| 730 | testBlkParam[compIdx].typeIdc = mergeType; |
---|
| 731 | |
---|
| 732 | SAOOffset& mergedOffsetParam = (*(mergeList[mergeType]))[compIdx]; |
---|
| 733 | |
---|
| 734 | if( mergedOffsetParam.modeIdc != SAO_MODE_OFF) |
---|
| 735 | { |
---|
| 736 | //offsets have been reconstructed. Don't call inversed quantization function. |
---|
[1287] | 737 | normDist += (((Double)getDistortion(bitDepths.recon[toChannelType(ComponentID(compIdx))], mergedOffsetParam.typeIdc, mergedOffsetParam.typeAuxInfo, mergedOffsetParam.offset, blkStats[ctuRsAddr][compIdx][mergedOffsetParam.typeIdc])) |
---|
[595] | 738 | /m_lambda[compIdx] |
---|
[540] | 739 | ); |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | //rate |
---|
| 745 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
---|
| 746 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
[1287] | 747 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(testBlkParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), false); |
---|
[540] | 748 | Int rate = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
| 749 | |
---|
| 750 | cost = normDist+(Double)rate; |
---|
| 751 | |
---|
| 752 | if(cost < modeNormCost) |
---|
| 753 | { |
---|
| 754 | modeNormCost = cost; |
---|
| 755 | modeParam = testBlkParam; |
---|
| 756 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
| 757 | } |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
| 761 | } |
---|
| 762 | |
---|
[1332] | 763 | Void TEncSampleAdaptiveOffset::decideBlkParams(TComPic* pic, Bool* sliceEnabled, SAOStatData*** blkStats, TComPicYuv* srcYuv, TComPicYuv* resYuv, |
---|
| 764 | SAOBlkParam* reconParams, SAOBlkParam* codedParams, const Bool bTestSAODisableAtPictureLevel, |
---|
| 765 | const Double saoEncodingRate, const Double saoEncodingRateChroma) |
---|
[540] | 766 | { |
---|
[1029] | 767 | Bool allBlksDisabled = true; |
---|
| 768 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
| 769 | for(Int compId = COMPONENT_Y; compId < numberOfComponents; compId++) |
---|
[540] | 770 | { |
---|
[1029] | 771 | if (sliceEnabled[compId]) |
---|
[1246] | 772 | { |
---|
[1029] | 773 | allBlksDisabled = false; |
---|
[1246] | 774 | } |
---|
[540] | 775 | } |
---|
| 776 | |
---|
| 777 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_PIC_INIT ]); |
---|
| 778 | |
---|
| 779 | SAOBlkParam modeParam; |
---|
| 780 | Double minCost, modeCost; |
---|
| 781 | |
---|
[1029] | 782 | |
---|
[1331] | 783 | Double totalCost = 0; // Used if bTestSAODisableAtPictureLevel==true |
---|
[1029] | 784 | |
---|
| 785 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
---|
[540] | 786 | { |
---|
[1029] | 787 | if(allBlksDisabled) |
---|
[540] | 788 | { |
---|
[1029] | 789 | codedParams[ctuRsAddr].reset(); |
---|
[540] | 790 | continue; |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_CUR ]); |
---|
| 794 | |
---|
| 795 | //get merge list |
---|
[1029] | 796 | SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES] = { NULL }; |
---|
| 797 | getMergeList(pic, ctuRsAddr, reconParams, mergeList); |
---|
[540] | 798 | |
---|
| 799 | minCost = MAX_DOUBLE; |
---|
| 800 | for(Int mode=0; mode < NUM_SAO_MODES; mode++) |
---|
| 801 | { |
---|
| 802 | switch(mode) |
---|
| 803 | { |
---|
| 804 | case SAO_MODE_OFF: |
---|
| 805 | { |
---|
| 806 | continue; //not necessary, since all-off case will be tested in SAO_MODE_NEW case. |
---|
| 807 | } |
---|
| 808 | break; |
---|
| 809 | case SAO_MODE_NEW: |
---|
| 810 | { |
---|
[1287] | 811 | deriveModeNewRDO(pic->getPicSym()->getSPS().getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats, modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
---|
[540] | 812 | } |
---|
| 813 | break; |
---|
| 814 | case SAO_MODE_MERGE: |
---|
| 815 | { |
---|
[1287] | 816 | deriveModeMergeRDO(pic->getPicSym()->getSPS().getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats , modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
---|
[540] | 817 | } |
---|
| 818 | break; |
---|
| 819 | default: |
---|
| 820 | { |
---|
| 821 | printf("Not a supported SAO mode\n"); |
---|
| 822 | assert(0); |
---|
| 823 | exit(-1); |
---|
| 824 | } |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | if(modeCost < minCost) |
---|
| 828 | { |
---|
| 829 | minCost = modeCost; |
---|
[1029] | 830 | codedParams[ctuRsAddr] = modeParam; |
---|
[540] | 831 | m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_NEXT ]); |
---|
| 832 | } |
---|
| 833 | } //mode |
---|
[1029] | 834 | |
---|
| 835 | totalCost += minCost; |
---|
| 836 | |
---|
[540] | 837 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_NEXT ]); |
---|
| 838 | |
---|
| 839 | //apply reconstructed offsets |
---|
[1029] | 840 | reconParams[ctuRsAddr] = codedParams[ctuRsAddr]; |
---|
| 841 | reconstructBlkSAOParam(reconParams[ctuRsAddr], mergeList); |
---|
| 842 | offsetCTU(ctuRsAddr, srcYuv, resYuv, reconParams[ctuRsAddr], pic); |
---|
| 843 | } //ctuRsAddr |
---|
[540] | 844 | |
---|
[1331] | 845 | if (!allBlksDisabled && (totalCost >= 0) && bTestSAODisableAtPictureLevel) //SAO has not beneficial in this case - disable it |
---|
[1029] | 846 | { |
---|
| 847 | for(Int ctuRsAddr = 0; ctuRsAddr < m_numCTUsPic; ctuRsAddr++) |
---|
| 848 | { |
---|
| 849 | codedParams[ctuRsAddr].reset(); |
---|
| 850 | } |
---|
| 851 | |
---|
| 852 | for (UInt componentIndex = 0; componentIndex < MAX_NUM_COMPONENT; componentIndex++) |
---|
| 853 | { |
---|
| 854 | sliceEnabled[componentIndex] = false; |
---|
| 855 | } |
---|
| 856 | |
---|
| 857 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_PIC_INIT ]); |
---|
| 858 | } |
---|
| 859 | |
---|
[1332] | 860 | if (saoEncodingRate > 0.0) |
---|
| 861 | { |
---|
| 862 | Int picTempLayer = pic->getSlice(0)->getDepth(); |
---|
| 863 | Int numCtusForSAOOff[MAX_NUM_COMPONENT]; |
---|
[540] | 864 | |
---|
[1332] | 865 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
[540] | 866 | { |
---|
[1332] | 867 | numCtusForSAOOff[compIdx] = 0; |
---|
| 868 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
---|
[540] | 869 | { |
---|
[1332] | 870 | if( reconParams[ctuRsAddr][compIdx].modeIdc == SAO_MODE_OFF) |
---|
| 871 | { |
---|
| 872 | numCtusForSAOOff[compIdx]++; |
---|
| 873 | } |
---|
[540] | 874 | } |
---|
| 875 | } |
---|
[1332] | 876 | if (saoEncodingRateChroma > 0.0) |
---|
| 877 | { |
---|
| 878 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
| 879 | { |
---|
| 880 | m_saoDisabledRate[compIdx][picTempLayer] = (Double)numCtusForSAOOff[compIdx]/(Double)m_numCTUsPic; |
---|
| 881 | } |
---|
| 882 | } |
---|
| 883 | else if (picTempLayer == 0) |
---|
| 884 | { |
---|
| 885 | m_saoDisabledRate[COMPONENT_Y][0] = (Double)(numCtusForSAOOff[COMPONENT_Y]+numCtusForSAOOff[COMPONENT_Cb]+numCtusForSAOOff[COMPONENT_Cr])/(Double)(m_numCTUsPic*3); |
---|
| 886 | } |
---|
[540] | 887 | } |
---|
| 888 | } |
---|
| 889 | |
---|
| 890 | |
---|
[1287] | 891 | Void TEncSampleAdaptiveOffset::getBlkStats(const ComponentID compIdx, const Int channelBitDepth, SAOStatData* statsDataTypes |
---|
[540] | 892 | , Pel* srcBlk, Pel* orgBlk, Int srcStride, Int orgStride, Int width, Int height |
---|
[1307] | 893 | , Bool isLeftAvail, Bool isRightAvail, Bool isAboveAvail, Bool isBelowAvail, Bool isAboveLeftAvail, Bool isAboveRightAvail |
---|
[540] | 894 | , Bool isCalculatePreDeblockSamples |
---|
| 895 | ) |
---|
| 896 | { |
---|
| 897 | if(m_lineBufWidth != m_maxCUWidth) |
---|
| 898 | { |
---|
| 899 | m_lineBufWidth = m_maxCUWidth; |
---|
| 900 | |
---|
[1246] | 901 | if (m_signLineBuf1) |
---|
| 902 | { |
---|
| 903 | delete[] m_signLineBuf1; |
---|
| 904 | m_signLineBuf1 = NULL; |
---|
| 905 | } |
---|
[1442] | 906 | m_signLineBuf1 = new SChar[m_lineBufWidth+1]; |
---|
[540] | 907 | |
---|
[1246] | 908 | if (m_signLineBuf2) |
---|
| 909 | { |
---|
| 910 | delete[] m_signLineBuf2; |
---|
| 911 | m_signLineBuf2 = NULL; |
---|
| 912 | } |
---|
[1442] | 913 | m_signLineBuf2 = new SChar[m_lineBufWidth+1]; |
---|
[540] | 914 | } |
---|
| 915 | |
---|
| 916 | Int x,y, startX, startY, endX, endY, edgeType, firstLineStartX, firstLineEndX; |
---|
[1442] | 917 | SChar signLeft, signRight, signDown; |
---|
[540] | 918 | Int64 *diff, *count; |
---|
| 919 | Pel *srcLine, *orgLine; |
---|
| 920 | Int* skipLinesR = m_skipLinesR[compIdx]; |
---|
| 921 | Int* skipLinesB = m_skipLinesB[compIdx]; |
---|
| 922 | |
---|
| 923 | for(Int typeIdx=0; typeIdx< NUM_SAO_NEW_TYPES; typeIdx++) |
---|
| 924 | { |
---|
| 925 | SAOStatData& statsData= statsDataTypes[typeIdx]; |
---|
| 926 | statsData.reset(); |
---|
| 927 | |
---|
| 928 | srcLine = srcBlk; |
---|
| 929 | orgLine = orgBlk; |
---|
| 930 | diff = statsData.diff; |
---|
| 931 | count = statsData.count; |
---|
| 932 | switch(typeIdx) |
---|
| 933 | { |
---|
| 934 | case SAO_TYPE_EO_0: |
---|
| 935 | { |
---|
| 936 | diff +=2; |
---|
| 937 | count+=2; |
---|
| 938 | endY = (isBelowAvail) ? (height - skipLinesB[typeIdx]) : height; |
---|
| 939 | startX = (!isCalculatePreDeblockSamples) ? (isLeftAvail ? 0 : 1) |
---|
| 940 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
| 941 | ; |
---|
| 942 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
| 943 | : (isRightAvail ? width : (width - 1)) |
---|
| 944 | ; |
---|
| 945 | for (y=0; y<endY; y++) |
---|
| 946 | { |
---|
[1442] | 947 | signLeft = (SChar)sgn(srcLine[startX] - srcLine[startX-1]); |
---|
[540] | 948 | for (x=startX; x<endX; x++) |
---|
| 949 | { |
---|
[1442] | 950 | signRight = (SChar)sgn(srcLine[x] - srcLine[x+1]); |
---|
[540] | 951 | edgeType = signRight + signLeft; |
---|
| 952 | signLeft = -signRight; |
---|
| 953 | |
---|
| 954 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 955 | count[edgeType] ++; |
---|
| 956 | } |
---|
| 957 | srcLine += srcStride; |
---|
| 958 | orgLine += orgStride; |
---|
| 959 | } |
---|
| 960 | if(isCalculatePreDeblockSamples) |
---|
| 961 | { |
---|
| 962 | if(isBelowAvail) |
---|
| 963 | { |
---|
| 964 | startX = isLeftAvail ? 0 : 1; |
---|
| 965 | endX = isRightAvail ? width : (width -1); |
---|
| 966 | |
---|
| 967 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
| 968 | { |
---|
[1442] | 969 | signLeft = (SChar)sgn(srcLine[startX] - srcLine[startX-1]); |
---|
[540] | 970 | for (x=startX; x<endX; x++) |
---|
| 971 | { |
---|
[1442] | 972 | signRight = (SChar)sgn(srcLine[x] - srcLine[x+1]); |
---|
[540] | 973 | edgeType = signRight + signLeft; |
---|
| 974 | signLeft = -signRight; |
---|
| 975 | |
---|
| 976 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 977 | count[edgeType] ++; |
---|
| 978 | } |
---|
| 979 | srcLine += srcStride; |
---|
| 980 | orgLine += orgStride; |
---|
| 981 | } |
---|
| 982 | } |
---|
| 983 | } |
---|
| 984 | } |
---|
| 985 | break; |
---|
| 986 | case SAO_TYPE_EO_90: |
---|
| 987 | { |
---|
| 988 | diff +=2; |
---|
| 989 | count+=2; |
---|
[1442] | 990 | SChar *signUpLine = m_signLineBuf1; |
---|
[540] | 991 | |
---|
| 992 | startX = (!isCalculatePreDeblockSamples) ? 0 |
---|
| 993 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : width) |
---|
| 994 | ; |
---|
| 995 | startY = isAboveAvail ? 0 : 1; |
---|
[1029] | 996 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]) : width) |
---|
[540] | 997 | : width |
---|
| 998 | ; |
---|
| 999 | endY = isBelowAvail ? (height - skipLinesB[typeIdx]) : (height - 1); |
---|
| 1000 | if (!isAboveAvail) |
---|
| 1001 | { |
---|
| 1002 | srcLine += srcStride; |
---|
| 1003 | orgLine += orgStride; |
---|
| 1004 | } |
---|
| 1005 | |
---|
| 1006 | Pel* srcLineAbove = srcLine - srcStride; |
---|
[1029] | 1007 | for (x=startX; x<endX; x++) |
---|
[540] | 1008 | { |
---|
[1442] | 1009 | signUpLine[x] = (SChar)sgn(srcLine[x] - srcLineAbove[x]); |
---|
[540] | 1010 | } |
---|
| 1011 | |
---|
| 1012 | Pel* srcLineBelow; |
---|
| 1013 | for (y=startY; y<endY; y++) |
---|
| 1014 | { |
---|
| 1015 | srcLineBelow = srcLine + srcStride; |
---|
| 1016 | |
---|
| 1017 | for (x=startX; x<endX; x++) |
---|
| 1018 | { |
---|
[1442] | 1019 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x]); |
---|
[540] | 1020 | edgeType = signDown + signUpLine[x]; |
---|
| 1021 | signUpLine[x]= -signDown; |
---|
| 1022 | |
---|
| 1023 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1024 | count[edgeType] ++; |
---|
| 1025 | } |
---|
| 1026 | srcLine += srcStride; |
---|
| 1027 | orgLine += orgStride; |
---|
| 1028 | } |
---|
| 1029 | if(isCalculatePreDeblockSamples) |
---|
| 1030 | { |
---|
| 1031 | if(isBelowAvail) |
---|
| 1032 | { |
---|
| 1033 | startX = 0; |
---|
| 1034 | endX = width; |
---|
| 1035 | |
---|
| 1036 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
| 1037 | { |
---|
| 1038 | srcLineBelow = srcLine + srcStride; |
---|
| 1039 | srcLineAbove = srcLine - srcStride; |
---|
| 1040 | |
---|
| 1041 | for (x=startX; x<endX; x++) |
---|
| 1042 | { |
---|
[713] | 1043 | edgeType = sgn(srcLine[x] - srcLineBelow[x]) + sgn(srcLine[x] - srcLineAbove[x]); |
---|
[540] | 1044 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1045 | count[edgeType] ++; |
---|
| 1046 | } |
---|
| 1047 | srcLine += srcStride; |
---|
| 1048 | orgLine += orgStride; |
---|
| 1049 | } |
---|
| 1050 | } |
---|
| 1051 | } |
---|
| 1052 | |
---|
| 1053 | } |
---|
| 1054 | break; |
---|
| 1055 | case SAO_TYPE_EO_135: |
---|
| 1056 | { |
---|
| 1057 | diff +=2; |
---|
| 1058 | count+=2; |
---|
[1442] | 1059 | SChar *signUpLine, *signDownLine, *signTmpLine; |
---|
[540] | 1060 | |
---|
| 1061 | signUpLine = m_signLineBuf1; |
---|
| 1062 | signDownLine= m_signLineBuf2; |
---|
| 1063 | |
---|
| 1064 | startX = (!isCalculatePreDeblockSamples) ? (isLeftAvail ? 0 : 1) |
---|
| 1065 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
| 1066 | ; |
---|
| 1067 | |
---|
| 1068 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]): (width - 1)) |
---|
| 1069 | : (isRightAvail ? width : (width - 1)) |
---|
| 1070 | ; |
---|
| 1071 | endY = isBelowAvail ? (height - skipLinesB[typeIdx]) : (height - 1); |
---|
| 1072 | |
---|
| 1073 | //prepare 2nd line's upper sign |
---|
| 1074 | Pel* srcLineBelow = srcLine + srcStride; |
---|
| 1075 | for (x=startX; x<endX+1; x++) |
---|
| 1076 | { |
---|
[1442] | 1077 | signUpLine[x] = (SChar)sgn(srcLineBelow[x] - srcLine[x-1]); |
---|
[540] | 1078 | } |
---|
| 1079 | |
---|
| 1080 | //1st line |
---|
| 1081 | Pel* srcLineAbove = srcLine - srcStride; |
---|
| 1082 | firstLineStartX = (!isCalculatePreDeblockSamples) ? (isAboveLeftAvail ? 0 : 1) : startX; |
---|
| 1083 | firstLineEndX = (!isCalculatePreDeblockSamples) ? (isAboveAvail ? endX : 1) : endX; |
---|
| 1084 | for(x=firstLineStartX; x<firstLineEndX; x++) |
---|
| 1085 | { |
---|
[713] | 1086 | edgeType = sgn(srcLine[x] - srcLineAbove[x-1]) - signUpLine[x+1]; |
---|
[540] | 1087 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1088 | count[edgeType] ++; |
---|
| 1089 | } |
---|
| 1090 | srcLine += srcStride; |
---|
| 1091 | orgLine += orgStride; |
---|
| 1092 | |
---|
| 1093 | |
---|
| 1094 | //middle lines |
---|
| 1095 | for (y=1; y<endY; y++) |
---|
| 1096 | { |
---|
| 1097 | srcLineBelow = srcLine + srcStride; |
---|
| 1098 | |
---|
| 1099 | for (x=startX; x<endX; x++) |
---|
| 1100 | { |
---|
[1442] | 1101 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x+1]); |
---|
[540] | 1102 | edgeType = signDown + signUpLine[x]; |
---|
| 1103 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1104 | count[edgeType] ++; |
---|
| 1105 | |
---|
[1029] | 1106 | signDownLine[x+1] = -signDown; |
---|
[540] | 1107 | } |
---|
[1442] | 1108 | signDownLine[startX] = (SChar)sgn(srcLineBelow[startX] - srcLine[startX-1]); |
---|
[540] | 1109 | |
---|
| 1110 | signTmpLine = signUpLine; |
---|
| 1111 | signUpLine = signDownLine; |
---|
| 1112 | signDownLine = signTmpLine; |
---|
| 1113 | |
---|
| 1114 | srcLine += srcStride; |
---|
| 1115 | orgLine += orgStride; |
---|
| 1116 | } |
---|
| 1117 | if(isCalculatePreDeblockSamples) |
---|
| 1118 | { |
---|
| 1119 | if(isBelowAvail) |
---|
| 1120 | { |
---|
| 1121 | startX = isLeftAvail ? 0 : 1 ; |
---|
| 1122 | endX = isRightAvail ? width : (width -1); |
---|
| 1123 | |
---|
| 1124 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
| 1125 | { |
---|
| 1126 | srcLineBelow = srcLine + srcStride; |
---|
| 1127 | srcLineAbove = srcLine - srcStride; |
---|
| 1128 | |
---|
| 1129 | for (x=startX; x< endX; x++) |
---|
| 1130 | { |
---|
[713] | 1131 | edgeType = sgn(srcLine[x] - srcLineBelow[x+1]) + sgn(srcLine[x] - srcLineAbove[x-1]); |
---|
[540] | 1132 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1133 | count[edgeType] ++; |
---|
| 1134 | } |
---|
| 1135 | srcLine += srcStride; |
---|
| 1136 | orgLine += orgStride; |
---|
| 1137 | } |
---|
| 1138 | } |
---|
| 1139 | } |
---|
| 1140 | } |
---|
| 1141 | break; |
---|
| 1142 | case SAO_TYPE_EO_45: |
---|
| 1143 | { |
---|
| 1144 | diff +=2; |
---|
| 1145 | count+=2; |
---|
[1442] | 1146 | SChar *signUpLine = m_signLineBuf1+1; |
---|
[540] | 1147 | |
---|
| 1148 | startX = (!isCalculatePreDeblockSamples) ? (isLeftAvail ? 0 : 1) |
---|
| 1149 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
| 1150 | ; |
---|
| 1151 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
| 1152 | : (isRightAvail ? width : (width - 1)) |
---|
| 1153 | ; |
---|
| 1154 | endY = isBelowAvail ? (height - skipLinesB[typeIdx]) : (height - 1); |
---|
| 1155 | |
---|
| 1156 | //prepare 2nd line upper sign |
---|
| 1157 | Pel* srcLineBelow = srcLine + srcStride; |
---|
| 1158 | for (x=startX-1; x<endX; x++) |
---|
| 1159 | { |
---|
[1442] | 1160 | signUpLine[x] = (SChar)sgn(srcLineBelow[x] - srcLine[x+1]); |
---|
[540] | 1161 | } |
---|
| 1162 | |
---|
| 1163 | |
---|
| 1164 | //first line |
---|
| 1165 | Pel* srcLineAbove = srcLine - srcStride; |
---|
| 1166 | firstLineStartX = (!isCalculatePreDeblockSamples) ? (isAboveAvail ? startX : endX) |
---|
| 1167 | : startX |
---|
| 1168 | ; |
---|
| 1169 | firstLineEndX = (!isCalculatePreDeblockSamples) ? ((!isRightAvail && isAboveRightAvail) ? width : endX) |
---|
| 1170 | : endX |
---|
| 1171 | ; |
---|
| 1172 | for(x=firstLineStartX; x<firstLineEndX; x++) |
---|
| 1173 | { |
---|
[713] | 1174 | edgeType = sgn(srcLine[x] - srcLineAbove[x+1]) - signUpLine[x-1]; |
---|
[540] | 1175 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1176 | count[edgeType] ++; |
---|
| 1177 | } |
---|
| 1178 | |
---|
| 1179 | srcLine += srcStride; |
---|
| 1180 | orgLine += orgStride; |
---|
| 1181 | |
---|
| 1182 | //middle lines |
---|
| 1183 | for (y=1; y<endY; y++) |
---|
| 1184 | { |
---|
| 1185 | srcLineBelow = srcLine + srcStride; |
---|
| 1186 | |
---|
| 1187 | for(x=startX; x<endX; x++) |
---|
| 1188 | { |
---|
[1442] | 1189 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x-1]); |
---|
[540] | 1190 | edgeType = signDown + signUpLine[x]; |
---|
| 1191 | |
---|
| 1192 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1193 | count[edgeType] ++; |
---|
| 1194 | |
---|
[1029] | 1195 | signUpLine[x-1] = -signDown; |
---|
[540] | 1196 | } |
---|
[1442] | 1197 | signUpLine[endX-1] = (SChar)sgn(srcLineBelow[endX-1] - srcLine[endX]); |
---|
[540] | 1198 | srcLine += srcStride; |
---|
| 1199 | orgLine += orgStride; |
---|
| 1200 | } |
---|
| 1201 | if(isCalculatePreDeblockSamples) |
---|
| 1202 | { |
---|
| 1203 | if(isBelowAvail) |
---|
| 1204 | { |
---|
| 1205 | startX = isLeftAvail ? 0 : 1 ; |
---|
| 1206 | endX = isRightAvail ? width : (width -1); |
---|
| 1207 | |
---|
| 1208 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
| 1209 | { |
---|
| 1210 | srcLineBelow = srcLine + srcStride; |
---|
| 1211 | srcLineAbove = srcLine - srcStride; |
---|
| 1212 | |
---|
| 1213 | for (x=startX; x<endX; x++) |
---|
| 1214 | { |
---|
[713] | 1215 | edgeType = sgn(srcLine[x] - srcLineBelow[x-1]) + sgn(srcLine[x] - srcLineAbove[x+1]); |
---|
[540] | 1216 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
| 1217 | count[edgeType] ++; |
---|
| 1218 | } |
---|
| 1219 | srcLine += srcStride; |
---|
| 1220 | orgLine += orgStride; |
---|
| 1221 | } |
---|
| 1222 | } |
---|
| 1223 | } |
---|
| 1224 | } |
---|
| 1225 | break; |
---|
| 1226 | case SAO_TYPE_BO: |
---|
| 1227 | { |
---|
| 1228 | startX = (!isCalculatePreDeblockSamples)?0 |
---|
| 1229 | :( isRightAvail?(width- skipLinesR[typeIdx]):width) |
---|
| 1230 | ; |
---|
| 1231 | endX = (!isCalculatePreDeblockSamples)?(isRightAvail ? (width - skipLinesR[typeIdx]) : width ) |
---|
| 1232 | :width |
---|
| 1233 | ; |
---|
| 1234 | endY = isBelowAvail ? (height- skipLinesB[typeIdx]) : height; |
---|
[1287] | 1235 | Int shiftBits = channelBitDepth - NUM_SAO_BO_CLASSES_LOG2; |
---|
[540] | 1236 | for (y=0; y< endY; y++) |
---|
| 1237 | { |
---|
| 1238 | for (x=startX; x< endX; x++) |
---|
| 1239 | { |
---|
| 1240 | |
---|
[1029] | 1241 | Int bandIdx= srcLine[x] >> shiftBits; |
---|
[540] | 1242 | diff [bandIdx] += (orgLine[x] - srcLine[x]); |
---|
| 1243 | count[bandIdx] ++; |
---|
| 1244 | } |
---|
| 1245 | srcLine += srcStride; |
---|
| 1246 | orgLine += orgStride; |
---|
| 1247 | } |
---|
| 1248 | if(isCalculatePreDeblockSamples) |
---|
| 1249 | { |
---|
| 1250 | if(isBelowAvail) |
---|
| 1251 | { |
---|
| 1252 | startX = 0; |
---|
| 1253 | endX = width; |
---|
| 1254 | |
---|
| 1255 | for(y= 0; y< skipLinesB[typeIdx]; y++) |
---|
| 1256 | { |
---|
| 1257 | for (x=startX; x< endX; x++) |
---|
| 1258 | { |
---|
[1029] | 1259 | Int bandIdx= srcLine[x] >> shiftBits; |
---|
[540] | 1260 | diff [bandIdx] += (orgLine[x] - srcLine[x]); |
---|
| 1261 | count[bandIdx] ++; |
---|
| 1262 | } |
---|
| 1263 | srcLine += srcStride; |
---|
| 1264 | orgLine += orgStride; |
---|
| 1265 | |
---|
| 1266 | } |
---|
| 1267 | |
---|
| 1268 | } |
---|
| 1269 | } |
---|
| 1270 | } |
---|
| 1271 | break; |
---|
| 1272 | default: |
---|
| 1273 | { |
---|
| 1274 | printf("Not a supported SAO types\n"); |
---|
| 1275 | assert(0); |
---|
| 1276 | exit(-1); |
---|
| 1277 | } |
---|
| 1278 | } |
---|
| 1279 | } |
---|
| 1280 | } |
---|
| 1281 | |
---|
[1029] | 1282 | |
---|
[313] | 1283 | //! \} |
---|