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

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

-VSP remaining stuff wrapped up.
-4x4 VSP in effect.

  • Property svn:eol-style set to native
File size: 56.9 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-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
440  if ( iPartIdx >= 0 )
441  {
442    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
443#if H_3D_VSP
444    if ( 0 == pcCU->getVSPFlag(uiPartAddr) )
445    {
446#endif
447      if ( eRefPicList != REF_PIC_LIST_X )
448      {
449        if( pcCU->getSlice()->getPPS()->getUseWP())
450        {
451          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
452        }
453        else
454        {
455          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
456        }
457        if ( pcCU->getSlice()->getPPS()->getUseWP() )
458        {
459          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
460        }
461      }
462      else
463      {
464        if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
465        {
466          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
467        }
468        else
469        {
470          xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
471        }
472      }
473#if H_3D_VSP
474    }
475    else
476    {
477      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
478        xPredInterUniVSP( pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
479      else
480        xPredInterBiVSP ( pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
481    }
482#endif
483    return;
484  }
485
486  for ( iPartIdx = 0; iPartIdx < pcCU->getNumPartInter(); iPartIdx++ )
487  {
488    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iWidth, iHeight );
489
490#if H_3D_VSP
491    if ( 0 == pcCU->getVSPFlag(uiPartAddr) )
492    {
493#endif
494      if ( eRefPicList != REF_PIC_LIST_X )
495      {
496        if( pcCU->getSlice()->getPPS()->getUseWP())
497        {
498          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred, true );
499        }
500        else
501        {
502          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
503        }
504        if ( pcCU->getSlice()->getPPS()->getUseWP() )
505        {
506          xWeightedPredictionUni( pcCU, pcYuvPred, uiPartAddr, iWidth, iHeight, eRefPicList, pcYuvPred );
507        }
508      }
509      else
510      {
511        if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
512        {
513          xPredInterUni (pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
514        }
515        else
516        {
517          xPredInterBi  (pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
518        }
519      }
520#if H_3D_VSP
521    }
522    else
523    {
524      if ( xCheckIdenticalMotion( pcCU, uiPartAddr ) )
525        xPredInterUniVSP( pcCU, uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, pcYuvPred );
526      else
527        xPredInterBiVSP ( pcCU, uiPartAddr, iWidth, iHeight, pcYuvPred );
528    }
529#endif
530  }
531  return;
532}
533
534Void TComPrediction::xPredInterUni ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
535{
536  Int         iRefIdx     = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           assert (iRefIdx >= 0);
537  TComMv      cMv         = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
538  pcCU->clipMv(cMv);
539#if H_3D_ARP
540  if(  pcCU->getARPW( uiPartAddr ) > 0 
541    && pcCU->getPartitionSize(uiPartAddr)==SIZE_2Nx2N
542    && pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPOC()!= pcCU->getSlice()->getPOC() 
543    )
544  {
545    xPredInterUniARP( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, rpcYuvPred, bi );
546  }
547  else
548  {
549#endif
550#if H_3D_IC
551    Bool bICFlag = pcCU->getICFlag( uiPartAddr ) && ( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getViewIndex() != pcCU->getSlice()->getViewIndex() );
552    xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
553#if H_3D_ARP
554      , false
555#endif
556      , bICFlag );
557    xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi
558#if H_3D_ARP
559      , false
560#endif
561      , bICFlag );
562#else
563  xPredInterLumaBlk  ( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
564  xPredInterChromaBlk( pcCU, pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec(), uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi );
565#endif
566#if H_3D_ARP
567  }
568#endif
569}
570
571#if H_3D_VSP
572Void TComPrediction::xPredInterUniVSP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi )
573{
574  // Get depth reference
575  Int depthRefViewIdx = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan;
576  TComPic* pRefPicBaseDepth = pcCU->getSlice()->getIvPic (true, depthRefViewIdx );
577  assert(pRefPicBaseDepth != NULL);
578  TComPicYuv* pcBaseViewDepthPicYuv = pRefPicBaseDepth->getPicYuvRec();
579  assert(pcBaseViewDepthPicYuv != NULL);
580
581  // Get texture reference
582  Int iRefIdx = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
583  assert(iRefIdx >= 0);
584  TComPic* pRefPicBaseTxt = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx );
585  TComPicYuv* pcBaseViewTxtPicYuv = pRefPicBaseTxt->getPicYuvRec();
586  assert(pcBaseViewTxtPicYuv != NULL);
587
588  // Initialize LUT according to the reference viewIdx
589  Int txtRefViewIdx = pRefPicBaseTxt->getViewIndex();
590  Int* pShiftLUT    = pcCU->getSlice()->getDepthToDisparityB( txtRefViewIdx );
591  assert( txtRefViewIdx < pcCU->getSlice()->getViewIndex() );
592
593  // Do compensation
594  TComMv cDv  = pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr ); // cDv is the disparity vector derived from the neighbors
595  pcCU->clipMv(cDv);
596  UInt uiAbsPartIdx = pcCU->getZorderIdxInCU();
597  Int iBlkX = ( pcCU->getAddr() % pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUWidth  + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
598  Int iBlkY = ( pcCU->getAddr() / pRefPicBaseDepth->getFrameWidthInCU() ) * g_uiMaxCUHeight + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
599  xPredInterLumaBlkFromDM  ( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, &cDv, uiPartAddr, iBlkX,    iBlkY,    iWidth,    iHeight,    pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
600  xPredInterChromaBlkFromDM( pcBaseViewTxtPicYuv, pcBaseViewDepthPicYuv, pShiftLUT, &cDv, uiPartAddr, iBlkX>>1, iBlkY>>1, iWidth>>1, iHeight>>1, pcCU->getSlice()->getIsDepth(), rpcYuvPred, bi );
601}
602#endif
603
604#if H_3D_ARP
605Void TComPrediction::xPredInterUniARP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Bool bi, TComMvField * pNewMvFiled )
606{
607  Int         iRefIdx      = pNewMvFiled ? pNewMvFiled->getRefIdx() : pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );           
608  TComMv      cMv          = pNewMvFiled ? pNewMvFiled->getMv()     : pcCU->getCUMvField( eRefPicList )->getMv( uiPartAddr );
609  Bool        bTobeScaled  = false;
610  TComPic* pcPicYuvBaseCol = NULL;
611  TComPic* pcPicYuvBaseRef = NULL;
612
613#if H_3D_NBDV
614  DisInfo cDistparity;
615  cDistparity.bDV           = pcCU->getDvInfo(uiPartAddr).bDV;
616  if( cDistparity.bDV )
617  {
618    cDistparity.m_acNBDV = pcCU->getDvInfo(0).m_acNBDV;
619    assert(pcCU->getDvInfo(uiPartAddr).bDV ==  pcCU->getDvInfo(0).bDV);
620    cDistparity.m_aVIdxCan = pcCU->getDvInfo(uiPartAddr).m_aVIdxCan;
621  }
622#else
623  assert(0); // ARP can be applied only when a DV is available
624#endif
625
626  UChar dW = cDistparity.bDV ? pcCU->getARPW ( uiPartAddr ) : 0;
627
628  if( cDistparity.bDV ) 
629  {
630    if( dW > 0 && pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC()!= pcCU->getSlice()->getPOC() )
631    {
632      bTobeScaled = true;
633    }
634
635    pcPicYuvBaseCol =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getPOC(),                              cDistparity.m_aVIdxCan );
636    pcPicYuvBaseRef =  pcCU->getSlice()->getBaseViewRefPic( pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC(), cDistparity.m_aVIdxCan );
637   
638    if( ( !pcPicYuvBaseCol || pcPicYuvBaseCol->getPOC() != pcCU->getSlice()->getPOC() ) || ( !pcPicYuvBaseRef || pcPicYuvBaseRef->getPOC() != pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() ) )
639    {
640      dW = 0;
641      bTobeScaled = false;
642    }
643    else
644    {
645      assert( pcPicYuvBaseCol->getPOC() == pcCU->getSlice()->getPOC() && pcPicYuvBaseRef->getPOC() == pcCU->getSlice()->getRefPic( eRefPicList, 0 )->getPOC() );
646    }
647
648    if(bTobeScaled)
649    {     
650      Int iCurrPOC    = pcCU->getSlice()->getPOC();
651      Int iColRefPOC  = pcCU->getSlice()->getRefPOC( eRefPicList, iRefIdx );
652      Int iCurrRefPOC = pcCU->getSlice()->getRefPOC( eRefPicList,  0);
653      Int iScale = pcCU-> xGetDistScaleFactor(iCurrPOC, iCurrRefPOC, iCurrPOC, iColRefPOC);
654      if ( iScale != 4096 )
655      {
656        cMv = cMv.scaleMv( iScale );
657      }
658      iRefIdx = 0;
659    }
660  }
661
662  pcCU->clipMv(cMv);
663  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec();
664  xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, true );
665  xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMv, iWidth, iHeight, rpcYuvPred, bi, true );
666
667  if( dW > 0 )
668  {
669    TComYuv * pYuvB0 = &m_acYuvPredBase[0];
670    TComYuv * pYuvB1  = &m_acYuvPredBase[1];
671
672    TComMv cMVwithDisparity = cMv + cDistparity.m_acNBDV;
673    pcCU->clipMv(cMVwithDisparity);
674
675    assert ( cDistparity.bDV );
676
677    pcPicYuvRef = pcPicYuvBaseCol->getPicYuvRec();
678    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acNBDV, iWidth, iHeight, pYuvB0, bi, true );
679    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cDistparity.m_acNBDV, iWidth, iHeight, pYuvB0, bi, true );
680   
681    pcPicYuvRef = pcPicYuvBaseRef->getPicYuvRec();
682    xPredInterLumaBlk  ( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, bi, true );
683    xPredInterChromaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMVwithDisparity, iWidth, iHeight, pYuvB1, bi, true );
684
685    pYuvB0->subtractARP( pYuvB0 , pYuvB1 , uiPartAddr , iWidth , iHeight );
686
687    if( 2 == dW )
688    {
689      pYuvB0->multiplyARP( uiPartAddr , iWidth , iHeight , dW );
690    }
691
692    rpcYuvPred->addARP( rpcYuvPred , pYuvB0 , uiPartAddr , iWidth , iHeight , !bi );
693  }
694}
695#endif
696
697Void TComPrediction::xPredInterBi ( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
698{
699  TComYuv* pcMbYuv;
700  Int      iRefIdx[2] = {-1, -1};
701
702  for ( Int iRefList = 0; iRefList < 2; iRefList++ )
703  {
704    RefPicList eRefPicList = (iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
705    iRefIdx[iRefList] = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
706
707    if ( iRefIdx[iRefList] < 0 )
708    {
709      continue;
710    }
711
712    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
713
714    pcMbYuv = &m_acYuvPred[iRefList];
715    if( pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0 )
716    {
717      xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
718    }
719    else
720    {
721      if ( ( pcCU->getSlice()->getPPS()->getUseWP()       && pcCU->getSlice()->getSliceType() == P_SLICE ) || 
722           ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE ) )
723      {
724        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, true );
725      }
726      else
727      {
728        xPredInterUni ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv );
729      }
730    }
731  }
732
733  if ( pcCU->getSlice()->getPPS()->getWPBiPred() && pcCU->getSlice()->getSliceType() == B_SLICE  )
734  {
735    xWeightedPredictionBi( pcCU, &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
736  } 
737  else if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType() == P_SLICE )
738  {
739    xWeightedPredictionUni( pcCU, &m_acYuvPred[0], uiPartAddr, iWidth, iHeight, REF_PIC_LIST_0, rpcYuvPred ); 
740  }
741  else
742  {
743    xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
744  }
745}
746
747#if H_3D_VSP
748
749Void TComPrediction::xPredInterBiVSP( TComDataCU* pcCU, UInt uiPartAddr, Int iWidth, Int iHeight, TComYuv*& rpcYuvPred )
750{
751  TComYuv* pcMbYuv;
752  Int      iRefIdx[2] = {-1, -1};
753  Bool     bi = (pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiPartAddr ) >= 0 && pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiPartAddr ) >= 0);
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    assert( iRefIdx[iRefList] < pcCU->getSlice()->getNumRefIdx(eRefPicList) );
763
764    pcMbYuv = &m_acYuvPred[iRefList];
765    xPredInterUniVSP ( pcCU, uiPartAddr, iWidth, iHeight, eRefPicList, pcMbYuv, bi );
766  }
767
768  xWeightedAverage( &m_acYuvPred[0], &m_acYuvPred[1], iRefIdx[0], iRefIdx[1], uiPartAddr, iWidth, iHeight, rpcYuvPred );
769}
770
771#endif
772
773/**
774 * \brief Generate motion-compensated luma block
775 *
776 * \param cu       Pointer to current CU
777 * \param refPic   Pointer to reference picture
778 * \param partAddr Address of block within CU
779 * \param mv       Motion vector
780 * \param width    Width of block
781 * \param height   Height of block
782 * \param dstPic   Pointer to destination picture
783 * \param bi       Flag indicating whether bipred is used
784 */
785Void TComPrediction::xPredInterLumaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
786#if H_3D_ARP
787    , Bool filterType
788#endif
789#if H_3D_IC
790    , Bool bICFlag
791#endif
792  )
793{
794  Int refStride = refPic->getStride(); 
795  Int refOffset = ( mv->getHor() >> 2 ) + ( mv->getVer() >> 2 ) * refStride;
796  Pel *ref      = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
797 
798  Int dstStride = dstPic->getStride();
799  Pel *dst      = dstPic->getLumaAddr( partAddr );
800 
801  Int xFrac = mv->getHor() & 0x3;
802  Int yFrac = mv->getVer() & 0x3;
803
804#if H_3D_IC
805  if( cu->getSlice()->getIsDepth() )
806  {
807    refOffset = mv->getHor() + mv->getVer() * refStride;
808    ref       = refPic->getLumaAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
809    xFrac     = 0;
810    yFrac     = 0;
811  }
812#endif
813
814  if ( yFrac == 0 )
815  {
816    m_if.filterHorLuma( ref, refStride, dst, dstStride, width, height, xFrac,       !bi
817#if H_3D_ARP
818    , filterType
819#endif
820      );
821  }
822  else if ( xFrac == 0 )
823  {
824    m_if.filterVerLuma( ref, refStride, dst, dstStride, width, height, yFrac, true, !bi
825#if H_3D_ARP
826    , filterType
827#endif
828      );
829  }
830  else
831  {
832    Int tmpStride = m_filteredBlockTmp[0].getStride();
833    Short *tmp    = m_filteredBlockTmp[0].getLumaAddr();
834
835    Int filterSize = NTAPS_LUMA;
836    Int halfFilterSize = ( filterSize >> 1 );
837
838    m_if.filterHorLuma(ref - (halfFilterSize-1)*refStride, refStride, tmp, tmpStride, width, height+filterSize-1, xFrac, false     
839#if H_3D_ARP
840    , filterType
841#endif
842      );
843    m_if.filterVerLuma(tmp + (halfFilterSize-1)*tmpStride, tmpStride, dst, dstStride, width, height,              yFrac, false, !bi
844#if H_3D_ARP
845    , filterType
846#endif
847      );   
848  }
849
850#if H_3D_IC
851  if( bICFlag )
852  {
853    Int a, b, iShift, i, j;
854
855    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_LUMA );
856
857    for ( i = 0; i < height; i++ )
858    {
859      for ( j = 0; j < width; j++ )
860      {
861        if( bi )
862        {
863          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthY;
864          dst[j] = ( ( a*dst[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1 << iIFshift ) - IF_INTERNAL_OFFS;
865        }
866        else
867          dst[j] = Clip3( 0, ( 1 << g_bitDepthY ) - 1, ( ( a*dst[j] ) >> iShift ) + b );
868      }
869      dst += dstStride;
870    }
871  }
872#endif
873}
874
875/**
876 * \brief Generate motion-compensated chroma block
877 *
878 * \param cu       Pointer to current CU
879 * \param refPic   Pointer to reference picture
880 * \param partAddr Address of block within CU
881 * \param mv       Motion vector
882 * \param width    Width of block
883 * \param height   Height of block
884 * \param dstPic   Pointer to destination picture
885 * \param bi       Flag indicating whether bipred is used
886 */
887Void TComPrediction::xPredInterChromaBlk( TComDataCU *cu, TComPicYuv *refPic, UInt partAddr, TComMv *mv, Int width, Int height, TComYuv *&dstPic, Bool bi
888#if H_3D_ARP
889    , Bool filterType
890#endif
891#if H_3D_IC
892    , Bool bICFlag
893#endif
894  )
895{
896  Int     refStride  = refPic->getCStride();
897  Int     dstStride  = dstPic->getCStride();
898 
899  Int     refOffset  = (mv->getHor() >> 3) + (mv->getVer() >> 3) * refStride;
900 
901  Pel*    refCb     = refPic->getCbAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
902  Pel*    refCr     = refPic->getCrAddr( cu->getAddr(), cu->getZorderIdxInCU() + partAddr ) + refOffset;
903 
904  Pel* dstCb = dstPic->getCbAddr( partAddr );
905  Pel* dstCr = dstPic->getCrAddr( partAddr );
906 
907  Int     xFrac  = mv->getHor() & 0x7;
908  Int     yFrac  = mv->getVer() & 0x7;
909  UInt    cxWidth  = width  >> 1;
910  UInt    cxHeight = height >> 1;
911 
912  Int     extStride = m_filteredBlockTmp[0].getStride();
913  Short*  extY      = m_filteredBlockTmp[0].getLumaAddr();
914 
915  Int filterSize = NTAPS_CHROMA;
916 
917  Int halfFilterSize = (filterSize>>1);
918 
919  if ( yFrac == 0 )
920  {
921    m_if.filterHorChroma(refCb, refStride, dstCb,  dstStride, cxWidth, cxHeight, xFrac, !bi
922#if H_3D_ARP
923    , filterType
924#endif
925    );   
926    m_if.filterHorChroma(refCr, refStride, dstCr,  dstStride, cxWidth, cxHeight, xFrac, !bi
927#if H_3D_ARP
928    , filterType
929#endif
930    );
931  }
932  else if ( xFrac == 0 )
933  {
934    m_if.filterVerChroma(refCb, refStride, dstCb, dstStride, cxWidth, cxHeight, yFrac, true, !bi
935#if H_3D_ARP
936    , filterType
937#endif
938    );
939    m_if.filterVerChroma(refCr, refStride, dstCr, dstStride, cxWidth, cxHeight, yFrac, true, !bi
940#if H_3D_ARP
941    , filterType
942#endif
943    );
944  }
945  else
946  {
947    m_if.filterHorChroma(refCb - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
948#if H_3D_ARP
949    , filterType
950#endif 
951      );
952    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCb, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
953#if H_3D_ARP
954    , filterType
955#endif
956      );
957   
958    m_if.filterHorChroma(refCr - (halfFilterSize-1)*refStride, refStride, extY,  extStride, cxWidth, cxHeight+filterSize-1, xFrac, false
959#if H_3D_ARP
960    , filterType
961#endif
962      );
963    m_if.filterVerChroma(extY  + (halfFilterSize-1)*extStride, extStride, dstCr, dstStride, cxWidth, cxHeight  , yFrac, false, !bi
964#if H_3D_ARP
965    , filterType
966#endif
967      );   
968  }
969
970#if H_3D_IC
971  if( bICFlag )
972  {
973    Int a, b, iShift, i, j;
974    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_CHROMA_U ); // Cb
975    for ( i = 0; i < cxHeight; i++ )
976    {
977      for ( j = 0; j < cxWidth; j++ )
978      {
979        if( bi )
980        {
981          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthC;
982          dstCb[j] = ( ( a*dstCb[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1<<iIFshift ) - IF_INTERNAL_OFFS;
983        }
984        else
985          dstCb[j] = Clip3(  0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCb[j] ) >> iShift ) + b );
986      }
987      dstCb += dstStride;
988    }
989    xGetLLSICPrediction( cu, mv, refPic, a, b, iShift, TEXT_CHROMA_V ); // Cr
990    for ( i = 0; i < cxHeight; i++ )
991    {
992      for ( j = 0; j < cxWidth; j++ )
993      {
994        if( bi )
995        {
996          Int iIFshift = IF_INTERNAL_PREC - g_bitDepthC;
997          dstCr[j] = ( ( a*dstCr[j] + a*IF_INTERNAL_OFFS ) >> iShift ) + b*( 1<<iIFshift ) - IF_INTERNAL_OFFS;
998        }
999        else
1000          dstCr[j] = Clip3( 0, ( 1 << g_bitDepthC ) - 1, ( ( a*dstCr[j] ) >> iShift ) + b );
1001      }
1002      dstCr += dstStride;
1003    }
1004  }
1005#endif
1006}
1007
1008Void TComPrediction::xWeightedAverage( TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, Int iRefIdx0, Int iRefIdx1, UInt uiPartIdx, Int iWidth, Int iHeight, TComYuv*& rpcYuvDst )
1009{
1010  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
1011  {
1012    rpcYuvDst->addAvg( pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight );
1013  }
1014  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
1015  {
1016    pcYuvSrc0->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
1017  }
1018  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
1019  {
1020    pcYuvSrc1->copyPartToPartYuv( rpcYuvDst, uiPartIdx, iWidth, iHeight );
1021  }
1022}
1023
1024// AMVP
1025Void TComPrediction::getMvPredAMVP( TComDataCU* pcCU, UInt uiPartIdx, UInt uiPartAddr, RefPicList eRefPicList, TComMv& rcMvPred )
1026{
1027  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
1028  if( pcAMVPInfo->iN <= 1 )
1029  {
1030    rcMvPred = pcAMVPInfo->m_acMvCand[0];
1031
1032    pcCU->setMVPIdxSubParts( 0, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
1033    pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
1034    return;
1035  }
1036
1037  assert(pcCU->getMVPIdx(eRefPicList,uiPartAddr) >= 0);
1038  rcMvPred = pcAMVPInfo->m_acMvCand[pcCU->getMVPIdx(eRefPicList,uiPartAddr)];
1039  return;
1040}
1041
1042/** Function for deriving planar intra prediction.
1043 * \param pSrc pointer to reconstructed sample array
1044 * \param srcStride the stride of the reconstructed sample array
1045 * \param rpDst reference to pointer for the prediction sample array
1046 * \param dstStride the stride of the prediction sample array
1047 * \param width the width of the block
1048 * \param height the height of the block
1049 *
1050 * This function derives the prediction samples for planar mode (intra coding).
1051 */
1052Void TComPrediction::xPredIntraPlanar( Int* pSrc, Int srcStride, Pel* rpDst, Int dstStride, UInt width, UInt height )
1053{
1054  assert(width == height);
1055
1056  Int k, l, bottomLeft, topRight;
1057  Int horPred;
1058  Int leftColumn[MAX_CU_SIZE], topRow[MAX_CU_SIZE], bottomRow[MAX_CU_SIZE], rightColumn[MAX_CU_SIZE];
1059  UInt blkSize = width;
1060  UInt offset2D = width;
1061  UInt shift1D = g_aucConvertToBit[ width ] + 2;
1062  UInt shift2D = shift1D + 1;
1063
1064  // Get left and above reference column and row
1065  for(k=0;k<blkSize+1;k++)
1066  {
1067    topRow[k] = pSrc[k-srcStride];
1068    leftColumn[k] = pSrc[k*srcStride-1];
1069  }
1070
1071  // Prepare intermediate variables used in interpolation
1072  bottomLeft = leftColumn[blkSize];
1073  topRight   = topRow[blkSize];
1074  for (k=0;k<blkSize;k++)
1075  {
1076    bottomRow[k]   = bottomLeft - topRow[k];
1077    rightColumn[k] = topRight   - leftColumn[k];
1078    topRow[k]      <<= shift1D;
1079    leftColumn[k]  <<= shift1D;
1080  }
1081
1082  // Generate prediction signal
1083  for (k=0;k<blkSize;k++)
1084  {
1085    horPred = leftColumn[k] + offset2D;
1086    for (l=0;l<blkSize;l++)
1087    {
1088      horPred += rightColumn[k];
1089      topRow[l] += bottomRow[l];
1090      rpDst[k*dstStride+l] = ( (horPred + topRow[l]) >> shift2D );
1091    }
1092  }
1093}
1094
1095/** Function for filtering intra DC predictor.
1096 * \param pSrc pointer to reconstructed sample array
1097 * \param iSrcStride the stride of the reconstructed sample array
1098 * \param rpDst reference to pointer for the prediction sample array
1099 * \param iDstStride the stride of the prediction sample array
1100 * \param iWidth the width of the block
1101 * \param iHeight the height of the block
1102 *
1103 * This function performs filtering left and top edges of the prediction samples for DC mode (intra coding).
1104 */
1105Void TComPrediction::xDCPredFiltering( Int* pSrc, Int iSrcStride, Pel*& rpDst, Int iDstStride, Int iWidth, Int iHeight )
1106{
1107  Pel* pDst = rpDst;
1108  Int x, y, iDstStride2, iSrcStride2;
1109
1110  // boundary pixels processing
1111  pDst[0] = (Pel)((pSrc[-iSrcStride] + pSrc[-1] + 2 * pDst[0] + 2) >> 2);
1112
1113  for ( x = 1; x < iWidth; x++ )
1114  {
1115    pDst[x] = (Pel)((pSrc[x - iSrcStride] +  3 * pDst[x] + 2) >> 2);
1116  }
1117
1118  for ( y = 1, iDstStride2 = iDstStride, iSrcStride2 = iSrcStride-1; y < iHeight; y++, iDstStride2+=iDstStride, iSrcStride2+=iSrcStride )
1119  {
1120    pDst[iDstStride2] = (Pel)((pSrc[iSrcStride2] + 3 * pDst[iDstStride2] + 2) >> 2);
1121  }
1122
1123  return;
1124}
1125
1126#if H_3D_IC
1127/** Function for deriving the position of first non-zero binary bit of a value
1128 * \param x input value
1129 *
1130 * This function derives the position of first non-zero binary bit of a value
1131 */
1132Int GetMSB( UInt x )
1133{
1134  Int iMSB = 0, bits = ( sizeof( Int ) << 3 ), y = 1;
1135
1136  while( x > 1 )
1137  {
1138    bits >>= 1;
1139    y = x >> bits;
1140
1141    if( y )
1142    {
1143      x = y;
1144      iMSB += bits;
1145    }
1146  }
1147
1148  iMSB+=y;
1149
1150  return iMSB;
1151}
1152
1153/** Function for counting leading number of zeros/ones
1154 * \param x input value
1155 \ This function counts leading number of zeros for positive numbers and
1156 \ leading number of ones for negative numbers. This can be implemented in
1157 \ single instructure cycle on many processors.
1158 */
1159
1160Short CountLeadingZerosOnes (Short x)
1161{
1162  Short clz;
1163  Short i;
1164
1165  if(x == 0)
1166  {
1167    clz = 0;
1168  }
1169  else
1170  {
1171    if (x == -1)
1172    {
1173      clz = 15;
1174    }
1175    else
1176    {
1177      if(x < 0)
1178      {
1179        x = ~x;
1180      }
1181      clz = 15;
1182      for(i = 0;i < 15;++i)
1183      {
1184        if(x) 
1185        {
1186          clz --;
1187        }
1188        x = x >> 1;
1189      }
1190    }
1191  }
1192  return clz;
1193}
1194
1195/** Function for deriving LM illumination compensation.
1196 */
1197Void TComPrediction::xGetLLSICPrediction( TComDataCU* pcCU, TComMv *pMv, TComPicYuv *pRefPic, Int &a, Int &b, Int &iShift, TextType eType )
1198{
1199  TComPicYuv *pRecPic = pcCU->getPic()->getPicYuvRec();
1200  Pel *pRec = NULL, *pRef = NULL;
1201  UInt uiWidth, uiHeight, uiTmpPartIdx;
1202  Int iRecStride = ( eType == TEXT_LUMA ) ? pRecPic->getStride() : pRecPic->getCStride();
1203  Int iRefStride = ( eType == TEXT_LUMA ) ? pRefPic->getStride() : pRefPic->getCStride();
1204  Int iCUPelX, iCUPelY, iRefX, iRefY, iRefOffset, iHor, iVer;
1205
1206  iCUPelX = pcCU->getCUPelX() + g_auiRasterToPelX[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
1207  iCUPelY = pcCU->getCUPelY() + g_auiRasterToPelY[g_auiZscanToRaster[pcCU->getZorderIdxInCU()]];
1208  iHor = pcCU->getSlice()->getIsDepth() ? pMv->getHor() : ( ( pMv->getHor() + 2 ) >> 2 );
1209  iVer = pcCU->getSlice()->getIsDepth() ? pMv->getVer() : ( ( pMv->getVer() + 2 ) >> 2 );
1210  iRefX   = iCUPelX + iHor;
1211  iRefY   = iCUPelY + iVer;
1212  if( eType != TEXT_LUMA )
1213  {
1214    iHor = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getHor() + 1 ) >> 1 ) : ( ( pMv->getHor() + 4 ) >> 3 );
1215    iVer = pcCU->getSlice()->getIsDepth() ? ( ( pMv->getVer() + 1 ) >> 1 ) : ( ( pMv->getVer() + 4 ) >> 3 );
1216  }
1217  uiWidth  = ( eType == TEXT_LUMA ) ? pcCU->getWidth( 0 )  : ( pcCU->getWidth( 0 )  >> 1 );
1218  uiHeight = ( eType == TEXT_LUMA ) ? pcCU->getHeight( 0 ) : ( pcCU->getHeight( 0 ) >> 1 );
1219
1220  Int i, j, iCountShift = 0;
1221
1222  // LLS parameters estimation -->
1223
1224  Int x = 0, y = 0, xx = 0, xy = 0;
1225
1226  if( pcCU->getPUAbove( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) && iCUPelY > 0 && iRefY > 0 )
1227  {
1228    iRefOffset = iHor + iVer * iRefStride - iRefStride;
1229    if( eType == TEXT_LUMA )
1230    {
1231      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1232      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1233    }
1234    else if( eType == TEXT_CHROMA_U )
1235    {
1236      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1237      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1238    }
1239    else
1240    {
1241      assert( eType == TEXT_CHROMA_V );
1242      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1243      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - iRecStride;
1244    }
1245
1246    for( j = 0; j < uiWidth; j++ )
1247    {
1248      x += pRef[j];
1249      y += pRec[j];
1250      xx += pRef[j] * pRef[j];
1251      xy += pRef[j] * pRec[j];
1252    }
1253    iCountShift += g_aucConvertToBit[ uiWidth ] + 2;
1254  }
1255
1256
1257  if( pcCU->getPULeft( uiTmpPartIdx, pcCU->getZorderIdxInCU() ) && iCUPelX > 0 && iRefX > 0 )
1258  {
1259    iRefOffset = iHor + iVer * iRefStride - 1;
1260    if( eType == TEXT_LUMA )
1261    {
1262      pRef = pRefPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1263      pRec = pRecPic->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1264    }
1265    else if( eType == TEXT_CHROMA_U )
1266    {
1267      pRef = pRefPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1268      pRec = pRecPic->getCbAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1269    }
1270    else
1271    {
1272      assert( eType == TEXT_CHROMA_V );
1273      pRef = pRefPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) + iRefOffset;
1274      pRec = pRecPic->getCrAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() ) - 1;
1275    }
1276
1277    for( i = 0; i < uiHeight; i++ )
1278    {
1279      x += pRef[0];
1280      y += pRec[0];
1281      xx += pRef[0] * pRef[0];
1282      xy += pRef[0] * pRec[0];
1283
1284      pRef += iRefStride;
1285      pRec += iRecStride;
1286    }
1287    iCountShift += iCountShift > 0 ? 1 : ( g_aucConvertToBit[ uiWidth ] + 2 );
1288  }
1289
1290  Int iTempShift = ( ( eType == TEXT_LUMA ) ? g_bitDepthY : g_bitDepthC ) + g_aucConvertToBit[ uiWidth ] + 3 - 15;
1291
1292  if( iTempShift > 0 )
1293  {
1294    x  = ( x +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1295    y  = ( y +  ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1296    xx = ( xx + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1297    xy = ( xy + ( 1 << ( iTempShift - 1 ) ) ) >> iTempShift;
1298    iCountShift -= iTempShift;
1299  }
1300
1301  iShift = 13;
1302
1303  if( iCountShift == 0 )
1304  {
1305    a = 1;
1306    b = 0;
1307    iShift = 0;
1308  }
1309  else
1310  {
1311    Int a1 = ( xy << iCountShift ) - y * x;
1312    Int a2 = ( xx << iCountShift ) - x * x;             
1313
1314    {
1315      const Int iShiftA2 = 6;
1316      const Int iShiftA1 = 15;
1317      const Int iAccuracyShift = 15;
1318
1319      Int iScaleShiftA2 = 0;
1320      Int iScaleShiftA1 = 0;
1321      Int a1s = a1;
1322      Int a2s = a2;
1323
1324      iScaleShiftA1 = GetMSB( abs( a1 ) ) - iShiftA1;
1325      iScaleShiftA2 = GetMSB( abs( a2 ) ) - iShiftA2; 
1326
1327      if( iScaleShiftA1 < 0 )
1328      {
1329        iScaleShiftA1 = 0;
1330      }
1331
1332      if( iScaleShiftA2 < 0 )
1333      {
1334        iScaleShiftA2 = 0;
1335      }
1336
1337      Int iScaleShiftA = iScaleShiftA2 + iAccuracyShift - iShift - iScaleShiftA1;
1338
1339      a2s = a2 >> iScaleShiftA2;
1340
1341      a1s = a1 >> iScaleShiftA1;
1342
1343      if (a2s >= 1)
1344      {
1345        a = a1s * m_uiaShift[ a2s - 1];
1346      }
1347      else
1348      {
1349        a = 0;
1350      }
1351
1352      if( iScaleShiftA < 0 )
1353      {
1354        a = a << -iScaleShiftA;
1355      }
1356      else
1357      {
1358        a = a >> iScaleShiftA;
1359      }
1360
1361      a = Clip3( -( 1 << 15 ), ( 1 << 15 ) - 1, a ); 
1362
1363      Int minA = -(1 << (6));
1364      Int maxA = (1 << 6) - 1;
1365      if( a <= maxA && a >= minA )
1366      {
1367        // do nothing
1368      }
1369      else
1370      {
1371        Short n = CountLeadingZerosOnes( a );
1372        a = a >> (9-n);
1373        iShift -= (9-n);
1374      }
1375
1376      b = (  y - ( ( a * x ) >> iShift ) + ( 1 << ( iCountShift - 1 ) ) ) >> iCountShift;
1377    }
1378  }   
1379}
1380#endif
1381
1382#if H_3D_VSP
1383// Input:
1384// refPic: Ref picture. Full picture, with padding
1385// posX, posY:     PU position, texture
1386// sizeX, sizeY: PU size
1387// partAddr: z-order index
1388// dv: disparity vector. derived from neighboring blocks
1389//
1390// Output: dstPic, PU predictor 64x64
1391Void TComPrediction::xPredInterLumaBlkFromDM( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, TComMv* dv, UInt partAddr,Int posX, Int posY
1392                                            , Int sizeX, Int sizeY, Bool isDepth, TComYuv *&dstPic, Bool bi )
1393{
1394  Int widthLuma;
1395  Int heightLuma;
1396
1397  if (isDepth)
1398  {
1399    widthLuma   =  pPicBaseDepth->getWidth();
1400    heightLuma  =  pPicBaseDepth->getHeight();
1401  }
1402  else
1403  {
1404    widthLuma   =  refPic->getWidth();
1405    heightLuma  =  refPic->getHeight();
1406  }
1407
1408#if H_3D_VSP_BLOCKSIZE != 1
1409  Int widthDepth  = pPicBaseDepth->getWidth();
1410  Int heightDepth = pPicBaseDepth->getHeight();
1411#endif
1412
1413#if H_3D_VSP_CONSTRAINED
1414  Int widthDepth  = pPicBaseDepth->getWidth();
1415  Int heightDepth = pPicBaseDepth->getHeight();
1416#endif
1417
1418  Int nTxtPerDepthX = widthLuma  / ( pPicBaseDepth->getWidth() );  // texture pixel # per depth pixel
1419  Int nTxtPerDepthY = heightLuma / ( pPicBaseDepth->getHeight() );
1420
1421  Int refStride = refPic->getStride();
1422  Int dstStride = dstPic->getStride();
1423  Int depStride =  pPicBaseDepth->getStride();
1424  Int depthPosX = Clip3(0,   widthLuma - sizeX,  (posX/nTxtPerDepthX) + ((dv->getHor()+2)>>2));
1425  Int depthPosY = Clip3(0,   heightLuma- sizeY,  (posY/nTxtPerDepthY) + ((dv->getVer()+2)>>2));
1426  Pel *ref    = refPic->getLumaAddr() + posX + posY * refStride;
1427  Pel *dst    = dstPic->getLumaAddr(partAddr);
1428  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;
1429
1430#if H_3D_VSP_BLOCKSIZE != 1
1431#if H_3D_VSP_BLOCKSIZE == 2
1432  Int  dW = sizeX>>1;
1433  Int  dH = sizeY>>1;
1434#endif
1435#if H_3D_VSP_BLOCKSIZE == 4
1436  Int  dW = sizeX>>2;
1437  Int  dH = sizeY>>2;
1438#endif
1439  {
1440    Pel* depthi = depth;
1441    for (Int j = 0; j < dH; j++)
1442    {
1443      for (Int i = 0; i < dW; i++)
1444      {
1445        Pel* depthTmp;
1446#if H_3D_VSP_BLOCKSIZE == 2
1447        if (depthPosX + (i<<1) < widthDepth)
1448          depthTmp = depthi + (i << 1);
1449        else
1450          depthTmp = depthi + (widthDepth - depthPosX - 1);
1451#endif
1452#if H_3D_VSP_BLOCKSIZE == 4
1453        if (depthPosX + (i<<2) < widthDepth)
1454          depthTmp = depthi + (i << 2);
1455        else
1456          depthTmp = depthi + (widthDepth - depthPosX - 1);
1457#endif
1458        Int maxV = 0;
1459        for (Int blockj = 0; blockj < H_3D_VSP_BLOCKSIZE; blockj+=(H_3D_VSP_BLOCKSIZE-1))
1460        {
1461          Int iX = 0;
1462          for (Int blocki = 0; blocki < H_3D_VSP_BLOCKSIZE; blocki+=(H_3D_VSP_BLOCKSIZE-1))
1463          {
1464            if (maxV < depthTmp[iX])
1465              maxV = depthTmp[iX];
1466#if H_3D_VSP_BLOCKSIZE == 2
1467            if (depthPosX + (i<<1) + blocki < widthDepth - 1)
1468#else // H_3D_VSP_BLOCKSIZE == 4
1469            if (depthPosX + (i<<2) + blocki < widthDepth - 1)
1470#endif
1471              iX = (H_3D_VSP_BLOCKSIZE-1);
1472          }
1473#if H_3D_VSP_BLOCKSIZE == 2
1474          if (depthPosY + (j<<1) + blockj < heightDepth - 1)
1475#else // H_3D_VSP_BLOCKSIZE == 4
1476          if (depthPosY + (j<<2) + blockj < heightDepth - 1)
1477#endif
1478            depthTmp += depStride * (H_3D_VSP_BLOCKSIZE-1);
1479        }
1480        m_pDepthBlock[i+j*dW] = maxV;
1481      } // end of i < dW
1482#if H_3D_VSP_BLOCKSIZE == 2
1483      if (depthPosY + ((j+1)<<1) < heightDepth)
1484        depthi += (depStride << 1);
1485      else
1486        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
1487#endif
1488#if H_3D_VSP_BLOCKSIZE == 4
1489      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
1490        depthi += (depStride << 2);
1491      else
1492        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
1493#endif
1494    }
1495  }
1496#endif // H_3D_VSP_BLOCKSIZE != 1
1497
1498#if H_3D_VSP_BLOCKSIZE == 1
1499#if H_3D_VSP_CONSTRAINED
1500  //get LUT based horizontal reference range
1501  Int range = xGetConstrainedSize(sizeX, sizeY);
1502
1503  // The minimum depth value
1504  Int minRelativePos = MAX_INT;
1505  Int maxRelativePos = MIN_INT;
1506
1507  Pel* depthTemp, *depthInitial=depth;
1508  for (Int yTxt = 0; yTxt < sizeY; yTxt++)
1509  {
1510    for (Int xTxt = 0; xTxt < sizeX; xTxt++)
1511    {
1512      if (depthPosX+xTxt < widthDepth)
1513        depthTemp = depthInitial + xTxt;
1514      else
1515        depthTemp = depthInitial + (widthDepth - depthPosX - 1);
1516
1517      Int disparity = pShiftLUT[ *depthTemp ]; // << iShiftPrec;
1518      Int disparityInt = disparity >> 2;
1519
1520      if( disparity <= 0)
1521      {
1522        if (minRelativePos > disparityInt+xTxt)
1523            minRelativePos = disparityInt+xTxt;
1524      }
1525      else
1526      {
1527        if (maxRelativePos < disparityInt+xTxt)
1528            maxRelativePos = disparityInt+xTxt;
1529      }
1530    }
1531    if (depthPosY+yTxt < heightDepth)
1532      depthInitial = depthInitial + depStride;
1533  }
1534
1535  Int disparity_tmp = pShiftLUT[ *depth ]; // << iShiftPrec;
1536  if (disparity_tmp <= 0)
1537    maxRelativePos = minRelativePos + range -1 ;
1538  else
1539    minRelativePos = maxRelativePos - range +1 ;
1540#endif
1541#endif // H_3D_VSP_BLOCKSIZE == 1
1542
1543#if H_3D_VSP_BLOCKSIZE != 1
1544  Int yDepth = 0;
1545#endif
1546  for ( Int yTxt = 0; yTxt < sizeY; yTxt += nTxtPerDepthY )
1547  {
1548    for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth++ )
1549    {
1550      Pel repDepth = 0; // to store the depth value used for warping
1551#if H_3D_VSP_BLOCKSIZE == 1
1552      repDepth = depth[xDepth];
1553#endif
1554#if H_3D_VSP_BLOCKSIZE == 2
1555      repDepth = m_pDepthBlock[(xTxt>>1) + (yTxt>>1)*dW];
1556#endif
1557#if H_3D_VSP_BLOCKSIZE == 4
1558      repDepth = m_pDepthBlock[(xTxt>>2) + (yTxt>>2)*dW];
1559#endif
1560
1561      assert( repDepth >= 0 && repDepth <= 255 );
1562      Int disparity = pShiftLUT[ repDepth ]; // remove << iShiftPrec ??
1563      Int refOffset = xTxt + (disparity >> 2);
1564      Int xFrac = disparity & 0x3;
1565#if H_3D_VSP_CONSTRAINED
1566      if(refOffset<minRelativePos || refOffset>maxRelativePos)
1567        xFrac = 0;
1568      refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
1569#endif
1570      Int absX  = posX + refOffset;
1571
1572      if (xFrac == 0)
1573        absX = Clip3(0, widthLuma-1, absX);
1574      else
1575        absX = Clip3(4, widthLuma-5, absX);
1576
1577      refOffset = absX - posX;
1578
1579      assert( ref[refOffset] >= 0 && ref[refOffset]<= 255 );
1580      m_if.filterHorLuma( &ref[refOffset], refStride, &dst[xTxt], dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi );
1581    }
1582    ref   += refStride*nTxtPerDepthY;
1583    dst   += dstStride*nTxtPerDepthY;
1584    depth += depStride;
1585#if H_3D_VSP_BLOCKSIZE != 1
1586    yDepth++;
1587#endif
1588
1589  }
1590}
1591
1592Void TComPrediction::xPredInterChromaBlkFromDM ( TComPicYuv *refPic, TComPicYuv *pPicBaseDepth, Int* pShiftLUT, TComMv*dv, UInt partAddr, Int posX, Int posY
1593                                               , Int sizeX, Int sizeY, Bool isDepth, TComYuv *&dstPic, Bool bi)
1594{
1595  Int refStride = refPic->getCStride();
1596  Int dstStride = dstPic->getCStride();
1597  Int depStride = pPicBaseDepth->getStride();
1598
1599  Int widthChroma, heightChroma;
1600  if( isDepth)
1601  {
1602     widthChroma   = pPicBaseDepth->getWidth()>>1;
1603     heightChroma  = pPicBaseDepth->getHeight()>>1;
1604  }
1605  else
1606  {
1607     widthChroma   = refPic->getWidth()>>1;
1608     heightChroma  = refPic->getHeight()>>1;
1609  }
1610
1611  // Below is only for Texture chroma component
1612
1613  Int widthDepth  = pPicBaseDepth->getWidth();
1614  Int heightDepth = pPicBaseDepth->getHeight();
1615
1616  Int nTxtPerDepthX, nTxtPerDepthY;  // Number of texture samples per one depth sample
1617  Int nDepthPerTxtX, nDepthPerTxtY;  // Number of depth samples per one texture sample
1618
1619  Int depthPosX;  // Starting position in depth image
1620  Int depthPosY;
1621
1622  if ( widthChroma > widthDepth )
1623  {
1624    nTxtPerDepthX = widthChroma / widthDepth;
1625    nDepthPerTxtX = 1;
1626    depthPosX = posX / nTxtPerDepthX + ((dv->getHor()+2)>>2);
1627  }
1628  else
1629  {
1630    nTxtPerDepthX = 1;
1631    nDepthPerTxtX = widthDepth / widthChroma;
1632    depthPosX = posX * nDepthPerTxtX + ((dv->getHor()+2)>>2);
1633  }
1634  depthPosX = Clip3(0, widthDepth - (sizeX<<1), depthPosX);
1635  if ( heightChroma > heightDepth )
1636  {
1637    nTxtPerDepthY = heightChroma / heightDepth;
1638    nDepthPerTxtY = 1;
1639    depthPosY = posY / nTxtPerDepthY + ((dv->getVer()+2)>>2);
1640  }
1641  else
1642  {
1643    nTxtPerDepthY = 1;
1644    nDepthPerTxtY = heightDepth / heightChroma;
1645    depthPosY = posY * nDepthPerTxtY + ((dv->getVer()+2)>>2);
1646  }
1647  depthPosY = Clip3(0, heightDepth - (sizeY<<1), depthPosY);
1648
1649  Pel *refCb  = refPic->getCbAddr() + posX + posY * refStride;
1650  Pel *refCr  = refPic->getCrAddr() + posX + posY * refStride;
1651  Pel *dstCb  = dstPic->getCbAddr(partAddr);
1652  Pel *dstCr  = dstPic->getCrAddr(partAddr);
1653  Pel *depth  = pPicBaseDepth->getLumaAddr() + depthPosX + depthPosY * depStride;  // move the pointer to the current depth pixel position
1654
1655  Int refStrideBlock = refStride * nTxtPerDepthY;
1656  Int dstStrideBlock = dstStride * nTxtPerDepthY;
1657  Int depStrideBlock = depStride * nDepthPerTxtY;
1658
1659  if ( widthChroma > widthDepth ) // We assume
1660  {
1661    assert( heightChroma > heightDepth );
1662    printf("This branch should never been reached.\n");
1663    exit(0);
1664  }
1665  else
1666  {
1667#if H_3D_VSP_BLOCKSIZE == 1
1668  Int  dW = sizeX;
1669  Int  dH = sizeY;
1670  Int  sW = 2; // search window size
1671  Int  sH = 2;
1672#endif
1673#if H_3D_VSP_BLOCKSIZE == 2
1674  Int  dW = sizeX;
1675  Int  dH = sizeY;
1676  Int  sW = 2; // search window size
1677  Int  sH = 2;
1678#endif
1679#if H_3D_VSP_BLOCKSIZE == 4
1680  Int  dW = sizeX>>1;
1681  Int  dH = sizeY>>1;
1682  Int  sW = 4; // search window size
1683  Int  sH = 4;
1684#endif
1685
1686  {
1687    Pel* depthi = depth;
1688    for (Int j = 0; j < dH; j++)
1689    {
1690      for (Int i = 0; i < dW; i++)
1691      {
1692        Pel* depthTmp;
1693#if H_3D_VSP_BLOCKSIZE == 1
1694        depthTmp = depthi + (i << 1);
1695#endif
1696#if H_3D_VSP_BLOCKSIZE == 2
1697        if (depthPosX + (i<<1) < widthDepth)
1698          depthTmp = depthi + (i << 1);
1699        else
1700          depthTmp = depthi + (widthDepth - depthPosX - 1);
1701#endif
1702#if H_3D_VSP_BLOCKSIZE == 4
1703        if (depthPosX + (i<<2) < widthDepth)
1704          depthTmp = depthi + (i << 2);
1705        else
1706          depthTmp = depthi + (widthDepth - depthPosX - 1);
1707#endif
1708        Int maxV = 0;
1709        for (Int blockj = 0; blockj < sH; blockj+=(sH-1))
1710        {
1711          Int iX = 0;
1712          for (Int blocki = 0; blocki < sW; blocki+=(sW-1))
1713          {
1714            if (maxV < depthTmp[iX])
1715              maxV = depthTmp[iX];
1716            if (depthPosX + i*sW + blocki < widthDepth - 1)
1717                iX = (sW-1);
1718          }
1719          if (depthPosY + j*sH + blockj < heightDepth - 1)
1720                depthTmp += depStride * (sH-1);
1721        }
1722        m_pDepthBlock[i+j*dW] = maxV;
1723      } // end of i < dW
1724#if H_3D_VSP_BLOCKSIZE == 1
1725      if (depthPosY + ((j+1)<<1) < heightDepth)
1726        depthi += (depStride << 1);
1727      else
1728        depthi  = depth + (heightDepth-1)*depStride;
1729#endif
1730#if H_3D_VSP_BLOCKSIZE == 2
1731      if (depthPosY + ((j+1)<<1) < heightDepth)
1732        depthi += (depStride << 1);
1733      else
1734        depthi  = depth + (heightDepth-depthPosY-1)*depStride;
1735#endif
1736#if H_3D_VSP_BLOCKSIZE == 4
1737      if (depthPosY + ((j+1)<<2) < heightDepth) // heightDepth-1
1738        depthi += (depStride << 2);
1739      else
1740        depthi  = depth + (heightDepth-depthPosY-1)*depStride; // the last line
1741#endif
1742    }
1743  }
1744
1745
1746#if H_3D_VSP_BLOCKSIZE == 1
1747#if H_3D_VSP_CONSTRAINED
1748  //get LUT based horizontal reference range
1749  Int range = xGetConstrainedSize(sizeX, sizeY, false);
1750
1751  // The minimum depth value
1752  Int minRelativePos = MAX_INT;
1753  Int maxRelativePos = MIN_INT;
1754
1755  Int depthTmp;
1756  for (Int yTxt=0; yTxt<sizeY; yTxt++)
1757  {
1758    for (Int xTxt=0; xTxt<sizeX; xTxt++)
1759    {
1760      depthTmp = m_pDepthBlock[xTxt+yTxt*dW];
1761      Int disparity = pShiftLUT[ depthTmp ]; // << iShiftPrec;
1762      Int disparityInt = disparity >> 3;//in chroma resolution
1763
1764      if (disparityInt < 0)
1765      {
1766        if (minRelativePos > disparityInt+xTxt)
1767            minRelativePos = disparityInt+xTxt;
1768      }
1769      else
1770      {
1771        if (maxRelativePos < disparityInt+xTxt)
1772            maxRelativePos = disparityInt+xTxt;
1773      }
1774    }
1775  }
1776
1777  depthTmp = m_pDepthBlock[0];
1778  Int disparity_tmp = pShiftLUT[ depthTmp ]; // << iShiftPrec;
1779  if ( disparity_tmp < 0 )
1780    maxRelativePos = minRelativePos + range - 1;
1781  else
1782    minRelativePos = maxRelativePos - range + 1;
1783
1784#endif // H_3D_VSP_CONSTRAINED
1785#endif // H_3D_VSP_BLOCKSIZE == 1
1786
1787    // (sizeX, sizeY) is Chroma block size
1788    for ( Int yTxt = 0, yDepth = 0; yTxt < sizeY; yTxt += nTxtPerDepthY, yDepth += nDepthPerTxtY )
1789    {
1790      for ( Int xTxt = 0, xDepth = 0; xTxt < sizeX; xTxt += nTxtPerDepthX, xDepth += nDepthPerTxtX )
1791      {
1792        Pel repDepth = 0; // to store the depth value used for warping
1793#if H_3D_VSP_BLOCKSIZE == 1
1794        repDepth = m_pDepthBlock[(xTxt) + (yTxt)*dW];
1795#endif
1796#if H_3D_VSP_BLOCKSIZE == 2
1797        repDepth = m_pDepthBlock[(xTxt) + (yTxt)*dW];
1798#endif
1799#if H_3D_VSP_BLOCKSIZE == 4
1800        repDepth = m_pDepthBlock[(xTxt>>1) + (yTxt>>1)*dW];
1801#endif
1802
1803      // calculate the offset in the reference picture
1804        Int disparity = pShiftLUT[ repDepth ]; // Remove << iShiftPrec;
1805        Int refOffset = xTxt + (disparity >> 3); // in integer pixel in chroma image
1806        Int xFrac = disparity & 0x7;
1807#if H_3D_VSP_CONSTRAINED
1808        if(refOffset < minRelativePos || refOffset > maxRelativePos)
1809          xFrac = 0;
1810        refOffset = Clip3(minRelativePos, maxRelativePos, refOffset);
1811#endif
1812        Int absX  = posX + refOffset;
1813
1814        if (xFrac == 0)
1815          absX = Clip3(0, widthChroma-1, absX);
1816        else
1817          absX = Clip3(4, widthChroma-5, absX);
1818
1819        refOffset = absX - posX;
1820
1821        assert( refCb[refOffset] >= 0 && refCb[refOffset]<= 255 );
1822        assert( refCr[refOffset] >= 0 && refCr[refOffset]<= 255 );
1823        m_if.filterHorChroma(&refCb[refOffset], refStride, &dstCb[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
1824        m_if.filterHorChroma(&refCr[refOffset], refStride, &dstCr[xTxt],  dstStride, nTxtPerDepthX, nTxtPerDepthY, xFrac, !bi);
1825      }
1826      refCb += refStrideBlock;
1827      refCr += refStrideBlock;
1828      dstCb += dstStrideBlock;
1829      dstCr += dstStrideBlock;
1830      depth += depStrideBlock;
1831    }
1832  }
1833
1834}
1835
1836#if H_3D_VSP_CONSTRAINED
1837Int TComPrediction::xGetConstrainedSize(Int nPbW, Int nPbH, Bool bLuma)
1838{
1839  Int iSize = 0;
1840  if (bLuma)
1841  {
1842    Int iArea = (nPbW+7) * (nPbH+7);
1843    Int iAlpha = iArea / nPbH - nPbW - 7;
1844    iSize = iAlpha + nPbW;
1845  }
1846  else // chroma
1847  {
1848    Int iArea = (nPbW+2) * (nPbH+2);
1849    Int iAlpha = iArea / nPbH - nPbW - 4;
1850    iSize = iAlpha + nPbW;
1851  }
1852  return iSize;
1853}
1854#endif // H_3D_VSP_CONSTRAINED
1855
1856#endif // H_3D_VSP
1857
1858//! \}
Note: See TracBrowser for help on using the repository browser.