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

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

Further fixes.

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