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 | |
---|
54 | TComPicYuv::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 | |
---|
74 | TComPicYuv::~TComPicYuv() |
---|
75 | { |
---|
76 | destroy(); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | Void TComPicYuv::createWithoutCUInfo ( const Int picWidth, ///< picture width |
---|
82 | const Int picHeight, ///< picture height |
---|
83 | const ChromaFormat chromaFormatIDC, ///< chroma format |
---|
84 | const Bool bUseMargin, ///< if true, then a margin of uiMaxCUWidth+16 and uiMaxCUHeight+16 is created around the image. |
---|
85 | const UInt maxCUWidth, ///< used for margin only |
---|
86 | const UInt maxCUHeight ///< used for margin only |
---|
87 | #if SVC_EXTENSION |
---|
88 | , const Window* conformanceWindow |
---|
89 | #endif |
---|
90 | ) |
---|
91 | |
---|
92 | { |
---|
93 | destroy(); |
---|
94 | |
---|
95 | #if SVC_EXTENSION |
---|
96 | if(conformanceWindow != NULL) |
---|
97 | { |
---|
98 | m_conformanceWindow = *conformanceWindow; |
---|
99 | } |
---|
100 | #endif |
---|
101 | |
---|
102 | m_picWidth = picWidth; |
---|
103 | m_picHeight = picHeight; |
---|
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 | |
---|
132 | Void 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 | #if SVC_EXTENSION |
---|
140 | , const Window* conformanceWindow |
---|
141 | #endif |
---|
142 | ) |
---|
143 | |
---|
144 | { |
---|
145 | #if SVC_EXTENSION |
---|
146 | createWithoutCUInfo(picWidth, picHeight, chromaFormatIDC, bUseMargin, maxCUWidth, maxCUHeight, conformanceWindow); |
---|
147 | #else |
---|
148 | createWithoutCUInfo(picWidth, picHeight, chromaFormatIDC, bUseMargin, maxCUWidth, maxCUHeight); |
---|
149 | #endif |
---|
150 | |
---|
151 | |
---|
152 | const Int numCuInWidth = m_picWidth / maxCUWidth + (m_picWidth % maxCUWidth != 0); |
---|
153 | const Int numCuInHeight = m_picHeight / maxCUHeight + (m_picHeight % maxCUHeight != 0); |
---|
154 | for(Int chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++) |
---|
155 | { |
---|
156 | const ChannelType ch= ChannelType(chan); |
---|
157 | const Int ctuHeight = maxCUHeight>>getChannelTypeScaleY(ch); |
---|
158 | const Int ctuWidth = maxCUWidth>>getChannelTypeScaleX(ch); |
---|
159 | const Int stride = getStride(ch); |
---|
160 | |
---|
161 | m_ctuOffsetInBuffer[chan] = new Int[numCuInWidth * numCuInHeight]; |
---|
162 | |
---|
163 | for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++) |
---|
164 | { |
---|
165 | for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++) |
---|
166 | { |
---|
167 | m_ctuOffsetInBuffer[chan][cuRow * numCuInWidth + cuCol] = stride * cuRow * ctuHeight + cuCol * ctuWidth; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | m_subCuOffsetInBuffer[chan] = new Int[(size_t)1 << (2 * maxCUDepth)]; |
---|
172 | |
---|
173 | const Int numSubBlockPartitions=(1<<maxCUDepth); |
---|
174 | const Int minSubBlockHeight =(ctuHeight >> maxCUDepth); |
---|
175 | const Int minSubBlockWidth =(ctuWidth >> maxCUDepth); |
---|
176 | |
---|
177 | for (Int buRow = 0; buRow < numSubBlockPartitions; buRow++) |
---|
178 | { |
---|
179 | for (Int buCol = 0; buCol < numSubBlockPartitions; buCol++) |
---|
180 | { |
---|
181 | m_subCuOffsetInBuffer[chan][(buRow << maxCUDepth) + buCol] = stride * buRow * minSubBlockHeight + buCol * minSubBlockWidth; |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | Void TComPicYuv::destroy() |
---|
188 | { |
---|
189 | for(Int comp=0; comp<MAX_NUM_COMPONENT; comp++) |
---|
190 | { |
---|
191 | m_piPicOrg[comp] = NULL; |
---|
192 | |
---|
193 | if( m_apiPicBuf[comp] ) |
---|
194 | { |
---|
195 | xFree( m_apiPicBuf[comp] ); |
---|
196 | m_apiPicBuf[comp] = NULL; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | for(UInt chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++) |
---|
201 | { |
---|
202 | if (m_ctuOffsetInBuffer[chan]) |
---|
203 | { |
---|
204 | delete[] m_ctuOffsetInBuffer[chan]; |
---|
205 | m_ctuOffsetInBuffer[chan] = NULL; |
---|
206 | } |
---|
207 | if (m_subCuOffsetInBuffer[chan]) |
---|
208 | { |
---|
209 | delete[] m_subCuOffsetInBuffer[chan]; |
---|
210 | m_subCuOffsetInBuffer[chan] = NULL; |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | Void TComPicYuv::copyToPic (TComPicYuv* pcPicYuvDst) const |
---|
218 | { |
---|
219 | assert( m_chromaFormatIDC == pcPicYuvDst->getChromaFormat() ); |
---|
220 | |
---|
221 | for(Int comp=0; comp<getNumberValidComponents(); comp++) |
---|
222 | { |
---|
223 | const ComponentID compId=ComponentID(comp); |
---|
224 | const Int width = getWidth(compId); |
---|
225 | const Int height = getHeight(compId); |
---|
226 | const Int strideSrc = getStride(compId); |
---|
227 | assert(pcPicYuvDst->getWidth(compId) == width); |
---|
228 | assert(pcPicYuvDst->getHeight(compId) == height); |
---|
229 | if (strideSrc==pcPicYuvDst->getStride(compId)) |
---|
230 | { |
---|
231 | ::memcpy ( pcPicYuvDst->getBuf(compId), getBuf(compId), sizeof(Pel)*strideSrc*getTotalHeight(compId)); |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | const Pel *pSrc = getAddr(compId); |
---|
236 | Pel *pDest = pcPicYuvDst->getAddr(compId); |
---|
237 | const UInt strideDest = pcPicYuvDst->getStride(compId); |
---|
238 | |
---|
239 | for(Int y=0; y<height; y++, pSrc+=strideSrc, pDest+=strideDest) |
---|
240 | { |
---|
241 | ::memcpy(pDest, pSrc, width*sizeof(Pel)); |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | Void TComPicYuv::extendPicBorder () |
---|
249 | { |
---|
250 | if ( m_bIsBorderExtended ) |
---|
251 | { |
---|
252 | return; |
---|
253 | } |
---|
254 | |
---|
255 | for(Int comp=0; comp<getNumberValidComponents(); comp++) |
---|
256 | { |
---|
257 | const ComponentID compId=ComponentID(comp); |
---|
258 | Pel *piTxt=getAddr(compId); // piTxt = point to (0,0) of image within bigger picture. |
---|
259 | const Int stride=getStride(compId); |
---|
260 | const Int width=getWidth(compId); |
---|
261 | const Int height=getHeight(compId); |
---|
262 | const Int marginX=getMarginX(compId); |
---|
263 | const Int marginY=getMarginY(compId); |
---|
264 | |
---|
265 | Pel* pi = piTxt; |
---|
266 | // do left and right margins |
---|
267 | for (Int y = 0; y < height; y++) |
---|
268 | { |
---|
269 | for (Int x = 0; x < marginX; x++ ) |
---|
270 | { |
---|
271 | pi[ -marginX + x ] = pi[0]; |
---|
272 | pi[ width + x ] = pi[width-1]; |
---|
273 | } |
---|
274 | pi += stride; |
---|
275 | } |
---|
276 | |
---|
277 | // pi is now the (0,height) (bottom left of image within bigger picture |
---|
278 | pi -= (stride + marginX); |
---|
279 | // pi is now the (-marginX, height-1) |
---|
280 | for (Int y = 0; y < marginY; y++ ) |
---|
281 | { |
---|
282 | ::memcpy( pi + (y+1)*stride, pi, sizeof(Pel)*(width + (marginX<<1)) ); |
---|
283 | } |
---|
284 | |
---|
285 | // pi is still (-marginX, height-1) |
---|
286 | pi -= ((height-1) * stride); |
---|
287 | // pi is now (-marginX, 0) |
---|
288 | for (Int y = 0; y < marginY; y++ ) |
---|
289 | { |
---|
290 | ::memcpy( pi - (y+1)*stride, pi, sizeof(Pel)*(width + (marginX<<1)) ); |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | m_bIsBorderExtended = true; |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | // NOTE: This function is never called, but may be useful for developers. |
---|
300 | Void TComPicYuv::dump (const std::string &fileName, const BitDepths &bitDepths, const Bool bAppend, const Bool bForceTo8Bit) const |
---|
301 | { |
---|
302 | FILE *pFile = fopen (fileName.c_str(), bAppend?"ab":"wb"); |
---|
303 | |
---|
304 | Bool is16bit=false; |
---|
305 | for(Int comp = 0; comp < getNumberValidComponents() && !bForceTo8Bit; comp++) |
---|
306 | { |
---|
307 | if (bitDepths.recon[toChannelType(ComponentID(comp))]>8) |
---|
308 | { |
---|
309 | is16bit=true; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | for(Int comp = 0; comp < getNumberValidComponents(); comp++) |
---|
314 | { |
---|
315 | const ComponentID compId = ComponentID(comp); |
---|
316 | const Pel *pi = getAddr(compId); |
---|
317 | const Int stride = getStride(compId); |
---|
318 | const Int height = getHeight(compId); |
---|
319 | const Int width = getWidth(compId); |
---|
320 | |
---|
321 | if (is16bit) |
---|
322 | { |
---|
323 | for (Int y = 0; y < height; y++ ) |
---|
324 | { |
---|
325 | for (Int x = 0; x < width; x++ ) |
---|
326 | { |
---|
327 | UChar uc = (UChar)((pi[x]>>0) & 0xff); |
---|
328 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
329 | uc = (UChar)((pi[x]>>8) & 0xff); |
---|
330 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
331 | } |
---|
332 | pi += stride; |
---|
333 | } |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | const Int shift = bitDepths.recon[toChannelType(compId)] - 8; |
---|
338 | const Int offset = (shift>0)?(1<<(shift-1)):0; |
---|
339 | for (Int y = 0; y < height; y++ ) |
---|
340 | { |
---|
341 | for (Int x = 0; x < width; x++ ) |
---|
342 | { |
---|
343 | UChar uc = (UChar)Clip3<Pel>(0, 255, (pi[x]+offset)>>shift); |
---|
344 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
345 | } |
---|
346 | pi += stride; |
---|
347 | } |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | fclose(pFile); |
---|
352 | } |
---|
353 | |
---|
354 | #if AUXILIARY_PICTURES |
---|
355 | Void TComPicYuv::convertToMonochrome(const Int bitDepthChroma) |
---|
356 | { |
---|
357 | Pel grayVal = (1 << (bitDepthChroma - 1)); |
---|
358 | |
---|
359 | for( UInt comp = 1; comp < getNumberValidComponents(); comp++ ) |
---|
360 | { |
---|
361 | const ComponentID ch=ComponentID(comp); |
---|
362 | for( UInt i = 0; i < getStride(ch) * getTotalHeight(ch); i++ ) |
---|
363 | { |
---|
364 | m_apiPicBuf[ch][i] = grayVal; |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|
368 | #endif |
---|
369 | |
---|
370 | //! \} |
---|