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

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

Merged 14.1-update-dev2@1277.

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