source: SHVCSoftware/branches/SHM-5.0-dev/source/Lib/TLibCommon/TComPicYuv.cpp @ 852

Last change on this file since 852 was 593, checked in by seregin, 11 years ago

update to HM-13.0

  • Property svn:eol-style set to native
File size: 12.2 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-2014, 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#if AUXILIARY_PICTURES
70#if SVC_UPSAMPLING
71Void TComPicYuv::create( Int iPicWidth, Int iPicHeight, ChromaFormat chromaFormatIDC, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth, TComSPS* pcSps )
72#else
73Void TComPicYuv::create( Int iPicWidth, Int iPicHeight, ChromaFormat chromaFormatIDC, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth )
74#endif
75#else
76#if SVC_UPSAMPLING
77Void TComPicYuv::create( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth, TComSPS* pcSps )
78#else
79Void TComPicYuv::create( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth )
80#endif
81#endif
82{
83  m_iPicWidth       = iPicWidth;
84  m_iPicHeight      = iPicHeight;
85 
86#if SVC_UPSAMPLING
87  if(pcSps != NULL)
88  {
89    m_conformanceWindow = pcSps->getConformanceWindow();
90  }
91#endif
92
93  // --> After config finished!
94  m_iCuWidth        = uiMaxCUWidth;
95  m_iCuHeight       = uiMaxCUHeight;
96#if AUXILIARY_PICTURES
97  m_chromaFormatIDC = chromaFormatIDC;
98#endif
99
100  Int numCuInWidth  = m_iPicWidth  / m_iCuWidth  + (m_iPicWidth  % m_iCuWidth  != 0);
101  Int numCuInHeight = m_iPicHeight / m_iCuHeight + (m_iPicHeight % m_iCuHeight != 0);
102 
103#if LAYER_CTB
104  m_iLumaMarginX    = uiMaxCUWidth  + 16; // for 16-byte alignment
105  m_iLumaMarginY    = uiMaxCUHeight + 16;  // margin for 8-tap filter and infinite padding
106#else
107  m_iLumaMarginX    = g_uiMaxCUWidth  + 16; // for 16-byte alignment
108  m_iLumaMarginY    = g_uiMaxCUHeight + 16;  // margin for 8-tap filter and infinite padding
109#endif
110 
111  m_iChromaMarginX  = m_iLumaMarginX>>1;
112  m_iChromaMarginY  = m_iLumaMarginY>>1;
113 
114  m_apiPicBufY      = (Pel*)xMalloc( Pel, ( m_iPicWidth       + (m_iLumaMarginX  <<1)) * ( m_iPicHeight       + (m_iLumaMarginY  <<1)));
115  m_apiPicBufU      = (Pel*)xMalloc( Pel, ((m_iPicWidth >> 1) + (m_iChromaMarginX<<1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY<<1)));
116  m_apiPicBufV      = (Pel*)xMalloc( Pel, ((m_iPicWidth >> 1) + (m_iChromaMarginX<<1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY<<1)));
117 
118  m_piPicOrgY       = m_apiPicBufY + m_iLumaMarginY   * getStride()  + m_iLumaMarginX;
119  m_piPicOrgU       = m_apiPicBufU + m_iChromaMarginY * getCStride() + m_iChromaMarginX;
120  m_piPicOrgV       = m_apiPicBufV + m_iChromaMarginY * getCStride() + m_iChromaMarginX;
121 
122  m_bIsBorderExtended = false;
123 
124  m_cuOffsetY = new Int[numCuInWidth * numCuInHeight];
125  m_cuOffsetC = new Int[numCuInWidth * numCuInHeight];
126  for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++)
127  {
128    for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++)
129    {
130      m_cuOffsetY[cuRow * numCuInWidth + cuCol] = getStride() * cuRow * m_iCuHeight + cuCol * m_iCuWidth;
131      m_cuOffsetC[cuRow * numCuInWidth + cuCol] = getCStride() * cuRow * (m_iCuHeight / 2) + cuCol * (m_iCuWidth / 2);
132    }
133  }
134 
135  m_buOffsetY = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
136  m_buOffsetC = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
137  for (Int buRow = 0; buRow < (1 << uiMaxCUDepth); buRow++)
138  {
139    for (Int buCol = 0; buCol < (1 << uiMaxCUDepth); buCol++)
140    {
141      m_buOffsetY[(buRow << uiMaxCUDepth) + buCol] = getStride() * buRow * (uiMaxCUHeight >> uiMaxCUDepth) + buCol * (uiMaxCUWidth  >> uiMaxCUDepth);
142      m_buOffsetC[(buRow << uiMaxCUDepth) + buCol] = getCStride() * buRow * (uiMaxCUHeight / 2 >> uiMaxCUDepth) + buCol * (uiMaxCUWidth / 2 >> uiMaxCUDepth);
143    }
144  }
145  return;
146}
147
148Void TComPicYuv::destroy()
149{
150  m_piPicOrgY       = NULL;
151  m_piPicOrgU       = NULL;
152  m_piPicOrgV       = NULL;
153 
154  if( m_apiPicBufY ){ xFree( m_apiPicBufY );    m_apiPicBufY = NULL; }
155  if( m_apiPicBufU ){ xFree( m_apiPicBufU );    m_apiPicBufU = NULL; }
156  if( m_apiPicBufV ){ xFree( m_apiPicBufV );    m_apiPicBufV = NULL; }
157
158  delete[] m_cuOffsetY;
159  delete[] m_cuOffsetC;
160  delete[] m_buOffsetY;
161  delete[] m_buOffsetC;
162}
163
164Void TComPicYuv::createLuma( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth )
165{
166  m_iPicWidth       = iPicWidth;
167  m_iPicHeight      = iPicHeight;
168 
169  // --> After config finished!
170  m_iCuWidth        = uiMaxCUWidth;
171  m_iCuHeight       = uiMaxCUHeight;
172 
173  Int numCuInWidth  = m_iPicWidth  / m_iCuWidth  + (m_iPicWidth  % m_iCuWidth  != 0);
174  Int numCuInHeight = m_iPicHeight / m_iCuHeight + (m_iPicHeight % m_iCuHeight != 0);
175 
176  m_iLumaMarginX    = g_uiMaxCUWidth  + 16; // for 16-byte alignment
177  m_iLumaMarginY    = g_uiMaxCUHeight + 16;  // margin for 8-tap filter and infinite padding
178 
179  m_apiPicBufY      = (Pel*)xMalloc( Pel, ( m_iPicWidth       + (m_iLumaMarginX  <<1)) * ( m_iPicHeight       + (m_iLumaMarginY  <<1)));
180  m_piPicOrgY       = m_apiPicBufY + m_iLumaMarginY   * getStride()  + m_iLumaMarginX;
181 
182  m_cuOffsetY = new Int[numCuInWidth * numCuInHeight];
183  m_cuOffsetC = NULL;
184  for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++)
185  {
186    for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++)
187    {
188      m_cuOffsetY[cuRow * numCuInWidth + cuCol] = getStride() * cuRow * m_iCuHeight + cuCol * m_iCuWidth;
189    }
190  }
191 
192  m_buOffsetY = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
193  m_buOffsetC = NULL;
194  for (Int buRow = 0; buRow < (1 << uiMaxCUDepth); buRow++)
195  {
196    for (Int buCol = 0; buCol < (1 << uiMaxCUDepth); buCol++)
197    {
198      m_buOffsetY[(buRow << uiMaxCUDepth) + buCol] = getStride() * buRow * (uiMaxCUHeight >> uiMaxCUDepth) + buCol * (uiMaxCUWidth  >> uiMaxCUDepth);
199    }
200  }
201  return;
202}
203
204Void TComPicYuv::destroyLuma()
205{
206  m_piPicOrgY       = NULL;
207 
208  if( m_apiPicBufY ){ xFree( m_apiPicBufY );    m_apiPicBufY = NULL; }
209 
210  delete[] m_cuOffsetY;
211  delete[] m_buOffsetY;
212}
213
214Void  TComPicYuv::copyToPic (TComPicYuv*  pcPicYuvDst)
215{
216  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
217  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
218 
219  ::memcpy ( pcPicYuvDst->getBufY(), m_apiPicBufY, sizeof (Pel) * ( m_iPicWidth       + (m_iLumaMarginX   << 1)) * ( m_iPicHeight       + (m_iLumaMarginY   << 1)) );
220  ::memcpy ( pcPicYuvDst->getBufU(), m_apiPicBufU, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
221  ::memcpy ( pcPicYuvDst->getBufV(), m_apiPicBufV, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
222  return;
223}
224
225Void  TComPicYuv::copyToPicLuma (TComPicYuv*  pcPicYuvDst)
226{
227  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
228  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
229 
230  ::memcpy ( pcPicYuvDst->getBufY(), m_apiPicBufY, sizeof (Pel) * ( m_iPicWidth       + (m_iLumaMarginX   << 1)) * ( m_iPicHeight       + (m_iLumaMarginY   << 1)) );
231  return;
232}
233
234Void  TComPicYuv::copyToPicCb (TComPicYuv*  pcPicYuvDst)
235{
236  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
237  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
238 
239  ::memcpy ( pcPicYuvDst->getBufU(), m_apiPicBufU, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
240  return;
241}
242
243Void  TComPicYuv::copyToPicCr (TComPicYuv*  pcPicYuvDst)
244{
245  assert( m_iPicWidth  == pcPicYuvDst->getWidth()  );
246  assert( m_iPicHeight == pcPicYuvDst->getHeight() );
247 
248  ::memcpy ( pcPicYuvDst->getBufV(), m_apiPicBufV, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) );
249  return;
250}
251
252#if AUXILIARY_PICTURES
253Void TComPicYuv::convertToMonochrome()
254{
255  Int numPix = ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1));
256  Pel grayVal = (1 << (g_bitDepthC - 1));
257
258  for (UInt i = 0; i < numPix; i++)
259  {
260    m_apiPicBufU[i] = grayVal;
261    m_apiPicBufV[i] = grayVal;
262  }
263}
264#endif
265
266Void TComPicYuv::extendPicBorder ()
267{
268  if ( m_bIsBorderExtended ) return;
269 
270  xExtendPicCompBorder( getLumaAddr(), getStride(),  getWidth(),      getHeight(),      m_iLumaMarginX,   m_iLumaMarginY   );
271  xExtendPicCompBorder( getCbAddr()  , getCStride(), getWidth() >> 1, getHeight() >> 1, m_iChromaMarginX, m_iChromaMarginY );
272  xExtendPicCompBorder( getCrAddr()  , getCStride(), getWidth() >> 1, getHeight() >> 1, m_iChromaMarginX, m_iChromaMarginY );
273 
274  m_bIsBorderExtended = true;
275}
276
277Void TComPicYuv::xExtendPicCompBorder  (Pel* piTxt, Int iStride, Int iWidth, Int iHeight, Int iMarginX, Int iMarginY)
278{
279  Int   x, y;
280  Pel*  pi;
281 
282  pi = piTxt;
283  for ( y = 0; y < iHeight; y++)
284  {
285    for ( x = 0; x < iMarginX; x++ )
286    {
287      pi[ -iMarginX + x ] = pi[0];
288      pi[    iWidth + x ] = pi[iWidth-1];
289    }
290    pi += iStride;
291  }
292 
293  pi -= (iStride + iMarginX);
294  for ( y = 0; y < iMarginY; y++ )
295  {
296    ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
297  }
298 
299  pi -= ((iHeight-1) * iStride);
300  for ( y = 0; y < iMarginY; y++ )
301  {
302    ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
303  }
304}
305
306
307Void TComPicYuv::dump (Char* pFileName, Bool bAdd)
308{
309  FILE* pFile;
310  if (!bAdd)
311  {
312    pFile = fopen (pFileName, "wb");
313  }
314  else
315  {
316    pFile = fopen (pFileName, "ab");
317  }
318 
319  Int     shift = g_bitDepthY-8;
320  Int     offset = (shift>0)?(1<<(shift-1)):0;
321 
322  Int   x, y;
323  UChar uc;
324 
325  Pel*  piY   = getLumaAddr();
326  Pel*  piCb  = getCbAddr();
327  Pel*  piCr  = getCrAddr();
328 
329  for ( y = 0; y < m_iPicHeight; y++ )
330  {
331    for ( x = 0; x < m_iPicWidth; x++ )
332    {
333      uc = (UChar)Clip3<Pel>(0, 255, (piY[x]+offset)>>shift);
334     
335      fwrite( &uc, sizeof(UChar), 1, pFile );
336    }
337    piY += getStride();
338  }
339 
340  shift = g_bitDepthC-8;
341  offset = (shift>0)?(1<<(shift-1)):0;
342
343  for ( y = 0; y < m_iPicHeight >> 1; y++ )
344  {
345    for ( x = 0; x < m_iPicWidth >> 1; x++ )
346    {
347      uc = (UChar)Clip3<Pel>(0, 255, (piCb[x]+offset)>>shift);
348      fwrite( &uc, sizeof(UChar), 1, pFile );
349    }
350    piCb += getCStride();
351  }
352 
353  for ( y = 0; y < m_iPicHeight >> 1; y++ )
354  {
355    for ( x = 0; x < m_iPicWidth >> 1; x++ )
356    {
357      uc = (UChar)Clip3<Pel>(0, 255, (piCr[x]+offset)>>shift);
358      fwrite( &uc, sizeof(UChar), 1, pFile );
359    }
360    piCr += getCStride();
361  }
362 
363  fclose(pFile);
364}
365
366
367//! \}
Note: See TracBrowser for help on using the repository browser.