source: 3DVCSoftware/branches/HTM-14.1-update-dev2-Sharp/source/Lib/TLibEncoder/TEncEntropy.cpp @ 1225

Last change on this file since 1225 was 1225, checked in by sharpjp-htm, 9 years ago

Migration of illumination compensation(NH_3D_IC) and small fix(H_MV_ENC_DEC_TRAC_FIX)

  • Property svn:eol-style set to native
File size: 26.1 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-2015, 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/CommonDef.h"
40#include "TLibCommon/TComSampleAdaptiveOffset.h"
41#include "TLibCommon/TComTU.h"
42
43#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
44#include "../TLibCommon/Debug.h"
45static const Bool bDebugPredEnabled = DebugOptionList::DebugPred.getInt()!=0;
46#endif
47
48//! \ingroup TLibEncoder
49//! \{
50
51Void TEncEntropy::setEntropyCoder ( TEncEntropyIf* e )
52{
53  m_pcEntropyCoderIf = e;
54}
55
56Void TEncEntropy::encodeSliceHeader ( TComSlice* pcSlice )
57{
58  m_pcEntropyCoderIf->codeSliceHeader( pcSlice );
59  return;
60}
61
62Void  TEncEntropy::encodeTilesWPPEntryPoint( TComSlice* pSlice )
63{
64  m_pcEntropyCoderIf->codeTilesWPPEntryPoint( pSlice );
65}
66
67Void TEncEntropy::encodeTerminatingBit      ( UInt uiIsLast )
68{
69  m_pcEntropyCoderIf->codeTerminatingBit( uiIsLast );
70
71  return;
72}
73
74Void TEncEntropy::encodeSliceFinish()
75{
76  m_pcEntropyCoderIf->codeSliceFinish();
77}
78
79Void TEncEntropy::encodePPS( const TComPPS* pcPPS )
80{
81  m_pcEntropyCoderIf->codePPS( pcPPS );
82  return;
83}
84
85Void TEncEntropy::encodeSPS( const TComSPS* pcSPS )
86{
87  m_pcEntropyCoderIf->codeSPS( pcSPS );
88  return;
89}
90
91Void TEncEntropy::encodeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
92{
93  if( bRD )
94  {
95    uiAbsPartIdx = 0;
96  }
97  m_pcEntropyCoderIf->codeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
98}
99
100Void TEncEntropy::encodeVPS( const TComVPS* pcVPS )
101{
102  m_pcEntropyCoderIf->codeVPS( pcVPS );
103  return;
104}
105
106Void TEncEntropy::encodeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
107{
108  if ( pcCU->getSlice()->isIntra() )
109  {
110    return;
111  }
112  if( bRD )
113  {
114    uiAbsPartIdx = 0;
115  }
116  m_pcEntropyCoderIf->codeSkipFlag( pcCU, uiAbsPartIdx );
117}
118#if H_3D
119Void TEncEntropy::encodeDIS( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
120{
121  if ( !pcCU->getSlice()->getIsDepth() )
122  {
123    return;
124  }
125  if( bRD )
126  {
127    uiAbsPartIdx = 0;
128  }
129  m_pcEntropyCoderIf->codeDIS( pcCU, uiAbsPartIdx );
130}
131#endif
132//! encode merge flag
133Void TEncEntropy::encodeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
134{
135  // at least one merge candidate exists
136  m_pcEntropyCoderIf->codeMergeFlag( pcCU, uiAbsPartIdx );
137}
138
139//! encode merge index
140Void TEncEntropy::encodeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
141{
142  if( bRD )
143  {
144    uiAbsPartIdx = 0;
145    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
146  }
147  m_pcEntropyCoderIf->codeMergeIndex( pcCU, uiAbsPartIdx );
148}
149
150#if NH_3D_IC
151Void TEncEntropy::encodeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
152{
153#if H_3D_ARP
154  if ( pcCU->isIntra( uiAbsPartIdx ) || ( pcCU->getSlice()->getViewIndex() == 0 ) || pcCU->getSlice()->getIsDepth() || pcCU->getARPW( uiAbsPartIdx ) > 0 )
155#else
156  if ( pcCU->isIntra( uiAbsPartIdx ) || ( pcCU->getSlice()->getViewIndex() == 0 ) || pcCU->getSlice()->getIsDepth() )
157#endif
158  {
159    return;
160  }
161
162  if( !pcCU->getSlice()->getApplyIC() )
163  {
164    return;
165  }
166
167  if( bRD )
168  {
169    uiAbsPartIdx = 0;
170  }
171  else
172  {
173    Int ICEnableCandidate = pcCU->getSlice()->getICEnableCandidate(pcCU->getSlice()->getDepth());
174    Int ICEnableNum = pcCU->getSlice()->getICEnableNum(pcCU->getSlice()->getDepth());
175    ICEnableCandidate++;
176    if(pcCU->getICFlag(uiAbsPartIdx))
177    {
178      ICEnableNum++;
179    }
180    pcCU->getSlice()->setICEnableCandidate(pcCU->getSlice()->getDepth(), ICEnableCandidate);
181    pcCU->getSlice()->setICEnableNum(pcCU->getSlice()->getDepth(), ICEnableNum);
182  }
183  if( pcCU->isICFlagRequired( uiAbsPartIdx ) )
184  {
185    m_pcEntropyCoderIf->codeICFlag( pcCU, uiAbsPartIdx );
186  }
187}
188#endif
189
190#if H_3D_ARP
191Void TEncEntropy::encodeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
192{
193  if( !pcCU->getSlice()->getARPStepNum() || pcCU->isIntra( uiAbsPartIdx ) ) 
194  {
195    return;
196  }
197
198  if ( pcCU->getPartitionSize(uiAbsPartIdx)!=SIZE_2Nx2N )
199  {
200    assert(pcCU->getARPW (uiAbsPartIdx) == 0);
201  }
202  else
203  {
204    m_pcEntropyCoderIf->codeARPW( pcCU, uiAbsPartIdx );
205  }
206}
207#endif
208
209
210//! encode prediction mode
211Void TEncEntropy::encodePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
212{
213  if( bRD )
214  {
215    uiAbsPartIdx = 0;
216  }
217
218  if ( pcCU->getSlice()->isIntra() )
219  {
220    return;
221  }
222
223  m_pcEntropyCoderIf->codePredMode( pcCU, uiAbsPartIdx );
224}
225
226//! encode split flag
227Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
228{
229  if( bRD )
230  {
231    uiAbsPartIdx = 0;
232  }
233
234  m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
235}
236
237//! encode partition size
238Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
239{
240  if( bRD )
241  {
242    uiAbsPartIdx = 0;
243  }
244
245  m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth );
246}
247
248
249/** Encode I_PCM information.
250 * \param pcCU          pointer to CU
251 * \param uiAbsPartIdx  CU index
252 * \param bRD           flag indicating estimation or encoding
253 */
254Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
255{
256  if(!pcCU->getSlice()->getSPS()->getUsePCM()
257    || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize())
258    || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize()))
259  {
260    return;
261  }
262
263  if( bRD )
264  {
265    uiAbsPartIdx = 0;
266  }
267
268  m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx );
269
270}
271
272Void TEncEntropy::xEncodeTransform( Bool& bCodeDQP, Bool& codeChromaQpAdj, TComTU &rTu )
273{
274
275//pcCU, absPartIdxCU, uiAbsPartIdx, uiDepth+1, uiTrIdx+1, quadrant,
276  TComDataCU *pcCU=rTu.getCU();
277  const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU();
278
279  const UInt numValidComponent = pcCU->getPic()->getNumberValidComponents();
280  const Bool bChroma = isChromaEnabled(pcCU->getPic()->getChromaFormat());
281  const UInt uiTrIdx = rTu.GetTransformDepthRel();
282  const UInt uiDepth = rTu.GetTransformDepthTotal();
283#if H_MV_ENC_DEC_TRAC
284#if ENC_DEC_TRACE
285  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
286  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
287
288  DTRACE_TU_S("=========== transform_tree ===========\n")
289    DTRACE_TU("x0", uiLPelX)
290    DTRACE_TU("x1", uiTPelY)
291    DTRACE_TU("log2TrafoSize", pcCU->getSlice()->getSPS()->getMaxCUWidth()  >> uiDepth )
292    DTRACE_TU("trafoDepth"  , uiDepth)
293#endif
294#endif
295
296#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
297  const Bool bDebugRQT=pcCU->getSlice()->getFinalized() && DebugOptionList::DebugRQT.getInt()!=0;
298  if (bDebugRQT)
299  {
300    printf("x..codeTransform: offsetLuma=%d offsetChroma=%d absPartIdx=%d, uiDepth=%d\n width=%d, height=%d, uiTrIdx=%d, uiInnerQuadIdx=%d\n",
301           rTu.getCoefficientOffset(COMPONENT_Y), rTu.getCoefficientOffset(COMPONENT_Cb), uiAbsPartIdx, uiDepth, rTu.getRect(COMPONENT_Y).width, rTu.getRect(COMPONENT_Y).height, rTu.GetTransformDepthRel(), rTu.GetSectionNumber());
302  }
303#endif
304  const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) > uiTrIdx;// + pcCU->getDepth( uiAbsPartIdx ) > uiDepth;
305  const UInt uiLog2TrafoSize = rTu.GetLog2LumaTrSize();
306
307
308  UInt cbf[MAX_NUM_COMPONENT] = {0,0,0};
309  Bool bHaveACodedBlock       = false;
310  Bool bHaveACodedChromaBlock = false;
311
312  for(UInt ch=0; ch<numValidComponent; ch++)
313  {
314    const ComponentID compID = ComponentID(ch);
315
316    cbf[compID] = pcCU->getCbf( uiAbsPartIdx, compID , uiTrIdx );
317   
318    if (cbf[ch] != 0)
319    {
320      bHaveACodedBlock = true;
321      if (isChroma(compID))
322      {
323        bHaveACodedChromaBlock = true;
324      }
325    }
326  }
327
328  if( pcCU->isIntra(uiAbsPartIdx) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) )
329  {
330    assert( uiSubdiv );
331  }
332  else if( pcCU->isInter(uiAbsPartIdx) && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) &&  (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) )
333  {
334    if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
335    {
336      assert( uiSubdiv );
337    }
338    else
339    {
340      assert(!uiSubdiv );
341    }
342  }
343  else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
344  {
345    assert( uiSubdiv );
346  }
347  else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
348  {
349    assert( !uiSubdiv );
350  }
351  else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
352  {
353    assert( !uiSubdiv );
354  }
355  else
356  {
357    assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
358    m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, 5 - uiLog2TrafoSize );
359  }
360
361  const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx );
362  const Bool bFirstCbfOfCU = uiTrDepthCurr == 0;
363
364  for(UInt ch=COMPONENT_Cb; ch<numValidComponent; ch++)
365  {
366    const ComponentID compID=ComponentID(ch);
367    if( bFirstCbfOfCU || rTu.ProcessingAllQuadrants(compID) )
368    {
369      if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, compID, uiTrDepthCurr - 1 ) )
370      {
371        m_pcEntropyCoderIf->codeQtCbf( rTu, compID, (uiSubdiv == 0) );
372      }
373    }
374    else
375    {
376      assert( pcCU->getCbf( uiAbsPartIdx, compID, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, compID, uiTrDepthCurr - 1 ) );
377    }
378  }
379
380  if( uiSubdiv )
381  {
382    TComTURecurse tuRecurseChild(rTu, true);
383    do
384    {
385      xEncodeTransform( bCodeDQP, codeChromaQpAdj, tuRecurseChild );
386    } while (tuRecurseChild.nextSection(rTu));
387  }
388  else
389  {
390#if !H_MV_ENC_DEC_TRAC
391    {
392      DTRACE_CABAC_VL( g_nSymbolCounter++ );
393      DTRACE_CABAC_T( "\tTrIdx: abspart=" );
394      DTRACE_CABAC_V( uiAbsPartIdx );
395      DTRACE_CABAC_T( "\tdepth=" );
396      DTRACE_CABAC_V( uiDepth );
397      DTRACE_CABAC_T( "\ttrdepth=" );
398      DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) );
399      DTRACE_CABAC_T( "\n" );
400    }
401#endif
402    if( !pcCU->isIntra(uiAbsPartIdx) && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && (!bChroma || (!pcCU->getCbf( uiAbsPartIdx, COMPONENT_Cb, 0 ) && !pcCU->getCbf( uiAbsPartIdx, COMPONENT_Cr, 0 ) ) ) )
403    {
404      assert( pcCU->getCbf( uiAbsPartIdx, COMPONENT_Y, 0 ) );
405      //      printf( "saved one bin! " );
406    }
407    else
408    {
409      m_pcEntropyCoderIf->codeQtCbf( rTu, COMPONENT_Y, true ); //luma CBF is always at the lowest level
410    }
411
412    if ( bHaveACodedBlock )
413    {
414      // dQP: only for CTU once
415      if ( pcCU->getSlice()->getPPS()->getUseDQP() )
416      {
417        if ( bCodeDQP )
418        {
419          encodeQP( pcCU, rTu.GetAbsPartIdxCU() );
420          bCodeDQP = false;
421        }
422      }
423
424      if ( pcCU->getSlice()->getUseChromaQpAdj() )
425      {
426        if ( bHaveACodedChromaBlock && codeChromaQpAdj && !pcCU->getCUTransquantBypass(rTu.GetAbsPartIdxCU()) )
427        {
428          encodeChromaQpAdjustment( pcCU, rTu.GetAbsPartIdxCU() );
429          codeChromaQpAdj = false;
430        }
431      }
432
433      const UInt numValidComp=pcCU->getPic()->getNumberValidComponents();
434
435      for(UInt ch=COMPONENT_Y; ch<numValidComp; ch++)
436      {
437        const ComponentID compID=ComponentID(ch);
438
439        if (rTu.ProcessComponentSection(compID))
440        {
441#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
442          if (bDebugRQT)
443          {
444            printf("Call NxN for chan %d width=%d height=%d cbf=%d\n", compID, rTu.getRect(compID).width, rTu.getRect(compID).height, 1);
445          }
446#endif
447
448          if (rTu.getRect(compID).width != rTu.getRect(compID).height)
449          {
450            //code two sub-TUs
451            TComTURecurse subTUIterator(rTu, false, TComTU::VERTICAL_SPLIT, true, compID);
452
453            do
454            {
455              const UChar subTUCBF = pcCU->getCbf(subTUIterator.GetAbsPartIdxTU(compID), compID, (uiTrIdx + 1));
456
457              if (subTUCBF != 0)
458              {
459#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
460                if (bDebugRQT)
461                {
462                  printf("Call NxN for chan %d width=%d height=%d cbf=%d\n", compID, subTUIterator.getRect(compID).width, subTUIterator.getRect(compID).height, 1);
463                }
464#endif
465                m_pcEntropyCoderIf->codeCoeffNxN( subTUIterator, (pcCU->getCoeff(compID) + subTUIterator.getCoefficientOffset(compID)), compID );
466              }
467            }
468            while (subTUIterator.nextSection(rTu));
469          }
470          else
471          {
472            if (isChroma(compID) && (cbf[COMPONENT_Y] != 0))
473            {
474              m_pcEntropyCoderIf->codeCrossComponentPrediction( rTu, compID );
475            }
476
477            if (cbf[compID] != 0)
478            {
479              m_pcEntropyCoderIf->codeCoeffNxN( rTu, (pcCU->getCoeff(compID) + rTu.getCoefficientOffset(compID)), compID );
480            }
481          }
482        }
483      }
484    }
485  }
486}
487
488
489//! encode intra direction for luma
490Void TEncEntropy::encodeIntraDirModeLuma  ( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiplePU )
491{
492  m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, absPartIdx , isMultiplePU);
493}
494
495
496//! encode intra direction for chroma
497Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
498{
499  m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx );
500
501#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
502  if (bDebugPredEnabled && pcCU->getSlice()->getFinalized())
503  {
504    UInt cdir=pcCU->getIntraDir(CHANNEL_TYPE_CHROMA, uiAbsPartIdx);
505    if (cdir==36)
506    {
507      cdir=pcCU->getIntraDir(CHANNEL_TYPE_LUMA, uiAbsPartIdx);
508    }
509    printf("coding chroma Intra dir: %d, uiAbsPartIdx: %d, luma dir: %d\n", cdir, uiAbsPartIdx, pcCU->getIntraDir(CHANNEL_TYPE_LUMA, uiAbsPartIdx));
510  }
511#endif
512}
513
514
515Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
516{
517  if( pcCU->isIntra( uiAbsPartIdx ) )                                 // If it is Intra mode, encode intra prediction mode.
518  {
519    encodeIntraDirModeLuma  ( pcCU, uiAbsPartIdx,true );
520#if H_3D_DIM_SDC
521    if(!pcCU->getSDCFlag(uiAbsPartIdx))
522#endif
523    if (pcCU->getPic()->getChromaFormat()!=CHROMA_400)
524    {
525      encodeIntraDirModeChroma( pcCU, uiAbsPartIdx );
526
527      if (enable4ChromaPUsInIntraNxNCU(pcCU->getPic()->getChromaFormat()) && pcCU->getPartitionSize( uiAbsPartIdx )==SIZE_NxN)
528      {
529        UInt uiPartOffset = ( pcCU->getPic()->getNumPartitionsInCtu() >> ( pcCU->getDepth(uiAbsPartIdx) << 1 ) ) >> 2;
530        encodeIntraDirModeChroma( pcCU, uiAbsPartIdx + uiPartOffset   );
531        encodeIntraDirModeChroma( pcCU, uiAbsPartIdx + uiPartOffset*2 );
532        encodeIntraDirModeChroma( pcCU, uiAbsPartIdx + uiPartOffset*3 );
533      }
534    }
535  }
536  else                                                                // if it is Inter mode, encode motion vector and reference index
537  {
538    encodePUWise( pcCU, uiAbsPartIdx );
539  }
540}
541
542Void TEncEntropy::encodeCrossComponentPrediction( TComTU &rTu, ComponentID compID )
543{
544  m_pcEntropyCoderIf->codeCrossComponentPrediction( rTu, compID );
545}
546
547//! encode motion information for every PU block
548Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx )
549{
550#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
551  const Bool bDebugPred = bDebugPredEnabled && pcCU->getSlice()->getFinalized();
552#endif
553
554  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
555  UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) );
556  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
557  UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxTotalCUDepth() - uiDepth ) << 1 ) ) >> 4;
558
559  for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset )
560  {
561#if H_MV_ENC_DEC_TRAC
562    DTRACE_PU_S("=========== prediction_unit ===========\n")
563       //Todo:
564      //DTRACE_PU("x0", uiLPelX)
565      //DTRACE_PU("x1", uiTPelY)
566#endif
567    encodeMergeFlag( pcCU, uiSubPartIdx );
568    if ( pcCU->getMergeFlag( uiSubPartIdx ) )
569    {
570      encodeMergeIndex( pcCU, uiSubPartIdx );
571#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
572      if (bDebugPred)
573      {
574        std::cout << "Coded merge flag, CU absPartIdx: " << uiAbsPartIdx << " PU(" << uiPartIdx << ") absPartIdx: " << uiSubPartIdx;
575        std::cout << " merge index: " << (UInt)pcCU->getMergeIndex(uiSubPartIdx) << std::endl;
576      }
577#endif
578    }
579    else
580    {
581      encodeInterDirPU( pcCU, uiSubPartIdx );
582      for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
583      {
584        if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
585        {
586          encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
587          encodeMvdPU       ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
588          encodeMVPIdxPU    ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
589#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
590          if (bDebugPred)
591          {
592            std::cout << "refListIdx: " << uiRefListIdx << std::endl;
593            std::cout << "MVD horizontal: " << pcCU->getCUMvField(RefPicList(uiRefListIdx))->getMvd( uiAbsPartIdx ).getHor() << std::endl;
594            std::cout << "MVD vertical:   " << pcCU->getCUMvField(RefPicList(uiRefListIdx))->getMvd( uiAbsPartIdx ).getVer() << std::endl;
595            std::cout << "MVPIdxPU: " << pcCU->getMVPIdx(RefPicList( uiRefListIdx ), uiSubPartIdx) << std::endl;
596            std::cout << "InterDir: " << (UInt)pcCU->getInterDir(uiSubPartIdx) << std::endl;
597          }
598#endif
599        }
600      }
601    }
602  }
603
604  return;
605}
606
607Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx )
608{
609  if ( !pcCU->getSlice()->isInterB() )
610  {
611    return;
612  }
613
614  m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx );
615
616  return;
617}
618
619//! encode reference frame index for a PU block
620Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
621{
622  assert( pcCU->isInter( uiAbsPartIdx ) );
623
624  if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) )
625  {
626    return;
627  }
628
629  if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
630  {
631    m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList );
632  }
633
634  return;
635}
636
637//! encode motion vector difference for a PU block
638Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
639{
640  assert( pcCU->isInter( uiAbsPartIdx ) );
641
642  if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
643  {
644    m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList );
645  }
646  return;
647}
648
649Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
650{
651  if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) )
652  {
653    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList );
654  }
655
656  return;
657}
658
659Void TEncEntropy::encodeQtCbf( TComTU &rTu, const ComponentID compID, const Bool lowestLevel )
660{
661  m_pcEntropyCoderIf->codeQtCbf( rTu, compID, lowestLevel );
662}
663
664Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
665{
666  m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx );
667}
668
669Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
670{
671  m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
672}
673
674Void TEncEntropy::encodeQtCbfZero( TComTU &rTu, const ChannelType chType )
675{
676  m_pcEntropyCoderIf->codeQtCbfZero( rTu, chType );
677}
678
679Void TEncEntropy::encodeQtRootCbfZero( )
680{
681  m_pcEntropyCoderIf->codeQtRootCbfZero( );
682}
683
684// dQP
685Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
686{
687  if( bRD )
688  {
689    uiAbsPartIdx = 0;
690  }
691
692  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
693  {
694    m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx );
695  }
696}
697
698//! encode chroma qp adjustment
699Void TEncEntropy::encodeChromaQpAdjustment( TComDataCU* cu, UInt absPartIdx, Bool inRd )
700{
701  if( inRd )
702  {
703    absPartIdx = 0;
704  }
705
706  m_pcEntropyCoderIf->codeChromaQpAdjustment( cu, absPartIdx );
707}
708
709// texture
710
711//! encode coefficients
712Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool& bCodeDQP, Bool& codeChromaQpAdj )
713{
714
715#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
716  const Bool bDebugRQT=pcCU->getSlice()->getFinalized() && DebugOptionList::DebugRQT.getInt()!=0;
717#endif
718
719#if H_3D_DIM_SDC
720  if( pcCU->getSDCFlag( uiAbsPartIdx ) && pcCU->isIntra( uiAbsPartIdx ) )
721  {
722    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
723    assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
724    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
725    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
726    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
727  }
728
729
730  if( pcCU->getSDCFlag( uiAbsPartIdx ) && !pcCU->isIntra( uiAbsPartIdx ) )
731  {
732    assert( !pcCU->isSkipped( uiAbsPartIdx ) );
733    assert( !pcCU->isIntra( uiAbsPartIdx) );
734    assert( pcCU->getSlice()->getIsDepth() );
735  }
736
737  if( pcCU->getSlice()->getIsDepth() && ( pcCU->getSDCFlag( uiAbsPartIdx ) || pcCU->isIntra( uiAbsPartIdx ) ) )
738  {
739    Int iPartNum = ( pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_NxN ) ? 4 : 1;
740    UInt uiPartOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth( uiAbsPartIdx ) << 1 ) ) >> 2;
741   
742    if( !pcCU->getSDCFlag( uiAbsPartIdx ) )
743    {
744      for( Int iPart = 0; iPart < iPartNum; iPart++ )
745      {
746        if( getDimType( pcCU->getLumaIntraDir( uiAbsPartIdx + uiPartOffset*iPart ) ) < DIM_NUM_TYPE ) 
747        {
748          m_pcEntropyCoderIf->codeDeltaDC( pcCU, uiAbsPartIdx + uiPartOffset*iPart );
749        }
750      }
751    }
752    else
753    {
754      m_pcEntropyCoderIf->codeDeltaDC( pcCU, uiAbsPartIdx );
755      return;
756    }
757  }
758#endif
759
760
761  if( pcCU->isIntra(uiAbsPartIdx) )
762  {
763#if !NH_MV
764    if (false)
765    {
766      DTRACE_CABAC_VL( g_nSymbolCounter++ )
767      DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" )
768      DTRACE_CABAC_V( uiDepth )
769      DTRACE_CABAC_T( "\n" )
770    }
771#endif
772  }
773  else
774  {
775    if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) )
776    {
777      m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
778    }
779    if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) )
780    {
781      return;
782    }
783  }
784
785  TComTURecurse tuRecurse(pcCU, uiAbsPartIdx, uiDepth);
786#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
787  if (bDebugRQT)
788  {
789    printf("..codeCoeff: uiAbsPartIdx=%d, PU format=%d, 2Nx2N=%d, NxN=%d\n", uiAbsPartIdx, pcCU->getPartitionSize(uiAbsPartIdx), SIZE_2Nx2N, SIZE_NxN);
790  }
791#endif
792
793  xEncodeTransform( bCodeDQP, codeChromaQpAdj, tuRecurse );
794}
795
796Void TEncEntropy::encodeCoeffNxN( TComTU &rTu, TCoeff* pcCoef, const ComponentID compID)
797{
798  TComDataCU *pcCU = rTu.getCU();
799
800  if (pcCU->getCbf(rTu.GetAbsPartIdxTU(), compID, rTu.GetTransformDepthRel()) != 0)
801  {
802    if (rTu.getRect(compID).width != rTu.getRect(compID).height)
803    {
804      //code two sub-TUs
805      TComTURecurse subTUIterator(rTu, false, TComTU::VERTICAL_SPLIT, true, compID);
806
807      const UInt subTUSize = subTUIterator.getRect(compID).width * subTUIterator.getRect(compID).height;
808
809      do
810      {
811        const UChar subTUCBF = pcCU->getCbf(subTUIterator.GetAbsPartIdxTU(compID), compID, (subTUIterator.GetTransformDepthRel() + 1));
812
813        if (subTUCBF != 0)
814        {
815          m_pcEntropyCoderIf->codeCoeffNxN( subTUIterator, (pcCoef + (subTUIterator.GetSectionNumber() * subTUSize)), compID);
816        }
817      }
818      while (subTUIterator.nextSection(rTu));
819    }
820    else
821    {
822      m_pcEntropyCoderIf->codeCoeffNxN(rTu, pcCoef, compID);
823    }
824  }
825}
826
827Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, const ChannelType chType)
828{
829  const UInt heightAtEntropyCoding = (width != height) ? (height >> 1) : height;
830
831  m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, heightAtEntropyCoding, chType );
832}
833
834Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize )
835{
836  Int count = 0;
837
838  for ( Int i = 0; i < uiSize; i++ )
839  {
840    count += pcCoef[i] != 0;
841  }
842
843  return count;
844}
845
846#if H_3D_INTER_SDC
847Void TEncEntropy::encodeDeltaDC  ( TComDataCU* pcCU, UInt absPartIdx )
848{
849  m_pcEntropyCoderIf->codeDeltaDC( pcCU, absPartIdx );
850}
851
852Void TEncEntropy::encodeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
853{
854  if( ( !pcCU->isIntra( uiAbsPartIdx ) && !pcCU->getSlice()->getInterSdcFlag() ) || 
855    ( pcCU->isIntra( uiAbsPartIdx ) && !pcCU->getSlice()->getIntraSdcWedgeFlag() ) )
856  {
857    return;
858  }
859
860  if( !pcCU->getSlice()->getIsDepth() || pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_2Nx2N || pcCU->isSkipped( uiAbsPartIdx ) )
861  {
862    return;
863  }
864
865  assert( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N || ( !pcCU->isIntra( uiAbsPartIdx ) && !pcCU->isSkipped( uiAbsPartIdx ) ) );
866
867  if( bRD )
868  {
869    uiAbsPartIdx = 0;
870  }
871
872  m_pcEntropyCoderIf->codeSDCFlag( pcCU, uiAbsPartIdx );
873}
874
875#endif
876#if H_3D_DBBP
877Void TEncEntropy::encodeDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
878{
879  if( pcCU->getSlice()->getDepthBasedBlkPartFlag() && 
880    ( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2NxN || 
881      pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_Nx2N) && 
882      pcCU->getWidth(uiAbsPartIdx) > 8 && 
883      pcCU->getSlice()->getDefaultRefViewIdxAvailableFlag() )
884  {
885    if( bRD )
886    {
887      uiAbsPartIdx = 0;
888    }
889    m_pcEntropyCoderIf->codeDBBPFlag( pcCU, uiAbsPartIdx );
890  }
891}
892#endif
893
894//! \}
Note: See TracBrowser for help on using the repository browser.