source: 3DVCSoftware/branches/HTM-6.2-dev1-LG/source/Lib/TLibEncoder/TEncEntropy.cpp @ 408

Last change on this file since 408 was 408, checked in by lg, 12 years ago

D0135->D0092->D0091

  • Property svn:eol-style set to native
File size: 53.5 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/** parse the fixed length code (smaller than one max value) in ALF
470 * \param run: coded value
471 * \param rx: cur addr
472 * \param numLCUInWidth: # of LCU in one LCU
473 * \returns Void
474 */
475Void TEncEntropy::encodeAlfFixedLengthRun(UInt run, UInt rx, UInt numLCUInWidth)
476{
477  assert(numLCUInWidth > rx);
478  UInt maxValue = numLCUInWidth - rx - 1;
479  m_pcEntropyCoderIf->codeAlfFixedLengthIdx(run, maxValue);
480}
481
482/** parse the fixed length code (smaller than one max value) in ALF
483 * \param idx: coded value
484 * \param numFilterSetsInBuffer: max value
485 * \returns Void
486 */
487Void TEncEntropy::encodeAlfStoredFilterSetIdx(UInt idx, UInt numFilterSetsInBuffer)
488{
489  assert(numFilterSetsInBuffer > 0);
490  UInt maxValue = numFilterSetsInBuffer - 1;
491  m_pcEntropyCoderIf->codeAlfFixedLengthIdx(idx, maxValue);
492}
493
494Void TEncEntropy::encodeAlfParam(AlfParamSet* pAlfParamSet, Bool bSentInAPS, Int firstLCUAddr, Bool alfAcrossSlice)
495{
496  Bool isEnabled[NUM_ALF_COMPONENT];
497  Bool isUniParam[NUM_ALF_COMPONENT];
498
499  isEnabled[ALF_Y] = true;
500  isEnabled[ALF_Cb]= pAlfParamSet->isEnabled[ALF_Cb];
501  isEnabled[ALF_Cr]= pAlfParamSet->isEnabled[ALF_Cr];
502
503  isUniParam[ALF_Y]= pAlfParamSet->isUniParam[ALF_Y];
504  isUniParam[ALF_Cb]= pAlfParamSet->isUniParam[ALF_Cb];
505  isUniParam[ALF_Cr]= pAlfParamSet->isUniParam[ALF_Cr]; 
506
507
508  //alf_cb_enable_flag
509  m_pcEntropyCoderIf->codeAlfFlag(isEnabled[ALF_Cb]?1:0);
510  //alf_cr_enable_flag
511  m_pcEntropyCoderIf->codeAlfFlag(isEnabled[ALF_Cr]?1:0); 
512
513  for(Int compIdx = 0; compIdx< NUM_ALF_COMPONENT; compIdx++)
514  {
515    if(isEnabled[compIdx])
516    {
517      //alf_one_{luma, cb, cr}_unit_per_slice_flag
518      m_pcEntropyCoderIf->codeAlfFlag(isUniParam[compIdx]?1:0);
519    }
520  }
521  if(bSentInAPS)
522  {
523    //alf_num_lcu_in_width_minus1
524    m_pcEntropyCoderIf->codeAlfUvlc(pAlfParamSet->numLCUInWidth-1);
525    //alf_num_lcu_in_height_minus1
526    m_pcEntropyCoderIf->codeAlfUvlc(pAlfParamSet->numLCUInHeight-1);
527  }
528  else //sent in slice header
529  {
530    //alf_num_lcu_in_slice_minus1
531    m_pcEntropyCoderIf->codeAlfUvlc(pAlfParamSet->numLCU-1);
532  }
533
534
535  encodeAlfParamSet(pAlfParamSet, pAlfParamSet->numLCUInWidth, pAlfParamSet->numLCU, firstLCUAddr, alfAcrossSlice, 0, (Int)NUM_ALF_COMPONENT-1);
536
537}
538
539Bool TEncEntropy::getAlfRepeatRowFlag(Int compIdx, AlfParamSet* pAlfParamSet
540                                    , Int lcuIdxInSlice, Int lcuPos
541                                    , Int startlcuPosX, Int endlcuPosX
542                                    , Int numLCUInWidth
543                                    )
544{
545  assert(startlcuPosX == 0); //only the beginning of one LCU row needs to send repeat_row_flag
546 
547  Int len = endlcuPosX - startlcuPosX +1;
548  Bool isRepeatRow = true;
549  Int curPos;
550
551  for(Int i= 0; i < len; i++)
552  {
553    curPos = lcuIdxInSlice +i;
554    AlfUnitParam& alfUnitParam = pAlfParamSet->alfUnitParam[compIdx][curPos];
555    AlfUnitParam& alfUpUnitParam = pAlfParamSet->alfUnitParam[compIdx][curPos-numLCUInWidth];
556
557    if ( !(alfUnitParam == alfUpUnitParam) )
558    {
559      isRepeatRow = false;
560      break;
561    }
562  }
563
564  return isRepeatRow;
565}
566
567
568Int TEncEntropy::getAlfRun(Int compIdx, AlfParamSet* pAlfParamSet
569                          , Int lcuIdxInSlice, Int lcuPos
570                          , Int startlcuPosX, Int endlcuPosX
571                          )
572{
573  Int alfRun = 0;
574  Int len = endlcuPosX - startlcuPosX +1;
575  AlfUnitParam& alfLeftUnitParam = pAlfParamSet->alfUnitParam[compIdx][lcuIdxInSlice];
576
577
578
579  for(Int i= 1; i < len; i++)
580  {
581    AlfUnitParam& alfUnitParam = pAlfParamSet->alfUnitParam[compIdx][lcuIdxInSlice+ i];
582
583    if (alfUnitParam == alfLeftUnitParam)
584    {
585      alfRun++;
586    }
587    else
588    {
589      break;
590    }
591  }
592
593  return alfRun;
594
595}
596
597
598
599Void TEncEntropy::encodeAlfParamSet(AlfParamSet* pAlfParamSet, Int numLCUInWidth, Int numLCU, Int firstLCUAddr, Bool alfAcrossSlice, Int startCompIdx, Int endCompIdx)
600{
601  Int endLCUY       = (numLCU -1 + firstLCUAddr)/numLCUInWidth;
602  Int endLCUX       = (numLCU -1 + firstLCUAddr)%numLCUInWidth;
603
604  static Bool isRepeatedRow   [NUM_ALF_COMPONENT];
605  static Int  numStoredFilters[NUM_ALF_COMPONENT];
606  static Int* run             [NUM_ALF_COMPONENT];
607
608  for(Int compIdx =startCompIdx; compIdx <= endCompIdx; compIdx++)
609  {
610    isRepeatedRow[compIdx]    = false;
611    numStoredFilters[compIdx] = 0;
612
613    run[compIdx] = new Int[numLCU+1];
614    run[compIdx][0] = -1; 
615  }
616
617  Int  ry, rx, addrUp, endrX, lcuPos;
618
619  for(Int i=0; i< numLCU; i++)
620  {
621    lcuPos= firstLCUAddr+ i;
622    rx    = lcuPos% numLCUInWidth;
623    ry    = lcuPos/ numLCUInWidth;
624    endrX = ( ry == endLCUY)?( endLCUX ):(numLCUInWidth-1);
625
626    for(Int compIdx =startCompIdx; compIdx <= endCompIdx; compIdx++)
627    {
628      AlfUnitParam& alfUnitParam = pAlfParamSet->alfUnitParam[compIdx][i];
629      if(pAlfParamSet->isEnabled[compIdx])
630      {
631        if(!pAlfParamSet->isUniParam[compIdx])
632        {
633          addrUp = i-numLCUInWidth;
634          if(rx ==0 && addrUp >=0)
635          {
636            isRepeatedRow[compIdx] = getAlfRepeatRowFlag(compIdx, pAlfParamSet, i, lcuPos, rx, endrX, numLCUInWidth);
637
638            //alf_repeat_row_flag
639            m_pcEntropyCoderIf->codeAlfFlag(isRepeatedRow[compIdx]?1:0);
640          }
641
642          if(isRepeatedRow[compIdx])
643          {
644            assert(addrUp >=0);
645            run[compIdx][i] = run[compIdx][addrUp];
646          }
647          else
648          {
649            if(rx == 0 || run[compIdx][i] < 0)
650            {             
651              run[compIdx][i] = getAlfRun(compIdx, pAlfParamSet, i, lcuPos, rx, endrX);
652
653              if(addrUp < 0)
654              {
655                //alf_run_diff u(v)
656                encodeAlfFixedLengthRun(run[compIdx][i], rx, numLCUInWidth);               
657              }
658              else
659              {
660                //alf_run_diff s(v)
661                m_pcEntropyCoderIf->codeAlfSvlc(run[compIdx][i]- run[compIdx][addrUp]);
662
663              }
664
665              if(ry > 0 && (addrUp >=0 || alfAcrossSlice))
666              {
667                //alf_merge_up_flag
668                m_pcEntropyCoderIf->codeAlfFlag(  (alfUnitParam.mergeType == ALF_MERGE_UP)?1:0   ); 
669              }
670
671              if(alfUnitParam.mergeType != ALF_MERGE_UP)
672              {
673                assert(alfUnitParam.mergeType == ALF_MERGE_DISABLED);
674
675                //alf_lcu_enable_flag
676                m_pcEntropyCoderIf->codeAlfFlag(alfUnitParam.isEnabled ? 1 : 0);
677
678                if(alfUnitParam.isEnabled)
679                {
680                  if(numStoredFilters[compIdx] > 0)
681                  {
682                    //alf_new_filter_set_flag
683                    m_pcEntropyCoderIf->codeAlfFlag(alfUnitParam.isNewFilt ? 1:0);
684
685                    if(!alfUnitParam.isNewFilt)
686                    {
687                      //alf_stored_filter_set_idx
688                      encodeAlfStoredFilterSetIdx(alfUnitParam.storedFiltIdx, numStoredFilters[compIdx]);
689
690                    }
691                  }
692                  else
693                  {
694                    assert(alfUnitParam.isNewFilt);
695                  }
696
697                  if(alfUnitParam.isNewFilt)
698                  {
699                    assert(alfUnitParam.alfFiltParam->alf_flag == 1);
700                    encodeAlfParam(alfUnitParam.alfFiltParam);
701                    numStoredFilters[compIdx]++;
702                  }
703                }
704
705              }
706            }
707
708            run[compIdx][i+1] = run[compIdx][i] -1;
709          }
710
711        }
712        else // uni-param
713        {
714          if(i == 0)
715          {
716            //alf_lcu_enable_flag
717            m_pcEntropyCoderIf->codeAlfFlag(alfUnitParam.isEnabled?1:0);
718            if(alfUnitParam.isEnabled)
719            {
720              encodeAlfParam(alfUnitParam.alfFiltParam);
721            }
722          }
723        }
724      } // component enabled/disable
725    } //comp
726
727  }
728
729  for(Int compIdx =startCompIdx; compIdx <= endCompIdx; compIdx++)
730  {
731    delete[] run[compIdx];
732  }
733
734}
735
736
737Void TEncEntropy::encodeAlfParam(ALFParam* pAlfParam)
738{
739  const Int numCoeff = (Int)ALF_MAX_NUM_COEF;
740
741  switch(pAlfParam->componentID)
742  {
743  case ALF_Cb:
744  case ALF_Cr:
745    {
746      for(Int pos=0; pos< numCoeff; pos++)
747      {
748        m_pcEntropyCoderIf->codeAlfSvlc(  pAlfParam->coeffmulti[0][pos]);
749
750      }
751    }
752    break;
753  case ALF_Y:
754    {
755      codeAux(pAlfParam);
756      codeFilt(pAlfParam);
757    }
758    break;
759  default:
760    {
761      printf("Not a legal component ID\n");
762      assert(0);
763      exit(-1);
764    }
765  }
766}
767
768Void TEncEntropy::encodeAlfCtrlFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
769{
770  if( bRD )
771  {
772    uiAbsPartIdx = 0;
773  }
774  m_pcEntropyCoderIf->codeAlfCtrlFlag( pcCU, uiAbsPartIdx );
775}
776
777
778/** Encode ALF CU control flag
779 * \param uiFlag ALF CU control flag: 0 or 1
780 */
781Void TEncEntropy::encodeAlfCtrlFlag(UInt uiFlag)
782{
783  assert(uiFlag == 0 || uiFlag == 1);
784  m_pcEntropyCoderIf->codeAlfCtrlFlag( uiFlag );
785}
786
787
788/** Encode ALF CU control flag parameters
789 * \param pAlfParam ALF parameters
790 */
791Void TEncEntropy::encodeAlfCtrlParam(AlfCUCtrlInfo& cAlfParam, Int iNumCUsInPic)
792{
793  // region control parameters for luma
794  m_pcEntropyCoderIf->codeAlfFlag(cAlfParam.cu_control_flag);
795
796  if (cAlfParam.cu_control_flag == 0)
797  { 
798    return;
799  }
800
801  m_pcEntropyCoderIf->codeAlfCtrlDepth();
802
803  Int iSymbol    = ((Int)cAlfParam.num_alf_cu_flag - iNumCUsInPic);
804  m_pcEntropyCoderIf->codeAlfSvlc(iSymbol);
805
806  for(UInt i=0; i< cAlfParam.num_alf_cu_flag; i++)
807  {
808    m_pcEntropyCoderIf->codeAlfCtrlFlag( cAlfParam.alf_cu_flag[i] );
809  }
810}
811
812/** encode prediction mode
813 * \param pcCU
814 * \param uiAbsPartIdx
815 * \param bRD
816 * \returns Void
817 */
818Void TEncEntropy::encodePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
819{
820  if( bRD )
821  {
822    uiAbsPartIdx = 0;
823  }
824  if( !bRD )
825  {
826    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
827    {
828      return;
829    }
830  }
831
832#if !RWTH_SDC_DLT_B0036
833  if ( pcCU->getSlice()->isIntra() )
834  {
835    return;
836  }
837#endif
838
839  m_pcEntropyCoderIf->codePredMode( pcCU, uiAbsPartIdx );
840 
841#if RWTH_SDC_DLT_B0036
842  // if B-Slice, code SDC flag later
843  if( !pcCU->getSlice()->isInterB() && pcCU->getSlice()->getSPS()->isDepth() && pcCU->isIntra(uiAbsPartIdx) )
844  {
845    // encode SDC flag
846    encodeSDCFlag(pcCU, uiAbsPartIdx, bRD);
847  }
848#endif
849}
850
851// Split mode
852Void TEncEntropy::encodeSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
853{
854  if( bRD )
855  {
856    uiAbsPartIdx = 0;
857  }
858  if( !bRD )
859  {
860    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
861    {
862      return;
863    }
864  }
865
866  m_pcEntropyCoderIf->codeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
867}
868
869/** encode partition size
870 * \param pcCU
871 * \param uiAbsPartIdx
872 * \param uiDepth
873 * \param bRD
874 * \returns Void
875 */
876Void TEncEntropy::encodePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, Bool bRD )
877{
878  if( bRD )
879  {
880    uiAbsPartIdx = 0;
881  }
882  if( !bRD )
883  {
884    if( pcCU->getLastCUSucIPCMFlag() && pcCU->getIPCMFlag(uiAbsPartIdx) )
885    {
886      return;
887    }
888  }
889#if RWTH_SDC_DLT_B0036
890  if( !pcCU->getSlice()->isInterB() && pcCU->isIntra(uiAbsPartIdx) && pcCU->getSDCFlag(uiAbsPartIdx)  )
891  {
892    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
893    return;
894  }
895#endif
896 
897  m_pcEntropyCoderIf->codePartSize( pcCU, uiAbsPartIdx, uiDepth );
898 
899#if RWTH_SDC_DLT_B0036
900  // code SDC flag now!
901  if( pcCU->getSlice()->isInterB() && pcCU->isIntra(uiAbsPartIdx) && pcCU->getSlice()->getSPS()->isDepth() )
902  {
903    // encode SDC flag
904    encodeSDCFlag(pcCU, uiAbsPartIdx, bRD);
905   
906    if( pcCU->getSDCFlag(uiAbsPartIdx) )
907    {
908      // part size is also known for SDC intra
909      assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
910    }
911  }
912#endif
913}
914
915/** Encode I_PCM information.
916 * \param pcCU pointer to CU
917 * \param uiAbsPartIdx CU index
918 * \param bRD flag indicating estimation or encoding
919 * \returns Void
920 */
921Void TEncEntropy::encodeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
922{
923  if(!pcCU->getSlice()->getSPS()->getUsePCM()
924    || pcCU->getWidth(uiAbsPartIdx) > (1<<pcCU->getSlice()->getSPS()->getPCMLog2MaxSize())
925    || pcCU->getWidth(uiAbsPartIdx) < (1<<pcCU->getSlice()->getSPS()->getPCMLog2MinSize()))
926  {
927    return;
928  }
929 
930#if RWTH_SDC_DLT_B0036
931  if( pcCU->getSDCFlag(uiAbsPartIdx) )
932  {
933    return;
934  }
935#endif
936 
937  if( bRD )
938  {
939    uiAbsPartIdx = 0;
940  }
941 
942  Int numIPCM = 0;
943  Bool firstIPCMFlag = false;
944
945  if( pcCU->getIPCMFlag(uiAbsPartIdx) )
946  {
947    numIPCM = 1;
948    firstIPCMFlag = true;
949
950    if( !bRD )
951    {
952      numIPCM = pcCU->getNumSucIPCM();
953      firstIPCMFlag = !pcCU->getLastCUSucIPCMFlag();
954    }
955  }
956  m_pcEntropyCoderIf->codeIPCMInfo ( pcCU, uiAbsPartIdx, numIPCM, firstIPCMFlag);
957
958}
959
960Void 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 )
961{
962  const UInt uiSubdiv = pcCU->getTransformIdx( uiAbsPartIdx ) + pcCU->getDepth( uiAbsPartIdx ) > uiDepth;
963  const UInt uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()]+2 - uiDepth;
964  UInt cbfY = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA    , uiTrIdx );
965  UInt cbfU = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
966  UInt cbfV = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
967
968  if(uiTrIdx==0)
969  {
970    m_bakAbsPartIdxCU = uiAbsPartIdx;
971  }
972  if( uiLog2TrafoSize == 2 )
973  {
974    UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
975    if( ( uiAbsPartIdx % partNum ) == 0 )
976    {
977      m_uiBakAbsPartIdx   = uiAbsPartIdx;
978      m_uiBakChromaOffset = offsetChroma;
979    }
980    else if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
981    {
982      cbfU = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_U, uiTrIdx );
983      cbfV = pcCU->getCbf( m_uiBakAbsPartIdx, TEXT_CHROMA_V, uiTrIdx );
984    }
985  }
986  {//CABAC
987    if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_NxN && uiDepth == pcCU->getDepth(uiAbsPartIdx) )
988    {
989      assert( uiSubdiv );
990    }
991    else if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && (pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N) && uiDepth == pcCU->getDepth(uiAbsPartIdx) &&  (pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) )
992    {
993      if ( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
994      {
995        assert( uiSubdiv );
996      }
997      else
998      {
999        assert(!uiSubdiv );
1000      }
1001    }
1002    else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1003    {
1004      assert( uiSubdiv );
1005    }
1006    else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
1007    {
1008      assert( !uiSubdiv );
1009    }
1010    else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
1011    {
1012      assert( !uiSubdiv );
1013    }
1014    else
1015    {
1016      assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
1017      m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSubdiv, uiDepth );
1018    }
1019  }
1020
1021  {
1022    if( uiLog2TrafoSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1023    {
1024      const UInt uiTrDepthCurr = uiDepth - pcCU->getDepth( uiAbsPartIdx );
1025      const Bool bFirstCbfOfCU = uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() || uiTrDepthCurr == 0;
1026      if( bFirstCbfOfCU || uiLog2TrafoSize > 2 )
1027      {
1028        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) )
1029        {
1030          if ( uiInnerQuadIdx == 3 && uiUCbfFront3 == 0 && uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
1031          {
1032            uiUCbfFront3++;
1033          }
1034          else
1035          {
1036            m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1037            uiUCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1038          }
1039        }
1040        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) )
1041        {
1042          if ( uiInnerQuadIdx == 3 && uiVCbfFront3 == 0 && uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize()  )
1043          {
1044            uiVCbfFront3++;
1045          }
1046          else
1047          {
1048            m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1049            uiVCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1050          }
1051        }
1052      }
1053      else if( uiLog2TrafoSize == 2 )
1054      {
1055        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr - 1 ) );
1056        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr - 1 ) );
1057       
1058        uiUCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepthCurr );
1059        uiVCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepthCurr );
1060      }
1061    }
1062   
1063    if( uiSubdiv )
1064    {
1065      UInt size;
1066      width  >>= 1;
1067      height >>= 1;
1068      size = width*height;
1069      uiTrIdx++;
1070      ++uiDepth;
1071      const UInt partNum = pcCU->getPic()->getNumPartInCU() >> (uiDepth << 1);
1072     
1073      UInt uiCurrentCbfY = 0;
1074      UInt uiCurrentCbfU = 0;
1075      UInt uiCurrentCbfV = 0;
1076     
1077      UInt nsAddr = 0;
1078      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 0, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1079      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 0, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1080
1081      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1082      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 1, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1083      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 1, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1084
1085      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1086      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 2, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1087      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 2, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );
1088
1089      uiAbsPartIdx += partNum;  offsetLuma += size;  offsetChroma += (size>>2);
1090      nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrafoSize-1, uiAbsPartIdx, absTUPartIdx, 3, uiDepth - pcCU->getDepth( uiAbsPartIdx ) );
1091      xEncodeTransform( pcCU, offsetLuma, offsetChroma, uiAbsPartIdx, nsAddr, uiDepth, width, height, uiTrIdx, 3, uiCurrentCbfY, uiCurrentCbfU, uiCurrentCbfV, bCodeDQP );     
1092     
1093      uiYCbfFront3 += uiCurrentCbfY;
1094      uiUCbfFront3 += uiCurrentCbfU;
1095      uiVCbfFront3 += uiCurrentCbfV;
1096    }
1097    else
1098    {
1099      {
1100        DTRACE_CABAC_VL( g_nSymbolCounter++ );
1101        DTRACE_CABAC_T( "\tTrIdx: abspart=" );
1102        DTRACE_CABAC_V( uiAbsPartIdx );
1103        DTRACE_CABAC_T( "\tdepth=" );
1104        DTRACE_CABAC_V( uiDepth );
1105        DTRACE_CABAC_T( "\ttrdepth=" );
1106        DTRACE_CABAC_V( pcCU->getTransformIdx( uiAbsPartIdx ) );
1107        DTRACE_CABAC_T( "\n" );
1108      }
1109      UInt uiLumaTrMode, uiChromaTrMode;
1110      pcCU->convertTransIdx( uiAbsPartIdx, pcCU->getTransformIdx( uiAbsPartIdx ), uiLumaTrMode, uiChromaTrMode );
1111      if(pcCU->getPredictionMode( uiAbsPartIdx ) == MODE_INTER && pcCU->useNonSquarePU( uiAbsPartIdx ) )
1112      {
1113        pcCU->setNSQTIdxSubParts( uiLog2TrafoSize, uiAbsPartIdx, absTUPartIdx, uiLumaTrMode );
1114      }
1115      if( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA && uiDepth == pcCU->getDepth( uiAbsPartIdx ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, 0 ) && !pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, 0 ) )
1116      {
1117        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, 0 ) );
1118        //      printf( "saved one bin! " );
1119      }
1120      else
1121      {
1122        const UInt uiLog2CUSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - pcCU->getDepth( uiAbsPartIdx );
1123        if ( pcCU->getPredictionMode( uiAbsPartIdx ) != MODE_INTRA && uiInnerQuadIdx == 3 && uiYCbfFront3 == 0 && uiUCbfFront3 == 0 && uiVCbfFront3 == 0
1124            && ( uiLog2CUSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() + 1 || uiLog2TrafoSize < pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() ) )
1125        {     
1126          uiYCbfFront3++;
1127        }   
1128        else
1129        {
1130          m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, uiLumaTrMode );
1131          uiYCbfFront3 += pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, uiLumaTrMode );
1132        }
1133      }
1134     
1135      if ( cbfY || cbfU || cbfV )
1136      {
1137        // dQP: only for LCU once
1138        if ( pcCU->getSlice()->getPPS()->getUseDQP() )
1139        {
1140          if ( bCodeDQP )
1141          {
1142            encodeQP( pcCU, m_bakAbsPartIdxCU );
1143            bCodeDQP = false;
1144          }
1145        }
1146      }
1147      if( cbfY )
1148      {
1149        Int trWidth = width;
1150        Int trHeight = height;
1151        pcCU->getNSQTSize( uiTrIdx, uiAbsPartIdx, trWidth, trHeight );
1152        m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffY()+offsetLuma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
1153      }
1154      if( uiLog2TrafoSize > 2 )
1155      {
1156        Int trWidth = width >> 1;
1157        Int trHeight = height >> 1;
1158        pcCU->getNSQTSize( uiTrIdx, uiAbsPartIdx, trWidth, trHeight );
1159        if( cbfU )
1160        {
1161          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
1162        }
1163        if( cbfV )
1164        {
1165          m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+offsetChroma), uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
1166        }
1167      }
1168      else
1169      {
1170        UInt partNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiDepth - 1 ) << 1 );
1171        if( ( uiAbsPartIdx % partNum ) == (partNum - 1) )
1172        {
1173          Int trWidth = width;
1174          Int trHeight = height;
1175          pcCU->getNSQTSize( uiTrIdx - 1, uiAbsPartIdx, trWidth, trHeight );
1176          if( cbfU )
1177          {
1178            m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCb()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
1179          }
1180          if( cbfV )
1181          {
1182            m_pcEntropyCoderIf->codeCoeffNxN( pcCU, (pcCU->getCoeffCr()+m_uiBakChromaOffset), m_uiBakAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
1183          }
1184        }
1185      }
1186    }
1187  }
1188}
1189
1190
1191// Intra direction for Luma
1192Void TEncEntropy::encodeIntraDirModeLuma  ( TComDataCU* pcCU, UInt uiAbsPartIdx )
1193{
1194  m_pcEntropyCoderIf->codeIntraDirLumaAng( pcCU, uiAbsPartIdx );
1195}
1196
1197// Intra direction for Chroma
1198Void TEncEntropy::encodeIntraDirModeChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1199{
1200  if( bRD )
1201  {
1202    uiAbsPartIdx = 0;
1203  }
1204 
1205  m_pcEntropyCoderIf->codeIntraDirChroma( pcCU, uiAbsPartIdx );
1206}
1207
1208Void TEncEntropy::encodePredInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1209{
1210  if( bRD )
1211  {
1212    uiAbsPartIdx = 0;
1213  }
1214
1215#if RWTH_SDC_DLT_B0036
1216  if( pcCU->getSDCFlag(uiAbsPartIdx) )
1217  {
1218    encodeSDCPredMode(pcCU, uiAbsPartIdx, bRD);
1219    return;
1220  }
1221#endif
1222
1223  PartSize eSize = pcCU->getPartitionSize( uiAbsPartIdx );
1224 
1225  if( pcCU->isIntra( uiAbsPartIdx ) )                                 // If it is Intra mode, encode intra prediction mode.
1226  {
1227    if( eSize == SIZE_NxN )                                         // if it is NxN size, encode 4 intra directions.
1228    {
1229      UInt uiPartOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(uiAbsPartIdx) << 1 ) ) >> 2;
1230      // if it is NxN size, this size might be the smallest partition size.
1231      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx                  );
1232      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset   );
1233      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset*2 );
1234      encodeIntraDirModeLuma( pcCU, uiAbsPartIdx + uiPartOffset*3 );
1235      encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
1236    }
1237    else                                                              // if it is not NxN size, encode 1 intra directions
1238    {
1239      encodeIntraDirModeLuma  ( pcCU, uiAbsPartIdx );
1240      encodeIntraDirModeChroma( pcCU, uiAbsPartIdx, bRD );
1241    }
1242  }
1243  else                                                                // if it is Inter mode, encode motion vector and reference index
1244  {
1245    encodePUWise( pcCU, uiAbsPartIdx, bRD );
1246  }
1247}
1248
1249/** encode motion information for every PU block
1250 * \param pcCU
1251 * \param uiAbsPartIdx
1252 * \param bRD
1253 * \returns Void
1254 */
1255Void TEncEntropy::encodePUWise( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1256{
1257  if ( bRD )
1258  {
1259    uiAbsPartIdx = 0;
1260  }
1261 
1262  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
1263  UInt uiNumPU = ( ePartSize == SIZE_2Nx2N ? 1 : ( ePartSize == SIZE_NxN ? 4 : 2 ) );
1264  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
1265  UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() - uiDepth ) << 1 ) ) >> 4;
1266
1267  for ( UInt uiPartIdx = 0, uiSubPartIdx = uiAbsPartIdx; uiPartIdx < uiNumPU; uiPartIdx++, uiSubPartIdx += uiPUOffset )
1268  {
1269    encodeMergeFlag( pcCU, uiSubPartIdx, uiPartIdx );
1270    if ( pcCU->getMergeFlag( uiSubPartIdx ) )
1271    {
1272      encodeMergeIndex( pcCU, uiSubPartIdx, uiPartIdx );
1273    }
1274    else
1275    {
1276      encodeInterDirPU( pcCU, uiSubPartIdx );
1277      for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1278      {
1279        if ( pcCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1280        {
1281          encodeRefFrmIdxPU ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1282          encodeMvdPU       ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1283          encodeMVPIdxPU    ( pcCU, uiSubPartIdx, RefPicList( uiRefListIdx ) );
1284        }
1285      }
1286    }
1287  }
1288
1289  return;
1290}
1291
1292Void TEncEntropy::encodeInterDirPU( TComDataCU* pcCU, UInt uiAbsPartIdx )
1293{
1294  if ( !pcCU->getSlice()->isInterB() )
1295  {
1296    return;
1297  }
1298
1299  m_pcEntropyCoderIf->codeInterDir( pcCU, uiAbsPartIdx );
1300  return;
1301}
1302
1303/** encode reference frame index for a PU block
1304 * \param pcCU
1305 * \param uiAbsPartIdx
1306 * \param eRefList
1307 * \returns Void
1308 */
1309Void TEncEntropy::encodeRefFrmIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1310{
1311  assert( !pcCU->isIntra( uiAbsPartIdx ) );
1312
1313  if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C)>0 && pcCU->getInterDir( uiAbsPartIdx ) != 3)
1314  {
1315    if ((eRefList== REF_PIC_LIST_1) || ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_C ) == 1 ) )
1316    {
1317      return;
1318    }
1319
1320    if ( pcCU->getSlice()->getNumRefIdx ( REF_PIC_LIST_C ) > 1 )
1321    {
1322      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, RefPicList(pcCU->getInterDir( uiAbsPartIdx )-1) );
1323    }
1324
1325  }
1326  else
1327  {
1328    if ( ( pcCU->getSlice()->getNumRefIdx( eRefList ) == 1 ) )
1329    {
1330      return;
1331    }
1332
1333    if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
1334    {
1335      m_pcEntropyCoderIf->codeRefFrmIdx( pcCU, uiAbsPartIdx, eRefList );
1336    }
1337  }
1338
1339  return;
1340}
1341
1342/** encode motion vector difference for a PU block
1343 * \param pcCU
1344 * \param uiAbsPartIdx
1345 * \param eRefList
1346 * \returns Void
1347 */
1348Void TEncEntropy::encodeMvdPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1349{
1350  assert( !pcCU->isIntra( uiAbsPartIdx ) );
1351
1352  if ( pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList ) )
1353  {
1354    m_pcEntropyCoderIf->codeMvd( pcCU, uiAbsPartIdx, eRefList );
1355  }
1356  return;
1357}
1358
1359Void TEncEntropy::encodeMVPIdxPU( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1360{
1361  if ( (pcCU->getInterDir( uiAbsPartIdx ) & ( 1 << eRefList )) && (pcCU->getAMVPMode(uiAbsPartIdx) == AM_EXPL) )
1362  {
1363#if H3D_IVMP
1364#if SEC_TWO_CANDIDATES_FOR_AMVP_D0122
1365    const Int iNumCands = AMVP_MAX_NUM_CANDS;
1366#else
1367    const Int iNumCands = AMVP_MAX_NUM_CANDS + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
1368#endif
1369    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList, iNumCands );
1370#else
1371    m_pcEntropyCoderIf->codeMVPIdx( pcCU, uiAbsPartIdx, eRefList );
1372#endif
1373  }
1374
1375  return;
1376}
1377
1378Void TEncEntropy::encodeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1379{
1380  m_pcEntropyCoderIf->codeQtCbf( pcCU, uiAbsPartIdx, eType, uiTrDepth );
1381}
1382
1383Void TEncEntropy::encodeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1384{
1385  m_pcEntropyCoderIf->codeTransformSubdivFlag( uiSymbol, uiCtx );
1386}
1387
1388Void TEncEntropy::encodeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1389{
1390  m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
1391}
1392
1393// dQP
1394Void TEncEntropy::encodeQP( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1395{
1396  if( bRD )
1397  {
1398    uiAbsPartIdx = 0;
1399  }
1400 
1401  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
1402  {
1403    m_pcEntropyCoderIf->codeDeltaQP( pcCU, uiAbsPartIdx );
1404  }
1405}
1406
1407
1408// texture
1409
1410/** encode coefficients
1411 * \param pcCU
1412 * \param uiAbsPartIdx
1413 * \param uiDepth
1414 * \param uiWidth
1415 * \param uiHeight
1416 */
1417Void TEncEntropy::encodeCoeff( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiWidth, UInt uiHeight, Bool& bCodeDQP )
1418{
1419  UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1420  UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1421  UInt uiChromaOffset = uiLumaOffset>>2;
1422 
1423  UInt uiLumaTrMode, uiChromaTrMode;
1424  pcCU->convertTransIdx( uiAbsPartIdx, pcCU->getTransformIdx(uiAbsPartIdx), uiLumaTrMode, uiChromaTrMode );
1425 
1426#if RWTH_SDC_DLT_B0036
1427  if( pcCU->getSDCFlag( uiAbsPartIdx ) )
1428  {
1429    assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
1430    assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
1431    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
1432    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
1433    assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
1434   
1435    encodeSDCResidualData(pcCU, uiAbsPartIdx);
1436    return;
1437  }
1438#endif
1439 
1440  if( pcCU->isIntra(uiAbsPartIdx) )
1441  {
1442    DTRACE_CABAC_VL( g_nSymbolCounter++ )
1443    DTRACE_CABAC_T( "\tdecodeTransformIdx()\tCUDepth=" )
1444    DTRACE_CABAC_V( uiDepth )
1445    DTRACE_CABAC_T( "\n" )
1446  }
1447  else
1448  {
1449    {
1450#if HHI_MPI
1451      if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N &&
1452            ( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 || uiDepth == pcCU->getTextureModeDepth( uiAbsPartIdx ) ) ) )
1453#else
1454      if( !(pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N ) )
1455#endif
1456      {
1457        m_pcEntropyCoderIf->codeQtRootCbf( pcCU, uiAbsPartIdx );
1458      }
1459      if ( !pcCU->getQtRootCbf( uiAbsPartIdx ) )
1460      {
1461#if 1 // MW Bug Fix
1462        pcCU->setCbfSubParts( 0, 0, 0, uiAbsPartIdx, uiDepth );
1463        pcCU->setTrIdxSubParts( 0 , uiAbsPartIdx, uiDepth );
1464#endif
1465        pcCU->setNSQTIdxSubParts( uiAbsPartIdx, uiDepth );
1466        return;
1467      }
1468    }
1469  }
1470 
1471#if FIX_MPI_B0065
1472  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getMergeIndex( uiAbsPartIdx ) == 0 && pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N && pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1 )
1473  {
1474    TComDataCU *pcTextureCU = pcCU->getSlice()->getTexturePic()->getCU( pcCU->getAddr() );
1475    if( uiDepth == pcTextureCU->getDepth(uiAbsPartIdx))
1476    {
1477      PartSize partSize = pcTextureCU->getPartitionSize(uiAbsPartIdx);
1478      pcCU->setPartSizeSubParts( partSize, uiAbsPartIdx, uiDepth );
1479    }
1480    else
1481    {
1482      pcCU->setPartSizeSubParts( SIZE_NxN, uiAbsPartIdx, uiDepth );
1483    }
1484  }
1485#endif
1486
1487  UInt temp = 0;
1488  UInt temp1 = 0;
1489  UInt temp2 = 0;
1490  xEncodeTransform( pcCU, uiLumaOffset, uiChromaOffset, uiAbsPartIdx, uiAbsPartIdx, uiDepth, uiWidth, uiHeight, 0, 0, temp, temp1, temp2, bCodeDQP );
1491
1492#if FIX_MPI_B0065
1493  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && pcCU->getMergeFlag( uiAbsPartIdx ) && pcCU->getMergeIndex( uiAbsPartIdx ) == 0 && pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N &&  pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1 )
1494  {
1495    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth ); 
1496  }
1497#endif
1498}
1499
1500Void TEncEntropy::encodeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoeff, UInt uiAbsPartIdx, UInt uiTrWidth, UInt uiTrHeight, UInt uiDepth, TextType eType )
1501{ // This is for Transform unit processing. This may be used at mode selection stage for Inter.
1502  m_pcEntropyCoderIf->codeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiTrWidth, uiTrHeight, uiDepth, eType );
1503}
1504
1505Void TEncEntropy::estimateBit (estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType)
1506{ 
1507  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
1508 
1509  m_pcEntropyCoderIf->estBit ( pcEstBitsSbac, width, height, eTType );
1510}
1511
1512#if LGE_SAO_MIGRATION_D0091
1513Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam, UInt compIdx)
1514{
1515    UInt uiSymbol;
1516    Int i;
1517
1518    uiSymbol = saoLcuParam->typeIdx + 1;
1519    if (compIdx!=2)
1520    {
1521        m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
1522    }
1523
1524    if (uiSymbol)
1525    {
1526        if (saoLcuParam->typeIdx < 4 && compIdx != 2)
1527        {
1528            saoLcuParam->subTypeIdx = saoLcuParam->typeIdx;
1529        }
1530#if FULL_NBIT
1531        Int offsetTh = 1 << ( min((Int)(g_uiBitDepth + (g_uiBitDepth-8)-5),5) );
1532#else
1533        Int offsetTh = 1 << ( min((Int)(g_uiBitDepth + g_uiBitIncrement-5),5) );
1534#endif
1535        if( saoLcuParam->typeIdx == SAO_BO )
1536        {
1537            for( i=0; i< saoLcuParam->length; i++)
1538            {
1539                UInt absOffset = ( (saoLcuParam->offset[i] < 0) ? -saoLcuParam->offset[i] : saoLcuParam->offset[i]);
1540                m_pcEntropyCoderIf->codeSaoMaxUvlc(absOffset, offsetTh-1);
1541            } 
1542            for( i=0; i< saoLcuParam->length; i++)
1543            {
1544                if (saoLcuParam->offset[i] != 0)
1545                {
1546                    UInt sign = (saoLcuParam->offset[i] < 0) ? 1 : 0 ;
1547                    m_pcEntropyCoderIf->codeSAOSign(sign);
1548                }
1549            }
1550            uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
1551            m_pcEntropyCoderIf->codeSaoUflc(5, uiSymbol);
1552        }
1553        else if( saoLcuParam->typeIdx < 4 )
1554        {
1555            m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[0], offsetTh-1);
1556            m_pcEntropyCoderIf->codeSaoMaxUvlc( saoLcuParam->offset[1], offsetTh-1);
1557            m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[2], offsetTh-1);
1558            m_pcEntropyCoderIf->codeSaoMaxUvlc(-saoLcuParam->offset[3], offsetTh-1);
1559
1560            if (compIdx!=2)
1561            {
1562                uiSymbol = (UInt) (saoLcuParam->subTypeIdx);
1563                m_pcEntropyCoderIf->codeSaoUflc(2, uiSymbol);
1564            }
1565        }
1566    }
1567}
1568
1569/** Encode SAO unit interleaving
1570* \param  rx
1571* \param  ry
1572* \param  pSaoParam
1573* \param  pcCU
1574* \param  iCUAddrInSlice
1575* \param  iCUAddrUpInSlice
1576* \param  bLFCrossSliceBoundaryFlag
1577 */
1578Void TEncEntropy::encodeSaoUnitInterleaving(Int compIdx, Bool saoFlag, Int rx, Int ry, SaoLcuParam* saoLcuParam, Int cuAddrInSlice, Int cuAddrUpInSlice, Int allowMergeLeft, Int allowMergeUp)
1579{
1580    if (saoFlag)
1581    {
1582        if (rx>0 && cuAddrInSlice!=0 && allowMergeLeft)
1583        {
1584            m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeLeftFlag);
1585        }
1586        else
1587        {
1588            saoLcuParam->mergeLeftFlag = 0;
1589        }
1590       
1591        if (saoLcuParam->mergeLeftFlag == 0)
1592        {
1593            if ( (ry > 0) && (cuAddrUpInSlice>=0) && allowMergeUp )
1594            {
1595                m_pcEntropyCoderIf->codeSaoMerge(saoLcuParam->mergeUpFlag);
1596            }
1597            else
1598            {
1599                saoLcuParam->mergeUpFlag = 0;
1600            }
1601
1602            if (!saoLcuParam->mergeUpFlag)
1603            {
1604                encodeSaoOffset(saoLcuParam, compIdx);
1605            }
1606        }
1607    }
1608}
1609#else
1610/** Encode SAO Offset
1611 * \param  saoLcuParam SAO LCU paramters
1612 */
1613Void TEncEntropy::encodeSaoOffset(SaoLcuParam* saoLcuParam)
1614{
1615  UInt uiSymbol;
1616  Int i;
1617
1618  uiSymbol = saoLcuParam->typeIdx + 1;
1619  m_pcEntropyCoderIf->codeSaoTypeIdx(uiSymbol);
1620  if (uiSymbol)
1621  {
1622    if( saoLcuParam->typeIdx == SAO_BO )
1623    {
1624      // Code Left Band Index
1625      uiSymbol = (UInt) (saoLcuParam->bandPosition);
1626      m_pcEntropyCoderIf->codeSaoUflc(uiSymbol);
1627      for( i=0; i< saoLcuParam->length; i++)
1628      {
1629        m_pcEntropyCoderIf->codeSaoSvlc(saoLcuParam->offset[i]);
1630      } 
1631    }
1632    else
1633      if( saoLcuParam->typeIdx < 4 )
1634      {
1635        m_pcEntropyCoderIf->codeSaoUvlc( saoLcuParam->offset[0]);
1636        m_pcEntropyCoderIf->codeSaoUvlc( saoLcuParam->offset[1]);
1637        m_pcEntropyCoderIf->codeSaoUvlc(-saoLcuParam->offset[2]);
1638        m_pcEntropyCoderIf->codeSaoUvlc(-saoLcuParam->offset[3]);
1639      }
1640  }
1641}
1642/** Encode SAO unit
1643* \param  rx
1644* \param  ry
1645* \param  iCompIdx
1646* \param  pSaoParam
1647* \param  bRepeatedRow
1648 */
1649Void TEncEntropy::encodeSaoUnit(Int rx, Int ry, Int compIdx, SAOParam* saoParam, Int repeatedRow )
1650{
1651  int addr, addrLeft; 
1652  int numCuInWidth  = saoParam->numCuInWidth;
1653  SaoLcuParam* saoOneLcu;
1654  Int runLeft;
1655
1656  addr      =  rx + ry*numCuInWidth;
1657  addrLeft  =  (addr%numCuInWidth == 0) ? -1 : addr - 1;
1658
1659  if (!repeatedRow)
1660  {
1661    saoOneLcu = &(saoParam->saoLcuParam[compIdx][addr]);   
1662    runLeft = (addrLeft>=0 ) ? saoParam->saoLcuParam[compIdx][addrLeft].run : -1;
1663    if (rx == 0 || runLeft==0)
1664    {
1665      if (ry == 0)
1666      {
1667        m_pcEntropyCoderIf->codeSaoRun(saoOneLcu->runDiff, numCuInWidth-rx-1); 
1668        saoOneLcu->mergeUpFlag = 0;
1669      }
1670      else 
1671      {
1672        m_pcEntropyCoderIf->codeSaoSvlc(saoOneLcu->runDiff); 
1673        m_pcEntropyCoderIf->codeSaoFlag(saoOneLcu->mergeUpFlag); 
1674      }
1675      if (!saoOneLcu->mergeUpFlag)
1676      {
1677        encodeSaoOffset(saoOneLcu);
1678      }
1679    }
1680  }
1681}
1682
1683/** Encode SAO unit interleaving
1684* \param  rx
1685* \param  ry
1686* \param  pSaoParam
1687* \param  pcCU
1688* \param  iCUAddrInSlice
1689* \param  iCUAddrUpInSlice
1690* \param  bLFCrossSliceBoundaryFlag
1691 */
1692Void TEncEntropy::encodeSaoUnitInterleaving(Int rx, Int ry, SAOParam* saoParam, TComDataCU* cu, Int cuAddrInSlice, Int cuAddrUpInSlice, Bool lfCrossSliceBoundaryFlag)
1693{
1694  Int addr = cu->getAddr();
1695  for (Int compIdx=0; compIdx<3; compIdx++)
1696  {
1697    if (saoParam->bSaoFlag[compIdx])
1698    {
1699      if (rx>0 && cuAddrInSlice!=0)
1700      {
1701      m_pcEntropyCoderIf->codeSaoMergeLeft(saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag,compIdx);
1702      }
1703      else
1704      {
1705        saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag = 0;
1706      }
1707      if (saoParam->saoLcuParam[compIdx][addr].mergeLeftFlag == 0)
1708      {
1709        if ( (ry > 0) && (cuAddrUpInSlice>0||lfCrossSliceBoundaryFlag))
1710        {
1711          m_pcEntropyCoderIf->codeSaoMergeUp(saoParam->saoLcuParam[compIdx][addr].mergeUpFlag);
1712        }
1713        else
1714        {
1715          saoParam->saoLcuParam[compIdx][addr].mergeUpFlag = 0;
1716        }
1717        if (!saoParam->saoLcuParam[compIdx][addr].mergeUpFlag)
1718        {
1719          encodeSaoOffset(&(saoParam->saoLcuParam[compIdx][addr]));
1720        }
1721      }
1722    }
1723  }
1724}
1725
1726/** Encode SAO parameter
1727* \param  pcAPS
1728 */
1729Void TEncEntropy::encodeSaoParam(TComAPS* aps)
1730{
1731  SaoLcuParam* psSaoOneLcu;
1732  int i,j,k, compIdx; 
1733  int numCuInWidth  ;
1734  int numCuInHeight ;
1735  Bool repeatedRow[3];
1736  Int addr;
1737  m_pcEntropyCoderIf->codeSaoFlag(aps->getSaoInterleavingFlag()); 
1738  if(!aps->getSaoInterleavingFlag())
1739  {
1740    m_pcEntropyCoderIf->codeSaoFlag(aps->getSaoEnabled()); 
1741    if (aps->getSaoEnabled())
1742    {
1743      SAOParam* pSaoParam = aps->getSaoParam();
1744      numCuInWidth  = pSaoParam->numCuInWidth;
1745      numCuInHeight = pSaoParam->numCuInHeight;
1746      m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->bSaoFlag[1]); 
1747      m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->bSaoFlag[2]); 
1748      m_pcEntropyCoderIf->codeSaoUvlc(numCuInWidth-1); 
1749      m_pcEntropyCoderIf->codeSaoUvlc(numCuInHeight-1); 
1750      for (compIdx=0;compIdx<3;compIdx++)
1751      {
1752        if (pSaoParam->bSaoFlag[compIdx])
1753        {
1754          m_pcEntropyCoderIf->codeSaoFlag(pSaoParam->oneUnitFlag[compIdx]); 
1755          if (pSaoParam->oneUnitFlag[compIdx])
1756          {
1757            psSaoOneLcu = &(pSaoParam->saoLcuParam[compIdx][0]);   
1758            encodeSaoOffset(psSaoOneLcu);
1759          }
1760        }
1761      }
1762
1763      for (j=0;j<numCuInHeight;j++)
1764      {
1765        for (compIdx=0; compIdx<3; compIdx++)
1766        {
1767          repeatedRow[compIdx] = true;
1768          for (k=0;k<numCuInWidth;k++)
1769          {
1770            addr       =  k + j*numCuInWidth;
1771            psSaoOneLcu = &(pSaoParam->saoLcuParam[compIdx][addr]);   
1772            if (!psSaoOneLcu->mergeUpFlag || psSaoOneLcu->runDiff)
1773            {
1774              repeatedRow[compIdx] = false;
1775              break;
1776            }
1777          }
1778        }
1779        for (i=0;i<numCuInWidth;i++)
1780        {
1781          for (compIdx=0; compIdx<3; compIdx++)
1782          {
1783            if (pSaoParam->bSaoFlag[compIdx]  && !pSaoParam->oneUnitFlag[compIdx]) 
1784            {
1785              if (j>0 && i==0) 
1786              {
1787                m_pcEntropyCoderIf->codeSaoFlag(repeatedRow[compIdx]); 
1788              }
1789              encodeSaoUnit (i,j, compIdx, pSaoParam, repeatedRow[compIdx]);
1790            }
1791          }
1792        }
1793      }
1794    }
1795  }
1796}
1797#endif
1798
1799Int TEncEntropy::countNonZeroCoeffs( TCoeff* pcCoef, UInt uiSize )
1800{
1801  Int count = 0;
1802 
1803  for ( Int i = 0; i < uiSize; i++ )
1804  {
1805    count += pcCoef[i] != 0;
1806  }
1807 
1808  return count;
1809}
1810
1811/** encode quantization matrix
1812 * \param scalingList quantization matrix information
1813 */
1814Void TEncEntropy::encodeScalingList( TComScalingList* scalingList )
1815{
1816  m_pcEntropyCoderIf->codeScalingList( scalingList );
1817}
1818
1819Void TEncEntropy::encodeDFParams(TComAPS* pcAPS)
1820{
1821  m_pcEntropyCoderIf->codeDFFlag(pcAPS->getLoopFilterDisable(), "loop_filter_disable");
1822
1823  if (!pcAPS->getLoopFilterDisable())
1824  {
1825    m_pcEntropyCoderIf->codeDFSvlc(pcAPS->getLoopFilterBetaOffset(), "beta_offset_div2");
1826    m_pcEntropyCoderIf->codeDFSvlc(pcAPS->getLoopFilterTcOffset(), "tc_offset_div2");
1827  }
1828}
1829
1830#if RWTH_SDC_DLT_B0036
1831Void TEncEntropy::encodeSDCPredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1832{
1833  assert( pcCU->getSlice()->getSPS()->isDepth() );
1834 
1835  if( bRD )
1836    uiAbsPartIdx = 0;
1837 
1838  m_pcEntropyCoderIf->codeSDCPredMode(pcCU, uiAbsPartIdx);
1839}
1840
1841Void TEncEntropy::encodeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1842{
1843  assert( pcCU->getSlice()->getSPS()->isDepth() );
1844 
1845  if( bRD )
1846    uiAbsPartIdx = 0;
1847 
1848  m_pcEntropyCoderIf->codeSDCFlag(pcCU, uiAbsPartIdx);
1849}
1850
1851Void TEncEntropy::encodeSDCResidualData( TComDataCU* pcCU, UInt uiAbsPartIdx, Bool bRD )
1852{
1853  assert( pcCU->getSlice()->getSPS()->isDepth() );
1854  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA) == 1 );
1855  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U) == 1 );
1856  assert( pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V) == 1 );
1857  assert( pcCU->getTransformIdx(uiAbsPartIdx) == 0 );
1858 
1859  if( bRD )
1860    uiAbsPartIdx = 0;
1861 
1862  // number of segments depends on prediction mode for INTRA
1863  UInt uiNumSegments = 2;
1864  UInt uiLumaPredMode = pcCU->getLumaIntraDir( uiAbsPartIdx );
1865  if( uiLumaPredMode == DC_IDX || uiLumaPredMode == PLANAR_IDX )
1866    uiNumSegments = 1;
1867 
1868  // encode residual data for each segment
1869  for( UInt uiSeg = 0; uiSeg < uiNumSegments; uiSeg++ )
1870    m_pcEntropyCoderIf->codeSDCResidualData(pcCU, uiAbsPartIdx, uiSeg);
1871}
1872#endif
1873
1874//! \}
Note: See TracBrowser for help on using the repository browser.