source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComPrediction.cpp

Last change on this file was 1413, checked in by tech, 6 years ago

Merged HTM-16.2-dev@1412

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