source: 3DVCSoftware/branches/HTM-15.2-dev/source/Lib/TLibCommon/TComPrediction.cpp @ 1362

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

Align macros

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