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

Last change on this file was 427, checked in by tech, 12 years ago

Merged HTM-6.2-dev1-LG

  • Property svn:eol-style set to native
File size: 54.4 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-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}
880
881// Split mode
882Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
883{
884  if( bRD )
885  {
886    uiAbsPartIdx = 0;
887  }
888  if( !bRD )
889  {
890    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
891    {
892      return;
893    }
894  }
895
896  m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
897}
898
899/** encode partition size
900 * \param pcCU
901 * \param uiAbsPartIdx
902 * \param uiDepth
903 * \param bRD
904 * \returns Void
905 */
906Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
907{
908  if( bRD )
909  {
910    uiAbsPartIdx = 0;
911  }
912  if( !bRD )
913  {
914    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
915    {
916      return;
917    }
918  }
919#if RWTH_SDC_DLT_B0036
920  if( !pcCU->getSlice()->isInterB() && pcCU->isIntra(uiAbsPartIdx) && pcCU->getSDCFlag(uiAbsPartIdx)  )
921  {
922    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
923    return;
924  }
925#endif
926 
927  m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth );
928 
929#if RWTH_SDC_DLT_B0036
930  // code SDC flag now!
931  if( pcCU->getSlice()->isInterB() && pcCU->isIntra(uiAbsPartIdx) && pcCU->getSlice()->getSPS()->isDepth() )
932  {
933    // encode SDC flag
934    encodeSDCFlag(pcCU, uiAbsPartIdx, bRD);
935   
936    if( pcCU->getSDCFlag(uiAbsPartIdx) )
937    {
938      // part size is also known for SDC intra
939      assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
940    }
941  }
942#endif
943}
944
945/** Encode I_PCM information.
946 * \param pcCU pointer to CU
947 * \param uiAbsPartIdx CU index
948 * \param bRD flag indicating estimation or encoding
949 * \returns Void
950 */
951Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
952{
953  if(!pcCU->getSlice()->getSPS()->getUsePCM()
954    || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize())
955    || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize()))
956  {
957    return;
958  }
959 
960#if RWTH_SDC_DLT_B0036
961  if( pcCU->getSDCFlag(uiAbsPartIdx) )
962  {
963    return;
964  }
965#endif
966 
967  if( bRD )
968  {
969    uiAbsPartIdx = 0;
970  }
971 
972  Int numIPCM = 0;
973  Bool firstIPCMFlag = false;
974
975  if( pcCU->getIPCMFlag(uiAbsPartIdx) )
976  {
977    numIPCM = 1;
978    firstIPCMFlag = true;
979
980    if( !bRD )
981    {
982      numIPCM = pcCU->getNumSucIPCM();
983      firstIPCMFlag = !pcCU->getLastCUSucIPCMFlag();
984    }
985  }
986  m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx, numIPCM, firstIPCMFlag);
987
988}
989
990Void 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 )
991{
992  const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) + pcCU->getDepth( uiAbsPartIdx ) > uiDepth;
993  const UInt uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()]+2 - uiDepth;
994  UInt cbfY = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA    , uiTrIdx );
995  UInt cbfU = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
996  UInt cbfV = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
997
998  if(uiTrIdx==0)
999  {
1000    m_bakAbsPartIdxCU = uiAbsPartIdx;
1001  }
1002  if( uiLog2TrafoSize == 2 )
1003  {
1004    UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
1005    if( ( uiAbsPartIdx % partNum ) == 0 )
1006    {
1007      m_uiBakAbsPartIdx   = uiAbsPartIdx;
1008      m_uiBakChromaOffset = offsetChroma;
1009    }
1010    else if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
1011    {
1012      cbfU = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
1013      cbfV = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
1014    }
1015  }
1016  {//CABAC
1017    if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) )
1018    {
1019      assert( uiSubdiv );
1020    }
1021    else if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) &&  (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) )
1022    {
1023      if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
1024      {
1025        assert( uiSubdiv );
1026      }
1027      else
1028      {
1029        assert(!uiSubdiv );
1030      }
1031    }
1032    else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1033    {
1034      assert( uiSubdiv );
1035    }
1036    else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
1037    {
1038      assert( !uiSubdiv );
1039    }
1040    else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
1041    {
1042      assert( !uiSubdiv );
1043    }
1044    else
1045    {
1046      assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
1047      m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, uiDepth );
1048    }
1049  }
1050
1051  {
1052    if( uiLog2TrafoSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1053    {
1054      const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx );
1055      const Bool bFirstCbfOfCU = uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() || uiTrDepthCurr == 0;
1056      if( bFirstCbfOfCU || uiLog2TrafoSize > 2 )
1057      {
1058        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) )
1059        {
1060          if ( uiInnerQuadIdx == 3 && uiUCbfFront3 == 0 && uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1061          {
1062            uiUCbfFront3++;
1063          }
1064          else
1065          {
1066            m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1067            uiUCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1068          }
1069        }
1070        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) )
1071        {
1072          if ( uiInnerQuadIdx == 3 && uiVCbfFront3 == 0 && uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize()  )
1073          {
1074            uiVCbfFront3++;
1075          }
1076          else
1077          {
1078            m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1079            uiVCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1080          }
1081        }
1082      }
1083      else if( uiLog2TrafoSize == 2 )
1084      {
1085        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) );
1086        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) );
1087       
1088        uiUCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1089        uiVCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1090      }
1091    }
1092   
1093    if( uiSubdiv )
1094    {
1095      UInt size;
1096      width  >>= 1;
1097      height >>= 1;
1098      size = width*height;
1099      uiTrIdx++;
1100      ++uiDepth;
1101      const UInt partNum = pcCU->getPic()->getNumPartInCU() >> (uiDepth << 1);
1102     
1103      UInt uiCurrentCbfY = 0;
1104      UInt uiCurrentCbfU = 0;
1105      UInt uiCurrentCbfV = 0;
1106     
1107      UInt nsAddr = 0;
1108      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 0, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1109      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 0, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1110
1111      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1112      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 1, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1113      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 1, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1114
1115      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1116      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 2, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1117      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 2, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1118
1119      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1120      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 3, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1121      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 3, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );     
1122     
1123      uiYCbfFront3 += uiCurrentCbfY;
1124      uiUCbfFront3 += uiCurrentCbfU;
1125      uiVCbfFront3 += uiCurrentCbfV;
1126    }
1127    else
1128    {
1129      {
1130        DTRACE_CABAC_VL( g_nSymbolCounter++ );
1131        DTRACE_CABAC_T( "\tTrIdx: abspart=" );
1132        DTRACE_CABAC_V( uiAbsPartIdx );
1133        DTRACE_CABAC_T( "\tdepth=" );
1134        DTRACE_CABAC_V( uiDepth );
1135        DTRACE_CABAC_T( "\ttrdepth=" );
1136        DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) );
1137        DTRACE_CABAC_T( "\n" );
1138      }
1139      UInt uiLumaTrMode, uiChromaTrMode;
1140      pcCU->convertTransIdx( uiAbsPartIdx, pcCU->getTransformIdx( uiAbsPartIdx ), uiLumaTrMode, uiChromaTrMode );
1141      if(pcCU->getPredictionMode( uiAbsPartIdx ) == MODE_INTER && pcCU->useNonSquarePU( uiAbsPartIdx ) )
1142      {
1143        pcCU->setNSQTIdxSubParts( uiLog2TrafoSize, uiAbsPartIdx, absTUPartIdx, uiLumaTrMode );
1144      }
1145      if( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, 0 ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, 0 ) )
1146      {
1147        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, 0 ) );
1148        //      printf( "saved one bin! " );
1149      }
1150      else
1151      {
1152        const UInt uiLog2CUSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - pcCU->getDepth( uiAbsPartIdx );
1153        if ( pcCU->getPredictionMode( uiAbsPartIdx ) != MODE_INTRA && uiInnerQuadIdx == 3 && uiYCbfFront3 == 0 && uiUCbfFront3 == 0 && uiVCbfFront3 == 0
1154            && ( uiLog2CUSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() + 1 || uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() ) )
1155        {     
1156          uiYCbfFront3++;
1157        }   
1158        else
1159        {
1160          m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, uiLumaTrMode );
1161          uiYCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, uiLumaTrMode );
1162        }
1163      }
1164     
1165      if ( cbfY || cbfU || cbfV )
1166      {
1167        // dQP: only for LCU once
1168        if ( pcCU->getSlice()->getPPS()->getUseDQP() )
1169        {
1170          if ( bCodeDQP )
1171          {
1172            encodeQP( pcCU, m_bakAbsPartIdxCU );
1173            bCodeDQP = false;
1174          }
1175        }
1176      }
1177      if( cbfY )
1178      {
1179        Int trWidth = width;
1180        Int trHeight = height;
1181        pcCU->getNSQTSize( uiTrIdx, uiAbsPartIdx, trWidth, trHeight );
1182        m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffY()+offsetLuma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
1183      }
1184      if( uiLog2TrafoSize > 2 )
1185      {
1186        Int trWidth = width >> 1;
1187        Int trHeight = height >> 1;
1188        pcCU->getNSQTSize( uiTrIdx, uiAbsPartIdx, trWidth, trHeight );
1189        if( cbfU )
1190        {
1191          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
1192        }
1193        if( cbfV )
1194        {
1195          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
1196        }
1197      }
1198      else
1199      {
1200        UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
1201        if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
1202        {
1203          Int trWidth = width;
1204          Int trHeight = height;
1205          pcCU->getNSQTSize( uiTrIdx - 1, uiAbsPartIdx, trWidth, trHeight );
1206          if( cbfU )
1207          {
1208            m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
1209          }
1210          if( cbfV )
1211          {
1212            m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
1213          }
1214        }
1215      }
1216    }
1217  }
1218}
1219
1220
1221// Intra direction for Luma
1222Void TEncEntropy::encodeIntraDirModeLuma  ( TComDataCU* pcCU, UInt uiAbsPartIdx )
1223{
1224  m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, uiAbsPartIdx );
1225}
1226
1227// Intra direction for Chroma
1228Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1229{
1230  if( bRD )
1231  {
1232    uiAbsPartIdx = 0;
1233  }
1234 
1235  m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx );
1236}
1237
1238Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1239{
1240  if( bRD )
1241  {
1242    uiAbsPartIdx = 0;
1243  }
1244
1245#if RWTH_SDC_DLT_B0036
1246  if( pcCU->getSDCFlag(uiAbsPartIdx) )
1247  {
1248    encodeSDCPredMode(pcCU, uiAbsPartIdx, bRD);
1249    return;
1250  }
1251#endif
1252
1253  PartSize eSize = pcCU->getPartitionSize( uiAbsPartIdx );
1254 
1255  if( pcCU->isIntra( uiAbsPartIdx ) )                                 // If it is Intra mode, encode intra prediction mode.
1256  {
1257    if( eSize == SIZE_NxN )                                         // if it is NxN size, encode 4 intra directions.
1258    {
1259      UInt uiPartOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(uiAbsPartIdx) << 1 ) ) >> 2;
1260      // if it is NxN size, this size might be the smallest partition size.
1261      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx                  );
1262      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset   );
1263      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset*2 );
1264      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset*3 );
1265      encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
1266    }
1267    else                                                              // if it is not NxN size, encode 1 intra directions
1268    {
1269      encodeIntraDirModeLuma  ( pcCU, uiAbsPartIdx );
1270      encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
1271    }
1272  }
1273  else                                                                // if it is Inter mode, encode motion vector and reference index
1274  {
1275    encodePUWise( pcCU, uiAbsPartIdx, bRD );
1276#if QC_ARP_D0177
1277    encodeARPW( pcCU , uiAbsPartIdx , bRD );
1278#endif
1279  }
1280}
1281
1282/** encode motion information for every PU block
1283 * \param pcCU
1284 * \param uiAbsPartIdx
1285 * \param bRD
1286 * \returns Void
1287 */
1288Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1289{
1290  if ( bRD )
1291  {
1292    uiAbsPartIdx = 0;
1293  }
1294 
1295  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
1296  UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) );
1297  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
1298  UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() - uiDepth ) << 1 ) ) >> 4;
1299
1300  for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset )
1301  {
1302    encodeMergeFlag( pcCU, uiSubPartIdx, uiPartIdx );
1303    if ( pcCU->getMergeFlag( uiSubPartIdx ) )
1304    {
1305      encodeMergeIndex( pcCU, uiSubPartIdx, uiPartIdx );
1306    }
1307    else
1308    {
1309      encodeInterDirPU( pcCU, uiSubPartIdx );
1310      for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1311      {
1312        if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1313        {
1314          encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1315          encodeMvdPU       ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1316          encodeMVPIdxPU    ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1317        }
1318      }
1319    }
1320  }
1321
1322  return;
1323}
1324
1325Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx )
1326{
1327  if ( !pcCU->getSlice()->isInterB() )
1328  {
1329    return;
1330  }
1331
1332  m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx );
1333  return;
1334}
1335
1336/** encode reference frame index for a PU block
1337 * \param pcCU
1338 * \param uiAbsPartIdx
1339 * \param eRefList
1340 * \returns Void
1341 */
1342Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1343{
1344  assert( !pcCU->isIntra( uiAbsPartIdx ) );
1345
1346  if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C)>0 && pcCU->getInterDir( uiAbsPartIdx ) != 3)
1347  {
1348    if ((eRefList== REF_PIC_LIST_1) || ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_C ) == 1 ) )
1349    {
1350      return;
1351    }
1352
1353    if ( pcCU->getSlice()->getNumRefIdx ( REF_PIC_LIST_C ) > 1 )
1354    {
1355      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, RefPicList(pcCU->getInterDir( uiAbsPartIdx )-1) );
1356    }
1357
1358  }
1359  else
1360  {
1361    if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) )
1362    {
1363      return;
1364    }
1365
1366    if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
1367    {
1368      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList );
1369    }
1370  }
1371
1372  return;
1373}
1374
1375/** encode motion vector difference for a PU block
1376 * \param pcCU
1377 * \param uiAbsPartIdx
1378 * \param eRefList
1379 * \returns Void
1380 */
1381Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1382{
1383  assert( !pcCU->isIntra( uiAbsPartIdx ) );
1384
1385  if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
1386  {
1387    m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList );
1388  }
1389  return;
1390}
1391
1392Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1393{
1394  if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) && (pcCU->getAMVPMode(uiAbsPartIdx) == AM_EXPL) )
1395  {
1396#if H3D_IVMP
1397#if SEC_TWO_CANDIDATES_FOR_AMVP_D0122
1398    const Int iNumCands = AMVP_MAX_NUM_CANDS;
1399#else
1400    const Int iNumCands = AMVP_MAX_NUM_CANDS + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
1401#endif
1402    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList, iNumCands );
1403#else
1404    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList );
1405#endif
1406  }
1407
1408  return;
1409}
1410
1411Void TEncEntropy::encodeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1412{
1413  m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, eType, uiTrDepth );
1414}
1415
1416Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1417{
1418  m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx );
1419}
1420
1421Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1422{
1423  m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
1424}
1425
1426// dQP
1427Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1428{
1429  if( bRD )
1430  {
1431    uiAbsPartIdx = 0;
1432  }
1433 
1434  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
1435  {
1436    m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx );
1437  }
1438}
1439
1440
1441// texture
1442
1443/** encode coefficients
1444 * \param pcCU
1445 * \param uiAbsPartIdx
1446 * \param uiDepth
1447 * \param uiWidth
1448 * \param uiHeight
1449 */
1450Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiWidth, UInt uiHeight, Bool& bCodeDQP )
1451{
1452  UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1453  UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1454  UInt uiChromaOffset = uiLumaOffset>>2;
1455 
1456  UInt uiLumaTrMode, uiChromaTrMode;
1457  pcCU->convertTransIdx( uiAbsPartIdx, pcCU->getTransformIdx(uiAbsPartIdx), uiLumaTrMode, uiChromaTrMode );
1458 
1459#if RWTH_SDC_DLT_B0036
1460  if( pcCU->getSDCFlag( uiAbsPartIdx ) )
1461  {
1462    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
1463    assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
1464    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
1465    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
1466    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
1467   
1468    encodeSDCResidualData(pcCU, uiAbsPartIdx);
1469    return;
1470  }
1471#endif
1472 
1473  if( pcCU->isIntra(uiAbsPartIdx) )
1474  {
1475    DTRACE_CABAC_VL( g_nSymbolCounter++ )
1476    DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" )
1477    DTRACE_CABAC_V( uiDepth )
1478    DTRACE_CABAC_T( "\n" )
1479  }
1480  else
1481  {
1482    {
1483#if HHI_MPI
1484      if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N &&
1485            ( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 || uiDepth == pcCU->getTextureModeDepth( uiAbsPartIdx ) ) ) )
1486#else
1487      if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) )
1488#endif
1489      {
1490        m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
1491      }
1492      if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) )
1493      {
1494#if 1 // MW Bug Fix
1495        pcCU->setCbfSubParts( 0, 0, 0, uiAbsPartIdx, uiDepth );
1496        pcCU->setTrIdxSubParts( 0 , uiAbsPartIdx, uiDepth );
1497#endif
1498        pcCU->setNSQTIdxSubParts( uiAbsPartIdx, uiDepth );
1499        return;
1500      }
1501    }
1502  }
1503 
1504#if FIX_MPI_B0065
1505  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getMergeIndex( uiAbsPartIdx ) == 0 && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N && pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1 )
1506  {
1507    TComDataCU *pcTextureCU = pcCU->getSlice()->getTexturePic()->getCU( pcCU->getAddr() );
1508    if( uiDepth == pcTextureCU->getDepth(uiAbsPartIdx))
1509    {
1510      PartSize partSize = pcTextureCU->getPartitionSize(uiAbsPartIdx);
1511      pcCU->setPartSizeSubParts( partSize, uiAbsPartIdx, uiDepth );
1512    }
1513    else
1514    {
1515      pcCU->setPartSizeSubParts( SIZE_NxN, uiAbsPartIdx, uiDepth );
1516    }
1517  }
1518#endif
1519
1520  UInt temp = 0;
1521  UInt temp1 = 0;
1522  UInt temp2 = 0;
1523  xEncodeTransform( pcCU, uiLumaOffset, uiChromaOffset, uiAbsPartIdx, uiAbsPartIdx, uiDepth, uiWidth, uiHeight, 0, 0, temp, temp1, temp2, bCodeDQP );
1524
1525#if FIX_MPI_B0065
1526  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getMergeIndex( uiAbsPartIdx ) == 0 && pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N &&  pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1 )
1527  {
1528    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth ); 
1529  }
1530#endif
1531}
1532
1533Void TEncEntropy::encodeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoeff, UInt uiAbsPartIdx, UInt uiTrWidth, UInt uiTrHeight, UInt uiDepth, TextType eType )
1534{ // This is for Transform unit processing. This may be used at mode selection stage for Inter.
1535  m_pcEntropyCoderIf->codeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiTrWidth, uiTrHeight, uiDepth, eType );
1536}
1537
1538Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType)
1539{ 
1540  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
1541 
1542  m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, height, eTType );
1543}
1544
1545#if LGE_SAO_MIGRATION_D0091
1546Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam, UInt compIdx)
1547{
1548    UInt uiSymbol;
1549    Int i;
1550
1551    uiSymbol = saoLcuParam->typeIdx + 1;
1552    if (compIdx!=2)
1553    {
1554        m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
1555    }
1556
1557    if (uiSymbol)
1558    {
1559        if (saoLcuParam->typeIdx < 4 && compIdx != 2)
1560        {
1561            saoLcuParam->subTypeIdx = saoLcuParam->typeIdx;
1562        }
1563#if FULL_NBIT
1564        Int offsetTh = 1 << ( min((Int)(g_uiBitDepth + (g_uiBitDepth-8)-5),5) );
1565#else
1566        Int offsetTh = 1 << ( min((Int)(g_uiBitDepth + g_uiBitIncrement-5),5) );
1567#endif
1568        if( saoLcuParam->typeIdx == SAO_BO )
1569        {
1570            for( i=0; i< saoLcuParam->length; i++)
1571            {
1572                UInt absOffset = ( (saoLcuParam->offset[i] < 0) ? -saoLcuParam->offset[i] : saoLcuParam->offset[i]);
1573                m_pcEntropyCoderIf->codeSaoMaxUvlc(absOffset, offsetTh-1);
1574            } 
1575            for( i=0; i< saoLcuParam->length; i++)
1576            {
1577                if (saoLcuParam->offset[i] != 0)
1578                {
1579                    UInt sign = (saoLcuParam->offset[i] < 0) ? 1 : 0 ;
1580                    m_pcEntropyCoderIf->codeSAOSign(sign);
1581                }
1582            }
1583            uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
1584            m_pcEntropyCoderIf->codeSaoUflc(5, uiSymbol);
1585        }
1586        else if( saoLcuParam->typeIdx < 4 )
1587        {
1588            m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[0], offsetTh-1);
1589            m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[1], offsetTh-1);
1590            m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[2], offsetTh-1);
1591            m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[3], offsetTh-1);
1592
1593            if (compIdx!=2)
1594            {
1595                uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
1596                m_pcEntropyCoderIf->codeSaoUflc(2, uiSymbol);
1597            }
1598        }
1599    }
1600}
1601
1602/** Encode SAO unit interleaving
1603* \param  rx
1604* \param  ry
1605* \param  pSaoParam
1606* \param  pcCU
1607* \param  iCUAddrInSlice
1608* \param  iCUAddrUpInSlice
1609* \param  bLFCrossSliceBoundaryFlag
1610 */
1611Void TEncEntropy::encodeSaoUnitInterleaving(Int compIdx, Bool saoFlag, Int rx, Int ry, SaoLcuParam* saoLcuParam, Int cuAddrInSlice, Int cuAddrUpInSlice, Int allowMergeLeft, Int allowMergeUp)
1612{
1613    if (saoFlag)
1614    {
1615        if (rx>0 && cuAddrInSlice!=0 && allowMergeLeft)
1616        {
1617            m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeLeftFlag);
1618        }
1619        else
1620        {
1621            saoLcuParam->mergeLeftFlag = 0;
1622        }
1623       
1624        if (saoLcuParam->mergeLeftFlag == 0)
1625        {
1626            if ( (ry > 0) && (cuAddrUpInSlice>=0) && allowMergeUp )
1627            {
1628                m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeUpFlag);
1629            }
1630            else
1631            {
1632                saoLcuParam->mergeUpFlag = 0;
1633            }
1634
1635            if (!saoLcuParam->mergeUpFlag)
1636            {
1637                encodeSaoOffset(saoLcuParam, compIdx);
1638            }
1639        }
1640    }
1641}
1642#else
1643/** Encode SAO Offset
1644 * \param  saoLcuParam SAO LCU paramters
1645 */
1646Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam)
1647{
1648  UInt uiSymbol;
1649  Int i;
1650
1651  uiSymbol = saoLcuParam->typeIdx + 1;
1652  m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
1653  if (uiSymbol)
1654  {
1655    if( saoLcuParam->typeIdx == SAO_BO )
1656    {
1657      // Code Left Band Index
1658      uiSymbol = (UInt) (saoLcuParam->bandPosition);
1659      m_pcEntropyCoderIf->codeSaoUflc(uiSymbol);
1660      for( i=0; i< saoLcuParam->length; i++)
1661      {
1662        m_pcEntropyCoderIf->codeSaoSvlc(saoLcuParam->offset[i]);
1663      } 
1664    }
1665    else
1666      if( saoLcuParam->typeIdx < 4 )
1667      {
1668        m_pcEntropyCoderIf->codeSaoUvlc( saoLcuParam->offset[0]);
1669        m_pcEntropyCoderIf->codeSaoUvlc( saoLcuParam->offset[1]);
1670        m_pcEntropyCoderIf->codeSaoUvlc(-saoLcuParam->offset[2]);
1671        m_pcEntropyCoderIf->codeSaoUvlc(-saoLcuParam->offset[3]);
1672      }
1673  }
1674}
1675/** Encode SAO unit
1676* \param  rx
1677* \param  ry
1678* \param  iCompIdx
1679* \param  pSaoParam
1680* \param  bRepeatedRow
1681 */
1682Void TEncEntropy::encodeSaoUnit(Int rx, Int ry, Int compIdx, SAOParam* saoParam, Int repeatedRow )
1683{
1684  int addr, addrLeft; 
1685  int numCuInWidth  = saoParam->numCuInWidth;
1686  SaoLcuParam* saoOneLcu;
1687  Int runLeft;
1688
1689  addr      =  rx + ry*numCuInWidth;
1690  addrLeft  =  (addr%numCuInWidth == 0) ? -1 : addr - 1;
1691
1692  if (!repeatedRow)
1693  {
1694    saoOneLcu = &(saoParam->saoLcuParam[compIdx][addr]);   
1695    runLeft = (addrLeft>=0 ) ? saoParam->saoLcuParam[compIdx][addrLeft].run : -1;
1696    if (rx == 0 || runLeft==0)
1697    {
1698      if (ry == 0)
1699      {
1700        m_pcEntropyCoderIf->codeSaoRun(saoOneLcu->runDiff, numCuInWidth-rx-1); 
1701        saoOneLcu->mergeUpFlag = 0;
1702      }
1703      else 
1704      {
1705        m_pcEntropyCoderIf->codeSaoSvlc(saoOneLcu->runDiff); 
1706        m_pcEntropyCoderIf->codeSaoFlag(saoOneLcu->mergeUpFlag); 
1707      }
1708      if (!saoOneLcu->mergeUpFlag)
1709      {
1710        encodeSaoOffset(saoOneLcu);
1711      }
1712    }
1713  }
1714}
1715
1716/** Encode SAO unit interleaving
1717* \param  rx
1718* \param  ry
1719* \param  pSaoParam
1720* \param  pcCU
1721* \param  iCUAddrInSlice
1722* \param  iCUAddrUpInSlice
1723* \param  bLFCrossSliceBoundaryFlag
1724 */
1725Void TEncEntropy::encodeSaoUnitInterleaving(Int rx, Int ry, SAOParam* saoParam, TComDataCU* cu, Int cuAddrInSlice, Int cuAddrUpInSlice, Bool lfCrossSliceBoundaryFlag)
1726{
1727  Int addr = cu->getAddr();
1728  for (Int compIdx=0; compIdx<3; compIdx++)
1729  {
1730    if (saoParam->bSaoFlag[compIdx])
1731    {
1732      if (rx>0 && cuAddrInSlice!=0)
1733      {
1734      m_pcEntropyCoderIf->codeSaoMergeLeft(saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag,compIdx);
1735      }
1736      else
1737      {
1738        saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag = 0;
1739      }
1740      if (saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag == 0)
1741      {
1742        if ( (ry > 0) && (cuAddrUpInSlice>0||lfCrossSliceBoundaryFlag))
1743        {
1744          m_pcEntropyCoderIf->codeSaoMergeUp(saoParam->saoLcuParam[compIdx][addr].mergeUpFlag);
1745        }
1746        else
1747        {
1748          saoParam->saoLcuParam[compIdx][addr].mergeUpFlag = 0;
1749        }
1750        if (!saoParam->saoLcuParam[compIdx][addr].mergeUpFlag)
1751        {
1752          encodeSaoOffset(&(saoParam->saoLcuParam[compIdx][addr]));
1753        }
1754      }
1755    }
1756  }
1757}
1758
1759/** Encode SAO parameter
1760* \param  pcAPS
1761 */
1762Void TEncEntropy::encodeSaoParam(TComAPS* aps)
1763{
1764  SaoLcuParam* psSaoOneLcu;
1765  int i,j,k, compIdx; 
1766  int numCuInWidth  ;
1767  int numCuInHeight ;
1768  Bool repeatedRow[3];
1769  Int addr;
1770  m_pcEntropyCoderIf->codeSaoFlag(aps->getSaoInterleavingFlag()); 
1771  if(!aps->getSaoInterleavingFlag())
1772  {
1773    m_pcEntropyCoderIf->codeSaoFlag(aps->getSaoEnabled()); 
1774    if (aps->getSaoEnabled())
1775    {
1776      SAOParam* pSaoParam = aps->getSaoParam();
1777      numCuInWidth  = pSaoParam->numCuInWidth;
1778      numCuInHeight = pSaoParam->numCuInHeight;
1779      m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->bSaoFlag[1]); 
1780      m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->bSaoFlag[2]); 
1781      m_pcEntropyCoderIf->codeSaoUvlc(numCuInWidth-1); 
1782      m_pcEntropyCoderIf->codeSaoUvlc(numCuInHeight-1); 
1783      for (compIdx=0;compIdx<3;compIdx++)
1784      {
1785        if (pSaoParam->bSaoFlag[compIdx])
1786        {
1787          m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->oneUnitFlag[compIdx]); 
1788          if (pSaoParam->oneUnitFlag[compIdx])
1789          {
1790            psSaoOneLcu = &(pSaoParam->saoLcuParam[compIdx][0]);   
1791            encodeSaoOffset(psSaoOneLcu);
1792          }
1793        }
1794      }
1795
1796      for (j=0;j<numCuInHeight;j++)
1797      {
1798        for (compIdx=0; compIdx<3; compIdx++)
1799        {
1800          repeatedRow[compIdx] = true;
1801          for (k=0;k<numCuInWidth;k++)
1802          {
1803            addr       =  k + j*numCuInWidth;
1804            psSaoOneLcu = &(pSaoParam->saoLcuParam[compIdx][addr]);   
1805            if (!psSaoOneLcu->mergeUpFlag || psSaoOneLcu->runDiff)
1806            {
1807              repeatedRow[compIdx] = false;
1808              break;
1809            }
1810          }
1811        }
1812        for (i=0;i<numCuInWidth;i++)
1813        {
1814          for (compIdx=0; compIdx<3; compIdx++)
1815          {
1816            if (pSaoParam->bSaoFlag[compIdx]  && !pSaoParam->oneUnitFlag[compIdx]) 
1817            {
1818              if (j>0 && i==0) 
1819              {
1820                m_pcEntropyCoderIf->codeSaoFlag(repeatedRow[compIdx]); 
1821              }
1822              encodeSaoUnit (i,j, compIdx, pSaoParam, repeatedRow[compIdx]);
1823            }
1824          }
1825        }
1826      }
1827    }
1828  }
1829}
1830#endif
1831
1832Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize )
1833{
1834  Int count = 0;
1835 
1836  for ( Int i = 0; i < uiSize; i++ )
1837  {
1838    count += pcCoef[i] != 0;
1839  }
1840 
1841  return count;
1842}
1843
1844/** encode quantization matrix
1845 * \param scalingList quantization matrix information
1846 */
1847Void TEncEntropy::encodeScalingList( TComScalingList* scalingList )
1848{
1849  m_pcEntropyCoderIf->codeScalingList( scalingList );
1850}
1851
1852Void TEncEntropy::encodeDFParams(TComAPS* pcAPS)
1853{
1854  m_pcEntropyCoderIf->codeDFFlag(pcAPS->getLoopFilterDisable(), "loop_filter_disable");
1855
1856  if (!pcAPS->getLoopFilterDisable())
1857  {
1858    m_pcEntropyCoderIf->codeDFSvlc(pcAPS->getLoopFilterBetaOffset(), "beta_offset_div2");
1859    m_pcEntropyCoderIf->codeDFSvlc(pcAPS->getLoopFilterTcOffset(), "tc_offset_div2");
1860  }
1861}
1862
1863#if RWTH_SDC_DLT_B0036
1864Void TEncEntropy::encodeSDCPredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1865{
1866  assert( pcCU->getSlice()->getSPS()->isDepth() );
1867 
1868  if( bRD )
1869    uiAbsPartIdx = 0;
1870 
1871  m_pcEntropyCoderIf->codeSDCPredMode(pcCU, uiAbsPartIdx);
1872}
1873
1874Void TEncEntropy::encodeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1875{
1876  assert( pcCU->getSlice()->getSPS()->isDepth() );
1877 
1878  if( bRD )
1879    uiAbsPartIdx = 0;
1880 
1881  m_pcEntropyCoderIf->codeSDCFlag(pcCU, uiAbsPartIdx);
1882}
1883
1884Void TEncEntropy::encodeSDCResidualData( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1885{
1886  assert( pcCU->getSlice()->getSPS()->isDepth() );
1887  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
1888  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
1889  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
1890  assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
1891 
1892  if( bRD )
1893    uiAbsPartIdx = 0;
1894 
1895  // number of segments depends on prediction mode for INTRA
1896  UInt uiNumSegments = 2;
1897  UInt uiLumaPredMode = pcCU->getLumaIntraDir( uiAbsPartIdx );
1898  if( uiLumaPredMode == DC_IDX || uiLumaPredMode == PLANAR_IDX )
1899    uiNumSegments = 1;
1900 
1901  // encode residual data for each segment
1902  for( UInt uiSeg = 0; uiSeg < uiNumSegments; uiSeg++ )
1903    m_pcEntropyCoderIf->codeSDCResidualData(pcCU, uiAbsPartIdx, uiSeg);
1904}
1905#endif
1906
1907//! \}
Note: See TracBrowser for help on using the repository browser.