source: 3DVCSoftware/branches/HTM-10.2-dev0/source/Lib/TLibCommon/TComPrediction.cpp @ 936

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

Merged 10.2-dev2-MediaTek@930.

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