source: 3DVCSoftware/branches/HTM-14.1-update-dev2/source/Lib/TLibCommon/TComPrediction.cpp @ 1274

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

Merged HTM-14.1-update-dev3@1273.

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