source: 3DVCSoftware/branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncEntropy.cpp @ 836

Last change on this file since 836 was 833, checked in by tech, 10 years ago

Merged 9.3-dev0@831.

  • Property svn:eol-style set to native
File size: 28.5 KB
Line 
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
45Void TEncEntropy::setEntropyCoder ( TEncEntropyIf* e, TComSlice* pcSlice )
46{
47  m_pcEntropyCoderIf = e;
48  m_pcEntropyCoderIf->setSlice ( pcSlice );
49}
50
51Void 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
66Void  TEncEntropy::encodeTilesWPPEntryPoint( TComSlice* pSlice )
67{
68  m_pcEntropyCoderIf->codeTilesWPPEntryPoint( pSlice );
69}
70
71Void TEncEntropy::encodeTerminatingBit      ( UInt uiIsLast )
72{
73  m_pcEntropyCoderIf->codeTerminatingBit( uiIsLast );
74 
75  return;
76}
77
78Void TEncEntropy::encodeSliceFinish()
79{
80  m_pcEntropyCoderIf->codeSliceFinish();
81}
82
83Void TEncEntropy::encodePPS( TComPPS* pcPPS )
84{
85  m_pcEntropyCoderIf->codePPS( pcPPS );
86  return;
87}
88
89#if H_3D
90Void TEncEntropy::encodeSPS( TComSPS* pcSPS, Int viewIndex, Bool depthFlag )
91{
92  m_pcEntropyCoderIf->codeSPS( pcSPS, viewIndex, depthFlag );
93  return;
94}
95#else
96Void TEncEntropy::encodeSPS( TComSPS* pcSPS )
97{
98  m_pcEntropyCoderIf->codeSPS( pcSPS );
99  return;
100}
101#endif
102
103Void TEncEntropy::encodeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
104{
105  if( bRD )
106  {
107    uiAbsPartIdx = 0;
108  }
109  m_pcEntropyCoderIf->codeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
110}
111
112Void TEncEntropy::encodeVPS( TComVPS* pcVPS )
113{
114  m_pcEntropyCoderIf->codeVPS( pcVPS );
115  return;
116}
117
118Void TEncEntropy::encodeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
119{
120  if ( pcCU->getSlice()->isIntra() )
121  {
122    return;
123  }
124  if( bRD )
125  {
126    uiAbsPartIdx = 0;
127  }
128  m_pcEntropyCoderIf->codeSkipFlag( pcCU, uiAbsPartIdx );
129}
130
131/** encode merge flag
132 * \param pcCU
133 * \param uiAbsPartIdx
134 * \returns Void
135 */
136Void TEncEntropy::encodeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
137{ 
138  // at least one merge candidate exists
139  m_pcEntropyCoderIf->codeMergeFlag( pcCU, uiAbsPartIdx );
140}
141
142/** encode merge index
143 * \param pcCU
144 * \param uiAbsPartIdx
145 * \param uiPUIdx
146 * \param bRD
147 * \returns Void
148 */
149Void TEncEntropy::encodeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
150{
151  if( bRD )
152  {
153    uiAbsPartIdx = 0;
154    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
155  }
156  m_pcEntropyCoderIf->codeMergeIndex( pcCU, uiAbsPartIdx );
157}
158
159#if H_3D_IC
160Void TEncEntropy::encodeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
161{
162#if SEC_IC_ARP_SIG_G0072
163  if ( pcCU->isIntra( uiAbsPartIdx ) || ( pcCU->getSlice()->getViewIndex() == 0 ) || pcCU->getSlice()->getIsDepth() || pcCU->getARPW( uiAbsPartIdx ) > 0 )
164#else
165  if ( pcCU->isIntra( uiAbsPartIdx ) || ( pcCU->getSlice()->getViewIndex() == 0 ) || pcCU->getSlice()->getIsDepth() )
166#endif
167  {
168    return;
169  }
170
171  if( !pcCU->getSlice()->getApplyIC() )
172    return;
173
174  if( bRD )
175  {
176    uiAbsPartIdx = 0;
177  }
178
179  if( pcCU->isICFlagRequired( uiAbsPartIdx ) )
180    m_pcEntropyCoderIf->codeICFlag( pcCU, uiAbsPartIdx );
181}
182#endif
183
184#if H_3D_ARP
185Void TEncEntropy::encodeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
186{
187  if( !pcCU->getSlice()->getARPStepNum() || pcCU->isIntra( uiAbsPartIdx ) ) 
188  {
189    return;
190  }
191
192  if ( pcCU->getPartitionSize(uiAbsPartIdx)!=SIZE_2Nx2N )
193  {
194    assert(pcCU->getARPW (uiAbsPartIdx) == 0);
195  }
196  else
197  {
198    m_pcEntropyCoderIf->codeARPW( pcCU, uiAbsPartIdx );
199  }
200}
201#endif
202
203/** encode prediction mode
204 * \param pcCU
205 * \param uiAbsPartIdx
206 * \param bRD
207 * \returns Void
208 */
209Void TEncEntropy::encodePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
210{
211  if( bRD )
212  {
213    uiAbsPartIdx = 0;
214  }
215  if ( pcCU->getSlice()->isIntra() )
216  {
217    return;
218  }
219
220  m_pcEntropyCoderIf->codePredMode( pcCU, uiAbsPartIdx );
221}
222
223// Split mode
224Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
225{
226  if( bRD )
227  {
228    uiAbsPartIdx = 0;
229  }
230  m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
231}
232
233/** encode partition size
234 * \param pcCU
235 * \param uiAbsPartIdx
236 * \param uiDepth
237 * \param bRD
238 * \returns Void
239 */
240Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
241{
242  if( bRD )
243  {
244    uiAbsPartIdx = 0;
245  }
246 
247#if H_3D_DBBP
248  PartSize eVirtualPartSize = pcCU->getPartitionSize(uiAbsPartIdx);
249  if( pcCU->getDBBPFlag(uiAbsPartIdx) )
250  {
251    AOF( pcCU->getSlice()->getVPS()->getUseDBBP(pcCU->getSlice()->getLayerIdInVps()) );
252   
253    // temporarily change partition size for DBBP blocks
254    pcCU->setPartSizeSubParts(RWTH_DBBP_PACK_MODE, uiAbsPartIdx, uiDepth);
255  }
256#endif
257 
258  m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth );
259 
260#if H_3D_DBBP
261  if( pcCU->getSlice()->getVPS()->getUseDBBP(pcCU->getSlice()->getLayerIdInVps()) && pcCU->getPartitionSize(uiAbsPartIdx) == RWTH_DBBP_PACK_MODE )
262  {
263    encodeDBBPFlag(pcCU, uiAbsPartIdx, bRD);
264   
265    if( pcCU->getDBBPFlag(uiAbsPartIdx) )
266    {
267      AOF( pcCU->getPartitionSize(uiAbsPartIdx) == RWTH_DBBP_PACK_MODE );
268      // restore virtual partition size for DBBP blocks
269      pcCU->setPartSizeSubParts(eVirtualPartSize, uiAbsPartIdx, uiDepth);
270    }
271  }
272#endif
273}
274
275/** Encode I_PCM information.
276 * \param pcCU pointer to CU
277 * \param uiAbsPartIdx CU index
278 * \param bRD flag indicating estimation or encoding
279 * \returns Void
280 */
281Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
282{
283  if(!pcCU->getSlice()->getSPS()->getUsePCM()
284    || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize())
285    || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize()))
286  {
287    return;
288  }
289#if H_3D_DIM_SDC
290  if( pcCU->getSDCFlag(uiAbsPartIdx) )
291  {
292    return;
293  }
294#endif
295 
296  if( bRD )
297  {
298    uiAbsPartIdx = 0;
299  }
300 
301  m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx );
302}
303
304Void TEncEntropy::xEncodeTransform( TComDataCU* pcCU,UInt offsetLuma, UInt offsetChroma, UInt uiAbsPartIdx, UInt uiDepth, UInt width, UInt height, UInt uiTrIdx, Bool& bCodeDQP )
305{
306  const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) + pcCU->getDepth( uiAbsPartIdx ) > uiDepth;
307  const UInt uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()]+2 - uiDepth;
308  UInt cbfY = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA    , uiTrIdx );
309  UInt cbfU = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
310  UInt cbfV = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
311
312  if(uiTrIdx==0)
313  {
314    m_bakAbsPartIdxCU = uiAbsPartIdx;
315  }
316  if( uiLog2TrafoSize == 2 )
317  {
318    UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
319    if( ( uiAbsPartIdx % partNum ) == 0 )
320    {
321      m_uiBakAbsPartIdx   = uiAbsPartIdx;
322      m_uiBakChromaOffset = offsetChroma;
323    }
324    else if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
325    {
326      cbfU = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
327      cbfV = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
328    }
329  }
330 
331  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) )
332  {
333    assert( uiSubdiv );
334  }
335  else if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) &&  (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) )
336  {
337    if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
338    {
339      assert( uiSubdiv );
340    }
341    else
342    {
343      assert(!uiSubdiv );
344    }
345  }
346  else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
347  {
348    assert( uiSubdiv );
349  }
350  else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
351  {
352    assert( !uiSubdiv );
353  }
354  else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
355  {
356    assert( !uiSubdiv );
357  }
358  else
359  {
360    assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
361    m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, 5 - uiLog2TrafoSize );
362  }
363
364  const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx );
365  const Bool bFirstCbfOfCU = uiTrDepthCurr == 0;
366  if( bFirstCbfOfCU || uiLog2TrafoSize > 2 )
367  {
368    if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) )
369    {
370      m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
371    }
372    if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) )
373    {
374      m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
375    }
376  }
377  else if( uiLog2TrafoSize == 2 )
378  {
379    assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) );
380    assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) );
381  }
382 
383  if( uiSubdiv )
384  {
385    UInt size;
386    width  >>= 1;
387    height >>= 1;
388    size = width*height;
389    uiTrIdx++;
390    ++uiDepth;
391    const UInt partNum = pcCU->getPic()->getNumPartInCU() >> (uiDepth << 1);
392   
393    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
394
395    uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
396    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
397
398    uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
399    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
400
401    uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
402    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
403  }
404  else
405  {
406#if !H_MV_ENC_DEC_TRAC
407    {
408      DTRACE_CABAC_VL( g_nSymbolCounter++ );
409      DTRACE_CABAC_T( "\tTrIdx: abspart=" );
410      DTRACE_CABAC_V( uiAbsPartIdx );
411      DTRACE_CABAC_T( "\tdepth=" );
412      DTRACE_CABAC_V( uiDepth );
413      DTRACE_CABAC_T( "\ttrdepth=" );
414      DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) );
415      DTRACE_CABAC_T( "\n" );
416    }
417#endif
418   
419    if( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, 0 ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, 0 ) )
420    {
421      assert( pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, 0 ) );
422      //      printf( "saved one bin! " );
423    }
424    else
425    {
426      m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, pcCU->getTransformIdx( uiAbsPartIdx ) );
427    }
428
429
430    if ( cbfY || cbfU || cbfV )
431    {
432      // dQP: only for LCU once
433      if ( pcCU->getSlice()->getPPS()->getUseDQP() )
434      {
435        if ( bCodeDQP )
436        {
437          encodeQP( pcCU, m_bakAbsPartIdxCU );
438          bCodeDQP = false;
439        }
440      }
441    }
442    if( cbfY )
443    {
444      Int trWidth = width;
445      Int trHeight = height;
446      m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffY()+offsetLuma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
447    }
448    if( uiLog2TrafoSize > 2 )
449    {
450      Int trWidth = width >> 1;
451      Int trHeight = height >> 1;
452      if( cbfU )
453      {
454        m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
455      }
456      if( cbfV )
457      {
458        m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
459      }
460    }
461    else
462    {
463      UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
464      if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
465      {
466        Int trWidth = width;
467        Int trHeight = height;
468        if( cbfU )
469        {
470          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
471        }
472        if( cbfV )
473        {
474          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
475        }
476      }
477    }
478  }
479}
480
481// Intra direction for Luma
482Void TEncEntropy::encodeIntraDirModeLuma  ( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiplePU )
483{
484  m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, absPartIdx , isMultiplePU);
485}
486
487// Intra direction for Chroma
488Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
489{
490  if( bRD )
491  {
492    uiAbsPartIdx = 0;
493  }
494 
495  m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx );
496}
497
498Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
499{
500  if( bRD )
501  {
502    uiAbsPartIdx = 0;
503  }
504  if( pcCU->isIntra( uiAbsPartIdx ) )                                 // If it is Intra mode, encode intra prediction mode.
505  {
506    encodeIntraDirModeLuma  ( pcCU, uiAbsPartIdx,true );
507#if H_3D_DIM_SDC
508    if(!pcCU->getSDCFlag(uiAbsPartIdx))
509#endif
510    encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
511  }
512  else                                                                // if it is Inter mode, encode motion vector and reference index
513  {
514    encodePUWise( pcCU, uiAbsPartIdx, bRD );
515  }
516}
517
518/** encode motion information for every PU block
519 * \param pcCU
520 * \param uiAbsPartIdx
521 * \param bRD
522 * \returns Void
523 */
524Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
525{
526  if ( bRD )
527  {
528    uiAbsPartIdx = 0;
529  }
530 
531  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
532  UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) );
533  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
534  UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() - uiDepth ) << 1 ) ) >> 4;
535
536  for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset )
537  {
538#if H_MV_ENC_DEC_TRAC
539    DTRACE_PU_S("=========== prediction_unit ===========\n")
540       //Todo:
541      //DTRACE_PU("x0", uiLPelX)
542      //DTRACE_PU("x1", uiTPelY)
543#endif
544    encodeMergeFlag( pcCU, uiSubPartIdx );
545    if ( pcCU->getMergeFlag( uiSubPartIdx ) )
546    {
547      encodeMergeIndex( pcCU, uiSubPartIdx );
548    }
549    else
550    {
551      encodeInterDirPU( pcCU, uiSubPartIdx );
552      for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
553      {
554        if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
555        {
556          encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
557          encodeMvdPU       ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
558          encodeMVPIdxPU    ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
559        }
560      }
561    }
562  }
563
564  return;
565}
566
567Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx )
568{
569  if ( !pcCU->getSlice()->isInterB() )
570  {
571    return;
572  }
573
574  m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx );
575  return;
576}
577
578/** encode reference frame index for a PU block
579 * \param pcCU
580 * \param uiAbsPartIdx
581 * \param eRefList
582 * \returns Void
583 */
584Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
585{
586  assert( !pcCU->isIntra( uiAbsPartIdx ) );
587  {
588    if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) )
589    {
590      return;
591    }
592
593    if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
594    {
595      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList );
596    }
597  }
598
599  return;
600}
601
602/** encode motion vector difference for a PU block
603 * \param pcCU
604 * \param uiAbsPartIdx
605 * \param eRefList
606 * \returns Void
607 */
608Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
609{
610  assert( !pcCU->isIntra( uiAbsPartIdx ) );
611
612  if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
613  {
614    m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList );
615  }
616  return;
617}
618
619Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
620{
621  if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) )
622  {
623    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList );
624  }
625
626  return;
627}
628
629Void TEncEntropy::encodeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
630{
631  m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, eType, uiTrDepth );
632}
633
634Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
635{
636  m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx );
637}
638
639Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
640{
641  m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
642}
643
644Void TEncEntropy::encodeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
645{
646  m_pcEntropyCoderIf->codeQtCbfZero( pcCU, eType, uiTrDepth );
647}
648Void TEncEntropy::encodeQtRootCbfZero( TComDataCU* pcCU )
649{
650  m_pcEntropyCoderIf->codeQtRootCbfZero( pcCU );
651}
652
653// dQP
654Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
655{
656  if( bRD )
657  {
658    uiAbsPartIdx = 0;
659  }
660 
661  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
662  {
663    m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx );
664  }
665}
666
667
668// texture
669/** encode coefficients
670 * \param pcCU
671 * \param uiAbsPartIdx
672 * \param uiDepth
673 * \param uiWidth
674 * \param uiHeight
675 */
676Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiWidth, UInt uiHeight, Bool& bCodeDQP )
677{
678  UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
679  UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
680  UInt uiChromaOffset = uiLumaOffset>>2;
681#if QC_SDC_UNIFY_G0130
682#if H_3D_DIM_SDC
683  if( pcCU->getSDCFlag( uiAbsPartIdx ) && pcCU->isIntra( uiAbsPartIdx ) )
684  {
685    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
686    assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
687    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
688    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
689    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
690  }
691#endif
692
693#if H_3D_INTER_SDC
694  if( pcCU->getSDCFlag( uiAbsPartIdx ) && !pcCU->isIntra( uiAbsPartIdx ) )
695  {
696    assert( !pcCU->isSkipped( uiAbsPartIdx ) );
697    assert( !pcCU->isIntra( uiAbsPartIdx) );
698    assert( pcCU->getSlice()->getIsDepth() );
699  }
700#endif
701#if QC_SDC_UNIFY_G0130_FIX
702  if( pcCU->getSlice()->getIsDepth() && ( pcCU->getSDCFlag( uiAbsPartIdx ) || pcCU->isIntra( uiAbsPartIdx ) ) )
703#else
704  if( pcCU->getSDCFlag( uiAbsPartIdx ) || pcCU->isIntra( uiAbsPartIdx ) )
705#endif
706  {
707    Int iPartNum = ( pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_NxN ) ? 4 : 1;
708    UInt uiPartOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth( uiAbsPartIdx ) << 1 ) ) >> 2;
709   
710    if( !pcCU->getSDCFlag( uiAbsPartIdx ) )
711    {
712      for( Int iPart = 0; iPart < iPartNum; iPart++ )
713      {
714        if( getDimType( pcCU->getLumaIntraDir( uiAbsPartIdx + uiPartOffset*iPart ) ) < DIM_NUM_TYPE ) 
715        {
716          m_pcEntropyCoderIf->codeDeltaDC( pcCU, uiAbsPartIdx + uiPartOffset*iPart );
717        }
718      }
719    }
720    else
721    {
722      m_pcEntropyCoderIf->codeDeltaDC( pcCU, uiAbsPartIdx );
723      return;
724    }
725  }
726#else
727#if H_3D_DIM_SDC
728  if( pcCU->getSDCFlag( uiAbsPartIdx ) )
729  {
730    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
731    assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
732    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
733    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
734    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
735    return;
736  }
737#endif
738
739#if H_3D_INTER_SDC
740  if( pcCU->getInterSDCFlag( uiAbsPartIdx ) )
741  {
742    assert( !pcCU->isSkipped( uiAbsPartIdx ) );
743    assert( !pcCU->isIntra( uiAbsPartIdx) );
744    assert( pcCU->getSlice()->getIsDepth() );
745
746    encodeInterSDCResidualData( pcCU, uiAbsPartIdx, false );
747    return;
748  }
749#endif
750#endif
751
752  if( pcCU->isIntra(uiAbsPartIdx) )
753  {
754#if !H_MV
755    DTRACE_CABAC_VL( g_nSymbolCounter++ )
756    DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" )
757    DTRACE_CABAC_V( uiDepth )
758    DTRACE_CABAC_T( "\n" )
759#endif
760  }
761  else
762  {
763    if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) )
764    {
765      m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
766    }
767    if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) )
768    {
769      return;
770    }
771  }
772 
773  xEncodeTransform( pcCU, uiLumaOffset, uiChromaOffset, uiAbsPartIdx, uiDepth, uiWidth, uiHeight, 0, bCodeDQP);
774}
775
776Void TEncEntropy::encodeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoeff, UInt uiAbsPartIdx, UInt uiTrWidth, UInt uiTrHeight, UInt uiDepth, TextType eType )
777{
778  // This is for Transform unit processing. This may be used at mode selection stage for Inter.
779  m_pcEntropyCoderIf->codeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiTrWidth, uiTrHeight, uiDepth, eType );
780}
781
782Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType)
783{ 
784  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
785 
786  m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, height, eTType );
787}
788
789/** Encode SAO Offset
790 * \param  saoLcuParam SAO LCU paramters
791 */
792Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam, UInt compIdx)
793{
794  UInt uiSymbol;
795  Int i;
796
797  uiSymbol = saoLcuParam->typeIdx + 1;
798  if (compIdx!=2)
799  {
800    m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
801  }
802  if (uiSymbol)
803  {
804    if (saoLcuParam->typeIdx < 4 && compIdx != 2)
805    {
806      saoLcuParam->subTypeIdx = saoLcuParam->typeIdx;
807    }
808    Int bitDepth = compIdx ? g_bitDepthC : g_bitDepthY;
809    Int offsetTh = 1 << min(bitDepth - 5,5);
810    if( saoLcuParam->typeIdx == SAO_BO )
811    {
812      for( i=0; i< saoLcuParam->length; i++)
813      {
814        UInt absOffset = ( (saoLcuParam->offset[i] < 0) ? -saoLcuParam->offset[i] : saoLcuParam->offset[i]);
815        m_pcEntropyCoderIf->codeSaoMaxUvlc(absOffset, offsetTh-1);
816      } 
817      for( i=0; i< saoLcuParam->length; i++)
818      {
819        if (saoLcuParam->offset[i] != 0)
820        {
821          UInt sign = (saoLcuParam->offset[i] < 0) ? 1 : 0 ;
822          m_pcEntropyCoderIf->codeSAOSign(sign);
823        }
824      }
825      uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
826      m_pcEntropyCoderIf->codeSaoUflc(5, uiSymbol);
827    }
828    else if( saoLcuParam->typeIdx < 4 )
829    {
830      m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[0], offsetTh-1);
831      m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[1], offsetTh-1);
832      m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[2], offsetTh-1);
833      m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[3], offsetTh-1);
834      if (compIdx!=2)
835      {
836        uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
837        m_pcEntropyCoderIf->codeSaoUflc(2, uiSymbol);
838      }
839    }
840  }
841}
842
843/** Encode SAO unit interleaving
844* \param  rx
845* \param  ry
846* \param  pSaoParam
847* \param  pcCU
848* \param  iCUAddrInSlice
849* \param  iCUAddrUpInSlice
850* \param  bLFCrossSliceBoundaryFlag
851 */
852Void TEncEntropy::encodeSaoUnitInterleaving(Int compIdx, Bool saoFlag, Int rx, Int ry, SaoLcuParam* saoLcuParam, Int cuAddrInSlice, Int cuAddrUpInSlice, Int allowMergeLeft, Int allowMergeUp)
853{
854  if (saoFlag)
855  {
856    if (rx>0 && cuAddrInSlice!=0 && allowMergeLeft)
857    {
858      m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeLeftFlag);
859    }
860    else
861    {
862      saoLcuParam->mergeLeftFlag = 0;
863    }
864    if (saoLcuParam->mergeLeftFlag == 0)
865    {
866      if ( (ry > 0) && (cuAddrUpInSlice>=0) && allowMergeUp )
867      {
868        m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeUpFlag);
869      }
870      else
871      {
872        saoLcuParam->mergeUpFlag = 0;
873      }
874      if (!saoLcuParam->mergeUpFlag)
875      {
876        encodeSaoOffset(saoLcuParam, compIdx);
877      }
878    }
879  }
880}
881
882Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize )
883{
884  Int count = 0;
885 
886  for ( Int i = 0; i < uiSize; i++ )
887  {
888    count += pcCoef[i] != 0;
889  }
890 
891  return count;
892}
893
894/** encode quantization matrix
895 * \param scalingList quantization matrix information
896 */
897Void TEncEntropy::encodeScalingList( TComScalingList* scalingList )
898{
899  m_pcEntropyCoderIf->codeScalingList( scalingList );
900}
901
902#if H_3D_INTER_SDC
903#if QC_SDC_UNIFY_G0130
904Void TEncEntropy::encodeDeltaDC  ( TComDataCU* pcCU, UInt absPartIdx )
905{
906  m_pcEntropyCoderIf->codeDeltaDC( pcCU, absPartIdx );
907}
908
909Void TEncEntropy::encodeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
910{
911  if( ( !pcCU->isIntra( uiAbsPartIdx ) && !pcCU->getSlice()->getVPS()->getInterSDCFlag( pcCU->getSlice()->getLayerIdInVps() ) ) || 
912    ( pcCU->isIntra( uiAbsPartIdx ) && !pcCU->getSlice()->getVPS()->getVpsDepthModesFlag( pcCU->getSlice()->getLayerIdInVps() ) ) )
913  {
914    return;
915  }
916
917#if SEC_INTER_SDC_G0101
918  if( !pcCU->getSlice()->getIsDepth() || pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_2Nx2N || pcCU->isSkipped( uiAbsPartIdx ) )
919#else
920  if( !pcCU->getSlice()->getIsDepth() || ( pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_2Nx2N ) || pcCU->isSkipped( uiAbsPartIdx ) )
921#endif
922  {
923    return;
924  }
925
926#if SEC_INTER_SDC_G0101
927  assert( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N || ( !pcCU->isIntra( uiAbsPartIdx ) && !pcCU->isSkipped( uiAbsPartIdx ) ) );
928#else
929  assert( ( pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) || ( !pcCU->isIntra( uiAbsPartIdx ) && !pcCU->isSkipped( uiAbsPartIdx ) ) );
930#endif
931
932  if( bRD )
933  {
934    uiAbsPartIdx = 0;
935  }
936
937  m_pcEntropyCoderIf->codeSDCFlag( pcCU, uiAbsPartIdx );
938}
939#else
940Void TEncEntropy::encodeInterSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
941{
942  if( !pcCU->getSlice()->getVPS()->getInterSDCFlag( pcCU->getSlice()->getLayerIdInVps() ) )
943  {
944    return;
945  }
946
947  if( !pcCU->getSlice()->getIsDepth() || pcCU->isIntra( uiAbsPartIdx ) || pcCU->isSkipped( uiAbsPartIdx ) )
948  {
949    return;
950  }
951
952  if( bRD )
953  {
954    uiAbsPartIdx = 0;
955  }
956
957  m_pcEntropyCoderIf->codeInterSDCFlag( pcCU, uiAbsPartIdx );
958}
959
960Void TEncEntropy::encodeInterSDCResidualData( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
961{
962  if( !pcCU->getSlice()->getVPS()->getInterSDCFlag( pcCU->getSlice()->getLayerIdInVps() ) )
963  {
964    return;
965  }
966
967  if( !pcCU->getSlice()->getIsDepth() || pcCU->isIntra( uiAbsPartIdx ) || !pcCU->getInterSDCFlag( uiAbsPartIdx ) )
968  {
969    return;
970  }
971
972  if( bRD )
973  {
974    uiAbsPartIdx = 0;
975  }
976
977  // number of segments depends on prediction mode for INTRA
978  UInt uiNumSegments = ( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) ? 1 : ( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_NxN ? 4 : 2 );
979
980  // encode residual data for each segment
981  for( UInt uiSeg = 0; uiSeg < uiNumSegments; uiSeg++ )
982  {
983    m_pcEntropyCoderIf->codeInterSDCResidualData( pcCU, uiAbsPartIdx, uiSeg );
984  }
985}
986#endif
987#endif
988#if H_3D_DBBP
989Void TEncEntropy::encodeDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
990{
991  if( bRD )
992  {
993    uiAbsPartIdx = 0;
994  }
995  m_pcEntropyCoderIf->codeDBBPFlag( pcCU, uiAbsPartIdx );
996}
997#endif
998
999//! \}
Note: See TracBrowser for help on using the repository browser.