source: 3DVCSoftware/branches/HTM-6.2-dev0/source/Lib/TLibEncoder/TEncEntropy.cpp @ 433

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

Merged HTM-6.2-dev3-RWTH-Fix Rev. 415

  • Property svn:eol-style set to native
File size: 55.0 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-2012, 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/TComAdaptiveLoopFilter.h"
41#include "TLibCommon/TComSampleAdaptiveOffset.h"
42
43//! \ingroup TLibEncoder
44//! \{
45
46Void TEncEntropy::setEntropyCoder ( TEncEntropyIf* e, TComSlice* pcSlice )
47{
48  m_pcEntropyCoderIf = e;
49  m_pcEntropyCoderIf->setSlice ( pcSlice );
50}
51
52Void TEncEntropy::encodeSliceHeader ( TComSlice* pcSlice )
53{
54  if (pcSlice->getSPS()->getUseSAO())
55  {
56#if LGE_SAO_MIGRATION_D0091
57    if (pcSlice->getSPS()->getUseSAO())
58    {
59        SAOParam *saoParam = pcSlice->getAPS()->getSaoParam();
60        pcSlice->setSaoEnabledFlag     (saoParam->bSaoFlag[0]);
61        {
62            pcSlice->setSaoEnabledFlagChroma   (saoParam->bSaoFlag[1]);
63        }
64    }
65#else
66    pcSlice->setSaoInterleavingFlag(pcSlice->getAPS()->getSaoInterleavingFlag());
67    pcSlice->setSaoEnabledFlag     (pcSlice->getAPS()->getSaoParam()->bSaoFlag[0]);
68    if (pcSlice->getAPS()->getSaoInterleavingFlag())
69    {
70      pcSlice->setSaoEnabledFlagCb   (pcSlice->getAPS()->getSaoParam()->bSaoFlag[1]);
71      pcSlice->setSaoEnabledFlagCr   (pcSlice->getAPS()->getSaoParam()->bSaoFlag[2]);
72    }
73    else
74    {
75      pcSlice->setSaoEnabledFlagCb   (0);
76      pcSlice->setSaoEnabledFlagCr   (0);
77    }
78#endif
79  }
80
81  m_pcEntropyCoderIf->codeSliceHeader( pcSlice );
82  return;
83}
84
85Void  TEncEntropy::encodeTilesWPPEntryPoint( TComSlice* pSlice )
86{
87  m_pcEntropyCoderIf->codeTilesWPPEntryPoint( pSlice );
88}
89
90Void TEncEntropy::encodeTerminatingBit      ( UInt uiIsLast )
91{
92  m_pcEntropyCoderIf->codeTerminatingBit( uiIsLast );
93 
94  return;
95}
96
97Void TEncEntropy::encodeSliceFinish()
98{
99  m_pcEntropyCoderIf->codeSliceFinish();
100}
101
102Void TEncEntropy::encodeFlush()
103{
104  m_pcEntropyCoderIf->codeFlush();
105}
106Void TEncEntropy::encodeStart()
107{
108  m_pcEntropyCoderIf->encodeStart();
109}
110
111Void TEncEntropy::encodeSEI(const SEI& sei)
112{
113  m_pcEntropyCoderIf->codeSEI(sei);
114  return;
115}
116
117Void TEncEntropy::encodePPS( TComPPS* pcPPS )
118{
119  m_pcEntropyCoderIf->codePPS( pcPPS );
120  return;
121}
122
123#if VIDYO_VPS_INTEGRATION|QC_MVHEVC_B0046
124Void TEncEntropy::encodeVPS( TComVPS* pcVPS )
125{
126  m_pcEntropyCoderIf->codeVPS( pcVPS );
127  return;
128}
129#endif
130
131#if VIDYO_VPS_INTEGRATION|QC_MVHEVC_B0046
132Void  codeVPS                 ( TComVPS* pcVPS );
133#endif
134
135#if HHI_MPI || H3D_QTL
136Void TEncEntropy::encodeSPS( TComSPS* pcSPS, Bool bIsDepth )
137{
138  m_pcEntropyCoderIf->codeSPS( pcSPS, bIsDepth );
139  return;
140}
141#else
142Void TEncEntropy::encodeSPS( TComSPS* pcSPS )
143{
144  m_pcEntropyCoderIf->codeSPS( pcSPS );
145  return;
146}
147#endif
148
149Void TEncEntropy::encodeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
150{
151  if ( pcCU->getSlice()->isIntra() )
152  {
153    return;
154  }
155  if( bRD )
156  {
157    uiAbsPartIdx = 0;
158  }
159  if( !bRD )
160  {
161    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
162    {
163      return;
164    }
165  }
166  m_pcEntropyCoderIf->codeSkipFlag( pcCU, uiAbsPartIdx );
167}
168
169#if LGE_ILLUCOMP_B0045
170Void TEncEntropy::encodeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD
171#if LGE_ILLUCOMP_DEPTH_C0046
172    , UInt uiDepth
173#endif
174    )
175{
176  if (pcCU->isIntra(uiAbsPartIdx) || (pcCU->getSlice()->getViewId() == 0)
177#if !LGE_ILLUCOMP_DEPTH_C0046
178      || pcCU->getSlice()->getSPS()->isDepth()
179#endif
180      )
181  {
182    return;
183  }
184
185  if(!pcCU->getSlice()->getApplyIC())
186    return;
187
188  if( bRD )
189  {
190    uiAbsPartIdx = 0;
191  }
192
193  if(pcCU->isICFlagRequired(uiAbsPartIdx
194#if LGE_ILLUCOMP_DEPTH_C0046
195      , uiDepth //This modification is not needed after integrating JCT3V-C0137
196#endif
197      ))
198    m_pcEntropyCoderIf->codeICFlag( pcCU, uiAbsPartIdx );
199}
200#endif
201
202Void TEncEntropy::codeFiltCountBit(ALFParam* pAlfParam, Int64* ruiRate)
203{
204  resetEntropy();
205  resetBits();
206  codeFilt(pAlfParam);
207  *ruiRate = getNumberOfWrittenBits();
208  resetEntropy();
209  resetBits();
210}
211
212Void TEncEntropy::codeAuxCountBit(ALFParam* pAlfParam, Int64* ruiRate)
213{
214  resetEntropy();
215  resetBits();
216  codeAux(pAlfParam);
217  *ruiRate = getNumberOfWrittenBits();
218  resetEntropy();
219  resetBits();
220}
221
222Void TEncEntropy::codeAux(ALFParam* pAlfParam)
223{
224  //  m_pcEntropyCoderIf->codeAlfUvlc(pAlfParam->realfiltNo);
225
226  Int noFilters = min(pAlfParam->filters_per_group-1, 2);
227  m_pcEntropyCoderIf->codeAlfUvlc(noFilters);
228
229  if(noFilters == 1)
230  {
231    m_pcEntropyCoderIf->codeAlfUvlc(pAlfParam->startSecondFilter);
232  }
233  else if (noFilters == 2)
234  {
235    Int numMergeFlags = 16;
236    for (Int i=1; i<numMergeFlags; i++) 
237    {
238      m_pcEntropyCoderIf->codeAlfFlag (pAlfParam->filterPattern[i]);
239    }
240  }
241}
242
243Int TEncEntropy::lengthGolomb(int coeffVal, int k)
244{
245  int m = 2 << (k - 1);
246  int q = coeffVal / m;
247  if(coeffVal != 0)
248  {
249    return(q + 2 + k);
250  }
251  else
252  {
253    return(q + 1 + k);
254  }
255}
256
257Int TEncEntropy::codeFilterCoeff(ALFParam* ALFp)
258{
259  Int filters_per_group = ALFp->filters_per_group;
260  int sqrFiltLength = ALFp->num_coeff;
261  int i, k, kMin, kStart, minBits, ind, scanPos, maxScanVal, coeffVal, len = 0,
262    *pDepthInt=NULL, kMinTab[MAX_SCAN_VAL], bitsCoeffScan[MAX_SCAN_VAL][MAX_EXP_GOLOMB],
263    minKStart, minBitsKStart, bitsKStart;
264 
265  pDepthInt = pDepthIntTabShapes[ALFp->filter_shape];
266  maxScanVal = 0;
267  int minScanVal = MIN_SCAN_POS_CROSS;
268
269  for(i = 0; i < sqrFiltLength; i++)
270  {
271    maxScanVal = max(maxScanVal, pDepthInt[i]);
272  }
273 
274  // vlc for all
275  memset(bitsCoeffScan, 0, MAX_SCAN_VAL * MAX_EXP_GOLOMB * sizeof(int));
276  for(ind=0; ind<filters_per_group; ++ind)
277  {
278    for(i = 0; i < sqrFiltLength; i++)
279    {
280      scanPos=pDepthInt[i]-1;
281      coeffVal=abs(ALFp->coeffmulti[ind][i]);
282      for (k=1; k<15; k++)
283      {
284        bitsCoeffScan[scanPos][k]+=lengthGolomb(coeffVal, k);
285      }
286    }
287  }
288 
289  minBitsKStart = 0;
290  minKStart = -1;
291  for(k = 1; k < 8; k++)
292  { 
293    bitsKStart = 0; 
294    kStart = k;
295    for(scanPos = minScanVal; scanPos < maxScanVal; scanPos++)
296    {
297      kMin = kStart; 
298      minBits = bitsCoeffScan[scanPos][kMin];
299     
300      if(bitsCoeffScan[scanPos][kStart+1] < minBits)
301      {
302        kMin = kStart + 1; 
303        minBits = bitsCoeffScan[scanPos][kMin];
304      }
305      kStart = kMin;
306      bitsKStart += minBits;
307    }
308    if((bitsKStart < minBitsKStart) || (k == 1))
309    {
310      minBitsKStart = bitsKStart;
311      minKStart = k;
312    }
313  }
314 
315  kStart = minKStart; 
316  for(scanPos = minScanVal; scanPos < maxScanVal; scanPos++)
317  {
318    kMin = kStart; 
319    minBits = bitsCoeffScan[scanPos][kMin];
320   
321    if(bitsCoeffScan[scanPos][kStart+1] < minBits)
322    {
323      kMin = kStart + 1; 
324      minBits = bitsCoeffScan[scanPos][kMin];
325    }
326   
327    kMinTab[scanPos] = kMin;
328    kStart = kMin;
329  }
330 
331  // Coding parameters
332  ALFp->minKStart = minKStart;
333  for(scanPos = minScanVal; scanPos < maxScanVal; scanPos++)
334  {
335    ALFp->kMinTab[scanPos] = kMinTab[scanPos];
336  }
337
338  if (ALFp->filters_per_group == 1)
339  {
340    len += writeFilterCoeffs(sqrFiltLength, filters_per_group, pDepthInt, ALFp->coeffmulti, kTableTabShapes[ALF_CROSS9x7_SQUARE3x3]);
341  }
342  else
343  {
344  len += writeFilterCodingParams(minKStart, minScanVal, maxScanVal, kMinTab);
345
346  // Filter coefficients
347  len += writeFilterCoeffs(sqrFiltLength, filters_per_group, pDepthInt, ALFp->coeffmulti, kMinTab);
348  }
349 
350  return len;
351}
352
353Int TEncEntropy::writeFilterCodingParams(int minKStart, int minScanVal, int maxScanVal, int kMinTab[])
354{
355  int scanPos;
356  int golombIndexBit;
357  int kMin;
358
359  // Golomb parameters
360  m_pcEntropyCoderIf->codeAlfUvlc(minKStart - 1);
361 
362  kMin = minKStart; 
363  for(scanPos = minScanVal; scanPos < maxScanVal; scanPos++)
364  {
365    golombIndexBit = (kMinTab[scanPos] != kMin)? 1: 0;
366   
367    assert(kMinTab[scanPos] <= kMin + 1);
368   
369    m_pcEntropyCoderIf->codeAlfFlag(golombIndexBit);
370    kMin = kMinTab[scanPos];
371  }   
372 
373  return 0;
374}
375
376Int TEncEntropy::writeFilterCoeffs(int sqrFiltLength, int filters_per_group, int pDepthInt[], 
377                                   int **FilterCoeff, int kMinTab[])
378{
379  int ind, scanPos, i;
380 
381  for(ind = 0; ind < filters_per_group; ++ind)
382  {
383    for(i = 0; i < sqrFiltLength; i++)
384    {
385      scanPos = pDepthInt[i] - 1;
386      Int k = (filters_per_group == 1) ? kMinTab[i] : kMinTab[scanPos];
387      golombEncode(FilterCoeff[ind][i], k);
388    }
389  }
390  return 0;
391}
392
393Int TEncEntropy::golombEncode(int coeff, int k)
394{
395  int q, i;
396  int symbol = abs(coeff);
397 
398  q = symbol >> k;
399 
400  for (i = 0; i < q; i++)
401  {
402    m_pcEntropyCoderIf->codeAlfFlag(1);
403  }
404  m_pcEntropyCoderIf->codeAlfFlag(0);
405  // write one zero
406 
407  for(i = 0; i < k; i++)
408  {
409    m_pcEntropyCoderIf->codeAlfFlag(symbol & 0x01);
410    symbol >>= 1;
411  }
412 
413  if(coeff != 0)
414  {
415    int sign = (coeff > 0)? 1: 0;
416    m_pcEntropyCoderIf->codeAlfFlag(sign);
417  }
418  return 0;
419}
420
421Void TEncEntropy::codeFilt(ALFParam* pAlfParam)
422{
423  if(pAlfParam->filters_per_group > 1)
424  {
425    m_pcEntropyCoderIf->codeAlfFlag (pAlfParam->predMethod);
426  }
427  for(Int ind = 0; ind < pAlfParam->filters_per_group; ++ind)
428  {
429    m_pcEntropyCoderIf->codeAlfFlag (pAlfParam->nbSPred[ind]);
430  }
431  codeFilterCoeff (pAlfParam);
432}
433
434/** encode merge flag
435 * \param pcCU
436 * \param uiAbsPartIdx
437 * \param uiPUIdx
438 * \returns Void
439 */
440Void TEncEntropy::encodeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiPUIdx )
441{ 
442  // at least one merge candidate exists
443  m_pcEntropyCoderIf->codeMergeFlag( pcCU, uiAbsPartIdx );
444}
445
446/** encode merge index
447 * \param pcCU
448 * \param uiAbsPartIdx
449 * \param uiPUIdx
450 * \param bRD
451 * \returns Void
452 */
453Void TEncEntropy::encodeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiPUIdx, Bool bRD )
454{
455  if( bRD )
456  {
457    uiAbsPartIdx = 0;
458    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
459  }
460
461  UInt uiNumCand = MRG_MAX_NUM_CANDS;
462  if ( uiNumCand > 1 )
463  {
464    m_pcEntropyCoderIf->codeMergeIndex( pcCU, uiAbsPartIdx );
465  }
466}
467
468
469#if QC_ARP_D0177
470Void TEncEntropy::encodeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD, UInt uiDepth)
471{
472  if( pcCU->getSlice()->getViewId() == 0 || pcCU->getSlice()->getIsDepth() == true || !pcCU->getSlice()->getARPStepNum() )
473  {
474    return;
475  }
476  assert( pcCU->isIntra( uiAbsPartIdx ) == false );
477  if( bRD )
478  {
479    uiAbsPartIdx = 0;
480  }
481  bool bSignalflag[2] = {true, true};
482  if (!(pcCU->getPartitionSize(uiAbsPartIdx)==SIZE_2Nx2N || pcCU->isSkipped(uiAbsPartIdx)))
483  {
484    assert(pcCU->getARPW (uiAbsPartIdx) == 0);
485    bSignalflag[0] = false;
486    bSignalflag[1] = false;
487 }
488  if (!(bSignalflag[0]|| bSignalflag[1]))
489  {
490    assert(pcCU->getARPW (uiAbsPartIdx) == 0);
491    if (uiDepth != -1)
492      pcCU->setARPWSubParts(0, uiAbsPartIdx, uiDepth);
493  }
494  else
495     m_pcEntropyCoderIf->codeARPW( pcCU, uiAbsPartIdx );
496 
497}
498#endif
499/** parse the fixed length code (smaller than one max value) in ALF
500 * \param run: coded value
501 * \param rx: cur addr
502 * \param numLCUInWidth: # of LCU in one LCU
503 * \returns Void
504 */
505Void TEncEntropy::encodeAlfFixedLengthRun(UInt run, UInt rx, UInt numLCUInWidth)
506{
507  assert(numLCUInWidth > rx);
508  UInt maxValue = numLCUInWidth - rx - 1;
509  m_pcEntropyCoderIf->codeAlfFixedLengthIdx(run, maxValue);
510}
511
512/** parse the fixed length code (smaller than one max value) in ALF
513 * \param idx: coded value
514 * \param numFilterSetsInBuffer: max value
515 * \returns Void
516 */
517Void TEncEntropy::encodeAlfStoredFilterSetIdx(UInt idx, UInt numFilterSetsInBuffer)
518{
519  assert(numFilterSetsInBuffer > 0);
520  UInt maxValue = numFilterSetsInBuffer - 1;
521  m_pcEntropyCoderIf->codeAlfFixedLengthIdx(idx, maxValue);
522}
523
524Void TEncEntropy::encodeAlfParam(AlfParamSet* pAlfParamSet, Bool bSentInAPS, Int firstLCUAddr, Bool alfAcrossSlice)
525{
526  Bool isEnabled[NUM_ALF_COMPONENT];
527  Bool isUniParam[NUM_ALF_COMPONENT];
528
529  isEnabled[ALF_Y] = true;
530  isEnabled[ALF_Cb]= pAlfParamSet->isEnabled[ALF_Cb];
531  isEnabled[ALF_Cr]= pAlfParamSet->isEnabled[ALF_Cr];
532
533  isUniParam[ALF_Y]= pAlfParamSet->isUniParam[ALF_Y];
534  isUniParam[ALF_Cb]= pAlfParamSet->isUniParam[ALF_Cb];
535  isUniParam[ALF_Cr]= pAlfParamSet->isUniParam[ALF_Cr]; 
536
537
538  //alf_cb_enable_flag
539  m_pcEntropyCoderIf->codeAlfFlag(isEnabled[ALF_Cb]?1:0);
540  //alf_cr_enable_flag
541  m_pcEntropyCoderIf->codeAlfFlag(isEnabled[ALF_Cr]?1:0); 
542
543  for(Int compIdx = 0; compIdx< NUM_ALF_COMPONENT; compIdx++)
544  {
545    if(isEnabled[compIdx])
546    {
547      //alf_one_{luma, cb, cr}_unit_per_slice_flag
548      m_pcEntropyCoderIf->codeAlfFlag(isUniParam[compIdx]?1:0);
549    }
550  }
551  if(bSentInAPS)
552  {
553    //alf_num_lcu_in_width_minus1
554    m_pcEntropyCoderIf->codeAlfUvlc(pAlfParamSet->numLCUInWidth-1);
555    //alf_num_lcu_in_height_minus1
556    m_pcEntropyCoderIf->codeAlfUvlc(pAlfParamSet->numLCUInHeight-1);
557  }
558  else //sent in slice header
559  {
560    //alf_num_lcu_in_slice_minus1
561    m_pcEntropyCoderIf->codeAlfUvlc(pAlfParamSet->numLCU-1);
562  }
563
564
565  encodeAlfParamSet(pAlfParamSet, pAlfParamSet->numLCUInWidth, pAlfParamSet->numLCU, firstLCUAddr, alfAcrossSlice, 0, (Int)NUM_ALF_COMPONENT-1);
566
567}
568
569Bool TEncEntropy::getAlfRepeatRowFlag(Int compIdx, AlfParamSet* pAlfParamSet
570                                    , Int lcuIdxInSlice, Int lcuPos
571                                    , Int startlcuPosX, Int endlcuPosX
572                                    , Int numLCUInWidth
573                                    )
574{
575  assert(startlcuPosX == 0); //only the beginning of one LCU row needs to send repeat_row_flag
576 
577  Int len = endlcuPosX - startlcuPosX +1;
578  Bool isRepeatRow = true;
579  Int curPos;
580
581  for(Int i= 0; i < len; i++)
582  {
583    curPos = lcuIdxInSlice +i;
584    AlfUnitParam& alfUnitParam = pAlfParamSet->alfUnitParam[compIdx][curPos];
585    AlfUnitParam& alfUpUnitParam = pAlfParamSet->alfUnitParam[compIdx][curPos-numLCUInWidth];
586
587    if ( !(alfUnitParam == alfUpUnitParam) )
588    {
589      isRepeatRow = false;
590      break;
591    }
592  }
593
594  return isRepeatRow;
595}
596
597
598Int TEncEntropy::getAlfRun(Int compIdx, AlfParamSet* pAlfParamSet
599                          , Int lcuIdxInSlice, Int lcuPos
600                          , Int startlcuPosX, Int endlcuPosX
601                          )
602{
603  Int alfRun = 0;
604  Int len = endlcuPosX - startlcuPosX +1;
605  AlfUnitParam& alfLeftUnitParam = pAlfParamSet->alfUnitParam[compIdx][lcuIdxInSlice];
606
607
608
609  for(Int i= 1; i < len; i++)
610  {
611    AlfUnitParam& alfUnitParam = pAlfParamSet->alfUnitParam[compIdx][lcuIdxInSlice+ i];
612
613    if (alfUnitParam == alfLeftUnitParam)
614    {
615      alfRun++;
616    }
617    else
618    {
619      break;
620    }
621  }
622
623  return alfRun;
624
625}
626
627
628
629Void TEncEntropy::encodeAlfParamSet(AlfParamSet* pAlfParamSet, Int numLCUInWidth, Int numLCU, Int firstLCUAddr, Bool alfAcrossSlice, Int startCompIdx, Int endCompIdx)
630{
631  Int endLCUY       = (numLCU -1 + firstLCUAddr)/numLCUInWidth;
632  Int endLCUX       = (numLCU -1 + firstLCUAddr)%numLCUInWidth;
633
634  static Bool isRepeatedRow   [NUM_ALF_COMPONENT];
635  static Int  numStoredFilters[NUM_ALF_COMPONENT];
636  static Int* run             [NUM_ALF_COMPONENT];
637
638  for(Int compIdx =startCompIdx; compIdx <= endCompIdx; compIdx++)
639  {
640    isRepeatedRow[compIdx]    = false;
641    numStoredFilters[compIdx] = 0;
642
643    run[compIdx] = new Int[numLCU+1];
644    run[compIdx][0] = -1; 
645  }
646
647  Int  ry, rx, addrUp, endrX, lcuPos;
648
649  for(Int i=0; i< numLCU; i++)
650  {
651    lcuPos= firstLCUAddr+ i;
652    rx    = lcuPos% numLCUInWidth;
653    ry    = lcuPos/ numLCUInWidth;
654    endrX = ( ry == endLCUY)?( endLCUX ):(numLCUInWidth-1);
655
656    for(Int compIdx =startCompIdx; compIdx <= endCompIdx; compIdx++)
657    {
658      AlfUnitParam& alfUnitParam = pAlfParamSet->alfUnitParam[compIdx][i];
659      if(pAlfParamSet->isEnabled[compIdx])
660      {
661        if(!pAlfParamSet->isUniParam[compIdx])
662        {
663          addrUp = i-numLCUInWidth;
664          if(rx ==0 && addrUp >=0)
665          {
666            isRepeatedRow[compIdx] = getAlfRepeatRowFlag(compIdx, pAlfParamSet, i, lcuPos, rx, endrX, numLCUInWidth);
667
668            //alf_repeat_row_flag
669            m_pcEntropyCoderIf->codeAlfFlag(isRepeatedRow[compIdx]?1:0);
670          }
671
672          if(isRepeatedRow[compIdx])
673          {
674            assert(addrUp >=0);
675            run[compIdx][i] = run[compIdx][addrUp];
676          }
677          else
678          {
679            if(rx == 0 || run[compIdx][i] < 0)
680            {             
681              run[compIdx][i] = getAlfRun(compIdx, pAlfParamSet, i, lcuPos, rx, endrX);
682
683              if(addrUp < 0)
684              {
685                //alf_run_diff u(v)
686                encodeAlfFixedLengthRun(run[compIdx][i], rx, numLCUInWidth);               
687              }
688              else
689              {
690                //alf_run_diff s(v)
691                m_pcEntropyCoderIf->codeAlfSvlc(run[compIdx][i]- run[compIdx][addrUp]);
692
693              }
694
695              if(ry > 0 && (addrUp >=0 || alfAcrossSlice))
696              {
697                //alf_merge_up_flag
698                m_pcEntropyCoderIf->codeAlfFlag(  (alfUnitParam.mergeType == ALF_MERGE_UP)?1:0   ); 
699              }
700
701              if(alfUnitParam.mergeType != ALF_MERGE_UP)
702              {
703                assert(alfUnitParam.mergeType == ALF_MERGE_DISABLED);
704
705                //alf_lcu_enable_flag
706                m_pcEntropyCoderIf->codeAlfFlag(alfUnitParam.isEnabled ? 1 : 0);
707
708                if(alfUnitParam.isEnabled)
709                {
710                  if(numStoredFilters[compIdx] > 0)
711                  {
712                    //alf_new_filter_set_flag
713                    m_pcEntropyCoderIf->codeAlfFlag(alfUnitParam.isNewFilt ? 1:0);
714
715                    if(!alfUnitParam.isNewFilt)
716                    {
717                      //alf_stored_filter_set_idx
718                      encodeAlfStoredFilterSetIdx(alfUnitParam.storedFiltIdx, numStoredFilters[compIdx]);
719
720                    }
721                  }
722                  else
723                  {
724                    assert(alfUnitParam.isNewFilt);
725                  }
726
727                  if(alfUnitParam.isNewFilt)
728                  {
729                    assert(alfUnitParam.alfFiltParam->alf_flag == 1);
730                    encodeAlfParam(alfUnitParam.alfFiltParam);
731                    numStoredFilters[compIdx]++;
732                  }
733                }
734
735              }
736            }
737
738            run[compIdx][i+1] = run[compIdx][i] -1;
739          }
740
741        }
742        else // uni-param
743        {
744          if(i == 0)
745          {
746            //alf_lcu_enable_flag
747            m_pcEntropyCoderIf->codeAlfFlag(alfUnitParam.isEnabled?1:0);
748            if(alfUnitParam.isEnabled)
749            {
750              encodeAlfParam(alfUnitParam.alfFiltParam);
751            }
752          }
753        }
754      } // component enabled/disable
755    } //comp
756
757  }
758
759  for(Int compIdx =startCompIdx; compIdx <= endCompIdx; compIdx++)
760  {
761    delete[] run[compIdx];
762  }
763
764}
765
766
767Void TEncEntropy::encodeAlfParam(ALFParam* pAlfParam)
768{
769  const Int numCoeff = (Int)ALF_MAX_NUM_COEF;
770
771  switch(pAlfParam->componentID)
772  {
773  case ALF_Cb:
774  case ALF_Cr:
775    {
776      for(Int pos=0; pos< numCoeff; pos++)
777      {
778        m_pcEntropyCoderIf->codeAlfSvlc(  pAlfParam->coeffmulti[0][pos]);
779
780      }
781    }
782    break;
783  case ALF_Y:
784    {
785      codeAux(pAlfParam);
786      codeFilt(pAlfParam);
787    }
788    break;
789  default:
790    {
791      printf("Not a legal component ID\n");
792      assert(0);
793      exit(-1);
794    }
795  }
796}
797
798Void TEncEntropy::encodeAlfCtrlFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
799{
800  if( bRD )
801  {
802    uiAbsPartIdx = 0;
803  }
804  m_pcEntropyCoderIf->codeAlfCtrlFlag( pcCU, uiAbsPartIdx );
805}
806
807
808/** Encode ALF CU control flag
809 * \param uiFlag ALF CU control flag: 0 or 1
810 */
811Void TEncEntropy::encodeAlfCtrlFlag(UInt uiFlag)
812{
813  assert(uiFlag == 0 || uiFlag == 1);
814  m_pcEntropyCoderIf->codeAlfCtrlFlag( uiFlag );
815}
816
817
818/** Encode ALF CU control flag parameters
819 * \param pAlfParam ALF parameters
820 */
821Void TEncEntropy::encodeAlfCtrlParam(AlfCUCtrlInfo& cAlfParam, Int iNumCUsInPic)
822{
823  // region control parameters for luma
824  m_pcEntropyCoderIf->codeAlfFlag(cAlfParam.cu_control_flag);
825
826  if (cAlfParam.cu_control_flag == 0)
827  { 
828    return;
829  }
830
831  m_pcEntropyCoderIf->codeAlfCtrlDepth();
832
833  Int iSymbol    = ((Int)cAlfParam.num_alf_cu_flag - iNumCUsInPic);
834  m_pcEntropyCoderIf->codeAlfSvlc(iSymbol);
835
836  for(UInt i=0; i< cAlfParam.num_alf_cu_flag; i++)
837  {
838    m_pcEntropyCoderIf->codeAlfCtrlFlag( cAlfParam.alf_cu_flag[i] );
839  }
840}
841
842/** encode prediction mode
843 * \param pcCU
844 * \param uiAbsPartIdx
845 * \param bRD
846 * \returns Void
847 */
848Void TEncEntropy::encodePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
849{
850  if( bRD )
851  {
852    uiAbsPartIdx = 0;
853  }
854  if( !bRD )
855  {
856    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
857    {
858      return;
859    }
860  }
861
862#if !RWTH_SDC_DLT_B0036
863  if ( pcCU->getSlice()->isIntra() )
864  {
865    return;
866  }
867#endif
868
869  m_pcEntropyCoderIf->codePredMode( pcCU, uiAbsPartIdx );
870 
871#if RWTH_SDC_DLT_B0036
872  // if B-Slice, code SDC flag later
873  if( !pcCU->getSlice()->isInterB() && pcCU->getSlice()->getSPS()->isDepth() && pcCU->isIntra(uiAbsPartIdx) )
874  {
875    // encode SDC flag
876    encodeSDCFlag(pcCU, uiAbsPartIdx, bRD);
877  }
878#endif
879#endif
880}
881
882// Split mode
883Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
884{
885  if( bRD )
886  {
887    uiAbsPartIdx = 0;
888  }
889  if( !bRD )
890  {
891    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
892    {
893      return;
894    }
895  }
896
897  m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
898}
899
900/** encode partition size
901 * \param pcCU
902 * \param uiAbsPartIdx
903 * \param uiDepth
904 * \param bRD
905 * \returns Void
906 */
907Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
908{
909  if( bRD )
910  {
911    uiAbsPartIdx = 0;
912  }
913  if( !bRD )
914  {
915    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
916    {
917      return;
918    }
919  }
920#if !PKU_QC_DEPTH_INTRA_UNI_D0195
921#if RWTH_SDC_DLT_B0036
922  if( !pcCU->getSlice()->isInterB() && pcCU->isIntra(uiAbsPartIdx) && pcCU->getSDCFlag(uiAbsPartIdx)  )
923  {
924    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
925    return;
926  }
927#endif
928#endif
929 
930  m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth );
931 
932#if !PKU_QC_DEPTH_INTRA_UNI_D0195
933#if RWTH_SDC_DLT_B0036
934  // code SDC flag now!
935  if( pcCU->getSlice()->isInterB() && pcCU->isIntra(uiAbsPartIdx) && pcCU->getSlice()->getSPS()->isDepth() )
936  {
937    // encode SDC flag
938    encodeSDCFlag(pcCU, uiAbsPartIdx, bRD);
939   
940    if( pcCU->getSDCFlag(uiAbsPartIdx) )
941    {
942      // part size is also known for SDC intra
943      assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
944    }
945  }
946#endif
947#endif
948}
949
950/** Encode I_PCM information.
951 * \param pcCU pointer to CU
952 * \param uiAbsPartIdx CU index
953 * \param bRD flag indicating estimation or encoding
954 * \returns Void
955 */
956Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
957{
958  if(!pcCU->getSlice()->getSPS()->getUsePCM()
959    || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize())
960    || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize()))
961  {
962    return;
963  }
964 
965#if RWTH_SDC_DLT_B0036
966  if( pcCU->getSDCFlag(uiAbsPartIdx) )
967  {
968    return;
969  }
970#endif
971 
972  if( bRD )
973  {
974    uiAbsPartIdx = 0;
975  }
976 
977  Int numIPCM = 0;
978  Bool firstIPCMFlag = false;
979
980  if( pcCU->getIPCMFlag(uiAbsPartIdx) )
981  {
982    numIPCM = 1;
983    firstIPCMFlag = true;
984
985    if( !bRD )
986    {
987      numIPCM = pcCU->getNumSucIPCM();
988      firstIPCMFlag = !pcCU->getLastCUSucIPCMFlag();
989    }
990  }
991  m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx, numIPCM, firstIPCMFlag);
992
993}
994
995Void TEncEntropy::xEncodeTransform( TComDataCU* pcCU,UInt offsetLuma, UInt offsetChroma, UInt uiAbsPartIdx, UInt absTUPartIdx, UInt uiDepth, UInt width, UInt height, UInt uiTrIdx, UInt uiInnerQuadIdx, UInt& uiYCbfFront3, UInt& uiUCbfFront3, UInt& uiVCbfFront3, Bool& bCodeDQP )
996{
997  const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) + pcCU->getDepth( uiAbsPartIdx ) > uiDepth;
998  const UInt uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()]+2 - uiDepth;
999  UInt cbfY = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA    , uiTrIdx );
1000  UInt cbfU = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
1001  UInt cbfV = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
1002
1003  if(uiTrIdx==0)
1004  {
1005    m_bakAbsPartIdxCU = uiAbsPartIdx;
1006  }
1007  if( uiLog2TrafoSize == 2 )
1008  {
1009    UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
1010    if( ( uiAbsPartIdx % partNum ) == 0 )
1011    {
1012      m_uiBakAbsPartIdx   = uiAbsPartIdx;
1013      m_uiBakChromaOffset = offsetChroma;
1014    }
1015    else if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
1016    {
1017      cbfU = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
1018      cbfV = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
1019    }
1020  }
1021  {//CABAC
1022    if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) )
1023    {
1024      assert( uiSubdiv );
1025    }
1026    else if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) &&  (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) )
1027    {
1028      if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
1029      {
1030        assert( uiSubdiv );
1031      }
1032      else
1033      {
1034        assert(!uiSubdiv );
1035      }
1036    }
1037    else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1038    {
1039      assert( uiSubdiv );
1040    }
1041    else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
1042    {
1043      assert( !uiSubdiv );
1044    }
1045    else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
1046    {
1047      assert( !uiSubdiv );
1048    }
1049    else
1050    {
1051      assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
1052      m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, uiDepth );
1053    }
1054  }
1055
1056  {
1057    if( uiLog2TrafoSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1058    {
1059      const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx );
1060      const Bool bFirstCbfOfCU = uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() || uiTrDepthCurr == 0;
1061      if( bFirstCbfOfCU || uiLog2TrafoSize > 2 )
1062      {
1063        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) )
1064        {
1065          if ( uiInnerQuadIdx == 3 && uiUCbfFront3 == 0 && uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1066          {
1067            uiUCbfFront3++;
1068          }
1069          else
1070          {
1071            m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1072            uiUCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1073          }
1074        }
1075        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) )
1076        {
1077          if ( uiInnerQuadIdx == 3 && uiVCbfFront3 == 0 && uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize()  )
1078          {
1079            uiVCbfFront3++;
1080          }
1081          else
1082          {
1083            m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1084            uiVCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1085          }
1086        }
1087      }
1088      else if( uiLog2TrafoSize == 2 )
1089      {
1090        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) );
1091        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) );
1092       
1093        uiUCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1094        uiVCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1095      }
1096    }
1097   
1098    if( uiSubdiv )
1099    {
1100      UInt size;
1101      width  >>= 1;
1102      height >>= 1;
1103      size = width*height;
1104      uiTrIdx++;
1105      ++uiDepth;
1106      const UInt partNum = pcCU->getPic()->getNumPartInCU() >> (uiDepth << 1);
1107     
1108      UInt uiCurrentCbfY = 0;
1109      UInt uiCurrentCbfU = 0;
1110      UInt uiCurrentCbfV = 0;
1111     
1112      UInt nsAddr = 0;
1113      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 0, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1114      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 0, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1115
1116      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1117      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 1, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1118      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 1, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1119
1120      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1121      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 2, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1122      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 2, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1123
1124      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1125      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 3, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1126      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 3, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );     
1127     
1128      uiYCbfFront3 += uiCurrentCbfY;
1129      uiUCbfFront3 += uiCurrentCbfU;
1130      uiVCbfFront3 += uiCurrentCbfV;
1131    }
1132    else
1133    {
1134      {
1135        DTRACE_CABAC_VL( g_nSymbolCounter++ );
1136        DTRACE_CABAC_T( "\tTrIdx: abspart=" );
1137        DTRACE_CABAC_V( uiAbsPartIdx );
1138        DTRACE_CABAC_T( "\tdepth=" );
1139        DTRACE_CABAC_V( uiDepth );
1140        DTRACE_CABAC_T( "\ttrdepth=" );
1141        DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) );
1142        DTRACE_CABAC_T( "\n" );
1143      }
1144      UInt uiLumaTrMode, uiChromaTrMode;
1145      pcCU->convertTransIdx( uiAbsPartIdx, pcCU->getTransformIdx( uiAbsPartIdx ), uiLumaTrMode, uiChromaTrMode );
1146      if(pcCU->getPredictionMode( uiAbsPartIdx ) == MODE_INTER && pcCU->useNonSquarePU( uiAbsPartIdx ) )
1147      {
1148        pcCU->setNSQTIdxSubParts( uiLog2TrafoSize, uiAbsPartIdx, absTUPartIdx, uiLumaTrMode );
1149      }
1150      if( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, 0 ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, 0 ) )
1151      {
1152        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, 0 ) );
1153        //      printf( "saved one bin! " );
1154      }
1155      else
1156      {
1157        const UInt uiLog2CUSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - pcCU->getDepth( uiAbsPartIdx );
1158        if ( pcCU->getPredictionMode( uiAbsPartIdx ) != MODE_INTRA && uiInnerQuadIdx == 3 && uiYCbfFront3 == 0 && uiUCbfFront3 == 0 && uiVCbfFront3 == 0
1159            && ( uiLog2CUSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() + 1 || uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() ) )
1160        {     
1161          uiYCbfFront3++;
1162        }   
1163        else
1164        {
1165          m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, uiLumaTrMode );
1166          uiYCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, uiLumaTrMode );
1167        }
1168      }
1169     
1170      if ( cbfY || cbfU || cbfV )
1171      {
1172        // dQP: only for LCU once
1173        if ( pcCU->getSlice()->getPPS()->getUseDQP() )
1174        {
1175          if ( bCodeDQP )
1176          {
1177            encodeQP( pcCU, m_bakAbsPartIdxCU );
1178            bCodeDQP = false;
1179          }
1180        }
1181      }
1182      if( cbfY )
1183      {
1184        Int trWidth = width;
1185        Int trHeight = height;
1186        pcCU->getNSQTSize( uiTrIdx, uiAbsPartIdx, trWidth, trHeight );
1187        m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffY()+offsetLuma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
1188      }
1189      if( uiLog2TrafoSize > 2 )
1190      {
1191        Int trWidth = width >> 1;
1192        Int trHeight = height >> 1;
1193        pcCU->getNSQTSize( uiTrIdx, uiAbsPartIdx, trWidth, trHeight );
1194        if( cbfU )
1195        {
1196          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
1197        }
1198        if( cbfV )
1199        {
1200          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
1201        }
1202      }
1203      else
1204      {
1205        UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
1206        if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
1207        {
1208          Int trWidth = width;
1209          Int trHeight = height;
1210          pcCU->getNSQTSize( uiTrIdx - 1, uiAbsPartIdx, trWidth, trHeight );
1211          if( cbfU )
1212          {
1213            m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
1214          }
1215          if( cbfV )
1216          {
1217            m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
1218          }
1219        }
1220      }
1221    }
1222  }
1223}
1224
1225
1226// Intra direction for Luma
1227Void TEncEntropy::encodeIntraDirModeLuma  ( TComDataCU* pcCU, UInt uiAbsPartIdx
1228#if PKU_QC_DEPTH_INTRA_UNI_D0195
1229                                           ,Bool bSdcRD
1230#endif
1231                                           )
1232{
1233  m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, uiAbsPartIdx
1234#if PKU_QC_DEPTH_INTRA_UNI_D0195
1235    ,bSdcRD
1236#endif
1237    );
1238}
1239
1240// Intra direction for Chroma
1241Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1242{
1243  if( bRD )
1244  {
1245    uiAbsPartIdx = 0;
1246  }
1247 
1248  m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx );
1249}
1250
1251Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD
1252#if PKU_QC_DEPTH_INTRA_UNI_D0195
1253                                 ,Bool bSdcRD
1254#endif
1255                                 )
1256{
1257  if( bRD )
1258  {
1259    uiAbsPartIdx = 0;
1260  }
1261
1262#if !PKU_QC_DEPTH_INTRA_UNI_D0195
1263#if RWTH_SDC_DLT_B0036
1264  if( pcCU->getSDCFlag(uiAbsPartIdx) )
1265  {
1266    encodeSDCPredMode(pcCU, uiAbsPartIdx, bRD);
1267    return;
1268  }
1269#endif
1270#endif
1271
1272  PartSize eSize = pcCU->getPartitionSize( uiAbsPartIdx );
1273 
1274  if( pcCU->isIntra( uiAbsPartIdx ) )                                 // If it is Intra mode, encode intra prediction mode.
1275  {
1276    if( eSize == SIZE_NxN )                                         // if it is NxN size, encode 4 intra directions.
1277    {
1278      UInt uiPartOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(uiAbsPartIdx) << 1 ) ) >> 2;
1279      // if it is NxN size, this size might be the smallest partition size.
1280      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx                  );
1281      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset   );
1282      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset*2 );
1283      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset*3 );
1284#if PKU_QC_DEPTH_INTRA_UNI_D0195
1285      if(!pcCU->getSDCFlag(uiAbsPartIdx))
1286#endif
1287      encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
1288    }
1289    else                                                              // if it is not NxN size, encode 1 intra directions
1290    {
1291      encodeIntraDirModeLuma  ( pcCU, uiAbsPartIdx
1292      encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
1293    }
1294  }
1295  else                                                                // if it is Inter mode, encode motion vector and reference index
1296  {
1297    encodePUWise( pcCU, uiAbsPartIdx, bRD );
1298#if QC_ARP_D0177
1299    encodeARPW( pcCU , uiAbsPartIdx , bRD );
1300#endif
1301  }
1302}
1303
1304/** encode motion information for every PU block
1305 * \param pcCU
1306 * \param uiAbsPartIdx
1307 * \param bRD
1308 * \returns Void
1309 */
1310Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1311{
1312  if ( bRD )
1313  {
1314    uiAbsPartIdx = 0;
1315  }
1316 
1317  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
1318  UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) );
1319  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
1320  UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() - uiDepth ) << 1 ) ) >> 4;
1321
1322  for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset )
1323  {
1324    encodeMergeFlag( pcCU, uiSubPartIdx, uiPartIdx );
1325    if ( pcCU->getMergeFlag( uiSubPartIdx ) )
1326    {
1327      encodeMergeIndex( pcCU, uiSubPartIdx, uiPartIdx );
1328    }
1329    else
1330    {
1331      encodeInterDirPU( pcCU, uiSubPartIdx );
1332      for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1333      {
1334        if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1335        {
1336          encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1337          encodeMvdPU       ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1338          encodeMVPIdxPU    ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1339        }
1340      }
1341    }
1342  }
1343
1344  return;
1345}
1346
1347Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx )
1348{
1349  if ( !pcCU->getSlice()->isInterB() )
1350  {
1351    return;
1352  }
1353
1354  m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx );
1355  return;
1356}
1357
1358/** encode reference frame index for a PU block
1359 * \param pcCU
1360 * \param uiAbsPartIdx
1361 * \param eRefList
1362 * \returns Void
1363 */
1364Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1365{
1366  assert( !pcCU->isIntra( uiAbsPartIdx ) );
1367
1368  if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C)>0 && pcCU->getInterDir( uiAbsPartIdx ) != 3)
1369  {
1370    if ((eRefList== REF_PIC_LIST_1) || ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_C ) == 1 ) )
1371    {
1372      return;
1373    }
1374
1375    if ( pcCU->getSlice()->getNumRefIdx ( REF_PIC_LIST_C ) > 1 )
1376    {
1377      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, RefPicList(pcCU->getInterDir( uiAbsPartIdx )-1) );
1378    }
1379
1380  }
1381  else
1382  {
1383    if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) )
1384    {
1385      return;
1386    }
1387
1388    if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
1389    {
1390      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList );
1391    }
1392  }
1393
1394  return;
1395}
1396
1397/** encode motion vector difference for a PU block
1398 * \param pcCU
1399 * \param uiAbsPartIdx
1400 * \param eRefList
1401 * \returns Void
1402 */
1403Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1404{
1405  assert( !pcCU->isIntra( uiAbsPartIdx ) );
1406
1407  if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
1408  {
1409    m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList );
1410  }
1411  return;
1412}
1413
1414Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1415{
1416  if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) && (pcCU->getAMVPMode(uiAbsPartIdx) == AM_EXPL) )
1417  {
1418#if H3D_IVMP
1419#if SEC_TWO_CANDIDATES_FOR_AMVP_D0122
1420    const Int iNumCands = AMVP_MAX_NUM_CANDS;
1421#else
1422    const Int iNumCands = AMVP_MAX_NUM_CANDS + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
1423#endif
1424    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList, iNumCands );
1425#else
1426    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList );
1427#endif
1428  }
1429
1430  return;
1431}
1432
1433Void TEncEntropy::encodeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1434{
1435  m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, eType, uiTrDepth );
1436}
1437
1438Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1439{
1440  m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx );
1441}
1442
1443Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1444{
1445  m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
1446}
1447
1448// dQP
1449Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1450{
1451  if( bRD )
1452  {
1453    uiAbsPartIdx = 0;
1454  }
1455 
1456  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
1457  {
1458    m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx );
1459  }
1460}
1461
1462
1463// texture
1464
1465/** encode coefficients
1466 * \param pcCU
1467 * \param uiAbsPartIdx
1468 * \param uiDepth
1469 * \param uiWidth
1470 * \param uiHeight
1471 */
1472Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiWidth, UInt uiHeight, Bool& bCodeDQP )
1473{
1474  UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1475  UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1476  UInt uiChromaOffset = uiLumaOffset>>2;
1477 
1478  UInt uiLumaTrMode, uiChromaTrMode;
1479  pcCU->convertTransIdx( uiAbsPartIdx, pcCU->getTransformIdx(uiAbsPartIdx), uiLumaTrMode, uiChromaTrMode );
1480 
1481#if RWTH_SDC_DLT_B0036
1482  if( pcCU->getSDCFlag( uiAbsPartIdx ) )
1483  {
1484    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
1485    assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
1486    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
1487    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
1488    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
1489    encodeSDCResidualData(pcCU, uiAbsPartIdx);
1490#endif
1491    return;
1492  }
1493#endif
1494 
1495  if( pcCU->isIntra(uiAbsPartIdx) )
1496  {
1497    DTRACE_CABAC_VL( g_nSymbolCounter++ )
1498    DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" )
1499    DTRACE_CABAC_V( uiDepth )
1500    DTRACE_CABAC_T( "\n" )
1501  }
1502  else
1503  {
1504    {
1505#if HHI_MPI
1506      if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N &&
1507            ( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 || uiDepth == pcCU->getTextureModeDepth( uiAbsPartIdx ) ) ) )
1508#else
1509      if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) )
1510#endif
1511      {
1512        m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
1513      }
1514      if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) )
1515      {
1516#if 1 // MW Bug Fix
1517        pcCU->setCbfSubParts( 0, 0, 0, uiAbsPartIdx, uiDepth );
1518        pcCU->setTrIdxSubParts( 0 , uiAbsPartIdx, uiDepth );
1519#endif
1520        pcCU->setNSQTIdxSubParts( uiAbsPartIdx, uiDepth );
1521        return;
1522      }
1523    }
1524  }
1525 
1526#if FIX_MPI_B0065
1527  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getMergeIndex( uiAbsPartIdx ) == 0 && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N && pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1 )
1528  {
1529    TComDataCU *pcTextureCU = pcCU->getSlice()->getTexturePic()->getCU( pcCU->getAddr() );
1530    if( uiDepth == pcTextureCU->getDepth(uiAbsPartIdx))
1531    {
1532      PartSize partSize = pcTextureCU->getPartitionSize(uiAbsPartIdx);
1533      pcCU->setPartSizeSubParts( partSize, uiAbsPartIdx, uiDepth );
1534    }
1535    else
1536    {
1537      pcCU->setPartSizeSubParts( SIZE_NxN, uiAbsPartIdx, uiDepth );
1538    }
1539  }
1540#endif
1541
1542  UInt temp = 0;
1543  UInt temp1 = 0;
1544  UInt temp2 = 0;
1545  xEncodeTransform( pcCU, uiLumaOffset, uiChromaOffset, uiAbsPartIdx, uiAbsPartIdx, uiDepth, uiWidth, uiHeight, 0, 0, temp, temp1, temp2, bCodeDQP );
1546
1547#if FIX_MPI_B0065
1548  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getMergeIndex( uiAbsPartIdx ) == 0 && pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N &&  pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1 )
1549  {
1550    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth ); 
1551  }
1552#endif
1553}
1554
1555Void TEncEntropy::encodeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoeff, UInt uiAbsPartIdx, UInt uiTrWidth, UInt uiTrHeight, UInt uiDepth, TextType eType )
1556{ // This is for Transform unit processing. This may be used at mode selection stage for Inter.
1557  m_pcEntropyCoderIf->codeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiTrWidth, uiTrHeight, uiDepth, eType );
1558}
1559
1560Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType)
1561{ 
1562  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
1563 
1564  m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, height, eTType );
1565}
1566
1567#if LGE_SAO_MIGRATION_D0091
1568Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam, UInt compIdx)
1569{
1570    UInt uiSymbol;
1571    Int i;
1572
1573    uiSymbol = saoLcuParam->typeIdx + 1;
1574    if (compIdx!=2)
1575    {
1576        m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
1577    }
1578
1579    if (uiSymbol)
1580    {
1581        if (saoLcuParam->typeIdx < 4 && compIdx != 2)
1582        {
1583            saoLcuParam->subTypeIdx = saoLcuParam->typeIdx;
1584        }
1585#if FULL_NBIT
1586        Int offsetTh = 1 << ( min((Int)(g_uiBitDepth + (g_uiBitDepth-8)-5),5) );
1587#else
1588        Int offsetTh = 1 << ( min((Int)(g_uiBitDepth + g_uiBitIncrement-5),5) );
1589#endif
1590        if( saoLcuParam->typeIdx == SAO_BO )
1591        {
1592            for( i=0; i< saoLcuParam->length; i++)
1593            {
1594                UInt absOffset = ( (saoLcuParam->offset[i] < 0) ? -saoLcuParam->offset[i] : saoLcuParam->offset[i]);
1595                m_pcEntropyCoderIf->codeSaoMaxUvlc(absOffset, offsetTh-1);
1596            } 
1597            for( i=0; i< saoLcuParam->length; i++)
1598            {
1599                if (saoLcuParam->offset[i] != 0)
1600                {
1601                    UInt sign = (saoLcuParam->offset[i] < 0) ? 1 : 0 ;
1602                    m_pcEntropyCoderIf->codeSAOSign(sign);
1603                }
1604            }
1605            uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
1606            m_pcEntropyCoderIf->codeSaoUflc(5, uiSymbol);
1607        }
1608        else if( saoLcuParam->typeIdx < 4 )
1609        {
1610            m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[0], offsetTh-1);
1611            m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[1], offsetTh-1);
1612            m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[2], offsetTh-1);
1613            m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[3], offsetTh-1);
1614
1615            if (compIdx!=2)
1616            {
1617                uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
1618                m_pcEntropyCoderIf->codeSaoUflc(2, uiSymbol);
1619            }
1620        }
1621    }
1622}
1623
1624/** Encode SAO unit interleaving
1625* \param  rx
1626* \param  ry
1627* \param  pSaoParam
1628* \param  pcCU
1629* \param  iCUAddrInSlice
1630* \param  iCUAddrUpInSlice
1631* \param  bLFCrossSliceBoundaryFlag
1632 */
1633Void TEncEntropy::encodeSaoUnitInterleaving(Int compIdx, Bool saoFlag, Int rx, Int ry, SaoLcuParam* saoLcuParam, Int cuAddrInSlice, Int cuAddrUpInSlice, Int allowMergeLeft, Int allowMergeUp)
1634{
1635    if (saoFlag)
1636    {
1637        if (rx>0 && cuAddrInSlice!=0 && allowMergeLeft)
1638        {
1639            m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeLeftFlag);
1640        }
1641        else
1642        {
1643            saoLcuParam->mergeLeftFlag = 0;
1644        }
1645       
1646        if (saoLcuParam->mergeLeftFlag == 0)
1647        {
1648            if ( (ry > 0) && (cuAddrUpInSlice>=0) && allowMergeUp )
1649            {
1650                m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeUpFlag);
1651            }
1652            else
1653            {
1654                saoLcuParam->mergeUpFlag = 0;
1655            }
1656
1657            if (!saoLcuParam->mergeUpFlag)
1658            {
1659                encodeSaoOffset(saoLcuParam, compIdx);
1660            }
1661        }
1662    }
1663}
1664#else
1665/** Encode SAO Offset
1666 * \param  saoLcuParam SAO LCU paramters
1667 */
1668Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam)
1669{
1670  UInt uiSymbol;
1671  Int i;
1672
1673  uiSymbol = saoLcuParam->typeIdx + 1;
1674  m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
1675  if (uiSymbol)
1676  {
1677    if( saoLcuParam->typeIdx == SAO_BO )
1678    {
1679      // Code Left Band Index
1680      uiSymbol = (UInt) (saoLcuParam->bandPosition);
1681      m_pcEntropyCoderIf->codeSaoUflc(uiSymbol);
1682      for( i=0; i< saoLcuParam->length; i++)
1683      {
1684        m_pcEntropyCoderIf->codeSaoSvlc(saoLcuParam->offset[i]);
1685      } 
1686    }
1687    else
1688      if( saoLcuParam->typeIdx < 4 )
1689      {
1690        m_pcEntropyCoderIf->codeSaoUvlc( saoLcuParam->offset[0]);
1691        m_pcEntropyCoderIf->codeSaoUvlc( saoLcuParam->offset[1]);
1692        m_pcEntropyCoderIf->codeSaoUvlc(-saoLcuParam->offset[2]);
1693        m_pcEntropyCoderIf->codeSaoUvlc(-saoLcuParam->offset[3]);
1694      }
1695  }
1696}
1697/** Encode SAO unit
1698* \param  rx
1699* \param  ry
1700* \param  iCompIdx
1701* \param  pSaoParam
1702* \param  bRepeatedRow
1703 */
1704Void TEncEntropy::encodeSaoUnit(Int rx, Int ry, Int compIdx, SAOParam* saoParam, Int repeatedRow )
1705{
1706  int addr, addrLeft; 
1707  int numCuInWidth  = saoParam->numCuInWidth;
1708  SaoLcuParam* saoOneLcu;
1709  Int runLeft;
1710
1711  addr      =  rx + ry*numCuInWidth;
1712  addrLeft  =  (addr%numCuInWidth == 0) ? -1 : addr - 1;
1713
1714  if (!repeatedRow)
1715  {
1716    saoOneLcu = &(saoParam->saoLcuParam[compIdx][addr]);   
1717    runLeft = (addrLeft>=0 ) ? saoParam->saoLcuParam[compIdx][addrLeft].run : -1;
1718    if (rx == 0 || runLeft==0)
1719    {
1720      if (ry == 0)
1721      {
1722        m_pcEntropyCoderIf->codeSaoRun(saoOneLcu->runDiff, numCuInWidth-rx-1); 
1723        saoOneLcu->mergeUpFlag = 0;
1724      }
1725      else 
1726      {
1727        m_pcEntropyCoderIf->codeSaoSvlc(saoOneLcu->runDiff); 
1728        m_pcEntropyCoderIf->codeSaoFlag(saoOneLcu->mergeUpFlag); 
1729      }
1730      if (!saoOneLcu->mergeUpFlag)
1731      {
1732        encodeSaoOffset(saoOneLcu);
1733      }
1734    }
1735  }
1736}
1737
1738/** Encode SAO unit interleaving
1739* \param  rx
1740* \param  ry
1741* \param  pSaoParam
1742* \param  pcCU
1743* \param  iCUAddrInSlice
1744* \param  iCUAddrUpInSlice
1745* \param  bLFCrossSliceBoundaryFlag
1746 */
1747Void TEncEntropy::encodeSaoUnitInterleaving(Int rx, Int ry, SAOParam* saoParam, TComDataCU* cu, Int cuAddrInSlice, Int cuAddrUpInSlice, Bool lfCrossSliceBoundaryFlag)
1748{
1749  Int addr = cu->getAddr();
1750  for (Int compIdx=0; compIdx<3; compIdx++)
1751  {
1752    if (saoParam->bSaoFlag[compIdx])
1753    {
1754      if (rx>0 && cuAddrInSlice!=0)
1755      {
1756      m_pcEntropyCoderIf->codeSaoMergeLeft(saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag,compIdx);
1757      }
1758      else
1759      {
1760        saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag = 0;
1761      }
1762      if (saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag == 0)
1763      {
1764        if ( (ry > 0) && (cuAddrUpInSlice>0||lfCrossSliceBoundaryFlag))
1765        {
1766          m_pcEntropyCoderIf->codeSaoMergeUp(saoParam->saoLcuParam[compIdx][addr].mergeUpFlag);
1767        }
1768        else
1769        {
1770          saoParam->saoLcuParam[compIdx][addr].mergeUpFlag = 0;
1771        }
1772        if (!saoParam->saoLcuParam[compIdx][addr].mergeUpFlag)
1773        {
1774          encodeSaoOffset(&(saoParam->saoLcuParam[compIdx][addr]));
1775        }
1776      }
1777    }
1778  }
1779}
1780
1781/** Encode SAO parameter
1782* \param  pcAPS
1783 */
1784Void TEncEntropy::encodeSaoParam(TComAPS* aps)
1785{
1786  SaoLcuParam* psSaoOneLcu;
1787  int i,j,k, compIdx; 
1788  int numCuInWidth  ;
1789  int numCuInHeight ;
1790  Bool repeatedRow[3];
1791  Int addr;
1792  m_pcEntropyCoderIf->codeSaoFlag(aps->getSaoInterleavingFlag()); 
1793  if(!aps->getSaoInterleavingFlag())
1794  {
1795    m_pcEntropyCoderIf->codeSaoFlag(aps->getSaoEnabled()); 
1796    if (aps->getSaoEnabled())
1797    {
1798      SAOParam* pSaoParam = aps->getSaoParam();
1799      numCuInWidth  = pSaoParam->numCuInWidth;
1800      numCuInHeight = pSaoParam->numCuInHeight;
1801      m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->bSaoFlag[1]); 
1802      m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->bSaoFlag[2]); 
1803      m_pcEntropyCoderIf->codeSaoUvlc(numCuInWidth-1); 
1804      m_pcEntropyCoderIf->codeSaoUvlc(numCuInHeight-1); 
1805      for (compIdx=0;compIdx<3;compIdx++)
1806      {
1807        if (pSaoParam->bSaoFlag[compIdx])
1808        {
1809          m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->oneUnitFlag[compIdx]); 
1810          if (pSaoParam->oneUnitFlag[compIdx])
1811          {
1812            psSaoOneLcu = &(pSaoParam->saoLcuParam[compIdx][0]);   
1813            encodeSaoOffset(psSaoOneLcu);
1814          }
1815        }
1816      }
1817
1818      for (j=0;j<numCuInHeight;j++)
1819      {
1820        for (compIdx=0; compIdx<3; compIdx++)
1821        {
1822          repeatedRow[compIdx] = true;
1823          for (k=0;k<numCuInWidth;k++)
1824          {
1825            addr       =  k + j*numCuInWidth;
1826            psSaoOneLcu = &(pSaoParam->saoLcuParam[compIdx][addr]);   
1827            if (!psSaoOneLcu->mergeUpFlag || psSaoOneLcu->runDiff)
1828            {
1829              repeatedRow[compIdx] = false;
1830              break;
1831            }
1832          }
1833        }
1834        for (i=0;i<numCuInWidth;i++)
1835        {
1836          for (compIdx=0; compIdx<3; compIdx++)
1837          {
1838            if (pSaoParam->bSaoFlag[compIdx]  && !pSaoParam->oneUnitFlag[compIdx]) 
1839            {
1840              if (j>0 && i==0) 
1841              {
1842                m_pcEntropyCoderIf->codeSaoFlag(repeatedRow[compIdx]); 
1843              }
1844              encodeSaoUnit (i,j, compIdx, pSaoParam, repeatedRow[compIdx]);
1845            }
1846          }
1847        }
1848      }
1849    }
1850  }
1851}
1852#endif
1853
1854Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize )
1855{
1856  Int count = 0;
1857 
1858  for ( Int i = 0; i < uiSize; i++ )
1859  {
1860    count += pcCoef[i] != 0;
1861  }
1862 
1863  return count;
1864}
1865
1866/** encode quantization matrix
1867 * \param scalingList quantization matrix information
1868 */
1869Void TEncEntropy::encodeScalingList( TComScalingList* scalingList )
1870{
1871  m_pcEntropyCoderIf->codeScalingList( scalingList );
1872}
1873
1874Void TEncEntropy::encodeDFParams(TComAPS* pcAPS)
1875{
1876  m_pcEntropyCoderIf->codeDFFlag(pcAPS->getLoopFilterDisable(), "loop_filter_disable");
1877
1878  if (!pcAPS->getLoopFilterDisable())
1879  {
1880    m_pcEntropyCoderIf->codeDFSvlc(pcAPS->getLoopFilterBetaOffset(), "beta_offset_div2");
1881    m_pcEntropyCoderIf->codeDFSvlc(pcAPS->getLoopFilterTcOffset(), "tc_offset_div2");
1882  }
1883}
1884
1885#if RWTH_SDC_DLT_B0036
1886#if !PKU_QC_DEPTH_INTRA_UNI_D0195
1887Void TEncEntropy::encodeSDCPredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1888{
1889  assert( pcCU->getSlice()->getSPS()->isDepth() );
1890 
1891  if( bRD )
1892    uiAbsPartIdx = 0;
1893 
1894  m_pcEntropyCoderIf->codeSDCPredMode(pcCU, uiAbsPartIdx);
1895}
1896
1897Void TEncEntropy::encodeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1898{
1899  assert( pcCU->getSlice()->getSPS()->isDepth() );
1900 
1901  if( bRD )
1902    uiAbsPartIdx = 0;
1903 
1904  m_pcEntropyCoderIf->codeSDCFlag(pcCU, uiAbsPartIdx);
1905}
1906Void TEncEntropy::encodeSDCResidualData( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1907{
1908  assert( pcCU->getSlice()->getSPS()->isDepth() );
1909  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
1910  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
1911  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
1912  assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
1913 
1914  if( bRD )
1915    uiAbsPartIdx = 0;
1916 
1917  // number of segments depends on prediction mode for INTRA
1918  UInt uiNumSegments = 2;
1919  UInt uiLumaPredMode = pcCU->getLumaIntraDir( uiAbsPartIdx );
1920  if( uiLumaPredMode == DC_IDX || uiLumaPredMode == PLANAR_IDX )
1921    uiNumSegments = 1;
1922 
1923  // encode residual data for each segment
1924  for( UInt uiSeg = 0; uiSeg < uiNumSegments; uiSeg++ )
1925    m_pcEntropyCoderIf->codeSDCResidualData(pcCU, uiAbsPartIdx, uiSeg);
1926}
1927#endif
1928
1929//! \}
Note: See TracBrowser for help on using the repository browser.