source: 3DVCSoftware/branches/HTM-14.0-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 1186

Last change on this file since 1186 was 1186, checked in by tech, 10 years ago

Clean up. Part 2.

  • Property svn:eol-style set to native
File size: 105.8 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-2015, 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#if H_3D_DBBP
79  m_ppcOrigYuvDBBP = new TComYuv*[m_uhTotalDepth-1];
80#endif
81 
82  UInt uiNumPartitions;
83  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
84  {
85    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
86    UInt uiWidth  = uiMaxWidth  >> i;
87    UInt uiHeight = uiMaxHeight >> i;
88   
89    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
90    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
91   
92#if H_3D_ARP
93    m_ppcWeightedTempCU[i] = new TComDataCU; m_ppcWeightedTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
94#endif 
95
96    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight);
97    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight);
98    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight);
99   
100    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight);
101    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight);
102    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight);
103   
104    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
105#if H_3D_DBBP
106    m_ppcOrigYuvDBBP[i] = new TComYuv; m_ppcOrigYuvDBBP[i]->create(uiWidth, uiHeight);
107#endif
108  }
109 
110  m_bEncodeDQP = false;
111#if KWU_RC_MADPRED_E0227
112  m_LCUPredictionSAD = 0;
113  m_addSADDepth      = 0;
114  m_temporalSAD      = 0;
115  m_spatialSAD       = 0;
116#endif
117
118  // initialize partition order.
119  UInt* piTmp = &g_auiZscanToRaster[0];
120  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
121  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
122 
123  // initialize conversion matrix from partition index to pel
124  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
125}
126
127Void TEncCu::destroy()
128{
129  Int i;
130 
131  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
132  {
133    if(m_ppcBestCU[i])
134    {
135      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
136    }
137    if(m_ppcTempCU[i])
138    {
139      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
140    }
141#if H_3D_ARP
142    if(m_ppcWeightedTempCU[i])
143    {
144      m_ppcWeightedTempCU[i]->destroy(); delete m_ppcWeightedTempCU[i]; m_ppcWeightedTempCU[i] = NULL;
145    }
146#endif
147    if(m_ppcPredYuvBest[i])
148    {
149      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
150    }
151    if(m_ppcResiYuvBest[i])
152    {
153      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
154    }
155    if(m_ppcRecoYuvBest[i])
156    {
157      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
158    }
159    if(m_ppcPredYuvTemp[i])
160    {
161      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
162    }
163    if(m_ppcResiYuvTemp[i])
164    {
165      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
166    }
167    if(m_ppcRecoYuvTemp[i])
168    {
169      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
170    }
171    if(m_ppcOrigYuv[i])
172    {
173      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
174    }
175#if H_3D_DBBP
176    if(m_ppcOrigYuvDBBP[i])
177    {
178      m_ppcOrigYuvDBBP[i]->destroy(); delete m_ppcOrigYuvDBBP[i]; m_ppcOrigYuvDBBP[i] = NULL;
179    }
180#endif
181  }
182  if(m_ppcBestCU)
183  {
184    delete [] m_ppcBestCU;
185    m_ppcBestCU = NULL;
186  }
187  if(m_ppcTempCU)
188  {
189    delete [] m_ppcTempCU;
190    m_ppcTempCU = NULL;
191  }
192
193#if H_3D_ARP
194  if(m_ppcWeightedTempCU)
195  {
196    delete [] m_ppcWeightedTempCU; 
197    m_ppcWeightedTempCU = NULL; 
198  }
199#endif
200  if(m_ppcPredYuvBest)
201  {
202    delete [] m_ppcPredYuvBest;
203    m_ppcPredYuvBest = NULL;
204  }
205  if(m_ppcResiYuvBest)
206  {
207    delete [] m_ppcResiYuvBest;
208    m_ppcResiYuvBest = NULL;
209  }
210  if(m_ppcRecoYuvBest)
211  {
212    delete [] m_ppcRecoYuvBest;
213    m_ppcRecoYuvBest = NULL;
214  }
215  if(m_ppcPredYuvTemp)
216  {
217    delete [] m_ppcPredYuvTemp;
218    m_ppcPredYuvTemp = NULL;
219  }
220  if(m_ppcResiYuvTemp)
221  {
222    delete [] m_ppcResiYuvTemp;
223    m_ppcResiYuvTemp = NULL;
224  }
225  if(m_ppcRecoYuvTemp)
226  {
227    delete [] m_ppcRecoYuvTemp;
228    m_ppcRecoYuvTemp = NULL;
229  }
230  if(m_ppcOrigYuv)
231  {
232    delete [] m_ppcOrigYuv;
233    m_ppcOrigYuv = NULL;
234  }
235#if H_3D_DBBP
236  if(m_ppcOrigYuvDBBP)
237  {
238    delete [] m_ppcOrigYuvDBBP;
239    m_ppcOrigYuvDBBP = NULL;
240  }
241#endif
242}
243
244/** \param    pcEncTop      pointer of encoder class
245 */
246Void TEncCu::init( TEncTop* pcEncTop )
247{
248  m_pcEncCfg           = pcEncTop;
249  m_pcPredSearch       = pcEncTop->getPredSearch();
250  m_pcTrQuant          = pcEncTop->getTrQuant();
251  m_pcBitCounter       = pcEncTop->getBitCounter();
252  m_pcRdCost           = pcEncTop->getRdCost();
253 
254  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
255  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
256  m_pcSbacCoder       = pcEncTop->getSbacCoder();
257  m_pcBinCABAC         = pcEncTop->getBinCABAC();
258 
259  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
260  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
261 
262  m_pcRateCtrl        = pcEncTop->getRateCtrl();
263}
264
265// ====================================================================================================================
266// Public member functions
267// ====================================================================================================================
268
269/** \param  rpcCU pointer of CU data class
270 */
271Void TEncCu::compressCU( TComDataCU*& rpcCU )
272{
273  // initialize CU data
274  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
275  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
276
277#if H_3D_DISABLE_CHROMA
278  m_ppcWeightedTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
279#endif
280
281#if KWU_RC_MADPRED_E0227
282  m_LCUPredictionSAD = 0;
283  m_addSADDepth      = 0;
284  m_temporalSAD      = 0;
285  m_spatialSAD       = 0;
286#endif
287
288  // analysis of CU
289  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
290
291#if ADAPTIVE_QP_SELECTION
292  if( m_pcEncCfg->getUseAdaptQpSelect() )
293  {
294    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
295    {
296      xLcuCollectARLStats( rpcCU);
297    }
298  }
299#endif
300}
301/** \param  pcCU  pointer of CU data class
302 */
303Void TEncCu::encodeCU ( TComDataCU* pcCU )
304{
305  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
306  {
307    setdQPFlag(true);
308  }
309
310  // Encode CU data
311  xEncodeCU( pcCU, 0, 0 );
312}
313
314// ====================================================================================================================
315// Protected member functions
316// ====================================================================================================================
317/** Derive small set of test modes for AMP encoder speed-up
318 *\param   rpcBestCU
319 *\param   eParentPartSize
320 *\param   bTestAMP_Hor
321 *\param   bTestAMP_Ver
322 *\param   bTestMergeAMP_Hor
323 *\param   bTestMergeAMP_Ver
324 *\returns Void
325*/
326#if AMP_ENC_SPEEDUP
327#if AMP_MRG
328Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
329#else
330Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
331#endif
332{
333  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
334  {
335    bTestAMP_Hor = true;
336  }
337  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
338  {
339    bTestAMP_Ver = true;
340  }
341  else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false )
342  {
343    bTestAMP_Hor = true;         
344    bTestAMP_Ver = true;         
345  }
346
347#if AMP_MRG
348  //! Utilizing the partition size of parent PU   
349  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
350  { 
351    bTestMergeAMP_Hor = true;
352    bTestMergeAMP_Ver = true;
353  }
354
355  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
356  {
357    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
358    {
359      bTestMergeAMP_Hor = true;
360    }
361    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
362    {
363      bTestMergeAMP_Ver = true;
364    }
365  }
366
367  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
368  {
369    bTestMergeAMP_Hor = true;         
370    bTestMergeAMP_Ver = true;         
371  }
372
373  if ( rpcBestCU->getWidth(0) == 64 )
374  { 
375    bTestAMP_Hor = false;
376    bTestAMP_Ver = false;
377  }   
378#else
379  //! Utilizing the partition size of parent PU       
380  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
381  { 
382    bTestAMP_Hor = true;
383    bTestAMP_Ver = true;
384  }
385
386  if ( eParentPartSize == SIZE_2Nx2N )
387  { 
388    bTestAMP_Hor = false;
389    bTestAMP_Ver = false;
390  }     
391#endif
392}
393#endif
394
395// ====================================================================================================================
396// Protected member functions
397// ====================================================================================================================
398/** Compress a CU block recursively with enabling sub-LCU-level delta QP
399 *\param   rpcBestCU
400 *\param   rpcTempCU
401 *\param   uiDepth
402 *\returns Void
403 *
404 *- for loop of QP value to compress the current CU with all possible QP
405*/
406#if AMP_ENC_SPEEDUP
407Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize )
408#else
409Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
410#endif
411{
412  TComPic* pcPic = rpcBestCU->getPic();
413
414#if H_3D_QTLPC
415  Bool  bLimQtPredFalg    = pcPic->getSlice(0)->getQtPredFlag(); 
416  TComPic *pcTexture      = rpcBestCU->getSlice()->getTexturePic();
417
418  Bool  depthMapDetect    = (pcTexture != NULL);
419  Bool  bIntraSliceDetect = (rpcBestCU->getSlice()->getSliceType() == I_SLICE);
420
421  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);
422
423  Bool bTry2NxN           = true;
424  Bool bTryNx2N           = true;
425#endif
426  // get Original YUV data from picture
427  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
428
429#if H_3D_QTLPC 
430  Bool    bTrySplit     = true;
431  Bool    bTrySplitDQP  = true;
432#endif
433
434  // variable for Early CU determination
435  Bool    bSubBranch = true;
436
437  // variable for Cbf fast mode PU decision
438  Bool    doNotBlockPu = true;
439  Bool earlyDetectionSkipMode = false;
440
441#if H_3D_VSP
442  DisInfo DvInfo; 
443  DvInfo.m_acNBDV.setZero();
444  DvInfo.m_aVIdxCan = 0;
445#if H_3D_NBDV_REF
446  DvInfo.m_acDoNBDV.setZero();
447#endif
448#endif
449  Bool bBoundary = false;
450  UInt uiLPelX   = rpcBestCU->getCUPelX();
451  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
452  UInt uiTPelY   = rpcBestCU->getCUPelY();
453  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
454
455#if H_MV_ENC_DEC_TRAC
456#if ENC_DEC_TRACE
457    stopAtPos  ( rpcBestCU->getSlice()->getPOC(), 
458                 rpcBestCU->getSlice()->getLayerId(), 
459                 rpcBestCU->getCUPelX(),
460                 rpcBestCU->getCUPelY(),
461                 rpcBestCU->getWidth(0), 
462                 rpcBestCU->getHeight(0) );
463#endif
464#endif
465
466  Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
467  Int iMinQP;
468  Int iMaxQP;
469  Bool isAddLowestQP = false;
470
471  if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
472  {
473    Int idQP = m_pcEncCfg->getMaxDeltaQP();
474    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
475    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
476  }
477  else
478  {
479    iMinQP = rpcTempCU->getQP(0);
480    iMaxQP = rpcTempCU->getQP(0);
481  }
482
483  if ( m_pcEncCfg->getUseRateCtrl() )
484  {
485    iMinQP = m_pcRateCtrl->getRCQP();
486    iMaxQP = m_pcRateCtrl->getRCQP();
487  }
488  // transquant-bypass (TQB) processing loop variable initialisation ---
489
490  const Int lowestQP = iMinQP; // For TQB, use this QP which is the lowest non TQB QP tested (rather than QP'=0) - that way delta QPs are smaller, and TQB can be tested at all CU levels.
491
492  if ( (rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) )
493  {
494    isAddLowestQP = true; // mark that the first iteration is to cost TQB mode.
495    iMinQP = iMinQP - 1;  // increase loop variable range by 1, to allow testing of TQB mode along with other QPs
496    if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
497    {
498      iMaxQP = iMinQP;
499    }
500  }
501
502#if H_3D_IC
503  Bool bICEnabled = rpcTempCU->getSlice()->getViewIndex() && ( rpcTempCU->getSlice()->getSliceType() == P_SLICE || rpcTempCU->getSlice()->getSliceType() == B_SLICE ) && !rpcTempCU->getSlice()->getIsDepth();
504  bICEnabled = bICEnabled && rpcTempCU->getSlice()->getApplyIC();
505#endif
506  // If slice start or slice end is within this cu...
507  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
508  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart();
509  Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart());
510  Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() );
511  // We need to split, so don't try these modes.
512  if(!bSliceEnd && !bSliceStart && bInsidePicture )
513  {
514#if  H_3D_FAST_TEXTURE_ENCODING
515    Bool bIVFMerge = false;
516    Int  iIVFMaxD = 0;
517    Bool bFMD = false;
518#endif
519    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
520    {
521      const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP);
522
523      if (bIsLosslessMode)
524      {
525        iQP = lowestQP;
526      }
527
528#if H_3D_QTLPC
529      bTrySplit    = true;
530#endif
531
532      rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
533#if H_3D_QTLPC
534      //logic for setting bTrySplit using the partition information that is stored of the texture colocated CU
535#if H_3D_FCO
536      if(depthMapDetect && !bIntraSliceDetect && !rapPic && ( m_pcEncCfg->getUseQTL() || bLimQtPredFalg ) && pcTexture->getReconMark())
537#else
538      if(depthMapDetect && !bIntraSliceDetect && !rapPic && ( m_pcEncCfg->getUseQTL() || bLimQtPredFalg ))
539#endif
540      {
541        TComDataCU* pcTextureCU = pcTexture->getCU( rpcBestCU->getAddr() ); //Corresponding texture LCU
542        UInt uiCUIdx            = rpcBestCU->getZorderIdxInCU();
543        assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth); //Depth cannot be more partitionned than the texture.
544        if (pcTextureCU->getDepth(uiCUIdx) > uiDepth || pcTextureCU->getPartitionSize(uiCUIdx) == SIZE_NxN) //Texture was split.
545        {
546          bTrySplit = true;
547          bTryNx2N  = true;
548          bTry2NxN  = true;
549        }
550        else
551        {
552          bTrySplit = false;
553          bTryNx2N  = false;
554          bTry2NxN  = false;
555          if( pcTextureCU->getDepth(uiCUIdx) == uiDepth && pcTextureCU->getPartitionSize(uiCUIdx) != SIZE_2Nx2N)
556          {
557            if(pcTextureCU->getPartitionSize(uiCUIdx)==SIZE_2NxN || pcTextureCU->getPartitionSize(uiCUIdx)==SIZE_2NxnU|| pcTextureCU->getPartitionSize(uiCUIdx)==SIZE_2NxnD)
558              bTry2NxN  = true;
559            else
560              bTryNx2N  = true;
561          }
562        }
563      }
564#endif
565
566#if H_3D_NBDV
567      if( rpcTempCU->getSlice()->getSliceType() != I_SLICE )
568      {
569#if H_3D_ARP && H_3D_IV_MERGE
570        if( rpcTempCU->getSlice()->getIvResPredFlag() || rpcTempCU->getSlice()->getIvMvPredFlag() )
571#else
572#if H_3D_ARP
573        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) )
574#else
575#if H_3D_IV_MERGE
576        if( rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
577#else
578        if (0)
579#endif
580#endif
581#endif
582        {
583          PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
584          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );     
585#if H_3D_IV_MERGE
586          if (rpcTempCU->getSlice()->getIsDepth() )
587          {
588            rpcTempCU->getDispforDepth(0, 0, &DvInfo);
589          }
590          else
591          {
592#endif
593#if H_3D_NBDV_REF
594          if( rpcTempCU->getSlice()->getDepthRefinementFlag() )
595            rpcTempCU->getDisMvpCandNBDV(&DvInfo, true);
596          else
597#endif
598            rpcTempCU->getDisMvpCandNBDV(&DvInfo);
599
600#if H_3D_IV_MERGE
601          }
602#endif
603          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
604          rpcBestCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
605          rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
606        }
607      }
608#if  H_3D_FAST_TEXTURE_ENCODING
609      if(rpcTempCU->getSlice()->getViewIndex() && !rpcTempCU->getSlice()->getIsDepth() && rpcTempCU->getSlice()->getDefaultRefViewIdxAvailableFlag() )
610      {
611        PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
612        rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); 
613        rpcTempCU->getIVNStatus( 0, &DvInfo,  bIVFMerge, iIVFMaxD);
614        rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
615      }
616#endif
617#endif
618      // do inter modes, SKIP and 2Nx2N
619      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
620      {
621#if H_3D_IC
622        for( UInt uiICId = 0; uiICId < ( bICEnabled ? 2 : 1 ); uiICId++ )
623        {
624          Bool bICFlag = uiICId ? true : false;
625#endif
626        // 2Nx2N
627        if(m_pcEncCfg->getUseEarlySkipDetection())
628        {
629#if H_3D_IC
630          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
631#endif
632#if  H_3D_FAST_TEXTURE_ENCODING
633          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode  );//by Competition for inter_2Nx2N
634#else
635          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
636          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );//by Competition for inter_2Nx2N
637#endif
638#if H_3D_VSP
639          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
640#endif
641        }
642        // SKIP
643#if H_3D_IC
644        rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
645#endif
646        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
647#if  H_3D_FAST_TEXTURE_ENCODING
648        bFMD = bIVFMerge && rpcBestCU->isSkipped(0);
649#endif
650        rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
651#if H_3D_VSP
652        rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
653#endif
654
655        if(!m_pcEncCfg->getUseEarlySkipDetection())
656        {
657          // 2Nx2N, NxN
658#if H_3D_IC
659            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
660#endif
661#if  H_3D_FAST_TEXTURE_ENCODING
662            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
663#else
664          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
665          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
666#endif
667#if H_3D_VSP
668            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
669#endif
670         
671#if H_3D_DBBP
672          if( rpcTempCU->getSlice()->getDepthBasedBlkPartFlag() && rpcTempCU->getSlice()->getDefaultRefViewIdxAvailableFlag() )
673          {
674            xCheckRDCostInterDBBP( rpcBestCU, rpcTempCU, false );
675            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode  );
676#if H_3D_VSP
677            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
678#endif
679          }
680#endif
681         
682            if(m_pcEncCfg->getUseCbfFastMode())
683            {
684              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
685            }
686        }
687#if H_3D_IC
688        }
689#endif
690      }
691
692#if H_3D_QTLPC     
693      if(depthMapDetect && !bIntraSliceDetect && !rapPic && ( m_pcEncCfg->getUseQTL() || bLimQtPredFalg ))
694      {
695        bTrySplitDQP = bTrySplit;
696      }
697#endif
698      if ( bIsLosslessMode )
699      {
700        iQP = iMinQP;
701      }
702    }
703
704#if KWU_RC_MADPRED_E0227
705    if ( uiDepth <= m_addSADDepth )
706    {
707      m_LCUPredictionSAD += m_temporalSAD;
708      m_addSADDepth = uiDepth;
709    }
710#endif
711#if H_3D_DIM_ENC
712    if( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() )
713    {
714      earlyDetectionSkipMode = false;
715    }
716#endif
717#if H_3D
718    rpcTempCU->initEstData( uiDepth, iMinQP, isAddLowestQP  );
719    if( rpcBestCU->getSlice()->getDepthIntraSkipFlag() )
720    {
721      xCheckRDCostDIS( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
722      rpcTempCU->initEstData( uiDepth, iMinQP, isAddLowestQP  );
723    }
724#endif
725    if(!earlyDetectionSkipMode)
726    {
727      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
728      {
729        const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP);
730
731        if (bIsLosslessMode)
732        {
733          iQP = lowestQP;
734        }
735        rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
736
737        // do inter modes, NxN, 2NxN, and Nx2N
738        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
739        {
740          // 2Nx2N, NxN
741            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
742            {
743              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu
744#if H_3D_QTLPC
745                && bTrySplit
746#endif
747                )
748              {
749#if  H_3D_FAST_TEXTURE_ENCODING
750                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN, bFMD  );
751#else
752                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
753#endif
754                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
755#if H_3D_VSP
756                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
757#endif
758              }
759            }
760
761          // 2NxN, Nx2N
762          if(doNotBlockPu
763#if H_3D_QTLPC
764            && bTryNx2N
765#endif
766            )
767          {
768#if  H_3D_FAST_TEXTURE_ENCODING
769            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N, bFMD  );
770#else
771            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
772#endif
773            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
774#if H_3D_VSP
775            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
776#endif
777            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
778            {
779              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
780            }
781          }
782          if(doNotBlockPu
783#if H_3D_QTLPC
784            && bTry2NxN
785#endif
786            )
787          {
788#if  H_3D_FAST_TEXTURE_ENCODING
789            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN, bFMD  );
790#else
791            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
792#endif
793            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
794#if H_3D_VSP
795            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
796#endif
797            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
798            {
799              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
800            }
801          }
802
803#if 1
804          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
805          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
806          {
807#if AMP_ENC_SPEEDUP       
808            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
809
810#if AMP_MRG
811            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
812
813            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
814#else
815            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
816#endif
817
818            //! Do horizontal AMP
819            if ( bTestAMP_Hor )
820            {
821              if(doNotBlockPu
822#if H_3D_QTLPC
823                && bTry2NxN
824#endif
825                )
826              {
827#if  H_3D_FAST_TEXTURE_ENCODING
828                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD );
829#else
830                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
831#endif
832                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
833#if H_3D_VSP
834                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
835#endif
836                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
837                {
838                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
839                }
840              }
841              if(doNotBlockPu
842#if H_3D_QTLPC
843                && bTry2NxN
844#endif
845                )
846              {
847#if  H_3D_FAST_TEXTURE_ENCODING
848                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD );
849#else
850                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
851#endif
852                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
853#if H_3D_VSP
854                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
855#endif
856                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
857                {
858                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
859                }
860              }
861            }
862#if AMP_MRG
863            else if ( bTestMergeAMP_Hor ) 
864            {
865              if(doNotBlockPu
866#if H_3D_QTLPC
867                && bTry2NxN
868#endif
869                )
870              {
871#if  H_3D_FAST_TEXTURE_ENCODING
872                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD, true );
873#else
874                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
875#endif
876                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
877#if H_3D_VSP
878                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
879#endif
880                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
881                {
882                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
883                }
884              }
885              if(doNotBlockPu
886#if H_3D_QTLPC
887                && bTry2NxN
888#endif
889                )
890              {
891#if  H_3D_FAST_TEXTURE_ENCODING
892                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD, true );
893#else
894                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
895#endif
896                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
897#if H_3D_VSP
898                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
899#endif
900                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
901                {
902                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
903                }
904              }
905            }
906#endif
907
908            //! Do horizontal AMP
909            if ( bTestAMP_Ver )
910            {
911              if(doNotBlockPu
912#if H_3D_QTLPC
913                && bTryNx2N
914#endif
915                )
916              {
917#if  H_3D_FAST_TEXTURE_ENCODING
918                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD );
919#else
920                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
921#endif
922                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
923#if H_3D_VSP
924                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
925#endif
926                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
927                {
928                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
929                }
930              }
931              if(doNotBlockPu
932#if H_3D_QTLPC
933                && bTryNx2N
934#endif
935                )
936              {
937#if  H_3D_FAST_TEXTURE_ENCODING
938                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD );
939#else
940                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
941#endif
942                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
943#if H_3D_VSP
944                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
945#endif
946              }
947            }
948#if AMP_MRG
949            else if ( bTestMergeAMP_Ver )
950            {
951              if(doNotBlockPu
952#if H_3D_QTLPC
953                && bTryNx2N
954#endif
955                )
956              {
957#if  H_3D_FAST_TEXTURE_ENCODING
958                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD, true );
959#else
960                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
961#endif
962                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
963#if H_3D_VSP
964                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
965#endif
966                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
967                {
968                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
969                }
970              }
971              if(doNotBlockPu
972#if H_3D_QTLPC
973                && bTryNx2N
974#endif
975                )
976              {
977#if  H_3D_FAST_TEXTURE_ENCODING
978                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD, true );
979#else
980                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
981#endif
982                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
983#if H_3D_VSP
984                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
985#endif
986              }
987            }
988#endif
989
990#else
991#if H_3D_QTLPC
992            if (bTry2NxN)
993            {
994#endif
995              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
996              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
997#if H_3D_VSP
998              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
999#endif
1000              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
1001              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1002#if H_3D_VSP
1003              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
1004#endif
1005#if H_3D_QTLPC
1006            }
1007            if (bTryNx2N)
1008            {
1009#endif
1010              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
1011              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1012#if H_3D_VSP
1013              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
1014#endif
1015              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
1016              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1017#if H_3D_VSP
1018              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
1019#endif
1020#if H_3D_QTLPC
1021            }
1022#endif
1023
1024#endif
1025          }   
1026#endif
1027        }
1028#if  H_3D_FAST_TEXTURE_ENCODING
1029        if(!bFMD)
1030        {
1031#endif
1032        // do normal intra modes
1033       
1034          // speedup for inter frames
1035          if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
1036            rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
1037            rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
1038            rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     
1039#if H_3D_DIM_ENC
1040            || rpcBestCU->getSlice()->getIsDepth()
1041#endif
1042            ) // avoid very complex intra if it is unlikely
1043          {
1044#if H_3D_DIM
1045            Bool bOnlyIVP = false;
1046            Bool bUseIVP = true;
1047            if( rpcBestCU->getSlice()->getIsDepth() && !(rpcBestCU->getSlice()->isIRAP()) && 
1048              rpcBestCU->getSlice()->getSliceType() != I_SLICE && 
1049              rpcBestCU->getCbf( 0, TEXT_LUMA     ) == 0 &&
1050              rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) == 0 &&
1051              rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) == 0 
1052              )
1053            { 
1054              bOnlyIVP = true;
1055              bUseIVP = rpcBestCU->getSlice()->getIntraContourFlag();
1056            }
1057            if( bUseIVP )
1058            {
1059              xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bOnlyIVP );
1060#else
1061            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
1062#endif
1063
1064#if KWU_RC_MADPRED_E0227
1065            if ( uiDepth <= m_addSADDepth )
1066            {
1067              m_LCUPredictionSAD += m_spatialSAD;
1068              m_addSADDepth = uiDepth;
1069            }
1070#endif
1071
1072            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1073            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1074            {
1075#if H_3D_QTLPC //Try IntraNxN
1076              if(bTrySplit)
1077              {
1078#endif
1079                if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
1080                {
1081#if H_3D_DIM
1082                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN, bOnlyIVP );
1083#else
1084                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
1085#endif
1086                  rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1087                }
1088#if H_3D_QTLPC
1089              }
1090#endif
1091            }
1092#if H_3D
1093          }
1094#endif
1095        }
1096        // test PCM
1097        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
1098          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
1099          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
1100        {
1101          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
1102          UInt uiBestBits = rpcBestCU->getTotalBits();
1103#if H_3D_VSO // M7
1104          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
1105          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
1106#else
1107          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
1108#endif
1109          {
1110            xCheckIntraPCM (rpcBestCU, rpcTempCU);
1111            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1112          }
1113        }
1114#if  H_3D_FAST_TEXTURE_ENCODING
1115        }
1116#endif
1117        if (bIsLosslessMode)
1118        {
1119          iQP = iMinQP;
1120        }
1121      }
1122    }
1123
1124    m_pcEntropyCoder->resetBits();
1125    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1126    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1127      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1128    #if H_3D_VSO // M8
1129    if ( m_pcRdCost->getUseVSO() )   
1130      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
1131    else
1132#endif
1133    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1134
1135    // Early CU determination
1136    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
1137    {
1138      bSubBranch = false;
1139    }
1140    else
1141    {
1142      bSubBranch = true;
1143    }
1144#if  H_3D_FAST_TEXTURE_ENCODING
1145    if(rpcBestCU->getSlice()->getViewIndex() && !rpcBestCU->getSlice()->getIsDepth() && (uiDepth >=iIVFMaxD) && rpcBestCU->isSkipped(0))
1146    {
1147      bSubBranch = false;
1148    }
1149#endif
1150  }
1151  else if(!(bSliceEnd && bInsidePicture))
1152  {
1153    bBoundary = true;
1154  }
1155
1156  // copy orginal YUV samples to PCM buffer
1157  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
1158  {
1159    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
1160  }
1161  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1162  {
1163    Int idQP = m_pcEncCfg->getMaxDeltaQP();
1164    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
1165    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
1166  }
1167  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1168  {
1169    iMinQP = iBaseQP;
1170    iMaxQP = iBaseQP;
1171  }
1172  else
1173  {
1174    Int iStartQP;
1175    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
1176    {
1177      iStartQP = rpcTempCU->getQP(0);
1178    }
1179    else
1180    {
1181      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1182      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
1183    }
1184    iMinQP = iStartQP;
1185    iMaxQP = iStartQP;
1186  }
1187  if ( m_pcEncCfg->getUseRateCtrl() )
1188  {
1189    iMinQP = m_pcRateCtrl->getRCQP();
1190    iMaxQP = m_pcRateCtrl->getRCQP();
1191  }
1192
1193  if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
1194  {
1195    iMaxQP = iMinQP; // If all blocks are forced into using transquant bypass, do not loop here.
1196  }
1197  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1198  {
1199    const Bool bIsLosslessMode = false; // False at this level. Next level down may set it to true.
1200    rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1201
1202    // further split
1203#if H_3D_QTLPC
1204    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1205#else
1206    if( bSubBranch && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1207#endif
1208    {
1209#if H_3D_VSO // M9
1210      // reset Model
1211      if( m_pcRdCost->getUseRenModel() )
1212      {
1213        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
1214        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
1215        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
1216        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
1217        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1218      }
1219#endif
1220
1221      UChar       uhNextDepth         = uiDepth+1;
1222      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1223      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1224
1225#if H_3D_DISABLE_CHROMA
1226      m_ppcWeightedTempCU[uhNextDepth]->setSlice( m_ppcWeightedTempCU[ uiDepth]->getSlice()); 
1227#endif
1228      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1229      {
1230        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1231        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1232
1233        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
1234        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1235        {
1236            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1237            {
1238              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1239            }
1240            else
1241            {
1242              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1243            }
1244
1245#if AMP_ENC_SPEEDUP
1246          if ( rpcBestCU->isIntra(0) )
1247          {
1248            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
1249          }
1250          else
1251          {
1252            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
1253          }
1254#else
1255          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1256#endif
1257
1258          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1259          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1260        }
1261        else if (bInSlice)
1262        {
1263          pcSubBestPartCU->copyToPic( uhNextDepth );
1264          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
1265        }
1266      }
1267
1268      if( !bBoundary )
1269      {
1270        m_pcEntropyCoder->resetBits();
1271        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
1272
1273        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1274          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1275        }
1276#if H_3D_VSO // M10
1277      if ( m_pcRdCost->getUseVSO() )
1278        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1279      else
1280#endif
1281      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1282
1283      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
1284      {
1285        Bool hasResidual = false;
1286        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1287        {
1288          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
1289              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
1290          {
1291            hasResidual = true;
1292            break;
1293          }
1294        }
1295
1296        UInt uiTargetPartIdx;
1297        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
1298        {
1299          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1300        }
1301        else
1302        {
1303          uiTargetPartIdx = 0;
1304        }
1305        if ( hasResidual )
1306        {
1307#if !RDO_WITHOUT_DQP_BITS
1308          m_pcEntropyCoder->resetBits();
1309          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1310          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1311            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1312#if H_3D_VSO // M11
1313          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1314            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1315          else
1316#endif
1317          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1318#endif
1319
1320          Bool foundNonZeroCbf = false;
1321          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1322          assert( foundNonZeroCbf );
1323        }
1324        else
1325        {
1326          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1327        }
1328      }
1329
1330        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1331      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
1332                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1333      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1334                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
1335      if(isEndOfSlice||isEndOfSliceSegment)
1336      {
1337        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1338      }
1339      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1340    }                                                                                  // with sub partitioned prediction.
1341    }
1342
1343#if H_3D_VSO // M12
1344  if( m_pcRdCost->getUseRenModel() )
1345  {
1346    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1347    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1348    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1349    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1350    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1351  }
1352#endif
1353  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1354
1355  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1356  if( bBoundary ||(bSliceEnd && bInsidePicture))
1357  {
1358    return;
1359  }
1360
1361  // Assert if Best prediction mode is NONE
1362  // Selected mode's RD-cost must be not MAX_DOUBLE.
1363  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1364  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1365  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1366}
1367
1368/** finish encoding a cu and handle end-of-slice conditions
1369 * \param pcCU
1370 * \param uiAbsPartIdx
1371 * \param uiDepth
1372 * \returns Void
1373 */
1374Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1375{
1376  TComPic* pcPic = pcCU->getPic();
1377  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1378
1379  //Calculate end address
1380  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1381
1382  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1383  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1384  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1385  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1386  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1387  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1388  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1389  {
1390    uiInternalAddress--;
1391    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1392    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1393  }
1394  uiInternalAddress++;
1395  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1396  {
1397    uiInternalAddress = 0;
1398    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1399  }
1400  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1401
1402  // Encode slice finish
1403  Bool bTerminateSlice = false;
1404  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1405  {
1406    bTerminateSlice = true;
1407  }
1408  UInt uiGranularityWidth = g_uiMaxCUWidth;
1409  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1410  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1411  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1412    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1413 
1414  if(granularityBoundary)
1415  {
1416    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1417    if (!bTerminateSlice)
1418      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1419  }
1420 
1421  Int numberOfWrittenBits = 0;
1422  if (m_pcBitCounter)
1423  {
1424    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1425  }
1426 
1427  // Calculate slice end IF this CU puts us over slice bit size.
1428  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1429  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1430  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1431  {
1432    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1433  }
1434  // Set slice end parameter
1435  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1436  {
1437    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1438    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1439    return;
1440  }
1441  // Set dependent slice end parameter
1442  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1443  {
1444    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1445    return;
1446  }
1447  if(granularityBoundary)
1448  {
1449    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1450    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1451    if (m_pcBitCounter)
1452    {
1453      m_pcEntropyCoder->resetBits();     
1454    }
1455  }
1456}
1457
1458/** Compute QP for each CU
1459 * \param pcCU Target CU
1460 * \param uiDepth CU depth
1461 * \returns quantization parameter
1462 */
1463Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1464{
1465  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1466  Int iQpOffset = 0;
1467  if ( m_pcEncCfg->getUseAdaptiveQP() )
1468  {
1469    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1470    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1471    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1472    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1473    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1474    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1475    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1476
1477    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1478    Double dAvgAct = pcAQLayer->getAvgActivity();
1479    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1480    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1481    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1482    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1483  }
1484  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1485}
1486
1487/** encode a CU block recursively
1488 * \param pcCU
1489 * \param uiAbsPartIdx
1490 * \param uiDepth
1491 * \returns Void
1492 */
1493Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1494{
1495  TComPic* pcPic = pcCU->getPic();
1496 
1497  Bool bBoundary = false;
1498  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1499  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1500  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1501  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1502 
1503#if H_MV_ENC_DEC_TRAC
1504  DTRACE_CU_S("=========== coding_quadtree ===========\n")
1505  DTRACE_CU("x0", uiLPelX)
1506  DTRACE_CU("x1", uiTPelY)
1507  DTRACE_CU("log2CbSize", g_uiMaxCUWidth>>uiDepth)
1508  DTRACE_CU("cqtDepth"  , uiDepth)
1509#endif
1510
1511  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1512  // If slice start is within this cu...
1513  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1514    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1515  // We need to split, so don't try these modes.
1516  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1517  {
1518    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1519  }
1520  else
1521  {
1522    bBoundary = true;
1523  }
1524 
1525  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1526  {
1527    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1528    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1529    {
1530      setdQPFlag(true);
1531    }
1532    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1533    {
1534      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1535      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1536      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1537      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1538      {
1539        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1540      }
1541    }
1542    return;
1543  }
1544 
1545#if H_MV_ENC_DEC_TRAC
1546  DTRACE_CU_S("=========== coding_unit ===========\n")
1547#endif
1548
1549  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1550  {
1551    setdQPFlag(true);
1552  }
1553  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1554  {
1555    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1556  }
1557  if( !pcCU->getSlice()->isIntra() )
1558  {
1559    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1560  }
1561 
1562  if( pcCU->isSkipped( uiAbsPartIdx ) )
1563  {
1564#if H_MV_ENC_DEC_TRAC
1565    DTRACE_PU_S("=========== prediction_unit ===========\n")
1566    DTRACE_PU("x0", uiLPelX)
1567    DTRACE_PU("x1", uiTPelY)
1568#endif
1569    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1570#if H_3D_ARP
1571    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1572#endif
1573#if H_3D_IC
1574    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1575#endif
1576    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1577    return;
1578  }
1579#if H_3D
1580  m_pcEntropyCoder->encodeDIS( pcCU, uiAbsPartIdx );
1581  if(!pcCU->getDISFlag(uiAbsPartIdx))
1582  {
1583#endif
1584
1585  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1586 
1587  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1588 
1589  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1590  {
1591    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1592    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1593    {
1594#if H_3D_DIM_SDC
1595      m_pcEntropyCoder->encodeSDCFlag( pcCU, uiAbsPartIdx, false );
1596#endif 
1597      // Encode slice finish
1598      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1599      return;
1600    }
1601  }
1602
1603  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1604  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1605#if H_3D
1606  m_pcEntropyCoder->encodeDBBPFlag( pcCU, uiAbsPartIdx );
1607#endif
1608#if H_3D_DIM_SDC
1609  m_pcEntropyCoder->encodeSDCFlag( pcCU, uiAbsPartIdx, false );
1610#endif 
1611#if H_3D_ARP
1612  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1613#endif
1614#if H_3D_IC
1615  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1616#endif
1617  // Encode Coefficients
1618  Bool bCodeDQP = getdQPFlag();
1619#if H_3D_DISABLE_CHROMA
1620  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP, false );
1621#else
1622  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1623#endif
1624  setdQPFlag( bCodeDQP );
1625#if H_3D
1626  }
1627#endif
1628  // --- write terminating bit ---
1629  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1630}
1631
1632Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg) 
1633{
1634  Int k, i, j, jj;
1635  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1636
1637  for( k = 0; k < 64; k += 8 )
1638  {
1639    diff[k+0] = piOrg[0] ;
1640    diff[k+1] = piOrg[1] ;
1641    diff[k+2] = piOrg[2] ;
1642    diff[k+3] = piOrg[3] ;
1643    diff[k+4] = piOrg[4] ;
1644    diff[k+5] = piOrg[5] ;
1645    diff[k+6] = piOrg[6] ;
1646    diff[k+7] = piOrg[7] ;
1647 
1648    piOrg += iStrideOrg;
1649  }
1650 
1651  //horizontal
1652  for (j=0; j < 8; j++)
1653  {
1654    jj = j << 3;
1655    m2[j][0] = diff[jj  ] + diff[jj+4];
1656    m2[j][1] = diff[jj+1] + diff[jj+5];
1657    m2[j][2] = diff[jj+2] + diff[jj+6];
1658    m2[j][3] = diff[jj+3] + diff[jj+7];
1659    m2[j][4] = diff[jj  ] - diff[jj+4];
1660    m2[j][5] = diff[jj+1] - diff[jj+5];
1661    m2[j][6] = diff[jj+2] - diff[jj+6];
1662    m2[j][7] = diff[jj+3] - diff[jj+7];
1663   
1664    m1[j][0] = m2[j][0] + m2[j][2];
1665    m1[j][1] = m2[j][1] + m2[j][3];
1666    m1[j][2] = m2[j][0] - m2[j][2];
1667    m1[j][3] = m2[j][1] - m2[j][3];
1668    m1[j][4] = m2[j][4] + m2[j][6];
1669    m1[j][5] = m2[j][5] + m2[j][7];
1670    m1[j][6] = m2[j][4] - m2[j][6];
1671    m1[j][7] = m2[j][5] - m2[j][7];
1672   
1673    m2[j][0] = m1[j][0] + m1[j][1];
1674    m2[j][1] = m1[j][0] - m1[j][1];
1675    m2[j][2] = m1[j][2] + m1[j][3];
1676    m2[j][3] = m1[j][2] - m1[j][3];
1677    m2[j][4] = m1[j][4] + m1[j][5];
1678    m2[j][5] = m1[j][4] - m1[j][5];
1679    m2[j][6] = m1[j][6] + m1[j][7];
1680    m2[j][7] = m1[j][6] - m1[j][7];
1681  }
1682 
1683  //vertical
1684  for (i=0; i < 8; i++)
1685  {
1686    m3[0][i] = m2[0][i] + m2[4][i];
1687    m3[1][i] = m2[1][i] + m2[5][i];
1688    m3[2][i] = m2[2][i] + m2[6][i];
1689    m3[3][i] = m2[3][i] + m2[7][i];
1690    m3[4][i] = m2[0][i] - m2[4][i];
1691    m3[5][i] = m2[1][i] - m2[5][i];
1692    m3[6][i] = m2[2][i] - m2[6][i];
1693    m3[7][i] = m2[3][i] - m2[7][i];
1694   
1695    m1[0][i] = m3[0][i] + m3[2][i];
1696    m1[1][i] = m3[1][i] + m3[3][i];
1697    m1[2][i] = m3[0][i] - m3[2][i];
1698    m1[3][i] = m3[1][i] - m3[3][i];
1699    m1[4][i] = m3[4][i] + m3[6][i];
1700    m1[5][i] = m3[5][i] + m3[7][i];
1701    m1[6][i] = m3[4][i] - m3[6][i];
1702    m1[7][i] = m3[5][i] - m3[7][i];
1703   
1704    m2[0][i] = m1[0][i] + m1[1][i];
1705    m2[1][i] = m1[0][i] - m1[1][i];
1706    m2[2][i] = m1[2][i] + m1[3][i];
1707    m2[3][i] = m1[2][i] - m1[3][i];
1708    m2[4][i] = m1[4][i] + m1[5][i];
1709    m2[5][i] = m1[4][i] - m1[5][i];
1710    m2[6][i] = m1[6][i] + m1[7][i];
1711    m2[7][i] = m1[6][i] - m1[7][i];
1712  }
1713 
1714  for (i = 0; i < 8; i++)
1715  {
1716    for (j = 0; j < 8; j++)
1717    {
1718      iSumHad += abs(m2[i][j]);
1719    }
1720  }
1721  iSumHad -= abs(m2[0][0]);
1722  iSumHad =(iSumHad+2)>>2;
1723  return(iSumHad);
1724}
1725
1726Int  TEncCu::updateLCUDataISlice(TComDataCU* pcCU, Int LCUIdx, Int width, Int height)
1727{
1728  Int  xBl, yBl; 
1729  const Int iBlkSize = 8;
1730
1731  Pel* pOrgInit   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
1732  Int  iStrideOrig = pcCU->getPic()->getPicYuvOrg()->getStride();
1733  Pel  *pOrg;
1734
1735  Int iSumHad = 0;
1736  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1737  {
1738    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1739    {
1740      pOrg = pOrgInit + iStrideOrig*yBl + xBl; 
1741      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1742    }
1743  }
1744  return(iSumHad);
1745}
1746
1747/** check RD costs for a CU block encoded with merge
1748 * \param rpcBestCU
1749 * \param rpcTempCU
1750 * \returns Void
1751 */
1752Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1753{
1754  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1755#if H_3D_IV_MERGE
1756  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1757  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1758#else
1759  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1760  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1761#endif
1762  Int numValidMergeCand = 0;
1763  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
1764
1765  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1766  {
1767    uhInterDirNeighbours[ui] = 0;
1768  }
1769  UChar uhDepth = rpcTempCU->getDepth( 0 );
1770#if H_3D_IC
1771  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1772#endif
1773#if H_3D_VSO // M1  //nececcary here?
1774  if( m_pcRdCost->getUseRenModel() )
1775  {
1776    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1777    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1778    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1779    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1780    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1781  }
1782#endif
1783
1784#if H_3D_ARP
1785  DisInfo cOrigDisInfo = rpcTempCU->getDvInfo(0);
1786#else
1787  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1788#endif
1789
1790#if H_3D_VSP
1791#if !H_3D_ARP
1792  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1793  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1794  InheritedVSPDisInfo inheritedVSPDisInfo[MRG_MAX_NUM_CANDS_MEM];
1795  rpcTempCU->m_bAvailableFlagA1 = 0;
1796  rpcTempCU->m_bAvailableFlagB1 = 0;
1797  rpcTempCU->m_bAvailableFlagB0 = 0;
1798  rpcTempCU->m_bAvailableFlagA0 = 0;
1799  rpcTempCU->m_bAvailableFlagB2 = 0;
1800  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1801  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag,inheritedVSPDisInfo, numValidMergeCand );
1802#endif
1803#else
1804#if H_3D
1805  rpcTempCU->m_bAvailableFlagA1 = 0;
1806  rpcTempCU->m_bAvailableFlagB1 = 0;
1807  rpcTempCU->m_bAvailableFlagB0 = 0;
1808  rpcTempCU->m_bAvailableFlagA0 = 0;
1809  rpcTempCU->m_bAvailableFlagB2 = 0;
1810  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1811  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1812#else
1813  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1814#endif
1815#endif
1816
1817#if H_3D_IV_MERGE
1818  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1819#else
1820  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1821#endif
1822#if H_3D_ARP
1823for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1824#else
1825for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1826#endif
1827  {
1828    mergeCandBuffer[ui] = 0;
1829  }
1830
1831  Bool bestIsSkip = false;
1832
1833  UInt iteration;
1834  if ( rpcTempCU->isLosslessCoded(0))
1835  {
1836    iteration = 1;
1837  }
1838  else 
1839  {
1840    iteration = 2;
1841  }
1842
1843#if H_3D_ARP
1844  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1845  if( nARPWMax < 0 || bICFlag )
1846  {
1847    nARPWMax = 0;
1848  }
1849  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1850  {
1851    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS_MEM*sizeof(Int) );
1852    rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1853    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1854#if H_3D_IC
1855    rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1856#endif
1857    rpcTempCU->getDvInfo(0) = cOrigDisInfo;
1858    rpcTempCU->setDvInfoSubParts(cOrigDisInfo, 0, 0, uhDepth );
1859    Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1860    memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1861#if H_3D_SPIVMP
1862    Bool bSPIVMPFlag[MRG_MAX_NUM_CANDS_MEM];
1863    memset(bSPIVMPFlag, false, sizeof(Bool)*MRG_MAX_NUM_CANDS_MEM);
1864    TComMvField*  pcMvFieldSP;
1865    UChar* puhInterDirSP;
1866    pcMvFieldSP = new TComMvField[rpcTempCU->getPic()->getPicSym()->getNumPartition()*2]; 
1867    puhInterDirSP = new UChar[rpcTempCU->getPic()->getPicSym()->getNumPartition()]; 
1868#endif
1869#if H_3D
1870    rpcTempCU->initAvailableFlags();
1871    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1872    rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours
1873#if H_3D_SPIVMP
1874      , pcMvFieldSP, puhInterDirSP
1875#endif
1876      , numValidMergeCand
1877      );
1878
1879    rpcTempCU->buildMCL( cMvFieldNeighbours,uhInterDirNeighbours, vspFlag
1880#if H_3D_SPIVMP
1881      , bSPIVMPFlag
1882#endif
1883      , numValidMergeCand
1884      );
1885
1886#else
1887    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, numValidMergeCand );
1888#endif
1889
1890
1891#endif
1892
1893  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1894  {
1895    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1896    {     
1897#if H_3D_IC
1898        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1899        {
1900          if( bICFlag && uiMergeCand == 0 ) 
1901          {
1902            continue;
1903          }
1904        }
1905#endif
1906        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1907        {
1908        if( !(bestIsSkip && uiNoResidual == 0) )
1909        {
1910          // set MC parameters
1911          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1912          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag,     0, uhDepth );
1913          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1914#if H_3D_IC
1915          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1916#endif
1917#if H_3D_ARP
1918          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1919#endif
1920          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1921          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1922#if H_3D_VSP
1923          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1924#endif
1925#if H_3D_SPIVMP
1926          rpcTempCU->setSPIVMPFlagSubParts(bSPIVMPFlag[uiMergeCand], 0, 0, uhDepth);
1927          if (bSPIVMPFlag[uiMergeCand])
1928          {
1929            UInt uiSPAddr;
1930            Int iWidth = rpcTempCU->getWidth(0);
1931            Int iHeight = rpcTempCU->getHeight(0);
1932            Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
1933            rpcTempCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
1934            for (Int iPartitionIdx = 0; iPartitionIdx < iNumSP; iPartitionIdx++)
1935            {
1936              rpcTempCU->getSPAbsPartIdx(0, iSPWidth, iSPHeight, iPartitionIdx, iNumSPInOneLine, uiSPAddr);
1937              rpcTempCU->setInterDirSP(puhInterDirSP[iPartitionIdx], uiSPAddr, iSPWidth, iSPHeight);
1938              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx], iSPWidth, iSPHeight);
1939              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx + 1], iSPWidth, iSPHeight);
1940            }
1941          }
1942          else
1943#endif
1944#if H_3D_VSP
1945          {
1946          if ( vspFlag[uiMergeCand] )
1947          {
1948            UInt partAddr;
1949            Int vspSize;
1950            Int width, height;
1951            rpcTempCU->getPartIndexAndSize( 0, partAddr, width, height );
1952            if( uhInterDirNeighbours[ uiMergeCand ] & 0x01 )
1953            {
1954              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_0, cMvFieldNeighbours[ 2*uiMergeCand + 0 ].getRefIdx(), vspSize );
1955              rpcTempCU->setVSPFlag( partAddr, vspSize );
1956            }
1957            else
1958            {
1959              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1960            }
1961            if( uhInterDirNeighbours[ uiMergeCand ] & 0x02 )
1962            {
1963              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_1 , cMvFieldNeighbours[ 2*uiMergeCand + 1 ].getRefIdx(), vspSize );
1964              rpcTempCU->setVSPFlag( partAddr, vspSize );
1965            }
1966            else
1967            {
1968              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1969            }
1970            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1971          }
1972          else
1973          {
1974#endif
1975            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1976            rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1977            rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1978#if H_3D_VSP
1979          }
1980        }
1981#endif
1982       // do MC
1983       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1984       // estimate residual and encode everything
1985#if H_3D_VSO //M2
1986       if( m_pcRdCost->getUseRenModel() )
1987       { //Reset
1988         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1989         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1990         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1991         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1992         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1993       }
1994#endif
1995       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1996         m_ppcOrigYuv    [uhDepth],
1997         m_ppcPredYuvTemp[uhDepth],
1998         m_ppcResiYuvTemp[uhDepth],
1999         m_ppcResiYuvBest[uhDepth],
2000         m_ppcRecoYuvTemp[uhDepth],
2001         (uiNoResidual? true:false));
2002
2003
2004          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
2005         {
2006            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
2007           mergeCandBuffer[uiMergeCand] = 1;
2008         }
2009
2010          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
2011#if H_3D
2012          rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2013#endif
2014#if H_3D_VSP // possible bug fix
2015          if( rpcTempCU->getSkipFlag(0) )
2016          {
2017            rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2018          }
2019#endif
2020#if H_3D_INTER_SDC
2021          TComDataCU *rpcTempCUPre = rpcTempCU;
2022#endif
2023          Int orgQP = rpcTempCU->getQP( 0 );
2024          xCheckDQP( rpcTempCU );
2025          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2026#if H_3D_INTER_SDC
2027          if( rpcTempCU->getSlice()->getInterSdcFlag() && !uiNoResidual )
2028          {
2029            Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2030            for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2031            {
2032              if( uiOffest > 3)
2033              {
2034                if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2035                {
2036                  continue;
2037                }
2038                if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2039                {
2040                  continue;
2041                }
2042                if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2043                {
2044                  continue;
2045                }
2046              }
2047              if( rpcTempCU != rpcTempCUPre )
2048              {
2049                rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
2050                rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2051              }
2052              rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2053#if H_3D
2054              rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2055#endif
2056              rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2057              rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2058#if H_3D_VSO //M2
2059              if( m_pcRdCost->getUseRenModel() )
2060              { //Reset
2061                UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
2062                UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
2063                Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
2064                UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
2065                m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2066              }
2067#endif
2068              Int iSdcOffset = 0;
2069              if(uiOffest % 2 == 0)
2070              {
2071                iSdcOffset = uiOffest >> 1;
2072              }
2073              else
2074              {
2075                iSdcOffset = -1 * (uiOffest >> 1);
2076              }
2077              m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2078                m_ppcOrigYuv[uhDepth], 
2079                ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
2080                m_ppcResiYuvTemp[uhDepth], 
2081                m_ppcRecoYuvTemp[uhDepth],
2082                iSdcOffset,
2083                uhDepth );
2084              if (uiOffest <= 3 )
2085              {
2086                dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2087              }
2088
2089              xCheckDQP( rpcTempCU );
2090              xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
2091            }
2092          }
2093#endif
2094          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
2095
2096      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
2097      {
2098#if H_3D_INTER_SDC
2099        if( rpcTempCU->getSlice()->getInterSdcFlag() )
2100        {
2101          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
2102        }
2103        else
2104        {
2105#endif
2106        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
2107#if H_3D_INTER_SDC
2108        }
2109#endif
2110      }
2111    }
2112   }
2113  }
2114
2115  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
2116  {
2117    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
2118    {
2119      if( rpcBestCU->getMergeFlag( 0 ))
2120      {
2121        *earlyDetectionSkipMode = true;
2122      }
2123      else
2124      {
2125        Int absoulte_MV=0;
2126        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2127        {
2128          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
2129          {
2130            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
2131            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
2132            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
2133            absoulte_MV+=iHor+iVer;
2134          }
2135        }
2136
2137        if(absoulte_MV == 0)
2138        {
2139          *earlyDetectionSkipMode = true;
2140        }
2141      }
2142    }
2143  }
2144 }
2145#if H_3D_SPIVMP
2146 delete[] pcMvFieldSP;
2147 delete[] puhInterDirSP;
2148#endif
2149#if H_3D_ARP
2150 }
2151#endif
2152}
2153
2154
2155#if AMP_MRG
2156#if  H_3D_FAST_TEXTURE_ENCODING
2157Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
2158#else
2159Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2160#endif
2161#else
2162Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2163#endif
2164{
2165
2166#if H_3D
2167  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
2168#endif
2169#if  H_3D_FAST_TEXTURE_ENCODING
2170  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2171  {
2172#endif
2173  UChar uhDepth = rpcTempCU->getDepth( 0 );
2174#if H_3D_ARP
2175  Bool bFirstTime = true;
2176  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2177  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || rpcTempCU->getICFlag(0) )
2178  {
2179    nARPWMax = 0;
2180  }
2181
2182  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
2183  {
2184    if( !bFirstTime && rpcTempCU->getSlice()->getIvResPredFlag() )
2185    {
2186      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0),bTransquantBypassFlag );     
2187    }
2188#endif
2189#if H_3D_VSO // M3
2190  if( m_pcRdCost->getUseRenModel() )
2191  {
2192    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2193    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2194    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2195    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2196    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2197  }
2198#endif
2199
2200  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2201 
2202  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2203#if H_3D
2204  rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2205#endif
2206  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2207  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2208
2209#if H_3D_ARP
2210  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2211#endif
2212
2213#if H_3D_ARP
2214  if( bFirstTime == false && nARPWMax )
2215  {
2216    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2217    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2218
2219    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2220  }
2221  else
2222  {
2223    bFirstTime = false;
2224#endif
2225#if AMP_MRG
2226  rpcTempCU->setMergeAMP (true);
2227#if  H_3D_FAST_TEXTURE_ENCODING
2228  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
2229#else
2230  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
2231#endif
2232#else 
2233  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2234#endif
2235#if H_3D_ARP
2236   if( nARPWMax )
2237   {
2238     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2239   }
2240  }
2241#endif
2242
2243#if AMP_MRG
2244  if ( !rpcTempCU->getMergeAMP() )
2245  {
2246#if H_3D_ARP
2247    if( nARPWMax )
2248    {
2249      continue;
2250    }
2251    else
2252#endif
2253    return;
2254  }
2255#endif
2256
2257#if KWU_RC_MADPRED_E0227
2258  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2259  {
2260    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2261      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2262      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2263    m_temporalSAD = (Int)SAD;
2264  }
2265#endif
2266  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2267#if H_3D_VSP // possible bug fix
2268  if( rpcTempCU->getQtRootCbf(0)==0 )
2269  {
2270    rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2271  }
2272#endif
2273#if H_3D_VSO // M4
2274  if( m_pcRdCost->getUseLambdaScaleVSO() )
2275    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2276  else
2277#endif
2278  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2279#if H_3D_INTER_SDC
2280  TComDataCU *rpcTempCUPre = rpcTempCU;
2281#endif
2282  xCheckDQP( rpcTempCU );
2283  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2284#if H_3D_INTER_SDC
2285  if( rpcTempCU->getSlice()->getInterSdcFlag() && ePartSize == SIZE_2Nx2N)
2286  {
2287    Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2288    for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2289    {
2290      if( uiOffest > 3)
2291      {
2292        if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2293        {
2294          continue;
2295        }
2296        if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2297        {
2298          continue;
2299        }
2300        if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2301        {
2302          continue;
2303        }
2304      }
2305
2306      if( rpcTempCU != rpcTempCUPre )
2307      {
2308        Int orgQP = rpcBestCU->getQP( 0 );
2309        rpcTempCU->initEstData( uhDepth, orgQP ,bTransquantBypassFlag );     
2310        rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2311      }
2312      rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2313#if H_3D
2314      rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2315#endif
2316      rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2317      rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2318#if H_3D_VSO // M3
2319      if( m_pcRdCost->getUseRenModel() )
2320      {
2321        UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2322        UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2323        Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2324        UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2325        m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2326      }
2327#endif
2328
2329      Int iSdcOffset = 0;
2330      if(uiOffest % 2 == 0)
2331      {
2332        iSdcOffset = uiOffest >> 1;
2333      }
2334      else
2335      {
2336        iSdcOffset = -1 * (uiOffest >> 1);
2337      }
2338      m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2339        m_ppcOrigYuv[uhDepth],
2340        ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2341        m_ppcResiYuvTemp[uhDepth],
2342        m_ppcRecoYuvTemp[uhDepth],
2343        iSdcOffset,
2344        uhDepth );
2345      if (uiOffest <= 3 )
2346      {
2347        dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2348      }
2349
2350      xCheckDQP( rpcTempCU );
2351      xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2352    }
2353
2354  }
2355#endif
2356#if H_3D_ARP
2357  }
2358#endif
2359#if  H_3D_FAST_TEXTURE_ENCODING
2360  }
2361#endif
2362}
2363
2364#if H_3D_DBBP
2365Void TEncCu::xInvalidateOriginalSegments( TComYuv* pOrigYuv, TComYuv* pOrigYuvTemp, Bool* pMask, UInt uiValidSegment )
2366{
2367  UInt  uiWidth     = pOrigYuv->getWidth ( );
2368  UInt  uiHeight    = pOrigYuv->getHeight( );
2369  Pel*  piSrc       = pOrigYuv->getLumaAddr( );
2370  UInt  uiSrcStride = pOrigYuv->getStride();
2371  Pel*  piDst       = pOrigYuvTemp->getLumaAddr( );
2372  UInt  uiDstStride = pOrigYuvTemp->getStride();
2373 
2374  UInt  uiMaskStride= MAX_CU_SIZE;
2375 
2376  AOF( uiWidth == uiHeight );
2377 
2378  // backup pointer
2379  Bool* pMaskStart = pMask;
2380 
2381  for (Int y=0; y<uiHeight; y++)
2382  {
2383    for (Int x=0; x<uiWidth; x++)
2384    {
2385      UChar ucSegment = (UChar)pMask[x];
2386      AOF( ucSegment < 2 );
2387     
2388      piDst[x] = (ucSegment==uiValidSegment)?piSrc[x]:DBBP_INVALID_SHORT;
2389    }
2390   
2391    piSrc  += uiSrcStride;
2392    piDst  += uiDstStride;
2393    pMask  += uiMaskStride;
2394  }
2395 
2396  // now invalidate chroma
2397  Pel*  piSrcU       = pOrigYuv->getCbAddr();
2398  Pel*  piSrcV       = pOrigYuv->getCrAddr();
2399  UInt  uiSrcStrideC = pOrigYuv->getCStride();
2400  Pel*  piDstU       = pOrigYuvTemp->getCbAddr( );
2401  Pel*  piDstV       = pOrigYuvTemp->getCrAddr( );
2402  UInt  uiDstStrideC = pOrigYuvTemp->getCStride();
2403  pMask = pMaskStart;
2404 
2405  for (Int y=0; y<uiHeight/2; y++)
2406  {
2407    for (Int x=0; x<uiWidth/2; x++)
2408    {
2409      UChar ucSegment = (UChar)pMask[x*2];
2410      AOF( ucSegment < 2 );
2411     
2412      piDstU[x] = (ucSegment==uiValidSegment)?piSrcU[x]:DBBP_INVALID_SHORT;
2413      piDstV[x] = (ucSegment==uiValidSegment)?piSrcV[x]:DBBP_INVALID_SHORT;
2414    }
2415   
2416    piSrcU  += uiSrcStrideC;
2417    piSrcV  += uiSrcStrideC;
2418    piDstU  += uiDstStrideC;
2419    piDstV  += uiDstStrideC;
2420    pMask   += 2*uiMaskStride;
2421  }
2422}
2423
2424Void TEncCu::xCheckRDCostDIS( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2425{
2426  UInt uiDepth = rpcTempCU->getDepth( 0 );
2427  if( !rpcBestCU->getSlice()->getIsDepth() || (eSize != SIZE_2Nx2N))
2428  {
2429    return;
2430  }
2431
2432#if H_3D_VSO // M5
2433  if( m_pcRdCost->getUseRenModel() )
2434  {
2435    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2436    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2437    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2438    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2439    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2440  }
2441#endif
2442
2443  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2444  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2445  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2446  rpcTempCU->setCUTransquantBypassSubParts( rpcTempCU->getCUTransquantBypass(0), 0, uiDepth );
2447
2448  rpcTempCU->setTrIdxSubParts(0, 0, uiDepth);
2449  rpcTempCU->setCbfSubParts(0, 1, 1, 0, uiDepth);
2450  rpcTempCU->setDISFlagSubParts(true, 0, uiDepth);
2451  rpcTempCU->setLumaIntraDirSubParts (DC_IDX, 0, uiDepth);
2452#if H_3D_DIM_SDC
2453  rpcTempCU->setSDCFlagSubParts( false, 0, uiDepth);
2454#endif
2455
2456  UInt uiPreCalcDistC;
2457  m_pcPredSearch  ->estIntraPredDIS      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, false );
2458
2459#if ENC_DEC_TRACE && H_MV_ENC_DEC_TRAC
2460  Int oldTraceCopyBack = g_traceCopyBack; 
2461  g_traceCopyBack = false; 
2462#endif
2463  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2464  #if ENC_DEC_TRACE && H_MV_ENC_DEC_TRAC 
2465    g_traceCopyBack = oldTraceCopyBack; 
2466  #endif
2467
2468
2469  m_pcEntropyCoder->resetBits();
2470  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2471  {
2472    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2473  }
2474  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2475  m_pcEntropyCoder->encodeDIS( rpcTempCU, 0,          true );
2476
2477
2478  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2479
2480  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2481  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2482
2483
2484#if H_3D_VSO // M6
2485  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2486    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2487  else
2488#endif
2489    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2490
2491
2492  xCheckDQP( rpcTempCU );
2493  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2494}
2495
2496Void TEncCu::xCheckRDCostInterDBBP( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool bUseMRG )
2497{
2498  AOF( !rpcTempCU->getSlice()->getIsDepth() );
2499 
2500  UChar uhDepth = rpcTempCU->getDepth( 0 );
2501 
2502#if H_3D_VSO
2503  if( m_pcRdCost->getUseRenModel() )
2504  {
2505    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2506    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2507    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2508    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2509    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2510  }
2511#endif
2512 
2513  UInt uiWidth  = rpcTempCU->getWidth(0);
2514  UInt uiHeight = rpcTempCU->getHeight(0);
2515  AOF( uiWidth == uiHeight );
2516 
2517#if H_3D_DBBP
2518  // Is this correct here, was under the macro SEC_DBBP_DISALLOW_8x8_I0078, however the function is related to Single Depth Mode
2519  if(uiWidth <= 8)
2520  {
2521    return;
2522  }
2523#endif
2524 
2525  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2526 
2527  // fetch virtual depth block
2528  UInt uiDepthStride = 0;
2529#if H_3D_FCO
2530  Pel* pDepthPels = rpcTempCU->getVirtualDepthBlock(rpcTempCU->getZorderIdxInCU(), uiWidth, uiHeight, uiDepthStride);
2531#else
2532  Pel* pDepthPels = rpcTempCU->getVirtualDepthBlock(0, uiWidth, uiHeight, uiDepthStride);
2533#endif
2534  AOF( pDepthPels != NULL );
2535  AOF( uiDepthStride != 0 );
2536 
2537  PartSize eVirtualPartSize = m_pcPredSearch->getPartitionSizeFromDepth(pDepthPels, uiDepthStride, uiWidth, rpcTempCU);
2538
2539  // derive partitioning from depth
2540  Bool pMask[MAX_CU_SIZE*MAX_CU_SIZE];
2541  Bool bValidMask = m_pcPredSearch->getSegmentMaskFromDepth(pDepthPels, uiDepthStride, uiWidth, uiHeight, pMask, rpcTempCU);
2542 
2543  if( !bValidMask )
2544  {
2545    return;
2546  }
2547 
2548  // find optimal motion/disparity vector for each segment
2549  DisInfo originalDvInfo = rpcTempCU->getDvInfo(0);
2550  DBBPTmpData* pDBBPTmpData = rpcTempCU->getDBBPTmpData();
2551  TComYuv* apPredYuv[2] = { m_ppcRecoYuvTemp[uhDepth], m_ppcPredYuvTemp[uhDepth] };
2552 
2553  // find optimal motion vector fields for both segments (as 2Nx2N)
2554  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2555  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2556  rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth );
2557  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2558  {
2559    rpcTempCU->setDBBPFlagSubParts(true, 0, 0, uhDepth);
2560    rpcTempCU->setDvInfoSubParts(originalDvInfo, 0, uhDepth);
2561   
2562    // invalidate all other segments in original YUV
2563    xInvalidateOriginalSegments(m_ppcOrigYuv[uhDepth], m_ppcOrigYuvDBBP[uhDepth], pMask, uiSegment);
2564   
2565    // do motion estimation for this segment
2566    m_pcRdCost->setUseMask(true);
2567    rpcTempCU->getDBBPTmpData()->eVirtualPartSize = eVirtualPartSize;
2568    rpcTempCU->getDBBPTmpData()->uiVirtualPartIndex = uiSegment;
2569    m_pcPredSearch->predInterSearch( rpcTempCU, m_ppcOrigYuvDBBP[uhDepth], apPredYuv[uiSegment], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], false, false, bUseMRG );
2570    m_pcRdCost->setUseMask(false);
2571   
2572    // extract motion parameters of full block for this segment
2573    pDBBPTmpData->auhInterDir[uiSegment] = rpcTempCU->getInterDir(0);
2574   
2575    pDBBPTmpData->abMergeFlag[uiSegment] = rpcTempCU->getMergeFlag(0);
2576    pDBBPTmpData->auhMergeIndex[uiSegment] = rpcTempCU->getMergeIndex(0);
2577   
2578    AOF( rpcTempCU->getSPIVMPFlag(0) == false );
2579    AOF( rpcTempCU->getVSPFlag(0) == 0 );
2580   
2581    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2582    {
2583      RefPicList eRefList = (RefPicList)uiRefListIdx;
2584     
2585      pDBBPTmpData->acMvd[uiSegment][eRefList] = rpcTempCU->getCUMvField(eRefList)->getMvd(0);
2586      pDBBPTmpData->aiMvpNum[uiSegment][eRefList] = rpcTempCU->getMVPNum(eRefList, 0);
2587      pDBBPTmpData->aiMvpIdx[uiSegment][eRefList] = rpcTempCU->getMVPIdx(eRefList, 0);
2588     
2589      rpcTempCU->getMvField(rpcTempCU, 0, eRefList, pDBBPTmpData->acMvField[uiSegment][eRefList]);
2590    }
2591  }
2592 
2593  // store final motion/disparity information in each PU using derived partitioning
2594  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2595  rpcTempCU->setPartSizeSubParts  ( eVirtualPartSize,  0, uhDepth );
2596  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2597 
2598  UInt uiPUOffset = ( g_auiPUOffset[UInt( eVirtualPartSize )] << ( ( rpcTempCU->getSlice()->getSPS()->getMaxCUDepth() - uhDepth ) << 1 ) ) >> 4;
2599  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2600  {
2601    UInt uiPartAddr = uiSegment*uiPUOffset;
2602   
2603    rpcTempCU->setDBBPFlagSubParts(true, uiPartAddr, uiSegment, uhDepth);
2604   
2605    // now set stored information from 2Nx2N motion search to each partition
2606    rpcTempCU->setInterDirSubParts(pDBBPTmpData->auhInterDir[uiSegment], uiPartAddr, uiSegment, uhDepth); // interprets depth relative to LCU level
2607   
2608    rpcTempCU->setMergeFlagSubParts(pDBBPTmpData->abMergeFlag[uiSegment], uiPartAddr, uiSegment, uhDepth);
2609    rpcTempCU->setMergeIndexSubParts(pDBBPTmpData->auhMergeIndex[uiSegment], uiPartAddr, uiSegment, uhDepth);
2610       
2611    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2612    {
2613      RefPicList eRefList = (RefPicList)uiRefListIdx;
2614     
2615      rpcTempCU->getCUMvField( eRefList )->setAllMvd(pDBBPTmpData->acMvd[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment);
2616      rpcTempCU->setMVPNum(eRefList, uiPartAddr, pDBBPTmpData->aiMvpNum[uiSegment][eRefList]);
2617      rpcTempCU->setMVPIdx(eRefList, uiPartAddr, pDBBPTmpData->aiMvpIdx[uiSegment][eRefList]);
2618     
2619      rpcTempCU->getCUMvField( eRefList )->setAllMvField( pDBBPTmpData->acMvField[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment ); // interprets depth relative to rpcTempCU level
2620    }
2621  }
2622 
2623  // reconstruct final prediction signal by combining both segments
2624  m_pcPredSearch->combineSegmentsWithMask(apPredYuv, m_ppcPredYuvTemp[uhDepth], pMask, uiWidth, uiHeight, 0, eVirtualPartSize);
2625  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2626 
2627  xCheckDQP( rpcTempCU );
2628  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2629}
2630#endif
2631#if H_3D_DIM
2632Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize, Bool bOnlyIVP )
2633#else
2634Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2635#endif
2636{
2637  UInt uiDepth = rpcTempCU->getDepth( 0 ); 
2638#if H_3D_VSO // M5
2639  if( m_pcRdCost->getUseRenModel() )
2640  {
2641    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2642    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2643    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2644    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2645    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2646  }
2647#endif
2648
2649  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2650#if H_3D
2651  rpcTempCU->setDISFlagSubParts( false, 0, uiDepth );
2652#endif
2653  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2654  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2655 
2656  Bool bSeparateLumaChroma = true; // choose estimation mode
2657  UInt uiPreCalcDistC      = 0;
2658  if( !bSeparateLumaChroma )
2659  {
2660    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
2661  }
2662#if H_3D_DIM
2663  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma, bOnlyIVP );
2664#else
2665  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
2666#endif
2667  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2668 
2669#if H_3D_DIM_SDC
2670  if( !rpcTempCU->getSDCFlag( 0 ) )
2671#endif
2672  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
2673 
2674  m_pcEntropyCoder->resetBits();
2675  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2676  {
2677    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2678  }
2679  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2680#if H_3D
2681  m_pcEntropyCoder->encodeDIS( rpcTempCU, 0,          true );
2682  if(!rpcTempCU->getDISFlag(0))
2683  {
2684#endif
2685  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2686  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2687  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
2688  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2689#if H_3D_DIM_SDC
2690  m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2691#endif
2692
2693  // Encode Coefficients
2694  Bool bCodeDQP = getdQPFlag();
2695#if H_3D_DISABLE_CHROMA
2696  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP, true );
2697#else
2698  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2699#endif
2700
2701
2702  setdQPFlag( bCodeDQP );
2703#if H_3D
2704  }
2705#endif
2706  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2707 
2708  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2709    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2710#if H_3D_VSO // M6
2711  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2712    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2713  else
2714#endif
2715  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2716 
2717  xCheckDQP( rpcTempCU );
2718  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2719}
2720
2721/** Check R-D costs for a CU with PCM mode.
2722 * \param rpcBestCU pointer to best mode CU data structure
2723 * \param rpcTempCU pointer to testing mode CU data structure
2724 * \returns Void
2725 *
2726 * \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.
2727 */
2728Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2729{
2730  UInt uiDepth = rpcTempCU->getDepth( 0 );
2731
2732  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2733#if H_3D
2734  rpcTempCU->setDISFlagSubParts( false, 0, uiDepth );
2735#endif
2736  rpcTempCU->setIPCMFlag(0, true);
2737  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2738  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2739  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2740  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2741
2742  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2743
2744  m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2745
2746  m_pcEntropyCoder->resetBits();
2747  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2748  {
2749    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2750  }
2751  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2752#if H_3D
2753  m_pcEntropyCoder->encodeDIS( rpcTempCU, 0,          true );
2754#endif
2755  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2756  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2757  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2758#if H_3D_DIM_SDC
2759  m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2760#endif
2761  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2762
2763  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2764    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2765#if H_3D_VSO // M44
2766  if ( m_pcRdCost->getUseVSO() )
2767    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2768  else
2769#endif
2770  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2771
2772  xCheckDQP( rpcTempCU );
2773  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2774}
2775
2776/** check whether current try is the best with identifying the depth of current try
2777 * \param rpcBestCU
2778 * \param rpcTempCU
2779 * \returns Void
2780 */
2781Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2782{
2783  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2784  {
2785    TComYuv* pcYuv;
2786    // Change Information data
2787    TComDataCU* pcCU = rpcBestCU;
2788    rpcBestCU = rpcTempCU;
2789    rpcTempCU = pcCU;
2790
2791    // Change Prediction data
2792    pcYuv = m_ppcPredYuvBest[uiDepth];
2793    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2794    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2795
2796    // Change Reconstruction data
2797    pcYuv = m_ppcRecoYuvBest[uiDepth];
2798    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2799    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2800
2801    pcYuv = NULL;
2802    pcCU  = NULL;
2803
2804    // store temp best CI for next CU coding
2805      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2806  }
2807}
2808
2809Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2810{
2811  UInt uiDepth = pcCU->getDepth( 0 );
2812
2813  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2814  {
2815    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2816    {
2817#if !RDO_WITHOUT_DQP_BITS
2818      m_pcEntropyCoder->resetBits();
2819      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2820      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2821        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2822#if H_3D_VSO // M45
2823      if ( m_pcRdCost->getUseVSO() )     
2824        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2825      else
2826#endif
2827      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2828#endif
2829    }
2830    else
2831    {
2832      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2833    }
2834  }
2835}
2836
2837Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2838{
2839  pDst->iN = pSrc->iN;
2840  for (Int i = 0; i < pSrc->iN; i++)
2841  {
2842    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2843  }
2844}
2845Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2846{
2847  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2848  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2849  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2850  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2851    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2852  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2853    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2854  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2855  {
2856    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2857    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2858    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2859    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2860    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2861    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2862    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2863  }
2864  else
2865  {
2866    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2867
2868    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2869    {
2870      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2871      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2872
2873      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
2874        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
2875      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2876      {
2877        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2878      }
2879    }
2880  }
2881}
2882
2883Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2884{
2885  UInt uiCurrDepth = uiNextDepth - 1;
2886  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2887}
2888
2889/** Function for filling the PCM buffer of a CU using its original sample array
2890 * \param pcCU pointer to current CU
2891 * \param pcOrgYuv pointer to original sample array
2892 * \returns Void
2893 */
2894Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2895{
2896
2897  UInt   width        = pCU->getWidth(0);
2898  UInt   height       = pCU->getHeight(0);
2899
2900  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2901  Pel*   pDstY = pCU->getPCMSampleY();
2902  UInt   srcStride = pOrgYuv->getStride();
2903
2904  for(Int y = 0; y < height; y++ )
2905  {
2906    for(Int x = 0; x < width; x++ )
2907    {
2908      pDstY[x] = pSrcY[x];
2909    }
2910    pDstY += width;
2911    pSrcY += srcStride;
2912  }
2913
2914  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2915  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2916
2917  Pel* pDstCb       = pCU->getPCMSampleCb();
2918  Pel* pDstCr       = pCU->getPCMSampleCr();;
2919
2920  UInt srcStrideC = pOrgYuv->getCStride();
2921  UInt heightC   = height >> 1;
2922  UInt widthC    = width  >> 1;
2923
2924  for(Int y = 0; y < heightC; y++ )
2925  {
2926    for(Int x = 0; x < widthC; x++ )
2927    {
2928      pDstCb[x] = pSrcCb[x];
2929      pDstCr[x] = pSrcCr[x];
2930    }
2931    pDstCb += widthC;
2932    pDstCr += widthC;
2933    pSrcCb += srcStrideC;
2934    pSrcCr += srcStrideC;
2935  }
2936}
2937
2938#if ADAPTIVE_QP_SELECTION
2939/** Collect ARL statistics from one block
2940  */
2941Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2942{
2943  for( Int n = 0; n < NumCoeffInCU; n++ )
2944  {
2945    Int u = abs( rpcCoeff[ n ] );
2946    Int absc = rpcArlCoeff[ n ];
2947
2948    if( u != 0 )
2949    {
2950      if( u < LEVEL_RANGE )
2951      {
2952        cSum[ u ] += ( Double )absc;
2953        numSamples[ u ]++;
2954      }
2955      else 
2956      {
2957        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2958        numSamples[ LEVEL_RANGE ]++;
2959      }
2960    }
2961  }
2962
2963  return 0;
2964}
2965
2966/** Collect ARL statistics from one LCU
2967 * \param pcCU
2968 */
2969Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2970{
2971  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2972  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2973
2974  TCoeff* pCoeffY = rpcCU->getCoeffY();
2975  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2976
2977  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2978  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2979
2980  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2981  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2982
2983  // Collect stats to cSum[][] and numSamples[][]
2984  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2985  {
2986    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2987
2988    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2989    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2990    {
2991      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2992    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2993   
2994    pCoeffY  += uiMinNumCoeffInCU;
2995    pArlCoeffY  += uiMinNumCoeffInCU;
2996  }
2997
2998  for(Int u=1; u<LEVEL_RANGE;u++)
2999  {
3000    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
3001    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
3002  }
3003  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
3004  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
3005}
3006#endif
3007//! \}
Note: See TracBrowser for help on using the repository browser.