source: 3DVCSoftware/branches/HTM-15.0-dev0/source/Lib/TLibCommon/TComPrediction.cpp @ 1317

Last change on this file since 1317 was 1317, checked in by tech, 9 years ago

Clean-ups. HLS.

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