source: 3DVCSoftware/branches/HTM-14.1-update-dev1/source/Lib/TLibCommon/TComPicYuv.cpp @ 1304

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

Merged 14.1-update-dev2@1277.

  • Property svn:eol-style set to native
File size: 11.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     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::create ( const Int iPicWidth,                ///< picture width
82                          const Int iPicHeight,               ///< picture height
83                          const ChromaFormat chromaFormatIDC, ///< chroma format
84                          const UInt uiMaxCUWidth,            ///< used for generating offsets to CUs. Can use iPicWidth if no offsets are required
85                          const UInt uiMaxCUHeight,           ///< used for generating offsets to CUs. Can use iPicHeight if no offsets are required
86                          const UInt uiMaxCUDepth,            ///< used for generating offsets to CUs. Can use 0 if no offsets are required
87                          const Bool bUseMargin)              ///< if true, then a margin of uiMaxCUWidth+16 and uiMaxCUHeight+16 is created around the image.
88
89{
90  m_iPicWidth         = iPicWidth;
91  m_iPicHeight        = iPicHeight;
92
93#if NH_3D_IV_MERGE
94  m_iCuWidth        = uiMaxCUWidth;
95  m_iCuHeight       = uiMaxCUHeight;
96
97  m_iNumCuInWidth   = m_iPicWidth / m_iCuWidth;
98  m_iNumCuInWidth  += ( m_iPicWidth % m_iCuWidth ) ? 1 : 0;
99
100  m_iBaseUnitWidth  = uiMaxCUWidth  >> uiMaxCUDepth;
101  m_iBaseUnitHeight = uiMaxCUHeight >> uiMaxCUDepth;
102#endif
103
104  m_chromaFormatIDC   = chromaFormatIDC;
105  m_iMarginX          = (bUseMargin?uiMaxCUWidth:0) + 16;   // for 16-byte alignment
106  m_iMarginY          = (bUseMargin?uiMaxCUHeight:0) + 16;  // margin for 8-tap filter and infinite padding
107  m_bIsBorderExtended = false;
108
109  // assign the picture arrays and set up the ptr to the top left of the original picture
110  {
111    Int chan=0;
112    for(; chan<getNumberValidComponents(); chan++)
113    {
114      const ComponentID ch=ComponentID(chan);
115      m_apiPicBuf[chan] = (Pel*)xMalloc( Pel, getStride(ch)       * getTotalHeight(ch));
116      m_piPicOrg[chan]  = m_apiPicBuf[chan] + (m_iMarginY >> getComponentScaleY(ch))   * getStride(ch)       + (m_iMarginX >> getComponentScaleX(ch));
117    }
118    for(;chan<MAX_NUM_COMPONENT; chan++)
119    {
120      m_apiPicBuf[chan] = NULL;
121      m_piPicOrg[chan]  = NULL;
122    }
123  }
124
125
126  const Int numCuInWidth  = m_iPicWidth  / uiMaxCUWidth  + (m_iPicWidth  % uiMaxCUWidth  != 0);
127  const Int numCuInHeight = m_iPicHeight / uiMaxCUHeight + (m_iPicHeight % uiMaxCUHeight != 0);
128  for(Int chan=0; chan<2; chan++)
129  {
130    const ComponentID ch=ComponentID(chan);
131    const Int ctuHeight=uiMaxCUHeight>>getComponentScaleY(ch);
132    const Int ctuWidth=uiMaxCUWidth>>getComponentScaleX(ch);
133    const Int stride = getStride(ch);
134
135    m_ctuOffsetInBuffer[chan] = new Int[numCuInWidth * numCuInHeight];
136
137    for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++)
138    {
139      for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++)
140      {
141        m_ctuOffsetInBuffer[chan][cuRow * numCuInWidth + cuCol] = stride * cuRow * ctuHeight + cuCol * ctuWidth;
142      }
143    }
144
145    m_subCuOffsetInBuffer[chan] = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
146
147    const Int numSubBlockPartitions=(1<<uiMaxCUDepth);
148    const Int minSubBlockHeight    =(ctuHeight >> uiMaxCUDepth);
149    const Int minSubBlockWidth     =(ctuWidth  >> uiMaxCUDepth);
150
151    for (Int buRow = 0; buRow < numSubBlockPartitions; buRow++)
152    {
153      for (Int buCol = 0; buCol < numSubBlockPartitions; buCol++)
154      {
155        m_subCuOffsetInBuffer[chan][(buRow << uiMaxCUDepth) + buCol] = stride  * buRow * minSubBlockHeight + buCol * minSubBlockWidth;
156      }
157    }
158  }
159  return;
160}
161
162
163
164Void TComPicYuv::destroy()
165{
166  for(Int chan=0; chan<MAX_NUM_COMPONENT; chan++)
167  {
168    m_piPicOrg[chan] = NULL;
169
170    if( m_apiPicBuf[chan] )
171    {
172      xFree( m_apiPicBuf[chan] );
173      m_apiPicBuf[chan] = NULL;
174    }
175  }
176
177  for(UInt chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++)
178  {
179    if (m_ctuOffsetInBuffer[chan])
180    {
181      delete[] m_ctuOffsetInBuffer[chan];
182      m_ctuOffsetInBuffer[chan] = NULL;
183    }
184    if (m_subCuOffsetInBuffer[chan])
185    {
186      delete[] m_subCuOffsetInBuffer[chan];
187      m_subCuOffsetInBuffer[chan] = NULL;
188    }
189  }
190}
191
192
193
194Void  TComPicYuv::copyToPic (TComPicYuv*  pcPicYuvDst) const
195{
196  assert( m_iPicWidth  == pcPicYuvDst->getWidth(COMPONENT_Y)  );
197  assert( m_iPicHeight == pcPicYuvDst->getHeight(COMPONENT_Y) );
198  assert( m_chromaFormatIDC == pcPicYuvDst->getChromaFormat() );
199
200  for(Int chan=0; chan<getNumberValidComponents(); chan++)
201  {
202    const ComponentID ch=ComponentID(chan);
203    ::memcpy ( pcPicYuvDst->getBuf(ch), m_apiPicBuf[ch], sizeof (Pel) * getStride(ch) * getTotalHeight(ch));
204  }
205  return;
206}
207
208
209Void TComPicYuv::extendPicBorder ()
210{
211  if ( m_bIsBorderExtended )
212  {
213    return;
214  }
215
216  for(Int chan=0; chan<getNumberValidComponents(); chan++)
217  {
218    const ComponentID ch=ComponentID(chan);
219    Pel *piTxt=getAddr(ch); // piTxt = point to (0,0) of image within bigger picture.
220    const Int iStride=getStride(ch);
221    const Int iWidth=getWidth(ch);
222    const Int iHeight=getHeight(ch);
223    const Int iMarginX=getMarginX(ch);
224    const Int iMarginY=getMarginY(ch);
225
226    Pel*  pi = piTxt;
227    // do left and right margins
228    for (Int y = 0; y < iHeight; y++)
229    {
230      for (Int x = 0; x < iMarginX; x++ )
231      {
232        pi[ -iMarginX + x ] = pi[0];
233        pi[    iWidth + x ] = pi[iWidth-1];
234      }
235      pi += iStride;
236    }
237
238    // pi is now the (0,height) (bottom left of image within bigger picture
239    pi -= (iStride + iMarginX);
240    // pi is now the (-marginX, height-1)
241    for (Int y = 0; y < iMarginY; y++ )
242    {
243      ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
244    }
245
246    // pi is still (-marginX, height-1)
247    pi -= ((iHeight-1) * iStride);
248    // pi is now (-marginX, 0)
249    for (Int y = 0; y < iMarginY; y++ )
250    {
251      ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
252    }
253  }
254
255  m_bIsBorderExtended = true;
256}
257
258
259
260// NOTE: This function is never called, but may be useful for developers.
261Void TComPicYuv::dump (const Char* pFileName, const BitDepths &bitDepths, Bool bAdd) const
262{
263  FILE* pFile;
264  if (!bAdd)
265  {
266    pFile = fopen (pFileName, "wb");
267  }
268  else
269  {
270    pFile = fopen (pFileName, "ab");
271  }
272
273
274  for(Int chan = 0; chan < getNumberValidComponents(); chan++)
275  {
276    const ComponentID  ch     = ComponentID(chan);
277    const Int          shift  = bitDepths.recon[toChannelType(ch)] - 8;
278    const Int          offset = (shift>0)?(1<<(shift-1)):0;
279    const Pel         *pi     = getAddr(ch);
280    const Int          stride = getStride(ch);
281    const Int          height = getHeight(ch);
282    const Int          width  = getWidth(ch);
283
284    for (Int y = 0; y < height; y++ )
285    {
286      for (Int x = 0; x < width; x++ )
287      {
288        UChar uc = (UChar)Clip3<Pel>(0, 255, (pi[x]+offset)>>shift);
289        fwrite( &uc, sizeof(UChar), 1, pFile );
290      }
291      pi += stride;
292    }
293  }
294
295  fclose(pFile);
296}
297#if NH_3D_IV_MERGE
298Void
299TComPicYuv::getTopLeftSamplePos( Int iCuAddr, Int iAbsZorderIdx, Int& riX, Int& riY )
300{
301  Int iRastPartIdx    = g_auiZscanToRaster[iAbsZorderIdx];
302  Int iCuSizeInBases  = m_iCuWidth   / m_iBaseUnitWidth;
303  Int iCuX            = iCuAddr      % m_iNumCuInWidth;
304  Int iCuY            = iCuAddr      / m_iNumCuInWidth;
305  Int iBaseX          = iRastPartIdx % iCuSizeInBases;
306  Int iBaseY          = iRastPartIdx / iCuSizeInBases;
307  riX                 = iCuX * m_iCuWidth  + iBaseX * m_iBaseUnitWidth;
308  riY                 = iCuY * m_iCuHeight + iBaseY * m_iBaseUnitHeight; 
309}
310
311Void
312TComPicYuv::getCUAddrAndPartIdx( Int iX, Int iY, Int& riCuAddr, Int& riAbsZorderIdx )
313{
314  Int iCuX            = iX / m_iCuWidth;
315  Int iCuY            = iY / m_iCuHeight;
316  Int iBaseX          = ( iX - iCuX * m_iCuWidth  ) / m_iBaseUnitWidth;
317  Int iBaseY          = ( iY - iCuY * m_iCuHeight ) / m_iBaseUnitHeight;
318  Int iCuSizeInBases  = m_iCuWidth                  / m_iBaseUnitWidth;
319  riCuAddr            = iCuY   * m_iNumCuInWidth + iCuX;
320  Int iRastPartIdx    = iBaseY * iCuSizeInBases  + iBaseX;
321  riAbsZorderIdx      = g_auiRasterToZscan[ iRastPartIdx ];
322}
323
324#endif
325
326#if H_3D
327Void TComPicYuv::setLumaTo( Pel pVal )
328{
329  xSetPels( getLumaAddr(), getStride(), getWidth(), getHeight(), pVal );
330}
331#endif
332#if NH_3D_VSO
333Void TComPicYuv::setChromaTo( Pel pVal )
334{
335  xSetPels( getAddr( COMPONENT_Cb ), getStride( COMPONENT_Cb ), getWidth( COMPONENT_Cb), getHeight( COMPONENT_Cb ), pVal ); 
336  xSetPels( getAddr( COMPONENT_Cr ), getStride( COMPONENT_Cr ), getWidth( COMPONENT_Cr), getHeight( COMPONENT_Cr ), pVal );
337}
338
339Void TComPicYuv::xSetPels( Pel* piPelSource , Int iSourceStride, Int iWidth, Int iHeight, Pel iVal )
340{
341  for (Int iYPos = 0; iYPos < iHeight; iYPos++)
342  {
343    for (Int iXPos = 0; iXPos < iWidth; iXPos++)
344    {
345      piPelSource[iXPos] = iVal; 
346    }
347    piPelSource += iSourceStride; 
348  }
349}
350#endif
351//! \}
Note: See TracBrowser for help on using the repository browser.