source: SHVCSoftware/branches/HM-10.0-dev-SHM/source/Lib/TLibEncoder/TEncSearch.cpp @ 61

Last change on this file since 61 was 54, checked in by seregin, 12 years ago

port simulcast

File size: 247.9 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     TEncSearch.cpp
35 \brief    encoder search class
36 */
37
38#include "TLibCommon/TypeDef.h"
39#include "TLibCommon/TComRom.h"
40#include "TLibCommon/TComMotionInfo.h"
41#include "TEncSearch.h"
42#include <math.h>
43
44//! \ingroup TLibEncoder
45//! \{
46
47static const TComMv s_acMvRefineH[9] =
48{
49  TComMv(  0,  0 ), // 0
50  TComMv(  0, -1 ), // 1
51  TComMv(  0,  1 ), // 2
52  TComMv( -1,  0 ), // 3
53  TComMv(  1,  0 ), // 4
54  TComMv( -1, -1 ), // 5
55  TComMv(  1, -1 ), // 6
56  TComMv( -1,  1 ), // 7
57  TComMv(  1,  1 )  // 8
58};
59
60static const TComMv s_acMvRefineQ[9] =
61{
62  TComMv(  0,  0 ), // 0
63  TComMv(  0, -1 ), // 1
64  TComMv(  0,  1 ), // 2
65  TComMv( -1, -1 ), // 5
66  TComMv(  1, -1 ), // 6
67  TComMv( -1,  0 ), // 3
68  TComMv(  1,  0 ), // 4
69  TComMv( -1,  1 ), // 7
70  TComMv(  1,  1 )  // 8
71};
72
73static const UInt s_auiDFilter[9] =
74{
75  0, 1, 0,
76  2, 3, 2,
77  0, 1, 0
78};
79
80TEncSearch::TEncSearch()
81{
82  m_ppcQTTempCoeffY  = NULL;
83  m_ppcQTTempCoeffCb = NULL;
84  m_ppcQTTempCoeffCr = NULL;
85  m_pcQTTempCoeffY   = NULL;
86  m_pcQTTempCoeffCb  = NULL;
87  m_pcQTTempCoeffCr  = NULL;
88#if ADAPTIVE_QP_SELECTION
89  m_ppcQTTempArlCoeffY  = NULL;
90  m_ppcQTTempArlCoeffCb = NULL;
91  m_ppcQTTempArlCoeffCr = NULL;
92  m_pcQTTempArlCoeffY   = NULL;
93  m_pcQTTempArlCoeffCb  = NULL;
94  m_pcQTTempArlCoeffCr  = NULL;
95#endif
96  m_puhQTTempTrIdx   = NULL;
97  m_puhQTTempCbf[0] = m_puhQTTempCbf[1] = m_puhQTTempCbf[2] = NULL;
98  m_pcQTTempTComYuv  = NULL;
99  m_pcEncCfg = NULL;
100  m_pcEntropyCoder = NULL;
101  m_pTempPel = NULL;
102  m_pSharedPredTransformSkip[0] = m_pSharedPredTransformSkip[1] = m_pSharedPredTransformSkip[2] = NULL;
103  m_pcQTTempTUCoeffY   = NULL;
104  m_pcQTTempTUCoeffCb  = NULL;
105  m_pcQTTempTUCoeffCr  = NULL;
106#if ADAPTIVE_QP_SELECTION
107  m_ppcQTTempTUArlCoeffY  = NULL;
108  m_ppcQTTempTUArlCoeffCb = NULL;
109  m_ppcQTTempTUArlCoeffCr = NULL;
110#endif
111  m_puhQTTempTransformSkipFlag[0] = NULL;
112  m_puhQTTempTransformSkipFlag[1] = NULL;
113  m_puhQTTempTransformSkipFlag[2] = NULL;
114  setWpScalingDistParam( NULL, -1, REF_PIC_LIST_X );
115}
116
117TEncSearch::~TEncSearch()
118{
119  if ( m_pTempPel )
120  {
121    delete [] m_pTempPel;
122    m_pTempPel = NULL;
123  }
124 
125  if ( m_pcEncCfg )
126  {
127    const UInt uiNumLayersAllocated = m_pcEncCfg->getQuadtreeTULog2MaxSize()-m_pcEncCfg->getQuadtreeTULog2MinSize()+1;
128    for( UInt ui = 0; ui < uiNumLayersAllocated; ++ui )
129    {
130      delete[] m_ppcQTTempCoeffY[ui];
131      delete[] m_ppcQTTempCoeffCb[ui];
132      delete[] m_ppcQTTempCoeffCr[ui];
133#if ADAPTIVE_QP_SELECTION
134      delete[] m_ppcQTTempArlCoeffY[ui];
135      delete[] m_ppcQTTempArlCoeffCb[ui];
136      delete[] m_ppcQTTempArlCoeffCr[ui];
137#endif
138      m_pcQTTempTComYuv[ui].destroy();
139    }
140  }
141  delete[] m_ppcQTTempCoeffY;
142  delete[] m_ppcQTTempCoeffCb;
143  delete[] m_ppcQTTempCoeffCr;
144  delete[] m_pcQTTempCoeffY;
145  delete[] m_pcQTTempCoeffCb;
146  delete[] m_pcQTTempCoeffCr;
147#if ADAPTIVE_QP_SELECTION
148  delete[] m_ppcQTTempArlCoeffY;
149  delete[] m_ppcQTTempArlCoeffCb;
150  delete[] m_ppcQTTempArlCoeffCr;
151  delete[] m_pcQTTempArlCoeffY;
152  delete[] m_pcQTTempArlCoeffCb;
153  delete[] m_pcQTTempArlCoeffCr;
154#endif
155  delete[] m_puhQTTempTrIdx;
156  delete[] m_puhQTTempCbf[0];
157  delete[] m_puhQTTempCbf[1];
158  delete[] m_puhQTTempCbf[2];
159  delete[] m_pcQTTempTComYuv;
160  delete[] m_pSharedPredTransformSkip[0];
161  delete[] m_pSharedPredTransformSkip[1];
162  delete[] m_pSharedPredTransformSkip[2];
163  delete[] m_pcQTTempTUCoeffY;
164  delete[] m_pcQTTempTUCoeffCb;
165  delete[] m_pcQTTempTUCoeffCr;
166#if ADAPTIVE_QP_SELECTION
167  delete[] m_ppcQTTempTUArlCoeffY;
168  delete[] m_ppcQTTempTUArlCoeffCb;
169  delete[] m_ppcQTTempTUArlCoeffCr;
170#endif
171  delete[] m_puhQTTempTransformSkipFlag[0];
172  delete[] m_puhQTTempTransformSkipFlag[1];
173  delete[] m_puhQTTempTransformSkipFlag[2];
174  m_pcQTTempTransformSkipTComYuv.destroy();
175  m_tmpYuvPred.destroy();
176}
177
178void TEncSearch::init(TEncCfg*      pcEncCfg,
179                      TComTrQuant*  pcTrQuant,
180                      Int           iSearchRange,
181                      Int           bipredSearchRange,
182                      Int           iFastSearch,
183                      Int           iMaxDeltaQP,
184                      TEncEntropy*  pcEntropyCoder,
185                      TComRdCost*   pcRdCost,
186                      TEncSbac*** pppcRDSbacCoder,
187                      TEncSbac*   pcRDGoOnSbacCoder
188                      )
189{
190  m_pcEncCfg             = pcEncCfg;
191  m_pcTrQuant            = pcTrQuant;
192  m_iSearchRange         = iSearchRange;
193  m_bipredSearchRange    = bipredSearchRange;
194  m_iFastSearch          = iFastSearch;
195  m_iMaxDeltaQP          = iMaxDeltaQP;
196  m_pcEntropyCoder       = pcEntropyCoder;
197  m_pcRdCost             = pcRdCost;
198 
199  m_pppcRDSbacCoder     = pppcRDSbacCoder;
200  m_pcRDGoOnSbacCoder   = pcRDGoOnSbacCoder;
201 
202  m_bUseSBACRD          = pppcRDSbacCoder ? true : false;
203 
204  for (Int iDir = 0; iDir < 2; iDir++)
205  {
206    for (Int iRefIdx = 0; iRefIdx < 33; iRefIdx++)
207    {
208      m_aaiAdaptSR[iDir][iRefIdx] = iSearchRange;
209    }
210  }
211 
212  m_puiDFilter = s_auiDFilter + 4;
213 
214  // initialize motion cost
215#if !FIX203
216  m_pcRdCost->initRateDistortionModel( m_iSearchRange << 2 );
217#endif
218 
219  for( Int iNum = 0; iNum < AMVP_MAX_NUM_CANDS+1; iNum++)
220  {
221    for( Int iIdx = 0; iIdx < AMVP_MAX_NUM_CANDS; iIdx++)
222    {
223      if (iIdx < iNum)
224        m_auiMVPIdxCost[iIdx][iNum] = xGetMvpIdxBits(iIdx, iNum);
225      else
226        m_auiMVPIdxCost[iIdx][iNum] = MAX_INT;
227    }
228  }
229 
230  initTempBuff();
231 
232  m_pTempPel = new Pel[g_uiMaxCUWidth*g_uiMaxCUHeight];
233 
234  const UInt uiNumLayersToAllocate = pcEncCfg->getQuadtreeTULog2MaxSize()-pcEncCfg->getQuadtreeTULog2MinSize()+1;
235  m_ppcQTTempCoeffY  = new TCoeff*[uiNumLayersToAllocate];
236  m_ppcQTTempCoeffCb = new TCoeff*[uiNumLayersToAllocate];
237  m_ppcQTTempCoeffCr = new TCoeff*[uiNumLayersToAllocate];
238  m_pcQTTempCoeffY   = new TCoeff [g_uiMaxCUWidth*g_uiMaxCUHeight   ];
239  m_pcQTTempCoeffCb  = new TCoeff [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
240  m_pcQTTempCoeffCr  = new TCoeff [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
241#if ADAPTIVE_QP_SELECTION
242  m_ppcQTTempArlCoeffY  = new Int*[uiNumLayersToAllocate];
243  m_ppcQTTempArlCoeffCb = new Int*[uiNumLayersToAllocate];
244  m_ppcQTTempArlCoeffCr = new Int*[uiNumLayersToAllocate];
245  m_pcQTTempArlCoeffY   = new Int [g_uiMaxCUWidth*g_uiMaxCUHeight   ];
246  m_pcQTTempArlCoeffCb  = new Int [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
247  m_pcQTTempArlCoeffCr  = new Int [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
248#endif
249 
250  const UInt uiNumPartitions = 1<<(g_uiMaxCUDepth<<1);
251  m_puhQTTempTrIdx   = new UChar  [uiNumPartitions];
252  m_puhQTTempCbf[0]  = new UChar  [uiNumPartitions];
253  m_puhQTTempCbf[1]  = new UChar  [uiNumPartitions];
254  m_puhQTTempCbf[2]  = new UChar  [uiNumPartitions];
255  m_pcQTTempTComYuv  = new TComYuv[uiNumLayersToAllocate];
256  for( UInt ui = 0; ui < uiNumLayersToAllocate; ++ui )
257  {
258    m_ppcQTTempCoeffY[ui]  = new TCoeff[g_uiMaxCUWidth*g_uiMaxCUHeight   ];
259    m_ppcQTTempCoeffCb[ui] = new TCoeff[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
260    m_ppcQTTempCoeffCr[ui] = new TCoeff[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
261#if ADAPTIVE_QP_SELECTION
262    m_ppcQTTempArlCoeffY[ui]  = new Int[g_uiMaxCUWidth*g_uiMaxCUHeight   ];
263    m_ppcQTTempArlCoeffCb[ui] = new Int[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
264    m_ppcQTTempArlCoeffCr[ui] = new Int[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
265#endif
266    m_pcQTTempTComYuv[ui].create( g_uiMaxCUWidth, g_uiMaxCUHeight );
267  }
268  m_pSharedPredTransformSkip[0] = new Pel[MAX_TS_WIDTH*MAX_TS_HEIGHT];
269  m_pSharedPredTransformSkip[1] = new Pel[MAX_TS_WIDTH*MAX_TS_HEIGHT];
270  m_pSharedPredTransformSkip[2] = new Pel[MAX_TS_WIDTH*MAX_TS_HEIGHT];
271  m_pcQTTempTUCoeffY  = new TCoeff[MAX_TS_WIDTH*MAX_TS_HEIGHT];
272  m_pcQTTempTUCoeffCb = new TCoeff[MAX_TS_WIDTH*MAX_TS_HEIGHT];
273  m_pcQTTempTUCoeffCr = new TCoeff[MAX_TS_WIDTH*MAX_TS_HEIGHT];
274#if ADAPTIVE_QP_SELECTION
275  m_ppcQTTempTUArlCoeffY  = new Int[MAX_TS_WIDTH*MAX_TS_HEIGHT];
276  m_ppcQTTempTUArlCoeffCb = new Int[MAX_TS_WIDTH*MAX_TS_HEIGHT];
277  m_ppcQTTempTUArlCoeffCr = new Int[MAX_TS_WIDTH*MAX_TS_HEIGHT];
278#endif
279  m_pcQTTempTransformSkipTComYuv.create( g_uiMaxCUWidth, g_uiMaxCUHeight );
280
281  m_puhQTTempTransformSkipFlag[0] = new UChar  [uiNumPartitions];
282  m_puhQTTempTransformSkipFlag[1] = new UChar  [uiNumPartitions];
283  m_puhQTTempTransformSkipFlag[2] = new UChar  [uiNumPartitions];
284  m_tmpYuvPred.create(MAX_CU_SIZE, MAX_CU_SIZE);
285}
286
287#if FASTME_SMOOTHER_MV
288#define FIRSTSEARCHSTOP     1
289#else
290#define FIRSTSEARCHSTOP     0
291#endif
292
293#define TZ_SEARCH_CONFIGURATION                                                                                 \
294const Int  iRaster                  = 5;  /* TZ soll von aussen ?ergeben werden */                            \
295const Bool bTestOtherPredictedMV    = 0;                                                                      \
296const Bool bTestZeroVector          = 1;                                                                      \
297const Bool bTestZeroVectorStart     = 0;                                                                      \
298const Bool bTestZeroVectorStop      = 0;                                                                      \
299const Bool bFirstSearchDiamond      = 1;  /* 1 = xTZ8PointDiamondSearch   0 = xTZ8PointSquareSearch */        \
300const Bool bFirstSearchStop         = FIRSTSEARCHSTOP;                                                        \
301const UInt uiFirstSearchRounds      = 3;  /* first search stop X rounds after best match (must be >=1) */     \
302const Bool bEnableRasterSearch      = 1;                                                                      \
303const Bool bAlwaysRasterSearch      = 0;  /* ===== 1: BETTER but factor 2 slower ===== */                     \
304const Bool bRasterRefinementEnable  = 0;  /* enable either raster refinement or star refinement */            \
305const Bool bRasterRefinementDiamond = 0;  /* 1 = xTZ8PointDiamondSearch   0 = xTZ8PointSquareSearch */        \
306const Bool bStarRefinementEnable    = 1;  /* enable either star refinement or raster refinement */            \
307const Bool bStarRefinementDiamond   = 1;  /* 1 = xTZ8PointDiamondSearch   0 = xTZ8PointSquareSearch */        \
308const Bool bStarRefinementStop      = 0;                                                                      \
309const UInt uiStarRefinementRounds   = 2;  /* star refinement stop X rounds after best match (must be >=1) */  \
310
311
312__inline Void TEncSearch::xTZSearchHelp( TComPattern* pcPatternKey, IntTZSearchStruct& rcStruct, const Int iSearchX, const Int iSearchY, const UChar ucPointNr, const UInt uiDistance )
313{
314  UInt  uiSad;
315 
316  Pel*  piRefSrch;
317 
318  piRefSrch = rcStruct.piRefY + iSearchY * rcStruct.iYStride + iSearchX;
319 
320  //-- jclee for using the SAD function pointer
321  m_pcRdCost->setDistParam( pcPatternKey, piRefSrch, rcStruct.iYStride,  m_cDistParam );
322 
323  // fast encoder decision: use subsampled SAD when rows > 8 for integer ME
324  if ( m_pcEncCfg->getUseFastEnc() )
325  {
326    if ( m_cDistParam.iRows > 8 )
327    {
328      m_cDistParam.iSubShift = 1;
329    }
330  }
331
332  setDistParamComp(0);  // Y component
333
334  // distortion
335  m_cDistParam.bitDepth = g_bitDepthY;
336  uiSad = m_cDistParam.DistFunc( &m_cDistParam );
337 
338  // motion cost
339  uiSad += m_pcRdCost->getCost( iSearchX, iSearchY );
340 
341  if( uiSad < rcStruct.uiBestSad )
342  {
343    rcStruct.uiBestSad      = uiSad;
344    rcStruct.iBestX         = iSearchX;
345    rcStruct.iBestY         = iSearchY;
346    rcStruct.uiBestDistance = uiDistance;
347    rcStruct.uiBestRound    = 0;
348    rcStruct.ucPointNr      = ucPointNr;
349  }
350}
351
352__inline Void TEncSearch::xTZ2PointSearch( TComPattern* pcPatternKey, IntTZSearchStruct& rcStruct, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB )
353{
354  Int   iSrchRngHorLeft   = pcMvSrchRngLT->getHor();
355  Int   iSrchRngHorRight  = pcMvSrchRngRB->getHor();
356  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
357  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
358 
359  // 2 point search,                   //   1 2 3
360  // check only the 2 untested points  //   4 0 5
361  // around the start point            //   6 7 8
362  Int iStartX = rcStruct.iBestX;
363  Int iStartY = rcStruct.iBestY;
364  switch( rcStruct.ucPointNr )
365  {
366    case 1:
367    {
368      if ( (iStartX - 1) >= iSrchRngHorLeft )
369      {
370        xTZSearchHelp( pcPatternKey, rcStruct, iStartX - 1, iStartY, 0, 2 );
371      }
372      if ( (iStartY - 1) >= iSrchRngVerTop )
373      {
374        xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iStartY - 1, 0, 2 );
375      }
376    }
377      break;
378    case 2:
379    {
380      if ( (iStartY - 1) >= iSrchRngVerTop )
381      {
382        if ( (iStartX - 1) >= iSrchRngHorLeft )
383        {
384          xTZSearchHelp( pcPatternKey, rcStruct, iStartX - 1, iStartY - 1, 0, 2 );
385        }
386        if ( (iStartX + 1) <= iSrchRngHorRight )
387        {
388          xTZSearchHelp( pcPatternKey, rcStruct, iStartX + 1, iStartY - 1, 0, 2 );
389        }
390      }
391    }
392      break;
393    case 3:
394    {
395      if ( (iStartY - 1) >= iSrchRngVerTop )
396      {
397        xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iStartY - 1, 0, 2 );
398      }
399      if ( (iStartX + 1) <= iSrchRngHorRight )
400      {
401        xTZSearchHelp( pcPatternKey, rcStruct, iStartX + 1, iStartY, 0, 2 );
402      }
403    }
404      break;
405    case 4:
406    {
407      if ( (iStartX - 1) >= iSrchRngHorLeft )
408      {
409        if ( (iStartY + 1) <= iSrchRngVerBottom )
410        {
411          xTZSearchHelp( pcPatternKey, rcStruct, iStartX - 1, iStartY + 1, 0, 2 );
412        }
413        if ( (iStartY - 1) >= iSrchRngVerTop )
414        {
415          xTZSearchHelp( pcPatternKey, rcStruct, iStartX - 1, iStartY - 1, 0, 2 );
416        }
417      }
418    }
419      break;
420    case 5:
421    {
422      if ( (iStartX + 1) <= iSrchRngHorRight )
423      {
424        if ( (iStartY - 1) >= iSrchRngVerTop )
425        {
426          xTZSearchHelp( pcPatternKey, rcStruct, iStartX + 1, iStartY - 1, 0, 2 );
427        }
428        if ( (iStartY + 1) <= iSrchRngVerBottom )
429        {
430          xTZSearchHelp( pcPatternKey, rcStruct, iStartX + 1, iStartY + 1, 0, 2 );
431        }
432      }
433    }
434      break;
435    case 6:
436    {
437      if ( (iStartX - 1) >= iSrchRngHorLeft )
438      {
439        xTZSearchHelp( pcPatternKey, rcStruct, iStartX - 1, iStartY , 0, 2 );
440      }
441      if ( (iStartY + 1) <= iSrchRngVerBottom )
442      {
443        xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iStartY + 1, 0, 2 );
444      }
445    }
446      break;
447    case 7:
448    {
449      if ( (iStartY + 1) <= iSrchRngVerBottom )
450      {
451        if ( (iStartX - 1) >= iSrchRngHorLeft )
452        {
453          xTZSearchHelp( pcPatternKey, rcStruct, iStartX - 1, iStartY + 1, 0, 2 );
454        }
455        if ( (iStartX + 1) <= iSrchRngHorRight )
456        {
457          xTZSearchHelp( pcPatternKey, rcStruct, iStartX + 1, iStartY + 1, 0, 2 );
458        }
459      }
460    }
461      break;
462    case 8:
463    {
464      if ( (iStartX + 1) <= iSrchRngHorRight )
465      {
466        xTZSearchHelp( pcPatternKey, rcStruct, iStartX + 1, iStartY, 0, 2 );
467      }
468      if ( (iStartY + 1) <= iSrchRngVerBottom )
469      {
470        xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iStartY + 1, 0, 2 );
471      }
472    }
473      break;
474    default:
475    {
476      assert( false );
477    }
478      break;
479  } // switch( rcStruct.ucPointNr )
480}
481
482__inline Void TEncSearch::xTZ8PointSquareSearch( TComPattern* pcPatternKey, IntTZSearchStruct& rcStruct, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, const Int iStartX, const Int iStartY, const Int iDist )
483{
484  Int   iSrchRngHorLeft   = pcMvSrchRngLT->getHor();
485  Int   iSrchRngHorRight  = pcMvSrchRngRB->getHor();
486  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
487  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
488 
489  // 8 point search,                   //   1 2 3
490  // search around the start point     //   4 0 5
491  // with the required  distance       //   6 7 8
492  assert( iDist != 0 );
493  const Int iTop        = iStartY - iDist;
494  const Int iBottom     = iStartY + iDist;
495  const Int iLeft       = iStartX - iDist;
496  const Int iRight      = iStartX + iDist;
497  rcStruct.uiBestRound += 1;
498 
499  if ( iTop >= iSrchRngVerTop ) // check top
500  {
501    if ( iLeft >= iSrchRngHorLeft ) // check top left
502    {
503      xTZSearchHelp( pcPatternKey, rcStruct, iLeft, iTop, 1, iDist );
504    }
505    // top middle
506    xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iTop, 2, iDist );
507   
508    if ( iRight <= iSrchRngHorRight ) // check top right
509    {
510      xTZSearchHelp( pcPatternKey, rcStruct, iRight, iTop, 3, iDist );
511    }
512  } // check top
513  if ( iLeft >= iSrchRngHorLeft ) // check middle left
514  {
515    xTZSearchHelp( pcPatternKey, rcStruct, iLeft, iStartY, 4, iDist );
516  }
517  if ( iRight <= iSrchRngHorRight ) // check middle right
518  {
519    xTZSearchHelp( pcPatternKey, rcStruct, iRight, iStartY, 5, iDist );
520  }
521  if ( iBottom <= iSrchRngVerBottom ) // check bottom
522  {
523    if ( iLeft >= iSrchRngHorLeft ) // check bottom left
524    {
525      xTZSearchHelp( pcPatternKey, rcStruct, iLeft, iBottom, 6, iDist );
526    }
527    // check bottom middle
528    xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iBottom, 7, iDist );
529   
530    if ( iRight <= iSrchRngHorRight ) // check bottom right
531    {
532      xTZSearchHelp( pcPatternKey, rcStruct, iRight, iBottom, 8, iDist );
533    }
534  } // check bottom
535}
536
537__inline Void TEncSearch::xTZ8PointDiamondSearch( TComPattern* pcPatternKey, IntTZSearchStruct& rcStruct, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, const Int iStartX, const Int iStartY, const Int iDist )
538{
539  Int   iSrchRngHorLeft   = pcMvSrchRngLT->getHor();
540  Int   iSrchRngHorRight  = pcMvSrchRngRB->getHor();
541  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
542  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
543 
544  // 8 point search,                   //   1 2 3
545  // search around the start point     //   4 0 5
546  // with the required  distance       //   6 7 8
547  assert ( iDist != 0 );
548  const Int iTop        = iStartY - iDist;
549  const Int iBottom     = iStartY + iDist;
550  const Int iLeft       = iStartX - iDist;
551  const Int iRight      = iStartX + iDist;
552  rcStruct.uiBestRound += 1;
553 
554  if ( iDist == 1 ) // iDist == 1
555  {
556    if ( iTop >= iSrchRngVerTop ) // check top
557    {
558      xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iTop, 2, iDist );
559    }
560    if ( iLeft >= iSrchRngHorLeft ) // check middle left
561    {
562      xTZSearchHelp( pcPatternKey, rcStruct, iLeft, iStartY, 4, iDist );
563    }
564    if ( iRight <= iSrchRngHorRight ) // check middle right
565    {
566      xTZSearchHelp( pcPatternKey, rcStruct, iRight, iStartY, 5, iDist );
567    }
568    if ( iBottom <= iSrchRngVerBottom ) // check bottom
569    {
570      xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iBottom, 7, iDist );
571    }
572  }
573  else // if (iDist != 1)
574  {
575    if ( iDist <= 8 )
576    {
577      const Int iTop_2      = iStartY - (iDist>>1);
578      const Int iBottom_2   = iStartY + (iDist>>1);
579      const Int iLeft_2     = iStartX - (iDist>>1);
580      const Int iRight_2    = iStartX + (iDist>>1);
581     
582      if (  iTop >= iSrchRngVerTop && iLeft >= iSrchRngHorLeft &&
583          iRight <= iSrchRngHorRight && iBottom <= iSrchRngVerBottom ) // check border
584      {
585        xTZSearchHelp( pcPatternKey, rcStruct, iStartX,  iTop,      2, iDist    );
586        xTZSearchHelp( pcPatternKey, rcStruct, iLeft_2,  iTop_2,    1, iDist>>1 );
587        xTZSearchHelp( pcPatternKey, rcStruct, iRight_2, iTop_2,    3, iDist>>1 );
588        xTZSearchHelp( pcPatternKey, rcStruct, iLeft,    iStartY,   4, iDist    );
589        xTZSearchHelp( pcPatternKey, rcStruct, iRight,   iStartY,   5, iDist    );
590        xTZSearchHelp( pcPatternKey, rcStruct, iLeft_2,  iBottom_2, 6, iDist>>1 );
591        xTZSearchHelp( pcPatternKey, rcStruct, iRight_2, iBottom_2, 8, iDist>>1 );
592        xTZSearchHelp( pcPatternKey, rcStruct, iStartX,  iBottom,   7, iDist    );
593      }
594      else // check border
595      {
596        if ( iTop >= iSrchRngVerTop ) // check top
597        {
598          xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iTop, 2, iDist );
599        }
600        if ( iTop_2 >= iSrchRngVerTop ) // check half top
601        {
602          if ( iLeft_2 >= iSrchRngHorLeft ) // check half left
603          {
604            xTZSearchHelp( pcPatternKey, rcStruct, iLeft_2, iTop_2, 1, (iDist>>1) );
605          }
606          if ( iRight_2 <= iSrchRngHorRight ) // check half right
607          {
608            xTZSearchHelp( pcPatternKey, rcStruct, iRight_2, iTop_2, 3, (iDist>>1) );
609          }
610        } // check half top
611        if ( iLeft >= iSrchRngHorLeft ) // check left
612        {
613          xTZSearchHelp( pcPatternKey, rcStruct, iLeft, iStartY, 4, iDist );
614        }
615        if ( iRight <= iSrchRngHorRight ) // check right
616        {
617          xTZSearchHelp( pcPatternKey, rcStruct, iRight, iStartY, 5, iDist );
618        }
619        if ( iBottom_2 <= iSrchRngVerBottom ) // check half bottom
620        {
621          if ( iLeft_2 >= iSrchRngHorLeft ) // check half left
622          {
623            xTZSearchHelp( pcPatternKey, rcStruct, iLeft_2, iBottom_2, 6, (iDist>>1) );
624          }
625          if ( iRight_2 <= iSrchRngHorRight ) // check half right
626          {
627            xTZSearchHelp( pcPatternKey, rcStruct, iRight_2, iBottom_2, 8, (iDist>>1) );
628          }
629        } // check half bottom
630        if ( iBottom <= iSrchRngVerBottom ) // check bottom
631        {
632          xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iBottom, 7, iDist );
633        }
634      } // check border
635    }
636    else // iDist > 8
637    {
638      if ( iTop >= iSrchRngVerTop && iLeft >= iSrchRngHorLeft &&
639          iRight <= iSrchRngHorRight && iBottom <= iSrchRngVerBottom ) // check border
640      {
641        xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iTop,    0, iDist );
642        xTZSearchHelp( pcPatternKey, rcStruct, iLeft,   iStartY, 0, iDist );
643        xTZSearchHelp( pcPatternKey, rcStruct, iRight,  iStartY, 0, iDist );
644        xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iBottom, 0, iDist );
645        for ( Int index = 1; index < 4; index++ )
646        {
647          Int iPosYT = iTop    + ((iDist>>2) * index);
648          Int iPosYB = iBottom - ((iDist>>2) * index);
649          Int iPosXL = iStartX - ((iDist>>2) * index);
650          Int iPosXR = iStartX + ((iDist>>2) * index);
651          xTZSearchHelp( pcPatternKey, rcStruct, iPosXL, iPosYT, 0, iDist );
652          xTZSearchHelp( pcPatternKey, rcStruct, iPosXR, iPosYT, 0, iDist );
653          xTZSearchHelp( pcPatternKey, rcStruct, iPosXL, iPosYB, 0, iDist );
654          xTZSearchHelp( pcPatternKey, rcStruct, iPosXR, iPosYB, 0, iDist );
655        }
656      }
657      else // check border
658      {
659        if ( iTop >= iSrchRngVerTop ) // check top
660        {
661          xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iTop, 0, iDist );
662        }
663        if ( iLeft >= iSrchRngHorLeft ) // check left
664        {
665          xTZSearchHelp( pcPatternKey, rcStruct, iLeft, iStartY, 0, iDist );
666        }
667        if ( iRight <= iSrchRngHorRight ) // check right
668        {
669          xTZSearchHelp( pcPatternKey, rcStruct, iRight, iStartY, 0, iDist );
670        }
671        if ( iBottom <= iSrchRngVerBottom ) // check bottom
672        {
673          xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iBottom, 0, iDist );
674        }
675        for ( Int index = 1; index < 4; index++ )
676        {
677          Int iPosYT = iTop    + ((iDist>>2) * index);
678          Int iPosYB = iBottom - ((iDist>>2) * index);
679          Int iPosXL = iStartX - ((iDist>>2) * index);
680          Int iPosXR = iStartX + ((iDist>>2) * index);
681         
682          if ( iPosYT >= iSrchRngVerTop ) // check top
683          {
684            if ( iPosXL >= iSrchRngHorLeft ) // check left
685            {
686              xTZSearchHelp( pcPatternKey, rcStruct, iPosXL, iPosYT, 0, iDist );
687            }
688            if ( iPosXR <= iSrchRngHorRight ) // check right
689            {
690              xTZSearchHelp( pcPatternKey, rcStruct, iPosXR, iPosYT, 0, iDist );
691            }
692          } // check top
693          if ( iPosYB <= iSrchRngVerBottom ) // check bottom
694          {
695            if ( iPosXL >= iSrchRngHorLeft ) // check left
696            {
697              xTZSearchHelp( pcPatternKey, rcStruct, iPosXL, iPosYB, 0, iDist );
698            }
699            if ( iPosXR <= iSrchRngHorRight ) // check right
700            {
701              xTZSearchHelp( pcPatternKey, rcStruct, iPosXR, iPosYB, 0, iDist );
702            }
703          } // check bottom
704        } // for ...
705      } // check border
706    } // iDist <= 8
707  } // iDist == 1
708}
709
710//<--
711
712UInt TEncSearch::xPatternRefinement( TComPattern* pcPatternKey,
713                                    TComMv baseRefMv,
714                                    Int iFrac, TComMv& rcMvFrac )
715{
716  UInt  uiDist;
717  UInt  uiDistBest  = MAX_UINT;
718  UInt  uiDirecBest = 0;
719 
720  Pel*  piRefPos;
721  Int iRefStride = m_filteredBlock[0][0].getStride();
722#if NS_HAD
723  m_pcRdCost->setDistParam( pcPatternKey, m_filteredBlock[0][0].getLumaAddr(), iRefStride, 1, m_cDistParam, m_pcEncCfg->getUseHADME(), m_pcEncCfg->getUseNSQT() );
724#else
725  m_pcRdCost->setDistParam( pcPatternKey, m_filteredBlock[0][0].getLumaAddr(), iRefStride, 1, m_cDistParam, m_pcEncCfg->getUseHADME() );
726#endif
727 
728  const TComMv* pcMvRefine = (iFrac == 2 ? s_acMvRefineH : s_acMvRefineQ);
729 
730  for (UInt i = 0; i < 9; i++)
731  {
732    TComMv cMvTest = pcMvRefine[i];
733    cMvTest += baseRefMv;
734   
735    Int horVal = cMvTest.getHor() * iFrac;
736    Int verVal = cMvTest.getVer() * iFrac;
737    piRefPos = m_filteredBlock[ verVal & 3 ][ horVal & 3 ].getLumaAddr();
738    if ( horVal == 2 && ( verVal & 1 ) == 0 )
739      piRefPos += 1;
740    if ( ( horVal & 1 ) == 0 && verVal == 2 )
741      piRefPos += iRefStride;
742    cMvTest = pcMvRefine[i];
743    cMvTest += rcMvFrac;
744
745    setDistParamComp(0);  // Y component
746
747    m_cDistParam.pCur = piRefPos;
748    m_cDistParam.bitDepth = g_bitDepthY;
749    uiDist = m_cDistParam.DistFunc( &m_cDistParam );
750    uiDist += m_pcRdCost->getCost( cMvTest.getHor(), cMvTest.getVer() );
751   
752    if ( uiDist < uiDistBest )
753    {
754      uiDistBest  = uiDist;
755      uiDirecBest = i;
756    }
757  }
758 
759  rcMvFrac = pcMvRefine[uiDirecBest];
760 
761  return uiDistBest;
762}
763
764Void
765TEncSearch::xEncSubdivCbfQT( TComDataCU*  pcCU,
766                            UInt         uiTrDepth,
767                            UInt         uiAbsPartIdx,
768                            Bool         bLuma,
769                            Bool         bChroma )
770{
771  UInt  uiFullDepth     = pcCU->getDepth(0) + uiTrDepth;
772  UInt  uiTrMode        = pcCU->getTransformIdx( uiAbsPartIdx );
773  UInt  uiSubdiv        = ( uiTrMode > uiTrDepth ? 1 : 0 );
774  UInt  uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - uiFullDepth;
775
776  {
777    if( pcCU->getPredictionMode(0) == MODE_INTRA && pcCU->getPartitionSize(0) == SIZE_NxN && uiTrDepth == 0 )
778    {
779      assert( uiSubdiv );
780    }
781    else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
782    {
783      assert( uiSubdiv );
784    }
785    else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
786    {
787      assert( !uiSubdiv );
788    }
789    else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
790    {
791      assert( !uiSubdiv );
792    }
793    else
794    {
795      assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
796      if( bLuma )
797      {
798        m_pcEntropyCoder->encodeTransformSubdivFlag( uiSubdiv, 5 - uiLog2TrafoSize );
799      }
800    }
801  }
802 
803  if ( bChroma )
804  {
805    if( uiLog2TrafoSize > 2 )
806    {
807      if( uiTrDepth==0 || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth-1 ) )
808        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth );
809      if( uiTrDepth==0 || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth-1 ) )
810        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth );
811    }
812  }
813
814  if( uiSubdiv )
815  {
816    UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
817    for( UInt uiPart = 0; uiPart < 4; uiPart++ )
818    {
819      xEncSubdivCbfQT( pcCU, uiTrDepth + 1, uiAbsPartIdx + uiPart * uiQPartNum, bLuma, bChroma );
820    }
821    return;
822  }
823 
824  {
825    //===== Cbfs =====
826    if( bLuma )
827    {
828      m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
829    }
830  }
831}
832
833Void
834TEncSearch::xEncCoeffQT( TComDataCU*  pcCU,
835                        UInt         uiTrDepth,
836                        UInt         uiAbsPartIdx,
837                        TextType     eTextType,
838                        Bool         bRealCoeff )
839{
840  UInt  uiFullDepth     = pcCU->getDepth(0) + uiTrDepth;
841  UInt  uiTrMode        = pcCU->getTransformIdx( uiAbsPartIdx );
842  UInt  uiSubdiv        = ( uiTrMode > uiTrDepth ? 1 : 0 );
843  UInt  uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - uiFullDepth;
844  UInt  uiChroma        = ( eTextType != TEXT_LUMA ? 1 : 0 );
845 
846  if( uiSubdiv )
847  {
848    UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
849      for( UInt uiPart = 0; uiPart < 4; uiPart++ )
850      {
851        xEncCoeffQT( pcCU, uiTrDepth + 1, uiAbsPartIdx + uiPart * uiQPartNum, eTextType, bRealCoeff );
852      }
853    return;
854  }
855 
856  if( eTextType != TEXT_LUMA && uiLog2TrafoSize == 2 )
857  {
858    assert( uiTrDepth > 0 );
859    uiTrDepth--;
860    UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth ) << 1 );
861    Bool bFirstQ = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
862    if( !bFirstQ )
863    {
864      return;
865    }
866  }
867 
868  //===== coefficients =====
869  UInt    uiWidth         = pcCU->getWidth  ( 0 ) >> ( uiTrDepth + uiChroma );
870  UInt    uiHeight        = pcCU->getHeight ( 0 ) >> ( uiTrDepth + uiChroma );
871  UInt    uiCoeffOffset   = ( pcCU->getPic()->getMinCUWidth() * pcCU->getPic()->getMinCUHeight() * uiAbsPartIdx ) >> ( uiChroma << 1 );
872  UInt    uiQTLayer       = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrafoSize;
873  TCoeff* pcCoeff         = 0;
874  switch( eTextType )
875  {
876    case TEXT_LUMA:     pcCoeff = ( bRealCoeff ? pcCU->getCoeffY () : m_ppcQTTempCoeffY [uiQTLayer] );  break;
877    case TEXT_CHROMA_U: pcCoeff = ( bRealCoeff ? pcCU->getCoeffCb() : m_ppcQTTempCoeffCb[uiQTLayer] );  break;
878    case TEXT_CHROMA_V: pcCoeff = ( bRealCoeff ? pcCU->getCoeffCr() : m_ppcQTTempCoeffCr[uiQTLayer] );  break;
879    default:            assert(0);
880  }
881  pcCoeff += uiCoeffOffset;
882 
883  m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiWidth, uiHeight, uiFullDepth, eTextType );
884}
885
886
887Void
888TEncSearch::xEncIntraHeader( TComDataCU*  pcCU,
889                            UInt         uiTrDepth,
890                            UInt         uiAbsPartIdx,
891                            Bool         bLuma,
892                            Bool         bChroma )
893{
894  if( bLuma )
895  {
896    // CU header
897    if( uiAbsPartIdx == 0 )
898    {
899      if( !pcCU->getSlice()->isIntra() )
900      {
901        if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
902        {
903          m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, 0, true );
904        }
905        m_pcEntropyCoder->encodeSkipFlag( pcCU, 0, true );
906        m_pcEntropyCoder->encodePredMode( pcCU, 0, true );
907      }
908#if INTRA_BL
909      m_pcEntropyCoder->encodeIntraBLFlag ( pcCU, 0, true );
910      if( pcCU->isIntraBL( 0 ) )
911      {
912        return;
913      }
914#endif     
915      m_pcEntropyCoder  ->encodePartSize( pcCU, 0, pcCU->getDepth(0), true );
916
917      if (pcCU->isIntra(0) && pcCU->getPartitionSize(0) == SIZE_2Nx2N )
918      {
919        m_pcEntropyCoder->encodeIPCMInfo( pcCU, 0, true );
920
921        if ( pcCU->getIPCMFlag (0))
922        {
923          return;
924        }
925      }
926    }
927    // luma prediction mode
928    if( pcCU->getPartitionSize(0) == SIZE_2Nx2N )
929    {
930      if( uiAbsPartIdx == 0 )
931      {
932        m_pcEntropyCoder->encodeIntraDirModeLuma ( pcCU, 0 );
933      }
934    }
935    else
936    {
937      UInt uiQNumParts = pcCU->getTotalNumPart() >> 2;
938      if( uiTrDepth == 0 )
939      {
940        assert( uiAbsPartIdx == 0 );
941        for( UInt uiPart = 0; uiPart < 4; uiPart++ )
942        {
943          m_pcEntropyCoder->encodeIntraDirModeLuma ( pcCU, uiPart * uiQNumParts );
944        }
945      }
946      else if( ( uiAbsPartIdx % uiQNumParts ) == 0 )
947      {
948        m_pcEntropyCoder->encodeIntraDirModeLuma ( pcCU, uiAbsPartIdx );
949      }
950    }
951  }
952#if INTRA_BL
953  if( pcCU->isIntraBL( 0 ) )
954  {
955    return;
956  }
957#endif
958  if( bChroma )
959  {
960    // chroma prediction mode
961    if( uiAbsPartIdx == 0 )
962    {
963      m_pcEntropyCoder->encodeIntraDirModeChroma( pcCU, 0, true );
964    }
965  }
966}
967
968
969UInt
970TEncSearch::xGetIntraBitsQT( TComDataCU*  pcCU,
971                            UInt         uiTrDepth,
972                            UInt         uiAbsPartIdx,
973                            Bool         bLuma,
974                            Bool         bChroma,
975                            Bool         bRealCoeff /* just for test */ )
976{
977  m_pcEntropyCoder->resetBits();
978  xEncIntraHeader ( pcCU, uiTrDepth, uiAbsPartIdx, bLuma, bChroma );
979  xEncSubdivCbfQT ( pcCU, uiTrDepth, uiAbsPartIdx, bLuma, bChroma );
980 
981  if( bLuma )
982  {
983    xEncCoeffQT   ( pcCU, uiTrDepth, uiAbsPartIdx, TEXT_LUMA,      bRealCoeff );
984  }
985  if( bChroma )
986  {
987    xEncCoeffQT   ( pcCU, uiTrDepth, uiAbsPartIdx, TEXT_CHROMA_U,  bRealCoeff );
988    xEncCoeffQT   ( pcCU, uiTrDepth, uiAbsPartIdx, TEXT_CHROMA_V,  bRealCoeff );
989  }
990  UInt   uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
991  return uiBits;
992}
993
994UInt
995TEncSearch::xGetIntraBitsQTChroma( TComDataCU*  pcCU,
996                                  UInt         uiTrDepth,
997                                  UInt         uiAbsPartIdx,
998                                  UInt         uiChromaId,
999                                  Bool         bRealCoeff /* just for test */ )
1000{
1001  m_pcEntropyCoder->resetBits();
1002  if( uiChromaId == TEXT_CHROMA_U)
1003  {
1004    xEncCoeffQT   ( pcCU, uiTrDepth, uiAbsPartIdx, TEXT_CHROMA_U,  bRealCoeff );
1005  }
1006  else if(uiChromaId == TEXT_CHROMA_V)
1007  {
1008    xEncCoeffQT   ( pcCU, uiTrDepth, uiAbsPartIdx, TEXT_CHROMA_V,  bRealCoeff );
1009  }
1010
1011  UInt   uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1012  return uiBits;
1013}
1014
1015Void
1016TEncSearch::xIntraCodingLumaBlk( TComDataCU* pcCU,
1017                                UInt        uiTrDepth,
1018                                UInt        uiAbsPartIdx,
1019                                TComYuv*    pcOrgYuv, 
1020                                TComYuv*    pcPredYuv, 
1021                                TComYuv*    pcResiYuv, 
1022                                UInt&       ruiDist,
1023                                Int        default0Save1Load2 )
1024{
1025  UInt    uiLumaPredMode    = pcCU     ->getLumaIntraDir     ( uiAbsPartIdx );
1026  UInt    uiFullDepth       = pcCU     ->getDepth   ( 0 )  + uiTrDepth;
1027  UInt    uiWidth           = pcCU     ->getWidth   ( 0 ) >> uiTrDepth;
1028  UInt    uiHeight          = pcCU     ->getHeight  ( 0 ) >> uiTrDepth;
1029  UInt    uiStride          = pcOrgYuv ->getStride  ();
1030  Pel*    piOrg             = pcOrgYuv ->getLumaAddr( uiAbsPartIdx );
1031  Pel*    piPred            = pcPredYuv->getLumaAddr( uiAbsPartIdx );
1032  Pel*    piResi            = pcResiYuv->getLumaAddr( uiAbsPartIdx );
1033  Pel*    piReco            = pcPredYuv->getLumaAddr( uiAbsPartIdx );
1034 
1035  UInt    uiLog2TrSize      = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
1036  UInt    uiQTLayer         = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
1037  UInt    uiNumCoeffPerInc  = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
1038  TCoeff* pcCoeff           = m_ppcQTTempCoeffY[ uiQTLayer ] + uiNumCoeffPerInc * uiAbsPartIdx;
1039#if ADAPTIVE_QP_SELECTION
1040  Int*    pcArlCoeff        = m_ppcQTTempArlCoeffY[ uiQTLayer ] + uiNumCoeffPerInc * uiAbsPartIdx;
1041#endif
1042  Pel*    piRecQt           = m_pcQTTempTComYuv[ uiQTLayer ].getLumaAddr( uiAbsPartIdx );
1043  UInt    uiRecQtStride     = m_pcQTTempTComYuv[ uiQTLayer ].getStride  ();
1044 
1045  UInt    uiZOrder          = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
1046  Pel*    piRecIPred        = pcCU->getPic()->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), uiZOrder );
1047  UInt    uiRecIPredStride  = pcCU->getPic()->getPicYuvRec()->getStride  ();
1048  Bool    useTransformSkip  = pcCU->getTransformSkip(uiAbsPartIdx, TEXT_LUMA);
1049  //===== init availability pattern =====
1050  Bool  bAboveAvail = false;
1051  Bool  bLeftAvail  = false;
1052  if(default0Save1Load2 != 2)
1053  {
1054    pcCU->getPattern()->initPattern   ( pcCU, uiTrDepth, uiAbsPartIdx );
1055    pcCU->getPattern()->initAdiPattern( pcCU, uiAbsPartIdx, uiTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
1056    //===== get prediction signal =====
1057#if INTRA_BL && !NO_RESIDUAL_FLAG_FOR_BLPRED
1058    if(pcCU->isIntraBL ( uiAbsPartIdx ) )
1059    {
1060      pcCU->getBaseLumaBlk( uiWidth, uiHeight, uiAbsPartIdx, piPred, uiStride );
1061    }
1062    else
1063#endif
1064    predIntraLumaAng( pcCU->getPattern(), uiLumaPredMode, piPred, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
1065    // save prediction
1066    if(default0Save1Load2 == 1)
1067    {
1068      Pel*  pPred   = piPred;
1069      Pel*  pPredBuf = m_pSharedPredTransformSkip[0];
1070      Int k = 0;
1071      for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1072      {
1073        for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1074        {
1075          pPredBuf[ k ++ ] = pPred[ uiX ];
1076        }
1077        pPred += uiStride;
1078      }
1079    }
1080  }
1081  else 
1082  {
1083    // load prediction
1084    Pel*  pPred   = piPred;
1085    Pel*  pPredBuf = m_pSharedPredTransformSkip[0];
1086    Int k = 0;
1087    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1088    {
1089      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1090      {
1091        pPred[ uiX ] = pPredBuf[ k ++ ];
1092      }
1093      pPred += uiStride;
1094    }
1095  }
1096  //===== get residual signal =====
1097  {
1098    // get residual
1099    Pel*  pOrg    = piOrg;
1100    Pel*  pPred   = piPred;
1101    Pel*  pResi   = piResi;
1102    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1103    {
1104      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1105      {
1106        pResi[ uiX ] = pOrg[ uiX ] - pPred[ uiX ];
1107      }
1108      pOrg  += uiStride;
1109      pResi += uiStride;
1110      pPred += uiStride;
1111    }
1112  }
1113 
1114  //===== transform and quantization =====
1115  //--- init rate estimation arrays for RDOQ ---
1116  if( useTransformSkip? m_pcEncCfg->getUseRDOQTS():m_pcEncCfg->getUseRDOQ())
1117  {
1118    m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, uiWidth, TEXT_LUMA );
1119  }
1120  //--- transform and quantization ---
1121  UInt uiAbsSum = 0;
1122  pcCU       ->setTrIdxSubParts ( uiTrDepth, uiAbsPartIdx, uiFullDepth );
1123
1124  m_pcTrQuant->setQPforQuant    ( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
1125
1126#if RDOQ_CHROMA_LAMBDA
1127  m_pcTrQuant->selectLambda     (TEXT_LUMA); 
1128#endif
1129
1130  m_pcTrQuant->transformNxN     ( pcCU, piResi, uiStride, pcCoeff, 
1131#if ADAPTIVE_QP_SELECTION
1132    pcArlCoeff, 
1133#endif
1134    uiWidth, uiHeight, uiAbsSum, TEXT_LUMA, uiAbsPartIdx,useTransformSkip );
1135 
1136  //--- set coded block flag ---
1137  pcCU->setCbfSubParts          ( ( uiAbsSum ? 1 : 0 ) << uiTrDepth, TEXT_LUMA, uiAbsPartIdx, uiFullDepth );
1138  //--- inverse transform ---
1139  if( uiAbsSum )
1140  {
1141    Int scalingListType = 0 + g_eTTable[(Int)TEXT_LUMA];
1142    assert(scalingListType < 6);
1143    m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,pcCU->getLumaIntraDir( uiAbsPartIdx ), piResi, uiStride, pcCoeff, uiWidth, uiHeight, scalingListType, useTransformSkip );
1144  }
1145  else
1146  {
1147    Pel* pResi = piResi;
1148    memset( pcCoeff, 0, sizeof( TCoeff ) * uiWidth * uiHeight );
1149    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1150    {
1151      memset( pResi, 0, sizeof( Pel ) * uiWidth );
1152      pResi += uiStride;
1153    }
1154  }
1155 
1156  //===== reconstruction =====
1157  {
1158    Pel* pPred      = piPred;
1159    Pel* pResi      = piResi;
1160    Pel* pReco      = piReco;
1161    Pel* pRecQt     = piRecQt;
1162    Pel* pRecIPred  = piRecIPred;
1163    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1164    {
1165      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1166      {
1167        pReco    [ uiX ] = ClipY( pPred[ uiX ] + pResi[ uiX ] );
1168        pRecQt   [ uiX ] = pReco[ uiX ];
1169        pRecIPred[ uiX ] = pReco[ uiX ];
1170      }
1171      pPred     += uiStride;
1172      pResi     += uiStride;
1173      pReco     += uiStride;
1174      pRecQt    += uiRecQtStride;
1175      pRecIPred += uiRecIPredStride;
1176    }
1177  }
1178 
1179  //===== update distortion =====
1180  ruiDist += m_pcRdCost->getDistPart(g_bitDepthY, piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
1181}
1182
1183Void
1184TEncSearch::xIntraCodingChromaBlk( TComDataCU* pcCU,
1185                                  UInt        uiTrDepth,
1186                                  UInt        uiAbsPartIdx,
1187                                  TComYuv*    pcOrgYuv, 
1188                                  TComYuv*    pcPredYuv, 
1189                                  TComYuv*    pcResiYuv, 
1190                                  UInt&       ruiDist,
1191                                  UInt        uiChromaId,
1192                                  Int        default0Save1Load2 )
1193{
1194  UInt uiOrgTrDepth = uiTrDepth;
1195  UInt uiFullDepth  = pcCU->getDepth( 0 ) + uiTrDepth;
1196  UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
1197  if( uiLog2TrSize == 2 )
1198  {
1199    assert( uiTrDepth > 0 );
1200    uiTrDepth--;
1201    UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth ) << 1 );
1202    Bool bFirstQ = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
1203    if( !bFirstQ )
1204    {
1205      return;
1206    }
1207  }
1208 
1209  TextType  eText             = ( uiChromaId > 0 ? TEXT_CHROMA_V : TEXT_CHROMA_U );
1210  UInt      uiChromaPredMode  = pcCU     ->getChromaIntraDir( uiAbsPartIdx );
1211  UInt      uiWidth           = pcCU     ->getWidth   ( 0 ) >> ( uiTrDepth + 1 );
1212  UInt      uiHeight          = pcCU     ->getHeight  ( 0 ) >> ( uiTrDepth + 1 );
1213  UInt      uiStride          = pcOrgYuv ->getCStride ();
1214  Pel*      piOrg             = ( uiChromaId > 0 ? pcOrgYuv ->getCrAddr( uiAbsPartIdx ) : pcOrgYuv ->getCbAddr( uiAbsPartIdx ) );
1215  Pel*      piPred            = ( uiChromaId > 0 ? pcPredYuv->getCrAddr( uiAbsPartIdx ) : pcPredYuv->getCbAddr( uiAbsPartIdx ) );
1216  Pel*      piResi            = ( uiChromaId > 0 ? pcResiYuv->getCrAddr( uiAbsPartIdx ) : pcResiYuv->getCbAddr( uiAbsPartIdx ) );
1217  Pel*      piReco            = ( uiChromaId > 0 ? pcPredYuv->getCrAddr( uiAbsPartIdx ) : pcPredYuv->getCbAddr( uiAbsPartIdx ) );
1218 
1219  UInt      uiQTLayer         = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
1220  UInt      uiNumCoeffPerInc  = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 ) ) >> 2;
1221  TCoeff*   pcCoeff           = ( uiChromaId > 0 ? m_ppcQTTempCoeffCr[ uiQTLayer ] : m_ppcQTTempCoeffCb[ uiQTLayer ] ) + uiNumCoeffPerInc * uiAbsPartIdx;
1222#if ADAPTIVE_QP_SELECTION
1223  Int*      pcArlCoeff        = ( uiChromaId > 0 ? m_ppcQTTempArlCoeffCr[ uiQTLayer ] : m_ppcQTTempArlCoeffCb[ uiQTLayer ] ) + uiNumCoeffPerInc * uiAbsPartIdx;
1224#endif
1225  Pel*      piRecQt           = ( uiChromaId > 0 ? m_pcQTTempTComYuv[ uiQTLayer ].getCrAddr( uiAbsPartIdx ) : m_pcQTTempTComYuv[ uiQTLayer ].getCbAddr( uiAbsPartIdx ) );
1226  UInt      uiRecQtStride     = m_pcQTTempTComYuv[ uiQTLayer ].getCStride();
1227 
1228  UInt      uiZOrder          = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
1229  Pel*      piRecIPred        = ( uiChromaId > 0 ? pcCU->getPic()->getPicYuvRec()->getCrAddr( pcCU->getAddr(), uiZOrder ) : pcCU->getPic()->getPicYuvRec()->getCbAddr( pcCU->getAddr(), uiZOrder ) );
1230  UInt      uiRecIPredStride  = pcCU->getPic()->getPicYuvRec()->getCStride();
1231  Bool      useTransformSkipChroma       = pcCU->getTransformSkip(uiAbsPartIdx, eText);
1232  //===== update chroma mode =====
1233  if( uiChromaPredMode == DM_CHROMA_IDX )
1234  {
1235    uiChromaPredMode          = pcCU->getLumaIntraDir( 0 );
1236  }
1237 
1238  //===== init availability pattern =====
1239  Bool  bAboveAvail = false;
1240  Bool  bLeftAvail  = false;
1241  if( default0Save1Load2 != 2 )
1242  {
1243    pcCU->getPattern()->initPattern         ( pcCU, uiTrDepth, uiAbsPartIdx );
1244
1245    pcCU->getPattern()->initAdiPatternChroma( pcCU, uiAbsPartIdx, uiTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
1246    Int*  pPatChroma  = ( uiChromaId > 0 ? pcCU->getPattern()->getAdiCrBuf( uiWidth, uiHeight, m_piYuvExt ) : pcCU->getPattern()->getAdiCbBuf( uiWidth, uiHeight, m_piYuvExt ) );
1247
1248    //===== get prediction signal =====
1249#if INTRA_BL && !NO_RESIDUAL_FLAG_FOR_BLPRED
1250  if(pcCU->isIntraBL ( uiAbsPartIdx ) )
1251  {
1252    pcCU->getBaseChromaBlk( uiWidth, uiHeight, uiAbsPartIdx, piPred, uiStride, uiChromaId );
1253  }
1254  else
1255#endif
1256    {
1257      predIntraChromaAng( pPatChroma, uiChromaPredMode, piPred, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
1258    }
1259    // save prediction
1260    if( default0Save1Load2 == 1 )
1261    {
1262      Pel*  pPred   = piPred;
1263      Pel*  pPredBuf = m_pSharedPredTransformSkip[1 + uiChromaId];
1264      Int k = 0;
1265      for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1266      {
1267        for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1268        {
1269          pPredBuf[ k ++ ] = pPred[ uiX ];
1270        }
1271        pPred += uiStride;
1272      }
1273    }
1274  }
1275  else
1276  {
1277    // load prediction
1278    Pel*  pPred   = piPred;
1279    Pel*  pPredBuf = m_pSharedPredTransformSkip[1 + uiChromaId];
1280    Int k = 0;
1281    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1282    {
1283      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1284      {
1285        pPred[ uiX ] = pPredBuf[ k ++ ];
1286      }
1287      pPred += uiStride;
1288    }
1289  }
1290  //===== get residual signal =====
1291  {
1292    // get residual
1293    Pel*  pOrg    = piOrg;
1294    Pel*  pPred   = piPred;
1295    Pel*  pResi   = piResi;
1296    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1297    {
1298      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1299      {
1300        pResi[ uiX ] = pOrg[ uiX ] - pPred[ uiX ];
1301      }
1302      pOrg  += uiStride;
1303      pResi += uiStride;
1304      pPred += uiStride;
1305    }
1306  }
1307 
1308  //===== transform and quantization =====
1309  {
1310    //--- init rate estimation arrays for RDOQ ---
1311    if( useTransformSkipChroma? m_pcEncCfg->getUseRDOQTS():m_pcEncCfg->getUseRDOQ())
1312    {
1313      m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, uiWidth, eText );
1314    }
1315    //--- transform and quantization ---
1316    UInt uiAbsSum = 0;
1317
1318    Int curChromaQpOffset;
1319    if(eText == TEXT_CHROMA_U)
1320    {
1321      curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
1322    }
1323    else
1324    {
1325      curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
1326    }
1327    m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
1328
1329#if RDOQ_CHROMA_LAMBDA
1330    m_pcTrQuant->selectLambda      (TEXT_CHROMA); 
1331#endif
1332    m_pcTrQuant->transformNxN      ( pcCU, piResi, uiStride, pcCoeff, 
1333#if ADAPTIVE_QP_SELECTION
1334                                     pcArlCoeff, 
1335#endif
1336                                     uiWidth, uiHeight, uiAbsSum, eText, uiAbsPartIdx, useTransformSkipChroma );
1337    //--- set coded block flag ---
1338    pcCU->setCbfSubParts           ( ( uiAbsSum ? 1 : 0 ) << uiOrgTrDepth, eText, uiAbsPartIdx, pcCU->getDepth(0) + uiTrDepth );
1339    //--- inverse transform ---
1340    if( uiAbsSum )
1341    {
1342      Int scalingListType = 0 + g_eTTable[(Int)eText];
1343      assert(scalingListType < 6);
1344      m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA, REG_DCT, piResi, uiStride, pcCoeff, uiWidth, uiHeight, scalingListType, useTransformSkipChroma );
1345    }
1346    else
1347    {
1348      Pel* pResi = piResi;
1349      memset( pcCoeff, 0, sizeof( TCoeff ) * uiWidth * uiHeight );
1350      for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1351      {
1352        memset( pResi, 0, sizeof( Pel ) * uiWidth );
1353        pResi += uiStride;
1354      }
1355    }
1356  }
1357 
1358  //===== reconstruction =====
1359  {
1360    Pel* pPred      = piPred;
1361    Pel* pResi      = piResi;
1362    Pel* pReco      = piReco;
1363    Pel* pRecQt     = piRecQt;
1364    Pel* pRecIPred  = piRecIPred;
1365    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1366    {
1367      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1368      {
1369        pReco    [ uiX ] = ClipC( pPred[ uiX ] + pResi[ uiX ] );
1370        pRecQt   [ uiX ] = pReco[ uiX ];
1371        pRecIPred[ uiX ] = pReco[ uiX ];
1372      }
1373      pPred     += uiStride;
1374      pResi     += uiStride;
1375      pReco     += uiStride;
1376      pRecQt    += uiRecQtStride;
1377      pRecIPred += uiRecIPredStride;
1378    }
1379  }
1380 
1381  //===== update distortion =====
1382#if WEIGHTED_CHROMA_DISTORTION
1383  ruiDist += m_pcRdCost->getDistPart(g_bitDepthC, piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight, eText );
1384#else
1385  ruiDist += m_pcRdCost->getDistPart(g_bitDepthC, piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
1386#endif
1387}
1388
1389
1390
1391Void
1392TEncSearch::xRecurIntraCodingQT( TComDataCU*  pcCU, 
1393                                UInt         uiTrDepth,
1394                                UInt         uiAbsPartIdx, 
1395                                Bool         bLumaOnly,
1396                                TComYuv*     pcOrgYuv, 
1397                                TComYuv*     pcPredYuv, 
1398                                TComYuv*     pcResiYuv, 
1399                                UInt&        ruiDistY,
1400                                UInt&        ruiDistC,
1401#if HHI_RQT_INTRA_SPEEDUP
1402                                Bool         bCheckFirst,
1403#endif
1404                                Double&      dRDCost )
1405{
1406  UInt    uiFullDepth   = pcCU->getDepth( 0 ) +  uiTrDepth;
1407  UInt    uiLog2TrSize  = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
1408  Bool    bCheckFull    = ( uiLog2TrSize  <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
1409  Bool    bCheckSplit   = ( uiLog2TrSize  >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
1410 
1411#if HHI_RQT_INTRA_SPEEDUP
1412#if L0232_RD_PENALTY
1413  Int maxTuSize = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize();
1414  Int isIntraSlice = (pcCU->getSlice()->getSliceType() == I_SLICE);
1415  // don't check split if TU size is less or equal to max TU size
1416  Bool noSplitIntraMaxTuSize = bCheckFull;
1417  if(m_pcEncCfg->getRDpenalty() && ! isIntraSlice)
1418  {
1419    // in addition don't check split if TU size is less or equal to 16x16 TU size for non-intra slice
1420    noSplitIntraMaxTuSize = ( uiLog2TrSize  <= min(maxTuSize,4) );
1421
1422    // if maximum RD-penalty don't check TU size 32x32
1423    if(m_pcEncCfg->getRDpenalty()==2)
1424    {
1425      bCheckFull    = ( uiLog2TrSize  <= min(maxTuSize,4));
1426    }
1427  }
1428  if( bCheckFirst && noSplitIntraMaxTuSize )
1429#else
1430  if( bCheckFirst && bCheckFull )
1431#endif
1432  {
1433    bCheckSplit = false;
1434  }
1435#else
1436#if L0232_RD_PENALTY
1437  Int maxTuSize = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize();
1438  Int isIntraSlice = (pcCU->getSlice()->getSliceType() == I_SLICE);
1439  // if maximum RD-penalty don't check TU size 32x32
1440  if((m_pcEncCfg->getRDpenalty()==2)  && !isIntraSlice)
1441  {
1442    bCheckFull    = ( uiLog2TrSize  <= min(maxTuSize,4));
1443  }
1444#endif
1445#endif
1446  Double  dSingleCost   = MAX_DOUBLE;
1447  UInt    uiSingleDistY = 0;
1448  UInt    uiSingleDistC = 0;
1449  UInt    uiSingleCbfY  = 0;
1450  UInt    uiSingleCbfU  = 0;
1451  UInt    uiSingleCbfV  = 0;
1452  Bool    checkTransformSkip  = pcCU->getSlice()->getPPS()->getUseTransformSkip();
1453  UInt    widthTransformSkip  = pcCU->getWidth ( 0 ) >> uiTrDepth;
1454  UInt    heightTransformSkip = pcCU->getHeight( 0 ) >> uiTrDepth;
1455  Int     bestModeId    = 0;
1456  Int     bestModeIdUV[2] = {0, 0};
1457  checkTransformSkip         &= (widthTransformSkip == 4 && heightTransformSkip == 4);
1458  checkTransformSkip         &= (!pcCU->getCUTransquantBypass(0));
1459  checkTransformSkip         &= (!((pcCU->getQP( 0 ) == 0) && (pcCU->getSlice()->getSPS()->getUseLossless())));
1460  if ( m_pcEncCfg->getUseTransformSkipFast() )
1461  {
1462    checkTransformSkip       &= (pcCU->getPartitionSize(uiAbsPartIdx)==SIZE_NxN);
1463  }
1464  if( bCheckFull )
1465  {
1466    if(checkTransformSkip == true)
1467    {
1468      //----- store original entropy coding status -----
1469      if( m_bUseSBACRD)
1470      {
1471        m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
1472      }
1473      UInt   singleDistYTmp     = 0;
1474      UInt   singleDistCTmp     = 0;
1475      UInt   singleCbfYTmp      = 0;
1476      UInt   singleCbfUTmp      = 0;
1477      UInt   singleCbfVTmp      = 0;
1478      Double singleCostTmp      = 0;
1479      Int    default0Save1Load2 = 0;
1480      Int    firstCheckId       = 0;
1481
1482      UInt   uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + (uiTrDepth - 1) ) << 1 );
1483      Bool   bFirstQ = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
1484
1485      for(Int modeId = firstCheckId; modeId < 2; modeId ++)
1486      {
1487        singleDistYTmp = 0;
1488        singleDistCTmp = 0;
1489        pcCU ->setTransformSkipSubParts ( modeId, TEXT_LUMA, uiAbsPartIdx, uiFullDepth ); 
1490        if(modeId == firstCheckId)
1491        {
1492          default0Save1Load2 = 1;
1493        }
1494        else
1495        {
1496          default0Save1Load2 = 2;
1497        }
1498        //----- code luma block with given intra prediction mode and store Cbf-----
1499        xIntraCodingLumaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, singleDistYTmp,default0Save1Load2); 
1500        singleCbfYTmp = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, uiTrDepth );
1501        //----- code chroma blocks with given intra prediction mode and store Cbf-----
1502        if( !bLumaOnly )
1503        {
1504          if(bFirstQ)
1505          {
1506            pcCU ->setTransformSkipSubParts ( modeId, TEXT_CHROMA_U, uiAbsPartIdx, uiFullDepth); 
1507            pcCU ->setTransformSkipSubParts ( modeId, TEXT_CHROMA_V, uiAbsPartIdx, uiFullDepth); 
1508          }
1509          xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, singleDistCTmp, 0, default0Save1Load2); 
1510          xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, singleDistCTmp, 1, default0Save1Load2); 
1511          singleCbfUTmp = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth );
1512          singleCbfVTmp = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth );
1513        }
1514        //----- determine rate and r-d cost -----
1515        if(modeId == 1 && singleCbfYTmp == 0)
1516        {
1517          //In order not to code TS flag when cbf is zero, the case for TS with cbf being zero is forbidden.
1518          singleCostTmp = MAX_DOUBLE; 
1519        }
1520        else
1521        {
1522          UInt uiSingleBits = xGetIntraBitsQT( pcCU, uiTrDepth, uiAbsPartIdx, true, !bLumaOnly, false );
1523          singleCostTmp     = m_pcRdCost->calcRdCost( uiSingleBits, singleDistYTmp + singleDistCTmp );
1524        }
1525
1526        if(singleCostTmp < dSingleCost)
1527        {
1528          dSingleCost   = singleCostTmp;
1529          uiSingleDistY = singleDistYTmp;
1530          uiSingleDistC = singleDistCTmp;
1531          uiSingleCbfY  = singleCbfYTmp;
1532          uiSingleCbfU  = singleCbfUTmp;
1533          uiSingleCbfV  = singleCbfVTmp;
1534          bestModeId    = modeId;
1535          if(bestModeId == firstCheckId)
1536          {
1537            xStoreIntraResultQT(pcCU, uiTrDepth, uiAbsPartIdx,bLumaOnly );
1538            if( m_bUseSBACRD) 
1539            {
1540              m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiFullDepth ][ CI_TEMP_BEST ] );
1541            }
1542          }
1543        }
1544        if(modeId == firstCheckId)
1545        {
1546          m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
1547        }
1548      }
1549
1550      pcCU ->setTransformSkipSubParts ( bestModeId, TEXT_LUMA, uiAbsPartIdx, uiFullDepth ); 
1551
1552      if(bestModeId == firstCheckId)
1553      {
1554        xLoadIntraResultQT(pcCU, uiTrDepth, uiAbsPartIdx,bLumaOnly );
1555        pcCU->setCbfSubParts  ( uiSingleCbfY << uiTrDepth, TEXT_LUMA, uiAbsPartIdx, uiFullDepth );
1556        if( !bLumaOnly )
1557        {
1558          if(bFirstQ)
1559          {
1560            pcCU->setCbfSubParts( uiSingleCbfU << uiTrDepth, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth( 0 ) + uiTrDepth - 1 );
1561            pcCU->setCbfSubParts( uiSingleCbfV << uiTrDepth, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth( 0 ) + uiTrDepth - 1 );
1562          }
1563        }
1564        if(m_bUseSBACRD)
1565        {
1566          m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiFullDepth ][ CI_TEMP_BEST ] );
1567        } 
1568      }
1569
1570      if( !bLumaOnly )
1571      {
1572        bestModeIdUV[0] = bestModeIdUV[1] = bestModeId;
1573        if(bFirstQ && bestModeId == 1)
1574        {
1575          //In order not to code TS flag when cbf is zero, the case for TS with cbf being zero is forbidden.
1576          if(uiSingleCbfU == 0)
1577          {
1578            pcCU ->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, uiFullDepth); 
1579            bestModeIdUV[0] = 0;
1580          }
1581          if(uiSingleCbfV == 0)
1582          {
1583            pcCU ->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, uiFullDepth); 
1584            bestModeIdUV[1] = 0;
1585          }
1586        }
1587      }
1588    }
1589    else
1590    {
1591      pcCU ->setTransformSkipSubParts ( 0, TEXT_LUMA, uiAbsPartIdx, uiFullDepth ); 
1592      //----- store original entropy coding status -----
1593      if( m_bUseSBACRD && bCheckSplit )
1594      {
1595        m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
1596      }
1597      //----- code luma block with given intra prediction mode and store Cbf-----
1598      dSingleCost   = 0.0;
1599      xIntraCodingLumaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistY ); 
1600      if( bCheckSplit )
1601      {
1602        uiSingleCbfY = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA, uiTrDepth );
1603      }
1604      //----- code chroma blocks with given intra prediction mode and store Cbf-----
1605      if( !bLumaOnly )
1606      {
1607        pcCU ->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, uiFullDepth ); 
1608        pcCU ->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, uiFullDepth ); 
1609        xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistC, 0 ); 
1610        xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistC, 1 ); 
1611        if( bCheckSplit )
1612        {
1613          uiSingleCbfU = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth );
1614          uiSingleCbfV = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth );
1615        }
1616      }
1617      //----- determine rate and r-d cost -----
1618      UInt uiSingleBits = xGetIntraBitsQT( pcCU, uiTrDepth, uiAbsPartIdx, true, !bLumaOnly, false );
1619#if L0232_RD_PENALTY
1620      if(m_pcEncCfg->getRDpenalty() && (uiLog2TrSize==5) && !isIntraSlice)
1621      {
1622        uiSingleBits=uiSingleBits*4; 
1623      }
1624#endif
1625      dSingleCost       = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDistY + uiSingleDistC );
1626    }
1627  }
1628 
1629  if( bCheckSplit )
1630  {
1631    //----- store full entropy coding status, load original entropy coding status -----
1632    if( m_bUseSBACRD )
1633    {
1634      if( bCheckFull )
1635      {
1636        m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_TEST ] );
1637        m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
1638      }
1639      else
1640      {
1641        m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
1642      }
1643    }
1644    //----- code splitted block -----
1645    Double  dSplitCost      = 0.0;
1646    UInt    uiSplitDistY    = 0;
1647    UInt    uiSplitDistC    = 0;
1648    UInt    uiQPartsDiv     = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
1649    UInt    uiAbsPartIdxSub = uiAbsPartIdx;
1650
1651    UInt    uiSplitCbfY = 0;
1652    UInt    uiSplitCbfU = 0;
1653    UInt    uiSplitCbfV = 0;
1654
1655    for( UInt uiPart = 0; uiPart < 4; uiPart++, uiAbsPartIdxSub += uiQPartsDiv )
1656    {
1657#if HHI_RQT_INTRA_SPEEDUP
1658      xRecurIntraCodingQT( pcCU, uiTrDepth + 1, uiAbsPartIdxSub, bLumaOnly, pcOrgYuv, pcPredYuv, pcResiYuv, uiSplitDistY, uiSplitDistC, bCheckFirst, dSplitCost );
1659#else
1660      xRecurIntraCodingQT( pcCU, uiTrDepth + 1, uiAbsPartIdxSub, bLumaOnly, pcOrgYuv, pcPredYuv, pcResiYuv, uiSplitDistY, uiSplitDistC, dSplitCost );
1661#endif
1662
1663      uiSplitCbfY |= pcCU->getCbf( uiAbsPartIdxSub, TEXT_LUMA, uiTrDepth + 1 );
1664      if(!bLumaOnly)
1665      {
1666        uiSplitCbfU |= pcCU->getCbf( uiAbsPartIdxSub, TEXT_CHROMA_U, uiTrDepth + 1 );
1667        uiSplitCbfV |= pcCU->getCbf( uiAbsPartIdxSub, TEXT_CHROMA_V, uiTrDepth + 1 );
1668      }
1669    }
1670
1671    for( UInt uiOffs = 0; uiOffs < 4 * uiQPartsDiv; uiOffs++ )
1672    {
1673      pcCU->getCbf( TEXT_LUMA )[ uiAbsPartIdx + uiOffs ] |= ( uiSplitCbfY << uiTrDepth );
1674    }
1675    if( !bLumaOnly )
1676    {
1677      for( UInt uiOffs = 0; uiOffs < 4 * uiQPartsDiv; uiOffs++ )
1678      {
1679        pcCU->getCbf( TEXT_CHROMA_U )[ uiAbsPartIdx + uiOffs ] |= ( uiSplitCbfU << uiTrDepth );
1680        pcCU->getCbf( TEXT_CHROMA_V )[ uiAbsPartIdx + uiOffs ] |= ( uiSplitCbfV << uiTrDepth );
1681      }
1682    }
1683    //----- restore context states -----
1684    if( m_bUseSBACRD )
1685    {
1686      m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
1687    }
1688    //----- determine rate and r-d cost -----
1689    UInt uiSplitBits = xGetIntraBitsQT( pcCU, uiTrDepth, uiAbsPartIdx, true, !bLumaOnly, false );
1690    dSplitCost       = m_pcRdCost->calcRdCost( uiSplitBits, uiSplitDistY + uiSplitDistC );
1691   
1692    //===== compare and set best =====
1693    if( dSplitCost < dSingleCost )
1694    {
1695      //--- update cost ---
1696      ruiDistY += uiSplitDistY;
1697      ruiDistC += uiSplitDistC;
1698      dRDCost  += dSplitCost;
1699      return;
1700    }
1701    //----- set entropy coding status -----
1702    if( m_bUseSBACRD )
1703    {
1704      m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_TEST ] );
1705    }
1706   
1707    //--- set transform index and Cbf values ---
1708    pcCU->setTrIdxSubParts( uiTrDepth, uiAbsPartIdx, uiFullDepth );
1709    pcCU->setCbfSubParts  ( uiSingleCbfY << uiTrDepth, TEXT_LUMA, uiAbsPartIdx, uiFullDepth );
1710    pcCU ->setTransformSkipSubParts  ( bestModeId, TEXT_LUMA, uiAbsPartIdx, uiFullDepth ); 
1711    if( !bLumaOnly )
1712    {
1713      pcCU->setCbfSubParts( uiSingleCbfU << uiTrDepth, TEXT_CHROMA_U, uiAbsPartIdx, uiFullDepth );
1714      pcCU->setCbfSubParts( uiSingleCbfV << uiTrDepth, TEXT_CHROMA_V, uiAbsPartIdx, uiFullDepth );
1715      pcCU->setTransformSkipSubParts ( bestModeIdUV[0], TEXT_CHROMA_U, uiAbsPartIdx, uiFullDepth); 
1716      pcCU->setTransformSkipSubParts ( bestModeIdUV[1], TEXT_CHROMA_V, uiAbsPartIdx, uiFullDepth); 
1717    }
1718   
1719    //--- set reconstruction for next intra prediction blocks ---
1720    UInt  uiWidth     = pcCU->getWidth ( 0 ) >> uiTrDepth;
1721    UInt  uiHeight    = pcCU->getHeight( 0 ) >> uiTrDepth;
1722    UInt  uiQTLayer   = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
1723    UInt  uiZOrder    = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
1724    Pel*  piSrc       = m_pcQTTempTComYuv[ uiQTLayer ].getLumaAddr( uiAbsPartIdx );
1725    UInt  uiSrcStride = m_pcQTTempTComYuv[ uiQTLayer ].getStride  ();
1726    Pel*  piDes       = pcCU->getPic()->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), uiZOrder );
1727    UInt  uiDesStride = pcCU->getPic()->getPicYuvRec()->getStride  ();
1728    for( UInt uiY = 0; uiY < uiHeight; uiY++, piSrc += uiSrcStride, piDes += uiDesStride )
1729    {
1730      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1731      {
1732        piDes[ uiX ] = piSrc[ uiX ];
1733      }
1734    }
1735    if( !bLumaOnly )
1736    {
1737      uiWidth   >>= 1;
1738      uiHeight  >>= 1;
1739      piSrc       = m_pcQTTempTComYuv[ uiQTLayer ].getCbAddr  ( uiAbsPartIdx );
1740      uiSrcStride = m_pcQTTempTComYuv[ uiQTLayer ].getCStride ();
1741      piDes       = pcCU->getPic()->getPicYuvRec()->getCbAddr ( pcCU->getAddr(), uiZOrder );
1742      uiDesStride = pcCU->getPic()->getPicYuvRec()->getCStride();
1743      for( UInt uiY = 0; uiY < uiHeight; uiY++, piSrc += uiSrcStride, piDes += uiDesStride )
1744      {
1745        for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1746        {
1747          piDes[ uiX ] = piSrc[ uiX ];
1748        }
1749      }
1750      piSrc       = m_pcQTTempTComYuv[ uiQTLayer ].getCrAddr  ( uiAbsPartIdx );
1751      piDes       = pcCU->getPic()->getPicYuvRec()->getCrAddr ( pcCU->getAddr(), uiZOrder );
1752      for( UInt uiY = 0; uiY < uiHeight; uiY++, piSrc += uiSrcStride, piDes += uiDesStride )
1753      {
1754        for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1755        {
1756          piDes[ uiX ] = piSrc[ uiX ];
1757        }
1758      }
1759    }
1760  }
1761  ruiDistY += uiSingleDistY;
1762  ruiDistC += uiSingleDistC;
1763  dRDCost  += dSingleCost;
1764}
1765
1766
1767Void
1768TEncSearch::xSetIntraResultQT( TComDataCU* pcCU,
1769                              UInt        uiTrDepth,
1770                              UInt        uiAbsPartIdx,
1771                              Bool        bLumaOnly,
1772                              TComYuv*    pcRecoYuv )
1773{
1774  UInt uiFullDepth  = pcCU->getDepth(0) + uiTrDepth;
1775  UInt uiTrMode     = pcCU->getTransformIdx( uiAbsPartIdx );
1776  if(  uiTrMode == uiTrDepth )
1777  {
1778    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
1779    UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
1780   
1781    Bool bSkipChroma  = false;
1782    Bool bChromaSame  = false;
1783    if( !bLumaOnly && uiLog2TrSize == 2 )
1784    {
1785      assert( uiTrDepth > 0 );
1786      UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth - 1 ) << 1 );
1787      bSkipChroma  = ( ( uiAbsPartIdx % uiQPDiv ) != 0 );
1788      bChromaSame  = true;
1789    }
1790   
1791    //===== copy transform coefficients =====
1792    UInt uiNumCoeffY    = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
1793    UInt uiNumCoeffIncY = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
1794    TCoeff* pcCoeffSrcY = m_ppcQTTempCoeffY [ uiQTLayer ] + ( uiNumCoeffIncY * uiAbsPartIdx );
1795    TCoeff* pcCoeffDstY = pcCU->getCoeffY ()              + ( uiNumCoeffIncY * uiAbsPartIdx );
1796    ::memcpy( pcCoeffDstY, pcCoeffSrcY, sizeof( TCoeff ) * uiNumCoeffY );
1797#if ADAPTIVE_QP_SELECTION
1798    Int* pcArlCoeffSrcY = m_ppcQTTempArlCoeffY [ uiQTLayer ] + ( uiNumCoeffIncY * uiAbsPartIdx );
1799    Int* pcArlCoeffDstY = pcCU->getArlCoeffY ()              + ( uiNumCoeffIncY * uiAbsPartIdx );
1800    ::memcpy( pcArlCoeffDstY, pcArlCoeffSrcY, sizeof( Int ) * uiNumCoeffY );
1801#endif
1802    if( !bLumaOnly && !bSkipChroma )
1803    {
1804      UInt uiNumCoeffC    = ( bChromaSame ? uiNumCoeffY    : uiNumCoeffY    >> 2 );
1805      UInt uiNumCoeffIncC = uiNumCoeffIncY >> 2;
1806      TCoeff* pcCoeffSrcU = m_ppcQTTempCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1807      TCoeff* pcCoeffSrcV = m_ppcQTTempCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1808      TCoeff* pcCoeffDstU = pcCU->getCoeffCb()              + ( uiNumCoeffIncC * uiAbsPartIdx );
1809      TCoeff* pcCoeffDstV = pcCU->getCoeffCr()              + ( uiNumCoeffIncC * uiAbsPartIdx );
1810      ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
1811      ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
1812#if ADAPTIVE_QP_SELECTION
1813      Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1814      Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1815      Int* pcArlCoeffDstU = pcCU->getArlCoeffCb()              + ( uiNumCoeffIncC * uiAbsPartIdx );
1816      Int* pcArlCoeffDstV = pcCU->getArlCoeffCr()              + ( uiNumCoeffIncC * uiAbsPartIdx );
1817      ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
1818      ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
1819#endif
1820    }
1821   
1822    //===== copy reconstruction =====
1823    m_pcQTTempTComYuv[ uiQTLayer ].copyPartToPartLuma( pcRecoYuv, uiAbsPartIdx, 1 << uiLog2TrSize, 1 << uiLog2TrSize );
1824    if( !bLumaOnly && !bSkipChroma )
1825    {
1826      UInt uiLog2TrSizeChroma = ( bChromaSame ? uiLog2TrSize : uiLog2TrSize - 1 );
1827      m_pcQTTempTComYuv[ uiQTLayer ].copyPartToPartChroma( pcRecoYuv, uiAbsPartIdx, 1 << uiLog2TrSizeChroma, 1 << uiLog2TrSizeChroma );
1828    }
1829  }
1830  else
1831  {
1832    UInt uiNumQPart  = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
1833    for( UInt uiPart = 0; uiPart < 4; uiPart++ )
1834    {
1835      xSetIntraResultQT( pcCU, uiTrDepth + 1, uiAbsPartIdx + uiPart * uiNumQPart, bLumaOnly, pcRecoYuv );
1836    }
1837  }
1838}
1839
1840Void
1841TEncSearch::xStoreIntraResultQT( TComDataCU* pcCU,
1842                                UInt        uiTrDepth,
1843                                UInt        uiAbsPartIdx,
1844                                Bool        bLumaOnly )
1845{
1846  UInt uiFullDepth  = pcCU->getDepth(0) + uiTrDepth;
1847  UInt uiTrMode     = pcCU->getTransformIdx( uiAbsPartIdx );
1848  assert(  uiTrMode == uiTrDepth );
1849  UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
1850  UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
1851
1852  Bool bSkipChroma  = false;
1853  Bool bChromaSame  = false;
1854  if( !bLumaOnly && uiLog2TrSize == 2 )
1855  {
1856    assert( uiTrDepth > 0 );
1857    UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth - 1 ) << 1 );
1858    bSkipChroma  = ( ( uiAbsPartIdx % uiQPDiv ) != 0 );
1859    bChromaSame  = true;
1860  }
1861
1862  //===== copy transform coefficients =====
1863  UInt uiNumCoeffY    = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
1864  UInt uiNumCoeffIncY = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
1865  TCoeff* pcCoeffSrcY = m_ppcQTTempCoeffY [ uiQTLayer ] + ( uiNumCoeffIncY * uiAbsPartIdx );
1866  TCoeff* pcCoeffDstY = m_pcQTTempTUCoeffY;
1867
1868  ::memcpy( pcCoeffDstY, pcCoeffSrcY, sizeof( TCoeff ) * uiNumCoeffY );
1869#if ADAPTIVE_QP_SELECTION
1870  Int* pcArlCoeffSrcY = m_ppcQTTempArlCoeffY [ uiQTLayer ] + ( uiNumCoeffIncY * uiAbsPartIdx );
1871  Int* pcArlCoeffDstY = m_ppcQTTempTUArlCoeffY;
1872  ::memcpy( pcArlCoeffDstY, pcArlCoeffSrcY, sizeof( Int ) * uiNumCoeffY );
1873#endif
1874  if( !bLumaOnly && !bSkipChroma )
1875  {
1876    UInt uiNumCoeffC    = ( bChromaSame ? uiNumCoeffY    : uiNumCoeffY    >> 2 );
1877    UInt uiNumCoeffIncC = uiNumCoeffIncY >> 2;
1878    TCoeff* pcCoeffSrcU = m_ppcQTTempCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1879    TCoeff* pcCoeffSrcV = m_ppcQTTempCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1880    TCoeff* pcCoeffDstU = m_pcQTTempTUCoeffCb;
1881    TCoeff* pcCoeffDstV = m_pcQTTempTUCoeffCr;
1882    ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
1883    ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
1884#if ADAPTIVE_QP_SELECTION
1885    Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1886    Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1887    Int* pcArlCoeffDstU = m_ppcQTTempTUArlCoeffCb;
1888    Int* pcArlCoeffDstV = m_ppcQTTempTUArlCoeffCr;
1889    ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
1890    ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
1891#endif
1892  }
1893
1894  //===== copy reconstruction =====
1895  m_pcQTTempTComYuv[ uiQTLayer ].copyPartToPartLuma( &m_pcQTTempTransformSkipTComYuv, uiAbsPartIdx, 1 << uiLog2TrSize, 1 << uiLog2TrSize );
1896
1897  if( !bLumaOnly && !bSkipChroma )
1898  {
1899    UInt uiLog2TrSizeChroma = ( bChromaSame ? uiLog2TrSize : uiLog2TrSize - 1 );
1900    m_pcQTTempTComYuv[ uiQTLayer ].copyPartToPartChroma( &m_pcQTTempTransformSkipTComYuv, uiAbsPartIdx, 1 << uiLog2TrSizeChroma, 1 << uiLog2TrSizeChroma );
1901  }
1902}
1903
1904Void
1905TEncSearch::xLoadIntraResultQT( TComDataCU* pcCU,
1906                               UInt        uiTrDepth,
1907                               UInt        uiAbsPartIdx,
1908                               Bool        bLumaOnly )
1909{
1910  UInt uiFullDepth  = pcCU->getDepth(0) + uiTrDepth;
1911  UInt uiTrMode     = pcCU->getTransformIdx( uiAbsPartIdx );
1912  assert(  uiTrMode == uiTrDepth );
1913  UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
1914  UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
1915
1916  Bool bSkipChroma  = false;
1917  Bool bChromaSame  = false;
1918  if( !bLumaOnly && uiLog2TrSize == 2 )
1919  {
1920    assert( uiTrDepth > 0 );
1921    UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth - 1 ) << 1 );
1922    bSkipChroma  = ( ( uiAbsPartIdx % uiQPDiv ) != 0 );
1923    bChromaSame  = true;
1924  }
1925
1926  //===== copy transform coefficients =====
1927  UInt uiNumCoeffY    = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
1928  UInt uiNumCoeffIncY = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
1929  TCoeff* pcCoeffDstY = m_ppcQTTempCoeffY [ uiQTLayer ] + ( uiNumCoeffIncY * uiAbsPartIdx );
1930  TCoeff* pcCoeffSrcY = m_pcQTTempTUCoeffY;
1931
1932  ::memcpy( pcCoeffDstY, pcCoeffSrcY, sizeof( TCoeff ) * uiNumCoeffY );
1933#if ADAPTIVE_QP_SELECTION
1934  Int* pcArlCoeffDstY = m_ppcQTTempArlCoeffY [ uiQTLayer ] + ( uiNumCoeffIncY * uiAbsPartIdx );
1935  Int* pcArlCoeffSrcY = m_ppcQTTempTUArlCoeffY;
1936  ::memcpy( pcArlCoeffDstY, pcArlCoeffSrcY, sizeof( Int ) * uiNumCoeffY );
1937#endif
1938  if( !bLumaOnly && !bSkipChroma )
1939  {
1940    UInt uiNumCoeffC    = ( bChromaSame ? uiNumCoeffY    : uiNumCoeffY    >> 2 );
1941    UInt uiNumCoeffIncC = uiNumCoeffIncY >> 2;
1942    TCoeff* pcCoeffDstU = m_ppcQTTempCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1943    TCoeff* pcCoeffDstV = m_ppcQTTempCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1944    TCoeff* pcCoeffSrcU = m_pcQTTempTUCoeffCb;
1945    TCoeff* pcCoeffSrcV = m_pcQTTempTUCoeffCr;
1946    ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
1947    ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
1948#if ADAPTIVE_QP_SELECTION
1949    Int* pcArlCoeffDstU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1950    Int* pcArlCoeffDstV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
1951    Int* pcArlCoeffSrcU = m_ppcQTTempTUArlCoeffCb;
1952    Int* pcArlCoeffSrcV = m_ppcQTTempTUArlCoeffCr;
1953    ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
1954    ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
1955#endif
1956  }
1957
1958  //===== copy reconstruction =====
1959  m_pcQTTempTransformSkipTComYuv.copyPartToPartLuma( &m_pcQTTempTComYuv[ uiQTLayer ] , uiAbsPartIdx, 1 << uiLog2TrSize, 1 << uiLog2TrSize );
1960
1961  if( !bLumaOnly && !bSkipChroma )
1962  {
1963    UInt uiLog2TrSizeChroma = ( bChromaSame ? uiLog2TrSize : uiLog2TrSize - 1 );
1964    m_pcQTTempTransformSkipTComYuv.copyPartToPartChroma( &m_pcQTTempTComYuv[ uiQTLayer ], uiAbsPartIdx, 1 << uiLog2TrSizeChroma, 1 << uiLog2TrSizeChroma );
1965  }
1966
1967  UInt    uiZOrder          = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
1968  Pel*    piRecIPred        = pcCU->getPic()->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), uiZOrder );
1969  UInt    uiRecIPredStride  = pcCU->getPic()->getPicYuvRec()->getStride  ();
1970  Pel*    piRecQt           = m_pcQTTempTComYuv[ uiQTLayer ].getLumaAddr( uiAbsPartIdx );
1971  UInt    uiRecQtStride     = m_pcQTTempTComYuv[ uiQTLayer ].getStride  ();
1972  UInt    uiWidth           = pcCU     ->getWidth   ( 0 ) >> uiTrDepth;
1973  UInt    uiHeight          = pcCU     ->getHeight  ( 0 ) >> uiTrDepth;
1974  Pel* pRecQt     = piRecQt;
1975  Pel* pRecIPred  = piRecIPred;
1976  for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1977  {
1978    for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1979    {
1980      pRecIPred[ uiX ] = pRecQt   [ uiX ];
1981    }
1982    pRecQt    += uiRecQtStride;
1983    pRecIPred += uiRecIPredStride;
1984  }
1985
1986  if( !bLumaOnly && !bSkipChroma )
1987  {
1988    piRecIPred = pcCU->getPic()->getPicYuvRec()->getCbAddr( pcCU->getAddr(), uiZOrder );
1989    piRecQt    = m_pcQTTempTComYuv[ uiQTLayer ].getCbAddr( uiAbsPartIdx );
1990    pRecQt     = piRecQt;
1991    pRecIPred  = piRecIPred;
1992    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1993    {
1994      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1995      {
1996        pRecIPred[ uiX ] = pRecQt[ uiX ];
1997      }
1998      pRecQt    += uiRecQtStride;
1999      pRecIPred += uiRecIPredStride;
2000    }
2001
2002    piRecIPred = pcCU->getPic()->getPicYuvRec()->getCrAddr( pcCU->getAddr(), uiZOrder );
2003    piRecQt    = m_pcQTTempTComYuv[ uiQTLayer ].getCrAddr( uiAbsPartIdx );
2004    pRecQt     = piRecQt;
2005    pRecIPred  = piRecIPred;
2006    for( UInt uiY = 0; uiY < uiHeight; uiY++ )
2007    {
2008      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
2009      {
2010        pRecIPred[ uiX ] = pRecQt[ uiX ];
2011      }
2012      pRecQt    += uiRecQtStride;
2013      pRecIPred += uiRecIPredStride;
2014    }
2015  }
2016}
2017
2018Void
2019TEncSearch::xStoreIntraResultChromaQT( TComDataCU* pcCU,
2020                                      UInt        uiTrDepth,
2021                                      UInt        uiAbsPartIdx,
2022                                      UInt        stateU0V1Both2 )
2023{
2024  UInt uiFullDepth = pcCU->getDepth(0) + uiTrDepth;
2025  UInt uiTrMode    = pcCU->getTransformIdx( uiAbsPartIdx );
2026  if(  uiTrMode == uiTrDepth )
2027  {
2028    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
2029    UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
2030
2031    Bool bChromaSame = false;
2032    if( uiLog2TrSize == 2 )
2033    {
2034      assert( uiTrDepth > 0 );
2035      uiTrDepth --;
2036      UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth) << 1 );
2037      if( ( uiAbsPartIdx % uiQPDiv ) != 0 )
2038      {
2039        return;
2040      }
2041      bChromaSame = true;
2042    }
2043
2044    //===== copy transform coefficients =====
2045    UInt uiNumCoeffC    = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
2046    if( !bChromaSame )
2047    {
2048      uiNumCoeffC     >>= 2;
2049    }
2050    UInt uiNumCoeffIncC = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 ) + 2 );
2051    if(stateU0V1Both2 == 0 || stateU0V1Both2 == 2)
2052    {
2053      TCoeff* pcCoeffSrcU = m_ppcQTTempCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2054      TCoeff* pcCoeffDstU = m_pcQTTempTUCoeffCb;
2055      ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
2056
2057#if ADAPTIVE_QP_SELECTION   
2058      Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2059      Int* pcArlCoeffDstU = m_ppcQTTempTUArlCoeffCb;
2060      ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
2061#endif
2062    }
2063    if(stateU0V1Both2 == 1 || stateU0V1Both2 == 2)
2064    {
2065      TCoeff* pcCoeffSrcV = m_ppcQTTempCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2066      TCoeff* pcCoeffDstV = m_pcQTTempTUCoeffCr;
2067      ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
2068#if ADAPTIVE_QP_SELECTION   
2069      Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2070      Int* pcArlCoeffDstV = m_ppcQTTempTUArlCoeffCr;
2071      ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
2072#endif
2073    }
2074
2075    //===== copy reconstruction =====
2076    UInt uiLog2TrSizeChroma = ( bChromaSame ? uiLog2TrSize : uiLog2TrSize - 1 );
2077    m_pcQTTempTComYuv[ uiQTLayer ].copyPartToPartChroma(&m_pcQTTempTransformSkipTComYuv, uiAbsPartIdx, 1 << uiLog2TrSizeChroma, 1 << uiLog2TrSizeChroma, stateU0V1Both2 );
2078  }
2079}
2080
2081
2082Void
2083TEncSearch::xLoadIntraResultChromaQT( TComDataCU* pcCU,
2084                                     UInt        uiTrDepth,
2085                                     UInt        uiAbsPartIdx,
2086                                     UInt        stateU0V1Both2 )
2087{
2088  UInt uiFullDepth = pcCU->getDepth(0) + uiTrDepth;
2089  UInt uiTrMode    = pcCU->getTransformIdx( uiAbsPartIdx );
2090  if(  uiTrMode == uiTrDepth )
2091  {
2092    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
2093    UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
2094
2095    Bool bChromaSame = false;
2096    if( uiLog2TrSize == 2 )
2097    {
2098      assert( uiTrDepth > 0 );
2099      uiTrDepth --;
2100      UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth ) << 1 );
2101      if( ( uiAbsPartIdx % uiQPDiv ) != 0 )
2102      {
2103        return;
2104      }
2105      bChromaSame = true;
2106    }
2107
2108    //===== copy transform coefficients =====
2109    UInt uiNumCoeffC = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
2110    if( !bChromaSame )
2111    {
2112      uiNumCoeffC >>= 2;
2113    }
2114    UInt uiNumCoeffIncC = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 ) + 2 );
2115
2116    if(stateU0V1Both2 ==0 || stateU0V1Both2 == 2)
2117    {
2118      TCoeff* pcCoeffDstU = m_ppcQTTempCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2119      TCoeff* pcCoeffSrcU = m_pcQTTempTUCoeffCb;
2120      ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
2121#if ADAPTIVE_QP_SELECTION   
2122      Int* pcArlCoeffDstU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2123      Int* pcArlCoeffSrcU = m_ppcQTTempTUArlCoeffCb;
2124      ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
2125#endif
2126    }
2127    if(stateU0V1Both2 ==1 || stateU0V1Both2 == 2)
2128    {
2129      TCoeff* pcCoeffDstV = m_ppcQTTempCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2130      TCoeff* pcCoeffSrcV = m_pcQTTempTUCoeffCr;
2131      ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
2132#if ADAPTIVE_QP_SELECTION   
2133      Int* pcArlCoeffDstV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2134      Int* pcArlCoeffSrcV = m_ppcQTTempTUArlCoeffCr;       
2135      ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
2136#endif
2137    }
2138
2139    //===== copy reconstruction =====
2140    UInt uiLog2TrSizeChroma = ( bChromaSame ? uiLog2TrSize : uiLog2TrSize - 1 );
2141    m_pcQTTempTransformSkipTComYuv.copyPartToPartChroma( &m_pcQTTempTComYuv[ uiQTLayer ], uiAbsPartIdx, 1 << uiLog2TrSizeChroma, 1 << uiLog2TrSizeChroma, stateU0V1Both2);
2142
2143    UInt    uiZOrder          = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
2144    UInt    uiWidth           = pcCU->getWidth   ( 0 ) >> (uiTrDepth + 1);
2145    UInt    uiHeight          = pcCU->getHeight  ( 0 ) >> (uiTrDepth + 1);
2146    UInt    uiRecQtStride     = m_pcQTTempTComYuv[ uiQTLayer ].getCStride  ();
2147    UInt    uiRecIPredStride  = pcCU->getPic()->getPicYuvRec()->getCStride  ();
2148
2149    if(stateU0V1Both2 ==0 || stateU0V1Both2 == 2)
2150    {
2151      Pel* piRecIPred = pcCU->getPic()->getPicYuvRec()->getCbAddr( pcCU->getAddr(), uiZOrder );
2152      Pel* piRecQt    = m_pcQTTempTComYuv[ uiQTLayer ].getCbAddr( uiAbsPartIdx );
2153      Pel* pRecQt     = piRecQt;
2154      Pel* pRecIPred  = piRecIPred;
2155      for( UInt uiY = 0; uiY < uiHeight; uiY++ )
2156      {
2157        for( UInt uiX = 0; uiX < uiWidth; uiX++ )
2158        {
2159          pRecIPred[ uiX ] = pRecQt[ uiX ];
2160        }
2161        pRecQt    += uiRecQtStride;
2162        pRecIPred += uiRecIPredStride;
2163      }
2164    }
2165    if(stateU0V1Both2 == 1 || stateU0V1Both2 == 2)
2166    {
2167      Pel* piRecIPred = pcCU->getPic()->getPicYuvRec()->getCrAddr( pcCU->getAddr(), uiZOrder );
2168      Pel* piRecQt    = m_pcQTTempTComYuv[ uiQTLayer ].getCrAddr( uiAbsPartIdx );
2169      Pel* pRecQt     = piRecQt;
2170      Pel* pRecIPred  = piRecIPred;
2171      for( UInt uiY = 0; uiY < uiHeight; uiY++ )
2172      {
2173        for( UInt uiX = 0; uiX < uiWidth; uiX++ )
2174        {
2175          pRecIPred[ uiX ] = pRecQt[ uiX ];
2176        }
2177        pRecQt    += uiRecQtStride;
2178        pRecIPred += uiRecIPredStride;
2179      }
2180    }
2181  }
2182}
2183
2184Void
2185TEncSearch::xRecurIntraChromaCodingQT( TComDataCU*  pcCU, 
2186                                      UInt         uiTrDepth,
2187                                      UInt         uiAbsPartIdx, 
2188                                      TComYuv*     pcOrgYuv, 
2189                                      TComYuv*     pcPredYuv, 
2190                                      TComYuv*     pcResiYuv, 
2191                                      UInt&        ruiDist )
2192{
2193  UInt uiFullDepth = pcCU->getDepth( 0 ) +  uiTrDepth;
2194  UInt uiTrMode    = pcCU->getTransformIdx( uiAbsPartIdx );
2195  if(  uiTrMode == uiTrDepth )
2196  {
2197    Bool checkTransformSkip = pcCU->getSlice()->getPPS()->getUseTransformSkip();
2198    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
2199
2200    UInt actualTrDepth = uiTrDepth;
2201    if( uiLog2TrSize == 2 )
2202    {
2203      assert( uiTrDepth > 0 );
2204      actualTrDepth--;
2205      UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + actualTrDepth) << 1 );
2206      Bool bFirstQ = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
2207      if( !bFirstQ )
2208      {
2209        return;
2210      }
2211    }
2212
2213    checkTransformSkip &= (uiLog2TrSize <= 3);
2214    if ( m_pcEncCfg->getUseTransformSkipFast() )
2215    {
2216      checkTransformSkip &= (uiLog2TrSize < 3);
2217      if (checkTransformSkip)
2218      {
2219        Int nbLumaSkip = 0;
2220        for(UInt absPartIdxSub = uiAbsPartIdx; absPartIdxSub < uiAbsPartIdx + 4; absPartIdxSub ++)
2221        {
2222          nbLumaSkip += pcCU->getTransformSkip(absPartIdxSub, TEXT_LUMA);
2223        }
2224        checkTransformSkip &= (nbLumaSkip > 0);
2225      }
2226    }
2227
2228    if(checkTransformSkip)
2229    {
2230        //use RDO to decide whether Cr/Cb takes TS
2231        if( m_bUseSBACRD )
2232        {
2233          m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[uiFullDepth][CI_QT_TRAFO_ROOT] );
2234        }
2235
2236        for(Int chromaId = 0; chromaId < 2; chromaId ++)
2237        {
2238          Double  dSingleCost    = MAX_DOUBLE;
2239          Int     bestModeId     = 0;
2240          UInt    singleDistC    = 0;
2241          UInt    singleCbfC     = 0;
2242          UInt    singleDistCTmp = 0;
2243          Double  singleCostTmp  = 0;
2244          UInt    singleCbfCTmp  = 0;
2245
2246          Int     default0Save1Load2 = 0;
2247          Int     firstCheckId       = 0;
2248
2249          for(Int chromaModeId = firstCheckId; chromaModeId < 2; chromaModeId ++)
2250          {
2251            pcCU->setTransformSkipSubParts ( chromaModeId, (TextType)(chromaId + 2), uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth); 
2252            if(chromaModeId == firstCheckId)
2253            {
2254              default0Save1Load2 = 1;
2255            }
2256            else
2257            {
2258              default0Save1Load2 = 2;
2259            }
2260            singleDistCTmp = 0;
2261            xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, singleDistCTmp, chromaId ,default0Save1Load2);
2262            singleCbfCTmp = pcCU->getCbf( uiAbsPartIdx, (TextType)(chromaId + 2), uiTrDepth);
2263           
2264            if(chromaModeId == 1 && singleCbfCTmp == 0)
2265            {
2266              //In order not to code TS flag when cbf is zero, the case for TS with cbf being zero is forbidden.
2267              singleCostTmp = MAX_DOUBLE;
2268            }
2269            else
2270            {
2271              UInt bitsTmp = xGetIntraBitsQTChroma( pcCU,uiTrDepth, uiAbsPartIdx,chromaId + 2, false );
2272              singleCostTmp  = m_pcRdCost->calcRdCost( bitsTmp, singleDistCTmp);
2273            }
2274
2275            if(singleCostTmp < dSingleCost)
2276            {
2277              dSingleCost = singleCostTmp;
2278              singleDistC = singleDistCTmp;
2279              bestModeId  = chromaModeId;
2280              singleCbfC  = singleCbfCTmp;
2281
2282              if(bestModeId == firstCheckId)
2283              {
2284                xStoreIntraResultChromaQT(pcCU, uiTrDepth, uiAbsPartIdx,chromaId);
2285                if( m_bUseSBACRD) 
2286                {
2287                  m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiFullDepth ][ CI_TEMP_BEST ] );
2288                }
2289              }
2290            }
2291            if(chromaModeId == firstCheckId)
2292            {
2293              m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
2294            }
2295          }
2296
2297          if(bestModeId == firstCheckId)
2298          {
2299            xLoadIntraResultChromaQT(pcCU, uiTrDepth, uiAbsPartIdx,chromaId);
2300            pcCU->setCbfSubParts ( singleCbfC << uiTrDepth, (TextType)(chromaId + 2), uiAbsPartIdx, pcCU->getDepth(0) + actualTrDepth );
2301            if(m_bUseSBACRD)
2302            {
2303              m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiFullDepth ][ CI_TEMP_BEST ] );
2304            } 
2305          }
2306          pcCU ->setTransformSkipSubParts( bestModeId, (TextType)(chromaId + 2), uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth ); 
2307          ruiDist += singleDistC;
2308
2309          if(chromaId == 0)
2310          {
2311            if( m_bUseSBACRD )
2312            {
2313              m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[uiFullDepth][CI_QT_TRAFO_ROOT] );
2314            }
2315          }
2316        }
2317    }
2318    else
2319    {
2320      pcCU ->setTransformSkipSubParts( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth ); 
2321      pcCU ->setTransformSkipSubParts( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth ); 
2322      xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 0 ); 
2323      xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 1 ); 
2324    }
2325  }
2326  else
2327  {
2328    UInt uiSplitCbfU     = 0;
2329    UInt uiSplitCbfV     = 0;
2330    UInt uiQPartsDiv     = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
2331    UInt uiAbsPartIdxSub = uiAbsPartIdx;
2332    for( UInt uiPart = 0; uiPart < 4; uiPart++, uiAbsPartIdxSub += uiQPartsDiv )
2333    {
2334      xRecurIntraChromaCodingQT( pcCU, uiTrDepth + 1, uiAbsPartIdxSub, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist );
2335      uiSplitCbfU |= pcCU->getCbf( uiAbsPartIdxSub, TEXT_CHROMA_U, uiTrDepth + 1 );
2336      uiSplitCbfV |= pcCU->getCbf( uiAbsPartIdxSub, TEXT_CHROMA_V, uiTrDepth + 1 );
2337    }
2338    for( UInt uiOffs = 0; uiOffs < 4 * uiQPartsDiv; uiOffs++ )
2339    {
2340      pcCU->getCbf( TEXT_CHROMA_U )[ uiAbsPartIdx + uiOffs ] |= ( uiSplitCbfU << uiTrDepth );
2341      pcCU->getCbf( TEXT_CHROMA_V )[ uiAbsPartIdx + uiOffs ] |= ( uiSplitCbfV << uiTrDepth );
2342    }
2343  }
2344}
2345
2346Void
2347TEncSearch::xSetIntraResultChromaQT( TComDataCU* pcCU,
2348                                    UInt        uiTrDepth,
2349                                    UInt        uiAbsPartIdx,
2350                                    TComYuv*    pcRecoYuv )
2351{
2352  UInt uiFullDepth  = pcCU->getDepth(0) + uiTrDepth;
2353  UInt uiTrMode     = pcCU->getTransformIdx( uiAbsPartIdx );
2354  if(  uiTrMode == uiTrDepth )
2355  {
2356    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
2357    UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
2358   
2359    Bool bChromaSame  = false;
2360    if( uiLog2TrSize == 2 )
2361    {
2362      assert( uiTrDepth > 0 );
2363      UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth - 1 ) << 1 );
2364      if( ( uiAbsPartIdx % uiQPDiv ) != 0 )
2365      {
2366        return;
2367      }
2368      bChromaSame     = true;
2369    }
2370   
2371    //===== copy transform coefficients =====
2372    UInt uiNumCoeffC    = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
2373    if( !bChromaSame )
2374    {
2375      uiNumCoeffC     >>= 2;
2376    }
2377    UInt uiNumCoeffIncC = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 ) + 2 );
2378    TCoeff* pcCoeffSrcU = m_ppcQTTempCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2379    TCoeff* pcCoeffSrcV = m_ppcQTTempCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2380    TCoeff* pcCoeffDstU = pcCU->getCoeffCb()              + ( uiNumCoeffIncC * uiAbsPartIdx );
2381    TCoeff* pcCoeffDstV = pcCU->getCoeffCr()              + ( uiNumCoeffIncC * uiAbsPartIdx );
2382    ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
2383    ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
2384#if ADAPTIVE_QP_SELECTION   
2385    Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2386    Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
2387    Int* pcArlCoeffDstU = pcCU->getArlCoeffCb()              + ( uiNumCoeffIncC * uiAbsPartIdx );
2388    Int* pcArlCoeffDstV = pcCU->getArlCoeffCr()              + ( uiNumCoeffIncC * uiAbsPartIdx );
2389    ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
2390    ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
2391#endif
2392   
2393    //===== copy reconstruction =====
2394    UInt uiLog2TrSizeChroma = ( bChromaSame ? uiLog2TrSize : uiLog2TrSize - 1 );
2395    m_pcQTTempTComYuv[ uiQTLayer ].copyPartToPartChroma( pcRecoYuv, uiAbsPartIdx, 1 << uiLog2TrSizeChroma, 1 << uiLog2TrSizeChroma );
2396  }
2397  else
2398  {
2399    UInt uiNumQPart  = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
2400    for( UInt uiPart = 0; uiPart < 4; uiPart++ )
2401    {
2402      xSetIntraResultChromaQT( pcCU, uiTrDepth + 1, uiAbsPartIdx + uiPart * uiNumQPart, pcRecoYuv );
2403    }
2404  }
2405}
2406
2407
2408Void
2409TEncSearch::preestChromaPredMode( TComDataCU* pcCU, 
2410                                 TComYuv*    pcOrgYuv, 
2411                                 TComYuv*    pcPredYuv )
2412{
2413  UInt  uiWidth     = pcCU->getWidth ( 0 ) >> 1;
2414  UInt  uiHeight    = pcCU->getHeight( 0 ) >> 1;
2415  UInt  uiStride    = pcOrgYuv ->getCStride();
2416  Pel*  piOrgU      = pcOrgYuv ->getCbAddr ( 0 );
2417  Pel*  piOrgV      = pcOrgYuv ->getCrAddr ( 0 );
2418  Pel*  piPredU     = pcPredYuv->getCbAddr ( 0 );
2419  Pel*  piPredV     = pcPredYuv->getCrAddr ( 0 );
2420 
2421  //===== init pattern =====
2422  Bool  bAboveAvail = false;
2423  Bool  bLeftAvail  = false;
2424  pcCU->getPattern()->initPattern         ( pcCU, 0, 0 );
2425  pcCU->getPattern()->initAdiPatternChroma( pcCU, 0, 0, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
2426  Int*  pPatChromaU = pcCU->getPattern()->getAdiCbBuf( uiWidth, uiHeight, m_piYuvExt );
2427  Int*  pPatChromaV = pcCU->getPattern()->getAdiCrBuf( uiWidth, uiHeight, m_piYuvExt );
2428 
2429  //===== get best prediction modes (using SAD) =====
2430  UInt  uiMinMode   = 0;
2431  UInt  uiMaxMode   = 4;
2432  UInt  uiBestMode  = MAX_UINT;
2433  UInt  uiMinSAD    = MAX_UINT;
2434  for( UInt uiMode  = uiMinMode; uiMode < uiMaxMode; uiMode++ )
2435  {
2436    //--- get prediction ---
2437    predIntraChromaAng( pPatChromaU, uiMode, piPredU, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
2438    predIntraChromaAng( pPatChromaV, uiMode, piPredV, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
2439   
2440    //--- get SAD ---
2441    UInt  uiSAD  = m_pcRdCost->calcHAD(g_bitDepthC, piOrgU, uiStride, piPredU, uiStride, uiWidth, uiHeight );
2442    uiSAD       += m_pcRdCost->calcHAD(g_bitDepthC, piOrgV, uiStride, piPredV, uiStride, uiWidth, uiHeight );
2443    //--- check ---
2444    if( uiSAD < uiMinSAD )
2445    {
2446      uiMinSAD   = uiSAD;
2447      uiBestMode = uiMode;
2448    }
2449  }
2450 
2451  //===== set chroma pred mode =====
2452  pcCU->setChromIntraDirSubParts( uiBestMode, 0, pcCU->getDepth( 0 ) );
2453}
2454
2455Void
2456TEncSearch::estIntraPredQT( TComDataCU* pcCU, 
2457                           TComYuv*    pcOrgYuv, 
2458                           TComYuv*    pcPredYuv, 
2459                           TComYuv*    pcResiYuv, 
2460                           TComYuv*    pcRecoYuv,
2461                           UInt&       ruiDistC,
2462                           Bool        bLumaOnly )
2463{
2464  UInt    uiDepth        = pcCU->getDepth(0);
2465  UInt    uiNumPU        = pcCU->getNumPartInter();
2466  UInt    uiInitTrDepth  = pcCU->getPartitionSize(0) == SIZE_2Nx2N ? 0 : 1;
2467  UInt    uiWidth        = pcCU->getWidth (0) >> uiInitTrDepth;
2468  UInt    uiHeight       = pcCU->getHeight(0) >> uiInitTrDepth;
2469  UInt    uiQNumParts    = pcCU->getTotalNumPart() >> 2;
2470  UInt    uiWidthBit     = pcCU->getIntraSizeIdx(0);
2471  UInt    uiOverallDistY = 0;
2472  UInt    uiOverallDistC = 0;
2473  UInt    CandNum;
2474  Double  CandCostList[ FAST_UDI_MAX_RDMODE_NUM ];
2475 
2476  //===== set QP and clear Cbf =====
2477  if ( pcCU->getSlice()->getPPS()->getUseDQP() == true)
2478  {
2479    pcCU->setQPSubParts( pcCU->getQP(0), 0, uiDepth );
2480  }
2481  else
2482  {
2483    pcCU->setQPSubParts( pcCU->getSlice()->getSliceQp(), 0, uiDepth );
2484  }
2485 
2486  //===== loop over partitions =====
2487  UInt uiPartOffset = 0;
2488  for( UInt uiPU = 0; uiPU < uiNumPU; uiPU++, uiPartOffset += uiQNumParts )
2489  {
2490    //===== init pattern for luma prediction =====
2491    Bool bAboveAvail = false;
2492    Bool bLeftAvail  = false;
2493    pcCU->getPattern()->initPattern   ( pcCU, uiInitTrDepth, uiPartOffset );
2494    pcCU->getPattern()->initAdiPattern( pcCU, uiPartOffset, uiInitTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
2495   
2496    //===== determine set of modes to be tested (using prediction signal only) =====
2497    Int numModesAvailable     = 35; //total number of Intra modes
2498    Pel* piOrg         = pcOrgYuv ->getLumaAddr( uiPU, uiWidth );
2499    Pel* piPred        = pcPredYuv->getLumaAddr( uiPU, uiWidth );
2500    UInt uiStride      = pcPredYuv->getStride();
2501    UInt uiRdModeList[FAST_UDI_MAX_RDMODE_NUM];
2502    Int numModesForFullRD = g_aucIntraModeNumFast[ uiWidthBit ];
2503   
2504    Bool doFastSearch = (numModesForFullRD != numModesAvailable);
2505    if (doFastSearch)
2506    {
2507      assert(numModesForFullRD < numModesAvailable);
2508
2509      for( Int i=0; i < numModesForFullRD; i++ ) 
2510      {
2511        CandCostList[ i ] = MAX_DOUBLE;
2512      }
2513      CandNum = 0;
2514     
2515      for( Int modeIdx = 0; modeIdx < numModesAvailable; modeIdx++ )
2516      {
2517        UInt uiMode = modeIdx;
2518
2519        predIntraLumaAng( pcCU->getPattern(), uiMode, piPred, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
2520       
2521        // use hadamard transform here
2522        UInt uiSad = m_pcRdCost->calcHAD(g_bitDepthY, piOrg, uiStride, piPred, uiStride, uiWidth, uiHeight );
2523       
2524        UInt   iModeBits = xModeBitsIntra( pcCU, uiMode, uiPU, uiPartOffset, uiDepth, uiInitTrDepth );
2525        Double cost      = (Double)uiSad + (Double)iModeBits * m_pcRdCost->getSqrtLambda();
2526       
2527        CandNum += xUpdateCandList( uiMode, cost, numModesForFullRD, uiRdModeList, CandCostList );
2528      }
2529   
2530#if FAST_UDI_USE_MPM
2531      Int uiPreds[3] = {-1, -1, -1};
2532      Int iMode = -1;
2533      Int numCand = pcCU->getIntraDirLumaPredictor( uiPartOffset, uiPreds, &iMode );
2534      if( iMode >= 0 )
2535      {
2536        numCand = iMode;
2537      }
2538     
2539      for( Int j=0; j < numCand; j++)
2540
2541      {
2542        Bool mostProbableModeIncluded = false;
2543        Int mostProbableMode = uiPreds[j];
2544       
2545        for( Int i=0; i < numModesForFullRD; i++)
2546        {
2547          mostProbableModeIncluded |= (mostProbableMode == uiRdModeList[i]);
2548        }
2549        if (!mostProbableModeIncluded)
2550        {
2551          uiRdModeList[numModesForFullRD++] = mostProbableMode;
2552        }
2553      }
2554#endif // FAST_UDI_USE_MPM
2555    }
2556    else
2557    {
2558      for( Int i=0; i < numModesForFullRD; i++)
2559      {
2560        uiRdModeList[i] = i;
2561      }
2562    }
2563   
2564    //===== check modes (using r-d costs) =====
2565#if HHI_RQT_INTRA_SPEEDUP_MOD
2566    UInt   uiSecondBestMode  = MAX_UINT;
2567    Double dSecondBestPUCost = MAX_DOUBLE;
2568#endif
2569   
2570    UInt    uiBestPUMode  = 0;
2571    UInt    uiBestPUDistY = 0;
2572    UInt    uiBestPUDistC = 0;
2573    Double  dBestPUCost   = MAX_DOUBLE;
2574    for( UInt uiMode = 0; uiMode < numModesForFullRD; uiMode++ )
2575    {
2576      // set luma prediction mode
2577      UInt uiOrgMode = uiRdModeList[uiMode];
2578     
2579      pcCU->setLumaIntraDirSubParts ( uiOrgMode, uiPartOffset, uiDepth + uiInitTrDepth );
2580     
2581      // set context models
2582      if( m_bUseSBACRD )
2583      {
2584        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
2585      }
2586     
2587      // determine residual for partition
2588      UInt   uiPUDistY = 0;
2589      UInt   uiPUDistC = 0;
2590      Double dPUCost   = 0.0;
2591#if HHI_RQT_INTRA_SPEEDUP
2592      xRecurIntraCodingQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcOrgYuv, pcPredYuv, pcResiYuv, uiPUDistY, uiPUDistC, true, dPUCost );
2593#else
2594      xRecurIntraCodingQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcOrgYuv, pcPredYuv, pcResiYuv, uiPUDistY, uiPUDistC, dPUCost );
2595#endif
2596     
2597      // check r-d cost
2598      if( dPUCost < dBestPUCost )
2599      {
2600#if HHI_RQT_INTRA_SPEEDUP_MOD
2601        uiSecondBestMode  = uiBestPUMode;
2602        dSecondBestPUCost = dBestPUCost;
2603#endif
2604        uiBestPUMode  = uiOrgMode;
2605        uiBestPUDistY = uiPUDistY;
2606        uiBestPUDistC = uiPUDistC;
2607        dBestPUCost   = dPUCost;
2608       
2609        xSetIntraResultQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcRecoYuv );
2610       
2611        UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth(0) + uiInitTrDepth ) << 1 );
2612        ::memcpy( m_puhQTTempTrIdx,  pcCU->getTransformIdx()       + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2613        ::memcpy( m_puhQTTempCbf[0], pcCU->getCbf( TEXT_LUMA     ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2614        ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2615        ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2616        ::memcpy( m_puhQTTempTransformSkipFlag[0], pcCU->getTransformSkip(TEXT_LUMA)     + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2617        ::memcpy( m_puhQTTempTransformSkipFlag[1], pcCU->getTransformSkip(TEXT_CHROMA_U) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2618        ::memcpy( m_puhQTTempTransformSkipFlag[2], pcCU->getTransformSkip(TEXT_CHROMA_V) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2619      }
2620#if HHI_RQT_INTRA_SPEEDUP_MOD
2621      else if( dPUCost < dSecondBestPUCost )
2622      {
2623        uiSecondBestMode  = uiOrgMode;
2624        dSecondBestPUCost = dPUCost;
2625      }
2626#endif
2627    } // Mode loop
2628   
2629#if HHI_RQT_INTRA_SPEEDUP
2630#if HHI_RQT_INTRA_SPEEDUP_MOD
2631    for( UInt ui =0; ui < 2; ++ui )
2632#endif
2633    {
2634#if HHI_RQT_INTRA_SPEEDUP_MOD
2635      UInt uiOrgMode   = ui ? uiSecondBestMode  : uiBestPUMode;
2636      if( uiOrgMode == MAX_UINT )
2637      {
2638        break;
2639      }
2640#else
2641      UInt uiOrgMode = uiBestPUMode;
2642#endif
2643     
2644      pcCU->setLumaIntraDirSubParts ( uiOrgMode, uiPartOffset, uiDepth + uiInitTrDepth );
2645     
2646      // set context models
2647      if( m_bUseSBACRD )
2648      {
2649        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
2650      }
2651     
2652      // determine residual for partition
2653      UInt   uiPUDistY = 0;
2654      UInt   uiPUDistC = 0;
2655      Double dPUCost   = 0.0;
2656      xRecurIntraCodingQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcOrgYuv, pcPredYuv, pcResiYuv, uiPUDistY, uiPUDistC, false, dPUCost );
2657     
2658      // check r-d cost
2659      if( dPUCost < dBestPUCost )
2660      {
2661        uiBestPUMode  = uiOrgMode;
2662        uiBestPUDistY = uiPUDistY;
2663        uiBestPUDistC = uiPUDistC;
2664        dBestPUCost   = dPUCost;
2665       
2666        xSetIntraResultQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcRecoYuv );
2667       
2668        UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth(0) + uiInitTrDepth ) << 1 );
2669        ::memcpy( m_puhQTTempTrIdx,  pcCU->getTransformIdx()       + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2670        ::memcpy( m_puhQTTempCbf[0], pcCU->getCbf( TEXT_LUMA     ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2671        ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2672        ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2673        ::memcpy( m_puhQTTempTransformSkipFlag[0], pcCU->getTransformSkip(TEXT_LUMA)     + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2674        ::memcpy( m_puhQTTempTransformSkipFlag[1], pcCU->getTransformSkip(TEXT_CHROMA_U) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2675        ::memcpy( m_puhQTTempTransformSkipFlag[2], pcCU->getTransformSkip(TEXT_CHROMA_V) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
2676      }
2677    } // Mode loop
2678#endif
2679   
2680    //--- update overall distortion ---
2681    uiOverallDistY += uiBestPUDistY;
2682    uiOverallDistC += uiBestPUDistC;
2683   
2684    //--- update transform index and cbf ---
2685    UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth(0) + uiInitTrDepth ) << 1 );
2686    ::memcpy( pcCU->getTransformIdx()       + uiPartOffset, m_puhQTTempTrIdx,  uiQPartNum * sizeof( UChar ) );
2687    ::memcpy( pcCU->getCbf( TEXT_LUMA     ) + uiPartOffset, m_puhQTTempCbf[0], uiQPartNum * sizeof( UChar ) );
2688    ::memcpy( pcCU->getCbf( TEXT_CHROMA_U ) + uiPartOffset, m_puhQTTempCbf[1], uiQPartNum * sizeof( UChar ) );
2689    ::memcpy( pcCU->getCbf( TEXT_CHROMA_V ) + uiPartOffset, m_puhQTTempCbf[2], uiQPartNum * sizeof( UChar ) );
2690    ::memcpy( pcCU->getTransformSkip(TEXT_LUMA)     + uiPartOffset, m_puhQTTempTransformSkipFlag[0], uiQPartNum * sizeof( UChar ) );
2691    ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_U) + uiPartOffset, m_puhQTTempTransformSkipFlag[1], uiQPartNum * sizeof( UChar ) );
2692    ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_V) + uiPartOffset, m_puhQTTempTransformSkipFlag[2], uiQPartNum * sizeof( UChar ) );
2693    //--- set reconstruction for next intra prediction blocks ---
2694    if( uiPU != uiNumPU - 1 )
2695    {
2696      Bool bSkipChroma  = false;
2697      Bool bChromaSame  = false;
2698      UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> ( pcCU->getDepth(0) + uiInitTrDepth ) ] + 2;
2699      if( !bLumaOnly && uiLog2TrSize == 2 )
2700      {
2701        assert( uiInitTrDepth  > 0 );
2702        bSkipChroma  = ( uiPU != 0 );
2703        bChromaSame  = true;
2704      }
2705     
2706      UInt    uiCompWidth   = pcCU->getWidth ( 0 ) >> uiInitTrDepth;
2707      UInt    uiCompHeight  = pcCU->getHeight( 0 ) >> uiInitTrDepth;
2708      UInt    uiZOrder      = pcCU->getZorderIdxInCU() + uiPartOffset;
2709      Pel*    piDes         = pcCU->getPic()->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), uiZOrder );
2710      UInt    uiDesStride   = pcCU->getPic()->getPicYuvRec()->getStride();
2711      Pel*    piSrc         = pcRecoYuv->getLumaAddr( uiPartOffset );
2712      UInt    uiSrcStride   = pcRecoYuv->getStride();
2713      for( UInt uiY = 0; uiY < uiCompHeight; uiY++, piSrc += uiSrcStride, piDes += uiDesStride )
2714      {
2715        for( UInt uiX = 0; uiX < uiCompWidth; uiX++ )
2716        {
2717          piDes[ uiX ] = piSrc[ uiX ];
2718        }
2719      }
2720      if( !bLumaOnly && !bSkipChroma )
2721      {
2722        if( !bChromaSame )
2723        {
2724          uiCompWidth   >>= 1;
2725          uiCompHeight  >>= 1;
2726        }
2727        piDes         = pcCU->getPic()->getPicYuvRec()->getCbAddr( pcCU->getAddr(), uiZOrder );
2728        uiDesStride   = pcCU->getPic()->getPicYuvRec()->getCStride();
2729        piSrc         = pcRecoYuv->getCbAddr( uiPartOffset );
2730        uiSrcStride   = pcRecoYuv->getCStride();
2731        for( UInt uiY = 0; uiY < uiCompHeight; uiY++, piSrc += uiSrcStride, piDes += uiDesStride )
2732        {
2733          for( UInt uiX = 0; uiX < uiCompWidth; uiX++ )
2734          {
2735            piDes[ uiX ] = piSrc[ uiX ];
2736          }
2737        }
2738        piDes         = pcCU->getPic()->getPicYuvRec()->getCrAddr( pcCU->getAddr(), uiZOrder );
2739        piSrc         = pcRecoYuv->getCrAddr( uiPartOffset );
2740        for( UInt uiY = 0; uiY < uiCompHeight; uiY++, piSrc += uiSrcStride, piDes += uiDesStride )
2741        {
2742          for( UInt uiX = 0; uiX < uiCompWidth; uiX++ )
2743          {
2744            piDes[ uiX ] = piSrc[ uiX ];
2745          }
2746        }
2747      }
2748    }
2749   
2750    //=== update PU data ====
2751    pcCU->setLumaIntraDirSubParts     ( uiBestPUMode, uiPartOffset, uiDepth + uiInitTrDepth );
2752    pcCU->copyToPic                   ( uiDepth, uiPU, uiInitTrDepth );
2753  } // PU loop
2754 
2755 
2756  if( uiNumPU > 1 )
2757  { // set Cbf for all blocks
2758    UInt uiCombCbfY = 0;
2759    UInt uiCombCbfU = 0;
2760    UInt uiCombCbfV = 0;
2761    UInt uiPartIdx  = 0;
2762    for( UInt uiPart = 0; uiPart < 4; uiPart++, uiPartIdx += uiQNumParts )
2763    {
2764      uiCombCbfY |= pcCU->getCbf( uiPartIdx, TEXT_LUMA,     1 );
2765      uiCombCbfU |= pcCU->getCbf( uiPartIdx, TEXT_CHROMA_U, 1 );
2766      uiCombCbfV |= pcCU->getCbf( uiPartIdx, TEXT_CHROMA_V, 1 );
2767    }
2768    for( UInt uiOffs = 0; uiOffs < 4 * uiQNumParts; uiOffs++ )
2769    {
2770      pcCU->getCbf( TEXT_LUMA     )[ uiOffs ] |= uiCombCbfY;
2771      pcCU->getCbf( TEXT_CHROMA_U )[ uiOffs ] |= uiCombCbfU;
2772      pcCU->getCbf( TEXT_CHROMA_V )[ uiOffs ] |= uiCombCbfV;
2773    }
2774  }
2775 
2776  //===== reset context models =====
2777  if(m_bUseSBACRD)
2778  {
2779    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2780  }
2781 
2782  //===== set distortion (rate and r-d costs are determined later) =====
2783  ruiDistC                   = uiOverallDistC;
2784  pcCU->getTotalDistortion() = uiOverallDistY + uiOverallDistC;
2785}
2786
2787
2788
2789Void
2790TEncSearch::estIntraPredChromaQT( TComDataCU* pcCU, 
2791                                 TComYuv*    pcOrgYuv, 
2792                                 TComYuv*    pcPredYuv, 
2793                                 TComYuv*    pcResiYuv, 
2794                                 TComYuv*    pcRecoYuv,
2795                                 UInt        uiPreCalcDistC )
2796{
2797  UInt    uiDepth     = pcCU->getDepth(0);
2798  UInt    uiBestMode  = 0;
2799  UInt    uiBestDist  = 0;
2800  Double  dBestCost   = MAX_DOUBLE;
2801 
2802  //----- init mode list -----
2803  UInt  uiMinMode = 0;
2804  UInt  uiModeList[ NUM_CHROMA_MODE ];
2805  pcCU->getAllowedChromaDir( 0, uiModeList );
2806  UInt  uiMaxMode = NUM_CHROMA_MODE;
2807
2808  //----- check chroma modes -----
2809  for( UInt uiMode = uiMinMode; uiMode < uiMaxMode; uiMode++ )
2810  {
2811    //----- restore context models -----
2812    if( m_bUseSBACRD )
2813    {
2814      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
2815    }
2816   
2817    //----- chroma coding -----
2818    UInt    uiDist = 0;
2819    pcCU->setChromIntraDirSubParts  ( uiModeList[uiMode], 0, uiDepth );
2820    xRecurIntraChromaCodingQT       ( pcCU,   0, 0, pcOrgYuv, pcPredYuv, pcResiYuv, uiDist );
2821    if( m_bUseSBACRD && pcCU->getSlice()->getPPS()->getUseTransformSkip() )
2822    {
2823      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
2824    }
2825    UInt    uiBits = xGetIntraBitsQT( pcCU,   0, 0, false, true, false );
2826    Double  dCost  = m_pcRdCost->calcRdCost( uiBits, uiDist );
2827   
2828    //----- compare -----
2829    if( dCost < dBestCost )
2830    {
2831      dBestCost   = dCost;
2832      uiBestDist  = uiDist;
2833      uiBestMode  = uiModeList[uiMode];
2834      UInt  uiQPN = pcCU->getPic()->getNumPartInCU() >> ( uiDepth << 1 );
2835      xSetIntraResultChromaQT( pcCU, 0, 0, pcRecoYuv );
2836      ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ), uiQPN * sizeof( UChar ) );
2837      ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ), uiQPN * sizeof( UChar ) );
2838      ::memcpy( m_puhQTTempTransformSkipFlag[1], pcCU->getTransformSkip( TEXT_CHROMA_U ), uiQPN * sizeof( UChar ) );
2839      ::memcpy( m_puhQTTempTransformSkipFlag[2], pcCU->getTransformSkip( TEXT_CHROMA_V ), uiQPN * sizeof( UChar ) );
2840    }
2841  }
2842 
2843  //----- set data -----
2844  UInt  uiQPN = pcCU->getPic()->getNumPartInCU() >> ( uiDepth << 1 );
2845  ::memcpy( pcCU->getCbf( TEXT_CHROMA_U ), m_puhQTTempCbf[1], uiQPN * sizeof( UChar ) );
2846  ::memcpy( pcCU->getCbf( TEXT_CHROMA_V ), m_puhQTTempCbf[2], uiQPN * sizeof( UChar ) );
2847  ::memcpy( pcCU->getTransformSkip( TEXT_CHROMA_U ), m_puhQTTempTransformSkipFlag[1], uiQPN * sizeof( UChar ) );
2848  ::memcpy( pcCU->getTransformSkip( TEXT_CHROMA_V ), m_puhQTTempTransformSkipFlag[2], uiQPN * sizeof( UChar ) );
2849  pcCU->setChromIntraDirSubParts( uiBestMode, 0, uiDepth );
2850  pcCU->getTotalDistortion      () += uiBestDist - uiPreCalcDistC;
2851 
2852  //----- restore context models -----
2853  if( m_bUseSBACRD )
2854  {
2855    m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
2856  }
2857}
2858
2859/** Function for encoding and reconstructing luma/chroma samples of a PCM mode CU.
2860 * \param pcCU pointer to current CU
2861 * \param uiAbsPartIdx part index
2862 * \param piOrg pointer to original sample arrays
2863 * \param piPCM pointer to PCM code arrays
2864 * \param piPred pointer to prediction signal arrays
2865 * \param piResi pointer to residual signal arrays
2866 * \param piReco pointer to reconstructed sample arrays
2867 * \param uiStride stride of the original/prediction/residual sample arrays
2868 * \param uiWidth block width
2869 * \param uiHeight block height
2870 * \param ttText texture component type
2871 * \returns Void
2872 */
2873Void TEncSearch::xEncPCM (TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piOrg, Pel* piPCM, Pel* piPred, Pel* piResi, Pel* piReco, UInt uiStride, UInt uiWidth, UInt uiHeight, TextType eText )
2874{
2875  UInt uiX, uiY;
2876  UInt uiReconStride;
2877  Pel* pOrg  = piOrg;
2878  Pel* pPCM  = piPCM;
2879  Pel* pPred = piPred;
2880  Pel* pResi = piResi;
2881  Pel* pReco = piReco;
2882  Pel* pRecoPic;
2883  Int shiftPcm;
2884
2885  if( eText == TEXT_LUMA)
2886  {
2887    uiReconStride = pcCU->getPic()->getPicYuvRec()->getStride();
2888    pRecoPic      = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
2889    shiftPcm = g_bitDepthY - pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
2890  }
2891  else
2892  {
2893    uiReconStride = pcCU->getPic()->getPicYuvRec()->getCStride();
2894
2895    if( eText == TEXT_CHROMA_U )
2896    {
2897      pRecoPic = pcCU->getPic()->getPicYuvRec()->getCbAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
2898    }
2899    else
2900    {
2901      pRecoPic = pcCU->getPic()->getPicYuvRec()->getCrAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
2902    }
2903    shiftPcm = g_bitDepthC - pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
2904  }
2905
2906  // Reset pred and residual
2907  for( uiY = 0; uiY < uiHeight; uiY++ )
2908  {
2909    for( uiX = 0; uiX < uiWidth; uiX++ )
2910    {
2911      pPred[uiX] = 0;
2912      pResi[uiX] = 0;
2913    }
2914    pPred += uiStride;
2915    pResi += uiStride;
2916  }
2917
2918  // Encode
2919  for( uiY = 0; uiY < uiHeight; uiY++ )
2920  {
2921    for( uiX = 0; uiX < uiWidth; uiX++ )
2922    {
2923      pPCM[uiX] = pOrg[uiX]>> shiftPcm;
2924    }
2925    pPCM += uiWidth;
2926    pOrg += uiStride;
2927  }
2928
2929  pPCM  = piPCM;
2930
2931  // Reconstruction
2932  for( uiY = 0; uiY < uiHeight; uiY++ )
2933  {
2934    for( uiX = 0; uiX < uiWidth; uiX++ )
2935    {
2936      pReco   [uiX] = pPCM[uiX]<< shiftPcm;
2937      pRecoPic[uiX] = pReco[uiX];
2938    }
2939    pPCM += uiWidth;
2940    pReco += uiStride;
2941    pRecoPic += uiReconStride;
2942  }
2943}
2944
2945/**  Function for PCM mode estimation.
2946 * \param pcCU
2947 * \param pcOrgYuv
2948 * \param rpcPredYuv
2949 * \param rpcResiYuv
2950 * \param rpcRecoYuv
2951 * \returns Void
2952 */
2953Void TEncSearch::IPCMSearch( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv )
2954{
2955  UInt   uiDepth        = pcCU->getDepth(0);
2956  UInt   uiWidth        = pcCU->getWidth(0);
2957  UInt   uiHeight       = pcCU->getHeight(0);
2958  UInt   uiStride       = rpcPredYuv->getStride();
2959  UInt   uiStrideC      = rpcPredYuv->getCStride();
2960  UInt   uiWidthC       = uiWidth  >> 1;
2961  UInt   uiHeightC      = uiHeight >> 1;
2962  UInt   uiDistortion = 0;
2963  UInt   uiBits;
2964
2965  Double dCost;
2966
2967  Pel*    pOrig;
2968  Pel*    pResi;
2969  Pel*    pReco;
2970  Pel*    pPred;
2971  Pel*    pPCM;
2972
2973  UInt uiAbsPartIdx = 0;
2974
2975  UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
2976  UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
2977  UInt uiChromaOffset = uiLumaOffset>>2;
2978
2979  // Luminance
2980  pOrig    = pcOrgYuv->getLumaAddr(0, uiWidth);
2981  pResi    = rpcResiYuv->getLumaAddr(0, uiWidth);
2982  pPred    = rpcPredYuv->getLumaAddr(0, uiWidth);
2983  pReco    = rpcRecoYuv->getLumaAddr(0, uiWidth);
2984  pPCM     = pcCU->getPCMSampleY() + uiLumaOffset;
2985
2986  xEncPCM ( pcCU, 0, pOrig, pPCM, pPred, pResi, pReco, uiStride, uiWidth, uiHeight, TEXT_LUMA );
2987
2988  // Chroma U
2989  pOrig    = pcOrgYuv->getCbAddr();
2990  pResi    = rpcResiYuv->getCbAddr();
2991  pPred    = rpcPredYuv->getCbAddr();
2992  pReco    = rpcRecoYuv->getCbAddr();
2993  pPCM     = pcCU->getPCMSampleCb() + uiChromaOffset;
2994
2995  xEncPCM ( pcCU, 0, pOrig, pPCM, pPred, pResi, pReco, uiStrideC, uiWidthC, uiHeightC, TEXT_CHROMA_U );
2996
2997  // Chroma V
2998  pOrig    = pcOrgYuv->getCrAddr();
2999  pResi    = rpcResiYuv->getCrAddr();
3000  pPred    = rpcPredYuv->getCrAddr();
3001  pReco    = rpcRecoYuv->getCrAddr();
3002  pPCM     = pcCU->getPCMSampleCr() + uiChromaOffset;
3003
3004  xEncPCM ( pcCU, 0, pOrig, pPCM, pPred, pResi, pReco, uiStrideC, uiWidthC, uiHeightC, TEXT_CHROMA_V );
3005
3006  m_pcEntropyCoder->resetBits();
3007  xEncIntraHeader ( pcCU, uiDepth, uiAbsPartIdx, true, false);
3008  uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
3009
3010  dCost = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
3011
3012  if(m_bUseSBACRD)
3013  {
3014    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
3015  }
3016
3017  pcCU->getTotalBits()       = uiBits;
3018  pcCU->getTotalCost()       = dCost;
3019  pcCU->getTotalDistortion() = uiDistortion;
3020
3021  pcCU->copyToPic(uiDepth, 0, 0);
3022}
3023
3024Void TEncSearch::xGetInterPredictionError( TComDataCU* pcCU, TComYuv* pcYuvOrg, Int iPartIdx, UInt& ruiErr, Bool bHadamard )
3025{
3026  motionCompensation( pcCU, &m_tmpYuvPred, REF_PIC_LIST_X, iPartIdx );
3027
3028  UInt uiAbsPartIdx = 0;
3029  Int iWidth = 0;
3030  Int iHeight = 0;
3031  pcCU->getPartIndexAndSize( iPartIdx, uiAbsPartIdx, iWidth, iHeight );
3032
3033  DistParam cDistParam;
3034
3035  cDistParam.bApplyWeight = false;
3036
3037  m_pcRdCost->setDistParam( cDistParam, g_bitDepthY,
3038                            pcYuvOrg->getLumaAddr( uiAbsPartIdx ), pcYuvOrg->getStride(), 
3039                            m_tmpYuvPred .getLumaAddr( uiAbsPartIdx ), m_tmpYuvPred .getStride(), 
3040#if NS_HAD
3041                            iWidth, iHeight, m_pcEncCfg->getUseHADME(), m_pcEncCfg->getUseNSQT() );
3042#else
3043                            iWidth, iHeight, m_pcEncCfg->getUseHADME() );
3044#endif
3045  ruiErr = cDistParam.DistFunc( &cDistParam );
3046}
3047
3048/** estimation of best merge coding
3049 * \param pcCU
3050 * \param pcYuvOrg
3051 * \param iPUIdx
3052 * \param uiInterDir
3053 * \param pacMvField
3054 * \param uiMergeIndex
3055 * \param ruiCost
3056 * \param ruiBits
3057 * \param puhNeighCands
3058 * \param bValid
3059 * \returns Void
3060 */
3061Void TEncSearch::xMergeEstimation( TComDataCU* pcCU, TComYuv* pcYuvOrg, Int iPUIdx, UInt& uiInterDir, TComMvField* pacMvField, UInt& uiMergeIndex, UInt& ruiCost, TComMvField* cMvFieldNeighbours, UChar* uhInterDirNeighbours, Int& numValidMergeCand )
3062{
3063  UInt uiAbsPartIdx = 0;
3064  Int iWidth = 0;
3065  Int iHeight = 0; 
3066
3067#if REF_IDX_FRAMEWORK  // HM bug fix
3068  for( UInt uiMergeCand = 0; uiMergeCand < pcCU->getSlice()->getMaxNumMergeCand(); ++uiMergeCand )
3069  {
3070    uhInterDirNeighbours[uiMergeCand] = 0;
3071    cMvFieldNeighbours[0 + 2*uiMergeCand].setMvField(TComMv(), NOT_VALID);
3072    cMvFieldNeighbours[1 + 2*uiMergeCand].setMvField(TComMv(), NOT_VALID);
3073  }
3074#endif
3075  pcCU->getPartIndexAndSize( iPUIdx, uiAbsPartIdx, iWidth, iHeight );
3076  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
3077  PartSize partSize = pcCU->getPartitionSize( 0 );
3078  if ( pcCU->getSlice()->getPPS()->getLog2ParallelMergeLevelMinus2() && partSize != SIZE_2Nx2N && pcCU->getWidth( 0 ) <= 8 )
3079  {
3080    pcCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
3081    if ( iPUIdx == 0 )
3082    {
3083      pcCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
3084    }
3085    pcCU->setPartSizeSubParts( partSize, 0, uiDepth );
3086  }
3087  else
3088  {
3089    pcCU->getInterMergeCandidates( uiAbsPartIdx, iPUIdx, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
3090  }
3091  xRestrictBipredMergeCand( pcCU, iPUIdx, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
3092
3093  ruiCost = MAX_UINT;
3094  for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
3095  {
3096#if REF_IDX_ME_ZEROMV
3097    Bool bZeroMVILR = pcCU->xCheckZeroMVILRMerge(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]);
3098    if(bZeroMVILR)
3099    {
3100#endif
3101      UInt uiCostCand = MAX_UINT;
3102      UInt uiBitsCand = 0;
3103     
3104      PartSize ePartSize = pcCU->getPartitionSize( 0 );
3105
3106      pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], ePartSize, uiAbsPartIdx, 0, iPUIdx );
3107      pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], ePartSize, uiAbsPartIdx, 0, iPUIdx );
3108
3109      xGetInterPredictionError( pcCU, pcYuvOrg, iPUIdx, uiCostCand, m_pcEncCfg->getUseHADME() );
3110      uiBitsCand = uiMergeCand + 1;
3111      if (uiMergeCand == m_pcEncCfg->getMaxNumMergeCand() -1)
3112      {
3113         uiBitsCand--;
3114      }
3115      uiCostCand = uiCostCand + m_pcRdCost->getCost( uiBitsCand );
3116      if ( uiCostCand < ruiCost )
3117      {
3118        ruiCost = uiCostCand;
3119        pacMvField[0] = cMvFieldNeighbours[0 + 2*uiMergeCand];
3120        pacMvField[1] = cMvFieldNeighbours[1 + 2*uiMergeCand];
3121        uiInterDir = uhInterDirNeighbours[uiMergeCand];
3122        uiMergeIndex = uiMergeCand;
3123      }
3124#if REF_IDX_ME_ZEROMV
3125    }
3126#endif
3127  }
3128}
3129
3130/** convert bi-pred merge candidates to uni-pred
3131 * \param pcCU
3132 * \param puIdx
3133 * \param mvFieldNeighbours
3134 * \param interDirNeighbours
3135 * \param numValidMergeCand
3136 * \returns Void
3137 */
3138Void TEncSearch::xRestrictBipredMergeCand( TComDataCU* pcCU, UInt puIdx, TComMvField* mvFieldNeighbours, UChar* interDirNeighbours, Int numValidMergeCand )
3139{
3140  if ( pcCU->isBipredRestriction(puIdx) )
3141  {
3142    for( UInt mergeCand = 0; mergeCand < numValidMergeCand; ++mergeCand )
3143    {
3144      if ( interDirNeighbours[mergeCand] == 3 )
3145      {
3146        interDirNeighbours[mergeCand] = 1;
3147        mvFieldNeighbours[(mergeCand << 1) + 1].setMvField(TComMv(0,0), -1);
3148      }
3149    }
3150  }
3151}
3152
3153/** search of the best candidate for inter prediction
3154 * \param pcCU
3155 * \param pcOrgYuv
3156 * \param rpcPredYuv
3157 * \param rpcResiYuv
3158 * \param rpcRecoYuv
3159 * \param bUseRes
3160 * \returns Void
3161 */
3162#if AMP_MRG
3163Void TEncSearch::predInterSearch( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv, Bool bUseRes, Bool bUseMRG )
3164#else
3165Void TEncSearch::predInterSearch( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv, Bool bUseRes )
3166#endif
3167{
3168  m_acYuvPred[0].clear();
3169  m_acYuvPred[1].clear();
3170  m_cYuvPredTemp.clear();
3171  rpcPredYuv->clear();
3172 
3173  if ( !bUseRes )
3174  {
3175    rpcResiYuv->clear();
3176  }
3177 
3178  rpcRecoYuv->clear();
3179 
3180  TComMv        cMvSrchRngLT;
3181  TComMv        cMvSrchRngRB;
3182 
3183  TComMv        cMvZero;
3184  TComMv        TempMv; //kolya
3185 
3186  TComMv        cMv[2];
3187  TComMv        cMvBi[2];
3188  TComMv        cMvTemp[2][33];
3189 
3190  Int           iNumPart    = pcCU->getNumPartInter();
3191  Int           iNumPredDir = pcCU->getSlice()->isInterP() ? 1 : 2;
3192 
3193  TComMv        cMvPred[2][33];
3194 
3195  TComMv        cMvPredBi[2][33];
3196  Int           aaiMvpIdxBi[2][33];
3197 
3198  Int           aaiMvpIdx[2][33];
3199  Int           aaiMvpNum[2][33];
3200 
3201  AMVPInfo aacAMVPInfo[2][33];
3202 
3203  Int           iRefIdx[2]={0,0}; //If un-initialized, may cause SEGV in bi-directional prediction iterative stage.
3204  Int           iRefIdxBi[2];
3205 
3206  UInt          uiPartAddr;
3207  Int           iRoiWidth, iRoiHeight;
3208 
3209  UInt          uiMbBits[3] = {1, 1, 0};
3210 
3211  UInt          uiLastMode = 0;
3212  Int           iRefStart, iRefEnd;
3213 
3214  PartSize      ePartSize = pcCU->getPartitionSize( 0 );
3215
3216  Int           bestBiPRefIdxL1 = 0;
3217  Int           bestBiPMvpL1 = 0;
3218  UInt          biPDistTemp = MAX_INT;
3219
3220#if ZERO_MVD_EST
3221  Int           aiZeroMvdMvpIdx[2] = {-1, -1};
3222  Int           aiZeroMvdRefIdx[2] = {0, 0};
3223  Int           iZeroMvdDir = -1;
3224#endif
3225
3226  TComMvField cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
3227  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
3228  Int numValidMergeCand = 0 ;
3229
3230  for ( Int iPartIdx = 0; iPartIdx < iNumPart; iPartIdx++ )
3231  {
3232    UInt          uiCost[2] = { MAX_UINT, MAX_UINT };
3233    UInt          uiCostBi  =   MAX_UINT;
3234    UInt          uiCostTemp;
3235   
3236    UInt          uiBits[3];
3237    UInt          uiBitsTemp;
3238#if ZERO_MVD_EST
3239    UInt          uiZeroMvdCost = MAX_UINT;
3240    UInt          uiZeroMvdCostTemp;
3241    UInt          uiZeroMvdBitsTemp;
3242    UInt          uiZeroMvdDistTemp = MAX_UINT;
3243    UInt          auiZeroMvdBits[3];
3244#endif
3245    UInt          bestBiPDist = MAX_INT;
3246
3247    UInt          uiCostTempL0[MAX_NUM_REF];
3248    for (Int iNumRef=0; iNumRef < MAX_NUM_REF; iNumRef++) uiCostTempL0[iNumRef] = MAX_UINT;
3249    UInt          uiBitsTempL0[MAX_NUM_REF];
3250
3251#if (ENCODER_FAST_MODE)
3252    Bool          testILR;
3253#endif
3254
3255    xGetBlkBits( ePartSize, pcCU->getSlice()->isInterP(), iPartIdx, uiLastMode, uiMbBits);
3256   
3257    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
3258   
3259#if AMP_MRG
3260    Bool bTestNormalMC = true;
3261   
3262    if ( bUseMRG && pcCU->getWidth( 0 ) > 8 && iNumPart == 2 )
3263    {
3264      bTestNormalMC = false;
3265    }
3266   
3267    if (bTestNormalMC)
3268    {
3269#endif
3270
3271    //  Uni-directional prediction
3272    for ( Int iRefList = 0; iRefList < iNumPredDir; iRefList++ )
3273    {
3274      RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
3275     
3276#if (ENCODER_FAST_MODE)
3277      if (pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1)
3278      {
3279        testILR = false;
3280      }
3281      else
3282      {
3283        testILR = true;
3284      }
3285#endif
3286
3287      for ( Int iRefIdxTemp = 0; iRefIdxTemp < pcCU->getSlice()->getNumRefIdx(eRefPicList); iRefIdxTemp++ )
3288      {
3289#if (ENCODER_FAST_MODE)
3290        TComPic* pcPic    = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxTemp );
3291        if( !testILR && pcPic->getIsILR() && (ePartSize == SIZE_2Nx2N) ) 
3292        {
3293          continue;
3294        }
3295#endif
3296
3297        uiBitsTemp = uiMbBits[iRefList];
3298        if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
3299        {
3300          uiBitsTemp += iRefIdxTemp+1;
3301          if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
3302        }
3303#if ZERO_MVD_EST
3304        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp, &uiZeroMvdDistTemp);
3305#else
3306        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp);
3307#endif
3308        aaiMvpIdx[iRefList][iRefIdxTemp] = pcCU->getMVPIdx(eRefPicList, uiPartAddr);
3309        aaiMvpNum[iRefList][iRefIdxTemp] = pcCU->getMVPNum(eRefPicList, uiPartAddr);
3310       
3311        if(pcCU->getSlice()->getMvdL1ZeroFlag() && iRefList==1 && biPDistTemp < bestBiPDist)
3312        {
3313#if REF_IDX_ME_ZEROMV
3314          Bool bZeroMVILR = pcCU->xCheckZeroMVILRMvdL1Zero(iRefList, iRefIdxTemp, aaiMvpIdx[iRefList][iRefIdxTemp]);
3315          if(bZeroMVILR)
3316          {
3317#endif
3318          bestBiPDist = biPDistTemp;
3319          bestBiPMvpL1 = aaiMvpIdx[iRefList][iRefIdxTemp];
3320          bestBiPRefIdxL1 = iRefIdxTemp;
3321#if REF_IDX_ME_ZEROMV
3322          }
3323#endif
3324        }
3325
3326        uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
3327#if ZERO_MVD_EST
3328        if ((iRefList != 1 || !pcCU->getSlice()->getNoBackPredFlag()) &&
3329            (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) <= 0 || pcCU->getSlice()->getRefIdxOfLC(eRefPicList, iRefIdxTemp)>=0))
3330        {
3331          uiZeroMvdBitsTemp = uiBitsTemp;
3332          uiZeroMvdBitsTemp += 2; //zero mvd bits
3333
3334          m_pcRdCost->getMotionCost( 1, 0 );
3335          uiZeroMvdCostTemp = uiZeroMvdDistTemp + m_pcRdCost->getCost(uiZeroMvdBitsTemp);
3336
3337          if (uiZeroMvdCostTemp < uiZeroMvdCost)
3338          {
3339            uiZeroMvdCost = uiZeroMvdCostTemp;
3340            iZeroMvdDir = iRefList + 1;
3341            aiZeroMvdRefIdx[iRefList] = iRefIdxTemp;
3342            aiZeroMvdMvpIdx[iRefList] = aaiMvpIdx[iRefList][iRefIdxTemp];
3343            auiZeroMvdBits[iRefList] = uiZeroMvdBitsTemp;
3344          }         
3345        }
3346#endif
3347       
3348#if GPB_SIMPLE_UNI
3349        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0)
3350        {
3351          if ( iRefList && ( pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && !pcCU->getSlice()->getNoBackPredFlag() && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)>=0 ) ) )
3352            {
3353              if ( pcCU->getSlice()->getNoBackPredFlag() )
3354              {
3355                cMvTemp[1][iRefIdxTemp] = cMvTemp[0][iRefIdxTemp];
3356                uiCostTemp = uiCostTempL0[iRefIdxTemp];
3357                /*first subtract the bit-rate part of the cost of the other list*/
3358                uiCostTemp -= m_pcRdCost->getCost( uiBitsTempL0[iRefIdxTemp] );
3359              }
3360              else
3361              {
3362                cMvTemp[1][iRefIdxTemp] = cMvTemp[0][pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)]; 
3363                uiCostTemp = uiCostTempL0[pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)];
3364                /*first subtract the bit-rate part of the cost of the other list*/
3365                uiCostTemp -= m_pcRdCost->getCost( uiBitsTempL0[pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)] );
3366              }
3367              /*correct the bit-rate part of the current ref*/
3368              m_pcRdCost->setPredictor  ( cMvPred[iRefList][iRefIdxTemp] );
3369              uiBitsTemp += m_pcRdCost->getBits( cMvTemp[1][iRefIdxTemp].getHor(), cMvTemp[1][iRefIdxTemp].getVer() );
3370              /*calculate the correct cost*/
3371              uiCostTemp += m_pcRdCost->getCost( uiBitsTemp );
3372            }
3373            else
3374            {
3375              xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
3376            }
3377        }
3378        else
3379        {
3380          if (iRefList && pcCU->getSlice()->getNoBackPredFlag())
3381          {
3382            uiCostTemp = MAX_UINT;
3383            cMvTemp[1][iRefIdxTemp] = cMvTemp[0][iRefIdxTemp];
3384          }
3385          else
3386          { 
3387            xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
3388          }       
3389        }
3390#else
3391        xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
3392#endif
3393        xCopyAMVPInfo(pcCU->getCUMvField(eRefPicList)->getAMVPInfo(), &aacAMVPInfo[iRefList][iRefIdxTemp]); // must always be done ( also when AMVP_MODE = AM_NONE )
3394        xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
3395
3396        if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && !pcCU->getSlice()->getNoBackPredFlag())
3397        {
3398          if(iRefList==REF_PIC_LIST_0)
3399          {
3400            uiCostTempL0[iRefIdxTemp] = uiCostTemp;
3401            uiBitsTempL0[iRefIdxTemp] = uiBitsTemp;
3402            if(pcCU->getSlice()->getRefIdxOfLC(REF_PIC_LIST_0, iRefIdxTemp)<0)
3403            {
3404              uiCostTemp = MAX_UINT;
3405            }
3406          }
3407          else
3408          {
3409            if(pcCU->getSlice()->getRefIdxOfLC(REF_PIC_LIST_1, iRefIdxTemp)<0)
3410            {
3411              uiCostTemp = MAX_UINT;
3412            }           
3413          }
3414        }
3415
3416#if ENCODER_BUGFIX
3417        if ( ( iRefList == 0 && uiCostTemp < uiCost[iRefList] ) ||
3418            ( iRefList == 1 &&  pcCU->getSlice()->getNoBackPredFlag() && iRefIdxTemp == iRefIdx[0] ) ||
3419            ( iRefList == 1 && !pcCU->getSlice()->getNoBackPredFlag() && uiCostTemp < uiCost[iRefList] ) )
3420#else
3421        if ( ( iRefList == 0 && uiCostTemp < uiCost[iRefList] ) ||
3422            ( iRefList == 1 &&  pcCU->getSlice()->getNoBackPredFlag() && iRefIdxTemp == iRefIdx[0] ) ||
3423            ( iRefList == 1 && (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0) && (iRefIdxTemp==0 || iRefIdxTemp == iRefIdx[0]) && !pcCU->getSlice()->getNoBackPredFlag() && (iRefIdxTemp == pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)) ) ||
3424            ( iRefList == 1 && !pcCU->getSlice()->getNoBackPredFlag() && uiCostTemp < uiCost[iRefList] ) )
3425#endif
3426          {
3427            uiCost[iRefList] = uiCostTemp;
3428            uiBits[iRefList] = uiBitsTemp; // storing for bi-prediction
3429           
3430            // set motion
3431            cMv[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
3432            iRefIdx[iRefList] = iRefIdxTemp;
3433            pcCU->getCUMvField(eRefPicList)->setAllMv( cMv[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
3434            pcCU->getCUMvField(eRefPicList)->setAllRefIdx( iRefIdx[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
3435
3436            if(!pcCU->getSlice()->getMvdL1ZeroFlag())
3437            {
3438              // storing list 1 prediction signal for iterative bi-directional prediction
3439              if ( eRefPicList == REF_PIC_LIST_1 )
3440              {
3441                TComYuv*  pcYuvPred = &m_acYuvPred[iRefList];
3442                motionCompensation ( pcCU, pcYuvPred, eRefPicList, iPartIdx );
3443              }
3444              if ( (pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 )) && eRefPicList == REF_PIC_LIST_0 )
3445              {
3446                TComYuv*  pcYuvPred = &m_acYuvPred[iRefList];
3447                motionCompensation ( pcCU, pcYuvPred, eRefPicList, iPartIdx );
3448              }
3449            }
3450          }
3451      }
3452    }
3453    //  Bi-directional prediction
3454#if REF_IDX_ME_ZEROMV
3455    if ( (pcCU->getSlice()->isInterB()) && (pcCU->isBipredRestriction(iPartIdx) == false) && !(pcCU->getSlice()->getMvdL1ZeroFlag() && bestBiPDist == MAX_INT) )
3456#else
3457    if ( (pcCU->getSlice()->isInterB()) && (pcCU->isBipredRestriction(iPartIdx) == false) )
3458#endif
3459    {
3460     
3461      cMvBi[0] = cMv[0];            cMvBi[1] = cMv[1];
3462      iRefIdxBi[0] = iRefIdx[0];    iRefIdxBi[1] = iRefIdx[1];
3463     
3464      ::memcpy(cMvPredBi, cMvPred, sizeof(cMvPred));
3465      ::memcpy(aaiMvpIdxBi, aaiMvpIdx, sizeof(aaiMvpIdx));
3466     
3467      UInt uiMotBits[2];
3468
3469      if(pcCU->getSlice()->getMvdL1ZeroFlag())
3470      {
3471        xCopyAMVPInfo(&aacAMVPInfo[1][bestBiPRefIdxL1], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
3472        pcCU->setMVPIdxSubParts( bestBiPMvpL1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3473        aaiMvpIdxBi[1][bestBiPRefIdxL1] = bestBiPMvpL1;
3474        cMvPredBi[1][bestBiPRefIdxL1]   = pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo()->m_acMvCand[bestBiPMvpL1];
3475
3476        cMvBi[1] = cMvPredBi[1][bestBiPRefIdxL1];
3477        iRefIdxBi[1] = bestBiPRefIdxL1;
3478        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
3479        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
3480        TComYuv* pcYuvPred = &m_acYuvPred[1];
3481        motionCompensation( pcCU, pcYuvPred, REF_PIC_LIST_1, iPartIdx );
3482
3483        uiMotBits[0] = uiBits[0] - uiMbBits[0];
3484        uiMotBits[1] = uiMbBits[1];
3485
3486        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 1 )
3487        {
3488          uiMotBits[1] += bestBiPRefIdxL1+1;
3489          if ( bestBiPRefIdxL1 == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1 ) uiMotBits[1]--;
3490        }
3491
3492        uiMotBits[1] += m_auiMVPIdxCost[aaiMvpIdxBi[1][bestBiPRefIdxL1]][AMVP_MAX_NUM_CANDS];
3493
3494        uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
3495
3496        cMvTemp[1][bestBiPRefIdxL1] = cMvBi[1];
3497      }
3498      else
3499      {
3500        uiMotBits[0] = uiBits[0] - uiMbBits[0];
3501        uiMotBits[1] = uiBits[1] - uiMbBits[1];
3502        uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
3503      }
3504
3505      // 4-times iteration (default)
3506      Int iNumIter = 4;
3507     
3508      // fast encoder setting: only one iteration
3509      if ( m_pcEncCfg->getUseFastEnc() || pcCU->getSlice()->getMvdL1ZeroFlag())
3510      {
3511        iNumIter = 1;
3512      }
3513     
3514      for ( Int iIter = 0; iIter < iNumIter; iIter++ )
3515      {
3516       
3517        Int         iRefList    = iIter % 2;
3518        if ( m_pcEncCfg->getUseFastEnc() && (pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 )) )
3519        {
3520          iRefList = 1;
3521        }
3522        RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
3523
3524        if(pcCU->getSlice()->getMvdL1ZeroFlag())
3525        {
3526          iRefList = 0;
3527          eRefPicList = REF_PIC_LIST_0;
3528        }
3529
3530        Bool bChanged = false;
3531       
3532#if (ENCODER_FAST_MODE)
3533        Bool     testIter = true;
3534        TComPic* pcPic    = pcCU->getSlice()->getRefPic( RefPicList(1 - iRefList), iRefIdxBi[1 - iRefList] );
3535        if(pcPic->getIsILR() && (ePartSize == SIZE_2Nx2N))
3536          testIter = false;  //the fixed part is ILR, skip this iteration       
3537        if(testIter)
3538        {
3539#endif
3540        iRefStart = 0;
3541        iRefEnd   = pcCU->getSlice()->getNumRefIdx(eRefPicList)-1;
3542       
3543        for ( Int iRefIdxTemp = iRefStart; iRefIdxTemp <= iRefEnd; iRefIdxTemp++ )
3544        {
3545#if (ENCODER_FAST_MODE)
3546            Bool testRefIdx = true;
3547            pcPic           = pcCU->getSlice()->getRefPic( RefPicList(iRefList) , iRefIdxTemp );
3548            if(pcPic->getIsILR() && (ePartSize == SIZE_2Nx2N))
3549              testRefIdx = false;  //the refined part is ILR, skip this reference pic           
3550            if(testRefIdx)
3551            {
3552#endif
3553          uiBitsTemp = uiMbBits[2] + uiMotBits[1-iRefList];
3554          if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
3555          {
3556            uiBitsTemp += iRefIdxTemp+1;
3557            if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
3558          }
3559          uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdxBi[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
3560          // call ME
3561          xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPredBi[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, true );
3562          xCopyAMVPInfo(&aacAMVPInfo[iRefList][iRefIdxTemp], pcCU->getCUMvField(eRefPicList)->getAMVPInfo());
3563          xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPredBi[iRefList][iRefIdxTemp], aaiMvpIdxBi[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
3564
3565          if ( uiCostTemp < uiCostBi )
3566          {
3567            bChanged = true;
3568           
3569            cMvBi[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
3570            iRefIdxBi[iRefList] = iRefIdxTemp;
3571           
3572            uiCostBi            = uiCostTemp;
3573            uiMotBits[iRefList] = uiBitsTemp - uiMbBits[2] - uiMotBits[1-iRefList];
3574            uiBits[2]           = uiBitsTemp;
3575           
3576            if(iNumIter!=1)
3577            {
3578              //  Set motion
3579              pcCU->getCUMvField( eRefPicList )->setAllMv( cMvBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
3580              pcCU->getCUMvField( eRefPicList )->setAllRefIdx( iRefIdxBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
3581
3582              TComYuv* pcYuvPred = &m_acYuvPred[iRefList];
3583              motionCompensation( pcCU, pcYuvPred, eRefPicList, iPartIdx );
3584            }
3585          }
3586#if (ENCODER_FAST_MODE)
3587            }
3588#endif
3589          }
3590#if (ENCODER_FAST_MODE)
3591        } // for loop-iRefIdxTemp
3592#endif
3593       
3594        if ( !bChanged )
3595        {
3596          if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1] )
3597          {
3598            xCopyAMVPInfo(&aacAMVPInfo[0][iRefIdxBi[0]], pcCU->getCUMvField(REF_PIC_LIST_0)->getAMVPInfo());
3599            xCheckBestMVP(pcCU, REF_PIC_LIST_0, cMvBi[0], cMvPredBi[0][iRefIdxBi[0]], aaiMvpIdxBi[0][iRefIdxBi[0]], uiBits[2], uiCostBi);
3600            if(!pcCU->getSlice()->getMvdL1ZeroFlag())
3601            {
3602              xCopyAMVPInfo(&aacAMVPInfo[1][iRefIdxBi[1]], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
3603              xCheckBestMVP(pcCU, REF_PIC_LIST_1, cMvBi[1], cMvPredBi[1][iRefIdxBi[1]], aaiMvpIdxBi[1][iRefIdxBi[1]], uiBits[2], uiCostBi);
3604            }
3605          }
3606          break;
3607        }
3608      } // for loop-iter
3609    } // if (B_SLICE)
3610#if ZERO_MVD_EST
3611    if ( (pcCU->getSlice()->isInterB()) && (pcCU->isBipredRestriction(iPartIdx) == false) )
3612    {
3613      m_pcRdCost->getMotionCost( 1, 0 );
3614
3615      for ( Int iL0RefIdxTemp = 0; iL0RefIdxTemp <= pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0)-1; iL0RefIdxTemp++ )
3616      for ( Int iL1RefIdxTemp = 0; iL1RefIdxTemp <= pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1; iL1RefIdxTemp++ )
3617      {
3618        UInt uiRefIdxBitsTemp = 0;
3619        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0) > 1 )
3620        {
3621          uiRefIdxBitsTemp += iL0RefIdxTemp+1;
3622          if ( iL0RefIdxTemp == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0)-1 ) uiRefIdxBitsTemp--;
3623        }
3624        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 1 )
3625        {
3626          uiRefIdxBitsTemp += iL1RefIdxTemp+1;
3627          if ( iL1RefIdxTemp == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1 ) uiRefIdxBitsTemp--;
3628        }
3629
3630        Int iL0MVPIdx = 0;
3631        Int iL1MVPIdx = 0;
3632
3633        for (iL0MVPIdx = 0; iL0MVPIdx < aaiMvpNum[0][iL0RefIdxTemp]; iL0MVPIdx++)
3634        {
3635          for (iL1MVPIdx = 0; iL1MVPIdx < aaiMvpNum[1][iL1RefIdxTemp]; iL1MVPIdx++)
3636          {
3637            uiZeroMvdBitsTemp = uiRefIdxBitsTemp;
3638            uiZeroMvdBitsTemp += uiMbBits[2];
3639            uiZeroMvdBitsTemp += m_auiMVPIdxCost[iL0MVPIdx][aaiMvpNum[0][iL0RefIdxTemp]] + m_auiMVPIdxCost[iL1MVPIdx][aaiMvpNum[1][iL1RefIdxTemp]];
3640            uiZeroMvdBitsTemp += 4; //zero mvd for both directions
3641            pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( aacAMVPInfo[0][iL0RefIdxTemp].m_acMvCand[iL0MVPIdx], iL0RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
3642            pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( aacAMVPInfo[1][iL1RefIdxTemp].m_acMvCand[iL1MVPIdx], iL1RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
3643 
3644            xGetInterPredictionError( pcCU, pcOrgYuv, iPartIdx, uiZeroMvdDistTemp, m_pcEncCfg->getUseHADME() );
3645            uiZeroMvdCostTemp = uiZeroMvdDistTemp + m_pcRdCost->getCost( uiZeroMvdBitsTemp );
3646            if (uiZeroMvdCostTemp < uiZeroMvdCost)
3647            {
3648              uiZeroMvdCost = uiZeroMvdCostTemp;
3649              iZeroMvdDir = 3;
3650              aiZeroMvdMvpIdx[0] = iL0MVPIdx;
3651              aiZeroMvdMvpIdx[1] = iL1MVPIdx;
3652              aiZeroMvdRefIdx[0] = iL0RefIdxTemp;
3653              aiZeroMvdRefIdx[1] = iL1RefIdxTemp;
3654              auiZeroMvdBits[2] = uiZeroMvdBitsTemp;
3655            }
3656          }
3657        }
3658      }
3659    }
3660#endif
3661
3662#if AMP_MRG
3663    } //end if bTestNormalMC
3664#endif
3665    //  Clear Motion Field
3666    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
3667    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
3668    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,       ePartSize, uiPartAddr, 0, iPartIdx );
3669    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,       ePartSize, uiPartAddr, 0, iPartIdx );
3670
3671    pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3672    pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3673    pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3674    pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3675   
3676    UInt uiMEBits = 0;
3677    // Set Motion Field_
3678    if ( pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 ) )
3679    {
3680      uiCost[1] = MAX_UINT;
3681    }
3682#if AMP_MRG
3683    if (bTestNormalMC)
3684    {
3685#endif
3686#if ZERO_MVD_EST
3687    if (uiZeroMvdCost <= uiCostBi && uiZeroMvdCost <= uiCost[0] && uiZeroMvdCost <= uiCost[1])
3688    {
3689      if (iZeroMvdDir == 3)
3690      {
3691        uiLastMode = 2;
3692
3693        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( aacAMVPInfo[0][aiZeroMvdRefIdx[0]].m_acMvCand[aiZeroMvdMvpIdx[0]], aiZeroMvdRefIdx[0], ePartSize, uiPartAddr, iPartIdx, 0 );
3694        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( aacAMVPInfo[1][aiZeroMvdRefIdx[1]].m_acMvCand[aiZeroMvdMvpIdx[1]], aiZeroMvdRefIdx[1], ePartSize, uiPartAddr, iPartIdx, 0 );
3695 
3696        pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
3697       
3698        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3699        pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3700        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[1], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3701        pcCU->setMVPNumSubParts( aaiMvpNum[1][aiZeroMvdRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3702        uiMEBits = auiZeroMvdBits[2];
3703      }
3704      else if (iZeroMvdDir == 1)
3705      {       
3706        uiLastMode = 0;
3707
3708        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( aacAMVPInfo[0][aiZeroMvdRefIdx[0]].m_acMvCand[aiZeroMvdMvpIdx[0]], aiZeroMvdRefIdx[0], ePartSize, uiPartAddr, iPartIdx, 0 );
3709
3710        pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
3711       
3712        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3713        pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3714        uiMEBits = auiZeroMvdBits[0];
3715      }
3716      else if (iZeroMvdDir == 2)
3717      {
3718        uiLastMode = 1;
3719
3720        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( aacAMVPInfo[1][aiZeroMvdRefIdx[1]].m_acMvCand[aiZeroMvdMvpIdx[1]], aiZeroMvdRefIdx[1], ePartSize, uiPartAddr, iPartIdx, 0 );
3721
3722        pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
3723       
3724        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[1], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3725        pcCU->setMVPNumSubParts( aaiMvpNum[1][aiZeroMvdRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3726        uiMEBits = auiZeroMvdBits[1];
3727      }
3728      else
3729      {
3730        assert(0);
3731      }
3732    }
3733    else
3734#endif
3735    if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1])
3736    {
3737      uiLastMode = 2;
3738      {
3739            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv( cMvBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
3740            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdxBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
3741            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
3742            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
3743      }
3744      {
3745        TempMv = cMvBi[0] - cMvPredBi[0][iRefIdxBi[0]];
3746            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
3747      }
3748      {
3749        TempMv = cMvBi[1] - cMvPredBi[1][iRefIdxBi[1]];
3750            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
3751      }
3752     
3753      pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
3754     
3755      pcCU->setMVPIdxSubParts( aaiMvpIdxBi[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3756      pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3757      pcCU->setMVPIdxSubParts( aaiMvpIdxBi[1][iRefIdxBi[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3758      pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdxBi[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3759
3760      uiMEBits = uiBits[2];
3761    }
3762    else if ( uiCost[0] <= uiCost[1] )
3763    {
3764      uiLastMode = 0;
3765          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv( cMv[0], ePartSize, uiPartAddr, 0, iPartIdx );
3766          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdx[0], ePartSize, uiPartAddr, 0, iPartIdx );
3767      {
3768        TempMv = cMv[0] - cMvPred[0][iRefIdx[0]];
3769            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
3770      }
3771      pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
3772     
3773      pcCU->setMVPIdxSubParts( aaiMvpIdx[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3774      pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3775
3776      uiMEBits = uiBits[0];
3777    }
3778    else
3779    {
3780      uiLastMode = 1;
3781          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv( cMv[1], ePartSize, uiPartAddr, 0, iPartIdx );
3782          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdx[1], ePartSize, uiPartAddr, 0, iPartIdx );
3783      {
3784        TempMv = cMv[1] - cMvPred[1][iRefIdx[1]];
3785            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
3786      }
3787      pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
3788     
3789      pcCU->setMVPIdxSubParts( aaiMvpIdx[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3790      pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3791
3792      uiMEBits = uiBits[1];
3793    }
3794#if AMP_MRG
3795    } // end if bTestNormalMC
3796#endif
3797
3798    if ( pcCU->getPartitionSize( uiPartAddr ) != SIZE_2Nx2N )
3799    {
3800      UInt uiMRGInterDir = 0;     
3801      TComMvField cMRGMvField[2];
3802      UInt uiMRGIndex = 0;
3803
3804      UInt uiMEInterDir = 0;
3805      TComMvField cMEMvField[2];
3806
3807      m_pcRdCost->getMotionCost( 1, 0 );
3808#if AMP_MRG
3809      // calculate ME cost
3810      UInt uiMEError = MAX_UINT;
3811      UInt uiMECost = MAX_UINT;
3812
3813      if (bTestNormalMC)
3814      {
3815        xGetInterPredictionError( pcCU, pcOrgYuv, iPartIdx, uiMEError, m_pcEncCfg->getUseHADME() );
3816        uiMECost = uiMEError + m_pcRdCost->getCost( uiMEBits );
3817      }
3818#else
3819      // calculate ME cost
3820      UInt uiMEError = MAX_UINT;
3821      xGetInterPredictionError( pcCU, pcOrgYuv, iPartIdx, uiMEError, m_pcEncCfg->getUseHADME() );
3822      UInt uiMECost = uiMEError + m_pcRdCost->getCost( uiMEBits );
3823#endif
3824      // save ME result.
3825      uiMEInterDir = pcCU->getInterDir( uiPartAddr );
3826      pcCU->getMvField( pcCU, uiPartAddr, REF_PIC_LIST_0, cMEMvField[0] );
3827      pcCU->getMvField( pcCU, uiPartAddr, REF_PIC_LIST_1, cMEMvField[1] );
3828
3829      // find Merge result
3830      UInt uiMRGCost = MAX_UINT;
3831      xMergeEstimation( pcCU, pcOrgYuv, iPartIdx, uiMRGInterDir, cMRGMvField, uiMRGIndex, uiMRGCost, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand);
3832      if ( uiMRGCost < uiMECost )
3833      {
3834        // set Merge result
3835        pcCU->setMergeFlagSubParts ( true,          uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
3836        pcCU->setMergeIndexSubParts( uiMRGIndex,    uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
3837        pcCU->setInterDirSubParts  ( uiMRGInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
3838        {
3839          pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMRGMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
3840          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMRGMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
3841        }
3842
3843        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
3844        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
3845
3846        pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3847        pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3848        pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3849        pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
3850      }
3851      else
3852      {
3853        // set ME result
3854        pcCU->setMergeFlagSubParts( false,        uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
3855        pcCU->setInterDirSubParts ( uiMEInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
3856        {
3857          pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMEMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
3858          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMEMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
3859        }
3860      }
3861    }
3862
3863    //  MC
3864    motionCompensation ( pcCU, rpcPredYuv, REF_PIC_LIST_X, iPartIdx );
3865   
3866  } //  end of for ( Int iPartIdx = 0; iPartIdx < iNumPart; iPartIdx++ )
3867
3868  setWpScalingDistParam( pcCU, -1, REF_PIC_LIST_X );
3869
3870  return;
3871}
3872
3873// AMVP
3874#if ZERO_MVD_EST
3875Void TEncSearch::xEstimateMvPredAMVP( TComDataCU* pcCU, TComYuv* pcOrgYuv, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMvPred, Bool bFilled, UInt* puiDistBiP, UInt* puiDist  )
3876#else
3877Void TEncSearch::xEstimateMvPredAMVP( TComDataCU* pcCU, TComYuv* pcOrgYuv, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMvPred, Bool bFilled, UInt* puiDistBiP )
3878#endif
3879{
3880  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
3881 
3882  TComMv  cBestMv;
3883  Int     iBestIdx = 0;
3884  TComMv  cZeroMv;
3885  TComMv  cMvPred;
3886  UInt    uiBestCost = MAX_INT;
3887  UInt    uiPartAddr = 0;
3888  Int     iRoiWidth, iRoiHeight;
3889  Int     i;
3890 
3891  pcCU->getPartIndexAndSize( uiPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
3892  // Fill the MV Candidates
3893  if (!bFilled)
3894  {
3895    pcCU->fillMvpCand( uiPartIdx, uiPartAddr, eRefPicList, iRefIdx, pcAMVPInfo );
3896  }
3897 
3898  // initialize Mvp index & Mvp
3899  iBestIdx = 0;
3900  cBestMv  = pcAMVPInfo->m_acMvCand[0];
3901#if !ZERO_MVD_EST
3902  if (pcAMVPInfo->iN <= 1)
3903  {
3904    rcMvPred = cBestMv;
3905   
3906    pcCU->setMVPIdxSubParts( iBestIdx, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
3907    pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
3908
3909    if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefPicList==REF_PIC_LIST_1)
3910    {
3911#if ZERO_MVD_EST
3912      (*puiDistBiP) = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, rcMvPred, 0, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight, uiDist );
3913#else
3914      (*puiDistBiP) = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, rcMvPred, 0, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight);
3915#endif
3916    }
3917    return;
3918  }
3919#endif 
3920  if (bFilled)
3921  {
3922    assert(pcCU->getMVPIdx(eRefPicList,uiPartAddr) >= 0);
3923    rcMvPred = pcAMVPInfo->m_acMvCand[pcCU->getMVPIdx(eRefPicList,uiPartAddr)];
3924    return;
3925  }
3926 
3927  m_cYuvPredTemp.clear();
3928#if ZERO_MVD_EST
3929  UInt uiDist;
3930#endif
3931  //-- Check Minimum Cost.
3932  for ( i = 0 ; i < pcAMVPInfo->iN; i++)
3933  {
3934    UInt uiTmpCost;
3935#if ZERO_MVD_EST
3936    uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight, uiDist );
3937#else
3938    uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight);
3939#endif     
3940    if ( uiBestCost > uiTmpCost )
3941    {
3942      uiBestCost = uiTmpCost;
3943      cBestMv   = pcAMVPInfo->m_acMvCand[i];
3944      iBestIdx  = i;
3945      (*puiDistBiP) = uiTmpCost;
3946#if ZERO_MVD_EST
3947      (*puiDist) = uiDist;
3948#endif
3949    }
3950  }
3951
3952  m_cYuvPredTemp.clear();
3953 
3954  // Setting Best MVP
3955  rcMvPred = cBestMv;
3956  pcCU->setMVPIdxSubParts( iBestIdx, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
3957  pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
3958  return;
3959}
3960
3961UInt TEncSearch::xGetMvpIdxBits(Int iIdx, Int iNum)
3962{
3963  assert(iIdx >= 0 && iNum >= 0 && iIdx < iNum);
3964 
3965  if (iNum == 1)
3966    return 0;
3967 
3968  UInt uiLength = 1;
3969  Int iTemp = iIdx;
3970  if ( iTemp == 0 )
3971  {
3972    return uiLength;
3973  }
3974 
3975  Bool bCodeLast = ( iNum-1 > iTemp );
3976 
3977  uiLength += (iTemp-1);
3978 
3979  if( bCodeLast )
3980  {
3981    uiLength++;
3982  }
3983 
3984  return uiLength;
3985}
3986
3987Void TEncSearch::xGetBlkBits( PartSize eCUMode, Bool bPSlice, Int iPartIdx, UInt uiLastMode, UInt uiBlkBit[3])
3988{
3989  if ( eCUMode == SIZE_2Nx2N )
3990  {
3991    uiBlkBit[0] = (! bPSlice) ? 3 : 1;
3992    uiBlkBit[1] = 3;
3993    uiBlkBit[2] = 5;
3994  }
3995  else if ( (eCUMode == SIZE_2NxN || eCUMode == SIZE_2NxnU) || eCUMode == SIZE_2NxnD )
3996  {
3997    UInt aauiMbBits[2][3][3] = { { {0,0,3}, {0,0,0}, {0,0,0} } , { {5,7,7}, {7,5,7}, {9-3,9-3,9-3} } };
3998    if ( bPSlice )
3999    {
4000      uiBlkBit[0] = 3;
4001      uiBlkBit[1] = 0;
4002      uiBlkBit[2] = 0;
4003    }
4004    else
4005    {
4006      ::memcpy( uiBlkBit, aauiMbBits[iPartIdx][uiLastMode], 3*sizeof(UInt) );
4007    }
4008  }
4009  else if ( (eCUMode == SIZE_Nx2N || eCUMode == SIZE_nLx2N) || eCUMode == SIZE_nRx2N )
4010  {
4011    UInt aauiMbBits[2][3][3] = { { {0,2,3}, {0,0,0}, {0,0,0} } , { {5,7,7}, {7-2,7-2,9-2}, {9-3,9-3,9-3} } };
4012    if ( bPSlice )
4013    {
4014      uiBlkBit[0] = 3;
4015      uiBlkBit[1] = 0;
4016      uiBlkBit[2] = 0;
4017    }
4018    else
4019    {
4020      ::memcpy( uiBlkBit, aauiMbBits[iPartIdx][uiLastMode], 3*sizeof(UInt) );
4021    }
4022  }
4023  else if ( eCUMode == SIZE_NxN )
4024  {
4025    uiBlkBit[0] = (! bPSlice) ? 3 : 1;
4026    uiBlkBit[1] = 3;
4027    uiBlkBit[2] = 5;
4028  }
4029  else
4030  {
4031    printf("Wrong!\n");
4032    assert( 0 );
4033  }
4034}
4035
4036Void TEncSearch::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
4037{
4038  pDst->iN = pSrc->iN;
4039  for (Int i = 0; i < pSrc->iN; i++)
4040  {
4041    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
4042  }
4043}
4044
4045Void TEncSearch::xCheckBestMVP ( TComDataCU* pcCU, RefPicList eRefPicList, TComMv cMv, TComMv& rcMvPred, Int& riMVPIdx, UInt& ruiBits, UInt& ruiCost )
4046{
4047  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
4048 
4049  assert(pcAMVPInfo->m_acMvCand[riMVPIdx] == rcMvPred);
4050 
4051  if (pcAMVPInfo->iN < 2) return;
4052 
4053  m_pcRdCost->getMotionCost( 1, 0 );
4054  m_pcRdCost->setCostScale ( 0    );
4055 
4056  Int iBestMVPIdx = riMVPIdx;
4057 
4058  m_pcRdCost->setPredictor( rcMvPred );
4059  Int iOrgMvBits  = m_pcRdCost->getBits(cMv.getHor(), cMv.getVer());
4060  iOrgMvBits += m_auiMVPIdxCost[riMVPIdx][AMVP_MAX_NUM_CANDS];
4061  Int iBestMvBits = iOrgMvBits;
4062 
4063  for (Int iMVPIdx = 0; iMVPIdx < pcAMVPInfo->iN; iMVPIdx++)
4064  {
4065    if (iMVPIdx == riMVPIdx) continue;
4066   
4067    m_pcRdCost->setPredictor( pcAMVPInfo->m_acMvCand[iMVPIdx] );
4068   
4069    Int iMvBits = m_pcRdCost->getBits(cMv.getHor(), cMv.getVer());
4070    iMvBits += m_auiMVPIdxCost[iMVPIdx][AMVP_MAX_NUM_CANDS];
4071   
4072    if (iMvBits < iBestMvBits)
4073    {
4074      iBestMvBits = iMvBits;
4075      iBestMVPIdx = iMVPIdx;
4076    }
4077  }
4078 
4079  if (iBestMVPIdx != riMVPIdx)  //if changed
4080  {
4081    rcMvPred = pcAMVPInfo->m_acMvCand[iBestMVPIdx];
4082   
4083    riMVPIdx = iBestMVPIdx;
4084    UInt uiOrgBits = ruiBits;
4085    ruiBits = uiOrgBits - iOrgMvBits + iBestMvBits;
4086    ruiCost = (ruiCost - m_pcRdCost->getCost( uiOrgBits ))  + m_pcRdCost->getCost( ruiBits );
4087  }
4088}
4089
4090UInt TEncSearch::xGetTemplateCost( TComDataCU* pcCU,
4091                                  UInt        uiPartIdx,
4092                                  UInt      uiPartAddr,
4093                                  TComYuv*    pcOrgYuv,
4094                                  TComYuv*    pcTemplateCand,
4095                                  TComMv      cMvCand,
4096                                  Int         iMVPIdx,
4097                                  Int     iMVPNum,
4098                                  RefPicList  eRefPicList,
4099                                  Int         iRefIdx,
4100                                  Int         iSizeX,
4101                                  Int         iSizeY
4102                               #if ZERO_MVD_EST
4103                                , UInt&       ruiDist
4104                               #endif
4105                                  )
4106{
4107  UInt uiCost  = MAX_INT;
4108 
4109  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec();
4110 
4111  pcCU->clipMv( cMvCand );
4112
4113  // prediction pattern
4114  if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType()==P_SLICE )
4115  {
4116    xPredInterLumaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMvCand, iSizeX, iSizeY, pcTemplateCand, true );
4117  }
4118  else
4119  {
4120    xPredInterLumaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMvCand, iSizeX, iSizeY, pcTemplateCand, false );
4121  }
4122
4123  if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType()==P_SLICE )
4124  {
4125    xWeightedPredictionUni( pcCU, pcTemplateCand, uiPartAddr, iSizeX, iSizeY, eRefPicList, pcTemplateCand, iRefIdx );
4126  }
4127
4128  // calc distortion
4129#if ZERO_MVD_EST
4130  m_pcRdCost->getMotionCost( 1, 0 );
4131  DistParam cDistParam;
4132  m_pcRdCost->setDistParam( cDistParam, g_bitDepthY,
4133                            pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), 
4134                            pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), 
4135#if NS_HAD
4136                            iSizeX, iSizeY, m_pcEncCfg->getUseHADME(), m_pcEncCfg->getUseNSQT() );
4137#else
4138                            iSizeX, iSizeY, m_pcEncCfg->getUseHADME() );
4139#endif
4140  ruiDist = cDistParam.DistFunc( &cDistParam );
4141  uiCost = ruiDist + m_pcRdCost->getCost( m_auiMVPIdxCost[iMVPIdx][iMVPNum] );
4142#else
4143#if WEIGHTED_CHROMA_DISTORTION
4144  uiCost = m_pcRdCost->getDistPart(g_bitDepthY, pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, TEXT_LUMA, DF_SAD );
4145#else
4146  uiCost = m_pcRdCost->getDistPart(g_bitDepthY, pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, DF_SAD );
4147#endif
4148  uiCost = (UInt) m_pcRdCost->calcRdCost( m_auiMVPIdxCost[iMVPIdx][iMVPNum], uiCost, false, DF_SAD );
4149#endif
4150  return uiCost;
4151}
4152
4153Void TEncSearch::xMotionEstimation( TComDataCU* pcCU, TComYuv* pcYuvOrg, Int iPartIdx, RefPicList eRefPicList, TComMv* pcMvPred, Int iRefIdxPred, TComMv& rcMv, UInt& ruiBits, UInt& ruiCost, Bool bBi  )
4154{
4155  UInt          uiPartAddr;
4156  Int           iRoiWidth;
4157  Int           iRoiHeight;
4158 
4159  TComMv        cMvHalf, cMvQter;
4160  TComMv        cMvSrchRngLT;
4161  TComMv        cMvSrchRngRB;
4162 
4163  TComYuv*      pcYuv = pcYuvOrg;
4164  m_iSearchRange = m_aaiAdaptSR[eRefPicList][iRefIdxPred];
4165 
4166  Int           iSrchRng      = ( bBi ? m_bipredSearchRange : m_iSearchRange );
4167  TComPattern*  pcPatternKey  = pcCU->getPattern        ();
4168 
4169  Double        fWeight       = 1.0;
4170 
4171  pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
4172 
4173  if ( bBi )
4174  {
4175    TComYuv*  pcYuvOther = &m_acYuvPred[1-(Int)eRefPicList];
4176    pcYuv                = &m_cYuvPredTemp;
4177   
4178    pcYuvOrg->copyPartToPartYuv( pcYuv, uiPartAddr, iRoiWidth, iRoiHeight );
4179   
4180    pcYuv->removeHighFreq( pcYuvOther, uiPartAddr, iRoiWidth, iRoiHeight );
4181   
4182    fWeight = 0.5;
4183  }
4184 
4185  //  Search key pattern initialization
4186  pcPatternKey->initPattern( pcYuv->getLumaAddr( uiPartAddr ),
4187                            pcYuv->getCbAddr  ( uiPartAddr ),
4188                            pcYuv->getCrAddr  ( uiPartAddr ),
4189                            iRoiWidth,
4190                            iRoiHeight,
4191                            pcYuv->getStride(),
4192                            0, 0 );
4193 
4194  Pel*        piRefY      = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr );
4195  Int         iRefStride  = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getPicYuvRec()->getStride();
4196 
4197  TComMv      cMvPred = *pcMvPred;
4198 
4199  if ( bBi )  xSetSearchRange   ( pcCU, rcMv   , iSrchRng, cMvSrchRngLT, cMvSrchRngRB );
4200  else        xSetSearchRange   ( pcCU, cMvPred, iSrchRng, cMvSrchRngLT, cMvSrchRngRB );
4201 
4202  m_pcRdCost->getMotionCost ( 1, 0 );
4203 
4204  m_pcRdCost->setPredictor  ( *pcMvPred );
4205  m_pcRdCost->setCostScale  ( 2 );
4206
4207  setWpScalingDistParam( pcCU, iRefIdxPred, eRefPicList );
4208  //  Do integer search
4209#if REF_IDX_ME_AROUND_ZEROMV || REF_IDX_ME_ZEROMV
4210#if REF_IDX_ME_AROUND_ZEROMV
4211  if( pcCU->getSlice()->getRefPic(eRefPicList, iRefIdxPred )->getIsILR())  //ILR reference pic
4212  {
4213    xPatternSearchILR  ( pcCU, pcPatternKey, piRefY, iRefStride, rcMv, ruiCost );
4214  }
4215  else  //non ILR reference pic
4216  {
4217  if ( !m_iFastSearch || bBi )
4218  {
4219    xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
4220  }
4221  else
4222  {
4223    rcMv = *pcMvPred;
4224    xPatternSearchFast  ( pcCU, pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
4225    }
4226  }
4227#endif
4228#if REF_IDX_ME_ZEROMV
4229  if( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getIsILR())  //ILR reference pic
4230  {
4231    rcMv.setZero();  //use Mv(0, 0) for integer ME
4232  }
4233  else  //non ILR reference pic
4234  {
4235    if ( !m_iFastSearch || bBi ) 
4236    { 
4237      xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost ); 
4238    } 
4239    else 
4240    { 
4241      rcMv = *pcMvPred; 
4242      xPatternSearchFast  ( pcCU, pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost ); 
4243    }
4244  }
4245#endif
4246#else
4247  if ( !m_iFastSearch || bBi )
4248  {
4249    xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
4250  }
4251  else
4252  {
4253    rcMv = *pcMvPred;
4254    xPatternSearchFast  ( pcCU, pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
4255  }
4256#endif
4257 
4258  m_pcRdCost->getMotionCost( 1, 0 );
4259  m_pcRdCost->setCostScale ( 1 );
4260 
4261  {
4262#if REF_IDX_ME_ZEROMV
4263    if( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getIsILR())  //ILR reference pic
4264    {
4265      xPatternSearchFracDIFMv0( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost, bBi );
4266    }
4267    else    //non ILR reference pic
4268    {
4269      xPatternSearchFracDIF( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost, bBi );
4270    }
4271#else
4272    xPatternSearchFracDIF( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost
4273                          ,bBi
4274                          );
4275#endif
4276  }
4277 
4278 
4279 
4280  m_pcRdCost->setCostScale( 0 );
4281  rcMv <<= 2;
4282  rcMv += (cMvHalf <<= 1);
4283  rcMv +=  cMvQter;
4284 
4285  UInt uiMvBits = m_pcRdCost->getBits( rcMv.getHor(), rcMv.getVer() );
4286 
4287  ruiBits      += uiMvBits;
4288  ruiCost       = (UInt)( floor( fWeight * ( (Double)ruiCost - (Double)m_pcRdCost->getCost( uiMvBits ) ) ) + (Double)m_pcRdCost->getCost( ruiBits ) );
4289}
4290
4291
4292Void TEncSearch::xSetSearchRange ( TComDataCU* pcCU, TComMv& cMvPred, Int iSrchRng, TComMv& rcMvSrchRngLT, TComMv& rcMvSrchRngRB )
4293{
4294  Int  iMvShift = 2;
4295  TComMv cTmpMvPred = cMvPred;
4296  pcCU->clipMv( cTmpMvPred );
4297
4298  rcMvSrchRngLT.setHor( cTmpMvPred.getHor() - (iSrchRng << iMvShift) );
4299  rcMvSrchRngLT.setVer( cTmpMvPred.getVer() - (iSrchRng << iMvShift) );
4300 
4301  rcMvSrchRngRB.setHor( cTmpMvPred.getHor() + (iSrchRng << iMvShift) );
4302  rcMvSrchRngRB.setVer( cTmpMvPred.getVer() + (iSrchRng << iMvShift) );
4303  pcCU->clipMv        ( rcMvSrchRngLT );
4304  pcCU->clipMv        ( rcMvSrchRngRB );
4305 
4306  rcMvSrchRngLT >>= iMvShift;
4307  rcMvSrchRngRB >>= iMvShift;
4308}
4309
4310Void TEncSearch::xPatternSearch( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD )
4311{
4312  Int   iSrchRngHorLeft   = pcMvSrchRngLT->getHor();
4313  Int   iSrchRngHorRight  = pcMvSrchRngRB->getHor();
4314  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
4315  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
4316 
4317  UInt  uiSad;
4318  UInt  uiSadBest         = MAX_UINT;
4319  Int   iBestX = 0;
4320  Int   iBestY = 0;
4321 
4322  Pel*  piRefSrch;
4323 
4324  //-- jclee for using the SAD function pointer
4325  m_pcRdCost->setDistParam( pcPatternKey, piRefY, iRefStride,  m_cDistParam );
4326 
4327  // fast encoder decision: use subsampled SAD for integer ME
4328  if ( m_pcEncCfg->getUseFastEnc() )
4329  {
4330    if ( m_cDistParam.iRows > 8 )
4331    {
4332      m_cDistParam.iSubShift = 1;
4333    }
4334  }
4335 
4336  piRefY += (iSrchRngVerTop * iRefStride);
4337  for ( Int y = iSrchRngVerTop; y <= iSrchRngVerBottom; y++ )
4338  {
4339    for ( Int x = iSrchRngHorLeft; x <= iSrchRngHorRight; x++ )
4340    {
4341      //  find min. distortion position
4342      piRefSrch = piRefY + x;
4343      m_cDistParam.pCur = piRefSrch;
4344
4345      setDistParamComp(0);
4346
4347      m_cDistParam.bitDepth = g_bitDepthY;
4348      uiSad = m_cDistParam.DistFunc( &m_cDistParam );
4349     
4350      // motion cost
4351      uiSad += m_pcRdCost->getCost( x, y );
4352     
4353      if ( uiSad < uiSadBest )
4354      {
4355        uiSadBest = uiSad;
4356        iBestX    = x;
4357        iBestY    = y;
4358      }
4359    }
4360    piRefY += iRefStride;
4361  }
4362 
4363  rcMv.set( iBestX, iBestY );
4364 
4365  ruiSAD = uiSadBest - m_pcRdCost->getCost( iBestX, iBestY );
4366  return;
4367}
4368
4369Void TEncSearch::xPatternSearchFast( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD )
4370{
4371  pcCU->getMvPredLeft       ( m_acMvPredictors[0] );
4372  pcCU->getMvPredAbove      ( m_acMvPredictors[1] );
4373  pcCU->getMvPredAboveRight ( m_acMvPredictors[2] );
4374 
4375  switch ( m_iFastSearch )
4376  {
4377    case 1:
4378      xTZSearch( pcCU, pcPatternKey, piRefY, iRefStride, pcMvSrchRngLT, pcMvSrchRngRB, rcMv, ruiSAD );
4379      break;
4380     
4381    default:
4382      break;
4383  }
4384}
4385
4386Void TEncSearch::xTZSearch( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD )
4387{
4388  Int   iSrchRngHorLeft   = pcMvSrchRngLT->getHor();
4389  Int   iSrchRngHorRight  = pcMvSrchRngRB->getHor();
4390  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
4391  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
4392 
4393  TZ_SEARCH_CONFIGURATION
4394 
4395  UInt uiSearchRange = m_iSearchRange;
4396  pcCU->clipMv( rcMv );
4397  rcMv >>= 2;
4398  // init TZSearchStruct
4399  IntTZSearchStruct cStruct;
4400  cStruct.iYStride    = iRefStride;
4401  cStruct.piRefY      = piRefY;
4402  cStruct.uiBestSad   = MAX_UINT;
4403 
4404  // set rcMv (Median predictor) as start point and as best point
4405  xTZSearchHelp( pcPatternKey, cStruct, rcMv.getHor(), rcMv.getVer(), 0, 0 );
4406 
4407  // test whether one of PRED_A, PRED_B, PRED_C MV is better start point than Median predictor
4408  if ( bTestOtherPredictedMV )
4409  {
4410    for ( UInt index = 0; index < 3; index++ )
4411    {
4412      TComMv cMv = m_acMvPredictors[index];
4413      pcCU->clipMv( cMv );
4414      cMv >>= 2;
4415      xTZSearchHelp( pcPatternKey, cStruct, cMv.getHor(), cMv.getVer(), 0, 0 );
4416    }
4417  }
4418 
4419  // test whether zero Mv is better start point than Median predictor
4420  if ( bTestZeroVector )
4421  {
4422    xTZSearchHelp( pcPatternKey, cStruct, 0, 0, 0, 0 );
4423  }
4424 
4425  // start search
4426  Int  iDist = 0;
4427  Int  iStartX = cStruct.iBestX;
4428  Int  iStartY = cStruct.iBestY;
4429 
4430  // first search
4431  for ( iDist = 1; iDist <= (Int)uiSearchRange; iDist*=2 )
4432  {
4433    if ( bFirstSearchDiamond == 1 )
4434    {
4435      xTZ8PointDiamondSearch ( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, iStartX, iStartY, iDist );
4436    }
4437    else
4438    {
4439      xTZ8PointSquareSearch  ( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, iStartX, iStartY, iDist );
4440    }
4441   
4442    if ( bFirstSearchStop && ( cStruct.uiBestRound >= uiFirstSearchRounds ) ) // stop criterion
4443    {
4444      break;
4445    }
4446  }
4447 
4448  // test whether zero Mv is a better start point than Median predictor
4449  if ( bTestZeroVectorStart && ((cStruct.iBestX != 0) || (cStruct.iBestY != 0)) )
4450  {
4451    xTZSearchHelp( pcPatternKey, cStruct, 0, 0, 0, 0 );
4452    if ( (cStruct.iBestX == 0) && (cStruct.iBestY == 0) )
4453    {
4454      // test its neighborhood
4455      for ( iDist = 1; iDist <= (Int)uiSearchRange; iDist*=2 )
4456      {
4457        xTZ8PointDiamondSearch( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, 0, 0, iDist );
4458        if ( bTestZeroVectorStop && (cStruct.uiBestRound > 0) ) // stop criterion
4459        {
4460          break;
4461        }
4462      }
4463    }
4464  }
4465 
4466  // calculate only 2 missing points instead 8 points if cStruct.uiBestDistance == 1
4467  if ( cStruct.uiBestDistance == 1 )
4468  {
4469    cStruct.uiBestDistance = 0;
4470    xTZ2PointSearch( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB );
4471  }
4472 
4473  // raster search if distance is too big
4474  if ( bEnableRasterSearch && ( ((Int)(cStruct.uiBestDistance) > iRaster) || bAlwaysRasterSearch ) )
4475  {
4476    cStruct.uiBestDistance = iRaster;
4477    for ( iStartY = iSrchRngVerTop; iStartY <= iSrchRngVerBottom; iStartY += iRaster )
4478    {
4479      for ( iStartX = iSrchRngHorLeft; iStartX <= iSrchRngHorRight; iStartX += iRaster )
4480      {
4481        xTZSearchHelp( pcPatternKey, cStruct, iStartX, iStartY, 0, iRaster );
4482      }
4483    }
4484  }
4485 
4486  // raster refinement
4487  if ( bRasterRefinementEnable && cStruct.uiBestDistance > 0 )
4488  {
4489    while ( cStruct.uiBestDistance > 0 )
4490    {
4491      iStartX = cStruct.iBestX;
4492      iStartY = cStruct.iBestY;
4493      if ( cStruct.uiBestDistance > 1 )
4494      {
4495        iDist = cStruct.uiBestDistance >>= 1;
4496        if ( bRasterRefinementDiamond == 1 )
4497        {
4498          xTZ8PointDiamondSearch ( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, iStartX, iStartY, iDist );
4499        }
4500        else
4501        {
4502          xTZ8PointSquareSearch  ( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, iStartX, iStartY, iDist );
4503        }
4504      }
4505     
4506      // calculate only 2 missing points instead 8 points if cStruct.uiBestDistance == 1
4507      if ( cStruct.uiBestDistance == 1 )
4508      {
4509        cStruct.uiBestDistance = 0;
4510        if ( cStruct.ucPointNr != 0 )
4511        {
4512          xTZ2PointSearch( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB );
4513        }
4514      }
4515    }
4516  }
4517 
4518  // start refinement
4519  if ( bStarRefinementEnable && cStruct.uiBestDistance > 0 )
4520  {
4521    while ( cStruct.uiBestDistance > 0 )
4522    {
4523      iStartX = cStruct.iBestX;
4524      iStartY = cStruct.iBestY;
4525      cStruct.uiBestDistance = 0;
4526      cStruct.ucPointNr = 0;
4527      for ( iDist = 1; iDist < (Int)uiSearchRange + 1; iDist*=2 )
4528      {
4529        if ( bStarRefinementDiamond == 1 )
4530        {
4531          xTZ8PointDiamondSearch ( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, iStartX, iStartY, iDist );
4532        }
4533        else
4534        {
4535          xTZ8PointSquareSearch  ( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, iStartX, iStartY, iDist );
4536        }
4537        if ( bStarRefinementStop && (cStruct.uiBestRound >= uiStarRefinementRounds) ) // stop criterion
4538        {
4539          break;
4540        }
4541      }
4542     
4543      // calculate only 2 missing points instead 8 points if cStrukt.uiBestDistance == 1
4544      if ( cStruct.uiBestDistance == 1 )
4545      {
4546        cStruct.uiBestDistance = 0;
4547        if ( cStruct.ucPointNr != 0 )
4548        {
4549          xTZ2PointSearch( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB );
4550        }
4551      }
4552    }
4553  }
4554 
4555  // write out best match
4556  rcMv.set( cStruct.iBestX, cStruct.iBestY );
4557  ruiSAD = cStruct.uiBestSad - m_pcRdCost->getCost( cStruct.iBestX, cStruct.iBestY );
4558}
4559
4560Void TEncSearch::xPatternSearchFracDIF(TComDataCU* pcCU,
4561                                       TComPattern* pcPatternKey,
4562                                       Pel* piRefY,
4563                                       Int iRefStride,
4564                                       TComMv* pcMvInt,
4565                                       TComMv& rcMvHalf,
4566                                       TComMv& rcMvQter,
4567                                       UInt& ruiCost
4568                                       ,Bool biPred
4569                                       )
4570{
4571  //  Reference pattern initialization (integer scale)
4572  TComPattern cPatternRoi;
4573  Int         iOffset    = pcMvInt->getHor() + pcMvInt->getVer() * iRefStride;
4574  cPatternRoi.initPattern( piRefY +  iOffset,
4575                          NULL,
4576                          NULL,
4577                          pcPatternKey->getROIYWidth(),
4578                          pcPatternKey->getROIYHeight(),
4579                          iRefStride,
4580                          0, 0 );
4581 
4582  //  Half-pel refinement
4583  xExtDIFUpSamplingH ( &cPatternRoi, biPred );
4584 
4585  rcMvHalf = *pcMvInt;   rcMvHalf <<= 1;    // for mv-cost
4586  TComMv baseRefMv(0, 0);
4587  ruiCost = xPatternRefinement( pcPatternKey, baseRefMv, 2, rcMvHalf   );
4588 
4589  m_pcRdCost->setCostScale( 0 );
4590 
4591  xExtDIFUpSamplingQ ( &cPatternRoi, rcMvHalf, biPred );
4592  baseRefMv = rcMvHalf;
4593  baseRefMv <<= 1;
4594 
4595  rcMvQter = *pcMvInt;   rcMvQter <<= 1;    // for mv-cost
4596  rcMvQter += rcMvHalf;  rcMvQter <<= 1;
4597  ruiCost = xPatternRefinement( pcPatternKey, baseRefMv, 1, rcMvQter );
4598}
4599
4600/** encode residual and calculate rate-distortion for a CU block
4601 * \param pcCU
4602 * \param pcYuvOrg
4603 * \param pcYuvPred
4604 * \param rpcYuvResi
4605 * \param rpcYuvResiBest
4606 * \param rpcYuvRec
4607 * \param bSkipRes
4608 * \returns Void
4609 */
4610Void TEncSearch::encodeResAndCalcRdInterCU( TComDataCU* pcCU, TComYuv* pcYuvOrg, TComYuv* pcYuvPred, TComYuv*& rpcYuvResi, TComYuv*& rpcYuvResiBest, TComYuv*& rpcYuvRec, Bool bSkipRes )
4611{
4612#if NO_RESIDUAL_FLAG_FOR_BLPRED
4613  if ( pcCU->isIntra(0) && !pcCU->isIntraBL(0))
4614#else
4615  if ( pcCU->isIntra(0) )
4616#endif
4617  {
4618    return;
4619  }
4620 
4621  Bool      bHighPass    = pcCU->getSlice()->getDepth() ? true : false;
4622  UInt      uiBits       = 0, uiBitsBest = 0;
4623  UInt      uiDistortion = 0, uiDistortionBest = 0;
4624 
4625  UInt      uiWidth      = pcCU->getWidth ( 0 );
4626  UInt      uiHeight     = pcCU->getHeight( 0 );
4627 
4628  //  No residual coding : SKIP mode
4629  if ( bSkipRes )
4630  {
4631    pcCU->setSkipFlagSubParts( true, 0, pcCU->getDepth(0) );
4632
4633    rpcYuvResi->clear();
4634   
4635    pcYuvPred->copyToPartYuv( rpcYuvRec, 0 );
4636   
4637#if WEIGHTED_CHROMA_DISTORTION
4638    uiDistortion = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
4639    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_U )
4640    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_V );
4641#else
4642    uiDistortion = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
4643    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
4644    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
4645#endif
4646
4647    if( m_bUseSBACRD )
4648      m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST]);
4649   
4650    m_pcEntropyCoder->resetBits();
4651    if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
4652    {
4653      m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
4654    }
4655    m_pcEntropyCoder->encodeSkipFlag(pcCU, 0, true);
4656    m_pcEntropyCoder->encodeMergeIndex( pcCU, 0, true );
4657   
4658    uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
4659    pcCU->getTotalBits()       = uiBits;
4660    pcCU->getTotalDistortion() = uiDistortion;
4661    pcCU->getTotalCost()       = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
4662   
4663    if( m_bUseSBACRD )
4664      m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_TEMP_BEST]);
4665   
4666    pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
4667    pcCU->setTrIdxSubParts( 0, 0, pcCU->getDepth(0) );
4668   
4669    return;
4670  }
4671 
4672  //  Residual coding.
4673  Int    qp, qpBest = 0, qpMin, qpMax;
4674  Double  dCost, dCostBest = MAX_DOUBLE;
4675 
4676  UInt uiTrLevel = 0;
4677  if( (pcCU->getWidth(0) > pcCU->getSlice()->getSPS()->getMaxTrSize()) )
4678  {
4679    while( pcCU->getWidth(0) > (pcCU->getSlice()->getSPS()->getMaxTrSize()<<uiTrLevel) ) uiTrLevel++;
4680  }
4681  UInt uiMaxTrMode = pcCU->getSlice()->getSPS()->getMaxTrDepth() + uiTrLevel;
4682 
4683  while((uiWidth>>uiMaxTrMode) < (g_uiMaxCUWidth>>g_uiMaxCUDepth)) uiMaxTrMode--;
4684 
4685  qpMin =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) - m_iMaxDeltaQP ) : pcCU->getQP( 0 );
4686  qpMax =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) + m_iMaxDeltaQP ) : pcCU->getQP( 0 );
4687
4688  rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
4689
4690  for ( qp = qpMin; qp <= qpMax; qp++ )
4691  {
4692    dCost = 0.;
4693    uiBits = 0;
4694    uiDistortion = 0;
4695    if( m_bUseSBACRD )
4696    {
4697      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_CURR_BEST ] );
4698    }
4699   
4700    UInt uiZeroDistortion = 0;
4701    xEstimateResidualQT( pcCU, 0, 0, 0, rpcYuvResi,  pcCU->getDepth(0), dCost, uiBits, uiDistortion, &uiZeroDistortion );
4702   
4703    m_pcEntropyCoder->resetBits();
4704    m_pcEntropyCoder->encodeQtRootCbfZero( pcCU );
4705    UInt zeroResiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
4706    Double dZeroCost = m_pcRdCost->calcRdCost( zeroResiBits, uiZeroDistortion );
4707    if(pcCU->isLosslessCoded( 0 ))
4708    { 
4709      dZeroCost = dCost + 1;
4710    }
4711    if ( dZeroCost < dCost )
4712    {
4713      dCost        = dZeroCost;
4714      uiBits       = 0;
4715      uiDistortion = uiZeroDistortion;
4716     
4717      const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
4718      ::memset( pcCU->getTransformIdx()      , 0, uiQPartNum * sizeof(UChar) );
4719      ::memset( pcCU->getCbf( TEXT_LUMA )    , 0, uiQPartNum * sizeof(UChar) );
4720      ::memset( pcCU->getCbf( TEXT_CHROMA_U ), 0, uiQPartNum * sizeof(UChar) );
4721      ::memset( pcCU->getCbf( TEXT_CHROMA_V ), 0, uiQPartNum * sizeof(UChar) );
4722      ::memset( pcCU->getCoeffY()            , 0, uiWidth * uiHeight * sizeof( TCoeff )      );
4723      ::memset( pcCU->getCoeffCb()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4724      ::memset( pcCU->getCoeffCr()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4725      pcCU->setTransformSkipSubParts ( 0, 0, 0, 0, pcCU->getDepth(0) );
4726    }
4727#if NO_RESIDUAL_FLAG_FOR_BLPRED
4728    else if(pcCU->getLayerId() > 0 && pcCU->isIntraBL(0) && uiZeroDistortion == uiDistortion) // all zeros
4729    {
4730      const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
4731      ::memset( pcCU->getTransformIdx()      , 0, uiQPartNum * sizeof(UChar) );
4732      ::memset( pcCU->getCbf( TEXT_LUMA )    , 0, uiQPartNum * sizeof(UChar) );
4733      ::memset( pcCU->getCbf( TEXT_CHROMA_U ), 0, uiQPartNum * sizeof(UChar) );
4734      ::memset( pcCU->getCbf( TEXT_CHROMA_V ), 0, uiQPartNum * sizeof(UChar) );
4735      ::memset( pcCU->getCoeffY()            , 0, uiWidth * uiHeight * sizeof( TCoeff )      );
4736      ::memset( pcCU->getCoeffCb()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4737      ::memset( pcCU->getCoeffCr()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4738      pcCU->setTransformSkipSubParts ( 0, 0, 0, 0, pcCU->getDepth(0) );
4739    }
4740#endif
4741    else
4742    {
4743      xSetResidualQTData( pcCU, 0, 0, 0, NULL, pcCU->getDepth(0), false );
4744    }
4745   
4746    if( m_bUseSBACRD )
4747    {
4748      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST] );
4749    }
4750#if 0 // check
4751    {
4752      m_pcEntropyCoder->resetBits();
4753      m_pcEntropyCoder->encodeCoeff( pcCU, 0, pcCU->getDepth(0), pcCU->getWidth(0), pcCU->getHeight(0) );
4754      const UInt uiBitsForCoeff = m_pcEntropyCoder->getNumberOfWrittenBits();
4755      if( m_bUseSBACRD )
4756      {
4757        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST] );
4758      }
4759      if( uiBitsForCoeff != uiBits )
4760        assert( 0 );
4761    }
4762#endif
4763    uiBits = 0;
4764    {
4765      TComYuv *pDummy = NULL;
4766      xAddSymbolBitsInter( pcCU, 0, 0, uiBits, pDummy, NULL, pDummy );
4767    }
4768   
4769   
4770    Double dExactCost = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
4771    dCost = dExactCost;
4772   
4773    if ( dCost < dCostBest )
4774    {
4775      if ( !pcCU->getQtRootCbf( 0 ) )
4776      {
4777        rpcYuvResiBest->clear();
4778      }
4779      else
4780      {
4781        xSetResidualQTData( pcCU, 0, 0, 0, rpcYuvResiBest, pcCU->getDepth(0), true );
4782      }
4783     
4784      if( qpMin != qpMax && qp != qpMax )
4785      {
4786        const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
4787        ::memcpy( m_puhQTTempTrIdx, pcCU->getTransformIdx(),        uiQPartNum * sizeof(UChar) );
4788        ::memcpy( m_puhQTTempCbf[0], pcCU->getCbf( TEXT_LUMA ),     uiQPartNum * sizeof(UChar) );
4789        ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ), uiQPartNum * sizeof(UChar) );
4790        ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ), uiQPartNum * sizeof(UChar) );
4791        ::memcpy( m_pcQTTempCoeffY,  pcCU->getCoeffY(),  uiWidth * uiHeight * sizeof( TCoeff )      );
4792        ::memcpy( m_pcQTTempCoeffCb, pcCU->getCoeffCb(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4793        ::memcpy( m_pcQTTempCoeffCr, pcCU->getCoeffCr(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4794#if ADAPTIVE_QP_SELECTION
4795        ::memcpy( m_pcQTTempArlCoeffY,  pcCU->getArlCoeffY(),  uiWidth * uiHeight * sizeof( Int )      );
4796        ::memcpy( m_pcQTTempArlCoeffCb, pcCU->getArlCoeffCb(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
4797        ::memcpy( m_pcQTTempArlCoeffCr, pcCU->getArlCoeffCr(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
4798#endif
4799        ::memcpy( m_puhQTTempTransformSkipFlag[0], pcCU->getTransformSkip(TEXT_LUMA),     uiQPartNum * sizeof( UChar ) );
4800        ::memcpy( m_puhQTTempTransformSkipFlag[1], pcCU->getTransformSkip(TEXT_CHROMA_U), uiQPartNum * sizeof( UChar ) );
4801        ::memcpy( m_puhQTTempTransformSkipFlag[2], pcCU->getTransformSkip(TEXT_CHROMA_V), uiQPartNum * sizeof( UChar ) );
4802      }
4803      uiBitsBest       = uiBits;
4804      uiDistortionBest = uiDistortion;
4805      dCostBest        = dCost;
4806      qpBest           = qp;
4807      if( m_bUseSBACRD )
4808      {
4809        m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_TEMP_BEST ] );
4810      }
4811    }
4812  }
4813 
4814  assert ( dCostBest != MAX_DOUBLE );
4815 
4816  if( qpMin != qpMax && qpBest != qpMax )
4817  {
4818    if( m_bUseSBACRD )
4819    {
4820      assert( 0 ); // check
4821      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_TEMP_BEST ] );
4822    }
4823    // copy best cbf and trIdx to pcCU
4824    const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
4825    ::memcpy( pcCU->getTransformIdx(),       m_puhQTTempTrIdx,  uiQPartNum * sizeof(UChar) );
4826    ::memcpy( pcCU->getCbf( TEXT_LUMA ),     m_puhQTTempCbf[0], uiQPartNum * sizeof(UChar) );
4827    ::memcpy( pcCU->getCbf( TEXT_CHROMA_U ), m_puhQTTempCbf[1], uiQPartNum * sizeof(UChar) );
4828    ::memcpy( pcCU->getCbf( TEXT_CHROMA_V ), m_puhQTTempCbf[2], uiQPartNum * sizeof(UChar) );
4829    ::memcpy( pcCU->getCoeffY(),  m_pcQTTempCoeffY,  uiWidth * uiHeight * sizeof( TCoeff )      );
4830    ::memcpy( pcCU->getCoeffCb(), m_pcQTTempCoeffCb, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4831    ::memcpy( pcCU->getCoeffCr(), m_pcQTTempCoeffCr, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
4832#if ADAPTIVE_QP_SELECTION
4833    ::memcpy( pcCU->getArlCoeffY(),  m_pcQTTempArlCoeffY,  uiWidth * uiHeight * sizeof( Int )      );
4834    ::memcpy( pcCU->getArlCoeffCb(), m_pcQTTempArlCoeffCb, uiWidth * uiHeight * sizeof( Int ) >> 2 );
4835    ::memcpy( pcCU->getArlCoeffCr(), m_pcQTTempArlCoeffCr, uiWidth * uiHeight * sizeof( Int ) >> 2 );
4836#endif
4837    ::memcpy( pcCU->getTransformSkip(TEXT_LUMA),     m_puhQTTempTransformSkipFlag[0], uiQPartNum * sizeof( UChar ) );
4838    ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_U), m_puhQTTempTransformSkipFlag[1], uiQPartNum * sizeof( UChar ) );
4839    ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_V), m_puhQTTempTransformSkipFlag[2], uiQPartNum * sizeof( UChar ) );
4840  }
4841  rpcYuvRec->addClip ( pcYuvPred, rpcYuvResiBest, 0, uiWidth );
4842 
4843  // update with clipped distortion and cost (qp estimation loop uses unclipped values)
4844#if WEIGHTED_CHROMA_DISTORTION
4845    uiDistortionBest = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
4846    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_U )
4847    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_V );
4848#else
4849  uiDistortionBest = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
4850  + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
4851  + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
4852#endif
4853  dCostBest = m_pcRdCost->calcRdCost( uiBitsBest, uiDistortionBest );
4854 
4855  pcCU->getTotalBits()       = uiBitsBest;
4856  pcCU->getTotalDistortion() = uiDistortionBest;
4857  pcCU->getTotalCost()       = dCostBest;
4858 
4859  if ( pcCU->isSkipped(0) )
4860  {
4861    pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
4862  }
4863 
4864  pcCU->setQPSubParts( qpBest, 0, pcCU->getDepth(0) );
4865}
4866
4867Void TEncSearch::xEstimateResidualQT( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcResi, const UInt uiDepth, Double &rdCost, UInt &ruiBits, UInt &ruiDist, UInt *puiZeroDist )
4868{
4869  const UInt uiTrMode = uiDepth - pcCU->getDepth( 0 );
4870 
4871  assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
4872  const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
4873 
4874  UInt SplitFlag = ((pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) && pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && ( pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N ));
4875  Bool bCheckFull;
4876  if ( SplitFlag && uiDepth == pcCU->getDepth(uiAbsPartIdx) && ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) )
4877     bCheckFull = false;
4878  else
4879     bCheckFull =  ( uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
4880
4881  const Bool bCheckSplit  = ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
4882 
4883  assert( bCheckFull || bCheckSplit );
4884 
4885  Bool  bCodeChroma   = true;
4886  UInt  uiTrModeC     = uiTrMode;
4887  UInt  uiLog2TrSizeC = uiLog2TrSize-1;
4888  if( uiLog2TrSize == 2 )
4889  {
4890    uiLog2TrSizeC++;
4891    uiTrModeC    --;
4892    UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
4893    bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
4894  }
4895 
4896  const UInt uiSetCbf = 1 << uiTrMode;
4897  // code full block
4898  Double dSingleCost = MAX_DOUBLE;
4899  UInt uiSingleBits = 0;
4900  UInt uiSingleDist = 0;
4901  UInt uiAbsSumY = 0, uiAbsSumU = 0, uiAbsSumV = 0;
4902  UInt uiBestTransformMode[3] = {0};
4903
4904  if( m_bUseSBACRD )
4905  {
4906    m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
4907  }
4908 
4909  if( bCheckFull )
4910  {
4911    const UInt uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
4912    const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
4913    TCoeff *pcCoeffCurrY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
4914    TCoeff *pcCoeffCurrU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
4915    TCoeff *pcCoeffCurrV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
4916#if ADAPTIVE_QP_SELECTION   
4917    Int *pcArlCoeffCurrY = m_ppcQTTempArlCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
4918    Int *pcArlCoeffCurrU = m_ppcQTTempArlCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
4919    Int *pcArlCoeffCurrV = m_ppcQTTempArlCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);   
4920#endif
4921   
4922    Int trWidth = 0, trHeight = 0, trWidthC = 0, trHeightC = 0;
4923    UInt absTUPartIdxC = uiAbsPartIdx;
4924
4925    trWidth  = trHeight  = 1 << uiLog2TrSize;
4926    trWidthC = trHeightC = 1 <<uiLog2TrSizeC;
4927    pcCU->setTrIdxSubParts( uiDepth - pcCU->getDepth( 0 ), uiAbsPartIdx, uiDepth );
4928    Double minCostY = MAX_DOUBLE;
4929    Double minCostU = MAX_DOUBLE;
4930    Double minCostV = MAX_DOUBLE;
4931    Bool checkTransformSkipY  = pcCU->getSlice()->getPPS()->getUseTransformSkip() && trWidth == 4 && trHeight == 4;
4932    Bool checkTransformSkipUV = pcCU->getSlice()->getPPS()->getUseTransformSkip() && trWidthC == 4 && trHeightC == 4;
4933
4934    checkTransformSkipY         &= (!pcCU->isLosslessCoded(0));
4935    checkTransformSkipUV        &= (!pcCU->isLosslessCoded(0));
4936
4937    pcCU->setTransformSkipSubParts ( 0, TEXT_LUMA, uiAbsPartIdx, uiDepth ); 
4938    if( bCodeChroma )
4939    {
4940      pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC ); 
4941      pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC ); 
4942    }
4943
4944    if (m_pcEncCfg->getUseRDOQ())
4945    {
4946      m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidth, trHeight, TEXT_LUMA );       
4947    }
4948
4949    m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
4950
4951#if RDOQ_CHROMA_LAMBDA
4952    m_pcTrQuant->selectLambda(TEXT_LUMA); 
4953#endif
4954    m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride (), pcCoeffCurrY, 
4955#if ADAPTIVE_QP_SELECTION
4956                                 pcArlCoeffCurrY, 
4957#endif     
4958                                 trWidth,   trHeight,    uiAbsSumY, TEXT_LUMA,     uiAbsPartIdx );
4959   
4960    pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
4961   
4962    if( bCodeChroma )
4963    {
4964      if (m_pcEncCfg->getUseRDOQ())
4965      {
4966        m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidthC, trHeightC, TEXT_CHROMA );         
4967      }
4968
4969      Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
4970      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
4971
4972#if RDOQ_CHROMA_LAMBDA
4973      m_pcTrQuant->selectLambda(TEXT_CHROMA); 
4974#endif
4975
4976      m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrU, 
4977#if ADAPTIVE_QP_SELECTION
4978                                 pcArlCoeffCurrU, 
4979#endif       
4980                                 trWidthC, trHeightC, uiAbsSumU, TEXT_CHROMA_U, uiAbsPartIdx );
4981
4982      curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
4983      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
4984      m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrV, 
4985#if ADAPTIVE_QP_SELECTION
4986                                 pcArlCoeffCurrV, 
4987#endif       
4988                                 trWidthC, trHeightC, uiAbsSumV, TEXT_CHROMA_V, uiAbsPartIdx );
4989
4990      pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
4991      pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
4992    }
4993   
4994    m_pcEntropyCoder->resetBits();
4995   
4996    {
4997      m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
4998    }
4999   
5000    m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx,  trWidth,  trHeight,    uiDepth, TEXT_LUMA );
5001    const UInt uiSingleBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
5002   
5003    UInt uiSingleBitsU = 0;
5004    UInt uiSingleBitsV = 0;
5005    if( bCodeChroma )
5006    {
5007      {
5008        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
5009      }
5010      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
5011      uiSingleBitsU = m_pcEntropyCoder->getNumberOfWrittenBits() - uiSingleBitsY;
5012     
5013      {
5014        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
5015      }
5016      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
5017      uiSingleBitsV = m_pcEntropyCoder->getNumberOfWrittenBits() - ( uiSingleBitsY + uiSingleBitsU );
5018    }
5019   
5020    const UInt uiNumSamplesLuma = 1 << (uiLog2TrSize<<1);
5021    const UInt uiNumSamplesChro = 1 << (uiLog2TrSizeC<<1);
5022   
5023    ::memset( m_pTempPel, 0, sizeof( Pel ) * uiNumSamplesLuma ); // not necessary needed for inside of recursion (only at the beginning)
5024   
5025    UInt uiDistY = m_pcRdCost->getDistPart(g_bitDepthY, m_pTempPel, trWidth, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth, trHeight ); // initialized with zero residual destortion
5026
5027    if ( puiZeroDist )
5028    {
5029      *puiZeroDist += uiDistY;
5030    }
5031    if( uiAbsSumY )
5032    {
5033      Pel *pcResiCurrY = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getLumaAddr( absTUPartIdx );
5034
5035      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
5036
5037      Int scalingListType = 3 + g_eTTable[(Int)TEXT_LUMA];
5038      assert(scalingListType < 6);     
5039#if NO_RESIDUAL_FLAG_FOR_BLPRED
5040      if(pcCU->isIntraBL(uiAbsPartIdx) )
5041      {
5042        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,DC_IDX, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
5043      }
5044      else
5045      {
5046        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
5047      }
5048#else
5049      m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
5050#endif
5051     
5052      const UInt uiNonzeroDistY = m_pcRdCost->getDistPart(g_bitDepthY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
5053      pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth,trHeight );
5054      if (pcCU->isLosslessCoded(0)) 
5055      {
5056        uiDistY = uiNonzeroDistY;
5057      }
5058      else
5059      {
5060        const Double singleCostY = m_pcRdCost->calcRdCost( uiSingleBitsY, uiNonzeroDistY );
5061        m_pcEntropyCoder->resetBits();
5062        m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_LUMA,     uiTrMode );
5063        const UInt uiNullBitsY   = m_pcEntropyCoder->getNumberOfWrittenBits();
5064        const Double nullCostY   = m_pcRdCost->calcRdCost( uiNullBitsY, uiDistY );
5065        if( nullCostY < singleCostY ) 
5066        {   
5067          uiAbsSumY = 0;
5068          ::memset( pcCoeffCurrY, 0, sizeof( TCoeff ) * uiNumSamplesLuma );
5069          if( checkTransformSkipY )
5070          {
5071            minCostY = nullCostY;
5072          }
5073        }
5074        else
5075        {
5076          uiDistY = uiNonzeroDistY;
5077          if( checkTransformSkipY )
5078          {
5079            minCostY = singleCostY;
5080          }
5081        }
5082      }
5083    }
5084    else if( checkTransformSkipY )
5085    {
5086      m_pcEntropyCoder->resetBits();
5087      m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_LUMA, uiTrMode );
5088      const UInt uiNullBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
5089      minCostY = m_pcRdCost->calcRdCost( uiNullBitsY, uiDistY );
5090    }
5091
5092    if( !uiAbsSumY )
5093    {
5094      Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx );
5095      const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride();
5096      for( UInt uiY = 0; uiY < trHeight; ++uiY )
5097      {
5098        ::memset( pcPtr, 0, sizeof( Pel ) * trWidth );
5099        pcPtr += uiStride;
5100      } 
5101    }
5102   
5103    UInt uiDistU = 0;
5104    UInt uiDistV = 0;
5105    if( bCodeChroma )
5106    {
5107      uiDistU = m_pcRdCost->getDistPart(g_bitDepthC, m_pTempPel, trWidthC, pcResi->getCbAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
5108#if WEIGHTED_CHROMA_DISTORTION
5109                                        , TEXT_CHROMA_U
5110#endif
5111                                        ); // initialized with zero residual destortion
5112      if ( puiZeroDist )
5113      {
5114        *puiZeroDist += uiDistU;
5115      }
5116      if( uiAbsSumU )
5117      {
5118        Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
5119
5120        Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
5121        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
5122
5123        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_U];
5124        assert(scalingListType < 6);
5125        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType  );
5126       
5127        const UInt uiNonzeroDistU = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
5128                                                            pcResi->getCbAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
5129#if WEIGHTED_CHROMA_DISTORTION
5130                                                            , TEXT_CHROMA_U
5131#endif
5132                                                            );
5133
5134        if(pcCU->isLosslessCoded(0)) 
5135        {
5136          uiDistU = uiNonzeroDistU;
5137        }
5138        else
5139        {
5140          const Double dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
5141          m_pcEntropyCoder->resetBits();
5142          m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_U,     uiTrMode );
5143          const UInt uiNullBitsU    = m_pcEntropyCoder->getNumberOfWrittenBits();
5144          const Double dNullCostU   = m_pcRdCost->calcRdCost( uiNullBitsU, uiDistU );
5145          if( dNullCostU < dSingleCostU )
5146          {
5147            uiAbsSumU = 0;
5148            ::memset( pcCoeffCurrU, 0, sizeof( TCoeff ) * uiNumSamplesChro );
5149            if( checkTransformSkipUV )
5150            {
5151              minCostU = dNullCostU;
5152            }
5153          }
5154          else
5155          {
5156            uiDistU = uiNonzeroDistU;
5157            if( checkTransformSkipUV )
5158            {
5159              minCostU = dSingleCostU;
5160            }
5161          }
5162        }
5163      }
5164      else if( checkTransformSkipUV )
5165      {
5166        m_pcEntropyCoder->resetBits();
5167        m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_U, uiTrModeC );
5168        const UInt uiNullBitsU = m_pcEntropyCoder->getNumberOfWrittenBits();
5169        minCostU = m_pcRdCost->calcRdCost( uiNullBitsU, uiDistU );
5170      }
5171      if( !uiAbsSumU )
5172      {
5173        Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
5174          const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
5175        for( UInt uiY = 0; uiY < trHeightC; ++uiY )
5176        {
5177          ::memset( pcPtr, 0, sizeof(Pel) * trWidthC );
5178          pcPtr += uiStride;
5179        }
5180      }
5181     
5182      uiDistV = m_pcRdCost->getDistPart(g_bitDepthC, m_pTempPel, trWidthC, pcResi->getCrAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
5183#if WEIGHTED_CHROMA_DISTORTION
5184                                        , TEXT_CHROMA_V
5185#endif
5186                                        ); // initialized with zero residual destortion
5187      if ( puiZeroDist )
5188      {
5189        *puiZeroDist += uiDistV;
5190      }
5191      if( uiAbsSumV )
5192      {
5193        Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
5194        Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
5195        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
5196
5197        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_V];
5198        assert(scalingListType < 6);
5199        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, trWidthC, trHeightC, scalingListType );
5200       
5201        const UInt uiNonzeroDistV = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
5202                                                            pcResi->getCrAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
5203#if WEIGHTED_CHROMA_DISTORTION
5204                                                            , TEXT_CHROMA_V
5205#endif
5206                                                            );
5207        if (pcCU->isLosslessCoded(0)) 
5208        {
5209          uiDistV = uiNonzeroDistV;
5210        }
5211        else
5212        {
5213          const Double dSingleCostV = m_pcRdCost->calcRdCost( uiSingleBitsV, uiNonzeroDistV );
5214          m_pcEntropyCoder->resetBits();
5215          m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_V,     uiTrMode );
5216          const UInt uiNullBitsV    = m_pcEntropyCoder->getNumberOfWrittenBits();
5217          const Double dNullCostV   = m_pcRdCost->calcRdCost( uiNullBitsV, uiDistV );
5218          if( dNullCostV < dSingleCostV )
5219          {
5220            uiAbsSumV = 0;
5221            ::memset( pcCoeffCurrV, 0, sizeof( TCoeff ) * uiNumSamplesChro );
5222            if( checkTransformSkipUV )
5223            {
5224              minCostV = dNullCostV;
5225            }
5226          }
5227          else
5228          {
5229            uiDistV = uiNonzeroDistV;
5230            if( checkTransformSkipUV )
5231            {
5232              minCostV = dSingleCostV;
5233            }
5234          }
5235        }
5236      }
5237      else if( checkTransformSkipUV )
5238      {
5239        m_pcEntropyCoder->resetBits();
5240        m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_V, uiTrModeC );
5241        const UInt uiNullBitsV = m_pcEntropyCoder->getNumberOfWrittenBits();
5242        minCostV = m_pcRdCost->calcRdCost( uiNullBitsV, uiDistV );
5243      }
5244      if( !uiAbsSumV )
5245      {
5246        Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
5247        const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
5248        for( UInt uiY = 0; uiY < trHeightC; ++uiY )
5249        {   
5250          ::memset( pcPtr, 0, sizeof(Pel) * trWidthC );
5251          pcPtr += uiStride;
5252        }
5253      }
5254    }
5255    pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
5256    if( bCodeChroma )
5257    {
5258      pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5259      pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5260    }
5261
5262    if( checkTransformSkipY )
5263    {
5264      UInt uiNonzeroDistY, uiAbsSumTransformSkipY;
5265      Double dSingleCostY;
5266
5267      Pel *pcResiCurrY = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getLumaAddr( absTUPartIdx );
5268      UInt resiYStride = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getStride();
5269
5270      TCoeff bestCoeffY[32*32];
5271      memcpy( bestCoeffY, pcCoeffCurrY, sizeof(TCoeff) * uiNumSamplesLuma );
5272     
5273#if ADAPTIVE_QP_SELECTION
5274      TCoeff bestArlCoeffY[32*32];
5275      memcpy( bestArlCoeffY, pcArlCoeffCurrY, sizeof(TCoeff) * uiNumSamplesLuma );
5276#endif
5277
5278      Pel bestResiY[32*32];
5279      for ( Int i = 0; i < trHeight; ++i )
5280      {
5281        memcpy( &bestResiY[i*trWidth], pcResiCurrY+i*resiYStride, sizeof(Pel) * trWidth );
5282      }
5283
5284      if( m_bUseSBACRD )
5285      {
5286        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
5287      }
5288
5289      pcCU->setTransformSkipSubParts ( 1, TEXT_LUMA, uiAbsPartIdx, uiDepth );
5290
5291      if (m_pcEncCfg->getUseRDOQTS())
5292      {
5293        m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, trWidth, trHeight, TEXT_LUMA );       
5294      }
5295
5296      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
5297
5298#if RDOQ_CHROMA_LAMBDA
5299      m_pcTrQuant->selectLambda(TEXT_LUMA);
5300#endif
5301      m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride (), pcCoeffCurrY, 
5302#if ADAPTIVE_QP_SELECTION
5303        pcArlCoeffCurrY, 
5304#endif     
5305        trWidth,   trHeight,    uiAbsSumTransformSkipY, TEXT_LUMA, uiAbsPartIdx, true );
5306      pcCU->setCbfSubParts( uiAbsSumTransformSkipY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
5307
5308      if( uiAbsSumTransformSkipY != 0 )
5309      {
5310        m_pcEntropyCoder->resetBits();
5311        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, uiTrMode );
5312        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
5313        const UInt uiTsSingleBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
5314
5315        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
5316
5317        Int scalingListType = 3 + g_eTTable[(Int)TEXT_LUMA];
5318        assert(scalingListType < 6);     
5319
5320#if NO_RESIDUAL_FLAG_FOR_BLPRED
5321        if(pcCU->isIntraBL(uiAbsPartIdx) )
5322        {
5323          m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,DC_IDX, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
5324        }
5325        else
5326        {
5327          m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
5328        }
5329#else
5330        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
5331#endif
5332
5333        uiNonzeroDistY = m_pcRdCost->getDistPart(g_bitDepthY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
5334          pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth, trHeight );
5335
5336        dSingleCostY = m_pcRdCost->calcRdCost( uiTsSingleBitsY, uiNonzeroDistY );
5337      }
5338
5339      if( !uiAbsSumTransformSkipY || minCostY < dSingleCostY )
5340      {
5341        pcCU->setTransformSkipSubParts ( 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
5342        memcpy( pcCoeffCurrY, bestCoeffY, sizeof(TCoeff) * uiNumSamplesLuma );
5343#if ADAPTIVE_QP_SELECTION
5344        memcpy( pcArlCoeffCurrY, bestArlCoeffY, sizeof(TCoeff) * uiNumSamplesLuma );
5345#endif
5346        for( Int i = 0; i < trHeight; ++i )
5347        {
5348          memcpy( pcResiCurrY+i*resiYStride, &bestResiY[i*trWidth], sizeof(Pel) * trWidth );
5349        }
5350      }
5351      else
5352      {
5353        uiDistY = uiNonzeroDistY;
5354        uiAbsSumY = uiAbsSumTransformSkipY;
5355        uiBestTransformMode[0] = 1;
5356      }
5357
5358      pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
5359    }
5360
5361    if( bCodeChroma && checkTransformSkipUV  )
5362    {
5363      UInt uiNonzeroDistU, uiNonzeroDistV, uiAbsSumTransformSkipU, uiAbsSumTransformSkipV;
5364      Double dSingleCostU, dSingleCostV;
5365
5366      Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
5367      Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
5368      UInt resiCStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
5369
5370      TCoeff bestCoeffU[32*32], bestCoeffV[32*32];
5371      memcpy( bestCoeffU, pcCoeffCurrU, sizeof(TCoeff) * uiNumSamplesChro );
5372      memcpy( bestCoeffV, pcCoeffCurrV, sizeof(TCoeff) * uiNumSamplesChro );
5373
5374#if ADAPTIVE_QP_SELECTION
5375      TCoeff bestArlCoeffU[32*32], bestArlCoeffV[32*32];
5376      memcpy( bestArlCoeffU, pcArlCoeffCurrU, sizeof(TCoeff) * uiNumSamplesChro );
5377      memcpy( bestArlCoeffV, pcArlCoeffCurrV, sizeof(TCoeff) * uiNumSamplesChro );
5378#endif
5379
5380      Pel bestResiU[32*32], bestResiV[32*32];
5381      for (Int i = 0; i < trHeightC; ++i )
5382      {
5383        memcpy( &bestResiU[i*trWidthC], pcResiCurrU+i*resiCStride, sizeof(Pel) * trWidthC );
5384        memcpy( &bestResiV[i*trWidthC], pcResiCurrV+i*resiCStride, sizeof(Pel) * trWidthC );
5385      }
5386
5387      if( m_bUseSBACRD )
5388      {
5389        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
5390      }
5391
5392      pcCU->setTransformSkipSubParts ( 1, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC ); 
5393      pcCU->setTransformSkipSubParts ( 1, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5394
5395      if (m_pcEncCfg->getUseRDOQTS())
5396      {
5397        m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidthC, trHeightC, TEXT_CHROMA );         
5398      }
5399
5400      Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
5401      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
5402
5403#if RDOQ_CHROMA_LAMBDA
5404      m_pcTrQuant->selectLambda(TEXT_CHROMA); 
5405#endif
5406
5407      m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrU, 
5408#if ADAPTIVE_QP_SELECTION
5409        pcArlCoeffCurrU, 
5410#endif       
5411        trWidthC, trHeightC, uiAbsSumTransformSkipU, TEXT_CHROMA_U, uiAbsPartIdx, true );
5412      curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
5413      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
5414      m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrV, 
5415#if ADAPTIVE_QP_SELECTION
5416        pcArlCoeffCurrV, 
5417#endif       
5418        trWidthC, trHeightC, uiAbsSumTransformSkipV, TEXT_CHROMA_V, uiAbsPartIdx, true );
5419
5420      pcCU->setCbfSubParts( uiAbsSumTransformSkipU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5421      pcCU->setCbfSubParts( uiAbsSumTransformSkipV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5422
5423      m_pcEntropyCoder->resetBits();
5424      uiSingleBitsU = 0;
5425      uiSingleBitsV = 0;
5426
5427      if( uiAbsSumTransformSkipU )
5428      {
5429        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
5430        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
5431        uiSingleBitsU = m_pcEntropyCoder->getNumberOfWrittenBits();   
5432
5433        curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
5434        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
5435
5436        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_U];
5437        assert(scalingListType < 6);
5438
5439        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType, true  );
5440
5441        uiNonzeroDistU = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
5442                                                 pcResi->getCbAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
5443#if WEIGHTED_CHROMA_DISTORTION
5444                                                 , TEXT_CHROMA_U
5445#endif
5446                                                 );
5447
5448        dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
5449      }
5450
5451      if( !uiAbsSumTransformSkipU || minCostU < dSingleCostU )
5452      {
5453        pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC ); 
5454
5455        memcpy( pcCoeffCurrU, bestCoeffU, sizeof (TCoeff) * uiNumSamplesChro );
5456#if ADAPTIVE_QP_SELECTION
5457        memcpy( pcArlCoeffCurrU, bestArlCoeffU, sizeof (TCoeff) * uiNumSamplesChro );
5458#endif
5459        for( Int i = 0; i < trHeightC; ++i )
5460        {
5461          memcpy( pcResiCurrU+i*resiCStride, &bestResiU[i*trWidthC], sizeof(Pel) * trWidthC );
5462        }
5463      }
5464      else
5465      {
5466        uiDistU = uiNonzeroDistU;
5467        uiAbsSumU = uiAbsSumTransformSkipU;
5468        uiBestTransformMode[1] = 1;
5469      }
5470
5471      if( uiAbsSumTransformSkipV )
5472      {
5473        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
5474        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
5475        uiSingleBitsV = m_pcEntropyCoder->getNumberOfWrittenBits() - uiSingleBitsU;
5476
5477        curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
5478        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
5479
5480        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_V];
5481        assert(scalingListType < 6);
5482
5483        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, trWidthC, trHeightC, scalingListType, true );
5484
5485        uiNonzeroDistV = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
5486                                                 pcResi->getCrAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
5487#if WEIGHTED_CHROMA_DISTORTION
5488                                                 , TEXT_CHROMA_V
5489#endif
5490                                                 );
5491
5492        dSingleCostV = m_pcRdCost->calcRdCost( uiSingleBitsV, uiNonzeroDistV );
5493      }
5494
5495      if( !uiAbsSumTransformSkipV || minCostV < dSingleCostV )
5496      {
5497        pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC ); 
5498
5499        memcpy( pcCoeffCurrV, bestCoeffV, sizeof(TCoeff) * uiNumSamplesChro );
5500#if ADAPTIVE_QP_SELECTION
5501        memcpy( pcArlCoeffCurrV, bestArlCoeffV, sizeof(TCoeff) * uiNumSamplesChro );
5502#endif
5503        for( Int i = 0; i < trHeightC; ++i )
5504        {
5505          memcpy( pcResiCurrV+i*resiCStride, &bestResiV[i*trWidthC], sizeof(Pel) * trWidthC );
5506        }
5507      }
5508      else
5509      {
5510        uiDistV = uiNonzeroDistV;
5511        uiAbsSumV = uiAbsSumTransformSkipV;
5512        uiBestTransformMode[2] = 1;
5513      }
5514
5515      pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5516      pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5517    }
5518
5519    if( m_bUseSBACRD )
5520    {
5521      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
5522    }
5523
5524    m_pcEntropyCoder->resetBits();
5525
5526    {
5527      if( uiLog2TrSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
5528      {
5529        m_pcEntropyCoder->encodeTransformSubdivFlag( 0, 5 - uiLog2TrSize );
5530      }
5531    }
5532
5533    {
5534      if( bCodeChroma )
5535      {
5536        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
5537        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
5538      }
5539
5540      m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
5541    }
5542
5543    m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight,    uiDepth, TEXT_LUMA );
5544
5545    if( bCodeChroma )
5546    {
5547      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
5548      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
5549    }
5550
5551    uiSingleBits = m_pcEntropyCoder->getNumberOfWrittenBits();
5552
5553    uiSingleDist = uiDistY + uiDistU + uiDistV;
5554    dSingleCost = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDist );
5555  } 
5556 
5557  // code sub-blocks
5558  if( bCheckSplit )
5559  {
5560    if( m_bUseSBACRD && bCheckFull )
5561    {
5562      m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_TEST ] );
5563      m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
5564    }
5565    UInt uiSubdivDist = 0;
5566    UInt uiSubdivBits = 0;
5567    Double dSubdivCost = 0.0;
5568   
5569    const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
5570    for( UInt ui = 0; ui < 4; ++ui )
5571    {
5572      UInt nsAddr = uiAbsPartIdx + ui * uiQPartNumSubdiv;
5573      xEstimateResidualQT( pcCU, ui, uiAbsPartIdx + ui * uiQPartNumSubdiv, nsAddr, pcResi, uiDepth + 1, dSubdivCost, uiSubdivBits, uiSubdivDist, bCheckFull ? NULL : puiZeroDist );
5574    }
5575   
5576    UInt uiYCbf = 0;
5577    UInt uiUCbf = 0;
5578    UInt uiVCbf = 0;
5579    for( UInt ui = 0; ui < 4; ++ui )
5580    {
5581      uiYCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_LUMA,     uiTrMode + 1 );
5582      uiUCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_CHROMA_U, uiTrMode + 1 );
5583      uiVCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_CHROMA_V, uiTrMode + 1 );
5584    }
5585    for( UInt ui = 0; ui < 4 * uiQPartNumSubdiv; ++ui )
5586    {
5587      pcCU->getCbf( TEXT_LUMA     )[uiAbsPartIdx + ui] |= uiYCbf << uiTrMode;
5588      pcCU->getCbf( TEXT_CHROMA_U )[uiAbsPartIdx + ui] |= uiUCbf << uiTrMode;
5589      pcCU->getCbf( TEXT_CHROMA_V )[uiAbsPartIdx + ui] |= uiVCbf << uiTrMode;
5590    }
5591   
5592    if( m_bUseSBACRD )
5593    {
5594      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
5595    }
5596    m_pcEntropyCoder->resetBits();
5597   
5598    {
5599      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, true,  TEXT_LUMA );
5600      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_LUMA );
5601      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_CHROMA_U );
5602      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_CHROMA_V );
5603    }
5604   
5605    uiSubdivBits = m_pcEntropyCoder->getNumberOfWrittenBits();
5606    dSubdivCost  = m_pcRdCost->calcRdCost( uiSubdivBits, uiSubdivDist );
5607   
5608    if( uiYCbf || uiUCbf || uiVCbf || !bCheckFull )
5609    {
5610      if( dSubdivCost < dSingleCost )
5611      {
5612        rdCost += dSubdivCost;
5613        ruiBits += uiSubdivBits;
5614        ruiDist += uiSubdivDist;
5615        return;
5616      }
5617    }
5618    pcCU->setTransformSkipSubParts ( uiBestTransformMode[0], TEXT_LUMA, uiAbsPartIdx, uiDepth ); 
5619    if(bCodeChroma)
5620    {
5621      pcCU->setTransformSkipSubParts ( uiBestTransformMode[1], TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC ); 
5622      pcCU->setTransformSkipSubParts ( uiBestTransformMode[2], TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC ); 
5623    }
5624    assert( bCheckFull );
5625    if( m_bUseSBACRD )
5626    {
5627      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_TEST ] );
5628    }
5629  }
5630  rdCost += dSingleCost;
5631  ruiBits += uiSingleBits;
5632  ruiDist += uiSingleDist;
5633 
5634  pcCU->setTrIdxSubParts( uiTrMode, uiAbsPartIdx, uiDepth );
5635 
5636  pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
5637  if( bCodeChroma )
5638  {
5639    pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5640    pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
5641  }
5642}
5643
5644Void TEncSearch::xEncodeResidualQT( TComDataCU* pcCU, UInt uiAbsPartIdx, const UInt uiDepth, Bool bSubdivAndCbf, TextType eType )
5645{
5646  assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
5647  const UInt uiCurrTrMode = uiDepth - pcCU->getDepth( 0 );
5648  const UInt uiTrMode = pcCU->getTransformIdx( uiAbsPartIdx );
5649 
5650  const Bool bSubdiv = uiCurrTrMode != uiTrMode;
5651 
5652  const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
5653
5654  {
5655    if( bSubdivAndCbf && uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() && uiLog2TrSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
5656    {
5657      m_pcEntropyCoder->encodeTransformSubdivFlag( bSubdiv, 5 - uiLog2TrSize );
5658    }
5659  }
5660
5661  {
5662#if INTRA_BL
5663#if NO_RESIDUAL_FLAG_FOR_BLPRED
5664    assert( !pcCU->isIntra(uiAbsPartIdx) || pcCU->isIntraBL(uiAbsPartIdx));
5665#else
5666    assert( !pcCU->isIntra(uiAbsPartIdx) );
5667#endif
5668#else
5669    assert( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA );
5670#endif
5671    if( bSubdivAndCbf )
5672    {
5673      const Bool bFirstCbfOfCU = uiCurrTrMode == 0;
5674      if( bFirstCbfOfCU || uiLog2TrSize > 2 )
5675      {
5676        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode - 1 ) )
5677        {
5678          m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode );
5679        }
5680        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode - 1 ) )
5681        {
5682          m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode );
5683        }
5684      }
5685      else if( uiLog2TrSize == 2 )
5686      {
5687        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode - 1 ) );
5688        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode - 1 ) );
5689      }
5690    }
5691  }
5692 
5693  if( !bSubdiv )
5694  {
5695    const UInt uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
5696    //assert( 16 == uiNumCoeffPerAbsPartIdxIncrement ); // check
5697    const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
5698    TCoeff *pcCoeffCurrY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
5699    TCoeff *pcCoeffCurrU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5700    TCoeff *pcCoeffCurrV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5701   
5702    Bool  bCodeChroma   = true;
5703    UInt  uiTrModeC     = uiTrMode;
5704    UInt  uiLog2TrSizeC = uiLog2TrSize-1;
5705    if( uiLog2TrSize == 2 )
5706    {
5707      uiLog2TrSizeC++;
5708      uiTrModeC    --;
5709      UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
5710      bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
5711    }
5712   
5713    if( bSubdivAndCbf )
5714    {
5715      {
5716        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
5717      }
5718    }
5719    else
5720    {
5721      if( eType == TEXT_LUMA     && pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA,     uiTrMode ) )
5722      {
5723        Int trWidth  = 1 << uiLog2TrSize;
5724        Int trHeight = 1 << uiLog2TrSize;
5725        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight,    uiDepth, TEXT_LUMA );
5726      }
5727      if( bCodeChroma )
5728      {
5729        Int trWidth  = 1 << uiLog2TrSizeC;
5730        Int trHeight = 1 << uiLog2TrSizeC;
5731        if( eType == TEXT_CHROMA_U && pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode ) )
5732        {
5733          m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
5734        }
5735        if( eType == TEXT_CHROMA_V && pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode ) )
5736        {
5737          m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
5738        }
5739      }
5740    }
5741  }
5742  else
5743  {
5744    if( bSubdivAndCbf || pcCU->getCbf( uiAbsPartIdx, eType, uiCurrTrMode ) )
5745    {
5746      const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
5747      for( UInt ui = 0; ui < 4; ++ui )
5748      {
5749        xEncodeResidualQT( pcCU, uiAbsPartIdx + ui * uiQPartNumSubdiv, uiDepth + 1, bSubdivAndCbf, eType );
5750      }
5751    }
5752  }
5753}
5754
5755Void TEncSearch::xSetResidualQTData( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcResi, UInt uiDepth, Bool bSpatial )
5756{
5757  assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
5758  const UInt uiCurrTrMode = uiDepth - pcCU->getDepth( 0 );
5759  const UInt uiTrMode = pcCU->getTransformIdx( uiAbsPartIdx );
5760
5761  if( uiCurrTrMode == uiTrMode )
5762  {
5763    const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
5764    const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
5765
5766    Bool  bCodeChroma   = true;
5767    UInt  uiTrModeC     = uiTrMode;
5768    UInt  uiLog2TrSizeC = uiLog2TrSize-1;
5769    if( uiLog2TrSize == 2 )
5770    {
5771      uiLog2TrSizeC++;
5772      uiTrModeC    --;
5773      UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
5774      bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
5775    }
5776
5777    if( bSpatial )
5778    {     
5779      Int trWidth  = 1 << uiLog2TrSize;
5780      Int trHeight = 1 << uiLog2TrSize;
5781      m_pcQTTempTComYuv[uiQTTempAccessLayer].copyPartToPartLuma    ( pcResi, absTUPartIdx, trWidth , trHeight );
5782
5783      if( bCodeChroma )
5784      {
5785        {
5786          m_pcQTTempTComYuv[uiQTTempAccessLayer].copyPartToPartChroma( pcResi, uiAbsPartIdx, 1 << uiLog2TrSizeC, 1 << uiLog2TrSizeC );
5787        }
5788      }
5789    }
5790    else
5791    {
5792      UInt    uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
5793      UInt    uiNumCoeffY = ( 1 << ( uiLog2TrSize << 1 ) );
5794      TCoeff* pcCoeffSrcY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
5795      TCoeff* pcCoeffDstY = pcCU->getCoeffY() + uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
5796      ::memcpy( pcCoeffDstY, pcCoeffSrcY, sizeof( TCoeff ) * uiNumCoeffY );
5797#if ADAPTIVE_QP_SELECTION
5798      Int* pcArlCoeffSrcY = m_ppcQTTempArlCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
5799      Int* pcArlCoeffDstY = pcCU->getArlCoeffY() + uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
5800      ::memcpy( pcArlCoeffDstY, pcArlCoeffSrcY, sizeof( Int ) * uiNumCoeffY );
5801#endif
5802      if( bCodeChroma )
5803      {
5804        UInt    uiNumCoeffC = ( 1 << ( uiLog2TrSizeC << 1 ) );
5805        TCoeff* pcCoeffSrcU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5806        TCoeff* pcCoeffSrcV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5807        TCoeff* pcCoeffDstU = pcCU->getCoeffCb() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5808        TCoeff* pcCoeffDstV = pcCU->getCoeffCr() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5809        ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
5810        ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
5811#if ADAPTIVE_QP_SELECTION
5812        Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5813        Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5814        Int* pcArlCoeffDstU = pcCU->getArlCoeffCb() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5815        Int* pcArlCoeffDstV = pcCU->getArlCoeffCr() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
5816        ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
5817        ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
5818#endif
5819      }
5820    }
5821  }
5822  else
5823  {
5824    const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
5825    for( UInt ui = 0; ui < 4; ++ui )
5826    {
5827      UInt nsAddr = uiAbsPartIdx + ui * uiQPartNumSubdiv;
5828      xSetResidualQTData( pcCU, ui, uiAbsPartIdx + ui * uiQPartNumSubdiv, nsAddr, pcResi, uiDepth + 1, bSpatial );
5829    }
5830  }
5831}
5832
5833UInt TEncSearch::xModeBitsIntra( TComDataCU* pcCU, UInt uiMode, UInt uiPU, UInt uiPartOffset, UInt uiDepth, UInt uiInitTrDepth )
5834{
5835  if( m_bUseSBACRD )
5836  {
5837    // Reload only contexts required for coding intra mode information
5838    m_pcRDGoOnSbacCoder->loadIntraDirModeLuma( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
5839  }
5840 
5841  pcCU->setLumaIntraDirSubParts ( uiMode, uiPartOffset, uiDepth + uiInitTrDepth );
5842 
5843  m_pcEntropyCoder->resetBits();
5844  m_pcEntropyCoder->encodeIntraDirModeLuma ( pcCU, uiPartOffset);
5845 
5846  return m_pcEntropyCoder->getNumberOfWrittenBits();
5847}
5848
5849UInt TEncSearch::xUpdateCandList( UInt uiMode, Double uiCost, UInt uiFastCandNum, UInt * CandModeList, Double * CandCostList )
5850{
5851  UInt i;
5852  UInt shift=0;
5853 
5854  while ( shift<uiFastCandNum && uiCost<CandCostList[ uiFastCandNum-1-shift ] ) shift++;
5855 
5856  if( shift!=0 )
5857  {
5858    for(i=1; i<shift; i++)
5859    {
5860      CandModeList[ uiFastCandNum-i ] = CandModeList[ uiFastCandNum-1-i ];
5861      CandCostList[ uiFastCandNum-i ] = CandCostList[ uiFastCandNum-1-i ];
5862    }
5863    CandModeList[ uiFastCandNum-shift ] = uiMode;
5864    CandCostList[ uiFastCandNum-shift ] = uiCost;
5865    return 1;
5866  }
5867 
5868  return 0;
5869}
5870
5871/** add inter-prediction syntax elements for a CU block
5872 * \param pcCU
5873 * \param uiQp
5874 * \param uiTrMode
5875 * \param ruiBits
5876 * \param rpcYuvRec
5877 * \param pcYuvPred
5878 * \param rpcYuvResi
5879 * \returns Void
5880 */
5881Void  TEncSearch::xAddSymbolBitsInter( TComDataCU* pcCU, UInt uiQp, UInt uiTrMode, UInt& ruiBits, TComYuv*& rpcYuvRec, TComYuv*pcYuvPred, TComYuv*& rpcYuvResi )
5882{
5883  if(pcCU->getMergeFlag( 0 ) && pcCU->getPartitionSize( 0 ) == SIZE_2Nx2N && !pcCU->getQtRootCbf( 0 ))
5884  {
5885    pcCU->setSkipFlagSubParts( true, 0, pcCU->getDepth(0) );
5886
5887    m_pcEntropyCoder->resetBits();
5888    if(pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
5889    {
5890      m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
5891    }
5892    m_pcEntropyCoder->encodeSkipFlag(pcCU, 0, true);
5893    m_pcEntropyCoder->encodeMergeIndex(pcCU, 0, true);
5894    ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
5895  }
5896  else
5897  {
5898    m_pcEntropyCoder->resetBits();
5899    if(pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
5900    {
5901      m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
5902    }
5903    m_pcEntropyCoder->encodeSkipFlag ( pcCU, 0, true );
5904#if INTRA_BL
5905    if(m_pcEncCfg->getLayerId())
5906    {
5907      m_pcEntropyCoder->encodeIntraBLFlag(pcCU, 0, true);
5908#if !NO_RESIDUAL_FLAG_FOR_BLPRED
5909      assert( pcCU->isIntraBL( 0 ) == false );
5910#endif
5911    }
5912#endif
5913#if NO_RESIDUAL_FLAG_FOR_BLPRED
5914    if( !pcCU->isIntraBL(0))
5915    {
5916#endif
5917    m_pcEntropyCoder->encodePredMode( pcCU, 0, true );
5918    m_pcEntropyCoder->encodePartSize( pcCU, 0, pcCU->getDepth(0), true );
5919    m_pcEntropyCoder->encodePredInfo( pcCU, 0, true );
5920#if NO_RESIDUAL_FLAG_FOR_BLPRED
5921    }
5922#endif
5923    Bool bDummy = false;
5924    m_pcEntropyCoder->encodeCoeff   ( pcCU, 0, pcCU->getDepth(0), pcCU->getWidth(0), pcCU->getHeight(0), bDummy );
5925   
5926    ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
5927  }
5928}
5929
5930/**
5931 * \brief Generate half-sample interpolated block
5932 *
5933 * \param pattern Reference picture ROI
5934 * \param biPred    Flag indicating whether block is for biprediction
5935 */
5936Void TEncSearch::xExtDIFUpSamplingH( TComPattern* pattern, Bool biPred )
5937{
5938  Int width      = pattern->getROIYWidth();
5939  Int height     = pattern->getROIYHeight();
5940  Int srcStride  = pattern->getPatternLStride();
5941 
5942  Int intStride = m_filteredBlockTmp[0].getStride();
5943  Int dstStride = m_filteredBlock[0][0].getStride();
5944  Short *intPtr;
5945  Short *dstPtr;
5946  Int filterSize = NTAPS_LUMA;
5947  Int halfFilterSize = (filterSize>>1);
5948  Pel *srcPtr = pattern->getROIY() - halfFilterSize*srcStride - 1;
5949 
5950  m_if.filterHorLuma(srcPtr, srcStride, m_filteredBlockTmp[0].getLumaAddr(), intStride, width+1, height+filterSize, 0, false);
5951  m_if.filterHorLuma(srcPtr, srcStride, m_filteredBlockTmp[2].getLumaAddr(), intStride, width+1, height+filterSize, 2, false);
5952 
5953  intPtr = m_filteredBlockTmp[0].getLumaAddr() + halfFilterSize * intStride + 1; 
5954  dstPtr = m_filteredBlock[0][0].getLumaAddr();
5955  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+0, height+0, 0, false, true);
5956 
5957  intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1; 
5958  dstPtr = m_filteredBlock[2][0].getLumaAddr();
5959  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+0, height+1, 2, false, true);
5960 
5961  intPtr = m_filteredBlockTmp[2].getLumaAddr() + halfFilterSize * intStride;
5962  dstPtr = m_filteredBlock[0][2].getLumaAddr();
5963  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+1, height+0, 0, false, true);
5964 
5965  intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
5966  dstPtr = m_filteredBlock[2][2].getLumaAddr();
5967  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+1, height+1, 2, false, true);
5968}
5969
5970/**
5971 * \brief Generate quarter-sample interpolated blocks
5972 *
5973 * \param pattern    Reference picture ROI
5974 * \param halfPelRef Half-pel mv
5975 * \param biPred     Flag indicating whether block is for biprediction
5976 */
5977Void TEncSearch::xExtDIFUpSamplingQ( TComPattern* pattern, TComMv halfPelRef, Bool biPred )
5978{
5979  Int width      = pattern->getROIYWidth();
5980  Int height     = pattern->getROIYHeight();
5981  Int srcStride  = pattern->getPatternLStride();
5982 
5983  Pel *srcPtr;
5984  Int intStride = m_filteredBlockTmp[0].getStride();
5985  Int dstStride = m_filteredBlock[0][0].getStride();
5986  Short *intPtr;
5987  Short *dstPtr;
5988  Int filterSize = NTAPS_LUMA;
5989 
5990  Int halfFilterSize = (filterSize>>1);
5991
5992  Int extHeight = (halfPelRef.getVer() == 0) ? height + filterSize : height + filterSize-1;
5993 
5994  // Horizontal filter 1/4
5995  srcPtr = pattern->getROIY() - halfFilterSize * srcStride - 1;
5996  intPtr = m_filteredBlockTmp[1].getLumaAddr();
5997  if (halfPelRef.getVer() > 0)
5998  {
5999    srcPtr += srcStride;
6000  }
6001  if (halfPelRef.getHor() >= 0)
6002  {
6003    srcPtr += 1;
6004  }
6005  m_if.filterHorLuma(srcPtr, srcStride, intPtr, intStride, width, extHeight, 1, false);
6006 
6007  // Horizontal filter 3/4
6008  srcPtr = pattern->getROIY() - halfFilterSize*srcStride - 1;
6009  intPtr = m_filteredBlockTmp[3].getLumaAddr();
6010  if (halfPelRef.getVer() > 0)
6011  {
6012    srcPtr += srcStride;
6013  }
6014  if (halfPelRef.getHor() > 0)
6015  {
6016    srcPtr += 1;
6017  }
6018  m_if.filterHorLuma(srcPtr, srcStride, intPtr, intStride, width, extHeight, 3, false);       
6019 
6020  // Generate @ 1,1
6021  intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
6022  dstPtr = m_filteredBlock[1][1].getLumaAddr();
6023  if (halfPelRef.getVer() == 0)
6024  {
6025    intPtr += intStride;
6026  }
6027  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
6028 
6029  // Generate @ 3,1
6030  intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
6031  dstPtr = m_filteredBlock[3][1].getLumaAddr();
6032  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
6033 
6034  if (halfPelRef.getVer() != 0)
6035  {
6036    // Generate @ 2,1
6037    intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
6038    dstPtr = m_filteredBlock[2][1].getLumaAddr();
6039    if (halfPelRef.getVer() == 0)
6040    {
6041      intPtr += intStride;
6042    }
6043    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 2, false, true);
6044   
6045    // Generate @ 2,3
6046    intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
6047    dstPtr = m_filteredBlock[2][3].getLumaAddr();
6048    if (halfPelRef.getVer() == 0)
6049    {
6050      intPtr += intStride;
6051    }
6052    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 2, false, true);
6053  }
6054  else
6055  {
6056    // Generate @ 0,1
6057    intPtr = m_filteredBlockTmp[1].getLumaAddr() + halfFilterSize * intStride;
6058    dstPtr = m_filteredBlock[0][1].getLumaAddr();
6059    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 0, false, true);
6060   
6061    // Generate @ 0,3
6062    intPtr = m_filteredBlockTmp[3].getLumaAddr() + halfFilterSize * intStride;
6063    dstPtr = m_filteredBlock[0][3].getLumaAddr();
6064    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 0, false, true);
6065  }
6066 
6067  if (halfPelRef.getHor() != 0)
6068  {
6069    // Generate @ 1,2
6070    intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
6071    dstPtr = m_filteredBlock[1][2].getLumaAddr();
6072    if (halfPelRef.getHor() > 0)
6073    {
6074      intPtr += 1;
6075    }
6076    if (halfPelRef.getVer() >= 0)
6077    {
6078      intPtr += intStride;
6079    }
6080    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
6081   
6082    // Generate @ 3,2
6083    intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
6084    dstPtr = m_filteredBlock[3][2].getLumaAddr();
6085    if (halfPelRef.getHor() > 0)
6086    {
6087      intPtr += 1;
6088    }
6089    if (halfPelRef.getVer() > 0)
6090    {
6091      intPtr += intStride;
6092    }
6093    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true); 
6094  }
6095  else
6096  {
6097    // Generate @ 1,0
6098    intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1;
6099    dstPtr = m_filteredBlock[1][0].getLumaAddr();
6100    if (halfPelRef.getVer() >= 0)
6101    {
6102      intPtr += intStride;
6103    }
6104    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
6105   
6106    // Generate @ 3,0
6107    intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1;
6108    dstPtr = m_filteredBlock[3][0].getLumaAddr();
6109    if (halfPelRef.getVer() > 0)
6110    {
6111      intPtr += intStride;
6112    }
6113    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
6114  }
6115 
6116  // Generate @ 1,3
6117  intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
6118  dstPtr = m_filteredBlock[1][3].getLumaAddr();
6119  if (halfPelRef.getVer() == 0)
6120  {
6121    intPtr += intStride;
6122  }
6123  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
6124 
6125  // Generate @ 3,3
6126  intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
6127  dstPtr = m_filteredBlock[3][3].getLumaAddr();
6128  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
6129}
6130
6131/** set wp tables
6132 * \param TComDataCU* pcCU
6133 * \param iRefIdx
6134 * \param eRefPicListCur
6135 * \returns Void
6136 */
6137Void  TEncSearch::setWpScalingDistParam( TComDataCU* pcCU, Int iRefIdx, RefPicList eRefPicListCur )
6138{
6139  if ( iRefIdx<0 )
6140  {
6141    m_cDistParam.bApplyWeight = false;
6142    return;
6143  }
6144
6145  TComSlice       *pcSlice  = pcCU->getSlice();
6146  TComPPS         *pps      = pcCU->getSlice()->getPPS();
6147  wpScalingParam  *wp0 , *wp1;
6148  m_cDistParam.bApplyWeight = ( pcSlice->getSliceType()==P_SLICE && pps->getUseWP() ) || ( pcSlice->getSliceType()==B_SLICE && pps->getWPBiPred() ) ;
6149  if ( !m_cDistParam.bApplyWeight ) return;
6150
6151  Int iRefIdx0 = ( eRefPicListCur == REF_PIC_LIST_0 ) ? iRefIdx : (-1);
6152  Int iRefIdx1 = ( eRefPicListCur == REF_PIC_LIST_1 ) ? iRefIdx : (-1);
6153
6154  getWpScaling( pcCU, iRefIdx0, iRefIdx1, wp0 , wp1 );
6155
6156  if ( iRefIdx0 < 0 ) wp0 = NULL;
6157  if ( iRefIdx1 < 0 ) wp1 = NULL;
6158
6159  m_cDistParam.wpCur  = NULL;
6160
6161  if ( eRefPicListCur == REF_PIC_LIST_0 )
6162  {
6163    m_cDistParam.wpCur = wp0;
6164  }
6165  else
6166  {
6167    m_cDistParam.wpCur = wp1;
6168  }
6169}
6170
6171#if REF_IDX_ME_ZEROMV
6172Void TEncSearch::xPatternSearchFracDIFMv0(TComDataCU* pcCU,
6173                                          TComPattern* pcPatternKey,
6174                                          Pel* piRefY,
6175                                          Int iRefStride,
6176                                          TComMv* pcMvInt,
6177                                          TComMv& rcMvHalf,
6178                                          TComMv& rcMvQter,
6179                                          UInt& ruiCost,
6180                                          Bool biPred
6181                                          )
6182{
6183  assert(pcMvInt->getHor() == 0 && pcMvInt->getVer() == 0);
6184  Int         iOffset    = pcMvInt->getHor() + pcMvInt->getVer() * iRefStride;
6185  m_pcRdCost->setDistParam( pcPatternKey, piRefY + iOffset, iRefStride, 1, m_cDistParam, m_pcEncCfg->getUseHADME() );
6186  m_pcRdCost->setCostScale ( 2 );
6187  setDistParamComp(0);
6188  ruiCost = m_cDistParam.DistFunc( &m_cDistParam );  //SATD
6189  ruiCost += m_pcRdCost->getCost( pcMvInt->getHor(), pcMvInt->getVer() );  //SATD rdCost
6190  rcMvHalf.setZero();
6191  rcMvQter.setZero();
6192}
6193#endif
6194
6195#if REF_IDX_ME_AROUND_ZEROMV
6196//ILR integer pixel motion estimation search
6197//pcCU is CU pointer
6198//pcPatterney contains target PU infor
6199//piRefY is the PU at the same position as the source PU, but in the reference pic
6200//iRefStride is the reference stride
6201//rcMv output best integer MV
6202//ruiSAD outputs the SAD of best integer MV
6203Void TEncSearch::xPatternSearchILR( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv& rcMv, UInt& ruiSAD )
6204{
6205  Int      candMvX[5] = {0, -1, 1,  0, 0};
6206  Int      candMvY[5] = {0,  0, 0, -1, 1};
6207  Int      numCand    = 1;
6208  Int      iBestX = 0;
6209  Int      iBestY = 0;
6210  Int      uiSadBest = MAX_UINT;
6211  UInt     uiSad;
6212
6213  Pel*  piRefSrch;
6214
6215  //-- jclee for using the SAD function pointer
6216  m_pcRdCost->setDistParam( pcPatternKey, piRefY, iRefStride,  m_cDistParam );
6217
6218  // fast encoder decision: use subsampled SAD for integer ME
6219  if ( m_pcEncCfg->getUseFastEnc() )
6220  {
6221    if ( m_cDistParam.iRows > 8 )
6222    {
6223      m_cDistParam.iSubShift = 1;
6224    }
6225  }
6226
6227  for(Int candId = 0; candId < numCand; candId++)
6228  {
6229    piRefSrch         = piRefY + (candMvY[candId] * iRefStride) + candMvX[candId];
6230    m_cDistParam.pCur = piRefSrch;
6231
6232    setDistParamComp(0);
6233   
6234    uiSad = m_cDistParam.DistFunc( &m_cDistParam );  //SAD
6235
6236    //motion cost
6237    uiSad += m_pcRdCost->getCost( candMvX[candId], candMvY[candId] );  //SAD rdCost
6238
6239    if(uiSad < uiSadBest)
6240    {
6241      uiSadBest = uiSad;
6242      iBestX    = candMvX[candId];
6243      iBestY    = candMvY[candId];
6244    }
6245  }
6246
6247  rcMv.set( iBestX, iBestY );
6248
6249  ruiSAD = uiSadBest - m_pcRdCost->getCost( iBestX, iBestY );
6250  return;
6251}
6252#endif
6253
6254#if ENCODER_FAST_MODE
6255Bool TEncSearch::predInterSearchILRUni( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv )
6256{
6257  rpcPredYuv->clear();
6258  rpcRecoYuv->clear();
6259
6260  Int           iNumPredDir = pcCU->getSlice()->isInterP() ? 1 : 2;
6261
6262  TComMv        cMv[2];
6263  TComMv        cMvPred[2][33];
6264  TComMv        cMvTemp[2][33];
6265  TComMv        TempMv;
6266
6267  Int           iRefIdx[2]={0,0};
6268
6269  Int           aaiMvpIdx[2][33];
6270  Int           aaiMvpNum[2][33];
6271
6272  UInt          uiMbBits[3] = {1, 1, 0};
6273  UInt          uiLastMode = 0;
6274
6275  UInt          uiCost[2]   = { MAX_UINT, MAX_UINT };     //uni, rdCost
6276  UInt          uiCostTemp;
6277  UInt          biPDistTemp = MAX_INT;
6278  UInt          uiBitsTemp;
6279
6280  PartSize      ePartSize = pcCU->getPartitionSize( 0 );  //2Nx2N
6281  Int           iPartIdx  = 0;                            //one PU in CU
6282  UInt          uiPartAddr;
6283  Int           iRoiWidth, iRoiHeight;
6284
6285  xGetBlkBits( ePartSize, pcCU->getSlice()->isInterP(), iPartIdx, uiLastMode, uiMbBits);
6286  pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
6287
6288  for( Int iRefList = 0; iRefList < iNumPredDir; iRefList++)  //list
6289  {
6290    RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
6291
6292    Int  iRefIdxTemp = -1;
6293    Bool foundILR    = false;
6294    for( Int refIdx = 0; refIdx < pcCU->getSlice()->getNumRefIdx(eRefPicList); refIdx++ )
6295      if( pcCU->getSlice()->getRefPic(eRefPicList, refIdx)->getIsILR() )
6296      {
6297        iRefIdxTemp = refIdx;
6298        foundILR    = true;
6299        break;
6300      }
6301
6302    if(!foundILR)  //no ILR in eRefPiclist
6303      continue; 
6304
6305    uiBitsTemp = uiMbBits[iRefList];
6306    if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 ) 
6307    { 
6308      uiBitsTemp += iRefIdxTemp+1; 
6309      if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--; 
6310    }
6311
6312    xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp);
6313    aaiMvpIdx[iRefList][iRefIdxTemp] = pcCU->getMVPIdx(eRefPicList, uiPartAddr);
6314    aaiMvpNum[iRefList][iRefIdxTemp] = pcCU->getMVPNum(eRefPicList, uiPartAddr);
6315
6316    uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
6317
6318    xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
6319    xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
6320
6321    if( uiCostTemp < uiCost[iRefList] )
6322    {
6323      uiCost[iRefList] = uiCostTemp;
6324
6325      cMv[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
6326      iRefIdx[iRefList] = iRefIdxTemp;
6327
6328      pcCU->getCUMvField(eRefPicList)->setAllMv( cMv[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
6329      pcCU->getCUMvField(eRefPicList)->setAllRefIdx( iRefIdx[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
6330    }
6331  }
6332
6333  if( uiCost[0] == MAX_UINT && uiCost[1] == MAX_UINT )  //no ILR in both list0 and list1
6334    return false;
6335
6336  //  Clear Motion Field
6337  pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx ); 
6338  pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx ); 
6339  pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TComMv(),      ePartSize, uiPartAddr, 0, iPartIdx ); 
6340  pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TComMv(),      ePartSize, uiPartAddr, 0, iPartIdx );
6341
6342  pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr)); 
6343  pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr)); 
6344  pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr)); 
6345  pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
6346
6347  if( uiCost[0] <= uiCost[1] )  //list0 ILR
6348  {
6349    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv    ( cMv[0], ePartSize, uiPartAddr, 0, iPartIdx );
6350    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdx[0], ePartSize, uiPartAddr, 0, iPartIdx );
6351
6352    TempMv = cMv[0] - cMvPred[0][iRefIdx[0]]; 
6353    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd( TempMv, ePartSize, uiPartAddr, 0, iPartIdx );
6354
6355    pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
6356
6357    pcCU->setMVPIdxSubParts( aaiMvpIdx[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr)); 
6358    pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
6359  }
6360  else if( uiCost[1] < uiCost[0] )  //list1 ILR
6361  {
6362    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv    ( cMv[1], ePartSize, uiPartAddr, 0, iPartIdx );
6363    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdx[1], ePartSize, uiPartAddr, 0, iPartIdx );
6364
6365    TempMv = cMv[1] - cMvPred[1][iRefIdx[1]]; 
6366    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd( TempMv, ePartSize, uiPartAddr, 0, iPartIdx );
6367
6368    pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
6369
6370    pcCU->setMVPIdxSubParts( aaiMvpIdx[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr)); 
6371    pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
6372  }
6373  else
6374    assert(0);
6375
6376  pcCU->setMergeFlagSubParts( false, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
6377
6378  motionCompensation ( pcCU, rpcPredYuv, REF_PIC_LIST_X, iPartIdx );
6379
6380  setWpScalingDistParam( pcCU, -1, REF_PIC_LIST_X );
6381
6382  return true;
6383}
6384
6385
6386#endif
6387
6388#if INTRA_BL
6389Void
6390TEncSearch::estIntraBLPredQT( TComDataCU* pcCU, 
6391                           TComYuv*    pcOrgYuv, 
6392                           TComYuv*    pcPredYuv, 
6393                           TComYuv*    pcResiYuv,
6394                           TComYuv*    pcRecoYuv )
6395{
6396  UInt    uiDepth        = pcCU->getDepth(0);
6397  UInt    uiOverallDistY = 0;
6398  UInt    uiOverallDistC = 0;
6399 
6400  //===== set QP and clear Cbf =====
6401  if ( pcCU->getSlice()->getPPS()->getUseDQP() == true)
6402  {
6403    pcCU->setQPSubParts( pcCU->getQP(0), 0, uiDepth );
6404  }
6405  else
6406  {
6407    pcCU->setQPSubParts( pcCU->getSlice()->getSliceQp(), 0, uiDepth );
6408  }
6409 
6410  //===== init pattern for luma prediction =====
6411  Bool bAboveAvail = false;
6412  Bool bLeftAvail  = false;
6413  pcCU->getPattern()->initPattern   ( pcCU, 0, 0 );
6414  pcCU->getPattern()->initAdiPattern( pcCU, 0, 0, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
6415 
6416  pcCU->setLumaIntraDirSubParts ( DC_IDX, 0, uiDepth );
6417 
6418  // set context models
6419  if( m_bUseSBACRD )
6420  {
6421    m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
6422  }
6423 
6424  // determine residual for partition
6425  Double dPUCost   = 0.0;
6426  xRecurIntraCodingQT( pcCU, 0, 0, false, pcOrgYuv, pcPredYuv, pcResiYuv, uiOverallDistY, uiOverallDistC, false, dPUCost );
6427  xSetIntraResultQT( pcCU, 0, 0, false, pcRecoYuv );
6428 
6429  //=== update PU data ====
6430  pcCU->copyToPic( uiDepth, 0, 0 );
6431   
6432  //===== reset context models =====
6433  if(m_bUseSBACRD)
6434  {
6435    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
6436  }
6437
6438  //===== set distortion (rate and r-d costs are determined later) =====
6439  pcCU->getTotalDistortion() = uiOverallDistY + uiOverallDistC;
6440}
6441
6442#endif
6443//! \}
Note: See TracBrowser for help on using the repository browser.