source: 3DVCSoftware/branches/HTM-16.0-MV-draft-5/source/Lib/TLibCommon/TComPrediction.cpp @ 1389

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

Merged 15.1-dev1@1381.

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