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

Last change on this file since 1039 was 1039, checked in by tech, 11 years ago

Merged 11.2-dev0@1038.

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