HEVC Test Model (HM)  HM-16.3
TComPicYuv.cpp
Go to the documentation of this file.
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 
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"
50 
53 
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  {
66  }
67 
68  m_bIsBorderExtended = false;
69 }
70 
71 
72 
73 
75 {
76 }
77 
78 
79 
80 
81 Void TComPicYuv::create( const Int iPicWidth, const Int iPicHeight, const ChromaFormat chromaFormatIDC,
82  const UInt uiMaxCUWidth, const UInt uiMaxCUHeight, const UInt uiMaxCUDepth )
83 {
84  m_iPicWidth = iPicWidth;
85  m_iPicHeight = iPicHeight;
86  m_chromaFormatIDC = chromaFormatIDC;
87  m_iMarginX = g_uiMaxCUWidth + 16; // for 16-byte alignment
88  m_iMarginY = g_uiMaxCUHeight + 16; // margin for 8-tap filter and infinite padding
89  m_bIsBorderExtended = false;
90 
91  // assign the picture arrays and set up the ptr to the top left of the original picture
92  {
93  Int chan=0;
94  for(; chan<getNumberValidComponents(); chan++)
95  {
96  const ComponentID ch=ComponentID(chan);
97  m_apiPicBuf[chan] = (Pel*)xMalloc( Pel, getStride(ch) * getTotalHeight(ch));
99  }
100  for(;chan<MAX_NUM_COMPONENT; chan++)
101  {
102  m_apiPicBuf[chan] = NULL;
103  m_piPicOrg[chan] = NULL;
104  }
105  }
106 
107 
108  const Int numCuInWidth = m_iPicWidth / uiMaxCUWidth + (m_iPicWidth % uiMaxCUWidth != 0);
109  const Int numCuInHeight = m_iPicHeight / uiMaxCUHeight + (m_iPicHeight % uiMaxCUHeight != 0);
110  for(Int chan=0; chan<2; chan++)
111  {
112  const ComponentID ch=ComponentID(chan);
113  const Int ctuHeight=uiMaxCUHeight>>getComponentScaleY(ch);
114  const Int ctuWidth=uiMaxCUWidth>>getComponentScaleX(ch);
115  const Int stride = getStride(ch);
116 
117  m_ctuOffsetInBuffer[chan] = new Int[numCuInWidth * numCuInHeight];
118 
119  for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++)
120  {
121  for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++)
122  {
123  m_ctuOffsetInBuffer[chan][cuRow * numCuInWidth + cuCol] = stride * cuRow * ctuHeight + cuCol * ctuWidth;
124  }
125  }
126 
127  m_subCuOffsetInBuffer[chan] = new Int[(size_t)1 << (2 * uiMaxCUDepth)];
128 
129  const Int numSubBlockPartitions=(1<<uiMaxCUDepth);
130  const Int minSubBlockHeight =(ctuHeight >> uiMaxCUDepth);
131  const Int minSubBlockWidth =(ctuWidth >> uiMaxCUDepth);
132 
133  for (Int buRow = 0; buRow < numSubBlockPartitions; buRow++)
134  {
135  for (Int buCol = 0; buCol < numSubBlockPartitions; buCol++)
136  {
137  m_subCuOffsetInBuffer[chan][(buRow << uiMaxCUDepth) + buCol] = stride * buRow * minSubBlockHeight + buCol * minSubBlockWidth;
138  }
139  }
140  }
141  return;
142 }
143 
144 
145 
147 {
148  for(Int chan=0; chan<MAX_NUM_COMPONENT; chan++)
149  {
150  m_piPicOrg[chan] = NULL;
151 
152  if( m_apiPicBuf[chan] )
153  {
154  xFree( m_apiPicBuf[chan] );
155  m_apiPicBuf[chan] = NULL;
156  }
157  }
158 
159  for(UInt chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++)
160  {
161  if (m_ctuOffsetInBuffer[chan])
162  {
163  delete[] m_ctuOffsetInBuffer[chan];
164  m_ctuOffsetInBuffer[chan] = NULL;
165  }
166  if (m_subCuOffsetInBuffer[chan])
167  {
168  delete[] m_subCuOffsetInBuffer[chan];
169  m_subCuOffsetInBuffer[chan] = NULL;
170  }
171  }
172 }
173 
174 
175 
177 {
178  assert( m_iPicWidth == pcPicYuvDst->getWidth(COMPONENT_Y) );
179  assert( m_iPicHeight == pcPicYuvDst->getHeight(COMPONENT_Y) );
180  assert( m_chromaFormatIDC == pcPicYuvDst->getChromaFormat() );
181 
182  for(Int chan=0; chan<getNumberValidComponents(); chan++)
183  {
184  const ComponentID ch=ComponentID(chan);
185  ::memcpy ( pcPicYuvDst->getBuf(ch), m_apiPicBuf[ch], sizeof (Pel) * getStride(ch) * getTotalHeight(ch));
186  }
187  return;
188 }
189 
190 
192 {
193  if ( m_bIsBorderExtended )
194  {
195  return;
196  }
197 
198  for(Int chan=0; chan<getNumberValidComponents(); chan++)
199  {
200  const ComponentID ch=ComponentID(chan);
201  Pel *piTxt=getAddr(ch); // piTxt = point to (0,0) of image within bigger picture.
202  const Int iStride=getStride(ch);
203  const Int iWidth=getWidth(ch);
204  const Int iHeight=getHeight(ch);
205  const Int iMarginX=getMarginX(ch);
206  const Int iMarginY=getMarginY(ch);
207 
208  Pel* pi = piTxt;
209  // do left and right margins
210  for (Int y = 0; y < iHeight; y++)
211  {
212  for (Int x = 0; x < iMarginX; x++ )
213  {
214  pi[ -iMarginX + x ] = pi[0];
215  pi[ iWidth + x ] = pi[iWidth-1];
216  }
217  pi += iStride;
218  }
219 
220  // pi is now the (0,height) (bottom left of image within bigger picture
221  pi -= (iStride + iMarginX);
222  // pi is now the (-marginX, height-1)
223  for (Int y = 0; y < iMarginY; y++ )
224  {
225  ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
226  }
227 
228  // pi is still (-marginX, height-1)
229  pi -= ((iHeight-1) * iStride);
230  // pi is now (-marginX, 0)
231  for (Int y = 0; y < iMarginY; y++ )
232  {
233  ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) );
234  }
235  }
236 
237  m_bIsBorderExtended = true;
238 }
239 
240 
241 
242 // NOTE: This function is never called, but may be useful for developers.
243 Void TComPicYuv::dump (const Char* pFileName, Bool bAdd) const
244 {
245  FILE* pFile;
246  if (!bAdd)
247  {
248  pFile = fopen (pFileName, "wb");
249  }
250  else
251  {
252  pFile = fopen (pFileName, "ab");
253  }
254 
255 
256  for(Int chan = 0; chan < getNumberValidComponents(); chan++)
257  {
258  const ComponentID ch = ComponentID(chan);
259  const Int shift = g_bitDepth[toChannelType(ch)] - 8;
260  const Int offset = (shift>0)?(1<<(shift-1)):0;
261  const Pel *pi = getAddr(ch);
262  const Int stride = getStride(ch);
263  const Int height = getHeight(ch);
264  const Int width = getWidth(ch);
265 
266  for (Int y = 0; y < height; y++ )
267  {
268  for (Int x = 0; x < width; x++ )
269  {
270  UChar uc = (UChar)Clip3<Pel>(0, 255, (pi[x]+offset)>>shift);
271  fwrite( &uc, sizeof(UChar), 1, pFile );
272  }
273  pi += stride;
274  }
275  }
276 
277  fclose(pFile);
278 }
279 
Void create(const Int iPicWidth, const Int iPicHeight, const ChromaFormat chromaFormatIDC, const UInt uiMaxCUWidth, const UInt uiMaxCUHeight, const UInt uiMaxCUDepth)
Definition: TComPicYuv.cpp:81
Void extendPicBorder()
Definition: TComPicYuv.cpp:191
picture YUV buffer class
Definition: TComPicYuv.h:55
void Void
Definition: TypeDef.h:285
Int getStride(const ComponentID id) const
Definition: TComPicYuv.h:113
#define NULL
Definition: CommonDef.h:100
Int getHeight(const ComponentID id) const
Definition: TComPicYuv.h:109
char Char
Definition: TypeDef.h:291
unsigned int UInt
Definition: TypeDef.h:297
Pel * m_apiPicBuf[MAX_NUM_COMPONENT]
Buffer (including margin)
Definition: TComPicYuv.h:63
Short Pel
pixel type
Definition: TypeDef.h:692
YUV file I/O class (header)
Int * m_ctuOffsetInBuffer[MAX_NUM_CHANNEL_TYPE]
Gives an offset in the buffer for a given CTU (and channel)
Definition: TComPicYuv.h:75
Int getWidth(const ComponentID id) const
Definition: TComPicYuv.h:108
UInt getComponentScaleY(const ComponentID id) const
Definition: TComPicYuv.h:139
UInt getComponentScaleX(const ComponentID id) const
Definition: TComPicYuv.h:138
bool Bool
Definition: TypeDef.h:286
UInt g_uiMaxCUHeight
Definition: TComRom.cpp:252
UInt g_uiMaxCUWidth
Definition: TComRom.cpp:251
Void copyToPic(TComPicYuv *pcPicYuvDst) const
Definition: TComPicYuv.cpp:176
static ChannelType toChannelType(const ComponentID id)
virtual ~TComPicYuv()
Definition: TComPicYuv.cpp:74
Pel * m_piPicOrg[MAX_NUM_COMPONENT]
m_apiPicBufY + m_iMarginLuma*getStride() + m_iMarginLuma
Definition: TComPicYuv.h:65
Int getTotalHeight(const ComponentID id) const
Definition: TComPicYuv.h:114
ChromaFormat
chroma formats (according to semantics of chroma_format_idc)
Definition: TypeDef.h:352
#define xFree(ptr)
Definition: CommonDef.h:157
unsigned char UChar
Definition: TypeDef.h:293
picture YUV buffer class (header)
UInt getNumberValidComponents() const
Definition: TComPicYuv.h:111
Int * m_subCuOffsetInBuffer[MAX_NUM_CHANNEL_TYPE]
Gives an offset in the buffer for a given sub-CU (and channel), relative to start of CTU...
Definition: TComPicYuv.h:76
#define xMalloc(type, len)
Definition: CommonDef.h:156
Int getMarginX(const ComponentID id) const
Definition: TComPicYuv.h:116
Int g_bitDepth[MAX_NUM_CHANNEL_TYPE]
Definition: TComRom.cpp:548
Int m_iPicHeight
Height of picture in pixels.
Definition: TComPicYuv.h:72
Int m_iMarginX
margin of Luma channel (chroma's may be smaller, depending on ratio)
Definition: TComPicYuv.h:78
Pel * getAddr(const ComponentID ch)
Definition: TComPicYuv.h:127
Int m_iMarginY
margin of Luma channel (chroma's may be smaller, depending on ratio)
Definition: TComPicYuv.h:79
int Int
Definition: TypeDef.h:296
ComponentID
Definition: TypeDef.h:368
Int getMarginY(const ComponentID id) const
Definition: TComPicYuv.h:117
Int m_iPicWidth
Width of picture in pixels.
Definition: TComPicYuv.h:71
Pel * getBuf(const ComponentID ch)
Definition: TComPicYuv.h:124
Void dump(const Char *pFileName, Bool bAdd=false) const
Definition: TComPicYuv.cpp:243
ChromaFormat m_chromaFormatIDC
Chroma Format.
Definition: TComPicYuv.h:73
Void destroy()
Definition: TComPicYuv.cpp:146
Bool m_bIsBorderExtended
Definition: TComPicYuv.h:81
ChromaFormat getChromaFormat() const
Definition: TComPicYuv.h:110