source: 3DVCSoftware/branches/HTM-11.2-dev0/source/Lib/TLibCommon/TComPrediction.cpp @ 1029

Last change on this file since 1029 was 1029, checked in by tech, 10 years ago

Merged 11.2-dev1-MediaTek@1004.

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