[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 |
---|
| 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 TEncEntropy.cpp |
---|
| 35 | \brief entropy encoder class |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TEncEntropy.h" |
---|
| 39 | #include "TLibCommon/TypeDef.h" |
---|
| 40 | #include "TLibCommon/TComSampleAdaptiveOffset.h" |
---|
| 41 | |
---|
| 42 | //! \ingroup TLibEncoder |
---|
| 43 | //! \{ |
---|
| 44 | |
---|
| 45 | Void TEncEntropy::setEntropyCoder ( TEncEntropyIf* e, TComSlice* pcSlice ) |
---|
| 46 | { |
---|
| 47 | m_pcEntropyCoderIf = e; |
---|
| 48 | m_pcEntropyCoderIf->setSlice ( pcSlice ); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | Void TEncEntropy::encodeSliceHeader ( TComSlice* pcSlice ) |
---|
| 52 | { |
---|
[532] | 53 | #if !HM_CLEANUP_SAO |
---|
[313] | 54 | if (pcSlice->getSPS()->getUseSAO()) |
---|
| 55 | { |
---|
| 56 | SAOParam *saoParam = pcSlice->getPic()->getPicSym()->getSaoParam(); |
---|
| 57 | pcSlice->setSaoEnabledFlag (saoParam->bSaoFlag[0]); |
---|
| 58 | { |
---|
| 59 | pcSlice->setSaoEnabledFlagChroma (saoParam->bSaoFlag[1]); |
---|
| 60 | } |
---|
| 61 | } |
---|
[532] | 62 | #endif |
---|
[313] | 63 | m_pcEntropyCoderIf->codeSliceHeader( pcSlice ); |
---|
| 64 | return; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | Void TEncEntropy::encodeTilesWPPEntryPoint( TComSlice* pSlice ) |
---|
| 68 | { |
---|
| 69 | m_pcEntropyCoderIf->codeTilesWPPEntryPoint( pSlice ); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | Void TEncEntropy::encodeTerminatingBit ( UInt uiIsLast ) |
---|
| 73 | { |
---|
| 74 | m_pcEntropyCoderIf->codeTerminatingBit( uiIsLast ); |
---|
| 75 | |
---|
| 76 | return; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | Void TEncEntropy::encodeSliceFinish() |
---|
| 80 | { |
---|
| 81 | m_pcEntropyCoderIf->codeSliceFinish(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | Void TEncEntropy::encodePPS( TComPPS* pcPPS ) |
---|
| 85 | { |
---|
| 86 | m_pcEntropyCoderIf->codePPS( pcPPS ); |
---|
| 87 | return; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | Void TEncEntropy::encodeSPS( TComSPS* pcSPS ) |
---|
| 91 | { |
---|
| 92 | m_pcEntropyCoderIf->codeSPS( pcSPS ); |
---|
| 93 | return; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | Void TEncEntropy::encodeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 97 | { |
---|
| 98 | if( bRD ) |
---|
| 99 | { |
---|
| 100 | uiAbsPartIdx = 0; |
---|
| 101 | } |
---|
| 102 | m_pcEntropyCoderIf->codeCUTransquantBypassFlag( pcCU, uiAbsPartIdx ); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | Void TEncEntropy::encodeVPS( TComVPS* pcVPS ) |
---|
| 106 | { |
---|
| 107 | m_pcEntropyCoderIf->codeVPS( pcVPS ); |
---|
| 108 | return; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | Void TEncEntropy::encodeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 112 | { |
---|
| 113 | if ( pcCU->getSlice()->isIntra() ) |
---|
| 114 | { |
---|
| 115 | return; |
---|
| 116 | } |
---|
| 117 | if( bRD ) |
---|
| 118 | { |
---|
| 119 | uiAbsPartIdx = 0; |
---|
| 120 | } |
---|
| 121 | m_pcEntropyCoderIf->codeSkipFlag( pcCU, uiAbsPartIdx ); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** encode merge flag |
---|
| 125 | * \param pcCU |
---|
| 126 | * \param uiAbsPartIdx |
---|
| 127 | * \returns Void |
---|
| 128 | */ |
---|
| 129 | Void TEncEntropy::encodeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 130 | { |
---|
| 131 | // at least one merge candidate exists |
---|
| 132 | m_pcEntropyCoderIf->codeMergeFlag( pcCU, uiAbsPartIdx ); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** encode merge index |
---|
| 136 | * \param pcCU |
---|
| 137 | * \param uiAbsPartIdx |
---|
| 138 | * \param uiPUIdx |
---|
| 139 | * \param bRD |
---|
| 140 | * \returns Void |
---|
| 141 | */ |
---|
| 142 | Void TEncEntropy::encodeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 143 | { |
---|
| 144 | if( bRD ) |
---|
| 145 | { |
---|
| 146 | uiAbsPartIdx = 0; |
---|
| 147 | assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ); |
---|
| 148 | } |
---|
| 149 | m_pcEntropyCoderIf->codeMergeIndex( pcCU, uiAbsPartIdx ); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** encode prediction mode |
---|
| 153 | * \param pcCU |
---|
| 154 | * \param uiAbsPartIdx |
---|
| 155 | * \param bRD |
---|
| 156 | * \returns Void |
---|
| 157 | */ |
---|
| 158 | Void TEncEntropy::encodePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 159 | { |
---|
| 160 | if( bRD ) |
---|
| 161 | { |
---|
| 162 | uiAbsPartIdx = 0; |
---|
| 163 | } |
---|
| 164 | if ( pcCU->getSlice()->isIntra() ) |
---|
| 165 | { |
---|
| 166 | return; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | m_pcEntropyCoderIf->codePredMode( pcCU, uiAbsPartIdx ); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | // Split mode |
---|
| 173 | Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD ) |
---|
| 174 | { |
---|
| 175 | if( bRD ) |
---|
| 176 | { |
---|
| 177 | uiAbsPartIdx = 0; |
---|
| 178 | } |
---|
| 179 | m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** encode partition size |
---|
| 183 | * \param pcCU |
---|
| 184 | * \param uiAbsPartIdx |
---|
| 185 | * \param uiDepth |
---|
| 186 | * \param bRD |
---|
| 187 | * \returns Void |
---|
| 188 | */ |
---|
| 189 | Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD ) |
---|
| 190 | { |
---|
| 191 | if( bRD ) |
---|
| 192 | { |
---|
| 193 | uiAbsPartIdx = 0; |
---|
| 194 | } |
---|
| 195 | m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth ); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** Encode I_PCM information. |
---|
| 199 | * \param pcCU pointer to CU |
---|
| 200 | * \param uiAbsPartIdx CU index |
---|
| 201 | * \param bRD flag indicating estimation or encoding |
---|
| 202 | * \returns Void |
---|
| 203 | */ |
---|
| 204 | Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 205 | { |
---|
| 206 | if(!pcCU->getSlice()->getSPS()->getUsePCM() |
---|
| 207 | || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize()) |
---|
| 208 | || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize())) |
---|
| 209 | { |
---|
| 210 | return; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | if( bRD ) |
---|
| 214 | { |
---|
| 215 | uiAbsPartIdx = 0; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx ); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | Void TEncEntropy::xEncodeTransform( TComDataCU* pcCU,UInt offsetLuma, UInt offsetChroma, UInt uiAbsPartIdx, UInt uiDepth, UInt width, UInt height, UInt uiTrIdx, Bool& bCodeDQP ) |
---|
| 222 | { |
---|
| 223 | const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) + pcCU->getDepth( uiAbsPartIdx ) > uiDepth; |
---|
| 224 | const UInt uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()]+2 - uiDepth; |
---|
| 225 | UInt cbfY = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA , uiTrIdx ); |
---|
| 226 | UInt cbfU = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrIdx ); |
---|
| 227 | UInt cbfV = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrIdx ); |
---|
| 228 | |
---|
| 229 | if(uiTrIdx==0) |
---|
| 230 | { |
---|
| 231 | m_bakAbsPartIdxCU = uiAbsPartIdx; |
---|
| 232 | } |
---|
| 233 | if( uiLog2TrafoSize == 2 ) |
---|
| 234 | { |
---|
| 235 | UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 ); |
---|
| 236 | if( ( uiAbsPartIdx % partNum ) == 0 ) |
---|
| 237 | { |
---|
| 238 | m_uiBakAbsPartIdx = uiAbsPartIdx; |
---|
| 239 | m_uiBakChromaOffset = offsetChroma; |
---|
| 240 | } |
---|
| 241 | else if( ( uiAbsPartIdx % partNum ) == (partNum - 1) ) |
---|
| 242 | { |
---|
| 243 | cbfU = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_U, uiTrIdx ); |
---|
| 244 | cbfV = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_V, uiTrIdx ); |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) ) |
---|
| 249 | { |
---|
| 250 | assert( uiSubdiv ); |
---|
| 251 | } |
---|
| 252 | else if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) && (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) ) |
---|
| 253 | { |
---|
| 254 | if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) |
---|
| 255 | { |
---|
| 256 | assert( uiSubdiv ); |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | assert(!uiSubdiv ); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() ) |
---|
| 264 | { |
---|
| 265 | assert( uiSubdiv ); |
---|
| 266 | } |
---|
| 267 | else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) |
---|
| 268 | { |
---|
| 269 | assert( !uiSubdiv ); |
---|
| 270 | } |
---|
| 271 | else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) |
---|
| 272 | { |
---|
| 273 | assert( !uiSubdiv ); |
---|
| 274 | } |
---|
| 275 | else |
---|
| 276 | { |
---|
| 277 | assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ); |
---|
| 278 | m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, 5 - uiLog2TrafoSize ); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx ); |
---|
| 282 | const Bool bFirstCbfOfCU = uiTrDepthCurr == 0; |
---|
[494] | 283 | #if AUXILIARY_PICTURES |
---|
| 284 | if (pcCU->getSlice()->getChromaFormatIdc() != CHROMA_400) |
---|
| 285 | { |
---|
| 286 | #endif |
---|
[313] | 287 | if( bFirstCbfOfCU || uiLog2TrafoSize > 2 ) |
---|
| 288 | { |
---|
| 289 | if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) ) |
---|
| 290 | { |
---|
| 291 | m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ); |
---|
| 292 | } |
---|
| 293 | if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) ) |
---|
| 294 | { |
---|
| 295 | m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | else if( uiLog2TrafoSize == 2 ) |
---|
| 299 | { |
---|
| 300 | assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) ); |
---|
| 301 | assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) ); |
---|
| 302 | } |
---|
[494] | 303 | #if AUXILIARY_PICTURES |
---|
| 304 | } |
---|
| 305 | else |
---|
| 306 | { |
---|
| 307 | assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ) == 0 ); |
---|
| 308 | assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ) == 0 ); |
---|
| 309 | } |
---|
| 310 | #endif |
---|
[313] | 311 | |
---|
| 312 | if( uiSubdiv ) |
---|
| 313 | { |
---|
| 314 | UInt size; |
---|
| 315 | width >>= 1; |
---|
| 316 | height >>= 1; |
---|
| 317 | size = width*height; |
---|
| 318 | uiTrIdx++; |
---|
| 319 | ++uiDepth; |
---|
| 320 | const UInt partNum = pcCU->getPic()->getNumPartInCU() >> (uiDepth << 1); |
---|
| 321 | |
---|
| 322 | xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP ); |
---|
| 323 | |
---|
| 324 | uiAbsPartIdx += partNum; offsetLuma += size; offsetChroma += (size>>2); |
---|
| 325 | xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP ); |
---|
| 326 | |
---|
| 327 | uiAbsPartIdx += partNum; offsetLuma += size; offsetChroma += (size>>2); |
---|
| 328 | xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP ); |
---|
| 329 | |
---|
| 330 | uiAbsPartIdx += partNum; offsetLuma += size; offsetChroma += (size>>2); |
---|
| 331 | xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP ); |
---|
| 332 | } |
---|
| 333 | else |
---|
| 334 | { |
---|
| 335 | { |
---|
| 336 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 337 | DTRACE_CABAC_T( "\tTrIdx: abspart=" ); |
---|
| 338 | DTRACE_CABAC_V( uiAbsPartIdx ); |
---|
| 339 | DTRACE_CABAC_T( "\tdepth=" ); |
---|
| 340 | DTRACE_CABAC_V( uiDepth ); |
---|
| 341 | DTRACE_CABAC_T( "\ttrdepth=" ); |
---|
| 342 | DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) ); |
---|
| 343 | DTRACE_CABAC_T( "\n" ); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | if( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, 0 ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, 0 ) ) |
---|
| 347 | { |
---|
| 348 | assert( pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, 0 ) ); |
---|
| 349 | // printf( "saved one bin! " ); |
---|
| 350 | } |
---|
| 351 | else |
---|
| 352 | { |
---|
| 353 | m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, pcCU->getTransformIdx( uiAbsPartIdx ) ); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | |
---|
| 357 | if ( cbfY || cbfU || cbfV ) |
---|
| 358 | { |
---|
| 359 | // dQP: only for LCU once |
---|
| 360 | if ( pcCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 361 | { |
---|
| 362 | if ( bCodeDQP ) |
---|
| 363 | { |
---|
| 364 | encodeQP( pcCU, m_bakAbsPartIdxCU ); |
---|
| 365 | bCodeDQP = false; |
---|
| 366 | } |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | if( cbfY ) |
---|
| 370 | { |
---|
| 371 | Int trWidth = width; |
---|
| 372 | Int trHeight = height; |
---|
| 373 | m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffY()+offsetLuma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA ); |
---|
| 374 | } |
---|
| 375 | if( uiLog2TrafoSize > 2 ) |
---|
| 376 | { |
---|
| 377 | Int trWidth = width >> 1; |
---|
| 378 | Int trHeight = height >> 1; |
---|
| 379 | if( cbfU ) |
---|
| 380 | { |
---|
| 381 | m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U ); |
---|
| 382 | } |
---|
| 383 | if( cbfV ) |
---|
| 384 | { |
---|
| 385 | m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V ); |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | else |
---|
| 389 | { |
---|
| 390 | UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 ); |
---|
| 391 | if( ( uiAbsPartIdx % partNum ) == (partNum - 1) ) |
---|
| 392 | { |
---|
| 393 | Int trWidth = width; |
---|
| 394 | Int trHeight = height; |
---|
| 395 | if( cbfU ) |
---|
| 396 | { |
---|
| 397 | m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U ); |
---|
| 398 | } |
---|
| 399 | if( cbfV ) |
---|
| 400 | { |
---|
| 401 | m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V ); |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | // Intra direction for Luma |
---|
| 409 | Void TEncEntropy::encodeIntraDirModeLuma ( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiplePU ) |
---|
| 410 | { |
---|
| 411 | m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, absPartIdx , isMultiplePU); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | // Intra direction for Chroma |
---|
| 415 | Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 416 | { |
---|
[494] | 417 | #if AUXILIARY_PICTURES |
---|
| 418 | if ( pcCU->getSlice()->getChromaFormatIdc() == CHROMA_400 ) |
---|
| 419 | { |
---|
| 420 | return; |
---|
| 421 | } |
---|
| 422 | #endif |
---|
[313] | 423 | if( bRD ) |
---|
| 424 | { |
---|
| 425 | uiAbsPartIdx = 0; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx ); |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 432 | { |
---|
| 433 | if( bRD ) |
---|
| 434 | { |
---|
| 435 | uiAbsPartIdx = 0; |
---|
| 436 | } |
---|
| 437 | if( pcCU->isIntra( uiAbsPartIdx ) ) // If it is Intra mode, encode intra prediction mode. |
---|
| 438 | { |
---|
| 439 | encodeIntraDirModeLuma ( pcCU, uiAbsPartIdx,true ); |
---|
| 440 | encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD ); |
---|
| 441 | } |
---|
| 442 | else // if it is Inter mode, encode motion vector and reference index |
---|
| 443 | { |
---|
| 444 | encodePUWise( pcCU, uiAbsPartIdx, bRD ); |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | /** encode motion information for every PU block |
---|
| 449 | * \param pcCU |
---|
| 450 | * \param uiAbsPartIdx |
---|
| 451 | * \param bRD |
---|
| 452 | * \returns Void |
---|
| 453 | */ |
---|
| 454 | Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 455 | { |
---|
| 456 | if ( bRD ) |
---|
| 457 | { |
---|
| 458 | uiAbsPartIdx = 0; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx ); |
---|
| 462 | UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) ); |
---|
| 463 | UInt uiDepth = pcCU->getDepth( uiAbsPartIdx ); |
---|
| 464 | UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() - uiDepth ) << 1 ) ) >> 4; |
---|
| 465 | |
---|
| 466 | for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset ) |
---|
| 467 | { |
---|
| 468 | encodeMergeFlag( pcCU, uiSubPartIdx ); |
---|
| 469 | if ( pcCU->getMergeFlag( uiSubPartIdx ) ) |
---|
| 470 | { |
---|
| 471 | encodeMergeIndex( pcCU, uiSubPartIdx ); |
---|
| 472 | } |
---|
| 473 | else |
---|
| 474 | { |
---|
| 475 | encodeInterDirPU( pcCU, uiSubPartIdx ); |
---|
| 476 | for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ ) |
---|
| 477 | { |
---|
| 478 | if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 ) |
---|
| 479 | { |
---|
| 480 | encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) ); |
---|
| 481 | encodeMvdPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) ); |
---|
| 482 | encodeMVPIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) ); |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | return; |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 492 | { |
---|
| 493 | if ( !pcCU->getSlice()->isInterB() ) |
---|
| 494 | { |
---|
| 495 | return; |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx ); |
---|
| 499 | return; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | /** encode reference frame index for a PU block |
---|
| 503 | * \param pcCU |
---|
| 504 | * \param uiAbsPartIdx |
---|
| 505 | * \param eRefList |
---|
| 506 | * \returns Void |
---|
| 507 | */ |
---|
| 508 | Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 509 | { |
---|
| 510 | assert( !pcCU->isIntra( uiAbsPartIdx ) ); |
---|
| 511 | { |
---|
| 512 | if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) ) |
---|
| 513 | { |
---|
| 514 | return; |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) ) |
---|
| 518 | { |
---|
| 519 | m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList ); |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | return; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | /** encode motion vector difference for a PU block |
---|
| 527 | * \param pcCU |
---|
| 528 | * \param uiAbsPartIdx |
---|
| 529 | * \param eRefList |
---|
| 530 | * \returns Void |
---|
| 531 | */ |
---|
| 532 | Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 533 | { |
---|
| 534 | assert( !pcCU->isIntra( uiAbsPartIdx ) ); |
---|
| 535 | |
---|
| 536 | if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) ) |
---|
| 537 | { |
---|
| 538 | m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList ); |
---|
| 539 | } |
---|
| 540 | return; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 544 | { |
---|
| 545 | if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) ) |
---|
| 546 | { |
---|
| 547 | m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList ); |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | return; |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | Void TEncEntropy::encodeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth ) |
---|
| 554 | { |
---|
| 555 | m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, eType, uiTrDepth ); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx ) |
---|
| 559 | { |
---|
| 560 | m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx ); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 564 | { |
---|
| 565 | m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx ); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | Void TEncEntropy::encodeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth ) |
---|
| 569 | { |
---|
| 570 | m_pcEntropyCoderIf->codeQtCbfZero( pcCU, eType, uiTrDepth ); |
---|
| 571 | } |
---|
| 572 | Void TEncEntropy::encodeQtRootCbfZero( TComDataCU* pcCU ) |
---|
| 573 | { |
---|
| 574 | m_pcEntropyCoderIf->codeQtRootCbfZero( pcCU ); |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | // dQP |
---|
| 578 | Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD ) |
---|
| 579 | { |
---|
| 580 | if( bRD ) |
---|
| 581 | { |
---|
| 582 | uiAbsPartIdx = 0; |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | if ( pcCU->getSlice()->getPPS()->getUseDQP() ) |
---|
| 586 | { |
---|
| 587 | m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx ); |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | |
---|
| 591 | |
---|
| 592 | // texture |
---|
| 593 | /** encode coefficients |
---|
| 594 | * \param pcCU |
---|
| 595 | * \param uiAbsPartIdx |
---|
| 596 | * \param uiDepth |
---|
| 597 | * \param uiWidth |
---|
| 598 | * \param uiHeight |
---|
| 599 | */ |
---|
| 600 | Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiWidth, UInt uiHeight, Bool& bCodeDQP ) |
---|
| 601 | { |
---|
| 602 | UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight(); |
---|
| 603 | UInt uiLumaOffset = uiMinCoeffSize*uiAbsPartIdx; |
---|
| 604 | UInt uiChromaOffset = uiLumaOffset>>2; |
---|
| 605 | |
---|
| 606 | if( pcCU->isIntra(uiAbsPartIdx) ) |
---|
| 607 | { |
---|
| 608 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 609 | DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" ) |
---|
| 610 | DTRACE_CABAC_V( uiDepth ) |
---|
| 611 | DTRACE_CABAC_T( "\n" ) |
---|
| 612 | } |
---|
| 613 | else |
---|
| 614 | { |
---|
| 615 | if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) ) |
---|
| 616 | { |
---|
| 617 | m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx ); |
---|
| 618 | } |
---|
| 619 | if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) ) |
---|
| 620 | { |
---|
| 621 | return; |
---|
| 622 | } |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | xEncodeTransform( pcCU, uiLumaOffset, uiChromaOffset, uiAbsPartIdx, uiDepth, uiWidth, uiHeight, 0, bCodeDQP); |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | Void TEncEntropy::encodeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoeff, UInt uiAbsPartIdx, UInt uiTrWidth, UInt uiTrHeight, UInt uiDepth, TextType eType ) |
---|
| 629 | { |
---|
| 630 | // This is for Transform unit processing. This may be used at mode selection stage for Inter. |
---|
| 631 | m_pcEntropyCoderIf->codeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiTrWidth, uiTrHeight, uiDepth, eType ); |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType) |
---|
| 635 | { |
---|
| 636 | eTType = eTType == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA; |
---|
| 637 | |
---|
| 638 | m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, height, eTType ); |
---|
| 639 | } |
---|
| 640 | |
---|
[532] | 641 | #if !HM_CLEANUP_SAO |
---|
[313] | 642 | /** Encode SAO Offset |
---|
| 643 | * \param saoLcuParam SAO LCU paramters |
---|
| 644 | */ |
---|
| 645 | Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam, UInt compIdx) |
---|
| 646 | { |
---|
| 647 | UInt uiSymbol; |
---|
| 648 | Int i; |
---|
| 649 | |
---|
| 650 | uiSymbol = saoLcuParam->typeIdx + 1; |
---|
| 651 | if (compIdx!=2) |
---|
| 652 | { |
---|
| 653 | m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol); |
---|
| 654 | } |
---|
| 655 | if (uiSymbol) |
---|
| 656 | { |
---|
| 657 | if (saoLcuParam->typeIdx < 4 && compIdx != 2) |
---|
| 658 | { |
---|
| 659 | saoLcuParam->subTypeIdx = saoLcuParam->typeIdx; |
---|
| 660 | } |
---|
| 661 | Int bitDepth = compIdx ? g_bitDepthC : g_bitDepthY; |
---|
| 662 | Int offsetTh = 1 << min(bitDepth - 5,5); |
---|
| 663 | if( saoLcuParam->typeIdx == SAO_BO ) |
---|
| 664 | { |
---|
| 665 | for( i=0; i< saoLcuParam->length; i++) |
---|
| 666 | { |
---|
| 667 | UInt absOffset = ( (saoLcuParam->offset[i] < 0) ? -saoLcuParam->offset[i] : saoLcuParam->offset[i]); |
---|
| 668 | m_pcEntropyCoderIf->codeSaoMaxUvlc(absOffset, offsetTh-1); |
---|
| 669 | } |
---|
| 670 | for( i=0; i< saoLcuParam->length; i++) |
---|
| 671 | { |
---|
| 672 | if (saoLcuParam->offset[i] != 0) |
---|
| 673 | { |
---|
| 674 | UInt sign = (saoLcuParam->offset[i] < 0) ? 1 : 0 ; |
---|
| 675 | m_pcEntropyCoderIf->codeSAOSign(sign); |
---|
| 676 | } |
---|
| 677 | } |
---|
| 678 | uiSymbol = (UInt) (saoLcuParam->subTypeIdx); |
---|
| 679 | m_pcEntropyCoderIf->codeSaoUflc(5, uiSymbol); |
---|
| 680 | } |
---|
| 681 | else if( saoLcuParam->typeIdx < 4 ) |
---|
| 682 | { |
---|
| 683 | m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[0], offsetTh-1); |
---|
| 684 | m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[1], offsetTh-1); |
---|
| 685 | m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[2], offsetTh-1); |
---|
| 686 | m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[3], offsetTh-1); |
---|
| 687 | if (compIdx!=2) |
---|
| 688 | { |
---|
| 689 | uiSymbol = (UInt) (saoLcuParam->subTypeIdx); |
---|
| 690 | m_pcEntropyCoderIf->codeSaoUflc(2, uiSymbol); |
---|
| 691 | } |
---|
| 692 | } |
---|
| 693 | } |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | /** Encode SAO unit interleaving |
---|
| 697 | * \param rx |
---|
| 698 | * \param ry |
---|
| 699 | * \param pSaoParam |
---|
| 700 | * \param pcCU |
---|
| 701 | * \param iCUAddrInSlice |
---|
| 702 | * \param iCUAddrUpInSlice |
---|
| 703 | * \param bLFCrossSliceBoundaryFlag |
---|
| 704 | */ |
---|
| 705 | Void TEncEntropy::encodeSaoUnitInterleaving(Int compIdx, Bool saoFlag, Int rx, Int ry, SaoLcuParam* saoLcuParam, Int cuAddrInSlice, Int cuAddrUpInSlice, Int allowMergeLeft, Int allowMergeUp) |
---|
| 706 | { |
---|
| 707 | if (saoFlag) |
---|
| 708 | { |
---|
| 709 | if (rx>0 && cuAddrInSlice!=0 && allowMergeLeft) |
---|
| 710 | { |
---|
| 711 | m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeLeftFlag); |
---|
| 712 | } |
---|
| 713 | else |
---|
| 714 | { |
---|
| 715 | saoLcuParam->mergeLeftFlag = 0; |
---|
| 716 | } |
---|
| 717 | if (saoLcuParam->mergeLeftFlag == 0) |
---|
| 718 | { |
---|
| 719 | if ( (ry > 0) && (cuAddrUpInSlice>=0) && allowMergeUp ) |
---|
| 720 | { |
---|
| 721 | m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeUpFlag); |
---|
| 722 | } |
---|
| 723 | else |
---|
| 724 | { |
---|
| 725 | saoLcuParam->mergeUpFlag = 0; |
---|
| 726 | } |
---|
| 727 | if (!saoLcuParam->mergeUpFlag) |
---|
| 728 | { |
---|
| 729 | encodeSaoOffset(saoLcuParam, compIdx); |
---|
| 730 | } |
---|
| 731 | } |
---|
| 732 | } |
---|
| 733 | } |
---|
| 734 | |
---|
[532] | 735 | #endif |
---|
| 736 | |
---|
[313] | 737 | Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize ) |
---|
| 738 | { |
---|
| 739 | Int count = 0; |
---|
| 740 | |
---|
| 741 | for ( Int i = 0; i < uiSize; i++ ) |
---|
| 742 | { |
---|
| 743 | count += pcCoef[i] != 0; |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | return count; |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | /** encode quantization matrix |
---|
| 750 | * \param scalingList quantization matrix information |
---|
| 751 | */ |
---|
| 752 | Void TEncEntropy::encodeScalingList( TComScalingList* scalingList ) |
---|
| 753 | { |
---|
| 754 | m_pcEntropyCoderIf->codeScalingList( scalingList ); |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | //! \} |
---|