source: 3DVCSoftware/branches/HTM-DEV-2.0-dev2-MediaTek/source/Lib/TLibEncoder/TEncCu.cpp @ 559

Last change on this file since 559 was 559, checked in by mediatek-htm, 11 years ago

Integration of JCT3V-E0173, fast texture encoding algorithm for dependent views
The MACRO is "MTK_FAST_TEXTURE_ENCODING_E0173"

by Na Zhang (Na.Zhang@…)

  • Property svn:eol-style set to native
File size: 82.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#if H_3D_ARP
68  m_ppcWeightedTempCU = new TComDataCU*[m_uhTotalDepth-1];
69#endif
70
71  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
72  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
73  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
74  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
75  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
76  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
77  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
78 
79  UInt uiNumPartitions;
80  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
81  {
82    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
83    UInt uiWidth  = uiMaxWidth  >> i;
84    UInt uiHeight = uiMaxHeight >> i;
85   
86    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
87    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
88   
89#if H_3D_ARP
90    m_ppcWeightedTempCU[i] = new TComDataCU; m_ppcWeightedTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
91#endif 
92
93    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight);
94    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight);
95    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight);
96   
97    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight);
98    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight);
99    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight);
100   
101    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
102  }
103 
104  m_bEncodeDQP = false;
105#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
106  m_LCUPredictionSAD = 0;
107  m_addSADDepth      = 0;
108  m_temporalSAD      = 0;
109#endif
110
111  // initialize partition order.
112  UInt* piTmp = &g_auiZscanToRaster[0];
113  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
114  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
115 
116  // initialize conversion matrix from partition index to pel
117  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
118}
119
120Void TEncCu::destroy()
121{
122  Int i;
123 
124  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
125  {
126    if(m_ppcBestCU[i])
127    {
128      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
129    }
130    if(m_ppcTempCU[i])
131    {
132      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
133    }
134#if H_3D_ARP
135    if(m_ppcWeightedTempCU[i])
136    {
137      m_ppcWeightedTempCU[i]->destroy(); delete m_ppcWeightedTempCU[i]; m_ppcWeightedTempCU[i] = NULL;
138    }
139#endif
140    if(m_ppcPredYuvBest[i])
141    {
142      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
143    }
144    if(m_ppcResiYuvBest[i])
145    {
146      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
147    }
148    if(m_ppcRecoYuvBest[i])
149    {
150      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
151    }
152    if(m_ppcPredYuvTemp[i])
153    {
154      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
155    }
156    if(m_ppcResiYuvTemp[i])
157    {
158      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
159    }
160    if(m_ppcRecoYuvTemp[i])
161    {
162      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
163    }
164    if(m_ppcOrigYuv[i])
165    {
166      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
167    }
168  }
169  if(m_ppcBestCU)
170  {
171    delete [] m_ppcBestCU;
172    m_ppcBestCU = NULL;
173  }
174  if(m_ppcTempCU)
175  {
176    delete [] m_ppcTempCU;
177    m_ppcTempCU = NULL;
178  }
179
180#if H_3D_ARP
181  if(m_ppcWeightedTempCU)
182  {
183    delete [] m_ppcWeightedTempCU; 
184    m_ppcWeightedTempCU = NULL; 
185  }
186#endif
187  if(m_ppcPredYuvBest)
188  {
189    delete [] m_ppcPredYuvBest;
190    m_ppcPredYuvBest = NULL;
191  }
192  if(m_ppcResiYuvBest)
193  {
194    delete [] m_ppcResiYuvBest;
195    m_ppcResiYuvBest = NULL;
196  }
197  if(m_ppcRecoYuvBest)
198  {
199    delete [] m_ppcRecoYuvBest;
200    m_ppcRecoYuvBest = NULL;
201  }
202  if(m_ppcPredYuvTemp)
203  {
204    delete [] m_ppcPredYuvTemp;
205    m_ppcPredYuvTemp = NULL;
206  }
207  if(m_ppcResiYuvTemp)
208  {
209    delete [] m_ppcResiYuvTemp;
210    m_ppcResiYuvTemp = NULL;
211  }
212  if(m_ppcRecoYuvTemp)
213  {
214    delete [] m_ppcRecoYuvTemp;
215    m_ppcRecoYuvTemp = NULL;
216  }
217  if(m_ppcOrigYuv)
218  {
219    delete [] m_ppcOrigYuv;
220    m_ppcOrigYuv = NULL;
221  }
222}
223
224/** \param    pcEncTop      pointer of encoder class
225 */
226Void TEncCu::init( TEncTop* pcEncTop )
227{
228  m_pcEncCfg           = pcEncTop;
229  m_pcPredSearch       = pcEncTop->getPredSearch();
230  m_pcTrQuant          = pcEncTop->getTrQuant();
231  m_pcBitCounter       = pcEncTop->getBitCounter();
232  m_pcRdCost           = pcEncTop->getRdCost();
233 
234  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
235  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
236  m_pcSbacCoder       = pcEncTop->getSbacCoder();
237  m_pcBinCABAC         = pcEncTop->getBinCABAC();
238 
239  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
240  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
241 
242  m_bUseSBACRD        = pcEncTop->getUseSBACRD();
243  m_pcRateCtrl        = pcEncTop->getRateCtrl();
244}
245
246// ====================================================================================================================
247// Public member functions
248// ====================================================================================================================
249
250/** \param  rpcCU pointer of CU data class
251 */
252Void TEncCu::compressCU( TComDataCU*& rpcCU )
253{
254  // initialize CU data
255  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
256  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
257
258#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
259  m_addSADDepth      = 0;
260  m_LCUPredictionSAD = 0;
261  m_temporalSAD      = 0;
262#endif
263
264  // analysis of CU
265  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
266
267#if ADAPTIVE_QP_SELECTION
268  if( m_pcEncCfg->getUseAdaptQpSelect() )
269  {
270    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
271    {
272      xLcuCollectARLStats( rpcCU);
273    }
274  }
275#endif
276}
277/** \param  pcCU  pointer of CU data class
278 */
279Void TEncCu::encodeCU ( TComDataCU* pcCU )
280{
281  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
282  {
283    setdQPFlag(true);
284  }
285
286  // Encode CU data
287  xEncodeCU( pcCU, 0, 0 );
288}
289
290// ====================================================================================================================
291// Protected member functions
292// ====================================================================================================================
293/** Derive small set of test modes for AMP encoder speed-up
294 *\param   rpcBestCU
295 *\param   eParentPartSize
296 *\param   bTestAMP_Hor
297 *\param   bTestAMP_Ver
298 *\param   bTestMergeAMP_Hor
299 *\param   bTestMergeAMP_Ver
300 *\returns Void
301*/
302#if AMP_ENC_SPEEDUP
303#if AMP_MRG
304Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
305#else
306Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
307#endif
308{
309  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
310  {
311    bTestAMP_Hor = true;
312  }
313  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
314  {
315    bTestAMP_Ver = true;
316  }
317  else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false )
318  {
319    bTestAMP_Hor = true;         
320    bTestAMP_Ver = true;         
321  }
322
323#if AMP_MRG
324  //! Utilizing the partition size of parent PU   
325  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
326  { 
327    bTestMergeAMP_Hor = true;
328    bTestMergeAMP_Ver = true;
329  }
330
331  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
332  {
333    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
334    {
335      bTestMergeAMP_Hor = true;
336    }
337    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
338    {
339      bTestMergeAMP_Ver = true;
340    }
341  }
342
343  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
344  {
345    bTestMergeAMP_Hor = true;         
346    bTestMergeAMP_Ver = true;         
347  }
348
349  if ( rpcBestCU->getWidth(0) == 64 )
350  { 
351    bTestAMP_Hor = false;
352    bTestAMP_Ver = false;
353  }   
354#else
355  //! Utilizing the partition size of parent PU       
356  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
357  { 
358    bTestAMP_Hor = true;
359    bTestAMP_Ver = true;
360  }
361
362  if ( eParentPartSize == SIZE_2Nx2N )
363  { 
364    bTestAMP_Hor = false;
365    bTestAMP_Ver = false;
366  }     
367#endif
368}
369#endif
370
371// ====================================================================================================================
372// Protected member functions
373// ====================================================================================================================
374/** Compress a CU block recursively with enabling sub-LCU-level delta QP
375 *\param   rpcBestCU
376 *\param   rpcTempCU
377 *\param   uiDepth
378 *\returns Void
379 *
380 *- for loop of QP value to compress the current CU with all possible QP
381*/
382#if AMP_ENC_SPEEDUP
383Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize )
384#else
385Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
386#endif
387{
388  TComPic* pcPic = rpcBestCU->getPic();
389
390#if H_3D_QTLPC
391  TComSPS *sps            = pcPic->getSlice(0)->getSPS();
392  TComPic *pcTexture      = rpcBestCU->getSlice()->getTexturePic();
393
394  Bool  depthMapDetect    = (pcTexture != NULL);
395  Bool  bIntraSliceDetect = (rpcBestCU->getSlice()->getSliceType() == I_SLICE);
396
397  Bool rapPic             = (rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA);
398
399  Bool bTry2NxN           = true;
400  Bool bTryNx2N           = true;
401#endif
402  // get Original YUV data from picture
403  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
404
405  // variables for fast encoder decision
406  Bool    bEarlySkip  = false;
407  Bool    bTrySplit    = true;
408  Double  fRD_Skip    = MAX_DOUBLE;
409
410  // variable for Early CU determination
411  Bool    bSubBranch = true;
412
413  // variable for Cbf fast mode PU decision
414  Bool    doNotBlockPu = true;
415  Bool earlyDetectionSkipMode = false;
416
417  Bool    bTrySplitDQP  = true;
418
419  static  Double  afCost[ MAX_CU_DEPTH ];
420  static  Int      aiNum [ MAX_CU_DEPTH ];
421
422  if ( rpcBestCU->getAddr() == 0 )
423  {
424    ::memset( afCost, 0, sizeof( afCost ) );
425    ::memset( aiNum,  0, sizeof( aiNum  ) );
426  }
427
428  Bool bBoundary = false;
429  UInt uiLPelX   = rpcBestCU->getCUPelX();
430  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
431  UInt uiTPelY   = rpcBestCU->getCUPelY();
432  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
433
434  Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
435  Int iMinQP;
436  Int iMaxQP;
437  Bool isAddLowestQP = false;
438  Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY();
439
440  if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
441  {
442    Int idQP = m_pcEncCfg->getMaxDeltaQP();
443    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
444    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
445    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
446    {
447      isAddLowestQP = true; 
448      iMinQP = iMinQP - 1;
449    }
450  }
451  else
452  {
453    iMinQP = rpcTempCU->getQP(0);
454    iMaxQP = rpcTempCU->getQP(0);
455  }
456
457#if RATE_CONTROL_LAMBDA_DOMAIN
458  if ( m_pcEncCfg->getUseRateCtrl() )
459  {
460    iMinQP = m_pcRateCtrl->getRCQP();
461    iMaxQP = m_pcRateCtrl->getRCQP();
462  }
463#else
464  if(m_pcEncCfg->getUseRateCtrl())
465  {
466    Int qp = m_pcRateCtrl->getUnitQP();
467    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
468    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
469  }
470#endif
471#if H_3D_IC
472  Bool bICEnabled = rpcTempCU->getSlice()->getViewIndex() && ( rpcTempCU->getSlice()->getSliceType() == P_SLICE || rpcTempCU->getSlice()->getSliceType() == B_SLICE );
473  bICEnabled = bICEnabled && rpcTempCU->getSlice()->getApplyIC();
474#endif
475  // If slice start or slice end is within this cu...
476  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
477  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart();
478  Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart());
479  Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() );
480  // We need to split, so don't try these modes.
481  if(!bSliceEnd && !bSliceStart && bInsidePicture )
482  {
483#if  MTK_FAST_TEXTURE_ENCODING_E0173
484    Bool bIVFMerge = false;
485    Int  iIVFMaxD = 0;
486    Bool bFMD = false;
487#endif
488    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
489    {
490      if (isAddLowestQP && (iQP == iMinQP))
491      {
492        iQP = lowestQP;
493      }
494      // variables for fast encoder decision
495      bEarlySkip  = false;
496      bTrySplit    = true;
497      fRD_Skip    = MAX_DOUBLE;
498
499      rpcTempCU->initEstData( uiDepth, iQP );
500#if H_3D_QTLPC
501      //logic for setting bTrySplit using the partition information that is stored of the texture colocated CU
502
503      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL())
504      {
505        TComDataCU* pcTextureCU = pcTexture->getCU( rpcBestCU->getAddr() ); //Corresponding texture LCU
506        UInt uiCUIdx            = rpcBestCU->getZorderIdxInCU();
507        assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth); //Depth cannot be more partitionned than the texture.
508        if (pcTextureCU->getDepth(uiCUIdx) > uiDepth || pcTextureCU->getPartitionSize(uiCUIdx) == SIZE_NxN) //Texture was split.
509        {
510          bTrySplit = true;
511          bTryNx2N  = true;
512          bTry2NxN  = true;
513        }
514        else
515        {
516          bTrySplit = false;
517          bTryNx2N  = false;
518          bTry2NxN  = false;
519        }
520      }
521#endif
522
523#if H_3D_NBDV
524      DisInfo DvInfo; 
525      DvInfo.bDV = false;
526      DvInfo.m_acNBDV.setZero();
527      DvInfo.m_aVIdxCan = 0;
528#if H_3D_NBDV_REF
529      DvInfo.m_acDoNBDV.setZero();
530#endif
531
532      if( rpcTempCU->getSlice()->getSliceType() != I_SLICE )
533      {
534#if H_3D_ARP && H_3D_IV_MERGE
535        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) || rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
536#else
537#if H_3D_ARP
538        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) )
539#else
540#if H_3D_IV_MERGE
541        if( rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
542#else
543        if (0)
544#endif
545#endif
546#endif
547        {
548          PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
549          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );     
550#if H_3D_NBDV_REF
551          if(rpcTempCU->getSlice()->getVPS()->getDepthRefinementFlag( rpcTempCU->getSlice()->getLayerIdInVps()))
552            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo, true);
553          else
554#endif
555            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo);
556
557          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
558          rpcBestCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
559          rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
560        }
561      }
562#if  MTK_FAST_TEXTURE_ENCODING_E0173
563      if(rpcTempCU->getSlice()->getViewIndex() && !rpcTempCU->getSlice()->getIsDepth())
564      {
565        PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
566        rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); 
567        rpcTempCU->getIVNStatus( 0, &DvInfo,  bIVFMerge, iIVFMaxD);
568        rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
569      }
570#endif
571#endif
572      // do inter modes, SKIP and 2Nx2N
573      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
574      {
575#if H_3D_IC
576        for( UInt uiICId = 0; uiICId < ( bICEnabled ? 2 : 1 ); uiICId++ )
577        {
578          Bool bICFlag = uiICId ? true : false;
579#endif
580        // 2Nx2N
581        if(m_pcEncCfg->getUseEarlySkipDetection())
582        {
583#if H_3D_IC
584          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
585#endif
586#if  MTK_FAST_TEXTURE_ENCODING_E0173
587          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N
588#else
589          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N
590#endif
591        }
592        // SKIP
593#if H_3D_IC
594        rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
595#endif
596        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
597#if  MTK_FAST_TEXTURE_ENCODING_E0173
598        bFMD = bIVFMerge && rpcBestCU->isSkipped(0);
599#endif
600        rpcTempCU->initEstData( uiDepth, iQP );
601
602        // fast encoder decision for early skip
603        if ( m_pcEncCfg->getUseFastEnc() )
604        {
605          Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
606          if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] )
607          {
608            bEarlySkip = true;
609            bTrySplit  = false;
610          }
611        }
612
613        if(!m_pcEncCfg->getUseEarlySkipDetection())
614        {
615          // 2Nx2N, NxN
616          if ( !bEarlySkip )
617          {
618#if H_3D_IC
619            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
620#endif
621#if  MTK_FAST_TEXTURE_ENCODING_E0173
622            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP );
623#else
624            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );
625#endif
626            if(m_pcEncCfg->getUseCbfFastMode())
627            {
628              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
629            }
630          }
631        }
632#if H_3D_IC
633        }
634#endif
635      }
636
637#if H_3D_QTLPC
638      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL())
639      {
640        bTrySplitDQP = bTrySplit;
641      }
642      else
643      {
644#endif
645        if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
646        {
647          if(iQP == iBaseQP)
648          {
649            bTrySplitDQP = bTrySplit;
650          }
651        }
652        else
653        {
654          bTrySplitDQP = bTrySplit;
655        }
656#if H_3D_QTLPC
657      }
658#endif
659      if (isAddLowestQP && (iQP == lowestQP))
660      {
661        iQP = iMinQP;
662      }
663    }
664
665#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
666    if ( uiDepth <= m_addSADDepth )
667    {
668      m_LCUPredictionSAD += m_temporalSAD;
669      m_addSADDepth = uiDepth;
670    }
671#endif
672
673#if H_3D_DIM_ENC
674    if( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() )
675    {
676      earlyDetectionSkipMode = false;
677    }
678#endif
679
680    if(!earlyDetectionSkipMode)
681    {
682      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
683      {
684        if (isAddLowestQP && (iQP == iMinQP))
685        {
686          iQP = lowestQP;
687        }
688        rpcTempCU->initEstData( uiDepth, iQP );
689
690        // do inter modes, NxN, 2NxN, and Nx2N
691        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
692        {
693          // 2Nx2N, NxN
694          if ( !bEarlySkip )
695          {
696            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
697            {
698              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu
699#if H_3D_QTLPC
700                && bTrySplit
701#endif
702                )
703              {
704#if  MTK_FAST_TEXTURE_ENCODING_E0173
705                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN, bFMD  );
706#else
707                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
708#endif
709                rpcTempCU->initEstData( uiDepth, iQP );
710              }
711            }
712          }
713
714          // 2NxN, Nx2N
715          if(doNotBlockPu
716#if H_3D_QTLPC
717            && bTryNx2N
718#endif
719            )
720          {
721#if  MTK_FAST_TEXTURE_ENCODING_E0173
722            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N, bFMD  );
723#else
724            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
725#endif
726            rpcTempCU->initEstData( uiDepth, iQP );
727            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
728            {
729              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
730            }
731          }
732          if(doNotBlockPu
733#if H_3D_QTLPC
734            && bTry2NxN
735#endif
736            )
737          {
738#if  MTK_FAST_TEXTURE_ENCODING_E0173
739            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN, bFMD  );
740#else
741            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
742#endif
743            rpcTempCU->initEstData( uiDepth, iQP );
744            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
745            {
746              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
747            }
748          }
749
750#if 1
751          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
752          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
753          {
754#if AMP_ENC_SPEEDUP       
755            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
756
757#if AMP_MRG
758            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
759
760            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
761#else
762            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
763#endif
764
765            //! Do horizontal AMP
766            if ( bTestAMP_Hor )
767            {
768              if(doNotBlockPu
769#if H_3D_QTLPC
770                && bTry2NxN
771#endif
772                )
773              {
774#if  MTK_FAST_TEXTURE_ENCODING_E0173
775                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD );
776#else
777                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
778#endif
779                rpcTempCU->initEstData( uiDepth, iQP );
780                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
781                {
782                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
783                }
784              }
785              if(doNotBlockPu
786#if H_3D_QTLPC
787                && bTry2NxN
788#endif
789                )
790              {
791#if  MTK_FAST_TEXTURE_ENCODING_E0173
792                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD );
793#else
794                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
795#endif
796                rpcTempCU->initEstData( uiDepth, iQP );
797                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
798                {
799                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
800                }
801              }
802            }
803#if AMP_MRG
804            else if ( bTestMergeAMP_Hor ) 
805            {
806              if(doNotBlockPu
807#if H_3D_QTLPC
808                && bTry2NxN
809#endif
810                )
811              {
812#if  MTK_FAST_TEXTURE_ENCODING_E0173
813                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD, true );
814#else
815                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
816#endif
817                rpcTempCU->initEstData( uiDepth, iQP );
818                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
819                {
820                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
821                }
822              }
823              if(doNotBlockPu
824#if H_3D_QTLPC
825                && bTry2NxN
826#endif
827                )
828              {
829#if  MTK_FAST_TEXTURE_ENCODING_E0173
830                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD, true );
831#else
832                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
833#endif
834                rpcTempCU->initEstData( uiDepth, iQP );
835                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
836                {
837                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
838                }
839              }
840            }
841#endif
842
843            //! Do horizontal AMP
844            if ( bTestAMP_Ver )
845            {
846              if(doNotBlockPu
847#if H_3D_QTLPC
848                && bTryNx2N
849#endif
850                )
851              {
852#if  MTK_FAST_TEXTURE_ENCODING_E0173
853                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD );
854#else
855                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
856#endif
857                rpcTempCU->initEstData( uiDepth, iQP );
858                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
859                {
860                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
861                }
862              }
863              if(doNotBlockPu
864#if H_3D_QTLPC
865                && bTryNx2N
866#endif
867                )
868              {
869#if  MTK_FAST_TEXTURE_ENCODING_E0173
870                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD );
871#else
872                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
873#endif
874                rpcTempCU->initEstData( uiDepth, iQP );
875              }
876            }
877#if AMP_MRG
878            else if ( bTestMergeAMP_Ver )
879            {
880              if(doNotBlockPu
881#if H_3D_QTLPC
882                && bTryNx2N
883#endif
884                )
885              {
886#if  MTK_FAST_TEXTURE_ENCODING_E0173
887                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD, true );
888#else
889                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
890#endif
891                rpcTempCU->initEstData( uiDepth, iQP );
892                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
893                {
894                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
895                }
896              }
897              if(doNotBlockPu
898#if H_3D_QTLPC
899                && bTryNx2N
900#endif
901                )
902              {
903#if  MTK_FAST_TEXTURE_ENCODING_E0173
904                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD, true );
905#else
906                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
907#endif
908                rpcTempCU->initEstData( uiDepth, iQP );
909              }
910            }
911#endif
912
913#else
914#if H_3D_QTLPC
915            if (bTry2NxN)
916            {
917#endif
918              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
919              rpcTempCU->initEstData( uiDepth, iQP );
920              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
921              rpcTempCU->initEstData( uiDepth, iQP );
922#if H_3D_QTLPC
923            }
924            if (bTryNx2N)
925            {
926#endif
927              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
928              rpcTempCU->initEstData( uiDepth, iQP );
929              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
930              rpcTempCU->initEstData( uiDepth, iQP );
931#if H_3D_QTLPC
932            }
933#endif
934
935#endif
936          }   
937#endif
938        }
939#if  MTK_FAST_TEXTURE_ENCODING_E0173
940        if(!bFMD)
941        {
942#endif
943        // do normal intra modes
944#if H_3D_DIM_ENC
945        if ( !bEarlySkip || ( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() ) )
946#else
947        if ( !bEarlySkip )
948#endif
949        {
950          // speedup for inter frames
951          if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
952            rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
953            rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
954              rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     
955#if H_3D_DIM_ENC
956            || ( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() )
957#endif
958            ) // avoid very complex intra if it is unlikely
959          {
960            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
961            rpcTempCU->initEstData( uiDepth, iQP );
962            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
963            {
964#if H_3D_QTLPC //Try IntraNxN
965              if(bTrySplit)
966              {
967#endif
968                if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
969                {
970                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
971                  rpcTempCU->initEstData( uiDepth, iQP );
972                }
973#if H_3D_QTLPC
974              }
975#endif
976            }
977          }
978        }
979
980        // test PCM
981        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
982          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
983          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
984        {
985          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
986          UInt uiBestBits = rpcBestCU->getTotalBits();
987#if H_3D_VSO // M7
988          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
989          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
990#else
991          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
992#endif
993          {
994            xCheckIntraPCM (rpcBestCU, rpcTempCU);
995            rpcTempCU->initEstData( uiDepth, iQP );
996          }
997        }
998#if  MTK_FAST_TEXTURE_ENCODING_E0173
999        }
1000#endif
1001        if (isAddLowestQP && (iQP == lowestQP))
1002        {
1003          iQP = iMinQP;
1004        }
1005      }
1006    }
1007
1008    m_pcEntropyCoder->resetBits();
1009    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1010    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1011    if(m_pcEncCfg->getUseSBACRD())
1012    {
1013      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1014    }
1015
1016#if H_3D_VSO // M8
1017    if ( m_pcRdCost->getUseVSO() )   
1018      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
1019    else
1020#endif
1021    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1022
1023    // accumulate statistics for early skip
1024    if ( m_pcEncCfg->getUseFastEnc() )
1025    {
1026      if ( rpcBestCU->isSkipped(0) )
1027      {
1028        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
1029        afCost[ iIdx ] += rpcBestCU->getTotalCost();
1030        aiNum [ iIdx ] ++;
1031      }
1032    }
1033
1034    // Early CU determination
1035    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
1036    {
1037      bSubBranch = false;
1038    }
1039    else
1040    {
1041      bSubBranch = true;
1042    }
1043#if  MTK_FAST_TEXTURE_ENCODING_E0173
1044    if(rpcBestCU->getSlice()->getViewIndex() && !rpcBestCU->getSlice()->getIsDepth() && (uiDepth >=iIVFMaxD) && rpcBestCU->isSkipped(0))
1045    {
1046      bSubBranch = false;
1047    }
1048#endif
1049  }
1050  else if(!(bSliceEnd && bInsidePicture))
1051  {
1052    bBoundary = true;
1053#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
1054    m_addSADDepth++;
1055#endif
1056  }
1057
1058  // copy orginal YUV samples to PCM buffer
1059  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
1060  {
1061    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
1062  }
1063  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1064  {
1065    Int idQP = m_pcEncCfg->getMaxDeltaQP();
1066    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
1067    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
1068    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
1069    {
1070      isAddLowestQP = true;
1071      iMinQP = iMinQP - 1;     
1072    }
1073  }
1074  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1075  {
1076    iMinQP = iBaseQP;
1077    iMaxQP = iBaseQP;
1078  }
1079  else
1080  {
1081    Int iStartQP;
1082    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
1083    {
1084      iStartQP = rpcTempCU->getQP(0);
1085    }
1086    else
1087    {
1088      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1089      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
1090    }
1091    iMinQP = iStartQP;
1092    iMaxQP = iStartQP;
1093  }
1094#if RATE_CONTROL_LAMBDA_DOMAIN
1095  if ( m_pcEncCfg->getUseRateCtrl() )
1096  {
1097    iMinQP = m_pcRateCtrl->getRCQP();
1098    iMaxQP = m_pcRateCtrl->getRCQP();
1099  }
1100#else
1101  if(m_pcEncCfg->getUseRateCtrl())
1102  {
1103    Int qp = m_pcRateCtrl->getUnitQP();
1104    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
1105    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
1106  }
1107#endif
1108  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1109  {
1110    if (isAddLowestQP && (iQP == iMinQP))
1111    {
1112      iQP = lowestQP;
1113    }
1114    rpcTempCU->initEstData( uiDepth, iQP );
1115
1116    // further split
1117    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1118    {
1119#if H_3D_VSO // M9
1120      // reset Model
1121      if( m_pcRdCost->getUseRenModel() )
1122      {
1123        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
1124        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
1125        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
1126        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
1127        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1128      }
1129#endif
1130
1131      UChar       uhNextDepth         = uiDepth+1;
1132      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1133      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1134
1135      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1136      {
1137        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1138        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1139
1140        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
1141        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1142        {
1143          if( m_bUseSBACRD )
1144          {
1145            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1146            {
1147              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1148            }
1149            else
1150            {
1151              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1152            }
1153          }
1154
1155#if AMP_ENC_SPEEDUP
1156          if ( rpcBestCU->isIntra(0) )
1157          {
1158            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
1159          }
1160          else
1161          {
1162            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
1163          }
1164#else
1165          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1166#endif
1167
1168          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1169          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1170        }
1171        else if (bInSlice)
1172        {
1173          pcSubBestPartCU->copyToPic( uhNextDepth );
1174          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
1175        }
1176      }
1177
1178      if( !bBoundary )
1179      {
1180        m_pcEntropyCoder->resetBits();
1181        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
1182
1183        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1184        if(m_pcEncCfg->getUseSBACRD())
1185        {
1186          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1187        }
1188      }
1189
1190#if H_3D_VSO // M10
1191      if ( m_pcRdCost->getUseVSO() )
1192        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1193      else
1194#endif
1195      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1196
1197      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
1198      {
1199        Bool hasResidual = false;
1200        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1201        {
1202          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
1203              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
1204          {
1205            hasResidual = true;
1206            break;
1207          }
1208        }
1209
1210        UInt uiTargetPartIdx;
1211        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
1212        {
1213          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1214        }
1215        else
1216        {
1217          uiTargetPartIdx = 0;
1218        }
1219        if ( hasResidual )
1220        {
1221#if !RDO_WITHOUT_DQP_BITS
1222          m_pcEntropyCoder->resetBits();
1223          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1224          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1225          if(m_pcEncCfg->getUseSBACRD())
1226          {
1227            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1228          }
1229#if H_3D_VSO // M11
1230          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1231            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1232          else
1233#endif
1234          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1235#endif
1236
1237          Bool foundNonZeroCbf = false;
1238          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1239          assert( foundNonZeroCbf );
1240        }
1241        else
1242        {
1243          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1244        }
1245      }
1246
1247      if( m_bUseSBACRD )
1248      {
1249        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1250      }
1251      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
1252                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1253      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1254                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
1255      if(isEndOfSlice||isEndOfSliceSegment)
1256      {
1257        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1258      }
1259      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1260    }                                                                                  // with sub partitioned prediction.
1261    if (isAddLowestQP && (iQP == lowestQP))
1262    {
1263      iQP = iMinQP;
1264    }
1265  }
1266
1267
1268#if H_3D_VSO // M12
1269  if( m_pcRdCost->getUseRenModel() )
1270  {
1271    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1272    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1273    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1274    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1275    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1276  }
1277#endif
1278
1279  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1280
1281  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1282  if( bBoundary ||(bSliceEnd && bInsidePicture))
1283  {
1284    return;
1285  }
1286
1287  // Assert if Best prediction mode is NONE
1288  // Selected mode's RD-cost must be not MAX_DOUBLE.
1289  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1290  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1291  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1292}
1293
1294/** finish encoding a cu and handle end-of-slice conditions
1295 * \param pcCU
1296 * \param uiAbsPartIdx
1297 * \param uiDepth
1298 * \returns Void
1299 */
1300Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1301{
1302  TComPic* pcPic = pcCU->getPic();
1303  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1304
1305  //Calculate end address
1306  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1307
1308  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1309  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1310  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1311  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1312  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1313  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1314  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1315  {
1316    uiInternalAddress--;
1317    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1318    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1319  }
1320  uiInternalAddress++;
1321  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1322  {
1323    uiInternalAddress = 0;
1324    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1325  }
1326  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1327
1328  // Encode slice finish
1329  Bool bTerminateSlice = false;
1330  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1331  {
1332    bTerminateSlice = true;
1333  }
1334  UInt uiGranularityWidth = g_uiMaxCUWidth;
1335  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1336  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1337  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1338    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1339 
1340  if(granularityBoundary)
1341  {
1342    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1343    if (!bTerminateSlice)
1344      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1345  }
1346 
1347  Int numberOfWrittenBits = 0;
1348  if (m_pcBitCounter)
1349  {
1350    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1351  }
1352 
1353  // Calculate slice end IF this CU puts us over slice bit size.
1354  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1355  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1356  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1357  {
1358    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1359  }
1360  // Set slice end parameter
1361  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1362  {
1363    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1364    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1365    return;
1366  }
1367  // Set dependent slice end parameter
1368  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1369  {
1370    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1371    return;
1372  }
1373  if(granularityBoundary)
1374  {
1375    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1376    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1377    if (m_pcBitCounter)
1378    {
1379      m_pcEntropyCoder->resetBits();     
1380    }
1381  }
1382}
1383
1384/** Compute QP for each CU
1385 * \param pcCU Target CU
1386 * \param uiDepth CU depth
1387 * \returns quantization parameter
1388 */
1389Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1390{
1391  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1392  Int iQpOffset = 0;
1393  if ( m_pcEncCfg->getUseAdaptiveQP() )
1394  {
1395    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1396    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1397    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1398    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1399    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1400    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1401    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1402
1403    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1404    Double dAvgAct = pcAQLayer->getAvgActivity();
1405    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1406    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1407    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1408    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1409  }
1410  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1411}
1412
1413/** encode a CU block recursively
1414 * \param pcCU
1415 * \param uiAbsPartIdx
1416 * \param uiDepth
1417 * \returns Void
1418 */
1419Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1420{
1421  TComPic* pcPic = pcCU->getPic();
1422 
1423  Bool bBoundary = false;
1424  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1425  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1426  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1427  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1428 
1429#if H_MV_ENC_DEC_TRAC
1430  DTRACE_CU_S("=========== coding_quadtree ===========\n")
1431  DTRACE_CU("x0", uiLPelX)
1432  DTRACE_CU("x1", uiTPelY)
1433  DTRACE_CU("log2CbSize", g_uiMaxCUWidth>>uiDepth)
1434  DTRACE_CU("cqtDepth"  , uiDepth)
1435#endif
1436
1437  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1438  // If slice start is within this cu...
1439  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1440    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1441  // We need to split, so don't try these modes.
1442  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1443  {
1444    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1445  }
1446  else
1447  {
1448    bBoundary = true;
1449  }
1450 
1451  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1452  {
1453    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1454    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1455    {
1456      setdQPFlag(true);
1457    }
1458    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1459    {
1460      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1461      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1462      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1463      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1464      {
1465        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1466      }
1467    }
1468    return;
1469  }
1470 
1471#if H_MV_ENC_DEC_TRAC
1472  DTRACE_CU_S("=========== coding_unit ===========\n")
1473#endif
1474
1475  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1476  {
1477    setdQPFlag(true);
1478  }
1479  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1480  {
1481    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1482  }
1483  if( !pcCU->getSlice()->isIntra() )
1484  {
1485    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1486  }
1487 
1488  if( pcCU->isSkipped( uiAbsPartIdx ) )
1489  {
1490#if H_MV_ENC_DEC_TRAC
1491    DTRACE_PU_S("=========== prediction_unit ===========\n")
1492    DTRACE_PU("x0", uiLPelX)
1493    DTRACE_PU("x1", uiTPelY)
1494#endif
1495    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1496#if H_3D_IC
1497    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1498#endif
1499#if H_3D_ARP
1500    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1501#endif
1502    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1503    return;
1504  }
1505  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1506 
1507  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1508 
1509  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1510  {
1511    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1512
1513    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1514    {
1515      // Encode slice finish
1516      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1517      return;
1518    }
1519  }
1520
1521  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1522  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1523#if H_3D_IC
1524  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1525#endif
1526#if H_3D_ARP
1527  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1528#endif
1529
1530  // Encode Coefficients
1531  Bool bCodeDQP = getdQPFlag();
1532  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1533  setdQPFlag( bCodeDQP );
1534
1535  // --- write terminating bit ---
1536  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1537}
1538
1539#if RATE_CONTROL_INTRA
1540Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg) 
1541{
1542  Int k, i, j, jj;
1543  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1544
1545  for( k = 0; k < 64; k += 8 )
1546  {
1547    diff[k+0] = piOrg[0] ;
1548    diff[k+1] = piOrg[1] ;
1549    diff[k+2] = piOrg[2] ;
1550    diff[k+3] = piOrg[3] ;
1551    diff[k+4] = piOrg[4] ;
1552    diff[k+5] = piOrg[5] ;
1553    diff[k+6] = piOrg[6] ;
1554    diff[k+7] = piOrg[7] ;
1555 
1556    piOrg += iStrideOrg;
1557  }
1558 
1559  //horizontal
1560  for (j=0; j < 8; j++)
1561  {
1562    jj = j << 3;
1563    m2[j][0] = diff[jj  ] + diff[jj+4];
1564    m2[j][1] = diff[jj+1] + diff[jj+5];
1565    m2[j][2] = diff[jj+2] + diff[jj+6];
1566    m2[j][3] = diff[jj+3] + diff[jj+7];
1567    m2[j][4] = diff[jj  ] - diff[jj+4];
1568    m2[j][5] = diff[jj+1] - diff[jj+5];
1569    m2[j][6] = diff[jj+2] - diff[jj+6];
1570    m2[j][7] = diff[jj+3] - diff[jj+7];
1571   
1572    m1[j][0] = m2[j][0] + m2[j][2];
1573    m1[j][1] = m2[j][1] + m2[j][3];
1574    m1[j][2] = m2[j][0] - m2[j][2];
1575    m1[j][3] = m2[j][1] - m2[j][3];
1576    m1[j][4] = m2[j][4] + m2[j][6];
1577    m1[j][5] = m2[j][5] + m2[j][7];
1578    m1[j][6] = m2[j][4] - m2[j][6];
1579    m1[j][7] = m2[j][5] - m2[j][7];
1580   
1581    m2[j][0] = m1[j][0] + m1[j][1];
1582    m2[j][1] = m1[j][0] - m1[j][1];
1583    m2[j][2] = m1[j][2] + m1[j][3];
1584    m2[j][3] = m1[j][2] - m1[j][3];
1585    m2[j][4] = m1[j][4] + m1[j][5];
1586    m2[j][5] = m1[j][4] - m1[j][5];
1587    m2[j][6] = m1[j][6] + m1[j][7];
1588    m2[j][7] = m1[j][6] - m1[j][7];
1589  }
1590 
1591  //vertical
1592  for (i=0; i < 8; i++)
1593  {
1594    m3[0][i] = m2[0][i] + m2[4][i];
1595    m3[1][i] = m2[1][i] + m2[5][i];
1596    m3[2][i] = m2[2][i] + m2[6][i];
1597    m3[3][i] = m2[3][i] + m2[7][i];
1598    m3[4][i] = m2[0][i] - m2[4][i];
1599    m3[5][i] = m2[1][i] - m2[5][i];
1600    m3[6][i] = m2[2][i] - m2[6][i];
1601    m3[7][i] = m2[3][i] - m2[7][i];
1602   
1603    m1[0][i] = m3[0][i] + m3[2][i];
1604    m1[1][i] = m3[1][i] + m3[3][i];
1605    m1[2][i] = m3[0][i] - m3[2][i];
1606    m1[3][i] = m3[1][i] - m3[3][i];
1607    m1[4][i] = m3[4][i] + m3[6][i];
1608    m1[5][i] = m3[5][i] + m3[7][i];
1609    m1[6][i] = m3[4][i] - m3[6][i];
1610    m1[7][i] = m3[5][i] - m3[7][i];
1611   
1612    m2[0][i] = m1[0][i] + m1[1][i];
1613    m2[1][i] = m1[0][i] - m1[1][i];
1614    m2[2][i] = m1[2][i] + m1[3][i];
1615    m2[3][i] = m1[2][i] - m1[3][i];
1616    m2[4][i] = m1[4][i] + m1[5][i];
1617    m2[5][i] = m1[4][i] - m1[5][i];
1618    m2[6][i] = m1[6][i] + m1[7][i];
1619    m2[7][i] = m1[6][i] - m1[7][i];
1620  }
1621 
1622  for (i = 0; i < 8; i++)
1623  {
1624    for (j = 0; j < 8; j++)
1625    {
1626      iSumHad += abs(m2[i][j]);
1627    }
1628  }
1629  iSumHad -= abs(m2[0][0]);
1630  iSumHad =(iSumHad+2)>>2;
1631  return(iSumHad);
1632}
1633
1634Int  TEncCu::updateLCUDataISlice(TComDataCU* pcCU, Int LCUIdx, Int width, Int height)
1635{
1636  Int  xBl, yBl; 
1637  const Int iBlkSize = 8;
1638
1639  Pel* pOrgInit   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
1640  Int  iStrideOrig = pcCU->getPic()->getPicYuvOrg()->getStride();
1641  Pel  *pOrg;
1642
1643  Int iSumHad = 0;
1644  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1645  {
1646    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1647    {
1648      pOrg = pOrgInit + iStrideOrig*yBl + xBl; 
1649      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1650    }
1651  }
1652  return(iSumHad);
1653}
1654#endif
1655
1656/** check RD costs for a CU block encoded with merge
1657 * \param rpcBestCU
1658 * \param rpcTempCU
1659 * \returns Void
1660 */
1661Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1662{
1663  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1664#if H_3D_IV_MERGE
1665  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1666  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1667#else
1668  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1669  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1670#endif
1671  Int numValidMergeCand = 0;
1672
1673  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1674  {
1675    uhInterDirNeighbours[ui] = 0;
1676  }
1677  UChar uhDepth = rpcTempCU->getDepth( 0 );
1678#if H_3D_IC
1679  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1680#endif
1681#if H_3D_VSO // M1  //nececcary here?
1682  if( m_pcRdCost->getUseRenModel() )
1683  {
1684    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1685    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1686    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1687    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1688    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1689  }
1690#endif
1691
1692  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1693  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1694
1695#if H_3D_VSP
1696  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1697  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1698  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, numValidMergeCand );
1699#else
1700  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1701#endif
1702
1703#if H_3D_IV_MERGE
1704  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1705#else
1706  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1707#endif
1708for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1709  {
1710    mergeCandBuffer[ui] = 0;
1711  }
1712
1713  Bool bestIsSkip = false;
1714
1715  UInt iteration;
1716  if ( rpcTempCU->isLosslessCoded(0))
1717  {
1718    iteration = 1;
1719  }
1720  else 
1721  {
1722    iteration = 2;
1723  }
1724
1725#if H_3D_ARP
1726  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1727  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
1728  {
1729    nARPWMax = 0;
1730  }
1731  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1732  {
1733    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS*sizeof(Int) );
1734#endif
1735  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1736  {
1737    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1738    {     
1739#if H_3D_IC
1740        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1741        {
1742          if( bICFlag && uiMergeCand == 0 ) 
1743          {
1744            continue;
1745          }
1746        }
1747#endif
1748        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1749        {
1750        if( !(bestIsSkip && uiNoResidual == 0) )
1751        {
1752          // set MC parameters
1753          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1754          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1755          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1756#if H_3D_IC
1757          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1758#endif
1759#if H_3D_ARP
1760          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1761#endif
1762          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1763          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1764#if H_3D_VSP
1765          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1766#endif
1767          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1768          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1769          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1770
1771#if H_3D_ARP
1772          if( nARPW )
1773          {
1774            Bool bSignalflag[2] = { true, true };
1775            for( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1776            {
1777              Int iRefIdx = cMvFieldNeighbours[uiRefListIdx + 2*uiMergeCand].getRefIdx();
1778              RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1779              if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1780              {
1781                bSignalflag[uiRefListIdx] = false;
1782              }
1783            }
1784            if( !bSignalflag[0] && !bSignalflag[1] )
1785            {
1786              rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1787            }
1788          }
1789#endif
1790       // do MC
1791       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1792       // estimate residual and encode everything
1793#if H_3D_VSO //M2
1794       if( m_pcRdCost->getUseRenModel() )
1795       { //Reset
1796         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1797         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1798         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1799         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1800         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1801       }
1802#endif
1803       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1804         m_ppcOrigYuv    [uhDepth],
1805         m_ppcPredYuvTemp[uhDepth],
1806         m_ppcResiYuvTemp[uhDepth],
1807         m_ppcResiYuvBest[uhDepth],
1808         m_ppcRecoYuvTemp[uhDepth],
1809         (uiNoResidual? true:false));
1810
1811
1812          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
1813         {
1814            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1815           mergeCandBuffer[uiMergeCand] = 1;
1816         }
1817
1818          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1819          Int orgQP = rpcTempCU->getQP( 0 );
1820          xCheckDQP( rpcTempCU );
1821          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1822          rpcTempCU->initEstData( uhDepth, orgQP );
1823
1824      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1825      {
1826        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1827      }
1828    }
1829   }
1830  }
1831
1832  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1833  {
1834    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1835    {
1836      if( rpcBestCU->getMergeFlag( 0 ))
1837      {
1838        *earlyDetectionSkipMode = true;
1839      }
1840      else
1841      {
1842        Int absoulte_MV=0;
1843        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1844        {
1845          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1846          {
1847            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1848            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1849            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1850            absoulte_MV+=iHor+iVer;
1851          }
1852        }
1853
1854        if(absoulte_MV == 0)
1855        {
1856          *earlyDetectionSkipMode = true;
1857        }
1858      }
1859    }
1860  }
1861 }
1862#if H_3D_ARP
1863 }
1864#endif
1865}
1866
1867
1868#if AMP_MRG
1869#if  MTK_FAST_TEXTURE_ENCODING_E0173
1870Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
1871#else
1872Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
1873#endif
1874#else
1875Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1876#endif
1877{
1878#if  MTK_FAST_TEXTURE_ENCODING_E0173
1879  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
1880  {
1881#endif
1882  UChar uhDepth = rpcTempCU->getDepth( 0 );
1883#if H_3D_ARP
1884  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
1885  Bool bFirstTime = true;
1886  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
1887
1888  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
1889  {
1890    nARPWMax = 0;
1891  }
1892
1893  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
1894  {
1895    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
1896    {
1897      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
1898    }
1899#endif
1900#if H_3D_VSO // M3
1901  if( m_pcRdCost->getUseRenModel() )
1902  {
1903    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1904    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1905    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1906    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1907    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1908  }
1909#endif
1910
1911  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1912 
1913  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1914
1915  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1916  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1917  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
1918 
1919#if H_3D_ARP
1920  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1921#endif
1922
1923#if H_3D_ARP
1924  if( bFirstTime == false && nARPWMax )
1925  {
1926    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
1927    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1928
1929    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
1930
1931    if(rpcTempCU->getPartitionSize(0)==SIZE_2Nx2N)
1932    {
1933      Bool bSignalflag[2] = { true, true };
1934      for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1935      {
1936        RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1937        Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1938        if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1939        {
1940          bSignalflag[uiRefListIdx] = false;
1941        }
1942      }
1943      if( !bSignalflag[0] && !bSignalflag[1] )
1944      {
1945        rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1946      }
1947    }
1948  }
1949  else
1950  {
1951    bFirstTime = false;
1952#endif
1953#if AMP_MRG
1954  rpcTempCU->setMergeAMP (true);
1955#if  MTK_FAST_TEXTURE_ENCODING_E0173
1956  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
1957#else
1958  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
1959#endif
1960#else 
1961  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1962#endif
1963#if H_3D_ARP
1964   if( nARPWMax )
1965   {
1966     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
1967
1968     Bool bSignalflag[2] = { true, true };
1969     for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1970     {
1971       RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1972       Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1973       if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1974       {
1975         bSignalflag[uiRefListIdx] = false;
1976       }
1977     }
1978     if( !bSignalflag[0] && !bSignalflag[1])
1979     {
1980       rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1981     }
1982   }
1983  }
1984#endif
1985
1986#if AMP_MRG
1987  if ( !rpcTempCU->getMergeAMP() )
1988  {
1989#if H_3D_ARP
1990    if( nARPWMax )
1991    {
1992      continue;
1993    }
1994    else
1995#endif
1996    return;
1997  }
1998#endif
1999
2000#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
2001  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2002  {
2003    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2004      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2005      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2006    m_temporalSAD = (Int)SAD;
2007  }
2008#endif
2009
2010  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2011
2012
2013#if H_3D_VSO // M4
2014  if( m_pcRdCost->getUseLambdaScaleVSO() )
2015    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2016  else
2017#endif
2018  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2019
2020  xCheckDQP( rpcTempCU );
2021  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2022#if H_3D_ARP
2023  }
2024#endif
2025#if  MTK_FAST_TEXTURE_ENCODING_E0173
2026  }
2027#endif
2028}
2029
2030Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2031{
2032  UInt uiDepth = rpcTempCU->getDepth( 0 );
2033 
2034#if H_3D_VSO // M5
2035  if( m_pcRdCost->getUseRenModel() )
2036  {
2037    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2038    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2039    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2040    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2041    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2042  }
2043#endif
2044
2045  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2046
2047  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2048  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2049  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2050 
2051  Bool bSeparateLumaChroma = true; // choose estimation mode
2052  UInt uiPreCalcDistC      = 0;
2053  if( !bSeparateLumaChroma )
2054  {
2055    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
2056  }
2057  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
2058
2059  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2060 
2061#if H_3D_DIM_SDC
2062  if( !rpcTempCU->getSDCFlag( 0 ) )
2063#endif
2064  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
2065 
2066  m_pcEntropyCoder->resetBits();
2067  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2068  {
2069    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2070  }
2071  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2072  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2073  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2074  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
2075  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2076
2077  // Encode Coefficients
2078  Bool bCodeDQP = getdQPFlag();
2079  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2080  setdQPFlag( bCodeDQP );
2081 
2082  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2083 
2084  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2085  if(m_pcEncCfg->getUseSBACRD())
2086  {
2087    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2088  }
2089
2090#if H_3D_VSO // M6
2091  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2092    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2093  else
2094#endif
2095  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2096 
2097  xCheckDQP( rpcTempCU );
2098  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2099}
2100
2101/** Check R-D costs for a CU with PCM mode.
2102 * \param rpcBestCU pointer to best mode CU data structure
2103 * \param rpcTempCU pointer to testing mode CU data structure
2104 * \returns Void
2105 *
2106 * \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.
2107 */
2108Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2109{
2110  UInt uiDepth = rpcTempCU->getDepth( 0 );
2111
2112  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2113
2114  rpcTempCU->setIPCMFlag(0, true);
2115  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2116  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2117  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2118  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2119  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2120
2121  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2122
2123  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2124
2125  m_pcEntropyCoder->resetBits();
2126  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2127  {
2128    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2129  }
2130  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2131  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2132  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2133  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2134
2135  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2136
2137  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2138  if(m_pcEncCfg->getUseSBACRD())
2139  {
2140    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2141  }
2142#if H_3D_VSO // M44
2143  if ( m_pcRdCost->getUseVSO() )
2144    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2145  else
2146#endif
2147  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2148
2149  xCheckDQP( rpcTempCU );
2150  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2151}
2152
2153/** check whether current try is the best with identifying the depth of current try
2154 * \param rpcBestCU
2155 * \param rpcTempCU
2156 * \returns Void
2157 */
2158Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2159{
2160  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2161  {
2162    TComYuv* pcYuv;
2163    // Change Information data
2164    TComDataCU* pcCU = rpcBestCU;
2165    rpcBestCU = rpcTempCU;
2166    rpcTempCU = pcCU;
2167
2168    // Change Prediction data
2169    pcYuv = m_ppcPredYuvBest[uiDepth];
2170    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2171    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2172
2173    // Change Reconstruction data
2174    pcYuv = m_ppcRecoYuvBest[uiDepth];
2175    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2176    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2177
2178    pcYuv = NULL;
2179    pcCU  = NULL;
2180
2181    if( m_bUseSBACRD )  // store temp best CI for next CU coding
2182      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2183  }
2184}
2185
2186Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2187{
2188  UInt uiDepth = pcCU->getDepth( 0 );
2189
2190  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2191  {
2192    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2193    {
2194#if !RDO_WITHOUT_DQP_BITS
2195      m_pcEntropyCoder->resetBits();
2196      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2197      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2198      if(m_pcEncCfg->getUseSBACRD())
2199      {
2200        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2201      }
2202#if H_3D_VSO // M45
2203      if ( m_pcRdCost->getUseVSO() )     
2204        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2205      else
2206#endif
2207      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2208#endif
2209    }
2210    else
2211    {
2212      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2213    }
2214  }
2215}
2216
2217Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2218{
2219  pDst->iN = pSrc->iN;
2220  for (Int i = 0; i < pSrc->iN; i++)
2221  {
2222    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2223  }
2224}
2225Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2226{
2227  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2228  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2229  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2230  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2231    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2232  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2233    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2234  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2235  {
2236    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2237    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2238    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2239    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2240    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2241    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2242    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2243  }
2244  else
2245  {
2246    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2247
2248    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2249    {
2250      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2251      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2252
2253      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
2254        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
2255      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2256      {
2257        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2258      }
2259    }
2260  }
2261}
2262
2263Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2264{
2265  UInt uiCurrDepth = uiNextDepth - 1;
2266  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2267}
2268
2269/** Function for filling the PCM buffer of a CU using its original sample array
2270 * \param pcCU pointer to current CU
2271 * \param pcOrgYuv pointer to original sample array
2272 * \returns Void
2273 */
2274Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2275{
2276
2277  UInt   width        = pCU->getWidth(0);
2278  UInt   height       = pCU->getHeight(0);
2279
2280  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2281  Pel*   pDstY = pCU->getPCMSampleY();
2282  UInt   srcStride = pOrgYuv->getStride();
2283
2284  for(Int y = 0; y < height; y++ )
2285  {
2286    for(Int x = 0; x < width; x++ )
2287    {
2288      pDstY[x] = pSrcY[x];
2289    }
2290    pDstY += width;
2291    pSrcY += srcStride;
2292  }
2293
2294  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2295  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2296
2297  Pel* pDstCb       = pCU->getPCMSampleCb();
2298  Pel* pDstCr       = pCU->getPCMSampleCr();;
2299
2300  UInt srcStrideC = pOrgYuv->getCStride();
2301  UInt heightC   = height >> 1;
2302  UInt widthC    = width  >> 1;
2303
2304  for(Int y = 0; y < heightC; y++ )
2305  {
2306    for(Int x = 0; x < widthC; x++ )
2307    {
2308      pDstCb[x] = pSrcCb[x];
2309      pDstCr[x] = pSrcCr[x];
2310    }
2311    pDstCb += widthC;
2312    pDstCr += widthC;
2313    pSrcCb += srcStrideC;
2314    pSrcCr += srcStrideC;
2315  }
2316}
2317
2318#if ADAPTIVE_QP_SELECTION
2319/** Collect ARL statistics from one block
2320  */
2321Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2322{
2323  for( Int n = 0; n < NumCoeffInCU; n++ )
2324  {
2325    Int u = abs( rpcCoeff[ n ] );
2326    Int absc = rpcArlCoeff[ n ];
2327
2328    if( u != 0 )
2329    {
2330      if( u < LEVEL_RANGE )
2331      {
2332        cSum[ u ] += ( Double )absc;
2333        numSamples[ u ]++;
2334      }
2335      else 
2336      {
2337        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2338        numSamples[ LEVEL_RANGE ]++;
2339      }
2340    }
2341  }
2342
2343  return 0;
2344}
2345
2346/** Collect ARL statistics from one LCU
2347 * \param pcCU
2348 */
2349Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2350{
2351  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2352  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2353
2354  TCoeff* pCoeffY = rpcCU->getCoeffY();
2355  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2356
2357  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2358  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2359
2360  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2361  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2362
2363  // Collect stats to cSum[][] and numSamples[][]
2364  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2365  {
2366    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2367
2368    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2369    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2370    {
2371      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2372    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2373   
2374    pCoeffY  += uiMinNumCoeffInCU;
2375    pArlCoeffY  += uiMinNumCoeffInCU;
2376  }
2377
2378  for(Int u=1; u<LEVEL_RANGE;u++)
2379  {
2380    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2381    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2382  }
2383  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2384  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2385}
2386#endif
2387//! \}
Note: See TracBrowser for help on using the repository browser.