source: 3DVCSoftware/branches/HTM-6.2-dev2-MERL/source/Lib/TLibCommon/TComPrediction.cpp @ 419

Last change on this file since 419 was 419, checked in by mitsubishi-htm, 12 years ago

-macro updates.

  • Property svn:eol-style set to native
File size: 136.0 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-2012, 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//! \ingroup TLibCommon
42//! \{
43
44// ====================================================================================================================
45// Constructor / destructor / initialize
46// ====================================================================================================================
47
48#if LGE_EDGE_INTRA_A0070
49#define MAX_DISTANCE_EDGEINTRA 255
50#endif
51
52#if MERL_VSP_C0152
53#if MERL_CVSP_D0165
54Int TComPrediction::m_iRangeLuma[12]   = {14, 34, 21, 15, 36, 26, 21, 49, 41, 36, 80, 72};
55Int TComPrediction::m_iRangeChroma[12] = { 2,  8,  5,  4, 11,  9,  8, 19, 17, 15, 34, 32};
56#endif
57#endif
58
59TComPrediction::TComPrediction()
60: m_pLumaRecBuffer(0)
61{
62  m_piYuvExt = NULL;
63#if MERL_VSP_C0152
64  m_pDepth = (Int*) malloc(64*64*sizeof(Int)); // TODO: Use a smart way to determine the size of the array
65  if (m_pDepth == NULL)
66  {
67      printf("ERROR: UKTGHU, No memory allocated.\n");
68  }
69#endif
70}
71
72TComPrediction::~TComPrediction()
73{
74 
75#if MERL_VSP_C0152
76  if (m_pDepth != NULL)
77  {
78      free(m_pDepth);
79  }
80#endif
81  delete[] m_piYuvExt;
82
83  m_acYuvPred[0].destroy();
84  m_acYuvPred[1].destroy();
85
86  m_cYuvPredTemp.destroy();
87#if QC_ARP_D0177
88  m_acYuvPredBase[0].destroy();
89  m_acYuvPredBase[1].destroy();
90  m_acYuvDiff[0].destroy();
91  m_acYuvDiff[1].destroy();
92#endif
93  if( m_pLumaRecBuffer )
94  {
95    delete [] m_pLumaRecBuffer;
96  }
97 
98  Int i, j;
99  for (i = 0; i < 4; i++)
100  {
101    for (j = 0; j < 4; j++)
102    {
103      m_filteredBlock[i][j].destroy();
104    }
105    m_filteredBlockTmp[i].destroy();
106  }
107}
108
109Void TComPrediction::initTempBuff()
110{
111  if( m_piYuvExt == NULL )
112  {
113    Int extWidth  = g_uiMaxCUWidth + 16; 
114    Int extHeight = g_uiMaxCUHeight + 1;
115    Int i, j;
116    for (i = 0; i < 4; i++)
117    {
118      m_filteredBlockTmp[i].create(extWidth, extHeight + 7);
119      for (j = 0; j < 4; j++)
120      {
121        m_filteredBlock[i][j].create(extWidth, extHeight);
122      }
123    }
124    m_iYuvExtHeight  = ((g_uiMaxCUHeight + 2) << 4);
125    m_iYuvExtStride = ((g_uiMaxCUWidth  + 8) << 4);
126    m_piYuvExt = new Int[ m_iYuvExtStride * m_iYuvExtHeight ];
127
128    // new structure
129    m_acYuvPred[0] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
130    m_acYuvPred[1] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
131
132    m_cYuvPredTemp.create( g_uiMaxCUWidth, g_uiMaxCUHeight );
133#if QC_ARP_D0177
134    m_acYuvPredBase[0] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
135    m_acYuvPredBase[1] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
136    m_acYuvDiff    [0] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
137    m_acYuvDiff    [1] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
138#endif
139  }
140
141  m_iLumaRecStride =  (g_uiMaxCUWidth>>1) + 1;
142  m_pLumaRecBuffer = new Pel[ m_iLumaRecStride * m_iLumaRecStride ];
143
144  for( Int i = 1; i < 64; i++ )
145  {
146    m_uiaShift[i-1] = ( (1 << 15) + i/2 ) / i;
147  }
148}
149
150// ====================================================================================================================
151// Public member functions
152// ====================================================================================================================
153
154// Function for calculating DC value of the reference samples used in Intra prediction
155Pel TComPrediction::predIntraGetPredValDC( Int* pSrc, Int iSrcStride, UInt iWidth, UInt iHeight, Bool bAbove, Bool bLeft )
156{
157  Int iInd, iSum = 0;
158  Pel pDcVal;
159
160  if (bAbove)
161  {
162    for (iInd = 0;iInd < iWidth;iInd++)
163    {
164      iSum += pSrc[iInd-iSrcStride];
165    }
166  }
167  if (bLeft)
168  {
169    for (iInd = 0;iInd < iHeight;iInd++)
170    {
171      iSum += pSrc[iInd*iSrcStride-1];
172    }
173  }
174
175  if (bAbove && bLeft)
176  {
177    pDcVal = (iSum + iWidth) / (iWidth + iHeight);
178  }
179  else if (bAbove)
180  {
181    pDcVal = (iSum + iWidth/2) / iWidth;
182  }
183  else if (bLeft)
184  {
185    pDcVal = (iSum + iHeight/2) / iHeight;
186  }
187  else
188  {
189    pDcVal = pSrc[-1]; // Default DC value already calculated and placed in the prediction array if no neighbors are available
190  }
191 
192  return pDcVal;
193}
194
195// Function for deriving the angular Intra predictions
196
197/** Function for deriving the simplified angular intra predictions.
198 * \param pSrc pointer to reconstructed sample array
199 * \param srcStride the stride of the reconstructed sample array
200 * \param rpDst reference to pointer for the prediction sample array
201 * \param dstStride the stride of the prediction sample array
202 * \param width the width of the block
203 * \param height the height of the block
204 * \param dirMode the intra prediction mode index
205 * \param blkAboveAvailable boolean indication if the block above is available
206 * \param blkLeftAvailable boolean indication if the block to the left is available
207 *
208 * This function derives the prediction samples for the angular mode based on the prediction direction indicated by
209 * the prediction mode index. The prediction direction is given by the displacement of the bottom row of the block and
210 * the reference row above the block in the case of vertical prediction or displacement of the rightmost column
211 * of the block and reference column left from the block in the case of the horizontal prediction. The displacement
212 * is signalled at 1/32 pixel accuracy. When projection of the predicted pixel falls inbetween reference samples,
213 * the predicted value for the pixel is linearly interpolated from the reference samples. All reference samples are taken
214 * from the extended main reference.
215 */
216Void TComPrediction::xPredIntraAng( Int* pSrc, Int srcStride, Pel*& rpDst, Int dstStride, UInt width, UInt height, UInt dirMode, Bool blkAboveAvailable, Bool blkLeftAvailable, Bool bFilter )
217{
218  Int k,l;
219  Int blkSize        = width;
220  Pel* pDst          = rpDst;
221
222  // Map the mode index to main prediction direction and angle
223  assert( dirMode > 0 ); //no planar
224  Bool modeDC        = dirMode < 2;
225  Bool modeHor       = !modeDC && (dirMode < 18);
226  Bool modeVer       = !modeDC && !modeHor;
227  Int intraPredAngle = modeVer ? (Int)dirMode - VER_IDX : modeHor ? -((Int)dirMode - HOR_IDX) : 0;
228  Int absAng         = abs(intraPredAngle);
229  Int signAng        = intraPredAngle < 0 ? -1 : 1;
230
231  // Set bitshifts and scale the angle parameter to block size
232  Int angTable[9]    = {0,    2,    5,   9,  13,  17,  21,  26,  32};
233  Int invAngTable[9] = {0, 4096, 1638, 910, 630, 482, 390, 315, 256}; // (256 * 32) / Angle
234  Int invAngle       = invAngTable[absAng];
235  absAng             = angTable[absAng];
236  intraPredAngle     = signAng * absAng;
237
238  // Do the DC prediction
239  if (modeDC)
240  {
241    Pel dcval = predIntraGetPredValDC(pSrc, srcStride, width, height, blkAboveAvailable, blkLeftAvailable);
242
243    for (k=0;k<blkSize;k++)
244    {
245      for (l=0;l<blkSize;l++)
246      {
247        pDst[k*dstStride+l] = dcval;
248      }
249    }
250  }
251
252  // Do angular predictions
253  else
254  {
255    Pel* refMain;
256    Pel* refSide;
257    Pel  refAbove[2*MAX_CU_SIZE+1];
258    Pel  refLeft[2*MAX_CU_SIZE+1];
259
260    // Initialise the Main and Left reference array.
261    if (intraPredAngle < 0)
262    {
263      for (k=0;k<blkSize+1;k++)
264      {
265        refAbove[k+blkSize-1] = pSrc[k-srcStride-1];
266      }
267      for (k=0;k<blkSize+1;k++)
268      {
269        refLeft[k+blkSize-1] = pSrc[(k-1)*srcStride-1];
270      }
271      refMain = (modeVer ? refAbove : refLeft) + (blkSize-1);
272      refSide = (modeVer ? refLeft : refAbove) + (blkSize-1);
273
274      // Extend the Main reference to the left.
275      Int invAngleSum    = 128;       // rounding for (shift by 8)
276      for (k=-1; k>blkSize*intraPredAngle>>5; k--)
277      {
278        invAngleSum += invAngle;
279        refMain[k] = refSide[invAngleSum>>8];
280      }
281    }
282    else
283    {
284      for (k=0;k<2*blkSize+1;k++)
285      {
286        refAbove[k] = pSrc[k-srcStride-1];
287      }
288      for (k=0;k<2*blkSize+1;k++)
289      {
290        refLeft[k] = pSrc[(k-1)*srcStride-1];
291      }
292      refMain = modeVer ? refAbove : refLeft;
293      refSide = modeVer ? refLeft  : refAbove;
294    }
295
296    if (intraPredAngle == 0)
297    {
298      for (k=0;k<blkSize;k++)
299      {
300        for (l=0;l<blkSize;l++)
301        {
302          pDst[k*dstStride+l] = refMain[l+1];
303        }
304      }
305
306      if ( bFilter )
307      {
308        for (k=0;k<blkSize;k++)
309        {
310          pDst[k*dstStride] = Clip ( pDst[k*dstStride] + (( refSide[k+1] - refSide[0] ) >> 1) );
311        }
312      }
313    }
314    else
315    {
316      Int deltaPos=0;
317      Int deltaInt;
318      Int deltaFract;
319      Int refMainIndex;
320
321      for (k=0;k<blkSize;k++)
322      {
323        deltaPos += intraPredAngle;
324        deltaInt   = deltaPos >> 5;
325        deltaFract = deltaPos & (32 - 1);
326
327        if (deltaFract)
328        {
329          // Do linear filtering
330          for (l=0;l<blkSize;l++)
331          {
332            refMainIndex        = l+deltaInt+1;
333            pDst[k*dstStride+l] = (Pel) ( ((32-deltaFract)*refMain[refMainIndex]+deltaFract*refMain[refMainIndex+1]+16) >> 5 );
334          }
335        }
336        else
337        {
338          // Just copy the integer samples
339          for (l=0;l<blkSize;l++)
340          {
341            pDst[k*dstStride+l] = refMain[l+deltaInt+1];
342          }
343        }
344      }
345    }
346
347    // Flip the block if this is the horizontal mode
348    if (modeHor)
349    {
350      Pel  tmp;
351      for (k=0;k<blkSize-1;k++)
352      {
353        for (l=k+1;l<blkSize;l++)
354        {
355          tmp                 = pDst[k*dstStride+l];
356          pDst[k*dstStride+l] = pDst[l*dstStride+k];
357          pDst[l*dstStride+k] = tmp;
358        }
359      }
360    }
361  }
362}
363
364Void TComPrediction::predIntraLumaAng(TComPattern* pcTComPattern, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight,  TComDataCU* pcCU, Bool bAbove, Bool bLeft )
365{
366  Pel *pDst = piPred;
367  Int *ptrSrc;
368
369  assert( g_aucConvertToBit[ iWidth ] >= 0 ); //   4x  4
370  assert( g_aucConvertToBit[ iWidth ] <= 5 ); // 128x128
371  assert( iWidth == iHeight  );
372
373  ptrSrc = pcTComPattern->getPredictorPtr( uiDirMode, g_aucConvertToBit[ iWidth ] + 2, m_piYuvExt );
374
375  // get starting pixel in block
376  Int sw = 2 * iWidth + 1;
377
378  // Create the prediction
379  if ( uiDirMode == PLANAR_IDX )
380  {
381    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
382  }
383  else
384  {
385    xPredIntraAng( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, true );
386
387    if( (uiDirMode == DC_IDX ) && bAbove && bLeft )
388    {
389      xDCPredFiltering( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight);
390    }
391  }
392}
393
394// Angular chroma
395Void TComPrediction::predIntraChromaAng( TComPattern* pcTComPattern, Int* piSrc, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, TComDataCU* pcCU, Bool bAbove, Bool bLeft )
396{
397  Pel *pDst = piPred;
398  Int *ptrSrc = piSrc;
399
400  // get starting pixel in block
401  Int sw = 2 * iWidth + 1;
402
403  if ( uiDirMode == PLANAR_IDX )
404  {
405    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
406  }
407  else
408  {
409    // Create the prediction
410    xPredIntraAng( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, false );
411  }
412}
413
414/** Function for checking identical motion.
415 * \param TComDataCU* pcCU
416 * \param UInt PartAddr
417 */
418Bool TComPrediction::xCheckIdenticalMotion ( TComDataCU* pcCU, UInt PartAddr )
419{
420  if( pcCU->getSlice()->isInterB() && pcCU->getSlice()->getPPS()->getWPBiPredIdc() == 0 )
421  {
422    if( pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr) >= 0 && pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr) >= 0)
423    {
424      Int RefPOCL0    = pcCU->getSlice()->getRefPic(REF_PIC_LIST_0, pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr))->getPOC();
425      Int RefViewIdL0 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_0, pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr))->getViewId();
426      Int RefPOCL1    = pcCU->getSlice()->getRefPic(REF_PIC_LIST_1, pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr))->getPOC();
427      Int RefViewIdL1 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_1, pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr))->getViewId();
428      if(RefPOCL0 == RefPOCL1 && RefViewIdL0 == RefViewIdL1 && pcCU->getCUMvField(REF_PIC_LIST_0)->getMv(PartAddr) == pcCU->getCUMvField(REF_PIC_LIST_1)->getMv(PartAddr))
429      {
430        return true;
431      }
432    }
433  }
434  return false;
435}
436
437#if LGE_EDGE_INTRA_A0070
438Void TComPrediction::predIntraLumaEdge ( TComDataCU* pcCU, TComPattern* pcTComPattern, UInt uiAbsPartIdx, Int iWidth, Int iHeight, Pel* piPred, UInt uiStride, Bool bDelta )
439{
440  Pel *piDst = piPred;
441  Int *piSrc;
442  Int iSrcStride = ( iWidth<<1 ) + 1;
443  Int iDstStride = uiStride;
444
445  piSrc = pcTComPattern->getPredictorPtr( 0, g_aucConvertToBit[ iWidth ] + 2, m_piYuvExt );
446
447  xPredIntraEdge ( pcCU, uiAbsPartIdx, iWidth, iHeight, piSrc, iSrcStride, piDst, iDstStride
448#if LGE_EDGE_INTRA_DELTA_DC
449    , bDelta
450#endif
451    );
452}
453
454Pel  TComPrediction::xGetNearestNeighbor( Int x, Int y, Int* pSrc, Int srcStride, Int iWidth, Int iHeight, Bool* bpRegion )
455{
456  Bool bLeft = (x < y) ? true : false;
457  Bool bFound = false;
458  Int  iFoundX = -1, iFoundY = -1;
459  Int  cResult = 0;
460
461  UChar* piTopDistance = new UChar[iWidth];
462  UChar* piLeftDistance = new UChar[iHeight];
463
464  for( Int i = 0; i < iWidth; i++ )
465  {
466    int Abs = x > i ? x - i : i - x;
467    piTopDistance[ i ] = y + Abs;
468
469    Abs = y > i ? y - i : i - y;
470    piLeftDistance[ i ] = x + Abs;
471  }
472
473  for( Int dist = 0; dist < MAX_DISTANCE_EDGEINTRA && !bFound; dist++ )
474  {
475    if( !bLeft )
476    {
477      for( Int i = 0; i < iWidth; i++ )
478      {
479        if( piTopDistance[ i ] == dist )
480        {
481          if( bpRegion[ i ] == bpRegion[ x + y * iWidth ] )
482          {
483            iFoundX = i;
484            iFoundY = 0;
485            bFound = true;
486          }
487        }
488      }
489      for( Int i = 0; i < iHeight; i++ )
490      {
491        if( piLeftDistance[ i ] == dist )
492        {
493          if( bpRegion[ i * iWidth ] == bpRegion[ x + y * iWidth ] )
494          {
495            iFoundX = 0;
496            iFoundY = i;
497            bFound = true;
498          }
499        }
500      }
501    }
502    else
503    {
504      for( Int i = 0; i < iHeight; i++ )
505      {
506        if( piLeftDistance[ i ] == dist )
507        {
508          if( bpRegion[ i * iWidth ] == bpRegion[ x + y * iWidth ] )
509          {
510            iFoundX = 0;
511            iFoundY = i;
512            bFound = true;
513          }
514        }
515      }
516      for( Int i = 0; i < iWidth; i++ )
517      {
518        if( piTopDistance[ i ] == dist )
519        {
520          if( bpRegion[ i ] == bpRegion[ x + y * iWidth ] )
521          {
522            iFoundX = i;
523            iFoundY = 0;
524            bFound = true;
525          }
526        }
527      }
528    }
529  }
530
531  if( iFoundY == 0 )
532  {
533    cResult = pSrc[ iFoundX + 1 ];
534  }
535  else // iFoundX == 0
536  {
537    cResult = pSrc[ (iFoundY + 1) * srcStride ];
538  }
539
540  delete[] piTopDistance;  piTopDistance = NULL;
541  delete[] piLeftDistance; piLeftDistance = NULL;
542
543  assert( bFound );
544
545  return cResult;
546}
547
548Void TComPrediction::xPredIntraEdge( TComDataCU* pcCU, UInt uiAbsPartIdx, Int iWidth, Int iHeight, Int* pSrc, Int srcStride, Pel*& rpDst, Int dstStride, Bool bDelta )
549{
550  Pel* pDst = rpDst;
551  Bool* pbRegion = pcCU->getEdgePartition( uiAbsPartIdx );
552
553  // Do prediction
554  {
555    //UInt uiSum0 = 0, uiSum1 = 0;
556    Int iSum0 = 0, iSum1 = 0;
557    //UInt uiMean0, uiMean1;
558    Int iMean0, iMean1;
559    //UInt uiCount0 = 0, uiCount1 = 0;
560    Int iCount0 = 0, iCount1 = 0;
561    for( UInt ui = 0; ui < iWidth; ui++ )
562    {
563      if( pbRegion[ ui ] == false )
564      {
565        iSum0 += (pSrc[ ui + 1 ]);
566        iCount0++;
567      }
568      else
569      {
570        iSum1 += (pSrc[ ui + 1 ]);
571        iCount1++;
572      }
573    }
574    for( UInt ui = 0; ui < iHeight; ui++ ) // (0,0) recount (to avoid division)
575    {
576      if( pbRegion[ ui * iWidth ] == false )
577      {
578        iSum0 += (pSrc[ (ui + 1) * srcStride ]);
579        iCount0++;
580      }
581      else
582      {
583        iSum1 += (pSrc[ (ui + 1) * srcStride ]);
584        iCount1++;
585      }
586    }
587    if( iCount0 == 0 )
588      assert(false);
589    if( iCount1 == 0 )
590      assert(false);
591    iMean0 = iSum0 / iCount0; // TODO : integer op.
592    iMean1 = iSum1 / iCount1;
593#if LGE_EDGE_INTRA_DELTA_DC
594    if( bDelta ) 
595    {
596      Int iDeltaDC0 = pcCU->getEdgeDeltaDC0( uiAbsPartIdx );
597      Int iDeltaDC1 = pcCU->getEdgeDeltaDC1( uiAbsPartIdx );
598      xDeltaDCQuantScaleUp( pcCU, iDeltaDC0 );
599      xDeltaDCQuantScaleUp( pcCU, iDeltaDC1 );
600      iMean0 = Clip( iMean0 + iDeltaDC0 );
601      iMean1 = Clip( iMean1 + iDeltaDC1 );
602    }
603#endif
604    for( UInt ui = 0; ui < iHeight; ui++ )
605    {
606      for( UInt uii = 0; uii < iWidth; uii++ )
607      {
608        if( pbRegion[ uii + ui * iWidth ] == false )
609          pDst[ uii + ui * dstStride ] = iMean0;
610        else
611          pDst[ uii + ui * dstStride ] = iMean1;
612      }
613    }
614  }
615}
616#endif
617
618#if DEPTH_MAP_GENERATION
619#if MERL_VSP_C0152
620Void TComPrediction::motionCompensation( TComDataCU* pcCU, TComYuv* pcYuvPred, UInt uiAbsPartIdx, RefPicList eRefPicList, Int iPartIdx, Bool bPrdDepthMap, UInt uiSubSampExpX, UInt uiSubSampExpY )
621#else
622Void TComPrediction::motionCompensation( TComDataCU* pcCU, TComYuv* pcYuvPred, RefPicList eRefPicList, Int iPartIdx, Bool bPrdDepthMap, UInt uiSubSampExpX, UInt uiSubSampExpY )
623#endif
624#else
625#if MERL_VSP_C0152
626Void TComPrediction::motionCompensation ( TComDataCU* pcCU, TComYuv* pcYuvPred, UInt uiAbsPartIdx, RefPicList eRefPicList, Int iPartIdx )
627#else
628Void TComPrediction::motionCompensation ( TComDataCU* pcCU, TComYuv* pcYuvPred, RefPicList eRefPicList, Int iPartIdx )
629#endif
630#endif
631{
632  Int         iWidth;
633  Int         iHeight;
634  UInt        uiPartAddr;
635
636  if ( iPartIdx >= 0 )
637  {
638    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
639
640#if DEPTH_MAP_GENERATION
641    if( bPrdDepthMap )
642    {
643      iWidth  >>= uiSubSampExpX;
644      iHeight >>= uiSubSampExpY;
645    }
646#endif
647
648    if ( eRefPicList != REF_PIC_LIST_X )
649    {
650#if LGE_ILLUCOMP_B0045
651      if( pcCU->getSlice()->getPPS()->getUseWP() && !pcCU->getICFlag(uiPartAddr))
652#else
653      if( pcCU->getSlice()->getPPS()->getUseWP())
654#endif
655      {
656#if MERL_VSP_C0152
657        xPredInterUni (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
658#else
659        xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
660#endif
661      }
662      else
663      {
664#if MERL_VSP_C0152
665        xPredInterUni (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
666#else       
667        xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
668#endif
669      }
670#if LGE_ILLUCOMP_B0045
671      if( pcCU->getSlice()->getPPS()->getUseWP() && !pcCU->getICFlag(uiPartAddr) )
672#else
673      if ( pcCU->getSlice()->getPPS()->getUseWP() )
674#endif
675      {
676        xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx );
677      }
678    }
679    else
680    {
681      if( xCheckIdenticalMotion( pcCU, uiPartAddr ) && !bPrdDepthMap )
682      {
683#if MERL_VSP_C0152
684        xPredInterUni (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
685#else
686        xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
687#endif
688      }
689      else
690      {
691#if MERL_VSP_C0152
692        xPredInterBi  (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY, pcYuvPred, iPartIdx, bPrdDepthMap );
693#else
694        xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY, pcYuvPred, iPartIdx, bPrdDepthMap );
695#endif
696      }
697    }
698    return;
699  }
700
701  for ( iPartIdx = 0; iPartIdx < pcCU->getNumPartInter(); iPartIdx++ )
702  {
703    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
704
705    if( bPrdDepthMap )
706    {
707      iWidth  >>= uiSubSampExpX;
708      iHeight >>= uiSubSampExpY;
709    }
710
711    if ( eRefPicList != REF_PIC_LIST_X )
712    {
713#if LGE_ILLUCOMP_B0045
714      if( pcCU->getSlice()->getPPS()->getUseWP() && !pcCU->getICFlag(uiPartAddr))
715#else
716      if( pcCU->getSlice()->getPPS()->getUseWP())
717#endif
718      {
719#if MERL_VSP_C0152
720        xPredInterUni (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
721#else
722        xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
723#endif
724  }
725  else
726  {
727#if MERL_VSP_C0152
728        xPredInterUni (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
729#else
730        xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
731#endif
732      }
733#if MERL_VSP_C0152
734      xPredInterUni (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
735#else
736      xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
737#endif
738
739#if LGE_ILLUCOMP_B0045
740      if( pcCU->getSlice()->getPPS()->getUseWP() && !pcCU->getICFlag(uiPartAddr))
741#else
742      if( pcCU->getSlice()->getPPS()->getUseWP())
743#endif
744      {
745        xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, iPartIdx );
746      }
747    }
748    else
749    {
750      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
751      {
752#if MERL_VSP_C0152
753        xPredInterUni (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
754#else
755        xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
756#endif
757      }
758      else
759      {
760#if MERL_VSP_C0152
761        xPredInterBi  (pcCU, uiPartAddr, uiAbsPartIdx+uiPartAddr, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY, pcYuvPred, iPartIdx, bPrdDepthMap );
762#else
763        xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY, pcYuvPred, iPartIdx, bPrdDepthMap );
764#endif   
765      }
766    }
767  }
768  return;
769}
770
771#if H3D_IVRP & !QC_ARP_D0177
772Void TComPrediction::residualPrediction(TComDataCU* pcCU, TComYuv* pcYuvPred, TComYuv* pcYuvResPred)
773{
774  Int         iWidth;
775  Int         iHeight;
776  UInt        uiPartAddr;
777
778  pcCU->getPartIndexAndSize( 0, uiPartAddr, iWidth, iHeight );
779
780  Bool bResAvail = false;
781
782  bResAvail = pcCU->getResidualSamples( 0, true, pcYuvResPred );
783
784  assert (bResAvail);
785
786  pcYuvPred->add(pcYuvResPred, iWidth, iHeight);
787}
788#endif
789
790#if MERL_General_Fix
791#if MERL_VSP_C0152
792// Function to perform VSP block compensation
793Void  TComPrediction::xPredInterVSPBlk(TComDataCU* pcCU, UInt uiPartAddr, UInt uiAbsPartIdx, Int iWidth, Int iHeight, TComMv cMv, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
794{
795  TComPic*    pRefPicBaseTxt        = NULL;
796  TComPicYuv* pcBaseViewTxtPicYuv   = NULL;
797  TComPicYuv* pcBaseViewDepthPicYuv = NULL;
798  Int iBlkX = 0;
799  Int iBlkY = 0;
800  Int* pShiftLUT;
801  Int  iShiftPrec;
802
803#if !MERL_VSP_NBDV_RefVId_Fix_D0166
804  pRefPicBaseTxt        = pcCU->getSlice()->getRefPicBaseTxt();
805  pcBaseViewTxtPicYuv   = pRefPicBaseTxt->getPicYuvRec();
806  TComPic* pRefPicBaseDepth = pcCU->getSlice()->getRefPicBaseDepth();
807  pcBaseViewDepthPicYuv     = pRefPicBaseDepth->getPicYuvRec();
808
809  Int iBlkX = ( pcCU->getAddr() % pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
810  Int iBlkY = ( pcCU->getAddr() / pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
811  pcCU->getSlice()->getBWVSPLUTParam(pShiftLUT, iShiftPrec);
812  xPredInterLumaBlkFromDM  ( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,    pcCU->getSlice()->getSPS()->isDepth(), rpcYuvPred );
813  xPredInterChromaBlkFromDM( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1, pcCU->getSlice()->getSPS()->isDepth(), rpcYuvPred );
814
815#else // MERL_VSP_NBDV_RefVId_Fix_D0166
816
817  //recover VSP reference frame according to negative refIdx number
818  RefPicList privateRefPicList = (RefPicList) pcCU->getVSPDir( uiPartAddr );
819  assert(privateRefPicList == REF_PIC_LIST_0 || privateRefPicList == REF_PIC_LIST_1);
820
821  // Step 1: get depth reference
822  Int  refIdx = -1-pcCU->getCUMvField( privateRefPicList )->getRefIdx( uiPartAddr ); // texture ref index, a trick when storing refIdx
823  Int  viewId = pcCU->getSlice()->getRefViewId(privateRefPicList, refIdx);  // texture view id
824  Int  refPoc = pcCU->getSlice()->getRefPOC(privateRefPicList, refIdx);     // texture POC
825  TComPic* pRefPicBaseDepth = pcCU->getSlice()->getDepthRefPic(viewId, refPoc);
826
827  pcBaseViewDepthPicYuv = pRefPicBaseDepth->getPicYuvRec();
828  assert(refPoc == pcCU->getSlice()->getPOC());
829  assert(pRefPicBaseDepth != NULL);
830  assert(pcBaseViewDepthPicYuv != NULL);
831
832  iBlkX = ( pcCU->getAddr() % pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
833  iBlkY = ( pcCU->getAddr() / pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
834#if MERL_Bi_VSP_D0166
835  // Step 2: get texture reference
836  pRefPicBaseTxt = xGetVspRefTxt( pcCU, uiPartAddr, eRefPicList);
837  pcBaseViewTxtPicYuv = pRefPicBaseTxt->getPicYuvRec();
838  assert(pcBaseViewTxtPicYuv != NULL);
839
840  // initialize the LUT according to the reference view idx
841  pcCU->getSlice()->getBWVSPLUTParam(pShiftLUT, iShiftPrec, pRefPicBaseTxt->getViewId());
842
843  // Step 3: Do compensation
844  xPredInterLumaBlkFromDM  ( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,    pcCU->getSlice()->getSPS()->isDepth(), rpcYuvPred, bi );
845  xPredInterChromaBlkFromDM( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1, pcCU->getSlice()->getSPS()->isDepth(), rpcYuvPred, bi );
846#else
847  // Step 2: get texture reference
848  pRefPicBaseTxt = pcCU->getSlice()->getRefPic(privateRefPicList, refIdx);
849  pcBaseViewTxtPicYuv = pRefPicBaseTxt->getPicYuvRec();
850  assert(pcBaseViewTxtPicYuv != NULL);
851
852  //initialize the LUT according to the reference view idx
853  pcCU->getSlice()->getBWVSPLUTParam(pShiftLUT, iShiftPrec, pRefPicBaseTxt->getViewId());
854
855  // Step 3: Do compensation
856  xPredInterLumaBlkFromDM  ( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,    pcCU->getSlice()->getSPS()->isDepth(), rpcYuvPred );
857  xPredInterChromaBlkFromDM( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1, pcCU->getSlice()->getSPS()->isDepth(), rpcYuvPred );
858#endif
859
860#endif
861}
862
863#endif
864
865#if MERL_Bi_VSP_D0166
866TComPic*  TComPrediction::xGetVspRefTxt(TComDataCU* pcCU, UInt uiPartAddr, RefPicList eRefPicList)
867{
868  RefPicList  privateRefPicList = (RefPicList) pcCU->getVSPDir( uiPartAddr );
869  Int         refIdx = -1-pcCU->getCUMvField( privateRefPicList )->getRefIdx( uiPartAddr ); // texture ref index, a trick when storing refIdx
870  Int         viewId = pcCU->getSlice()->getRefViewId(privateRefPicList, refIdx);  // texture view id
871  TComPic*    refPic = NULL;
872
873  assert(privateRefPicList == REF_PIC_LIST_0 || privateRefPicList == REF_PIC_LIST_1);
874
875  if (privateRefPicList == eRefPicList)
876  {
877    Int  refIdxt = -1-pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
878    assert(refIdxt>= 0);
879    refPic = pcCU->getSlice()->getRefPic(eRefPicList, refIdxt);
880  }
881  else
882  {
883    // Find the other interview reference in order to do VSP
884    RefPicList otherRefPicList = privateRefPicList == REF_PIC_LIST_0 ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
885    Bool isFound = false;
886    for (Int iRefIdx = 0; iRefIdx <pcCU->getSlice()->getNumRefIdx(otherRefPicList); iRefIdx ++ )
887    {
888      Int refViewIdx  = pcCU->getSlice()->getRefViewId( otherRefPicList, iRefIdx);
889      if ( (refViewIdx != pcCU->getSlice()->getViewId()) && (refViewIdx != viewId ) )
890      {
891        refPic = pcCU->getSlice()->getRefPic(otherRefPicList, iRefIdx);
892        isFound = true;
893        break;
894      }
895    }
896
897    if (isFound == false)
898    {
899      Int  refIdxTxt = -1-pcCU->getCUMvField( privateRefPicList )->getRefIdx( uiPartAddr );
900      assert(refIdxTxt >= 0);
901      refPic = pcCU->getSlice()->getRefPic(privateRefPicList, refIdxTxt);
902    }
903    assert(isFound);
904  }
905  assert(refPic != NULL);
906  return refPic;
907}
908#endif
909#endif
910
911#if MERL_VSP_C0152
912Void TComPrediction::xPredInterUni ( TComDataCU* pcCU, UInt uiPartAddr, UInt uiAbsPartIdx, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Int iPartIdx, Bool bPrdDepthMap, UInt uiSubSampExpX, UInt uiSubSampExpY, Bool bi )
913#else
914Void TComPrediction::xPredInterUni ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Int iPartIdx, Bool bPrdDepthMap, UInt uiSubSampExpX, UInt uiSubSampExpY, Bool bi )
915#endif
916{
917#if MERL_VSP_C0152
918  Int  iRefIdx = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );   
919  Int  vspIdx  = pcCU->getVSPIndex(uiPartAddr);
920  if (vspIdx != 0)
921  {
922    if (iRefIdx >= 0)
923    {
924      printf("vspIdx = %d, iRefIdx = %d\n", vspIdx, iRefIdx);
925    }
926    assert (iRefIdx < 0); // assert (iRefIdx == NOT_VALID);
927  }
928  else
929  {
930    assert (iRefIdx >= 0);
931  }
932#else
933  Int         iRefIdx     = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           assert (iRefIdx >= 0);
934#endif
935
936  TComMv cMv = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
937  pcCU->clipMv(cMv);
938
939#if DEPTH_MAP_GENERATION
940  if( bPrdDepthMap )
941  {
942    UInt uiRShift = 0;
943    if( pcCU->getPic()->getStoredPDMforV2() == 1 )
944      xPredInterPrdDepthMap( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPredDepthMapTemp(), uiPartAddr, &cMv, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY, rpcYuvPred, uiRShift, 0 );
945    else
946      xPredInterPrdDepthMap( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPredDepthMap(), uiPartAddr, &cMv, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY, rpcYuvPred, uiRShift, 0 );
947
948    return;
949  }
950#endif
951#if QC_ARP_D0177
952  if(
953#if MERL_General_Fix // TODO: Maybe logically redundant, but easier to read. Need verification before being removed
954#if MERL_VSP_C0152
955       vspIdx == 0 &&
956#endif
957#endif
958       pcCU->getSlice()->getSPS()->isDepth() == false
959    && pcCU->getSlice()->getSPS()->getViewId() > 0
960    && pcCU->getSlice()->getSPS()->getUseAdvRP() > 0
961    && pcCU->getARPW( uiPartAddr ) > 0 
962    && pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPOC()!= pcCU->getSlice()->getPOC()
963    && (pcCU->getPartitionSize(uiPartAddr)==SIZE_2Nx2N || pcCU->isSkipped(uiPartAddr))
964    )
965  {
966    xPredInterUniARP( pcCU , uiPartAddr , iWidth , iHeight , eRefPicList , rpcYuvPred , iPartIdx , bi );
967  }
968  else
969  {
970#endif
971#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
972  if( pcCU->getSlice()->getSPS()->isDepth() )
973  {
974#if MERL_VSP_C0152
975    if (vspIdx != 0)
976    { // depth, vsp compensation
977#if !MERL_General_Fix
978      // get depth estimator here
979      TComPic* pRefPicBaseDepth = pcCU->getSlice()->getRefPicBaseDepth();
980      TComPicYuv* pcBaseViewDepthPicYuv = NULL;
981      if (vspIdx < 4) // spatial
982      {
983        pcBaseViewDepthPicYuv = pRefPicBaseDepth->getPicYuvRec();
984      }
985      Int iBlkX = ( pcCU->getAddr() % pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
986      Int iBlkY = ( pcCU->getAddr() / pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
987      Int* pShiftLUT;
988      Int iShiftPrec;
989      pcCU->getSlice()->getBWVSPLUTParam(pShiftLUT, iShiftPrec);
990      //using disparity to find the depth block of the base view as the depth block estimator of the current block
991      //using depth block estimator and base view texture to get Backward warping
992      xPredInterLumaBlkFromDM  ( pcBaseViewDepthPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,     pcCU->getSlice()->getSPS()->isDepth(), vspIdx, rpcYuvPred );
993      xPredInterChromaBlkFromDM( pcBaseViewDepthPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1,  pcCU->getSlice()->getSPS()->isDepth(), vspIdx, rpcYuvPred );
994#else
995      xPredInterVSPBlk(pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, cMv, eRefPicList, rpcYuvPred, bi );
996#endif
997    }
998    else
999    {
1000#endif
1001      UInt uiRShift = ( bi ? 14-g_uiBitDepth-g_uiBitIncrement : 0 );
1002      UInt uiOffset = bi ? IF_INTERNAL_OFFS : 0;
1003#if LGE_ILLUCOMP_DEPTH_C0046
1004    Bool bICFlag = pcCU->getICFlag(uiPartAddr) && (pcCU->getSlice()->getRefViewId( eRefPicList, iRefIdx ) != pcCU->getSlice()->getViewId());
1005#endif
1006#if DEPTH_MAP_GENERATION
1007    xPredInterPrdDepthMap( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, 0, 0, rpcYuvPred, uiRShift, uiOffset
1008#if LGE_ILLUCOMP_DEPTH_C0046
1009        , bICFlag
1010#endif
1011        );
1012#else
1013      xPredInterPrdDepthMap( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, uiRShift, uiOffset );
1014#endif
1015#if MERL_VSP_C0152
1016    }
1017#endif// MERL_VSP_C0152 //else
1018  }
1019  else  // texture
1020  {
1021#endif
1022#if MERL_VSP_C0152
1023    if ( vspIdx != 0 )
1024    { // texture, vsp compensation
1025#if !MERL_General_Fix
1026      TComPic*    pRefPicBaseTxt        = pcCU->getSlice()->getRefPicBaseTxt();
1027      TComPicYuv* pcBaseViewTxtPicYuv   = pRefPicBaseTxt->getPicYuvRec();
1028      TComPicYuv* pcBaseViewDepthPicYuv = NULL;
1029      if (vspIdx < 4) // spatial
1030      {
1031        TComPic* pRefPicBaseDepth = pcCU->getSlice()->getRefPicBaseDepth();
1032        pcBaseViewDepthPicYuv     = pRefPicBaseDepth->getPicYuvRec();
1033      }
1034      Int iBlkX = ( pcCU->getAddr() % pRefPicBaseTxt->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
1035      Int iBlkY = ( pcCU->getAddr() / pRefPicBaseTxt->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
1036      Int* pShiftLUT;
1037      Int iShiftPrec;
1038      pcCU->getSlice()->getBWVSPLUTParam(pShiftLUT, iShiftPrec);
1039
1040      //using disparity to find the depth block of the base view as the depth block estimator of the current block
1041      //using depth block estimator and base view texture to get Backward warping
1042      xPredInterLumaBlkFromDM  ( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,    pcCU->getSlice()->getSPS()->isDepth(), vspIdx, rpcYuvPred );
1043      xPredInterChromaBlkFromDM( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, iShiftPrec, &cMv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1, pcCU->getSlice()->getSPS()->isDepth(), vspIdx, rpcYuvPred );
1044#else
1045      xPredInterVSPBlk(pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, cMv, eRefPicList, rpcYuvPred, bi );
1046#endif
1047    }
1048    else//texture not VSP
1049    {
1050#endif //MERL_VSP_C0152
1051#if LGE_ILLUCOMP_B0045
1052      Bool bICFlag = pcCU->getICFlag(uiPartAddr) && (pcCU->getSlice()->getRefViewId( eRefPicList, iRefIdx ) != pcCU->getSlice()->getViewId());
1053
1054      xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, bICFlag);
1055#else
1056      xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
1057#endif
1058#if MERL_VSP_C0152
1059     } //texture not VSP
1060#endif
1061#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
1062  }
1063#endif
1064
1065#if MERL_VSP_C0152
1066  if ( vspIdx == 0 )//Not VSP
1067  {
1068#endif
1069#if LGE_ILLUCOMP_B0045
1070  Bool bICFlag = pcCU->getICFlag(uiPartAddr) && (pcCU->getSlice()->getRefViewId( eRefPicList, iRefIdx ) != pcCU->getSlice()->getViewId());
1071
1072  xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, bICFlag );
1073#else
1074  xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
1075#endif
1076#if MERL_VSP_C0152
1077   }
1078#endif
1079#if QC_ARP_D0177
1080  }
1081#endif
1082}
1083
1084#if QC_ARP_D0177
1085Void TComPrediction::xPredInterUniARP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Int iPartIdx, Bool bi, TComMvField * pNewMvFiled )
1086{
1087  Int         iRefIdx     = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           
1088  TComMv      cMv         = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
1089  Bool        bTobeScaled = false;
1090  UChar dW = pcCU->getARPW ( uiPartAddr );
1091  TComPic* pcPicYuvBaseCol =  NULL;
1092  TComPic* pcPicYuvBaseRef =  NULL;
1093  DisInfo cDistparity;
1094
1095  if( pNewMvFiled )
1096  {
1097    iRefIdx = pNewMvFiled->getRefIdx(); 
1098    cMv = pNewMvFiled->getMv();
1099  }
1100
1101#if QC_CU_NBDV_D0181
1102  cDistparity.bDV           = pcCU->getDvInfo(uiPartAddr).bDV;
1103  if( cDistparity.bDV )
1104  {
1105#if MERL_VSP_C0152
1106    cDistparity.m_acMvCand[0] = pcCU->getDvInfo(0).m_acMvCandNoRef[0];
1107#else
1108    cDistparity.m_acMvCand[0] = pcCU->getDvInfo(0).m_acMvCand[0];
1109#endif
1110    assert(pcCU->getDvInfo(uiPartAddr).bDV ==  pcCU->getDvInfo(0).bDV);
1111    cDistparity.m_aVIdxCan[0] = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan[0];
1112    cDistparity.iN            = pcCU->getDvInfo(uiPartAddr).iN;
1113  }
1114  else
1115    cDistparity.iN    =  0;
1116#else
1117  pcCU->getDisMvpCandNBDV( iPartIdx, uiPartAddr,  &cDistparity, false );
1118#endif
1119
1120  dW = !cDistparity.iN ? 0: dW;
1121  if( cDistparity.iN ) 
1122  {
1123    if(dW > 0 && pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC()!= pcCU->getSlice()->getPOC())
1124      bTobeScaled = true;
1125    pcPicYuvBaseCol =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getPOC(),                              cDistparity.m_aVIdxCan[0] );
1126    pcPicYuvBaseRef =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC(), cDistparity.m_aVIdxCan[0] );
1127    if( (!pcPicYuvBaseCol || pcPicYuvBaseCol->getPOC() != pcCU->getSlice()->getPOC()) || (!pcPicYuvBaseRef || pcPicYuvBaseRef->getPOC() != pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() ))
1128    {
1129      dW = 0;
1130      bTobeScaled = false;
1131    }
1132    else
1133      assert( pcPicYuvBaseCol->getPOC() == pcCU->getSlice()->getPOC() && pcPicYuvBaseRef->getPOC() == pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() );
1134    if(bTobeScaled)
1135    {     
1136      Int iCurrPOC = pcCU->getSlice()->getPOC();
1137      Int iColRefPOC  = pcCU->getSlice()->getRefPOC( eRefPicList, iRefIdx );
1138      Int iCurrRefPOC = pcCU->getSlice()->getRefPOC( eRefPicList,  0);
1139      Int iScale = pcCU-> xGetDistScaleFactor(iCurrPOC, iCurrRefPOC, iCurrPOC, iColRefPOC);
1140      if ( iScale != 4096 )
1141        cMv = cMv.scaleMv( iScale );
1142      iRefIdx = 0;
1143    }
1144  }
1145  pcCU->clipMv(cMv);
1146  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec();
1147  xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
1148#if LGE_ILLUCOMP_B0045
1149    , false
1150#endif
1151    , true 
1152    );
1153  xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
1154#if LGE_ILLUCOMP_B0045
1155    , false
1156#endif
1157    , true
1158    );
1159  if( dW > 0 )
1160  {
1161    TComYuv * pYuvmB0 = &m_acYuvPredBase[0];
1162    TComYuv * pYuvB1  = &m_acYuvPredBase[1];
1163    assert ( cDistparity.iN == 1 );
1164    pcPicYuvRef = pcPicYuvBaseCol->getPicYuvRec();
1165    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acMvCand[0], iWidth, iHeight, pYuvB1, bi
1166#if LGE_ILLUCOMP_B0045
1167      , false
1168#endif
1169      ,  true
1170      );
1171    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acMvCand[0], iWidth, iHeight, pYuvB1, bi
1172#if LGE_ILLUCOMP_B0045
1173      , false
1174#endif
1175      , true
1176      );
1177    pcPicYuvRef = pcPicYuvBaseRef->getPicYuvRec();
1178    TComMv cMVwithDisparity = cMv + cDistparity.m_acMvCand[0];
1179    pcCU->clipMv(cMVwithDisparity);
1180    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvmB0, bi
1181#if LGE_ILLUCOMP_B0045
1182      , false
1183#endif
1184      , true
1185      );
1186    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvmB0, bi
1187#if LGE_ILLUCOMP_B0045
1188      , false
1189#endif
1190      , true
1191      );
1192    pYuvB1->subtractARP( pYuvB1 , pYuvmB0 , uiPartAddr , iWidth , iHeight );
1193    if(dW == 2)
1194      pYuvB1->multiplyARP( uiPartAddr , iWidth , iHeight , dW );
1195    rpcYuvPred->addARP( rpcYuvPred , pYuvB1 , uiPartAddr , iWidth , iHeight , !bi );
1196  }
1197}
1198#endif
1199
1200#if MERL_VSP_C0152
1201Void TComPrediction::xPredInterBi ( TComDataCU* pcCU, UInt uiPartAddr, UInt uiAbsPartIdx, Int iWidth, Int iHeight, UInt uiSubSampExpX, UInt uiSubSampExpY, TComYuv*& rpcYuvPred, Int iPartIdx, Bool bPrdDepthMap )
1202#else
1203Void TComPrediction::xPredInterBi ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, UInt uiSubSampExpX, UInt uiSubSampExpY, TComYuv*& rpcYuvPred, Int iPartIdx, Bool bPrdDepthMap )
1204#endif
1205{
1206  TComYuv* pcMbYuv;
1207  Int      iRefIdx[2] = {-1, -1};
1208
1209#if MERL_Bi_VSP_D0166
1210  Bool biDecision = 0;
1211  Int  predDirVSP = 0;
1212  if (pcCU->getVSPIndex(uiPartAddr) != 0) // is VSP
1213  {
1214    Int biVSPAvail = 0;
1215    //test whether VSP is Bi or Uni
1216    //Step1. Get derived DV view id
1217    RefPicList privateRefPicList = (RefPicList) pcCU->getVSPDir( uiPartAddr );
1218    RefPicList otherRefPicList = privateRefPicList == REF_PIC_LIST_0 ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1219    assert(privateRefPicList == REF_PIC_LIST_0 || privateRefPicList == REF_PIC_LIST_1);
1220    Int  refIdx = -1-pcCU->getCUMvField( privateRefPicList )->getRefIdx( uiPartAddr );
1221    assert(refIdx >= 0);
1222    Int  viewId = pcCU->getSlice()->getRefViewId(privateRefPicList, refIdx);
1223    Int  refPoc = pcCU->getSlice()->getRefPOC(privateRefPicList, refIdx);
1224
1225    assert(refPoc == pcCU->getSlice()->getPOC());
1226//    if(refPoc != pcCU->getSlice()->getPOC() )
1227//    {
1228//      printf("refPOC= %d, and current POC=%d\n", refPoc, pcCU->getSlice()->getPOC() );
1229//    }
1230    //Step 2. Get initial prediction direction value according to reference picture list availability
1231    Int iInterDir = ((pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0) > 0 && pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 0) ? 3 :
1232      (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0) > 0 ? 1 : 2)); 
1233    //Step 3.  Check the availability of Bi VSP by checking the interview reference availability in the other reference list
1234    if(iInterDir == 3)
1235    {
1236      for (Int jRefIdx = 0; jRefIdx <pcCU->getSlice()->getNumRefIdx(otherRefPicList); jRefIdx++ )
1237      {
1238        Int refViewIdx  = pcCU->getSlice()->getRefViewId( otherRefPicList, jRefIdx);
1239        if ( (refViewIdx != pcCU->getSlice()->getViewId()) && (refViewIdx != viewId ) )
1240        {
1241          biVSPAvail = 1;
1242          break;
1243        }
1244      }
1245    }
1246    //Step 4. Update the Bi VSP prediction direction
1247    if ( iInterDir == 3 && biVSPAvail == 1)
1248    {
1249      biDecision   = 1;
1250      predDirVSP = 3;
1251    }
1252    else
1253    {
1254      biDecision = 0;
1255      if ( privateRefPicList == REF_PIC_LIST_0 )
1256        predDirVSP = 1;
1257      else
1258        predDirVSP = 2;
1259    }
1260  }
1261  else 
1262  {//not VSP
1263    if( ( pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0 ) )
1264      biDecision = 1;
1265    else
1266      biDecision = 0;
1267  }
1268#endif
1269
1270  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
1271  {
1272    RefPicList eRefPicList = (iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
1273    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
1274
1275#if MERL_VSP_C0152
1276    if(!pcCU->getVSPIndex(uiPartAddr))
1277    {
1278      if ( iRefIdx[iRefList] < 0 )
1279      {
1280        continue;
1281      }
1282    }
1283    else
1284    {
1285
1286#if !MERL_Bi_VSP_D0166 //both lists should go
1287      if ( iRefList == REF_PIC_LIST_1 && iRefIdx[iRefList] < 0 ) // iRefIdx[iRefList] ==NOT_VALID
1288      {
1289        continue;
1290      }
1291#else
1292      //Reference list loop termination
1293      RefPicList privateVSPRefPicList = (RefPicList) pcCU->getVSPDir( uiPartAddr );
1294      if( (pcCU->getVSPIndex(uiPartAddr)!=0) &&  iRefList != privateVSPRefPicList && !biDecision  ) 
1295      {//when VSP mode, if it is uni prediction, the other reference list should skip
1296        continue;
1297      }
1298#endif
1299
1300    }
1301#else
1302    if ( iRefIdx[iRefList] < 0 )
1303    {
1304      continue;
1305    }
1306#endif
1307
1308    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
1309
1310    pcMbYuv = &m_acYuvPred[iRefList];
1311
1312#if MERL_Bi_VSP_D0166
1313    if(biDecision == 1)
1314#else
1315    if( pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0 )
1316#endif
1317    {
1318#if MERL_VSP_C0152
1319      xPredInterUni ( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, eRefPicList, pcMbYuv, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
1320#else
1321      xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
1322#endif
1323    }
1324    else
1325    {
1326#if FIX_LGE_WP_FOR_3D_C0223
1327      if ( ( pcCU->getSlice()->getPPS()->getUseWP()      && pcCU->getSlice()->getSliceType() == P_SLICE ) || 
1328         ( pcCU->getSlice()->getPPS()->getWPBiPredIdc() && pcCU->getSlice()->getSliceType() == B_SLICE ) )
1329#else
1330      if ( pcCU->getSlice()->getPPS()->getWPBiPredIdc() )
1331#endif
1332      {
1333#if MERL_VSP_C0152
1334        xPredInterUni ( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, eRefPicList, pcMbYuv, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
1335#else
1336        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, true );
1337#endif
1338      }
1339      else
1340      {
1341#if MERL_VSP_C0152
1342        xPredInterUni ( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, eRefPicList, pcMbYuv, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
1343#else
1344        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, iPartIdx, bPrdDepthMap, uiSubSampExpX, uiSubSampExpY, false );
1345#endif
1346      }
1347    }
1348  }
1349#if FIX_LGE_WP_FOR_3D_C0223
1350  if ( pcCU->getSlice()->getPPS()->getWPBiPredIdc() && pcCU->getSlice()->getSliceType() == B_SLICE  )
1351#else
1352  if ( pcCU->getSlice()->getPPS()->getWPBiPredIdc() )
1353#endif
1354  {
1355#if MERL_VSP_C0152
1356#if !MERL_Bi_VSP_D0166
1357    if(pcCU->getVSPIndex(uiPartAddr))
1358      m_acYuvPred[0].copyPartToPartYuv( rpcYuvPred, uiPartAddr, iWidth, iHeight );
1359    else
1360#endif
1361#endif
1362    xWeightedPredictionBi( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
1363  }
1364#if FIX_LGE_WP_FOR_3D_C0223
1365  else if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType() == P_SLICE )
1366
1367  {
1368#if MERL_VSP_C0152
1369#if !MERL_Bi_VSP_D0166
1370    if(pcCU->getVSPIndex(uiPartAddr))
1371      m_acYuvPred[0].copyPartToPartYuv( rpcYuvPred, uiPartAddr, iWidth, iHeight );
1372    else
1373#endif
1374#endif
1375      xWeightedPredictionUni( pcCU, &m_acYuvPred[0], uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, rpcYuvPred, iPartIdx ); 
1376  }
1377#endif
1378  else
1379  {
1380    if ( bPrdDepthMap )
1381    {
1382      xWeightedAveragePdm( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred, uiSubSampExpX, uiSubSampExpY );
1383    }
1384    else
1385    {
1386#if MERL_VSP_C0152
1387#if !MERL_Bi_VSP_D0166
1388      if(pcCU->getVSPIndex(uiPartAddr))
1389        m_acYuvPred[0].copyPartToPartYuv( rpcYuvPred, uiPartAddr, iWidth, iHeight );
1390      else
1391        xWeightedAverage( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
1392#else
1393      xWeightedAverage( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred, predDirVSP );
1394#endif
1395#else
1396      xWeightedAverage( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
1397#endif
1398    }
1399  }
1400}
1401
1402
1403
1404Void
1405TComPrediction::xPredInterPrdDepthMap( TComDataCU* pcCU, TComPicYuv* pcPicYuvRef, UInt uiPartAddr, TComMv* pcMv, Int iWidth, Int iHeight, UInt uiSubSampExpX, UInt uiSubSampExpY, TComYuv*& rpcYuv, UInt uiRShift, UInt uiOffset
1406#if LGE_ILLUCOMP_DEPTH_C0046
1407, Bool bICFlag
1408#endif
1409)
1410{
1411  Int     iShiftX     = 2 + uiSubSampExpX;
1412  Int     iShiftY     = 2 + uiSubSampExpY;
1413  Int     iAddX       = ( 1 << iShiftX ) >> 1;
1414  Int     iAddY       = ( 1 << iShiftY ) >> 1;
1415  Int     iHor        = ( pcMv->getHor() + iAddX ) >> iShiftX;
1416  Int     iVer        = ( pcMv->getVer() + iAddY ) >> iShiftY;
1417#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
1418  if( pcCU->getSlice()->getSPS()->isDepth() )
1419  {
1420    iHor = pcMv->getHor();
1421    iVer = pcMv->getVer();
1422  }
1423#endif
1424  Int     iRefStride  = pcPicYuvRef->getStride();
1425  Int     iDstStride  = rpcYuv->getStride();
1426  Int     iRefOffset  = iHor + iVer * iRefStride;
1427
1428  Pel*    piRefY      = pcPicYuvRef->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr ) + iRefOffset;
1429  Pel*    piDstY      = rpcYuv->getLumaAddr( uiPartAddr );
1430
1431  for( Int y = 0; y < iHeight; y++, piDstY += iDstStride, piRefY += iRefStride )
1432  {
1433    for( Int x = 0; x < iWidth; x++ )
1434    {
1435      piDstY[ x ] = ( piRefY[ x ] << uiRShift ) - uiOffset;
1436    }
1437  }
1438
1439#if LGE_ILLUCOMP_DEPTH_C0046
1440  if(bICFlag)
1441  {
1442    Int a, b, iShift;
1443    TComMv tTmpMV(pcMv->getHor()<<2, pcMv->getVer()<<2);
1444
1445    piRefY      = pcPicYuvRef->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr ) + iRefOffset;
1446    piDstY      = rpcYuv->getLumaAddr( uiPartAddr );
1447
1448    xGetLLSICPrediction(pcCU, &tTmpMV, pcPicYuvRef, a, b, iShift);
1449
1450    for( Int y = 0; y < iHeight; y++, piDstY += iDstStride, piRefY += iRefStride )
1451    {
1452      for( Int x = 0; x < iWidth; x++ )
1453      {
1454        if(uiOffset)
1455        {
1456          Int iIFshift = IF_INTERNAL_PREC - ( g_uiBitDepth + g_uiBitIncrement );
1457          piDstY[ x ] = ( (a*piDstY[ x ]+a*IF_INTERNAL_OFFS) >> iShift ) + b*(1<<iIFshift) - IF_INTERNAL_OFFS;
1458        }
1459        else
1460          piDstY[ x ] = Clip( ( (a*piDstY[ x ]) >> iShift ) + b );
1461      }
1462    }
1463  }
1464#endif
1465}
1466
1467
1468/**
1469 * \brief Generate motion-compensated luma block
1470 *
1471 * \param cu       Pointer to current CU
1472 * \param refPic   Pointer to reference picture
1473 * \param partAddr Address of block within CU
1474 * \param mv       Motion vector
1475 * \param width    Width of block
1476 * \param height   Height of block
1477 * \param dstPic   Pointer to destination picture
1478 * \param bi       Flag indicating whether bipred is used
1479 */
1480#if LGE_ILLUCOMP_B0045
1481Void TComPrediction::xPredInterLumaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi, Bool bICFlag
1482#if QC_ARP_D0177
1483    ,
1484    Int filterType
1485#endif
1486    )
1487#else
1488Void TComPrediction::xPredInterLumaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
1489#if QC_ARP_D0177
1490    ,
1491    Int filterType
1492#endif
1493    )
1494#endif
1495{
1496  Int refStride = refPic->getStride(); 
1497  Int refOffset = ( mv->getHor() >> 2 ) + ( mv->getVer() >> 2 ) * refStride;
1498  Pel *ref      = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1499 
1500  Int dstStride = dstPic->getStride();
1501  Pel *dst      = dstPic->getLumaAddr( partAddr );
1502 
1503  Int xFrac = mv->getHor() & 0x3;
1504  Int yFrac = mv->getVer() & 0x3;
1505
1506#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
1507  assert( ! cu->getSlice()->getIsDepth() || ( xFrac == 0 && yFrac == 0 ) );
1508#endif
1509
1510  if ( yFrac == 0 )
1511  {
1512    m_if.filterHorLuma( ref, refStride, dst, dstStride, width, height, xFrac,       !bi
1513#if QC_ARP_D0177
1514    ,
1515    filterType
1516#endif
1517    );
1518  }
1519  else if ( xFrac == 0 )
1520  {
1521    m_if.filterVerLuma( ref, refStride, dst, dstStride, width, height, yFrac, true, !bi
1522#if QC_ARP_D0177
1523    ,
1524    filterType
1525#endif
1526    );
1527  }
1528  else
1529  {
1530    Int tmpStride = m_filteredBlockTmp[0].getStride();
1531    Short *tmp    = m_filteredBlockTmp[0].getLumaAddr();
1532
1533    Int filterSize = NTAPS_LUMA;
1534    Int halfFilterSize = ( filterSize >> 1 );
1535
1536    m_if.filterHorLuma(ref - (halfFilterSize-1)*refStride, refStride, tmp, tmpStride, width, height+filterSize-1, xFrac, false     
1537 #if QC_ARP_D0177
1538    ,
1539    filterType
1540#endif
1541    );
1542    m_if.filterVerLuma(tmp + (halfFilterSize-1)*tmpStride, tmpStride, dst, dstStride, width, height,              yFrac, false, !bi
1543 #if QC_ARP_D0177
1544    ,
1545    filterType
1546#endif
1547    );   
1548  }
1549
1550#if LGE_ILLUCOMP_B0045
1551  if(bICFlag)
1552  {
1553    Int a, b, iShift, i, j;
1554
1555    xGetLLSICPrediction(cu, mv, refPic, a, b, iShift);
1556
1557    for (i = 0; i < height; i++)
1558    {
1559      for (j = 0; j < width; j++)
1560      {
1561        if(bi)
1562        {
1563          Int iIFshift = IF_INTERNAL_PREC - ( g_uiBitDepth + g_uiBitIncrement );
1564          dst[j] = ( (a*dst[j]+a*IF_INTERNAL_OFFS) >> iShift ) + b*(1<<iIFshift) - IF_INTERNAL_OFFS;
1565        }
1566        else
1567          dst[j] = Clip( ( (a*dst[j]) >> iShift ) + b );
1568      }
1569      dst += dstStride;
1570    }
1571  }
1572#endif
1573}
1574
1575/**
1576 * \brief Generate motion-compensated chroma block
1577 *
1578 * \param cu       Pointer to current CU
1579 * \param refPic   Pointer to reference picture
1580 * \param partAddr Address of block within CU
1581 * \param mv       Motion vector
1582 * \param width    Width of block
1583 * \param height   Height of block
1584 * \param dstPic   Pointer to destination picture
1585 * \param bi       Flag indicating whether bipred is used
1586 */
1587#if LGE_ILLUCOMP_B0045
1588Void TComPrediction::xPredInterChromaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi, Bool bICFlag
1589#if QC_ARP_D0177
1590    ,
1591    Int filterType
1592#endif   
1593    )
1594#else
1595Void TComPrediction::xPredInterChromaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
1596#if QC_ARP_D0177
1597  , Int filterType
1598#endif
1599  )
1600#endif
1601{
1602  Int     refStride  = refPic->getCStride();
1603  Int     dstStride  = dstPic->getCStride();
1604 
1605  Int     refOffset  = (mv->getHor() >> 3) + (mv->getVer() >> 3) * refStride;
1606 
1607  Pel*    refCb     = refPic->getCbAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1608  Pel*    refCr     = refPic->getCrAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1609 
1610  Pel* dstCb = dstPic->getCbAddr( partAddr );
1611  Pel* dstCr = dstPic->getCrAddr( partAddr );
1612 
1613  Int     xFrac  = mv->getHor() & 0x7;
1614  Int     yFrac  = mv->getVer() & 0x7;
1615  UInt    cxWidth  = width  >> 1;
1616  UInt    cxHeight = height >> 1;
1617 
1618  Int     extStride = m_filteredBlockTmp[0].getStride();
1619  Short*  extY      = m_filteredBlockTmp[0].getLumaAddr();
1620 
1621  Int filterSize = NTAPS_CHROMA;
1622 
1623  Int halfFilterSize = (filterSize>>1);
1624 
1625  if ( yFrac == 0 )
1626  {
1627    m_if.filterHorChroma(refCb, refStride, dstCb,  dstStride, cxWidth, cxHeight, xFrac, !bi
1628#if QC_ARP_D0177
1629    ,
1630    filterType
1631#endif   
1632    );   
1633    m_if.filterHorChroma(refCr, refStride, dstCr,  dstStride, cxWidth, cxHeight, xFrac, !bi
1634#if QC_ARP_D0177
1635    ,
1636    filterType
1637#endif   
1638    );   
1639  }
1640  else if ( xFrac == 0 )
1641  {
1642    m_if.filterVerChroma(refCb, refStride, dstCb, dstStride, cxWidth, cxHeight, yFrac, true, !bi
1643 #if QC_ARP_D0177
1644    ,
1645    filterType
1646#endif   
1647    );   
1648    m_if.filterVerChroma(refCr, refStride, dstCr, dstStride, cxWidth, cxHeight, yFrac, true, !bi
1649 #if QC_ARP_D0177
1650    ,
1651    filterType
1652#endif   
1653    );   
1654  }
1655  else
1656  {
1657    m_if.filterHorChroma(refCb - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
1658#if QC_ARP_D0177
1659    ,
1660    filterType
1661#endif   
1662    );
1663    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCb, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
1664#if QC_ARP_D0177
1665    ,
1666    filterType
1667#endif   
1668    );
1669   
1670    m_if.filterHorChroma(refCr - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
1671#if QC_ARP_D0177
1672    ,
1673    filterType
1674#endif   
1675    );
1676    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCr, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
1677#if QC_ARP_D0177
1678    ,
1679    filterType
1680#endif   
1681    );   
1682  }
1683#if LGE_ILLUCOMP_B0045
1684  if(bICFlag)
1685  {
1686    Int a, b, iShift, i, j;
1687    xGetLLSICPredictionChroma(cu, mv, refPic, a, b, iShift, 0); // Cb
1688    for (i = 0; i < cxHeight; i++)
1689    {
1690      for (j = 0; j < cxWidth; j++)
1691      {
1692        if(bi)
1693        {
1694          Int iIFshift = IF_INTERNAL_PREC - ( g_uiBitDepth + g_uiBitIncrement );
1695          dstCb[j] = ( (a*dstCb[j]+a*IF_INTERNAL_OFFS) >> iShift ) + b*(1<<iIFshift) - IF_INTERNAL_OFFS;
1696        }
1697        else
1698          dstCb[j] = Clip3(0, 255, ((a*dstCb[j])>>iShift)+b);
1699      }
1700      dstCb += dstStride;
1701    }
1702
1703    xGetLLSICPredictionChroma(cu, mv, refPic, a, b, iShift, 1); // Cr
1704    for (i = 0; i < cxHeight; i++)
1705    {
1706      for (j = 0; j < cxWidth; j++)
1707      {
1708        if(bi)
1709        {
1710          Int iIFshift = IF_INTERNAL_PREC - ( g_uiBitDepth + g_uiBitIncrement );
1711          dstCr[j] = ( (a*dstCr[j]+a*IF_INTERNAL_OFFS) >> iShift ) + b*(1<<iIFshift) - IF_INTERNAL_OFFS;
1712        }
1713        else
1714          dstCr[j] = Clip3(0, 255, ((a*dstCr[j])>>iShift)+b);
1715      }
1716      dstCr += dstStride;
1717    }
1718  }
1719#endif
1720}
1721
1722#if MERL_VSP_C0152
1723// Input:
1724// refPic: Ref picture. Full picture, with padding
1725// posX, posY:     PU position, texture
1726// sizeX, sizeY: PU size
1727// partAddr: z-order index
1728// mv: disparity vector. derived from neighboring blocks
1729//
1730// Output: dstPic, PU predictor 64x64
1731Void TComPrediction::xPredInterLumaBlkFromDM( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, Int iShiftPrec, TComMv* mv, UInt partAddr,Int posX, Int posY, Int sizeX, Int sizeY, Bool isDepth
1732#if !MERL_Bi_VSP_D0166
1733                                            , Int vspIdx
1734#endif
1735                                            , TComYuv *&dstPic
1736#if MERL_Bi_VSP_D0166
1737                                            , Bool bi
1738#endif         
1739                                            )
1740{
1741  Int widthLuma;
1742  Int heightLuma;
1743
1744  if (isDepth)
1745  {
1746    widthLuma   =  pPicBaseDepth->getWidth();
1747    heightLuma  =  pPicBaseDepth->getHeight();
1748  }
1749  else
1750  {
1751    widthLuma   =  refPic->getWidth();
1752    heightLuma  =  refPic->getHeight();
1753  }
1754
1755#if MERL_VSP_BLOCKSIZE_C0152 != 1
1756  Int widthDepth  = pPicBaseDepth->getWidth();
1757  Int heightDepth = pPicBaseDepth->getHeight();
1758#endif
1759
1760#if MERL_CVSP_D0165
1761  Int widthDepth  = pPicBaseDepth->getWidth();
1762  Int heightDepth = pPicBaseDepth->getHeight();
1763#endif
1764
1765  Int nTxtPerDepthX = widthLuma  / ( pPicBaseDepth->getWidth() );  // texture pixel # per depth pixel
1766  Int nTxtPerDepthY = heightLuma / ( pPicBaseDepth->getHeight() );
1767
1768  Int refStride = refPic->getStride();
1769  Int dstStride = dstPic->getStride();
1770  Int depStride =  pPicBaseDepth->getStride();
1771  Int depthPosX = Clip3(0,   widthLuma - sizeX,  (posX/nTxtPerDepthX) + (mv->getHor()>>2));
1772  Int depthPosY = Clip3(0,   heightLuma- sizeY,  (posY/nTxtPerDepthY) + (mv->getVer()>>2));
1773  Pel *ref    = refPic->getLumaAddr() + posX + posY * refStride;
1774  Pel *dst    = dstPic->getLumaAddr(partAddr);
1775  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;
1776
1777#if MERL_VSP_BLOCKSIZE_C0152 != 1
1778#if MERL_VSP_BLOCKSIZE_C0152 == 2
1779  Int  dW = sizeX>>1;
1780  Int  dH = sizeY>>1;
1781#endif
1782#if MERL_VSP_BLOCKSIZE_C0152 == 4
1783  Int  dW = sizeX>>2;
1784  Int  dH = sizeY>>2;
1785#endif
1786  {
1787    Pel* depthi = depth;
1788    for (Int j = 0; j < dH; j++)
1789    {
1790      for (Int i = 0; i < dW; i++)
1791      {
1792        Pel* depthTmp;
1793#if MERL_VSP_BLOCKSIZE_C0152 == 2
1794        if (depthPosX + (i<<1) < widthDepth)
1795          depthTmp = depthi + (i << 1);
1796        else
1797          depthTmp = depthi + (widthDepth - depthPosX - 1);
1798#endif
1799#if MERL_VSP_BLOCKSIZE_C0152 == 4
1800        if (depthPosX + (i<<2) < widthDepth)
1801          depthTmp = depthi + (i << 2);
1802        else
1803          depthTmp = depthi + (widthDepth - depthPosX - 1);
1804#endif
1805        Int maxV = 0;
1806#if MTK_DEPTH_TO_DISP_D0138
1807        for (Int blockj = 0; blockj < MERL_VSP_BLOCKSIZE_C0152; blockj+=(MERL_VSP_BLOCKSIZE_C0152-1))
1808#else
1809        for (Int blockj = 0; blockj < MERL_VSP_BLOCKSIZE_C0152; blockj++)
1810#endif
1811        {
1812          Int iX = 0;
1813#if MTK_DEPTH_TO_DISP_D0138
1814          for (Int blocki = 0; blocki < MERL_VSP_BLOCKSIZE_C0152; blocki+=(MERL_VSP_BLOCKSIZE_C0152-1))
1815#else
1816          for (Int blocki = 0; blocki < MERL_VSP_BLOCKSIZE_C0152; blocki++)
1817#endif
1818          {
1819            if (maxV < depthTmp[iX])
1820              maxV = depthTmp[iX];
1821#if MERL_VSP_BLOCKSIZE_C0152 == 2
1822            if (depthPosX + (i<<1) + blocki < widthDepth - 1)
1823#else // MERL_VSP_BLOCKSIZE_C0152 == 4
1824            if (depthPosX + (i<<2) + blocki < widthDepth - 1)
1825#endif
1826              iX++;
1827          }
1828#if MERL_VSP_BLOCKSIZE_C0152 == 2
1829          if (depthPosY + (j<<1) + blockj < heightDepth - 1)
1830#else // MERL_VSP_BLOCKSIZE_C0152 == 4
1831          if (depthPosY + (j<<2) + blockj < heightDepth - 1)
1832#endif
1833            depthTmp += depStride;
1834        }
1835        m_pDepth[i+j*dW] = maxV;
1836      } // end of i < dW
1837#if MERL_VSP_BLOCKSIZE_C0152 == 2
1838      if (depthPosY + ((j+1)<<1) < heightDepth)
1839        depthi += (depStride << 1);
1840      else
1841        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
1842#endif
1843#if MERL_VSP_BLOCKSIZE_C0152 == 4
1844      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
1845        depthi += (depStride << 2);
1846      else
1847        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
1848#endif
1849    }
1850  }
1851#endif
1852 
1853#if MERL_VSP_BLOCKSIZE_C0152 == 1
1854#if MERL_CVSP_D0165
1855  //get LUT based horizontal reference range
1856  Int range = 0;
1857  if( sizeX == 4 && sizeY == 8 )
1858    range = m_iRangeLuma[0];
1859  else if( sizeX == 8 && sizeY == 4 )
1860    range = m_iRangeLuma[1];
1861  else if( sizeX == 8 && sizeY == 8 )
1862    range = m_iRangeLuma[2];
1863  else if( sizeX == 8 && sizeY == 16 )
1864    range = m_iRangeLuma[3];
1865  else if( sizeX == 16 && sizeY == 8 )
1866    range = m_iRangeLuma[4];
1867  else if( sizeX == 16 && sizeY == 16 )
1868    range = m_iRangeLuma[5];
1869  else if( sizeX == 16 && sizeY == 32 )
1870    range = m_iRangeLuma[6];
1871  else if( sizeX == 32 && sizeY == 16 )
1872    range = m_iRangeLuma[7];
1873  else if( sizeX == 32 && sizeY == 32 )
1874    range = m_iRangeLuma[8];
1875  else if( sizeX == 32 && sizeY == 64 )
1876    range = m_iRangeLuma[9];
1877  else if( sizeX == 64 && sizeY == 32 )
1878    range = m_iRangeLuma[10];
1879  else if( sizeX == 64 && sizeY == 64 )
1880    range = m_iRangeLuma[11];
1881  else 
1882    assert(0);
1883
1884  // The minimum depth value
1885  Int minRelativePos = 5000;
1886  Int maxRelativePos = -5000;
1887
1888  Pel* depthTemp, *depthInitial=depth;
1889  for (Int yTxt =0; yTxt<sizeY; yTxt++)
1890  {
1891    for (Int xTxt =0; xTxt<sizeX; xTxt++)
1892    {
1893      if (depthPosX+xTxt < widthDepth)
1894        depthTemp = depthInitial + xTxt;
1895      else
1896        depthTemp = depthInitial + (widthDepth - depthPosX - 1);
1897
1898      Int disparity = pShiftLUT[ *depthTemp ] << iShiftPrec;
1899      Int disparityInt = disparity >> 2;
1900
1901      if( disparity <= 0)
1902      {
1903        if (minRelativePos > disparityInt+xTxt)
1904            minRelativePos = disparityInt+xTxt;
1905      }
1906      else
1907      {
1908        if (maxRelativePos < disparityInt+xTxt)
1909            maxRelativePos = disparityInt+xTxt;
1910      }
1911    }
1912    if (depthPosY+yTxt < heightDepth)
1913      depthInitial = depthInitial + depStride;
1914  }
1915
1916  Int disparity_tmp = pShiftLUT[ *depth ] << iShiftPrec;
1917  if (disparity_tmp <= 0)
1918    maxRelativePos = minRelativePos + range -1 ;
1919  else
1920    minRelativePos = maxRelativePos - range +1 ;
1921#endif
1922#endif
1923
1924#if MERL_VSP_BLOCKSIZE_C0152 != 1
1925  Int yDepth = 0;
1926#endif
1927  for ( Int yTxt = 0; yTxt < sizeY; yTxt += nTxtPerDepthY )
1928  {
1929    for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth++ )
1930    {
1931      Pel repDepth = 0; // to store the depth value used for warping
1932#if MERL_VSP_BLOCKSIZE_C0152 == 1
1933      repDepth = depth[xDepth];
1934#endif
1935#if MERL_VSP_BLOCKSIZE_C0152 == 2
1936      repDepth = m_pDepth[(xTxt>>1) + (yTxt>>1)*dW];
1937#endif
1938#if MERL_VSP_BLOCKSIZE_C0152 == 4
1939      repDepth = m_pDepth[(xTxt>>2) + (yTxt>>2)*dW];
1940#endif
1941
1942      assert( repDepth >= 0 && repDepth <= 255 );
1943      Int disparity = pShiftLUT[ repDepth ] << iShiftPrec;
1944      Int refOffset = xTxt + (disparity >> 2);
1945      Int xFrac = disparity & 0x3;
1946#if MERL_CVSP_D0165
1947      if(refOffset<minRelativePos || refOffset>maxRelativePos)
1948        xFrac = 0;
1949      refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
1950#endif
1951      Int absX  = posX + refOffset;
1952
1953      if (xFrac == 0)
1954        absX = Clip3(0, widthLuma-1, absX);
1955      else
1956        absX = Clip3(4, widthLuma-5, absX);
1957
1958      refOffset = absX - posX;
1959
1960      assert( ref[refOffset] >= 0 && ref[refOffset]<= 255 );
1961#if MERL_Bi_VSP_D0166
1962      m_if.filterHorLuma( &ref[refOffset], refStride, &dst[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi );
1963#else
1964      m_if.filterHorLuma( &ref[refOffset], refStride, &dst[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, true );
1965#endif
1966
1967    }
1968    ref   += refStride*nTxtPerDepthY;
1969    dst   += dstStride*nTxtPerDepthY;
1970    depth += depStride;
1971#if MERL_VSP_BLOCKSIZE_C0152 != 1
1972    yDepth++;
1973#endif
1974
1975  }
1976}
1977
1978Void TComPrediction::xPredInterChromaBlkFromDM ( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, Int iShiftPrec, TComMv*mv, UInt partAddr, Int posX, Int posY, Int sizeX, Int sizeY, Bool isDepth
1979#if !MERL_Bi_VSP_D0166
1980                                               , Int vspIdx
1981#endif
1982                                               , TComYuv *&dstPic
1983#if MERL_Bi_VSP_D0166
1984                                               , Bool bi
1985#endif
1986                                               )
1987{
1988  Int refStride = refPic->getCStride();
1989  Int dstStride = dstPic->getCStride();
1990  Int depStride = pPicBaseDepth->getStride();
1991
1992  Int widthChroma, heightChroma;
1993  if( isDepth)
1994  {
1995     widthChroma   = pPicBaseDepth->getWidth()>>1;
1996     heightChroma  = pPicBaseDepth->getHeight()>>1;
1997  }
1998  else
1999  {
2000     widthChroma   = refPic->getWidth()>>1;
2001     heightChroma  = refPic->getHeight()>>1;
2002  }
2003
2004  // Below is only for Texture chroma component
2005
2006  Int widthDepth  = pPicBaseDepth->getWidth();
2007  Int heightDepth = pPicBaseDepth->getHeight();
2008
2009  Int nTxtPerDepthX, nTxtPerDepthY;  // Number of texture samples per one depth sample
2010  Int nDepthPerTxtX, nDepthPerTxtY;  // Number of depth samples per one texture sample
2011
2012  Int depthPosX;  // Starting position in depth image
2013  Int depthPosY;
2014
2015  if ( widthChroma > widthDepth )
2016  {
2017    nTxtPerDepthX = widthChroma / widthDepth;
2018    nDepthPerTxtX = 1;
2019    depthPosX = posX / nTxtPerDepthX + (mv->getHor()>>2);        //mv denotes the disparity for VSP
2020  }
2021  else
2022  {
2023    nTxtPerDepthX = 1;
2024    nDepthPerTxtX = widthDepth / widthChroma;
2025    depthPosX = posX * nDepthPerTxtX + (mv->getHor()>>2);        //mv denotes the disparity for VSP
2026  }
2027  depthPosX = Clip3(0, widthDepth - (sizeX<<1), depthPosX);
2028  if ( heightChroma > heightDepth )
2029  {
2030    nTxtPerDepthY = heightChroma / heightDepth;
2031    nDepthPerTxtY = 1;
2032    depthPosY = posY / nTxtPerDepthY + (mv->getVer()>>2);     //mv denotes the disparity for VSP
2033  }
2034  else
2035  {
2036    nTxtPerDepthY = 1;
2037    nDepthPerTxtY = heightDepth / heightChroma;
2038    depthPosY = posY * nDepthPerTxtY + (mv->getVer()>>2);     //mv denotes the disparity for VSP
2039  }
2040  depthPosY = Clip3(0, heightDepth - (sizeY<<1), depthPosY);
2041
2042  Pel *refCb  = refPic->getCbAddr() + posX + posY * refStride;
2043  Pel *refCr  = refPic->getCrAddr() + posX + posY * refStride;
2044  Pel *dstCb  = dstPic->getCbAddr(partAddr);
2045  Pel *dstCr  = dstPic->getCrAddr(partAddr);
2046  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;  // move the pointer to the current depth pixel position
2047 
2048  Int refStrideBlock = refStride * nTxtPerDepthY;
2049  Int dstStrideBlock = dstStride * nTxtPerDepthY;
2050  Int depStrideBlock = depStride * nDepthPerTxtY;
2051
2052  if ( widthChroma > widthDepth ) // We assume
2053  {
2054    assert( heightChroma > heightDepth );
2055    printf("This branch should never been reached.\n");
2056    exit(0);
2057  }
2058  else
2059  {
2060#if MERL_VSP_BLOCKSIZE_C0152 == 1
2061  Int  dW = sizeX;
2062  Int  dH = sizeY;
2063  Int  sW = 2; // search window size
2064  Int  sH = 2;
2065#endif
2066#if MERL_VSP_BLOCKSIZE_C0152 == 2
2067  Int  dW = sizeX;
2068  Int  dH = sizeY;
2069  Int  sW = 2; // search window size
2070  Int  sH = 2;
2071#endif
2072#if MERL_VSP_BLOCKSIZE_C0152 == 4
2073  Int  dW = sizeX>>1;
2074  Int  dH = sizeY>>1;
2075  Int  sW = 4; // search window size
2076  Int  sH = 4;
2077#endif
2078
2079  {
2080    Pel* depthi = depth;
2081    for (Int j = 0; j < dH; j++)
2082    {
2083      for (Int i = 0; i < dW; i++)
2084      {
2085        Pel* depthTmp;
2086#if MERL_VSP_BLOCKSIZE_C0152 == 1
2087        depthTmp = depthi + (i << 1);
2088#endif
2089#if MERL_VSP_BLOCKSIZE_C0152 == 2
2090        if (depthPosX + (i<<1) < widthDepth)
2091          depthTmp = depthi + (i << 1);
2092        else
2093          depthTmp = depthi + (widthDepth - depthPosX - 1);
2094#endif
2095#if MERL_VSP_BLOCKSIZE_C0152 == 4
2096        if (depthPosX + (i<<2) < widthDepth)
2097          depthTmp = depthi + (i << 2);
2098        else
2099          depthTmp = depthi + (widthDepth - depthPosX - 1);
2100#endif
2101        Int maxV = 0;
2102        for (Int blockj = 0; blockj < sH; blockj++)
2103        {
2104          Int iX = 0;
2105          for (Int blocki = 0; blocki < sW; blocki++)
2106          {
2107            if (maxV < depthTmp[iX])
2108              maxV = depthTmp[iX];
2109            if (depthPosX + i*sW + blocki < widthDepth - 1)
2110              iX++;
2111          }
2112          if (depthPosY + j*sH + blockj < heightDepth - 1)
2113            depthTmp += depStride;
2114        }
2115        m_pDepth[i+j*dW] = maxV;
2116      } // end of i < dW
2117#if MERL_VSP_BLOCKSIZE_C0152 == 1
2118      if (depthPosY + ((j+1)<<1) < heightDepth)
2119        depthi += (depStride << 1);
2120      else
2121        depthi  = depth + (heightDepth-1)*depStride;
2122#endif
2123#if MERL_VSP_BLOCKSIZE_C0152 == 2
2124      if (depthPosY + ((j+1)<<1) < heightDepth)
2125        depthi += (depStride << 1);
2126      else
2127        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
2128#endif
2129#if MERL_VSP_BLOCKSIZE_C0152 == 4
2130      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
2131        depthi += (depStride << 2);
2132      else
2133        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
2134#endif
2135    }
2136  }
2137
2138
2139#if MERL_VSP_BLOCKSIZE_C0152 == 1
2140#if MERL_CVSP_D0165
2141  //get LUT based horizontal reference range
2142  Int range=0;
2143  if( sizeX == 2 && sizeY == 4 )
2144    range = m_iRangeChroma[0];
2145  else if( sizeX == 4 && sizeY == 2 )
2146    range = m_iRangeChroma[1];
2147  else if( sizeX == 4 && sizeY == 4 )
2148    range = m_iRangeChroma[2];
2149  else if( sizeX == 4 && sizeY == 8 )
2150    range = m_iRangeChroma[3];
2151  else if( sizeX == 8 && sizeY == 4 )
2152    range = m_iRangeChroma[4];
2153  else if( sizeX == 8 && sizeY == 8 )
2154    range = m_iRangeChroma[5];
2155  else if( sizeX == 8 && sizeY == 16 )
2156    range = m_iRangeChroma[6];
2157  else if( sizeX == 16 && sizeY == 8 )
2158    range = m_iRangeChroma[7];
2159  else if( sizeX == 16 && sizeY == 16 )
2160    range = m_iRangeChroma[8];
2161  else if( sizeX == 16 && sizeY == 32 )
2162    range = m_iRangeChroma[9];
2163  else if( sizeX == 32 && sizeY == 16 )
2164    range = m_iRangeChroma[10];
2165  else if( sizeX == 32 && sizeY == 32 )
2166    range = m_iRangeChroma[11];
2167  else
2168    assert(0);
2169 
2170  // The minimum depth value
2171  Int minRelativePos = 5000;
2172  Int maxRelativePos = -5000;
2173
2174  Int depthTmp;
2175  for (Int yTxt=0; yTxt<sizeY; yTxt++)
2176  {
2177    for (Int xTxt=0; xTxt<sizeX; xTxt++)
2178    {
2179      depthTmp = m_pDepth[xTxt+yTxt*dW];
2180      Int disparity = pShiftLUT[ depthTmp ] << iShiftPrec;
2181      Int disparityInt = disparity >> 3;//in chroma resolution
2182
2183      if (disparityInt < 0)
2184      {
2185        if (minRelativePos > disparityInt+xTxt)
2186            minRelativePos = disparityInt+xTxt;
2187      }
2188      else
2189      {
2190        if (maxRelativePos < disparityInt+xTxt)
2191            maxRelativePos = disparityInt+xTxt;
2192      }
2193    }
2194  }
2195
2196  depthTmp = m_pDepth[0];
2197  Int disparity_tmp = pShiftLUT[ depthTmp ] << iShiftPrec;
2198  if ( disparity_tmp < 0 )
2199    maxRelativePos = minRelativePos + range - 1;
2200  else
2201    minRelativePos = maxRelativePos - range + 1;
2202
2203#endif
2204#endif
2205
2206    // (sizeX, sizeY) is Chroma block size
2207    for ( Int yTxt = 0, yDepth = 0; yTxt < sizeY; yTxt += nTxtPerDepthY, yDepth += nDepthPerTxtY )
2208    {
2209      for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth += nDepthPerTxtX )
2210      {
2211        Pel repDepth = 0; // to store the depth value used for warping
2212#if MERL_VSP_BLOCKSIZE_C0152 == 1
2213        repDepth = m_pDepth[(xTxt) + (yTxt)*dW];
2214#endif
2215#if MERL_VSP_BLOCKSIZE_C0152 == 2
2216        repDepth = m_pDepth[(xTxt) + (yTxt)*dW];
2217#endif
2218#if MERL_VSP_BLOCKSIZE_C0152 == 4
2219        repDepth = m_pDepth[(xTxt>>1) + (yTxt>>1)*dW];
2220#endif
2221
2222      // calculate the offset in the reference picture
2223        Int disparity = pShiftLUT[ repDepth ] << iShiftPrec;
2224        Int refOffset = xTxt + (disparity >> 3); // in integer pixel in chroma image
2225        Int xFrac = disparity & 0x7;
2226#if MERL_CVSP_D0165
2227        if(refOffset < minRelativePos || refOffset > maxRelativePos)
2228          xFrac = 0;
2229        refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
2230#endif
2231        Int absX  = posX + refOffset;
2232
2233        if (xFrac == 0)
2234          absX = Clip3(0, widthChroma-1, absX);
2235        else
2236          absX = Clip3(4, widthChroma-5, absX);
2237
2238        refOffset = absX - posX;
2239
2240        assert( refCb[refOffset] >= 0 && refCb[refOffset]<= 255 );
2241        assert( refCr[refOffset] >= 0 && refCr[refOffset]<= 255 );
2242#if MERL_Bi_VSP_D0166
2243        m_if.filterHorChroma(&refCb[refOffset], refStride, &dstCb[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
2244        m_if.filterHorChroma(&refCr[refOffset], refStride, &dstCr[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
2245#else
2246        m_if.filterHorChroma(&refCb[refOffset], refStride, &dstCb[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, true);
2247        m_if.filterHorChroma(&refCr[refOffset], refStride, &dstCr[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, true);
2248#endif
2249
2250      }
2251      refCb += refStrideBlock;
2252      refCr += refStrideBlock;
2253      dstCb += dstStrideBlock;
2254      dstCr += dstStrideBlock;
2255      depth += depStrideBlock;
2256    }
2257  }
2258
2259}
2260
2261#endif // MERL_VSP_C0152
2262
2263#if DEPTH_MAP_GENERATION
2264Void TComPrediction::xWeightedAveragePdm( TComDataCU* pcCU, TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, Int iRefIdx0, Int iRefIdx1, UInt uiPartIdx, Int iWidth, Int iHeight, TComYuv*& rpcYuvDst, UInt uiSubSampExpX, UInt uiSubSampExpY )
2265{
2266
2267  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
2268  {
2269    rpcYuvDst->addAvgPdm( pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY );
2270  }
2271  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
2272  {
2273    pcYuvSrc0->copyPartToPartYuvPdm( rpcYuvDst, uiPartIdx, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY );
2274  }
2275  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
2276  {
2277    pcYuvSrc1->copyPartToPartYuvPdm( rpcYuvDst, uiPartIdx, iWidth, iHeight, uiSubSampExpX, uiSubSampExpY );
2278  }
2279  else
2280  {
2281    assert (0);
2282  }
2283}
2284#endif
2285
2286Void TComPrediction::xWeightedAverage( TComDataCU* pcCU, TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, Int iRefIdx0, Int iRefIdx1, UInt uiPartIdx, Int iWidth, Int iHeight, TComYuv*& rpcYuvDst
2287#if MERL_Bi_VSP_D0166
2288                                 , Int predDirVSP
2289#endif
2290  )
2291{
2292#if MERL_Bi_VSP_D0166
2293  Bool isVSP = 0;
2294  if (pcCU->getVSPIndex(uiPartIdx)!=0)//is VSP
2295  {
2296    isVSP = 1;
2297  }
2298
2299  if(( !isVSP && iRefIdx0 >= 0 && iRefIdx1 >= 0 ) || ( isVSP && predDirVSP == 3 ))
2300#else
2301  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
2302#endif
2303  {
2304    rpcYuvDst->addAvg( pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight );
2305  }
2306#if MERL_Bi_VSP_D0166
2307  else if ( ( !isVSP && iRefIdx0 >= 0 && iRefIdx1 <  0 ) || ( isVSP && predDirVSP == 1))
2308#else
2309  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
2310#endif
2311  {
2312    pcYuvSrc0->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
2313  }
2314#if MERL_Bi_VSP_D0166
2315  else if (( !isVSP && iRefIdx0 <  0 && iRefIdx1 >= 0 ) || ( isVSP && predDirVSP == 2))
2316#else
2317  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
2318#endif
2319  {
2320    pcYuvSrc1->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
2321  }
2322#if MERL_Bi_VSP_D0166
2323  else
2324  {//for debug test only
2325    assert(0);
2326  }
2327#endif
2328}
2329
2330// AMVP
2331Void TComPrediction::getMvPredAMVP( TComDataCU* pcCU, UInt uiPartIdx, UInt uiPartAddr, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMvPred )
2332{
2333  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
2334
2335  if( pcCU->getAMVPMode(uiPartAddr) == AM_NONE || (pcAMVPInfo->iN <= 1 && pcCU->getAMVPMode(uiPartAddr) == AM_EXPL) )
2336  {
2337    rcMvPred = pcAMVPInfo->m_acMvCand[0];
2338
2339    pcCU->setMVPIdxSubParts( 0, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
2340    pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
2341    return;
2342  }
2343
2344  assert(pcCU->getMVPIdx(eRefPicList,uiPartAddr) >= 0);
2345  rcMvPred = pcAMVPInfo->m_acMvCand[pcCU->getMVPIdx(eRefPicList,uiPartAddr)];
2346  return;
2347}
2348
2349/** Function for deriving planar intra prediction.
2350 * \param pSrc pointer to reconstructed sample array
2351 * \param srcStride the stride of the reconstructed sample array
2352 * \param rpDst reference to pointer for the prediction sample array
2353 * \param dstStride the stride of the prediction sample array
2354 * \param width the width of the block
2355 * \param height the height of the block
2356 *
2357 * This function derives the prediction samples for planar mode (intra coding).
2358 */
2359Void TComPrediction::xPredIntraPlanar( Int* pSrc, Int srcStride, Pel* rpDst, Int dstStride, UInt width, UInt height )
2360{
2361  assert(width == height);
2362
2363  Int k, l, bottomLeft, topRight;
2364  Int horPred;
2365  Int leftColumn[MAX_CU_SIZE], topRow[MAX_CU_SIZE], bottomRow[MAX_CU_SIZE], rightColumn[MAX_CU_SIZE];
2366  UInt blkSize = width;
2367  UInt offset2D = width;
2368  UInt shift1D = g_aucConvertToBit[ width ] + 2;
2369  UInt shift2D = shift1D + 1;
2370
2371  // Get left and above reference column and row
2372  for(k=0;k<blkSize+1;k++)
2373  {
2374    topRow[k] = pSrc[k-srcStride];
2375    leftColumn[k] = pSrc[k*srcStride-1];
2376  }
2377
2378  // Prepare intermediate variables used in interpolation
2379  bottomLeft = leftColumn[blkSize];
2380  topRight   = topRow[blkSize];
2381  for (k=0;k<blkSize;k++)
2382  {
2383    bottomRow[k]   = bottomLeft - topRow[k];
2384    rightColumn[k] = topRight   - leftColumn[k];
2385    topRow[k]      <<= shift1D;
2386    leftColumn[k]  <<= shift1D;
2387  }
2388
2389  // Generate prediction signal
2390  for (k=0;k<blkSize;k++)
2391  {
2392    horPred = leftColumn[k] + offset2D;
2393    for (l=0;l<blkSize;l++)
2394    {
2395      horPred += rightColumn[k];
2396      topRow[l] += bottomRow[l];
2397      rpDst[k*dstStride+l] = ( (horPred + topRow[l]) >> shift2D );
2398    }
2399  }
2400}
2401
2402/** Function for deriving chroma LM intra prediction.
2403 * \param pcPattern pointer to neighbouring pixel access pattern
2404 * \param piSrc pointer to reconstructed chroma sample array
2405 * \param pPred pointer for the prediction sample array
2406 * \param uiPredStride the stride of the prediction sample array
2407 * \param uiCWidth the width of the chroma block
2408 * \param uiCHeight the height of the chroma block
2409 * \param uiChromaId boolean indication of chroma component
2410 *
2411 * This function derives the prediction samples for chroma LM mode (chroma intra coding)
2412 */
2413Void TComPrediction::predLMIntraChroma( TComPattern* pcPattern, Int* piSrc, Pel* pPred, UInt uiPredStride, UInt uiCWidth, UInt uiCHeight, UInt uiChromaId )
2414{
2415  UInt uiWidth  = 2 * uiCWidth;
2416
2417  xGetLLSPrediction( pcPattern, piSrc+uiWidth+2, uiWidth+1, pPred, uiPredStride, uiCWidth, uiCHeight, 1 ); 
2418}
2419
2420/** Function for deriving downsampled luma sample of current chroma block and its above, left causal pixel
2421 * \param pcPattern pointer to neighbouring pixel access pattern
2422 * \param uiCWidth the width of the chroma block
2423 * \param uiCHeight the height of the chroma block
2424 *
2425 * This function derives downsampled luma sample of current chroma block and its above, left causal pixel
2426 */
2427Void TComPrediction::getLumaRecPixels( TComPattern* pcPattern, UInt uiCWidth, UInt uiCHeight )
2428{
2429  UInt uiWidth  = 2 * uiCWidth;
2430  UInt uiHeight = 2 * uiCHeight; 
2431
2432  Pel* pRecSrc = pcPattern->getROIY();
2433  Pel* pDst0 = m_pLumaRecBuffer + m_iLumaRecStride + 1;
2434
2435  Int iRecSrcStride = pcPattern->getPatternLStride();
2436  Int iRecSrcStride2 = iRecSrcStride << 1;
2437  Int iDstStride = m_iLumaRecStride;
2438  Int iSrcStride = ( max( uiWidth, uiHeight ) << 1 ) + 1;
2439
2440  Int* ptrSrc = pcPattern->getAdiOrgBuf( uiWidth, uiHeight, m_piYuvExt );
2441
2442  // initial pointers
2443  Pel* pDst = pDst0 - 1 - iDstStride; 
2444  Int* piSrc = ptrSrc;
2445
2446  // top left corner downsampled from ADI buffer
2447  // don't need this point
2448
2449  // top row downsampled from ADI buffer
2450  pDst++;     
2451  piSrc ++;
2452  for (Int i = 0; i < uiCWidth; i++)
2453  {
2454    pDst[i] = ((piSrc[2*i] * 2 ) + piSrc[2*i - 1] + piSrc[2*i + 1] + 2) >> 2;
2455  }
2456
2457  // left column downsampled from ADI buffer
2458  pDst = pDst0 - 1; 
2459  piSrc = ptrSrc + iSrcStride;
2460  for (Int j = 0; j < uiCHeight; j++)
2461  {
2462    pDst[0] = ( piSrc[0] + piSrc[iSrcStride] ) >> 1;
2463    piSrc += iSrcStride << 1; 
2464    pDst += iDstStride;   
2465  }
2466
2467  // inner part from reconstructed picture buffer
2468  for( Int j = 0; j < uiCHeight; j++ )
2469  {
2470    for (Int i = 0; i < uiCWidth; i++)
2471    {
2472      pDst0[i] = (pRecSrc[2*i] + pRecSrc[2*i + iRecSrcStride]) >> 1;
2473    }
2474
2475    pDst0 += iDstStride;
2476    pRecSrc += iRecSrcStride2;
2477  }
2478}
2479
2480/** Function for deriving the positon of first non-zero binary bit of a value
2481 * \param x input value
2482 *
2483 * This function derives the positon of first non-zero binary bit of a value
2484 */
2485Int GetMSB( UInt x )
2486{
2487  Int iMSB = 0, bits = ( sizeof( Int ) << 3 ), y = 1;
2488
2489  while( x > 1 )
2490  {
2491    bits >>= 1;
2492    y = x >> bits;
2493
2494    if( y )
2495    {
2496      x = y;
2497      iMSB += bits;
2498    }
2499  }
2500
2501  iMSB+=y;
2502
2503  return iMSB;
2504}
2505
2506/** Function for counting leading number of zeros/ones
2507 * \param x input value
2508 \ This function counts leading number of zeros for positive numbers and
2509 \ leading number of ones for negative numbers. This can be implemented in
2510 \ single instructure cycle on many processors.
2511 */
2512
2513Short CountLeadingZerosOnes (Short x)
2514{
2515  Short clz;
2516  Short i;
2517
2518  if(x == 0)
2519  {
2520    clz = 0;
2521  }
2522  else
2523  {
2524    if (x == -1)
2525    {
2526      clz = 15;
2527    }
2528    else
2529    {
2530      if(x < 0)
2531      {
2532        x = ~x;
2533      }
2534      clz = 15;
2535      for(i = 0;i < 15;++i)
2536      {
2537        if(x) 
2538        {
2539          clz --;
2540        }
2541        x = x >> 1;
2542      }
2543    }
2544  }
2545  return clz;
2546}
2547
2548/** Function for deriving LM intra prediction.
2549 * \param pcPattern pointer to neighbouring pixel access pattern
2550 * \param pSrc0 pointer to reconstructed chroma sample array
2551 * \param iSrcStride the stride of reconstructed chroma sample array
2552 * \param pDst0 reference to pointer for the prediction sample array
2553 * \param iDstStride the stride of the prediction sample array
2554 * \param uiWidth the width of the chroma block
2555 * \param uiHeight the height of the chroma block
2556 * \param uiExt0 line number of neiggboirng pixels for calculating LM model parameter, default value is 1
2557 *
2558 * This function derives the prediction samples for chroma LM mode (chroma intra coding)
2559 */
2560Void TComPrediction::xGetLLSPrediction( TComPattern* pcPattern, Int* pSrc0, Int iSrcStride, Pel* pDst0, Int iDstStride, UInt uiWidth, UInt uiHeight, UInt uiExt0 )
2561{
2562
2563  Pel  *pDst, *pLuma;
2564  Int  *pSrc;
2565
2566  Int  iLumaStride = m_iLumaRecStride;
2567  Pel* pLuma0 = m_pLumaRecBuffer + uiExt0 * iLumaStride + uiExt0;
2568
2569  Int i, j, iCountShift = 0;
2570
2571  UInt uiExt = uiExt0;
2572
2573  // LLS parameters estimation -->
2574
2575  Int x = 0, y = 0, xx = 0, xy = 0;
2576
2577  pSrc  = pSrc0  - iSrcStride;
2578  pLuma = pLuma0 - iLumaStride;
2579
2580  for( j = 0; j < uiWidth; j++ )
2581  {
2582    x += pLuma[j];
2583    y += pSrc[j];
2584    xx += pLuma[j] * pLuma[j];
2585    xy += pLuma[j] * pSrc[j];
2586  }
2587  iCountShift += g_aucConvertToBit[ uiWidth ] + 2;
2588
2589  pSrc  = pSrc0 - uiExt;
2590  pLuma = pLuma0 - uiExt;
2591
2592  for( i = 0; i < uiHeight; i++ )
2593  {
2594    x += pLuma[0];
2595    y += pSrc[0];
2596    xx += pLuma[0] * pLuma[0];
2597    xy += pLuma[0] * pSrc[0];
2598
2599    pSrc  += iSrcStride;
2600    pLuma += iLumaStride;
2601  }
2602  iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 2 );
2603
2604  Int iTempShift = ( g_uiBitDepth + g_uiBitIncrement ) + g_aucConvertToBit[ uiWidth ] + 3 - 15;
2605
2606  if(iTempShift > 0)
2607  {
2608    x  = ( x +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2609    y  = ( y +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2610    xx = ( xx + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2611    xy = ( xy + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2612    iCountShift -= iTempShift;
2613  }
2614
2615  Int a, b, iShift = 13;
2616
2617  if( iCountShift == 0 )
2618  {
2619    a = 0;
2620    b = 1 << (g_uiBitDepth + g_uiBitIncrement - 1);
2621    iShift = 0;
2622  }
2623  else
2624  {
2625    Int a1 = ( xy << iCountShift ) - y * x;
2626    Int a2 = ( xx << iCountShift ) - x * x;             
2627
2628    {
2629      const Int iShiftA2 = 6;
2630      const Int iShiftA1 = 15;
2631      const Int iAccuracyShift = 15;
2632
2633      Int iScaleShiftA2 = 0;
2634      Int iScaleShiftA1 = 0;
2635      Int a1s = a1;
2636      Int a2s = a2;
2637
2638      iScaleShiftA1 = GetMSB( abs( a1 ) ) - iShiftA1;
2639      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2; 
2640
2641      if( iScaleShiftA1 < 0 )
2642      {
2643        iScaleShiftA1 = 0;
2644      }
2645     
2646      if( iScaleShiftA2 < 0 )
2647      {
2648        iScaleShiftA2 = 0;
2649      }
2650     
2651      Int iScaleShiftA = iScaleShiftA2 + iAccuracyShift - iShift - iScaleShiftA1;
2652
2653      a2s = a2 >> iScaleShiftA2;
2654
2655      a1s = a1 >> iScaleShiftA1;
2656
2657      if (a2s >= 1)
2658      {
2659        a = a1s * m_uiaShift[ a2s - 1];
2660      }
2661      else
2662      {
2663        a = 0;
2664      }
2665     
2666      if( iScaleShiftA < 0 )
2667      {
2668        a = a << -iScaleShiftA;
2669      }
2670      else
2671      {
2672        a = a >> iScaleShiftA;
2673      }
2674     
2675       a = Clip3(-( 1 << 15 ), ( 1 << 15 ) - 1, a); 
2676     
2677      Int minA = -(1 << (6));
2678      Int maxA = (1 << 6) - 1;
2679      if( a <= maxA && a >= minA )
2680      {
2681        // do nothing
2682      }
2683      else
2684      {
2685        Short n = CountLeadingZerosOnes(a);
2686        a = a >> (9-n);
2687        iShift -= (9-n);
2688      }
2689
2690      b = (  y - ( ( a * x ) >> iShift ) + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
2691    }
2692  }   
2693
2694  // <-- end of LLS parameters estimation
2695
2696  // get prediction -->
2697  uiExt = uiExt0;
2698  pLuma = pLuma0;
2699  pDst = pDst0;
2700
2701  for( i = 0; i < uiHeight; i++ )
2702  {
2703    for( j = 0; j < uiWidth; j++ )
2704    {
2705      pDst[j] = Clip( ( ( a * pLuma[j] ) >> iShift ) + b );
2706    }
2707   
2708    pDst  += iDstStride;
2709    pLuma += iLumaStride;
2710  }
2711  // <-- end of get prediction
2712
2713}
2714
2715
2716#if LGE_ILLUCOMP_B0045
2717/** Function for deriving LM illumination compensation.
2718 */
2719Void TComPrediction::xGetLLSICPrediction(TComDataCU* pcCU, TComMv *pMv, TComPicYuv *pRefPic, Int &a, Int &b, Int &iShift)
2720{
2721  TComPicYuv *pRecPic = pcCU->getPic()->getPicYuvRec();
2722  Pel *pRec, *pRef;
2723  UInt uiWidth, uiHeight, uiTmpPartIdx;
2724  Int iRecStride = pRecPic->getStride(), iRefStride = pRefPic->getStride();
2725  Int iCUPelX, iCUPelY, iRefX, iRefY, iRefOffset;
2726
2727  iCUPelX = pcCU->getCUPelX() + g_auiRasterToPelX[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
2728  iCUPelY = pcCU->getCUPelY() + g_auiRasterToPelY[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
2729  iRefX   = iCUPelX + (pMv->getHor() >> 2);
2730  iRefY   = iCUPelY + (pMv->getVer() >> 2);
2731  uiWidth = pcCU->getWidth(0);
2732  uiHeight = pcCU->getHeight(0);
2733
2734  Int i, j, iCountShift = 0;
2735
2736  // LLS parameters estimation -->
2737
2738  Int x = 0, y = 0, xx = 0, xy = 0;
2739
2740  if(pcCU->getPUAbove(uiTmpPartIdx, pcCU->getZorderIdxInCU()) && iCUPelY > 0 && iRefY > 0)
2741  {
2742    iRefOffset = ( pMv->getHor() >> 2 ) + ( pMv->getVer() >> 2 ) * iRefStride - iRefStride;
2743    pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2744    pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
2745
2746    for( j = 0; j < uiWidth; j++ )
2747    {
2748      x += pRef[j];
2749      y += pRec[j];
2750      xx += pRef[j] * pRef[j];
2751      xy += pRef[j] * pRec[j];
2752    }
2753    iCountShift += g_aucConvertToBit[ uiWidth ] + 2;
2754  }
2755
2756
2757  if(pcCU->getPULeft(uiTmpPartIdx, pcCU->getZorderIdxInCU()) && iCUPelX > 0 && iRefX > 0)
2758  {
2759    iRefOffset = ( pMv->getHor() >> 2 ) + ( pMv->getVer() >> 2 ) * iRefStride - 1;
2760    pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2761    pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
2762
2763    for( i = 0; i < uiHeight; i++ )
2764    {
2765      x += pRef[0];
2766      y += pRec[0];
2767      xx += pRef[0] * pRef[0];
2768      xy += pRef[0] * pRec[0];
2769
2770      pRef += iRefStride;
2771      pRec += iRecStride;
2772    }
2773    iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 2 );
2774  }
2775
2776  Int iTempShift = ( g_uiBitDepth + g_uiBitIncrement ) + g_aucConvertToBit[ uiWidth ] + 3 - 15;
2777
2778  if(iTempShift > 0)
2779  {
2780    x  = ( x +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2781    y  = ( y +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2782    xx = ( xx + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2783    xy = ( xy + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2784    iCountShift -= iTempShift;
2785  }
2786
2787  iShift = 13;
2788
2789  if( iCountShift == 0 )
2790  {
2791    a = 1;
2792    b = 0;
2793    iShift = 0;
2794  }
2795  else
2796  {
2797    Int a1 = ( xy << iCountShift ) - y * x;
2798    Int a2 = ( xx << iCountShift ) - x * x;             
2799
2800    {
2801      const Int iShiftA2 = 6;
2802      const Int iShiftA1 = 15;
2803      const Int iAccuracyShift = 15;
2804
2805      Int iScaleShiftA2 = 0;
2806      Int iScaleShiftA1 = 0;
2807      Int a1s = a1;
2808      Int a2s = a2;
2809
2810      iScaleShiftA1 = GetMSB( abs( a1 ) ) - iShiftA1;
2811      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2; 
2812
2813      if( iScaleShiftA1 < 0 )
2814      {
2815        iScaleShiftA1 = 0;
2816      }
2817
2818      if( iScaleShiftA2 < 0 )
2819      {
2820        iScaleShiftA2 = 0;
2821      }
2822
2823      Int iScaleShiftA = iScaleShiftA2 + iAccuracyShift - iShift - iScaleShiftA1;
2824
2825      a2s = a2 >> iScaleShiftA2;
2826
2827      a1s = a1 >> iScaleShiftA1;
2828
2829      if (a2s >= 1)
2830      {
2831        a = a1s * m_uiaShift[ a2s - 1];
2832      }
2833      else
2834      {
2835        a = 0;
2836      }
2837
2838      if( iScaleShiftA < 0 )
2839      {
2840        a = a << -iScaleShiftA;
2841      }
2842      else
2843      {
2844        a = a >> iScaleShiftA;
2845      }
2846
2847      a = Clip3(-( 1 << 15 ), ( 1 << 15 ) - 1, a); 
2848
2849      Int minA = -(1 << (6));
2850      Int maxA = (1 << 6) - 1;
2851      if( a <= maxA && a >= minA )
2852      {
2853        // do nothing
2854      }
2855      else
2856      {
2857        Short n = CountLeadingZerosOnes(a);
2858        a = a >> (9-n);
2859        iShift -= (9-n);
2860      }
2861
2862      b = (  y - ( ( a * x ) >> iShift ) + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
2863    }
2864  }   
2865}
2866
2867Void TComPrediction::xGetLLSICPredictionChroma(TComDataCU* pcCU, TComMv *pMv, TComPicYuv *pRefPic, Int &a, Int &b, Int &iShift, Int iChromaId)
2868{
2869  TComPicYuv *pRecPic = pcCU->getPic()->getPicYuvRec();
2870  Pel *pRec = NULL, *pRef = NULL;
2871  UInt uiWidth, uiHeight, uiTmpPartIdx;
2872  Int iRecStride = pRecPic->getCStride(), iRefStride = pRefPic->getCStride();
2873  Int iCUPelX, iCUPelY, iRefX, iRefY, iRefOffset;
2874
2875  iCUPelX = pcCU->getCUPelX() + g_auiRasterToPelX[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
2876  iCUPelY = pcCU->getCUPelY() + g_auiRasterToPelY[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
2877  iRefX   = iCUPelX + (pMv->getHor() >> 2);
2878  iRefY   = iCUPelY + (pMv->getVer() >> 2);
2879  uiWidth = pcCU->getWidth(0) >> 1;
2880  uiHeight = pcCU->getHeight(0) >> 1;
2881
2882  Int i, j, iCountShift = 0;
2883
2884  // LLS parameters estimation -->
2885
2886  Int x = 0, y = 0, xx = 0, xy = 0;
2887
2888  if(pcCU->getPUAbove(uiTmpPartIdx, pcCU->getZorderIdxInCU()) && iCUPelY > 0 && iRefY > 0)
2889  {
2890    iRefOffset = ( pMv->getHor() >> 3 ) + ( pMv->getVer() >> 3 ) * iRefStride - iRefStride;
2891    if (iChromaId == 0) // Cb
2892    {
2893      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2894      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
2895    }
2896    else if (iChromaId == 1) // Cr
2897    {
2898      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2899      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
2900    }
2901
2902    for( j = 0; j < uiWidth; j++ )
2903    {
2904      x += pRef[j];
2905      y += pRec[j];
2906      xx += pRef[j] * pRef[j];
2907      xy += pRef[j] * pRec[j];
2908    }
2909    iCountShift += g_aucConvertToBit[ uiWidth ] + 2;
2910  }
2911
2912
2913  if(pcCU->getPULeft(uiTmpPartIdx, pcCU->getZorderIdxInCU()) && iCUPelX > 0 && iRefX > 0)
2914  {
2915    iRefOffset = ( pMv->getHor() >> 3 ) + ( pMv->getVer() >> 3 ) * iRefStride - 1;
2916    if (iChromaId == 0) // Cb
2917    {
2918      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2919      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
2920    }
2921    else if (iChromaId == 1) // Cr
2922    {
2923      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2924      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
2925    }
2926
2927    for( i = 0; i < uiHeight; i++ )
2928    {
2929      x += pRef[0];
2930      y += pRec[0];
2931      xx += pRef[0] * pRef[0];
2932      xy += pRef[0] * pRec[0];
2933
2934      pRef += iRefStride;
2935      pRec += iRecStride;
2936    }
2937    iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 2 );
2938  }
2939
2940  Int iTempShift = ( g_uiBitDepth + g_uiBitIncrement ) + g_aucConvertToBit[ uiWidth ] + 3 - 15;
2941
2942  if(iTempShift > 0)
2943  {
2944    x  = ( x +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2945    y  = ( y +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2946    xx = ( xx + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2947    xy = ( xy + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
2948    iCountShift -= iTempShift;
2949  }
2950
2951  iShift = 13;
2952
2953  if( iCountShift == 0 )
2954  {
2955    a = 1;
2956    b = 0;
2957    iShift = 0;
2958  }
2959  else
2960  {
2961    Int a1 = ( xy << iCountShift ) - y * x;
2962    Int a2 = ( xx << iCountShift ) - x * x;             
2963
2964    {
2965      const Int iShiftA2 = 6;
2966      const Int iShiftA1 = 15;
2967      const Int iAccuracyShift = 15;
2968
2969      Int iScaleShiftA2 = 0;
2970      Int iScaleShiftA1 = 0;
2971      Int a1s = a1;
2972      Int a2s = a2;
2973
2974      iScaleShiftA1 = GetMSB( abs( a1 ) ) - iShiftA1;
2975      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2; 
2976
2977      if( iScaleShiftA1 < 0 )
2978      {
2979        iScaleShiftA1 = 0;
2980      }
2981
2982      if( iScaleShiftA2 < 0 )
2983      {
2984        iScaleShiftA2 = 0;
2985      }
2986
2987      Int iScaleShiftA = iScaleShiftA2 + iAccuracyShift - iShift - iScaleShiftA1;
2988
2989      a2s = a2 >> iScaleShiftA2;
2990
2991      a1s = a1 >> iScaleShiftA1;
2992
2993      if (a2s >= 1)
2994      {
2995        a = a1s * m_uiaShift[ a2s - 1];
2996      }
2997      else
2998      {
2999        a = 0;
3000      }
3001
3002      if( iScaleShiftA < 0 )
3003      {
3004        a = a << -iScaleShiftA;
3005      }
3006      else
3007      {
3008        a = a >> iScaleShiftA;
3009      }
3010
3011      a = Clip3(-( 1 << 15 ), ( 1 << 15 ) - 1, a); 
3012
3013      Int minA = -(1 << (6));
3014      Int maxA = (1 << 6) - 1;
3015      if( a <= maxA && a >= minA )
3016      {
3017        // do nothing
3018      }
3019      else
3020      {
3021        Short n = CountLeadingZerosOnes(a);
3022        a = a >> (9-n);
3023        iShift -= (9-n);
3024      }
3025
3026      b = (  y - ( ( a * x ) >> iShift ) + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
3027    }
3028  }   
3029}
3030#endif
3031/** Function for filtering intra DC predictor.
3032 * \param pSrc pointer to reconstructed sample array
3033 * \param iSrcStride the stride of the reconstructed sample array
3034 * \param rpDst reference to pointer for the prediction sample array
3035 * \param iDstStride the stride of the prediction sample array
3036 * \param iWidth the width of the block
3037 * \param iHeight the height of the block
3038 *
3039 * This function performs filtering left and top edges of the prediction samples for DC mode (intra coding).
3040 */
3041Void TComPrediction::xDCPredFiltering( Int* pSrc, Int iSrcStride, Pel*& rpDst, Int iDstStride, Int iWidth, Int iHeight )
3042{
3043  Pel* pDst = rpDst;
3044  Int x, y, iDstStride2, iSrcStride2;
3045
3046  // boundary pixels processing
3047  pDst[0] = (Pel)((pSrc[-iSrcStride] + pSrc[-1] + 2 * pDst[0] + 2) >> 2);
3048
3049  for ( x = 1; x < iWidth; x++ )
3050  {
3051    pDst[x] = (Pel)((pSrc[x - iSrcStride] +  3 * pDst[x] + 2) >> 2);
3052  }
3053
3054  for ( y = 1, iDstStride2 = iDstStride, iSrcStride2 = iSrcStride-1; y < iHeight; y++, iDstStride2+=iDstStride, iSrcStride2+=iSrcStride )
3055  {
3056    pDst[iDstStride2] = (Pel)((pSrc[iSrcStride2] + 3 * pDst[iDstStride2] + 2) >> 2);
3057  }
3058
3059  return;
3060}
3061
3062#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
3063Void TComPrediction::predIntraLumaDMM( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft, Bool bEncoder )
3064{
3065#if HHI_DMM_WEDGE_INTRA
3066  if( uiMode == DMM_WEDGE_FULL_IDX        ) { xPredIntraWedgeFull ( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, false, pcCU->getWedgeFullTabIdx ( uiAbsPartIdx ) ); }
3067  if( uiMode == DMM_WEDGE_FULL_D_IDX      ) { xPredIntraWedgeFull ( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, true,  pcCU->getWedgeFullTabIdx( uiAbsPartIdx ), pcCU->getWedgeFullDeltaDC1( uiAbsPartIdx ), pcCU->getWedgeFullDeltaDC2( uiAbsPartIdx ) ); }
3068  if( uiMode == DMM_WEDGE_PREDDIR_IDX     ) { xPredIntraWedgeDir  ( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, false, pcCU->getWedgePredDirDeltaEnd( uiAbsPartIdx ) ); }
3069  if( uiMode == DMM_WEDGE_PREDDIR_D_IDX   ) { xPredIntraWedgeDir  ( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, true,  pcCU->getWedgePredDirDeltaEnd( uiAbsPartIdx ), pcCU->getWedgePredDirDeltaDC1( uiAbsPartIdx ), pcCU->getWedgePredDirDeltaDC2( uiAbsPartIdx ) ); }
3070#endif
3071#if HHI_DMM_PRED_TEX
3072  if( uiMode == DMM_WEDGE_PREDTEX_IDX     ) { xPredIntraWedgeTex  ( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, false ); }
3073  if( uiMode == DMM_WEDGE_PREDTEX_D_IDX   ) { xPredIntraWedgeTex  ( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, true, pcCU->getWedgePredTexDeltaDC1( uiAbsPartIdx ), pcCU->getWedgePredTexDeltaDC2( uiAbsPartIdx ) ); }
3074  if( uiMode == DMM_CONTOUR_PREDTEX_IDX   ) { xPredIntraContourTex( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, false ); }
3075  if( uiMode == DMM_CONTOUR_PREDTEX_D_IDX ) { xPredIntraContourTex( pcCU, uiAbsPartIdx, piPred, uiStride, iWidth, iHeight, bAbove, bLeft, bEncoder, true, pcCU->getContourPredTexDeltaDC1( uiAbsPartIdx ), pcCU->getContourPredTexDeltaDC2( uiAbsPartIdx ) ); }
3076#endif
3077}
3078
3079Void TComPrediction::getWedgePredDCs( TComWedgelet* pcWedgelet, Int* piMask, Int iMaskStride, Int& riPredDC1, Int& riPredDC2, Bool bAbove, Bool bLeft )
3080{
3081  riPredDC1 = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) ); //pred val, if no neighbors are available
3082  riPredDC2 = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) );
3083
3084  if( !bAbove && !bLeft ) { return; }
3085
3086  UInt uiNumSmpDC1 = 0, uiNumSmpDC2 = 0;
3087  Int iPredDC1 = 0, iPredDC2 = 0;
3088
3089  Bool* pabWedgePattern = pcWedgelet->getPattern();
3090  UInt  uiWedgeStride   = pcWedgelet->getStride();
3091
3092#if HS_REFERENCE_SUBSAMPLE_C0154
3093  Int subSamplePix;
3094  if ( pcWedgelet->getWidth() == 32 )
3095  {
3096    subSamplePix = 2;
3097  }
3098  else
3099  {
3100    subSamplePix = 1;
3101  }
3102#endif
3103
3104  if( bAbove )
3105  {
3106#if HS_REFERENCE_SUBSAMPLE_C0154
3107    for( Int k = 0; k < pcWedgelet->getWidth(); k+=subSamplePix )
3108#else
3109    for( Int k = 0; k < pcWedgelet->getWidth(); k++ )
3110#endif
3111    {
3112      if( true == pabWedgePattern[k] )
3113      {
3114        iPredDC2 += piMask[k-iMaskStride];
3115        uiNumSmpDC2++;
3116      }
3117      else
3118      {
3119        iPredDC1 += piMask[k-iMaskStride];
3120        uiNumSmpDC1++;
3121      }
3122    }
3123  }
3124  if( bLeft )
3125  {
3126#if HS_REFERENCE_SUBSAMPLE_C0154
3127    for( Int k = 0; k < pcWedgelet->getHeight(); k+=subSamplePix )
3128#else
3129    for( Int k = 0; k < pcWedgelet->getHeight(); k++ )
3130#endif
3131    {
3132      if( true == pabWedgePattern[k*uiWedgeStride] )
3133      {
3134        iPredDC2 += piMask[k*iMaskStride-1];
3135        uiNumSmpDC2++;
3136      } 
3137      else
3138      {
3139        iPredDC1 += piMask[k*iMaskStride-1];
3140        uiNumSmpDC1++;
3141      }
3142    }
3143  }
3144
3145  if( uiNumSmpDC1 > 0 )
3146  {
3147    iPredDC1 /= uiNumSmpDC1;
3148    riPredDC1 = iPredDC1;
3149  }
3150  if( uiNumSmpDC2 > 0 )
3151  {
3152    iPredDC2 /= uiNumSmpDC2;
3153    riPredDC2 = iPredDC2;
3154  }
3155}
3156
3157Void TComPrediction::calcWedgeDCs( TComWedgelet* pcWedgelet, Pel* piOrig, UInt uiStride, Int& riDC1, Int& riDC2 )
3158{
3159  UInt uiDC1 = 0;
3160  UInt uiDC2 = 0;
3161  UInt uiNumPixDC1 = 0, uiNumPixDC2 = 0;
3162  Bool* pabWedgePattern = pcWedgelet->getPattern();
3163  if( uiStride == pcWedgelet->getStride() )
3164  {
3165    for( UInt k = 0; k < (pcWedgelet->getWidth() * pcWedgelet->getHeight()); k++ )
3166    {
3167      if( true == pabWedgePattern[k] ) 
3168      {
3169        uiDC2 += piOrig[k];
3170        uiNumPixDC2++;
3171      }
3172      else
3173      {
3174        uiDC1 += piOrig[k];
3175        uiNumPixDC1++;
3176      }
3177    }
3178  }
3179  else
3180  {
3181    Pel* piTemp = piOrig;
3182    UInt uiWedgeStride = pcWedgelet->getStride();
3183    for( UInt uiY = 0; uiY < pcWedgelet->getHeight(); uiY++ )
3184    {
3185      for( UInt uiX = 0; uiX < pcWedgelet->getWidth(); uiX++ )
3186      {
3187        if( true == pabWedgePattern[uiX] ) 
3188        {
3189          uiDC2 += piTemp[uiX];
3190          uiNumPixDC2++;
3191        }
3192        else
3193        {
3194          uiDC1 += piTemp[uiX];
3195          uiNumPixDC1++;
3196        }
3197      }
3198      piTemp          += uiStride;
3199      pabWedgePattern += uiWedgeStride;
3200    }
3201  }
3202
3203  if( uiNumPixDC1 > 0 ) { riDC1 = uiDC1 / uiNumPixDC1; }
3204  else                  { riDC1 = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) ); }
3205
3206  if( uiNumPixDC2 > 0 ) { riDC2 = uiDC2 / uiNumPixDC2; }
3207  else                  { riDC2 = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) ); }
3208}
3209
3210Void TComPrediction::assignWedgeDCs2Pred( TComWedgelet* pcWedgelet, Pel* piPred, UInt uiStride, Int iDC1, Int iDC2 )
3211{
3212  Bool* pabWedgePattern = pcWedgelet->getPattern();
3213
3214  if( uiStride == pcWedgelet->getStride() )
3215  {
3216    for( UInt k = 0; k < (pcWedgelet->getWidth() * pcWedgelet->getHeight()); k++ )
3217    {
3218      if( true == pabWedgePattern[k] ) 
3219      {
3220        piPred[k] = iDC2;
3221      }
3222      else
3223      {
3224        piPred[k] = iDC1;
3225      }
3226    }
3227  }
3228  else
3229  {
3230    Pel* piTemp = piPred;
3231    UInt uiWedgeStride = pcWedgelet->getStride();
3232    for( UInt uiY = 0; uiY < pcWedgelet->getHeight(); uiY++ )
3233    {
3234      for( UInt uiX = 0; uiX < pcWedgelet->getWidth(); uiX++ )
3235      {
3236        if( true == pabWedgePattern[uiX] ) 
3237        {
3238          piTemp[uiX] = iDC2;
3239        }
3240        else
3241        {
3242          piTemp[uiX] = iDC1;
3243        }
3244      }
3245      piTemp          += uiStride;
3246      pabWedgePattern += uiWedgeStride;
3247    }
3248  }
3249}
3250
3251Void TComPrediction::xDeltaDCQuantScaleUp( TComDataCU* pcCU, Int& riDeltaDC )
3252{
3253  Int  iSign  = riDeltaDC < 0 ? -1 : 1;
3254  UInt uiAbs  = abs( riDeltaDC );
3255
3256  Int iQp = pcCU->getQP(0);
3257  Double dMax = (Double)( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) );
3258  Double dStepSize = Clip3( 1.0, dMax, pow( 2.0, iQp/10.0 + g_iDeltaDCsQuantOffset ) );
3259
3260  riDeltaDC = iSign * roftoi( uiAbs * dStepSize );
3261  return;
3262}
3263
3264Void TComPrediction::xDeltaDCQuantScaleDown( TComDataCU*  pcCU, Int& riDeltaDC )
3265{
3266  Int  iSign  = riDeltaDC < 0 ? -1 : 1;
3267  UInt uiAbs  = abs( riDeltaDC );
3268
3269  Int iQp = pcCU->getQP(0);
3270  Double dMax = (Double)( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) );
3271  Double dStepSize = Clip3( 1.0, dMax, pow( 2.0, iQp/10.0 + g_iDeltaDCsQuantOffset ) );
3272
3273  riDeltaDC = iSign * roftoi( uiAbs / dStepSize );
3274  return;
3275}
3276#endif
3277
3278#if HHI_DMM_PRED_TEX
3279Void TComPrediction::getBestContourFromTex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, TComWedgelet* pcContourWedge )
3280{
3281  pcContourWedge->clear();
3282
3283  // get copy of co-located texture luma block
3284  TComYuv cTempYuv;
3285  cTempYuv.create( uiWidth, uiHeight ); 
3286  cTempYuv.clear();
3287  Pel* piRefBlkY = cTempYuv.getLumaAddr();
3288  copyTextureLumaBlock( pcCU, uiAbsPartIdx, piRefBlkY, uiWidth, uiHeight );
3289  piRefBlkY = cTempYuv.getLumaAddr();
3290
3291  // find contour for texture luma block
3292  UInt iDC = 0;
3293  for( UInt k = 0; k < (uiWidth*uiHeight); k++ ) 
3294  { 
3295    iDC += piRefBlkY[k]; 
3296  }
3297  iDC /= (uiWidth*uiHeight);
3298  piRefBlkY = cTempYuv.getLumaAddr();
3299
3300  Bool* pabContourPattern = pcContourWedge->getPattern();
3301  for( UInt k = 0; k < (uiWidth*uiHeight); k++ ) 
3302  { 
3303    pabContourPattern[k] = (piRefBlkY[k] > iDC) ? true : false;
3304  }
3305
3306  cTempYuv.destroy();
3307}
3308
3309#if LGE_DMM3_SIMP_C0044
3310/**
3311 - fetch best Wedgelet pattern at decoder
3312 */
3313UInt TComPrediction::getBestWedgeFromTex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt IntraTabIdx)
3314{
3315  assert( uiWidth >= DMM_WEDGEMODEL_MIN_SIZE && uiWidth <= DMM_WEDGEMODEL_MAX_SIZE );
3316
3317  UInt          uiBestTabIdx = 0;
3318  TComPic*      pcPicTex = pcCU->getSlice()->getTexturePic();
3319  TComDataCU*   pcColTexCU = pcPicTex->getCU(pcCU->getAddr());
3320  UInt          uiTexPartIdx = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
3321  Int           uiColTexIntraDir = pcColTexCU->isIntra( uiTexPartIdx ) ? pcColTexCU->getLumaIntraDir( uiTexPartIdx ) : 255;
3322
3323  std::vector< std::vector<UInt> > pauiWdgLstSz = g_aauiWdgLstM3[g_aucConvertToBit[uiWidth]];
3324
3325  if( uiColTexIntraDir > DC_IDX && uiColTexIntraDir < 35 )
3326  {
3327    std::vector<UInt>* pauiWdgLst = &pauiWdgLstSz[uiColTexIntraDir-2];
3328    uiBestTabIdx    =   pauiWdgLst->at(IntraTabIdx);
3329  }
3330  else
3331  {
3332    WedgeNodeList* pacWedgeNodeList = &g_aacWedgeNodeLists[(g_aucConvertToBit[uiWidth])];
3333    uiBestTabIdx = pacWedgeNodeList->at(IntraTabIdx).getPatternIdx();
3334  }
3335
3336  return uiBestTabIdx;
3337}
3338#endif
3339
3340#if LGE_DMM3_SIMP_C0044
3341/**
3342 - calculate best Wedgelet pattern at encoder
3343 */
3344UInt TComPrediction::getBestWedgeFromTex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, Pel* piOrigi, UInt uiStride, UInt & ruiIntraTabIdx)
3345#else
3346UInt TComPrediction::getBestWedgeFromTex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight )
3347#endif
3348{
3349  assert( uiWidth >= DMM_WEDGEMODEL_MIN_SIZE && uiWidth <= DMM_WEDGEMODEL_MAX_SIZE );
3350
3351  // get copy of co-located texture luma block
3352  TComYuv cTempYuv; 
3353  cTempYuv.create( uiWidth, uiHeight ); 
3354  cTempYuv.clear();
3355  Pel* piRefBlkY = cTempYuv.getLumaAddr();
3356
3357  copyTextureLumaBlock( pcCU, uiAbsPartIdx, piRefBlkY, uiWidth, uiHeight );
3358  piRefBlkY = cTempYuv.getLumaAddr();
3359
3360  // local pred buffer
3361  TComYuv cPredYuv; 
3362  cPredYuv.create( uiWidth, uiHeight ); 
3363  cPredYuv.clear();
3364  Pel* piPred = cPredYuv.getLumaAddr();
3365
3366  UInt uiPredStride = cPredYuv.getStride();
3367
3368  // wedge search
3369  TComWedgeDist cWedgeDist;
3370  UInt uiBestDist = MAX_UINT;
3371  UInt uiBestTabIdx = 0;
3372  Int  iDC1 = 0;
3373  Int  iDC2 = 0;
3374  WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[uiWidth])];
3375#if LGE_DMM3_SIMP_C0044
3376  ruiIntraTabIdx  = 0;
3377#endif
3378  TComPic*      pcPicTex = pcCU->getSlice()->getTexturePic();
3379  TComDataCU* pcColTexCU = pcPicTex->getCU(pcCU->getAddr());
3380  UInt      uiTexPartIdx = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
3381  Int   uiColTexIntraDir = pcColTexCU->isIntra( uiTexPartIdx ) ? pcColTexCU->getLumaIntraDir( uiTexPartIdx ) : 255;
3382
3383  std::vector< std::vector<UInt> > pauiWdgLstSz = g_aauiWdgLstM3[g_aucConvertToBit[uiWidth]];
3384  if( uiColTexIntraDir > DC_IDX && uiColTexIntraDir < 35 )
3385  {
3386    std::vector<UInt>* pauiWdgLst = &pauiWdgLstSz[uiColTexIntraDir-2];
3387    for( UInt uiIdxW = 0; uiIdxW < pauiWdgLst->size(); uiIdxW++ )
3388    {
3389      UInt uiIdx     =   pauiWdgLst->at(uiIdxW);
3390#if LGE_DMM3_SIMP_C0044
3391      calcWedgeDCs       ( &(pacWedgeList->at(uiIdx)), piOrigi,   uiWidth,      iDC1, iDC2 );
3392#else
3393      calcWedgeDCs       ( &(pacWedgeList->at(uiIdx)), piRefBlkY, uiWidth,      iDC1, iDC2 );
3394#endif
3395      assignWedgeDCs2Pred( &(pacWedgeList->at(uiIdx)), piPred,    uiPredStride, iDC1, iDC2 );
3396
3397#if LGE_DMM3_SIMP_C0044
3398      UInt uiActDist = cWedgeDist.getDistPart( piPred, uiPredStride, piOrigi, uiStride, uiWidth, uiHeight, WedgeDist_SAD );
3399#else
3400      UInt uiActDist = cWedgeDist.getDistPart( piPred, uiPredStride, piRefBlkY, uiWidth, uiWidth, uiHeight, WedgeDist_SAD );
3401#endif
3402
3403      if( uiActDist < uiBestDist || uiBestDist == MAX_UINT )
3404      {
3405        uiBestDist   = uiActDist;
3406        uiBestTabIdx = uiIdx;
3407#if LGE_DMM3_SIMP_C0044
3408        ruiIntraTabIdx = uiIdxW;
3409#endif
3410      }
3411    }
3412  }
3413  else
3414  {
3415    WedgeNodeList* pacWedgeNodeList = &g_aacWedgeNodeLists[(g_aucConvertToBit[uiWidth])];
3416    UInt uiBestNodeDist = MAX_UINT;
3417    UInt uiBestNodeId   = 0;
3418    for( UInt uiNodeId = 0; uiNodeId < pacWedgeNodeList->size(); uiNodeId++ )
3419    {
3420#if LGE_DMM3_SIMP_C0044
3421      calcWedgeDCs       ( &(pacWedgeList->at(pacWedgeNodeList->at(uiNodeId).getPatternIdx())), piOrigi, uiWidth,      iDC1, iDC2 );
3422#else
3423      calcWedgeDCs       ( &(pacWedgeList->at(pacWedgeNodeList->at(uiNodeId).getPatternIdx())), piRefBlkY, uiWidth,      iDC1, iDC2 );
3424#endif
3425      assignWedgeDCs2Pred( &(pacWedgeList->at(pacWedgeNodeList->at(uiNodeId).getPatternIdx())), piPred,    uiPredStride, iDC1, iDC2 );
3426
3427#if LGE_DMM3_SIMP_C0044
3428      UInt uiActDist = cWedgeDist.getDistPart( piPred, uiPredStride, piOrigi, uiStride, uiWidth, uiHeight, WedgeDist_SAD );
3429#else
3430      UInt uiActDist = cWedgeDist.getDistPart( piPred, uiPredStride, piRefBlkY, uiWidth, uiWidth, uiHeight, WedgeDist_SAD );
3431#endif
3432
3433      if( uiActDist < uiBestNodeDist || uiBestNodeDist == MAX_UINT )
3434      {
3435        uiBestNodeDist = uiActDist;
3436        uiBestNodeId   = uiNodeId;
3437#if LGE_DMM3_SIMP_C0044
3438        ruiIntraTabIdx = uiNodeId;
3439#endif
3440      }
3441    }
3442#if LGE_DMM3_SIMP_C0044
3443    uiBestTabIdx = pacWedgeNodeList->at(uiBestNodeId).getPatternIdx();
3444#else
3445    // refinement
3446    uiBestDist   = uiBestNodeDist;
3447    uiBestTabIdx = pacWedgeNodeList->at(uiBestNodeId).getPatternIdx();
3448    for( UInt uiRefId = 0; uiRefId < NUM_WEDGE_REFINES; uiRefId++ )
3449    {
3450      if( pacWedgeNodeList->at(uiBestNodeId).getRefineIdx( uiRefId ) != NO_IDX )
3451      {
3452        calcWedgeDCs       ( &(pacWedgeList->at(pacWedgeNodeList->at(uiBestNodeId).getRefineIdx( uiRefId ))), piRefBlkY, uiWidth,      iDC1, iDC2 );
3453        assignWedgeDCs2Pred( &(pacWedgeList->at(pacWedgeNodeList->at(uiBestNodeId).getRefineIdx( uiRefId ))), piPred,    uiPredStride, iDC1, iDC2 );
3454
3455        UInt uiActDist = cWedgeDist.getDistPart( piPred, uiPredStride, piRefBlkY, uiWidth, uiWidth, uiHeight, WedgeDist_SAD );
3456
3457        if( uiActDist < uiBestDist || uiBestDist == MAX_UINT )
3458        {
3459          uiBestDist   = uiActDist;
3460          uiBestTabIdx = pacWedgeNodeList->at(uiBestNodeId).getRefineIdx( uiRefId );
3461        }
3462      }
3463    }
3464#endif
3465  }
3466
3467  cPredYuv.destroy();
3468  cTempYuv.destroy();
3469  return uiBestTabIdx;
3470}
3471
3472Void TComPrediction::copyTextureLumaBlock( TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piDestBlockY, UInt uiWidth, UInt uiHeight )
3473{
3474  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getTexturePic()->getPicYuvRec();
3475  Int         iRefStride = pcPicYuvRef->getStride();
3476  Pel*        piRefY;
3477
3478  piRefY = pcPicYuvRef->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiAbsPartIdx );
3479
3480  for ( Int y = 0; y < uiHeight; y++ )
3481  {
3482    ::memcpy(piDestBlockY, piRefY, sizeof(Pel)*uiWidth);
3483//    ::memset(piDestBlockY, 128, sizeof(Pel)*uiWidth);
3484    piDestBlockY += uiWidth;
3485    piRefY += iRefStride;
3486  }
3487}
3488
3489Void TComPrediction::xPredIntraWedgeTex( TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft, Bool bEncoder, Bool bDelta, Int iDeltaDC1, Int iDeltaDC2 )
3490{
3491  assert( iWidth >= DMM_WEDGEMODEL_MIN_SIZE && iWidth <= DMM_WEDGEMODEL_MAX_SIZE );
3492  WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[iWidth])];
3493
3494  // get wedge pattern
3495  UInt uiTextureWedgeTabIdx = 0;
3496  if( bEncoder ) 
3497  {
3498    // encoder: load stored wedge pattern from CU
3499    uiTextureWedgeTabIdx = pcCU->getWedgePredTexTabIdx( uiAbsPartIdx );
3500  }
3501  else
3502  {
3503    // decoder: get and store wedge pattern in CU
3504      // decoder: get and store wedge pattern in CU
3505#if LGE_DMM3_SIMP_C0044
3506    UInt uiIntraTabIdx   = pcCU->getWedgePredTexIntraTabIdx ( uiAbsPartIdx );
3507    uiTextureWedgeTabIdx = getBestWedgeFromTex( pcCU, uiAbsPartIdx, (UInt)iWidth, (UInt)iHeight, uiIntraTabIdx );
3508#else
3509    uiTextureWedgeTabIdx = getBestWedgeFromTex( pcCU, uiAbsPartIdx, (UInt)iWidth, (UInt)iHeight );
3510#endif
3511
3512    UInt uiDepth = (pcCU->getDepth(0)) + (pcCU->getPartitionSize(0) == SIZE_2Nx2N ? 0 : 1);
3513    pcCU->setWedgePredTexTabIdxSubParts( uiTextureWedgeTabIdx, uiAbsPartIdx, uiDepth );
3514  }
3515  TComWedgelet* pcWedgelet = &(pacWedgeList->at(uiTextureWedgeTabIdx));
3516
3517  // get wedge pred DCs
3518  Int iPredDC1 = 0;
3519  Int iPredDC2 = 0;
3520  Int* piMask = pcCU->getPattern()->getAdiOrgBuf( iWidth, iHeight, m_piYuvExt );
3521  Int iMaskStride = ( iWidth<<1 ) + 1;
3522  piMask += iMaskStride+1;
3523  getWedgePredDCs( pcWedgelet, piMask, iMaskStride, iPredDC1, iPredDC2, bAbove, bLeft );
3524
3525  // assign wedge pred DCs to prediction
3526  if( bDelta ) { assignWedgeDCs2Pred( pcWedgelet, piPred, uiStride, Clip ( iPredDC1+iDeltaDC1 ), Clip( iPredDC2+iDeltaDC2 ) ); }
3527  else         { assignWedgeDCs2Pred( pcWedgelet, piPred, uiStride,        iPredDC1,                   iPredDC2           ); }
3528}
3529
3530Void TComPrediction::xPredIntraContourTex( TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft, Bool bEncoder, Bool bDelta, Int iDeltaDC1, Int iDeltaDC2 )
3531{
3532  // get contour pattern
3533  TComWedgelet* pcContourWedge = new TComWedgelet( iWidth, iHeight );
3534  getBestContourFromTex( pcCU, uiAbsPartIdx, (UInt)iWidth, (UInt)iHeight, pcContourWedge );
3535
3536  // get wedge pred DCs
3537  Int iPredDC1 = 0;
3538  Int iPredDC2 = 0;
3539  Int* piMask = pcCU->getPattern()->getAdiOrgBuf( iWidth, iHeight, m_piYuvExt );
3540  Int iMaskStride = ( iWidth<<1 ) + 1;
3541  piMask += iMaskStride+1;
3542  getWedgePredDCs( pcContourWedge, piMask, iMaskStride, iPredDC1, iPredDC2, bAbove, bLeft );
3543
3544  // assign wedge pred DCs to prediction
3545  if( bDelta ) { assignWedgeDCs2Pred( pcContourWedge, piPred, uiStride, Clip ( iPredDC1+iDeltaDC1 ), Clip( iPredDC2+iDeltaDC2 ) ); }
3546  else         { assignWedgeDCs2Pred( pcContourWedge, piPred, uiStride,        iPredDC1,                   iPredDC2           ); }
3547
3548  pcContourWedge->destroy();
3549  delete pcContourWedge;
3550}
3551#endif // HHI_DMM_PRED_TEX
3552
3553#if HHI_DMM_WEDGE_INTRA
3554UInt TComPrediction::getBestContinueWedge( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, Int iDeltaEnd )
3555{
3556  UInt uiThisBlockSize = uiWidth;
3557  assert( uiThisBlockSize >= DMM_WEDGEMODEL_MIN_SIZE && uiThisBlockSize <= DMM_WEDGEMODEL_MAX_SIZE );
3558  WedgeRefList* pacContDWedgeRefList = &g_aacWedgeRefLists[(g_aucConvertToBit[uiThisBlockSize])];
3559
3560  UInt uiPredDirWedgeTabIdx = 0;
3561  TComDataCU* pcTempCU;
3562  UInt        uiTempPartIdx;
3563  // 1st: try continue above wedgelet
3564  pcTempCU = pcCU->getPUAbove( uiTempPartIdx, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
3565  if( pcTempCU )
3566  {
3567    UChar uhLumaIntraDir = pcTempCU->getLumaIntraDir( uiTempPartIdx );
3568    if( DMM_WEDGE_FULL_IDX      == uhLumaIntraDir || 
3569        DMM_WEDGE_FULL_D_IDX    == uhLumaIntraDir || 
3570        DMM_WEDGE_PREDDIR_IDX   == uhLumaIntraDir || 
3571        DMM_WEDGE_PREDDIR_D_IDX == uhLumaIntraDir
3572#if HHI_DMM_PRED_TEX
3573        ||
3574        DMM_WEDGE_PREDTEX_IDX   == uhLumaIntraDir ||
3575        DMM_WEDGE_PREDTEX_D_IDX == uhLumaIntraDir   
3576#endif
3577      )
3578    {
3579      UInt uiRefWedgeSize = (UInt)g_aucIntraSizeIdxToWedgeSize[pcTempCU->getIntraSizeIdx( uiTempPartIdx )];
3580      WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[uiRefWedgeSize])];
3581
3582      // get offset between current and reference block
3583      UInt uiOffsetX = 0;
3584      UInt uiOffsetY = 0;
3585      xGetBlockOffset( pcCU, uiAbsPartIdx, pcTempCU, uiTempPartIdx, uiOffsetX, uiOffsetY );
3586
3587      // get reference wedgelet
3588      UInt uiRefWedgeTabIdx = 0;
3589      switch( uhLumaIntraDir )
3590      {
3591      case( DMM_WEDGE_FULL_IDX      ): { uiRefWedgeTabIdx = pcTempCU->getWedgeFullTabIdx   ( uiTempPartIdx ); } break;
3592      case( DMM_WEDGE_FULL_D_IDX    ): { uiRefWedgeTabIdx = pcTempCU->getWedgeFullTabIdx   ( uiTempPartIdx ); } break;
3593      case( DMM_WEDGE_PREDDIR_IDX   ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredDirTabIdx( uiTempPartIdx ); } break;
3594      case( DMM_WEDGE_PREDDIR_D_IDX ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredDirTabIdx( uiTempPartIdx ); } break;
3595#if HHI_DMM_PRED_TEX
3596      case( DMM_WEDGE_PREDTEX_IDX   ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredTexTabIdx( uiTempPartIdx ); } break;
3597      case( DMM_WEDGE_PREDTEX_D_IDX ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredTexTabIdx( uiTempPartIdx ); } break;
3598#endif
3599      default: { assert( 0 ); return uiPredDirWedgeTabIdx; }
3600      }
3601      TComWedgelet* pcRefWedgelet;
3602      pcRefWedgelet = &(pacWedgeList->at( uiRefWedgeTabIdx ));
3603
3604      // find reference wedgelet, if direction is suitable for continue wedge
3605      if( pcRefWedgelet->checkPredDirAbovePossible( uiThisBlockSize, uiOffsetX ) )
3606      {
3607        UChar uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye;
3608        pcRefWedgelet->getPredDirStartEndAbove( uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye, uiThisBlockSize, uiOffsetX, iDeltaEnd );
3609        getWedgePatternIdx( pacContDWedgeRefList, uiPredDirWedgeTabIdx, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye );
3610        return uiPredDirWedgeTabIdx;
3611      }
3612    }
3613  }
3614
3615  // 2nd: try continue left wedglelet
3616  pcTempCU = pcCU->getPULeft( uiTempPartIdx, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
3617  if( pcTempCU )
3618  {
3619    UChar uhLumaIntraDir = pcTempCU->getLumaIntraDir( uiTempPartIdx );
3620    if( DMM_WEDGE_FULL_IDX      == uhLumaIntraDir || 
3621        DMM_WEDGE_FULL_D_IDX    == uhLumaIntraDir || 
3622        DMM_WEDGE_PREDDIR_IDX   == uhLumaIntraDir || 
3623        DMM_WEDGE_PREDDIR_D_IDX == uhLumaIntraDir
3624#if HHI_DMM_PRED_TEX
3625        ||
3626        DMM_WEDGE_PREDTEX_IDX   == uhLumaIntraDir ||
3627        DMM_WEDGE_PREDTEX_D_IDX == uhLumaIntraDir   
3628#endif
3629      )
3630    {
3631      UInt uiRefWedgeSize = (UInt)g_aucIntraSizeIdxToWedgeSize[pcTempCU->getIntraSizeIdx( uiTempPartIdx )];
3632      WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[uiRefWedgeSize])];
3633
3634      // get offset between current and reference block
3635      UInt uiOffsetX = 0;
3636      UInt uiOffsetY = 0;
3637      xGetBlockOffset( pcCU, uiAbsPartIdx, pcTempCU, uiTempPartIdx, uiOffsetX, uiOffsetY );
3638
3639      // get reference wedgelet
3640      UInt uiRefWedgeTabIdx = 0;
3641      switch( uhLumaIntraDir )
3642      {
3643      case( DMM_WEDGE_FULL_IDX      ): { uiRefWedgeTabIdx = pcTempCU->getWedgeFullTabIdx   ( uiTempPartIdx ); } break;
3644      case( DMM_WEDGE_FULL_D_IDX    ): { uiRefWedgeTabIdx = pcTempCU->getWedgeFullTabIdx   ( uiTempPartIdx ); } break;
3645      case( DMM_WEDGE_PREDDIR_IDX   ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredDirTabIdx( uiTempPartIdx ); } break;
3646      case( DMM_WEDGE_PREDDIR_D_IDX ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredDirTabIdx( uiTempPartIdx ); } break;
3647#if HHI_DMM_PRED_TEX
3648      case( DMM_WEDGE_PREDTEX_IDX   ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredTexTabIdx( uiTempPartIdx ); } break;
3649      case( DMM_WEDGE_PREDTEX_D_IDX ): { uiRefWedgeTabIdx = pcTempCU->getWedgePredTexTabIdx( uiTempPartIdx ); } break;
3650#endif
3651      default: { assert( 0 ); return uiPredDirWedgeTabIdx; }
3652      }
3653      TComWedgelet* pcRefWedgelet;
3654      pcRefWedgelet = &(pacWedgeList->at( uiRefWedgeTabIdx ));
3655
3656      // find reference wedgelet, if direction is suitable for continue wedge
3657      if( pcRefWedgelet->checkPredDirLeftPossible( uiThisBlockSize, uiOffsetY ) )
3658      {
3659        UChar uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye;
3660        pcRefWedgelet->getPredDirStartEndLeft( uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye, uiThisBlockSize, uiOffsetY, iDeltaEnd );
3661        getWedgePatternIdx( pacContDWedgeRefList, uiPredDirWedgeTabIdx, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye );
3662        return uiPredDirWedgeTabIdx;
3663      }
3664    }
3665  }
3666
3667  // 3rd: (default) make wedglet from intra dir and max slope point
3668  Int iSlopeX = 0;
3669  Int iSlopeY = 0;
3670  UInt uiStartPosX = 0;
3671  UInt uiStartPosY = 0;
3672  if( xGetWedgeIntraDirPredData( pcCU, uiAbsPartIdx, uiThisBlockSize, iSlopeX, iSlopeY, uiStartPosX, uiStartPosY ) )
3673  {
3674    UChar uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye;
3675    xGetWedgeIntraDirStartEnd( pcCU, uiAbsPartIdx, uiThisBlockSize, iSlopeX, iSlopeY, uiStartPosX, uiStartPosY, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye, iDeltaEnd );
3676    getWedgePatternIdx( pacContDWedgeRefList, uiPredDirWedgeTabIdx, uhContD_Xs, uhContD_Ys, uhContD_Xe, uhContD_Ye );
3677    return uiPredDirWedgeTabIdx;
3678  }
3679
3680  return uiPredDirWedgeTabIdx;
3681}
3682
3683Bool TComPrediction::getWedgePatternIdx( WedgeRefList* pcWedgeRefList, UInt& ruiTabIdx, UChar uhXs, UChar uhYs, UChar uhXe, UChar uhYe )
3684{
3685  ruiTabIdx = 0;
3686
3687  for( UInt uiIdx = 0; uiIdx < pcWedgeRefList->size(); uiIdx++ )
3688  {
3689    TComWedgeRef* pcTestWedgeRef = &(pcWedgeRefList->at(uiIdx));
3690
3691    if( pcTestWedgeRef->getStartX() == uhXs &&
3692      pcTestWedgeRef->getStartY() == uhYs &&
3693      pcTestWedgeRef->getEndX()   == uhXe &&
3694      pcTestWedgeRef->getEndY()   == uhYe    )
3695    {
3696      ruiTabIdx = pcTestWedgeRef->getRefIdx();
3697      return true;
3698    }
3699  }
3700
3701  return false;
3702}
3703
3704Void TComPrediction::xPredIntraWedgeFull( TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft, Bool bEncoder, Bool bDelta, UInt uiTabIdx, Int iDeltaDC1, Int iDeltaDC2 )
3705{
3706  assert( iWidth >= DMM_WEDGEMODEL_MIN_SIZE && iWidth <= DMM_WEDGEMODEL_MAX_SIZE );
3707  WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[iWidth])];
3708  TComWedgelet* pcWedgelet = &(pacWedgeList->at(uiTabIdx));
3709
3710  // get wedge pred DCs
3711  Int iPredDC1 = 0;
3712  Int iPredDC2 = 0;
3713
3714  Int* piMask = pcCU->getPattern()->getAdiOrgBuf( iWidth, iHeight, m_piYuvExt );
3715  Int iMaskStride = ( iWidth<<1 ) + 1;
3716  piMask += iMaskStride+1;
3717  getWedgePredDCs( pcWedgelet, piMask, iMaskStride, iPredDC1, iPredDC2, bAbove, bLeft );
3718
3719  // assign wedge pred DCs to prediction
3720  if( bDelta ) { assignWedgeDCs2Pred( pcWedgelet, piPred, uiStride, Clip( iPredDC1+iDeltaDC1 ), Clip( iPredDC2+iDeltaDC2 ) ); }
3721  else         { assignWedgeDCs2Pred( pcWedgelet, piPred, uiStride, iPredDC1,           iPredDC2           ); }
3722}
3723
3724Void TComPrediction::xPredIntraWedgeDir( TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft, Bool bEncoder, Bool bDelta, Int iWedgeDeltaEnd, Int iDeltaDC1, Int iDeltaDC2 )
3725{
3726  assert( iWidth >= DMM_WEDGEMODEL_MIN_SIZE && iWidth <= DMM_WEDGEMODEL_MAX_SIZE );
3727  WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[iWidth])];
3728
3729  // get wedge pattern
3730  UInt uiDirWedgeTabIdx = 0;
3731  if( bEncoder )
3732  {
3733    // encoder: load stored wedge pattern from CU
3734    uiDirWedgeTabIdx = pcCU->getWedgePredDirTabIdx( uiAbsPartIdx );
3735  }
3736  else
3737  {
3738    uiDirWedgeTabIdx = getBestContinueWedge( pcCU, uiAbsPartIdx, iWidth, iHeight, iWedgeDeltaEnd );
3739
3740    UInt uiDepth = (pcCU->getDepth(0)) + (pcCU->getPartitionSize(0) == SIZE_2Nx2N ? 0 : 1);
3741    pcCU->setWedgePredDirTabIdxSubParts( uiDirWedgeTabIdx, uiAbsPartIdx, uiDepth );
3742  }
3743  TComWedgelet* pcWedgelet = &(pacWedgeList->at(uiDirWedgeTabIdx));
3744
3745  // get wedge pred DCs
3746  Int iPredDC1 = 0;
3747  Int iPredDC2 = 0;
3748
3749  Int* piMask = pcCU->getPattern()->getAdiOrgBuf( iWidth, iHeight, m_piYuvExt );
3750  Int iMaskStride = ( iWidth<<1 ) + 1;
3751  piMask += iMaskStride+1;
3752  getWedgePredDCs( pcWedgelet, piMask, iMaskStride, iPredDC1, iPredDC2, bAbove, bLeft );
3753
3754  // assign wedge pred DCs to prediction
3755  if( bDelta ) { assignWedgeDCs2Pred( pcWedgelet, piPred, uiStride, Clip( iPredDC1+iDeltaDC1 ), Clip( iPredDC2+iDeltaDC2 ) ); }
3756  else         { assignWedgeDCs2Pred( pcWedgelet, piPred, uiStride,       iPredDC1,                   iPredDC2             ); }
3757}
3758
3759Void TComPrediction::xGetBlockOffset( TComDataCU* pcCU, UInt uiAbsPartIdx, TComDataCU* pcRefCU, UInt uiRefAbsPartIdx, UInt& ruiOffsetX, UInt& ruiOffsetY )
3760{
3761  ruiOffsetX = 0;
3762  ruiOffsetY = 0;
3763
3764  // get offset between current and above/left block
3765  UInt uiThisOriginX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
3766  UInt uiThisOriginY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
3767
3768  UInt uiNumPartInRefCU = pcRefCU->getTotalNumPart();
3769  UInt uiMaxDepthRefCU = 0;
3770  while( uiNumPartInRefCU > 1 )
3771  {
3772    uiNumPartInRefCU >>= 2;
3773    uiMaxDepthRefCU++;
3774  }
3775
3776  UInt uiDepthRefPU = (pcRefCU->getDepth(uiRefAbsPartIdx)) + (pcRefCU->getPartitionSize(uiRefAbsPartIdx) == SIZE_2Nx2N ? 0 : 1);
3777  UInt uiShifts = (uiMaxDepthRefCU - uiDepthRefPU)*2;
3778  UInt uiRefBlockOriginPartIdx = (uiRefAbsPartIdx>>uiShifts)<<uiShifts;
3779
3780  UInt uiRefOriginX = pcRefCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiRefBlockOriginPartIdx] ];
3781  UInt uiRefOriginY = pcRefCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiRefBlockOriginPartIdx] ];
3782
3783  if( (uiThisOriginX - uiRefOriginX) > 0 ) { ruiOffsetX = (UInt)(uiThisOriginX - uiRefOriginX); }
3784  if( (uiThisOriginY - uiRefOriginY) > 0 ) { ruiOffsetY = (UInt)(uiThisOriginY - uiRefOriginY); }
3785}
3786
3787Bool TComPrediction::xGetWedgeIntraDirPredData( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiBlockSize, Int& riSlopeX, Int& riSlopeY, UInt& ruiStartPosX, UInt& ruiStartPosY )
3788{
3789  riSlopeX     = 0;
3790  riSlopeY     = 0;
3791  ruiStartPosX = 0;
3792  ruiStartPosY = 0;
3793
3794  // 1st step: get wedge start point (max. slope)
3795  Int* piSource = pcCU->getPattern()->getAdiOrgBuf( uiBlockSize, uiBlockSize, m_piYuvExt );
3796  Int iSourceStride = ( uiBlockSize<<1 ) + 1;
3797
3798  UInt uiSlopeMaxAbove = 0;
3799  UInt uiPosSlopeMaxAbove = 0;
3800  for( UInt uiPosHor = 0; uiPosHor < (uiBlockSize-1); uiPosHor++ )
3801  {
3802    if( abs( piSource[uiPosHor+1] - piSource[uiPosHor] ) > uiSlopeMaxAbove )
3803    {
3804      uiSlopeMaxAbove = abs( piSource[uiPosHor+1] - piSource[uiPosHor] );
3805      uiPosSlopeMaxAbove = uiPosHor;
3806    }
3807  }
3808
3809  UInt uiSlopeMaxLeft = 0;
3810  UInt uiPosSlopeMaxLeft = 0;
3811  for( UInt uiPosVer = 0; uiPosVer < (uiBlockSize-1); uiPosVer++ )
3812  {
3813    if( abs( piSource[(uiPosVer+1)*iSourceStride] - piSource[uiPosVer*iSourceStride] ) > uiSlopeMaxLeft )
3814    {
3815      uiSlopeMaxLeft = abs( piSource[(uiPosVer+1)*iSourceStride] - piSource[uiPosVer*iSourceStride] );
3816      uiPosSlopeMaxLeft = uiPosVer;
3817    }
3818  }
3819
3820  if( uiSlopeMaxAbove == 0 && uiSlopeMaxLeft == 0 ) 
3821  { 
3822    return false; 
3823  }
3824
3825  if( uiSlopeMaxAbove > uiSlopeMaxLeft )
3826  {
3827    ruiStartPosX = uiPosSlopeMaxAbove;
3828    ruiStartPosY = 0;
3829  }
3830  else
3831  {
3832    ruiStartPosX = 0;
3833    ruiStartPosY = uiPosSlopeMaxLeft;
3834  }
3835
3836  // 2nd step: derive wedge direction
3837  Int uiPreds[3] = {-1, -1, -1};
3838  Int iMode = -1;
3839  Int iPredNum = pcCU->getIntraDirLumaPredictor( uiAbsPartIdx, uiPreds, &iMode ); 
3840
3841  UInt uiDirMode = 0;
3842  if( iMode >= 0 ) { iPredNum = iMode; }
3843  if( iPredNum == 1 ) { uiDirMode = uiPreds[0]; }
3844  if( iPredNum == 2 ) { uiDirMode = uiPreds[1]; }
3845
3846  if( uiDirMode < 2 ) { return false; } // no planar & DC
3847
3848  Bool modeHor       = (uiDirMode < 18);
3849  Bool modeVer       = !modeHor;
3850  Int intraPredAngle = modeVer ? (Int)uiDirMode - VER_IDX : modeHor ? -((Int)uiDirMode - HOR_IDX) : 0;
3851  Int absAng         = abs(intraPredAngle);
3852  Int signAng        = intraPredAngle < 0 ? -1 : 1;
3853  Int angTable[9]    = {0,2,5,9,13,17,21,26,32};
3854  absAng             = angTable[absAng];
3855  intraPredAngle     = signAng * absAng;
3856
3857  // 3rd step: set slope for direction
3858  if( modeHor )
3859  {
3860    if( intraPredAngle > 0 )
3861    {
3862      riSlopeX = -32;
3863      riSlopeY = intraPredAngle;
3864    }
3865    else
3866    {
3867      riSlopeX = 32;
3868      riSlopeY = -intraPredAngle;
3869    }
3870  }
3871  else if( modeVer )
3872  {
3873    if( intraPredAngle > 0 )
3874    {
3875      riSlopeX = intraPredAngle;
3876      riSlopeY = -32;
3877    }
3878    else
3879    {
3880      riSlopeX = -intraPredAngle;
3881      riSlopeY = 32;
3882    }
3883  }
3884
3885  return true;
3886}
3887
3888Void 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 )
3889{
3890  ruhXs = 0;
3891  ruhYs = 0;
3892  ruhXe = 0;
3893  ruhYe = 0;
3894
3895  // scaling of start pos and block size to wedge resolution
3896  UInt uiScaledStartPosX = 0;
3897  UInt uiScaledStartPosY = 0;
3898  UInt uiScaledBlockSize = 0;
3899  WedgeResolution eWedgeRes = g_aeWedgeResolutionList[(UInt)g_aucConvertToBit[uiBlockSize]];
3900  switch( eWedgeRes )
3901  {
3902  case( DOUBLE_PEL ): { uiScaledStartPosX = (uiPMSPosX>>1); uiScaledStartPosY = (uiPMSPosY>>1); uiScaledBlockSize = (uiBlockSize>>1); break; }
3903  case(   FULL_PEL ): { uiScaledStartPosX =  uiPMSPosX;     uiScaledStartPosY =  uiPMSPosY;     uiScaledBlockSize =  uiBlockSize;     break; }
3904  case(   HALF_PEL ): { uiScaledStartPosX = (uiPMSPosX<<1); uiScaledStartPosY = (uiPMSPosY<<1); uiScaledBlockSize = (uiBlockSize<<1); break; }
3905  }
3906  Int iMaxPos = (Int)uiScaledBlockSize - 1;
3907
3908  // case above
3909  if( uiScaledStartPosX > 0 && uiScaledStartPosY == 0 )
3910  {
3911    ruhXs = (UChar)uiScaledStartPosX;
3912    ruhYs = 0;
3913
3914    if( iDeltaY == 0 )
3915    {
3916      if( iDeltaX < 0 )
3917      {
3918        ruhXe = 0;
3919        ruhYe = (UChar)std::min( std::max( iDeltaEnd, 0 ), iMaxPos );
3920        return;
3921      }
3922      else
3923      {
3924        ruhXe = (UChar)iMaxPos;
3925        ruhYe = (UChar)std::min( std::max( -iDeltaEnd, 0 ), iMaxPos );
3926        std::swap( ruhXs, ruhXe );
3927        std::swap( ruhYs, ruhYe );
3928        return;
3929      }
3930    }
3931
3932    // regular case
3933    Int iVirtualEndX = (Int)ruhXs + roftoi( (Double)iMaxPos * ((Double)iDeltaX / (Double)iDeltaY) );
3934
3935    if( iVirtualEndX < 0 )
3936    {
3937      Int iYe = roftoi( (Double)(0 - (Int)ruhXs) * ((Double)iDeltaY / (Double)iDeltaX) ) + iDeltaEnd;
3938      if( iYe < (Int)uiScaledBlockSize )
3939      {
3940        ruhXe = 0;
3941        ruhYe = (UChar)std::max( iYe, 0 );
3942        return;
3943      }
3944      else
3945      {
3946        ruhXe = (UChar)std::min( (iYe - iMaxPos), iMaxPos );
3947        ruhYe = (UChar)iMaxPos;
3948        return;
3949      }
3950    }
3951    else if( iVirtualEndX > iMaxPos )
3952    {
3953      Int iYe = roftoi( (Double)(iMaxPos - (Int)ruhXs) * ((Double)iDeltaY / (Double)iDeltaX) ) - iDeltaEnd;
3954      if( iYe < (Int)uiScaledBlockSize )
3955      {
3956        ruhXe = (UChar)iMaxPos;
3957        ruhYe = (UChar)std::max( iYe, 0 );
3958        std::swap( ruhXs, ruhXe );
3959        std::swap( ruhYs, ruhYe );
3960        return;
3961      }
3962      else
3963      {
3964        ruhXe = (UChar)std::max( (iMaxPos - (iYe - iMaxPos)), 0 );
3965        ruhYe = (UChar)iMaxPos;
3966        return;
3967      }
3968    }
3969    else
3970    {
3971      Int iXe = iVirtualEndX + iDeltaEnd;
3972      if( iXe < 0 )
3973      {
3974        ruhXe = 0;
3975        ruhYe = (UChar)std::max( (iMaxPos + iXe), 0 );
3976        return;
3977      }
3978      else if( iXe > iMaxPos )
3979      {
3980        ruhXe = (UChar)iMaxPos;
3981        ruhYe = (UChar)std::max( (iMaxPos - (iXe - iMaxPos)), 0 );
3982        std::swap( ruhXs, ruhXe );
3983        std::swap( ruhYs, ruhYe );
3984        return;
3985      }
3986      else
3987      {
3988        ruhXe = (UChar)iXe;
3989        ruhYe = (UChar)iMaxPos;
3990        return;
3991      }
3992    }
3993  }
3994
3995  // case left
3996  if( uiScaledStartPosY > 0 && uiScaledStartPosX == 0 )
3997  {
3998    ruhXs = 0;
3999    ruhYs = (UChar)uiScaledStartPosY;
4000
4001    if( iDeltaX == 0 )
4002    {
4003      if( iDeltaY < 0 )
4004      {
4005        ruhXe = (UChar)std::min( std::max( -iDeltaEnd, 0 ), iMaxPos );
4006        ruhYe = 0;
4007        std::swap( ruhXs, ruhXe );
4008        std::swap( ruhYs, ruhYe );
4009        return;
4010      }
4011      else
4012      {
4013        ruhXe = (UChar)std::min( std::max( iDeltaEnd, 0 ), iMaxPos );
4014        ruhYe = (UChar)iMaxPos;
4015        return; 
4016      }
4017    }
4018
4019    // regular case
4020    Int iVirtualEndY = (Int)ruhYs + roftoi( (Double)iMaxPos * ((Double)iDeltaY / (Double)iDeltaX) );
4021
4022    if( iVirtualEndY < 0 )
4023    {
4024      Int iXe = roftoi( (Double)(0 - (Int)ruhYs ) * ((Double)iDeltaX / (Double)iDeltaY) ) - iDeltaEnd;
4025      if( iXe < (Int)uiScaledBlockSize )
4026      {
4027        ruhXe = (UChar)std::max( iXe, 0 );
4028        ruhYe = 0;
4029        std::swap( ruhXs, ruhXe );
4030        std::swap( ruhYs, ruhYe );
4031        return;
4032      }
4033      else
4034      {
4035        ruhXe = (UChar)iMaxPos;
4036        ruhYe = (UChar)std::min( (iXe - iMaxPos), iMaxPos );
4037        std::swap( ruhXs, ruhXe );
4038        std::swap( ruhYs, ruhYe );
4039        return;
4040      }
4041    }
4042    else if( iVirtualEndY > (uiScaledBlockSize-1) )
4043    {
4044      Int iXe = roftoi( (Double)((Int)(uiScaledBlockSize-1) - (Int)ruhYs ) * ((Double)iDeltaX / (Double)iDeltaY) ) + iDeltaEnd;
4045      if( iXe < (Int)uiScaledBlockSize )
4046      {
4047        ruhXe = (UChar)std::max( iXe, 0 );
4048        ruhYe = (UChar)(uiScaledBlockSize-1);
4049        return;
4050      }
4051      else
4052      {
4053        ruhXe = (UChar)iMaxPos;
4054        ruhYe = (UChar)std::max( (iMaxPos - (iXe - iMaxPos)), 0 );
4055        std::swap( ruhXs, ruhXe );
4056        std::swap( ruhYs, ruhYe );
4057        return;
4058      }
4059    }
4060    else
4061    {
4062      Int iYe = iVirtualEndY - iDeltaEnd;
4063      if( iYe < 0 )
4064      {
4065        ruhXe = (UChar)std::max( (iMaxPos + iYe), 0 );
4066        ruhYe = 0;
4067        std::swap( ruhXs, ruhXe );
4068        std::swap( ruhYs, ruhYe );
4069        return;
4070      }
4071      else if( iYe > iMaxPos )
4072      {
4073        ruhXe = (UChar)std::max( (iMaxPos - (iYe - iMaxPos)), 0 );
4074        ruhYe = (UChar)iMaxPos;
4075        return;
4076      }
4077      else
4078      {
4079        ruhXe = (UChar)iMaxPos;
4080        ruhYe = (UChar)iYe;
4081        std::swap( ruhXs, ruhXe );
4082        std::swap( ruhYs, ruhYe );
4083        return;
4084      }
4085    }
4086  }
4087
4088  // case origin
4089  if( uiScaledStartPosX == 0 && uiScaledStartPosY == 0 )
4090  {
4091    if( iDeltaX*iDeltaY < 0 )
4092    {
4093      return;
4094    }
4095
4096    ruhXs = 0;
4097    ruhYs = 0;
4098
4099    if( iDeltaY == 0 )
4100    {
4101      ruhXe = (UChar)iMaxPos;
4102      ruhYe = 0;
4103      std::swap( ruhXs, ruhXe );
4104      std::swap( ruhYs, ruhYe );
4105      return;
4106    }
4107
4108    if( iDeltaX == 0 )
4109    {
4110      ruhXe = 0;
4111      ruhYe = (UChar)iMaxPos;
4112      return;
4113    }
4114
4115    Int iVirtualEndX = (Int)ruhXs + roftoi( (Double)iMaxPos * ((Double)iDeltaX / (Double)iDeltaY) );
4116
4117    if( iVirtualEndX > iMaxPos )
4118    {
4119      Int iYe = roftoi( (Double)((Int)iMaxPos - (Int)ruhXs) * ((Double)iDeltaY / (Double)iDeltaX) ) - iDeltaEnd;
4120      if( iYe < (Int)uiScaledBlockSize )
4121      {
4122        ruhXe = (UChar)(uiScaledBlockSize-1);
4123        ruhYe = (UChar)std::max( iYe, 0 );
4124        std::swap( ruhXs, ruhXe );
4125        std::swap( ruhYs, ruhYe );
4126        return;
4127      }
4128      else
4129      {
4130        ruhXe = (UChar)std::max( (iMaxPos - (iYe - iMaxPos)), 0 );
4131        ruhYe = (UChar)(uiScaledBlockSize-1);
4132        return;
4133      }
4134    }
4135    else
4136    {
4137      Int iXe = iVirtualEndX + iDeltaEnd;
4138      if( iXe < 0 )
4139      {
4140        ruhXe = 0;
4141        ruhYe = (UChar)std::max( (iMaxPos + iXe), 0 );
4142        return;
4143      }
4144      else if( iXe > iMaxPos )
4145      {
4146        ruhXe = (UChar)(uiScaledBlockSize-1);
4147        ruhYe = (UChar)std::max( (iMaxPos - (iXe - iMaxPos)), 0 );
4148        std::swap( ruhXs, ruhXe );
4149        std::swap( ruhYs, ruhYe );
4150        return;
4151      }
4152      else
4153      {
4154        ruhXe = (UChar)iXe;
4155        ruhYe = (UChar)(uiScaledBlockSize-1);
4156        return;
4157      }
4158    }
4159  }
4160}
4161#endif
4162
4163Void
4164TComPrediction::predIntraDepthAng(TComPattern* pcTComPattern, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight )
4165{
4166  Pel*  pDst    = piPred;
4167  Int*  ptrSrc  = pcTComPattern->getAdiOrgBuf( iWidth, iHeight, m_piYuvExt );
4168  Int   sw      = ( iWidth<<1 ) + 1;
4169  xPredIntraAngDepth( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode );
4170}
4171
4172Int
4173TComPrediction::xGetDCDepth( Int* pSrc, Int iDelta, Int iBlkSize )
4174{
4175  Int iDC    = PDM_UNDEFINED_DEPTH;
4176  Int iSum   = 0;
4177  Int iNum   = 0;
4178  for( Int k = 0; k < iBlkSize; k++, pSrc += iDelta )
4179  {
4180    if( *pSrc != PDM_UNDEFINED_DEPTH )
4181    {
4182      iSum += *pSrc;
4183      iNum ++;
4184    }
4185  }
4186  if( iNum )
4187  {
4188    iDC = ( iSum + ( iNum >> 1 ) ) / iNum;
4189  }
4190  return iDC;
4191}
4192
4193Int
4194TComPrediction::xGetDCValDepth( Int iVal1, Int iVal2, Int iVal3, Int iVal4 )
4195{
4196  if     ( iVal1 != PDM_UNDEFINED_DEPTH )   return iVal1;
4197  else if( iVal2 != PDM_UNDEFINED_DEPTH )   return iVal2;
4198  else if( iVal3 != PDM_UNDEFINED_DEPTH )   return iVal3;
4199  return   iVal4;
4200}
4201
4202Void
4203TComPrediction::xPredIntraAngDepth( Int* pSrc, Int srcStride, Pel* pDst, Int dstStride, UInt width, UInt height, UInt dirMode )
4204{
4205  AOF( width == height );
4206  Int blkSize       = width;
4207  Int iDCAbove      = xGetDCDepth( pSrc - srcStride,                               1, blkSize );
4208  Int iDCAboveRight = xGetDCDepth( pSrc - srcStride + blkSize,                     1, blkSize );
4209  Int iDCLeft       = xGetDCDepth( pSrc -         1,                       srcStride, blkSize );
4210  Int iDCBelowLeft  = xGetDCDepth( pSrc -         1 + blkSize * srcStride, srcStride, blkSize );
4211  Int iWgt, iDC1, iDC2;
4212  if( dirMode < 2 ) // 1..2
4213  {
4214    iDC1  = xGetDCValDepth( iDCAbove, iDCAboveRight, iDCLeft,  iDCBelowLeft  );
4215    iDC2  = xGetDCValDepth( iDCLeft,  iDCBelowLeft,  iDCAbove, iDCAboveRight );
4216    iWgt  = 8;
4217  }
4218  else if( dirMode < 11 ) // 3..10
4219  {
4220    iDC1  = xGetDCValDepth( iDCLeft,  iDCBelowLeft,  iDCAbove, iDCAboveRight );
4221    iDC2  = xGetDCValDepth( iDCBelowLeft,  iDCLeft,  iDCAbove, iDCAboveRight );
4222    iWgt  = 6 + dirMode; 
4223  }
4224  else if( dirMode < 27 ) // 11..26
4225  {
4226    iDC1  = xGetDCValDepth( iDCAbove, iDCAboveRight, iDCLeft,  iDCBelowLeft  );
4227    iDC2  = xGetDCValDepth( iDCLeft,  iDCBelowLeft,  iDCAbove, iDCAboveRight );
4228    iWgt  = dirMode - 10;
4229  }
4230  else if( dirMode < 35 ) // 27..34
4231  {
4232    iDC1  = xGetDCValDepth( iDCAbove, iDCAboveRight, iDCLeft,  iDCBelowLeft  );
4233    iDC2  = xGetDCValDepth( iDCAboveRight, iDCAbove, iDCLeft,  iDCBelowLeft  );
4234    iWgt  = 42 - dirMode;
4235  }
4236  else // (wedgelet -> use simple DC prediction
4237  {
4238    iDC1  = xGetDCValDepth( iDCAbove, iDCAboveRight, iDCLeft,  iDCBelowLeft  );
4239    iDC2  = xGetDCValDepth( iDCLeft,  iDCBelowLeft,  iDCAbove, iDCAboveRight );
4240    iWgt  = 8;
4241  }
4242  Int iWgt2   = 16 - iWgt;
4243  Int iDCVal  = ( iWgt * iDC1 + iWgt2 * iDC2 + 8 ) >> 4;
4244
4245  // set depth
4246  for( Int iY = 0; iY < blkSize; iY++, pDst += dstStride )
4247  {
4248    for( Int iX = 0; iX < blkSize; iX++ )
4249    {
4250      pDst[ iX ] = iDCVal;
4251    }
4252  }
4253}
4254
4255//! \}
Note: See TracBrowser for help on using the repository browser.