source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncEntropy.cpp @ 773

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

Merged branch/9.2-dev0@722.

  • Property svn:eol-style set to native
File size: 24.4 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 ( pcCU->isIntra( uiAbsPartIdx ) || ( pcCU->getSlice()->getViewIndex() == 0 ) || pcCU->getSlice()->getIsDepth() )
163  {
164    return;
165  }
166
167  if( !pcCU->getSlice()->getApplyIC() )
168    return;
169
170  if( bRD )
171  {
172    uiAbsPartIdx = 0;
173  }
174
175  if( pcCU->isICFlagRequired( uiAbsPartIdx ) )
176    m_pcEntropyCoderIf->codeICFlag( pcCU, uiAbsPartIdx );
177}
178#endif
179
180#if H_3D_ARP
181Void TEncEntropy::encodeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
182{
183  if( !pcCU->getSlice()->getARPStepNum() || pcCU->isIntra( uiAbsPartIdx ) ) 
184  {
185    return;
186  }
187
188  if ( pcCU->getPartitionSize(uiAbsPartIdx)!=SIZE_2Nx2N )
189  {
190    assert(pcCU->getARPW (uiAbsPartIdx) == 0);
191  }
192  else
193  {
194    m_pcEntropyCoderIf->codeARPW( pcCU, uiAbsPartIdx );
195  }
196}
197#endif
198
199/** encode prediction mode
200 * \param pcCU
201 * \param uiAbsPartIdx
202 * \param bRD
203 * \returns Void
204 */
205Void TEncEntropy::encodePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
206{
207  if( bRD )
208  {
209    uiAbsPartIdx = 0;
210  }
211  if ( pcCU->getSlice()->isIntra() )
212  {
213    return;
214  }
215
216  m_pcEntropyCoderIf->codePredMode( pcCU, uiAbsPartIdx );
217}
218
219// Split mode
220Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
221{
222  if( bRD )
223  {
224    uiAbsPartIdx = 0;
225  }
226  m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
227}
228
229/** encode partition size
230 * \param pcCU
231 * \param uiAbsPartIdx
232 * \param uiDepth
233 * \param bRD
234 * \returns Void
235 */
236Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
237{
238  if( bRD )
239  {
240    uiAbsPartIdx = 0;
241  }
242  m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth );
243}
244
245/** Encode I_PCM information.
246 * \param pcCU pointer to CU
247 * \param uiAbsPartIdx CU index
248 * \param bRD flag indicating estimation or encoding
249 * \returns Void
250 */
251Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
252{
253  if(!pcCU->getSlice()->getSPS()->getUsePCM()
254    || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize())
255    || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize()))
256  {
257    return;
258  }
259#if H_3D_DIM_SDC
260  if( pcCU->getSDCFlag(uiAbsPartIdx) )
261  {
262    return;
263  }
264#endif
265 
266  if( bRD )
267  {
268    uiAbsPartIdx = 0;
269  }
270 
271  m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx );
272}
273
274Void TEncEntropy::xEncodeTransform( TComDataCU* pcCU,UInt offsetLuma, UInt offsetChroma, UInt uiAbsPartIdx, UInt uiDepth, UInt width, UInt height, UInt uiTrIdx, Bool& bCodeDQP )
275{
276  const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) + pcCU->getDepth( uiAbsPartIdx ) > uiDepth;
277  const UInt uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()]+2 - uiDepth;
278  UInt cbfY = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA    , uiTrIdx );
279  UInt cbfU = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
280  UInt cbfV = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
281
282  if(uiTrIdx==0)
283  {
284    m_bakAbsPartIdxCU = uiAbsPartIdx;
285  }
286  if( uiLog2TrafoSize == 2 )
287  {
288    UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
289    if( ( uiAbsPartIdx % partNum ) == 0 )
290    {
291      m_uiBakAbsPartIdx   = uiAbsPartIdx;
292      m_uiBakChromaOffset = offsetChroma;
293    }
294    else if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
295    {
296      cbfU = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
297      cbfV = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
298    }
299  }
300 
301  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) )
302  {
303    assert( uiSubdiv );
304  }
305  else if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) &&  (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) )
306  {
307    if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
308    {
309      assert( uiSubdiv );
310    }
311    else
312    {
313      assert(!uiSubdiv );
314    }
315  }
316  else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
317  {
318    assert( uiSubdiv );
319  }
320  else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
321  {
322    assert( !uiSubdiv );
323  }
324  else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
325  {
326    assert( !uiSubdiv );
327  }
328  else
329  {
330    assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
331    m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, 5 - uiLog2TrafoSize );
332  }
333
334  const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx );
335  const Bool bFirstCbfOfCU = uiTrDepthCurr == 0;
336  if( bFirstCbfOfCU || uiLog2TrafoSize > 2 )
337  {
338    if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) )
339    {
340      m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
341    }
342    if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) )
343    {
344      m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
345    }
346  }
347  else if( uiLog2TrafoSize == 2 )
348  {
349    assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) );
350    assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) );
351  }
352 
353  if( uiSubdiv )
354  {
355    UInt size;
356    width  >>= 1;
357    height >>= 1;
358    size = width*height;
359    uiTrIdx++;
360    ++uiDepth;
361    const UInt partNum = pcCU->getPic()->getNumPartInCU() >> (uiDepth << 1);
362   
363    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
364
365    uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
366    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
367
368    uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
369    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
370
371    uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
372    xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, uiDepth, width, height, uiTrIdx, bCodeDQP );
373  }
374  else
375  {
376#if !H_MV_ENC_DEC_TRAC
377    {
378      DTRACE_CABAC_VL( g_nSymbolCounter++ );
379      DTRACE_CABAC_T( "\tTrIdx: abspart=" );
380      DTRACE_CABAC_V( uiAbsPartIdx );
381      DTRACE_CABAC_T( "\tdepth=" );
382      DTRACE_CABAC_V( uiDepth );
383      DTRACE_CABAC_T( "\ttrdepth=" );
384      DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) );
385      DTRACE_CABAC_T( "\n" );
386    }
387#endif
388   
389    if( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, 0 ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, 0 ) )
390    {
391      assert( pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, 0 ) );
392      //      printf( "saved one bin! " );
393    }
394    else
395    {
396      m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, pcCU->getTransformIdx( uiAbsPartIdx ) );
397    }
398
399
400    if ( cbfY || cbfU || cbfV )
401    {
402      // dQP: only for LCU once
403      if ( pcCU->getSlice()->getPPS()->getUseDQP() )
404      {
405        if ( bCodeDQP )
406        {
407          encodeQP( pcCU, m_bakAbsPartIdxCU );
408          bCodeDQP = false;
409        }
410      }
411    }
412    if( cbfY )
413    {
414      Int trWidth = width;
415      Int trHeight = height;
416      m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffY()+offsetLuma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
417    }
418    if( uiLog2TrafoSize > 2 )
419    {
420      Int trWidth = width >> 1;
421      Int trHeight = height >> 1;
422      if( cbfU )
423      {
424        m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
425      }
426      if( cbfV )
427      {
428        m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
429      }
430    }
431    else
432    {
433      UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
434      if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
435      {
436        Int trWidth = width;
437        Int trHeight = height;
438        if( cbfU )
439        {
440          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
441        }
442        if( cbfV )
443        {
444          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
445        }
446      }
447    }
448  }
449}
450
451// Intra direction for Luma
452Void TEncEntropy::encodeIntraDirModeLuma  ( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiplePU )
453{
454  m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, absPartIdx , isMultiplePU);
455}
456
457// Intra direction for Chroma
458Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
459{
460  if( bRD )
461  {
462    uiAbsPartIdx = 0;
463  }
464 
465  m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx );
466}
467
468Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
469{
470  if( bRD )
471  {
472    uiAbsPartIdx = 0;
473  }
474  if( pcCU->isIntra( uiAbsPartIdx ) )                                 // If it is Intra mode, encode intra prediction mode.
475  {
476    encodeIntraDirModeLuma  ( pcCU, uiAbsPartIdx,true );
477#if H_3D_DIM_SDC
478    if(!pcCU->getSDCFlag(uiAbsPartIdx))
479#endif
480    encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
481  }
482  else                                                                // if it is Inter mode, encode motion vector and reference index
483  {
484    encodePUWise( pcCU, uiAbsPartIdx, bRD );
485  }
486}
487
488/** encode motion information for every PU block
489 * \param pcCU
490 * \param uiAbsPartIdx
491 * \param bRD
492 * \returns Void
493 */
494Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
495{
496  if ( bRD )
497  {
498    uiAbsPartIdx = 0;
499  }
500 
501  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
502  UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) );
503  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
504  UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() - uiDepth ) << 1 ) ) >> 4;
505
506  for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset )
507  {
508#if H_MV_ENC_DEC_TRAC
509    DTRACE_PU_S("=========== prediction_unit ===========\n")
510       //Todo:
511      //DTRACE_PU("x0", uiLPelX)
512      //DTRACE_PU("x1", uiTPelY)
513#endif
514    encodeMergeFlag( pcCU, uiSubPartIdx );
515    if ( pcCU->getMergeFlag( uiSubPartIdx ) )
516    {
517      encodeMergeIndex( pcCU, uiSubPartIdx );
518    }
519    else
520    {
521      encodeInterDirPU( pcCU, uiSubPartIdx );
522      for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
523      {
524        if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
525        {
526          encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
527          encodeMvdPU       ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
528          encodeMVPIdxPU    ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
529        }
530      }
531    }
532  }
533
534  return;
535}
536
537Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx )
538{
539  if ( !pcCU->getSlice()->isInterB() )
540  {
541    return;
542  }
543
544  m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx );
545  return;
546}
547
548/** encode reference frame index for a PU block
549 * \param pcCU
550 * \param uiAbsPartIdx
551 * \param eRefList
552 * \returns Void
553 */
554Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
555{
556  assert( !pcCU->isIntra( uiAbsPartIdx ) );
557  {
558    if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) )
559    {
560      return;
561    }
562
563    if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
564    {
565      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList );
566    }
567  }
568
569  return;
570}
571
572/** encode motion vector difference for a PU block
573 * \param pcCU
574 * \param uiAbsPartIdx
575 * \param eRefList
576 * \returns Void
577 */
578Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
579{
580  assert( !pcCU->isIntra( uiAbsPartIdx ) );
581
582  if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
583  {
584    m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList );
585  }
586  return;
587}
588
589Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
590{
591  if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) )
592  {
593    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList );
594  }
595
596  return;
597}
598
599Void TEncEntropy::encodeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
600{
601  m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, eType, uiTrDepth );
602}
603
604Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
605{
606  m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx );
607}
608
609Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
610{
611  m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
612}
613
614Void TEncEntropy::encodeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
615{
616  m_pcEntropyCoderIf->codeQtCbfZero( pcCU, eType, uiTrDepth );
617}
618Void TEncEntropy::encodeQtRootCbfZero( TComDataCU* pcCU )
619{
620  m_pcEntropyCoderIf->codeQtRootCbfZero( pcCU );
621}
622
623// dQP
624Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
625{
626  if( bRD )
627  {
628    uiAbsPartIdx = 0;
629  }
630 
631  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
632  {
633    m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx );
634  }
635}
636
637
638// texture
639/** encode coefficients
640 * \param pcCU
641 * \param uiAbsPartIdx
642 * \param uiDepth
643 * \param uiWidth
644 * \param uiHeight
645 */
646Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiWidth, UInt uiHeight, Bool& bCodeDQP )
647{
648  UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
649  UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
650  UInt uiChromaOffset = uiLumaOffset>>2;
651   
652#if H_3D_DIM_SDC
653  if( pcCU->getSDCFlag( uiAbsPartIdx ) )
654  {
655    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
656    assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
657    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
658    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
659    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
660    return;
661  }
662#endif
663
664#if H_3D_INTER_SDC
665  if( pcCU->getInterSDCFlag( uiAbsPartIdx ) )
666  {
667    assert( !pcCU->isSkipped( uiAbsPartIdx ) );
668    assert( !pcCU->isIntra( uiAbsPartIdx) );
669    assert( pcCU->getSlice()->getIsDepth() );
670
671    encodeInterSDCResidualData( pcCU, uiAbsPartIdx, false );
672    return;
673  }
674#endif
675
676  if( pcCU->isIntra(uiAbsPartIdx) )
677  {
678#if !H_MV
679    DTRACE_CABAC_VL( g_nSymbolCounter++ )
680    DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" )
681    DTRACE_CABAC_V( uiDepth )
682    DTRACE_CABAC_T( "\n" )
683#endif
684  }
685  else
686  {
687    if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) )
688    {
689      m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
690    }
691    if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) )
692    {
693      return;
694    }
695  }
696 
697  xEncodeTransform( pcCU, uiLumaOffset, uiChromaOffset, uiAbsPartIdx, uiDepth, uiWidth, uiHeight, 0, bCodeDQP);
698}
699
700Void TEncEntropy::encodeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoeff, UInt uiAbsPartIdx, UInt uiTrWidth, UInt uiTrHeight, UInt uiDepth, TextType eType )
701{
702  // This is for Transform unit processing. This may be used at mode selection stage for Inter.
703  m_pcEntropyCoderIf->codeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiTrWidth, uiTrHeight, uiDepth, eType );
704}
705
706Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType)
707{ 
708  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
709 
710  m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, height, eTType );
711}
712
713/** Encode SAO Offset
714 * \param  saoLcuParam SAO LCU paramters
715 */
716Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam, UInt compIdx)
717{
718  UInt uiSymbol;
719  Int i;
720
721  uiSymbol = saoLcuParam->typeIdx + 1;
722  if (compIdx!=2)
723  {
724    m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
725  }
726  if (uiSymbol)
727  {
728    if (saoLcuParam->typeIdx < 4 && compIdx != 2)
729    {
730      saoLcuParam->subTypeIdx = saoLcuParam->typeIdx;
731    }
732    Int bitDepth = compIdx ? g_bitDepthC : g_bitDepthY;
733    Int offsetTh = 1 << min(bitDepth - 5,5);
734    if( saoLcuParam->typeIdx == SAO_BO )
735    {
736      for( i=0; i< saoLcuParam->length; i++)
737      {
738        UInt absOffset = ( (saoLcuParam->offset[i] < 0) ? -saoLcuParam->offset[i] : saoLcuParam->offset[i]);
739        m_pcEntropyCoderIf->codeSaoMaxUvlc(absOffset, offsetTh-1);
740      } 
741      for( i=0; i< saoLcuParam->length; i++)
742      {
743        if (saoLcuParam->offset[i] != 0)
744        {
745          UInt sign = (saoLcuParam->offset[i] < 0) ? 1 : 0 ;
746          m_pcEntropyCoderIf->codeSAOSign(sign);
747        }
748      }
749      uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
750      m_pcEntropyCoderIf->codeSaoUflc(5, uiSymbol);
751    }
752    else if( saoLcuParam->typeIdx < 4 )
753    {
754      m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[0], offsetTh-1);
755      m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[1], offsetTh-1);
756      m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[2], offsetTh-1);
757      m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[3], offsetTh-1);
758      if (compIdx!=2)
759      {
760        uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
761        m_pcEntropyCoderIf->codeSaoUflc(2, uiSymbol);
762      }
763    }
764  }
765}
766
767/** Encode SAO unit interleaving
768* \param  rx
769* \param  ry
770* \param  pSaoParam
771* \param  pcCU
772* \param  iCUAddrInSlice
773* \param  iCUAddrUpInSlice
774* \param  bLFCrossSliceBoundaryFlag
775 */
776Void TEncEntropy::encodeSaoUnitInterleaving(Int compIdx, Bool saoFlag, Int rx, Int ry, SaoLcuParam* saoLcuParam, Int cuAddrInSlice, Int cuAddrUpInSlice, Int allowMergeLeft, Int allowMergeUp)
777{
778  if (saoFlag)
779  {
780    if (rx>0 && cuAddrInSlice!=0 && allowMergeLeft)
781    {
782      m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeLeftFlag);
783    }
784    else
785    {
786      saoLcuParam->mergeLeftFlag = 0;
787    }
788    if (saoLcuParam->mergeLeftFlag == 0)
789    {
790      if ( (ry > 0) && (cuAddrUpInSlice>=0) && allowMergeUp )
791      {
792        m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeUpFlag);
793      }
794      else
795      {
796        saoLcuParam->mergeUpFlag = 0;
797      }
798      if (!saoLcuParam->mergeUpFlag)
799      {
800        encodeSaoOffset(saoLcuParam, compIdx);
801      }
802    }
803  }
804}
805
806Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize )
807{
808  Int count = 0;
809 
810  for ( Int i = 0; i < uiSize; i++ )
811  {
812    count += pcCoef[i] != 0;
813  }
814 
815  return count;
816}
817
818/** encode quantization matrix
819 * \param scalingList quantization matrix information
820 */
821Void TEncEntropy::encodeScalingList( TComScalingList* scalingList )
822{
823  m_pcEntropyCoderIf->codeScalingList( scalingList );
824}
825
826#if H_3D_INTER_SDC
827Void TEncEntropy::encodeInterSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
828{
829  if( !pcCU->getSlice()->getVPS()->getInterSDCFlag( pcCU->getSlice()->getLayerIdInVps() ) )
830  {
831    return;
832  }
833
834  if( !pcCU->getSlice()->getIsDepth() || pcCU->isIntra( uiAbsPartIdx ) || pcCU->isSkipped( uiAbsPartIdx ) )
835  {
836    return;
837  }
838
839  if( bRD )
840  {
841    uiAbsPartIdx = 0;
842  }
843
844  m_pcEntropyCoderIf->codeInterSDCFlag( pcCU, uiAbsPartIdx );
845}
846
847Void TEncEntropy::encodeInterSDCResidualData( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
848{
849  if( !pcCU->getSlice()->getVPS()->getInterSDCFlag( pcCU->getSlice()->getLayerIdInVps() ) )
850  {
851    return;
852  }
853
854  if( !pcCU->getSlice()->getIsDepth() || pcCU->isIntra( uiAbsPartIdx ) || !pcCU->getInterSDCFlag( uiAbsPartIdx ) )
855  {
856    return;
857  }
858
859  if( bRD )
860  {
861    uiAbsPartIdx = 0;
862  }
863
864  // number of segments depends on prediction mode for INTRA
865  UInt uiNumSegments = ( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) ? 1 : ( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_NxN ? 4 : 2 );
866
867  // encode residual data for each segment
868  for( UInt uiSeg = 0; uiSeg < uiNumSegments; uiSeg++ )
869  {
870    m_pcEntropyCoderIf->codeInterSDCResidualData( pcCU, uiAbsPartIdx, uiSeg );
871  }
872}
873#endif
874
875//! \}
Note: See TracBrowser for help on using the repository browser.