source: 3DVCSoftware/branches/HTM-16.1-dev/source/Lib/TLibCommon/TComPicYuv.cpp @ 1402

Last change on this file since 1402 was 1402, checked in by tech, 8 years ago

Initial merge of HM-16.9.

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