[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) |
---|
[191] | 458 | bool testInter = true; |
---|
| 459 | if (rpcBestCU->getLayerId() > 0) |
---|
| 460 | { |
---|
[270] | 461 | TComList<TComPic*> *cListPic = m_ppcTEncTop[rpcBestCU->getLayerId()]->getRefLayerEnc(rpcBestCU->getLayerId()-1)->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 | } |
---|
| 469 | #endif |
---|
| 470 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
| 471 | { |
---|
| 472 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
| 473 | { |
---|
| 474 | iQP = lowestQP; |
---|
| 475 | } |
---|
| 476 | // variables for fast encoder decision |
---|
| 477 | bEarlySkip = false; |
---|
| 478 | bTrySplit = true; |
---|
| 479 | fRD_Skip = MAX_DOUBLE; |
---|
| 480 | |
---|
| 481 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 482 | |
---|
| 483 | // do inter modes, SKIP and 2Nx2N |
---|
| 484 | #if (ENCODER_FAST_MODE == 1) |
---|
| 485 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter ) |
---|
| 486 | #else |
---|
| 487 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
| 488 | #endif |
---|
| 489 | { |
---|
| 490 | // 2Nx2N |
---|
| 491 | if(m_pcEncCfg->getUseEarlySkipDetection()) |
---|
| 492 | { |
---|
| 493 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N |
---|
| 494 | } |
---|
| 495 | // SKIP |
---|
| 496 | xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N |
---|
| 497 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 498 | |
---|
| 499 | // fast encoder decision for early skip |
---|
| 500 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
| 501 | { |
---|
| 502 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
| 503 | if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] ) |
---|
| 504 | { |
---|
| 505 | bEarlySkip = true; |
---|
| 506 | bTrySplit = false; |
---|
| 507 | } |
---|
| 508 | } |
---|
| 509 | #if (ENCODER_FAST_MODE == 2) |
---|
| 510 | if (testInter) |
---|
| 511 | { |
---|
| 512 | #endif |
---|
| 513 | |
---|
| 514 | if(!m_pcEncCfg->getUseEarlySkipDetection()) |
---|
| 515 | { |
---|
| 516 | // 2Nx2N, NxN |
---|
| 517 | if ( !bEarlySkip ) |
---|
| 518 | { |
---|
| 519 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 520 | if(m_pcEncCfg->getUseCbfFastMode()) |
---|
| 521 | { |
---|
| 522 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | } |
---|
| 526 | #if (ENCODER_FAST_MODE == 2) |
---|
| 527 | } |
---|
| 528 | #endif |
---|
| 529 | |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 533 | { |
---|
| 534 | if(iQP == iBaseQP) |
---|
| 535 | { |
---|
| 536 | bTrySplitDQP = bTrySplit; |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | else |
---|
| 540 | { |
---|
| 541 | bTrySplitDQP = bTrySplit; |
---|
| 542 | } |
---|
| 543 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
| 544 | { |
---|
| 545 | iQP = iMinQP; |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 550 | if ( uiDepth <= m_addSADDepth ) |
---|
| 551 | { |
---|
| 552 | m_LCUPredictionSAD += m_temporalSAD; |
---|
| 553 | m_addSADDepth = uiDepth; |
---|
| 554 | } |
---|
| 555 | #endif |
---|
| 556 | |
---|
| 557 | if(!earlyDetectionSkipMode) |
---|
| 558 | { |
---|
| 559 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
| 560 | { |
---|
| 561 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
| 562 | { |
---|
| 563 | iQP = lowestQP; |
---|
| 564 | } |
---|
| 565 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 566 | |
---|
| 567 | // do inter modes, NxN, 2NxN, and Nx2N |
---|
| 568 | #if (ENCODER_FAST_MODE) |
---|
| 569 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter ) |
---|
| 570 | #else |
---|
| 571 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
| 572 | #endif |
---|
| 573 | { |
---|
| 574 | // 2Nx2N, NxN |
---|
| 575 | if ( !bEarlySkip ) |
---|
| 576 | { |
---|
| 577 | if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) )) |
---|
| 578 | { |
---|
| 579 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu) |
---|
| 580 | { |
---|
| 581 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
| 582 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 583 | } |
---|
| 584 | } |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | // 2NxN, Nx2N |
---|
| 588 | if(doNotBlockPu) |
---|
| 589 | { |
---|
| 590 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N ); |
---|
| 591 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 592 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
| 593 | { |
---|
| 594 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 595 | } |
---|
| 596 | } |
---|
| 597 | if(doNotBlockPu) |
---|
| 598 | { |
---|
| 599 | xCheckRDCostInter ( rpcBestCU, rpcTempCU, SIZE_2NxN ); |
---|
| 600 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 601 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN) |
---|
| 602 | { |
---|
| 603 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 604 | } |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | #if 1 |
---|
| 608 | //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N) |
---|
| 609 | if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) ) |
---|
| 610 | { |
---|
| 611 | #if AMP_ENC_SPEEDUP |
---|
| 612 | Bool bTestAMP_Hor = false, bTestAMP_Ver = false; |
---|
| 613 | |
---|
| 614 | #if AMP_MRG |
---|
| 615 | Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false; |
---|
| 616 | |
---|
| 617 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver); |
---|
| 618 | #else |
---|
| 619 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver); |
---|
| 620 | #endif |
---|
| 621 | |
---|
| 622 | //! Do horizontal AMP |
---|
| 623 | if ( bTestAMP_Hor ) |
---|
| 624 | { |
---|
| 625 | if(doNotBlockPu) |
---|
| 626 | { |
---|
| 627 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
| 628 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 629 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
| 630 | { |
---|
| 631 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | if(doNotBlockPu) |
---|
| 635 | { |
---|
| 636 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
| 637 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 638 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
| 639 | { |
---|
| 640 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 641 | } |
---|
| 642 | } |
---|
| 643 | } |
---|
| 644 | #if AMP_MRG |
---|
| 645 | else if ( bTestMergeAMP_Hor ) |
---|
| 646 | { |
---|
| 647 | if(doNotBlockPu) |
---|
| 648 | { |
---|
| 649 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true ); |
---|
| 650 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 651 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
| 652 | { |
---|
| 653 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 654 | } |
---|
| 655 | } |
---|
| 656 | if(doNotBlockPu) |
---|
| 657 | { |
---|
| 658 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true ); |
---|
| 659 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 660 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
| 661 | { |
---|
| 662 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | #endif |
---|
| 667 | |
---|
| 668 | //! Do horizontal AMP |
---|
| 669 | if ( bTestAMP_Ver ) |
---|
| 670 | { |
---|
| 671 | if(doNotBlockPu) |
---|
| 672 | { |
---|
| 673 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
| 674 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 675 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
| 676 | { |
---|
| 677 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 678 | } |
---|
| 679 | } |
---|
| 680 | if(doNotBlockPu) |
---|
| 681 | { |
---|
| 682 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
| 683 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 684 | } |
---|
| 685 | } |
---|
| 686 | #if AMP_MRG |
---|
| 687 | else if ( bTestMergeAMP_Ver ) |
---|
| 688 | { |
---|
| 689 | if(doNotBlockPu) |
---|
| 690 | { |
---|
| 691 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true ); |
---|
| 692 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 693 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
| 694 | { |
---|
| 695 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
| 696 | } |
---|
| 697 | } |
---|
| 698 | if(doNotBlockPu) |
---|
| 699 | { |
---|
| 700 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true ); |
---|
| 701 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | #endif |
---|
| 705 | |
---|
| 706 | #else |
---|
| 707 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
| 708 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 709 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
| 710 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 711 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
| 712 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 713 | |
---|
| 714 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
| 715 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 716 | |
---|
| 717 | #endif |
---|
| 718 | } |
---|
| 719 | #endif |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | // do normal intra modes |
---|
| 723 | if ( !bEarlySkip ) |
---|
| 724 | { |
---|
| 725 | // speedup for inter frames |
---|
| 726 | #if (ENCODER_FAST_MODE) |
---|
| 727 | if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || |
---|
| 728 | !testInter || |
---|
| 729 | rpcBestCU->getCbf( 0, TEXT_LUMA ) != 0 || |
---|
| 730 | rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0 || |
---|
| 731 | rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) // avoid very complex intra if it is unlikely |
---|
| 732 | #else |
---|
| 733 | if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || |
---|
| 734 | rpcBestCU->getCbf( 0, TEXT_LUMA ) != 0 || |
---|
| 735 | rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0 || |
---|
| 736 | rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) // avoid very complex intra if it is unlikely |
---|
| 737 | #endif |
---|
| 738 | { |
---|
| 739 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); |
---|
| 740 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 741 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 742 | { |
---|
| 743 | if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) ) |
---|
| 744 | { |
---|
| 745 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
| 746 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 747 | } |
---|
| 748 | } |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | // test PCM |
---|
| 753 | if(pcPic->getSlice(0)->getSPS()->getUsePCM() |
---|
| 754 | && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize()) |
---|
| 755 | && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) ) |
---|
| 756 | { |
---|
| 757 | UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2; |
---|
| 758 | UInt uiBestBits = rpcBestCU->getTotalBits(); |
---|
| 759 | if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0))) |
---|
| 760 | { |
---|
| 761 | xCheckIntraPCM (rpcBestCU, rpcTempCU); |
---|
| 762 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 763 | } |
---|
| 764 | } |
---|
| 765 | #if INTRA_BL |
---|
| 766 | if(m_pcPicYuvRecBase) |
---|
| 767 | { |
---|
| 768 | xCheckRDCostIntraBL( rpcBestCU, rpcTempCU ); |
---|
| 769 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 770 | } |
---|
| 771 | #endif |
---|
| 772 | |
---|
| 773 | #if (ENCODER_FAST_MODE) |
---|
| 774 | if(pcPic->getLayerId() > 0) |
---|
| 775 | { |
---|
| 776 | xCheckRDCostILRUni( rpcBestCU, rpcTempCU); |
---|
| 777 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 778 | } |
---|
| 779 | #endif |
---|
| 780 | |
---|
| 781 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
| 782 | { |
---|
| 783 | iQP = iMinQP; |
---|
| 784 | } |
---|
| 785 | } |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | m_pcEntropyCoder->resetBits(); |
---|
| 789 | m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true ); |
---|
| 790 | rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
| 791 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 792 | { |
---|
| 793 | rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 794 | } |
---|
| 795 | rpcBestCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() ); |
---|
| 796 | |
---|
| 797 | // accumulate statistics for early skip |
---|
| 798 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
| 799 | { |
---|
| 800 | if ( rpcBestCU->isSkipped(0) ) |
---|
| 801 | { |
---|
| 802 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
| 803 | afCost[ iIdx ] += rpcBestCU->getTotalCost(); |
---|
| 804 | aiNum [ iIdx ] ++; |
---|
| 805 | } |
---|
| 806 | } |
---|
| 807 | |
---|
| 808 | // Early CU determination |
---|
| 809 | if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) ) |
---|
| 810 | { |
---|
| 811 | bSubBranch = false; |
---|
| 812 | } |
---|
| 813 | else |
---|
| 814 | { |
---|
| 815 | bSubBranch = true; |
---|
| 816 | } |
---|
| 817 | } |
---|
| 818 | else if(!(bSliceEnd && bInsidePicture)) |
---|
| 819 | { |
---|
| 820 | bBoundary = true; |
---|
| 821 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 822 | m_addSADDepth++; |
---|
| 823 | #endif |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | // copy orginal YUV samples to PCM buffer |
---|
| 827 | if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false)) |
---|
| 828 | { |
---|
| 829 | xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]); |
---|
| 830 | } |
---|
| 831 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 832 | { |
---|
| 833 | Int idQP = m_pcEncCfg->getMaxDeltaQP(); |
---|
| 834 | iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP ); |
---|
| 835 | iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP ); |
---|
| 836 | if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 837 | { |
---|
| 838 | isAddLowestQP = true; |
---|
| 839 | iMinQP = iMinQP - 1; |
---|
| 840 | } |
---|
| 841 | } |
---|
| 842 | else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 843 | { |
---|
| 844 | iMinQP = iBaseQP; |
---|
| 845 | iMaxQP = iBaseQP; |
---|
| 846 | } |
---|
| 847 | else |
---|
| 848 | { |
---|
| 849 | Int iStartQP; |
---|
| 850 | if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
| 851 | { |
---|
| 852 | iStartQP = rpcTempCU->getQP(0); |
---|
| 853 | } |
---|
| 854 | else |
---|
| 855 | { |
---|
| 856 | UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
| 857 | iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx); |
---|
| 858 | } |
---|
| 859 | iMinQP = iStartQP; |
---|
| 860 | iMaxQP = iStartQP; |
---|
| 861 | } |
---|
| 862 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 863 | if ( m_pcEncCfg->getUseRateCtrl() ) |
---|
| 864 | { |
---|
| 865 | iMinQP = m_pcRateCtrl->getRCQP(); |
---|
| 866 | iMaxQP = m_pcRateCtrl->getRCQP(); |
---|
| 867 | } |
---|
| 868 | #else |
---|
| 869 | if(m_pcEncCfg->getUseRateCtrl()) |
---|
| 870 | { |
---|
| 871 | Int qp = m_pcRateCtrl->getUnitQP(); |
---|
| 872 | iMinQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
| 873 | iMaxQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
| 874 | } |
---|
| 875 | #endif |
---|
| 876 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
| 877 | { |
---|
| 878 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
| 879 | { |
---|
| 880 | iQP = lowestQP; |
---|
| 881 | } |
---|
| 882 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
| 883 | |
---|
| 884 | // further split |
---|
| 885 | if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 886 | { |
---|
| 887 | UChar uhNextDepth = uiDepth+1; |
---|
| 888 | TComDataCU* pcSubBestPartCU = m_ppcBestCU[uhNextDepth]; |
---|
| 889 | TComDataCU* pcSubTempPartCU = m_ppcTempCU[uhNextDepth]; |
---|
| 890 | |
---|
| 891 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ ) |
---|
| 892 | { |
---|
| 893 | pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
| 894 | pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
| 895 | |
---|
| 896 | Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
| 897 | if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 898 | { |
---|
| 899 | if( m_bUseSBACRD ) |
---|
| 900 | { |
---|
| 901 | if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer |
---|
| 902 | { |
---|
| 903 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
| 904 | } |
---|
| 905 | else |
---|
| 906 | { |
---|
| 907 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]); |
---|
| 908 | } |
---|
| 909 | } |
---|
| 910 | |
---|
| 911 | #if AMP_ENC_SPEEDUP |
---|
| 912 | if ( rpcBestCU->isIntra(0) ) |
---|
| 913 | { |
---|
| 914 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE ); |
---|
| 915 | } |
---|
| 916 | else |
---|
| 917 | { |
---|
| 918 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) ); |
---|
| 919 | } |
---|
| 920 | #else |
---|
| 921 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth ); |
---|
| 922 | #endif |
---|
| 923 | |
---|
| 924 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); // Keep best part data to current temporary data. |
---|
| 925 | xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth ); |
---|
| 926 | } |
---|
| 927 | else if (bInSlice) |
---|
| 928 | { |
---|
| 929 | pcSubBestPartCU->copyToPic( uhNextDepth ); |
---|
| 930 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); |
---|
| 931 | } |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | if( !bBoundary ) |
---|
| 935 | { |
---|
| 936 | m_pcEntropyCoder->resetBits(); |
---|
| 937 | m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true ); |
---|
| 938 | |
---|
| 939 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
| 940 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 941 | { |
---|
| 942 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 943 | } |
---|
| 944 | } |
---|
| 945 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 946 | |
---|
| 947 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP()) |
---|
| 948 | { |
---|
| 949 | Bool hasResidual = false; |
---|
| 950 | for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++) |
---|
| 951 | { |
---|
| 952 | if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && |
---|
| 953 | ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) ) |
---|
| 954 | { |
---|
| 955 | hasResidual = true; |
---|
| 956 | break; |
---|
| 957 | } |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | UInt uiTargetPartIdx; |
---|
| 961 | if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() ) |
---|
| 962 | { |
---|
| 963 | uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
| 964 | } |
---|
| 965 | else |
---|
| 966 | { |
---|
| 967 | uiTargetPartIdx = 0; |
---|
| 968 | } |
---|
| 969 | if ( hasResidual ) |
---|
| 970 | { |
---|
| 971 | #if !RDO_WITHOUT_DQP_BITS |
---|
| 972 | m_pcEntropyCoder->resetBits(); |
---|
| 973 | m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false ); |
---|
| 974 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
| 975 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 976 | { |
---|
| 977 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 978 | } |
---|
| 979 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 980 | #endif |
---|
| 981 | |
---|
| 982 | Bool foundNonZeroCbf = false; |
---|
| 983 | rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf ); |
---|
| 984 | assert( foundNonZeroCbf ); |
---|
| 985 | } |
---|
| 986 | else |
---|
| 987 | { |
---|
| 988 | rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP |
---|
| 989 | } |
---|
| 990 | } |
---|
| 991 | |
---|
| 992 | if( m_bUseSBACRD ) |
---|
| 993 | { |
---|
| 994 | m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 995 | } |
---|
| 996 | Bool isEndOfSlice = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES |
---|
| 997 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3); |
---|
| 998 | Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES |
---|
| 999 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3); |
---|
| 1000 | if(isEndOfSlice||isEndOfSliceSegment) |
---|
| 1001 | { |
---|
| 1002 | rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1; |
---|
| 1003 | } |
---|
| 1004 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth); // RD compare current larger prediction |
---|
| 1005 | } // with sub partitioned prediction. |
---|
| 1006 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
| 1007 | { |
---|
| 1008 | iQP = iMinQP; |
---|
| 1009 | } |
---|
| 1010 | } |
---|
| 1011 | |
---|
| 1012 | rpcBestCU->copyToPic(uiDepth); // Copy Best data to Picture for next partition prediction. |
---|
| 1013 | |
---|
| 1014 | xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY ); // Copy Yuv data to picture Yuv |
---|
| 1015 | if( bBoundary ||(bSliceEnd && bInsidePicture)) |
---|
| 1016 | { |
---|
| 1017 | return; |
---|
| 1018 | } |
---|
| 1019 | |
---|
| 1020 | // Assert if Best prediction mode is NONE |
---|
| 1021 | // Selected mode's RD-cost must be not MAX_DOUBLE. |
---|
| 1022 | assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE ); |
---|
| 1023 | assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE ); |
---|
| 1024 | assert( rpcBestCU->getTotalCost ( ) != MAX_DOUBLE ); |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | /** finish encoding a cu and handle end-of-slice conditions |
---|
| 1028 | * \param pcCU |
---|
| 1029 | * \param uiAbsPartIdx |
---|
| 1030 | * \param uiDepth |
---|
| 1031 | * \returns Void |
---|
| 1032 | */ |
---|
| 1033 | Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
| 1034 | { |
---|
| 1035 | TComPic* pcPic = pcCU->getPic(); |
---|
| 1036 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
| 1037 | |
---|
| 1038 | //Calculate end address |
---|
| 1039 | UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx; |
---|
| 1040 | |
---|
| 1041 | UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU(); |
---|
| 1042 | UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU(); |
---|
| 1043 | UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1044 | UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1045 | UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples(); |
---|
| 1046 | UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples(); |
---|
| 1047 | while(uiPosX>=uiWidth||uiPosY>=uiHeight) |
---|
| 1048 | { |
---|
| 1049 | uiInternalAddress--; |
---|
| 1050 | uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1051 | uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
| 1052 | } |
---|
| 1053 | uiInternalAddress++; |
---|
| 1054 | if(uiInternalAddress==pcCU->getPic()->getNumPartInCU()) |
---|
| 1055 | { |
---|
| 1056 | uiInternalAddress = 0; |
---|
| 1057 | uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1); |
---|
| 1058 | } |
---|
| 1059 | UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress); |
---|
| 1060 | |
---|
| 1061 | // Encode slice finish |
---|
| 1062 | Bool bTerminateSlice = false; |
---|
| 1063 | if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress) |
---|
| 1064 | { |
---|
| 1065 | bTerminateSlice = true; |
---|
| 1066 | } |
---|
| 1067 | UInt uiGranularityWidth = g_uiMaxCUWidth; |
---|
| 1068 | uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1069 | uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1070 | Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth)) |
---|
| 1071 | &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight)); |
---|
| 1072 | |
---|
| 1073 | if(granularityBoundary) |
---|
| 1074 | { |
---|
| 1075 | // The 1-terminating bit is added to all streams, so don't add it here when it's 1. |
---|
| 1076 | if (!bTerminateSlice) |
---|
| 1077 | m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 ); |
---|
| 1078 | } |
---|
| 1079 | |
---|
| 1080 | Int numberOfWrittenBits = 0; |
---|
| 1081 | if (m_pcBitCounter) |
---|
| 1082 | { |
---|
| 1083 | numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1084 | } |
---|
| 1085 | |
---|
| 1086 | // Calculate slice end IF this CU puts us over slice bit size. |
---|
| 1087 | UInt iGranularitySize = pcCU->getPic()->getNumPartInCU(); |
---|
| 1088 | Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize; |
---|
| 1089 | if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
| 1090 | { |
---|
| 1091 | iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1))); |
---|
| 1092 | } |
---|
| 1093 | // Set slice end parameter |
---|
| 1094 | if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) |
---|
| 1095 | { |
---|
| 1096 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
| 1097 | pcSlice->setSliceCurEndCUAddr(iGranularityEnd); |
---|
| 1098 | return; |
---|
| 1099 | } |
---|
| 1100 | // Set dependent slice end parameter |
---|
| 1101 | if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) |
---|
| 1102 | { |
---|
| 1103 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
| 1104 | return; |
---|
| 1105 | } |
---|
| 1106 | if(granularityBoundary) |
---|
| 1107 | { |
---|
| 1108 | pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) ); |
---|
| 1109 | pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits); |
---|
| 1110 | if (m_pcBitCounter) |
---|
| 1111 | { |
---|
| 1112 | m_pcEntropyCoder->resetBits(); |
---|
| 1113 | } |
---|
| 1114 | } |
---|
| 1115 | } |
---|
| 1116 | |
---|
| 1117 | /** Compute QP for each CU |
---|
| 1118 | * \param pcCU Target CU |
---|
| 1119 | * \param uiDepth CU depth |
---|
| 1120 | * \returns quantization parameter |
---|
| 1121 | */ |
---|
| 1122 | Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth ) |
---|
| 1123 | { |
---|
| 1124 | Int iBaseQp = pcCU->getSlice()->getSliceQp(); |
---|
| 1125 | Int iQpOffset = 0; |
---|
| 1126 | if ( m_pcEncCfg->getUseAdaptiveQP() ) |
---|
| 1127 | { |
---|
| 1128 | TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() ); |
---|
| 1129 | UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 ); |
---|
| 1130 | TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth ); |
---|
| 1131 | UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth(); |
---|
| 1132 | UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight(); |
---|
| 1133 | UInt uiAQUStride = pcAQLayer->getAQPartStride(); |
---|
| 1134 | TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit(); |
---|
| 1135 | |
---|
| 1136 | Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0); |
---|
| 1137 | Double dAvgAct = pcAQLayer->getAvgActivity(); |
---|
| 1138 | Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity(); |
---|
| 1139 | Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct); |
---|
| 1140 | Double dQpOffset = log(dNormAct) / log(2.0) * 6.0; |
---|
| 1141 | iQpOffset = Int(floor( dQpOffset + 0.49999 )); |
---|
| 1142 | } |
---|
| 1143 | return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset ); |
---|
| 1144 | } |
---|
| 1145 | |
---|
| 1146 | /** encode a CU block recursively |
---|
| 1147 | * \param pcCU |
---|
| 1148 | * \param uiAbsPartIdx |
---|
| 1149 | * \param uiDepth |
---|
| 1150 | * \returns Void |
---|
| 1151 | */ |
---|
| 1152 | Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
| 1153 | { |
---|
| 1154 | TComPic* pcPic = pcCU->getPic(); |
---|
| 1155 | |
---|
| 1156 | Bool bBoundary = false; |
---|
| 1157 | UInt uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1158 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
| 1159 | UInt uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1160 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
| 1161 | |
---|
| 1162 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
| 1163 | // If slice start is within this cu... |
---|
| 1164 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
| 1165 | pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) ); |
---|
| 1166 | // We need to split, so don't try these modes. |
---|
| 1167 | if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1168 | { |
---|
| 1169 | m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 1170 | } |
---|
| 1171 | else |
---|
| 1172 | { |
---|
| 1173 | bBoundary = true; |
---|
| 1174 | } |
---|
| 1175 | |
---|
| 1176 | if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary ) |
---|
| 1177 | { |
---|
| 1178 | UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
| 1179 | if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
| 1180 | { |
---|
| 1181 | setdQPFlag(true); |
---|
| 1182 | } |
---|
| 1183 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
| 1184 | { |
---|
| 1185 | uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1186 | uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
| 1187 | Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
| 1188 | if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1189 | { |
---|
| 1190 | xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 ); |
---|
| 1191 | } |
---|
| 1192 | } |
---|
| 1193 | return; |
---|
| 1194 | } |
---|
| 1195 | |
---|
| 1196 | if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
| 1197 | { |
---|
| 1198 | setdQPFlag(true); |
---|
| 1199 | } |
---|
| 1200 | if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1201 | { |
---|
| 1202 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx ); |
---|
| 1203 | } |
---|
| 1204 | if( !pcCU->getSlice()->isIntra() ) |
---|
| 1205 | { |
---|
| 1206 | m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx ); |
---|
| 1207 | } |
---|
| 1208 | |
---|
| 1209 | if( pcCU->isSkipped( uiAbsPartIdx ) ) |
---|
| 1210 | { |
---|
| 1211 | m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx ); |
---|
| 1212 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
| 1213 | return; |
---|
| 1214 | } |
---|
| 1215 | #if INTRA_BL |
---|
| 1216 | m_pcEntropyCoder->encodeIntraBLFlag( pcCU, uiAbsPartIdx ); |
---|
| 1217 | if ( !pcCU->isIntraBL( uiAbsPartIdx ) ) |
---|
| 1218 | { |
---|
| 1219 | #endif |
---|
| 1220 | m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx ); |
---|
| 1221 | |
---|
| 1222 | m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 1223 | |
---|
| 1224 | if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) |
---|
| 1225 | { |
---|
| 1226 | m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx ); |
---|
| 1227 | |
---|
| 1228 | if(pcCU->getIPCMFlag(uiAbsPartIdx)) |
---|
| 1229 | { |
---|
| 1230 | // Encode slice finish |
---|
| 1231 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
| 1232 | return; |
---|
| 1233 | } |
---|
| 1234 | } |
---|
| 1235 | |
---|
| 1236 | // prediction Info ( Intra : direction mode, Inter : Mv, reference idx ) |
---|
| 1237 | m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx ); |
---|
| 1238 | #if INTRA_BL |
---|
| 1239 | } |
---|
| 1240 | #endif |
---|
| 1241 | |
---|
| 1242 | // Encode Coefficients |
---|
| 1243 | Bool bCodeDQP = getdQPFlag(); |
---|
| 1244 | m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP ); |
---|
| 1245 | setdQPFlag( bCodeDQP ); |
---|
| 1246 | |
---|
| 1247 | // --- write terminating bit --- |
---|
| 1248 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
| 1249 | } |
---|
| 1250 | |
---|
| 1251 | /** check RD costs for a CU block encoded with merge |
---|
| 1252 | * \param rpcBestCU |
---|
| 1253 | * \param rpcTempCU |
---|
| 1254 | * \returns Void |
---|
| 1255 | */ |
---|
| 1256 | Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode ) |
---|
| 1257 | { |
---|
| 1258 | assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE ); |
---|
| 1259 | TComMvField cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists |
---|
| 1260 | UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS]; |
---|
| 1261 | Int numValidMergeCand = 0; |
---|
| 1262 | |
---|
| 1263 | for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui ) |
---|
| 1264 | { |
---|
| 1265 | uhInterDirNeighbours[ui] = 0; |
---|
| 1266 | } |
---|
| 1267 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1268 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1269 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
| 1270 | rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand ); |
---|
| 1271 | |
---|
| 1272 | Int mergeCandBuffer[MRG_MAX_NUM_CANDS]; |
---|
| 1273 | for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui ) |
---|
| 1274 | { |
---|
| 1275 | mergeCandBuffer[ui] = 0; |
---|
| 1276 | } |
---|
| 1277 | |
---|
| 1278 | Bool bestIsSkip = false; |
---|
| 1279 | |
---|
| 1280 | UInt iteration; |
---|
| 1281 | if ( rpcTempCU->isLosslessCoded(0)) |
---|
| 1282 | { |
---|
| 1283 | iteration = 1; |
---|
| 1284 | } |
---|
| 1285 | else |
---|
| 1286 | { |
---|
| 1287 | iteration = 2; |
---|
| 1288 | } |
---|
| 1289 | |
---|
| 1290 | for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual ) |
---|
| 1291 | { |
---|
| 1292 | for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand ) |
---|
| 1293 | { |
---|
| 1294 | #if REF_IDX_ME_ZEROMV |
---|
| 1295 | Bool bZeroMVILR = rpcTempCU->xCheckZeroMVILRMerge(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]); |
---|
| 1296 | if(bZeroMVILR) |
---|
| 1297 | { |
---|
| 1298 | #endif |
---|
| 1299 | if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1)) |
---|
| 1300 | { |
---|
| 1301 | |
---|
| 1302 | if( !(bestIsSkip && uiNoResidual == 0) ) |
---|
| 1303 | { |
---|
| 1304 | // set MC parameters |
---|
| 1305 | rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1306 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
| 1307 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1308 | rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1309 | rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1310 | rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
| 1311 | rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
| 1312 | rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
| 1313 | |
---|
| 1314 | // do MC |
---|
| 1315 | m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] ); |
---|
| 1316 | // estimate residual and encode everything |
---|
| 1317 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, |
---|
| 1318 | m_ppcOrigYuv [uhDepth], |
---|
| 1319 | m_ppcPredYuvTemp[uhDepth], |
---|
| 1320 | m_ppcResiYuvTemp[uhDepth], |
---|
| 1321 | m_ppcResiYuvBest[uhDepth], |
---|
| 1322 | m_ppcRecoYuvTemp[uhDepth], |
---|
| 1323 | (uiNoResidual? true:false)); |
---|
| 1324 | |
---|
| 1325 | |
---|
| 1326 | if(uiNoResidual==0) |
---|
| 1327 | { |
---|
| 1328 | if(rpcTempCU->getQtRootCbf(0) == 0) |
---|
| 1329 | { |
---|
| 1330 | mergeCandBuffer[uiMergeCand] = 1; |
---|
| 1331 | } |
---|
| 1332 | } |
---|
| 1333 | |
---|
| 1334 | rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth ); |
---|
| 1335 | Int orgQP = rpcTempCU->getQP( 0 ); |
---|
| 1336 | xCheckDQP( rpcTempCU ); |
---|
| 1337 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
| 1338 | rpcTempCU->initEstData( uhDepth, orgQP ); |
---|
| 1339 | |
---|
| 1340 | |
---|
| 1341 | if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip ) |
---|
| 1342 | { |
---|
| 1343 | bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0; |
---|
| 1344 | } |
---|
| 1345 | |
---|
| 1346 | } |
---|
| 1347 | } |
---|
| 1348 | #if REF_IDX_ME_ZEROMV |
---|
| 1349 | } |
---|
| 1350 | #endif |
---|
| 1351 | } |
---|
| 1352 | |
---|
| 1353 | if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection()) |
---|
| 1354 | { |
---|
| 1355 | if(rpcBestCU->getQtRootCbf( 0 ) == 0) |
---|
| 1356 | { |
---|
| 1357 | if( rpcBestCU->getMergeFlag( 0 )) |
---|
| 1358 | { |
---|
| 1359 | *earlyDetectionSkipMode = true; |
---|
| 1360 | } |
---|
| 1361 | else |
---|
| 1362 | { |
---|
| 1363 | Int absoulte_MV=0; |
---|
| 1364 | for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ ) |
---|
| 1365 | { |
---|
| 1366 | if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 ) |
---|
| 1367 | { |
---|
| 1368 | TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx )); |
---|
| 1369 | Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor(); |
---|
| 1370 | Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer(); |
---|
| 1371 | absoulte_MV+=iHor+iVer; |
---|
| 1372 | } |
---|
| 1373 | } |
---|
| 1374 | |
---|
| 1375 | if(absoulte_MV == 0) |
---|
| 1376 | { |
---|
| 1377 | *earlyDetectionSkipMode = true; |
---|
| 1378 | } |
---|
| 1379 | } |
---|
| 1380 | } |
---|
| 1381 | } |
---|
| 1382 | } |
---|
| 1383 | } |
---|
| 1384 | |
---|
| 1385 | |
---|
| 1386 | #if AMP_MRG |
---|
| 1387 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG) |
---|
| 1388 | #else |
---|
| 1389 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize ) |
---|
| 1390 | #endif |
---|
| 1391 | { |
---|
| 1392 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1393 | |
---|
| 1394 | rpcTempCU->setDepthSubParts( uhDepth, 0 ); |
---|
| 1395 | |
---|
| 1396 | rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth ); |
---|
| 1397 | |
---|
| 1398 | rpcTempCU->setPartSizeSubParts ( ePartSize, 0, uhDepth ); |
---|
| 1399 | rpcTempCU->setPredModeSubParts ( MODE_INTER, 0, uhDepth ); |
---|
| 1400 | rpcTempCU->setCUTransquantBypassSubParts ( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
| 1401 | |
---|
| 1402 | #if AMP_MRG |
---|
| 1403 | rpcTempCU->setMergeAMP (true); |
---|
| 1404 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG ); |
---|
| 1405 | #else |
---|
| 1406 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] ); |
---|
| 1407 | #endif |
---|
| 1408 | |
---|
| 1409 | #if AMP_MRG |
---|
| 1410 | if ( !rpcTempCU->getMergeAMP() ) |
---|
| 1411 | { |
---|
| 1412 | return; |
---|
| 1413 | } |
---|
| 1414 | #endif |
---|
| 1415 | |
---|
| 1416 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 1417 | if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth ) |
---|
| 1418 | { |
---|
| 1419 | UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(), |
---|
| 1420 | m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(), |
---|
| 1421 | rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) ); |
---|
| 1422 | m_temporalSAD = (Int)SAD; |
---|
| 1423 | } |
---|
| 1424 | #endif |
---|
| 1425 | |
---|
| 1426 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false ); |
---|
| 1427 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1428 | |
---|
| 1429 | xCheckDQP( rpcTempCU ); |
---|
| 1430 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
| 1431 | } |
---|
| 1432 | |
---|
| 1433 | Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize ) |
---|
| 1434 | { |
---|
| 1435 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1436 | |
---|
| 1437 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
| 1438 | |
---|
| 1439 | rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth ); |
---|
| 1440 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
| 1441 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
| 1442 | |
---|
| 1443 | Bool bSeparateLumaChroma = true; // choose estimation mode |
---|
| 1444 | UInt uiPreCalcDistC = 0; |
---|
| 1445 | if( !bSeparateLumaChroma ) |
---|
| 1446 | { |
---|
| 1447 | m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] ); |
---|
| 1448 | } |
---|
| 1449 | m_pcPredSearch ->estIntraPredQT ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma ); |
---|
| 1450 | |
---|
| 1451 | m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() ); |
---|
| 1452 | |
---|
| 1453 | m_pcPredSearch ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC ); |
---|
| 1454 | |
---|
| 1455 | m_pcEntropyCoder->resetBits(); |
---|
| 1456 | #if INTRA_BL |
---|
| 1457 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
| 1458 | #endif |
---|
| 1459 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1460 | { |
---|
| 1461 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
| 1462 | } |
---|
| 1463 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
| 1464 | m_pcEntropyCoder->encodePredMode( rpcTempCU, 0, true ); |
---|
| 1465 | m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true ); |
---|
| 1466 | m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0, true ); |
---|
| 1467 | m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true ); |
---|
| 1468 | |
---|
| 1469 | // Encode Coefficients |
---|
| 1470 | Bool bCodeDQP = getdQPFlag(); |
---|
| 1471 | m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP ); |
---|
| 1472 | setdQPFlag( bCodeDQP ); |
---|
| 1473 | |
---|
| 1474 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 1475 | |
---|
| 1476 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1477 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1478 | { |
---|
| 1479 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1480 | } |
---|
| 1481 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1482 | |
---|
| 1483 | xCheckDQP( rpcTempCU ); |
---|
| 1484 | xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth); |
---|
| 1485 | } |
---|
| 1486 | |
---|
| 1487 | /** Check R-D costs for a CU with PCM mode. |
---|
| 1488 | * \param rpcBestCU pointer to best mode CU data structure |
---|
| 1489 | * \param rpcTempCU pointer to testing mode CU data structure |
---|
| 1490 | * \returns Void |
---|
| 1491 | * |
---|
| 1492 | * \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. |
---|
| 1493 | */ |
---|
| 1494 | Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU ) |
---|
| 1495 | { |
---|
| 1496 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1497 | |
---|
| 1498 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
| 1499 | |
---|
| 1500 | rpcTempCU->setIPCMFlag(0, true); |
---|
| 1501 | rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0)); |
---|
| 1502 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); |
---|
| 1503 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
| 1504 | rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth ); |
---|
| 1505 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
| 1506 | |
---|
| 1507 | m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]); |
---|
| 1508 | |
---|
| 1509 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
| 1510 | |
---|
| 1511 | m_pcEntropyCoder->resetBits(); |
---|
| 1512 | #if INTRA_BL |
---|
| 1513 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
| 1514 | #endif |
---|
| 1515 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1516 | { |
---|
| 1517 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
| 1518 | } |
---|
| 1519 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
| 1520 | m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0, true ); |
---|
| 1521 | m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true ); |
---|
| 1522 | m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true ); |
---|
| 1523 | |
---|
| 1524 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 1525 | |
---|
| 1526 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1527 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1528 | { |
---|
| 1529 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1530 | } |
---|
| 1531 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1532 | |
---|
| 1533 | xCheckDQP( rpcTempCU ); |
---|
| 1534 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth ); |
---|
| 1535 | } |
---|
| 1536 | |
---|
| 1537 | /** check whether current try is the best with identifying the depth of current try |
---|
| 1538 | * \param rpcBestCU |
---|
| 1539 | * \param rpcTempCU |
---|
| 1540 | * \returns Void |
---|
| 1541 | */ |
---|
| 1542 | Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth ) |
---|
| 1543 | { |
---|
| 1544 | if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() ) |
---|
| 1545 | { |
---|
| 1546 | TComYuv* pcYuv; |
---|
| 1547 | // Change Information data |
---|
| 1548 | TComDataCU* pcCU = rpcBestCU; |
---|
| 1549 | rpcBestCU = rpcTempCU; |
---|
| 1550 | rpcTempCU = pcCU; |
---|
| 1551 | |
---|
| 1552 | // Change Prediction data |
---|
| 1553 | pcYuv = m_ppcPredYuvBest[uiDepth]; |
---|
| 1554 | m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth]; |
---|
| 1555 | m_ppcPredYuvTemp[uiDepth] = pcYuv; |
---|
| 1556 | |
---|
| 1557 | // Change Reconstruction data |
---|
| 1558 | pcYuv = m_ppcRecoYuvBest[uiDepth]; |
---|
| 1559 | m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth]; |
---|
| 1560 | m_ppcRecoYuvTemp[uiDepth] = pcYuv; |
---|
| 1561 | |
---|
| 1562 | pcYuv = NULL; |
---|
| 1563 | pcCU = NULL; |
---|
| 1564 | |
---|
| 1565 | if( m_bUseSBACRD ) // store temp best CI for next CU coding |
---|
| 1566 | m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]); |
---|
| 1567 | } |
---|
| 1568 | } |
---|
| 1569 | |
---|
| 1570 | Void TEncCu::xCheckDQP( TComDataCU* pcCU ) |
---|
| 1571 | { |
---|
| 1572 | UInt uiDepth = pcCU->getDepth( 0 ); |
---|
| 1573 | |
---|
| 1574 | if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
| 1575 | { |
---|
| 1576 | if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) ) |
---|
| 1577 | { |
---|
| 1578 | #if !RDO_WITHOUT_DQP_BITS |
---|
| 1579 | m_pcEntropyCoder->resetBits(); |
---|
| 1580 | m_pcEntropyCoder->encodeQP( pcCU, 0, false ); |
---|
| 1581 | pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
| 1582 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1583 | { |
---|
| 1584 | pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1585 | } |
---|
| 1586 | pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() ); |
---|
| 1587 | #endif |
---|
| 1588 | } |
---|
| 1589 | else |
---|
| 1590 | { |
---|
| 1591 | pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP |
---|
| 1592 | } |
---|
| 1593 | } |
---|
| 1594 | } |
---|
| 1595 | |
---|
| 1596 | Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst) |
---|
| 1597 | { |
---|
| 1598 | pDst->iN = pSrc->iN; |
---|
| 1599 | for (Int i = 0; i < pSrc->iN; i++) |
---|
| 1600 | { |
---|
| 1601 | pDst->m_acMvCand[i] = pSrc->m_acMvCand[i]; |
---|
| 1602 | } |
---|
| 1603 | } |
---|
| 1604 | Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY ) |
---|
| 1605 | { |
---|
| 1606 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
| 1607 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
| 1608 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
| 1609 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
| 1610 | pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
| 1611 | Bool bSliceEnd = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
| 1612 | pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
| 1613 | if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1614 | { |
---|
| 1615 | UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx]; |
---|
| 1616 | UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth); |
---|
| 1617 | UInt uiBlkWidth = rpcPic->getNumPartInWidth() >> (uiDepth); |
---|
| 1618 | UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
| 1619 | UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
| 1620 | UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX; |
---|
| 1621 | m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx); |
---|
| 1622 | } |
---|
| 1623 | else |
---|
| 1624 | { |
---|
| 1625 | UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
| 1626 | |
---|
| 1627 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
| 1628 | { |
---|
| 1629 | UInt uiSubCULPelX = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx & 1 ); |
---|
| 1630 | UInt uiSubCUTPelY = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 ); |
---|
| 1631 | |
---|
| 1632 | Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && |
---|
| 1633 | rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
| 1634 | if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
| 1635 | { |
---|
| 1636 | xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY ); // Copy Yuv data to picture Yuv |
---|
| 1637 | } |
---|
| 1638 | } |
---|
| 1639 | } |
---|
| 1640 | } |
---|
| 1641 | |
---|
| 1642 | Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth ) |
---|
| 1643 | { |
---|
| 1644 | UInt uiCurrDepth = uiNextDepth - 1; |
---|
| 1645 | m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx ); |
---|
| 1646 | } |
---|
| 1647 | |
---|
| 1648 | /** Function for filling the PCM buffer of a CU using its original sample array |
---|
| 1649 | * \param pcCU pointer to current CU |
---|
| 1650 | * \param pcOrgYuv pointer to original sample array |
---|
| 1651 | * \returns Void |
---|
| 1652 | */ |
---|
| 1653 | Void TEncCu::xFillPCMBuffer ( TComDataCU*& pCU, TComYuv* pOrgYuv ) |
---|
| 1654 | { |
---|
| 1655 | |
---|
| 1656 | UInt width = pCU->getWidth(0); |
---|
| 1657 | UInt height = pCU->getHeight(0); |
---|
| 1658 | |
---|
| 1659 | Pel* pSrcY = pOrgYuv->getLumaAddr(0, width); |
---|
| 1660 | Pel* pDstY = pCU->getPCMSampleY(); |
---|
| 1661 | UInt srcStride = pOrgYuv->getStride(); |
---|
| 1662 | |
---|
| 1663 | for(Int y = 0; y < height; y++ ) |
---|
| 1664 | { |
---|
| 1665 | for(Int x = 0; x < width; x++ ) |
---|
| 1666 | { |
---|
| 1667 | pDstY[x] = pSrcY[x]; |
---|
| 1668 | } |
---|
| 1669 | pDstY += width; |
---|
| 1670 | pSrcY += srcStride; |
---|
| 1671 | } |
---|
| 1672 | |
---|
| 1673 | Pel* pSrcCb = pOrgYuv->getCbAddr(); |
---|
| 1674 | Pel* pSrcCr = pOrgYuv->getCrAddr();; |
---|
| 1675 | |
---|
| 1676 | Pel* pDstCb = pCU->getPCMSampleCb(); |
---|
| 1677 | Pel* pDstCr = pCU->getPCMSampleCr();; |
---|
| 1678 | |
---|
| 1679 | UInt srcStrideC = pOrgYuv->getCStride(); |
---|
| 1680 | UInt heightC = height >> 1; |
---|
| 1681 | UInt widthC = width >> 1; |
---|
| 1682 | |
---|
| 1683 | for(Int y = 0; y < heightC; y++ ) |
---|
| 1684 | { |
---|
| 1685 | for(Int x = 0; x < widthC; x++ ) |
---|
| 1686 | { |
---|
| 1687 | pDstCb[x] = pSrcCb[x]; |
---|
| 1688 | pDstCr[x] = pSrcCr[x]; |
---|
| 1689 | } |
---|
| 1690 | pDstCb += widthC; |
---|
| 1691 | pDstCr += widthC; |
---|
| 1692 | pSrcCb += srcStrideC; |
---|
| 1693 | pSrcCr += srcStrideC; |
---|
| 1694 | } |
---|
| 1695 | } |
---|
| 1696 | |
---|
| 1697 | #if ADAPTIVE_QP_SELECTION |
---|
| 1698 | /** Collect ARL statistics from one block |
---|
| 1699 | */ |
---|
| 1700 | Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples ) |
---|
| 1701 | { |
---|
| 1702 | for( Int n = 0; n < NumCoeffInCU; n++ ) |
---|
| 1703 | { |
---|
| 1704 | Int u = abs( rpcCoeff[ n ] ); |
---|
| 1705 | Int absc = rpcArlCoeff[ n ]; |
---|
| 1706 | |
---|
| 1707 | if( u != 0 ) |
---|
| 1708 | { |
---|
| 1709 | if( u < LEVEL_RANGE ) |
---|
| 1710 | { |
---|
| 1711 | cSum[ u ] += ( Double )absc; |
---|
| 1712 | numSamples[ u ]++; |
---|
| 1713 | } |
---|
| 1714 | else |
---|
| 1715 | { |
---|
| 1716 | cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION ); |
---|
| 1717 | numSamples[ LEVEL_RANGE ]++; |
---|
| 1718 | } |
---|
| 1719 | } |
---|
| 1720 | } |
---|
| 1721 | |
---|
| 1722 | return 0; |
---|
| 1723 | } |
---|
| 1724 | |
---|
| 1725 | /** Collect ARL statistics from one LCU |
---|
| 1726 | * \param pcCU |
---|
| 1727 | */ |
---|
| 1728 | Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU ) |
---|
| 1729 | { |
---|
| 1730 | Double cSum[ LEVEL_RANGE + 1 ]; //: the sum of DCT coefficients corresponding to datatype and quantization output |
---|
| 1731 | UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output |
---|
| 1732 | |
---|
| 1733 | TCoeff* pCoeffY = rpcCU->getCoeffY(); |
---|
| 1734 | Int* pArlCoeffY = rpcCU->getArlCoeffY(); |
---|
| 1735 | |
---|
| 1736 | UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth; |
---|
| 1737 | UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth; |
---|
| 1738 | |
---|
| 1739 | memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) ); |
---|
| 1740 | memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) ); |
---|
| 1741 | |
---|
| 1742 | // Collect stats to cSum[][] and numSamples[][] |
---|
| 1743 | for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ ) |
---|
| 1744 | { |
---|
| 1745 | UInt uiTrIdx = rpcCU->getTransformIdx(i); |
---|
| 1746 | |
---|
| 1747 | if(rpcCU->getPredictionMode(i) == MODE_INTER) |
---|
| 1748 | if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) ) |
---|
| 1749 | { |
---|
| 1750 | xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples); |
---|
| 1751 | }//Note that only InterY is processed. QP rounding is based on InterY data only. |
---|
| 1752 | |
---|
| 1753 | pCoeffY += uiMinNumCoeffInCU; |
---|
| 1754 | pArlCoeffY += uiMinNumCoeffInCU; |
---|
| 1755 | } |
---|
| 1756 | |
---|
| 1757 | for(Int u=1; u<LEVEL_RANGE;u++) |
---|
| 1758 | { |
---|
| 1759 | m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ; |
---|
| 1760 | m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ; |
---|
| 1761 | } |
---|
| 1762 | m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ; |
---|
| 1763 | m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ; |
---|
| 1764 | } |
---|
| 1765 | #endif |
---|
| 1766 | |
---|
| 1767 | #if INTRA_BL |
---|
| 1768 | Void TEncCu::xCheckRDCostIntraBL( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU ) |
---|
| 1769 | { |
---|
| 1770 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
| 1771 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
| 1772 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); |
---|
| 1773 | rpcTempCU->setPredModeSubParts( MODE_INTRA_BL, 0, uiDepth ); |
---|
| 1774 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
| 1775 | |
---|
| 1776 | m_pcPredSearch->setBaseRecPic( m_pcPicYuvRecBase ); |
---|
| 1777 | #if NO_RESIDUAL_FLAG_FOR_BLPRED |
---|
| 1778 | rpcTempCU->setDepthSubParts( uiDepth, 0 ); |
---|
| 1779 | // rpcTempCU->setLumaIntraDirSubParts( DC_IDX, 0, uiDepth ); |
---|
| 1780 | // rpcTempCU->setChromIntraDirSubParts( DC_IDX, 0, uiDepth ); |
---|
| 1781 | m_ppcPredYuvTemp[uiDepth]->copyFromPicLuma ( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, rpcTempCU->getWidth(0), rpcTempCU->getHeight(0)); |
---|
| 1782 | m_ppcPredYuvTemp[uiDepth]->copyFromPicChroma( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, (rpcTempCU->getWidth(0)>>1), (rpcTempCU->getHeight(0)>>1), 0); |
---|
| 1783 | m_ppcPredYuvTemp[uiDepth]->copyFromPicChroma( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, (rpcTempCU->getWidth(0)>>1), (rpcTempCU->getHeight(0)>>1), 1); |
---|
| 1784 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcResiYuvBest[uiDepth], m_ppcRecoYuvTemp[uiDepth], false ); |
---|
| 1785 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1786 | #else |
---|
| 1787 | |
---|
| 1788 | m_pcPredSearch->estIntraBLPredQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth] ); |
---|
| 1789 | |
---|
| 1790 | m_pcEntropyCoder->resetBits(); |
---|
| 1791 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
| 1792 | m_pcEntropyCoder->encodeSkipFlag( rpcTempCU, 0, true ); |
---|
| 1793 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
| 1794 | { |
---|
| 1795 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
| 1796 | } |
---|
| 1797 | |
---|
| 1798 | // Encode Coefficients |
---|
| 1799 | Bool bCodeDQP = getdQPFlag(); |
---|
| 1800 | m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP ); |
---|
| 1801 | setdQPFlag( bCodeDQP ); |
---|
| 1802 | |
---|
| 1803 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
| 1804 | |
---|
| 1805 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
| 1806 | if(m_pcEncCfg->getUseSBACRD()) |
---|
| 1807 | { |
---|
| 1808 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
| 1809 | } |
---|
| 1810 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1811 | #endif |
---|
| 1812 | |
---|
| 1813 | xCheckDQP( rpcTempCU ); |
---|
| 1814 | xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth); |
---|
| 1815 | } |
---|
| 1816 | #endif |
---|
| 1817 | |
---|
| 1818 | #if (ENCODER_FAST_MODE) |
---|
[20] | 1819 | Void TEncCu::xCheckRDCostILRUni(TComDataCU *&rpcBestCU, TComDataCU *&rpcTempCU) |
---|
| 1820 | { |
---|
| 1821 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
[2] | 1822 | |
---|
[20] | 1823 | rpcTempCU->setDepthSubParts( uhDepth, 0 ); |
---|
| 1824 | |
---|
| 1825 | #if SKIP_FLAG |
---|
| 1826 | rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth ); |
---|
| 1827 | #endif |
---|
| 1828 | |
---|
| 1829 | rpcTempCU->setPartSizeSubParts ( SIZE_2Nx2N, 0, uhDepth ); //2Nx2N |
---|
| 1830 | rpcTempCU->setPredModeSubParts ( MODE_INTER, 0, uhDepth ); |
---|
| 1831 | rpcTempCU->setCUTransquantBypassSubParts ( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
| 1832 | |
---|
| 1833 | Bool exitILR = m_pcPredSearch->predInterSearchILRUni( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] ); |
---|
| 1834 | |
---|
| 1835 | if(!exitILR) |
---|
[125] | 1836 | { |
---|
[20] | 1837 | return; |
---|
[125] | 1838 | } |
---|
[20] | 1839 | |
---|
| 1840 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false ); |
---|
| 1841 | |
---|
| 1842 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
| 1843 | |
---|
| 1844 | xCheckDQP( rpcTempCU ); |
---|
| 1845 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
| 1846 | |
---|
| 1847 | return; |
---|
| 1848 | } |
---|
[191] | 1849 | #endif |
---|
| 1850 | |
---|
| 1851 | |
---|
| 1852 | //! \} |
---|