source: 3DVCSoftware/branches/HTM-13.1-dev2-Sony/source/Lib/TLibCommon/TComPrediction.cpp @ 1157

Last change on this file since 1157 was 1152, checked in by hisilicon-htm, 10 years ago

Integration of K0048

  • Property svn:eol-style set to native
File size: 82.4 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-2014, 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
48TComPrediction::TComPrediction()
49: m_pLumaRecBuffer(0)
50, m_iLumaRecStride(0)
51{
52  m_piYuvExt = NULL;
53#if H_3D_VSP
54  m_pDepthBlock = (Int*) malloc(MAX_NUM_SPU_W*MAX_NUM_SPU_W*sizeof(Int));
55  if (m_pDepthBlock == NULL)
56      printf("ERROR: UKTGHU, No memory allocated.\n");
57#endif
58}
59
60TComPrediction::~TComPrediction()
61{
62#if H_3D_VSP
63  if (m_pDepthBlock != NULL)
64      free(m_pDepthBlock);
65  m_cYuvDepthOnVsp.destroy();
66#endif
67
68  delete[] m_piYuvExt;
69
70  m_acYuvPred[0].destroy();
71  m_acYuvPred[1].destroy();
72
73  m_cYuvPredTemp.destroy();
74
75#if H_3D_ARP
76  m_acYuvPredBase[0].destroy();
77  m_acYuvPredBase[1].destroy();
78#endif
79  if( m_pLumaRecBuffer )
80  {
81    delete [] m_pLumaRecBuffer;
82  }
83 
84  Int i, j;
85  for (i = 0; i < 4; i++)
86  {
87    for (j = 0; j < 4; j++)
88    {
89      m_filteredBlock[i][j].destroy();
90    }
91    m_filteredBlockTmp[i].destroy();
92  }
93}
94
95Void TComPrediction::initTempBuff()
96{
97  if( m_piYuvExt == NULL )
98  {
99    Int extWidth  = MAX_CU_SIZE + 16; 
100    Int extHeight = MAX_CU_SIZE + 1;
101    Int i, j;
102    for (i = 0; i < 4; i++)
103    {
104      m_filteredBlockTmp[i].create(extWidth, extHeight + 7);
105      for (j = 0; j < 4; j++)
106      {
107        m_filteredBlock[i][j].create(extWidth, extHeight);
108      }
109    }
110    m_iYuvExtHeight  = ((MAX_CU_SIZE + 2) << 4);
111    m_iYuvExtStride = ((MAX_CU_SIZE  + 8) << 4);
112    m_piYuvExt = new Int[ m_iYuvExtStride * m_iYuvExtHeight ];
113
114    // new structure
115    m_acYuvPred[0] .create( MAX_CU_SIZE, MAX_CU_SIZE );
116    m_acYuvPred[1] .create( MAX_CU_SIZE, MAX_CU_SIZE );
117
118    m_cYuvPredTemp.create( MAX_CU_SIZE, MAX_CU_SIZE );
119#if H_3D_ARP
120    m_acYuvPredBase[0] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
121    m_acYuvPredBase[1] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
122#endif
123#if H_3D_VSP
124    m_cYuvDepthOnVsp.create( g_uiMaxCUWidth, g_uiMaxCUHeight );
125#endif
126  }
127
128  if (m_iLumaRecStride != (MAX_CU_SIZE>>1) + 1)
129  {
130    m_iLumaRecStride =  (MAX_CU_SIZE>>1) + 1;
131    if (!m_pLumaRecBuffer)
132    {
133      m_pLumaRecBuffer = new Pel[ m_iLumaRecStride * m_iLumaRecStride ];
134    }
135  }
136#if H_3D_IC
137  m_uiaShift[0] = 0;
138  for( Int i = 1; i < 64; i++ )
139  {
140    m_uiaShift[i] = ( (1 << 15) + i/2 ) / i;
141  }
142#endif
143}
144
145// ====================================================================================================================
146// Public member functions
147// ====================================================================================================================
148
149// Function for calculating DC value of the reference samples used in Intra prediction
150Pel TComPrediction::predIntraGetPredValDC( Int* pSrc, Int iSrcStride, UInt iWidth, UInt iHeight, Bool bAbove, Bool bLeft )
151{
152  assert(iWidth > 0 && iHeight > 0);
153  Int iInd, iSum = 0;
154  Pel pDcVal;
155
156  if (bAbove)
157  {
158    for (iInd = 0;iInd < iWidth;iInd++)
159    {
160      iSum += pSrc[iInd-iSrcStride];
161    }
162  }
163  if (bLeft)
164  {
165    for (iInd = 0;iInd < iHeight;iInd++)
166    {
167      iSum += pSrc[iInd*iSrcStride-1];
168    }
169  }
170
171  if (bAbove && bLeft)
172  {
173    pDcVal = (iSum + iWidth) / (iWidth + iHeight);
174  }
175  else if (bAbove)
176  {
177    pDcVal = (iSum + iWidth/2) / iWidth;
178  }
179  else if (bLeft)
180  {
181    pDcVal = (iSum + iHeight/2) / iHeight;
182  }
183  else
184  {
185    pDcVal = pSrc[-1]; // Default DC value already calculated and placed in the prediction array if no neighbors are available
186  }
187 
188  return pDcVal;
189}
190
191// Function for deriving the angular Intra predictions
192
193/** Function for deriving the simplified angular intra predictions.
194 * \param pSrc pointer to reconstructed sample array
195 * \param srcStride the stride of the reconstructed sample array
196 * \param rpDst reference to pointer for the prediction sample array
197 * \param dstStride the stride of the prediction sample array
198 * \param width the width of the block
199 * \param height the height of the block
200 * \param dirMode the intra prediction mode index
201 * \param blkAboveAvailable boolean indication if the block above is available
202 * \param blkLeftAvailable boolean indication if the block to the left is available
203 *
204 * This function derives the prediction samples for the angular mode based on the prediction direction indicated by
205 * the prediction mode index. The prediction direction is given by the displacement of the bottom row of the block and
206 * the reference row above the block in the case of vertical prediction or displacement of the rightmost column
207 * of the block and reference column left from the block in the case of the horizontal prediction. The displacement
208 * is signalled at 1/32 pixel accuracy. When projection of the predicted pixel falls inbetween reference samples,
209 * the predicted value for the pixel is linearly interpolated from the reference samples. All reference samples are taken
210 * from the extended main reference.
211 */
212Void TComPrediction::xPredIntraAng(Int bitDepth, Int* pSrc, Int srcStride, Pel*& rpDst, Int dstStride, UInt width, UInt height, UInt dirMode, Bool blkAboveAvailable, Bool blkLeftAvailable, Bool bFilter )
213{
214  Int k,l;
215  Int blkSize        = width;
216  Pel* pDst          = rpDst;
217
218  // Map the mode index to main prediction direction and angle
219  assert( dirMode > 0 ); //no planar
220  Bool modeDC        = dirMode < 2;
221  Bool modeHor       = !modeDC && (dirMode < 18);
222  Bool modeVer       = !modeDC && !modeHor;
223  Int intraPredAngle = modeVer ? (Int)dirMode - VER_IDX : modeHor ? -((Int)dirMode - HOR_IDX) : 0;
224  Int absAng         = abs(intraPredAngle);
225  Int signAng        = intraPredAngle < 0 ? -1 : 1;
226
227  // Set bitshifts and scale the angle parameter to block size
228  Int angTable[9]    = {0,    2,    5,   9,  13,  17,  21,  26,  32};
229  Int invAngTable[9] = {0, 4096, 1638, 910, 630, 482, 390, 315, 256}; // (256 * 32) / Angle
230  Int invAngle       = invAngTable[absAng];
231  absAng             = angTable[absAng];
232  intraPredAngle     = signAng * absAng;
233
234  // Do the DC prediction
235  if (modeDC)
236  {
237    Pel dcval = predIntraGetPredValDC(pSrc, srcStride, width, height, blkAboveAvailable, blkLeftAvailable);
238
239    for (k=0;k<blkSize;k++)
240    {
241      for (l=0;l<blkSize;l++)
242      {
243        pDst[k*dstStride+l] = dcval;
244      }
245    }
246  }
247
248  // Do angular predictions
249  else
250  {
251    Pel* refMain;
252    Pel* refSide;
253    Pel  refAbove[2*MAX_CU_SIZE+1];
254    Pel  refLeft[2*MAX_CU_SIZE+1];
255
256    // Initialise the Main and Left reference array.
257    if (intraPredAngle < 0)
258    {
259      for (k=0;k<blkSize+1;k++)
260      {
261        refAbove[k+blkSize-1] = pSrc[k-srcStride-1];
262      }
263      for (k=0;k<blkSize+1;k++)
264      {
265        refLeft[k+blkSize-1] = pSrc[(k-1)*srcStride-1];
266      }
267      refMain = (modeVer ? refAbove : refLeft) + (blkSize-1);
268      refSide = (modeVer ? refLeft : refAbove) + (blkSize-1);
269
270      // Extend the Main reference to the left.
271      Int invAngleSum    = 128;       // rounding for (shift by 8)
272      for (k=-1; k>blkSize*intraPredAngle>>5; k--)
273      {
274        invAngleSum += invAngle;
275        refMain[k] = refSide[invAngleSum>>8];
276      }
277    }
278    else
279    {
280      for (k=0;k<2*blkSize+1;k++)
281      {
282        refAbove[k] = pSrc[k-srcStride-1];
283      }
284      for (k=0;k<2*blkSize+1;k++)
285      {
286        refLeft[k] = pSrc[(k-1)*srcStride-1];
287      }
288      refMain = modeVer ? refAbove : refLeft;
289      refSide = modeVer ? refLeft  : refAbove;
290    }
291
292    if (intraPredAngle == 0)
293    {
294      for (k=0;k<blkSize;k++)
295      {
296        for (l=0;l<blkSize;l++)
297        {
298          pDst[k*dstStride+l] = refMain[l+1];
299        }
300      }
301
302      if ( bFilter )
303      {
304        for (k=0;k<blkSize;k++)
305        {
306          pDst[k*dstStride] = Clip3(0, (1<<bitDepth)-1, pDst[k*dstStride] + (( refSide[k+1] - refSide[0] ) >> 1) );
307        }
308      }
309    }
310    else
311    {
312      Int deltaPos=0;
313      Int deltaInt;
314      Int deltaFract;
315      Int refMainIndex;
316
317      for (k=0;k<blkSize;k++)
318      {
319        deltaPos += intraPredAngle;
320        deltaInt   = deltaPos >> 5;
321        deltaFract = deltaPos & (32 - 1);
322
323        if (deltaFract)
324        {
325          // Do linear filtering
326          for (l=0;l<blkSize;l++)
327          {
328            refMainIndex        = l+deltaInt+1;
329            pDst[k*dstStride+l] = (Pel) ( ((32-deltaFract)*refMain[refMainIndex]+deltaFract*refMain[refMainIndex+1]+16) >> 5 );
330          }
331        }
332        else
333        {
334          // Just copy the integer samples
335          for (l=0;l<blkSize;l++)
336          {
337            pDst[k*dstStride+l] = refMain[l+deltaInt+1];
338          }
339        }
340      }
341    }
342
343    // Flip the block if this is the horizontal mode
344    if (modeHor)
345    {
346      Pel  tmp;
347      for (k=0;k<blkSize-1;k++)
348      {
349        for (l=k+1;l<blkSize;l++)
350        {
351          tmp                 = pDst[k*dstStride+l];
352          pDst[k*dstStride+l] = pDst[l*dstStride+k];
353          pDst[l*dstStride+k] = tmp;
354        }
355      }
356    }
357  }
358}
359
360Void TComPrediction::predIntraLumaAng(TComPattern* pcTComPattern, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft )
361{
362  Pel *pDst = piPred;
363  Int *ptrSrc;
364
365  assert( g_aucConvertToBit[ iWidth ] >= 0 ); //   4x  4
366  assert( g_aucConvertToBit[ iWidth ] <= 5 ); // 128x128
367  assert( iWidth == iHeight  );
368
369  ptrSrc = pcTComPattern->getPredictorPtr( uiDirMode, g_aucConvertToBit[ iWidth ] + 2, m_piYuvExt );
370
371  // get starting pixel in block
372  Int sw = 2 * iWidth + 1;
373
374  // Create the prediction
375  if ( uiDirMode == PLANAR_IDX )
376  {
377    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
378  }
379  else
380  {
381    if ( (iWidth > 16) || (iHeight > 16) )
382    {
383      xPredIntraAng(g_bitDepthY, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, false );
384    }
385    else
386    {
387      xPredIntraAng(g_bitDepthY, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, true );
388
389      if( (uiDirMode == DC_IDX ) && bAbove && bLeft )
390      {
391        xDCPredFiltering( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight);
392      }
393    }
394  }
395}
396
397// Angular chroma
398Void TComPrediction::predIntraChromaAng( Int* piSrc, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft )
399{
400  Pel *pDst = piPred;
401  Int *ptrSrc = piSrc;
402
403  // get starting pixel in block
404  Int sw = 2 * iWidth + 1;
405
406  if ( uiDirMode == PLANAR_IDX )
407  {
408    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
409  }
410  else
411  {
412    // Create the prediction
413    xPredIntraAng(g_bitDepthC, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, false );
414  }
415}
416
417#if H_3D_DIM
418Void TComPrediction::predIntraLumaDepth( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiIntraMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bFastEnc, TComWedgelet* dmm4Segmentation  )
419{
420  assert( iWidth == iHeight  );
421  assert( iWidth >= DIM_MIN_SIZE && iWidth <= DIM_MAX_SIZE );
422  assert( isDimMode( uiIntraMode ) );
423
424  UInt dimType    = getDimType  ( uiIntraMode );
425  Bool isDmmMode  = (dimType <  DMM_NUM_TYPE);
426
427  Bool* biSegPattern  = NULL;
428  UInt  patternStride = 0;
429
430  // get partiton
431#if H_3D_DIM_DMM
432  TComWedgelet* dmmSegmentation = NULL;
433  if( isDmmMode )
434  {
435    switch( dimType )
436    {
437    case( DMM1_IDX ): 
438      {
439        dmmSegmentation = pcCU->isDMM1UpscaleMode((UInt)iWidth) ? 
440            &(g_dmmWedgeLists[ g_aucConvertToBit[pcCU->getDMM1BasePatternWidth((UInt)iWidth)] ][ pcCU->getDmmWedgeTabIdx( dimType, uiAbsPartIdx ) ]) : 
441            &(g_dmmWedgeLists[ g_aucConvertToBit[iWidth] ][ pcCU->getDmmWedgeTabIdx( dimType, uiAbsPartIdx ) ]);
442      } break;
443    case( DMM4_IDX ): 
444      {
445        if( dmm4Segmentation == NULL )
446        { 
447          dmmSegmentation = new TComWedgelet( iWidth, iHeight );
448          xPredContourFromTex( pcCU, uiAbsPartIdx, iWidth, iHeight, dmmSegmentation );
449        }
450        else
451        {
452          xPredContourFromTex( pcCU, uiAbsPartIdx, iWidth, iHeight, dmm4Segmentation );
453          dmmSegmentation = dmm4Segmentation;
454        }
455      } break;
456    default: assert(0);
457    }
458    assert( dmmSegmentation );
459    if( dimType == DMM1_IDX && pcCU->isDMM1UpscaleMode((UInt)iWidth) ) 
460    {
461        biSegPattern = dmmSegmentation->getScaledPattern((UInt)iWidth);
462        patternStride = iWidth;
463    } 
464    else 
465    { 
466        biSegPattern  = dmmSegmentation->getPattern();
467        patternStride = dmmSegmentation->getStride ();
468    }
469  }
470#endif
471
472  // get predicted partition values
473  assert( biSegPattern );
474  Int* piMask = NULL;
475  piMask = pcCU->getPattern()->getAdiOrgBuf( iWidth, iHeight, m_piYuvExt ); // no filtering
476  assert( piMask );
477  Int maskStride = 2*iWidth + 1; 
478  Int* ptrSrc = piMask+maskStride+1;
479  Pel predDC1 = 0; Pel predDC2 = 0;
480  xPredBiSegDCs( ptrSrc, maskStride, biSegPattern, patternStride, predDC1, predDC2 );
481
482  // set segment values with deltaDC offsets
483  Pel segDC1 = 0;
484  Pel segDC2 = 0;
485  if( !pcCU->getSDCFlag( uiAbsPartIdx ) )
486  {
487    Pel deltaDC1 = pcCU->getDimDeltaDC( dimType, 0, uiAbsPartIdx );
488    Pel deltaDC2 = pcCU->getDimDeltaDC( dimType, 1, uiAbsPartIdx );
489#if H_3D_DIM_DMM
490    if( isDmmMode )
491    {
492#if H_3D_DIM_DLT
493      segDC1 = pcCU->getSlice()->getPPS()->getDLT()->idx2DepthValue( pcCU->getSlice()->getLayerIdInVps(), pcCU->getSlice()->getPPS()->getDLT()->depthValue2idx( pcCU->getSlice()->getLayerIdInVps(), predDC1 ) + deltaDC1 );
494      segDC2 = pcCU->getSlice()->getPPS()->getDLT()->idx2DepthValue( pcCU->getSlice()->getLayerIdInVps(), pcCU->getSlice()->getPPS()->getDLT()->depthValue2idx( pcCU->getSlice()->getLayerIdInVps(), predDC2 ) + deltaDC2 );
495#else
496      segDC1 = ClipY( predDC1 + deltaDC1 );
497      segDC2 = ClipY( predDC2 + deltaDC2 );
498#endif
499    }
500#endif
501  }
502  else
503  {
504    segDC1 = predDC1;
505    segDC2 = predDC2;
506  }
507
508  // set prediction signal
509  Pel* pDst = piPred;
510  xAssignBiSegDCs( pDst, uiStride, biSegPattern, patternStride, segDC1, segDC2 );
511  pcCU->setDmmPredictor(segDC1, 0);
512  pcCU->setDmmPredictor(segDC2, 1);
513
514#if H_3D_DIM_DMM
515  if( dimType == DMM4_IDX && dmm4Segmentation == NULL ) { dmmSegmentation->destroy(); delete dmmSegmentation; }
516#endif
517}
518#endif
519
520/** Function for checking identical motion.
521 * \param TComDataCU* pcCU
522 * \param UInt PartAddr
523 */
524Bool TComPrediction::xCheckIdenticalMotion ( TComDataCU* pcCU, UInt PartAddr )
525{
526  if( pcCU->getSlice()->isInterB() && !pcCU->getSlice()->getPPS()->getWPBiPred() )
527  {
528    if( pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr) >= 0 && pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr) >= 0)
529    {
530      Int RefPOCL0 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_0, pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr))->getPOC();
531      Int RefPOCL1 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_1, pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr))->getPOC();
532#if H_3D_ARP
533      if(!pcCU->getARPW(PartAddr) && RefPOCL0 == RefPOCL1 && pcCU->getCUMvField(REF_PIC_LIST_0)->getMv(PartAddr) == pcCU->getCUMvField(REF_PIC_LIST_1)->getMv(PartAddr))
534#else
535      if(RefPOCL0 == RefPOCL1 && pcCU->getCUMvField(REF_PIC_LIST_0)->getMv(PartAddr) == pcCU->getCUMvField(REF_PIC_LIST_1)->getMv(PartAddr))
536#endif
537      {
538        return true;
539      }
540    }
541  }
542  return false;
543}
544
545#if H_3D_SPIVMP
546Void TComPrediction::xGetSubPUAddrAndMerge(TComDataCU* pcCU, UInt uiPartAddr, Int iSPWidth, Int iSPHeight, Int iNumSPInOneLine, Int iNumSP, UInt* uiMergedSPW, UInt* uiMergedSPH, UInt* uiSPAddr )
547{
548  for (Int i = 0; i < iNumSP; i++)
549  {
550    uiMergedSPW[i] = iSPWidth;
551    uiMergedSPH[i] = iSPHeight;
552    pcCU->getSPAbsPartIdx(uiPartAddr, iSPWidth, iSPHeight, i, iNumSPInOneLine, uiSPAddr[i]);
553  }
554  if( pcCU->getARPW( uiPartAddr ) != 0 )
555  {
556    return;
557  }
558
559  // horizontal sub-PU merge
560  for (Int i=0; i<iNumSP; i++)
561  {
562    if (i % iNumSPInOneLine == iNumSPInOneLine - 1 || uiMergedSPW[i]==0 || uiMergedSPH[i]==0)
563    {
564      continue;
565    }
566    for (Int j=i+1; j<i+iNumSPInOneLine-i%iNumSPInOneLine; j++)
567    {
568      if (xCheckTwoSPMotion(pcCU, uiSPAddr[i], uiSPAddr[j]))
569      {
570        uiMergedSPW[i] += iSPWidth;
571        uiMergedSPW[j] = uiMergedSPH[j] = 0;
572      }
573      else
574      {
575        break;
576      }
577    }
578  }
579  //vertical sub-PU merge
580  for (Int i=0; i<iNumSP-iNumSPInOneLine; i++)
581  {
582    if (uiMergedSPW[i]==0 || uiMergedSPH[i]==0)
583    {
584      continue;
585    }
586    for (Int j=i+iNumSPInOneLine; j<iNumSP; j+=iNumSPInOneLine)
587    {
588      if (xCheckTwoSPMotion(pcCU, uiSPAddr[i], uiSPAddr[j]) && uiMergedSPW[i]==uiMergedSPW[j])
589      {
590        uiMergedSPH[i] += iSPHeight;
591        uiMergedSPH[j] = uiMergedSPW[j] = 0;
592      }
593      else
594      {
595        break;
596      }
597    }
598  }
599}
600
601Bool TComPrediction::xCheckTwoSPMotion ( TComDataCU* pcCU, UInt PartAddr0, UInt PartAddr1 )
602{
603  if( pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr0) != pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr1))
604  {
605    return false;
606  }
607  if( pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr0) != pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr1))
608  {
609    return false;
610  }
611
612  if (pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr0) >= 0)
613  {
614    if (pcCU->getCUMvField(REF_PIC_LIST_0)->getMv(PartAddr0) != pcCU->getCUMvField(REF_PIC_LIST_0)->getMv(PartAddr1))
615    {
616      return false;
617    }
618  }
619
620  if (pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr0) >= 0)
621  {
622    if (pcCU->getCUMvField(REF_PIC_LIST_1)->getMv(PartAddr0) != pcCU->getCUMvField(REF_PIC_LIST_1)->getMv(PartAddr1))
623    {
624      return false;
625    }
626  }
627  return true;
628}
629#endif
630
631#if H_3D_DBBP
632#if HS_DBBP_CLEAN_K0048
633PartSize TComPrediction::getPartitionSizeFromDepth(Pel* pDepthPels, UInt uiDepthStride, UInt uiSize, TComDataCU*& pcCU)
634#else
635PartSize TComPrediction::getPartitionSizeFromDepth(Pel* pDepthPels, UInt uiDepthStride, UInt uiSize)
636#endif
637{
638  // find virtual partitioning for this CU based on depth block
639  // segmentation of texture block --> mask IDs
640  Pel*  pDepthBlockStart    = pDepthPels;
641 
642  // first compute average of depth block for thresholding
643  Int iSumDepth = 0;
644  Int iSubSample = 4;
645#if HS_DBBP_CLEAN_K0048
646  Int iPictureWidth = pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getPicYuvRec()->getWidth();
647  Int iPictureHeight = pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getPicYuvRec()->getHeight();
648  TComMv cDv = pcCU->getSlice()->getDepthRefinementFlag(  ) ? pcCU->getDvInfo(0).m_acDoNBDV : pcCU->getDvInfo(0).m_acNBDV;
649  if( pcCU->getSlice()->getDepthRefinementFlag(  ) )
650  {
651    cDv.setVer(0);
652  }
653  Int iBlkX = ( pcCU->getAddr() % pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ pcCU->getZorderIdxInCU() ] ]+ ((cDv.getHor()+2)>>2);
654  Int iBlkY = ( pcCU->getAddr() / pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ pcCU->getZorderIdxInCU() ] ]+ ((cDv.getVer()+2)>>2);
655  UInt t=0;
656
657  for (Int y=0; y<uiSize; y+=iSubSample)
658   {
659    for (Int x=0; x<uiSize; x+=iSubSample)
660   {
661if (iBlkX+x>iPictureWidth)
662{
663    Int depthPel = pDepthPels[t];
664    iSumDepth += depthPel;
665} 
666else
667{
668    Int depthPel = pDepthPels[x];
669    t=x;
670    iSumDepth += depthPel;
671}
672   }
673   
674    // next row
675    if (!(iBlkY+y+4>iPictureHeight))
676    pDepthPels += uiDepthStride*iSubSample;
677#else
678  for (Int y=0; y<uiSize; y+=iSubSample)
679  {
680     for (Int x=0; x<uiSize; x+=iSubSample)
681        {
682                  Int depthPel = pDepthPels[x];
683
684                  iSumDepth += depthPel;
685        }
686
687          // next row
688          pDepthPels += uiDepthStride*iSubSample;
689#endif
690  }
691 
692  Int iSizeInBits = g_aucConvertToBit[uiSize] - g_aucConvertToBit[iSubSample];  // respect sub-sampling factor
693  Int iMean = iSumDepth >> iSizeInBits*2;       // iMean /= (uiSize*uiSize);
694 
695  // start again for segmentation
696  pDepthPels = pDepthBlockStart;
697 
698  // start mapping process
699  Int matchedPartSum[2][2] = {{0,0},{0,0}}; // counter for each part size and boolean option
700  PartSize virtualPartSizes[2] = { SIZE_Nx2N, SIZE_2NxN};
701 
702  UInt uiHalfSize = uiSize>>1;
703  for (Int y=0; y<uiSize; y+=iSubSample)
704  {
705    for (Int x=0; x<uiSize; x+=iSubSample)
706    {
707#if HS_DBBP_CLEAN_K0048
708      Int depthPel = 0;
709if (iBlkX+x>iPictureWidth)
710{
711    depthPel = pDepthPels[t];
712}
713else
714{ 
715    depthPel = pDepthPels[x];
716     t=x;
717}
718#else
719            Int depthPel = pDepthPels[x];
720#endif
721     
722      // decide which segment this pixel belongs to
723      Int ucSegment = (Int)(depthPel>iMean);
724     
725      // Matched Filter to find optimal (conventional) partitioning
726     
727      // SIZE_Nx2N
728      if(x<uiHalfSize)  // left
729      {
730        matchedPartSum[0][ucSegment]++;
731      }
732      else  // right
733      {
734        matchedPartSum[0][1-ucSegment]++;
735      }
736     
737      // SIZE_2NxN
738      if(y<uiHalfSize)  // top
739      {
740        matchedPartSum[1][ucSegment]++;
741      }
742      else  // bottom
743      {
744        matchedPartSum[1][1-ucSegment]++;
745      }
746    }
747   
748    // next row
749#if HS_DBBP_CLEAN_K0048
750    if (!(iBlkY+y+4>iPictureHeight))
751#endif
752    pDepthPels += uiDepthStride*iSubSample;
753  }
754 
755  PartSize matchedPartSize = SIZE_NONE;
756 
757  Int iMaxMatchSum = 0;
758  for(Int p=0; p<2; p++)  // loop over partition
759  {
760    for( Int b=0; b<=1; b++ ) // loop over boolean options
761    {
762      if(matchedPartSum[p][b] > iMaxMatchSum)
763      {
764        iMaxMatchSum = matchedPartSum[p][b];
765        matchedPartSize = virtualPartSizes[p];
766      }
767    }
768  }
769 
770  AOF( matchedPartSize != SIZE_NONE );
771 
772  return matchedPartSize;
773}
774
775#if HS_DBBP_CLEAN_K0048
776Bool TComPrediction::getSegmentMaskFromDepth( Pel* pDepthPels, UInt uiDepthStride, UInt uiWidth, UInt uiHeight, Bool* pMask, TComDataCU*& pcCU)
777#else
778Bool TComPrediction::getSegmentMaskFromDepth( Pel* pDepthPels, UInt uiDepthStride, UInt uiWidth, UInt uiHeight, Bool* pMask)
779#endif
780{
781  // segmentation of texture block --> mask IDs
782  Pel*  pDepthBlockStart      = pDepthPels;
783 
784  // first compute average of depth block for thresholding
785  Int iSumDepth = 0;
786  Int uiMinDepth = MAX_INT;
787  Int uiMaxDepth = 0;
788#if HS_DBBP_CLEAN_K0048
789  uiMinDepth = pDepthPels[ 0 ];
790  uiMaxDepth = pDepthPels[ 0 ];
791  iSumDepth  = pDepthPels[ 0 ];
792    UInt t=0;
793  Int iPictureWidth = pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getPicYuvRec()->getWidth();
794  Int iPictureHeight = pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getPicYuvRec()->getHeight(); 
795  TComMv cDv = pcCU->getSlice()->getDepthRefinementFlag(  ) ? pcCU->getDvInfo(0).m_acDoNBDV : pcCU->getDvInfo(0).m_acNBDV;
796  if( pcCU->getSlice()->getDepthRefinementFlag(  ) )
797  {
798      cDv.setVer(0);
799  }
800  Int iBlkX = ( pcCU->getAddr() % pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ pcCU->getZorderIdxInCU() ] ]+ ((cDv.getHor()+2)>>2);
801  Int iBlkY = ( pcCU->getAddr() / pcCU->getSlice()->getIvPic (true, pcCU->getDvInfo(0).m_aVIdxCan)->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ pcCU->getZorderIdxInCU() ] ]+ ((cDv.getVer()+2)>>2);
802if (iBlkX>(Int)(iPictureWidth - uiWidth))
803{
804  iSumDepth += pDepthPels[ iPictureWidth - iBlkX - 1 ];
805  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ iPictureWidth - iBlkX - 1 ]);
806  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ iPictureWidth - iBlkX - 1 ]);
807}
808else
809{
810  iSumDepth += pDepthPels[ uiWidth - 1 ];
811  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiWidth - 1 ]);
812  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiWidth - 1 ]);
813}
814if (iBlkY>(Int)(iPictureHeight - uiHeight))
815{
816  iSumDepth += pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) ];
817  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) ]);
818  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) ]);
819}
820else
821{
822  iSumDepth += pDepthPels[ uiDepthStride * (uiHeight - 1) ];
823  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) ]);
824  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) ]);
825}
826if (iBlkY>(Int)(iPictureHeight - uiHeight) && iBlkX>(Int)(iPictureWidth - uiWidth))
827{
828  iSumDepth += pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) + iPictureWidth - iBlkX - 1 ];
829  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) + iPictureWidth - iBlkX - 1 ]);
830  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) + iPictureWidth - iBlkX - 1 ]);
831}
832else if (iBlkY>(Int)(iPictureHeight - uiHeight))
833{
834  iSumDepth += pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) + uiWidth - 1 ];
835  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) + uiWidth - 1 ]);
836  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (iPictureHeight - iBlkY - 1) + uiWidth - 1 ]);
837}
838else if (iBlkX>(Int)(iPictureWidth - uiWidth))
839{
840  iSumDepth += pDepthPels[ uiDepthStride * (uiHeight - 1) + iPictureWidth - iBlkX - 1 ];
841  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) + iPictureWidth - iBlkX - 1 ]);
842  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) + iPictureWidth - iBlkX - 1 ]);
843}
844else
845{
846  iSumDepth += pDepthPels[ uiDepthStride * (uiHeight - 1) + uiWidth - 1 ];
847  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) + uiWidth - 1 ]);
848  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) + uiWidth - 1 ]);
849}
850#else
851  iSumDepth  = pDepthPels[ 0 ];
852  iSumDepth += pDepthPels[ uiWidth - 1 ];
853  iSumDepth += pDepthPels[ uiDepthStride * (uiHeight - 1) ];
854  iSumDepth += pDepthPels[ uiDepthStride * (uiHeight - 1) + uiWidth - 1 ];
855
856  uiMinDepth = pDepthPels[ 0 ];
857  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiWidth - 1 ]);
858  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) ]);
859  uiMinDepth = std::min( uiMinDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) + uiWidth - 1 ]);
860
861  uiMaxDepth = pDepthPels[ 0 ];
862  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiWidth - 1 ]);
863  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) ]);
864  uiMaxDepth = std::max( uiMaxDepth, (Int)pDepthPels[ uiDepthStride * (uiHeight - 1) + uiWidth - 1 ]);
865#endif
866 
867  // don't generate mask for blocks with small depth range (encoder decision)
868  if( uiMaxDepth - uiMinDepth < 10 )
869  {
870    return false;
871  }
872 
873  AOF(uiWidth==uiHeight);
874  Int iMean = iSumDepth >> 2;
875 
876  // start again for segmentation
877  pDepthPels = pDepthBlockStart;
878 
879  Bool bInvertMask = pDepthPels[0]>iMean; // top-left segment needs to be mapped to partIdx 0
880 
881  // generate mask
882  UInt uiSumPix[2] = {0,0};
883  for (Int y=0; y<uiHeight; y++)
884  {
885    for (Int x=0; x<uiHeight; x++)
886    {
887#if HS_DBBP_CLEAN_K0048
888      Int depthPel = 0;
889      if (iBlkX+x>iPictureWidth)
890      {
891        depthPel = pDepthPels[t];
892      }
893      else
894      {
895        depthPel = pDepthPels[x];
896         t=x;
897      }
898#else
899      Int depthPel = pDepthPels[x];
900#endif
901     
902      // decide which segment this pixel belongs to
903      Int ucSegment = (Int)(depthPel>iMean);
904     
905      if( bInvertMask )
906      {
907        ucSegment = 1-ucSegment;
908      }
909     
910      // count pixels for each segment
911      uiSumPix[ucSegment]++;
912     
913      // set mask value
914      pMask[x] = (Bool)ucSegment;
915    }
916   
917    // next row
918#if HS_DBBP_CLEAN_K0048
919    if (!(iBlkY+y+1>iPictureHeight))
920#endif
921    pDepthPels += uiDepthStride;
922    pMask += MAX_CU_SIZE;
923  }
924 
925  // don't generate valid mask for tiny segments (encoder decision)
926  // each segment needs to cover at least 1/8th of block
927  UInt uiMinPixPerSegment = (uiWidth*uiHeight) >> 3;
928  if( !( uiSumPix[0] > uiMinPixPerSegment && uiSumPix[1] > uiMinPixPerSegment ) )
929  {
930    return false;
931  }
932 
933  // all good
934  return true;
935}
936
937Void TComPrediction::combineSegmentsWithMask( TComYuv* pInYuv[2], TComYuv* pOutYuv, Bool* pMask, UInt uiWidth, UInt uiHeight, UInt uiPartAddr, UInt partSize )
938{
939  Pel*  piSrc[2]    = {pInYuv[0]->getLumaAddr(uiPartAddr), pInYuv[1]->getLumaAddr(uiPartAddr)};
940  UInt  uiSrcStride = pInYuv[0]->getStride();
941  Pel*  piDst       = pOutYuv->getLumaAddr(uiPartAddr);
942  UInt  uiDstStride = pOutYuv->getStride();
943 
944  UInt  uiMaskStride= MAX_CU_SIZE;
945  Pel* tmpTar = 0;
946  tmpTar = (Pel *)xMalloc(Pel, uiWidth*uiHeight);
947 
948  // backup pointer
949  Bool* pMaskStart = pMask;
950 
951  // combine luma first
952  for (Int y=0; y<uiHeight; y++)
953  {
954    for (Int x=0; x<uiWidth; x++)
955    {
956      UChar ucSegment = (UChar)pMask[x];
957      AOF( ucSegment < 2 );
958     
959      // filtering
960      tmpTar[y*uiWidth+x] = piSrc[ucSegment][x];
961    }
962   
963    piSrc[0]  += uiSrcStride;
964    piSrc[1]  += uiSrcStride;
965    pMask     += uiMaskStride;
966  }
967 
968  if (partSize == SIZE_Nx2N)
969  {
970    for (Int y=0; y<uiHeight; y++)
971    {
972      for (Int x=0; x<uiWidth; x++)
973      {
974        Bool l = (x==0)?pMaskStart[y*uiMaskStride+x]:pMaskStart[y*uiMaskStride+x-1];
975        Bool r = (x==uiWidth-1)?pMaskStart[y*uiMaskStride+x]:pMaskStart[y*uiMaskStride+x+1];
976       
977        Pel left, right;
978        left   = (x==0)          ? tmpTar[y*uiWidth+x] : tmpTar[y*uiWidth+x-1];
979        right  = (x==uiWidth-1)  ? tmpTar[y*uiWidth+x] : tmpTar[y*uiWidth+x+1];
980       
981        piDst[x] = (l!=r) ? ClipY( Pel(( left + (tmpTar[y*uiWidth+x] << 1) + right ) >> 2 )) : tmpTar[y*uiWidth+x]; 
982      }
983      piDst     += uiDstStride;
984    }
985  }
986  else // SIZE_2NxN
987  {
988    for (Int y=0; y<uiHeight; y++)
989    {
990      for (Int x=0; x<uiWidth; x++)
991      {
992        Bool t = (y==0)?pMaskStart[y*uiMaskStride+x]:pMaskStart[(y-1)*uiMaskStride+x];
993        Bool b = (y==uiHeight-1)?pMaskStart[y*uiMaskStride+x]:pMaskStart[(y+1)*uiMaskStride+x];
994       
995        Pel top, bottom;
996        top    = (y==0)          ? tmpTar[y*uiWidth+x] : tmpTar[(y-1)*uiWidth+x];
997        bottom = (y==uiHeight-1) ? tmpTar[y*uiWidth+x] : tmpTar[(y+1)*uiWidth+x];
998       
999        piDst[x] = (t!=b) ? ClipY( Pel(( top + (tmpTar[y*uiWidth+x] << 1) + bottom ) >> 2 )) : tmpTar[y*uiWidth+x];
1000      }
1001      piDst     += uiDstStride;
1002    }
1003  }
1004
1005  if ( tmpTar    ) { xFree(tmpTar);             tmpTar        = NULL; }
1006 
1007  // now combine chroma
1008  Pel*  piSrcU[2]       = { pInYuv[0]->getCbAddr(uiPartAddr), pInYuv[1]->getCbAddr(uiPartAddr) };
1009  Pel*  piSrcV[2]       = { pInYuv[0]->getCrAddr(uiPartAddr), pInYuv[1]->getCrAddr(uiPartAddr) };
1010  UInt  uiSrcStrideC    = pInYuv[0]->getCStride();
1011  Pel*  piDstU          = pOutYuv->getCbAddr(uiPartAddr);
1012  Pel*  piDstV          = pOutYuv->getCrAddr(uiPartAddr);
1013  UInt  uiDstStrideC    = pOutYuv->getCStride();
1014  UInt  uiWidthC        = uiWidth >> 1;
1015  UInt  uiHeightC       = uiHeight >> 1;
1016  Pel  filSrcU = 0, filSrcV = 0;
1017  Pel* tmpTarU = 0, *tmpTarV = 0;
1018  tmpTarU = (Pel *)xMalloc(Pel, uiWidthC*uiHeightC);
1019  tmpTarV = (Pel *)xMalloc(Pel, uiWidthC*uiHeightC);
1020  pMask = pMaskStart;
1021 
1022  for (Int y=0; y<uiHeightC; y++)
1023  {
1024    for (Int x=0; x<uiWidthC; x++)
1025    {
1026      UChar ucSegment = (UChar)pMask[x*2];
1027      AOF( ucSegment < 2 );
1028     
1029      // filtering
1030      tmpTarU[y*uiWidthC+x] = piSrcU[ucSegment][x];
1031      tmpTarV[y*uiWidthC+x] = piSrcV[ucSegment][x];
1032    }
1033   
1034    piSrcU[0]   += uiSrcStrideC;
1035    piSrcU[1]   += uiSrcStrideC;
1036    piSrcV[0]   += uiSrcStrideC;
1037    piSrcV[1]   += uiSrcStrideC;
1038    pMask       += 2*uiMaskStride;
1039  }
1040
1041  if (partSize == SIZE_Nx2N)
1042  {
1043    for (Int y=0; y<uiHeightC; y++)
1044    {
1045      for (Int x=0; x<uiWidthC; x++)
1046      {
1047        Bool l = (x==0)?pMaskStart[y*2*uiMaskStride+x*2]:pMaskStart[y*2*uiMaskStride+(x-1)*2];
1048        Bool r = (x==uiWidthC-1)?pMaskStart[y*2*uiMaskStride+x*2]:pMaskStart[y*2*uiMaskStride+(x+1)*2];
1049
1050        Pel leftU, rightU;
1051        leftU   = (x==0)           ? tmpTarU[y*uiWidthC+x] : tmpTarU[y*uiWidthC+x-1];
1052        rightU  = (x==uiWidthC-1)  ? tmpTarU[y*uiWidthC+x] : tmpTarU[y*uiWidthC+x+1];
1053        Pel leftV, rightV;
1054        leftV   = (x==0)           ? tmpTarV[y*uiWidthC+x] : tmpTarV[y*uiWidthC+x-1];
1055        rightV  = (x==uiWidthC-1)  ? tmpTarV[y*uiWidthC+x] : tmpTarV[y*uiWidthC+x+1];
1056
1057        if (l!=r)
1058        {
1059          filSrcU = ClipC( Pel(( leftU + (tmpTarU[y*uiWidthC+x] << 1) + rightU ) >> 2 ));
1060          filSrcV = ClipC( Pel(( leftV + (tmpTarV[y*uiWidthC+x] << 1) + rightV ) >> 2 ));
1061        }
1062        else
1063        {
1064          filSrcU = tmpTarU[y*uiWidthC+x];
1065          filSrcV = tmpTarV[y*uiWidthC+x];
1066        }
1067        piDstU[x] = filSrcU;
1068        piDstV[x] = filSrcV;
1069      }
1070      piDstU      += uiDstStrideC;
1071      piDstV      += uiDstStrideC;
1072    }
1073  }
1074  else
1075  {
1076    for (Int y=0; y<uiHeightC; y++)
1077    {
1078      for (Int x=0; x<uiWidthC; x++)
1079      {
1080        Bool t = (y==0)?pMaskStart[y*2*uiMaskStride+x*2]:pMaskStart[(y-1)*2*uiMaskStride+x*2];
1081        Bool b = (y==uiHeightC-1)?pMaskStart[y*2*uiMaskStride+x*2]:pMaskStart[(y+1)*2*uiMaskStride+x*2];
1082
1083        Pel topU, bottomU;
1084        topU    = (y==0)           ? tmpTarU[y*uiWidthC+x] : tmpTarU[(y-1)*uiWidthC+x];
1085        bottomU = (y==uiHeightC-1) ? tmpTarU[y*uiWidthC+x] : tmpTarU[(y+1)*uiWidthC+x];
1086        Pel topV, bottomV;
1087        topV    = (y==0)           ? tmpTarV[y*uiWidthC+x] : tmpTarV[(y-1)*uiWidthC+x];
1088        bottomV = (y==uiHeightC-1) ? tmpTarV[y*uiWidthC+x] : tmpTarV[(y+1)*uiWidthC+x];
1089
1090        if (t!=b)
1091        {
1092          filSrcU = ClipC( Pel(( topU + (tmpTarU[y*uiWidthC+x] << 1) + bottomU ) >> 2 ));
1093          filSrcV = ClipC( Pel(( topV + (tmpTarV[y*uiWidthC+x] << 1) + bottomV ) >> 2 ));
1094        }
1095        else
1096        {
1097          filSrcU = tmpTarU[y*uiWidthC+x];
1098          filSrcV = tmpTarV[y*uiWidthC+x];
1099        }
1100        piDstU[x] = filSrcU;
1101        piDstV[x] = filSrcV;
1102      }
1103      piDstU      += uiDstStrideC;
1104      piDstV      += uiDstStrideC;
1105    }
1106  }
1107
1108  if ( tmpTarU    ) { xFree(tmpTarU);             tmpTarU        = NULL; }
1109  if ( tmpTarV    ) { xFree(tmpTarV);             tmpTarV        = NULL; }
1110}
1111#endif
1112
1113Void TComPrediction::motionCompensation ( TComDataCU* pcCU, TComYuv* pcYuvPred, RefPicList eRefPicList, Int iPartIdx )
1114{
1115  Int         iWidth;
1116  Int         iHeight;
1117  UInt        uiPartAddr;
1118
1119  if ( iPartIdx >= 0 )
1120  {
1121    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
1122#if H_3D_VSP
1123    if ( pcCU->getVSPFlag(uiPartAddr) == 0)
1124    {
1125#endif
1126      if ( eRefPicList != REF_PIC_LIST_X )
1127      {
1128        if( pcCU->getSlice()->getPPS()->getUseWP())
1129        {
1130          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
1131        }
1132        else
1133        {
1134          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
1135        }
1136        if ( pcCU->getSlice()->getPPS()->getUseWP() )
1137        {
1138          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
1139        }
1140      }
1141      else
1142      {
1143#if H_3D_SPIVMP
1144        if ( pcCU->getSPIVMPFlag(uiPartAddr)!=0) 
1145        {
1146          Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
1147
1148          pcCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
1149
1150          UInt uiW[256], uiH[256];
1151          UInt uiSPAddr[256];
1152
1153          xGetSubPUAddrAndMerge(pcCU, uiPartAddr, iSPWidth, iSPHeight, iNumSPInOneLine, iNumSP, uiW, uiH, uiSPAddr);
1154
1155          //MC
1156          for (Int i = 0; i < iNumSP; i++)
1157          {
1158            if (uiW[i]==0 || uiH[i]==0)
1159            {
1160              continue;
1161            }
1162            if( xCheckIdenticalMotion( pcCU, uiSPAddr[i] ))
1163            {
1164              xPredInterUni (pcCU, uiSPAddr[i], uiW[i], uiH[i], REF_PIC_LIST_0, pcYuvPred );
1165            }
1166            else
1167            {
1168              xPredInterBi  (pcCU, uiSPAddr[i], uiW[i], uiH[i], pcYuvPred);
1169            }
1170          }
1171        }
1172        else
1173        {
1174#endif
1175          if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
1176          {
1177            xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
1178          }
1179          else
1180          {
1181            xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
1182          }
1183#if H_3D_SPIVMP
1184        }
1185#endif
1186      }
1187#if H_3D_VSP
1188    }
1189    else
1190    {
1191      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
1192      {
1193        xPredInterUniVSP( pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
1194      }
1195      else
1196      {
1197        xPredInterBiVSP ( pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
1198      }
1199    }
1200#endif
1201    return;
1202  }
1203
1204  for ( iPartIdx = 0; iPartIdx < pcCU->getNumPartitions(); iPartIdx++ )
1205  {
1206    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
1207
1208#if H_3D_VSP
1209    if ( pcCU->getVSPFlag(uiPartAddr) == 0 )
1210    {
1211#endif
1212      if ( eRefPicList != REF_PIC_LIST_X )
1213      {
1214        if( pcCU->getSlice()->getPPS()->getUseWP())
1215        {
1216          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
1217        }
1218        else
1219        {
1220          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
1221        }
1222        if ( pcCU->getSlice()->getPPS()->getUseWP() )
1223        {
1224          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
1225        }
1226      }
1227      else
1228      {
1229#if H_3D_SPIVMP
1230       if (pcCU->getSPIVMPFlag(uiPartAddr)!=0) 
1231      {
1232        Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
1233
1234        pcCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
1235
1236        UInt uiW[256], uiH[256];
1237        UInt uiSPAddr[256];
1238
1239        xGetSubPUAddrAndMerge(pcCU, uiPartAddr, iSPWidth, iSPHeight, iNumSPInOneLine, iNumSP, uiW, uiH, uiSPAddr);
1240        //MC
1241        for (Int i = 0; i < iNumSP; i++)
1242        {
1243          if (uiW[i]==0 || uiH[i]==0)
1244          {
1245            continue;
1246          }
1247          if( xCheckIdenticalMotion( pcCU, uiSPAddr[i] ))
1248          {
1249            xPredInterUni (pcCU, uiSPAddr[i], uiW[i], uiH[i], REF_PIC_LIST_0, pcYuvPred );
1250          }
1251          else
1252          {
1253            xPredInterBi  (pcCU, uiSPAddr[i], uiW[i], uiH[i], pcYuvPred);
1254          }
1255        }
1256      }
1257      else
1258      {
1259#endif
1260        if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
1261        {
1262          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
1263        }
1264        else
1265        {
1266          xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
1267        }
1268#if H_3D_SPIVMP
1269       }
1270#endif
1271      }
1272#if H_3D_VSP
1273    }
1274    else
1275    {
1276      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
1277      {
1278        xPredInterUniVSP( pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
1279      }
1280      else
1281      {
1282        xPredInterBiVSP ( pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
1283      }
1284    }
1285#endif
1286  }
1287  return;
1288}
1289
1290Void TComPrediction::xPredInterUni ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
1291{
1292  Int         iRefIdx     = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           assert (iRefIdx >= 0);
1293  TComMv      cMv         = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
1294  pcCU->clipMv(cMv);
1295
1296#if H_3D_ARP
1297  if(pcCU->getARPW( uiPartAddr ) > 0  && pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPOC()== pcCU->getSlice()->getPOC())
1298  {
1299    xPredInterUniARPviewRef( pcCU , uiPartAddr , iWidth , iHeight , eRefPicList , rpcYuvPred , bi );
1300  }
1301  else
1302  {
1303    if(  pcCU->getARPW( uiPartAddr ) > 0 
1304      && pcCU->getPartitionSize(uiPartAddr)==SIZE_2Nx2N
1305      && pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPOC()!= pcCU->getSlice()->getPOC() 
1306      )
1307    {
1308      xPredInterUniARP( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, rpcYuvPred, bi );
1309    }
1310    else
1311    {
1312#endif
1313#if H_3D_IC
1314      Bool bICFlag = pcCU->getICFlag( uiPartAddr ) && ( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getViewIndex() != pcCU->getSlice()->getViewIndex() );
1315      xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
1316#if H_3D_ARP
1317        , false
1318#endif
1319        , bICFlag );
1320      bICFlag = bICFlag && (iWidth > 8);
1321      xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
1322#if H_3D_ARP
1323        , false
1324#endif
1325        , bICFlag );
1326#else
1327      xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
1328      xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
1329#endif
1330#if H_3D_ARP
1331    }
1332  }
1333#endif
1334}
1335
1336#if H_3D_VSP
1337Void TComPrediction::xPredInterUniVSP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
1338{
1339  Int vspSize = pcCU->getVSPFlag( uiPartAddr ) >> 1;
1340
1341  Int widthSubPU, heightSubPU;
1342  if (vspSize)
1343  {
1344    widthSubPU  = 8;
1345    heightSubPU = 4;
1346  }
1347  else
1348  {
1349    widthSubPU  = 4;
1350    heightSubPU = 8;
1351  }
1352  xPredInterUniSubPU( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, rpcYuvPred, bi, widthSubPU, heightSubPU );
1353}
1354
1355Void TComPrediction::xPredInterUniSubPU( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi, Int widthSubPU, Int heightSubPU )
1356{
1357  UInt numPartsInLine       = pcCU->getPic()->getNumPartInWidth();
1358  UInt horiNumPartsInSubPU  = widthSubPU >> 2;
1359  UInt vertNumPartsInSubPU  = (heightSubPU >> 2) * numPartsInLine;
1360
1361  UInt partAddrRasterLine = g_auiZscanToRaster[ uiPartAddr ];
1362
1363  for( Int posY=0; posY<iHeight; posY+=heightSubPU, partAddrRasterLine+=vertNumPartsInSubPU )
1364  {
1365    UInt partAddrRasterSubPU = partAddrRasterLine;
1366    for( Int posX=0; posX<iWidth; posX+=widthSubPU, partAddrRasterSubPU+=horiNumPartsInSubPU )
1367    {
1368      UInt    partAddrSubPU = g_auiRasterToZscan[ partAddrRasterSubPU ];
1369      Int     refIdx        = pcCU->getCUMvField( eRefPicList )->getRefIdx( partAddrSubPU );           assert (refIdx >= 0);
1370      TComMv  cMv           = pcCU->getCUMvField( eRefPicList )->getMv( partAddrSubPU );
1371      pcCU->clipMv(cMv);
1372
1373      xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, refIdx )->getPicYuvRec(), partAddrSubPU, &cMv, widthSubPU, heightSubPU, rpcYuvPred, bi );
1374      xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, refIdx )->getPicYuvRec(), partAddrSubPU, &cMv, widthSubPU, heightSubPU, rpcYuvPred, bi );
1375
1376    }
1377  }
1378}
1379
1380#endif
1381
1382#if H_3D_ARP
1383Void TComPrediction::xPredInterUniARP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi, TComMvField * pNewMvFiled )
1384{
1385  Int         iRefIdx      = pNewMvFiled ? pNewMvFiled->getRefIdx() : pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           
1386  TComMv      cMv          = pNewMvFiled ? pNewMvFiled->getMv()     : pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
1387  Bool        bTobeScaled  = false;
1388  TComPic* pcPicYuvBaseCol = NULL;
1389  TComPic* pcPicYuvBaseRef = NULL;
1390
1391#if H_3D_NBDV
1392  DisInfo cDistparity;
1393  cDistparity.bDV           = pcCU->getDvInfo(uiPartAddr).bDV;
1394  if( cDistparity.bDV )
1395  {
1396    cDistparity.m_acNBDV = pcCU->getDvInfo(0).m_acNBDV;
1397    assert(pcCU->getDvInfo(uiPartAddr).bDV ==  pcCU->getDvInfo(0).bDV);
1398    cDistparity.m_aVIdxCan = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan;
1399  }
1400#else
1401  assert(0); // ARP can be applied only when a DV is available
1402#endif
1403
1404  UChar dW = cDistparity.bDV ? pcCU->getARPW ( uiPartAddr ) : 0;
1405
1406  if( cDistparity.bDV ) 
1407  {
1408    Int arpRefIdx = pcCU->getSlice()->getFirstTRefIdx(eRefPicList);
1409    if( dW > 0 && pcCU->getSlice()->getRefPic( eRefPicList, arpRefIdx )->getPOC()!= pcCU->getSlice()->getPOC() )
1410    {
1411      bTobeScaled = true;
1412    }
1413
1414    pcPicYuvBaseCol =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getPOC(),                              cDistparity.m_aVIdxCan );
1415
1416    pcPicYuvBaseRef =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getRefPic( eRefPicList, arpRefIdx )->getPOC(), cDistparity.m_aVIdxCan );
1417
1418    if (!pcCU->getSlice()->getArpRefPicAvailable( eRefPicList, cDistparity.m_aVIdxCan))
1419    {
1420      dW = 0;
1421      bTobeScaled = false;
1422    }
1423    else
1424    {
1425      assert( pcPicYuvBaseCol->getPOC() == pcCU->getSlice()->getPOC() && pcPicYuvBaseRef->getPOC() == pcCU->getSlice()->getRefPic( eRefPicList, arpRefIdx )->getPOC() );
1426    }
1427
1428    if(bTobeScaled)
1429    {     
1430      Int iCurrPOC    = pcCU->getSlice()->getPOC();
1431      Int iColRefPOC  = pcCU->getSlice()->getRefPOC( eRefPicList, iRefIdx );
1432      Int iCurrRefPOC = pcCU->getSlice()->getRefPOC( eRefPicList,  0);
1433      Int iScale = pcCU-> xGetDistScaleFactor(iCurrPOC, iCurrRefPOC, iCurrPOC, iColRefPOC);
1434      if ( iScale != 4096 )
1435      {
1436        cMv = cMv.scaleMv( iScale );
1437      }
1438      iRefIdx = 0;
1439    }
1440  }
1441
1442  pcCU->clipMv(cMv);
1443  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec();
1444  xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi || ( dW > 0 ), true );
1445  xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi || ( dW > 0 ), true );
1446
1447  if( dW > 0 )
1448  {
1449    TComYuv * pYuvB0 = &m_acYuvPredBase[0];
1450    TComYuv * pYuvB1  = &m_acYuvPredBase[1];
1451
1452    TComMv cMVwithDisparity = cMv + cDistparity.m_acNBDV;
1453    pcCU->clipMv(cMVwithDisparity);
1454    if (iWidth <= 8)
1455    {
1456      pYuvB0->clear(); pYuvB1->clear();
1457    }
1458
1459    assert ( cDistparity.bDV );
1460   
1461    TComMv cNBDV = cDistparity.m_acNBDV;
1462    pcCU->clipMv( cNBDV );
1463   
1464    pcPicYuvRef = pcPicYuvBaseCol->getPicYuvRec();
1465    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cNBDV, iWidth, iHeight, pYuvB0, true, true );
1466    if (iWidth > 8)
1467      xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cNBDV, iWidth, iHeight, pYuvB0, true, true );
1468   
1469    pcPicYuvRef = pcPicYuvBaseRef->getPicYuvRec();
1470    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, true, true );
1471 
1472    if (iWidth > 8)
1473      xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, true, true );
1474   
1475    pYuvB0->subtractARP( pYuvB0 , pYuvB1 , uiPartAddr , iWidth , iHeight );
1476
1477    if( 2 == dW )
1478    {
1479      pYuvB0->multiplyARP( uiPartAddr , iWidth , iHeight , dW );
1480    }
1481    rpcYuvPred->addARP( rpcYuvPred , pYuvB0 , uiPartAddr , iWidth , iHeight , !bi );
1482  }
1483}
1484
1485Bool TComPrediction::xCheckBiInterviewARP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eBaseRefPicList, TComPic*& pcPicYuvCurrTRef, TComMv& cBaseTMV, Int& iCurrTRefPoc )
1486{
1487  Int         iRefIdx       = pcCU->getCUMvField( eBaseRefPicList )->getRefIdx( uiPartAddr );
1488  TComMv      cDMv          = pcCU->getCUMvField( eBaseRefPicList )->getMv( uiPartAddr );
1489  TComPic* pcPicYuvBaseCol  = pcCU->getSlice()->getRefPic( eBaseRefPicList, iRefIdx ); 
1490  TComPicYuv* pcYuvBaseCol  = pcPicYuvBaseCol->getPicYuvRec();
1491  Int uiLCUAddr,uiAbsPartAddr;
1492  Int irefPUX = pcCU->getCUPelX() + g_auiRasterToPelX[g_auiZscanToRaster[uiPartAddr]] + iWidth/2  + ((cDMv.getHor() + 2)>>2);
1493  Int irefPUY = pcCU->getCUPelY() + g_auiRasterToPelY[g_auiZscanToRaster[uiPartAddr]] + iHeight/2 + ((cDMv.getVer() + 2)>>2);
1494
1495  irefPUX = (Int)Clip3<Int>(0, pcCU->getSlice()->getSPS()-> getPicWidthInLumaSamples()-1, irefPUX);
1496  irefPUY = (Int)Clip3<Int>(0, pcCU->getSlice()->getSPS()->getPicHeightInLumaSamples()-1, irefPUY); 
1497  pcYuvBaseCol->getCUAddrAndPartIdx( irefPUX, irefPUY, uiLCUAddr, uiAbsPartAddr);
1498  TComDataCU *pColCU = pcPicYuvBaseCol->getCU( uiLCUAddr );
1499
1500  TComPic* pcPicYuvBaseTRef = NULL;
1501  pcPicYuvCurrTRef = NULL;
1502
1503  //If there is available motion in base reference list, use it
1504  if(!pColCU->isIntra(uiAbsPartAddr))
1505  {
1506    for(Int iList = 0; iList < (pColCU->getSlice()->isInterB() ? 2: 1); iList ++)
1507    {
1508      RefPicList eRefPicListCurr = RefPicList(iList);
1509      Int iRef = pColCU->getCUMvField(eRefPicListCurr)->getRefIdx(uiAbsPartAddr);
1510      if( iRef != -1)
1511      {
1512        pcPicYuvBaseTRef = pColCU->getSlice()->getRefPic(eRefPicListCurr, iRef); 
1513        Int  iCurrPOC    = pColCU->getSlice()->getPOC();
1514        Int  iCurrRefPOC = pcPicYuvBaseTRef->getPOC();
1515        Int  iCurrRef    = pcCU->getSlice()->getFirstTRefIdx(eRefPicListCurr);
1516
1517        if( iCurrRef >= 0 && iCurrPOC != iCurrRefPOC)
1518        {
1519          pcPicYuvCurrTRef =  pcCU->getSlice()->getRefPic(eRefPicListCurr,iCurrRef); 
1520          Int iTargetPOC = pcPicYuvCurrTRef->getPOC();
1521          pcPicYuvBaseTRef =  pcCU->getSlice()->getBaseViewRefPic(iTargetPOC,  pcPicYuvBaseCol->getViewIndex() ); 
1522          if(pcPicYuvBaseTRef)
1523          {
1524            cBaseTMV = pColCU->getCUMvField(eRefPicListCurr)->getMv(uiAbsPartAddr);
1525            Int iScale = pcCU-> xGetDistScaleFactor(iCurrPOC, iTargetPOC, iCurrPOC, iCurrRefPOC);
1526            if ( iScale != 4096 )
1527            {
1528              cBaseTMV = cBaseTMV.scaleMv( iScale );
1529            }
1530            iCurrTRefPoc = iTargetPOC;
1531            return true;
1532          }
1533        }
1534      }
1535    }
1536  }
1537
1538  //If there is no available motion in base reference list, use ( 0, 0 )
1539  if( pcCU->getSlice()->getFirstTRefIdx( eBaseRefPicList ) >= 0 )
1540  {
1541    cBaseTMV.set( 0, 0 );
1542    pcPicYuvCurrTRef = pcCU->getSlice()->getRefPic( eBaseRefPicList,  pcCU->getSlice()->getFirstTRefIdx( eBaseRefPicList ) );
1543    iCurrTRefPoc = pcPicYuvCurrTRef->getPOC();
1544    return true;
1545  }
1546
1547  return false;
1548}
1549
1550Void TComPrediction::xPredInterUniARPviewRef( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi, TComMvField * pNewMvFiled )
1551{
1552  Int         iRefIdx       = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           
1553  TComMv      cDMv          = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
1554  TComMv      cTempDMv      = cDMv;
1555  UChar       dW            = pcCU->getARPW ( uiPartAddr );
1556
1557  TComPic* pcPicYuvBaseTRef = NULL;
1558  TComPic* pcPicYuvCurrTRef = NULL;
1559  TComPic* pcPicYuvBaseCol  = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx ); 
1560  TComPicYuv* pcYuvBaseCol  = pcPicYuvBaseCol->getPicYuvRec();   
1561  Bool bTMVAvai = false;     
1562  TComMv cBaseTMV;
1563  if( pNewMvFiled )
1564  {
1565    iRefIdx = pNewMvFiled->getRefIdx(); 
1566    cDMv = pNewMvFiled->getMv();
1567  }
1568  pcCU->clipMv(cTempDMv);
1569
1570  assert(dW > 0);
1571  if (!pcCU->getSlice()->getArpRefPicAvailable( eRefPicList, pcPicYuvBaseCol->getViewIndex()))
1572  {
1573    dW = 0;
1574  }
1575  Int uiLCUAddr,uiAbsPartAddr;
1576  Int irefPUX = pcCU->getCUPelX() + g_auiRasterToPelX[g_auiZscanToRaster[uiPartAddr]] + iWidth/2  + ((cDMv.getHor() + 2)>>2);
1577  Int irefPUY = pcCU->getCUPelY() + g_auiRasterToPelY[g_auiZscanToRaster[uiPartAddr]] + iHeight/2 + ((cDMv.getVer() + 2)>>2);
1578
1579  irefPUX = (Int)Clip3<Int>(0, pcCU->getSlice()->getSPS()-> getPicWidthInLumaSamples()-1, irefPUX);
1580  irefPUY = (Int)Clip3<Int>(0, pcCU->getSlice()->getSPS()->getPicHeightInLumaSamples()-1, irefPUY); 
1581  pcYuvBaseCol->getCUAddrAndPartIdx( irefPUX, irefPUY, uiLCUAddr, uiAbsPartAddr);
1582  TComDataCU *pColCU = pcPicYuvBaseCol->getCU( uiLCUAddr );
1583  if( pcCU->getSlice()->isInterB() && !pcCU->getSlice()->getIsDepth() )
1584  {
1585    RefPicList eOtherRefList = ( eRefPicList == REF_PIC_LIST_0 ) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1586    Int iOtherRefIdx = pcCU->getCUMvField( eOtherRefList )->getRefIdx( uiPartAddr );
1587    //The other prediction direction is temporal ARP
1588    if( iOtherRefIdx >= 0 && pcCU->getSlice()->getViewIndex() == pcCU->getSlice()->getRefPic( eOtherRefList, iOtherRefIdx )->getViewIndex() )
1589    {
1590      bTMVAvai = true;
1591      pcPicYuvBaseTRef = pcCU->getSlice()->getRefPic( eOtherRefList, iOtherRefIdx );
1592      Int  iCurrPOC    = pcCU->getSlice()->getPOC();
1593      Int  iCurrRefPOC = pcPicYuvBaseTRef->getPOC();
1594      Int  iCurrRef    = pcCU->getSlice()->getFirstTRefIdx( eOtherRefList );
1595     
1596      if( iCurrRef >= 0 )
1597      {
1598        pcPicYuvCurrTRef =  pcCU->getSlice()->getRefPic( eOtherRefList,iCurrRef ); 
1599        Int iTargetPOC = pcPicYuvCurrTRef->getPOC();
1600        pcPicYuvBaseTRef =  pcCU->getSlice()->getBaseViewRefPic( iTargetPOC,  pcPicYuvBaseCol->getViewIndex() );
1601        if( pcPicYuvBaseTRef )
1602        {
1603          cBaseTMV = pcCU->getCUMvField( eOtherRefList )->getMv( uiPartAddr );
1604          Int iScale = pcCU-> xGetDistScaleFactor( iCurrPOC, iTargetPOC, iCurrPOC, iCurrRefPOC );
1605          if ( iScale != 4096 )
1606          {
1607            cBaseTMV = cBaseTMV.scaleMv( iScale );
1608          }
1609        }
1610        else
1611        {
1612          dW = 0;
1613        }
1614      }
1615      else
1616      {
1617        dW = 0;
1618      }
1619    }
1620
1621    //Both prediction directions are inter-view ARP
1622    if ( iOtherRefIdx >= 0 && !bTMVAvai )
1623    {
1624      RefPicList eBaseList = REF_PIC_LIST_0;
1625      Int iCurrTRefPoc;
1626      bTMVAvai = ( eBaseList != eRefPicList ) && ( pcCU->getSlice()->getViewIndex() != pcCU->getSlice()->getRefPic( eOtherRefList, iOtherRefIdx )->getViewIndex() );
1627
1628      if ( bTMVAvai )
1629      {
1630        if( xCheckBiInterviewARP( pcCU, uiPartAddr, iWidth, iHeight, eBaseList, pcPicYuvCurrTRef, cBaseTMV, iCurrTRefPoc ) )
1631        {
1632          pcPicYuvBaseTRef = pcCU->getSlice()->getBaseViewRefPic( iCurrTRefPoc,  pcPicYuvBaseCol->getViewIndex() );
1633          if ( pcPicYuvBaseTRef == NULL )
1634          {
1635            dW = 0;
1636          }
1637        }
1638        else
1639        {
1640          dW = 0;
1641        }
1642      }
1643    }
1644  }
1645
1646  if( !pColCU->isIntra( uiAbsPartAddr ) && !bTMVAvai )
1647  {
1648    TComMvField puMVField;
1649    for(Int iList = 0; iList < (pColCU->getSlice()->isInterB() ? 2: 1) && !bTMVAvai; iList ++)
1650    {
1651      RefPicList eRefPicListCurr = RefPicList(iList);
1652      Int iRef = pColCU->getCUMvField(eRefPicListCurr)->getRefIdx(uiAbsPartAddr);
1653      if( iRef != -1)
1654      {
1655        pcPicYuvBaseTRef = pColCU->getSlice()->getRefPic(eRefPicListCurr, iRef); 
1656        Int  iCurrPOC    = pColCU->getSlice()->getPOC();
1657        Int  iCurrRefPOC = pcPicYuvBaseTRef->getPOC();
1658        Int  iCurrRef    = pcCU->getSlice()->getFirstTRefIdx(eRefPicListCurr);
1659        if (iCurrRef >= 0 && iCurrRefPOC != iCurrPOC)
1660        {
1661          pcPicYuvCurrTRef =  pcCU->getSlice()->getRefPic(eRefPicListCurr,iCurrRef); 
1662          Int iTargetPOC = pcPicYuvCurrTRef->getPOC();
1663          {
1664            pcPicYuvBaseTRef =  pcCU->getSlice()->getBaseViewRefPic(iTargetPOC,  pcPicYuvBaseCol->getViewIndex() ); 
1665            if(pcPicYuvBaseTRef)
1666            {
1667              cBaseTMV = pColCU->getCUMvField(eRefPicListCurr)->getMv(uiAbsPartAddr);
1668              Int iScale = pcCU-> xGetDistScaleFactor(iCurrPOC, iTargetPOC, iCurrPOC, iCurrRefPOC);
1669              if ( iScale != 4096 )
1670                cBaseTMV = cBaseTMV.scaleMv( iScale );                 
1671              bTMVAvai = true;
1672              break;
1673            }
1674          }
1675        }
1676      }
1677    }
1678  }
1679  if (bTMVAvai == false)
1680  { 
1681    bTMVAvai = true;
1682    cBaseTMV.set(0, 0);
1683    pcPicYuvBaseTRef =  pColCU->getSlice()->getRefPic(eRefPicList,  pcCU->getSlice()->getFirstTRefIdx(eRefPicList)); 
1684    pcPicYuvCurrTRef =  pcCU->getSlice()->getRefPic  (eRefPicList,  pcCU->getSlice()->getFirstTRefIdx(eRefPicList));     
1685  }
1686
1687  xPredInterLumaBlk  ( pcCU, pcYuvBaseCol, uiPartAddr, &cTempDMv, iWidth, iHeight, rpcYuvPred, bi || ( dW > 0 && bTMVAvai ),        bTMVAvai);
1688  xPredInterChromaBlk( pcCU, pcYuvBaseCol, uiPartAddr, &cTempDMv, iWidth, iHeight, rpcYuvPred, bi || ( dW > 0 && bTMVAvai ),        bTMVAvai);
1689
1690  if( dW > 0 && bTMVAvai ) 
1691  {
1692    TComYuv*    pYuvCurrTRef    = &m_acYuvPredBase[0];
1693    TComYuv*    pYuvBaseTRef    = &m_acYuvPredBase[1];
1694    TComPicYuv* pcYuvCurrTref   = pcPicYuvCurrTRef->getPicYuvRec();       
1695    TComPicYuv* pcYuvBaseTref   = pcPicYuvBaseTRef->getPicYuvRec(); 
1696    TComMv      cTempMv         = cDMv + cBaseTMV;
1697
1698    pcCU->clipMv(cBaseTMV);
1699    pcCU->clipMv(cTempMv);
1700
1701    if (iWidth <= 8)
1702    {
1703      pYuvCurrTRef->clear(); pYuvBaseTRef->clear();
1704    }
1705    xPredInterLumaBlk  ( pcCU, pcYuvCurrTref, uiPartAddr, &cBaseTMV, iWidth, iHeight, pYuvCurrTRef, true,   true);
1706
1707    if (iWidth > 8)
1708      xPredInterChromaBlk( pcCU, pcYuvCurrTref, uiPartAddr, &cBaseTMV, iWidth, iHeight, pYuvCurrTRef, true,   true);
1709
1710    xPredInterLumaBlk  ( pcCU, pcYuvBaseTref, uiPartAddr, &cTempMv,  iWidth, iHeight, pYuvBaseTRef, true,   true); 
1711
1712    if (iWidth > 8)
1713      xPredInterChromaBlk( pcCU, pcYuvBaseTref, uiPartAddr, &cTempMv,  iWidth, iHeight, pYuvBaseTRef, true,   true); 
1714
1715    pYuvCurrTRef->subtractARP( pYuvCurrTRef , pYuvBaseTRef , uiPartAddr , iWidth , iHeight ); 
1716    if(dW == 2)
1717    {
1718      pYuvCurrTRef->multiplyARP( uiPartAddr , iWidth , iHeight , dW );
1719    }
1720    rpcYuvPred->addARP( rpcYuvPred , pYuvCurrTRef , uiPartAddr , iWidth , iHeight , !bi ); 
1721  }
1722}
1723#endif
1724
1725Void TComPrediction::xPredInterBi ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
1726{
1727  TComYuv* pcMbYuv;
1728  Int      iRefIdx[2] = {-1, -1};
1729
1730  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
1731  {
1732    RefPicList eRefPicList = (iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
1733    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
1734
1735    if ( iRefIdx[iRefList] < 0 )
1736    {
1737      continue;
1738    }
1739
1740    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
1741
1742    pcMbYuv = &m_acYuvPred[iRefList];
1743    if( pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0 )
1744    {
1745      xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
1746    }
1747    else
1748    {
1749      if ( ( pcCU->getSlice()->getPPS()->getUseWP()       && pcCU->getSlice()->getSliceType() == P_SLICE ) || 
1750           ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE ) )
1751      {
1752        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
1753      }
1754      else
1755      {
1756        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv );
1757      }
1758    }
1759  }
1760
1761  if ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE  )
1762  {
1763    xWeightedPredictionBi( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
1764  } 
1765  else if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType() == P_SLICE )
1766  {
1767    xWeightedPredictionUni( pcCU, &m_acYuvPred[0], uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, rpcYuvPred ); 
1768  }
1769  else
1770  {
1771    xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
1772  }
1773}
1774
1775#if H_3D_VSP
1776
1777Void TComPrediction::xPredInterBiVSP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
1778{
1779  TComYuv* pcMbYuv;
1780  Int      iRefIdx[2] = {-1, -1};
1781  Bool     bi = (pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0);
1782
1783  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
1784  {
1785    RefPicList eRefPicList = RefPicList(iRefList);
1786    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
1787
1788    if ( iRefIdx[iRefList] < 0 )
1789    {
1790      continue;
1791    }
1792    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
1793
1794    pcMbYuv = &m_acYuvPred[iRefList];
1795    xPredInterUniVSP ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, bi );
1796  }
1797
1798  xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
1799}
1800
1801#endif
1802
1803/**
1804 * \brief Generate motion-compensated luma block
1805 *
1806 * \param cu       Pointer to current CU
1807 * \param refPic   Pointer to reference picture
1808 * \param partAddr Address of block within CU
1809 * \param mv       Motion vector
1810 * \param width    Width of block
1811 * \param height   Height of block
1812 * \param dstPic   Pointer to destination picture
1813 * \param bi       Flag indicating whether bipred is used
1814 */
1815Void TComPrediction::xPredInterLumaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
1816#if H_3D_ARP
1817    , Bool filterType
1818#endif
1819#if H_3D_IC
1820    , Bool bICFlag
1821#endif
1822  )
1823{
1824  Int refStride = refPic->getStride(); 
1825  Int refOffset = ( mv->getHor() >> 2 ) + ( mv->getVer() >> 2 ) * refStride;
1826  Pel *ref      = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1827 
1828  Int dstStride = dstPic->getStride();
1829  Pel *dst      = dstPic->getLumaAddr( partAddr );
1830 
1831  Int xFrac = mv->getHor() & 0x3;
1832  Int yFrac = mv->getVer() & 0x3;
1833
1834#if H_3D_IC
1835  if( cu->getSlice()->getIsDepth() )
1836  {
1837    refOffset = mv->getHor() + mv->getVer() * refStride;
1838    ref       = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1839    xFrac     = 0;
1840    yFrac     = 0;
1841  }
1842#endif
1843  if ( yFrac == 0 )
1844  {
1845#if H_3D_IC
1846    m_if.filterHorLuma( ref, refStride, dst, dstStride, width, height, xFrac,       !bi || bICFlag
1847#else
1848    m_if.filterHorLuma( ref, refStride, dst, dstStride, width, height, xFrac,       !bi
1849#endif
1850#if H_3D_ARP
1851    , filterType
1852#endif
1853      );
1854  }
1855  else if ( xFrac == 0 )
1856  {
1857#if H_3D_IC
1858    m_if.filterVerLuma( ref, refStride, dst, dstStride, width, height, yFrac, true, !bi || bICFlag
1859#else
1860    m_if.filterVerLuma( ref, refStride, dst, dstStride, width, height, yFrac, true, !bi
1861#endif
1862#if H_3D_ARP
1863    , filterType
1864#endif
1865      );
1866  }
1867  else
1868  {
1869    Int tmpStride = m_filteredBlockTmp[0].getStride();
1870    Short *tmp    = m_filteredBlockTmp[0].getLumaAddr();
1871
1872    Int filterSize = NTAPS_LUMA;
1873    Int halfFilterSize = ( filterSize >> 1 );
1874
1875    m_if.filterHorLuma(ref - (halfFilterSize-1)*refStride, refStride, tmp, tmpStride, width, height+filterSize-1, xFrac, false     
1876#if H_3D_ARP
1877    , filterType
1878#endif
1879      );
1880#if H_3D_IC
1881    m_if.filterVerLuma(tmp + (halfFilterSize-1)*tmpStride, tmpStride, dst, dstStride, width, height,              yFrac, false, !bi || bICFlag
1882#else
1883    m_if.filterVerLuma(tmp + (halfFilterSize-1)*tmpStride, tmpStride, dst, dstStride, width, height,              yFrac, false, !bi
1884#endif
1885#if H_3D_ARP
1886    , filterType
1887#endif
1888      );   
1889  }
1890
1891#if H_3D_IC
1892  if( bICFlag )
1893  {
1894    Int a, b, i, j;
1895    const Int iShift = IC_CONST_SHIFT;
1896
1897    xGetLLSICPrediction( cu, mv, refPic, a, b, TEXT_LUMA );
1898
1899
1900    for ( i = 0; i < height; i++ )
1901    {
1902      for ( j = 0; j < width; j++ )
1903      {
1904          dst[j] = Clip3( 0, ( 1 << g_bitDepthY ) - 1, ( ( a*dst[j] ) >> iShift ) + b );
1905      }
1906      dst += dstStride;
1907    }
1908
1909    if(bi)
1910    {
1911      Pel *dst2      = dstPic->getLumaAddr( partAddr );
1912      Int shift = IF_INTERNAL_PREC - g_bitDepthY;
1913      for (i = 0; i < height; i++)
1914      {
1915        for (j = 0; j < width; j++)
1916        {
1917          Short val = dst2[j] << shift;
1918          dst2[j] = val - (Short)IF_INTERNAL_OFFS;
1919        }
1920        dst2 += dstStride;
1921      }
1922    }
1923  }
1924#endif
1925}
1926
1927/**
1928 * \brief Generate motion-compensated chroma block
1929 *
1930 * \param cu       Pointer to current CU
1931 * \param refPic   Pointer to reference picture
1932 * \param partAddr Address of block within CU
1933 * \param mv       Motion vector
1934 * \param width    Width of block
1935 * \param height   Height of block
1936 * \param dstPic   Pointer to destination picture
1937 * \param bi       Flag indicating whether bipred is used
1938 */
1939Void TComPrediction::xPredInterChromaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
1940#if H_3D_ARP
1941    , Bool filterType
1942#endif
1943#if H_3D_IC
1944    , Bool bICFlag
1945#endif
1946  )
1947{
1948  Int     refStride  = refPic->getCStride();
1949  Int     dstStride  = dstPic->getCStride();
1950 
1951  Int     refOffset  = (mv->getHor() >> 3) + (mv->getVer() >> 3) * refStride;
1952 
1953  Pel*    refCb     = refPic->getCbAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1954  Pel*    refCr     = refPic->getCrAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
1955 
1956  Pel* dstCb = dstPic->getCbAddr( partAddr );
1957  Pel* dstCr = dstPic->getCrAddr( partAddr );
1958 
1959  Int     xFrac  = mv->getHor() & 0x7;
1960  Int     yFrac  = mv->getVer() & 0x7;
1961  UInt    cxWidth  = width  >> 1;
1962  UInt    cxHeight = height >> 1;
1963 
1964  Int     extStride = m_filteredBlockTmp[0].getStride();
1965  Short*  extY      = m_filteredBlockTmp[0].getLumaAddr();
1966 
1967  Int filterSize = NTAPS_CHROMA;
1968 
1969  Int halfFilterSize = (filterSize>>1);
1970 
1971  if ( yFrac == 0 )
1972  {
1973#if H_3D_IC
1974    m_if.filterHorChroma(refCb, refStride, dstCb,  dstStride, cxWidth, cxHeight, xFrac, !bi || bICFlag
1975#else
1976    m_if.filterHorChroma(refCb, refStride, dstCb,  dstStride, cxWidth, cxHeight, xFrac, !bi
1977#endif
1978#if H_3D_ARP
1979    , filterType
1980#endif
1981    );   
1982#if H_3D_IC
1983    m_if.filterHorChroma(refCr, refStride, dstCr,  dstStride, cxWidth, cxHeight, xFrac, !bi || bICFlag
1984#else
1985    m_if.filterHorChroma(refCr, refStride, dstCr,  dstStride, cxWidth, cxHeight, xFrac, !bi
1986#endif
1987#if H_3D_ARP
1988    , filterType
1989#endif
1990    );
1991  }
1992  else if ( xFrac == 0 )
1993  {
1994#if H_3D_IC
1995    m_if.filterVerChroma(refCb, refStride, dstCb, dstStride, cxWidth, cxHeight, yFrac, true, !bi || bICFlag
1996#else
1997    m_if.filterVerChroma(refCb, refStride, dstCb, dstStride, cxWidth, cxHeight, yFrac, true, !bi
1998#endif
1999#if H_3D_ARP
2000    , filterType
2001#endif
2002    );
2003#if H_3D_IC
2004    m_if.filterVerChroma(refCr, refStride, dstCr, dstStride, cxWidth, cxHeight, yFrac, true, !bi || bICFlag
2005#else
2006    m_if.filterVerChroma(refCr, refStride, dstCr, dstStride, cxWidth, cxHeight, yFrac, true, !bi
2007#endif
2008#if H_3D_ARP
2009    , filterType
2010#endif
2011    );
2012  }
2013  else
2014  {
2015    m_if.filterHorChroma(refCb - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
2016#if H_3D_ARP
2017    , filterType
2018#endif 
2019      );
2020#if H_3D_IC
2021    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCb, dstStride, cxWidth, cxHeight  , yFrac, false, !bi || bICFlag
2022#else
2023    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCb, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
2024#endif
2025#if H_3D_ARP
2026    , filterType
2027#endif
2028      );
2029   
2030    m_if.filterHorChroma(refCr - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
2031#if H_3D_ARP
2032    , filterType
2033#endif
2034      );
2035#if H_3D_IC
2036    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCr, dstStride, cxWidth, cxHeight  , yFrac, false, !bi || bICFlag
2037#else
2038    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCr, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
2039#endif
2040#if H_3D_ARP
2041    , filterType
2042#endif
2043      );   
2044  }
2045
2046#if H_3D_IC
2047  if( bICFlag )
2048  {
2049    Int a, b, i, j;
2050    const Int iShift = IC_CONST_SHIFT;
2051
2052    xGetLLSICPrediction( cu, mv, refPic, a, b, TEXT_CHROMA_U ); // Cb
2053    for ( i = 0; i < cxHeight; i++ )
2054    {
2055      for ( j = 0; j < cxWidth; j++ )
2056      {
2057          dstCb[j] = Clip3(  0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCb[j] ) >> iShift ) + b );
2058      }
2059      dstCb += dstStride;
2060    }
2061    xGetLLSICPrediction( cu, mv, refPic, a, b, TEXT_CHROMA_V ); // Cr
2062    for ( i = 0; i < cxHeight; i++ )
2063    {
2064      for ( j = 0; j < cxWidth; j++ )
2065      {
2066          dstCr[j] = Clip3( 0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCr[j] ) >> iShift ) + b );
2067      }
2068      dstCr += dstStride;
2069    }
2070
2071    if(bi)
2072    {
2073      Pel* dstCb2 = dstPic->getCbAddr( partAddr );
2074      Pel* dstCr2 = dstPic->getCrAddr( partAddr );
2075      Int shift = IF_INTERNAL_PREC - g_bitDepthC;
2076      for (i = 0; i < cxHeight; i++)
2077      {
2078        for (j = 0; j < cxWidth; j++)
2079        {
2080          Short val = dstCb2[j] << shift;
2081          dstCb2[j] = val - (Short)IF_INTERNAL_OFFS;
2082
2083          val = dstCr2[j] << shift;
2084          dstCr2[j] = val - (Short)IF_INTERNAL_OFFS;
2085        }
2086        dstCb2 += dstStride;
2087        dstCr2 += dstStride;
2088      }
2089    }
2090  }
2091#endif
2092}
2093
2094Void TComPrediction::xWeightedAverage( TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, Int iRefIdx0, Int iRefIdx1, UInt uiPartIdx, Int iWidth, Int iHeight, TComYuv*& rpcYuvDst )
2095{
2096  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
2097  {
2098    rpcYuvDst->addAvg( pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight );
2099  }
2100  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
2101  {
2102    pcYuvSrc0->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
2103  }
2104  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
2105  {
2106    pcYuvSrc1->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
2107  }
2108}
2109
2110// AMVP
2111Void TComPrediction::getMvPredAMVP( TComDataCU* pcCU, UInt uiPartIdx, UInt uiPartAddr, RefPicList eRefPicList, TComMv& rcMvPred )
2112{
2113  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
2114  if( pcAMVPInfo->iN <= 1 )
2115  {
2116    rcMvPred = pcAMVPInfo->m_acMvCand[0];
2117
2118    pcCU->setMVPIdxSubParts( 0, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
2119    pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
2120    return;
2121  }
2122
2123  assert(pcCU->getMVPIdx(eRefPicList,uiPartAddr) >= 0);
2124  rcMvPred = pcAMVPInfo->m_acMvCand[pcCU->getMVPIdx(eRefPicList,uiPartAddr)];
2125  return;
2126}
2127
2128/** Function for deriving planar intra prediction.
2129 * \param pSrc pointer to reconstructed sample array
2130 * \param srcStride the stride of the reconstructed sample array
2131 * \param rpDst reference to pointer for the prediction sample array
2132 * \param dstStride the stride of the prediction sample array
2133 * \param width the width of the block
2134 * \param height the height of the block
2135 *
2136 * This function derives the prediction samples for planar mode (intra coding).
2137 */
2138Void TComPrediction::xPredIntraPlanar( Int* pSrc, Int srcStride, Pel* rpDst, Int dstStride, UInt width, UInt height )
2139{
2140  assert(width == height);
2141
2142  Int k, l, bottomLeft, topRight;
2143  Int horPred;
2144  Int leftColumn[MAX_CU_SIZE+1], topRow[MAX_CU_SIZE+1], bottomRow[MAX_CU_SIZE], rightColumn[MAX_CU_SIZE];
2145  UInt blkSize = width;
2146  UInt offset2D = width;
2147  UInt shift1D = g_aucConvertToBit[ width ] + 2;
2148  UInt shift2D = shift1D + 1;
2149
2150  // Get left and above reference column and row
2151  for(k=0;k<blkSize+1;k++)
2152  {
2153    topRow[k] = pSrc[k-srcStride];
2154    leftColumn[k] = pSrc[k*srcStride-1];
2155  }
2156
2157  // Prepare intermediate variables used in interpolation
2158  bottomLeft = leftColumn[blkSize];
2159  topRight   = topRow[blkSize];
2160  for (k=0;k<blkSize;k++)
2161  {
2162    bottomRow[k]   = bottomLeft - topRow[k];
2163    rightColumn[k] = topRight   - leftColumn[k];
2164    topRow[k]      <<= shift1D;
2165    leftColumn[k]  <<= shift1D;
2166  }
2167
2168  // Generate prediction signal
2169  for (k=0;k<blkSize;k++)
2170  {
2171    horPred = leftColumn[k] + offset2D;
2172    for (l=0;l<blkSize;l++)
2173    {
2174      horPred += rightColumn[k];
2175      topRow[l] += bottomRow[l];
2176      rpDst[k*dstStride+l] = ( (horPred + topRow[l]) >> shift2D );
2177    }
2178  }
2179}
2180
2181/** Function for filtering intra DC predictor.
2182 * \param pSrc pointer to reconstructed sample array
2183 * \param iSrcStride the stride of the reconstructed sample array
2184 * \param rpDst reference to pointer for the prediction sample array
2185 * \param iDstStride the stride of the prediction sample array
2186 * \param iWidth the width of the block
2187 * \param iHeight the height of the block
2188 *
2189 * This function performs filtering left and top edges of the prediction samples for DC mode (intra coding).
2190 */
2191Void TComPrediction::xDCPredFiltering( Int* pSrc, Int iSrcStride, Pel*& rpDst, Int iDstStride, Int iWidth, Int iHeight )
2192{
2193  Pel* pDst = rpDst;
2194  Int x, y, iDstStride2, iSrcStride2;
2195
2196  // boundary pixels processing
2197  pDst[0] = (Pel)((pSrc[-iSrcStride] + pSrc[-1] + 2 * pDst[0] + 2) >> 2);
2198
2199  for ( x = 1; x < iWidth; x++ )
2200  {
2201    pDst[x] = (Pel)((pSrc[x - iSrcStride] +  3 * pDst[x] + 2) >> 2);
2202  }
2203
2204  for ( y = 1, iDstStride2 = iDstStride, iSrcStride2 = iSrcStride-1; y < iHeight; y++, iDstStride2+=iDstStride, iSrcStride2+=iSrcStride )
2205  {
2206    pDst[iDstStride2] = (Pel)((pSrc[iSrcStride2] + 3 * pDst[iDstStride2] + 2) >> 2);
2207  }
2208
2209  return;
2210}
2211#if H_3D_IC
2212/** Function for deriving the position of first non-zero binary bit of a value
2213 * \param x input value
2214 *
2215 * This function derives the position of first non-zero binary bit of a value
2216 */
2217Int GetMSB( UInt x )
2218{
2219  Int iMSB = 0, bits = ( sizeof( Int ) << 3 ), y = 1;
2220
2221  while( x > 1 )
2222  {
2223    bits >>= 1;
2224    y = x >> bits;
2225
2226    if( y )
2227    {
2228      x = y;
2229      iMSB += bits;
2230    }
2231  }
2232
2233  iMSB+=y;
2234
2235  return iMSB;
2236}
2237
2238
2239/** Function for deriving LM illumination compensation.
2240 */
2241Void TComPrediction::xGetLLSICPrediction( TComDataCU* pcCU, TComMv *pMv, TComPicYuv *pRefPic, Int &a, Int &b, TextType eType )
2242{
2243  TComPicYuv *pRecPic = pcCU->getPic()->getPicYuvRec();
2244  Pel *pRec = NULL, *pRef = NULL;
2245  UInt uiWidth, uiHeight, uiTmpPartIdx;
2246  Int iRecStride = ( eType == TEXT_LUMA ) ? pRecPic->getStride() : pRecPic->getCStride();
2247  Int iRefStride = ( eType == TEXT_LUMA ) ? pRefPic->getStride() : pRefPic->getCStride();
2248  Int iRefOffset, iHor, iVer;
2249  iHor = pcCU->getSlice()->getIsDepth() ? pMv->getHor() : ( ( pMv->getHor() + 2 ) >> 2 );
2250  iVer = pcCU->getSlice()->getIsDepth() ? pMv->getVer() : ( ( pMv->getVer() + 2 ) >> 2 );
2251  if( eType != TEXT_LUMA )
2252  {
2253    iHor = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getHor() + 1 ) >> 1 ) : ( ( pMv->getHor() + 4 ) >> 3 );
2254    iVer = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getVer() + 1 ) >> 1 ) : ( ( pMv->getVer() + 4 ) >> 3 );
2255  }
2256  uiWidth  = ( eType == TEXT_LUMA ) ? pcCU->getWidth( 0 )  : ( pcCU->getWidth( 0 )  >> 1 );
2257  uiHeight = ( eType == TEXT_LUMA ) ? pcCU->getHeight( 0 ) : ( pcCU->getHeight( 0 ) >> 1 );
2258
2259  Int i, j, iCountShift = 0;
2260
2261  // LLS parameters estimation -->
2262
2263  Int x = 0, y = 0, xx = 0, xy = 0;
2264  Int precShift = std::max(0, (( eType == TEXT_LUMA ) ? g_bitDepthY : g_bitDepthC) - 12);
2265
2266  if( pcCU->getPUAbove( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) )
2267  {
2268    iRefOffset = iHor + iVer * iRefStride - iRefStride;
2269    if( eType == TEXT_LUMA )
2270    {
2271      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2272      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
2273    }
2274    else if( eType == TEXT_CHROMA_U )
2275    {
2276      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2277      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
2278    }
2279    else
2280    {
2281      assert( eType == TEXT_CHROMA_V );
2282      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2283      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
2284    }
2285
2286    for( j = 0; j < uiWidth; j+=2 )
2287    {
2288      x += pRef[j];
2289      y += pRec[j];
2290      if ( eType == TEXT_LUMA )
2291      {
2292        xx += (pRef[j] * pRef[j])>>precShift;
2293        xy += (pRef[j] * pRec[j])>>precShift;
2294      }
2295    }
2296    iCountShift += g_aucConvertToBit[ uiWidth ] + 1;
2297  }
2298
2299  if( pcCU->getPULeft( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) )
2300  {
2301    iRefOffset = iHor + iVer * iRefStride - 1;
2302    if( eType == TEXT_LUMA )
2303    {
2304      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2305      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
2306    }
2307    else if( eType == TEXT_CHROMA_U )
2308    {
2309      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2310      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
2311    }
2312    else
2313    {
2314      assert( eType == TEXT_CHROMA_V );
2315      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
2316      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
2317    }
2318
2319    for( i = 0; i < uiHeight; i+=2 )
2320    {
2321      x += pRef[0];
2322      y += pRec[0];
2323      if ( eType == TEXT_LUMA )
2324      {
2325        xx += (pRef[0] * pRef[0])>>precShift;
2326        xy += (pRef[0] * pRec[0])>>precShift;
2327      }
2328      pRef += iRefStride*2;
2329      pRec += iRecStride*2;
2330    }
2331    iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 1 );
2332  }
2333
2334  if( iCountShift == 0 )
2335  {
2336    a = ( 1 << IC_CONST_SHIFT );
2337    b = 0;
2338    return;
2339  }
2340
2341  if (  eType != TEXT_LUMA )
2342  {
2343    a = 32;
2344    b = (  y - x + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
2345  }
2346  else
2347  {
2348  xy += xx >> IC_REG_COST_SHIFT;
2349  xx += xx >> IC_REG_COST_SHIFT;
2350  Int a1 = ( xy << iCountShift ) - ((y * x) >> precShift);
2351  Int a2 = ( xx << iCountShift ) - ((x * x) >> precShift);
2352  const Int iShift = IC_CONST_SHIFT;
2353  {
2354    {
2355      const Int iShiftA2 = 6;
2356      const Int iAccuracyShift = 15;
2357
2358      Int iScaleShiftA2 = 0;
2359      Int iScaleShiftA1 = 0;
2360      Int a1s = a1;
2361      Int a2s = a2;
2362
2363      a1 = Clip3(0, 2*a2, a1);
2364      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2;
2365      iScaleShiftA1 = iScaleShiftA2 - IC_SHIFT_DIFF;
2366
2367      if( iScaleShiftA1 < 0 )
2368      {
2369        iScaleShiftA1 = 0;
2370      }
2371
2372      if( iScaleShiftA2 < 0 )
2373      {
2374        iScaleShiftA2 = 0;
2375      }
2376
2377      Int iScaleShiftA = iScaleShiftA2 + iAccuracyShift - iShift - iScaleShiftA1;
2378
2379
2380      a2s = a2 >> iScaleShiftA2;
2381
2382      a1s = a1 >> iScaleShiftA1;
2383
2384      a = a1s * m_uiaShift[ a2s ];
2385      a = a >> iScaleShiftA;
2386      b = (  y - ( ( a * x ) >> iShift ) + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
2387    }
2388  }   
2389  }
2390}
2391#endif
2392
2393#if H_3D_DIM
2394Void TComPrediction::xPredBiSegDCs( Int* ptrSrc, UInt srcStride, Bool* biSegPattern, Int patternStride, Pel& predDC1, Pel& predDC2 )
2395{
2396  Int  refDC1, refDC2;
2397  const Int  iTR = (   patternStride - 1        ) - srcStride;
2398  const Int  iTM = ( ( patternStride - 1 ) >> 1 ) - srcStride;
2399  const Int  iLB = (   patternStride - 1        ) * srcStride - 1;
2400  const Int  iLM = ( ( patternStride - 1 ) >> 1 ) * srcStride - 1;
2401
2402  Bool bL = ( biSegPattern[0] != biSegPattern[(patternStride-1)*patternStride] );
2403  Bool bT = ( biSegPattern[0] != biSegPattern[(patternStride-1)]               );
2404
2405  if( bL == bT )
2406  {
2407    const Int  iTRR = ( patternStride * 2 - 1  ) - srcStride; 
2408    const Int  iLBB = ( patternStride * 2 - 1  ) * srcStride - 1;
2409    refDC1 = bL ? ( ptrSrc[iTR] + ptrSrc[iLB] )>>1 : (abs(ptrSrc[iTRR] - ptrSrc[-(Int)srcStride]) > abs(ptrSrc[iLBB] - ptrSrc[ -1]) ? ptrSrc[iTRR] : ptrSrc[iLBB]);
2410    refDC2 =      ( ptrSrc[ -1] + ptrSrc[-(Int)srcStride] )>>1;
2411  }
2412  else
2413  {
2414    refDC1 = bL ? ptrSrc[iLB] : ptrSrc[iTR];
2415    refDC2 = bL ? ptrSrc[iTM] : ptrSrc[iLM];
2416  }
2417
2418  predDC1 = biSegPattern[0] ? refDC1 : refDC2;
2419  predDC2 = biSegPattern[0] ? refDC2 : refDC1;
2420}
2421
2422Void TComPrediction::xAssignBiSegDCs( Pel* ptrDst, UInt dstStride, Bool* biSegPattern, Int patternStride, Pel valDC1, Pel valDC2 )
2423{
2424  if( dstStride == patternStride )
2425  {
2426    for( UInt k = 0; k < (patternStride * patternStride); k++ )
2427    {
2428      if( true == biSegPattern[k] ) { ptrDst[k] = valDC2; }
2429      else                          { ptrDst[k] = valDC1; }
2430    }
2431  }
2432  else
2433  {
2434    Pel* piTemp = ptrDst;
2435    for( UInt uiY = 0; uiY < patternStride; uiY++ )
2436    {
2437      for( UInt uiX = 0; uiX < patternStride; uiX++ )
2438      {
2439        if( true == biSegPattern[uiX] ) { piTemp[uiX] = valDC2; }
2440        else                            { piTemp[uiX] = valDC1; }
2441      }
2442      piTemp       += dstStride;
2443      biSegPattern += patternStride;
2444    }
2445  }
2446}
2447
2448#if H_3D_DIM_DMM
2449
2450Void TComPrediction::xPredContourFromTex( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, TComWedgelet* pcContourWedge )
2451{
2452  pcContourWedge->clear();
2453
2454  // get copy of co-located texture luma block
2455  TComYuv cTempYuv;
2456  cTempYuv.create( uiWidth, uiHeight ); 
2457  cTempYuv.clear();
2458  Pel* piRefBlkY = cTempYuv.getLumaAddr();
2459  xCopyTextureLumaBlock( pcCU, uiAbsPartIdx, piRefBlkY, uiWidth, uiHeight );
2460  piRefBlkY = cTempYuv.getLumaAddr();
2461
2462  // find contour for texture luma block
2463  UInt iDC = 0;
2464
2465  iDC  = piRefBlkY[ 0 ];
2466  iDC += piRefBlkY[ uiWidth - 1 ];
2467  iDC += piRefBlkY[ uiWidth * (uiHeight - 1) ];
2468  iDC += piRefBlkY[ uiWidth * (uiHeight - 1) + uiWidth - 1 ];
2469  iDC = iDC >> 2;
2470
2471  piRefBlkY = cTempYuv.getLumaAddr();
2472
2473  Bool* pabContourPattern = pcContourWedge->getPattern();
2474  for( UInt k = 0; k < (uiWidth*uiHeight); k++ ) 
2475  { 
2476    pabContourPattern[k] = (piRefBlkY[k] > iDC) ? true : false;
2477  }
2478
2479  cTempYuv.destroy();
2480}
2481
2482
2483Void TComPrediction::xCopyTextureLumaBlock( TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piDestBlockY, UInt uiWidth, UInt uiHeight )
2484{
2485  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getTexturePic()->getPicYuvRec();
2486  assert( pcPicYuvRef != NULL );
2487  Int         iRefStride = pcPicYuvRef->getStride();
2488  Pel*        piRefY = pcPicYuvRef->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiAbsPartIdx );
2489
2490  for ( Int y = 0; y < uiHeight; y++ )
2491  {
2492    ::memcpy(piDestBlockY, piRefY, sizeof(Pel)*uiWidth);
2493    piDestBlockY += uiWidth;
2494    piRefY += iRefStride;
2495  }
2496}
2497#endif
2498
2499
2500#if H_3D_DIM_SDC
2501Void TComPrediction::analyzeSegmentsSDC( Pel* pOrig, UInt uiStride, UInt uiSize, Pel* rpSegMeans, UInt uiNumSegments, Bool* pMask, UInt uiMaskStride
2502                                         ,UInt uiIntraMode
2503                                         ,Bool orgDC
2504                                        )
2505{
2506  Int iSumDepth[2];
2507  memset(iSumDepth, 0, sizeof(Int)*2);
2508  Int iSumPix[2];
2509  memset(iSumPix, 0, sizeof(Int)*2);
2510  for( Int i = 0; i < uiNumSegments; i++ )
2511  {
2512    rpSegMeans[i] = 0;
2513  }
2514  if (orgDC == false)
2515  {
2516    Pel* pLeftTop = pOrig;
2517    Pel* pRightTop = pOrig + (uiSize-1);
2518    Pel* pLeftBottom = (pOrig+ (uiStride*(uiSize-1)));
2519    Pel* pRightBottom = (pOrig+ (uiStride*(uiSize-1)) + (uiSize-1));
2520
2521    rpSegMeans[0] = (*pLeftTop + *pRightTop + *pLeftBottom + *pRightBottom + 2)>>2;
2522    return;
2523  }
2524
2525  Int subSamplePix;
2526  if ( uiSize == 64 || uiSize == 32 )
2527  {
2528    subSamplePix = 2;
2529  }
2530  else
2531  {
2532    subSamplePix = 1;
2533  }
2534  for (Int y=0; y<uiSize; y+=subSamplePix)
2535  {
2536    for (Int x=0; x<uiSize; x+=subSamplePix)
2537    {
2538      UChar ucSegment = pMask?(UChar)pMask[x]:0;
2539      assert( ucSegment < uiNumSegments );
2540     
2541      iSumDepth[ucSegment] += pOrig[x];
2542      iSumPix[ucSegment]   += 1;
2543    }
2544   
2545    pOrig  += uiStride*subSamplePix;
2546    pMask  += uiMaskStride*subSamplePix;
2547  }
2548 
2549  // compute mean for each segment
2550  for( UChar ucSeg = 0; ucSeg < uiNumSegments; ucSeg++ )
2551  {
2552    if( iSumPix[ucSeg] > 0 )
2553      rpSegMeans[ucSeg] = iSumDepth[ucSeg] / iSumPix[ucSeg];
2554    else
2555      rpSegMeans[ucSeg] = 0;  // this happens for zero-segments
2556  }
2557}
2558#endif // H_3D_DIM_SDC
2559#endif
2560//! \}
Note: See TracBrowser for help on using the repository browser.