source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComPrediction.cpp @ 622

Last change on this file since 622 was 622, checked in by tech, 11 years ago

Merged 8.0-dev0@621 (MV-HEVC 5 HLS).

  • Property svn:eol-style set to native
File size: 105.2 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     TComPrediction.cpp
35    \brief    prediction class
36*/
37
38#include <memory.h>
39#include "TComPrediction.h"
40
41#if SHARP_ILLUCOMP_REFINE_E0046
42#define IC_REG_COST_SHIFT 7
43#define IC_CONST_SHIFT 5
44#define IC_SHIFT_DIFF 12
45#endif
46
47//! \ingroup TLibCommon
48//! \{
49
50// ====================================================================================================================
51// Constructor / destructor / initialize
52// ====================================================================================================================
53
54TComPrediction::TComPrediction()
55: m_pLumaRecBuffer(0)
56, m_iLumaRecStride(0)
57{
58  m_piYuvExt = NULL;
59#if H_3D_VSP
60  m_pDepthBlock = (Int*) malloc(MAX_NUM_SPU_W*MAX_NUM_SPU_W*sizeof(Int));
61  if (m_pDepthBlock == NULL)
62      printf("ERROR: UKTGHU, No memory allocated.\n");
63#endif
64}
65
66TComPrediction::~TComPrediction()
67{
68#if H_3D_VSP
69  if (m_pDepthBlock != NULL)
70      free(m_pDepthBlock);
71#if NTT_VSP_COMMON_E0207_E0208
72  m_cYuvDepthOnVsp.destroy();
73#endif
74#endif
75
76  delete[] m_piYuvExt;
77
78  m_acYuvPred[0].destroy();
79  m_acYuvPred[1].destroy();
80
81  m_cYuvPredTemp.destroy();
82
83#if H_3D_ARP
84  m_acYuvPredBase[0].destroy();
85  m_acYuvPredBase[1].destroy();
86#endif
87  if( m_pLumaRecBuffer )
88  {
89    delete [] m_pLumaRecBuffer;
90  }
91 
92  Int i, j;
93  for (i = 0; i < 4; i++)
94  {
95    for (j = 0; j < 4; j++)
96    {
97      m_filteredBlock[i][j].destroy();
98    }
99    m_filteredBlockTmp[i].destroy();
100  }
101}
102
103Void TComPrediction::initTempBuff()
104{
105  if( m_piYuvExt == NULL )
106  {
107    Int extWidth  = MAX_CU_SIZE + 16; 
108    Int extHeight = MAX_CU_SIZE + 1;
109    Int i, j;
110    for (i = 0; i < 4; i++)
111    {
112      m_filteredBlockTmp[i].create(extWidth, extHeight + 7);
113      for (j = 0; j < 4; j++)
114      {
115        m_filteredBlock[i][j].create(extWidth, extHeight);
116      }
117    }
118    m_iYuvExtHeight  = ((MAX_CU_SIZE + 2) << 4);
119    m_iYuvExtStride = ((MAX_CU_SIZE  + 8) << 4);
120    m_piYuvExt = new Int[ m_iYuvExtStride * m_iYuvExtHeight ];
121
122    // new structure
123    m_acYuvPred[0] .create( MAX_CU_SIZE, MAX_CU_SIZE );
124    m_acYuvPred[1] .create( MAX_CU_SIZE, MAX_CU_SIZE );
125
126    m_cYuvPredTemp.create( MAX_CU_SIZE, MAX_CU_SIZE );
127#if H_3D_ARP
128    m_acYuvPredBase[0] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
129    m_acYuvPredBase[1] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
130#endif
131#if NTT_VSP_COMMON_E0207_E0208
132    m_cYuvDepthOnVsp.create( g_uiMaxCUWidth, g_uiMaxCUHeight );
133#endif
134  }
135
136  if (m_iLumaRecStride != (MAX_CU_SIZE>>1) + 1)
137  {
138    m_iLumaRecStride =  (MAX_CU_SIZE>>1) + 1;
139    if (!m_pLumaRecBuffer)
140    {
141      m_pLumaRecBuffer = new Pel[ m_iLumaRecStride * m_iLumaRecStride ];
142    }
143  }
144#if H_3D_IC
145#if SHARP_ILLUCOMP_REFINE_E0046
146  m_uiaShift[0] = 0;
147  for( Int i = 1; i < 64; i++ )
148  {
149    m_uiaShift[i] = ( (1 << 15) + i/2 ) / i;
150  }
151#else
152  for( Int i = 1; i < 64; i++ )
153  {
154    m_uiaShift[i-1] = ( (1 << 15) + i/2 ) / i;
155  }
156#endif
157#endif
158}
159
160// ====================================================================================================================
161// Public member functions
162// ====================================================================================================================
163
164// Function for calculating DC value of the reference samples used in Intra prediction
165Pel TComPrediction::predIntraGetPredValDC( Int* pSrc, Int iSrcStride, UInt iWidth, UInt iHeight, Bool bAbove, Bool bLeft )
166{
167  assert(iWidth > 0 && iHeight > 0);
168  Int iInd, iSum = 0;
169  Pel pDcVal;
170
171  if (bAbove)
172  {
173    for (iInd = 0;iInd < iWidth;iInd++)
174    {
175      iSum += pSrc[iInd-iSrcStride];
176    }
177  }
178  if (bLeft)
179  {
180    for (iInd = 0;iInd < iHeight;iInd++)
181    {
182      iSum += pSrc[iInd*iSrcStride-1];
183    }
184  }
185
186  if (bAbove && bLeft)
187  {
188    pDcVal = (iSum + iWidth) / (iWidth + iHeight);
189  }
190  else if (bAbove)
191  {
192    pDcVal = (iSum + iWidth/2) / iWidth;
193  }
194  else if (bLeft)
195  {
196    pDcVal = (iSum + iHeight/2) / iHeight;
197  }
198  else
199  {
200    pDcVal = pSrc[-1]; // Default DC value already calculated and placed in the prediction array if no neighbors are available
201  }
202 
203  return pDcVal;
204}
205
206// Function for deriving the angular Intra predictions
207
208/** Function for deriving the simplified angular intra predictions.
209 * \param pSrc pointer to reconstructed sample array
210 * \param srcStride the stride of the reconstructed sample array
211 * \param rpDst reference to pointer for the prediction sample array
212 * \param dstStride the stride of the prediction sample array
213 * \param width the width of the block
214 * \param height the height of the block
215 * \param dirMode the intra prediction mode index
216 * \param blkAboveAvailable boolean indication if the block above is available
217 * \param blkLeftAvailable boolean indication if the block to the left is available
218 *
219 * This function derives the prediction samples for the angular mode based on the prediction direction indicated by
220 * the prediction mode index. The prediction direction is given by the displacement of the bottom row of the block and
221 * the reference row above the block in the case of vertical prediction or displacement of the rightmost column
222 * of the block and reference column left from the block in the case of the horizontal prediction. The displacement
223 * is signalled at 1/32 pixel accuracy. When projection of the predicted pixel falls inbetween reference samples,
224 * the predicted value for the pixel is linearly interpolated from the reference samples. All reference samples are taken
225 * from the extended main reference.
226 */
227Void TComPrediction::xPredIntraAng(Int bitDepth, Int* pSrc, Int srcStride, Pel*& rpDst, Int dstStride, UInt width, UInt height, UInt dirMode, Bool blkAboveAvailable, Bool blkLeftAvailable, Bool bFilter )
228{
229  Int k,l;
230  Int blkSize        = width;
231  Pel* pDst          = rpDst;
232
233  // Map the mode index to main prediction direction and angle
234  assert( dirMode > 0 ); //no planar
235  Bool modeDC        = dirMode < 2;
236  Bool modeHor       = !modeDC && (dirMode < 18);
237  Bool modeVer       = !modeDC && !modeHor;
238  Int intraPredAngle = modeVer ? (Int)dirMode - VER_IDX : modeHor ? -((Int)dirMode - HOR_IDX) : 0;
239  Int absAng         = abs(intraPredAngle);
240  Int signAng        = intraPredAngle < 0 ? -1 : 1;
241
242  // Set bitshifts and scale the angle parameter to block size
243  Int angTable[9]    = {0,    2,    5,   9,  13,  17,  21,  26,  32};
244  Int invAngTable[9] = {0, 4096, 1638, 910, 630, 482, 390, 315, 256}; // (256 * 32) / Angle
245  Int invAngle       = invAngTable[absAng];
246  absAng             = angTable[absAng];
247  intraPredAngle     = signAng * absAng;
248
249  // Do the DC prediction
250  if (modeDC)
251  {
252    Pel dcval = predIntraGetPredValDC(pSrc, srcStride, width, height, blkAboveAvailable, blkLeftAvailable);
253
254    for (k=0;k<blkSize;k++)
255    {
256      for (l=0;l<blkSize;l++)
257      {
258        pDst[k*dstStride+l] = dcval;
259      }
260    }
261  }
262
263  // Do angular predictions
264  else
265  {
266    Pel* refMain;
267    Pel* refSide;
268    Pel  refAbove[2*MAX_CU_SIZE+1];
269    Pel  refLeft[2*MAX_CU_SIZE+1];
270
271    // Initialise the Main and Left reference array.
272    if (intraPredAngle < 0)
273    {
274      for (k=0;k<blkSize+1;k++)
275      {
276        refAbove[k+blkSize-1] = pSrc[k-srcStride-1];
277      }
278      for (k=0;k<blkSize+1;k++)
279      {
280        refLeft[k+blkSize-1] = pSrc[(k-1)*srcStride-1];
281      }
282      refMain = (modeVer ? refAbove : refLeft) + (blkSize-1);
283      refSide = (modeVer ? refLeft : refAbove) + (blkSize-1);
284
285      // Extend the Main reference to the left.
286      Int invAngleSum    = 128;       // rounding for (shift by 8)
287      for (k=-1; k>blkSize*intraPredAngle>>5; k--)
288      {
289        invAngleSum += invAngle;
290        refMain[k] = refSide[invAngleSum>>8];
291      }
292    }
293    else
294    {
295      for (k=0;k<2*blkSize+1;k++)
296      {
297        refAbove[k] = pSrc[k-srcStride-1];
298      }
299      for (k=0;k<2*blkSize+1;k++)
300      {
301        refLeft[k] = pSrc[(k-1)*srcStride-1];
302      }
303      refMain = modeVer ? refAbove : refLeft;
304      refSide = modeVer ? refLeft  : refAbove;
305    }
306
307    if (intraPredAngle == 0)
308    {
309      for (k=0;k<blkSize;k++)
310      {
311        for (l=0;l<blkSize;l++)
312        {
313          pDst[k*dstStride+l] = refMain[l+1];
314        }
315      }
316
317      if ( bFilter )
318      {
319        for (k=0;k<blkSize;k++)
320        {
321          pDst[k*dstStride] = Clip3(0, (1<<bitDepth)-1, pDst[k*dstStride] + (( refSide[k+1] - refSide[0] ) >> 1) );
322        }
323      }
324    }
325    else
326    {
327      Int deltaPos=0;
328      Int deltaInt;
329      Int deltaFract;
330      Int refMainIndex;
331
332      for (k=0;k<blkSize;k++)
333      {
334        deltaPos += intraPredAngle;
335        deltaInt   = deltaPos >> 5;
336        deltaFract = deltaPos & (32 - 1);
337
338        if (deltaFract)
339        {
340          // Do linear filtering
341          for (l=0;l<blkSize;l++)
342          {
343            refMainIndex        = l+deltaInt+1;
344            pDst[k*dstStride+l] = (Pel) ( ((32-deltaFract)*refMain[refMainIndex]+deltaFract*refMain[refMainIndex+1]+16) >> 5 );
345          }
346        }
347        else
348        {
349          // Just copy the integer samples
350          for (l=0;l<blkSize;l++)
351          {
352            pDst[k*dstStride+l] = refMain[l+deltaInt+1];
353          }
354        }
355      }
356    }
357
358    // Flip the block if this is the horizontal mode
359    if (modeHor)
360    {
361      Pel  tmp;
362      for (k=0;k<blkSize-1;k++)
363      {
364        for (l=k+1;l<blkSize;l++)
365        {
366          tmp                 = pDst[k*dstStride+l];
367          pDst[k*dstStride+l] = pDst[l*dstStride+k];
368          pDst[l*dstStride+k] = tmp;
369        }
370      }
371    }
372  }
373}
374
375Void TComPrediction::predIntraLumaAng(TComPattern* pcTComPattern, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft )
376{
377  Pel *pDst = piPred;
378  Int *ptrSrc;
379
380  assert( g_aucConvertToBit[ iWidth ] >= 0 ); //   4x  4
381  assert( g_aucConvertToBit[ iWidth ] <= 5 ); // 128x128
382  assert( iWidth == iHeight  );
383
384  ptrSrc = pcTComPattern->getPredictorPtr( uiDirMode, g_aucConvertToBit[ iWidth ] + 2, m_piYuvExt );
385
386  // get starting pixel in block
387  Int sw = 2 * iWidth + 1;
388
389  // Create the prediction
390  if ( uiDirMode == PLANAR_IDX )
391  {
392    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
393  }
394  else
395  {
396    if ( (iWidth > 16) || (iHeight > 16) )
397    {
398      xPredIntraAng(g_bitDepthY, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, false );
399    }
400    else
401    {
402      xPredIntraAng(g_bitDepthY, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, true );
403
404      if( (uiDirMode == DC_IDX ) && bAbove && bLeft )
405      {
406        xDCPredFiltering( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight);
407      }
408    }
409  }
410}
411
412// Angular chroma
413Void TComPrediction::predIntraChromaAng( Int* piSrc, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft )
414{
415  Pel *pDst = piPred;
416  Int *ptrSrc = piSrc;
417
418  // get starting pixel in block
419  Int sw = 2 * iWidth + 1;
420
421  if ( uiDirMode == PLANAR_IDX )
422  {
423    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
424  }
425  else
426  {
427    // Create the prediction
428    xPredIntraAng(g_bitDepthC, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, false );
429  }
430}
431
432#if H_3D_DIM
433Void TComPrediction::predIntraLumaDepth( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiIntraMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bFastEnc )
434{
435  assert( iWidth == iHeight  );
436  assert( iWidth >= DIM_MIN_SIZE && iWidth <= DIM_MAX_SIZE );
437  assert( isDimMode( uiIntraMode ) );
438
439  UInt dimType    = getDimType  ( uiIntraMode );
440  Bool dimDeltaDC = isDimDeltaDC( uiIntraMode );   
441  Bool isDmmMode  = (dimType <  DMM_NUM_TYPE);
442  Bool isRbcMode  = (dimType == RBC_IDX);
443
444  Bool* biSegPattern  = NULL;
445  UInt  patternStride = 0;
446
447  // get partiton
448#if H_3D_DIM_DMM
449  TComWedgelet* dmmSegmentation = NULL;
450  if( isDmmMode )
451  {
452    switch( dimType )
453    {
454    case( DMM1_IDX ): 
455      {
456        dmmSegmentation = &(g_dmmWedgeLists[ g_aucConvertToBit[iWidth] ][ pcCU->getDmmWedgeTabIdx( dimType, uiAbsPartIdx ) ]);
457      } break;
458#if !SEC_DMM2_E0146_HHIFIX
459    case( DMM2_IDX ):
460      {
461        UInt uiTabIdx = 0;
462        if( bFastEnc ) { uiTabIdx = pcCU->getDmmWedgeTabIdx( dimType, uiAbsPartIdx ); }
463        else
464        {
465          uiTabIdx = xPredWedgeFromIntra( pcCU, uiAbsPartIdx, iWidth, iHeight, pcCU->getDmm2DeltaEnd( uiAbsPartIdx ) );
466          pcCU->setDmmWedgeTabIdxSubParts( uiTabIdx, dimType, uiAbsPartIdx, (pcCU->getDepth(0) + (pcCU->getPartitionSize(0) == SIZE_2Nx2N ? 0 : 1)) );
467        }
468        dmmSegmentation = &(g_dmmWedgeLists[ g_aucConvertToBit[iWidth] ][ uiTabIdx ]);
469      } break;
470#endif
471    case( DMM3_IDX ): 
472      {
473        UInt uiTabIdx = 0;
474        if( bFastEnc ) { uiTabIdx = pcCU->getDmmWedgeTabIdx( dimType, uiAbsPartIdx ); }
475        else
476        {
477          uiTabIdx = xPredWedgeFromTex( pcCU, uiAbsPartIdx, iWidth, iHeight, pcCU->getDmm3IntraTabIdx( uiAbsPartIdx ) );
478          pcCU->setDmmWedgeTabIdxSubParts( uiTabIdx, dimType, uiAbsPartIdx, (pcCU->getDepth(0) + (pcCU->getPartitionSize(0) == SIZE_2Nx2N ? 0 : 1)) );
479        }
480        dmmSegmentation = &(g_dmmWedgeLists[ g_aucConvertToBit[iWidth] ][ uiTabIdx ]);
481      } break;
482    case( DMM4_IDX ): 
483      {
484        dmmSegmentation = new TComWedgelet( iWidth, iHeight );
485        xPredContourFromTex( pcCU, uiAbsPartIdx, iWidth, iHeight, dmmSegmentation );
486      } break;
487    default: assert(0);
488    }
489    assert( dmmSegmentation );
490    biSegPattern  = dmmSegmentation->getPattern();
491    patternStride = dmmSegmentation->getStride ();
492  }
493#endif
494#if H_3D_DIM_RBC
495  if( isRbcMode )
496  {
497    biSegPattern  = pcCU->getEdgePartition( uiAbsPartIdx );
498    patternStride = iWidth;
499  }
500#endif
501
502  // get predicted partition values
503  assert( biSegPattern );
504  Int* piMask = NULL;
505  if( isDmmMode ) piMask = pcCU->getPattern()->getAdiOrgBuf( iWidth, iHeight, m_piYuvExt ); // no filtering for DMM
506  else            piMask = pcCU->getPattern()->getPredictorPtr( 0, g_aucConvertToBit[ iWidth ] + 2, m_piYuvExt );
507  assert( piMask );
508  Int maskStride = 2*iWidth + 1; 
509  Int* ptrSrc = piMask+maskStride+1;
510  Pel predDC1 = 0; Pel predDC2 = 0;
511  xPredBiSegDCs( ptrSrc, maskStride, biSegPattern, patternStride, predDC1, predDC2 );
512
513  // set segment values with deltaDC offsets
514  Pel segDC1 = 0;
515  Pel segDC2 = 0;
516  if( dimDeltaDC )
517  {
518    Pel deltaDC1 = pcCU->getDimDeltaDC( dimType, 0, uiAbsPartIdx );
519    Pel deltaDC2 = pcCU->getDimDeltaDC( dimType, 1, uiAbsPartIdx );
520#if H_3D_DIM_DMM
521    if( isDmmMode )
522    {
523#if H_3D_DIM_DLT
524      segDC1 = pcCU->getSlice()->getVPS()->idx2DepthValue( pcCU->getSlice()->getLayerIdInVps(), pcCU->getSlice()->getVPS()->depthValue2idx( pcCU->getSlice()->getLayerIdInVps(), predDC1 ) + deltaDC1 );
525      segDC2 = pcCU->getSlice()->getVPS()->idx2DepthValue( pcCU->getSlice()->getLayerIdInVps(), pcCU->getSlice()->getVPS()->depthValue2idx( pcCU->getSlice()->getLayerIdInVps(), predDC2 ) + deltaDC2 );
526#else
527      segDC1 = ClipY( predDC1 + deltaDC1 );
528      segDC2 = ClipY( predDC2 + deltaDC2 );
529#endif
530    }
531#endif
532#if H_3D_DIM_RBC
533    if( isRbcMode )
534    {
535      xDeltaDCQuantScaleUp( pcCU, deltaDC1 );
536      xDeltaDCQuantScaleUp( pcCU, deltaDC2 );
537      segDC1 = ClipY( predDC1 + deltaDC1 );
538      segDC2 = ClipY( predDC2 + deltaDC2 );
539    }
540#endif
541  }
542  else
543  {
544    segDC1 = predDC1;
545    segDC2 = predDC2;
546  }
547
548  // set prediction signal
549  Pel* pDst = piPred;
550  xAssignBiSegDCs( pDst, uiStride, biSegPattern, patternStride, segDC1, segDC2 );
551
552#if H_3D_DIM_DMM
553  if( dimType == DMM4_IDX ) { dmmSegmentation->destroy(); delete dmmSegmentation; }
554#endif
555}
556#endif
557
558/** Function for checking identical motion.
559 * \param TComDataCU* pcCU
560 * \param UInt PartAddr
561 */
562Bool TComPrediction::xCheckIdenticalMotion ( TComDataCU* pcCU, UInt PartAddr )
563{
564  if( pcCU->getSlice()->isInterB() && !pcCU->getSlice()->getPPS()->getWPBiPred() )
565  {
566    if( pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr) >= 0 && pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr) >= 0)
567    {
568      Int RefPOCL0 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_0, pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr))->getPOC();
569      Int RefPOCL1 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_1, pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr))->getPOC();
570      if(RefPOCL0 == RefPOCL1 && pcCU->getCUMvField(REF_PIC_LIST_0)->getMv(PartAddr) == pcCU->getCUMvField(REF_PIC_LIST_1)->getMv(PartAddr))
571      {
572        return true;
573      }
574    }
575  }
576  return false;
577}
578
579
580Void TComPrediction::motionCompensation ( TComDataCU* pcCU, TComYuv* pcYuvPred, RefPicList eRefPicList, Int iPartIdx )
581{
582  Int         iWidth;
583  Int         iHeight;
584  UInt        uiPartAddr;
585
586  if ( iPartIdx >= 0 )
587  {
588    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
589#if H_3D_VSP
590    if ( pcCU->getVSPFlag(uiPartAddr) == 0)
591    {
592#endif
593      if ( eRefPicList != REF_PIC_LIST_X )
594      {
595        if( pcCU->getSlice()->getPPS()->getUseWP())
596        {
597          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
598        }
599        else
600        {
601          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
602        }
603        if ( pcCU->getSlice()->getPPS()->getUseWP() )
604        {
605          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
606        }
607      }
608      else
609      {
610        if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
611        {
612          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
613        }
614        else
615        {
616          xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
617        }
618      }
619#if H_3D_VSP
620    }
621    else
622    {
623      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
624      {
625        xPredInterUniVSP( pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
626      }
627      else
628      {
629        xPredInterBiVSP ( pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
630      }
631    }
632#endif
633    return;
634  }
635
636  for ( iPartIdx = 0; iPartIdx < pcCU->getNumPartInter(); iPartIdx++ )
637  {
638    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
639
640#if H_3D_VSP
641    if ( pcCU->getVSPFlag(uiPartAddr) == 0 )
642    {
643#endif
644      if ( eRefPicList != REF_PIC_LIST_X )
645      {
646        if( pcCU->getSlice()->getPPS()->getUseWP())
647        {
648          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
649        }
650        else
651        {
652          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
653        }
654        if ( pcCU->getSlice()->getPPS()->getUseWP() )
655        {
656          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
657        }
658      }
659      else
660      {
661        if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
662        {
663          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
664        }
665        else
666        {
667          xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
668        }
669      }
670#if H_3D_VSP
671    }
672    else
673    {
674      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
675      {
676        xPredInterUniVSP( pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
677      }
678      else
679      {
680        xPredInterBiVSP ( pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
681      }
682    }
683#endif
684  }
685  return;
686}
687
688Void TComPrediction::xPredInterUni ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
689{
690  Int         iRefIdx     = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           assert (iRefIdx >= 0);
691  TComMv      cMv         = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
692  pcCU->clipMv(cMv);
693#if H_3D_ARP
694  if(  pcCU->getARPW( uiPartAddr ) > 0 
695    && pcCU->getPartitionSize(uiPartAddr)==SIZE_2Nx2N
696    && pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPOC()!= pcCU->getSlice()->getPOC() 
697    )
698  {
699    xPredInterUniARP( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, rpcYuvPred, bi );
700  }
701  else
702  {
703#endif
704#if H_3D_IC
705    Bool bICFlag = pcCU->getICFlag( uiPartAddr ) && ( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getViewIndex() != pcCU->getSlice()->getViewIndex() );
706    xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
707#if H_3D_ARP
708      , false
709#endif
710      , bICFlag );
711#if SHARP_ILLUCOMP_REFINE_E0046
712    bICFlag = bICFlag && (iWidth > 8);
713#endif
714    xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
715#if H_3D_ARP
716      , false
717#endif
718      , bICFlag );
719#else
720  xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
721  xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
722#endif
723#if H_3D_ARP
724  }
725#endif
726}
727
728#if H_3D_VSP
729Void TComPrediction::xPredInterUniVSP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
730{
731  // Get depth reference
732  Int       depthRefViewIdx = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan;
733  TComPic* pRefPicBaseDepth = pcCU->getSlice()->getIvPic (true, depthRefViewIdx );
734  assert(pRefPicBaseDepth != NULL);
735  TComPicYuv* pcBaseViewDepthPicYuv = pRefPicBaseDepth->getPicYuvRec();
736  assert(pcBaseViewDepthPicYuv != NULL);
737
738  // Get texture reference
739  Int iRefIdx = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
740  assert(iRefIdx >= 0);
741  TComPic* pRefPicBaseTxt = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx );
742  TComPicYuv* pcBaseViewTxtPicYuv = pRefPicBaseTxt->getPicYuvRec();
743  assert(pcBaseViewTxtPicYuv != NULL);
744
745  // Initialize LUT according to the reference viewIdx
746  Int txtRefViewIdx = pRefPicBaseTxt->getViewIndex();
747  Int* pShiftLUT    = pcCU->getSlice()->getDepthToDisparityB( txtRefViewIdx );
748  assert( txtRefViewIdx < pcCU->getSlice()->getViewIndex() );
749
750  // Do compensation
751  TComMv cDv  = pcCU->getDvInfo(uiPartAddr).m_acNBDV;
752  pcCU->clipMv(cDv);
753
754#if NTT_VSP_COMMON_E0207_E0208
755  // fetch virtual depth map
756#if NTT_VSP_VECTOR_CLIP_E0208
757  pcBaseViewDepthPicYuv->extendPicBorder();
758#endif
759  xGetVirtualDepth( pcCU, pcBaseViewDepthPicYuv, &cDv, uiPartAddr, iWidth, iHeight, &m_cYuvDepthOnVsp );
760  // sub-PU based compensation
761  xPredInterLumaBlkFromDM   ( pcCU, pcBaseViewTxtPicYuv, &m_cYuvDepthOnVsp, pShiftLUT, &cDv, uiPartAddr, iWidth, iHeight, pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
762  xPredInterChromaBlkFromDM ( pcCU, pcBaseViewTxtPicYuv, &m_cYuvDepthOnVsp, pShiftLUT, &cDv, uiPartAddr, iWidth, iHeight, pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
763#else
764  UInt uiAbsPartIdx = pcCU->getZorderIdxInCU();
765  Int iBlkX = ( pcCU->getAddr() % pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
766  Int iBlkY = ( pcCU->getAddr() / pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
767  xPredInterLumaBlkFromDM  ( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, &cDv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,    pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
768  xPredInterChromaBlkFromDM( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, &cDv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1, pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
769#endif
770
771}
772#endif
773
774#if H_3D_ARP
775Void TComPrediction::xPredInterUniARP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi, TComMvField * pNewMvFiled )
776{
777  Int         iRefIdx      = pNewMvFiled ? pNewMvFiled->getRefIdx() : pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           
778  TComMv      cMv          = pNewMvFiled ? pNewMvFiled->getMv()     : pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
779  Bool        bTobeScaled  = false;
780  TComPic* pcPicYuvBaseCol = NULL;
781  TComPic* pcPicYuvBaseRef = NULL;
782
783#if H_3D_NBDV
784  DisInfo cDistparity;
785  cDistparity.bDV           = pcCU->getDvInfo(uiPartAddr).bDV;
786  if( cDistparity.bDV )
787  {
788    cDistparity.m_acNBDV = pcCU->getDvInfo(0).m_acNBDV;
789    assert(pcCU->getDvInfo(uiPartAddr).bDV ==  pcCU->getDvInfo(0).bDV);
790    cDistparity.m_aVIdxCan = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan;
791  }
792#else
793  assert(0); // ARP can be applied only when a DV is available
794#endif
795
796  UChar dW = cDistparity.bDV ? pcCU->getARPW ( uiPartAddr ) : 0;
797
798  if( cDistparity.bDV ) 
799  {
800    if( dW > 0 && pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC()!= pcCU->getSlice()->getPOC() )
801    {
802      bTobeScaled = true;
803    }
804
805    pcPicYuvBaseCol =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getPOC(),                              cDistparity.m_aVIdxCan );
806    pcPicYuvBaseRef =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC(), cDistparity.m_aVIdxCan );
807   
808    if( ( !pcPicYuvBaseCol || pcPicYuvBaseCol->getPOC() != pcCU->getSlice()->getPOC() ) || ( !pcPicYuvBaseRef || pcPicYuvBaseRef->getPOC() != pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() ) )
809    {
810      dW = 0;
811      bTobeScaled = false;
812    }
813    else
814    {
815      assert( pcPicYuvBaseCol->getPOC() == pcCU->getSlice()->getPOC() && pcPicYuvBaseRef->getPOC() == pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() );
816    }
817
818    if(bTobeScaled)
819    {     
820      Int iCurrPOC    = pcCU->getSlice()->getPOC();
821      Int iColRefPOC  = pcCU->getSlice()->getRefPOC( eRefPicList, iRefIdx );
822      Int iCurrRefPOC = pcCU->getSlice()->getRefPOC( eRefPicList,  0);
823      Int iScale = pcCU-> xGetDistScaleFactor(iCurrPOC, iCurrRefPOC, iCurrPOC, iColRefPOC);
824      if ( iScale != 4096 )
825      {
826        cMv = cMv.scaleMv( iScale );
827      }
828      iRefIdx = 0;
829    }
830  }
831
832  pcCU->clipMv(cMv);
833  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec();
834  xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, true );
835  xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, true );
836
837  if( dW > 0 )
838  {
839    TComYuv * pYuvB0 = &m_acYuvPredBase[0];
840    TComYuv * pYuvB1  = &m_acYuvPredBase[1];
841
842    TComMv cMVwithDisparity = cMv + cDistparity.m_acNBDV;
843    pcCU->clipMv(cMVwithDisparity);
844
845    assert ( cDistparity.bDV );
846
847    pcPicYuvRef = pcPicYuvBaseCol->getPicYuvRec();
848    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acNBDV, iWidth, iHeight, pYuvB0, bi, true );
849    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acNBDV, iWidth, iHeight, pYuvB0, bi, true );
850   
851    pcPicYuvRef = pcPicYuvBaseRef->getPicYuvRec();
852    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, bi, true );
853    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, bi, true );
854
855    pYuvB0->subtractARP( pYuvB0 , pYuvB1 , uiPartAddr , iWidth , iHeight );
856
857    if( 2 == dW )
858    {
859      pYuvB0->multiplyARP( uiPartAddr , iWidth , iHeight , dW );
860    }
861    rpcYuvPred->addARP( rpcYuvPred , pYuvB0 , uiPartAddr , iWidth , iHeight , !bi );
862  }
863}
864#endif
865
866Void TComPrediction::xPredInterBi ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
867{
868  TComYuv* pcMbYuv;
869  Int      iRefIdx[2] = {-1, -1};
870
871  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
872  {
873    RefPicList eRefPicList = (iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
874    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
875
876    if ( iRefIdx[iRefList] < 0 )
877    {
878      continue;
879    }
880
881    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
882
883    pcMbYuv = &m_acYuvPred[iRefList];
884    if( pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0 )
885    {
886      xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
887    }
888    else
889    {
890      if ( ( pcCU->getSlice()->getPPS()->getUseWP()       && pcCU->getSlice()->getSliceType() == P_SLICE ) || 
891           ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE ) )
892      {
893        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
894      }
895      else
896      {
897        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv );
898      }
899    }
900  }
901
902  if ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE  )
903  {
904    xWeightedPredictionBi( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
905  } 
906  else if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType() == P_SLICE )
907  {
908    xWeightedPredictionUni( pcCU, &m_acYuvPred[0], uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, rpcYuvPred ); 
909  }
910  else
911  {
912    xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
913  }
914}
915
916#if H_3D_VSP
917
918Void TComPrediction::xPredInterBiVSP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
919{
920  TComYuv* pcMbYuv;
921  Int      iRefIdx[2] = {-1, -1};
922  Bool     bi = (pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0);
923
924  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
925  {
926    RefPicList eRefPicList = RefPicList(iRefList);
927    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
928
929    if ( iRefIdx[iRefList] < 0 )
930    {
931      continue;
932    }
933    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
934
935    pcMbYuv = &m_acYuvPred[iRefList];
936    xPredInterUniVSP ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, bi );
937  }
938
939  xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
940}
941
942#endif
943
944/**
945 * \brief Generate motion-compensated luma block
946 *
947 * \param cu       Pointer to current CU
948 * \param refPic   Pointer to reference picture
949 * \param partAddr Address of block within CU
950 * \param mv       Motion vector
951 * \param width    Width of block
952 * \param height   Height of block
953 * \param dstPic   Pointer to destination picture
954 * \param bi       Flag indicating whether bipred is used
955 */
956Void TComPrediction::xPredInterLumaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
957#if H_3D_ARP
958    , Bool filterType
959#endif
960#if H_3D_IC
961    , Bool bICFlag
962#endif
963  )
964{
965  Int refStride = refPic->getStride(); 
966  Int refOffset = ( mv->getHor() >> 2 ) + ( mv->getVer() >> 2 ) * refStride;
967  Pel *ref      = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
968 
969  Int dstStride = dstPic->getStride();
970  Pel *dst      = dstPic->getLumaAddr( partAddr );
971 
972  Int xFrac = mv->getHor() & 0x3;
973  Int yFrac = mv->getVer() & 0x3;
974
975#if H_3D_IC
976  if( cu->getSlice()->getIsDepth() )
977  {
978    refOffset = mv->getHor() + mv->getVer() * refStride;
979    ref       = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
980    xFrac     = 0;
981    yFrac     = 0;
982  }
983#endif
984  if ( yFrac == 0 )
985  {
986#if MTK_CLIPPING_ALIGN_IC_E0168
987    m_if.filterHorLuma( ref, refStride, dst, dstStride, width, height, xFrac,       !bi || bICFlag
988#else
989    m_if.filterHorLuma( ref, refStride, dst, dstStride, width, height, xFrac,       !bi
990#endif
991#if H_3D_ARP
992    , filterType
993#endif
994      );
995  }
996  else if ( xFrac == 0 )
997  {
998#if MTK_CLIPPING_ALIGN_IC_E0168
999    m_if.filterVerLuma( ref, refStride, dst, dstStride, width, height, yFrac, true, !bi || bICFlag
1000#else
1001    m_if.filterVerLuma( ref, refStride, dst, dstStride, width, height, yFrac, true, !bi
1002#endif
1003#if H_3D_ARP
1004    , filterType
1005#endif
1006      );
1007  }
1008  else
1009  {
1010    Int tmpStride = m_filteredBlockTmp[0].getStride();
1011    Short *tmp    = m_filteredBlockTmp[0].getLumaAddr();
1012
1013    Int filterSize = NTAPS_LUMA;
1014    Int halfFilterSize = ( filterSize >> 1 );
1015
1016    m_if.filterHorLuma(ref - (halfFilterSize-1)*refStride, refStride, tmp, tmpStride, width, height+filterSize-1, xFrac, false     
1017#if H_3D_ARP
1018    , filterType
1019#endif
1020      );
1021#if MTK_CLIPPING_ALIGN_IC_E0168
1022    m_if.filterVerLuma(tmp + (halfFilterSize-1)*tmpStride, tmpStride, dst, dstStride, width, height,              yFrac, false, !bi || bICFlag
1023#else
1024    m_if.filterVerLuma(tmp + (halfFilterSize-1)*tmpStride, tmpStride, dst, dstStride, width, height,              yFrac, false, !bi
1025#endif
1026#if H_3D_ARP
1027    , filterType
1028#endif
1029      );   
1030  }
1031
1032#if H_3D_IC
1033  if( bICFlag )
1034  {
1035#if SHARP_ILLUCOMP_REFINE_E0046
1036    Int a, b, i, j;
1037    const Int iShift = IC_CONST_SHIFT;
1038
1039    xGetLLSICPrediction( cu, mv, refPic, a, b, TEXT_LUMA );
1040#else
1041    Int a, b, iShift, i, j;
1042
1043    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_LUMA );
1044#endif
1045
1046
1047    for ( i = 0; i < height; i++ )
1048    {
1049      for ( j = 0; j < width; j++ )
1050      {
1051#if !MTK_CLIPPING_ALIGN_IC_E0168
1052        if( bi )
1053        {
1054          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthY;
1055          dst[j] = ( ( a*dst[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1 << iIFshift ) - IF_INTERNAL_OFFS;
1056        }
1057        else
1058#endif
1059          dst[j] = Clip3( 0, ( 1 << g_bitDepthY ) - 1, ( ( a*dst[j] ) >> iShift ) + b );
1060      }
1061      dst += dstStride;
1062    }
1063#if MTK_CLIPPING_ALIGN_IC_E0168
1064    if(bi)
1065    {
1066      Pel *dst2      = dstPic->getLumaAddr( partAddr );
1067      Int shift = IF_INTERNAL_PREC - g_bitDepthY;
1068      for (i = 0; i < height; i++)
1069      {
1070        for (j = 0; j < width; j++)
1071        {
1072          Short val = dst2[j] << shift;
1073          dst2[j] = val - (Short)IF_INTERNAL_OFFS;
1074        }
1075        dst2 += dstStride;
1076      }
1077    }
1078#endif
1079  }
1080#endif
1081}
1082
1083/**
1084 * \brief Generate motion-compensated chroma block
1085 *
1086 * \param cu       Pointer to current CU
1087 * \param refPic   Pointer to reference picture
1088 * \param partAddr Address of block within CU
1089 * \param mv       Motion vector
1090 * \param width    Width of block
1091 * \param height   Height of block
1092 * \param dstPic   Pointer to destination picture
1093 * \param bi       Flag indicating whether bipred is used
1094 */
1095Void TComPrediction::xPredInterChromaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
1096#if H_3D_ARP
1097    , Bool filterType
1098#endif
1099#if H_3D_IC
1100    , Bool bICFlag
1101#endif
1102  )
1103{
1104  Int     refStride  = refPic->getCStride();
1105  Int     dstStride  = dstPic->getCStride();
1106 
1107  Int     refOffset  = (mv->getHor() >> 3) + (mv->getVer() >> 3) * refStride;
1108 
1109  Pel*    refCb     = refPic->getCbAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1110  Pel*    refCr     = refPic->getCrAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1111 
1112  Pel* dstCb = dstPic->getCbAddr( partAddr );
1113  Pel* dstCr = dstPic->getCrAddr( partAddr );
1114 
1115  Int     xFrac  = mv->getHor() & 0x7;
1116  Int     yFrac  = mv->getVer() & 0x7;
1117  UInt    cxWidth  = width  >> 1;
1118  UInt    cxHeight = height >> 1;
1119 
1120  Int     extStride = m_filteredBlockTmp[0].getStride();
1121  Short*  extY      = m_filteredBlockTmp[0].getLumaAddr();
1122 
1123  Int filterSize = NTAPS_CHROMA;
1124 
1125  Int halfFilterSize = (filterSize>>1);
1126 
1127  if ( yFrac == 0 )
1128  {
1129#if MTK_CLIPPING_ALIGN_IC_E0168
1130    m_if.filterHorChroma(refCb, refStride, dstCb,  dstStride, cxWidth, cxHeight, xFrac, !bi || bICFlag
1131#else
1132    m_if.filterHorChroma(refCb, refStride, dstCb,  dstStride, cxWidth, cxHeight, xFrac, !bi
1133#endif
1134#if H_3D_ARP
1135    , filterType
1136#endif
1137    );   
1138#if MTK_CLIPPING_ALIGN_IC_E0168
1139    m_if.filterHorChroma(refCr, refStride, dstCr,  dstStride, cxWidth, cxHeight, xFrac, !bi || bICFlag
1140#else
1141    m_if.filterHorChroma(refCr, refStride, dstCr,  dstStride, cxWidth, cxHeight, xFrac, !bi
1142#endif
1143#if H_3D_ARP
1144    , filterType
1145#endif
1146    );
1147  }
1148  else if ( xFrac == 0 )
1149  {
1150#if MTK_CLIPPING_ALIGN_IC_E0168
1151    m_if.filterVerChroma(refCb, refStride, dstCb, dstStride, cxWidth, cxHeight, yFrac, true, !bi || bICFlag
1152#else
1153    m_if.filterVerChroma(refCb, refStride, dstCb, dstStride, cxWidth, cxHeight, yFrac, true, !bi
1154#endif
1155#if H_3D_ARP
1156    , filterType
1157#endif
1158    );
1159#if MTK_CLIPPING_ALIGN_IC_E0168
1160    m_if.filterVerChroma(refCr, refStride, dstCr, dstStride, cxWidth, cxHeight, yFrac, true, !bi || bICFlag
1161#else
1162    m_if.filterVerChroma(refCr, refStride, dstCr, dstStride, cxWidth, cxHeight, yFrac, true, !bi
1163#endif
1164#if H_3D_ARP
1165    , filterType
1166#endif
1167    );
1168  }
1169  else
1170  {
1171    m_if.filterHorChroma(refCb - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
1172#if H_3D_ARP
1173    , filterType
1174#endif 
1175      );
1176#if MTK_CLIPPING_ALIGN_IC_E0168
1177    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCb, dstStride, cxWidth, cxHeight  , yFrac, false, !bi || bICFlag
1178#else
1179    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCb, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
1180#endif
1181#if H_3D_ARP
1182    , filterType
1183#endif
1184      );
1185   
1186    m_if.filterHorChroma(refCr - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
1187#if H_3D_ARP
1188    , filterType
1189#endif
1190      );
1191#if MTK_CLIPPING_ALIGN_IC_E0168
1192    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCr, dstStride, cxWidth, cxHeight  , yFrac, false, !bi || bICFlag
1193#else
1194    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCr, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
1195#endif
1196#if H_3D_ARP
1197    , filterType
1198#endif
1199      );   
1200  }
1201
1202#if H_3D_IC
1203  if( bICFlag )
1204  {
1205#if SHARP_ILLUCOMP_REFINE_E0046
1206    Int a, b, i, j;
1207    const Int iShift = IC_CONST_SHIFT;
1208    xGetLLSICPrediction( cu, mv, refPic, a, b, TEXT_CHROMA_U ); // Cb
1209#else
1210    Int a, b, iShift, i, j;
1211    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_CHROMA_U ); // Cb
1212#endif
1213    for ( i = 0; i < cxHeight; i++ )
1214    {
1215      for ( j = 0; j < cxWidth; j++ )
1216      {
1217#if !MTK_CLIPPING_ALIGN_IC_E0168
1218        if( bi )
1219        {
1220          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthC;
1221          dstCb[j] = ( ( a*dstCb[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1<<iIFshift ) - IF_INTERNAL_OFFS;
1222        }
1223        else
1224#endif
1225          dstCb[j] = Clip3(  0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCb[j] ) >> iShift ) + b );
1226      }
1227      dstCb += dstStride;
1228    }
1229#if SHARP_ILLUCOMP_REFINE_E0046
1230    xGetLLSICPrediction( cu, mv, refPic, a, b, TEXT_CHROMA_V ); // Cr
1231#else
1232    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_CHROMA_V ); // Cr
1233#endif
1234    for ( i = 0; i < cxHeight; i++ )
1235    {
1236      for ( j = 0; j < cxWidth; j++ )
1237      {
1238#if !MTK_CLIPPING_ALIGN_IC_E0168
1239        if( bi )
1240        {
1241          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthC;
1242          dstCr[j] = ( ( a*dstCr[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1<<iIFshift ) - IF_INTERNAL_OFFS;
1243        }
1244        else
1245#endif
1246          dstCr[j] = Clip3( 0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCr[j] ) >> iShift ) + b );
1247      }
1248      dstCr += dstStride;
1249    }
1250#if MTK_CLIPPING_ALIGN_IC_E0168
1251    if(bi)
1252    {
1253      Pel* dstCb2 = dstPic->getCbAddr( partAddr );
1254      Pel* dstCr2 = dstPic->getCrAddr( partAddr );
1255      Int shift = IF_INTERNAL_PREC - g_bitDepthC;
1256      for (i = 0; i < cxHeight; i++)
1257      {
1258        for (j = 0; j < cxWidth; j++)
1259        {
1260          Short val = dstCb2[j] << shift;
1261          dstCb2[j] = val - (Short)IF_INTERNAL_OFFS;
1262
1263          val = dstCr2[j] << shift;
1264          dstCr2[j] = val - (Short)IF_INTERNAL_OFFS;
1265        }
1266        dstCb2 += dstStride;
1267        dstCr2 += dstStride;
1268      }
1269    }
1270#endif
1271  }
1272#endif
1273}
1274
1275Void TComPrediction::xWeightedAverage( TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, Int iRefIdx0, Int iRefIdx1, UInt uiPartIdx, Int iWidth, Int iHeight, TComYuv*& rpcYuvDst )
1276{
1277  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
1278  {
1279    rpcYuvDst->addAvg( pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight );
1280  }
1281  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
1282  {
1283    pcYuvSrc0->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
1284  }
1285  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
1286  {
1287    pcYuvSrc1->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
1288  }
1289}
1290
1291// AMVP
1292Void TComPrediction::getMvPredAMVP( TComDataCU* pcCU, UInt uiPartIdx, UInt uiPartAddr, RefPicList eRefPicList, TComMv& rcMvPred )
1293{
1294  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
1295  if( pcAMVPInfo->iN <= 1 )
1296  {
1297    rcMvPred = pcAMVPInfo->m_acMvCand[0];
1298
1299    pcCU->setMVPIdxSubParts( 0, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
1300    pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
1301    return;
1302  }
1303
1304  assert(pcCU->getMVPIdx(eRefPicList,uiPartAddr) >= 0);
1305  rcMvPred = pcAMVPInfo->m_acMvCand[pcCU->getMVPIdx(eRefPicList,uiPartAddr)];
1306  return;
1307}
1308
1309/** Function for deriving planar intra prediction.
1310 * \param pSrc pointer to reconstructed sample array
1311 * \param srcStride the stride of the reconstructed sample array
1312 * \param rpDst reference to pointer for the prediction sample array
1313 * \param dstStride the stride of the prediction sample array
1314 * \param width the width of the block
1315 * \param height the height of the block
1316 *
1317 * This function derives the prediction samples for planar mode (intra coding).
1318 */
1319Void TComPrediction::xPredIntraPlanar( Int* pSrc, Int srcStride, Pel* rpDst, Int dstStride, UInt width, UInt height )
1320{
1321  assert(width == height);
1322
1323  Int k, l, bottomLeft, topRight;
1324  Int horPred;
1325  Int leftColumn[MAX_CU_SIZE], topRow[MAX_CU_SIZE], bottomRow[MAX_CU_SIZE], rightColumn[MAX_CU_SIZE];
1326  UInt blkSize = width;
1327  UInt offset2D = width;
1328  UInt shift1D = g_aucConvertToBit[ width ] + 2;
1329  UInt shift2D = shift1D + 1;
1330
1331  // Get left and above reference column and row
1332  for(k=0;k<blkSize+1;k++)
1333  {
1334    topRow[k] = pSrc[k-srcStride];
1335    leftColumn[k] = pSrc[k*srcStride-1];
1336  }
1337
1338  // Prepare intermediate variables used in interpolation
1339  bottomLeft = leftColumn[blkSize];
1340  topRight   = topRow[blkSize];
1341  for (k=0;k<blkSize;k++)
1342  {
1343    bottomRow[k]   = bottomLeft - topRow[k];
1344    rightColumn[k] = topRight   - leftColumn[k];
1345    topRow[k]      <<= shift1D;
1346    leftColumn[k]  <<= shift1D;
1347  }
1348
1349  // Generate prediction signal
1350  for (k=0;k<blkSize;k++)
1351  {
1352    horPred = leftColumn[k] + offset2D;
1353    for (l=0;l<blkSize;l++)
1354    {
1355      horPred += rightColumn[k];
1356      topRow[l] += bottomRow[l];
1357      rpDst[k*dstStride+l] = ( (horPred + topRow[l]) >> shift2D );
1358    }
1359  }
1360}
1361
1362/** Function for filtering intra DC predictor.
1363 * \param pSrc pointer to reconstructed sample array
1364 * \param iSrcStride the stride of the reconstructed sample array
1365 * \param rpDst reference to pointer for the prediction sample array
1366 * \param iDstStride the stride of the prediction sample array
1367 * \param iWidth the width of the block
1368 * \param iHeight the height of the block
1369 *
1370 * This function performs filtering left and top edges of the prediction samples for DC mode (intra coding).
1371 */
1372Void TComPrediction::xDCPredFiltering( Int* pSrc, Int iSrcStride, Pel*& rpDst, Int iDstStride, Int iWidth, Int iHeight )
1373{
1374  Pel* pDst = rpDst;
1375  Int x, y, iDstStride2, iSrcStride2;
1376
1377  // boundary pixels processing
1378  pDst[0] = (Pel)((pSrc[-iSrcStride] + pSrc[-1] + 2 * pDst[0] + 2) >> 2);
1379
1380  for ( x = 1; x < iWidth; x++ )
1381  {
1382    pDst[x] = (Pel)((pSrc[x - iSrcStride] +  3 * pDst[x] + 2) >> 2);
1383  }
1384
1385  for ( y = 1, iDstStride2 = iDstStride, iSrcStride2 = iSrcStride-1; y < iHeight; y++, iDstStride2+=iDstStride, iSrcStride2+=iSrcStride )
1386  {
1387    pDst[iDstStride2] = (Pel)((pSrc[iSrcStride2] + 3 * pDst[iDstStride2] + 2) >> 2);
1388  }
1389
1390  return;
1391}
1392#if H_3D_IC
1393/** Function for deriving the position of first non-zero binary bit of a value
1394 * \param x input value
1395 *
1396 * This function derives the position of first non-zero binary bit of a value
1397 */
1398Int GetMSB( UInt x )
1399{
1400  Int iMSB = 0, bits = ( sizeof( Int ) << 3 ), y = 1;
1401
1402  while( x > 1 )
1403  {
1404    bits >>= 1;
1405    y = x >> bits;
1406
1407    if( y )
1408    {
1409      x = y;
1410      iMSB += bits;
1411    }
1412  }
1413
1414  iMSB+=y;
1415
1416  return iMSB;
1417}
1418
1419#if !SHARP_ILLUCOMP_REFINE_E0046
1420/** Function for counting leading number of zeros/ones
1421 * \param x input value
1422 \ This function counts leading number of zeros for positive numbers and
1423 \ leading number of ones for negative numbers. This can be implemented in
1424 \ single instructure cycle on many processors.
1425 */
1426
1427Short CountLeadingZerosOnes (Short x)
1428{
1429  Short clz;
1430  Short i;
1431
1432  if(x == 0)
1433  {
1434    clz = 0;
1435  }
1436  else
1437  {
1438    if (x == -1)
1439    {
1440      clz = 15;
1441    }
1442    else
1443    {
1444      if(x < 0)
1445      {
1446        x = ~x;
1447      }
1448      clz = 15;
1449      for(i = 0;i < 15;++i)
1450      {
1451        if(x) 
1452        {
1453          clz --;
1454        }
1455        x = x >> 1;
1456      }
1457    }
1458  }
1459  return clz;
1460}
1461#endif
1462
1463/** Function for deriving LM illumination compensation.
1464 */
1465#if SHARP_ILLUCOMP_REFINE_E0046
1466Void TComPrediction::xGetLLSICPrediction( TComDataCU* pcCU, TComMv *pMv, TComPicYuv *pRefPic, Int &a, Int &b, TextType eType )
1467#else
1468Void TComPrediction::xGetLLSICPrediction( TComDataCU* pcCU, TComMv *pMv, TComPicYuv *pRefPic, Int &a, Int &b, Int &iShift, TextType eType )
1469#endif
1470{
1471  TComPicYuv *pRecPic = pcCU->getPic()->getPicYuvRec();
1472  Pel *pRec = NULL, *pRef = NULL;
1473  UInt uiWidth, uiHeight, uiTmpPartIdx;
1474  Int iRecStride = ( eType == TEXT_LUMA ) ? pRecPic->getStride() : pRecPic->getCStride();
1475  Int iRefStride = ( eType == TEXT_LUMA ) ? pRefPic->getStride() : pRefPic->getCStride();
1476  Int iCUPelX, iCUPelY, iRefX, iRefY, iRefOffset, iHor, iVer;
1477
1478  iCUPelX = pcCU->getCUPelX() + g_auiRasterToPelX[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
1479  iCUPelY = pcCU->getCUPelY() + g_auiRasterToPelY[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
1480  iHor = pcCU->getSlice()->getIsDepth() ? pMv->getHor() : ( ( pMv->getHor() + 2 ) >> 2 );
1481  iVer = pcCU->getSlice()->getIsDepth() ? pMv->getVer() : ( ( pMv->getVer() + 2 ) >> 2 );
1482  iRefX   = iCUPelX + iHor;
1483  iRefY   = iCUPelY + iVer;
1484  if( eType != TEXT_LUMA )
1485  {
1486    iHor = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getHor() + 1 ) >> 1 ) : ( ( pMv->getHor() + 4 ) >> 3 );
1487    iVer = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getVer() + 1 ) >> 1 ) : ( ( pMv->getVer() + 4 ) >> 3 );
1488  }
1489  uiWidth  = ( eType == TEXT_LUMA ) ? pcCU->getWidth( 0 )  : ( pcCU->getWidth( 0 )  >> 1 );
1490  uiHeight = ( eType == TEXT_LUMA ) ? pcCU->getHeight( 0 ) : ( pcCU->getHeight( 0 ) >> 1 );
1491
1492  Int i, j, iCountShift = 0;
1493
1494  // LLS parameters estimation -->
1495
1496  Int x = 0, y = 0, xx = 0, xy = 0;
1497#if SHARP_ILLUCOMP_REFINE_E0046
1498  Int precShift = std::max(0, (( eType == TEXT_LUMA ) ? g_bitDepthY : g_bitDepthC) - 12);
1499#endif
1500
1501  if( pcCU->getPUAbove( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) && iCUPelY > 0 && iRefY > 0 )
1502  {
1503    iRefOffset = iHor + iVer * iRefStride - iRefStride;
1504    if( eType == TEXT_LUMA )
1505    {
1506      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1507      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1508    }
1509    else if( eType == TEXT_CHROMA_U )
1510    {
1511      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1512      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1513    }
1514    else
1515    {
1516      assert( eType == TEXT_CHROMA_V );
1517      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1518      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1519    }
1520
1521#if SHARP_ILLUCOMP_REFINE_E0046
1522    for( j = 0; j < uiWidth; j+=2 )
1523#else
1524    for( j = 0; j < uiWidth; j++ )
1525#endif
1526    {
1527      x += pRef[j];
1528      y += pRec[j];
1529#if SHARP_ILLUCOMP_REFINE_E0046
1530      xx += (pRef[j] * pRef[j])>>precShift;
1531      xy += (pRef[j] * pRec[j])>>precShift;
1532#else
1533      xx += pRef[j] * pRef[j];
1534      xy += pRef[j] * pRec[j];
1535#endif
1536    }
1537#if SHARP_ILLUCOMP_REFINE_E0046
1538    iCountShift += g_aucConvertToBit[ uiWidth ] + 1;
1539#else
1540    iCountShift += g_aucConvertToBit[ uiWidth ] + 2;
1541#endif
1542  }
1543
1544
1545  if( pcCU->getPULeft( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) && iCUPelX > 0 && iRefX > 0 )
1546  {
1547    iRefOffset = iHor + iVer * iRefStride - 1;
1548    if( eType == TEXT_LUMA )
1549    {
1550      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1551      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1552    }
1553    else if( eType == TEXT_CHROMA_U )
1554    {
1555      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1556      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1557    }
1558    else
1559    {
1560      assert( eType == TEXT_CHROMA_V );
1561      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1562      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1563    }
1564
1565#if SHARP_ILLUCOMP_REFINE_E0046
1566    for( i = 0; i < uiHeight; i+=2 )
1567#else
1568    for( i = 0; i < uiHeight; i++ )
1569#endif
1570    {
1571      x += pRef[0];
1572      y += pRec[0];
1573#if SHARP_ILLUCOMP_REFINE_E0046
1574      xx += (pRef[0] * pRef[0])>>precShift;
1575      xy += (pRef[0] * pRec[0])>>precShift;
1576
1577      pRef += iRefStride*2;
1578      pRec += iRecStride*2;
1579#else
1580      xx += pRef[0] * pRef[0];
1581      xy += pRef[0] * pRec[0];
1582
1583      pRef += iRefStride;
1584      pRec += iRecStride;
1585#endif
1586    }
1587#if SHARP_ILLUCOMP_REFINE_E0046
1588    iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 1 );
1589#else
1590    iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 2 );
1591#endif
1592  }
1593
1594#if SHARP_ILLUCOMP_REFINE_E0046
1595  xy += xx >> IC_REG_COST_SHIFT;
1596  xx += xx >> IC_REG_COST_SHIFT;
1597  Int a1 = ( xy << iCountShift ) - ((y * x) >> precShift);
1598  Int a2 = ( xx << iCountShift ) - ((x * x) >> precShift);
1599  const Int iShift = IC_CONST_SHIFT;
1600  {
1601#else
1602  Int iTempShift = ( ( eType == TEXT_LUMA ) ? g_bitDepthY : g_bitDepthC ) + g_aucConvertToBit[ uiWidth ] + 3 - 15;
1603
1604  if( iTempShift > 0 )
1605  {
1606    x  = ( x +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1607    y  = ( y +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1608    xx = ( xx + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1609    xy = ( xy + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1610    iCountShift -= iTempShift;
1611  }
1612
1613  iShift = 13;
1614
1615  if( iCountShift == 0 )
1616  {
1617    a = 1;
1618    b = 0;
1619    iShift = 0;
1620  }
1621  else
1622  {
1623    Int a1 = ( xy << iCountShift ) - y * x;
1624    Int a2 = ( xx << iCountShift ) - x * x;             
1625#endif
1626    {
1627      const Int iShiftA2 = 6;
1628#if !SHARP_ILLUCOMP_REFINE_E0046
1629      const Int iShiftA1 = 15;
1630#endif
1631      const Int iAccuracyShift = 15;
1632
1633      Int iScaleShiftA2 = 0;
1634      Int iScaleShiftA1 = 0;
1635      Int a1s = a1;
1636      Int a2s = a2;
1637
1638#if SHARP_ILLUCOMP_REFINE_E0046
1639      a1 = Clip3(0, 2*a2, a1);
1640      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2;
1641      iScaleShiftA1 = iScaleShiftA2 - IC_SHIFT_DIFF;
1642#else
1643      iScaleShiftA1 = GetMSB( abs( a1 ) ) - iShiftA1;
1644      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2; 
1645#endif
1646
1647      if( iScaleShiftA1 < 0 )
1648      {
1649        iScaleShiftA1 = 0;
1650      }
1651
1652      if( iScaleShiftA2 < 0 )
1653      {
1654        iScaleShiftA2 = 0;
1655      }
1656
1657      Int iScaleShiftA = iScaleShiftA2 + iAccuracyShift - iShift - iScaleShiftA1;
1658
1659
1660      a2s = a2 >> iScaleShiftA2;
1661
1662      a1s = a1 >> iScaleShiftA1;
1663
1664#if SHARP_ILLUCOMP_REFINE_E0046
1665      a = a1s * m_uiaShift[ a2s ];
1666      a = a >> iScaleShiftA;
1667#else
1668      if (a2s >= 1)
1669      {
1670        a = a1s * m_uiaShift[ a2s - 1];
1671      }
1672      else
1673      {
1674        a = 0;
1675      }
1676
1677      if( iScaleShiftA < 0 )
1678      {
1679        a = a << -iScaleShiftA;
1680      }
1681      else
1682      {
1683        a = a >> iScaleShiftA;
1684      }
1685
1686      a = Clip3( -( 1 << 15 ), ( 1 << 15 ) - 1, a ); 
1687
1688      Int minA = -(1 << (6));
1689      Int maxA = (1 << 6) - 1;
1690      if( a <= maxA && a >= minA )
1691      {
1692        // do nothing
1693      }
1694      else
1695      {
1696        Short n = CountLeadingZerosOnes( a );
1697        a = a >> (9-n);
1698        iShift -= (9-n);
1699      }
1700#endif
1701      b = (  y - ( ( a * x ) >> iShift ) + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
1702    }
1703  }   
1704}
1705#endif
1706
1707#if H_3D_VSP
1708
1709#if NTT_VSP_COMMON_E0207_E0208
1710// not fully support iRatioTxtPerDepth* != 1
1711Void TComPrediction::xGetVirtualDepth( TComDataCU *cu, TComPicYuv *picRefDepth, TComMv *mv, UInt partAddr, Int width, Int height, TComYuv *yuvDepth, Int ratioTxtPerDepthX, Int ratioTxtPerDepthY )
1712{
1713  Int nTxtPerDepthX = H_3D_VSP_BLOCKSIZE;
1714  Int nTxtPerDepthY = H_3D_VSP_BLOCKSIZE;
1715
1716  Int refDepStride = picRefDepth->getStride();
1717
1718#if NTT_VSP_VECTOR_CLIP_E0208
1719
1720  Int refDepOffset  = ( (mv->getHor()+2) >> 2 ) + ( (mv->getVer()+2) >> 2 ) * refDepStride;
1721  Pel *refDepth     = picRefDepth->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr );
1722
1723  if( ratioTxtPerDepthX!=1 || ratioTxtPerDepthY!=1 )
1724  {
1725    Int posX, posY;
1726    refDepth    = picRefDepth->getLumaAddr( );
1727    cu->getPic()->getPicYuvRec()->getTopLeftSamplePos( cu->getAddr(), cu->getZorderIdxInCU() + partAddr, posX, posY ); // top-left position in texture
1728    posX /= ratioTxtPerDepthX; // texture position -> depth postion
1729    posY /= ratioTxtPerDepthY;
1730    refDepOffset += posX + posY * refDepStride;
1731
1732    width  /= ratioTxtPerDepthX; // texture size -> depth size
1733    height /= ratioTxtPerDepthY;
1734  }
1735
1736  refDepth += refDepOffset;
1737
1738#else // NTT_VSP_VECTOR_CLIP_E0208
1739
1740  Int widthDepth = picRefDepth->getWidth();
1741  Int heightDepth = picRefDepth->getHeight();
1742  Int posX, posY;
1743  cu->getPic()->getPicYuvRec()->getTopLeftSamplePos( cu->getAddr(), cu->getZorderIdxInCU() + partAddr, posX, posY ); // top-left position in texture
1744  posX /= ratioTxtPerDepthX; // texture position -> depth postion
1745  posY /= ratioTxtPerDepthY;
1746 
1747  posX = Clip3(0, widthDepth-width,   posX + ((mv->getHor()+2)>>2));
1748  posY = Clip3(0, heightDepth-height, posY + ((mv->getVer()+2)>>2));
1749 
1750  Pel *refDepth  = picRefDepth->getLumaAddr() + posX + posY * refDepStride;
1751
1752#endif // NTT_VSP_VECTOR_CLIP_E0208
1753
1754  Int depStride = yuvDepth->getStride();
1755  Pel *depth = yuvDepth->getLumaAddr();
1756
1757#if NTT_VSP_ADAPTIVE_SPLIT_E0207
1758
1759  if( width<8 || height<8 )
1760  { // no split
1761    Int rightOffset = width - 1;
1762    Int depStrideBlock = depStride * nTxtPerDepthY;
1763    Pel *refDepthTop = refDepth;
1764    Pel *refDepthBot = refDepthTop + (height-1)*refDepStride;
1765
1766    Pel maxDepth = refDepthTop[0] > refDepthBot[0] ? refDepthTop[0] : refDepthBot[0];
1767    if( maxDepth < refDepthTop[rightOffset] ) { maxDepth = refDepthTop[rightOffset]; }
1768    if( maxDepth < refDepthBot[rightOffset] ) { maxDepth = refDepthBot[rightOffset]; }
1769
1770    for( Int sY=0; sY<height; sY+=nTxtPerDepthY )
1771    {
1772      for( Int sX=0; sX<width; sX+=nTxtPerDepthX )
1773      {
1774        depth[sX] = maxDepth;
1775      }
1776      depth += depStrideBlock;
1777    }
1778  }
1779  else
1780  { // split to 4x8, or 8x4
1781    Int blocksize    = 8;
1782    Int subblocksize = 4;
1783    Int depStrideBlock = depStride * blocksize;
1784    Pel *depthTmp = NULL;
1785    Int depStrideTmp = depStride * nTxtPerDepthY;
1786    Int offset[4] = { 0, subblocksize-1, subblocksize, blocksize-1 };
1787    Pel *refDepthTmp[4] = { NULL, NULL, NULL, NULL };
1788    Pel repDepth4x8[2] = {0, 0};
1789    Pel repDepth8x4[2] = {0, 0};
1790
1791    Int refDepStrideBlock    = refDepStride * blocksize;
1792    Int refDepStrideSubBlock = refDepStride * subblocksize;
1793
1794    refDepthTmp[0] = refDepth;
1795    refDepthTmp[2] = refDepthTmp[0] + refDepStrideSubBlock;
1796    refDepthTmp[1] = refDepthTmp[2] - refDepStride;
1797    refDepthTmp[3] = refDepthTmp[1] + refDepStrideSubBlock;
1798
1799    for( Int y=0; y<height; y+=blocksize )
1800    {
1801      for( Int x=0; x<width; x+=blocksize )
1802      {
1803        Bool ULvsBR = false, URvsBL = false;
1804
1805        ULvsBR = refDepthTmp[0][x+offset[0]] < refDepthTmp[3][x+offset[3]];
1806        URvsBL = refDepthTmp[0][x+offset[3]] < refDepthTmp[3][x+offset[0]];
1807
1808        if( ULvsBR ^ URvsBL )
1809        { // 4x8
1810          repDepth4x8[0] = refDepthTmp[0][x+offset[0]] > refDepthTmp[0][x+offset[1]] ? refDepthTmp[0][x+offset[0]] : refDepthTmp[0][x+offset[1]];
1811          if( repDepth4x8[0] < refDepthTmp[3][x+offset[0]] )
1812          {
1813            repDepth4x8[0] = refDepthTmp[3][x+offset[0]];
1814          }
1815          if( repDepth4x8[0] < refDepthTmp[3][x+offset[1]] )
1816          {
1817            repDepth4x8[0] = refDepthTmp[3][x+offset[1]];
1818          }
1819          repDepth4x8[1] = refDepthTmp[0][x+offset[2]] > refDepthTmp[0][x+offset[3]] ? refDepthTmp[0][x+offset[2]] : refDepthTmp[0][x+offset[3]];
1820          if( repDepth4x8[1] < refDepthTmp[3][x+offset[2]] )
1821          {
1822            repDepth4x8[1] = refDepthTmp[3][x+offset[2]];
1823          }
1824          if( repDepth4x8[1] < refDepthTmp[3][x+offset[3]] )
1825          {
1826            repDepth4x8[1] = refDepthTmp[3][x+offset[3]];
1827          }
1828
1829          depthTmp = &depth[x];
1830          for( Int sY=0; sY<blocksize; sY+=nTxtPerDepthY )
1831          {
1832            for( Int sX=0; sX<subblocksize; sX+=nTxtPerDepthX )
1833            {
1834              depthTmp[sX] = repDepth4x8[0];
1835            }
1836            depthTmp += depStrideTmp;
1837          }
1838          depthTmp = &depth[x+subblocksize];
1839          for( Int sY=0; sY<blocksize; sY+=nTxtPerDepthY )
1840          {
1841            for( Int sX=0; sX<subblocksize; sX+=nTxtPerDepthX )
1842            {
1843              depthTmp[sX] = repDepth4x8[1];
1844            }
1845            depthTmp += depStrideTmp;
1846          }
1847        }
1848        else
1849        { // 8x4
1850          repDepth8x4[0] = refDepthTmp[0][x+offset[0]] > refDepthTmp[0][x+offset[3]] ? refDepthTmp[0][x+offset[0]] : refDepthTmp[0][x+offset[3]];
1851          if( repDepth8x4[0] < refDepthTmp[1][x+offset[0]] )
1852          {
1853            repDepth8x4[0] = refDepthTmp[1][x+offset[0]];
1854          }
1855          if( repDepth8x4[0] < refDepthTmp[1][x+offset[3]] )
1856          {
1857            repDepth8x4[0] = refDepthTmp[1][x+offset[3]];
1858          }
1859          repDepth8x4[1] = refDepthTmp[2][x+offset[0]] > refDepthTmp[2][x+offset[3]] ? refDepthTmp[2][x+offset[0]] : refDepthTmp[2][x+offset[3]];
1860          if( repDepth8x4[1] < refDepthTmp[3][x+offset[0]] )
1861          {
1862            repDepth8x4[1] = refDepthTmp[3][x+offset[0]];
1863          }
1864          if( repDepth8x4[1] < refDepthTmp[3][x+offset[3]] )
1865          {
1866            repDepth8x4[1] = refDepthTmp[3][x+offset[3]];
1867          }
1868         
1869          depthTmp = &depth[x];
1870          for( Int sY=0; sY<subblocksize; sY+=nTxtPerDepthY )
1871          {
1872            for( Int sX=0; sX<blocksize; sX+=nTxtPerDepthX )
1873            {
1874              depthTmp[sX] = repDepth8x4[0];
1875            }
1876            depthTmp += depStrideTmp;
1877          }
1878          for( Int sY=0; sY<subblocksize; sY+=nTxtPerDepthY )
1879          {
1880            for( Int sX=0; sX<blocksize; sX+=nTxtPerDepthX )
1881            {
1882              depthTmp[sX] = repDepth8x4[1];
1883            }
1884            depthTmp += depStrideTmp;
1885          }
1886        }
1887      }
1888      refDepthTmp[0] += refDepStrideBlock;
1889      refDepthTmp[1] += refDepStrideBlock;
1890      refDepthTmp[2] += refDepStrideBlock;
1891      refDepthTmp[3] += refDepStrideBlock;
1892      depth       += depStrideBlock;
1893    }
1894  }
1895
1896#else // NTT_VSP_ADAPTIVE_SPLIT_E0207
1897
1898  Int rightOffset = nTxtPerDepthX - 1;
1899  Int depStrideBlock = depStride * nTxtPerDepthY;
1900  Int refDepStrideBlock = refDepStride * nTxtPerDepthY;
1901  Pel *refDepthTop = refDepth;
1902  Pel *refDepthBot = refDepthTop + (nTxtPerDepthY-1)*refDepStride;
1903
1904  for( Int y=0; y<height; y+= nTxtPerDepthY )
1905  {
1906    for( Int x=0; x<width; x+=nTxtPerDepthX )
1907    {
1908      Pel maxDepth = refDepthTop[x] > refDepthBot[x] ? refDepthTop[x] : refDepthBot[x]; 
1909
1910      if( maxDepth < refDepthTop[x+rightOffset] )
1911      {
1912        maxDepth = refDepthTop[x+rightOffset];
1913      }
1914      if( maxDepth < refDepthBot[x+rightOffset] )
1915      {
1916        maxDepth = refDepthBot[x+rightOffset];
1917      }
1918
1919      depth[x] = maxDepth;
1920
1921    }
1922    refDepthTop += refDepStrideBlock;
1923    refDepthBot += refDepStrideBlock;
1924    depth       += depStrideBlock;
1925  }
1926
1927#endif // NTT_VSP_ADAPTIVE_SPLIT_E0207
1928}
1929
1930Void TComPrediction::xPredInterLumaBlkFromDM( TComDataCU *cu, TComPicYuv *picRef, TComYuv *yuvDepth, Int* shiftLUT, TComMv *mv, UInt partAddr, Int width, Int height, Bool isDepth, TComYuv *&yuvDst, Bool isBi )
1931{
1932  Int nTxtPerDepthX = H_3D_VSP_BLOCKSIZE;
1933  Int nTxtPerDepthY = H_3D_VSP_BLOCKSIZE;
1934 
1935  Int refStride = picRef->getStride();
1936  Int dstStride = yuvDst->getStride();
1937  Int depStride = yuvDepth->getStride();
1938  Int refStrideBlock = refStride  * nTxtPerDepthY;
1939  Int dstStrideBlock = dstStride * nTxtPerDepthY;
1940  Int depStrideBlock = depStride * nTxtPerDepthY;
1941
1942  Pel *ref    = picRef->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr );
1943  Pel *dst    = yuvDst->getLumaAddr(partAddr);
1944  Pel *depth  = yuvDepth->getLumaAddr();
1945 
1946#if !(NTT_VSP_DC_BUGFIX_E0208)
1947  Int widthLuma = picRef->getWidth();
1948  Int posX, posY;
1949  cu->getPic()->getPicYuvRec()->getTopLeftSamplePos( cu->getAddr(), cu->getZorderIdxInCU() + partAddr, posX, posY ); // top-left position in texture
1950#endif
1951
1952#if H_3D_VSP_BLOCKSIZE == 1
1953#if H_3D_VSP_CONSTRAINED
1954  //get LUT based horizontal reference range
1955  Int range = xGetConstrainedSize(width, height);
1956
1957  // The minimum depth value
1958  Int minRelativePos = MAX_INT;
1959  Int maxRelativePos = MIN_INT;
1960
1961  Pel* depthTemp, *depthInitial=depth;
1962  for (Int yTxt = 0; yTxt < height; yTxt++)
1963  {
1964    for (Int xTxt = 0; xTxt < width; xTxt++)
1965    {
1966      if (depthPosX+xTxt < widthDepth)
1967      {
1968        depthTemp = depthInitial + xTxt;
1969      }
1970      else
1971      {
1972        depthTemp = depthInitial + (widthDepth - depthPosX - 1);
1973      }
1974
1975      Int disparity = shiftLUT[ *depthTemp ]; // << iShiftPrec;
1976      Int disparityInt = disparity >> 2;
1977
1978      if( disparity <= 0)
1979      {
1980        if (minRelativePos > disparityInt+xTxt)
1981        {
1982          minRelativePos = disparityInt+xTxt;
1983        }
1984      }
1985      else
1986      {
1987        if (maxRelativePos < disparityInt+xTxt)
1988        {
1989          maxRelativePos = disparityInt+xTxt;
1990        }
1991      }
1992    }
1993    if (depthPosY+yTxt < heightDepth)
1994    {
1995      depthInitial = depthInitial + depStride;
1996    }
1997  }
1998
1999  Int disparity_tmp = shiftLUT[ *depth ]; // << iShiftPrec;
2000  if (disparity_tmp <= 0)
2001  {
2002    maxRelativePos = minRelativePos + range -1 ;
2003  }
2004  else
2005  {
2006    minRelativePos = maxRelativePos - range +1 ;
2007  }
2008#endif
2009#endif // H_3D_VSP_BLOCKSIZE == 1
2010
2011  TComMv dv(0, 0);
2012
2013  for ( Int yTxt = 0; yTxt < height; yTxt += nTxtPerDepthY )
2014  {
2015    for ( Int xTxt = 0; xTxt < width; xTxt += nTxtPerDepthX )
2016    {
2017      Pel repDepth = depth[ xTxt ];
2018      assert( repDepth >= 0 && repDepth <= 255 );
2019
2020      Int disparity = shiftLUT[ repDepth ]; // remove << iShiftPrec ??
2021      Int xFrac = disparity & 0x3;
2022
2023#if NTT_VSP_DC_BUGFIX_E0208
2024
2025      dv.setHor( disparity );
2026      cu->clipMv( dv );
2027
2028      Int refOffset = xTxt + (dv.getHor() >> 2);
2029     
2030#if H_3D_VSP_CONSTRAINED
2031      if(refOffset<minRelativePos || refOffset>maxRelativePos)
2032      {
2033        xFrac = 0;
2034      }
2035      refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
2036#endif
2037
2038      assert( ref[refOffset] >= 0 && ref[refOffset]<= 255 );
2039      m_if.filterHorLuma( &ref[refOffset], refStride, &dst[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !isBi );
2040
2041#else // NTT_VSP_DC_BUGFIX_E0208
2042
2043      for( Int j=0; j < nTxtPerDepthX; j++ )
2044      {
2045        Int refOffset = xTxt+j + (disparity >> 2);
2046#if H_3D_VSP_CONSTRAINED
2047        if(refOffset<minRelativePos || refOffset>maxRelativePos)
2048        {
2049          xFrac = 0;
2050        }
2051        refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
2052#endif
2053        Int absX  = posX + refOffset;
2054
2055        if (xFrac == 0)
2056        {
2057          absX = Clip3(0, widthLuma-1, absX);
2058        }
2059        else
2060        {
2061          absX = Clip3(4, widthLuma-5, absX);
2062        }
2063
2064        refOffset = absX - posX;
2065        assert( ref[refOffset] >= 0 && ref[refOffset] <= 255 );
2066       
2067        m_if.filterHorLuma( &ref[refOffset], refStride, &dst[xTxt+j], dstStride, 1, nTxtPerDepthY, xFrac, !isBi );
2068      }
2069
2070#endif // NTT_VSP_DC_BUGFIX_E0208
2071
2072    }
2073    ref   += refStrideBlock;
2074    dst   += dstStrideBlock;
2075    depth += depStrideBlock;
2076  }
2077
2078}
2079
2080Void TComPrediction::xPredInterChromaBlkFromDM  ( TComDataCU *cu, TComPicYuv *picRef, TComYuv *yuvDepth, Int* shiftLUT, TComMv *mv, UInt partAddr, Int width, Int height, Bool isDepth, TComYuv *&yuvDst, Bool isBi )
2081{
2082#if (H_3D_VSP_BLOCKSIZE==1)
2083  Int nTxtPerDepthX = 1;
2084  Int nTxtPerDepthY = 1;
2085#else
2086  Int nTxtPerDepthX = H_3D_VSP_BLOCKSIZE >> 1;
2087  Int nTxtPerDepthY = H_3D_VSP_BLOCKSIZE >> 1;
2088#endif
2089
2090  Int refStride = picRef->getCStride();
2091  Int dstStride = yuvDst->getCStride();
2092  Int depStride = yuvDepth->getStride();
2093  Int refStrideBlock = refStride * nTxtPerDepthY;
2094  Int dstStrideBlock = dstStride * nTxtPerDepthY;
2095  Int depStrideBlock = depStride * (nTxtPerDepthY<<1);
2096
2097  Pel *refCb  = picRef->getCbAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr );
2098  Pel *refCr  = picRef->getCrAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr );
2099  Pel *dstCb  = yuvDst->getCbAddr(partAddr);
2100  Pel *dstCr  = yuvDst->getCrAddr(partAddr);
2101  Pel *depth  = yuvDepth->getLumaAddr();
2102
2103#if !(NTT_VSP_DC_BUGFIX_E0208)
2104  Int widthChroma = picRef->getWidth() >> 1;
2105  Int posX, posY;
2106  cu->getPic()->getPicYuvRec()->getTopLeftSamplePos( cu->getAddr(), cu->getZorderIdxInCU() + partAddr, posX, posY ); // top-left position in texture
2107  posX >>= 1;
2108  posY >>= 1;
2109#endif
2110 
2111#if H_3D_VSP_BLOCKSIZE == 1
2112#if H_3D_VSP_CONSTRAINED
2113  //get LUT based horizontal reference range
2114  Int range = xGetConstrainedSize(width, height, false);
2115
2116  // The minimum depth value
2117  Int minRelativePos = MAX_INT;
2118  Int maxRelativePos = MIN_INT;
2119
2120  Int depthTmp;
2121  for (Int yTxt=0; yTxt<height; yTxt++)
2122  {
2123    for (Int xTxt=0; xTxt<width; xTxt++)
2124    {
2125      depthTmp = m_pDepthBlock[xTxt+yTxt*width];
2126      Int disparity = shiftLUT[ depthTmp ]; // << iShiftPrec;
2127      Int disparityInt = disparity >> 3;//in chroma resolution
2128
2129      if (disparityInt < 0)
2130      {
2131        if (minRelativePos > disparityInt+xTxt)
2132        {
2133          minRelativePos = disparityInt+xTxt;
2134        }
2135      }
2136      else
2137      {
2138        if (maxRelativePos < disparityInt+xTxt)
2139        {
2140          maxRelativePos = disparityInt+xTxt;
2141        }
2142      }
2143    }
2144  }
2145
2146  depthTmp = m_pDepthBlock[0];
2147  Int disparity_tmp = shiftLUT[ depthTmp ]; // << iShiftPrec;
2148  if ( disparity_tmp < 0 )
2149  {
2150    maxRelativePos = minRelativePos + range - 1;
2151  }
2152  else
2153  {
2154    minRelativePos = maxRelativePos - range + 1;
2155  }
2156
2157#endif // H_3D_VSP_CONSTRAINED
2158#endif // H_3D_VSP_BLOCKSIZE == 1
2159
2160  TComMv dv(0, 0);
2161  // luma size -> chroma size
2162  height >>= 1;
2163  width  >>= 1;
2164
2165  for ( Int yTxt = 0; yTxt < height; yTxt += nTxtPerDepthY )
2166  {
2167    for ( Int xTxt = 0; xTxt < width; xTxt += nTxtPerDepthX )
2168    {
2169      Pel repDepth = depth[ xTxt<<1 ];
2170      assert( repDepth >= 0 && repDepth <= 255 );
2171
2172      Int disparity = shiftLUT[ repDepth ]; // remove << iShiftPrec;
2173      Int xFrac = disparity & 0x7;
2174     
2175#if NTT_VSP_DC_BUGFIX_E0208
2176
2177      dv.setHor( disparity );
2178      cu->clipMv( dv );
2179
2180      Int refOffset = xTxt + (dv.getHor() >> 3);
2181
2182#if H_3D_VSP_CONSTRAINED
2183      if(refOffset<minRelativePos || refOffset>maxRelativePos)
2184      {
2185        xFrac = 0;
2186      }
2187      refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
2188#endif
2189
2190      assert( refCb[refOffset] >= 0 && refCb[refOffset]<= 255 );
2191      assert( refCr[refOffset] >= 0 && refCr[refOffset]<= 255 );
2192
2193      m_if.filterHorChroma( &refCb[refOffset], refStride, &dstCb[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !isBi );
2194      m_if.filterHorChroma( &refCr[refOffset], refStride, &dstCr[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !isBi );
2195
2196#else // NTT_VSP_DC_BUGFIX_E0208
2197     
2198      for( Int j=0; j < nTxtPerDepthX; j++ )
2199      {
2200        Int refOffset = xTxt+j + (disparity >> 3);
2201#if H_3D_VSP_CONSTRAINED
2202        if(refOffset<minRelativePos || refOffset>maxRelativePos)
2203        {
2204          xFrac = 0;
2205        }
2206        refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
2207#endif
2208        Int absX  = posX + refOffset;
2209
2210        if (xFrac == 0)
2211        {
2212          absX = Clip3(0, widthChroma-1, absX);
2213        }
2214        else
2215        {
2216          absX = Clip3(4, widthChroma-5, absX);
2217        }
2218
2219        refOffset = absX - posX;
2220        assert( refCb[refOffset] >= 0 && refCb[refOffset] <= 255 );
2221        assert( refCr[refOffset] >= 0 && refCr[refOffset] <= 255 );
2222
2223        m_if.filterHorChroma( &refCb[refOffset], refStride, &dstCb[xTxt+j], dstStride, 1, nTxtPerDepthY, xFrac, !isBi );
2224        m_if.filterHorChroma( &refCr[refOffset], refStride, &dstCr[xTxt+j], dstStride, 1, nTxtPerDepthY, xFrac, !isBi );
2225      }
2226
2227#endif // NTT_VSP_DC_BUGFIX_E0208
2228    }
2229    refCb += refStrideBlock;
2230    refCr += refStrideBlock;
2231    dstCb += dstStrideBlock;
2232    dstCr += dstStrideBlock;
2233    depth += depStrideBlock;
2234  }
2235
2236}
2237#else // NTT_VSP_COMMON_E0207_E0208
2238
2239// Input:
2240// refPic: Ref picture. Full picture, with padding
2241// posX, posY:     PU position, texture
2242// sizeX, sizeY: PU size
2243// partAddr: z-order index
2244// dv: disparity vector. derived from neighboring blocks
2245//
2246// Output: dstPic, PU predictor 64x64
2247Void TComPrediction::xPredInterLumaBlkFromDM( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, TComMv* dv, UInt partAddr,Int posX, Int posY
2248                                            , Int sizeX, Int sizeY, Bool isDepth, TComYuv *&dstPic, Bool bi )
2249{
2250  Int widthLuma;
2251  Int heightLuma;
2252
2253  if (isDepth)
2254  {
2255    widthLuma   =  pPicBaseDepth->getWidth();
2256    heightLuma  =  pPicBaseDepth->getHeight();
2257  }
2258  else
2259  {
2260    widthLuma   =  refPic->getWidth();
2261    heightLuma  =  refPic->getHeight();
2262  }
2263
2264#if H_3D_VSP_BLOCKSIZE != 1
2265  Int widthDepth  = pPicBaseDepth->getWidth();
2266  Int heightDepth = pPicBaseDepth->getHeight();
2267#endif
2268
2269#if H_3D_VSP_CONSTRAINED
2270  Int widthDepth  = pPicBaseDepth->getWidth();
2271  Int heightDepth = pPicBaseDepth->getHeight();
2272#endif
2273
2274  Int nTxtPerDepthX = widthLuma  / ( pPicBaseDepth->getWidth() );  // texture pixel # per depth pixel
2275  Int nTxtPerDepthY = heightLuma / ( pPicBaseDepth->getHeight() );
2276
2277  Int refStride = refPic->getStride();
2278  Int dstStride = dstPic->getStride();
2279  Int depStride =  pPicBaseDepth->getStride();
2280  Int depthPosX = Clip3(0,   widthLuma - sizeX,  (posX/nTxtPerDepthX) + ((dv->getHor()+2)>>2));
2281  Int depthPosY = Clip3(0,   heightLuma- sizeY,  (posY/nTxtPerDepthY) + ((dv->getVer()+2)>>2));
2282  Pel *ref    = refPic->getLumaAddr() + posX + posY * refStride;
2283  Pel *dst    = dstPic->getLumaAddr(partAddr);
2284  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;
2285
2286#if H_3D_VSP_BLOCKSIZE != 1
2287#if H_3D_VSP_BLOCKSIZE == 2
2288  Int  dW = sizeX>>1;
2289  Int  dH = sizeY>>1;
2290#endif
2291#if H_3D_VSP_BLOCKSIZE == 4
2292  Int  dW = sizeX>>2;
2293  Int  dH = sizeY>>2;
2294#endif
2295  {
2296    Pel* depthi = depth;
2297    for (Int j = 0; j < dH; j++)
2298    {
2299      for (Int i = 0; i < dW; i++)
2300      {
2301        Pel* depthTmp;
2302#if H_3D_VSP_BLOCKSIZE == 2
2303        if (depthPosX + (i<<1) < widthDepth)
2304          depthTmp = depthi + (i << 1);
2305        else
2306          depthTmp = depthi + (widthDepth - depthPosX - 1);
2307#endif
2308#if H_3D_VSP_BLOCKSIZE == 4
2309        if (depthPosX + (i<<2) < widthDepth)
2310          depthTmp = depthi + (i << 2);
2311        else
2312          depthTmp = depthi + (widthDepth - depthPosX - 1);
2313#endif
2314        Int maxV = 0;
2315        for (Int blockj = 0; blockj < H_3D_VSP_BLOCKSIZE; blockj+=(H_3D_VSP_BLOCKSIZE-1))
2316        {
2317          Int iX = 0;
2318          for (Int blocki = 0; blocki < H_3D_VSP_BLOCKSIZE; blocki+=(H_3D_VSP_BLOCKSIZE-1))
2319          {
2320            if (maxV < depthTmp[iX])
2321              maxV = depthTmp[iX];
2322#if H_3D_VSP_BLOCKSIZE == 2
2323            if (depthPosX + (i<<1) + blocki < widthDepth - 1)
2324#else // H_3D_VSP_BLOCKSIZE == 4
2325            if (depthPosX + (i<<2) + blocki < widthDepth - 1)
2326#endif
2327              iX = (H_3D_VSP_BLOCKSIZE-1);
2328          }
2329#if H_3D_VSP_BLOCKSIZE == 2
2330          if (depthPosY + (j<<1) + blockj < heightDepth - 1)
2331#else // H_3D_VSP_BLOCKSIZE == 4
2332          if (depthPosY + (j<<2) + blockj < heightDepth - 1)
2333#endif
2334            depthTmp += depStride * (H_3D_VSP_BLOCKSIZE-1);
2335        }
2336        m_pDepthBlock[i+j*dW] = maxV;
2337      } // end of i < dW
2338#if H_3D_VSP_BLOCKSIZE == 2
2339      if (depthPosY + ((j+1)<<1) < heightDepth)
2340        depthi += (depStride << 1);
2341      else
2342        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
2343#endif
2344#if H_3D_VSP_BLOCKSIZE == 4
2345      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
2346        depthi += (depStride << 2);
2347      else
2348        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
2349#endif
2350    }
2351  }
2352#endif // H_3D_VSP_BLOCKSIZE != 1
2353
2354#if H_3D_VSP_BLOCKSIZE == 1
2355#if H_3D_VSP_CONSTRAINED
2356  //get LUT based horizontal reference range
2357  Int range = xGetConstrainedSize(sizeX, sizeY);
2358
2359  // The minimum depth value
2360  Int minRelativePos = MAX_INT;
2361  Int maxRelativePos = MIN_INT;
2362
2363  Pel* depthTemp, *depthInitial=depth;
2364  for (Int yTxt = 0; yTxt < sizeY; yTxt++)
2365  {
2366    for (Int xTxt = 0; xTxt < sizeX; xTxt++)
2367    {
2368      if (depthPosX+xTxt < widthDepth)
2369        depthTemp = depthInitial + xTxt;
2370      else
2371        depthTemp = depthInitial + (widthDepth - depthPosX - 1);
2372
2373      Int disparity = pShiftLUT[ *depthTemp ]; // << iShiftPrec;
2374      Int disparityInt = disparity >> 2;
2375
2376      if( disparity <= 0)
2377      {
2378        if (minRelativePos > disparityInt+xTxt)
2379            minRelativePos = disparityInt+xTxt;
2380      }
2381      else
2382      {
2383        if (maxRelativePos < disparityInt+xTxt)
2384            maxRelativePos = disparityInt+xTxt;
2385      }
2386    }
2387    if (depthPosY+yTxt < heightDepth)
2388      depthInitial = depthInitial + depStride;
2389  }
2390
2391  Int disparity_tmp = pShiftLUT[ *depth ]; // << iShiftPrec;
2392  if (disparity_tmp <= 0)
2393    maxRelativePos = minRelativePos + range -1 ;
2394  else
2395    minRelativePos = maxRelativePos - range +1 ;
2396#endif
2397#endif // H_3D_VSP_BLOCKSIZE == 1
2398
2399#if H_3D_VSP_BLOCKSIZE != 1
2400  Int yDepth = 0;
2401#endif
2402  for ( Int yTxt = 0; yTxt < sizeY; yTxt += nTxtPerDepthY )
2403  {
2404    for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth++ )
2405    {
2406      Pel repDepth = 0; // to store the depth value used for warping
2407#if H_3D_VSP_BLOCKSIZE == 1
2408      repDepth = depth[xDepth];
2409#endif
2410#if H_3D_VSP_BLOCKSIZE == 2
2411      repDepth = m_pDepthBlock[(xTxt>>1) + (yTxt>>1)*dW];
2412#endif
2413#if H_3D_VSP_BLOCKSIZE == 4
2414      repDepth = m_pDepthBlock[(xTxt>>2) + (yTxt>>2)*dW];
2415#endif
2416
2417      assert( repDepth >= 0 && repDepth <= 255 );
2418      Int disparity = pShiftLUT[ repDepth ]; // remove << iShiftPrec ??
2419      Int refOffset = xTxt + (disparity >> 2);
2420      Int xFrac = disparity & 0x3;
2421#if H_3D_VSP_CONSTRAINED
2422      if(refOffset<minRelativePos || refOffset>maxRelativePos)
2423        xFrac = 0;
2424      refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
2425#endif
2426      Int absX  = posX + refOffset;
2427
2428      if (xFrac == 0)
2429        absX = Clip3(0, widthLuma-1, absX);
2430      else
2431        absX = Clip3(4, widthLuma-5, absX);
2432
2433      refOffset = absX - posX;
2434
2435      assert( ref[refOffset] >= 0 && ref[refOffset]<= 255 );
2436      m_if.filterHorLuma( &ref[refOffset], refStride, &dst[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi );
2437    }
2438    ref   += refStride*nTxtPerDepthY;
2439    dst   += dstStride*nTxtPerDepthY;
2440    depth += depStride;
2441#if H_3D_VSP_BLOCKSIZE != 1
2442    yDepth++;
2443#endif
2444
2445  }
2446}
2447
2448Void TComPrediction::xPredInterChromaBlkFromDM ( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, TComMv*dv, UInt partAddr, Int posX, Int posY
2449                                               , Int sizeX, Int sizeY, Bool isDepth, TComYuv *&dstPic, Bool bi)
2450{
2451  Int refStride = refPic->getCStride();
2452  Int dstStride = dstPic->getCStride();
2453  Int depStride = pPicBaseDepth->getStride();
2454
2455  Int widthChroma, heightChroma;
2456  if( isDepth)
2457  {
2458     widthChroma   = pPicBaseDepth->getWidth()>>1;
2459     heightChroma  = pPicBaseDepth->getHeight()>>1;
2460  }
2461  else
2462  {
2463     widthChroma   = refPic->getWidth()>>1;
2464     heightChroma  = refPic->getHeight()>>1;
2465  }
2466
2467  // Below is only for Texture chroma component
2468
2469  Int widthDepth  = pPicBaseDepth->getWidth();
2470  Int heightDepth = pPicBaseDepth->getHeight();
2471
2472  Int nTxtPerDepthX, nTxtPerDepthY;  // Number of texture samples per one depth sample
2473  Int nDepthPerTxtX, nDepthPerTxtY;  // Number of depth samples per one texture sample
2474
2475  Int depthPosX;  // Starting position in depth image
2476  Int depthPosY;
2477
2478  if ( widthChroma > widthDepth )
2479  {
2480    nTxtPerDepthX = widthChroma / widthDepth;
2481    nDepthPerTxtX = 1;
2482    depthPosX = posX / nTxtPerDepthX + ((dv->getHor()+2)>>2);
2483  }
2484  else
2485  {
2486    nTxtPerDepthX = 1;
2487    nDepthPerTxtX = widthDepth / widthChroma;
2488    depthPosX = posX * nDepthPerTxtX + ((dv->getHor()+2)>>2);
2489  }
2490  depthPosX = Clip3(0, widthDepth - (sizeX<<1), depthPosX);
2491  if ( heightChroma > heightDepth )
2492  {
2493    nTxtPerDepthY = heightChroma / heightDepth;
2494    nDepthPerTxtY = 1;
2495    depthPosY = posY / nTxtPerDepthY + ((dv->getVer()+2)>>2);
2496  }
2497  else
2498  {
2499    nTxtPerDepthY = 1;
2500    nDepthPerTxtY = heightDepth / heightChroma;
2501    depthPosY = posY * nDepthPerTxtY + ((dv->getVer()+2)>>2);
2502  }
2503  depthPosY = Clip3(0, heightDepth - (sizeY<<1), depthPosY);
2504
2505  Pel *refCb  = refPic->getCbAddr() + posX + posY * refStride;
2506  Pel *refCr  = refPic->getCrAddr() + posX + posY * refStride;
2507  Pel *dstCb  = dstPic->getCbAddr(partAddr);
2508  Pel *dstCr  = dstPic->getCrAddr(partAddr);
2509  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;  // move the pointer to the current depth pixel position
2510
2511  Int refStrideBlock = refStride * nTxtPerDepthY;
2512  Int dstStrideBlock = dstStride * nTxtPerDepthY;
2513  Int depStrideBlock = depStride * nDepthPerTxtY;
2514
2515  if ( widthChroma > widthDepth ) // We assume
2516  {
2517    assert( heightChroma > heightDepth );
2518    printf("This branch should never been reached.\n");
2519    exit(0);
2520  }
2521  else
2522  {
2523#if H_3D_VSP_BLOCKSIZE == 1
2524  Int  dW = sizeX;
2525  Int  dH = sizeY;
2526  Int  sW = 2; // search window size
2527  Int  sH = 2;
2528#endif
2529#if H_3D_VSP_BLOCKSIZE == 2
2530  Int  dW = sizeX;
2531  Int  dH = sizeY;
2532  Int  sW = 2; // search window size
2533  Int  sH = 2;
2534#endif
2535#if H_3D_VSP_BLOCKSIZE == 4
2536  Int  dW = sizeX>>1;
2537  Int  dH = sizeY>>1;
2538  Int  sW = 4; // search window size
2539  Int  sH = 4;
2540#endif
2541
2542  {
2543    Pel* depthi = depth;
2544    for (Int j = 0; j < dH; j++)
2545    {
2546      for (Int i = 0; i < dW; i++)
2547      {
2548        Pel* depthTmp;
2549#if H_3D_VSP_BLOCKSIZE == 1
2550        depthTmp = depthi + (i << 1);
2551#endif
2552#if H_3D_VSP_BLOCKSIZE == 2
2553        if (depthPosX + (i<<1) < widthDepth)
2554          depthTmp = depthi + (i << 1);
2555        else
2556          depthTmp = depthi + (widthDepth - depthPosX - 1);
2557#endif
2558#if H_3D_VSP_BLOCKSIZE == 4
2559        if (depthPosX + (i<<2) < widthDepth)
2560          depthTmp = depthi + (i << 2);
2561        else
2562          depthTmp = depthi + (widthDepth - depthPosX - 1);
2563#endif
2564        Int maxV = 0;
2565        for (Int blockj = 0; blockj < sH; blockj+=(sH-1))
2566        {
2567          Int iX = 0;
2568          for (Int blocki = 0; blocki < sW; blocki+=(sW-1))
2569          {
2570            if (maxV < depthTmp[iX])
2571              maxV = depthTmp[iX];
2572            if (depthPosX + i*sW + blocki < widthDepth - 1)
2573                iX = (sW-1);
2574          }
2575          if (depthPosY + j*sH + blockj < heightDepth - 1)
2576                depthTmp += depStride * (sH-1);
2577        }
2578        m_pDepthBlock[i+j*dW] = maxV;
2579      } // end of i < dW
2580#if H_3D_VSP_BLOCKSIZE == 1
2581      if (depthPosY + ((j+1)<<1) < heightDepth)
2582        depthi += (depStride << 1);
2583      else
2584        depthi  = depth + (heightDepth-1)*depStride;
2585#endif
2586#if H_3D_VSP_BLOCKSIZE == 2
2587      if (depthPosY + ((j+1)<<1) < heightDepth)
2588        depthi += (depStride << 1);
2589      else
2590        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
2591#endif
2592#if H_3D_VSP_BLOCKSIZE == 4
2593      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
2594        depthi += (depStride << 2);
2595      else
2596        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
2597#endif
2598    }
2599  }
2600
2601
2602#if H_3D_VSP_BLOCKSIZE == 1
2603#if H_3D_VSP_CONSTRAINED
2604  //get LUT based horizontal reference range
2605  Int range = xGetConstrainedSize(sizeX, sizeY, false);
2606
2607  // The minimum depth value
2608  Int minRelativePos = MAX_INT;
2609  Int maxRelativePos = MIN_INT;
2610
2611  Int depthTmp;
2612  for (Int yTxt=0; yTxt<sizeY; yTxt++)
2613  {
2614    for (Int xTxt=0; xTxt<sizeX; xTxt++)
2615    {
2616      depthTmp = m_pDepthBlock[xTxt+yTxt*dW];
2617      Int disparity = pShiftLUT[ depthTmp ]; // << iShiftPrec;
2618      Int disparityInt = disparity >> 3;//in chroma resolution
2619
2620      if (disparityInt < 0)
2621      {
2622        if (minRelativePos > disparityInt+xTxt)
2623            minRelativePos = disparityInt+xTxt;
2624      }
2625      else
2626      {
2627        if (maxRelativePos < disparityInt+xTxt)
2628            maxRelativePos = disparityInt+xTxt;
2629      }
2630    }
2631  }
2632
2633  depthTmp = m_pDepthBlock[0];
2634  Int disparity_tmp = pShiftLUT[ depthTmp ]; // << iShiftPrec;
2635  if ( disparity_tmp < 0 )
2636    maxRelativePos = minRelativePos + range - 1;
2637  else
2638    minRelativePos = maxRelativePos - range + 1;
2639
2640#endif // H_3D_VSP_CONSTRAINED
2641#endif // H_3D_VSP_BLOCKSIZE == 1
2642
2643    // (sizeX, sizeY) is Chroma block size
2644    for ( Int yTxt = 0, yDepth = 0; yTxt < sizeY; yTxt += nTxtPerDepthY, yDepth += nDepthPerTxtY )
2645    {
2646      for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth += nDepthPerTxtX )
2647      {
2648        Pel repDepth = 0; // to store the depth value used for warping
2649#if H_3D_VSP_BLOCKSIZE == 1
2650        repDepth = m_pDepthBlock[(xTxt) + (yTxt)*dW];
2651#endif
2652#if H_3D_VSP_BLOCKSIZE == 2
2653        repDepth = m_pDepthBlock[(xTxt) + (yTxt)*dW];
2654#endif
2655#if H_3D_VSP_BLOCKSIZE == 4
2656        repDepth = m_pDepthBlock[(xTxt>>1) + (yTxt>>1)*dW];
2657#endif
2658
2659      // calculate the offset in the reference picture
2660        Int disparity = pShiftLUT[ repDepth ]; // Remove << iShiftPrec;
2661        Int refOffset = xTxt + (disparity >> 3); // in integer pixel in chroma image
2662        Int xFrac = disparity & 0x7;
2663#if H_3D_VSP_CONSTRAINED
2664        if(refOffset < minRelativePos || refOffset > maxRelativePos)
2665          xFrac = 0;
2666        refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
2667#endif
2668        Int absX  = posX + refOffset;
2669
2670        if (xFrac == 0)
2671          absX = Clip3(0, widthChroma-1, absX);
2672        else
2673          absX = Clip3(4, widthChroma-5, absX);
2674
2675        refOffset = absX - posX;
2676
2677        assert( refCb[refOffset] >= 0 && refCb[refOffset]<= 255 );
2678        assert( refCr[refOffset] >= 0 && refCr[refOffset]<= 255 );
2679        m_if.filterHorChroma(&refCb[refOffset], refStride, &dstCb[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
2680        m_if.filterHorChroma(&refCr[refOffset], refStride, &dstCr[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
2681      }
2682      refCb += refStrideBlock;
2683      refCr += refStrideBlock;
2684      dstCb += dstStrideBlock;
2685      dstCr += dstStrideBlock;
2686      depth += depStrideBlock;
2687    }
2688  }
2689
2690}
2691
2692#endif // NTT_VSP_COMMON_E0207_E0208
2693
2694#if H_3D_VSP_CONSTRAINED
2695Int TComPrediction::xGetConstrainedSize(Int nPbW, Int nPbH, Bool bLuma)
2696{
2697  Int iSize = 0;
2698  if (bLuma)
2699  {
2700    Int iArea = (nPbW+7) * (nPbH+7);
2701    Int iAlpha = iArea / nPbH - nPbW - 7;
2702    iSize = iAlpha + nPbW;
2703  }
2704  else // chroma
2705  {
2706    Int iArea = (nPbW+2) * (nPbH+2);
2707    Int iAlpha = iArea / nPbH - nPbW - 4;
2708    iSize = iAlpha + nPbW;
2709  }
2710  return iSize;
2711}
2712#endif // H_3D_VSP_CONSTRAINED
2713
2714#endif // H_3D_VSP
2715
2716#if H_3D_DIM
2717Void TComPrediction::xPredBiSegDCs( Int* ptrSrc, UInt srcStride, Bool* biSegPattern, Int patternStride, Pel& predDC1, Pel& predDC2 )
2718{
2719  Int  refDC1, refDC2;
2720  const Int  iTR = (   patternStride - 1        ) - srcStride;
2721  const Int  iTM = ( ( patternStride - 1 ) >> 1 ) - srcStride;
2722  const Int  iLB = (   patternStride - 1        ) * srcStride - 1;
2723  const Int  iLM = ( ( patternStride - 1 ) >> 1 ) * srcStride - 1;
2724
2725  Bool bL = ( biSegPattern[0] != biSegPattern[(patternStride-1)*patternStride] );
2726  Bool bT = ( biSegPattern[0] != biSegPattern[(patternStride-1)]               );
2727
2728  if( bL == bT )
2729  {
2730    refDC1 = bL ? ( ptrSrc[iTR] + ptrSrc[iLB] )>>1 : 1<<( g_bitDepthY - 1 );
2731    refDC2 =      ( ptrSrc[ -1] + ptrSrc[-(Int)srcStride] )>>1;
2732  }
2733  else
2734  {
2735    refDC1 = bL ? ptrSrc[iLB] : ptrSrc[iTR];
2736    refDC2 = bL ? ptrSrc[iTM] : ptrSrc[iLM];
2737  }
2738
2739  predDC1 = biSegPattern[0] ? refDC1 : refDC2;
2740  predDC2 = biSegPattern[0] ? refDC2 : refDC1;
2741}
2742
2743Void TComPrediction::xAssignBiSegDCs( Pel* ptrDst, UInt dstStride, Bool* biSegPattern, Int patternStride, Pel valDC1, Pel valDC2 )
2744{
2745  if( dstStride == patternStride )
2746  {
2747    for( UInt k = 0; k < (patternStride * patternStride); k++ )
2748    {
2749      if( true == biSegPattern[k] ) { ptrDst[k] = valDC2; }
2750      else                          { ptrDst[k] = valDC1; }
2751    }
2752  }
2753  else
2754  {
2755    Pel* piTemp = ptrDst;
2756    for( UInt uiY = 0; uiY < patternStride; uiY++ )
2757    {
2758      for( UInt uiX = 0; uiX < patternStride; uiX++ )
2759      {
2760        if( true == biSegPattern[uiX] ) { piTemp[uiX] = valDC2; }
2761        else                            { piTemp[uiX] = valDC1; }
2762      }
2763      piTemp       += dstStride;
2764      biSegPattern += patternStride;
2765    }
2766  }
2767}
2768
2769#if H_3D_DIM_DMM
2770#if !SEC_DMM2_E0146_HHIFIX
2771UInt TComPrediction::xPredWedgeFromIntra( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, Int iDeltaEnd )
2772{
2773  UInt uiThisBlockSize = uiWidth;
2774
2775  TComDataCU* pcTempCU;
2776  UInt        uiTempPartIdx;
2777  // 1st: try continue above wedgelet
2778  pcTempCU = pcCU->getPUAbove( uiTempPartIdx, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
2779  if( pcTempCU && isDimMode( pcTempCU->getLumaIntraDir( uiTempPartIdx ) ) )
2780  {
2781    UInt dimType =  getDimType( pcTempCU->getLumaIntraDir( uiTempPartIdx ) );
2782    if( DMM1_IDX == dimType || DMM2_IDX == dimType || DMM3_IDX == dimType )
2783    {
2784      // get offset between current and reference block
2785      UInt uiOffsetX = 0, uiOffsetY = 0;
2786      xGetBlockOffset( pcCU, uiAbsPartIdx, pcTempCU, uiTempPartIdx, uiOffsetX, uiOffsetY );
2787
2788      // get reference wedgelet
2789      WedgeList* pacWedgeList = &g_dmmWedgeLists[(g_aucConvertToBit[(pcTempCU->getWidth( uiTempPartIdx )>>((pcTempCU->getPartitionSize( uiTempPartIdx ) == SIZE_NxN) ? 1 : 0))])];
2790      TComWedgelet* pcRefWedgelet = &(pacWedgeList->at( pcTempCU->getDmmWedgeTabIdx( dimType, uiTempPartIdx ) ) );
2791
2792      // find wedgelet, if direction is suitable for continue wedge
2793      if( pcRefWedgelet->checkPredDirAbovePossible( uiThisBlockSize, uiOffsetX ) )
2794      {
2795        UChar uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye;
2796        pcRefWedgelet->getPredDirStartEndAbove( uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye, uiThisBlockSize, uiOffsetX, iDeltaEnd );
2797        return xGetWedgePatternIdx( uiThisBlockSize, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye );
2798      }
2799    }
2800  }
2801
2802  // 2nd: try continue left wedglelet
2803  pcTempCU = pcCU->getPULeft( uiTempPartIdx, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
2804  if( pcTempCU && isDimMode( pcTempCU->getLumaIntraDir( uiTempPartIdx ) ) )
2805  {
2806    UInt dimType = getDimType( pcTempCU->getLumaIntraDir( uiTempPartIdx ) );
2807    if( DMM1_IDX == dimType || DMM2_IDX == dimType || DMM3_IDX == dimType )
2808    {
2809      // get offset between current and reference block
2810      UInt uiOffsetX = 0, uiOffsetY = 0;
2811      xGetBlockOffset( pcCU, uiAbsPartIdx, pcTempCU, uiTempPartIdx, uiOffsetX, uiOffsetY );
2812
2813      // get reference wedgelet
2814      WedgeList* pacWedgeList = &g_dmmWedgeLists[(g_aucConvertToBit[(pcTempCU->getWidth( uiTempPartIdx )>>((pcTempCU->getPartitionSize( uiTempPartIdx ) == SIZE_NxN) ? 1 : 0))])];
2815      TComWedgelet* pcRefWedgelet = &(pacWedgeList->at( pcTempCU->getDmmWedgeTabIdx( dimType, uiTempPartIdx ) ) );
2816
2817      // find wedgelet, if direction is suitable for continue wedge
2818      if( pcRefWedgelet->checkPredDirLeftPossible( uiThisBlockSize, uiOffsetY ) )
2819      {
2820        UChar uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye;
2821        pcRefWedgelet->getPredDirStartEndLeft( uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye, uiThisBlockSize, uiOffsetY, iDeltaEnd );
2822        return xGetWedgePatternIdx( uiThisBlockSize, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye );
2823      }
2824    }
2825  }
2826
2827  // 3rd: (default) make wedglet from intra dir and max slope point
2828  Int iSlopeX = 0, iSlopeY = 0;
2829  UInt uiStartPosX = 0, uiStartPosY = 0;
2830  if( xGetWedgeIntraDirPredData( pcCU, uiAbsPartIdx, uiThisBlockSize, iSlopeX, iSlopeY, uiStartPosX, uiStartPosY ) )
2831  {
2832    UChar uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye;
2833    xGetWedgeIntraDirStartEnd( pcCU, uiAbsPartIdx, uiThisBlockSize, iSlopeX, iSlopeY, uiStartPosX, uiStartPosY, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye, iDeltaEnd );
2834    return xGetWedgePatternIdx( uiThisBlockSize, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye );
2835  }
2836
2837  return 0;
2838}
2839#endif
2840
2841UInt TComPrediction::xPredWedgeFromTex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt intraTabIdx )
2842{
2843  TComPic*      pcPicTex = pcCU->getSlice()->getTexturePic();
2844  assert( pcPicTex != NULL );
2845  TComDataCU*   pcColTexCU = pcPicTex->getCU(pcCU->getAddr());
2846  UInt          uiTexPartIdx = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
2847  Int           uiColTexIntraDir = pcColTexCU->isIntra( uiTexPartIdx ) ? pcColTexCU->getLumaIntraDir( uiTexPartIdx ) : 255;
2848
2849#if LGE_PKU_DMM3_OVERLAP_E0159_HHIFIX
2850  assert( uiColTexIntraDir > DC_IDX && uiColTexIntraDir < 35 );
2851  return g_aauiWdgLstM3[g_aucConvertToBit[uiWidth]][uiColTexIntraDir-2].at(intraTabIdx);
2852#else
2853  if( uiColTexIntraDir > DC_IDX && uiColTexIntraDir < 35 ) { return g_aauiWdgLstM3[g_aucConvertToBit[uiWidth]][uiColTexIntraDir-2].at(intraTabIdx); }
2854  else                                                     { return g_dmmWedgeNodeLists[(g_aucConvertToBit[uiWidth])].at(intraTabIdx).getPatternIdx(); }
2855#endif
2856}
2857
2858Void TComPrediction::xPredContourFromTex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, TComWedgelet* pcContourWedge )
2859{
2860  pcContourWedge->clear();
2861
2862  // get copy of co-located texture luma block
2863  TComYuv cTempYuv;
2864  cTempYuv.create( uiWidth, uiHeight ); 
2865  cTempYuv.clear();
2866  Pel* piRefBlkY = cTempYuv.getLumaAddr();
2867  xCopyTextureLumaBlock( pcCU, uiAbsPartIdx, piRefBlkY, uiWidth, uiHeight );
2868  piRefBlkY = cTempYuv.getLumaAddr();
2869
2870  // find contour for texture luma block
2871  UInt iDC = 0;
2872  for( UInt k = 0; k < (uiWidth*uiHeight); k++ ) 
2873  { 
2874    iDC += piRefBlkY[k]; 
2875  }
2876
2877#if SCU_HS_DMM4_REMOVE_DIV_E0242
2878  Int cuMaxLog2Size = g_aucConvertToBit[g_uiMaxCUWidth]+2;
2879  iDC = iDC >> (cuMaxLog2Size - pcCU->getDepth(0))*2;
2880#else
2881  iDC /= (uiWidth*uiHeight);
2882#endif
2883  piRefBlkY = cTempYuv.getLumaAddr();
2884
2885  Bool* pabContourPattern = pcContourWedge->getPattern();
2886  for( UInt k = 0; k < (uiWidth*uiHeight); k++ ) 
2887  { 
2888    pabContourPattern[k] = (piRefBlkY[k] > iDC) ? true : false;
2889  }
2890
2891  cTempYuv.destroy();
2892}
2893
2894
2895Void TComPrediction::xCopyTextureLumaBlock( TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piDestBlockY, UInt uiWidth, UInt uiHeight )
2896{
2897  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getTexturePic()->getPicYuvRec();
2898  assert( pcPicYuvRef != NULL );
2899  Int         iRefStride = pcPicYuvRef->getStride();
2900  Pel*        piRefY = pcPicYuvRef->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiAbsPartIdx );
2901
2902  for ( Int y = 0; y < uiHeight; y++ )
2903  {
2904    ::memcpy(piDestBlockY, piRefY, sizeof(Pel)*uiWidth);
2905    piDestBlockY += uiWidth;
2906    piRefY += iRefStride;
2907  }
2908}
2909
2910#if !SEC_DMM2_E0146_HHIFIX
2911Void TComPrediction::xGetBlockOffset( TComDataCU* pcCU, UInt uiAbsPartIdx, TComDataCU* pcRefCU, UInt uiRefAbsPartIdx, UInt& ruiOffsetX, UInt& ruiOffsetY )
2912{
2913  ruiOffsetX = 0;
2914  ruiOffsetY = 0;
2915
2916  // get offset between current and above/left block
2917  UInt uiThisOriginX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
2918  UInt uiThisOriginY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
2919
2920  UInt uiNumPartInRefCU = pcRefCU->getTotalNumPart();
2921  UInt uiMaxDepthRefCU = 0;
2922  while( uiNumPartInRefCU > 1 )
2923  {
2924    uiNumPartInRefCU >>= 2;
2925    uiMaxDepthRefCU++;
2926  }
2927
2928  UInt uiDepthRefPU = (pcRefCU->getDepth(uiRefAbsPartIdx)) + (pcRefCU->getPartitionSize(uiRefAbsPartIdx) == SIZE_2Nx2N ? 0 : 1);
2929  UInt uiShifts = (uiMaxDepthRefCU - uiDepthRefPU)*2;
2930  UInt uiRefBlockOriginPartIdx = (uiRefAbsPartIdx>>uiShifts)<<uiShifts;
2931
2932  UInt uiRefOriginX = pcRefCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiRefBlockOriginPartIdx] ];
2933  UInt uiRefOriginY = pcRefCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiRefBlockOriginPartIdx] ];
2934
2935  if( (uiThisOriginX - uiRefOriginX) > 0 ) { ruiOffsetX = (UInt)(uiThisOriginX - uiRefOriginX); }
2936  if( (uiThisOriginY - uiRefOriginY) > 0 ) { ruiOffsetY = (UInt)(uiThisOriginY - uiRefOriginY); }
2937}
2938
2939Bool TComPrediction::xGetWedgeIntraDirPredData( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiBlockSize, Int& riSlopeX, Int& riSlopeY, UInt& ruiStartPosX, UInt& ruiStartPosY )
2940{
2941  riSlopeX = 0, riSlopeY = 0, ruiStartPosX = 0, ruiStartPosY = 0;
2942
2943  // 1st step: get wedge start point (max. slope)
2944  Int* piSource = pcCU->getPattern()->getAdiOrgBuf( uiBlockSize, uiBlockSize, m_piYuvExt );
2945  Int iSourceStride = ( uiBlockSize<<1 ) + 1;
2946
2947  UInt uiSlopeMaxAbove = 0, uiPosSlopeMaxAbove = 0;
2948  for( UInt uiPosHor = 0; uiPosHor < (uiBlockSize-1); uiPosHor++ )
2949  {
2950    if( abs( piSource[uiPosHor+1] - piSource[uiPosHor] ) > uiSlopeMaxAbove )
2951    {
2952      uiSlopeMaxAbove = abs( piSource[uiPosHor+1] - piSource[uiPosHor] );
2953      uiPosSlopeMaxAbove = uiPosHor;
2954    }
2955  }
2956
2957  UInt uiSlopeMaxLeft = 0, uiPosSlopeMaxLeft = 0;
2958  for( UInt uiPosVer = 0; uiPosVer < (uiBlockSize-1); uiPosVer++ )
2959  {
2960    if( abs( piSource[(uiPosVer+1)*iSourceStride] - piSource[uiPosVer*iSourceStride] ) > uiSlopeMaxLeft )
2961    {
2962      uiSlopeMaxLeft = abs( piSource[(uiPosVer+1)*iSourceStride] - piSource[uiPosVer*iSourceStride] );
2963      uiPosSlopeMaxLeft = uiPosVer;
2964    }
2965  }
2966
2967  if( uiSlopeMaxAbove == 0 && uiSlopeMaxLeft == 0 ) 
2968  { 
2969    return false; 
2970  }
2971
2972  ruiStartPosX = ( uiSlopeMaxAbove >  uiSlopeMaxLeft  ) ? uiPosSlopeMaxAbove : 0;
2973  ruiStartPosY = ( uiSlopeMaxLeft  >= uiSlopeMaxAbove ) ? uiPosSlopeMaxLeft  : 0;
2974
2975  // 2nd step: derive wedge direction
2976  Int uiPreds[3] = {-1, -1, -1};
2977  Int iMode = -1;
2978  Int iPredNum = pcCU->getIntraDirLumaPredictor( uiAbsPartIdx, uiPreds, &iMode ); 
2979
2980  UInt uiDirMode = 0;
2981  if( iMode >= 0 ) { iPredNum = iMode; }
2982  if( iPredNum == 1 ) { uiDirMode = uiPreds[0]; }
2983  if( iPredNum == 2 ) { uiDirMode = uiPreds[1]; }
2984
2985  if( uiDirMode < 2 ) { return false; } // no planar & DC
2986
2987  Bool modeHor       = (uiDirMode < 18);
2988  Bool modeVer       = !modeHor;
2989  Int intraPredAngle = modeVer ? (Int)uiDirMode - VER_IDX : modeHor ? -((Int)uiDirMode - HOR_IDX) : 0;
2990  Int absAng         = abs(intraPredAngle);
2991  Int signAng        = intraPredAngle < 0 ? -1 : 1;
2992  Int angTable[9]    = {0,2,5,9,13,17,21,26,32};
2993  absAng             = angTable[absAng];
2994  intraPredAngle     = signAng * absAng;
2995
2996  // 3rd step: set slope for direction
2997  if( modeHor )
2998  {
2999    riSlopeX = ( intraPredAngle > 0 ) ?            -32 :              32;
3000    riSlopeY = ( intraPredAngle > 0 ) ? intraPredAngle : -intraPredAngle;
3001  }
3002  else if( modeVer )
3003  {
3004    riSlopeX = ( intraPredAngle > 0 ) ? intraPredAngle : -intraPredAngle;
3005    riSlopeY = ( intraPredAngle > 0 ) ?            -32 :              32;
3006  }
3007
3008  return true;
3009}
3010
3011Void TComPrediction::xGetWedgeIntraDirStartEnd( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiBlockSize, Int iDeltaX, Int iDeltaY, UInt uiPMSPosX, UInt uiPMSPosY, UChar& ruhXs, UChar& ruhYs, UChar& ruhXe, UChar& ruhYe, Int iDeltaEnd )
3012{
3013  ruhXs = 0;
3014  ruhYs = 0;
3015  ruhXe = 0;
3016  ruhYe = 0;
3017
3018  // scaling of start pos and block size to wedge resolution
3019  UInt uiScaledStartPosX = 0;
3020  UInt uiScaledStartPosY = 0;
3021  UInt uiScaledBlockSize = 0;
3022  WedgeResolution eWedgeRes = g_dmmWedgeResolution[(UInt)g_aucConvertToBit[uiBlockSize]];
3023  switch( eWedgeRes )
3024  {
3025  case( DOUBLE_PEL ): { uiScaledStartPosX = (uiPMSPosX>>1); uiScaledStartPosY = (uiPMSPosY>>1); uiScaledBlockSize = (uiBlockSize>>1); break; }
3026  case(   FULL_PEL ): { uiScaledStartPosX =  uiPMSPosX;     uiScaledStartPosY =  uiPMSPosY;     uiScaledBlockSize =  uiBlockSize;     break; }
3027  case(   HALF_PEL ): { uiScaledStartPosX = (uiPMSPosX<<1); uiScaledStartPosY = (uiPMSPosY<<1); uiScaledBlockSize = (uiBlockSize<<1); break; }
3028  }
3029  Int iMaxPos = (Int)uiScaledBlockSize - 1;
3030
3031  // case above
3032  if( uiScaledStartPosX > 0 && uiScaledStartPosY == 0 )
3033  {
3034    ruhXs = (UChar)uiScaledStartPosX;
3035    ruhYs = 0;
3036
3037    if( iDeltaY == 0 )
3038    {
3039      if( iDeltaX < 0 )
3040      {
3041        ruhXe = 0;
3042        ruhYe = (UChar)std::min( std::max( iDeltaEnd, 0 ), iMaxPos );
3043        return;
3044      }
3045      else
3046      {
3047        ruhXe = (UChar)iMaxPos;
3048        ruhYe = (UChar)std::min( std::max( -iDeltaEnd, 0 ), iMaxPos );
3049        std::swap( ruhXs, ruhXe );
3050        std::swap( ruhYs, ruhYe );
3051        return;
3052      }
3053    }
3054
3055    // regular case
3056    Int iVirtualEndX = (Int)ruhXs + roftoi( (Double)iMaxPos * ((Double)iDeltaX / (Double)iDeltaY) );
3057
3058    if( iVirtualEndX < 0 )
3059    {
3060      Int iYe = roftoi( (Double)(0 - (Int)ruhXs) * ((Double)iDeltaY / (Double)iDeltaX) ) + iDeltaEnd;
3061      if( iYe < (Int)uiScaledBlockSize )
3062      {
3063        ruhXe = 0;
3064        ruhYe = (UChar)std::max( iYe, 0 );
3065        return;
3066      }
3067      else
3068      {
3069        ruhXe = (UChar)std::min( (iYe - iMaxPos), iMaxPos );
3070        ruhYe = (UChar)iMaxPos;
3071        return;
3072      }
3073    }
3074    else if( iVirtualEndX > iMaxPos )
3075    {
3076      Int iYe = roftoi( (Double)(iMaxPos - (Int)ruhXs) * ((Double)iDeltaY / (Double)iDeltaX) ) - iDeltaEnd;
3077      if( iYe < (Int)uiScaledBlockSize )
3078      {
3079        ruhXe = (UChar)iMaxPos;
3080        ruhYe = (UChar)std::max( iYe, 0 );
3081        std::swap( ruhXs, ruhXe );
3082        std::swap( ruhYs, ruhYe );
3083        return;
3084      }
3085      else
3086      {
3087        ruhXe = (UChar)std::max( (iMaxPos - (iYe - iMaxPos)), 0 );
3088        ruhYe = (UChar)iMaxPos;
3089        return;
3090      }
3091    }
3092    else
3093    {
3094      Int iXe = iVirtualEndX + iDeltaEnd;
3095      if( iXe < 0 )
3096      {
3097        ruhXe = 0;
3098        ruhYe = (UChar)std::max( (iMaxPos + iXe), 0 );
3099        return;
3100      }
3101      else if( iXe > iMaxPos )
3102      {
3103        ruhXe = (UChar)iMaxPos;
3104        ruhYe = (UChar)std::max( (iMaxPos - (iXe - iMaxPos)), 0 );
3105        std::swap( ruhXs, ruhXe );
3106        std::swap( ruhYs, ruhYe );
3107        return;
3108      }
3109      else
3110      {
3111        ruhXe = (UChar)iXe;
3112        ruhYe = (UChar)iMaxPos;
3113        return;
3114      }
3115    }
3116  }
3117
3118  // case left
3119  if( uiScaledStartPosY > 0 && uiScaledStartPosX == 0 )
3120  {
3121    ruhXs = 0;
3122    ruhYs = (UChar)uiScaledStartPosY;
3123
3124    if( iDeltaX == 0 )
3125    {
3126      if( iDeltaY < 0 )
3127      {
3128        ruhXe = (UChar)std::min( std::max( -iDeltaEnd, 0 ), iMaxPos );
3129        ruhYe = 0;
3130        std::swap( ruhXs, ruhXe );
3131        std::swap( ruhYs, ruhYe );
3132        return;
3133      }
3134      else
3135      {
3136        ruhXe = (UChar)std::min( std::max( iDeltaEnd, 0 ), iMaxPos );
3137        ruhYe = (UChar)iMaxPos;
3138        return; 
3139      }
3140    }
3141
3142    // regular case
3143    Int iVirtualEndY = (Int)ruhYs + roftoi( (Double)iMaxPos * ((Double)iDeltaY / (Double)iDeltaX) );
3144
3145    if( iVirtualEndY < 0 )
3146    {
3147      Int iXe = roftoi( (Double)(0 - (Int)ruhYs ) * ((Double)iDeltaX / (Double)iDeltaY) ) - iDeltaEnd;
3148      if( iXe < (Int)uiScaledBlockSize )
3149      {
3150        ruhXe = (UChar)std::max( iXe, 0 );
3151        ruhYe = 0;
3152        std::swap( ruhXs, ruhXe );
3153        std::swap( ruhYs, ruhYe );
3154        return;
3155      }
3156      else
3157      {
3158        ruhXe = (UChar)iMaxPos;
3159        ruhYe = (UChar)std::min( (iXe - iMaxPos), iMaxPos );
3160        std::swap( ruhXs, ruhXe );
3161        std::swap( ruhYs, ruhYe );
3162        return;
3163      }
3164    }
3165    else if( iVirtualEndY > (uiScaledBlockSize-1) )
3166    {
3167      Int iXe = roftoi( (Double)((Int)(uiScaledBlockSize-1) - (Int)ruhYs ) * ((Double)iDeltaX / (Double)iDeltaY) ) + iDeltaEnd;
3168      if( iXe < (Int)uiScaledBlockSize )
3169      {
3170        ruhXe = (UChar)std::max( iXe, 0 );
3171        ruhYe = (UChar)(uiScaledBlockSize-1);
3172        return;
3173      }
3174      else
3175      {
3176        ruhXe = (UChar)iMaxPos;
3177        ruhYe = (UChar)std::max( (iMaxPos - (iXe - iMaxPos)), 0 );
3178        std::swap( ruhXs, ruhXe );
3179        std::swap( ruhYs, ruhYe );
3180        return;
3181      }
3182    }
3183    else
3184    {
3185      Int iYe = iVirtualEndY - iDeltaEnd;
3186      if( iYe < 0 )
3187      {
3188        ruhXe = (UChar)std::max( (iMaxPos + iYe), 0 );
3189        ruhYe = 0;
3190        std::swap( ruhXs, ruhXe );
3191        std::swap( ruhYs, ruhYe );
3192        return;
3193      }
3194      else if( iYe > iMaxPos )
3195      {
3196        ruhXe = (UChar)std::max( (iMaxPos - (iYe - iMaxPos)), 0 );
3197        ruhYe = (UChar)iMaxPos;
3198        return;
3199      }
3200      else
3201      {
3202        ruhXe = (UChar)iMaxPos;
3203        ruhYe = (UChar)iYe;
3204        std::swap( ruhXs, ruhXe );
3205        std::swap( ruhYs, ruhYe );
3206        return;
3207      }
3208    }
3209  }
3210
3211  // case origin
3212  if( uiScaledStartPosX == 0 && uiScaledStartPosY == 0 )
3213  {
3214    if( iDeltaX*iDeltaY < 0 )
3215    {
3216      return;
3217    }
3218
3219    ruhXs = 0;
3220    ruhYs = 0;
3221
3222    if( iDeltaY == 0 )
3223    {
3224      ruhXe = (UChar)iMaxPos;
3225      ruhYe = 0;
3226      std::swap( ruhXs, ruhXe );
3227      std::swap( ruhYs, ruhYe );
3228      return;
3229    }
3230
3231    if( iDeltaX == 0 )
3232    {
3233      ruhXe = 0;
3234      ruhYe = (UChar)iMaxPos;
3235      return;
3236    }
3237
3238    Int iVirtualEndX = (Int)ruhXs + roftoi( (Double)iMaxPos * ((Double)iDeltaX / (Double)iDeltaY) );
3239
3240    if( iVirtualEndX > iMaxPos )
3241    {
3242      Int iYe = roftoi( (Double)((Int)iMaxPos - (Int)ruhXs) * ((Double)iDeltaY / (Double)iDeltaX) ) - iDeltaEnd;
3243      if( iYe < (Int)uiScaledBlockSize )
3244      {
3245        ruhXe = (UChar)(uiScaledBlockSize-1);
3246        ruhYe = (UChar)std::max( iYe, 0 );
3247        std::swap( ruhXs, ruhXe );
3248        std::swap( ruhYs, ruhYe );
3249        return;
3250      }
3251      else
3252      {
3253        ruhXe = (UChar)std::max( (iMaxPos - (iYe - iMaxPos)), 0 );
3254        ruhYe = (UChar)(uiScaledBlockSize-1);
3255        return;
3256      }
3257    }
3258    else
3259    {
3260      Int iXe = iVirtualEndX + iDeltaEnd;
3261      if( iXe < 0 )
3262      {
3263        ruhXe = 0;
3264        ruhYe = (UChar)std::max( (iMaxPos + iXe), 0 );
3265        return;
3266      }
3267      else if( iXe > iMaxPos )
3268      {
3269        ruhXe = (UChar)(uiScaledBlockSize-1);
3270        ruhYe = (UChar)std::max( (iMaxPos - (iXe - iMaxPos)), 0 );
3271        std::swap( ruhXs, ruhXe );
3272        std::swap( ruhYs, ruhYe );
3273        return;
3274      }
3275      else
3276      {
3277        ruhXe = (UChar)iXe;
3278        ruhYe = (UChar)(uiScaledBlockSize-1);
3279        return;
3280      }
3281    }
3282  }
3283}
3284
3285UInt TComPrediction::xGetWedgePatternIdx( UInt uiBlockSize, UChar uhXs, UChar uhYs, UChar uhXe, UChar uhYe )
3286{
3287  WedgeRefList* pcWedgeRefList = &g_dmmWedgeRefLists[(g_aucConvertToBit[uiBlockSize])];
3288  for( UInt uiIdx = 0; uiIdx < pcWedgeRefList->size(); uiIdx++ )
3289  {
3290    TComWedgeRef* pcTestWedgeRef = &(pcWedgeRefList->at(uiIdx));
3291    if( pcTestWedgeRef->getStartX() == uhXs && pcTestWedgeRef->getStartY() == uhYs && pcTestWedgeRef->getEndX() == uhXe && pcTestWedgeRef->getEndY() == uhYe )
3292    {
3293      return pcTestWedgeRef->getRefIdx();
3294    }
3295  }
3296  return 0;
3297}
3298#endif
3299#endif
3300#if H_3D_DIM_RBC
3301Void TComPrediction::xDeltaDCQuantScaleUp( TComDataCU* pcCU, Pel& rDeltaDC )
3302{
3303  Int  iSign  = rDeltaDC < 0 ? -1 : 1;
3304  UInt uiAbs  = abs( rDeltaDC );
3305
3306  Int iQp = pcCU->getQP(0);
3307  Double dMax = (Double)( 1<<( g_bitDepthY - 1 ) );
3308  Double dStepSize = Clip3( 1.0, dMax, pow( 2.0, iQp/10.0 - 2.0 ) );
3309
3310  rDeltaDC = iSign * roftoi( uiAbs * dStepSize );
3311  return;
3312}
3313
3314Void TComPrediction::xDeltaDCQuantScaleDown( TComDataCU*  pcCU, Pel& rDeltaDC )
3315{
3316  Int  iSign  = rDeltaDC < 0 ? -1 : 1;
3317  UInt uiAbs  = abs( rDeltaDC );
3318
3319  Int iQp = pcCU->getQP(0);
3320  Double dMax = (Double)( 1<<( g_bitDepthY - 1 ) );
3321  Double dStepSize = Clip3( 1.0, dMax, pow( 2.0, iQp/10.0 - 2.0 ) );
3322
3323  rDeltaDC = iSign * roftoi( uiAbs / dStepSize );
3324  return;
3325}
3326#endif
3327#if H_3D_DIM_SDC
3328Void TComPrediction::analyzeSegmentsSDC( Pel* pOrig, UInt uiStride, UInt uiSize, Pel* rpSegMeans, UInt uiNumSegments, Bool* pMask, UInt uiMaskStride
3329#if KWU_SDC_SIMPLE_DC_E0117
3330                                         ,UInt uiIntraMode
3331                                         ,Bool orgDC
3332#endif
3333                                        )
3334{
3335  Int iSumDepth[2];
3336  memset(iSumDepth, 0, sizeof(Int)*2);
3337  Int iSumPix[2];
3338  memset(iSumPix, 0, sizeof(Int)*2);
3339 
3340#if KWU_SDC_SIMPLE_DC_E0117
3341  if (orgDC == false)
3342  {
3343    if ( getDimType(uiIntraMode) == DMM1_IDX )
3344    {
3345      UChar ucSegmentLT = pMask[0];
3346      UChar ucSegmentRT = pMask[uiSize-1];
3347      UChar ucSegmentLB = pMask[uiMaskStride * (uiSize-1)]; 
3348      UChar ucSegmentRB = pMask[uiMaskStride * (uiSize-1) + (uiSize-1)]; 
3349
3350      rpSegMeans[ucSegmentLT] = pOrig[0];
3351      rpSegMeans[ucSegmentRT] = pOrig[uiSize-1];
3352      rpSegMeans[ucSegmentLB] = pOrig[uiStride * (uiSize-1) ];
3353      rpSegMeans[ucSegmentRB] = pOrig[uiStride * (uiSize-1) + (uiSize-1) ];
3354    }
3355    else if (uiIntraMode == PLANAR_IDX)
3356    {
3357      Pel* pLeftTop = pOrig;
3358      Pel* pRightTop = pOrig + (uiSize-1);
3359      Pel* pLeftBottom = (pOrig+ (uiStride*(uiSize-1)));
3360      Pel* pRightBottom = (pOrig+ (uiStride*(uiSize-1)) + (uiSize-1));
3361
3362      rpSegMeans[0] = (*pLeftTop + *pRightTop + *pLeftBottom + *pRightBottom + 2)>>2;
3363    }
3364    return;
3365  }
3366#endif
3367
3368  Int subSamplePix;
3369  if ( uiSize == 64 || uiSize == 32 )
3370  {
3371    subSamplePix = 2;
3372  }
3373  else
3374  {
3375    subSamplePix = 1;
3376  }
3377  for (Int y=0; y<uiSize; y+=subSamplePix)
3378  {
3379    for (Int x=0; x<uiSize; x+=subSamplePix)
3380    {
3381      UChar ucSegment = pMask?(UChar)pMask[x]:0;
3382      assert( ucSegment < uiNumSegments );
3383     
3384      iSumDepth[ucSegment] += pOrig[x];
3385      iSumPix[ucSegment]   += 1;
3386    }
3387   
3388    pOrig  += uiStride*subSamplePix;
3389    pMask  += uiMaskStride*subSamplePix;
3390  }
3391 
3392  // compute mean for each segment
3393  for( UChar ucSeg = 0; ucSeg < uiNumSegments; ucSeg++ )
3394  {
3395    if( iSumPix[ucSeg] > 0 )
3396      rpSegMeans[ucSeg] = iSumDepth[ucSeg] / iSumPix[ucSeg];
3397    else
3398      rpSegMeans[ucSeg] = 0;  // this happens for zero-segments
3399  }
3400}
3401#endif // H_3D_DIM_SDC
3402#endif
3403//! \}
Note: See TracBrowser for help on using the repository browser.