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