| 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 |
|---|
| 4 | * granted under this license. |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
|---|
| 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 | /** |
|---|
| 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 | |
|---|
| 47 | |
|---|
| 48 | //! rounding with IBDI |
|---|
| 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 | |
|---|
| 60 | TEncSampleAdaptiveOffset::TEncSampleAdaptiveOffset() |
|---|
| 61 | { |
|---|
| 62 | m_pppcRDSbacCoder = NULL; |
|---|
| 63 | m_pcRDGoOnSbacCoder = NULL; |
|---|
| 64 | m_pppcBinCoderCABAC = NULL; |
|---|
| 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]; |
|---|
| 79 | #if FAST_BIT_EST |
|---|
| 80 | m_pppcBinCoderCABAC = new TEncBinCABACCounter* [NUM_SAO_CABACSTATE_LABELS]; |
|---|
| 81 | #else |
|---|
| 82 | m_pppcBinCoderCABAC = new TEncBinCABAC* [NUM_SAO_CABACSTATE_LABELS]; |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | for(Int cs=0; cs < NUM_SAO_CABACSTATE_LABELS; cs++) |
|---|
| 86 | { |
|---|
| 87 | m_pppcRDSbacCoder[cs] = new TEncSbac; |
|---|
| 88 | #if FAST_BIT_EST |
|---|
| 89 | m_pppcBinCoderCABAC[cs] = new TEncBinCABACCounter; |
|---|
| 90 | #else |
|---|
| 91 | m_pppcBinCoderCABAC[cs] = new TEncBinCABAC; |
|---|
| 92 | #endif |
|---|
| 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 | { |
|---|
| 101 | m_statData[i] = new SAOStatData*[MAX_NUM_COMPONENT]; |
|---|
| 102 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
|---|
| 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 | { |
|---|
| 112 | m_preDBFstatData[i] = new SAOStatData*[MAX_NUM_COMPONENT]; |
|---|
| 113 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
|---|
| 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 | { |
|---|
| 125 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
|---|
| 126 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
|---|
| 127 | |
|---|
| 128 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
|---|
| 129 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
|---|
| 130 | |
|---|
| 131 | if(isPreDBFSamplesUsed) |
|---|
| 132 | { |
|---|
| 133 | switch(typeIdc) |
|---|
| 134 | { |
|---|
| 135 | case SAO_TYPE_EO_0: |
|---|
| 136 | { |
|---|
| 137 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
|---|
| 138 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
|---|
| 139 | |
|---|
| 140 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 3; |
|---|
| 141 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 1; |
|---|
| 142 | } |
|---|
| 143 | break; |
|---|
| 144 | case SAO_TYPE_EO_90: |
|---|
| 145 | { |
|---|
| 146 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 4; |
|---|
| 147 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 2; |
|---|
| 148 | |
|---|
| 149 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
|---|
| 150 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
|---|
| 151 | } |
|---|
| 152 | break; |
|---|
| 153 | case SAO_TYPE_EO_135: |
|---|
| 154 | case SAO_TYPE_EO_45: |
|---|
| 155 | { |
|---|
| 156 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
|---|
| 157 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
|---|
| 158 | |
|---|
| 159 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
|---|
| 160 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
|---|
| 161 | } |
|---|
| 162 | break; |
|---|
| 163 | case SAO_TYPE_BO: |
|---|
| 164 | { |
|---|
| 165 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 4; |
|---|
| 166 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 2; |
|---|
| 167 | |
|---|
| 168 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 3; |
|---|
| 169 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 1; |
|---|
| 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 | { |
|---|
| 208 | for(Int compIdx=0; compIdx< MAX_NUM_COMPONENT; compIdx++) |
|---|
| 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 | { |
|---|
| 220 | for(Int compIdx=0; compIdx< MAX_NUM_COMPONENT; compIdx++) |
|---|
| 221 | { |
|---|
| 222 | delete[] m_preDBFstatData[i][compIdx]; |
|---|
| 223 | } |
|---|
| 224 | delete[] m_preDBFstatData[i]; |
|---|
| 225 | } |
|---|
| 226 | delete[] m_preDBFstatData; m_preDBFstatData = NULL; |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | Void TEncSampleAdaptiveOffset::initRDOCabacCoder(TEncSbac* pcRDGoOnSbacCoder, TComSlice* pcSlice) |
|---|
| 231 | { |
|---|
| 232 | m_pcRDGoOnSbacCoder = pcRDGoOnSbacCoder; |
|---|
| 233 | m_pcRDGoOnSbacCoder->resetEntropy(pcSlice); |
|---|
| 234 | m_pcRDGoOnSbacCoder->resetBits(); |
|---|
| 235 | |
|---|
| 236 | m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[SAO_CABACSTATE_PIC_INIT]); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | Void TEncSampleAdaptiveOffset::SAOProcess(TComPic* pPic, Bool* sliceEnabled, const Double *lambdas, const Bool bTestSAODisableAtPictureLevel, const Double saoEncodingRate, const Double saoEncodingRateChroma, Bool isPreDBFSamplesUsed ) |
|---|
| 242 | { |
|---|
| 243 | TComPicYuv* orgYuv= pPic->getPicYuvOrg(); |
|---|
| 244 | TComPicYuv* resYuv= pPic->getPicYuvRec(); |
|---|
| 245 | memcpy(m_lambda, lambdas, sizeof(m_lambda)); |
|---|
| 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 | } |
|---|
| 257 | //slice on/off |
|---|
| 258 | decidePicParams(sliceEnabled, pPic->getSlice(0)->getDepth(), saoEncodingRate, saoEncodingRateChroma); |
|---|
| 259 | |
|---|
| 260 | //block on/off |
|---|
| 261 | SAOBlkParam* reconParams = new SAOBlkParam[m_numCTUsPic]; //temporary parameter buffer for storing reconstructed SAO parameters |
|---|
| 262 | decideBlkParams(pPic, sliceEnabled, m_statData, srcYuv, resYuv, reconParams, pPic->getPicSym()->getSAOBlkParam(), bTestSAODisableAtPictureLevel, saoEncodingRate, saoEncodingRateChroma); |
|---|
| 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 | { |
|---|
| 275 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
|---|
| 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 | |
|---|
| 285 | Void TEncSampleAdaptiveOffset::getStatistics(SAOStatData*** blkStats, TComPicYuv* orgYuv, TComPicYuv* srcYuv, TComPic* pPic, Bool isCalculatePreDeblockSamples) |
|---|
| 286 | { |
|---|
| 287 | Bool isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail; |
|---|
| 288 | |
|---|
| 289 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
|---|
| 290 | |
|---|
| 291 | for(Int ctuRsAddr= 0; ctuRsAddr < m_numCTUsPic; ctuRsAddr++) |
|---|
| 292 | { |
|---|
| 293 | Int yPos = (ctuRsAddr / m_numCTUInWidth)*m_maxCUHeight; |
|---|
| 294 | Int xPos = (ctuRsAddr % m_numCTUInWidth)*m_maxCUWidth; |
|---|
| 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 | |
|---|
| 298 | pPic->getPicSym()->deriveLoopFilterBoundaryAvailibility(ctuRsAddr, isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail); |
|---|
| 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 | |
|---|
| 309 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
|---|
| 310 | { |
|---|
| 311 | const ComponentID component = ComponentID(compIdx); |
|---|
| 312 | |
|---|
| 313 | const UInt componentScaleX = getComponentScaleX(component, pPic->getChromaFormat()); |
|---|
| 314 | const UInt componentScaleY = getComponentScaleY(component, pPic->getChromaFormat()); |
|---|
| 315 | |
|---|
| 316 | Int srcStride = srcYuv->getStride(component); |
|---|
| 317 | Pel* srcBlk = srcYuv->getAddr(component) + ((yPos >> componentScaleY) * srcStride) + (xPos >> componentScaleX); |
|---|
| 318 | |
|---|
| 319 | Int orgStride = orgYuv->getStride(component); |
|---|
| 320 | Pel* orgBlk = orgYuv->getAddr(component) + ((yPos >> componentScaleY) * orgStride) + (xPos >> componentScaleX); |
|---|
| 321 | |
|---|
| 322 | getBlkStats(component, pPic->getPicSym()->getSPS().getBitDepth(toChannelType(component)), blkStats[ctuRsAddr][component] |
|---|
| 323 | , srcBlk, orgBlk, srcStride, orgStride, (width >> componentScaleX), (height >> componentScaleY) |
|---|
| 324 | , isLeftAvail, isRightAvail, isAboveAvail, isBelowAvail, isAboveLeftAvail, isAboveRightAvail |
|---|
| 325 | , isCalculatePreDeblockSamples |
|---|
| 326 | ); |
|---|
| 327 | |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | Void TEncSampleAdaptiveOffset::decidePicParams(Bool* sliceEnabled, Int picTempLayer, const Double saoEncodingRate, const Double saoEncodingRateChroma) |
|---|
| 333 | { |
|---|
| 334 | //decide sliceEnabled[compIdx] |
|---|
| 335 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
|---|
| 336 | for (Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
|---|
| 337 | { |
|---|
| 338 | sliceEnabled[compIdx] = false; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
|---|
| 342 | { |
|---|
| 343 | // reset flags & counters |
|---|
| 344 | sliceEnabled[compIdx] = true; |
|---|
| 345 | |
|---|
| 346 | if (saoEncodingRate>0.0) |
|---|
| 347 | { |
|---|
| 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 | } |
|---|
| 366 | } |
|---|
| 367 | } |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | Int64 TEncSampleAdaptiveOffset::getDistortion(const Int channelBitDepth, Int typeIdc, Int typeAuxInfo, Int* invQuantOffset, SAOStatData& statData) |
|---|
| 371 | { |
|---|
| 372 | Int64 dist = 0; |
|---|
| 373 | Int shift = 2 * DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth - 8); |
|---|
| 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); |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | break; |
|---|
| 388 | case SAO_TYPE_BO: |
|---|
| 389 | { |
|---|
| 390 | for (Int offsetIdx=typeAuxInfo; offsetIdx<typeAuxInfo+4; offsetIdx++) |
|---|
| 391 | { |
|---|
| 392 | Int bandIdx = offsetIdx % NUM_SAO_BO_CLASSES ; |
|---|
| 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 | |
|---|
| 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 ) |
|---|
| 415 | { |
|---|
| 416 | Int iterOffset, tempOffset; |
|---|
| 417 | Int64 tempDist, tempRate; |
|---|
| 418 | Double tempCost, tempMinCost; |
|---|
| 419 | Int offsetOutput = 0; |
|---|
| 420 | iterOffset = offsetInput; |
|---|
| 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; |
|---|
| 423 | while (iterOffset != 0) |
|---|
| 424 | { |
|---|
| 425 | // Calculate the bits required for signaling the offset |
|---|
| 426 | tempRate = (typeIdx == SAO_TYPE_BO) ? (abs((Int)iterOffset)+2) : (abs((Int)iterOffset)+1); |
|---|
| 427 | if (abs((Int)iterOffset)==offsetTh) //inclusive |
|---|
| 428 | { |
|---|
| 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 | |
|---|
| 447 | Void TEncSampleAdaptiveOffset::deriveOffsets(ComponentID compIdx, const Int channelBitDepth, Int typeIdc, SAOStatData& statData, Int* quantOffsets, Int& typeAuxInfo) |
|---|
| 448 | { |
|---|
| 449 | Int bitDepth = channelBitDepth; |
|---|
| 450 | Int shift = 2 * DISTORTION_PRECISION_ADJUSTMENT(bitDepth-8); |
|---|
| 451 | Int offsetTh = TComSampleAdaptiveOffset::getMaxOffsetQVal(channelBitDepth); //inclusive |
|---|
| 452 | |
|---|
| 453 | ::memset(quantOffsets, 0, sizeof(Int)*MAX_NUM_SAO_CLASSES); |
|---|
| 454 | |
|---|
| 455 | //derive initial offsets |
|---|
| 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 | { |
|---|
| 459 | if( (typeIdc != SAO_TYPE_BO) && (classIdx==SAO_CLASS_EO_PLAIN) ) |
|---|
| 460 | { |
|---|
| 461 | continue; //offset will be zero |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | if(statData.count[classIdx] == 0) |
|---|
| 465 | { |
|---|
| 466 | continue; //offset will be zero |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | quantOffsets[classIdx] = (Int) xRoundIbdi(bitDepth, (Double)( statData.diff[classIdx]<<(bitDepth-8)) |
|---|
| 470 | / |
|---|
| 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; |
|---|
| 486 | for(Int classIdx=0; classIdx<NUM_SAO_EO_CLASSES; classIdx++) |
|---|
| 487 | { |
|---|
| 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 | } |
|---|
| 504 | |
|---|
| 505 | if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero |
|---|
| 506 | { |
|---|
| 507 | quantOffsets[classIdx] = estIterOffset( typeIdc, m_lambda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], classDist , classCost , offsetTh ); |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 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++) |
|---|
| 520 | { |
|---|
| 521 | costBOClasses[classIdx]= m_lambda[compIdx]; |
|---|
| 522 | if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero |
|---|
| 523 | { |
|---|
| 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 ); |
|---|
| 525 | } |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | //decide the starting band index |
|---|
| 529 | Double minCost = MAX_DOUBLE, cost; |
|---|
| 530 | for(Int band=0; band< NUM_SAO_BO_CLASSES- 4+ 1; band++) |
|---|
| 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); |
|---|
| 546 | for(Int i=0; i< 4; i++) |
|---|
| 547 | { |
|---|
| 548 | Int band = (typeAuxInfo+i)%NUM_SAO_BO_CLASSES; |
|---|
| 549 | clearQuantOffset[band] = quantOffsets[band]; |
|---|
| 550 | } |
|---|
| 551 | ::memcpy(quantOffsets, clearQuantOffset, sizeof(Int)*NUM_SAO_BO_CLASSES); |
|---|
| 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 | |
|---|
| 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) |
|---|
| 567 | { |
|---|
| 568 | Double minCost, cost; |
|---|
| 569 | UInt previousWrittenBits; |
|---|
| 570 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
|---|
| 571 | |
|---|
| 572 | Int64 dist[MAX_NUM_COMPONENT], modeDist[MAX_NUM_COMPONENT]; |
|---|
| 573 | SAOOffset testOffset[MAX_NUM_COMPONENT]; |
|---|
| 574 | Int invQuantOffset[MAX_NUM_SAO_CLASSES]; |
|---|
| 575 | for(Int comp=0; comp < MAX_NUM_COMPONENT; comp++) |
|---|
| 576 | { |
|---|
| 577 | modeDist[comp] = 0; |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | //pre-encode merge flags |
|---|
| 581 | modeParam[COMPONENT_Y].modeIdc = SAO_MODE_OFF; |
|---|
| 582 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
|---|
| 583 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(modeParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), true); |
|---|
| 584 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
|---|
| 585 | |
|---|
| 586 | //------ luma --------// |
|---|
| 587 | { |
|---|
| 588 | const ComponentID compIdx = COMPONENT_Y; |
|---|
| 589 | //"off" case as initial cost |
|---|
| 590 | modeParam[compIdx].modeIdc = SAO_MODE_OFF; |
|---|
| 591 | m_pcRDGoOnSbacCoder->resetBits(); |
|---|
| 592 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, modeParam[compIdx], sliceEnabled[compIdx], bitDepths.recon[CHANNEL_TYPE_LUMA]); |
|---|
| 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]) |
|---|
| 597 | { |
|---|
| 598 | for(Int typeIdc=0; typeIdc< NUM_SAO_NEW_TYPES; typeIdc++) |
|---|
| 599 | { |
|---|
| 600 | testOffset[compIdx].modeIdc = SAO_MODE_NEW; |
|---|
| 601 | testOffset[compIdx].typeIdc = typeIdc; |
|---|
| 602 | |
|---|
| 603 | //derive coded offset |
|---|
| 604 | deriveOffsets(compIdx, bitDepths.recon[CHANNEL_TYPE_LUMA], typeIdc, blkStats[ctuRsAddr][compIdx][typeIdc], testOffset[compIdx].offset, testOffset[compIdx].typeAuxInfo); |
|---|
| 605 | |
|---|
| 606 | //inversed quantized offsets |
|---|
| 607 | invertQuantOffsets(compIdx, typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, testOffset[compIdx].offset); |
|---|
| 608 | |
|---|
| 609 | //get distortion |
|---|
| 610 | dist[compIdx] = getDistortion(bitDepths.recon[CHANNEL_TYPE_LUMA], testOffset[compIdx].typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, blkStats[ctuRsAddr][compIdx][typeIdc]); |
|---|
| 611 | |
|---|
| 612 | //get rate |
|---|
| 613 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
|---|
| 614 | m_pcRDGoOnSbacCoder->resetBits(); |
|---|
| 615 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, testOffset[compIdx], sliceEnabled[compIdx], bitDepths.recon[CHANNEL_TYPE_LUMA]); |
|---|
| 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 | } |
|---|
| 625 | } |
|---|
| 626 | } |
|---|
| 627 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
|---|
| 628 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | //------ chroma --------// |
|---|
| 632 | //"off" case as initial cost |
|---|
| 633 | cost = 0; |
|---|
| 634 | previousWrittenBits = 0; |
|---|
| 635 | m_pcRDGoOnSbacCoder->resetBits(); |
|---|
| 636 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
|---|
| 637 | { |
|---|
| 638 | const ComponentID component = ComponentID(componentIndex); |
|---|
| 639 | |
|---|
| 640 | modeParam[component].modeIdc = SAO_MODE_OFF; |
|---|
| 641 | modeDist [component] = 0; |
|---|
| 642 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(component, modeParam[component], sliceEnabled[component], bitDepths.recon[CHANNEL_TYPE_CHROMA]); |
|---|
| 643 | |
|---|
| 644 | const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
|---|
| 645 | cost += m_lambda[component] * (currentWrittenBits - previousWrittenBits); |
|---|
| 646 | previousWrittenBits = currentWrittenBits; |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | minCost = cost; |
|---|
| 650 | |
|---|
| 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 | { |
|---|
| 655 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
|---|
| 656 | m_pcRDGoOnSbacCoder->resetBits(); |
|---|
| 657 | previousWrittenBits = 0; |
|---|
| 658 | cost = 0; |
|---|
| 659 | |
|---|
| 660 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
|---|
| 661 | { |
|---|
| 662 | const ComponentID component = ComponentID(componentIndex); |
|---|
| 663 | if(!sliceEnabled[component]) |
|---|
| 664 | { |
|---|
| 665 | testOffset[component].modeIdc = SAO_MODE_OFF; |
|---|
| 666 | dist[component]= 0; |
|---|
| 667 | continue; |
|---|
| 668 | } |
|---|
| 669 | testOffset[component].modeIdc = SAO_MODE_NEW; |
|---|
| 670 | testOffset[component].typeIdc = typeIdc; |
|---|
| 671 | |
|---|
| 672 | //derive offset & get distortion |
|---|
| 673 | deriveOffsets(component, bitDepths.recon[CHANNEL_TYPE_CHROMA], typeIdc, blkStats[ctuRsAddr][component][typeIdc], testOffset[component].offset, testOffset[component].typeAuxInfo); |
|---|
| 674 | invertQuantOffsets(component, typeIdc, testOffset[component].typeAuxInfo, invQuantOffset, testOffset[component].offset); |
|---|
| 675 | dist[component] = getDistortion(bitDepths.recon[CHANNEL_TYPE_CHROMA], typeIdc, testOffset[component].typeAuxInfo, invQuantOffset, blkStats[ctuRsAddr][component][typeIdc]); |
|---|
| 676 | |
|---|
| 677 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(component, testOffset[component], sliceEnabled[component], bitDepths.recon[CHANNEL_TYPE_CHROMA]); |
|---|
| 678 | |
|---|
| 679 | const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
|---|
| 680 | cost += dist[component] + (m_lambda[component] * (currentWrittenBits - previousWrittenBits)); |
|---|
| 681 | previousWrittenBits = currentWrittenBits; |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | if(cost < minCost) |
|---|
| 685 | { |
|---|
| 686 | minCost = cost; |
|---|
| 687 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
|---|
| 688 | { |
|---|
| 689 | modeDist[componentIndex] = dist[componentIndex]; |
|---|
| 690 | modeParam[componentIndex] = testOffset[componentIndex]; |
|---|
| 691 | } |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | } // SAO_TYPE loop |
|---|
| 695 | |
|---|
| 696 | //----- re-gen rate & normalized cost----// |
|---|
| 697 | modeNormCost = 0; |
|---|
| 698 | for(UInt componentIndex = COMPONENT_Y; componentIndex < numberOfComponents; componentIndex++) |
|---|
| 699 | { |
|---|
| 700 | modeNormCost += (Double)modeDist[componentIndex] / m_lambda[componentIndex]; |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
|---|
| 704 | m_pcRDGoOnSbacCoder->resetBits(); |
|---|
| 705 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(modeParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), false); |
|---|
| 706 | modeNormCost += (Double)m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 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) |
|---|
| 710 | { |
|---|
| 711 | modeNormCost = MAX_DOUBLE; |
|---|
| 712 | |
|---|
| 713 | Double cost; |
|---|
| 714 | SAOBlkParam testBlkParam; |
|---|
| 715 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
|---|
| 716 | |
|---|
| 717 | for(Int mergeType=0; mergeType< NUM_SAO_MERGE_TYPES; mergeType++) |
|---|
| 718 | { |
|---|
| 719 | if(mergeList[mergeType] == NULL) |
|---|
| 720 | { |
|---|
| 721 | continue; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | testBlkParam = *(mergeList[mergeType]); |
|---|
| 725 | //normalized distortion |
|---|
| 726 | Double normDist=0; |
|---|
| 727 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
|---|
| 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. |
|---|
| 737 | normDist += (((Double)getDistortion(bitDepths.recon[toChannelType(ComponentID(compIdx))], mergedOffsetParam.typeIdc, mergedOffsetParam.typeAuxInfo, mergedOffsetParam.offset, blkStats[ctuRsAddr][compIdx][mergedOffsetParam.typeIdc])) |
|---|
| 738 | /m_lambda[compIdx] |
|---|
| 739 | ); |
|---|
| 740 | } |
|---|
| 741 | |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | //rate |
|---|
| 745 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
|---|
| 746 | m_pcRDGoOnSbacCoder->resetBits(); |
|---|
| 747 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(testBlkParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), false); |
|---|
| 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 | |
|---|
| 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) |
|---|
| 766 | { |
|---|
| 767 | Bool allBlksDisabled = true; |
|---|
| 768 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
|---|
| 769 | for(Int compId = COMPONENT_Y; compId < numberOfComponents; compId++) |
|---|
| 770 | { |
|---|
| 771 | if (sliceEnabled[compId]) |
|---|
| 772 | { |
|---|
| 773 | allBlksDisabled = false; |
|---|
| 774 | } |
|---|
| 775 | } |
|---|
| 776 | |
|---|
| 777 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_PIC_INIT ]); |
|---|
| 778 | |
|---|
| 779 | SAOBlkParam modeParam; |
|---|
| 780 | Double minCost, modeCost; |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | Double totalCost = 0; // Used if bTestSAODisableAtPictureLevel==true |
|---|
| 784 | |
|---|
| 785 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
|---|
| 786 | { |
|---|
| 787 | if(allBlksDisabled) |
|---|
| 788 | { |
|---|
| 789 | codedParams[ctuRsAddr].reset(); |
|---|
| 790 | continue; |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_CUR ]); |
|---|
| 794 | |
|---|
| 795 | //get merge list |
|---|
| 796 | SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES] = { NULL }; |
|---|
| 797 | getMergeList(pic, ctuRsAddr, reconParams, mergeList); |
|---|
| 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 | { |
|---|
| 811 | deriveModeNewRDO(pic->getPicSym()->getSPS().getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats, modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
|---|
| 812 | } |
|---|
| 813 | break; |
|---|
| 814 | case SAO_MODE_MERGE: |
|---|
| 815 | { |
|---|
| 816 | deriveModeMergeRDO(pic->getPicSym()->getSPS().getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats , modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
|---|
| 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; |
|---|
| 830 | codedParams[ctuRsAddr] = modeParam; |
|---|
| 831 | m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_NEXT ]); |
|---|
| 832 | } |
|---|
| 833 | } //mode |
|---|
| 834 | |
|---|
| 835 | totalCost += minCost; |
|---|
| 836 | |
|---|
| 837 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_NEXT ]); |
|---|
| 838 | |
|---|
| 839 | //apply reconstructed offsets |
|---|
| 840 | reconParams[ctuRsAddr] = codedParams[ctuRsAddr]; |
|---|
| 841 | reconstructBlkSAOParam(reconParams[ctuRsAddr], mergeList); |
|---|
| 842 | offsetCTU(ctuRsAddr, srcYuv, resYuv, reconParams[ctuRsAddr], pic); |
|---|
| 843 | } //ctuRsAddr |
|---|
| 844 | |
|---|
| 845 | if (!allBlksDisabled && (totalCost >= 0) && bTestSAODisableAtPictureLevel) //SAO has not beneficial in this case - disable it |
|---|
| 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 | |
|---|
| 860 | if (saoEncodingRate > 0.0) |
|---|
| 861 | { |
|---|
| 862 | Int picTempLayer = pic->getSlice(0)->getDepth(); |
|---|
| 863 | Int numCtusForSAOOff[MAX_NUM_COMPONENT]; |
|---|
| 864 | |
|---|
| 865 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
|---|
| 866 | { |
|---|
| 867 | numCtusForSAOOff[compIdx] = 0; |
|---|
| 868 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
|---|
| 869 | { |
|---|
| 870 | if( reconParams[ctuRsAddr][compIdx].modeIdc == SAO_MODE_OFF) |
|---|
| 871 | { |
|---|
| 872 | numCtusForSAOOff[compIdx]++; |
|---|
| 873 | } |
|---|
| 874 | } |
|---|
| 875 | } |
|---|
| 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 | } |
|---|
| 887 | } |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | Void TEncSampleAdaptiveOffset::getBlkStats(const ComponentID compIdx, const Int channelBitDepth, SAOStatData* statsDataTypes |
|---|
| 892 | , Pel* srcBlk, Pel* orgBlk, Int srcStride, Int orgStride, Int width, Int height |
|---|
| 893 | , Bool isLeftAvail, Bool isRightAvail, Bool isAboveAvail, Bool isBelowAvail, Bool isAboveLeftAvail, Bool isAboveRightAvail |
|---|
| 894 | , Bool isCalculatePreDeblockSamples |
|---|
| 895 | ) |
|---|
| 896 | { |
|---|
| 897 | if(m_lineBufWidth != m_maxCUWidth) |
|---|
| 898 | { |
|---|
| 899 | m_lineBufWidth = m_maxCUWidth; |
|---|
| 900 | |
|---|
| 901 | if (m_signLineBuf1) |
|---|
| 902 | { |
|---|
| 903 | delete[] m_signLineBuf1; |
|---|
| 904 | m_signLineBuf1 = NULL; |
|---|
| 905 | } |
|---|
| 906 | m_signLineBuf1 = new SChar[m_lineBufWidth+1]; |
|---|
| 907 | |
|---|
| 908 | if (m_signLineBuf2) |
|---|
| 909 | { |
|---|
| 910 | delete[] m_signLineBuf2; |
|---|
| 911 | m_signLineBuf2 = NULL; |
|---|
| 912 | } |
|---|
| 913 | m_signLineBuf2 = new SChar[m_lineBufWidth+1]; |
|---|
| 914 | } |
|---|
| 915 | |
|---|
| 916 | Int x,y, startX, startY, endX, endY, edgeType, firstLineStartX, firstLineEndX; |
|---|
| 917 | SChar signLeft, signRight, signDown; |
|---|
| 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 | { |
|---|
| 947 | signLeft = (SChar)sgn(srcLine[startX] - srcLine[startX-1]); |
|---|
| 948 | for (x=startX; x<endX; x++) |
|---|
| 949 | { |
|---|
| 950 | signRight = (SChar)sgn(srcLine[x] - srcLine[x+1]); |
|---|
| 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 | { |
|---|
| 969 | signLeft = (SChar)sgn(srcLine[startX] - srcLine[startX-1]); |
|---|
| 970 | for (x=startX; x<endX; x++) |
|---|
| 971 | { |
|---|
| 972 | signRight = (SChar)sgn(srcLine[x] - srcLine[x+1]); |
|---|
| 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; |
|---|
| 990 | SChar *signUpLine = m_signLineBuf1; |
|---|
| 991 | |
|---|
| 992 | startX = (!isCalculatePreDeblockSamples) ? 0 |
|---|
| 993 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : width) |
|---|
| 994 | ; |
|---|
| 995 | startY = isAboveAvail ? 0 : 1; |
|---|
| 996 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]) : width) |
|---|
| 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; |
|---|
| 1007 | for (x=startX; x<endX; x++) |
|---|
| 1008 | { |
|---|
| 1009 | signUpLine[x] = (SChar)sgn(srcLine[x] - srcLineAbove[x]); |
|---|
| 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 | { |
|---|
| 1019 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x]); |
|---|
| 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 | { |
|---|
| 1043 | edgeType = sgn(srcLine[x] - srcLineBelow[x]) + sgn(srcLine[x] - srcLineAbove[x]); |
|---|
| 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; |
|---|
| 1059 | SChar *signUpLine, *signDownLine, *signTmpLine; |
|---|
| 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 | { |
|---|
| 1077 | signUpLine[x] = (SChar)sgn(srcLineBelow[x] - srcLine[x-1]); |
|---|
| 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 | { |
|---|
| 1086 | edgeType = sgn(srcLine[x] - srcLineAbove[x-1]) - signUpLine[x+1]; |
|---|
| 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 | { |
|---|
| 1101 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x+1]); |
|---|
| 1102 | edgeType = signDown + signUpLine[x]; |
|---|
| 1103 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
|---|
| 1104 | count[edgeType] ++; |
|---|
| 1105 | |
|---|
| 1106 | signDownLine[x+1] = -signDown; |
|---|
| 1107 | } |
|---|
| 1108 | signDownLine[startX] = (SChar)sgn(srcLineBelow[startX] - srcLine[startX-1]); |
|---|
| 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 | { |
|---|
| 1131 | edgeType = sgn(srcLine[x] - srcLineBelow[x+1]) + sgn(srcLine[x] - srcLineAbove[x-1]); |
|---|
| 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; |
|---|
| 1146 | SChar *signUpLine = m_signLineBuf1+1; |
|---|
| 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 | { |
|---|
| 1160 | signUpLine[x] = (SChar)sgn(srcLineBelow[x] - srcLine[x+1]); |
|---|
| 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 | { |
|---|
| 1174 | edgeType = sgn(srcLine[x] - srcLineAbove[x+1]) - signUpLine[x-1]; |
|---|
| 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 | { |
|---|
| 1189 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x-1]); |
|---|
| 1190 | edgeType = signDown + signUpLine[x]; |
|---|
| 1191 | |
|---|
| 1192 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
|---|
| 1193 | count[edgeType] ++; |
|---|
| 1194 | |
|---|
| 1195 | signUpLine[x-1] = -signDown; |
|---|
| 1196 | } |
|---|
| 1197 | signUpLine[endX-1] = (SChar)sgn(srcLineBelow[endX-1] - srcLine[endX]); |
|---|
| 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 | { |
|---|
| 1215 | edgeType = sgn(srcLine[x] - srcLineBelow[x-1]) + sgn(srcLine[x] - srcLineAbove[x+1]); |
|---|
| 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; |
|---|
| 1235 | Int shiftBits = channelBitDepth - NUM_SAO_BO_CLASSES_LOG2; |
|---|
| 1236 | for (y=0; y< endY; y++) |
|---|
| 1237 | { |
|---|
| 1238 | for (x=startX; x< endX; x++) |
|---|
| 1239 | { |
|---|
| 1240 | |
|---|
| 1241 | Int bandIdx= srcLine[x] >> shiftBits; |
|---|
| 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 | { |
|---|
| 1259 | Int bandIdx= srcLine[x] >> shiftBits; |
|---|
| 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 | |
|---|
| 1282 | |
|---|
| 1283 | //! \} |
|---|