source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibEncoder/TEncCu.cpp @ 455

Last change on this file since 455 was 455, checked in by zhang, 11 years ago

NBDV for 3D-HEVC

  • Property svn:eol-style set to native
File size: 66.6 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-2013, 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     TEncCu.cpp
35    \brief    Coding Unit (CU) encoder class
36*/
37
38#include <stdio.h>
39#include "TEncTop.h"
40#include "TEncCu.h"
41#include "TEncAnalyze.h"
42
43#include <cmath>
44#include <algorithm>
45using namespace std;
46
47//! \ingroup TLibEncoder
48//! \{
49
50// ====================================================================================================================
51// Constructor / destructor / create / destroy
52// ====================================================================================================================
53
54/**
55 \param    uiTotalDepth  total number of allowable depth
56 \param    uiMaxWidth    largest CU width
57 \param    uiMaxHeight   largest CU height
58 */
59Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight)
60{
61  Int i;
62 
63  m_uhTotalDepth   = uhTotalDepth + 1;
64  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
65  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
66   
67  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
68  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
69  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
70  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
71  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
72  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
73  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
74 
75  UInt uiNumPartitions;
76  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
77  {
78    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
79    UInt uiWidth  = uiMaxWidth  >> i;
80    UInt uiHeight = uiMaxHeight >> i;
81   
82    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
83    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
84   
85    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight);
86    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight);
87    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight);
88   
89    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight);
90    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight);
91    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight);
92   
93    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
94  }
95 
96  m_bEncodeDQP = false;
97#if RATE_CONTROL_LAMBDA_DOMAIN
98  m_LCUPredictionSAD = 0;
99  m_addSADDepth      = 0;
100  m_temporalSAD      = 0;
101#endif
102
103  // initialize partition order.
104  UInt* piTmp = &g_auiZscanToRaster[0];
105  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
106  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
107 
108  // initialize conversion matrix from partition index to pel
109  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
110}
111
112Void TEncCu::destroy()
113{
114  Int i;
115 
116  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
117  {
118    if(m_ppcBestCU[i])
119    {
120      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
121    }
122    if(m_ppcTempCU[i])
123    {
124      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
125    }
126    if(m_ppcPredYuvBest[i])
127    {
128      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
129    }
130    if(m_ppcResiYuvBest[i])
131    {
132      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
133    }
134    if(m_ppcRecoYuvBest[i])
135    {
136      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
137    }
138    if(m_ppcPredYuvTemp[i])
139    {
140      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
141    }
142    if(m_ppcResiYuvTemp[i])
143    {
144      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
145    }
146    if(m_ppcRecoYuvTemp[i])
147    {
148      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
149    }
150    if(m_ppcOrigYuv[i])
151    {
152      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
153    }
154  }
155  if(m_ppcBestCU)
156  {
157    delete [] m_ppcBestCU;
158    m_ppcBestCU = NULL;
159  }
160  if(m_ppcTempCU)
161  {
162    delete [] m_ppcTempCU;
163    m_ppcTempCU = NULL;
164  }
165 
166  if(m_ppcPredYuvBest)
167  {
168    delete [] m_ppcPredYuvBest;
169    m_ppcPredYuvBest = NULL;
170  }
171  if(m_ppcResiYuvBest)
172  {
173    delete [] m_ppcResiYuvBest;
174    m_ppcResiYuvBest = NULL;
175  }
176  if(m_ppcRecoYuvBest)
177  {
178    delete [] m_ppcRecoYuvBest;
179    m_ppcRecoYuvBest = NULL;
180  }
181  if(m_ppcPredYuvTemp)
182  {
183    delete [] m_ppcPredYuvTemp;
184    m_ppcPredYuvTemp = NULL;
185  }
186  if(m_ppcResiYuvTemp)
187  {
188    delete [] m_ppcResiYuvTemp;
189    m_ppcResiYuvTemp = NULL;
190  }
191  if(m_ppcRecoYuvTemp)
192  {
193    delete [] m_ppcRecoYuvTemp;
194    m_ppcRecoYuvTemp = NULL;
195  }
196  if(m_ppcOrigYuv)
197  {
198    delete [] m_ppcOrigYuv;
199    m_ppcOrigYuv = NULL;
200  }
201}
202
203/** \param    pcEncTop      pointer of encoder class
204 */
205Void TEncCu::init( TEncTop* pcEncTop )
206{
207  m_pcEncCfg           = pcEncTop;
208  m_pcPredSearch       = pcEncTop->getPredSearch();
209  m_pcTrQuant          = pcEncTop->getTrQuant();
210  m_pcBitCounter       = pcEncTop->getBitCounter();
211  m_pcRdCost           = pcEncTop->getRdCost();
212 
213  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
214  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
215  m_pcSbacCoder       = pcEncTop->getSbacCoder();
216  m_pcBinCABAC         = pcEncTop->getBinCABAC();
217 
218  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
219  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
220 
221  m_bUseSBACRD        = pcEncTop->getUseSBACRD();
222  m_pcRateCtrl        = pcEncTop->getRateCtrl();
223}
224
225// ====================================================================================================================
226// Public member functions
227// ====================================================================================================================
228
229/** \param  rpcCU pointer of CU data class
230 */
231Void TEncCu::compressCU( TComDataCU*& rpcCU )
232{
233  // initialize CU data
234  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
235  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
236
237#if RATE_CONTROL_LAMBDA_DOMAIN
238  m_addSADDepth      = 0;
239  m_LCUPredictionSAD = 0;
240  m_temporalSAD      = 0;
241#endif
242
243  // analysis of CU
244  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
245
246#if ADAPTIVE_QP_SELECTION
247  if( m_pcEncCfg->getUseAdaptQpSelect() )
248  {
249    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
250    {
251      xLcuCollectARLStats( rpcCU);
252    }
253  }
254#endif
255}
256/** \param  pcCU  pointer of CU data class
257 */
258Void TEncCu::encodeCU ( TComDataCU* pcCU )
259{
260  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
261  {
262    setdQPFlag(true);
263  }
264
265  // Encode CU data
266  xEncodeCU( pcCU, 0, 0 );
267}
268
269// ====================================================================================================================
270// Protected member functions
271// ====================================================================================================================
272/** Derive small set of test modes for AMP encoder speed-up
273 *\param   rpcBestCU
274 *\param   eParentPartSize
275 *\param   bTestAMP_Hor
276 *\param   bTestAMP_Ver
277 *\param   bTestMergeAMP_Hor
278 *\param   bTestMergeAMP_Ver
279 *\returns Void
280*/
281#if AMP_ENC_SPEEDUP
282#if AMP_MRG
283Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
284#else
285Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
286#endif
287{
288  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
289  {
290    bTestAMP_Hor = true;
291  }
292  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
293  {
294    bTestAMP_Ver = true;
295  }
296  else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false )
297  {
298    bTestAMP_Hor = true;         
299    bTestAMP_Ver = true;         
300  }
301
302#if AMP_MRG
303  //! Utilizing the partition size of parent PU   
304  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
305  { 
306    bTestMergeAMP_Hor = true;
307    bTestMergeAMP_Ver = true;
308  }
309
310  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
311  {
312    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
313    {
314      bTestMergeAMP_Hor = true;
315    }
316    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
317    {
318      bTestMergeAMP_Ver = true;
319    }
320  }
321
322  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
323  {
324    bTestMergeAMP_Hor = true;         
325    bTestMergeAMP_Ver = true;         
326  }
327
328  if ( rpcBestCU->getWidth(0) == 64 )
329  { 
330    bTestAMP_Hor = false;
331    bTestAMP_Ver = false;
332  }   
333#else
334  //! Utilizing the partition size of parent PU       
335  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
336  { 
337    bTestAMP_Hor = true;
338    bTestAMP_Ver = true;
339  }
340
341  if ( eParentPartSize == SIZE_2Nx2N )
342  { 
343    bTestAMP_Hor = false;
344    bTestAMP_Ver = false;
345  }     
346#endif
347}
348#endif
349
350// ====================================================================================================================
351// Protected member functions
352// ====================================================================================================================
353/** Compress a CU block recursively with enabling sub-LCU-level delta QP
354 *\param   rpcBestCU
355 *\param   rpcTempCU
356 *\param   uiDepth
357 *\returns Void
358 *
359 *- for loop of QP value to compress the current CU with all possible QP
360*/
361#if AMP_ENC_SPEEDUP
362Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize )
363#else
364Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
365#endif
366{
367  TComPic* pcPic = rpcBestCU->getPic();
368
369  // get Original YUV data from picture
370  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
371
372  // variables for fast encoder decision
373  Bool    bEarlySkip  = false;
374  Bool    bTrySplit    = true;
375  Double  fRD_Skip    = MAX_DOUBLE;
376
377  // variable for Early CU determination
378  Bool    bSubBranch = true;
379
380  // variable for Cbf fast mode PU decision
381  Bool    doNotBlockPu = true;
382  Bool earlyDetectionSkipMode = false;
383
384  Bool    bTrySplitDQP  = true;
385
386  static  Double  afCost[ MAX_CU_DEPTH ];
387  static  Int      aiNum [ MAX_CU_DEPTH ];
388
389  if ( rpcBestCU->getAddr() == 0 )
390  {
391    ::memset( afCost, 0, sizeof( afCost ) );
392    ::memset( aiNum,  0, sizeof( aiNum  ) );
393  }
394
395  Bool bBoundary = false;
396  UInt uiLPelX   = rpcBestCU->getCUPelX();
397  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
398  UInt uiTPelY   = rpcBestCU->getCUPelY();
399  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
400
401  Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
402  Int iMinQP;
403  Int iMaxQP;
404  Bool isAddLowestQP = false;
405  Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY();
406
407  if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
408  {
409    Int idQP = m_pcEncCfg->getMaxDeltaQP();
410    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
411    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
412    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
413    {
414      isAddLowestQP = true; 
415      iMinQP = iMinQP - 1;
416    }
417  }
418  else
419  {
420    iMinQP = rpcTempCU->getQP(0);
421    iMaxQP = rpcTempCU->getQP(0);
422  }
423
424#if RATE_CONTROL_LAMBDA_DOMAIN
425  if ( m_pcEncCfg->getUseRateCtrl() )
426  {
427    iMinQP = m_pcRateCtrl->getRCQP();
428    iMaxQP = m_pcRateCtrl->getRCQP();
429  }
430#else
431  if(m_pcEncCfg->getUseRateCtrl())
432  {
433    Int qp = m_pcRateCtrl->getUnitQP();
434    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
435    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
436  }
437#endif
438
439  // If slice start or slice end is within this cu...
440  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
441  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart();
442  Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart());
443  Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() );
444  // We need to split, so don't try these modes.
445  if(!bSliceEnd && !bSliceStart && bInsidePicture )
446  {
447    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
448    {
449      if (isAddLowestQP && (iQP == iMinQP))
450      {
451        iQP = lowestQP;
452      }
453      // variables for fast encoder decision
454      bEarlySkip  = false;
455      bTrySplit    = true;
456      fRD_Skip    = MAX_DOUBLE;
457
458      rpcTempCU->initEstData( uiDepth, iQP );
459#if H_3D_NBDV
460      DisInfo DvInfo; 
461      DvInfo.bDV = false;
462      DvInfo.m_acNBDV.setZero();
463      DvInfo.m_aVIdxCan = 0;
464#if H_3D_NBDV_REF
465      DvInfo.m_acDoNBDV.setZero();
466#endif
467
468      if( rpcTempCU->getSlice()->getSliceType() != I_SLICE )
469      {
470        if(rpcTempCU->getSlice()->getViewIndex() && !rpcTempCU->getSlice()->getIsDepth()) //Notes from QC: this condition shall be changed once the configuration is completed, e.g. in pcSlice->getSPS()->getMultiviewMvPredMode() || ARP in prev. HTM. Remove this comment once it is done. Remove this comment once it is done.
471        {
472          PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
473          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );     
474#if H_3D_NBDV_REF
475          if(m_pcSlice->getSPS()->getUseDVPRefine())  //Notes from QC: please check the condition for DoNBDV. Remove this comment once it is done.
476            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo, true);
477          else
478#endif
479            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo);
480
481          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
482          rpcBestCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
483          rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
484        }
485      }
486#endif
487      // do inter modes, SKIP and 2Nx2N
488      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
489      {
490        // 2Nx2N
491        if(m_pcEncCfg->getUseEarlySkipDetection())
492        {
493          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N
494        }
495        // SKIP
496        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
497        rpcTempCU->initEstData( uiDepth, iQP );
498
499        // fast encoder decision for early skip
500        if ( m_pcEncCfg->getUseFastEnc() )
501        {
502          Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
503          if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] )
504          {
505            bEarlySkip = true;
506            bTrySplit  = false;
507          }
508        }
509
510        if(!m_pcEncCfg->getUseEarlySkipDetection())
511        {
512          // 2Nx2N, NxN
513          if ( !bEarlySkip )
514          {
515            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );
516            if(m_pcEncCfg->getUseCbfFastMode())
517            {
518              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
519            }
520          }
521        }
522      }
523
524      if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
525      {
526        if(iQP == iBaseQP)
527        {
528          bTrySplitDQP = bTrySplit;
529        }
530      }
531      else
532      {
533        bTrySplitDQP = bTrySplit;
534      }
535      if (isAddLowestQP && (iQP == lowestQP))
536      {
537        iQP = iMinQP;
538      }
539    }
540
541#if RATE_CONTROL_LAMBDA_DOMAIN
542    if ( uiDepth <= m_addSADDepth )
543    {
544      m_LCUPredictionSAD += m_temporalSAD;
545      m_addSADDepth = uiDepth;
546    }
547#endif
548
549    if(!earlyDetectionSkipMode)
550    {
551      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
552      {
553        if (isAddLowestQP && (iQP == iMinQP))
554        {
555          iQP = lowestQP;
556        }
557        rpcTempCU->initEstData( uiDepth, iQP );
558
559        // do inter modes, NxN, 2NxN, and Nx2N
560        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
561        {
562          // 2Nx2N, NxN
563          if ( !bEarlySkip )
564          {
565            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
566            {
567              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu)
568              {
569                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
570                rpcTempCU->initEstData( uiDepth, iQP );
571              }
572            }
573          }
574
575          // 2NxN, Nx2N
576          if(doNotBlockPu)
577          {
578            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
579            rpcTempCU->initEstData( uiDepth, iQP );
580            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
581            {
582              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
583            }
584          }
585          if(doNotBlockPu)
586          {
587            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
588            rpcTempCU->initEstData( uiDepth, iQP );
589            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
590            {
591              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
592            }
593          }
594
595#if 1
596          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
597          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
598          {
599#if AMP_ENC_SPEEDUP       
600            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
601
602#if AMP_MRG
603            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
604
605            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
606#else
607            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
608#endif
609
610            //! Do horizontal AMP
611            if ( bTestAMP_Hor )
612            {
613              if(doNotBlockPu)
614              {
615                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
616                rpcTempCU->initEstData( uiDepth, iQP );
617                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
618                {
619                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
620                }
621              }
622              if(doNotBlockPu)
623              {
624                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
625                rpcTempCU->initEstData( uiDepth, iQP );
626                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
627                {
628                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
629                }
630              }
631            }
632#if AMP_MRG
633            else if ( bTestMergeAMP_Hor ) 
634            {
635              if(doNotBlockPu)
636              {
637                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
638                rpcTempCU->initEstData( uiDepth, iQP );
639                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
640                {
641                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
642                }
643              }
644              if(doNotBlockPu)
645              {
646                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
647                rpcTempCU->initEstData( uiDepth, iQP );
648                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
649                {
650                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
651                }
652              }
653            }
654#endif
655
656            //! Do horizontal AMP
657            if ( bTestAMP_Ver )
658            {
659              if(doNotBlockPu)
660              {
661                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
662                rpcTempCU->initEstData( uiDepth, iQP );
663                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
664                {
665                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
666                }
667              }
668              if(doNotBlockPu)
669              {
670                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
671                rpcTempCU->initEstData( uiDepth, iQP );
672              }
673            }
674#if AMP_MRG
675            else if ( bTestMergeAMP_Ver )
676            {
677              if(doNotBlockPu)
678              {
679                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
680                rpcTempCU->initEstData( uiDepth, iQP );
681                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
682                {
683                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
684                }
685              }
686              if(doNotBlockPu)
687              {
688                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
689                rpcTempCU->initEstData( uiDepth, iQP );
690              }
691            }
692#endif
693
694#else
695            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
696            rpcTempCU->initEstData( uiDepth, iQP );
697            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
698            rpcTempCU->initEstData( uiDepth, iQP );
699            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
700            rpcTempCU->initEstData( uiDepth, iQP );
701
702            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
703            rpcTempCU->initEstData( uiDepth, iQP );
704
705#endif
706          }   
707#endif
708        }
709
710        // do normal intra modes
711        if ( !bEarlySkip )
712        {
713          // speedup for inter frames
714          if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
715            rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
716            rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
717            rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     ) // avoid very complex intra if it is unlikely
718          {
719            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
720            rpcTempCU->initEstData( uiDepth, iQP );
721            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
722            {
723              if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
724              {
725                xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
726                rpcTempCU->initEstData( uiDepth, iQP );
727              }
728            }
729          }
730        }
731
732        // test PCM
733        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
734          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
735          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
736        {
737          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
738          UInt uiBestBits = rpcBestCU->getTotalBits();
739#if H_3D_VSO // M7
740          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
741          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
742#else
743          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
744#endif
745          {
746            xCheckIntraPCM (rpcBestCU, rpcTempCU);
747            rpcTempCU->initEstData( uiDepth, iQP );
748          }
749        }
750        if (isAddLowestQP && (iQP == lowestQP))
751        {
752          iQP = iMinQP;
753        }
754      }
755    }
756
757    m_pcEntropyCoder->resetBits();
758    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
759    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
760    if(m_pcEncCfg->getUseSBACRD())
761    {
762      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
763    }
764
765#if H_3D_VSO // M8
766    if ( m_pcRdCost->getUseVSO() )   
767      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
768    else
769#endif
770    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
771
772    // accumulate statistics for early skip
773    if ( m_pcEncCfg->getUseFastEnc() )
774    {
775      if ( rpcBestCU->isSkipped(0) )
776      {
777        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
778        afCost[ iIdx ] += rpcBestCU->getTotalCost();
779        aiNum [ iIdx ] ++;
780      }
781    }
782
783    // Early CU determination
784    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
785    {
786      bSubBranch = false;
787    }
788    else
789    {
790      bSubBranch = true;
791    }
792  }
793  else if(!(bSliceEnd && bInsidePicture))
794  {
795    bBoundary = true;
796#if RATE_CONTROL_LAMBDA_DOMAIN
797    m_addSADDepth++;
798#endif
799  }
800
801  // copy orginal YUV samples to PCM buffer
802  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
803  {
804    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
805  }
806  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
807  {
808    Int idQP = m_pcEncCfg->getMaxDeltaQP();
809    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
810    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
811    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
812    {
813      isAddLowestQP = true;
814      iMinQP = iMinQP - 1;     
815    }
816  }
817  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
818  {
819    iMinQP = iBaseQP;
820    iMaxQP = iBaseQP;
821  }
822  else
823  {
824    Int iStartQP;
825    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
826    {
827      iStartQP = rpcTempCU->getQP(0);
828    }
829    else
830    {
831      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
832      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
833    }
834    iMinQP = iStartQP;
835    iMaxQP = iStartQP;
836  }
837#if RATE_CONTROL_LAMBDA_DOMAIN
838  if ( m_pcEncCfg->getUseRateCtrl() )
839  {
840    iMinQP = m_pcRateCtrl->getRCQP();
841    iMaxQP = m_pcRateCtrl->getRCQP();
842  }
843#else
844  if(m_pcEncCfg->getUseRateCtrl())
845  {
846    Int qp = m_pcRateCtrl->getUnitQP();
847    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
848    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
849  }
850#endif
851  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
852  {
853    if (isAddLowestQP && (iQP == iMinQP))
854    {
855      iQP = lowestQP;
856    }
857    rpcTempCU->initEstData( uiDepth, iQP );
858
859    // further split
860    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
861    {
862#if H_3D_VSO // M9
863      // reset Model
864      if( m_pcRdCost->getUseRenModel() )
865      {
866        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
867        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
868        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
869        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
870        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
871      }
872#endif
873
874      UChar       uhNextDepth         = uiDepth+1;
875      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
876      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
877
878      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
879      {
880        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
881        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
882
883        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
884        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
885        {
886          if( m_bUseSBACRD )
887          {
888            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
889            {
890              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
891            }
892            else
893            {
894              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
895            }
896          }
897
898#if AMP_ENC_SPEEDUP
899          if ( rpcBestCU->isIntra(0) )
900          {
901            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
902          }
903          else
904          {
905            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
906          }
907#else
908          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
909#endif
910
911          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
912          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
913        }
914        else if (bInSlice)
915        {
916          pcSubBestPartCU->copyToPic( uhNextDepth );
917          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
918        }
919      }
920
921      if( !bBoundary )
922      {
923        m_pcEntropyCoder->resetBits();
924        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
925
926        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
927        if(m_pcEncCfg->getUseSBACRD())
928        {
929          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
930        }
931      }
932
933#if H_3D_VSO // M10
934      if ( m_pcRdCost->getUseVSO() )
935        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
936      else
937#endif
938      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
939
940      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
941      {
942        Bool hasResidual = false;
943        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
944        {
945          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
946              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
947          {
948            hasResidual = true;
949            break;
950          }
951        }
952
953        UInt uiTargetPartIdx;
954        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
955        {
956          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
957        }
958        else
959        {
960          uiTargetPartIdx = 0;
961        }
962        if ( hasResidual )
963        {
964#if !RDO_WITHOUT_DQP_BITS
965          m_pcEntropyCoder->resetBits();
966          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
967          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
968          if(m_pcEncCfg->getUseSBACRD())
969          {
970            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
971          }
972#if H_3D_VSO // M11
973          if ( m_pcRdCost->getUseLambdaScaleVSO())         
974            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
975          else
976#endif
977          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
978#endif
979
980          Bool foundNonZeroCbf = false;
981          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
982          assert( foundNonZeroCbf );
983        }
984        else
985        {
986          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
987        }
988      }
989
990      if( m_bUseSBACRD )
991      {
992        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
993      }
994      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
995                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
996      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
997                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
998      if(isEndOfSlice||isEndOfSliceSegment)
999      {
1000        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1001      }
1002      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1003    }                                                                                  // with sub partitioned prediction.
1004    if (isAddLowestQP && (iQP == lowestQP))
1005    {
1006      iQP = iMinQP;
1007    }
1008  }
1009
1010
1011#if H_3D_VSO // M12
1012  if( m_pcRdCost->getUseRenModel() )
1013  {
1014    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1015    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1016    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1017    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1018    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1019  }
1020#endif
1021
1022  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1023
1024  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1025  if( bBoundary ||(bSliceEnd && bInsidePicture))
1026  {
1027    return;
1028  }
1029
1030  // Assert if Best prediction mode is NONE
1031  // Selected mode's RD-cost must be not MAX_DOUBLE.
1032  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1033  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1034  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1035}
1036
1037/** finish encoding a cu and handle end-of-slice conditions
1038 * \param pcCU
1039 * \param uiAbsPartIdx
1040 * \param uiDepth
1041 * \returns Void
1042 */
1043Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1044{
1045  TComPic* pcPic = pcCU->getPic();
1046  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1047
1048  //Calculate end address
1049  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1050
1051  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1052  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1053  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1054  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1055  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1056  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1057  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1058  {
1059    uiInternalAddress--;
1060    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1061    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1062  }
1063  uiInternalAddress++;
1064  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1065  {
1066    uiInternalAddress = 0;
1067    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1068  }
1069  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1070
1071  // Encode slice finish
1072  Bool bTerminateSlice = false;
1073  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1074  {
1075    bTerminateSlice = true;
1076  }
1077  UInt uiGranularityWidth = g_uiMaxCUWidth;
1078  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1079  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1080  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1081    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1082 
1083  if(granularityBoundary)
1084  {
1085    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1086    if (!bTerminateSlice)
1087      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1088  }
1089 
1090  Int numberOfWrittenBits = 0;
1091  if (m_pcBitCounter)
1092  {
1093    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1094  }
1095 
1096  // Calculate slice end IF this CU puts us over slice bit size.
1097  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1098  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1099  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1100  {
1101    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1102  }
1103  // Set slice end parameter
1104  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1105  {
1106    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1107    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1108    return;
1109  }
1110  // Set dependent slice end parameter
1111  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1112  {
1113    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1114    return;
1115  }
1116  if(granularityBoundary)
1117  {
1118    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1119    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1120    if (m_pcBitCounter)
1121    {
1122      m_pcEntropyCoder->resetBits();     
1123    }
1124  }
1125}
1126
1127/** Compute QP for each CU
1128 * \param pcCU Target CU
1129 * \param uiDepth CU depth
1130 * \returns quantization parameter
1131 */
1132Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1133{
1134  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1135  Int iQpOffset = 0;
1136  if ( m_pcEncCfg->getUseAdaptiveQP() )
1137  {
1138    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1139    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1140    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1141    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1142    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1143    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1144    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1145
1146    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1147    Double dAvgAct = pcAQLayer->getAvgActivity();
1148    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1149    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1150    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1151    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1152  }
1153  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1154}
1155
1156/** encode a CU block recursively
1157 * \param pcCU
1158 * \param uiAbsPartIdx
1159 * \param uiDepth
1160 * \returns Void
1161 */
1162Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1163{
1164  TComPic* pcPic = pcCU->getPic();
1165 
1166  Bool bBoundary = false;
1167  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1168  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1169  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1170  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1171 
1172  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1173  // If slice start is within this cu...
1174  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1175    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1176  // We need to split, so don't try these modes.
1177  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1178  {
1179    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1180  }
1181  else
1182  {
1183    bBoundary = true;
1184  }
1185 
1186  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1187  {
1188    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1189    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1190    {
1191      setdQPFlag(true);
1192    }
1193    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1194    {
1195      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1196      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1197      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1198      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1199      {
1200        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1201      }
1202    }
1203    return;
1204  }
1205 
1206  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1207  {
1208    setdQPFlag(true);
1209  }
1210  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1211  {
1212    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1213  }
1214  if( !pcCU->getSlice()->isIntra() )
1215  {
1216    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1217  }
1218 
1219  if( pcCU->isSkipped( uiAbsPartIdx ) )
1220  {
1221    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1222    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1223    return;
1224  }
1225  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1226 
1227  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1228 
1229  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1230  {
1231    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1232
1233    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1234    {
1235      // Encode slice finish
1236      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1237      return;
1238    }
1239  }
1240
1241  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1242  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1243 
1244  // Encode Coefficients
1245  Bool bCodeDQP = getdQPFlag();
1246  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1247  setdQPFlag( bCodeDQP );
1248
1249  // --- write terminating bit ---
1250  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1251}
1252
1253/** check RD costs for a CU block encoded with merge
1254 * \param rpcBestCU
1255 * \param rpcTempCU
1256 * \returns Void
1257 */
1258Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1259{
1260  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1261  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
1262  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1263  Int numValidMergeCand = 0;
1264
1265  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1266  {
1267    uhInterDirNeighbours[ui] = 0;
1268  }
1269  UChar uhDepth = rpcTempCU->getDepth( 0 );
1270
1271#if H_3D_VSO // M1  //nececcary here?
1272  if( m_pcRdCost->getUseRenModel() )
1273  {
1274    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1275    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1276    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1277    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1278    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1279  }
1280#endif
1281
1282  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1283  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1284  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1285
1286  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1287  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1288  {
1289    mergeCandBuffer[ui] = 0;
1290  }
1291
1292  Bool bestIsSkip = false;
1293
1294  UInt iteration;
1295  if ( rpcTempCU->isLosslessCoded(0))
1296  {
1297    iteration = 1;
1298  }
1299  else 
1300  {
1301    iteration = 2;
1302  }
1303
1304  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1305  {
1306    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1307    {
1308      {
1309        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1310        {
1311
1312        if( !(bestIsSkip && uiNoResidual == 0) )
1313        {
1314          // set MC parameters
1315          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1316          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1317          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1318          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1319          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1320          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1321          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1322          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1323
1324       // do MC
1325       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1326       // estimate residual and encode everything
1327
1328#if H_3D_VSO //M2
1329       if( m_pcRdCost->getUseRenModel() )
1330       { //Reset
1331         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1332         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1333         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1334         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1335         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1336       }
1337#endif
1338
1339       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1340         m_ppcOrigYuv    [uhDepth],
1341         m_ppcPredYuvTemp[uhDepth],
1342         m_ppcResiYuvTemp[uhDepth],
1343         m_ppcResiYuvBest[uhDepth],
1344         m_ppcRecoYuvTemp[uhDepth],
1345         (uiNoResidual? true:false));
1346
1347
1348       if(uiNoResidual==0)
1349       {
1350         if(rpcTempCU->getQtRootCbf(0) == 0)
1351         {
1352           mergeCandBuffer[uiMergeCand] = 1;
1353         }
1354       }
1355
1356       rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1357          Int orgQP = rpcTempCU->getQP( 0 );
1358          xCheckDQP( rpcTempCU );
1359          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1360          rpcTempCU->initEstData( uhDepth, orgQP );
1361
1362
1363      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1364      {
1365        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1366      }
1367
1368    }
1369    }
1370   }
1371  }
1372
1373  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1374  {
1375    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1376    {
1377      if( rpcBestCU->getMergeFlag( 0 ))
1378      {
1379        *earlyDetectionSkipMode = true;
1380      }
1381      else
1382      {
1383        Int absoulte_MV=0;
1384        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1385        {
1386          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1387          {
1388            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1389            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1390            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1391            absoulte_MV+=iHor+iVer;
1392          }
1393        }
1394
1395        if(absoulte_MV == 0)
1396        {
1397          *earlyDetectionSkipMode = true;
1398        }
1399      }
1400    }
1401  }
1402 }
1403}
1404
1405
1406#if AMP_MRG
1407Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
1408#else
1409Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1410#endif
1411{
1412  UChar uhDepth = rpcTempCU->getDepth( 0 );
1413#if H_3D_VSO // M3
1414  if( m_pcRdCost->getUseRenModel() )
1415  {
1416    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1417    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1418    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1419    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1420    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1421  }
1422#endif
1423
1424  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1425 
1426  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1427
1428  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1429  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1430  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
1431 
1432#if AMP_MRG
1433  rpcTempCU->setMergeAMP (true);
1434  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
1435#else 
1436  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1437#endif
1438
1439#if AMP_MRG
1440  if ( !rpcTempCU->getMergeAMP() )
1441  {
1442    return;
1443  }
1444#endif
1445
1446#if RATE_CONTROL_LAMBDA_DOMAIN
1447  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
1448  {
1449    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
1450      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
1451      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
1452    m_temporalSAD = (Int)SAD;
1453  }
1454#endif
1455
1456  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
1457
1458
1459#if H_3D_VSO // M4
1460  if( m_pcRdCost->getUseLambdaScaleVSO() )
1461    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1462  else
1463#endif
1464  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1465
1466  xCheckDQP( rpcTempCU );
1467  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1468}
1469
1470Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
1471{
1472  UInt uiDepth = rpcTempCU->getDepth( 0 );
1473 
1474#if H_3D_VSO // M5
1475  if( m_pcRdCost->getUseRenModel() )
1476  {
1477    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
1478    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
1479    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
1480    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
1481    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1482  }
1483#endif
1484
1485  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1486
1487  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1488  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1489  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
1490 
1491  Bool bSeparateLumaChroma = true; // choose estimation mode
1492  UInt uiPreCalcDistC      = 0;
1493  if( !bSeparateLumaChroma )
1494  {
1495    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
1496  }
1497  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
1498
1499  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
1500 
1501  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
1502 
1503  m_pcEntropyCoder->resetBits();
1504  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1505  {
1506    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1507  }
1508  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1509  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1510  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1511  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
1512  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
1513
1514  // Encode Coefficients
1515  Bool bCodeDQP = getdQPFlag();
1516  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
1517  setdQPFlag( bCodeDQP );
1518 
1519  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1520 
1521  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1522  if(m_pcEncCfg->getUseSBACRD())
1523  {
1524    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1525  }
1526
1527#if H_3D_VSO // M6
1528  if( m_pcRdCost->getUseLambdaScaleVSO()) 
1529    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
1530  else
1531#endif
1532  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1533 
1534  xCheckDQP( rpcTempCU );
1535  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
1536}
1537
1538/** Check R-D costs for a CU with PCM mode.
1539 * \param rpcBestCU pointer to best mode CU data structure
1540 * \param rpcTempCU pointer to testing mode CU data structure
1541 * \returns Void
1542 *
1543 * \note Current PCM implementation encodes sample values in a lossless way. The distortion of PCM mode CUs are zero. PCM mode is selected if the best mode yields bits greater than that of PCM mode.
1544 */
1545Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1546{
1547  UInt uiDepth = rpcTempCU->getDepth( 0 );
1548
1549  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1550
1551  rpcTempCU->setIPCMFlag(0, true);
1552  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
1553  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
1554  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1555  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
1556  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
1557
1558  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
1559
1560  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1561
1562  m_pcEntropyCoder->resetBits();
1563  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1564  {
1565    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1566  }
1567  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1568  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
1569  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
1570  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
1571
1572  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1573
1574  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1575  if(m_pcEncCfg->getUseSBACRD())
1576  {
1577    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1578  }
1579#if H_3D_VSO // M44
1580  if ( m_pcRdCost->getUseVSO() )
1581    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1582  else
1583#endif
1584  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1585
1586  xCheckDQP( rpcTempCU );
1587  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
1588}
1589
1590/** check whether current try is the best with identifying the depth of current try
1591 * \param rpcBestCU
1592 * \param rpcTempCU
1593 * \returns Void
1594 */
1595Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
1596{
1597  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
1598  {
1599    TComYuv* pcYuv;
1600    // Change Information data
1601    TComDataCU* pcCU = rpcBestCU;
1602    rpcBestCU = rpcTempCU;
1603    rpcTempCU = pcCU;
1604
1605    // Change Prediction data
1606    pcYuv = m_ppcPredYuvBest[uiDepth];
1607    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
1608    m_ppcPredYuvTemp[uiDepth] = pcYuv;
1609
1610    // Change Reconstruction data
1611    pcYuv = m_ppcRecoYuvBest[uiDepth];
1612    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
1613    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
1614
1615    pcYuv = NULL;
1616    pcCU  = NULL;
1617
1618    if( m_bUseSBACRD )  // store temp best CI for next CU coding
1619      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1620  }
1621}
1622
1623Void TEncCu::xCheckDQP( TComDataCU* pcCU )
1624{
1625  UInt uiDepth = pcCU->getDepth( 0 );
1626
1627  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
1628  {
1629    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
1630    {
1631#if !RDO_WITHOUT_DQP_BITS
1632      m_pcEntropyCoder->resetBits();
1633      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
1634      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1635      if(m_pcEncCfg->getUseSBACRD())
1636      {
1637        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1638      }
1639#if H_3D_VSO // M45
1640      if ( m_pcRdCost->getUseVSO() )     
1641        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
1642      else
1643#endif
1644      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
1645#endif
1646    }
1647    else
1648    {
1649      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
1650    }
1651  }
1652}
1653
1654Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
1655{
1656  pDst->iN = pSrc->iN;
1657  for (Int i = 0; i < pSrc->iN; i++)
1658  {
1659    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
1660  }
1661}
1662Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
1663{
1664  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1665  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1666  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1667  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1668    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
1669  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1670    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
1671  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1672  {
1673    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
1674    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
1675    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
1676    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1677    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1678    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
1679    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
1680  }
1681  else
1682  {
1683    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
1684
1685    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1686    {
1687      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
1688      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
1689
1690      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
1691        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
1692      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1693      {
1694        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
1695      }
1696    }
1697  }
1698}
1699
1700Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
1701{
1702  UInt uiCurrDepth = uiNextDepth - 1;
1703  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
1704}
1705
1706/** Function for filling the PCM buffer of a CU using its original sample array
1707 * \param pcCU pointer to current CU
1708 * \param pcOrgYuv pointer to original sample array
1709 * \returns Void
1710 */
1711Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
1712{
1713
1714  UInt   width        = pCU->getWidth(0);
1715  UInt   height       = pCU->getHeight(0);
1716
1717  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
1718  Pel*   pDstY = pCU->getPCMSampleY();
1719  UInt   srcStride = pOrgYuv->getStride();
1720
1721  for(Int y = 0; y < height; y++ )
1722  {
1723    for(Int x = 0; x < width; x++ )
1724    {
1725      pDstY[x] = pSrcY[x];
1726    }
1727    pDstY += width;
1728    pSrcY += srcStride;
1729  }
1730
1731  Pel* pSrcCb       = pOrgYuv->getCbAddr();
1732  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
1733
1734  Pel* pDstCb       = pCU->getPCMSampleCb();
1735  Pel* pDstCr       = pCU->getPCMSampleCr();;
1736
1737  UInt srcStrideC = pOrgYuv->getCStride();
1738  UInt heightC   = height >> 1;
1739  UInt widthC    = width  >> 1;
1740
1741  for(Int y = 0; y < heightC; y++ )
1742  {
1743    for(Int x = 0; x < widthC; x++ )
1744    {
1745      pDstCb[x] = pSrcCb[x];
1746      pDstCr[x] = pSrcCr[x];
1747    }
1748    pDstCb += widthC;
1749    pDstCr += widthC;
1750    pSrcCb += srcStrideC;
1751    pSrcCr += srcStrideC;
1752  }
1753}
1754
1755#if ADAPTIVE_QP_SELECTION
1756/** Collect ARL statistics from one block
1757  */
1758Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
1759{
1760  for( Int n = 0; n < NumCoeffInCU; n++ )
1761  {
1762    Int u = abs( rpcCoeff[ n ] );
1763    Int absc = rpcArlCoeff[ n ];
1764
1765    if( u != 0 )
1766    {
1767      if( u < LEVEL_RANGE )
1768      {
1769        cSum[ u ] += ( Double )absc;
1770        numSamples[ u ]++;
1771      }
1772      else 
1773      {
1774        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
1775        numSamples[ LEVEL_RANGE ]++;
1776      }
1777    }
1778  }
1779
1780  return 0;
1781}
1782
1783/** Collect ARL statistics from one LCU
1784 * \param pcCU
1785 */
1786Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
1787{
1788  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
1789  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
1790
1791  TCoeff* pCoeffY = rpcCU->getCoeffY();
1792  Int* pArlCoeffY = rpcCU->getArlCoeffY();
1793
1794  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
1795  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
1796
1797  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
1798  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
1799
1800  // Collect stats to cSum[][] and numSamples[][]
1801  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
1802  {
1803    UInt uiTrIdx = rpcCU->getTransformIdx(i);
1804
1805    if(rpcCU->getPredictionMode(i) == MODE_INTER)
1806    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
1807    {
1808      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
1809    }//Note that only InterY is processed. QP rounding is based on InterY data only.
1810   
1811    pCoeffY  += uiMinNumCoeffInCU;
1812    pArlCoeffY  += uiMinNumCoeffInCU;
1813  }
1814
1815  for(Int u=1; u<LEVEL_RANGE;u++)
1816  {
1817    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
1818    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
1819  }
1820  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
1821  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
1822}
1823#endif
1824//! \}
Note: See TracBrowser for help on using the repository browser.