source: 3DVCSoftware/branches/HTM-DEV-0.2-dev/source/Lib/TLibCommon/TComPicYuv.cpp @ 446

Last change on this file since 446 was 446, checked in by tech, 11 years ago

Added missing parts.

  • Property svn:eol-style set to native
File size: 11.6 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     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
50//! \ingroup TLibCommon
51//! \{
52
53TComPicYuv::TComPicYuv()
54{
55  m_apiPicBufY      = NULL;   // Buffer (including margin)
56  m_apiPicBufU      = NULL;
57  m_apiPicBufV      = NULL;
58 
59  m_piPicOrgY       = NULL;    // m_apiPicBufY + m_iMarginLuma*getStride() + m_iMarginLuma
60  m_piPicOrgU       = NULL;
61  m_piPicOrgV       = NULL;
62 
63  m_bIsBorderExtended = false;
64}
65
66TComPicYuv::~TComPicYuv()
67{
68}
69
70Void TComPicYuv::create( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth )
71{
72  m_iPicWidth       = iPicWidth;
73  m_iPicHeight      = iPicHeight;
74 
75  // --> After config finished!
76  m_iCuWidth        = uiMaxCUWidth;
77  m_iCuHeight       = uiMaxCUHeight;
78
79  Int numCuInWidth  = m_iPicWidth  / m_iCuWidth  + (m_iPicWidth  % m_iCuWidth  != 0);
80  Int numCuInHeight = m_iPicHeight / m_iCuHeight + (m_iPicHeight % m_iCuHeight != 0);
81 
82  m_iLumaMarginX    = g_uiMaxCUWidth  + 16; // for 16-byte alignment
83  m_iLumaMarginY    = g_uiMaxCUHeight + 16;  // margin for 8-tap filter and infinite padding
84 
85  m_iChromaMarginX  = m_iLumaMarginX>>1;
86  m_iChromaMarginY  = m_iLumaMarginY>>1;
87 
88  m_apiPicBufY      = (Pel*)xMalloc( Pel, ( m_iPicWidth       + (m_iLumaMarginX  <<1)) * ( m_iPicHeight       + (m_iLumaMarginY  <<1)));
89  m_apiPicBufU      = (Pel*)xMalloc( Pel, ((m_iPicWidth >> 1) + (m_iChromaMarginX<<1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY<<1)));
90  m_apiPicBufV      = (Pel*)xMalloc( Pel, ((m_iPicWidth >> 1) + (m_iChromaMarginX<<1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY<<1)));
91 
92  m_piPicOrgY       = m_apiPicBufY + m_iLumaMarginY   * getStride()  + m_iLumaMarginX;
93  m_piPicOrgU       = m_apiPicBufU + m_iChromaMarginY * getCStride() + m_iChromaMarginX;
94  m_piPicOrgV       = m_apiPicBufV + m_iChromaMarginY * getCStride() + m_iChromaMarginX;
95 
96  m_bIsBorderExtended = false;
97 
98  m_cuOffsetY = new Int[numCuInWidth * numCuInHeight];
99  m_cuOffsetC = new Int[numCuInWidth * numCuInHeight];
100  for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++)
101  {
102    for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++)
103    {
104      m_cuOffsetY[cuRow * numCuInWidth + cuCol] = getStride() * cuRow * m_iCuHeight + cuCol * m_iCuWidth;
105      m_cuOffsetC[cuRow * numCuInWidth + cuCol] = getCStride() * cuRow * (m_iCuHeight / 2) + cuCol * (m_iCuWidth / 2);
106    }
107  }
108 
109  m_buOffsetY = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
110  m_buOffsetC = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
111  for (Int buRow = 0; buRow < (1 << uiMaxCUDepth); buRow++)
112  {
113    for (Int buCol = 0; buCol < (1 << uiMaxCUDepth); buCol++)
114    {
115      m_buOffsetY[(buRow << uiMaxCUDepth) + buCol] = getStride() * buRow * (uiMaxCUHeight >> uiMaxCUDepth) + buCol * (uiMaxCUWidth  >> uiMaxCUDepth);
116      m_buOffsetC[(buRow << uiMaxCUDepth) + buCol] = getCStride() * buRow * (uiMaxCUHeight / 2 >> uiMaxCUDepth) + buCol * (uiMaxCUWidth / 2 >> uiMaxCUDepth);
117    }
118  }
119  return;
120}
121
122Void TComPicYuv::destroy()
123{
124  m_piPicOrgY       = NULL;
125  m_piPicOrgU       = NULL;
126  m_piPicOrgV       = NULL;
127 
128  if( m_apiPicBufY ){ xFree( m_apiPicBufY );    m_apiPicBufY = NULL; }
129  if( m_apiPicBufU ){ xFree( m_apiPicBufU );    m_apiPicBufU = NULL; }
130  if( m_apiPicBufV ){ xFree( m_apiPicBufV );    m_apiPicBufV = NULL; }
131
132  delete[] m_cuOffsetY;
133  delete[] m_cuOffsetC;
134  delete[] m_buOffsetY;
135  delete[] m_buOffsetC;
136}
137
138Void TComPicYuv::createLuma( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth )
139{
140  m_iPicWidth       = iPicWidth;
141  m_iPicHeight      = iPicHeight;
142 
143  // --> After config finished!
144  m_iCuWidth        = uiMaxCUWidth;
145  m_iCuHeight       = uiMaxCUHeight;
146 
147  Int numCuInWidth  = m_iPicWidth  / m_iCuWidth  + (m_iPicWidth  % m_iCuWidth  != 0);
148  Int numCuInHeight = m_iPicHeight / m_iCuHeight + (m_iPicHeight % m_iCuHeight != 0);
149 
150  m_iLumaMarginX    = g_uiMaxCUWidth  + 16; // for 16-byte alignment
151  m_iLumaMarginY    = g_uiMaxCUHeight + 16;  // margin for 8-tap filter and infinite padding
152 
153  m_apiPicBufY      = (Pel*)xMalloc( Pel, ( m_iPicWidth       + (m_iLumaMarginX  <<1)) * ( m_iPicHeight       + (m_iLumaMarginY  <<1)));
154  m_piPicOrgY       = m_apiPicBufY + m_iLumaMarginY   * getStride()  + m_iLumaMarginX;
155 
156  m_cuOffsetY = new Int[numCuInWidth * numCuInHeight];
157  m_cuOffsetC = NULL;
158  for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++)
159  {
160    for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++)
161    {
162      m_cuOffsetY[cuRow * numCuInWidth + cuCol] = getStride() * cuRow * m_iCuHeight + cuCol * m_iCuWidth;
163    }
164  }
165 
166  m_buOffsetY = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
167  m_buOffsetC = NULL;
168  for (Int buRow = 0; buRow < (1 << uiMaxCUDepth); buRow++)
169  {
170    for (Int buCol = 0; buCol < (1 << uiMaxCUDepth); buCol++)
171    {
172      m_buOffsetY[(buRow << uiMaxCUDepth) + buCol] = getStride() * buRow * (uiMaxCUHeight >> uiMaxCUDepth) + buCol * (uiMaxCUWidth  >> uiMaxCUDepth);
173    }
174  }
175  return;
176}
177
178Void TComPicYuv::destroyLuma()
179{
180  m_piPicOrgY       = NULL;
181 
182  if( m_apiPicBufY ){ xFree( m_apiPicBufY );    m_apiPicBufY = NULL; }
183 
184  delete[] m_cuOffsetY;
185  delete[] m_buOffsetY;
186}
187
188Void  TComPicYuv::copyToPic (TComPicYuv*  pcPicYuvDst)
189{
190  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
191  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
192 
193  ::memcpy ( pcPicYuvDst->getBufY(), m_apiPicBufY, sizeof (Pel) * ( m_iPicWidth       + (m_iLumaMarginX   << 1)) * ( m_iPicHeight       + (m_iLumaMarginY   << 1)) );
194  ::memcpy ( pcPicYuvDst->getBufU(), m_apiPicBufU, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
195  ::memcpy ( pcPicYuvDst->getBufV(), m_apiPicBufV, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
196  return;
197}
198
199Void  TComPicYuv::copyToPicLuma (TComPicYuv*  pcPicYuvDst)
200{
201  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
202  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
203 
204  ::memcpy ( pcPicYuvDst->getBufY(), m_apiPicBufY, sizeof (Pel) * ( m_iPicWidth       + (m_iLumaMarginX   << 1)) * ( m_iPicHeight       + (m_iLumaMarginY   << 1)) );
205  return;
206}
207
208Void  TComPicYuv::copyToPicCb (TComPicYuv*  pcPicYuvDst)
209{
210  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
211  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
212 
213  ::memcpy ( pcPicYuvDst->getBufU(), m_apiPicBufU, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
214  return;
215}
216
217Void  TComPicYuv::copyToPicCr (TComPicYuv*  pcPicYuvDst)
218{
219  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
220  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
221 
222  ::memcpy ( pcPicYuvDst->getBufV(), m_apiPicBufV, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
223  return;
224}
225
226Void TComPicYuv::extendPicBorder ()
227{
228  if ( m_bIsBorderExtended ) return;
229 
230  xExtendPicCompBorder( getLumaAddr(), getStride(),  getWidth(),      getHeight(),      m_iLumaMarginX,   m_iLumaMarginY   );
231  xExtendPicCompBorder( getCbAddr()  , getCStride(), getWidth() >> 1, getHeight() >> 1, m_iChromaMarginX, m_iChromaMarginY );
232  xExtendPicCompBorder( getCrAddr()  , getCStride(), getWidth() >> 1, getHeight() >> 1, m_iChromaMarginX, m_iChromaMarginY );
233 
234  m_bIsBorderExtended = true;
235}
236
237Void TComPicYuv::xExtendPicCompBorder  (Pel* piTxt, Int iStride, Int iWidth, Int iHeight, Int iMarginX, Int iMarginY)
238{
239  Int   x, y;
240  Pel*  pi;
241 
242  pi = piTxt;
243  for ( y = 0; y < iHeight; y++)
244  {
245    for ( x = 0; x < iMarginX; x++ )
246    {
247      pi[ -iMarginX + x ] = pi[0];
248      pi[    iWidth + x ] = pi[iWidth-1];
249    }
250    pi += iStride;
251  }
252 
253  pi -= (iStride + iMarginX);
254  for ( y = 0; y < iMarginY; y++ )
255  {
256    ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
257  }
258 
259  pi -= ((iHeight-1) * iStride);
260  for ( y = 0; y < iMarginY; y++ )
261  {
262    ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
263  }
264}
265
266
267Void TComPicYuv::dump (Char* pFileName, Bool bAdd)
268{
269  FILE* pFile;
270  if (!bAdd)
271  {
272    pFile = fopen (pFileName, "wb");
273  }
274  else
275  {
276    pFile = fopen (pFileName, "ab");
277  }
278 
279  Int     shift = g_bitDepthY-8;
280  Int     offset = (shift>0)?(1<<(shift-1)):0;
281 
282  Int   x, y;
283  UChar uc;
284 
285  Pel*  piY   = getLumaAddr();
286  Pel*  piCb  = getCbAddr();
287  Pel*  piCr  = getCrAddr();
288 
289  for ( y = 0; y < m_iPicHeight; y++ )
290  {
291    for ( x = 0; x < m_iPicWidth; x++ )
292    {
293      uc = (UChar)Clip3<Pel>(0, 255, (piY[x]+offset)>>shift);
294     
295      fwrite( &uc, sizeof(UChar), 1, pFile );
296    }
297    piY += getStride();
298  }
299 
300  shift = g_bitDepthC-8;
301  offset = (shift>0)?(1<<(shift-1)):0;
302
303  for ( y = 0; y < m_iPicHeight >> 1; y++ )
304  {
305    for ( x = 0; x < m_iPicWidth >> 1; x++ )
306    {
307      uc = (UChar)Clip3<Pel>(0, 255, (piCb[x]+offset)>>shift);
308      fwrite( &uc, sizeof(UChar), 1, pFile );
309    }
310    piCb += getCStride();
311  }
312 
313  for ( y = 0; y < m_iPicHeight >> 1; y++ )
314  {
315    for ( x = 0; x < m_iPicWidth >> 1; x++ )
316    {
317      uc = (UChar)Clip3<Pel>(0, 255, (piCr[x]+offset)>>shift);
318      fwrite( &uc, sizeof(UChar), 1, pFile );
319    }
320    piCr += getCStride();
321  }
322 
323  fclose(pFile);
324}
325
326#if H_3D
327Void TComPicYuv::setLumaTo( Pel pVal )
328{
329  xSetPels( getLumaAddr(), getStride(), getWidth(), getHeight(), pVal );
330}
331
332Void TComPicYuv::setChromaTo( Pel pVal )
333{
334  xSetPels( getCbAddr(), getCStride(), getWidth() >> 1, getHeight() >> 1, pVal ); 
335  xSetPels( getCrAddr(), getCStride(), getWidth() >> 1, getHeight() >> 1, pVal );
336}
337
338Void TComPicYuv::xSetPels( Pel* piPelSource , Int iSourceStride, Int iWidth, Int iHeight, Pel iVal )
339{
340  for (Int iYPos = 0; iYPos < iHeight; iYPos++)
341  {
342    for (Int iXPos = 0; iXPos < iWidth; iXPos++)
343    {
344      piPelSource[iXPos] = iVal; 
345    }
346    piPelSource += iSourceStride; 
347  }
348}
349#endif
350
351//! \}
Note: See TracBrowser for help on using the repository browser.