[191] | 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-2013, 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 | /** \file TEncCu.cpp |
---|
| 35 | \brief Coding Unit (CU) encoder class |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include <stdio.h> |
---|
| 39 | #include "TEncTop.h" |
---|
| 40 | #include "TEncCu.h" |
---|
| 41 | #include "TEncAnalyze.h" |
---|
| 42 | |
---|
| 43 | #include <cmath> |
---|
| 44 | #include <algorithm> |
---|
| 45 | using namespace std; |
---|
| 46 | |
---|
| 47 | //! \ingroup TLibEncoder |
---|
| 48 | //! \{ |
---|
| 49 | |
---|
| 50 | // ==================================================================================================================== |
---|
| 51 | // Constructor / destructor / create / destroy |
---|
| 52 | // ==================================================================================================================== |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | \param uiTotalDepth total number of allowable depth |
---|
| 56 | \param uiMaxWidth largest CU width |
---|
| 57 | \param uiMaxHeight largest CU height |
---|
| 58 | */ |
---|
| 59 | Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight) |
---|
| 60 | { |
---|
| 61 | Int i; |
---|
| 62 | |
---|
| 63 | m_uhTotalDepth = uhTotalDepth + 1; |
---|
| 64 | m_ppcBestCU = new TComDataCU*[m_uhTotalDepth-1]; |
---|
| 65 | m_ppcTempCU = new TComDataCU*[m_uhTotalDepth-1]; |
---|
| 66 | |
---|
| 67 | m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1]; |
---|
| 68 | m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1]; |
---|
| 69 | m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1]; |
---|
| 70 | m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1]; |
---|
| 71 | m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1]; |
---|
| 72 | m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1]; |
---|
| 73 | m_ppcOrigYuv = new TComYuv*[m_uhTotalDepth-1]; |
---|
| 74 | |
---|
| 75 | UInt uiNumPartitions; |
---|
| 76 | for( i=0 ; i<m_uhTotalDepth-1 ; i++) |
---|
| 77 | { |
---|
| 78 | uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 ); |
---|
| 79 | UInt uiWidth = uiMaxWidth >> i; |
---|
| 80 | UInt uiHeight = uiMaxHeight >> i; |
---|
| 81 | |
---|
| 82 | m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) ); |
---|
| 83 | m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) ); |
---|
| 84 | |
---|
| 85 | m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight); |
---|
| 86 | m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight); |
---|
| 87 | m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight); |
---|
| 88 | |
---|
| 89 | m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight); |
---|
| 90 | m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight); |
---|
| 91 | m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight); |
---|
| 92 | |
---|
| 93 | m_ppcOrigYuv [i] = new TComYuv; m_ppcOrigYuv [i]->create(uiWidth, uiHeight); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | m_bEncodeDQP = false; |
---|
| 97 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 98 | m_LCUPredictionSAD = 0; |
---|
| 99 | m_addSADDepth = 0; |
---|
| 100 | m_temporalSAD = 0; |
---|
| 101 | #endif |
---|
| 102 | |
---|
| 103 | // initialize partition order. |
---|
| 104 | UInt* piTmp = &g_auiZscanToRaster[0]; |
---|
| 105 | initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp); |
---|
| 106 | initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth ); |
---|
| 107 | |
---|
| 108 | // initialize conversion matrix from partition index to pel |
---|
| 109 | initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth ); |
---|
[271] | 110 | |
---|
[191] | 111 | } |
---|
| 112 | |
---|
| 113 | Void TEncCu::destroy() |
---|
| 114 | { |
---|
| 115 | Int i; |
---|
| 116 | |
---|
| 117 | for( i=0 ; i<m_uhTotalDepth-1 ; i++) |
---|
| 118 | { |
---|
| 119 | if(m_ppcBestCU[i]) |
---|
| 120 | { |
---|
| 121 | m_ppcBestCU[i]->destroy(); delete m_ppcBestCU[i]; m_ppcBestCU[i] = NULL; |
---|
| 122 | } |
---|
| 123 | if(m_ppcTempCU[i]) |
---|
| 124 | { |
---|
| 125 | m_ppcTempCU[i]->destroy(); delete m_ppcTempCU[i]; m_ppcTempCU[i] = NULL; |
---|
| 126 | } |
---|
| 127 | if(m_ppcPredYuvBest[i]) |
---|
| 128 | { |
---|
| 129 | m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL; |
---|
| 130 | } |
---|
| 131 | if(m_ppcResiYuvBest[i]) |
---|
| 132 | { |
---|
| 133 | m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL; |
---|
| 134 | } |
---|
| 135 | if(m_ppcRecoYuvBest[i]) |
---|
| 136 | { |
---|
| 137 | m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL; |
---|
| 138 | } |
---|
| 139 | if(m_ppcPredYuvTemp[i]) |
---|
| 140 | { |
---|
| 141 | m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL; |
---|
| 142 | } |
---|
| 143 | if(m_ppcResiYuvTemp[i]) |
---|
| 144 | { |
---|
| 145 | m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL; |
---|
| 146 | } |
---|
| 147 | if(m_ppcRecoYuvTemp[i]) |
---|
| 148 | { |
---|
| 149 | m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL; |
---|
| 150 | } |
---|
| 151 | if(m_ppcOrigYuv[i]) |
---|
| 152 | { |
---|
| 153 | m_ppcOrigYuv[i]->destroy(); delete m_ppcOrigYuv[i]; m_ppcOrigYuv[i] = NULL; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | if(m_ppcBestCU) |
---|
| 157 | { |
---|
| 158 | delete [] m_ppcBestCU; |
---|
| 159 | m_ppcBestCU = NULL; |
---|
| 160 | } |
---|
| 161 | if(m_ppcTempCU) |
---|
| 162 | { |
---|
| 163 | delete [] m_ppcTempCU; |
---|
| 164 | m_ppcTempCU = NULL; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if(m_ppcPredYuvBest) |
---|
| 168 | { |
---|
| 169 | delete [] m_ppcPredYuvBest; |
---|
| 170 | m_ppcPredYuvBest = NULL; |
---|
| 171 | } |
---|
| 172 | if(m_ppcResiYuvBest) |
---|
| 173 | { |
---|
| 174 | delete [] m_ppcResiYuvBest; |
---|
| 175 | m_ppcResiYuvBest = NULL; |
---|
| 176 | } |
---|
| 177 | if(m_ppcRecoYuvBest) |
---|
| 178 | { |
---|
| 179 | delete [] m_ppcRecoYuvBest; |
---|
| 180 | m_ppcRecoYuvBest = NULL; |
---|
| 181 | } |
---|
| 182 | if(m_ppcPredYuvTemp) |
---|
| 183 | { |
---|
| 184 | delete [] m_ppcPredYuvTemp; |
---|
| 185 | m_ppcPredYuvTemp = NULL; |
---|
| 186 | } |
---|
| 187 | if(m_ppcResiYuvTemp) |
---|
| 188 | { |
---|
| 189 | delete [] m_ppcResiYuvTemp; |
---|
| 190 | m_ppcResiYuvTemp = NULL; |
---|
| 191 | } |
---|
| 192 | if(m_ppcRecoYuvTemp) |
---|
| 193 | { |
---|
| 194 | delete [] m_ppcRecoYuvTemp; |
---|
| 195 | m_ppcRecoYuvTemp = NULL; |
---|
| 196 | } |
---|
| 197 | if(m_ppcOrigYuv) |
---|
| 198 | { |
---|
| 199 | delete [] m_ppcOrigYuv; |
---|
| 200 | m_ppcOrigYuv = NULL; |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** \param pcEncTop pointer of encoder class |
---|
| 205 | */ |
---|
| 206 | Void TEncCu::init( TEncTop* pcEncTop ) |
---|
| 207 | { |
---|
| 208 | m_pcEncCfg = pcEncTop; |
---|
| 209 | m_pcPredSearch = pcEncTop->getPredSearch(); |
---|
| 210 | m_pcTrQuant = pcEncTop->getTrQuant(); |
---|
| 211 | m_pcBitCounter = pcEncTop->getBitCounter(); |
---|
| 212 | m_pcRdCost = pcEncTop->getRdCost(); |
---|
| 213 | |
---|
| 214 | #if SVC_EXTENSION |
---|
| 215 | m_ppcTEncTop = pcEncTop->getLayerEnc(); |
---|
| 216 | for(UInt i=0 ; i< m_uhTotalDepth-1 ; i++) |
---|
| 217 | { |
---|
| 218 | m_ppcBestCU[i]->setLayerId(pcEncTop->getLayerId()); |
---|
| 219 | m_ppcTempCU[i]->setLayerId(pcEncTop->getLayerId()); |
---|
| 220 | } |
---|
| 221 | #endif |
---|
| 222 | |
---|
| 223 | m_pcEntropyCoder = pcEncTop->getEntropyCoder(); |
---|
| 224 | m_pcCavlcCoder = pcEncTop->getCavlcCoder(); |
---|
| 225 | m_pcSbacCoder = pcEncTop->getSbacCoder(); |
---|
| 226 | m_pcBinCABAC = pcEncTop->getBinCABAC(); |
---|
| 227 | |
---|
| 228 | m_pppcRDSbacCoder = pcEncTop->getRDSbacCoder(); |
---|
| 229 | m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder(); |
---|
| 230 | |
---|
| 231 | m_bUseSBACRD = pcEncTop->getUseSBACRD(); |
---|
| 232 | m_pcRateCtrl = pcEncTop->getRateCtrl(); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | // ==================================================================================================================== |
---|
| 236 | // Public member functions |
---|
| 237 | // ==================================================================================================================== |
---|
| 238 | |
---|
| 239 | /** \param rpcCU pointer of CU data class |
---|
| 240 | */ |
---|
| 241 | Void TEncCu::compressCU( TComDataCU*& rpcCU ) |
---|
| 242 | { |
---|
| 243 | // initialize CU data |
---|
| 244 | m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() ); |
---|
| 245 | m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() ); |
---|
| 246 | |
---|
| 247 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 248 | m_addSADDepth = 0; |
---|
| 249 | m_LCUPredictionSAD = 0; |
---|
| 250 | m_temporalSAD = 0; |
---|
| 251 | #endif |
---|
| 252 | |
---|
| 253 | // analysis of CU |
---|
| 254 | xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 ); |
---|
| 255 | |
---|
| 256 | #if ADAPTIVE_QP_SELECTION |
---|
| 257 | if( m_pcEncCfg->getUseAdaptQpSelect() ) |
---|
| 258 | { |
---|
| 259 | if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII |
---|
| 260 | { |
---|
| 261 | xLcuCollectARLStats( rpcCU); |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | #endif |
---|
| 265 | } |
---|
| 266 | /** \param pcCU pointer of CU data class |
---|
| 267 | */ |
---|
| 268 | Void TEncCu::encodeCU ( TComDataCU* pcCU ) |
---|
| 269 | { |
---|
| 270 | if ( pcCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 271 | { |
---|
| 272 | setdQPFlag(true); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | // Encode CU data |
---|
| 276 | xEncodeCU( pcCU, 0, 0 ); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | // ==================================================================================================================== |
---|
| 280 | // Protected member functions |
---|
| 281 | // ==================================================================================================================== |
---|
| 282 | /** Derive small set of test modes for AMP encoder speed-up |
---|
| 283 | *\param rpcBestCU |
---|
| 284 | *\param eParentPartSize |
---|
| 285 | *\param bTestAMP_Hor |
---|
| 286 | *\param bTestAMP_Ver |
---|
| 287 | *\param bTestMergeAMP_Hor |
---|
| 288 | *\param bTestMergeAMP_Ver |
---|
| 289 | *\returns Void |
---|
| 290 | */ |
---|
| 291 | #if AMP_ENC_SPEEDUP |
---|
| 292 | #if AMP_MRG |
---|
| 293 | Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver) |
---|
| 294 | #else |
---|
| 295 | Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver) |
---|
| 296 | #endif |
---|
| 297 | { |
---|
| 298 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN ) |
---|
| 299 | { |
---|
| 300 | bTestAMP_Hor = true; |
---|
| 301 | } |
---|
| 302 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
| 303 | { |
---|
| 304 | bTestAMP_Ver = true; |
---|
| 305 | } |
---|
| 306 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false ) |
---|
| 307 | { |
---|
| 308 | bTestAMP_Hor = true; |
---|
| 309 | bTestAMP_Ver = true; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | #if AMP_MRG |
---|
| 313 | //! Utilizing the partition size of parent PU |
---|
| 314 | if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N ) |
---|
| 315 | { |
---|
| 316 | bTestMergeAMP_Hor = true; |
---|
| 317 | bTestMergeAMP_Ver = true; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | if ( eParentPartSize == SIZE_NONE ) //! if parent is intra |
---|
| 321 | { |
---|
| 322 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN ) |
---|
| 323 | { |
---|
| 324 | bTestMergeAMP_Hor = true; |
---|
| 325 | } |
---|
| 326 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
| 327 | { |
---|
| 328 | bTestMergeAMP_Ver = true; |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false ) |
---|
| 333 | { |
---|
| 334 | bTestMergeAMP_Hor = true; |
---|
| 335 | bTestMergeAMP_Ver = true; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | if ( rpcBestCU->getWidth(0) == 64 ) |
---|
| 339 | { |
---|
| 340 | bTestAMP_Hor = false; |
---|
| 341 | bTestAMP_Ver = false; |
---|
| 342 | } |
---|
| 343 | #else |
---|
| 344 | //! Utilizing the partition size of parent PU |
---|
| 345 | if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N ) |
---|
| 346 | { |
---|
| 347 | bTestAMP_Hor = true; |
---|
| 348 | bTestAMP_Ver = true; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | if ( eParentPartSize == SIZE_2Nx2N ) |
---|
| 352 | { |
---|
| 353 | bTestAMP_Hor = false; |
---|
| 354 | bTestAMP_Ver = false; |
---|
| 355 | } |
---|
| 356 | #endif |
---|
| 357 | } |
---|
| 358 | #endif |
---|
| 359 | |
---|
| 360 | // ==================================================================================================================== |
---|
| 361 | // Protected member functions |
---|
| 362 | // ==================================================================================================================== |
---|
| 363 | /** Compress a CU block recursively with enabling sub-LCU-level delta QP |
---|
| 364 | *\param rpcBestCU |
---|
| 365 | *\param rpcTempCU |
---|
| 366 | *\param uiDepth |
---|
| 367 | *\returns Void |
---|
| 368 | * |
---|
| 369 | *- for loop of QP value to compress the current CU with all possible QP |
---|
| 370 | */ |
---|
| 371 | #if AMP_ENC_SPEEDUP |
---|
| 372 | Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize ) |
---|
| 373 | #else |
---|
| 374 | Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth ) |
---|
| 375 | #endif |
---|
| 376 | { |
---|
| 377 | TComPic* pcPic = rpcBestCU->getPic(); |
---|
| 378 | |
---|
| 379 | // get Original YUV data from picture |
---|
| 380 | m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() ); |
---|
| 381 | |
---|
| 382 | // variables for fast encoder decision |
---|
| 383 | Bool bEarlySkip = false; |
---|
| 384 | Bool bTrySplit = true; |
---|
| 385 | Double fRD_Skip = MAX_DOUBLE; |
---|
| 386 | |
---|
| 387 | // variable for Early CU determination |
---|
| 388 | Bool bSubBranch = true; |
---|
| 389 | |
---|
| 390 | // variable for Cbf fast mode PU decision |
---|
| 391 | Bool doNotBlockPu = true; |
---|
| 392 | Bool earlyDetectionSkipMode = false; |
---|
| 393 | |
---|
| 394 | Bool bTrySplitDQP = true; |
---|
| 395 | |
---|
| 396 | static Double afCost[ MAX_CU_DEPTH ]; |
---|
| 397 | static Int aiNum [ MAX_CU_DEPTH ]; |
---|
| 398 | |
---|
| 399 | if ( rpcBestCU->getAddr() == 0 ) |
---|
| 400 | { |
---|
| 401 | ::memset( afCost, 0, sizeof( afCost ) ); |
---|
| 402 | ::memset( aiNum, 0, sizeof( aiNum ) ); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | Bool bBoundary = false; |
---|
| 406 | UInt uiLPelX = rpcBestCU->getCUPelX(); |
---|
| 407 | UInt uiRPelX = uiLPelX + rpcBestCU->getWidth(0) - 1; |
---|
| 408 | UInt uiTPelY = rpcBestCU->getCUPelY(); |
---|
| 409 | UInt uiBPelY = uiTPelY + rpcBestCU->getHeight(0) - 1; |
---|
| 410 | |
---|
| 411 | Int iBaseQP = xComputeQP( rpcBestCU, uiDepth ); |
---|
| 412 | Int iMinQP; |
---|
| 413 | Int iMaxQP; |
---|
| 414 | Bool isAddLowestQP = false; |
---|
| 415 | Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(); |
---|
| 416 | |
---|
| 417 | if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 418 | { |
---|
| 419 | Int idQP = m_pcEncCfg->getMaxDeltaQP(); |
---|
| 420 | iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP ); |
---|
| 421 | iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP ); |
---|
| 422 | if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 423 | { |
---|
| 424 | isAddLowestQP = true; |
---|
| 425 | iMinQP = iMinQP - 1; |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | else |
---|
| 429 | { |
---|
| 430 | iMinQP = rpcTempCU->getQP(0); |
---|
| 431 | iMaxQP = rpcTempCU->getQP(0); |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 435 | if ( m_pcEncCfg->getUseRateCtrl() ) |
---|
| 436 | { |
---|
| 437 | iMinQP = m_pcRateCtrl->getRCQP(); |
---|
| 438 | iMaxQP = m_pcRateCtrl->getRCQP(); |
---|
| 439 | } |
---|
| 440 | #else |
---|
| 441 | if(m_pcEncCfg->getUseRateCtrl()) |
---|
| 442 | { |
---|
| 443 | Int qp = m_pcRateCtrl->getUnitQP(); |
---|
| 444 | iMinQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
| 445 | iMaxQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
| 446 | } |
---|
| 447 | #endif |
---|
| 448 | |
---|
| 449 | // If slice start or slice end is within this cu... |
---|
| 450 | TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx()); |
---|
| 451 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart(); |
---|
| 452 | Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart()); |
---|
| 453 | Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() ); |
---|
| 454 | // We need to split, so don't try these modes. |
---|
| 455 | if(!bSliceEnd && !bSliceStart && bInsidePicture ) |
---|
| 456 | { |
---|
[20] | 457 | #if (ENCODER_FAST_MODE) |
---|
[274] | 458 | Bool testInter = true; |
---|
[191] | 459 | if (rpcBestCU->getLayerId() > 0) |
---|
[274] | 460 | { |
---|
| 461 | TComList<TComPic*> *cListPic = m_ppcTEncTop[rpcBestCU->getLayerId()]->getRefLayerEnc(0)->getListPic(); |
---|
[259] | 462 | TComPic* picLowerLayer = pcSlice->getRefPic( *cListPic, pcSlice->getPOC() ); |
---|
| 463 | if(picLowerLayer->getSlice(0)->getSliceType() == I_SLICE) |
---|
[191] | 464 | { |
---|
| 465 | testInter = false; |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | #endif |
---|
| 469 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
| 470 | { |
---|
| 471 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
| 472 | { |
---|
| 473 | iQP = lowestQP; |
---|
| 474 | } |
---|
| 475 | // variables for fast encoder decision |
---|
| 476 | bEarlySkip = false; |
---|
| 477 | bTrySplit = true; |
---|
| 478 | fRD_Skip = MAX_DOUBLE; |
---|
| 479 | |
---|
| 480 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 481 | |
---|
| 482 | // do inter modes, SKIP and 2Nx2N |
---|
| 483 | #if (ENCODER_FAST_MODE == 1) |
---|
| 484 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter ) |
---|
| 485 | #else |
---|
| 486 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
| 487 | #endif |
---|
| 488 | { |
---|
| 489 | // 2Nx2N |
---|
| 490 | if(m_pcEncCfg->getUseEarlySkipDetection()) |
---|
| 491 | { |
---|
| 492 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N |
---|
| 493 | } |
---|
| 494 | // SKIP |
---|
| 495 | xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N |
---|
| 496 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 497 | |
---|
| 498 | // fast encoder decision for early skip |
---|
| 499 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
| 500 | { |
---|
| 501 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
| 502 | if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] ) |
---|
| 503 | { |
---|
| 504 | bEarlySkip = true; |
---|
| 505 | bTrySplit = false; |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | #if (ENCODER_FAST_MODE == 2) |
---|
| 509 | if (testInter) |
---|
| 510 | { |
---|
| 511 | #endif |
---|
| 512 | |
---|
| 513 | if(!m_pcEncCfg->getUseEarlySkipDetection()) |
---|
| 514 | { |
---|
| 515 | // 2Nx2N, NxN |
---|
| 516 | if ( !bEarlySkip ) |
---|
| 517 | { |
---|
| 518 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 519 | if(m_pcEncCfg->getUseCbfFastMode()) |
---|
| 520 | { |
---|
| 521 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 522 | } |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | #if (ENCODER_FAST_MODE == 2) |
---|
| 526 | } |
---|
| 527 | #endif |
---|
| 528 | |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 532 | { |
---|
| 533 | if(iQP == iBaseQP) |
---|
| 534 | { |
---|
| 535 | bTrySplitDQP = bTrySplit; |
---|
| 536 | } |
---|
| 537 | } |
---|
| 538 | else |
---|
| 539 | { |
---|
| 540 | bTrySplitDQP = bTrySplit; |
---|
| 541 | } |
---|
| 542 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
| 543 | { |
---|
| 544 | iQP = iMinQP; |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 549 | if ( uiDepth <= m_addSADDepth ) |
---|
| 550 | { |
---|
| 551 | m_LCUPredictionSAD += m_temporalSAD; |
---|
| 552 | m_addSADDepth = uiDepth; |
---|
| 553 | } |
---|
| 554 | #endif |
---|
| 555 | |
---|
| 556 | if(!earlyDetectionSkipMode) |
---|
| 557 | { |
---|
| 558 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
| 559 | { |
---|
| 560 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
| 561 | { |
---|
| 562 | iQP = lowestQP; |
---|
| 563 | } |
---|
| 564 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 565 | |
---|
| 566 | // do inter modes, NxN, 2NxN, and Nx2N |
---|
| 567 | #if (ENCODER_FAST_MODE) |
---|
| 568 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter ) |
---|
| 569 | #else |
---|
| 570 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
| 571 | #endif |
---|
| 572 | { |
---|
| 573 | // 2Nx2N, NxN |
---|
| 574 | if ( !bEarlySkip ) |
---|
| 575 | { |
---|
| 576 | if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) )) |
---|
| 577 | { |
---|
| 578 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu) |
---|
| 579 | { |
---|
| 580 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
| 581 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | // 2NxN, Nx2N |
---|
| 587 | if(doNotBlockPu) |
---|
| 588 | { |
---|
| 589 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N ); |
---|
| 590 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 591 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
| 592 | { |
---|
| 593 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 594 | } |
---|
| 595 | } |
---|
| 596 | if(doNotBlockPu) |
---|
| 597 | { |
---|
| 598 | xCheckRDCostInter ( rpcBestCU, rpcTempCU, SIZE_2NxN ); |
---|
| 599 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 600 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN) |
---|
| 601 | { |
---|
| 602 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | #if 1 |
---|
| 607 | //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N) |
---|
| 608 | if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) ) |
---|
| 609 | { |
---|
| 610 | #if AMP_ENC_SPEEDUP |
---|
| 611 | Bool bTestAMP_Hor = false, bTestAMP_Ver = false; |
---|
| 612 | |
---|
| 613 | #if AMP_MRG |
---|
| 614 | Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false; |
---|
| 615 | |
---|
| 616 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver); |
---|
| 617 | #else |
---|
| 618 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver); |
---|
| 619 | #endif |
---|
| 620 | |
---|
| 621 | //! Do horizontal AMP |
---|
| 622 | if ( bTestAMP_Hor ) |
---|
| 623 | { |
---|
| 624 | if(doNotBlockPu) |
---|
| 625 | { |
---|
| 626 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
| 627 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 628 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
| 629 | { |
---|
| 630 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | if(doNotBlockPu) |
---|
| 634 | { |
---|
| 635 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
| 636 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 637 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
| 638 | { |
---|
| 639 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 640 | } |
---|
| 641 | } |
---|
| 642 | } |
---|
| 643 | #if AMP_MRG |
---|
| 644 | else if ( bTestMergeAMP_Hor ) |
---|
| 645 | { |
---|
| 646 | if(doNotBlockPu) |
---|
| 647 | { |
---|
| 648 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true ); |
---|
| 649 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 650 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
| 651 | { |
---|
| 652 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | if(doNotBlockPu) |
---|
| 656 | { |
---|
| 657 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true ); |
---|
| 658 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 659 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
| 660 | { |
---|
| 661 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 662 | } |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | #endif |
---|
| 666 | |
---|
| 667 | //! Do horizontal AMP |
---|
| 668 | if ( bTestAMP_Ver ) |
---|
| 669 | { |
---|
| 670 | if(doNotBlockPu) |
---|
| 671 | { |
---|
| 672 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
| 673 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 674 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
| 675 | { |
---|
| 676 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 677 | } |
---|
| 678 | } |
---|
| 679 | if(doNotBlockPu) |
---|
| 680 | { |
---|
| 681 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
| 682 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 683 | } |
---|
| 684 | } |
---|
| 685 | #if AMP_MRG |
---|
| 686 | else if ( bTestMergeAMP_Ver ) |
---|
| 687 | { |
---|
| 688 | if(doNotBlockPu) |
---|
| 689 | { |
---|
| 690 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true ); |
---|
| 691 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 692 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
| 693 | { |
---|
| 694 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 695 | } |
---|
| 696 | } |
---|
| 697 | if(doNotBlockPu) |
---|
| 698 | { |
---|
| 699 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true ); |
---|
| 700 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | #endif |
---|
| 704 | |
---|
| 705 | #else |
---|
| 706 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
| 707 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 708 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
| 709 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 710 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
| 711 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 712 | |
---|
| 713 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
| 714 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 715 | |
---|
| 716 | #endif |
---|
| 717 | } |
---|
| 718 | #endif |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | // do normal intra modes |
---|
| 722 | if ( !bEarlySkip ) |
---|
| 723 | { |
---|
| 724 | // speedup for inter frames |
---|
| 725 | #if (ENCODER_FAST_MODE) |
---|
| 726 | if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || |
---|
| 727 | !testInter || |
---|
| 728 | rpcBestCU->getCbf( 0, TEXT_LUMA ) != 0 || |
---|
| 729 | rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0 || |
---|
| 730 | rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) // avoid very complex intra if it is unlikely |
---|
| 731 | #else |
---|
| 732 | if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || |
---|
| 733 | rpcBestCU->getCbf( 0, TEXT_LUMA ) != 0 || |
---|
| 734 | rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0 || |
---|
| 735 | rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) // avoid very complex intra if it is unlikely |
---|
| 736 | #endif |
---|
| 737 | { |
---|
| 738 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); |
---|
| 739 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 740 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 741 | { |
---|
| 742 | if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) ) |
---|
| 743 | { |
---|
| 744 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
| 745 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 746 | } |
---|
| 747 | } |
---|
| 748 | } |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | // test PCM |
---|
| 752 | if(pcPic->getSlice(0)->getSPS()->getUsePCM() |
---|
| 753 | && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize()) |
---|
| 754 | && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) ) |
---|
| 755 | { |
---|
| 756 | UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2; |
---|
| 757 | UInt uiBestBits = rpcBestCU->getTotalBits(); |
---|
| 758 | if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0))) |
---|
| 759 | { |
---|
| 760 | xCheckIntraPCM (rpcBestCU, rpcTempCU); |
---|
| 761 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 762 | } |
---|
| 763 | } |
---|
| 764 | #if INTRA_BL |
---|
| 765 | if(m_pcPicYuvRecBase) |
---|
| 766 | { |
---|
| 767 | xCheckRDCostIntraBL( rpcBestCU, rpcTempCU ); |
---|
| 768 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 769 | } |
---|
| 770 | #endif |
---|
| 771 | #if (ENCODER_FAST_MODE) |
---|
[274] | 772 | if(pcPic->getLayerId() > 0) |
---|
| 773 | { |
---|
| 774 | for(Int refLayer = 0; refLayer < pcSlice->getActiveNumILRRefIdx(); refLayer++) |
---|
| 775 | { |
---|
| 776 | xCheckRDCostILRUni( rpcBestCU, rpcTempCU, pcSlice->getInterLayerPredLayerIdc(refLayer)); |
---|
| 777 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 778 | } |
---|
| 779 | } |
---|
[191] | 780 | #endif |
---|
| 781 | |
---|
| 782 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
| 783 | { |
---|
| 784 | iQP = iMinQP; |
---|
| 785 | } |
---|
| 786 | } |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | m_pcEntropyCoder->resetBits(); |
---|
| 790 | m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true ); |
---|
| 791 | rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
| 792 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 793 | { |
---|
| 794 | rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 795 | } |
---|
| 796 | rpcBestCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() ); |
---|
| 797 | |
---|
| 798 | // accumulate statistics for early skip |
---|
| 799 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
| 800 | { |
---|
| 801 | if ( rpcBestCU->isSkipped(0) ) |
---|
| 802 | { |
---|
| 803 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
| 804 | afCost[ iIdx ] += rpcBestCU->getTotalCost(); |
---|
| 805 | aiNum [ iIdx ] ++; |
---|
| 806 | } |
---|
| 807 | } |
---|
| 808 | |
---|
| 809 | // Early CU determination |
---|
| 810 | if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) ) |
---|
| 811 | { |
---|
| 812 | bSubBranch = false; |
---|
| 813 | } |
---|
| 814 | else |
---|
| 815 | { |
---|
| 816 | bSubBranch = true; |
---|
| 817 | } |
---|
| 818 | } |
---|
| 819 | else if(!(bSliceEnd && bInsidePicture)) |
---|
| 820 | { |
---|
| 821 | bBoundary = true; |
---|
| 822 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 823 | m_addSADDepth++; |
---|
| 824 | #endif |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | // copy orginal YUV samples to PCM buffer |
---|
| 828 | if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false)) |
---|
| 829 | { |
---|
| 830 | xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]); |
---|
| 831 | } |
---|
| 832 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 833 | { |
---|
| 834 | Int idQP = m_pcEncCfg->getMaxDeltaQP(); |
---|
| 835 | iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP ); |
---|
| 836 | iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP ); |
---|
| 837 | if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 838 | { |
---|
| 839 | isAddLowestQP = true; |
---|
| 840 | iMinQP = iMinQP - 1; |
---|
| 841 | } |
---|
| 842 | } |
---|
| 843 | else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 844 | { |
---|
| 845 | iMinQP = iBaseQP; |
---|
| 846 | iMaxQP = iBaseQP; |
---|
| 847 | } |
---|
| 848 | else |
---|
| 849 | { |
---|
| 850 | Int iStartQP; |
---|
| 851 | if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
| 852 | { |
---|
| 853 | iStartQP = rpcTempCU->getQP(0); |
---|
| 854 | } |
---|
| 855 | else |
---|
| 856 | { |
---|
| 857 | UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
| 858 | iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx); |
---|
| 859 | } |
---|
| 860 | iMinQP = iStartQP; |
---|
| 861 | iMaxQP = iStartQP; |
---|
| 862 | } |
---|
| 863 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 864 | if ( m_pcEncCfg->getUseRateCtrl() ) |
---|
| 865 | { |
---|
| 866 | iMinQP = m_pcRateCtrl->getRCQP(); |
---|
| 867 | iMaxQP = m_pcRateCtrl->getRCQP(); |
---|
| 868 | } |
---|
| 869 | #else |
---|
| 870 | if(m_pcEncCfg->getUseRateCtrl()) |
---|
| 871 | { |
---|
| 872 | Int qp = m_pcRateCtrl->getUnitQP(); |
---|
| 873 | iMinQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
| 874 | iMaxQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
| 875 | } |
---|
| 876 | #endif |
---|
| 877 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
| 878 | { |
---|
| 879 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
| 880 | { |
---|
| 881 | iQP = lowestQP; |
---|
| 882 | } |
---|
| 883 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 884 | |
---|
| 885 | // further split |
---|
| 886 | if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 887 | { |
---|
| 888 | UChar uhNextDepth = uiDepth+1; |
---|
| 889 | TComDataCU* pcSubBestPartCU = m_ppcBestCU[uhNextDepth]; |
---|
| 890 | TComDataCU* pcSubTempPartCU = m_ppcTempCU[uhNextDepth]; |
---|
| 891 | |
---|
| 892 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ ) |
---|
| 893 | { |
---|
| 894 | pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
| 895 | pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
| 896 | |
---|
| 897 | Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
| 898 | if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 899 | { |
---|
| 900 | if( m_bUseSBACRD ) |
---|
| 901 | { |
---|
| 902 | if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer |
---|
| 903 | { |
---|
| 904 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
| 905 | } |
---|
| 906 | else |
---|
| 907 | { |
---|
| 908 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]); |
---|
| 909 | } |
---|
| 910 | } |
---|
| 911 | |
---|
| 912 | #if AMP_ENC_SPEEDUP |
---|
| 913 | if ( rpcBestCU->isIntra(0) ) |
---|
| 914 | { |
---|
| 915 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE ); |
---|
| 916 | } |
---|
| 917 | else |
---|
| 918 | { |
---|
| 919 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) ); |
---|
| 920 | } |
---|
| 921 | #else |
---|
| 922 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth ); |
---|
| 923 | #endif |
---|
| 924 | |
---|
| 925 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); // Keep best part data to current temporary data. |
---|
| 926 | xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth ); |
---|
| 927 | } |
---|
| 928 | else if (bInSlice) |
---|
| 929 | { |
---|
| 930 | pcSubBestPartCU->copyToPic( uhNextDepth ); |
---|
| 931 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); |
---|
| 932 | } |
---|
| 933 | } |
---|
| 934 | |
---|
| 935 | if( !bBoundary ) |
---|
| 936 | { |
---|
| 937 | m_pcEntropyCoder->resetBits(); |
---|
| 938 | m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true ); |
---|
| 939 | |
---|
| 940 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
| 941 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 942 | { |
---|
| 943 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 944 | } |
---|
| 945 | } |
---|
| 946 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 947 | |
---|
| 948 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP()) |
---|
| 949 | { |
---|
| 950 | Bool hasResidual = false; |
---|
| 951 | for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++) |
---|
| 952 | { |
---|
| 953 | if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && |
---|
| 954 | ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) ) |
---|
| 955 | { |
---|
| 956 | hasResidual = true; |
---|
| 957 | break; |
---|
| 958 | } |
---|
| 959 | } |
---|
| 960 | |
---|
| 961 | UInt uiTargetPartIdx; |
---|
| 962 | if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() ) |
---|
| 963 | { |
---|
| 964 | uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
| 965 | } |
---|
| 966 | else |
---|
| 967 | { |
---|
| 968 | uiTargetPartIdx = 0; |
---|
| 969 | } |
---|
| 970 | if ( hasResidual ) |
---|
| 971 | { |
---|
| 972 | #if !RDO_WITHOUT_DQP_BITS |
---|
| 973 | m_pcEntropyCoder->resetBits(); |
---|
| 974 | m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false ); |
---|
| 975 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
| 976 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 977 | { |
---|
| 978 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 979 | } |
---|
| 980 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 981 | #endif |
---|
| 982 | |
---|
| 983 | Bool foundNonZeroCbf = false; |
---|
| 984 | rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf ); |
---|
| 985 | assert( foundNonZeroCbf ); |
---|
| 986 | } |
---|
| 987 | else |
---|
| 988 | { |
---|
| 989 | rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP |
---|
| 990 | } |
---|
| 991 | } |
---|
| 992 | |
---|
| 993 | if( m_bUseSBACRD ) |
---|
| 994 | { |
---|
| 995 | m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 996 | } |
---|
| 997 | Bool isEndOfSlice = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES |
---|
| 998 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3); |
---|
| 999 | Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES |
---|
| 1000 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3); |
---|
| 1001 | if(isEndOfSlice||isEndOfSliceSegment) |
---|
| 1002 | { |
---|
| 1003 | rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1; |
---|
| 1004 | } |
---|
| 1005 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth); // RD compare current larger prediction |
---|
| 1006 | } // with sub partitioned prediction. |
---|
| 1007 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
| 1008 | { |
---|
| 1009 | iQP = iMinQP; |
---|
| 1010 | } |
---|
| 1011 | } |
---|
| 1012 | |
---|
| 1013 | rpcBestCU->copyToPic(uiDepth); // Copy Best data to Picture for next partition prediction. |
---|
| 1014 | |
---|
| 1015 | xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY ); // Copy Yuv data to picture Yuv |
---|
| 1016 | if( bBoundary ||(bSliceEnd && bInsidePicture)) |
---|
| 1017 | { |
---|
| 1018 | return; |
---|
| 1019 | } |
---|
| 1020 | |
---|
| 1021 | // Assert if Best prediction mode is NONE |
---|
| 1022 | // Selected mode's RD-cost must be not MAX_DOUBLE. |
---|
| 1023 | assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE ); |
---|
| 1024 | assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE ); |
---|
| 1025 | assert( rpcBestCU->getTotalCost ( ) != MAX_DOUBLE ); |
---|
| 1026 | } |
---|
| 1027 | |
---|
| 1028 | /** finish encoding a cu and handle end-of-slice conditions |
---|
| 1029 | * \param pcCU |
---|
| 1030 | * \param uiAbsPartIdx |
---|
| 1031 | * \param uiDepth |
---|
| 1032 | * \returns Void |
---|
| 1033 | */ |
---|
| 1034 | Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
| 1035 | { |
---|
| 1036 | TComPic* pcPic = pcCU->getPic(); |
---|
| 1037 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
| 1038 | |
---|
| 1039 | //Calculate end address |
---|
| 1040 | UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx; |
---|
| 1041 | |
---|
| 1042 | UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU(); |
---|
| 1043 | UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU(); |
---|
| 1044 | UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1045 | UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1046 | UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples(); |
---|
| 1047 | UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples(); |
---|
| 1048 | while(uiPosX>=uiWidth||uiPosY>=uiHeight) |
---|
| 1049 | { |
---|
| 1050 | uiInternalAddress--; |
---|
| 1051 | uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1052 | uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1053 | } |
---|
| 1054 | uiInternalAddress++; |
---|
| 1055 | if(uiInternalAddress==pcCU->getPic()->getNumPartInCU()) |
---|
| 1056 | { |
---|
| 1057 | uiInternalAddress = 0; |
---|
| 1058 | uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1); |
---|
| 1059 | } |
---|
| 1060 | UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress); |
---|
| 1061 | |
---|
| 1062 | // Encode slice finish |
---|
| 1063 | Bool bTerminateSlice = false; |
---|
| 1064 | if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress) |
---|
| 1065 | { |
---|
| 1066 | bTerminateSlice = true; |
---|
| 1067 | } |
---|
| 1068 | UInt uiGranularityWidth = g_uiMaxCUWidth; |
---|
| 1069 | uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1070 | uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1071 | Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth)) |
---|
| 1072 | &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight)); |
---|
| 1073 | |
---|
| 1074 | if(granularityBoundary) |
---|
| 1075 | { |
---|
| 1076 | // The 1-terminating bit is added to all streams, so don't add it here when it's 1. |
---|
| 1077 | if (!bTerminateSlice) |
---|
| 1078 | m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 ); |
---|
| 1079 | } |
---|
| 1080 | |
---|
| 1081 | Int numberOfWrittenBits = 0; |
---|
| 1082 | if (m_pcBitCounter) |
---|
| 1083 | { |
---|
| 1084 | numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | // Calculate slice end IF this CU puts us over slice bit size. |
---|
| 1088 | UInt iGranularitySize = pcCU->getPic()->getNumPartInCU(); |
---|
| 1089 | Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize; |
---|
| 1090 | if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
| 1091 | { |
---|
| 1092 | iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1))); |
---|
| 1093 | } |
---|
| 1094 | // Set slice end parameter |
---|
| 1095 | if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) |
---|
| 1096 | { |
---|
| 1097 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
| 1098 | pcSlice->setSliceCurEndCUAddr(iGranularityEnd); |
---|
| 1099 | return; |
---|
| 1100 | } |
---|
| 1101 | // Set dependent slice end parameter |
---|
| 1102 | if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) |
---|
| 1103 | { |
---|
| 1104 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
| 1105 | return; |
---|
| 1106 | } |
---|
| 1107 | if(granularityBoundary) |
---|
| 1108 | { |
---|
| 1109 | pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) ); |
---|
| 1110 | pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits); |
---|
| 1111 | if (m_pcBitCounter) |
---|
| 1112 | { |
---|
| 1113 | m_pcEntropyCoder->resetBits(); |
---|
| 1114 | } |
---|
| 1115 | } |
---|
| 1116 | } |
---|
| 1117 | |
---|
| 1118 | /** Compute QP for each CU |
---|
| 1119 | * \param pcCU Target CU |
---|
| 1120 | * \param uiDepth CU depth |
---|
| 1121 | * \returns quantization parameter |
---|
| 1122 | */ |
---|
| 1123 | Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth ) |
---|
| 1124 | { |
---|
| 1125 | Int iBaseQp = pcCU->getSlice()->getSliceQp(); |
---|
| 1126 | Int iQpOffset = 0; |
---|
| 1127 | if ( m_pcEncCfg->getUseAdaptiveQP() ) |
---|
| 1128 | { |
---|
| 1129 | TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() ); |
---|
| 1130 | UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 ); |
---|
| 1131 | TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth ); |
---|
| 1132 | UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth(); |
---|
| 1133 | UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight(); |
---|
| 1134 | UInt uiAQUStride = pcAQLayer->getAQPartStride(); |
---|
| 1135 | TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit(); |
---|
| 1136 | |
---|
| 1137 | Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0); |
---|
| 1138 | Double dAvgAct = pcAQLayer->getAvgActivity(); |
---|
| 1139 | Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity(); |
---|
| 1140 | Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct); |
---|
| 1141 | Double dQpOffset = log(dNormAct) / log(2.0) * 6.0; |
---|
| 1142 | iQpOffset = Int(floor( dQpOffset + 0.49999 )); |
---|
| 1143 | } |
---|
| 1144 | return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset ); |
---|
| 1145 | } |
---|
| 1146 | |
---|
| 1147 | /** encode a CU block recursively |
---|
| 1148 | * \param pcCU |
---|
| 1149 | * \param uiAbsPartIdx |
---|
| 1150 | * \param uiDepth |
---|
| 1151 | * \returns Void |
---|
| 1152 | */ |
---|
| 1153 | Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
| 1154 | { |
---|
| 1155 | TComPic* pcPic = pcCU->getPic(); |
---|
| 1156 | |
---|
| 1157 | Bool bBoundary = false; |
---|
| 1158 | UInt uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1159 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
| 1160 | UInt uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1161 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
| 1162 | |
---|
| 1163 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
| 1164 | // If slice start is within this cu... |
---|
| 1165 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
| 1166 | pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) ); |
---|
| 1167 | // We need to split, so don't try these modes. |
---|
| 1168 | if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1169 | { |
---|
| 1170 | m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 1171 | } |
---|
| 1172 | else |
---|
| 1173 | { |
---|
| 1174 | bBoundary = true; |
---|
| 1175 | } |
---|
| 1176 | |
---|
| 1177 | if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary ) |
---|
| 1178 | { |
---|
| 1179 | UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
| 1180 | if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
| 1181 | { |
---|
| 1182 | setdQPFlag(true); |
---|
| 1183 | } |
---|
| 1184 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
| 1185 | { |
---|
| 1186 | uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1187 | uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1188 | Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
| 1189 | if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1190 | { |
---|
| 1191 | xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 ); |
---|
| 1192 | } |
---|
| 1193 | } |
---|
| 1194 | return; |
---|
| 1195 | } |
---|
| 1196 | |
---|
| 1197 | if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
| 1198 | { |
---|
| 1199 | setdQPFlag(true); |
---|
| 1200 | } |
---|
| 1201 | if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1202 | { |
---|
| 1203 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx ); |
---|
| 1204 | } |
---|
| 1205 | if( !pcCU->getSlice()->isIntra() ) |
---|
| 1206 | { |
---|
| 1207 | m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx ); |
---|
| 1208 | } |
---|
| 1209 | |
---|
| 1210 | if( pcCU->isSkipped( uiAbsPartIdx ) ) |
---|
| 1211 | { |
---|
| 1212 | m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx ); |
---|
| 1213 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
| 1214 | return; |
---|
| 1215 | } |
---|
| 1216 | #if INTRA_BL |
---|
| 1217 | m_pcEntropyCoder->encodeIntraBLFlag( pcCU, uiAbsPartIdx ); |
---|
| 1218 | if ( !pcCU->isIntraBL( uiAbsPartIdx ) ) |
---|
| 1219 | { |
---|
| 1220 | #endif |
---|
| 1221 | m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx ); |
---|
| 1222 | |
---|
| 1223 | m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 1224 | |
---|
| 1225 | if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) |
---|
| 1226 | { |
---|
| 1227 | m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx ); |
---|
| 1228 | |
---|
| 1229 | if(pcCU->getIPCMFlag(uiAbsPartIdx)) |
---|
| 1230 | { |
---|
| 1231 | // Encode slice finish |
---|
| 1232 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
| 1233 | return; |
---|
| 1234 | } |
---|
| 1235 | } |
---|
| 1236 | |
---|
| 1237 | // prediction Info ( Intra : direction mode, Inter : Mv, reference idx ) |
---|
| 1238 | m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx ); |
---|
| 1239 | #if INTRA_BL |
---|
| 1240 | } |
---|
| 1241 | #endif |
---|
| 1242 | |
---|
| 1243 | // Encode Coefficients |
---|
| 1244 | Bool bCodeDQP = getdQPFlag(); |
---|
| 1245 | m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP ); |
---|
| 1246 | setdQPFlag( bCodeDQP ); |
---|
| 1247 | |
---|
| 1248 | // --- write terminating bit --- |
---|
| 1249 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
| 1250 | } |
---|
| 1251 | |
---|
| 1252 | /** check RD costs for a CU block encoded with merge |
---|
| 1253 | * \param rpcBestCU |
---|
| 1254 | * \param rpcTempCU |
---|
| 1255 | * \returns Void |
---|
| 1256 | */ |
---|
| 1257 | Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode ) |
---|
| 1258 | { |
---|
| 1259 | assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE ); |
---|
| 1260 | TComMvField cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists |
---|
| 1261 | UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS]; |
---|
| 1262 | Int numValidMergeCand = 0; |
---|
| 1263 | |
---|
| 1264 | for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui ) |
---|
| 1265 | { |
---|
| 1266 | uhInterDirNeighbours[ui] = 0; |
---|
| 1267 | } |
---|
| 1268 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1269 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1270 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
| 1271 | rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand ); |
---|
| 1272 | |
---|
| 1273 | Int mergeCandBuffer[MRG_MAX_NUM_CANDS]; |
---|
| 1274 | for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui ) |
---|
| 1275 | { |
---|
| 1276 | mergeCandBuffer[ui] = 0; |
---|
| 1277 | } |
---|
| 1278 | |
---|
| 1279 | Bool bestIsSkip = false; |
---|
| 1280 | |
---|
| 1281 | UInt iteration; |
---|
| 1282 | if ( rpcTempCU->isLosslessCoded(0)) |
---|
| 1283 | { |
---|
| 1284 | iteration = 1; |
---|
| 1285 | } |
---|
| 1286 | else |
---|
| 1287 | { |
---|
| 1288 | iteration = 2; |
---|
| 1289 | } |
---|
| 1290 | |
---|
| 1291 | for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual ) |
---|
| 1292 | { |
---|
| 1293 | for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand ) |
---|
| 1294 | { |
---|
| 1295 | #if REF_IDX_ME_ZEROMV |
---|
| 1296 | Bool bZeroMVILR = rpcTempCU->xCheckZeroMVILRMerge(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]); |
---|
| 1297 | if(bZeroMVILR) |
---|
| 1298 | { |
---|
| 1299 | #endif |
---|
| 1300 | if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1)) |
---|
| 1301 | { |
---|
| 1302 | |
---|
| 1303 | if( !(bestIsSkip && uiNoResidual == 0) ) |
---|
| 1304 | { |
---|
| 1305 | // set MC parameters |
---|
| 1306 | rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1307 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
| 1308 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1309 | rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1310 | rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1311 | rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1312 | rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
| 1313 | rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
| 1314 | |
---|
| 1315 | // do MC |
---|
| 1316 | m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] ); |
---|
| 1317 | // estimate residual and encode everything |
---|
| 1318 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, |
---|
| 1319 | m_ppcOrigYuv [uhDepth], |
---|
| 1320 | m_ppcPredYuvTemp[uhDepth], |
---|
| 1321 | m_ppcResiYuvTemp[uhDepth], |
---|
| 1322 | m_ppcResiYuvBest[uhDepth], |
---|
| 1323 | m_ppcRecoYuvTemp[uhDepth], |
---|
| 1324 | (uiNoResidual? true:false)); |
---|
| 1325 | |
---|
| 1326 | |
---|
| 1327 | if(uiNoResidual==0) |
---|
| 1328 | { |
---|
| 1329 | if(rpcTempCU->getQtRootCbf(0) == 0) |
---|
| 1330 | { |
---|
| 1331 | mergeCandBuffer[uiMergeCand] = 1; |
---|
| 1332 | } |
---|
| 1333 | } |
---|
| 1334 | |
---|
| 1335 | rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth ); |
---|
| 1336 | Int orgQP = rpcTempCU->getQP( 0 ); |
---|
| 1337 | xCheckDQP( rpcTempCU ); |
---|
| 1338 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
| 1339 | rpcTempCU->initEstData( uhDepth, orgQP ); |
---|
| 1340 | |
---|
| 1341 | |
---|
| 1342 | if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip ) |
---|
| 1343 | { |
---|
| 1344 | bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0; |
---|
| 1345 | } |
---|
| 1346 | |
---|
| 1347 | } |
---|
| 1348 | } |
---|
| 1349 | #if REF_IDX_ME_ZEROMV |
---|
| 1350 | } |
---|
| 1351 | #endif |
---|
| 1352 | } |
---|
| 1353 | |
---|
| 1354 | if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection()) |
---|
| 1355 | { |
---|
| 1356 | if(rpcBestCU->getQtRootCbf( 0 ) == 0) |
---|
| 1357 | { |
---|
| 1358 | if( rpcBestCU->getMergeFlag( 0 )) |
---|
| 1359 | { |
---|
| 1360 | *earlyDetectionSkipMode = true; |
---|
| 1361 | } |
---|
| 1362 | else |
---|
| 1363 | { |
---|
| 1364 | Int absoulte_MV=0; |
---|
| 1365 | for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ ) |
---|
| 1366 | { |
---|
| 1367 | if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 ) |
---|
| 1368 | { |
---|
| 1369 | TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx )); |
---|
| 1370 | Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor(); |
---|
| 1371 | Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer(); |
---|
| 1372 | absoulte_MV+=iHor+iVer; |
---|
| 1373 | } |
---|
| 1374 | } |
---|
| 1375 | |
---|
| 1376 | if(absoulte_MV == 0) |
---|
| 1377 | { |
---|
| 1378 | *earlyDetectionSkipMode = true; |
---|
| 1379 | } |
---|
| 1380 | } |
---|
| 1381 | } |
---|
| 1382 | } |
---|
| 1383 | } |
---|
| 1384 | } |
---|
| 1385 | |
---|
| 1386 | |
---|
| 1387 | #if AMP_MRG |
---|
| 1388 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG) |
---|
| 1389 | #else |
---|
| 1390 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize ) |
---|
| 1391 | #endif |
---|
| 1392 | { |
---|
| 1393 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1394 | |
---|
| 1395 | rpcTempCU->setDepthSubParts( uhDepth, 0 ); |
---|
| 1396 | |
---|
| 1397 | rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth ); |
---|
| 1398 | |
---|
| 1399 | rpcTempCU->setPartSizeSubParts ( ePartSize, 0, uhDepth ); |
---|
| 1400 | rpcTempCU->setPredModeSubParts ( MODE_INTER, 0, uhDepth ); |
---|
| 1401 | rpcTempCU->setCUTransquantBypassSubParts ( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
| 1402 | |
---|
| 1403 | #if AMP_MRG |
---|
| 1404 | rpcTempCU->setMergeAMP (true); |
---|
| 1405 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG ); |
---|
| 1406 | #else |
---|
| 1407 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] ); |
---|
| 1408 | #endif |
---|
| 1409 | |
---|
| 1410 | #if AMP_MRG |
---|
| 1411 | if ( !rpcTempCU->getMergeAMP() ) |
---|
| 1412 | { |
---|
| 1413 | return; |
---|
| 1414 | } |
---|
| 1415 | #endif |
---|
| 1416 | |
---|
| 1417 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 1418 | if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth ) |
---|
| 1419 | { |
---|
| 1420 | UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(), |
---|
| 1421 | m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(), |
---|
| 1422 | rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) ); |
---|
| 1423 | m_temporalSAD = (Int)SAD; |
---|
| 1424 | } |
---|
| 1425 | #endif |
---|
| 1426 | |
---|
| 1427 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false ); |
---|
| 1428 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1429 | |
---|
| 1430 | xCheckDQP( rpcTempCU ); |
---|
| 1431 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
| 1432 | } |
---|
| 1433 | |
---|
| 1434 | Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize ) |
---|
| 1435 | { |
---|
| 1436 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1437 | |
---|
| 1438 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
| 1439 | |
---|
| 1440 | rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth ); |
---|
| 1441 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
| 1442 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
| 1443 | |
---|
| 1444 | Bool bSeparateLumaChroma = true; // choose estimation mode |
---|
| 1445 | UInt uiPreCalcDistC = 0; |
---|
| 1446 | if( !bSeparateLumaChroma ) |
---|
| 1447 | { |
---|
| 1448 | m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] ); |
---|
| 1449 | } |
---|
| 1450 | m_pcPredSearch ->estIntraPredQT ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma ); |
---|
| 1451 | |
---|
| 1452 | m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() ); |
---|
| 1453 | |
---|
| 1454 | m_pcPredSearch ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC ); |
---|
| 1455 | |
---|
| 1456 | m_pcEntropyCoder->resetBits(); |
---|
| 1457 | #if INTRA_BL |
---|
| 1458 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
| 1459 | #endif |
---|
| 1460 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1461 | { |
---|
| 1462 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
| 1463 | } |
---|
| 1464 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
| 1465 | m_pcEntropyCoder->encodePredMode( rpcTempCU, 0, true ); |
---|
| 1466 | m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true ); |
---|
| 1467 | m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0, true ); |
---|
| 1468 | m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true ); |
---|
| 1469 | |
---|
| 1470 | // Encode Coefficients |
---|
| 1471 | Bool bCodeDQP = getdQPFlag(); |
---|
| 1472 | m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP ); |
---|
| 1473 | setdQPFlag( bCodeDQP ); |
---|
| 1474 | |
---|
| 1475 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 1476 | |
---|
| 1477 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1478 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1479 | { |
---|
| 1480 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1481 | } |
---|
| 1482 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1483 | |
---|
| 1484 | xCheckDQP( rpcTempCU ); |
---|
| 1485 | xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth); |
---|
| 1486 | } |
---|
| 1487 | |
---|
| 1488 | /** Check R-D costs for a CU with PCM mode. |
---|
| 1489 | * \param rpcBestCU pointer to best mode CU data structure |
---|
| 1490 | * \param rpcTempCU pointer to testing mode CU data structure |
---|
| 1491 | * \returns Void |
---|
| 1492 | * |
---|
| 1493 | * \note Current PCM implementation encodes sample values in a lossless way. The distortion of PCM mode CUs are zero. PCM mode is selected if the best mode yields bits greater than that of PCM mode. |
---|
| 1494 | */ |
---|
| 1495 | Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU ) |
---|
| 1496 | { |
---|
| 1497 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1498 | |
---|
| 1499 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
| 1500 | |
---|
| 1501 | rpcTempCU->setIPCMFlag(0, true); |
---|
| 1502 | rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0)); |
---|
| 1503 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); |
---|
| 1504 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
| 1505 | rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth ); |
---|
| 1506 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
| 1507 | |
---|
| 1508 | m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]); |
---|
| 1509 | |
---|
| 1510 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
| 1511 | |
---|
| 1512 | m_pcEntropyCoder->resetBits(); |
---|
| 1513 | #if INTRA_BL |
---|
| 1514 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
| 1515 | #endif |
---|
| 1516 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1517 | { |
---|
| 1518 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
| 1519 | } |
---|
| 1520 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
| 1521 | m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0, true ); |
---|
| 1522 | m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true ); |
---|
| 1523 | m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true ); |
---|
| 1524 | |
---|
| 1525 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 1526 | |
---|
| 1527 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1528 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1529 | { |
---|
| 1530 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1531 | } |
---|
| 1532 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1533 | |
---|
| 1534 | xCheckDQP( rpcTempCU ); |
---|
| 1535 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth ); |
---|
| 1536 | } |
---|
| 1537 | |
---|
| 1538 | /** check whether current try is the best with identifying the depth of current try |
---|
| 1539 | * \param rpcBestCU |
---|
| 1540 | * \param rpcTempCU |
---|
| 1541 | * \returns Void |
---|
| 1542 | */ |
---|
| 1543 | Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth ) |
---|
| 1544 | { |
---|
| 1545 | if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() ) |
---|
| 1546 | { |
---|
| 1547 | TComYuv* pcYuv; |
---|
| 1548 | // Change Information data |
---|
| 1549 | TComDataCU* pcCU = rpcBestCU; |
---|
| 1550 | rpcBestCU = rpcTempCU; |
---|
| 1551 | rpcTempCU = pcCU; |
---|
| 1552 | |
---|
| 1553 | // Change Prediction data |
---|
| 1554 | pcYuv = m_ppcPredYuvBest[uiDepth]; |
---|
| 1555 | m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth]; |
---|
| 1556 | m_ppcPredYuvTemp[uiDepth] = pcYuv; |
---|
| 1557 | |
---|
| 1558 | // Change Reconstruction data |
---|
| 1559 | pcYuv = m_ppcRecoYuvBest[uiDepth]; |
---|
| 1560 | m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth]; |
---|
| 1561 | m_ppcRecoYuvTemp[uiDepth] = pcYuv; |
---|
| 1562 | |
---|
| 1563 | pcYuv = NULL; |
---|
| 1564 | pcCU = NULL; |
---|
| 1565 | |
---|
| 1566 | if( m_bUseSBACRD ) // store temp best CI for next CU coding |
---|
| 1567 | m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]); |
---|
| 1568 | } |
---|
| 1569 | } |
---|
| 1570 | |
---|
| 1571 | Void TEncCu::xCheckDQP( TComDataCU* pcCU ) |
---|
| 1572 | { |
---|
| 1573 | UInt uiDepth = pcCU->getDepth( 0 ); |
---|
| 1574 | |
---|
| 1575 | if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 1576 | { |
---|
| 1577 | if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) ) |
---|
| 1578 | { |
---|
| 1579 | #if !RDO_WITHOUT_DQP_BITS |
---|
| 1580 | m_pcEntropyCoder->resetBits(); |
---|
| 1581 | m_pcEntropyCoder->encodeQP( pcCU, 0, false ); |
---|
| 1582 | pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
| 1583 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1584 | { |
---|
| 1585 | pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1586 | } |
---|
| 1587 | pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() ); |
---|
| 1588 | #endif |
---|
| 1589 | } |
---|
| 1590 | else |
---|
| 1591 | { |
---|
| 1592 | pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP |
---|
| 1593 | } |
---|
| 1594 | } |
---|
| 1595 | } |
---|
| 1596 | |
---|
| 1597 | Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst) |
---|
| 1598 | { |
---|
| 1599 | pDst->iN = pSrc->iN; |
---|
| 1600 | for (Int i = 0; i < pSrc->iN; i++) |
---|
| 1601 | { |
---|
| 1602 | pDst->m_acMvCand[i] = pSrc->m_acMvCand[i]; |
---|
| 1603 | } |
---|
| 1604 | } |
---|
| 1605 | Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY ) |
---|
| 1606 | { |
---|
| 1607 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
| 1608 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
| 1609 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
| 1610 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
| 1611 | pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
| 1612 | Bool bSliceEnd = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
| 1613 | pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
| 1614 | if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1615 | { |
---|
| 1616 | UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx]; |
---|
| 1617 | UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth); |
---|
| 1618 | UInt uiBlkWidth = rpcPic->getNumPartInWidth() >> (uiDepth); |
---|
| 1619 | UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
| 1620 | UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
| 1621 | UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX; |
---|
| 1622 | m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx); |
---|
| 1623 | } |
---|
| 1624 | else |
---|
| 1625 | { |
---|
| 1626 | UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
| 1627 | |
---|
| 1628 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
| 1629 | { |
---|
| 1630 | UInt uiSubCULPelX = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx & 1 ); |
---|
| 1631 | UInt uiSubCUTPelY = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 ); |
---|
| 1632 | |
---|
| 1633 | Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && |
---|
| 1634 | rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
| 1635 | if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1636 | { |
---|
| 1637 | xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY ); // Copy Yuv data to picture Yuv |
---|
| 1638 | } |
---|
| 1639 | } |
---|
| 1640 | } |
---|
| 1641 | } |
---|
| 1642 | |
---|
| 1643 | Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth ) |
---|
| 1644 | { |
---|
| 1645 | UInt uiCurrDepth = uiNextDepth - 1; |
---|
| 1646 | m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx ); |
---|
| 1647 | } |
---|
| 1648 | |
---|
| 1649 | /** Function for filling the PCM buffer of a CU using its original sample array |
---|
| 1650 | * \param pcCU pointer to current CU |
---|
| 1651 | * \param pcOrgYuv pointer to original sample array |
---|
| 1652 | * \returns Void |
---|
| 1653 | */ |
---|
| 1654 | Void TEncCu::xFillPCMBuffer ( TComDataCU*& pCU, TComYuv* pOrgYuv ) |
---|
| 1655 | { |
---|
| 1656 | |
---|
| 1657 | UInt width = pCU->getWidth(0); |
---|
| 1658 | UInt height = pCU->getHeight(0); |
---|
| 1659 | |
---|
| 1660 | Pel* pSrcY = pOrgYuv->getLumaAddr(0, width); |
---|
| 1661 | Pel* pDstY = pCU->getPCMSampleY(); |
---|
| 1662 | UInt srcStride = pOrgYuv->getStride(); |
---|
| 1663 | |
---|
| 1664 | for(Int y = 0; y < height; y++ ) |
---|
| 1665 | { |
---|
| 1666 | for(Int x = 0; x < width; x++ ) |
---|
| 1667 | { |
---|
| 1668 | pDstY[x] = pSrcY[x]; |
---|
| 1669 | } |
---|
| 1670 | pDstY += width; |
---|
| 1671 | pSrcY += srcStride; |
---|
| 1672 | } |
---|
| 1673 | |
---|
| 1674 | Pel* pSrcCb = pOrgYuv->getCbAddr(); |
---|
| 1675 | Pel* pSrcCr = pOrgYuv->getCrAddr();; |
---|
| 1676 | |
---|
| 1677 | Pel* pDstCb = pCU->getPCMSampleCb(); |
---|
| 1678 | Pel* pDstCr = pCU->getPCMSampleCr();; |
---|
| 1679 | |
---|
| 1680 | UInt srcStrideC = pOrgYuv->getCStride(); |
---|
| 1681 | UInt heightC = height >> 1; |
---|
| 1682 | UInt widthC = width >> 1; |
---|
| 1683 | |
---|
| 1684 | for(Int y = 0; y < heightC; y++ ) |
---|
| 1685 | { |
---|
| 1686 | for(Int x = 0; x < widthC; x++ ) |
---|
| 1687 | { |
---|
| 1688 | pDstCb[x] = pSrcCb[x]; |
---|
| 1689 | pDstCr[x] = pSrcCr[x]; |
---|
| 1690 | } |
---|
| 1691 | pDstCb += widthC; |
---|
| 1692 | pDstCr += widthC; |
---|
| 1693 | pSrcCb += srcStrideC; |
---|
| 1694 | pSrcCr += srcStrideC; |
---|
| 1695 | } |
---|
| 1696 | } |
---|
| 1697 | |
---|
| 1698 | #if ADAPTIVE_QP_SELECTION |
---|
| 1699 | /** Collect ARL statistics from one block |
---|
| 1700 | */ |
---|
| 1701 | Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples ) |
---|
| 1702 | { |
---|
| 1703 | for( Int n = 0; n < NumCoeffInCU; n++ ) |
---|
| 1704 | { |
---|
| 1705 | Int u = abs( rpcCoeff[ n ] ); |
---|
| 1706 | Int absc = rpcArlCoeff[ n ]; |
---|
| 1707 | |
---|
| 1708 | if( u != 0 ) |
---|
| 1709 | { |
---|
| 1710 | if( u < LEVEL_RANGE ) |
---|
| 1711 | { |
---|
| 1712 | cSum[ u ] += ( Double )absc; |
---|
| 1713 | numSamples[ u ]++; |
---|
| 1714 | } |
---|
| 1715 | else |
---|
| 1716 | { |
---|
| 1717 | cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION ); |
---|
| 1718 | numSamples[ LEVEL_RANGE ]++; |
---|
| 1719 | } |
---|
| 1720 | } |
---|
| 1721 | } |
---|
| 1722 | |
---|
| 1723 | return 0; |
---|
| 1724 | } |
---|
| 1725 | |
---|
| 1726 | /** Collect ARL statistics from one LCU |
---|
| 1727 | * \param pcCU |
---|
| 1728 | */ |
---|
| 1729 | Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU ) |
---|
| 1730 | { |
---|
| 1731 | Double cSum[ LEVEL_RANGE + 1 ]; //: the sum of DCT coefficients corresponding to datatype and quantization output |
---|
| 1732 | UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output |
---|
| 1733 | |
---|
| 1734 | TCoeff* pCoeffY = rpcCU->getCoeffY(); |
---|
| 1735 | Int* pArlCoeffY = rpcCU->getArlCoeffY(); |
---|
| 1736 | |
---|
| 1737 | UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth; |
---|
| 1738 | UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth; |
---|
| 1739 | |
---|
| 1740 | memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) ); |
---|
| 1741 | memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) ); |
---|
| 1742 | |
---|
| 1743 | // Collect stats to cSum[][] and numSamples[][] |
---|
| 1744 | for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ ) |
---|
| 1745 | { |
---|
| 1746 | UInt uiTrIdx = rpcCU->getTransformIdx(i); |
---|
| 1747 | |
---|
| 1748 | if(rpcCU->getPredictionMode(i) == MODE_INTER) |
---|
| 1749 | if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) ) |
---|
| 1750 | { |
---|
| 1751 | xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples); |
---|
| 1752 | }//Note that only InterY is processed. QP rounding is based on InterY data only. |
---|
| 1753 | |
---|
| 1754 | pCoeffY += uiMinNumCoeffInCU; |
---|
| 1755 | pArlCoeffY += uiMinNumCoeffInCU; |
---|
| 1756 | } |
---|
| 1757 | |
---|
| 1758 | for(Int u=1; u<LEVEL_RANGE;u++) |
---|
| 1759 | { |
---|
| 1760 | m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ; |
---|
| 1761 | m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ; |
---|
| 1762 | } |
---|
| 1763 | m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ; |
---|
| 1764 | m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ; |
---|
| 1765 | } |
---|
| 1766 | #endif |
---|
| 1767 | |
---|
| 1768 | #if INTRA_BL |
---|
| 1769 | Void TEncCu::xCheckRDCostIntraBL( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU ) |
---|
| 1770 | { |
---|
| 1771 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1772 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
| 1773 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); |
---|
| 1774 | rpcTempCU->setPredModeSubParts( MODE_INTRA_BL, 0, uiDepth ); |
---|
| 1775 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
| 1776 | |
---|
| 1777 | m_pcPredSearch->setBaseRecPic( m_pcPicYuvRecBase ); |
---|
| 1778 | #if NO_RESIDUAL_FLAG_FOR_BLPRED |
---|
| 1779 | rpcTempCU->setDepthSubParts( uiDepth, 0 ); |
---|
| 1780 | // rpcTempCU->setLumaIntraDirSubParts( DC_IDX, 0, uiDepth ); |
---|
| 1781 | // rpcTempCU->setChromIntraDirSubParts( DC_IDX, 0, uiDepth ); |
---|
| 1782 | m_ppcPredYuvTemp[uiDepth]->copyFromPicLuma ( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, rpcTempCU->getWidth(0), rpcTempCU->getHeight(0)); |
---|
| 1783 | m_ppcPredYuvTemp[uiDepth]->copyFromPicChroma( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, (rpcTempCU->getWidth(0)>>1), (rpcTempCU->getHeight(0)>>1), 0); |
---|
| 1784 | m_ppcPredYuvTemp[uiDepth]->copyFromPicChroma( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, (rpcTempCU->getWidth(0)>>1), (rpcTempCU->getHeight(0)>>1), 1); |
---|
| 1785 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcResiYuvBest[uiDepth], m_ppcRecoYuvTemp[uiDepth], false ); |
---|
| 1786 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1787 | #else |
---|
| 1788 | |
---|
| 1789 | m_pcPredSearch->estIntraBLPredQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth] ); |
---|
| 1790 | |
---|
| 1791 | m_pcEntropyCoder->resetBits(); |
---|
| 1792 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
| 1793 | m_pcEntropyCoder->encodeSkipFlag( rpcTempCU, 0, true ); |
---|
| 1794 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1795 | { |
---|
| 1796 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
| 1797 | } |
---|
| 1798 | |
---|
| 1799 | // Encode Coefficients |
---|
| 1800 | Bool bCodeDQP = getdQPFlag(); |
---|
| 1801 | m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP ); |
---|
| 1802 | setdQPFlag( bCodeDQP ); |
---|
| 1803 | |
---|
| 1804 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 1805 | |
---|
| 1806 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1807 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1808 | { |
---|
| 1809 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1810 | } |
---|
| 1811 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1812 | #endif |
---|
| 1813 | |
---|
| 1814 | xCheckDQP( rpcTempCU ); |
---|
| 1815 | xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth); |
---|
| 1816 | } |
---|
| 1817 | #endif |
---|
| 1818 | #if (ENCODER_FAST_MODE) |
---|
[274] | 1819 | Void TEncCu::xCheckRDCostILRUni(TComDataCU *&rpcBestCU, TComDataCU *&rpcTempCU, UInt refLayerId) |
---|
[20] | 1820 | { |
---|
| 1821 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1822 | rpcTempCU->setDepthSubParts( uhDepth, 0 ); |
---|
| 1823 | #if SKIP_FLAG |
---|
| 1824 | rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth ); |
---|
| 1825 | #endif |
---|
| 1826 | rpcTempCU->setPartSizeSubParts ( SIZE_2Nx2N, 0, uhDepth ); //2Nx2N |
---|
| 1827 | rpcTempCU->setPredModeSubParts ( MODE_INTER, 0, uhDepth ); |
---|
| 1828 | rpcTempCU->setCUTransquantBypassSubParts ( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
[274] | 1829 | Bool exitILR = m_pcPredSearch->predInterSearchILRUni( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], refLayerId ); |
---|
[20] | 1830 | if(!exitILR) |
---|
[125] | 1831 | { |
---|
[274] | 1832 | return; |
---|
[125] | 1833 | } |
---|
[20] | 1834 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false ); |
---|
| 1835 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1836 | xCheckDQP( rpcTempCU ); |
---|
| 1837 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
| 1838 | return; |
---|
| 1839 | } |
---|
[191] | 1840 | #endif |
---|
| 1841 | //! \} |
---|