source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibCommon/TComPic.cpp @ 1429

Last change on this file since 1429 was 1428, checked in by seregin, 10 years ago

fix memory allocation

  • Property svn:eol-style set to native
File size: 16.0 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2015, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TComPic.cpp
35    \brief    picture class
36*/
37
38#include "TComPic.h"
39#include "SEI.h"
40
41//! \ingroup TLibCommon
42//! \{
43
44// ====================================================================================================================
45// Constructor / destructor / create / destroy
46// ====================================================================================================================
47
48TComPic::TComPic()
49: m_uiTLayer                              (0)
50, m_bUsedByCurr                           (false)
51, m_bIsLongTerm                           (false)
52, m_pcPicYuvPred                          (NULL)
53, m_pcPicYuvResi                          (NULL)
54, m_bReconstructed                        (false)
55, m_bNeededForOutput                      (false)
56, m_uiCurrSliceIdx                        (0)
57, m_bCheckLTMSB                           (false)
58#if SVC_EXTENSION
59, m_layerId( 0 )
60#endif
61{
62#if SVC_EXTENSION
63  memset( m_pcFullPelBaseRec, 0, sizeof( m_pcFullPelBaseRec ) );
64  memset( m_bSpatialEnhLayer, false, sizeof( m_bSpatialEnhLayer ) );
65  memset( m_equalPictureSizeAndOffsetFlag, false, sizeof( m_equalPictureSizeAndOffsetFlag ) );
66  memset( m_mvScalingFactor, 0, sizeof( m_mvScalingFactor ) );
67  memset( m_posScalingFactor, 0, sizeof( m_posScalingFactor ) );
68#endif
69  for(UInt i=0; i<NUM_PIC_YUV; i++)
70  {
71    m_apcPicYuv[i]      = NULL;
72  }
73}
74
75TComPic::~TComPic()
76{
77}
78#if SVC_EXTENSION
79Void TComPic::create( const TComVPS &vps, const TComSPS &sps, const TComPPS &pps, const Bool bIsVirtual, const UInt layerId )
80{
81  const ChromaFormat chromaFormatIDC = vps.getChromaFormatIdc(&sps, layerId);
82  const Int          iWidth          = vps.getPicWidthInLumaSamples(&sps, layerId);
83  const Int          iHeight         = vps.getPicHeightInLumaSamples(&sps, layerId);
84  const UInt         uiMaxCuWidth    = sps.getMaxCUWidth();
85  const UInt         uiMaxCuHeight   = sps.getMaxCUHeight();
86  const UInt         uiMaxDepth      = sps.getMaxTotalCUDepth();
87 
88  const Window& conformanceWindow = vps.getConformanceWindow( &sps, layerId );
89
90  m_picSym.create( vps, sps, pps, uiMaxDepth, layerId );
91
92  if (!bIsVirtual)
93  {
94    m_apcPicYuv[PIC_YUV_ORG]  = new TComPicYuv;  m_apcPicYuv[PIC_YUV_ORG]->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true, &conformanceWindow );
95    m_apcPicYuv[PIC_YUV_TRUE_ORG]  = new TComPicYuv;  m_apcPicYuv[PIC_YUV_TRUE_ORG]->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true, &conformanceWindow );
96  }
97  m_apcPicYuv[PIC_YUV_REC]  = new TComPicYuv;  m_apcPicYuv[PIC_YUV_REC]->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true, &conformanceWindow );
98
99  for( Int i = 0; i < MAX_LAYERS; i++ )
100  {
101    if( m_bSpatialEnhLayer[i] )
102    {
103      m_pcFullPelBaseRec[i] = new TComPicYuv;  m_pcFullPelBaseRec[i]->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true, &conformanceWindow );
104    }
105  }   
106
107  // there are no SEI messages associated with this picture initially
108  if (m_SEIs.size() > 0)
109  {
110    deleteSEIs (m_SEIs);
111  }
112  m_bUsedByCurr = false;
113}
114#else
115Void TComPic::create( const TComSPS &sps, const TComPPS &pps, const Bool bIsVirtual)
116{
117  const ChromaFormat chromaFormatIDC = sps.getChromaFormatIdc();
118  const Int          iWidth          = sps.getPicWidthInLumaSamples();
119  const Int          iHeight         = sps.getPicHeightInLumaSamples();
120  const UInt         uiMaxCuWidth    = sps.getMaxCUWidth();
121  const UInt         uiMaxCuHeight   = sps.getMaxCUHeight();
122  const UInt         uiMaxDepth      = sps.getMaxTotalCUDepth();
123
124  m_picSym.create( sps, pps, uiMaxDepth );
125  if (!bIsVirtual)
126  {
127    m_apcPicYuv[PIC_YUV_ORG    ]   = new TComPicYuv;  m_apcPicYuv[PIC_YUV_ORG     ]->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true );
128    m_apcPicYuv[PIC_YUV_TRUE_ORG]  = new TComPicYuv;  m_apcPicYuv[PIC_YUV_TRUE_ORG]->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true );
129  }
130  m_apcPicYuv[PIC_YUV_REC]  = new TComPicYuv;  m_apcPicYuv[PIC_YUV_REC]->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true );
131
132  // there are no SEI messages associated with this picture initially
133  if (m_SEIs.size() > 0)
134  {
135    deleteSEIs (m_SEIs);
136  }
137  m_bUsedByCurr = false;
138}
139#endif
140
141Void TComPic::destroy()
142{
143  m_picSym.destroy();
144
145  for(UInt i=0; i<NUM_PIC_YUV; i++)
146  {
147    if (m_apcPicYuv[i])
148    {
149      m_apcPicYuv[i]->destroy();
150      delete m_apcPicYuv[i];
151      m_apcPicYuv[i]  = NULL;
152    }
153  }
154
155  deleteSEIs(m_SEIs);
156#if SVC_EXTENSION
157  for( Int i = 0; i < MAX_LAYERS; i++ )
158  {
159    if( m_bSpatialEnhLayer[i] && m_pcFullPelBaseRec[i] )
160    {
161      m_pcFullPelBaseRec[i]->destroy();
162      delete m_pcFullPelBaseRec[i];
163      m_pcFullPelBaseRec[i]  = NULL;
164    }
165  }
166
167  for( Int comp = 0; comp < 2; comp++ )
168  {
169    if( m_mvScalingFactor[comp] )
170    {
171      delete [] m_mvScalingFactor[comp];
172      m_mvScalingFactor[comp] = NULL;
173    }
174
175    if( m_posScalingFactor[comp] )
176    {
177      delete [] m_posScalingFactor[comp];
178      m_posScalingFactor[comp] = NULL;
179    }
180  }
181#endif
182}
183
184Void TComPic::compressMotion()
185{
186  TComPicSym* pPicSym = getPicSym();
187  for ( UInt uiCUAddr = 0; uiCUAddr < pPicSym->getNumberOfCtusInFrame(); uiCUAddr++ )
188  {
189    TComDataCU* pCtu = pPicSym->getCtu(uiCUAddr);
190    pCtu->compressMV();
191  }
192}
193
194Bool  TComPic::getSAOMergeAvailability(Int currAddr, Int mergeAddr)
195{
196  Bool mergeCtbInSliceSeg = (mergeAddr >= getPicSym()->getCtuTsToRsAddrMap(getCtu(currAddr)->getSlice()->getSliceCurStartCtuTsAddr()));
197  Bool mergeCtbInTile     = (getPicSym()->getTileIdxMap(mergeAddr) == getPicSym()->getTileIdxMap(currAddr));
198  return (mergeCtbInSliceSeg && mergeCtbInTile);
199}
200
201UInt TComPic::getSubstreamForCtuAddr(const UInt ctuAddr, const Bool bAddressInRaster, TComSlice *pcSlice)
202{
203  UInt subStrm;
204  const bool bWPPEnabled=pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag();
205  const TComPicSym &picSym            = *(getPicSym());
206
207  if ((bWPPEnabled && picSym.getFrameHeightInCtus()>1) || (picSym.getNumTiles()>1)) // wavefronts, and possibly tiles being used.
208  {
209    if (bWPPEnabled)
210    {
211      const UInt ctuRsAddr                = bAddressInRaster?ctuAddr : picSym.getCtuTsToRsAddrMap(ctuAddr);
212      const UInt frameWidthInCtus         = picSym.getFrameWidthInCtus();
213      const UInt tileIndex                = picSym.getTileIdxMap(ctuRsAddr);
214      const UInt numTileColumns           = (picSym.getNumTileColumnsMinus1()+1);
215      const TComTile *pTile               = picSym.getTComTile(tileIndex);
216      const UInt firstCtuRsAddrOfTile     = pTile->getFirstCtuRsAddr();
217      const UInt tileYInCtus              = firstCtuRsAddrOfTile / frameWidthInCtus;
218      // independent tiles => substreams are "per tile"
219      const UInt ctuLine                  = ctuRsAddr / frameWidthInCtus;
220      const UInt startingSubstreamForTile =(tileYInCtus*numTileColumns) + (pTile->getTileHeightInCtus()*(tileIndex%numTileColumns));
221      subStrm = startingSubstreamForTile + (ctuLine - tileYInCtus);
222    }
223    else
224    {
225      const UInt ctuRsAddr                = bAddressInRaster?ctuAddr : picSym.getCtuTsToRsAddrMap(ctuAddr);
226      const UInt tileIndex                = picSym.getTileIdxMap(ctuRsAddr);
227      subStrm=tileIndex;
228    }
229  }
230  else
231  {
232    // dependent tiles => substreams are "per frame".
233    subStrm = 0;
234  }
235  return subStrm;
236}
237
238#if SVC_EXTENSION
239Void TComPic::createMvScalingFactor(UInt numOfILRPs)
240{
241  // picture object might be reused and hence m_mvScalingFactor[0] can be already allocated
242  if(m_mvScalingFactor[0])
243  {
244    delete m_mvScalingFactor[0];
245  }
246  m_mvScalingFactor[0] = new Int[numOfILRPs];
247
248  // picture object might be reused and hence m_mvScalingFactor[1] can be already allocated
249  if(m_mvScalingFactor[1])
250  {
251    delete m_mvScalingFactor[1];
252  }
253  m_mvScalingFactor[1] = new Int[numOfILRPs];
254}
255
256Void TComPic::createPosScalingFactor(UInt numOfILRPs)
257{
258  // picture object might be reused and hence m_posScalingFactor[0] can be already allocated
259  if(m_posScalingFactor[0])
260  {
261    delete m_posScalingFactor[0];
262  }
263  m_posScalingFactor[0] = new Int[numOfILRPs];
264
265  // picture object might be reused and hence m_posScalingFactor[1] can be already allocated
266  if(m_posScalingFactor[1])
267  {
268    delete m_posScalingFactor[1];
269  }
270  m_posScalingFactor[1] = new Int[numOfILRPs];
271}
272
273Void copyOnetoOnePicture(    // SVC_NONCOLL
274                  Pel *in,       
275                  Pel *out,     
276                  Int nCols,
277                  Int nRows, 
278                  Int fullRowWidth)
279{
280  Int rX;
281
282  for (rX = 0; rX < nRows; rX++)       
283  {
284    memcpy( out, in, sizeof(Pel) * nCols );
285    in = in + fullRowWidth;
286    out = out + fullRowWidth;
287  }
288}
289
290Void TComPic::copyUpsampledPictureYuv(TComPicYuv*   pcPicYuvIn, TComPicYuv*   pcPicYuvOut)
291{
292  Int upsampledRowWidthLuma = pcPicYuvOut->getStride(COMPONENT_Y); // 2 * pcPicYuvOut->getLumaMargin() + pcPicYuvOut->getWidth();
293  Int upsampledRowWidthCroma = pcPicYuvOut->getStride(COMPONENT_Cb); //2 * pcPicYuvOut->getChromaMargin() + (pcPicYuvOut->getWidth()>>1);
294
295  copyOnetoOnePicture(
296    pcPicYuvIn->getAddr(COMPONENT_Y),       
297    pcPicYuvOut->getAddr(COMPONENT_Y),     
298    pcPicYuvOut->getWidth(COMPONENT_Y), 
299    pcPicYuvOut->getHeight(COMPONENT_Y),
300    upsampledRowWidthLuma);
301  copyOnetoOnePicture(
302    pcPicYuvIn->getAddr(COMPONENT_Cr),       
303    pcPicYuvOut->getAddr(COMPONENT_Cr),     
304    pcPicYuvOut->getWidth(COMPONENT_Y)>>1, 
305    pcPicYuvOut->getHeight(COMPONENT_Y)>>1,
306    upsampledRowWidthCroma);
307  copyOnetoOnePicture(
308    pcPicYuvIn->getAddr(COMPONENT_Cb),       
309    pcPicYuvOut->getAddr(COMPONENT_Cb),     
310    pcPicYuvOut->getWidth(COMPONENT_Y)>>1, 
311    pcPicYuvOut->getHeight(COMPONENT_Y)>>1,
312    upsampledRowWidthCroma);
313}
314
315Void TComPic::copyUpsampledMvField(UInt refLayerIdc, Int** mvScalingFactor, Int** posScalingFactor)
316{
317  const TComSPS *sps       = getSlice(0)->getSPS();
318  const UInt uiMaxDepth    = sps->getMaxTotalCUDepth();
319  const UInt numPartitions = 1<<(uiMaxDepth<<1);
320  const UInt widthMinPU    = sps->getMaxCUWidth()  / (1<<uiMaxDepth);
321  const UInt heightMinPU   = sps->getMaxCUHeight() / (1<<uiMaxDepth);
322  const Int  unitNum       = max( 1, (Int)((16/widthMinPU)*(16/heightMinPU)) ); 
323
324  for(UInt cuIdx = 0; cuIdx < getPicSym()->getNumberOfCtusInFrame(); cuIdx++)  //each LCU
325  {
326    TComDataCU* pcCUDes = getCtu(cuIdx);
327
328    for(UInt absPartIdx = 0; absPartIdx < numPartitions; absPartIdx+=unitNum )  //each 16x16 unit
329    {
330      //pixel position of each unit in up-sampled layer
331      UInt pelX = pcCUDes->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[absPartIdx] ];
332      UInt pelY = pcCUDes->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[absPartIdx] ];
333      UInt baseCUAddr, baseAbsPartIdx;
334
335      TComDataCU *pcColCU = 0;
336      pcColCU = pcCUDes->getBaseColCU(refLayerIdc, pelX, pelY, baseCUAddr, baseAbsPartIdx, posScalingFactor, true);
337
338      if( pcColCU && pcColCU->getPredictionMode(baseAbsPartIdx) == MODE_INTER )  //base layer unit not skip and invalid mode
339      {
340        for(UInt refPicList = 0; refPicList < 2; refPicList++)  //for each reference list
341        {
342          TComMvField sMvFieldBase, sMvField;
343          pcColCU->getMvField( pcColCU, baseAbsPartIdx, (RefPicList)refPicList, sMvFieldBase);
344          pcCUDes->scaleBaseMV( refLayerIdc, sMvField, sMvFieldBase, mvScalingFactor );
345
346          pcCUDes->getCUMvField((RefPicList)refPicList)->setMvField(sMvField, absPartIdx);
347          pcCUDes->setPredictionMode(absPartIdx, MODE_INTER);
348        }
349      }
350      else
351      {
352        TComMvField zeroMvField;  //zero MV and invalid reference index
353        pcCUDes->getCUMvField(REF_PIC_LIST_0)->setMvField(zeroMvField, absPartIdx);
354        pcCUDes->getCUMvField(REF_PIC_LIST_1)->setMvField(zeroMvField, absPartIdx);
355        pcCUDes->setPredictionMode(absPartIdx, MODE_INTRA);
356      }
357
358      for(UInt i = 1; i < unitNum; i++ ) 
359      {
360        pcCUDes->getCUMvField(REF_PIC_LIST_0)->setMvField(pcCUDes->getCUMvField(REF_PIC_LIST_0)->getMv(absPartIdx), pcCUDes->getCUMvField(REF_PIC_LIST_0)->getRefIdx(absPartIdx), absPartIdx + i);
361        pcCUDes->getCUMvField(REF_PIC_LIST_1)->setMvField(pcCUDes->getCUMvField(REF_PIC_LIST_1)->getMv(absPartIdx), pcCUDes->getCUMvField(REF_PIC_LIST_1)->getRefIdx(absPartIdx), absPartIdx + i);
362        pcCUDes->setPredictionMode(absPartIdx+i, pcCUDes->getPredictionMode(absPartIdx));
363      }
364    }
365    memset( pcCUDes->getPartitionSize(), SIZE_2Nx2N, sizeof(Char)*numPartitions );
366  }
367}
368
369Void TComPic::initUpsampledMvField()
370{
371  const TComSPS *sps         = getSlice(0)->getSPS();
372  const UInt uiMaxDepth      = sps->getMaxTotalCUDepth();
373  const UInt uiNumPartitions = 1<<(uiMaxDepth<<1);
374
375  for(UInt cuIdx = 0; cuIdx < getPicSym()->getNumberOfCtusInFrame(); cuIdx++)  //each LCU
376  {
377    TComDataCU* pcCUDes = getCtu(cuIdx);
378    TComMvField zeroMvField;
379    for(UInt list = 0; list < 2; list++)  //each reference list
380    {
381      for(UInt i = 0; i < uiNumPartitions; i++ ) 
382      {
383        pcCUDes->getCUMvField(REF_PIC_LIST_0)->setMvField(zeroMvField, i);
384        pcCUDes->getCUMvField(REF_PIC_LIST_1)->setMvField(zeroMvField, i);
385        pcCUDes->setPredictionMode(i, MODE_INTRA);
386        pcCUDes->setPartitionSize(i, SIZE_2Nx2N);
387      }
388    }
389  }
390  return;
391}
392
393Bool TComPic::checkSameRefInfo()
394{
395  Bool bSameRefInfo = true;
396  TComSlice * pSlice0 = getSlice( 0 );
397  for( UInt uSliceID = getNumAllocatedSlice() - 1 ; bSameRefInfo && uSliceID > 0 ; uSliceID-- )
398  {
399    TComSlice * pSliceN = getSlice( uSliceID );
400    if( pSlice0->getSliceType() != pSliceN->getSliceType() )
401    {
402      bSameRefInfo = false;
403    }
404    else if( pSlice0->getSliceType() != I_SLICE )
405    {
406      Int nListNum = pSlice0->getSliceType() == B_SLICE ? 2 : 1;
407      for( Int nList = 0 ; nList < nListNum ; nList++ )
408      {
409        RefPicList eRefList = ( RefPicList )nList;
410        if( pSlice0->getNumRefIdx( eRefList ) == pSliceN->getNumRefIdx( eRefList ) )
411        {
412          for( Int refIdx = pSlice0->getNumRefIdx( eRefList ) - 1 ; refIdx >= 0 ; refIdx-- )
413          {
414            if( pSlice0->getRefPic( eRefList , refIdx ) != pSliceN->getRefPic( eRefList , refIdx ) )
415            {
416              bSameRefInfo = false;
417              break;
418            }
419          }
420        }
421        else
422        {
423          bSameRefInfo = false;
424          break;
425        }
426      }
427    }
428  }
429
430  return( bSameRefInfo ); 
431}
432#endif //SVC_EXTENSION
433
434//! \}
Note: See TracBrowser for help on using the repository browser.