[56] | 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 |
---|
[1200] | 4 | * granted under this license. |
---|
[56] | 5 | * |
---|
[1200] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[56] | 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 | */ |
---|
[2] | 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" |
---|
[1200] | 49 | #include "TLibVideoIO/TVideoIOYuv.h" |
---|
[2] | 50 | |
---|
[56] | 51 | //! \ingroup TLibCommon |
---|
| 52 | //! \{ |
---|
| 53 | |
---|
[2] | 54 | TComPicYuv::TComPicYuv() |
---|
| 55 | { |
---|
[1200] | 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 | |
---|
[2] | 68 | m_bIsBorderExtended = false; |
---|
| 69 | } |
---|
| 70 | |
---|
[1200] | 71 | |
---|
| 72 | |
---|
| 73 | |
---|
[2] | 74 | TComPicYuv::~TComPicYuv() |
---|
| 75 | { |
---|
| 76 | } |
---|
| 77 | |
---|
[1200] | 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | Void TComPicYuv::create ( const Int iPicWidth, ///< picture width |
---|
| 82 | const Int iPicHeight, ///< picture height |
---|
| 83 | const ChromaFormat chromaFormatIDC, ///< chroma format |
---|
| 84 | const UInt uiMaxCUWidth, ///< used for generating offsets to CUs. Can use iPicWidth if no offsets are required |
---|
| 85 | const UInt uiMaxCUHeight, ///< used for generating offsets to CUs. Can use iPicHeight if no offsets are required |
---|
| 86 | const UInt uiMaxCUDepth, ///< used for generating offsets to CUs. Can use 0 if no offsets are required |
---|
| 87 | const Bool bUseMargin) ///< if true, then a margin of uiMaxCUWidth+16 and uiMaxCUHeight+16 is created around the image. |
---|
| 88 | |
---|
[2] | 89 | { |
---|
[1200] | 90 | m_iPicWidth = iPicWidth; |
---|
| 91 | m_iPicHeight = iPicHeight; |
---|
[56] | 92 | |
---|
[608] | 93 | #if H_3D_IV_MERGE |
---|
[2] | 94 | m_iNumCuInWidth = m_iPicWidth / m_iCuWidth; |
---|
| 95 | m_iNumCuInWidth += ( m_iPicWidth % m_iCuWidth ) ? 1 : 0; |
---|
[56] | 96 | |
---|
[2] | 97 | m_iBaseUnitWidth = uiMaxCUWidth >> uiMaxCUDepth; |
---|
| 98 | m_iBaseUnitHeight = uiMaxCUHeight >> uiMaxCUDepth; |
---|
[608] | 99 | #endif |
---|
[1200] | 100 | |
---|
| 101 | m_chromaFormatIDC = chromaFormatIDC; |
---|
| 102 | m_iMarginX = (bUseMargin?uiMaxCUWidth:0) + 16; // for 16-byte alignment |
---|
| 103 | m_iMarginY = (bUseMargin?uiMaxCUHeight:0) + 16; // margin for 8-tap filter and infinite padding |
---|
[2] | 104 | m_bIsBorderExtended = false; |
---|
[1200] | 105 | |
---|
| 106 | // assign the picture arrays and set up the ptr to the top left of the original picture |
---|
[56] | 107 | { |
---|
[1200] | 108 | Int chan=0; |
---|
| 109 | for(; chan<getNumberValidComponents(); chan++) |
---|
[56] | 110 | { |
---|
[1200] | 111 | const ComponentID ch=ComponentID(chan); |
---|
| 112 | m_apiPicBuf[chan] = (Pel*)xMalloc( Pel, getStride(ch) * getTotalHeight(ch)); |
---|
| 113 | m_piPicOrg[chan] = m_apiPicBuf[chan] + (m_iMarginY >> getComponentScaleY(ch)) * getStride(ch) + (m_iMarginX >> getComponentScaleX(ch)); |
---|
[56] | 114 | } |
---|
[1200] | 115 | for(;chan<MAX_NUM_COMPONENT; chan++) |
---|
| 116 | { |
---|
| 117 | m_apiPicBuf[chan] = NULL; |
---|
| 118 | m_piPicOrg[chan] = NULL; |
---|
| 119 | } |
---|
[56] | 120 | } |
---|
[1200] | 121 | |
---|
| 122 | |
---|
| 123 | const Int numCuInWidth = m_iPicWidth / uiMaxCUWidth + (m_iPicWidth % uiMaxCUWidth != 0); |
---|
| 124 | const Int numCuInHeight = m_iPicHeight / uiMaxCUHeight + (m_iPicHeight % uiMaxCUHeight != 0); |
---|
| 125 | for(Int chan=0; chan<2; chan++) |
---|
[56] | 126 | { |
---|
[1200] | 127 | const ComponentID ch=ComponentID(chan); |
---|
| 128 | const Int ctuHeight=uiMaxCUHeight>>getComponentScaleY(ch); |
---|
| 129 | const Int ctuWidth=uiMaxCUWidth>>getComponentScaleX(ch); |
---|
| 130 | const Int stride = getStride(ch); |
---|
| 131 | |
---|
| 132 | m_ctuOffsetInBuffer[chan] = new Int[numCuInWidth * numCuInHeight]; |
---|
| 133 | |
---|
| 134 | for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++) |
---|
[56] | 135 | { |
---|
[1200] | 136 | for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++) |
---|
| 137 | { |
---|
| 138 | m_ctuOffsetInBuffer[chan][cuRow * numCuInWidth + cuCol] = stride * cuRow * ctuHeight + cuCol * ctuWidth; |
---|
| 139 | } |
---|
[56] | 140 | } |
---|
[1200] | 141 | |
---|
| 142 | m_subCuOffsetInBuffer[chan] = new Int[(size_t)1 << (2 * uiMaxCUDepth)]; |
---|
| 143 | |
---|
| 144 | const Int numSubBlockPartitions=(1<<uiMaxCUDepth); |
---|
| 145 | const Int minSubBlockHeight =(ctuHeight >> uiMaxCUDepth); |
---|
| 146 | const Int minSubBlockWidth =(ctuWidth >> uiMaxCUDepth); |
---|
| 147 | |
---|
| 148 | for (Int buRow = 0; buRow < numSubBlockPartitions; buRow++) |
---|
| 149 | { |
---|
| 150 | for (Int buCol = 0; buCol < numSubBlockPartitions; buCol++) |
---|
| 151 | { |
---|
| 152 | m_subCuOffsetInBuffer[chan][(buRow << uiMaxCUDepth) + buCol] = stride * buRow * minSubBlockHeight + buCol * minSubBlockWidth; |
---|
| 153 | } |
---|
| 154 | } |
---|
[56] | 155 | } |
---|
[2] | 156 | return; |
---|
| 157 | } |
---|
| 158 | |
---|
[608] | 159 | |
---|
[2] | 160 | |
---|
[1200] | 161 | Void TComPicYuv::destroy() |
---|
[2] | 162 | { |
---|
[1200] | 163 | for(Int chan=0; chan<MAX_NUM_COMPONENT; chan++) |
---|
[56] | 164 | { |
---|
[1200] | 165 | m_piPicOrg[chan] = NULL; |
---|
| 166 | |
---|
| 167 | if( m_apiPicBuf[chan] ) |
---|
[56] | 168 | { |
---|
[1200] | 169 | xFree( m_apiPicBuf[chan] ); |
---|
| 170 | m_apiPicBuf[chan] = NULL; |
---|
[56] | 171 | } |
---|
| 172 | } |
---|
[1200] | 173 | |
---|
| 174 | for(UInt chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++) |
---|
[56] | 175 | { |
---|
[1200] | 176 | if (m_ctuOffsetInBuffer[chan]) |
---|
[56] | 177 | { |
---|
[1200] | 178 | delete[] m_ctuOffsetInBuffer[chan]; |
---|
| 179 | m_ctuOffsetInBuffer[chan] = NULL; |
---|
[56] | 180 | } |
---|
[1200] | 181 | if (m_subCuOffsetInBuffer[chan]) |
---|
| 182 | { |
---|
| 183 | delete[] m_subCuOffsetInBuffer[chan]; |
---|
| 184 | m_subCuOffsetInBuffer[chan] = NULL; |
---|
| 185 | } |
---|
[56] | 186 | } |
---|
[2] | 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | |
---|
[1200] | 191 | Void TComPicYuv::copyToPic (TComPicYuv* pcPicYuvDst) const |
---|
[2] | 192 | { |
---|
[1200] | 193 | assert( m_iPicWidth == pcPicYuvDst->getWidth(COMPONENT_Y) ); |
---|
| 194 | assert( m_iPicHeight == pcPicYuvDst->getHeight(COMPONENT_Y) ); |
---|
| 195 | assert( m_chromaFormatIDC == pcPicYuvDst->getChromaFormat() ); |
---|
[2] | 196 | |
---|
[1200] | 197 | for(Int chan=0; chan<getNumberValidComponents(); chan++) |
---|
| 198 | { |
---|
| 199 | const ComponentID ch=ComponentID(chan); |
---|
| 200 | ::memcpy ( pcPicYuvDst->getBuf(ch), m_apiPicBuf[ch], sizeof (Pel) * getStride(ch) * getTotalHeight(ch)); |
---|
| 201 | } |
---|
[2] | 202 | return; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | Void TComPicYuv::extendPicBorder () |
---|
| 207 | { |
---|
[1200] | 208 | if ( m_bIsBorderExtended ) |
---|
| 209 | { |
---|
| 210 | return; |
---|
| 211 | } |
---|
[2] | 212 | |
---|
[1200] | 213 | for(Int chan=0; chan<getNumberValidComponents(); chan++) |
---|
[2] | 214 | { |
---|
[1200] | 215 | const ComponentID ch=ComponentID(chan); |
---|
| 216 | Pel *piTxt=getAddr(ch); // piTxt = point to (0,0) of image within bigger picture. |
---|
| 217 | const Int iStride=getStride(ch); |
---|
| 218 | const Int iWidth=getWidth(ch); |
---|
| 219 | const Int iHeight=getHeight(ch); |
---|
| 220 | const Int iMarginX=getMarginX(ch); |
---|
| 221 | const Int iMarginY=getMarginY(ch); |
---|
| 222 | |
---|
| 223 | Pel* pi = piTxt; |
---|
| 224 | // do left and right margins |
---|
| 225 | for (Int y = 0; y < iHeight; y++) |
---|
[2] | 226 | { |
---|
[1200] | 227 | for (Int x = 0; x < iMarginX; x++ ) |
---|
| 228 | { |
---|
| 229 | pi[ -iMarginX + x ] = pi[0]; |
---|
| 230 | pi[ iWidth + x ] = pi[iWidth-1]; |
---|
| 231 | } |
---|
| 232 | pi += iStride; |
---|
[2] | 233 | } |
---|
[1200] | 234 | |
---|
| 235 | // pi is now the (0,height) (bottom left of image within bigger picture |
---|
| 236 | pi -= (iStride + iMarginX); |
---|
| 237 | // pi is now the (-marginX, height-1) |
---|
| 238 | for (Int y = 0; y < iMarginY; y++ ) |
---|
| 239 | { |
---|
| 240 | ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | // pi is still (-marginX, height-1) |
---|
| 244 | pi -= ((iHeight-1) * iStride); |
---|
| 245 | // pi is now (-marginX, 0) |
---|
| 246 | for (Int y = 0; y < iMarginY; y++ ) |
---|
| 247 | { |
---|
| 248 | ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
---|
| 249 | } |
---|
[2] | 250 | } |
---|
[1200] | 251 | |
---|
| 252 | m_bIsBorderExtended = true; |
---|
[2] | 253 | } |
---|
| 254 | |
---|
| 255 | |
---|
[1200] | 256 | |
---|
| 257 | // NOTE: This function is never called, but may be useful for developers. |
---|
| 258 | Void TComPicYuv::dump (const Char* pFileName, const BitDepths &bitDepths, Bool bAdd) const |
---|
[2] | 259 | { |
---|
| 260 | FILE* pFile; |
---|
| 261 | if (!bAdd) |
---|
| 262 | { |
---|
| 263 | pFile = fopen (pFileName, "wb"); |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
| 267 | pFile = fopen (pFileName, "ab"); |
---|
| 268 | } |
---|
[1200] | 269 | |
---|
| 270 | |
---|
| 271 | for(Int chan = 0; chan < getNumberValidComponents(); chan++) |
---|
[2] | 272 | { |
---|
[1200] | 273 | const ComponentID ch = ComponentID(chan); |
---|
| 274 | const Int shift = bitDepths.recon[toChannelType(ch)] - 8; |
---|
| 275 | const Int offset = (shift>0)?(1<<(shift-1)):0; |
---|
| 276 | const Pel *pi = getAddr(ch); |
---|
| 277 | const Int stride = getStride(ch); |
---|
| 278 | const Int height = getHeight(ch); |
---|
| 279 | const Int width = getWidth(ch); |
---|
| 280 | |
---|
| 281 | for (Int y = 0; y < height; y++ ) |
---|
[2] | 282 | { |
---|
[1200] | 283 | for (Int x = 0; x < width; x++ ) |
---|
| 284 | { |
---|
| 285 | UChar uc = (UChar)Clip3<Pel>(0, 255, (pi[x]+offset)>>shift); |
---|
| 286 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
| 287 | } |
---|
| 288 | pi += stride; |
---|
[2] | 289 | } |
---|
| 290 | } |
---|
[608] | 291 | |
---|
[2] | 292 | fclose(pFile); |
---|
| 293 | } |
---|
[608] | 294 | #if H_3D |
---|
| 295 | #if H_3D_IV_MERGE |
---|
[2] | 296 | Void |
---|
| 297 | TComPicYuv::getTopLeftSamplePos( Int iCuAddr, Int iAbsZorderIdx, Int& riX, Int& riY ) |
---|
| 298 | { |
---|
| 299 | Int iRastPartIdx = g_auiZscanToRaster[iAbsZorderIdx]; |
---|
| 300 | Int iCuSizeInBases = m_iCuWidth / m_iBaseUnitWidth; |
---|
| 301 | Int iCuX = iCuAddr % m_iNumCuInWidth; |
---|
| 302 | Int iCuY = iCuAddr / m_iNumCuInWidth; |
---|
| 303 | Int iBaseX = iRastPartIdx % iCuSizeInBases; |
---|
| 304 | Int iBaseY = iRastPartIdx / iCuSizeInBases; |
---|
| 305 | riX = iCuX * m_iCuWidth + iBaseX * m_iBaseUnitWidth; |
---|
[56] | 306 | riY = iCuY * m_iCuHeight + iBaseY * m_iBaseUnitHeight; |
---|
[2] | 307 | } |
---|
| 308 | |
---|
| 309 | Void |
---|
| 310 | TComPicYuv::getCUAddrAndPartIdx( Int iX, Int iY, Int& riCuAddr, Int& riAbsZorderIdx ) |
---|
| 311 | { |
---|
| 312 | Int iCuX = iX / m_iCuWidth; |
---|
| 313 | Int iCuY = iY / m_iCuHeight; |
---|
| 314 | Int iBaseX = ( iX - iCuX * m_iCuWidth ) / m_iBaseUnitWidth; |
---|
| 315 | Int iBaseY = ( iY - iCuY * m_iCuHeight ) / m_iBaseUnitHeight; |
---|
| 316 | Int iCuSizeInBases = m_iCuWidth / m_iBaseUnitWidth; |
---|
| 317 | riCuAddr = iCuY * m_iNumCuInWidth + iCuX; |
---|
| 318 | Int iRastPartIdx = iBaseY * iCuSizeInBases + iBaseX; |
---|
| 319 | riAbsZorderIdx = g_auiRasterToZscan[ iRastPartIdx ]; |
---|
| 320 | } |
---|
[608] | 321 | #endif |
---|
[2] | 322 | Void TComPicYuv::setLumaTo( Pel pVal ) |
---|
| 323 | { |
---|
[332] | 324 | xSetPels( getLumaAddr(), getStride(), getWidth(), getHeight(), pVal ); |
---|
[2] | 325 | } |
---|
[1200] | 326 | #endif |
---|
| 327 | #if NH_3D_VSO |
---|
[2] | 328 | Void TComPicYuv::setChromaTo( Pel pVal ) |
---|
| 329 | { |
---|
[1200] | 330 | xSetPels( getAddr( COMPONENT_Cb ), getStride( COMPONENT_Cb ), getWidth( COMPONENT_Cb), getHeight( COMPONENT_Cb ), pVal ); |
---|
| 331 | xSetPels( getAddr( COMPONENT_Cr ), getStride( COMPONENT_Cr ), getWidth( COMPONENT_Cr), getHeight( COMPONENT_Cr ), pVal ); |
---|
[2] | 332 | } |
---|
| 333 | |
---|
| 334 | Void TComPicYuv::xSetPels( Pel* piPelSource , Int iSourceStride, Int iWidth, Int iHeight, Pel iVal ) |
---|
| 335 | { |
---|
| 336 | for (Int iYPos = 0; iYPos < iHeight; iYPos++) |
---|
| 337 | { |
---|
| 338 | for (Int iXPos = 0; iXPos < iWidth; iXPos++) |
---|
| 339 | { |
---|
| 340 | piPelSource[iXPos] = iVal; |
---|
| 341 | } |
---|
| 342 | piPelSource += iSourceStride; |
---|
| 343 | } |
---|
[56] | 344 | } |
---|
[443] | 345 | #endif |
---|
[56] | 346 | //! \} |
---|