source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibCommon/TComPrediction.cpp @ 510

Last change on this file since 510 was 510, checked in by mitsubishi-htm, 11 years ago

-VSP compensation part migration, not fully tested, intermediate version

  • Property svn:eol-style set to native
File size: 57.0 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-2013, 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
41//! \ingroup TLibCommon
42//! \{
43
44// ====================================================================================================================
45// Constructor / destructor / initialize
46// ====================================================================================================================
47
48TComPrediction::TComPrediction()
49: m_pLumaRecBuffer(0)
50, m_iLumaRecStride(0)
51{
52  m_piYuvExt = NULL;
53#if H_3D_VSP
54  m_pDepthBlock = (Int*) malloc(MAX_NUM_SPU_W*MAX_NUM_SPU_W*sizeof(Int));
55  if (m_pDepthBlock == NULL)
56      printf("ERROR: UKTGHU, No memory allocated.\n");
57#endif
58}
59
60TComPrediction::~TComPrediction()
61{
62#if H_3D_VSP
63  if (m_pDepthBlock != NULL)
64      free(m_pDepthBlock);
65#endif
66
67  delete[] m_piYuvExt;
68
69  m_acYuvPred[0].destroy();
70  m_acYuvPred[1].destroy();
71
72  m_cYuvPredTemp.destroy();
73
74#if H_3D_ARP
75  m_acYuvPredBase[0].destroy();
76  m_acYuvPredBase[1].destroy();
77#endif
78
79  if( m_pLumaRecBuffer )
80  {
81    delete [] m_pLumaRecBuffer;
82  }
83 
84  Int i, j;
85  for (i = 0; i < 4; i++)
86  {
87    for (j = 0; j < 4; j++)
88    {
89      m_filteredBlock[i][j].destroy();
90    }
91    m_filteredBlockTmp[i].destroy();
92  }
93}
94
95Void TComPrediction::initTempBuff()
96{
97  if( m_piYuvExt == NULL )
98  {
99    Int extWidth  = MAX_CU_SIZE + 16; 
100    Int extHeight = MAX_CU_SIZE + 1;
101    Int i, j;
102    for (i = 0; i < 4; i++)
103    {
104      m_filteredBlockTmp[i].create(extWidth, extHeight + 7);
105      for (j = 0; j < 4; j++)
106      {
107        m_filteredBlock[i][j].create(extWidth, extHeight);
108      }
109    }
110    m_iYuvExtHeight  = ((MAX_CU_SIZE + 2) << 4);
111    m_iYuvExtStride = ((MAX_CU_SIZE  + 8) << 4);
112    m_piYuvExt = new Int[ m_iYuvExtStride * m_iYuvExtHeight ];
113
114    // new structure
115    m_acYuvPred[0] .create( MAX_CU_SIZE, MAX_CU_SIZE );
116    m_acYuvPred[1] .create( MAX_CU_SIZE, MAX_CU_SIZE );
117
118    m_cYuvPredTemp.create( MAX_CU_SIZE, MAX_CU_SIZE );
119#if H_3D_ARP
120    m_acYuvPredBase[0] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
121    m_acYuvPredBase[1] .create( g_uiMaxCUWidth, g_uiMaxCUHeight );
122#endif
123  }
124
125  if (m_iLumaRecStride != (MAX_CU_SIZE>>1) + 1)
126  {
127    m_iLumaRecStride =  (MAX_CU_SIZE>>1) + 1;
128    if (!m_pLumaRecBuffer)
129    {
130      m_pLumaRecBuffer = new Pel[ m_iLumaRecStride * m_iLumaRecStride ];
131    }
132  }
133#if H_3D_IC
134  for( Int i = 1; i < 64; i++ )
135  {
136    m_uiaShift[i-1] = ( (1 << 15) + i/2 ) / i;
137  }
138#endif
139}
140
141// ====================================================================================================================
142// Public member functions
143// ====================================================================================================================
144
145// Function for calculating DC value of the reference samples used in Intra prediction
146Pel TComPrediction::predIntraGetPredValDC( Int* pSrc, Int iSrcStride, UInt iWidth, UInt iHeight, Bool bAbove, Bool bLeft )
147{
148  Int iInd, iSum = 0;
149  Pel pDcVal;
150
151  if (bAbove)
152  {
153    for (iInd = 0;iInd < iWidth;iInd++)
154    {
155      iSum += pSrc[iInd-iSrcStride];
156    }
157  }
158  if (bLeft)
159  {
160    for (iInd = 0;iInd < iHeight;iInd++)
161    {
162      iSum += pSrc[iInd*iSrcStride-1];
163    }
164  }
165
166  if (bAbove && bLeft)
167  {
168    pDcVal = (iSum + iWidth) / (iWidth + iHeight);
169  }
170  else if (bAbove)
171  {
172    pDcVal = (iSum + iWidth/2) / iWidth;
173  }
174  else if (bLeft)
175  {
176    pDcVal = (iSum + iHeight/2) / iHeight;
177  }
178  else
179  {
180    pDcVal = pSrc[-1]; // Default DC value already calculated and placed in the prediction array if no neighbors are available
181  }
182 
183  return pDcVal;
184}
185
186// Function for deriving the angular Intra predictions
187
188/** Function for deriving the simplified angular intra predictions.
189 * \param pSrc pointer to reconstructed sample array
190 * \param srcStride the stride of the reconstructed sample array
191 * \param rpDst reference to pointer for the prediction sample array
192 * \param dstStride the stride of the prediction sample array
193 * \param width the width of the block
194 * \param height the height of the block
195 * \param dirMode the intra prediction mode index
196 * \param blkAboveAvailable boolean indication if the block above is available
197 * \param blkLeftAvailable boolean indication if the block to the left is available
198 *
199 * This function derives the prediction samples for the angular mode based on the prediction direction indicated by
200 * the prediction mode index. The prediction direction is given by the displacement of the bottom row of the block and
201 * the reference row above the block in the case of vertical prediction or displacement of the rightmost column
202 * of the block and reference column left from the block in the case of the horizontal prediction. The displacement
203 * is signalled at 1/32 pixel accuracy. When projection of the predicted pixel falls inbetween reference samples,
204 * the predicted value for the pixel is linearly interpolated from the reference samples. All reference samples are taken
205 * from the extended main reference.
206 */
207Void TComPrediction::xPredIntraAng(Int bitDepth, Int* pSrc, Int srcStride, Pel*& rpDst, Int dstStride, UInt width, UInt height, UInt dirMode, Bool blkAboveAvailable, Bool blkLeftAvailable, Bool bFilter )
208{
209  Int k,l;
210  Int blkSize        = width;
211  Pel* pDst          = rpDst;
212
213  // Map the mode index to main prediction direction and angle
214  assert( dirMode > 0 ); //no planar
215  Bool modeDC        = dirMode < 2;
216  Bool modeHor       = !modeDC && (dirMode < 18);
217  Bool modeVer       = !modeDC && !modeHor;
218  Int intraPredAngle = modeVer ? (Int)dirMode - VER_IDX : modeHor ? -((Int)dirMode - HOR_IDX) : 0;
219  Int absAng         = abs(intraPredAngle);
220  Int signAng        = intraPredAngle < 0 ? -1 : 1;
221
222  // Set bitshifts and scale the angle parameter to block size
223  Int angTable[9]    = {0,    2,    5,   9,  13,  17,  21,  26,  32};
224  Int invAngTable[9] = {0, 4096, 1638, 910, 630, 482, 390, 315, 256}; // (256 * 32) / Angle
225  Int invAngle       = invAngTable[absAng];
226  absAng             = angTable[absAng];
227  intraPredAngle     = signAng * absAng;
228
229  // Do the DC prediction
230  if (modeDC)
231  {
232    Pel dcval = predIntraGetPredValDC(pSrc, srcStride, width, height, blkAboveAvailable, blkLeftAvailable);
233
234    for (k=0;k<blkSize;k++)
235    {
236      for (l=0;l<blkSize;l++)
237      {
238        pDst[k*dstStride+l] = dcval;
239      }
240    }
241  }
242
243  // Do angular predictions
244  else
245  {
246    Pel* refMain;
247    Pel* refSide;
248    Pel  refAbove[2*MAX_CU_SIZE+1];
249    Pel  refLeft[2*MAX_CU_SIZE+1];
250
251    // Initialise the Main and Left reference array.
252    if (intraPredAngle < 0)
253    {
254      for (k=0;k<blkSize+1;k++)
255      {
256        refAbove[k+blkSize-1] = pSrc[k-srcStride-1];
257      }
258      for (k=0;k<blkSize+1;k++)
259      {
260        refLeft[k+blkSize-1] = pSrc[(k-1)*srcStride-1];
261      }
262      refMain = (modeVer ? refAbove : refLeft) + (blkSize-1);
263      refSide = (modeVer ? refLeft : refAbove) + (blkSize-1);
264
265      // Extend the Main reference to the left.
266      Int invAngleSum    = 128;       // rounding for (shift by 8)
267      for (k=-1; k>blkSize*intraPredAngle>>5; k--)
268      {
269        invAngleSum += invAngle;
270        refMain[k] = refSide[invAngleSum>>8];
271      }
272    }
273    else
274    {
275      for (k=0;k<2*blkSize+1;k++)
276      {
277        refAbove[k] = pSrc[k-srcStride-1];
278      }
279      for (k=0;k<2*blkSize+1;k++)
280      {
281        refLeft[k] = pSrc[(k-1)*srcStride-1];
282      }
283      refMain = modeVer ? refAbove : refLeft;
284      refSide = modeVer ? refLeft  : refAbove;
285    }
286
287    if (intraPredAngle == 0)
288    {
289      for (k=0;k<blkSize;k++)
290      {
291        for (l=0;l<blkSize;l++)
292        {
293          pDst[k*dstStride+l] = refMain[l+1];
294        }
295      }
296
297      if ( bFilter )
298      {
299        for (k=0;k<blkSize;k++)
300        {
301          pDst[k*dstStride] = Clip3(0, (1<<bitDepth)-1, pDst[k*dstStride] + (( refSide[k+1] - refSide[0] ) >> 1) );
302        }
303      }
304    }
305    else
306    {
307      Int deltaPos=0;
308      Int deltaInt;
309      Int deltaFract;
310      Int refMainIndex;
311
312      for (k=0;k<blkSize;k++)
313      {
314        deltaPos += intraPredAngle;
315        deltaInt   = deltaPos >> 5;
316        deltaFract = deltaPos & (32 - 1);
317
318        if (deltaFract)
319        {
320          // Do linear filtering
321          for (l=0;l<blkSize;l++)
322          {
323            refMainIndex        = l+deltaInt+1;
324            pDst[k*dstStride+l] = (Pel) ( ((32-deltaFract)*refMain[refMainIndex]+deltaFract*refMain[refMainIndex+1]+16) >> 5 );
325          }
326        }
327        else
328        {
329          // Just copy the integer samples
330          for (l=0;l<blkSize;l++)
331          {
332            pDst[k*dstStride+l] = refMain[l+deltaInt+1];
333          }
334        }
335      }
336    }
337
338    // Flip the block if this is the horizontal mode
339    if (modeHor)
340    {
341      Pel  tmp;
342      for (k=0;k<blkSize-1;k++)
343      {
344        for (l=k+1;l<blkSize;l++)
345        {
346          tmp                 = pDst[k*dstStride+l];
347          pDst[k*dstStride+l] = pDst[l*dstStride+k];
348          pDst[l*dstStride+k] = tmp;
349        }
350      }
351    }
352  }
353}
354
355Void TComPrediction::predIntraLumaAng(TComPattern* pcTComPattern, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft )
356{
357  Pel *pDst = piPred;
358  Int *ptrSrc;
359
360  assert( g_aucConvertToBit[ iWidth ] >= 0 ); //   4x  4
361  assert( g_aucConvertToBit[ iWidth ] <= 5 ); // 128x128
362  assert( iWidth == iHeight  );
363
364  ptrSrc = pcTComPattern->getPredictorPtr( uiDirMode, g_aucConvertToBit[ iWidth ] + 2, m_piYuvExt );
365
366  // get starting pixel in block
367  Int sw = 2 * iWidth + 1;
368
369  // Create the prediction
370  if ( uiDirMode == PLANAR_IDX )
371  {
372    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
373  }
374  else
375  {
376    if ( (iWidth > 16) || (iHeight > 16) )
377    {
378      xPredIntraAng(g_bitDepthY, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, false );
379    }
380    else
381    {
382      xPredIntraAng(g_bitDepthY, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, true );
383
384      if( (uiDirMode == DC_IDX ) && bAbove && bLeft )
385      {
386        xDCPredFiltering( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight);
387      }
388    }
389  }
390}
391
392// Angular chroma
393Void TComPrediction::predIntraChromaAng( Int* piSrc, UInt uiDirMode, Pel* piPred, UInt uiStride, Int iWidth, Int iHeight, Bool bAbove, Bool bLeft )
394{
395  Pel *pDst = piPred;
396  Int *ptrSrc = piSrc;
397
398  // get starting pixel in block
399  Int sw = 2 * iWidth + 1;
400
401  if ( uiDirMode == PLANAR_IDX )
402  {
403    xPredIntraPlanar( ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight );
404  }
405  else
406  {
407    // Create the prediction
408    xPredIntraAng(g_bitDepthC, ptrSrc+sw+1, sw, pDst, uiStride, iWidth, iHeight, uiDirMode, bAbove, bLeft, false );
409  }
410}
411
412/** Function for checking identical motion.
413 * \param TComDataCU* pcCU
414 * \param UInt PartAddr
415 */
416Bool TComPrediction::xCheckIdenticalMotion ( TComDataCU* pcCU, UInt PartAddr )
417{
418  if( pcCU->getSlice()->isInterB() && !pcCU->getSlice()->getPPS()->getWPBiPred() )
419  {
420    if( pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr) >= 0 && pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr) >= 0)
421    {
422      Int RefPOCL0 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_0, pcCU->getCUMvField(REF_PIC_LIST_0)->getRefIdx(PartAddr))->getPOC();
423      Int RefPOCL1 = pcCU->getSlice()->getRefPic(REF_PIC_LIST_1, pcCU->getCUMvField(REF_PIC_LIST_1)->getRefIdx(PartAddr))->getPOC();
424      if(RefPOCL0 == RefPOCL1 && pcCU->getCUMvField(REF_PIC_LIST_0)->getMv(PartAddr) == pcCU->getCUMvField(REF_PIC_LIST_1)->getMv(PartAddr))
425      {
426        return true;
427      }
428    }
429  }
430  return false;
431}
432
433
434Void TComPrediction::motionCompensation ( TComDataCU* pcCU, TComYuv* pcYuvPred, RefPicList eRefPicList, Int iPartIdx )
435{
436  Int         iWidth;
437  Int         iHeight;
438  UInt        uiPartAddr;
439#if H_3D_VSP
440  UInt        uiAbsPartIdx = pcCU->getZorderIdxInCU();
441#endif
442
443  if ( iPartIdx >= 0 )
444  {
445    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
446#if H_3D_VSP
447    if ( 0 == pcCU->getVSPFlag(uiPartAddr) )
448    {
449#endif
450      if ( eRefPicList != REF_PIC_LIST_X )
451      {
452        if( pcCU->getSlice()->getPPS()->getUseWP())
453        {
454          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
455        }
456        else
457        {
458          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
459        }
460        if ( pcCU->getSlice()->getPPS()->getUseWP() )
461        {
462          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
463        }
464      }
465      else
466      {
467        if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
468        {
469          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
470        }
471        else
472        {
473          xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
474        }
475      }
476#if H_3D_VSP
477    }
478    else
479    {
480      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
481        xPredInterUniVSP( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
482      else
483        xPredInterBiVSP ( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, pcYuvPred );
484    }
485#endif
486    return;
487  }
488
489  for ( iPartIdx = 0; iPartIdx < pcCU->getNumPartInter(); iPartIdx++ )
490  {
491    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
492
493#if H_3D_VSP
494    if ( 0 == pcCU->getVSPFlag(uiPartAddr) )
495    {
496#endif
497      if ( eRefPicList != REF_PIC_LIST_X )
498      {
499        if( pcCU->getSlice()->getPPS()->getUseWP())
500        {
501          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
502        }
503        else
504        {
505          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
506        }
507        if ( pcCU->getSlice()->getPPS()->getUseWP() )
508        {
509          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
510        }
511      }
512      else
513      {
514        if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
515        {
516          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
517        }
518        else
519        {
520          xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
521        }
522      }
523#if H_3D_VSP
524    }
525    else
526    {
527      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
528        xPredInterUniVSP( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
529      else
530        xPredInterBiVSP ( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, pcYuvPred );
531    }
532#endif
533  }
534  return;
535}
536
537Void TComPrediction::xPredInterUni ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
538{
539  Int         iRefIdx     = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           assert (iRefIdx >= 0);
540  TComMv      cMv         = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
541  pcCU->clipMv(cMv);
542#if H_3D_ARP
543  if(  pcCU->getARPW( uiPartAddr ) > 0 
544    && pcCU->getPartitionSize(uiPartAddr)==SIZE_2Nx2N
545    && pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPOC()!= pcCU->getSlice()->getPOC() 
546    )
547  {
548    xPredInterUniARP( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, rpcYuvPred, bi );
549  }
550  else
551  {
552#endif
553#if H_3D_IC
554    Bool bICFlag = pcCU->getICFlag( uiPartAddr ) && ( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getViewIndex() != pcCU->getSlice()->getViewIndex() );
555    xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
556#if H_3D_ARP
557      , false
558#endif
559      , bICFlag );
560    xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
561#if H_3D_ARP
562      , false
563#endif
564      , bICFlag );
565#else
566  xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
567  xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
568#endif
569#if H_3D_ARP
570  }
571#endif
572}
573
574#if H_3D_VSP
575Void TComPrediction::xPredInterUniVSP( TComDataCU* pcCU, UInt uiPartAddr, UInt uiAbsPartIdx, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
576{
577  // Step 1: get depth reference
578  Int depthRefViewIdx = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan;
579  TComPic* pRefPicBaseDepth = pcCU->getSlice()->getIvPic (true, depthRefViewIdx );
580  assert(pRefPicBaseDepth != NULL);
581  TComPicYuv* pcBaseViewDepthPicYuv = pRefPicBaseDepth->getPicYuvRec();
582  assert(pcBaseViewDepthPicYuv != NULL);
583
584  // Step 2: get texture reference
585  Int iRefIdx = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
586  assert(iRefIdx >= 0);
587  TComPic* pRefPicBaseTxt = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx );
588  TComPicYuv* pcBaseViewTxtPicYuv = pRefPicBaseTxt->getPicYuvRec();
589  assert(pcBaseViewTxtPicYuv != NULL);
590
591  // Step 3: initialize the LUT according to the reference viewIdx
592  Int txtRefViewIdx = pRefPicBaseTxt->getViewIndex();
593  Int* pShiftLUT    = pcCU->getSlice()->getDepthToDisparityB( txtRefViewIdx );
594
595  // Step 4: Do compensation
596  TComMv cMv  = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
597  pcCU->clipMv(cMv);
598  Int iBlkX = ( pcCU->getAddr() % pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
599  Int iBlkY = ( pcCU->getAddr() / pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
600  xPredInterLumaBlkFromDM  ( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, &cMv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,    pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
601  xPredInterChromaBlkFromDM( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, &cMv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1, pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
602}
603#endif
604
605#if H_3D_ARP
606Void TComPrediction::xPredInterUniARP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi, TComMvField * pNewMvFiled )
607{
608  Int         iRefIdx      = pNewMvFiled ? pNewMvFiled->getRefIdx() : pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           
609  TComMv      cMv          = pNewMvFiled ? pNewMvFiled->getMv()     : pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
610  Bool        bTobeScaled  = false;
611  TComPic* pcPicYuvBaseCol = NULL;
612  TComPic* pcPicYuvBaseRef = NULL;
613
614#if H_3D_NBDV
615  DisInfo cDistparity;
616  cDistparity.bDV           = pcCU->getDvInfo(uiPartAddr).bDV;
617  if( cDistparity.bDV )
618  {
619    cDistparity.m_acNBDV = pcCU->getDvInfo(0).m_acNBDV;
620    assert(pcCU->getDvInfo(uiPartAddr).bDV ==  pcCU->getDvInfo(0).bDV);
621    cDistparity.m_aVIdxCan = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan;
622  }
623#else
624  assert(0); // ARP can be applied only when a DV is available
625#endif
626
627  UChar dW = cDistparity.bDV ? pcCU->getARPW ( uiPartAddr ) : 0;
628
629  if( cDistparity.bDV ) 
630  {
631    if( dW > 0 && pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC()!= pcCU->getSlice()->getPOC() )
632    {
633      bTobeScaled = true;
634    }
635
636    pcPicYuvBaseCol =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getPOC(),                              cDistparity.m_aVIdxCan );
637    pcPicYuvBaseRef =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC(), cDistparity.m_aVIdxCan );
638   
639    if( ( !pcPicYuvBaseCol || pcPicYuvBaseCol->getPOC() != pcCU->getSlice()->getPOC() ) || ( !pcPicYuvBaseRef || pcPicYuvBaseRef->getPOC() != pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() ) )
640    {
641      dW = 0;
642      bTobeScaled = false;
643    }
644    else
645    {
646      assert( pcPicYuvBaseCol->getPOC() == pcCU->getSlice()->getPOC() && pcPicYuvBaseRef->getPOC() == pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() );
647    }
648
649    if(bTobeScaled)
650    {     
651      Int iCurrPOC    = pcCU->getSlice()->getPOC();
652      Int iColRefPOC  = pcCU->getSlice()->getRefPOC( eRefPicList, iRefIdx );
653      Int iCurrRefPOC = pcCU->getSlice()->getRefPOC( eRefPicList,  0);
654      Int iScale = pcCU-> xGetDistScaleFactor(iCurrPOC, iCurrRefPOC, iCurrPOC, iColRefPOC);
655      if ( iScale != 4096 )
656      {
657        cMv = cMv.scaleMv( iScale );
658      }
659      iRefIdx = 0;
660    }
661  }
662
663  pcCU->clipMv(cMv);
664  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec();
665  xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, true );
666  xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, true );
667
668  if( dW > 0 )
669  {
670    TComYuv * pYuvB0 = &m_acYuvPredBase[0];
671    TComYuv * pYuvB1  = &m_acYuvPredBase[1];
672
673    TComMv cMVwithDisparity = cMv + cDistparity.m_acNBDV;
674    pcCU->clipMv(cMVwithDisparity);
675
676    assert ( cDistparity.bDV );
677
678    pcPicYuvRef = pcPicYuvBaseCol->getPicYuvRec();
679    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acNBDV, iWidth, iHeight, pYuvB0, bi, true );
680    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acNBDV, iWidth, iHeight, pYuvB0, bi, true );
681   
682    pcPicYuvRef = pcPicYuvBaseRef->getPicYuvRec();
683    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, bi, true );
684    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, bi, true );
685
686    pYuvB0->subtractARP( pYuvB0 , pYuvB1 , uiPartAddr , iWidth , iHeight );
687
688    if( 2 == dW )
689    {
690      pYuvB0->multiplyARP( uiPartAddr , iWidth , iHeight , dW );
691    }
692
693    rpcYuvPred->addARP( rpcYuvPred , pYuvB0 , uiPartAddr , iWidth , iHeight , !bi );
694  }
695}
696#endif
697
698Void TComPrediction::xPredInterBi ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
699{
700  TComYuv* pcMbYuv;
701  Int      iRefIdx[2] = {-1, -1};
702
703  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
704  {
705    RefPicList eRefPicList = (iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
706    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
707
708    if ( iRefIdx[iRefList] < 0 )
709    {
710      continue;
711    }
712
713    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
714
715    pcMbYuv = &m_acYuvPred[iRefList];
716    if( pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0 )
717    {
718      xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
719    }
720    else
721    {
722      if ( ( pcCU->getSlice()->getPPS()->getUseWP()       && pcCU->getSlice()->getSliceType() == P_SLICE ) || 
723           ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE ) )
724      {
725        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
726      }
727      else
728      {
729        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv );
730      }
731    }
732  }
733
734  if ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE  )
735  {
736    xWeightedPredictionBi( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
737  } 
738  else if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType() == P_SLICE )
739  {
740    xWeightedPredictionUni( pcCU, &m_acYuvPred[0], uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, rpcYuvPred ); 
741  }
742  else
743  {
744    xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
745  }
746}
747
748#if H_3D_VSP
749
750Void TComPrediction::xPredInterBiVSP( TComDataCU* pcCU, UInt uiPartAddr, UInt uiAbsPartIdx, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
751{
752  TComYuv* pcMbYuv;
753  Int      iRefIdx[2] = {-1, -1};
754
755  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
756  {
757    RefPicList eRefPicList = RefPicList(iRefList);
758    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
759
760    if ( iRefIdx[iRefList] < 0 )
761      continue;
762
763    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
764
765    pcMbYuv = &m_acYuvPred[iRefList];
766    if( pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0 )
767      xPredInterUniVSP ( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, eRefPicList, pcMbYuv, true );
768    else
769      xPredInterUniVSP ( pcCU, uiPartAddr, uiAbsPartIdx, iWidth, iHeight, eRefPicList, pcMbYuv );
770  }
771
772  xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
773}
774
775#endif
776
777/**
778 * \brief Generate motion-compensated luma block
779 *
780 * \param cu       Pointer to current CU
781 * \param refPic   Pointer to reference picture
782 * \param partAddr Address of block within CU
783 * \param mv       Motion vector
784 * \param width    Width of block
785 * \param height   Height of block
786 * \param dstPic   Pointer to destination picture
787 * \param bi       Flag indicating whether bipred is used
788 */
789Void TComPrediction::xPredInterLumaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
790#if H_3D_ARP
791    , Bool filterType
792#endif
793#if H_3D_IC
794    , Bool bICFlag
795#endif
796  )
797{
798  Int refStride = refPic->getStride(); 
799  Int refOffset = ( mv->getHor() >> 2 ) + ( mv->getVer() >> 2 ) * refStride;
800  Pel *ref      = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
801 
802  Int dstStride = dstPic->getStride();
803  Pel *dst      = dstPic->getLumaAddr( partAddr );
804 
805  Int xFrac = mv->getHor() & 0x3;
806  Int yFrac = mv->getVer() & 0x3;
807
808#if H_3D_IC
809  if( cu->getSlice()->getIsDepth() )
810  {
811    refOffset = mv->getHor() + mv->getVer() * refStride;
812    ref       = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
813    xFrac     = 0;
814    yFrac     = 0;
815  }
816#endif
817
818  if ( yFrac == 0 )
819  {
820    m_if.filterHorLuma( ref, refStride, dst, dstStride, width, height, xFrac,       !bi
821#if H_3D_ARP
822    , filterType
823#endif
824      );
825  }
826  else if ( xFrac == 0 )
827  {
828    m_if.filterVerLuma( ref, refStride, dst, dstStride, width, height, yFrac, true, !bi
829#if H_3D_ARP
830    , filterType
831#endif
832      );
833  }
834  else
835  {
836    Int tmpStride = m_filteredBlockTmp[0].getStride();
837    Short *tmp    = m_filteredBlockTmp[0].getLumaAddr();
838
839    Int filterSize = NTAPS_LUMA;
840    Int halfFilterSize = ( filterSize >> 1 );
841
842    m_if.filterHorLuma(ref - (halfFilterSize-1)*refStride, refStride, tmp, tmpStride, width, height+filterSize-1, xFrac, false     
843#if H_3D_ARP
844    , filterType
845#endif
846      );
847    m_if.filterVerLuma(tmp + (halfFilterSize-1)*tmpStride, tmpStride, dst, dstStride, width, height,              yFrac, false, !bi
848#if H_3D_ARP
849    , filterType
850#endif
851      );   
852  }
853
854#if H_3D_IC
855  if( bICFlag )
856  {
857    Int a, b, iShift, i, j;
858
859    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_LUMA );
860
861    for ( i = 0; i < height; i++ )
862    {
863      for ( j = 0; j < width; j++ )
864      {
865        if( bi )
866        {
867          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthY;
868          dst[j] = ( ( a*dst[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1 << iIFshift ) - IF_INTERNAL_OFFS;
869        }
870        else
871          dst[j] = Clip3( 0, ( 1 << g_bitDepthY ) - 1, ( ( a*dst[j] ) >> iShift ) + b );
872      }
873      dst += dstStride;
874    }
875  }
876#endif
877}
878
879/**
880 * \brief Generate motion-compensated chroma block
881 *
882 * \param cu       Pointer to current CU
883 * \param refPic   Pointer to reference picture
884 * \param partAddr Address of block within CU
885 * \param mv       Motion vector
886 * \param width    Width of block
887 * \param height   Height of block
888 * \param dstPic   Pointer to destination picture
889 * \param bi       Flag indicating whether bipred is used
890 */
891Void TComPrediction::xPredInterChromaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
892#if H_3D_ARP
893    , Bool filterType
894#endif
895#if H_3D_IC
896    , Bool bICFlag
897#endif
898  )
899{
900  Int     refStride  = refPic->getCStride();
901  Int     dstStride  = dstPic->getCStride();
902 
903  Int     refOffset  = (mv->getHor() >> 3) + (mv->getVer() >> 3) * refStride;
904 
905  Pel*    refCb     = refPic->getCbAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
906  Pel*    refCr     = refPic->getCrAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
907 
908  Pel* dstCb = dstPic->getCbAddr( partAddr );
909  Pel* dstCr = dstPic->getCrAddr( partAddr );
910 
911  Int     xFrac  = mv->getHor() & 0x7;
912  Int     yFrac  = mv->getVer() & 0x7;
913  UInt    cxWidth  = width  >> 1;
914  UInt    cxHeight = height >> 1;
915 
916  Int     extStride = m_filteredBlockTmp[0].getStride();
917  Short*  extY      = m_filteredBlockTmp[0].getLumaAddr();
918 
919  Int filterSize = NTAPS_CHROMA;
920 
921  Int halfFilterSize = (filterSize>>1);
922 
923  if ( yFrac == 0 )
924  {
925    m_if.filterHorChroma(refCb, refStride, dstCb,  dstStride, cxWidth, cxHeight, xFrac, !bi
926#if H_3D_ARP
927    , filterType
928#endif
929    );   
930    m_if.filterHorChroma(refCr, refStride, dstCr,  dstStride, cxWidth, cxHeight, xFrac, !bi
931#if H_3D_ARP
932    , filterType
933#endif
934    );
935  }
936  else if ( xFrac == 0 )
937  {
938    m_if.filterVerChroma(refCb, refStride, dstCb, dstStride, cxWidth, cxHeight, yFrac, true, !bi
939#if H_3D_ARP
940    , filterType
941#endif
942    );
943    m_if.filterVerChroma(refCr, refStride, dstCr, dstStride, cxWidth, cxHeight, yFrac, true, !bi
944#if H_3D_ARP
945    , filterType
946#endif
947    );
948  }
949  else
950  {
951    m_if.filterHorChroma(refCb - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
952#if H_3D_ARP
953    , filterType
954#endif 
955      );
956    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCb, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
957#if H_3D_ARP
958    , filterType
959#endif
960      );
961   
962    m_if.filterHorChroma(refCr - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
963#if H_3D_ARP
964    , filterType
965#endif
966      );
967    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCr, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
968#if H_3D_ARP
969    , filterType
970#endif
971      );   
972  }
973
974#if H_3D_IC
975  if( bICFlag )
976  {
977    Int a, b, iShift, i, j;
978    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_CHROMA_U ); // Cb
979    for ( i = 0; i < cxHeight; i++ )
980    {
981      for ( j = 0; j < cxWidth; j++ )
982      {
983        if( bi )
984        {
985          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthC;
986          dstCb[j] = ( ( a*dstCb[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1<<iIFshift ) - IF_INTERNAL_OFFS;
987        }
988        else
989          dstCb[j] = Clip3(  0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCb[j] ) >> iShift ) + b );
990      }
991      dstCb += dstStride;
992    }
993    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_CHROMA_V ); // Cr
994    for ( i = 0; i < cxHeight; i++ )
995    {
996      for ( j = 0; j < cxWidth; j++ )
997      {
998        if( bi )
999        {
1000          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthC;
1001          dstCr[j] = ( ( a*dstCr[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1<<iIFshift ) - IF_INTERNAL_OFFS;
1002        }
1003        else
1004          dstCr[j] = Clip3( 0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCr[j] ) >> iShift ) + b );
1005      }
1006      dstCr += dstStride;
1007    }
1008  }
1009#endif
1010}
1011
1012Void TComPrediction::xWeightedAverage( TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, Int iRefIdx0, Int iRefIdx1, UInt uiPartIdx, Int iWidth, Int iHeight, TComYuv*& rpcYuvDst )
1013{
1014  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
1015  {
1016    rpcYuvDst->addAvg( pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight );
1017  }
1018  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
1019  {
1020    pcYuvSrc0->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
1021  }
1022  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
1023  {
1024    pcYuvSrc1->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
1025  }
1026}
1027
1028// AMVP
1029Void TComPrediction::getMvPredAMVP( TComDataCU* pcCU, UInt uiPartIdx, UInt uiPartAddr, RefPicList eRefPicList, TComMv& rcMvPred )
1030{
1031  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
1032  if( pcAMVPInfo->iN <= 1 )
1033  {
1034    rcMvPred = pcAMVPInfo->m_acMvCand[0];
1035
1036    pcCU->setMVPIdxSubParts( 0, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
1037    pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
1038    return;
1039  }
1040
1041  assert(pcCU->getMVPIdx(eRefPicList,uiPartAddr) >= 0);
1042  rcMvPred = pcAMVPInfo->m_acMvCand[pcCU->getMVPIdx(eRefPicList,uiPartAddr)];
1043  return;
1044}
1045
1046/** Function for deriving planar intra prediction.
1047 * \param pSrc pointer to reconstructed sample array
1048 * \param srcStride the stride of the reconstructed sample array
1049 * \param rpDst reference to pointer for the prediction sample array
1050 * \param dstStride the stride of the prediction sample array
1051 * \param width the width of the block
1052 * \param height the height of the block
1053 *
1054 * This function derives the prediction samples for planar mode (intra coding).
1055 */
1056Void TComPrediction::xPredIntraPlanar( Int* pSrc, Int srcStride, Pel* rpDst, Int dstStride, UInt width, UInt height )
1057{
1058  assert(width == height);
1059
1060  Int k, l, bottomLeft, topRight;
1061  Int horPred;
1062  Int leftColumn[MAX_CU_SIZE], topRow[MAX_CU_SIZE], bottomRow[MAX_CU_SIZE], rightColumn[MAX_CU_SIZE];
1063  UInt blkSize = width;
1064  UInt offset2D = width;
1065  UInt shift1D = g_aucConvertToBit[ width ] + 2;
1066  UInt shift2D = shift1D + 1;
1067
1068  // Get left and above reference column and row
1069  for(k=0;k<blkSize+1;k++)
1070  {
1071    topRow[k] = pSrc[k-srcStride];
1072    leftColumn[k] = pSrc[k*srcStride-1];
1073  }
1074
1075  // Prepare intermediate variables used in interpolation
1076  bottomLeft = leftColumn[blkSize];
1077  topRight   = topRow[blkSize];
1078  for (k=0;k<blkSize;k++)
1079  {
1080    bottomRow[k]   = bottomLeft - topRow[k];
1081    rightColumn[k] = topRight   - leftColumn[k];
1082    topRow[k]      <<= shift1D;
1083    leftColumn[k]  <<= shift1D;
1084  }
1085
1086  // Generate prediction signal
1087  for (k=0;k<blkSize;k++)
1088  {
1089    horPred = leftColumn[k] + offset2D;
1090    for (l=0;l<blkSize;l++)
1091    {
1092      horPred += rightColumn[k];
1093      topRow[l] += bottomRow[l];
1094      rpDst[k*dstStride+l] = ( (horPred + topRow[l]) >> shift2D );
1095    }
1096  }
1097}
1098
1099/** Function for filtering intra DC predictor.
1100 * \param pSrc pointer to reconstructed sample array
1101 * \param iSrcStride the stride of the reconstructed sample array
1102 * \param rpDst reference to pointer for the prediction sample array
1103 * \param iDstStride the stride of the prediction sample array
1104 * \param iWidth the width of the block
1105 * \param iHeight the height of the block
1106 *
1107 * This function performs filtering left and top edges of the prediction samples for DC mode (intra coding).
1108 */
1109Void TComPrediction::xDCPredFiltering( Int* pSrc, Int iSrcStride, Pel*& rpDst, Int iDstStride, Int iWidth, Int iHeight )
1110{
1111  Pel* pDst = rpDst;
1112  Int x, y, iDstStride2, iSrcStride2;
1113
1114  // boundary pixels processing
1115  pDst[0] = (Pel)((pSrc[-iSrcStride] + pSrc[-1] + 2 * pDst[0] + 2) >> 2);
1116
1117  for ( x = 1; x < iWidth; x++ )
1118  {
1119    pDst[x] = (Pel)((pSrc[x - iSrcStride] +  3 * pDst[x] + 2) >> 2);
1120  }
1121
1122  for ( y = 1, iDstStride2 = iDstStride, iSrcStride2 = iSrcStride-1; y < iHeight; y++, iDstStride2+=iDstStride, iSrcStride2+=iSrcStride )
1123  {
1124    pDst[iDstStride2] = (Pel)((pSrc[iSrcStride2] + 3 * pDst[iDstStride2] + 2) >> 2);
1125  }
1126
1127  return;
1128}
1129
1130#if H_3D_IC
1131/** Function for deriving the position of first non-zero binary bit of a value
1132 * \param x input value
1133 *
1134 * This function derives the position of first non-zero binary bit of a value
1135 */
1136Int GetMSB( UInt x )
1137{
1138  Int iMSB = 0, bits = ( sizeof( Int ) << 3 ), y = 1;
1139
1140  while( x > 1 )
1141  {
1142    bits >>= 1;
1143    y = x >> bits;
1144
1145    if( y )
1146    {
1147      x = y;
1148      iMSB += bits;
1149    }
1150  }
1151
1152  iMSB+=y;
1153
1154  return iMSB;
1155}
1156
1157/** Function for counting leading number of zeros/ones
1158 * \param x input value
1159 \ This function counts leading number of zeros for positive numbers and
1160 \ leading number of ones for negative numbers. This can be implemented in
1161 \ single instructure cycle on many processors.
1162 */
1163
1164Short CountLeadingZerosOnes (Short x)
1165{
1166  Short clz;
1167  Short i;
1168
1169  if(x == 0)
1170  {
1171    clz = 0;
1172  }
1173  else
1174  {
1175    if (x == -1)
1176    {
1177      clz = 15;
1178    }
1179    else
1180    {
1181      if(x < 0)
1182      {
1183        x = ~x;
1184      }
1185      clz = 15;
1186      for(i = 0;i < 15;++i)
1187      {
1188        if(x) 
1189        {
1190          clz --;
1191        }
1192        x = x >> 1;
1193      }
1194    }
1195  }
1196  return clz;
1197}
1198
1199/** Function for deriving LM illumination compensation.
1200 */
1201Void TComPrediction::xGetLLSICPrediction( TComDataCU* pcCU, TComMv *pMv, TComPicYuv *pRefPic, Int &a, Int &b, Int &iShift, TextType eType )
1202{
1203  TComPicYuv *pRecPic = pcCU->getPic()->getPicYuvRec();
1204  Pel *pRec = NULL, *pRef = NULL;
1205  UInt uiWidth, uiHeight, uiTmpPartIdx;
1206  Int iRecStride = ( eType == TEXT_LUMA ) ? pRecPic->getStride() : pRecPic->getCStride();
1207  Int iRefStride = ( eType == TEXT_LUMA ) ? pRefPic->getStride() : pRefPic->getCStride();
1208  Int iCUPelX, iCUPelY, iRefX, iRefY, iRefOffset, iHor, iVer;
1209
1210  iCUPelX = pcCU->getCUPelX() + g_auiRasterToPelX[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
1211  iCUPelY = pcCU->getCUPelY() + g_auiRasterToPelY[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
1212  iHor = pcCU->getSlice()->getIsDepth() ? pMv->getHor() : ( ( pMv->getHor() + 2 ) >> 2 );
1213  iVer = pcCU->getSlice()->getIsDepth() ? pMv->getVer() : ( ( pMv->getVer() + 2 ) >> 2 );
1214  iRefX   = iCUPelX + iHor;
1215  iRefY   = iCUPelY + iVer;
1216  if( eType != TEXT_LUMA )
1217  {
1218    iHor = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getHor() + 1 ) >> 1 ) : ( ( pMv->getHor() + 4 ) >> 3 );
1219    iVer = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getVer() + 1 ) >> 1 ) : ( ( pMv->getVer() + 4 ) >> 3 );
1220  }
1221  uiWidth  = ( eType == TEXT_LUMA ) ? pcCU->getWidth( 0 )  : ( pcCU->getWidth( 0 )  >> 1 );
1222  uiHeight = ( eType == TEXT_LUMA ) ? pcCU->getHeight( 0 ) : ( pcCU->getHeight( 0 ) >> 1 );
1223
1224  Int i, j, iCountShift = 0;
1225
1226  // LLS parameters estimation -->
1227
1228  Int x = 0, y = 0, xx = 0, xy = 0;
1229
1230  if( pcCU->getPUAbove( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) && iCUPelY > 0 && iRefY > 0 )
1231  {
1232    iRefOffset = iHor + iVer * iRefStride - iRefStride;
1233    if( eType == TEXT_LUMA )
1234    {
1235      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1236      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1237    }
1238    else if( eType == TEXT_CHROMA_U )
1239    {
1240      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1241      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1242    }
1243    else
1244    {
1245      assert( eType == TEXT_CHROMA_V );
1246      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1247      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1248    }
1249
1250    for( j = 0; j < uiWidth; j++ )
1251    {
1252      x += pRef[j];
1253      y += pRec[j];
1254      xx += pRef[j] * pRef[j];
1255      xy += pRef[j] * pRec[j];
1256    }
1257    iCountShift += g_aucConvertToBit[ uiWidth ] + 2;
1258  }
1259
1260
1261  if( pcCU->getPULeft( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) && iCUPelX > 0 && iRefX > 0 )
1262  {
1263    iRefOffset = iHor + iVer * iRefStride - 1;
1264    if( eType == TEXT_LUMA )
1265    {
1266      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1267      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1268    }
1269    else if( eType == TEXT_CHROMA_U )
1270    {
1271      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1272      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1273    }
1274    else
1275    {
1276      assert( eType == TEXT_CHROMA_V );
1277      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1278      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1279    }
1280
1281    for( i = 0; i < uiHeight; i++ )
1282    {
1283      x += pRef[0];
1284      y += pRec[0];
1285      xx += pRef[0] * pRef[0];
1286      xy += pRef[0] * pRec[0];
1287
1288      pRef += iRefStride;
1289      pRec += iRecStride;
1290    }
1291    iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 2 );
1292  }
1293
1294  Int iTempShift = ( ( eType == TEXT_LUMA ) ? g_bitDepthY : g_bitDepthC ) + g_aucConvertToBit[ uiWidth ] + 3 - 15;
1295
1296  if( iTempShift > 0 )
1297  {
1298    x  = ( x +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1299    y  = ( y +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1300    xx = ( xx + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1301    xy = ( xy + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1302    iCountShift -= iTempShift;
1303  }
1304
1305  iShift = 13;
1306
1307  if( iCountShift == 0 )
1308  {
1309    a = 1;
1310    b = 0;
1311    iShift = 0;
1312  }
1313  else
1314  {
1315    Int a1 = ( xy << iCountShift ) - y * x;
1316    Int a2 = ( xx << iCountShift ) - x * x;             
1317
1318    {
1319      const Int iShiftA2 = 6;
1320      const Int iShiftA1 = 15;
1321      const Int iAccuracyShift = 15;
1322
1323      Int iScaleShiftA2 = 0;
1324      Int iScaleShiftA1 = 0;
1325      Int a1s = a1;
1326      Int a2s = a2;
1327
1328      iScaleShiftA1 = GetMSB( abs( a1 ) ) - iShiftA1;
1329      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2; 
1330
1331      if( iScaleShiftA1 < 0 )
1332      {
1333        iScaleShiftA1 = 0;
1334      }
1335
1336      if( iScaleShiftA2 < 0 )
1337      {
1338        iScaleShiftA2 = 0;
1339      }
1340
1341      Int iScaleShiftA = iScaleShiftA2 + iAccuracyShift - iShift - iScaleShiftA1;
1342
1343      a2s = a2 >> iScaleShiftA2;
1344
1345      a1s = a1 >> iScaleShiftA1;
1346
1347      if (a2s >= 1)
1348      {
1349        a = a1s * m_uiaShift[ a2s - 1];
1350      }
1351      else
1352      {
1353        a = 0;
1354      }
1355
1356      if( iScaleShiftA < 0 )
1357      {
1358        a = a << -iScaleShiftA;
1359      }
1360      else
1361      {
1362        a = a >> iScaleShiftA;
1363      }
1364
1365      a = Clip3( -( 1 << 15 ), ( 1 << 15 ) - 1, a ); 
1366
1367      Int minA = -(1 << (6));
1368      Int maxA = (1 << 6) - 1;
1369      if( a <= maxA && a >= minA )
1370      {
1371        // do nothing
1372      }
1373      else
1374      {
1375        Short n = CountLeadingZerosOnes( a );
1376        a = a >> (9-n);
1377        iShift -= (9-n);
1378      }
1379
1380      b = (  y - ( ( a * x ) >> iShift ) + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
1381    }
1382  }   
1383}
1384#endif
1385
1386#if H_3D_VSP
1387// Input:
1388// refPic: Ref picture. Full picture, with padding
1389// posX, posY:     PU position, texture
1390// sizeX, sizeY: PU size
1391// partAddr: z-order index
1392// mv: disparity vector. derived from neighboring blocks
1393//
1394// Output: dstPic, PU predictor 64x64
1395Void TComPrediction::xPredInterLumaBlkFromDM( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, TComMv* mv, UInt partAddr,Int posX, Int posY
1396                                            , Int sizeX, Int sizeY, Bool isDepth, TComYuv *&dstPic, Bool bi )
1397{
1398  Int widthLuma;
1399  Int heightLuma;
1400
1401  if (isDepth)
1402  {
1403    widthLuma   =  pPicBaseDepth->getWidth();
1404    heightLuma  =  pPicBaseDepth->getHeight();
1405  }
1406  else
1407  {
1408    widthLuma   =  refPic->getWidth();
1409    heightLuma  =  refPic->getHeight();
1410  }
1411
1412#if H_3D_VSP_BLOCKSIZE != 1
1413  Int widthDepth  = pPicBaseDepth->getWidth();
1414  Int heightDepth = pPicBaseDepth->getHeight();
1415#endif
1416
1417#if H_3D_VSP_CONSTRAINED
1418  Int widthDepth  = pPicBaseDepth->getWidth();
1419  Int heightDepth = pPicBaseDepth->getHeight();
1420#endif
1421
1422  Int nTxtPerDepthX = widthLuma  / ( pPicBaseDepth->getWidth() );  // texture pixel # per depth pixel
1423  Int nTxtPerDepthY = heightLuma / ( pPicBaseDepth->getHeight() );
1424
1425  Int refStride = refPic->getStride();
1426  Int dstStride = dstPic->getStride();
1427  Int depStride =  pPicBaseDepth->getStride();
1428  Int depthPosX = Clip3(0,   widthLuma - sizeX,  (posX/nTxtPerDepthX) + ((mv->getHor()+2)>>2));
1429  Int depthPosY = Clip3(0,   heightLuma- sizeY,  (posY/nTxtPerDepthY) + ((mv->getVer()+2)>>2));
1430  Pel *ref    = refPic->getLumaAddr() + posX + posY * refStride;
1431  Pel *dst    = dstPic->getLumaAddr(partAddr);
1432  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;
1433
1434#if H_3D_VSP_BLOCKSIZE != 1
1435#if H_3D_VSP_BLOCKSIZE == 2
1436  Int  dW = sizeX>>1;
1437  Int  dH = sizeY>>1;
1438#endif
1439#if H_3D_VSP_BLOCKSIZE == 4
1440  Int  dW = sizeX>>2;
1441  Int  dH = sizeY>>2;
1442#endif
1443  {
1444    Pel* depthi = depth;
1445    for (Int j = 0; j < dH; j++)
1446    {
1447      for (Int i = 0; i < dW; i++)
1448      {
1449        Pel* depthTmp;
1450#if H_3D_VSP_BLOCKSIZE == 2
1451        if (depthPosX + (i<<1) < widthDepth)
1452          depthTmp = depthi + (i << 1);
1453        else
1454          depthTmp = depthi + (widthDepth - depthPosX - 1);
1455#endif
1456#if H_3D_VSP_BLOCKSIZE == 4
1457        if (depthPosX + (i<<2) < widthDepth)
1458          depthTmp = depthi + (i << 2);
1459        else
1460          depthTmp = depthi + (widthDepth - depthPosX - 1);
1461#endif
1462        Int maxV = 0;
1463        for (Int blockj = 0; blockj < H_3D_VSP_BLOCKSIZE; blockj+=(H_3D_VSP_BLOCKSIZE-1))
1464        {
1465          Int iX = 0;
1466          for (Int blocki = 0; blocki < H_3D_VSP_BLOCKSIZE; blocki+=(H_3D_VSP_BLOCKSIZE-1))
1467          {
1468            if (maxV < depthTmp[iX])
1469              maxV = depthTmp[iX];
1470#if H_3D_VSP_BLOCKSIZE == 2
1471            if (depthPosX + (i<<1) + blocki < widthDepth - 1)
1472#else // H_3D_VSP_BLOCKSIZE == 4
1473            if (depthPosX + (i<<2) + blocki < widthDepth - 1)
1474#endif
1475              iX = (H_3D_VSP_BLOCKSIZE-1);
1476          }
1477#if H_3D_VSP_BLOCKSIZE == 2
1478          if (depthPosY + (j<<1) + blockj < heightDepth - 1)
1479#else // H_3D_VSP_BLOCKSIZE == 4
1480          if (depthPosY + (j<<2) + blockj < heightDepth - 1)
1481#endif
1482            depthTmp += depStride * (H_3D_VSP_BLOCKSIZE-1);
1483        }
1484        m_pDepthBlock[i+j*dW] = maxV;
1485      } // end of i < dW
1486#if H_3D_VSP_BLOCKSIZE == 2
1487      if (depthPosY + ((j+1)<<1) < heightDepth)
1488        depthi += (depStride << 1);
1489      else
1490        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
1491#endif
1492#if H_3D_VSP_BLOCKSIZE == 4
1493      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
1494        depthi += (depStride << 2);
1495      else
1496        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
1497#endif
1498    }
1499  }
1500#endif // H_3D_VSP_BLOCKSIZE != 1
1501
1502#if H_3D_VSP_BLOCKSIZE == 1
1503#if H_3D_VSP_CONSTRAINED
1504  //get LUT based horizontal reference range
1505  Int range = xGetConstrainedSize(sizeX, sizeY);
1506
1507  // The minimum depth value
1508  Int minRelativePos = MAX_INT;
1509  Int maxRelativePos = MIN_INT;
1510
1511  Pel* depthTemp, *depthInitial=depth;
1512  for (Int yTxt =0; yTxt<sizeY; yTxt++)
1513  {
1514    for (Int xTxt =0; xTxt<sizeX; xTxt++)
1515    {
1516      if (depthPosX+xTxt < widthDepth)
1517        depthTemp = depthInitial + xTxt;
1518      else
1519        depthTemp = depthInitial + (widthDepth - depthPosX - 1);
1520
1521      Int disparity = pShiftLUT[ *depthTemp ]; // << iShiftPrec;
1522      Int disparityInt = disparity >> 2;
1523
1524      if( disparity <= 0)
1525      {
1526        if (minRelativePos > disparityInt+xTxt)
1527            minRelativePos = disparityInt+xTxt;
1528      }
1529      else
1530      {
1531        if (maxRelativePos < disparityInt+xTxt)
1532            maxRelativePos = disparityInt+xTxt;
1533      }
1534    }
1535    if (depthPosY+yTxt < heightDepth)
1536      depthInitial = depthInitial + depStride;
1537  }
1538
1539  Int disparity_tmp = pShiftLUT[ *depth ]; // << iShiftPrec;
1540  if (disparity_tmp <= 0)
1541    maxRelativePos = minRelativePos + range -1 ;
1542  else
1543    minRelativePos = maxRelativePos - range +1 ;
1544#endif
1545#endif // H_3D_VSP_BLOCKSIZE == 1
1546
1547#if H_3D_VSP_BLOCKSIZE != 1
1548  Int yDepth = 0;
1549#endif
1550  for ( Int yTxt = 0; yTxt < sizeY; yTxt += nTxtPerDepthY )
1551  {
1552    for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth++ )
1553    {
1554      Pel repDepth = 0; // to store the depth value used for warping
1555#if H_3D_VSP_BLOCKSIZE == 1
1556      repDepth = depth[xDepth];
1557#endif
1558#if H_3D_VSP_BLOCKSIZE == 2
1559      repDepth = m_pDepthBlock[(xTxt>>1) + (yTxt>>1)*dW];
1560#endif
1561#if H_3D_VSP_BLOCKSIZE == 4
1562      repDepth = m_pDepthBlock[(xTxt>>2) + (yTxt>>2)*dW];
1563#endif
1564
1565      assert( repDepth >= 0 && repDepth <= 255 );
1566      Int disparity = pShiftLUT[ repDepth ]; // remove << iShiftPrec ??
1567      Int refOffset = xTxt + (disparity >> 2);
1568      Int xFrac = disparity & 0x3;
1569#if H_3D_VSP_CONSTRAINED
1570      if(refOffset<minRelativePos || refOffset>maxRelativePos)
1571        xFrac = 0;
1572      refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
1573#endif
1574      Int absX  = posX + refOffset;
1575
1576      if (xFrac == 0)
1577        absX = Clip3(0, widthLuma-1, absX);
1578      else
1579        absX = Clip3(4, widthLuma-5, absX);
1580
1581      refOffset = absX - posX;
1582
1583      assert( ref[refOffset] >= 0 && ref[refOffset]<= 255 );
1584      m_if.filterHorLuma( &ref[refOffset], refStride, &dst[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi );
1585
1586    }
1587    ref   += refStride*nTxtPerDepthY;
1588    dst   += dstStride*nTxtPerDepthY;
1589    depth += depStride;
1590#if H_3D_VSP_BLOCKSIZE != 1
1591    yDepth++;
1592#endif
1593
1594  }
1595}
1596
1597Void TComPrediction::xPredInterChromaBlkFromDM ( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, TComMv*mv, UInt partAddr, Int posX, Int posY
1598                                               , Int sizeX, Int sizeY, Bool isDepth, TComYuv *&dstPic, Bool bi)
1599{
1600  Int refStride = refPic->getCStride();
1601  Int dstStride = dstPic->getCStride();
1602  Int depStride = pPicBaseDepth->getStride();
1603
1604  Int widthChroma, heightChroma;
1605  if( isDepth)
1606  {
1607     widthChroma   = pPicBaseDepth->getWidth()>>1;
1608     heightChroma  = pPicBaseDepth->getHeight()>>1;
1609  }
1610  else
1611  {
1612     widthChroma   = refPic->getWidth()>>1;
1613     heightChroma  = refPic->getHeight()>>1;
1614  }
1615
1616  // Below is only for Texture chroma component
1617
1618  Int widthDepth  = pPicBaseDepth->getWidth();
1619  Int heightDepth = pPicBaseDepth->getHeight();
1620
1621  Int nTxtPerDepthX, nTxtPerDepthY;  // Number of texture samples per one depth sample
1622  Int nDepthPerTxtX, nDepthPerTxtY;  // Number of depth samples per one texture sample
1623
1624  Int depthPosX;  // Starting position in depth image
1625  Int depthPosY;
1626
1627  if ( widthChroma > widthDepth )
1628  {
1629    nTxtPerDepthX = widthChroma / widthDepth;
1630    nDepthPerTxtX = 1;
1631    depthPosX = posX / nTxtPerDepthX + ((mv->getHor()+2)>>2);
1632  }
1633  else
1634  {
1635    nTxtPerDepthX = 1;
1636    nDepthPerTxtX = widthDepth / widthChroma;
1637    depthPosX = posX * nDepthPerTxtX + ((mv->getHor()+2)>>2);
1638  }
1639  depthPosX = Clip3(0, widthDepth - (sizeX<<1), depthPosX);
1640  if ( heightChroma > heightDepth )
1641  {
1642    nTxtPerDepthY = heightChroma / heightDepth;
1643    nDepthPerTxtY = 1;
1644    depthPosY = posY / nTxtPerDepthY + ((mv->getVer()+2)>>2);
1645  }
1646  else
1647  {
1648    nTxtPerDepthY = 1;
1649    nDepthPerTxtY = heightDepth / heightChroma;
1650    depthPosY = posY * nDepthPerTxtY + ((mv->getVer()+2)>>2);
1651  }
1652  depthPosY = Clip3(0, heightDepth - (sizeY<<1), depthPosY);
1653
1654  Pel *refCb  = refPic->getCbAddr() + posX + posY * refStride;
1655  Pel *refCr  = refPic->getCrAddr() + posX + posY * refStride;
1656  Pel *dstCb  = dstPic->getCbAddr(partAddr);
1657  Pel *dstCr  = dstPic->getCrAddr(partAddr);
1658  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;  // move the pointer to the current depth pixel position
1659
1660  Int refStrideBlock = refStride * nTxtPerDepthY;
1661  Int dstStrideBlock = dstStride * nTxtPerDepthY;
1662  Int depStrideBlock = depStride * nDepthPerTxtY;
1663
1664  if ( widthChroma > widthDepth ) // We assume
1665  {
1666    assert( heightChroma > heightDepth );
1667    printf("This branch should never been reached.\n");
1668    exit(0);
1669  }
1670  else
1671  {
1672#if H_3D_VSP_BLOCKSIZE == 1
1673  Int  dW = sizeX;
1674  Int  dH = sizeY;
1675  Int  sW = 2; // search window size
1676  Int  sH = 2;
1677#endif
1678#if H_3D_VSP_BLOCKSIZE == 2
1679  Int  dW = sizeX;
1680  Int  dH = sizeY;
1681  Int  sW = 2; // search window size
1682  Int  sH = 2;
1683#endif
1684#if H_3D_VSP_BLOCKSIZE == 4
1685  Int  dW = sizeX>>1;
1686  Int  dH = sizeY>>1;
1687  Int  sW = 4; // search window size
1688  Int  sH = 4;
1689#endif
1690
1691  {
1692    Pel* depthi = depth;
1693    for (Int j = 0; j < dH; j++)
1694    {
1695      for (Int i = 0; i < dW; i++)
1696      {
1697        Pel* depthTmp;
1698#if H_3D_VSP_BLOCKSIZE == 1
1699        depthTmp = depthi + (i << 1);
1700#endif
1701#if H_3D_VSP_BLOCKSIZE == 2
1702        if (depthPosX + (i<<1) < widthDepth)
1703          depthTmp = depthi + (i << 1);
1704        else
1705          depthTmp = depthi + (widthDepth - depthPosX - 1);
1706#endif
1707#if H_3D_VSP_BLOCKSIZE == 4
1708        if (depthPosX + (i<<2) < widthDepth)
1709          depthTmp = depthi + (i << 2);
1710        else
1711          depthTmp = depthi + (widthDepth - depthPosX - 1);
1712#endif
1713        Int maxV = 0;
1714        for (Int blockj = 0; blockj < sH; blockj+=(sH-1))
1715        {
1716          Int iX = 0;
1717          for (Int blocki = 0; blocki < sW; blocki+=(sW-1))
1718          {
1719            if (maxV < depthTmp[iX])
1720              maxV = depthTmp[iX];
1721            if (depthPosX + i*sW + blocki < widthDepth - 1)
1722                iX = (sW-1);
1723          }
1724          if (depthPosY + j*sH + blockj < heightDepth - 1)
1725                depthTmp += depStride * (sH-1);
1726        }
1727        m_pDepthBlock[i+j*dW] = maxV;
1728      } // end of i < dW
1729#if H_3D_VSP_BLOCKSIZE == 1
1730      if (depthPosY + ((j+1)<<1) < heightDepth)
1731        depthi += (depStride << 1);
1732      else
1733        depthi  = depth + (heightDepth-1)*depStride;
1734#endif
1735#if H_3D_VSP_BLOCKSIZE == 2
1736      if (depthPosY + ((j+1)<<1) < heightDepth)
1737        depthi += (depStride << 1);
1738      else
1739        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
1740#endif
1741#if H_3D_VSP_BLOCKSIZE == 4
1742      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
1743        depthi += (depStride << 2);
1744      else
1745        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
1746#endif
1747    }
1748  }
1749
1750
1751#if H_3D_VSP_BLOCKSIZE == 1
1752#if H_3D_VSP_CONSTRAINED
1753  //get LUT based horizontal reference range
1754  Int range = xGetConstrainedSize(sizeX, sizeY, false);
1755
1756  // The minimum depth value
1757  Int minRelativePos = MAX_INT;
1758  Int maxRelativePos = MIN_INT;
1759
1760  Int depthTmp;
1761  for (Int yTxt=0; yTxt<sizeY; yTxt++)
1762  {
1763    for (Int xTxt=0; xTxt<sizeX; xTxt++)
1764    {
1765      depthTmp = m_pDepthBlock[xTxt+yTxt*dW];
1766      Int disparity = pShiftLUT[ depthTmp ]; // << iShiftPrec;
1767      Int disparityInt = disparity >> 3;//in chroma resolution
1768
1769      if (disparityInt < 0)
1770      {
1771        if (minRelativePos > disparityInt+xTxt)
1772            minRelativePos = disparityInt+xTxt;
1773      }
1774      else
1775      {
1776        if (maxRelativePos < disparityInt+xTxt)
1777            maxRelativePos = disparityInt+xTxt;
1778      }
1779    }
1780  }
1781
1782  depthTmp = m_pDepthBlock[0];
1783  Int disparity_tmp = pShiftLUT[ depthTmp ]; // << iShiftPrec;
1784  if ( disparity_tmp < 0 )
1785    maxRelativePos = minRelativePos + range - 1;
1786  else
1787    minRelativePos = maxRelativePos - range + 1;
1788
1789#endif // H_3D_VSP_CONSTRAINED
1790#endif // H_3D_VSP_BLOCKSIZE == 1
1791
1792    // (sizeX, sizeY) is Chroma block size
1793    for ( Int yTxt = 0, yDepth = 0; yTxt < sizeY; yTxt += nTxtPerDepthY, yDepth += nDepthPerTxtY )
1794    {
1795      for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth += nDepthPerTxtX )
1796      {
1797        Pel repDepth = 0; // to store the depth value used for warping
1798#if H_3D_VSP_BLOCKSIZE == 1
1799        repDepth = m_pDepthBlock[(xTxt) + (yTxt)*dW];
1800#endif
1801#if H_3D_VSP_BLOCKSIZE == 2
1802        repDepth = m_pDepthBlock[(xTxt) + (yTxt)*dW];
1803#endif
1804#if H_3D_VSP_BLOCKSIZE == 4
1805        repDepth = m_pDepthBlock[(xTxt>>1) + (yTxt>>1)*dW];
1806#endif
1807
1808      // calculate the offset in the reference picture
1809        Int disparity = pShiftLUT[ repDepth ]; // Remove << iShiftPrec;
1810        Int refOffset = xTxt + (disparity >> 3); // in integer pixel in chroma image
1811        Int xFrac = disparity & 0x7;
1812#if H_3D_VSP_CONSTRAINED
1813        if(refOffset < minRelativePos || refOffset > maxRelativePos)
1814          xFrac = 0;
1815        refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
1816#endif
1817        Int absX  = posX + refOffset;
1818
1819        if (xFrac == 0)
1820          absX = Clip3(0, widthChroma-1, absX);
1821        else
1822          absX = Clip3(4, widthChroma-5, absX);
1823
1824        refOffset = absX - posX;
1825
1826        assert( refCb[refOffset] >= 0 && refCb[refOffset]<= 255 );
1827        assert( refCr[refOffset] >= 0 && refCr[refOffset]<= 255 );
1828        m_if.filterHorChroma(&refCb[refOffset], refStride, &dstCb[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
1829        m_if.filterHorChroma(&refCr[refOffset], refStride, &dstCr[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
1830      }
1831      refCb += refStrideBlock;
1832      refCr += refStrideBlock;
1833      dstCb += dstStrideBlock;
1834      dstCr += dstStrideBlock;
1835      depth += depStrideBlock;
1836    }
1837  }
1838
1839}
1840
1841#if H_3D_VSP_CONSTRAINED
1842
1843Int TComPrediction::xGetConstrainedSize(Int nPbW, Int nPbH, Bool bLuma)
1844{
1845  Int iSize = 0;
1846  if (bLuma)
1847  {
1848    Int iArea = (nPbW+7) * (nPbH+7);
1849    Int iAlpha = iArea / nPbH - nPbW - 7;
1850    iSize = iAlpha + nPbW;
1851  }
1852  else // chroma
1853  {
1854    Int iArea = (nPbW+2) * (nPbH+2);
1855    Int iAlpha = iArea / nPbH - nPbW - 4;
1856    iSize = iAlpha + nPbW;
1857  }
1858  return iSize;
1859}
1860
1861#endif
1862
1863
1864#endif
1865
1866
1867//! \}
Note: See TracBrowser for help on using the repository browser.