source: SHVCSoftware/branches/SHM-3.1-dev/source/Lib/TLibEncoder/TEncSearch.cpp @ 398

Last change on this file since 398 was 389, checked in by qualcomm, 12 years ago

Signaling representation format in VPS (MACRO: REPN_FORMAT_IN_VPS)

Includes signaling of representation format - including picture resolution, bit depth, chroma format - in the VPS, with the option of updating them in the SPS. The configuration file has "RepFormatIdx%d" added to indicate for each layer which representation format is used. The rep_format() structures are automatically created by the encoder. If the bit depth and the chroma format are also changed across layers, some more configuration support would be needed.

From: Adarsh K. Ramasubramonian <aramasub@…>

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