source: SHVCSoftware/branches/SHM-2.1-dev/source/Lib/TLibEncoder/TEncCu.cpp @ 300

Last change on this file since 300 was 297, checked in by vidyo, 12 years ago

Implementation of inter_layer_sample_pred_only_flag part of M0457. The code is disabled by default. Enable by setting M0457_IL_SAMPLE_PRED_ONLY_FLAG to 1.

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