source: 3DVCSoftware/branches/HTM-11.2-dev1-Sharp/source/Lib/TLibCommon/TComPrediction.cpp @ 998

Last change on this file since 998 was 998, checked in by sharpjp-htm, 10 years ago

Integration of I0109

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