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