source: 3DVCSoftware/branches/HTM-15.2-dev/source/Lib/TLibCommon/TComPicYuv.cpp @ 1360

Last change on this file since 1360 was 1360, checked in by tech, 9 years ago

Update to HM-16.7.

  • Property svn:eol-style set to native
File size: 12.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-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     TComPicYuv.cpp
35    \brief    picture YUV buffer class
36*/
37
38#include <cstdlib>
39#include <assert.h>
40#include <memory.h>
41
42#ifdef __APPLE__
43#include <malloc/malloc.h>
44#else
45#include <malloc.h>
46#endif
47
48#include "TComPicYuv.h"
49#include "TLibVideoIO/TVideoIOYuv.h"
50
51//! \ingroup TLibCommon
52//! \{
53
54TComPicYuv::TComPicYuv()
55{
56  for(UInt i=0; i<MAX_NUM_COMPONENT; i++)
57  {
58    m_apiPicBuf[i]    = NULL;   // Buffer (including margin)
59    m_piPicOrg[i]     = NULL;    // m_apiPicBufY + m_iMarginLuma*getStride() + m_iMarginLuma
60  }
61
62  for(UInt i=0; i<MAX_NUM_CHANNEL_TYPE; i++)
63  {
64    m_ctuOffsetInBuffer[i]=0;
65    m_subCuOffsetInBuffer[i]=0;
66  }
67
68  m_bIsBorderExtended = false;
69}
70
71
72
73
74TComPicYuv::~TComPicYuv()
75{
76}
77
78
79
80
81Void TComPicYuv::createWithoutCUInfo ( const Int picWidth,                 ///< picture width
82                                       const Int picHeight,                ///< picture height
83                                       const ChromaFormat chromaFormatIDC, ///< chroma format
84                                       const Bool bUseMargin,              ///< if true, then a margin of uiMaxCUWidth+16 and uiMaxCUHeight+16 is created around the image.
85                                       const UInt maxCUWidth,              ///< used for margin only
86                                       const UInt maxCUHeight)             ///< used for margin only
87
88{
89  m_picWidth          = picWidth;
90  m_picHeight         = picHeight;
91
92#if NH_3D_IV_MERGE
93// Check if m_iBaseUnitWidth and m_iBaseUnitHeight need to be derived here
94#endif
95
96  m_chromaFormatIDC   = chromaFormatIDC;
97  m_marginX          = (bUseMargin?maxCUWidth:0) + 16;   // for 16-byte alignment
98  m_marginY          = (bUseMargin?maxCUHeight:0) + 16;  // margin for 8-tap filter and infinite padding
99  m_bIsBorderExtended = false;
100
101  // assign the picture arrays and set up the ptr to the top left of the original picture
102  for(UInt comp=0; comp<getNumberValidComponents(); comp++)
103  {
104    const ComponentID ch=ComponentID(comp);
105    m_apiPicBuf[comp] = (Pel*)xMalloc( Pel, getStride(ch) * getTotalHeight(ch));
106    m_piPicOrg[comp]  = m_apiPicBuf[comp] + (m_marginY >> getComponentScaleY(ch)) * getStride(ch) + (m_marginX >> getComponentScaleX(ch));
107  }
108  // initialize pointers for unused components to NULL
109  for(UInt comp=getNumberValidComponents();comp<MAX_NUM_COMPONENT; comp++)
110    {
111    }
112
113  for(Int chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++)
114    {
115    m_ctuOffsetInBuffer[chan]   = NULL;
116    m_subCuOffsetInBuffer[chan] = NULL;
117    }
118  }
119
120
121
122Void TComPicYuv::create ( const Int picWidth,                 ///< picture width
123                          const Int picHeight,                ///< picture height
124                          const ChromaFormat chromaFormatIDC, ///< chroma format
125                          const UInt maxCUWidth,              ///< used for generating offsets to CUs.
126                          const UInt maxCUHeight,             ///< used for generating offsets to CUs.
127                          const UInt maxCUDepth,              ///< used for generating offsets to CUs.
128                          const Bool bUseMargin)              ///< if true, then a margin of uiMaxCUWidth+16 and uiMaxCUHeight+16 is created around the image.
129
130{
131  createWithoutCUInfo(picWidth, picHeight, chromaFormatIDC, bUseMargin, maxCUWidth, maxCUHeight);
132
133#if NH_3D_IV_MERGE
134  m_iBaseUnitWidth  = maxCUWidth  >> maxCUDepth;
135  m_iBaseUnitHeight = maxCUHeight >> maxCUDepth;
136#endif
137
138
139  const Int numCuInWidth  = m_picWidth  / maxCUWidth  + (m_picWidth  % maxCUWidth  != 0);
140  const Int numCuInHeight = m_picHeight / maxCUHeight + (m_picHeight % maxCUHeight != 0);
141  for(Int chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++)
142  {
143    const ChannelType ch= ChannelType(chan);
144    const Int ctuHeight = maxCUHeight>>getChannelTypeScaleY(ch);
145    const Int ctuWidth  = maxCUWidth>>getChannelTypeScaleX(ch);
146    const Int stride = getStride(ch);
147
148    m_ctuOffsetInBuffer[chan] = new Int[numCuInWidth * numCuInHeight];
149
150    for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++)
151    {
152      for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++)
153      {
154        m_ctuOffsetInBuffer[chan][cuRow * numCuInWidth + cuCol] = stride * cuRow * ctuHeight + cuCol * ctuWidth;
155      }
156    }
157
158    m_subCuOffsetInBuffer[chan] = new Int[(size_t)1 << (2 * maxCUDepth)];
159
160    const Int numSubBlockPartitions=(1<<maxCUDepth);
161    const Int minSubBlockHeight    =(ctuHeight >> maxCUDepth);
162    const Int minSubBlockWidth     =(ctuWidth  >> maxCUDepth);
163
164    for (Int buRow = 0; buRow < numSubBlockPartitions; buRow++)
165    {
166      for (Int buCol = 0; buCol < numSubBlockPartitions; buCol++)
167      {
168        m_subCuOffsetInBuffer[chan][(buRow << maxCUDepth) + buCol] = stride  * buRow * minSubBlockHeight + buCol * minSubBlockWidth;
169      }
170    }
171  }
172}
173
174
175
176Void TComPicYuv::destroy()
177{
178  for(Int comp=0; comp<MAX_NUM_COMPONENT; comp++)
179  {
180    m_piPicOrg[comp] = NULL;
181
182    if( m_apiPicBuf[comp] )
183    {
184      xFree( m_apiPicBuf[comp] );
185      m_apiPicBuf[comp] = NULL;
186    }
187  }
188
189  for(UInt chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++)
190  {
191    if (m_ctuOffsetInBuffer[chan])
192    {
193      delete[] m_ctuOffsetInBuffer[chan];
194      m_ctuOffsetInBuffer[chan] = NULL;
195    }
196    if (m_subCuOffsetInBuffer[chan])
197    {
198      delete[] m_subCuOffsetInBuffer[chan];
199      m_subCuOffsetInBuffer[chan] = NULL;
200    }
201  }
202}
203
204
205
206Void  TComPicYuv::copyToPic (TComPicYuv*  pcPicYuvDst) const
207{
208  assert( m_chromaFormatIDC == pcPicYuvDst->getChromaFormat() );
209
210  for(Int comp=0; comp<getNumberValidComponents(); comp++)
211  {
212    const ComponentID compId=ComponentID(comp);
213    const Int width     = getWidth(compId);
214    const Int height    = getHeight(compId);
215    const Int strideSrc = getStride(compId);
216    assert(pcPicYuvDst->getWidth(compId) == width);
217    assert(pcPicYuvDst->getHeight(compId) == height);
218    if (strideSrc==pcPicYuvDst->getStride(compId))
219  {
220      ::memcpy ( pcPicYuvDst->getBuf(compId), getBuf(compId), sizeof(Pel)*strideSrc*getTotalHeight(compId));
221    }
222    else
223    {
224      const Pel *pSrc       = getAddr(compId);
225            Pel *pDest      = pcPicYuvDst->getAddr(compId);
226      const UInt strideDest = pcPicYuvDst->getStride(compId);
227
228      for(Int y=0; y<height; y++, pSrc+=strideSrc, pDest+=strideDest)
229      {
230        ::memcpy(pDest, pSrc, width*sizeof(Pel));
231      }
232    }
233  }
234}
235
236
237Void TComPicYuv::extendPicBorder ()
238{
239  if ( m_bIsBorderExtended )
240  {
241    return;
242  }
243
244  for(Int comp=0; comp<getNumberValidComponents(); comp++)
245  {
246    const ComponentID compId=ComponentID(comp);
247    Pel *piTxt=getAddr(compId); // piTxt = point to (0,0) of image within bigger picture.
248    const Int stride=getStride(compId);
249    const Int width=getWidth(compId);
250    const Int height=getHeight(compId);
251    const Int marginX=getMarginX(compId);
252    const Int marginY=getMarginY(compId);
253
254    Pel*  pi = piTxt;
255    // do left and right margins
256    for (Int y = 0; y < height; y++)
257    {
258      for (Int x = 0; x < marginX; x++ )
259      {
260        pi[ -marginX + x ] = pi[0];
261        pi[    width + x ] = pi[width-1];
262      }
263      pi += stride;
264    }
265
266    // pi is now the (0,height) (bottom left of image within bigger picture
267    pi -= (stride + marginX);
268    // pi is now the (-marginX, height-1)
269    for (Int y = 0; y < marginY; y++ )
270    {
271      ::memcpy( pi + (y+1)*stride, pi, sizeof(Pel)*(width + (marginX<<1)) );
272    }
273
274    // pi is still (-marginX, height-1)
275    pi -= ((height-1) * stride);
276    // pi is now (-marginX, 0)
277    for (Int y = 0; y < marginY; y++ )
278    {
279      ::memcpy( pi - (y+1)*stride, pi, sizeof(Pel)*(width + (marginX<<1)) );
280    }
281  }
282
283  m_bIsBorderExtended = true;
284}
285
286
287
288// NOTE: This function is never called, but may be useful for developers.
289Void TComPicYuv::dump (const std::string &fileName, const BitDepths &bitDepths, const Bool bAppend, const Bool bForceTo8Bit) const
290{
291  FILE *pFile = fopen (fileName.c_str(), bAppend?"ab":"wb");
292
293  Bool is16bit=false;
294  for(Int comp = 0; comp < getNumberValidComponents() && !bForceTo8Bit; comp++)
295  {
296    if (bitDepths.recon[toChannelType(ComponentID(comp))]>8)
297  {
298      is16bit=true;
299    }
300  }
301
302  for(Int comp = 0; comp < getNumberValidComponents(); comp++)
303  {
304    const ComponentID  compId = ComponentID(comp);
305    const Pel         *pi     = getAddr(compId);
306    const Int          stride = getStride(compId);
307    const Int          height = getHeight(compId);
308    const Int          width  = getWidth(compId);
309
310    if (is16bit)
311    {
312    for (Int y = 0; y < height; y++ )
313    {
314      for (Int x = 0; x < width; x++ )
315      {
316          UChar uc = (UChar)((pi[x]>>0) & 0xff);
317        fwrite( &uc, sizeof(UChar), 1, pFile );
318          uc = (UChar)((pi[x]>>8) & 0xff);
319          fwrite( &uc, sizeof(UChar), 1, pFile );
320      }
321      pi += stride;
322    }
323  }
324    else
325    {
326      const Int shift  = bitDepths.recon[toChannelType(compId)] - 8;
327      const Int offset = (shift>0)?(1<<(shift-1)):0;
328      for (Int y = 0; y < height; y++ )
329      {
330        for (Int x = 0; x < width; x++ )
331        {
332          UChar uc = (UChar)Clip3<Pel>(0, 255, (pi[x]+offset)>>shift);
333          fwrite( &uc, sizeof(UChar), 1, pFile );
334        }
335        pi += stride;
336      }
337    }
338  }
339
340  fclose(pFile);
341}
342#if NH_3D_IV_MERGE
343Void
344TComPicYuv::getTopLeftSamplePos( Int iCuAddr, Int iAbsZorderIdx, Int& riX, Int& riY )
345{
346  Int iRastPartIdx    = g_auiZscanToRaster[iAbsZorderIdx];
347  Int iCuSizeInBases  = m_iCuWidth   / m_iBaseUnitWidth;
348  Int iCuX            = iCuAddr      % m_iNumCuInWidth;
349  Int iCuY            = iCuAddr      / m_iNumCuInWidth;
350  Int iBaseX          = iRastPartIdx % iCuSizeInBases;
351  Int iBaseY          = iRastPartIdx / iCuSizeInBases;
352  riX                 = iCuX * m_iCuWidth  + iBaseX * m_iBaseUnitWidth;
353  riY                 = iCuY * m_iCuHeight + iBaseY * m_iBaseUnitHeight; 
354}
355
356Void
357TComPicYuv::getCUAddrAndPartIdx( Int iX, Int iY, Int& riCuAddr, Int& riAbsZorderIdx )
358{
359  Int iCuX            = iX / m_iCuWidth;
360  Int iCuY            = iY / m_iCuHeight;
361  Int iBaseX          = ( iX - iCuX * m_iCuWidth  ) / m_iBaseUnitWidth;
362  Int iBaseY          = ( iY - iCuY * m_iCuHeight ) / m_iBaseUnitHeight;
363  Int iCuSizeInBases  = m_iCuWidth                  / m_iBaseUnitWidth;
364  riCuAddr            = iCuY   * m_iNumCuInWidth + iCuX;
365  Int iRastPartIdx    = iBaseY * iCuSizeInBases  + iBaseX;
366  riAbsZorderIdx      = g_auiRasterToZscan[ iRastPartIdx ];
367}
368
369#endif
370
371#if NH_3D_VSO
372Void TComPicYuv::setChromaTo( Pel pVal )
373{
374  xSetPels( getAddr( COMPONENT_Cb ), getStride( COMPONENT_Cb ), getWidth( COMPONENT_Cb), getHeight( COMPONENT_Cb ), pVal ); 
375  xSetPels( getAddr( COMPONENT_Cr ), getStride( COMPONENT_Cr ), getWidth( COMPONENT_Cr), getHeight( COMPONENT_Cr ), pVal );
376}
377
378Void TComPicYuv::xSetPels( Pel* piPelSource , Int iSourceStride, Int iWidth, Int iHeight, Pel iVal )
379{
380  for (Int iYPos = 0; iYPos < iHeight; iYPos++)
381  {
382    for (Int iXPos = 0; iXPos < iWidth; iXPos++)
383    {
384      piPelSource[iXPos] = iVal; 
385    }
386    piPelSource += iSourceStride; 
387  }
388}
389#endif
390//! \}
Note: See TracBrowser for help on using the repository browser.