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