source: 3DVCSoftware/tags/0.2r1/source/Lib/TLibCommon/TComDepthMapGenerator.cpp @ 255

Last change on this file since 255 was 5, checked in by hhi, 13 years ago

Clean version with cfg-files

  • Property svn:eol-style set to native
File size: 59.5 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-2011, 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 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
35
36/** \file     TComDepthMapGenerator.cpp
37    \brief    depth map generator class
38*/
39
40
41
42#include "CommonDef.h"
43#include "TComDepthMapGenerator.h"
44
45
46#if DEPTH_MAP_GENERATION
47
48
49TComDepthMapGenerator::TComDepthMapGenerator()
50{
51  m_bCreated            = false;
52  m_bInit               = false;
53  m_bDecoder            = false;
54  m_pcPrediction        = 0;
55  m_pcSPSAccess         = 0;
56  m_pcAUPicAccess       = 0;
57  m_uiMaxDepth          = 0;
58  m_uiOrgDepthBitDepth  = 0;
59  m_ppcYuv              = 0;
60  m_ppcCU               = 0;
61}
62
63TComDepthMapGenerator::~TComDepthMapGenerator()
64{
65  destroy ();
66  uninit  ();
67}
68
69Void
70TComDepthMapGenerator::create( Bool bDecoder, UInt uiPicWidth, UInt uiPicHeight, UInt uiMaxCUDepth, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiOrgBitDepth )
71{
72  destroy();
73  m_bDecoder            = bDecoder;
74  m_uiMaxDepth          = uiMaxCUDepth;
75  m_uiOrgDepthBitDepth  = uiOrgBitDepth;
76  m_ppcYuv              = new TComYuv*    [ m_uiMaxDepth ];
77  m_ppcCU               = new TComDataCU* [ m_uiMaxDepth ];
78  for( UInt uiDepth = 0; uiDepth < m_uiMaxDepth; uiDepth++ )
79  {
80    UInt  uiNumPart = 1 << ( ( m_uiMaxDepth - uiDepth ) << 1 );
81    UInt  uiWidth   = uiMaxCUWidth  >> uiDepth;
82    UInt  uiHeight  = uiMaxCUHeight >> uiDepth;
83
84    m_ppcYuv[ uiDepth ] = new TComYuv;    m_ppcYuv[ uiDepth ]->create(            uiWidth, uiHeight       );
85    m_ppcCU [ uiDepth ] = new TComDataCU; m_ppcCU [ uiDepth ]->create( uiNumPart, uiWidth, uiHeight, true );
86  }
87  m_cTmpPic.create( uiPicWidth, uiPicHeight, uiMaxCUWidth, uiMaxCUHeight, uiMaxCUDepth );
88  xSetChroma( &m_cTmpPic, ( 1 << uiOrgBitDepth ) >> 1 );
89  m_bCreated    = true;
90}
91
92Void
93TComDepthMapGenerator::destroy()
94{
95  if( m_bCreated )
96  {
97    m_bCreated    = false;
98    for( UInt uiDepth = 0; uiDepth < m_uiMaxDepth; uiDepth++ )
99    {
100      m_ppcYuv[ uiDepth ]->destroy(); delete m_ppcYuv[ uiDepth ]; m_ppcYuv[ uiDepth ] = 0;
101      m_ppcCU [ uiDepth ]->destroy(); delete m_ppcCU [ uiDepth ]; m_ppcCU [ uiDepth ] = 0;
102    }
103    delete [] m_ppcYuv; m_ppcYuv = 0;
104    delete [] m_ppcCU;  m_ppcCU  = 0;
105    m_cTmpPic.destroy();
106    m_uiMaxDepth          = 0;
107    m_uiOrgDepthBitDepth  = 0;
108    m_bDecoder            = false;
109  }
110}
111
112Void
113TComDepthMapGenerator::init( TComPrediction* pcPrediction, TComSPSAccess* pcSPSAccess, TComAUPicAccess* pcAUPicAccess )
114{
115  AOF( pcPrediction  );
116  AOF( pcSPSAccess   );
117  AOF( pcAUPicAccess );
118  uninit();
119  m_pcPrediction  = pcPrediction;
120  m_pcSPSAccess   = pcSPSAccess;
121  m_pcAUPicAccess = pcAUPicAccess;
122  m_bInit         = true;
123}
124
125Void
126TComDepthMapGenerator::uninit()
127{
128  if( m_bInit )
129  {
130    m_bInit         = false;
131    m_pcPrediction  = 0;
132    m_pcSPSAccess   = 0;
133    m_pcAUPicAccess = 0;
134  }
135}
136
137Void 
138TComDepthMapGenerator::initViewComponent( TComPic* pcPic )
139{
140  AOF  ( m_bCreated && m_bInit );
141  AOF  ( pcPic );
142  AOT  ( pcPic->getSPS()->getViewId() && !pcPic->getSPS()->isDepth() && pcPic->getPOC() && pcPic->getSPS()->getPredDepthMapGeneration() != m_pcSPSAccess->getPdm() );
143  m_bPDMAvailable = false;
144  m_uiCurrViewId  = pcPic->getSPS()->getViewId();
145
146  // update SPS list and AU pic list and set depth map generator in SPS
147  m_pcSPSAccess  ->addSPS( pcPic->getSPS() );
148  m_pcAUPicAccess->addPic( pcPic );
149  pcPic->getSPS()->setDepthMapGenerator( this );
150
151  // check whether we have depth data or don't use pred depth prediction
152  ROFVS( pcPic->getSPS()->getViewId() );
153  ROTVS( pcPic->getSPS()->isDepth  () );
154  ROFVS( m_pcSPSAccess->getPdm     () );
155
156  // set basic SPS parameters
157  const Int iDisparityDir = 1; // 1 or -1, depending on the usage of disparity vectors
158  TComSPS*  pcSPS         = pcPic->getSPS                 ();
159  Int       iVOI          = pcSPS->getViewOrderIdx        ();
160  UInt      uiPdmPrec     = pcSPS->getPdmPrecision        ();
161  UInt      uiCamPrec     = pcSPS->getCamParPrecision     ();
162  Bool      bInSlice      = pcSPS->hasCamParInSliceHeader ();
163  Int       iScaleVOI01   = ( 1 << ( uiPdmPrec + PDM_INTER_CALC_SHIFT + PDM_VIRT_DEPTH_PRECISION - 2 ) );
164
165  // check availability of base views and set base id list
166  std::vector<Int>  aiAbsDeltaVOI;
167  for( UInt uiBaseId = 0; uiBaseId < m_uiCurrViewId; uiBaseId++ )
168  {
169    TComSPS*  pcBaseSPS     = m_pcSPSAccess  ->getSPS( uiBaseId );
170    TComPic*  pcBasePic     = m_pcAUPicAccess->getPic( uiBaseId );
171    AOF( pcBaseSPS != 0 && pcBasePic != 0 );
172    Int       iDeltaVOI     = iVOI - pcBaseSPS->getViewOrderIdx();
173    Int       iAbsDeltaVOI  = ( iDeltaVOI < 0 ? -iDeltaVOI : iDeltaVOI ); 
174    AOT( iAbsDeltaVOI == 0 );
175    aiAbsDeltaVOI.push_back( iAbsDeltaVOI );
176  }
177  m_auiBaseIdList.clear();
178  while( (UInt)m_auiBaseIdList.size() < m_uiCurrViewId )
179  {
180    Int       iMinAbsDelta  = MAX_INT;
181    UInt      uiNextBaseId  = MAX_NUMBER_VIEWS;
182    for( UInt uiBaseId = 0; uiBaseId < m_uiCurrViewId; uiBaseId++ )
183    {
184      if( aiAbsDeltaVOI[ uiBaseId ] > 0 && aiAbsDeltaVOI[ uiBaseId ] <= iMinAbsDelta )
185      {
186        iMinAbsDelta  = aiAbsDeltaVOI[ uiBaseId ];
187        uiNextBaseId  = uiBaseId;
188      }
189    }
190    m_auiBaseIdList.push_back( uiNextBaseId );
191    aiAbsDeltaVOI[ uiNextBaseId ] = 0;
192  }
193
194  // check availability of prediction depth map
195  if( m_uiCurrViewId )
196  {
197    Bool      bCheckVId1  = ( m_uiCurrViewId > 1 && m_auiBaseIdList[0] == 0 );
198    UInt      uiBaseVId   = ( bCheckVId1 ? 1 : m_auiBaseIdList[0] );
199    TComPic*  pcBasePic   = m_pcAUPicAccess->getPic( uiBaseVId );
200    SliceType eSliceType  = pcBasePic->getCurrSlice()->getSliceType();
201    Bool      bNoRAPdm    = ( pcPic->getSPS()->getPredDepthMapGeneration() == 1 );
202    m_bPDMAvailable       = ( eSliceType != I_SLICE || !bNoRAPdm );
203  }
204
205  // update disparity depth conversion parameters
206  for( UInt uiBaseId = 0; uiBaseId < m_uiCurrViewId; uiBaseId++ )
207  {
208    TComSPS*  pcBaseSPS   = m_pcSPSAccess->getSPS( uiBaseId );
209    Int       iBaseVOI    = pcBaseSPS->getViewOrderIdx();
210
211    // disparity -> virtual depth
212    Int       iVNominator = ( 1 << PDM_LOG4_SCALE_DENOMINATOR ) + pcSPS->getPdmScaleNomDelta()[ uiBaseId ];
213    Int       iVDiv       = iVOI - iBaseVOI;
214    Int       iVAdd       = ( iVDiv > 0 ? iVDiv / 2 : -iVDiv / 2 );
215    Int       iVScalePred = ( iScaleVOI01 + iVAdd ) / iVDiv;
216    Int       iVShift     = PDM_INTER_CALC_SHIFT;
217    Int       iVScale     = Int( ( (Int64)iVNominator * (Int64)iVScalePred + (Int64)( ( 1 << PDM_LOG4_SCALE_DENOMINATOR ) >> 1 ) ) >> PDM_LOG4_SCALE_DENOMINATOR );
218    Int       iVOffset    = pcSPS->getPdmOffset()[ uiBaseId ] << PDM_OFFSET_SHIFT;
219    m_aaiConvParsDisparity2VirtDepth[ uiBaseId ][ 0 ] = iDisparityDir * iVScale;
220    m_aaiConvParsDisparity2VirtDepth[ uiBaseId ][ 1 ] = iDisparityDir * iVOffset + ( ( 1 << iVShift ) >> 1 );
221    m_aaiConvParsDisparity2VirtDepth[ uiBaseId ][ 2 ] = iVShift;
222
223    // virtual depth -> disparity
224    Int       iVInvAdd    = ( iVScale > 0 ? iVScale / 2 : -iVScale / 2 );
225    Int       iVInvScale  = Int( ( ( Int64(  1        ) << ( iVShift << 1 ) ) + iVInvAdd ) / Int64( iVScale ) );
226    Int       iVInvOffset = Int( ( ( Int64( -iVOffset ) <<   iVShift        ) + iVInvAdd ) / Int64( iVScale ) );
227    m_aaiConvParsVirtDepth2Disparity[ uiBaseId ][ 0 ] = iDisparityDir * iVInvScale;
228    m_aaiConvParsVirtDepth2Disparity[ uiBaseId ][ 1 ] = iDisparityDir * iVInvOffset + ( ( 1 << iVShift ) >> 1 );
229    m_aaiConvParsVirtDepth2Disparity[ uiBaseId ][ 2 ] = iVShift;
230
231    // coded depth -> virtual depth
232    Int       iCScale     = ( bInSlice ? pcPic->getCurrSlice()->getCodedScale () : pcSPS->getCodedScale () )[ uiBaseId ];
233    Int       iCOffset    = ( bInSlice ? pcPic->getCurrSlice()->getCodedOffset() : pcSPS->getCodedOffset() )[ uiBaseId ] << m_uiOrgDepthBitDepth;
234    Int       iCShift     = m_uiOrgDepthBitDepth + uiCamPrec + 1 - 2;
235    Int       iCVShift    = PDM_INTER_CALC_SHIFT;
236    Int       iTmpShift   = iVShift + iCShift - iCVShift; AOF( iTmpShift >= 0 )
237    Int       iCVScale    = Int( ( Int64( iVScale ) * Int64( iCScale  ) + Int64( ( 1 << iTmpShift ) >> 1 ) ) >> iTmpShift );
238    Int       iCVOffset   = Int( ( Int64( iVScale ) * Int64( iCOffset ) + Int64( ( 1 << iTmpShift ) >> 1 ) ) >> iTmpShift );
239    iTmpShift             = iVShift - iCVShift;           AOF( iTmpShift >= 0 )
240    iCVOffset            +=      ( iVOffset                             +      ( ( 1 << iTmpShift ) >> 1 ) ) >> iTmpShift;
241    m_aaiConvParsOrigDepth2VirtDepth[ uiBaseId ][ 0 ] = iCVScale;
242    m_aaiConvParsOrigDepth2VirtDepth[ uiBaseId ][ 1 ] = iCVOffset + ( ( 1 << iCVShift ) >> 1 );
243    m_aaiConvParsOrigDepth2VirtDepth[ uiBaseId ][ 2 ] = iCVShift;
244
245    // virtual depth -> coded depth
246    Int       iCVAdd      = ( iCVScale > 0 ? iCVScale / 2 : -iCVScale / 2 );
247    Int       iCVInvScale = Int( ( ( Int64(  1         ) << ( iCVShift << 1 ) ) + iCVAdd ) / Int64( iCVScale ) );
248    Int       iCVInvOffset= Int( ( ( Int64( -iCVOffset ) <<   iCVShift        ) + iCVAdd ) / Int64( iCVScale ) );
249    m_aaiConvParsVirtDepth2OrigDepth[ uiBaseId ][ 0 ] = iCVInvScale;
250    m_aaiConvParsVirtDepth2OrigDepth[ uiBaseId ][ 1 ] = iCVInvOffset + ( ( 1 << iCVShift ) >> 1 );
251    m_aaiConvParsVirtDepth2OrigDepth[ uiBaseId ][ 2 ] = iCVShift;
252  }
253
254  if( m_uiCurrViewId > 0 )
255  {
256    UInt      uiBaseId    = 0;
257    UInt      uiBaseVOI   = 0; // per definition
258    Int       iVNominator = ( 1 << PDM_LOG4_SCALE_DENOMINATOR ) + pcSPS->getPdmScaleNomDelta()[ uiBaseId ];
259    Int       iVDiv       = iVOI - uiBaseVOI;
260    Int       iVAdd       = ( iVDiv > 0 ? iVDiv / 2 : -iVDiv / 2 );
261    Int       iVScalePred = ( iScaleVOI01 + iVAdd ) / iVDiv;
262    Int       iVShift     = PDM_INTER_CALC_SHIFT;
263    Int       iVScale     = Int( ( (Int64)iVNominator * (Int64)iVScalePred + (Int64)( ( 1 << PDM_LOG4_SCALE_DENOMINATOR ) >> 1 ) ) >> PDM_LOG4_SCALE_DENOMINATOR );
264    Int       iVOffset    = pcSPS->getPdmOffset()[ uiBaseId ] << PDM_OFFSET_SHIFT;
265
266    // coded depth -> virtual depth (current view)
267    Int       iCScale     = ( bInSlice ? pcPic->getCurrSlice()->getInvCodedScale () : pcSPS->getInvCodedScale () )[ uiBaseId ];
268    Int       iCOffset    = ( bInSlice ? pcPic->getCurrSlice()->getInvCodedOffset() : pcSPS->getInvCodedOffset() )[ uiBaseId ] << m_uiOrgDepthBitDepth;
269    Int       iCShift     = m_uiOrgDepthBitDepth + uiCamPrec + 1 - 2;
270    Int       iCVShift    = PDM_INTER_CALC_SHIFT;
271    Int       iTmpShift   = iVShift + iCShift - iCVShift; AOF( iTmpShift >= 0 )
272    Int       iCVScale    = Int( ( Int64( -iVScale ) * Int64( iCScale  ) + Int64( ( 1 << iTmpShift ) >> 1 ) ) >> iTmpShift );
273    Int       iCVOffset   = Int( ( Int64( -iVScale ) * Int64( iCOffset ) + Int64( ( 1 << iTmpShift ) >> 1 ) ) >> iTmpShift );
274    iTmpShift             = iVShift - iCVShift;           AOF( iTmpShift >= 0 )
275    iCVOffset            +=      ( iVOffset                             +      ( ( 1 << iTmpShift ) >> 1 ) ) >> iTmpShift;
276    m_aaiConvParsOrigDepth2VirtDepth[ m_uiCurrViewId ][ 0 ] = iCVScale;
277    m_aaiConvParsOrigDepth2VirtDepth[ m_uiCurrViewId ][ 1 ] = iCVOffset + ( ( 1 << iCVShift ) >> 1 );
278    m_aaiConvParsOrigDepth2VirtDepth[ m_uiCurrViewId ][ 2 ] = iCVShift;
279
280    // virtual depth -> coded depth
281    Int       iCVAdd      = ( iCVScale > 0 ? iCVScale / 2 : -iCVScale / 2 );
282    Int       iCVInvScale = Int( ( ( Int64(  1         ) << ( iCVShift << 1 ) ) + iCVAdd ) / Int64( iCVScale ) );
283    Int       iCVInvOffset= Int( ( ( Int64( -iCVOffset ) <<   iCVShift        ) + iCVAdd ) / Int64( iCVScale ) );
284    m_aaiConvParsVirtDepth2OrigDepth[ m_uiCurrViewId ][ 0 ] = iCVInvScale;
285    m_aaiConvParsVirtDepth2OrigDepth[ m_uiCurrViewId ][ 1 ] = iCVInvOffset + ( ( 1 << iCVShift ) >> 1 );
286    m_aaiConvParsVirtDepth2OrigDepth[ m_uiCurrViewId ][ 2 ] = iCVShift;
287  }
288  else if( pcPic->getPOC() == 0 )
289  { // set dummy values
290    m_aaiConvParsOrigDepth2VirtDepth[ m_uiCurrViewId ][ 0 ] = 0;
291    m_aaiConvParsOrigDepth2VirtDepth[ m_uiCurrViewId ][ 1 ] = 0;
292    m_aaiConvParsOrigDepth2VirtDepth[ m_uiCurrViewId ][ 2 ] = 0;
293    m_aaiConvParsVirtDepth2OrigDepth[ m_uiCurrViewId ][ 0 ] = 0;
294    m_aaiConvParsVirtDepth2OrigDepth[ m_uiCurrViewId ][ 1 ] = 0;
295    m_aaiConvParsVirtDepth2OrigDepth[ m_uiCurrViewId ][ 2 ] = 0;
296  }
297
298
299#if 0 // print out for debugging
300  if( m_uiCurrViewId )
301  {
302    printf( "\n\ninit slice of view %d (VOI=%2d):\n===============================\n", m_uiCurrViewId, iVOI );
303    {
304      printf( "\n  disparity -> virtual depth:\n" );
305      for( UInt uiBaseId = 0; uiBaseId < m_uiCurrViewId; uiBaseId++ )
306      {
307        Int*    pP = m_aaiConvParsDisparity2VirtDepth[ uiBaseId ];
308        Double  dF = 1.0 / Double( 1 << pP[ 2 ] );
309        Double  dA = dF  * Double( pP[ 0 ] );
310        Double  dB = dF  * Double( pP[ 1 ] - ( ( 1 << pP[ 2 ] ) >> 1 ) );
311        printf( "    BId=%d:    a = %10.4lf    b = %10.4lf\n", uiBaseId, dA, dB );
312      }
313      printf( "\n  virtual depth -> disparity:\n" );
314      for( UInt uiBaseId = 0; uiBaseId < m_uiCurrViewId; uiBaseId++ )
315      {
316        Int*    pP = m_aaiConvParsVirtDepth2Disparity[ uiBaseId ];
317        Double  dF = 1.0 / Double( 1 << pP[ 2 ] );
318        Double  dA = dF  * Double( pP[ 0 ] );
319        Double  dB = dF  * Double( pP[ 1 ] - ( ( 1 << pP[ 2 ] ) >> 1 ) );
320        printf( "    BId=%d:    a = %10.4lf    b = %10.4lf\n", uiBaseId, dA, dB );
321      }
322      printf( "\n  original depth -> virtual depth:\n" );
323      for( UInt uiBaseId = 0; uiBaseId <= m_uiCurrViewId; uiBaseId++ )
324      {
325        Int*    pP = m_aaiConvParsOrigDepth2VirtDepth[ uiBaseId ];
326        Double  dF = 1.0 / Double( 1 << pP[ 2 ] );
327        Double  dA = dF  * Double( pP[ 0 ] );
328        Double  dB = dF  * Double( pP[ 1 ] - ( ( 1 << pP[ 2 ] ) >> 1 ) );
329        printf( "    VId=%d:    a = %10.4lf    b = %10.4lf\n", uiBaseId, dA, dB );
330      }
331      printf( "\n  virtual depth -> original depth:\n" );
332      for( UInt uiBaseId = 0; uiBaseId <= m_uiCurrViewId; uiBaseId++ )
333      {
334        Int*    pP = m_aaiConvParsVirtDepth2OrigDepth[ uiBaseId ];
335        Double  dF = 1.0 / Double( 1 << pP[ 2 ] );
336        Double  dA = dF  * Double( pP[ 0 ] );
337        Double  dB = dF  * Double( pP[ 1 ] - ( ( 1 << pP[ 2 ] ) >> 1 ) );
338        printf( "    VId=%d:    a = %10.4lf    b = %10.4lf\n", uiBaseId, dA, dB );
339      }
340      printf( "\n" );
341    }
342  }
343#endif
344}
345
346
347Bool
348TComDepthMapGenerator::predictDepthMap( TComPic* pcPic )
349{
350  AOF  ( m_bCreated && m_bInit );
351  AOF  ( pcPic );
352  ROTRS( pcPic->getSPS()->isDepth(),  true );
353  ROFRS( m_pcSPSAccess->getPdm(),     true );
354  AOF  ( pcPic->getPredDepthMap() );
355  AOF  ( pcPic->getSPS()->getViewId() == m_uiCurrViewId );
356
357#if PDM_OUTPUT_PRED_DEPTH_MAP
358  Char acFilenameBase[1024];
359  ::sprintf( acFilenameBase, "PDM_%s_Prd", ( m_bDecoder ? "Dec" : "Enc" ) );
360#endif
361
362  Bool bUndefined = true;
363  if( m_uiCurrViewId )
364  {
365    AOF( m_auiBaseIdList.size() );
366    UInt        uiBaseId    = m_auiBaseIdList[ 0 ];
367    TComPic*    pcBasePic   = m_pcAUPicAccess->getPic( uiBaseId );
368    AOF( pcBasePic );
369
370    if( m_uiCurrViewId == 1 )
371    {
372      if( pcBasePic->getPOC() == 0 )
373      {
374        xClearDepthMap( pcBasePic );
375      }
376#if PDM_OUTPUT_PRED_DEPTH_MAP
377      dumpDepthMap( pcBasePic, acFilenameBase );
378#endif
379    }
380
381    Bool  bLoadDepth  = ( m_pcSPSAccess->getPdm() == 2 );
382    if( m_pcSPSAccess->getPdm() > 2 )
383    {
384      bLoadDepth = ( pcBasePic->getCurrSlice()->getSliceType() == I_SLICE );
385    }
386
387    if( bLoadDepth)
388    { // load coded depth of base view
389      TComPic*  pcBaseDepth = m_pcAUPicAccess->getPic( uiBaseId, true );
390      AOF( pcBaseDepth );
391      AOF( pcBaseDepth->getPicYuvRec() );
392      AOF( pcBaseDepth->getPicYuvRec()->getWidth () == pcBasePic->getPredDepthMap()->getWidth () );
393      AOF( pcBaseDepth->getPicYuvRec()->getHeight() == pcBasePic->getPredDepthMap()->getHeight() );
394      Int       iWidth      = pcBasePic  ->getPredDepthMap()->getWidth    ();
395      Int       iHeight     = pcBasePic  ->getPredDepthMap()->getHeight   ();
396      Int       iDesStride  = pcBasePic  ->getPredDepthMap()->getStride   ();
397      Int       iSrcStride  = pcBaseDepth->getPicYuvRec   ()->getStride   ();
398      Pel*      pDesSamples = pcBasePic  ->getPredDepthMap()->getLumaAddr ( 0 );
399      Pel*      pSrcSamples = pcBaseDepth->getPicYuvRec   ()->getLumaAddr ( 0 );
400      for( Int iY = 0; iY < iHeight; iY++, pSrcSamples += iSrcStride, pDesSamples += iDesStride )
401      {
402        for( Int iX = 0; iX < iWidth; iX++ )
403        {
404          pDesSamples[ iX ] = xGetVirtDepthFromOrigDepth( uiBaseId, pSrcSamples[ iX ] );
405        }
406      }
407    }
408
409    // convert depth of base view to current view
410    bUndefined = xConvertDepthMapRef2Curr( pcPic, pcBasePic );
411
412#if PDM_OUTPUT_PRED_DEPTH_MAP
413    dumpDepthMap( pcPic, acFilenameBase );
414#endif
415  }
416  else
417  {
418    xClearDepthMap( pcPic );
419  }
420  return bUndefined;
421}
422
423
424Void
425TComDepthMapGenerator::updateDepthMap( TComPic* pcPic )
426{
427  AOF  ( m_bCreated && m_bInit );
428  AOF  ( pcPic );
429  ROTVS( pcPic->getSPS()->isDepth() );
430  ROFVS( m_pcSPSAccess->getPdm() == 1 || m_pcSPSAccess->getPdm() == 3 );
431  AOF  ( pcPic->getPredDepthMap() );
432  AOF  ( pcPic->getSPS()->getViewId() == m_uiCurrViewId );
433
434#if PDM_OUTPUT_PRED_DEPTH_MAP
435  Char acFilenameBase[1024];
436  ::sprintf( acFilenameBase, "PDM_%s_Upd", ( m_bDecoder ? "Dec" : "Enc" ) );
437#endif
438
439  // predict depth map using current coding symbols
440  xPredictDepthMap( pcPic );
441#if PDM_OUTPUT_PRED_DEPTH_MAP
442  if( m_uiCurrViewId )
443  {
444    dumpDepthMap( pcPic, acFilenameBase );
445  }
446#endif
447
448  // generate base depth map
449  if( m_uiCurrViewId == 1 )
450  {
451    TComPic* pcBasePic = m_pcAUPicAccess->getPic( 0 );
452    AOF( pcBasePic );
453    xConvertDepthMapCurr2Ref( pcBasePic, pcPic );
454#if PDM_OUTPUT_PRED_DEPTH_MAP
455    dumpDepthMap( pcBasePic, acFilenameBase );
456#endif
457  }
458}
459
460
461Void
462TComDepthMapGenerator::dumpDepthMap( TComPic* pcPic, char* pFilenameBase )
463{
464  AOF( m_bCreated && m_bInit );
465  AOF( pcPic );
466  AOF( pFilenameBase );
467  AOF( m_uiOrgDepthBitDepth == 8 + g_uiBitIncrement );
468  AOF( pcPic->getSPS()->getViewId() <= m_uiCurrViewId );
469
470  // convert to output format
471  Int         iMax        = ( 1 << m_uiOrgDepthBitDepth ) - 1;
472  UInt        uiViewId    = pcPic->getSPS()->getViewId();
473  TComPicYuv* pcPicYuv    = pcPic->getPredDepthMap();
474  Int         iWidth      = pcPicYuv->getWidth    ();
475  Int         iHeight     = pcPicYuv->getHeight   ();
476  Int         iSrcStride  = pcPicYuv->getStride   ();
477  Int         iDstStride  = m_cTmpPic.getStride   ();
478  Pel*        pSrcSamples = pcPicYuv->getLumaAddr ( 0 );
479  Pel*        pDstSamples = m_cTmpPic.getLumaAddr ( 0 );
480  Int         iMidOrgDpth = ( 1 << m_uiOrgDepthBitDepth ) >> 1;
481  AOF( m_cTmpPic.getWidth () == iWidth  );
482  AOF( m_cTmpPic.getHeight() == iHeight );
483  for( Int iY = 0; iY < iHeight; iY++, pSrcSamples += iSrcStride, pDstSamples += iDstStride )
484  {
485    for( Int iX = 0; iX < iWidth; iX++ )
486    {
487      Int iOrgDepth     = ( pSrcSamples[ iX ] != PDM_UNDEFINED_DEPTH ? xGetOrigDepthFromVirtDepth( uiViewId, pSrcSamples[ iX ] ) : iMidOrgDpth );
488      pDstSamples[ iX ] = Max( 0, Min( iMax, iOrgDepth ) );
489    }
490  }
491
492  // output
493  Char  acFilename[1024];
494  ::sprintf     ( acFilename, "%s_V%d.yuv", pFilenameBase, uiViewId );
495  m_cTmpPic.dump( acFilename, ( pcPic->getPOC() != 0 )  );
496}
497
498
499#if HHI_INTER_VIEW_MOTION_PRED
500Void 
501TComDepthMapGenerator::covertOrgDepthMap( TComPic* pcPic )
502{
503  AOF  ( m_bCreated && m_bInit   );
504  AOF  ( pcPic );
505  ROFVS( pcPic->getOrgDepthMap() );
506  AOF  ( pcPic->getViewIdx() );
507
508  UInt  uiBaseId = pcPic->getViewIdx();
509  Int   iWidth   = pcPic->getOrgDepthMap()->getWidth    ();
510  Int   iHeight  = pcPic->getOrgDepthMap()->getHeight   ();
511  Int   iStride  = pcPic->getOrgDepthMap()->getStride   ();
512  Pel*  pSamples = pcPic->getOrgDepthMap()->getLumaAddr ( 0 );
513  for( Int iY = 0; iY < iHeight; iY++, pSamples += iStride )
514  {
515    for( Int iX = 0; iX < iWidth; iX++ )
516    {
517      pSamples[ iX ] = xGetVirtDepthFromOrigDepth( uiBaseId, pSamples[ iX ] );
518    }
519  }
520}
521#endif
522
523Int
524TComDepthMapGenerator::getDisparity( TComPic* pcPic, Int iPosX, Int iPosY, UInt uiRefViewId )
525{
526  AOF( pcPic );
527  AOF( pcPic->getPredDepthMap() );
528  AOF( iPosX >= 0 && iPosX < pcPic->getPredDepthMap()->getWidth () );
529  AOF( iPosY >= 0 && iPosY < pcPic->getPredDepthMap()->getHeight() );
530  Pel*   piPdmMap    = pcPic->getPredDepthMap()->getLumaAddr( 0 );
531  Int    iStride     = pcPic->getPredDepthMap()->getStride  ();
532  Int    iPrdDepth   = piPdmMap[ iPosX + iPosY * iStride ];
533  Int    iDisparity  = xGetDisparityFromVirtDepth( uiRefViewId, iPrdDepth );
534  return iDisparity;
535}
536
537
538
539#if HHI_INTER_VIEW_MOTION_PRED
540Int
541TComDepthMapGenerator::getPdmMergeCandidate( TComDataCU* pcCU, UInt uiPartIdx, Int* paiPdmRefIdx, TComMv* pacPdmMv )
542{
543  Int  iMaxNumInterPics  = 1;
544  Int  iMaxNumAllPics    = 2;
545
546  // inter-only
547  Bool abPdmAvailable[2] = {false,false};
548  for( Int iRefListId = 0; iRefListId < 2; iRefListId++ )
549  {
550    RefPicList  eRefPicList       = RefPicList( iRefListId );
551    Int         iNumRefPics       = pcCU->getSlice()->getNumRefIdx( eRefPicList );
552    TComMv      cMv;
553    for( Int iPdmRefIdx = 0, iInterPics = 0; iPdmRefIdx < iNumRefPics && iInterPics < iMaxNumInterPics; iPdmRefIdx++ )
554    {
555      if( pcCU->getSlice()->getRefPOC( eRefPicList, iPdmRefIdx ) != pcCU->getSlice()->getPOC() )
556      {
557        if( getPdmMvPred( pcCU, uiPartIdx, eRefPicList, iPdmRefIdx, cMv, true ) )
558        {
559          pcCU->clipMv( cMv );
560          abPdmAvailable[ iRefListId ] = true;
561          paiPdmRefIdx  [ iRefListId ] = iPdmRefIdx;
562          pacPdmMv      [ iRefListId ] = cMv;
563          break;
564        }
565        iInterPics++;
566      }
567    }
568  }
569  Int    iPdmInterDir = ( abPdmAvailable[0] ? 1 : 0 ) + ( abPdmAvailable[1] ? 2 : 0 );
570  if( 0==iPdmInterDir )
571  { // check all, including inter view references
572    for( Int iRefListId = 0; iRefListId < 2; iRefListId++ )
573    {
574      RefPicList  eRefPicList = RefPicList( iRefListId );
575      Int         iNumRefPics = Min( iMaxNumAllPics, pcCU->getSlice()->getNumRefIdx( eRefPicList ) );
576      TComMv      cMv;
577      for( Int iPdmRefIdx = 0; iPdmRefIdx < iNumRefPics; iPdmRefIdx++ )
578      {
579        if( getPdmMvPred( pcCU, uiPartIdx, eRefPicList, iPdmRefIdx, cMv, true ) )
580        {
581          pcCU->clipMv( cMv );
582          abPdmAvailable[ iRefListId ] = true;
583          paiPdmRefIdx  [ iRefListId ] = iPdmRefIdx;
584          pacPdmMv      [ iRefListId ] = cMv;
585          break;
586        }
587      }
588    }
589    iPdmInterDir = ( abPdmAvailable[0] ? 1 : 0 ) + ( abPdmAvailable[1] ? 2 : 0 );
590  }
591  return iPdmInterDir;
592}
593
594
595Bool 
596TComDepthMapGenerator::getPdmMvPred( TComDataCU* pcCU, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMv, Bool bMerge )
597{
598  AOF  ( m_bCreated && m_bInit );
599  AOF  ( iRefIdx >= 0 );
600  AOF  ( pcCU );
601  ROFRS( m_bPDMAvailable, false );
602
603  TComSlice*    pcSlice     = pcCU->getSlice ();
604  TComPic*      pcPic       = pcCU->getPic   ();
605  TComSPS*      pcSPS       = pcSlice->getSPS();
606  AOF  ( pcPic->getPredDepthMap() );
607  AOF  ( pcSPS->getViewId() == m_uiCurrViewId );
608 
609  TComPic*      pcRefPic    = pcSlice->getRefPic( eRefPicList, iRefIdx );
610  UInt          uiRefViewId = pcRefPic->getSPS()->getViewId();
611  Int           iRefPoc     = pcRefPic->getPOC();
612  Bool          bInterview  = ( uiRefViewId < m_uiCurrViewId );
613  AOT(  bInterview && iRefPoc != pcSlice->getPOC() );
614  AOT( !bInterview && iRefPoc == pcSlice->getPOC() );
615
616  Bool          bPdmIView   = ( ( pcSPS->getMultiviewMvPredMode() & PDM_USE_FOR_IVIEW ) == PDM_USE_FOR_IVIEW );
617  Bool          bPdmInter   = ( ( pcSPS->getMultiviewMvPredMode() & PDM_USE_FOR_INTER ) == PDM_USE_FOR_INTER );
618  Bool          bPdmMerge   = ( ( pcSPS->getMultiviewMvPredMode() & PDM_USE_FOR_MERGE ) == PDM_USE_FOR_MERGE );
619  ROTRS( ( bInterview && !bMerge ) && !bPdmIView, false );
620  ROTRS( (!bInterview && !bMerge ) && !bPdmInter, false );
621  ROTRS(                  bMerge   && !bPdmMerge, false );
622
623  //===== get predicted depth for middle position of current PU ===== 
624  Int  iPrdDepth, iCurrPosX, iCurrPosY;
625  Bool bAvailable  = xGetPredDepth( pcCU, uiPartIdx, iPrdDepth, &iCurrPosX, &iCurrPosY );
626  AOF( bAvailable );
627 
628  //===== inter-view motion vector prediction =====
629  if( bInterview )
630  {
631    Int         iDisparity  = xGetDisparityFromVirtDepth( uiRefViewId, iPrdDepth );
632    rcMv.set  ( iDisparity, 0 );
633    return      true;
634  }
635 
636  //===== inter motion vector prediction =====
637  for( UInt uiBId = 0; uiBId < m_uiCurrViewId; uiBId++ )
638  {
639    //--- get base CU/PU and check prediction mode ---
640    UInt        uiBaseId    = m_auiBaseIdList[ uiBId ];
641    TComPic*    pcBasePic   = m_pcAUPicAccess->getPic( uiBaseId );
642    TComPicYuv* pcBasePdm   = pcBasePic->getPredDepthMap();
643    TComPicYuv* pcBaseRec   = pcBasePic->getPicYuvRec   ();
644    Int         iDisparity  = xGetDisparityFromVirtDepth( uiBaseId, iPrdDepth );
645    Int         iBasePosX   = Clip3( 0, pcBasePdm->getWidth () - 1, iCurrPosX + ( ( iDisparity + 2 ) >> 2 ) );
646    Int         iBasePosY   = Clip3( 0, pcBasePdm->getHeight() - 1, iCurrPosY                               );
647    Int         iBaseCUAddr;
648    Int         iBaseAbsPartIdx;
649    pcBaseRec->getCUAddrAndPartIdx( iBasePosX, iBasePosY, iBaseCUAddr, iBaseAbsPartIdx );
650    TComDataCU* pcBaseCU    = pcBasePic->getCU( iBaseCUAddr );
651    if( pcBaseCU->getPredictionMode( iBaseAbsPartIdx ) != MODE_INTER && pcBaseCU->getPredictionMode( iBaseAbsPartIdx ) != MODE_SKIP )
652    {
653      continue;
654    }
655
656    for( UInt uiBaseRefListId = 0; uiBaseRefListId < 2; uiBaseRefListId++ )
657    {
658      RefPicList  eBaseRefPicList = RefPicList( uiBaseRefListId );
659      TComMvField cBaseMvField;
660      pcBaseCU->getMvField( pcBaseCU, iBaseAbsPartIdx, eBaseRefPicList, cBaseMvField );
661      Int         iBaseRefIdx     = cBaseMvField.getRefIdx();
662      Int         iBaseRefPoc     = ( iBaseRefIdx >= 0 ? pcBaseCU->getSlice()->getRefPic( eBaseRefPicList, iBaseRefIdx )->getPOC() : -(1<<30) );
663      if( iBaseRefIdx >= 0 && iBaseRefPoc == iRefPoc )
664      {
665        rcMv.set( cBaseMvField.getHor(), cBaseMvField.getVer() );
666        return true;
667      }
668    }
669  }
670  return false;
671}
672
673
674Bool  // first version
675TComDepthMapGenerator::getIViewOrgDepthMvPred( TComDataCU* pcCU, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMv )
676{
677  AOF  ( m_bCreated && m_bInit );
678  AOF  ( iRefIdx >= 0 );
679  AOF  ( pcCU );
680
681  TComSlice*    pcSlice     = pcCU->getSlice ();
682  TComPic*      pcPic       = pcCU->getPic   ();
683  TComSPS*      pcSPS       = pcSlice->getSPS();
684  AOF  ( pcSPS->getViewId() == m_uiCurrViewId );
685  ROFRS( pcPic->getOrgDepthMap(),      false );
686 
687  TComPic*      pcRefPic    = pcSlice->getRefPic( eRefPicList, iRefIdx );
688  UInt          uiRefViewId = pcRefPic->getSPS()->getViewId();
689  Int           iRefPoc     = pcRefPic->getPOC();
690  ROFRS( uiRefViewId < m_uiCurrViewId, false );
691  AOF  ( iRefPoc    == pcSlice->getPOC() );
692
693  //===== get predicted depth for middle position of current PU (first version) ===== 
694  UInt          uiPartAddr;
695  Int           iWidth;
696  Int           iHeight;
697  pcCU->getPartIndexAndSize( uiPartIdx, uiPartAddr, iWidth, iHeight );
698  TComPicYuv*   pcOrgDepthMap  = pcPic->getOrgDepthMap();
699  Pel*          piOrgDepthMap  = pcOrgDepthMap->getLumaAddr ( 0 );
700  Int           iCurrStride    = pcOrgDepthMap->getStride   ();
701  Int           iCurrPosX;
702  Int           iCurrPosY;
703  pcOrgDepthMap->getTopLeftSamplePos( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr, iCurrPosX, iCurrPosY );
704  iCurrPosX                    += ( iWidth  - 1 ) >> 1;
705  iCurrPosY                    += ( iHeight - 1 ) >> 1;
706  Int           iPrdDepth       = piOrgDepthMap[ iCurrPosX + iCurrPosY * iCurrStride ];
707 
708  //===== get disparity vector =====
709  Int           iDisparity      = xGetDisparityFromVirtDepth( uiRefViewId, iPrdDepth );
710  rcMv.set    ( iDisparity, 0 );
711  return        true;
712}
713#endif
714
715
716
717
718
719
720/*=======================================================*
721 *=====                                             =====*
722 *=====     p i c t u r e   o p e r a t i o n s     =====*
723 *=====                                             =====*
724 *=======================================================*/
725
726Bool
727TComDepthMapGenerator::xConvertDepthMapCurr2Ref( TComPic* pcRef, TComPic* pcCur )
728{
729  AOF( pcCur->getSPS()->getViewId() == m_uiCurrViewId );
730  AOF( pcCur->getSPS()->getViewId()  > pcRef->getSPS()->getViewId() );
731  AOF( pcCur->getPredDepthMap() );
732  AOF( pcRef->getPredDepthMap() );
733  AOF( pcRef->getPredDepthMap()->getWidth () == pcCur->getPredDepthMap()->getWidth () );
734  AOF( pcRef->getPredDepthMap()->getHeight() == pcCur->getPredDepthMap()->getHeight() );
735
736  xClearDepthMap( pcRef );
737  TComPicYuv* pcCurDepthMap =  pcCur->getPredDepthMap    ();
738  TComPicYuv* pcRefDepthMap =  pcRef->getPredDepthMap    ();
739  Int         iWidth        =  pcCurDepthMap->getWidth   ();
740  Int         iHeight       =  pcCurDepthMap->getHeight  ();
741  Int         iCurStride    =  pcCurDepthMap->getStride  ();
742  Int         iRefStride    =  pcRefDepthMap->getStride  ();
743  Pel*        pCurSamples   =  pcCurDepthMap->getLumaAddr( 0 );
744  Pel*        pRefSamples   =  pcRefDepthMap->getLumaAddr( 0 );
745  Int         iRefViewIdx   =  pcRef->getViewIdx();
746  for( Int iY = 0; iY < iHeight; iY++, pCurSamples += iCurStride, pRefSamples += iRefStride )
747  {
748    for( Int iXCur = 0; iXCur < iWidth; iXCur++ )
749    {
750      Int iDepth = pCurSamples[ iXCur ];
751      Int iDisp  = xGetDisparityFromVirtDepth( iRefViewIdx, iDepth );
752      Int iXRef  = iXCur + ( ( iDisp + 2 ) >> 2 );
753      if( iXRef >= 0 && iXRef < iWidth && iDepth > pRefSamples[ iXRef ] )
754      {
755        pRefSamples[ iXRef ] = iDepth;
756      }
757    }
758  }
759  Bool    bUndefined = xFillDepthMapHoles( pcRef );
760  pcRefDepthMap->setBorderExtension( false );
761  pcRefDepthMap->extendPicBorder   ();
762  return  bUndefined;
763}
764
765
766Bool
767TComDepthMapGenerator::xConvertDepthMapRef2Curr( TComPic* pcCur, TComPic* pcRef )
768{
769  AOF( pcCur->getSPS()->getViewId() == m_uiCurrViewId );
770  AOF( pcCur->getSPS()->getViewId()  > pcRef->getSPS()->getViewId() );
771  AOF( pcCur->getPredDepthMap() );
772  AOF( pcRef->getPredDepthMap() );
773  AOF( pcRef->getPredDepthMap()->getWidth () == pcCur->getPredDepthMap()->getWidth () );
774  AOF( pcRef->getPredDepthMap()->getHeight() == pcCur->getPredDepthMap()->getHeight() );
775
776  xClearDepthMap( pcCur );
777  TComPicYuv* pcRefDepthMap =  pcRef->getPredDepthMap    ();
778  TComPicYuv* pcCurDepthMap =  pcCur->getPredDepthMap    ();
779  Int         iWidth        =  pcRefDepthMap->getWidth   ();
780  Int         iHeight       =  pcRefDepthMap->getHeight  ();
781  Int         iRefStride    =  pcRefDepthMap->getStride  ();
782  Int         iCurStride    =  pcCurDepthMap->getStride  ();
783  Pel*        pRefSamples   =  pcRefDepthMap->getLumaAddr( 0 );
784  Pel*        pCurSamples   =  pcCurDepthMap->getLumaAddr( 0 );
785  Int         iRefViewIdx   =  pcRef->getViewIdx();
786  for( Int iY = 0; iY < iHeight; iY++, pRefSamples += iRefStride, pCurSamples += iCurStride )
787  {
788    for( Int iXRef = 0; iXRef < iWidth; iXRef++ )
789    {
790      Int iDepth = pRefSamples[ iXRef ];
791      Int iDisp  = xGetDisparityFromVirtDepth( iRefViewIdx, iDepth );
792      Int iXCur  = iXRef - ( ( iDisp + 2 ) >> 2 );
793      if( iXCur >= 0 && iXCur < iWidth && iDepth > pCurSamples[ iXCur ] )
794      {
795        pCurSamples[ iXCur ] = iDepth;
796      }
797    }
798  }
799  Bool    bUndefined = xFillDepthMapHoles( pcCur );
800  pcCurDepthMap->setBorderExtension( false );
801  pcCurDepthMap->extendPicBorder   ();
802  return  bUndefined;
803}
804
805
806Bool
807TComDepthMapGenerator::xPredictDepthMap( TComPic* pcPic )
808{
809  for( UInt uiCUAddr = 0; uiCUAddr < pcPic->getPicSym()->getNumberOfCUsInFrame(); uiCUAddr++ )
810  {
811    TComDataCU* pcCU = pcPic->getCU( uiCUAddr );
812    xPredictCUDepthMap( pcCU, 0, 0 );
813  }
814  Bool    bUndefined = xFillDepthMapHoles( pcPic );
815  pcPic->getPredDepthMap()->setBorderExtension( false );
816  pcPic->getPredDepthMap()->extendPicBorder   ();
817  return  bUndefined;
818}
819
820
821Bool
822TComDepthMapGenerator::xFillDepthMapHoles( TComPic* pcPic )
823{
824  Bool        bUndefined  = false;     
825  TComPicYuv* pcDepthMap  = pcPic->getPredDepthMap  ();
826  Int         iWidth      = pcDepthMap->getWidth    ();
827  Int         iHeight     = pcDepthMap->getHeight   ();
828  Int         iStride     = pcDepthMap->getStride   ();
829  Pel*        pDMSamples  = pcDepthMap->getLumaAddr ( 0 );
830  // horizontal
831  for( Int iY = 0; iY < iHeight && !bUndefined; iY++, pDMSamples += iStride )
832  {
833    for( Int iX = 0; iX < iWidth; iX++ )
834    {
835      if( pDMSamples[ iX ] == PDM_UNDEFINED_DEPTH )
836      {
837        Int  iE;
838        for( iE = iX + 1; iE < iWidth; iE++ )
839        {
840          if( pDMSamples[ iE ] != PDM_UNDEFINED_DEPTH )
841          {
842            break;
843          }
844        }
845        if( iX > 0 || iE < iWidth )
846        {
847          Int iDepth;
848          if     ( iX > 0 && iE < iWidth )  iDepth  = ( pDMSamples[ iX-1 ] < pDMSamples[ iE ] ? pDMSamples[ iX-1 ] : pDMSamples[ iE ] ); 
849          else if( iX > 0 )                 iDepth  =   pDMSamples[ iX-1 ]; 
850          else /*( iE < iWidth )*/          iDepth  =   pDMSamples[ iE   ]; 
851          for( Int iZ = iX; iZ < iE; iZ++ )
852          {
853            pDMSamples[ iZ ] = iDepth;
854          }
855        }
856        else
857        {
858          bUndefined = true;
859                  break;
860        }
861        iX = iE - 1;
862      }
863    }
864  }
865 
866  if( bUndefined && m_uiCurrViewId )
867  {
868    pDMSamples          = pcDepthMap->getLumaAddr( 0 );
869    Int  iMiddleOrgDpth = ( 1 << m_uiOrgDepthBitDepth ) >> 1;
870    Int  iMiddleDepth   = xGetVirtDepthFromOrigDepth( m_uiCurrViewId, iMiddleOrgDpth );
871    for( Int iY = 0; iY < iHeight; iY++, pDMSamples += iStride )
872    {
873      for( Int iX = 0; iX < iWidth; iX++ )
874      {
875        pDMSamples[ iX ] = iMiddleDepth;
876       }
877    }
878  }
879  return bUndefined;
880}
881
882
883Void
884TComDepthMapGenerator::xClearDepthMap( TComPic* pcPic, Int iVal )
885{
886  TComPicYuv* pcDepthMap  = pcPic->getPredDepthMap  ();
887  Int         iWidth      = pcDepthMap->getWidth    ();
888  Int         iHeight     = pcDepthMap->getHeight   ();
889  Int         iStride     = pcDepthMap->getStride   ();
890  Pel*        pDMSamples  = pcDepthMap->getLumaAddr ( 0 );
891  for( Int iY = 0; iY < iHeight; iY++, pDMSamples += iStride )
892  {
893    for( Int iX = 0; iX < iWidth; iX++ )
894    {
895      pDMSamples[ iX ] = iVal;
896    }
897  }
898  pcDepthMap->setBorderExtension( false );
899  pcDepthMap->extendPicBorder   ();
900}
901
902Void 
903TComDepthMapGenerator::xSetChroma( TComPicYuv* pcPic, Int iVal )
904{
905  Int   iWidth      = pcPic->getWidth   () >> 1;
906  Int   iHeight     = pcPic->getHeight  () >> 1;
907  Int   iStride     = pcPic->getCStride ();
908  Pel*  pCbSamples  = pcPic->getCbAddr  ( 0 );
909  Pel*  pCrSamples  = pcPic->getCrAddr  ( 0 );
910  for( Int iY = 0; iY < iHeight; iY++, pCbSamples += iStride, pCrSamples += iStride )
911  {
912    for( Int iX = 0; iX < iWidth; iX++ )
913    {
914      pCbSamples[ iX ] = pCrSamples[ iX ] = iVal;
915    }
916  }
917}
918
919
920
921
922
923
924
925/*===============================================*
926 *=====                                     =====*
927 *=====     C U   p r e d i c t i o n s     =====*
928 *=====                                     =====*
929 *===============================================*/
930
931Void
932TComDepthMapGenerator::xPredictCUDepthMap( TComDataCU* pcCU, UInt uiDepth, UInt uiAbsPartIdx )
933{
934  UInt  uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
935  UInt  uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
936  UInt  uiRPelX   = uiLPelX           + ( g_uiMaxCUWidth  >> uiDepth ) - 1;
937  UInt  uiBPelY   = uiTPelY           + ( g_uiMaxCUHeight >> uiDepth ) - 1;
938  Bool  bBoundary = ( uiRPelX >= pcCU->getSlice()->getSPS()->getWidth() || uiBPelY >= pcCU->getSlice()->getSPS()->getHeight() );
939  Bool  bSplit    = ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) && uiDepth < ( g_uiMaxCUDepth - g_uiAddCUDepth ) ) || bBoundary );
940  if(   bSplit )
941  {
942    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> ( uiDepth << 1 ) ) >> 2;
943    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx += uiQNumParts )
944    {
945      uiLPelX       = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
946      uiTPelY       = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[ uiAbsPartIdx ] ];
947      Bool  bInside = ( uiLPelX < pcCU->getSlice()->getSPS()->getWidth() && uiTPelY < pcCU->getSlice()->getSPS()->getHeight() );
948      if(   bInside )
949      {
950        xPredictCUDepthMap( pcCU, uiDepth + 1, uiAbsPartIdx );
951      }
952    }
953    return;
954  }
955
956  //--- set sub-CU and sub-depth-map ---
957  TComDataCU* pcSubCU   = m_ppcCU [ uiDepth ];
958  TComYuv*    pcSubDM   = m_ppcYuv[ uiDepth ];
959  TComPicYuv* pcPicDM   = pcCU->getPic()->getPredDepthMap();
960  UInt        uiCUAddr  = pcCU->getAddr();
961  pcSubCU->copySubCU( pcCU, uiAbsPartIdx, uiDepth );
962
963  //--- update depth map ---
964  switch( pcSubCU->getPredictionMode( 0 ) )
965  {
966  case MODE_INTRA:
967    xIntraPredictCUDepthMap( pcSubCU, pcSubDM );
968    break;
969  case MODE_SKIP:
970  case MODE_INTER:
971    xInterPredictCUDepthMap( pcSubCU, pcSubDM );
972    break;
973  default:
974    AOT( true );
975    break;
976  }
977
978  //--- copy sub-depth-map ---
979  pcSubDM->copyToPicYuv( pcPicDM, uiCUAddr, uiAbsPartIdx );
980}
981
982
983Void
984TComDepthMapGenerator::xIntraPredictCUDepthMap( TComDataCU* pcCU, TComYuv* pcCUDepthMap )
985{
986  UInt  uiInitTrDepth = ( pcCU->getPartitionSize( 0 ) == SIZE_2Nx2N ? 0 : 1 );
987  UInt  uiNumPart     =   pcCU->getNumPartInter ();
988  UInt  uiNumQParts   =   pcCU->getTotalNumPart () >> 2;
989  for( UInt uiPU = 0; uiPU < uiNumPart; uiPU++ )
990  {
991    xIntraPredictBlkDepthMap( pcCU, pcCUDepthMap, uiPU * uiNumQParts, uiInitTrDepth );
992  }
993}
994
995
996Void
997TComDepthMapGenerator::xInterPredictCUDepthMap( TComDataCU* pcCU, TComYuv* pcCUDepthMap )
998{
999  for( UInt uiPartIdx = 0; uiPartIdx < pcCU->getNumPartInter(); uiPartIdx++ )
1000  {
1001    xInterPredictPUDepthMap( pcCU, pcCUDepthMap, uiPartIdx );
1002  }
1003}
1004
1005
1006
1007
1008
1009
1010
1011/*=====================================================================*
1012 *=====                                                           =====*
1013 *=====     P U -   a n d   B l o c k   p r e d i c t i o n s     =====*
1014 *=====                                                           =====*
1015 *=====================================================================*/
1016
1017Void
1018TComDepthMapGenerator::xIntraPredictBlkDepthMap( TComDataCU* pcCU, TComYuv* pcCUDepthMap, UInt uiAbsPartIdx, UInt uiTrDepth )
1019{
1020  UInt uiFullDepth  = pcCU->getDepth( 0 ) + uiTrDepth;
1021  UInt uiTrMode     = pcCU->getTransformIdx( uiAbsPartIdx );
1022  if( uiTrMode == uiTrDepth )
1023  {
1024    UInt  uiWidth         = pcCU->getWidth ( 0 ) >> uiTrDepth;
1025    UInt  uiHeight        = pcCU->getHeight( 0 ) >> uiTrDepth;
1026    UInt  uiStride        = pcCUDepthMap->getStride  ();
1027    Pel*  pDepthMap       = pcCUDepthMap->getLumaAddr( uiAbsPartIdx );
1028    UInt  uiLumaPredMode  = pcCU->getLumaIntraDir    ( uiAbsPartIdx );
1029    Bool  bAboveAvail     = false;
1030    Bool  bLeftAvail      = false;
1031    pcCU->getPattern()->initPattern   ( pcCU, uiTrDepth, uiAbsPartIdx, true );
1032    pcCU->getPattern()->initAdiPattern( pcCU, uiAbsPartIdx, uiTrDepth, 
1033                                        m_pcPrediction->getPredicBuf       (),
1034                                        m_pcPrediction->getPredicBufWidth  (),
1035                                        m_pcPrediction->getPredicBufHeight (),
1036                                        bAboveAvail, bLeftAvail, true );
1037    m_pcPrediction->predIntraDepthAng ( pcCU->getPattern(), uiLumaPredMode, pDepthMap, uiStride, uiWidth, uiHeight ); // could be replaced with directional intra prediction
1038                                                                                                                      // using "predIntraLumaAng", but note:
1039                                                                                                                      //  - need to take care of neighbours with undefined depth
1040                                                                                                                      //  - special case for wedgelet mode (if available in normal views)
1041    // copy to picture array (for next intra prediction block)
1042    UInt  uiZOrderIdx     = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
1043    Pel*  pPicDepthMap    = pcCU->getPic()->getPredDepthMap()->getLumaAddr( pcCU->getAddr(), uiZOrderIdx );
1044    Int   iPicStride      = pcCU->getPic()->getPredDepthMap()->getStride  ();
1045    for( UInt uiY = 0; uiY < uiHeight; uiY++, pDepthMap += uiStride, pPicDepthMap += iPicStride )
1046    {
1047      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1048      {
1049        pPicDepthMap[ uiX ] = pDepthMap[ uiX ];
1050      }
1051    }
1052  }
1053  else
1054  {
1055    UInt uiNumQPart  = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
1056    for( UInt uiPart = 0; uiPart < 4; uiPart++ )
1057    {
1058      xIntraPredictBlkDepthMap( pcCU, pcCUDepthMap, uiAbsPartIdx + uiPart * uiNumQPart, uiTrDepth + 1 );
1059    }
1060  }
1061}
1062
1063
1064Void 
1065TComDepthMapGenerator::xInterPredictPUDepthMap( TComDataCU* pcCU, TComYuv* pcCUDepthMap, UInt uiPartIdx )
1066{
1067  if ( pcCU->getSlice()->getSPS()->getViewId() )
1068  {
1069    AOF( m_uiCurrViewId == pcCU->getSlice()->getSPS()->getViewId() );
1070    // check for interview prediction
1071    Int             iWidth;
1072    Int             iHeight;
1073    UInt            uiAbsPartIdx;
1074    pcCU->getPartIndexAndSize( uiPartIdx, uiAbsPartIdx, iWidth, iHeight );
1075    TComCUMvField*  aiCurrMvField[2]  = { pcCU->getCUMvField( REF_PIC_LIST_0 ),        pcCU->getCUMvField( REF_PIC_LIST_1 )        };
1076    Int             aiCurrRefIdx [2]  = { aiCurrMvField[0]->getRefIdx( uiAbsPartIdx ), aiCurrMvField[1]->getRefIdx( uiAbsPartIdx ) };
1077    Bool            abCurrIntView[2]  = { aiCurrRefIdx[0] >= 0 && pcCU->getSlice()->getRefPic( REF_PIC_LIST_0, aiCurrRefIdx[0] )->getSPS()->getViewId() != m_uiCurrViewId,
1078                                          aiCurrRefIdx[1] >= 0 && pcCU->getSlice()->getRefPic( REF_PIC_LIST_1, aiCurrRefIdx[1] )->getSPS()->getViewId() != m_uiCurrViewId };
1079    Bool            bUsesInterViewPrd = ( abCurrIntView[0] || abCurrIntView[1] );
1080    if( bUsesInterViewPrd )
1081    {
1082      xIViewPUDepthMapUpdate  ( pcCU, pcCUDepthMap, uiPartIdx );
1083    }
1084    else
1085    { 
1086#if PDM_NO_INTER_UPDATE
1087      xInterPUDepthMapPrediction( pcCU, pcCUDepthMap, uiPartIdx );
1088#else
1089      xInterPUDepthMapUpdate  ( pcCU, pcCUDepthMap, uiPartIdx );
1090#endif
1091    }
1092  }
1093  else
1094  {
1095    xInterPUDepthMapPrediction( pcCU, pcCUDepthMap, uiPartIdx );
1096  }
1097}
1098
1099
1100Void 
1101TComDepthMapGenerator::xIViewPUDepthMapUpdate( TComDataCU* pcCU, TComYuv* pcCUDepthMap, UInt uiPartIdx )
1102{
1103  // get width, height, and part address
1104  Int   iWidth;
1105  Int   iHeight;
1106  UInt  uiAbsPartIdx;
1107  pcCU->getPartIndexAndSize( uiPartIdx, uiAbsPartIdx, iWidth, iHeight );
1108
1109  // get depth values
1110  Int   iDepthValue   = PDM_UNDEFINED_DEPTH;
1111  Int   aiPrdDepth[2] = { PDM_UNDEFINED_DEPTH, PDM_UNDEFINED_DEPTH };
1112  for( Int iRefListId = 0; iRefListId < 2; iRefListId++ )
1113  {
1114    RefPicList      eRefPicList = RefPicList( iRefListId );
1115    TComCUMvField*  pcCUMvField = pcCU->getCUMvField( eRefPicList );
1116    Int             iRefIdx     = pcCUMvField->getRefIdx( uiAbsPartIdx );
1117    UInt            uiBaseId    = ( iRefIdx >= 0 ? pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getSPS()->getViewId() : MAX_NUMBER_VIEWS ); 
1118    Bool            bInterview  = ( iRefIdx >= 0 && uiBaseId < m_uiCurrViewId );
1119    if( bInterview )
1120    {
1121      Int           iMvX        = pcCUMvField->getMv( uiAbsPartIdx ).getHor();
1122      aiPrdDepth[ iRefListId ]  = xGetVirtDepthFromDisparity( uiBaseId, iMvX );
1123    }
1124  }
1125  if( aiPrdDepth[ 0 ] != PDM_UNDEFINED_DEPTH && aiPrdDepth[ 1 ] != PDM_UNDEFINED_DEPTH )
1126  {
1127    iDepthValue = ( aiPrdDepth[ 0 ] + aiPrdDepth[ 1 ] + 1 ) >> 2;
1128  }
1129  else
1130  {
1131    iDepthValue = ( aiPrdDepth[ 0 ] != PDM_UNDEFINED_DEPTH ? aiPrdDepth[ 0 ] : aiPrdDepth[ 1 ] );
1132    AOT( iDepthValue == PDM_UNDEFINED_DEPTH );
1133  }
1134  iDepthValue   = Max( 0, Min( PDM_MAX_ABS_VIRT_DEPTH, iDepthValue ) );
1135
1136  // set depth map for PU
1137  Pel*  pDMSamples  = pcCUDepthMap->getLumaAddr( uiAbsPartIdx );
1138  Int   iStride     = pcCUDepthMap->getStride  ();
1139  for( Int iY = 0; iY < iHeight; iY++, pDMSamples += iStride )
1140  {
1141    for( Int iX = 0; iX < iWidth; iX++)
1142    {
1143      pDMSamples[ iX ] = (Pel)iDepthValue;
1144    }
1145  }
1146}
1147
1148
1149Void
1150TComDepthMapGenerator::xInterPUDepthMapUpdate( TComDataCU* pcCU, TComYuv* pcCUDepthMap, UInt uiPartIdx )
1151{
1152  const Int       iMaxAbsDeltaMvY   = 8 << 2;
1153
1154  //===== determine block parameters for current access unit and current view =====
1155  Int             iWidth;
1156  Int             iHeight;
1157  UInt            uiAbsPartIdx;
1158  pcCU->getPartIndexAndSize ( uiPartIdx, uiAbsPartIdx, iWidth, iHeight );
1159  UInt            uiCurrViewId      = pcCU->getSlice()->getSPS()->getViewId(); 
1160  Int             iNum4x4BlksY      = iHeight >> 2;
1161  Int             iNum4x4BlksX      = iWidth  >> 2;
1162  TComPicYuv*     pcCurrDepthMap    = pcCU->getPic()->getPredDepthMap();
1163  Pel*            piCurrDepthMap    = pcCurrDepthMap->getLumaAddr();
1164  Int             iCurrStride       = pcCurrDepthMap->getStride();
1165  TComCUMvField*  aiCurrMvField[2]  = { pcCU->getCUMvField( REF_PIC_LIST_0 ),        pcCU->getCUMvField( REF_PIC_LIST_1 )        };
1166  Int             aiCurrRefIdx [2]  = { aiCurrMvField[0]->getRefIdx( uiAbsPartIdx ), aiCurrMvField[1]->getRefIdx( uiAbsPartIdx ) };
1167  Int             iMinCurrListId    = ( aiCurrRefIdx [0] < 0 ? 1 : 0 );
1168  Int             iMaxCurrListId    = ( aiCurrRefIdx [1] < 0 ? 0 : 1 );
1169  Int             iCurrPUPosX;
1170  Int             iCurrPUPosY;
1171  pcCurrDepthMap->getTopLeftSamplePos( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiAbsPartIdx, iCurrPUPosX, iCurrPUPosY );
1172  AOT( uiCurrViewId    != m_uiCurrViewId );
1173  AOT( iMinCurrListId  >  iMaxCurrListId );
1174  AOT( aiCurrRefIdx[0] >= 0 && pcCU->getSlice()->getRefPic( REF_PIC_LIST_0, aiCurrRefIdx[0] )->getSPS()->getViewId() != uiCurrViewId );
1175  AOT( aiCurrRefIdx[1] >= 0 && pcCU->getSlice()->getRefPic( REF_PIC_LIST_1, aiCurrRefIdx[1] )->getSPS()->getViewId() != uiCurrViewId );
1176
1177  //===== determine parameters for current access unit and base view =====
1178  AOF( m_auiBaseIdList.size() );
1179  UInt            uiBaseId          = m_auiBaseIdList[ 0 ];
1180  TComPic*        pcBasePic         = m_pcAUPicAccess->getPic( uiBaseId );
1181  AOF( pcBasePic );
1182  TComPicYuv*     pcBaseDepthMap    = pcBasePic->getPredDepthMap();
1183  TComPicYuv*     pcBaseRecPic      = pcBasePic->getPicYuvRec   ();
1184  Pel*            piBaseDepthMap    = pcBaseDepthMap->getLumaAddr();
1185  Int             iBaseStride       = pcBaseDepthMap->getStride();
1186
1187  //===== initialize 4x4 block arrays =====
1188  for( Int i4x4BlkY = 0; i4x4BlkY < iNum4x4BlksY; i4x4BlkY++ )
1189  {
1190    for( Int i4x4BlkX = 0; i4x4BlkX < iNum4x4BlksX; i4x4BlkX++ )
1191    {
1192      m_aabDepthSet[ i4x4BlkY ][ i4x4BlkX ] = false;
1193      m_aai4x4Depth[ i4x4BlkY ][ i4x4BlkX ] = PDM_UNDEFINED_DEPTH;
1194    }
1195  }
1196  Int iNum4x4Set = 0;
1197
1198  //===== determine depth based on 4x4 blocks =====
1199  for( Int i4x4BlkY = 0; i4x4BlkY < iNum4x4BlksY; i4x4BlkY++ )
1200  {
1201    for( Int i4x4BlkX = 0; i4x4BlkX < iNum4x4BlksX; i4x4BlkX++ )
1202    {
1203      // position parameters
1204      Int               iCurrBlkPosX        = iCurrPUPosX + ( i4x4BlkX << 2 );
1205      Int               iCurrBlkPosY        = iCurrPUPosY + ( i4x4BlkY << 2 );
1206      Int               iCurrSamplePosX     = iCurrBlkPosX + 1;
1207      Int               iCurrSamplePosY     = iCurrBlkPosY + 1;
1208      Int               iCurrPredDepth      = piCurrDepthMap[ iCurrSamplePosY * iCurrStride + iCurrSamplePosX ];
1209      Int               iCurrPredDisp       = xGetDisparityFromVirtDepth( uiBaseId, iCurrPredDepth );
1210      Int               iBaseSamplePosX     = iCurrSamplePosX + ( ( iCurrPredDisp + 2 ) >> 2 );
1211      Int               iBaseSamplePosY     = iCurrSamplePosY;
1212      iBaseSamplePosX                       = Clip3( 0, pcBaseDepthMap->getWidth () - 1, iBaseSamplePosX );
1213      iBaseSamplePosY                       = Clip3( 0, pcBaseDepthMap->getHeight() - 1, iBaseSamplePosY );
1214
1215      // check for occlusion
1216      if( piBaseDepthMap[ iBaseSamplePosY * iBaseStride + iBaseSamplePosX ] != iCurrPredDepth )
1217      {
1218        continue;
1219      }
1220
1221      // determine base motion data and check prediction mode
1222      Int               iBaseCUAddr;
1223      Int               iBaseAbsPartIdx;
1224      pcBaseRecPic->getCUAddrAndPartIdx( iBaseSamplePosX, iBaseSamplePosY, iBaseCUAddr, iBaseAbsPartIdx );
1225      TComDataCU*       pcBaseCU            = pcBasePic->getCU( iBaseCUAddr );
1226      if( pcBaseCU->getPredictionMode( iBaseAbsPartIdx ) != MODE_INTER && pcBaseCU->getPredictionMode( iBaseAbsPartIdx ) != MODE_SKIP )
1227      {
1228        continue;
1229      }
1230
1231      // check whether base was inter-view predicted
1232      TComCUMvField*    aiBaseMvField[2]    = { pcBaseCU->getCUMvField( REF_PIC_LIST_0 ),       pcBaseCU->getCUMvField( REF_PIC_LIST_1 )       };
1233      Int               aiBaseRefIdx [2]    = { aiBaseMvField[0]->getRefIdx( iBaseAbsPartIdx ), aiBaseMvField[1]->getRefIdx( iBaseAbsPartIdx ) };
1234      Bool              abBaseIntView[2]    = { aiBaseRefIdx[0] >= 0 && pcBaseCU->getSlice()->getRefPic( REF_PIC_LIST_0, aiBaseRefIdx[0] )->getSPS()->getViewId() != uiBaseId,
1235                                                aiBaseRefIdx[1] >= 0 && pcBaseCU->getSlice()->getRefPic( REF_PIC_LIST_1, aiBaseRefIdx[1] )->getSPS()->getViewId() != uiBaseId };
1236      if( abBaseIntView[0] || abBaseIntView[1] )
1237      { // current depth is reliable
1238        m_aai4x4Depth[i4x4BlkY][i4x4BlkX]   = iCurrPredDepth;
1239        m_aabDepthSet[i4x4BlkY][i4x4BlkX]   = true;
1240        iNum4x4Set++;
1241        continue;
1242      }
1243     
1244      // determine depth candidates using an approximate 4-point relationship (if appropriate)
1245      std::vector<Int>  aiDepthCand;
1246      Int               iMinBaseListId      = ( aiBaseRefIdx [0] < 0 ? 1 : 0 );
1247      Int               iMaxBaseListId      = ( aiBaseRefIdx [1] < 0 ? 0 : 1 );
1248      AOT( iMinBaseListId > iMaxBaseListId );
1249      for( Int iCurrRefListId  = iMinCurrListId; iCurrRefListId <= iMaxCurrListId; iCurrRefListId++ )
1250      {
1251        RefPicList      eCurrRefPicList     = RefPicList( iCurrRefListId );
1252        Int             iCurrRefPoc         = pcCU->getSlice()->getRefPOC( eCurrRefPicList, aiCurrRefIdx[ iCurrRefListId ] );
1253        TComPic*        pcCurrRefPic        = pcCU->getSlice()->getRefPic( eCurrRefPicList, aiCurrRefIdx[ iCurrRefListId ] );
1254        TComPicYuv*     pcCurrRefDMap       = pcCurrRefPic->getPredDepthMap();
1255        Pel*            piCurrRefDMap       = pcCurrRefDMap->getLumaAddr();
1256        Int             iCurrRefStride      = pcCurrRefDMap->getStride();
1257        TComMv&         rcCurrMv            = aiCurrMvField[ iCurrRefListId ]->getMv( uiAbsPartIdx );
1258        Int             iCurrRefSamplePosX  = iCurrSamplePosX + ( ( rcCurrMv.getHor() + 2 ) >> 2 );
1259        Int             iCurrRefSamplePosY  = iCurrSamplePosY + ( ( rcCurrMv.getVer() + 2 ) >> 2 );
1260        Int             iCurrRefSamplePosXC = Clip3( 0, pcCurrRefDMap->getWidth () - 1, iCurrRefSamplePosX );
1261        Int             iCurrRefSamplePosYC = Clip3( 0, pcCurrRefDMap->getHeight() - 1, iCurrRefSamplePosY );
1262        Int             iCurrRefDepth       = piCurrRefDMap[ iCurrRefSamplePosYC * iCurrRefStride + iCurrRefSamplePosXC ];
1263
1264        for( Int iBaseRefListId = iMinBaseListId; iBaseRefListId <= iMaxBaseListId; iBaseRefListId++ )
1265        {
1266          RefPicList    eBaseRefPicList     = RefPicList( iBaseRefListId );
1267          Int           iBaseRefPoc         = pcBaseCU->getSlice()->getRefPOC( eBaseRefPicList, aiBaseRefIdx[ iBaseRefListId ] );
1268
1269          if( iCurrRefPoc == iBaseRefPoc )
1270          {
1271            // location and depth for path currView/currAU -> currView/refAU -> baseView/refAU
1272            Int         iCurrRefDisp        = xGetDisparityFromVirtDepth( uiBaseId, iCurrRefDepth );
1273            Int         iBaseRefSamplePosX  = iCurrRefSamplePosX + ( ( iCurrRefDisp + 2 ) >> 2 );
1274            Int         iBaseRefSamplePosY  = iCurrRefSamplePosY;
1275            TComPic*    pcBaseRefPic        = pcBaseCU->getSlice()->getRefPic( eBaseRefPicList, aiBaseRefIdx[ iBaseRefListId ] );
1276            TComPicYuv* pcBaseRefDMap       = pcBaseRefPic->getPredDepthMap();
1277            Pel*        piBaseRefDMap       = pcBaseRefDMap->getLumaAddr();
1278            Int         iBaseRefStride      = pcBaseRefDMap->getStride();
1279            iBaseRefSamplePosX              = Clip3( 0, pcBaseRefDMap->getWidth () - 1, iBaseRefSamplePosX );
1280            iBaseRefSamplePosY              = Clip3( 0, pcBaseRefDMap->getHeight() - 1, iBaseRefSamplePosY );
1281            Int         iBaseRefDepth       = piBaseRefDMap[ iBaseRefSamplePosY * iBaseRefStride + iBaseRefSamplePosX ];
1282
1283            // location and depth for path currView/currAU ->baseView/currAU -> baseView/refAU
1284            TComMv&     rcBaseMv            = aiBaseMvField[ iBaseRefListId ]->getMv( iBaseAbsPartIdx );
1285            Int         iAbsDeltaMvY        = ( rcBaseMv.getAbsVer() > rcCurrMv.getVer() ? rcBaseMv.getAbsVer() - rcCurrMv.getVer() : rcCurrMv.getVer() - rcBaseMv.getAbsVer() );
1286
1287            // check reliability (occlusion in reference access unit / vertical motion vector difference)
1288            if( iBaseRefDepth != iCurrRefDepth || iAbsDeltaMvY > iMaxAbsDeltaMvY )
1289            {
1290              continue;
1291            }
1292
1293            // determine new depth
1294            Int         iCurrCandDisp       = iCurrRefDisp + rcCurrMv.getHor() - rcBaseMv.getHor();
1295            Int         iCurrCandDepth      = xGetVirtDepthFromDisparity( uiBaseId, iCurrCandDisp );
1296            aiDepthCand.push_back( iCurrCandDepth );
1297          } // iCurrRefPoc == iBaseRefPoc
1298        } // iBaseRefListId
1299      } // iCurrRefListId
1300     
1301      // set depth for 4x4 block
1302      if( aiDepthCand.size() )
1303      { // get depth with minimum change (probably most reliable)
1304        Int             iMinAbsDepthChange  = (1<<30);
1305        Int             iDepthForMinChange  = (1<<30);
1306        for( UInt uiCandId = 0; uiCandId < (UInt)aiDepthCand.size(); uiCandId++ )
1307        {
1308          Int           iAbsDeltaDepth      = ( aiDepthCand[uiCandId] > iCurrPredDepth ? aiDepthCand[uiCandId] > iCurrPredDepth : iCurrPredDepth - aiDepthCand[uiCandId] );
1309          if( iAbsDeltaDepth < iMinAbsDepthChange )
1310          {
1311            iMinAbsDepthChange              = iAbsDeltaDepth;
1312            iDepthForMinChange              = aiDepthCand[uiCandId];
1313          }
1314        }
1315        m_aai4x4Depth[i4x4BlkY][i4x4BlkX]   = Min( Max( 0, iDepthForMinChange ), PDM_MAX_ABS_VIRT_DEPTH );
1316        m_aabDepthSet[i4x4BlkY][i4x4BlkX]   = true;
1317        iNum4x4Set++;
1318      }
1319    } // i4x4BlkX
1320  } // i4x4BlkY
1321
1322  //===== fall back (take original depth for 4x4 blocks) ====
1323  if( iNum4x4Set < Max( 1, ( iNum4x4BlksY * iNum4x4BlksX ) >> 2 ) )
1324  {
1325    iNum4x4Set = 0;
1326    for( Int i4x4BlkY = 0; i4x4BlkY < iNum4x4BlksY; i4x4BlkY++ )
1327    {
1328      for( Int i4x4BlkX = 0; i4x4BlkX < iNum4x4BlksX; i4x4BlkX++ )
1329      {
1330        Int             iCurrSamplePosX     = iCurrPUPosX + ( i4x4BlkX << 2 ) + 1;
1331        Int             iCurrSamplePosY     = iCurrPUPosY + ( i4x4BlkY << 2 ) + 1;
1332        m_aai4x4Depth[i4x4BlkY][i4x4BlkX]   = piCurrDepthMap[ iCurrSamplePosY * iCurrStride + iCurrSamplePosX ];
1333        m_aabDepthSet[i4x4BlkY][i4x4BlkX]   = true;
1334        iNum4x4Set++;
1335      }
1336    }
1337  }
1338
1339#if PDM_ONE_DEPTH_PER_PU
1340  //===== set average in 4x4 depth array =====
1341  Int   iSum        = 0;
1342  for( Int i4x4BlkY = 0; i4x4BlkY < iNum4x4BlksY; i4x4BlkY++ )
1343  {
1344    for( Int i4x4BlkX = 0; i4x4BlkX < iNum4x4BlksX; i4x4BlkX++ )
1345    {
1346      if( m_aabDepthSet[ i4x4BlkY ][ i4x4BlkX ] )
1347      {
1348        iSum += m_aai4x4Depth[ i4x4BlkY ][ i4x4BlkX ];
1349      }
1350    }
1351  }
1352  Int   iDepth      = ( iSum + ( iNum4x4Set >> 1 ) ) / iNum4x4Set;
1353  iNum4x4Set        = iNum4x4BlksY * iNum4x4BlksX;
1354  for( Int i4x4BlkY = 0; i4x4BlkY < iNum4x4BlksY; i4x4BlkY++ )
1355  {
1356    for( Int i4x4BlkX = 0; i4x4BlkX < iNum4x4BlksX; i4x4BlkX++ )
1357    {
1358      m_aai4x4Depth[ i4x4BlkY ][ i4x4BlkX ] = iDepth;
1359      m_aabDepthSet[ i4x4BlkY ][ i4x4BlkX ] = true;
1360    }
1361  }
1362#endif
1363 
1364  //===== complete depth arrays =====
1365  while( iNum4x4BlksY * iNum4x4BlksX - iNum4x4Set )
1366  {
1367    for( Int i4x4BlkY = 0; i4x4BlkY < iNum4x4BlksY; i4x4BlkY++ )
1368    {
1369      for( Int i4x4BlkX = 0; i4x4BlkX < iNum4x4BlksX; i4x4BlkX++ )
1370      {
1371        if( !m_aabDepthSet[ i4x4BlkY ][ i4x4BlkX ] )
1372        { // could also use minimum of neighbours (occlusions)
1373          Int iNumNeighbours  = 0;
1374          Int iSumNeighbours  = 0;
1375          if( i4x4BlkY > 0              && m_aabDepthSet[ i4x4BlkY-1 ][ i4x4BlkX   ] ) { iSumNeighbours += m_aai4x4Depth[ i4x4BlkY-1 ][ i4x4BlkX   ]; iNumNeighbours++; }
1376          if( i4x4BlkY < iNum4x4BlksY-1 && m_aabDepthSet[ i4x4BlkY+1 ][ i4x4BlkX   ] ) { iSumNeighbours += m_aai4x4Depth[ i4x4BlkY+1 ][ i4x4BlkX   ]; iNumNeighbours++; }
1377          if( i4x4BlkX > 0              && m_aabDepthSet[ i4x4BlkY   ][ i4x4BlkX-1 ] ) { iSumNeighbours += m_aai4x4Depth[ i4x4BlkY   ][ i4x4BlkX-1 ]; iNumNeighbours++; }
1378          if( i4x4BlkX < iNum4x4BlksX-1 && m_aabDepthSet[ i4x4BlkY   ][ i4x4BlkX+1 ] ) { iSumNeighbours += m_aai4x4Depth[ i4x4BlkY   ][ i4x4BlkX+1 ]; iNumNeighbours++; }
1379          if( iNumNeighbours )
1380          {
1381            m_aai4x4Depth[ i4x4BlkY ][ i4x4BlkX ] = ( iSumNeighbours + ( iNumNeighbours >> 1 ) ) / iNumNeighbours;
1382            m_aabDepthSet[ i4x4BlkY ][ i4x4BlkX ] = true;
1383            iNum4x4Set++;
1384          }
1385        }
1386      }
1387    }
1388  }
1389
1390  //===== set depth values =====
1391  Pel* piDepthMap = pcCUDepthMap->getLumaAddr( uiAbsPartIdx );
1392  Int  iCUStride  = pcCUDepthMap->getStride  ();
1393  for( Int iRow = 0; iRow < iHeight; iRow++, piDepthMap += iCUStride )
1394  {
1395    for( Int iCol = 0; iCol < iWidth; iCol++ )
1396    {
1397      piDepthMap[ iCol ] = m_aai4x4Depth[ iRow >> 2 ][ iCol >> 2 ];
1398    }
1399  }
1400}
1401
1402
1403Void
1404TComDepthMapGenerator::xInterPUDepthMapPrediction( TComDataCU* pcCU, TComYuv* pcCUDepthMap, UInt uiPartIdx )
1405{
1406  m_pcPrediction->motionCompensation( pcCU, pcCUDepthMap, REF_PIC_LIST_X, (Int)uiPartIdx, true ); 
1407}
1408
1409
1410Bool
1411TComDepthMapGenerator::xGetPredDepth( TComDataCU* pcCU, UInt uiPartIdx, Int& riPrdDepth, Int* piPosX, Int* piPosY )
1412{
1413  AOF  ( m_bCreated && m_bInit );
1414  AOF  ( pcCU );
1415  ROFRS( m_bPDMAvailable, false );
1416
1417  TComSlice*    pcSlice     = pcCU->getSlice ();
1418  TComPic*      pcPic       = pcCU->getPic   ();
1419  TComSPS*      pcSPS       = pcSlice->getSPS();
1420  AOF  ( pcPic->getPredDepthMap() );
1421  AOF  ( pcSPS->getViewId() == m_uiCurrViewId );
1422 
1423  //===== get predicted depth and disprity for middle position of current PU ===== 
1424  UInt          uiPartAddr;
1425  Int           iWidth;
1426  Int           iHeight;
1427  pcCU->getPartIndexAndSize( uiPartIdx, uiPartAddr, iWidth, iHeight );
1428  TComPicYuv*   pcPredDepthMap  = pcPic->getPredDepthMap();
1429  Pel*          piPredDepthMap  = pcPredDepthMap->getLumaAddr ( 0 );
1430  Int           iCurrStride     = pcPredDepthMap->getStride   ();
1431  Int           iCurrPosX;
1432  Int           iCurrPosY;
1433  pcPredDepthMap->getTopLeftSamplePos( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr, iCurrPosX, iCurrPosY );
1434  iCurrPosX  += ( iWidth  - 1 ) >> 1;
1435  iCurrPosY  += ( iHeight - 1 ) >> 1;
1436  riPrdDepth  = piPredDepthMap[ iCurrPosX + iCurrPosY * iCurrStride ];
1437  if( piPosX )
1438  {
1439    *piPosX   = iCurrPosX;
1440  }
1441  if( piPosY )
1442  {
1443    *piPosY   = iCurrPosY;
1444  }
1445  return        true;
1446}
1447
1448#endif // DEPTH_MAP_GENERATION
1449
Note: See TracBrowser for help on using the repository browser.