[313] | 1 | /* The copyright in this software is being made available under the BSD |
---|
| 2 | * License, included below. This software may be subject to other third party |
---|
| 3 | * and contributor rights, including patent rights, and no such rights are |
---|
[1029] | 4 | * granted under this license. |
---|
[313] | 5 | * |
---|
[1259] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[313] | 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright notice, |
---|
| 13 | * this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 15 | * this list of conditions and the following disclaimer in the documentation |
---|
| 16 | * and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
| 18 | * be used to endorse or promote products derived from this software without |
---|
| 19 | * specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
| 31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** \file TEncEntropy.cpp |
---|
| 35 | \brief entropy encoder class |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TEncEntropy.h" |
---|
| 39 | #include "TLibCommon/TypeDef.h" |
---|
| 40 | #include "TLibCommon/TComSampleAdaptiveOffset.h" |
---|
[1029] | 41 | #include "TLibCommon/TComTU.h" |
---|
[313] | 42 | |
---|
[1029] | 43 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
| 44 | #include "../TLibCommon/Debug.h" |
---|
| 45 | static const Bool bDebugPredEnabled = DebugOptionList::DebugPred.getInt()!=0; |
---|
| 46 | #endif |
---|
| 47 | |
---|
[313] | 48 | //! \ingroup TLibEncoder |
---|
| 49 | //! \{ |
---|
| 50 | |
---|
| 51 | Void TEncEntropy::setEntropyCoder ( TEncEntropyIf* e, TComSlice* pcSlice ) |
---|
| 52 | { |
---|
| 53 | m_pcEntropyCoderIf = e; |
---|
| 54 | m_pcEntropyCoderIf->setSlice ( pcSlice ); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | Void TEncEntropy::encodeSliceHeader ( TComSlice* pcSlice ) |
---|
| 58 | { |
---|
| 59 | m_pcEntropyCoderIf->codeSliceHeader( pcSlice ); |
---|
| 60 | return; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | Void TEncEntropy::encodeTilesWPPEntryPoint( TComSlice* pSlice ) |
---|
| 64 | { |
---|
| 65 | m_pcEntropyCoderIf->codeTilesWPPEntryPoint( pSlice ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | Void TEncEntropy::encodeTerminatingBit ( UInt uiIsLast ) |
---|
| 69 | { |
---|
| 70 | m_pcEntropyCoderIf->codeTerminatingBit( uiIsLast ); |
---|
[1029] | 71 | |
---|
[313] | 72 | return; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | Void TEncEntropy::encodeSliceFinish() |
---|
| 76 | { |
---|
| 77 | m_pcEntropyCoderIf->codeSliceFinish(); |
---|
| 78 | } |
---|
| 79 | |
---|
[1212] | 80 | #if CGS_3D_ASYMLUT |
---|
[1235] | 81 | Void TEncEntropy::encodePPS( const TComPPS* pcPPS, TEnc3DAsymLUT * pc3DAsymLUT ) |
---|
[713] | 82 | { |
---|
| 83 | m_pcEntropyCoderIf->codePPS( pcPPS, pc3DAsymLUT ); |
---|
| 84 | return; |
---|
| 85 | } |
---|
| 86 | #else |
---|
[1235] | 87 | Void TEncEntropy::encodePPS( const TComPPS* pcPPS ) |
---|
[313] | 88 | { |
---|
| 89 | m_pcEntropyCoderIf->codePPS( pcPPS ); |
---|
| 90 | return; |
---|
| 91 | } |
---|
[713] | 92 | #endif |
---|
[313] | 93 | |
---|
[1235] | 94 | Void TEncEntropy::encodeSPS( const TComSPS* pcSPS ) |
---|
[313] | 95 | { |
---|
| 96 | m_pcEntropyCoderIf->codeSPS( pcSPS ); |
---|
| 97 | return; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | Void TEncEntropy::encodeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 101 | { |
---|
| 102 | if( bRD ) |
---|
| 103 | { |
---|
| 104 | uiAbsPartIdx = 0; |
---|
| 105 | } |
---|
| 106 | m_pcEntropyCoderIf->codeCUTransquantBypassFlag( pcCU, uiAbsPartIdx ); |
---|
| 107 | } |
---|
| 108 | |
---|
[1235] | 109 | Void TEncEntropy::encodeVPS( const TComVPS* pcVPS ) |
---|
[313] | 110 | { |
---|
| 111 | m_pcEntropyCoderIf->codeVPS( pcVPS ); |
---|
| 112 | return; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | Void TEncEntropy::encodeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 116 | { |
---|
| 117 | if ( pcCU->getSlice()->isIntra() ) |
---|
| 118 | { |
---|
| 119 | return; |
---|
| 120 | } |
---|
| 121 | if( bRD ) |
---|
| 122 | { |
---|
| 123 | uiAbsPartIdx = 0; |
---|
| 124 | } |
---|
| 125 | m_pcEntropyCoderIf->codeSkipFlag( pcCU, uiAbsPartIdx ); |
---|
| 126 | } |
---|
| 127 | |
---|
[1260] | 128 | //! encode merge flag |
---|
[313] | 129 | Void TEncEntropy::encodeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
[1029] | 130 | { |
---|
[313] | 131 | // at least one merge candidate exists |
---|
| 132 | m_pcEntropyCoderIf->codeMergeFlag( pcCU, uiAbsPartIdx ); |
---|
| 133 | } |
---|
| 134 | |
---|
[1260] | 135 | //! encode merge index |
---|
[313] | 136 | Void TEncEntropy::encodeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 137 | { |
---|
| 138 | if( bRD ) |
---|
| 139 | { |
---|
| 140 | uiAbsPartIdx = 0; |
---|
| 141 | assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ); |
---|
| 142 | } |
---|
| 143 | m_pcEntropyCoderIf->codeMergeIndex( pcCU, uiAbsPartIdx ); |
---|
| 144 | } |
---|
| 145 | |
---|
[1029] | 146 | |
---|
[1260] | 147 | //! encode prediction mode |
---|
[313] | 148 | Void TEncEntropy::encodePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 149 | { |
---|
| 150 | if( bRD ) |
---|
| 151 | { |
---|
| 152 | uiAbsPartIdx = 0; |
---|
| 153 | } |
---|
[1029] | 154 | |
---|
[313] | 155 | if ( pcCU->getSlice()->isIntra() ) |
---|
| 156 | { |
---|
| 157 | return; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | m_pcEntropyCoderIf->codePredMode( pcCU, uiAbsPartIdx ); |
---|
| 161 | } |
---|
| 162 | |
---|
[1260] | 163 | //! encode split flag |
---|
[313] | 164 | Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD ) |
---|
| 165 | { |
---|
| 166 | if( bRD ) |
---|
| 167 | { |
---|
| 168 | uiAbsPartIdx = 0; |
---|
| 169 | } |
---|
[1029] | 170 | |
---|
[313] | 171 | m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 172 | } |
---|
| 173 | |
---|
[1260] | 174 | //! encode partition size |
---|
[313] | 175 | Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD ) |
---|
| 176 | { |
---|
| 177 | if( bRD ) |
---|
| 178 | { |
---|
| 179 | uiAbsPartIdx = 0; |
---|
| 180 | } |
---|
[1029] | 181 | |
---|
[313] | 182 | m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 183 | } |
---|
| 184 | |
---|
[1029] | 185 | |
---|
| 186 | /** Encode I_PCM information. |
---|
[1260] | 187 | * \param pcCU pointer to CU |
---|
| 188 | * \param uiAbsPartIdx CU index |
---|
| 189 | * \param bRD flag indicating estimation or encoding |
---|
[313] | 190 | */ |
---|
| 191 | Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 192 | { |
---|
| 193 | if(!pcCU->getSlice()->getSPS()->getUsePCM() |
---|
| 194 | || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize()) |
---|
| 195 | || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize())) |
---|
| 196 | { |
---|
| 197 | return; |
---|
| 198 | } |
---|
[1029] | 199 | |
---|
[313] | 200 | if( bRD ) |
---|
| 201 | { |
---|
| 202 | uiAbsPartIdx = 0; |
---|
| 203 | } |
---|
[1029] | 204 | |
---|
[313] | 205 | m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx ); |
---|
[1029] | 206 | |
---|
[313] | 207 | } |
---|
| 208 | |
---|
[1029] | 209 | Void TEncEntropy::xEncodeTransform( Bool& bCodeDQP, Bool& codeChromaQpAdj, TComTU &rTu ) |
---|
[313] | 210 | { |
---|
[1029] | 211 | //pcCU, absPartIdxCU, uiAbsPartIdx, uiDepth+1, uiTrIdx+1, quadrant, |
---|
| 212 | TComDataCU *pcCU=rTu.getCU(); |
---|
| 213 | const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU(); |
---|
| 214 | const UInt numValidComponent = pcCU->getPic()->getNumberValidComponents(); |
---|
| 215 | const Bool bChroma = isChromaEnabled(pcCU->getPic()->getChromaFormat()); |
---|
| 216 | const UInt uiTrIdx = rTu.GetTransformDepthRel(); |
---|
| 217 | const UInt uiDepth = rTu.GetTransformDepthTotal(); |
---|
| 218 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
| 219 | const Bool bDebugRQT=g_bFinalEncode && DebugOptionList::DebugRQT.getInt()!=0; |
---|
| 220 | if (bDebugRQT) |
---|
[1246] | 221 | { |
---|
[1029] | 222 | printf("x..codeTransform: offsetLuma=%d offsetChroma=%d absPartIdx=%d, uiDepth=%d\n width=%d, height=%d, uiTrIdx=%d, uiInnerQuadIdx=%d\n", |
---|
| 223 | rTu.getCoefficientOffset(COMPONENT_Y), rTu.getCoefficientOffset(COMPONENT_Cb), uiAbsPartIdx, uiDepth, rTu.getRect(COMPONENT_Y).width, rTu.getRect(COMPONENT_Y).height, rTu.GetTransformDepthRel(), rTu.GetSectionNumber()); |
---|
[1246] | 224 | } |
---|
[1029] | 225 | #endif |
---|
| 226 | const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) > uiTrIdx;// + pcCU->getDepth( uiAbsPartIdx ) > uiDepth; |
---|
| 227 | const UInt uiLog2TrafoSize = rTu.GetLog2LumaTrSize(); |
---|
[313] | 228 | |
---|
[1029] | 229 | |
---|
| 230 | UInt cbf[MAX_NUM_COMPONENT] = {0,0,0}; |
---|
| 231 | Bool bHaveACodedBlock = false; |
---|
| 232 | Bool bHaveACodedChromaBlock = false; |
---|
| 233 | |
---|
| 234 | for(UInt ch=0; ch<numValidComponent; ch++) |
---|
[313] | 235 | { |
---|
[1029] | 236 | const ComponentID compID = ComponentID(ch); |
---|
| 237 | |
---|
| 238 | cbf[compID] = pcCU->getCbf( uiAbsPartIdx, compID , uiTrIdx ); |
---|
| 239 | |
---|
| 240 | if (cbf[ch] != 0) |
---|
[313] | 241 | { |
---|
[1029] | 242 | bHaveACodedBlock = true; |
---|
[1246] | 243 | if (isChroma(compID)) |
---|
| 244 | { |
---|
| 245 | bHaveACodedChromaBlock = true; |
---|
| 246 | } |
---|
[313] | 247 | } |
---|
| 248 | } |
---|
[1029] | 249 | |
---|
| 250 | if( pcCU->isIntra(uiAbsPartIdx) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) ) |
---|
[313] | 251 | { |
---|
| 252 | assert( uiSubdiv ); |
---|
| 253 | } |
---|
[1029] | 254 | else if( pcCU->isInter(uiAbsPartIdx) && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) && (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) ) |
---|
[313] | 255 | { |
---|
| 256 | if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) |
---|
| 257 | { |
---|
| 258 | assert( uiSubdiv ); |
---|
| 259 | } |
---|
| 260 | else |
---|
| 261 | { |
---|
| 262 | assert(!uiSubdiv ); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() ) |
---|
| 266 | { |
---|
| 267 | assert( uiSubdiv ); |
---|
| 268 | } |
---|
| 269 | else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) |
---|
| 270 | { |
---|
| 271 | assert( !uiSubdiv ); |
---|
| 272 | } |
---|
| 273 | else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) |
---|
| 274 | { |
---|
| 275 | assert( !uiSubdiv ); |
---|
| 276 | } |
---|
| 277 | else |
---|
| 278 | { |
---|
| 279 | assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ); |
---|
| 280 | m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, 5 - uiLog2TrafoSize ); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx ); |
---|
| 284 | const Bool bFirstCbfOfCU = uiTrDepthCurr == 0; |
---|
[1029] | 285 | |
---|
| 286 | for(UInt ch=COMPONENT_Cb; ch<numValidComponent; ch++) |
---|
[494] | 287 | { |
---|
[1029] | 288 | const ComponentID compID=ComponentID(ch); |
---|
| 289 | if( bFirstCbfOfCU || rTu.ProcessingAllQuadrants(compID) ) |
---|
[313] | 290 | { |
---|
[1029] | 291 | if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, compID, uiTrDepthCurr - 1 ) ) |
---|
| 292 | { |
---|
| 293 | m_pcEntropyCoderIf->codeQtCbf( rTu, compID, (uiSubdiv == 0) ); |
---|
| 294 | } |
---|
[313] | 295 | } |
---|
[1029] | 296 | else |
---|
[313] | 297 | { |
---|
[1029] | 298 | assert( pcCU->getCbf( uiAbsPartIdx, compID, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, compID, uiTrDepthCurr - 1 ) ); |
---|
[313] | 299 | } |
---|
| 300 | } |
---|
[1029] | 301 | |
---|
[313] | 302 | if( uiSubdiv ) |
---|
| 303 | { |
---|
[1029] | 304 | TComTURecurse tuRecurseChild(rTu, true); |
---|
| 305 | do |
---|
| 306 | { |
---|
| 307 | xEncodeTransform( bCodeDQP, codeChromaQpAdj, tuRecurseChild ); |
---|
[1246] | 308 | } while (tuRecurseChild.nextSection(rTu)); |
---|
[313] | 309 | } |
---|
| 310 | else |
---|
| 311 | { |
---|
| 312 | { |
---|
| 313 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 314 | DTRACE_CABAC_T( "\tTrIdx: abspart=" ); |
---|
| 315 | DTRACE_CABAC_V( uiAbsPartIdx ); |
---|
| 316 | DTRACE_CABAC_T( "\tdepth=" ); |
---|
| 317 | DTRACE_CABAC_V( uiDepth ); |
---|
| 318 | DTRACE_CABAC_T( "\ttrdepth=" ); |
---|
| 319 | DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) ); |
---|
| 320 | DTRACE_CABAC_T( "\n" ); |
---|
| 321 | } |
---|
[1029] | 322 | |
---|
| 323 | if( !pcCU->isIntra(uiAbsPartIdx) && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && (!bChroma || (!pcCU->getCbf( uiAbsPartIdx, COMPONENT_Cb, 0 ) && !pcCU->getCbf( uiAbsPartIdx, COMPONENT_Cr, 0 ) ) ) ) |
---|
[313] | 324 | { |
---|
[1029] | 325 | assert( pcCU->getCbf( uiAbsPartIdx, COMPONENT_Y, 0 ) ); |
---|
[313] | 326 | // printf( "saved one bin! " ); |
---|
| 327 | } |
---|
| 328 | else |
---|
| 329 | { |
---|
[1029] | 330 | m_pcEntropyCoderIf->codeQtCbf( rTu, COMPONENT_Y, true ); //luma CBF is always at the lowest level |
---|
[313] | 331 | } |
---|
| 332 | |
---|
[1029] | 333 | if ( bHaveACodedBlock ) |
---|
[313] | 334 | { |
---|
[1029] | 335 | // dQP: only for CTU once |
---|
[313] | 336 | if ( pcCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 337 | { |
---|
| 338 | if ( bCodeDQP ) |
---|
| 339 | { |
---|
[1029] | 340 | encodeQP( pcCU, rTu.GetAbsPartIdxCU() ); |
---|
[313] | 341 | bCodeDQP = false; |
---|
| 342 | } |
---|
| 343 | } |
---|
[1029] | 344 | |
---|
| 345 | if ( pcCU->getSlice()->getUseChromaQpAdj() ) |
---|
[313] | 346 | { |
---|
[1029] | 347 | if ( bHaveACodedChromaBlock && codeChromaQpAdj && !pcCU->getCUTransquantBypass(rTu.GetAbsPartIdxCU()) ) |
---|
| 348 | { |
---|
| 349 | encodeChromaQpAdjustment( pcCU, rTu.GetAbsPartIdxCU() ); |
---|
| 350 | codeChromaQpAdj = false; |
---|
| 351 | } |
---|
[313] | 352 | } |
---|
[1029] | 353 | |
---|
| 354 | const UInt numValidComp=pcCU->getPic()->getNumberValidComponents(); |
---|
| 355 | |
---|
| 356 | for(UInt ch=COMPONENT_Y; ch<numValidComp; ch++) |
---|
[313] | 357 | { |
---|
[1029] | 358 | const ComponentID compID=ComponentID(ch); |
---|
| 359 | |
---|
| 360 | if (rTu.ProcessComponentSection(compID)) |
---|
[313] | 361 | { |
---|
[1029] | 362 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
[1246] | 363 | if (bDebugRQT) |
---|
| 364 | { |
---|
| 365 | printf("Call NxN for chan %d width=%d height=%d cbf=%d\n", compID, rTu.getRect(compID).width, rTu.getRect(compID).height, 1); |
---|
| 366 | } |
---|
[1029] | 367 | #endif |
---|
| 368 | |
---|
| 369 | if (rTu.getRect(compID).width != rTu.getRect(compID).height) |
---|
| 370 | { |
---|
| 371 | //code two sub-TUs |
---|
| 372 | TComTURecurse subTUIterator(rTu, false, TComTU::VERTICAL_SPLIT, true, compID); |
---|
| 373 | |
---|
| 374 | do |
---|
| 375 | { |
---|
| 376 | const UChar subTUCBF = pcCU->getCbf(subTUIterator.GetAbsPartIdxTU(compID), compID, (uiTrIdx + 1)); |
---|
| 377 | |
---|
| 378 | if (subTUCBF != 0) |
---|
| 379 | { |
---|
| 380 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
[1246] | 381 | if (bDebugRQT) |
---|
| 382 | { |
---|
| 383 | printf("Call NxN for chan %d width=%d height=%d cbf=%d\n", compID, subTUIterator.getRect(compID).width, subTUIterator.getRect(compID).height, 1); |
---|
| 384 | } |
---|
[1029] | 385 | #endif |
---|
| 386 | m_pcEntropyCoderIf->codeCoeffNxN( subTUIterator, (pcCU->getCoeff(compID) + subTUIterator.getCoefficientOffset(compID)), compID ); |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | while (subTUIterator.nextSection(rTu)); |
---|
| 390 | } |
---|
| 391 | else |
---|
| 392 | { |
---|
| 393 | if (isChroma(compID) && (cbf[COMPONENT_Y] != 0)) |
---|
| 394 | { |
---|
| 395 | m_pcEntropyCoderIf->codeCrossComponentPrediction( rTu, compID ); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | if (cbf[compID] != 0) |
---|
| 399 | { |
---|
| 400 | m_pcEntropyCoderIf->codeCoeffNxN( rTu, (pcCU->getCoeff(compID) + rTu.getCoefficientOffset(compID)), compID ); |
---|
| 401 | } |
---|
| 402 | } |
---|
[313] | 403 | } |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
[1029] | 409 | |
---|
[1260] | 410 | //! encode intra direction for luma |
---|
[313] | 411 | Void TEncEntropy::encodeIntraDirModeLuma ( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiplePU ) |
---|
| 412 | { |
---|
| 413 | m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, absPartIdx , isMultiplePU); |
---|
| 414 | } |
---|
| 415 | |
---|
[1029] | 416 | |
---|
[1260] | 417 | //! encode intra direction for chroma |
---|
[1029] | 418 | Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
[313] | 419 | { |
---|
[1029] | 420 | m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx ); |
---|
| 421 | |
---|
| 422 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
| 423 | if (bDebugPredEnabled && g_bFinalEncode) |
---|
[494] | 424 | { |
---|
[1029] | 425 | UInt cdir=pcCU->getIntraDir(CHANNEL_TYPE_CHROMA, uiAbsPartIdx); |
---|
[1246] | 426 | if (cdir==36) |
---|
| 427 | { |
---|
| 428 | cdir=pcCU->getIntraDir(CHANNEL_TYPE_LUMA, uiAbsPartIdx); |
---|
| 429 | } |
---|
[1029] | 430 | printf("coding chroma Intra dir: %d, uiAbsPartIdx: %d, luma dir: %d\n", cdir, uiAbsPartIdx, pcCU->getIntraDir(CHANNEL_TYPE_LUMA, uiAbsPartIdx)); |
---|
[494] | 431 | } |
---|
| 432 | #endif |
---|
[313] | 433 | } |
---|
| 434 | |
---|
[1029] | 435 | |
---|
| 436 | Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
[313] | 437 | { |
---|
| 438 | if( pcCU->isIntra( uiAbsPartIdx ) ) // If it is Intra mode, encode intra prediction mode. |
---|
| 439 | { |
---|
| 440 | encodeIntraDirModeLuma ( pcCU, uiAbsPartIdx,true ); |
---|
[1029] | 441 | if (pcCU->getPic()->getChromaFormat()!=CHROMA_400) |
---|
| 442 | { |
---|
| 443 | encodeIntraDirModeChroma( pcCU, uiAbsPartIdx ); |
---|
| 444 | |
---|
| 445 | if (enable4ChromaPUsInIntraNxNCU(pcCU->getPic()->getChromaFormat()) && pcCU->getPartitionSize( uiAbsPartIdx )==SIZE_NxN) |
---|
| 446 | { |
---|
| 447 | UInt uiPartOffset = ( pcCU->getPic()->getNumPartitionsInCtu() >> ( pcCU->getDepth(uiAbsPartIdx) << 1 ) ) >> 2; |
---|
| 448 | encodeIntraDirModeChroma( pcCU, uiAbsPartIdx + uiPartOffset ); |
---|
| 449 | encodeIntraDirModeChroma( pcCU, uiAbsPartIdx + uiPartOffset*2 ); |
---|
| 450 | encodeIntraDirModeChroma( pcCU, uiAbsPartIdx + uiPartOffset*3 ); |
---|
| 451 | } |
---|
| 452 | } |
---|
[313] | 453 | } |
---|
| 454 | else // if it is Inter mode, encode motion vector and reference index |
---|
| 455 | { |
---|
[1029] | 456 | encodePUWise( pcCU, uiAbsPartIdx ); |
---|
[313] | 457 | } |
---|
| 458 | } |
---|
| 459 | |
---|
[1029] | 460 | Void TEncEntropy::encodeCrossComponentPrediction( TComTU &rTu, ComponentID compID ) |
---|
| 461 | { |
---|
| 462 | m_pcEntropyCoderIf->codeCrossComponentPrediction( rTu, compID ); |
---|
| 463 | } |
---|
| 464 | |
---|
[1260] | 465 | //! encode motion information for every PU block |
---|
[1029] | 466 | Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
[313] | 467 | { |
---|
[1029] | 468 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
| 469 | const Bool bDebugPred = bDebugPredEnabled && g_bFinalEncode; |
---|
| 470 | #endif |
---|
| 471 | |
---|
[313] | 472 | PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx ); |
---|
| 473 | UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) ); |
---|
| 474 | UInt uiDepth = pcCU->getDepth( uiAbsPartIdx ); |
---|
| 475 | UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() - uiDepth ) << 1 ) ) >> 4; |
---|
| 476 | |
---|
| 477 | for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset ) |
---|
| 478 | { |
---|
| 479 | encodeMergeFlag( pcCU, uiSubPartIdx ); |
---|
| 480 | if ( pcCU->getMergeFlag( uiSubPartIdx ) ) |
---|
| 481 | { |
---|
| 482 | encodeMergeIndex( pcCU, uiSubPartIdx ); |
---|
[1029] | 483 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
| 484 | if (bDebugPred) |
---|
| 485 | { |
---|
| 486 | std::cout << "Coded merge flag, CU absPartIdx: " << uiAbsPartIdx << " PU(" << uiPartIdx << ") absPartIdx: " << uiSubPartIdx; |
---|
| 487 | std::cout << " merge index: " << (UInt)pcCU->getMergeIndex(uiSubPartIdx) << std::endl; |
---|
| 488 | } |
---|
| 489 | #endif |
---|
[313] | 490 | } |
---|
| 491 | else |
---|
| 492 | { |
---|
| 493 | encodeInterDirPU( pcCU, uiSubPartIdx ); |
---|
| 494 | for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ ) |
---|
| 495 | { |
---|
| 496 | if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 ) |
---|
| 497 | { |
---|
| 498 | encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) ); |
---|
| 499 | encodeMvdPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) ); |
---|
| 500 | encodeMVPIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) ); |
---|
[1029] | 501 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
| 502 | if (bDebugPred) |
---|
| 503 | { |
---|
| 504 | std::cout << "refListIdx: " << uiRefListIdx << std::endl; |
---|
| 505 | std::cout << "MVD horizontal: " << pcCU->getCUMvField(RefPicList(uiRefListIdx))->getMvd( uiAbsPartIdx ).getHor() << std::endl; |
---|
| 506 | std::cout << "MVD vertical: " << pcCU->getCUMvField(RefPicList(uiRefListIdx))->getMvd( uiAbsPartIdx ).getVer() << std::endl; |
---|
| 507 | std::cout << "MVPIdxPU: " << pcCU->getMVPIdx(RefPicList( uiRefListIdx ), uiSubPartIdx) << std::endl; |
---|
| 508 | std::cout << "InterDir: " << (UInt)pcCU->getInterDir(uiSubPartIdx) << std::endl; |
---|
| 509 | } |
---|
| 510 | #endif |
---|
[313] | 511 | } |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | return; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 520 | { |
---|
| 521 | if ( !pcCU->getSlice()->isInterB() ) |
---|
| 522 | { |
---|
| 523 | return; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx ); |
---|
[1029] | 527 | |
---|
[313] | 528 | return; |
---|
| 529 | } |
---|
| 530 | |
---|
[1260] | 531 | //! encode reference frame index for a PU block |
---|
[313] | 532 | Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 533 | { |
---|
[1029] | 534 | assert( pcCU->isInter( uiAbsPartIdx ) ); |
---|
| 535 | |
---|
| 536 | if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) ) |
---|
[313] | 537 | { |
---|
[1029] | 538 | return; |
---|
| 539 | } |
---|
[313] | 540 | |
---|
[1029] | 541 | if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) ) |
---|
| 542 | { |
---|
| 543 | m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList ); |
---|
[313] | 544 | } |
---|
| 545 | |
---|
| 546 | return; |
---|
| 547 | } |
---|
| 548 | |
---|
[1260] | 549 | //! encode motion vector difference for a PU block |
---|
[313] | 550 | Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 551 | { |
---|
[1029] | 552 | assert( pcCU->isInter( uiAbsPartIdx ) ); |
---|
[313] | 553 | |
---|
| 554 | if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) ) |
---|
| 555 | { |
---|
| 556 | m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList ); |
---|
| 557 | } |
---|
| 558 | return; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 562 | { |
---|
| 563 | if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) ) |
---|
| 564 | { |
---|
| 565 | m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList ); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | return; |
---|
| 569 | } |
---|
| 570 | |
---|
[1029] | 571 | Void TEncEntropy::encodeQtCbf( TComTU &rTu, const ComponentID compID, const Bool lowestLevel ) |
---|
[313] | 572 | { |
---|
[1029] | 573 | m_pcEntropyCoderIf->codeQtCbf( rTu, compID, lowestLevel ); |
---|
[313] | 574 | } |
---|
| 575 | |
---|
| 576 | Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx ) |
---|
| 577 | { |
---|
| 578 | m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx ); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 582 | { |
---|
| 583 | m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx ); |
---|
| 584 | } |
---|
| 585 | |
---|
[1029] | 586 | Void TEncEntropy::encodeQtCbfZero( TComTU &rTu, const ChannelType chType ) |
---|
[313] | 587 | { |
---|
[1029] | 588 | m_pcEntropyCoderIf->codeQtCbfZero( rTu, chType ); |
---|
[313] | 589 | } |
---|
[1029] | 590 | |
---|
[313] | 591 | Void TEncEntropy::encodeQtRootCbfZero( TComDataCU* pcCU ) |
---|
| 592 | { |
---|
| 593 | m_pcEntropyCoderIf->codeQtRootCbfZero( pcCU ); |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | // dQP |
---|
| 597 | Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 598 | { |
---|
| 599 | if( bRD ) |
---|
| 600 | { |
---|
| 601 | uiAbsPartIdx = 0; |
---|
| 602 | } |
---|
[1029] | 603 | |
---|
[313] | 604 | if ( pcCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 605 | { |
---|
| 606 | m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx ); |
---|
| 607 | } |
---|
| 608 | } |
---|
| 609 | |
---|
[1260] | 610 | //! encode chroma qp adjustment |
---|
[1029] | 611 | Void TEncEntropy::encodeChromaQpAdjustment( TComDataCU* cu, UInt absPartIdx, Bool inRd ) |
---|
| 612 | { |
---|
| 613 | if( inRd ) |
---|
| 614 | { |
---|
| 615 | absPartIdx = 0; |
---|
| 616 | } |
---|
[313] | 617 | |
---|
[1029] | 618 | m_pcEntropyCoderIf->codeChromaQpAdjustment( cu, absPartIdx ); |
---|
| 619 | } |
---|
| 620 | |
---|
[313] | 621 | // texture |
---|
[1029] | 622 | |
---|
[1260] | 623 | //! encode coefficients |
---|
[1029] | 624 | Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool& bCodeDQP, Bool& codeChromaQpAdj ) |
---|
[313] | 625 | { |
---|
[1029] | 626 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
| 627 | const Bool bDebugRQT=g_bFinalEncode && DebugOptionList::DebugRQT.getInt()!=0; |
---|
| 628 | #endif |
---|
| 629 | |
---|
[313] | 630 | if( pcCU->isIntra(uiAbsPartIdx) ) |
---|
| 631 | { |
---|
[1029] | 632 | if (false) |
---|
| 633 | { |
---|
| 634 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 635 | DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" ) |
---|
| 636 | DTRACE_CABAC_V( uiDepth ) |
---|
| 637 | DTRACE_CABAC_T( "\n" ) |
---|
| 638 | } |
---|
[313] | 639 | } |
---|
| 640 | else |
---|
| 641 | { |
---|
| 642 | if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) ) |
---|
| 643 | { |
---|
| 644 | m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx ); |
---|
| 645 | } |
---|
| 646 | if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) ) |
---|
| 647 | { |
---|
| 648 | return; |
---|
| 649 | } |
---|
| 650 | } |
---|
[1029] | 651 | |
---|
| 652 | TComTURecurse tuRecurse(pcCU, uiAbsPartIdx, uiDepth); |
---|
| 653 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
[1246] | 654 | if (bDebugRQT) |
---|
| 655 | { |
---|
| 656 | printf("..codeCoeff: uiAbsPartIdx=%d, PU format=%d, 2Nx2N=%d, NxN=%d\n", uiAbsPartIdx, pcCU->getPartitionSize(uiAbsPartIdx), SIZE_2Nx2N, SIZE_NxN); |
---|
| 657 | } |
---|
[1029] | 658 | #endif |
---|
| 659 | |
---|
| 660 | xEncodeTransform( bCodeDQP, codeChromaQpAdj, tuRecurse ); |
---|
[313] | 661 | } |
---|
| 662 | |
---|
[1029] | 663 | Void TEncEntropy::encodeCoeffNxN( TComTU &rTu, TCoeff* pcCoef, const ComponentID compID) |
---|
[313] | 664 | { |
---|
[1029] | 665 | TComDataCU *pcCU = rTu.getCU(); |
---|
| 666 | |
---|
| 667 | if (pcCU->getCbf(rTu.GetAbsPartIdxTU(), compID, rTu.GetTransformDepthRel()) != 0) |
---|
| 668 | { |
---|
| 669 | if (rTu.getRect(compID).width != rTu.getRect(compID).height) |
---|
| 670 | { |
---|
| 671 | //code two sub-TUs |
---|
| 672 | TComTURecurse subTUIterator(rTu, false, TComTU::VERTICAL_SPLIT, true, compID); |
---|
| 673 | |
---|
| 674 | const UInt subTUSize = subTUIterator.getRect(compID).width * subTUIterator.getRect(compID).height; |
---|
| 675 | |
---|
| 676 | do |
---|
| 677 | { |
---|
| 678 | const UChar subTUCBF = pcCU->getCbf(subTUIterator.GetAbsPartIdxTU(compID), compID, (subTUIterator.GetTransformDepthRel() + 1)); |
---|
| 679 | |
---|
| 680 | if (subTUCBF != 0) |
---|
| 681 | { |
---|
| 682 | m_pcEntropyCoderIf->codeCoeffNxN( subTUIterator, (pcCoef + (subTUIterator.GetSectionNumber() * subTUSize)), compID); |
---|
| 683 | } |
---|
| 684 | } |
---|
| 685 | while (subTUIterator.nextSection(rTu)); |
---|
| 686 | } |
---|
| 687 | else |
---|
| 688 | { |
---|
| 689 | m_pcEntropyCoderIf->codeCoeffNxN(rTu, pcCoef, compID); |
---|
| 690 | } |
---|
| 691 | } |
---|
[313] | 692 | } |
---|
| 693 | |
---|
[1029] | 694 | Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, const ChannelType chType) |
---|
| 695 | { |
---|
| 696 | const UInt heightAtEntropyCoding = (width != height) ? (height >> 1) : height; |
---|
| 697 | |
---|
| 698 | m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, heightAtEntropyCoding, chType ); |
---|
[313] | 699 | } |
---|
| 700 | |
---|
| 701 | Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize ) |
---|
| 702 | { |
---|
| 703 | Int count = 0; |
---|
[1029] | 704 | |
---|
[313] | 705 | for ( Int i = 0; i < uiSize; i++ ) |
---|
| 706 | { |
---|
| 707 | count += pcCoef[i] != 0; |
---|
| 708 | } |
---|
[1029] | 709 | |
---|
[313] | 710 | return count; |
---|
| 711 | } |
---|
| 712 | |
---|
[1207] | 713 | #if SVC_EXTENSION |
---|
| 714 | Void TEncEntropy::encodeSliceHeaderExtn( TComSlice* pSlice, Int shBitsWrittenTillNow ) |
---|
[1029] | 715 | { |
---|
| 716 | m_pcEntropyCoderIf->codeSliceHeaderExtn( pSlice, shBitsWrittenTillNow ); |
---|
| 717 | } |
---|
| 718 | #endif |
---|
| 719 | |
---|
[313] | 720 | //! \} |
---|