[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 |
---|
[1313] | 4 | * granted under this license. |
---|
[56] | 5 | * |
---|
[1313] | 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" |
---|
[1313] | 49 | #include "TLibVideoIO/TVideoIOYuv.h" |
---|
[2] | 50 | |
---|
[56] | 51 | //! \ingroup TLibCommon |
---|
| 52 | //! \{ |
---|
| 53 | |
---|
[2] | 54 | TComPicYuv::TComPicYuv() |
---|
| 55 | { |
---|
[1313] | 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 | |
---|
[1313] | 71 | |
---|
| 72 | |
---|
| 73 | |
---|
[2] | 74 | TComPicYuv::~TComPicYuv() |
---|
| 75 | { |
---|
| 76 | } |
---|
| 77 | |
---|
[1313] | 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 | { |
---|
[1313] | 90 | m_iPicWidth = iPicWidth; |
---|
| 91 | m_iPicHeight = iPicHeight; |
---|
| 92 | |
---|
[56] | 93 | |
---|
[1313] | 94 | m_chromaFormatIDC = chromaFormatIDC; |
---|
| 95 | m_iMarginX = (bUseMargin?uiMaxCUWidth:0) + 16; // for 16-byte alignment |
---|
| 96 | m_iMarginY = (bUseMargin?uiMaxCUHeight:0) + 16; // margin for 8-tap filter and infinite padding |
---|
[2] | 97 | m_bIsBorderExtended = false; |
---|
[1313] | 98 | |
---|
| 99 | // assign the picture arrays and set up the ptr to the top left of the original picture |
---|
[56] | 100 | { |
---|
[1313] | 101 | Int chan=0; |
---|
| 102 | for(; chan<getNumberValidComponents(); chan++) |
---|
[56] | 103 | { |
---|
[1313] | 104 | const ComponentID ch=ComponentID(chan); |
---|
| 105 | m_apiPicBuf[chan] = (Pel*)xMalloc( Pel, getStride(ch) * getTotalHeight(ch)); |
---|
| 106 | m_piPicOrg[chan] = m_apiPicBuf[chan] + (m_iMarginY >> getComponentScaleY(ch)) * getStride(ch) + (m_iMarginX >> getComponentScaleX(ch)); |
---|
[56] | 107 | } |
---|
[1313] | 108 | for(;chan<MAX_NUM_COMPONENT; chan++) |
---|
| 109 | { |
---|
| 110 | m_apiPicBuf[chan] = NULL; |
---|
| 111 | m_piPicOrg[chan] = NULL; |
---|
| 112 | } |
---|
[56] | 113 | } |
---|
[1313] | 114 | |
---|
| 115 | |
---|
| 116 | const Int numCuInWidth = m_iPicWidth / uiMaxCUWidth + (m_iPicWidth % uiMaxCUWidth != 0); |
---|
| 117 | const Int numCuInHeight = m_iPicHeight / uiMaxCUHeight + (m_iPicHeight % uiMaxCUHeight != 0); |
---|
| 118 | for(Int chan=0; chan<2; chan++) |
---|
[56] | 119 | { |
---|
[1313] | 120 | const ComponentID ch=ComponentID(chan); |
---|
| 121 | const Int ctuHeight=uiMaxCUHeight>>getComponentScaleY(ch); |
---|
| 122 | const Int ctuWidth=uiMaxCUWidth>>getComponentScaleX(ch); |
---|
| 123 | const Int stride = getStride(ch); |
---|
| 124 | |
---|
| 125 | m_ctuOffsetInBuffer[chan] = new Int[numCuInWidth * numCuInHeight]; |
---|
| 126 | |
---|
| 127 | for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++) |
---|
[56] | 128 | { |
---|
[1313] | 129 | for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++) |
---|
| 130 | { |
---|
| 131 | m_ctuOffsetInBuffer[chan][cuRow * numCuInWidth + cuCol] = stride * cuRow * ctuHeight + cuCol * ctuWidth; |
---|
| 132 | } |
---|
[56] | 133 | } |
---|
[1313] | 134 | |
---|
| 135 | m_subCuOffsetInBuffer[chan] = new Int[(size_t)1 << (2 * uiMaxCUDepth)]; |
---|
| 136 | |
---|
| 137 | const Int numSubBlockPartitions=(1<<uiMaxCUDepth); |
---|
| 138 | const Int minSubBlockHeight =(ctuHeight >> uiMaxCUDepth); |
---|
| 139 | const Int minSubBlockWidth =(ctuWidth >> uiMaxCUDepth); |
---|
| 140 | |
---|
| 141 | for (Int buRow = 0; buRow < numSubBlockPartitions; buRow++) |
---|
| 142 | { |
---|
| 143 | for (Int buCol = 0; buCol < numSubBlockPartitions; buCol++) |
---|
| 144 | { |
---|
| 145 | m_subCuOffsetInBuffer[chan][(buRow << uiMaxCUDepth) + buCol] = stride * buRow * minSubBlockHeight + buCol * minSubBlockWidth; |
---|
| 146 | } |
---|
| 147 | } |
---|
[56] | 148 | } |
---|
[2] | 149 | return; |
---|
| 150 | } |
---|
| 151 | |
---|
[608] | 152 | |
---|
[2] | 153 | |
---|
[1313] | 154 | Void TComPicYuv::destroy() |
---|
[2] | 155 | { |
---|
[1313] | 156 | for(Int chan=0; chan<MAX_NUM_COMPONENT; chan++) |
---|
[56] | 157 | { |
---|
[1313] | 158 | m_piPicOrg[chan] = NULL; |
---|
| 159 | |
---|
| 160 | if( m_apiPicBuf[chan] ) |
---|
[56] | 161 | { |
---|
[1313] | 162 | xFree( m_apiPicBuf[chan] ); |
---|
| 163 | m_apiPicBuf[chan] = NULL; |
---|
[56] | 164 | } |
---|
| 165 | } |
---|
[1313] | 166 | |
---|
| 167 | for(UInt chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++) |
---|
[56] | 168 | { |
---|
[1313] | 169 | if (m_ctuOffsetInBuffer[chan]) |
---|
[56] | 170 | { |
---|
[1313] | 171 | delete[] m_ctuOffsetInBuffer[chan]; |
---|
| 172 | m_ctuOffsetInBuffer[chan] = NULL; |
---|
[56] | 173 | } |
---|
[1313] | 174 | if (m_subCuOffsetInBuffer[chan]) |
---|
| 175 | { |
---|
| 176 | delete[] m_subCuOffsetInBuffer[chan]; |
---|
| 177 | m_subCuOffsetInBuffer[chan] = NULL; |
---|
| 178 | } |
---|
[56] | 179 | } |
---|
[2] | 180 | } |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
[1313] | 184 | Void TComPicYuv::copyToPic (TComPicYuv* pcPicYuvDst) const |
---|
[2] | 185 | { |
---|
[1313] | 186 | assert( m_iPicWidth == pcPicYuvDst->getWidth(COMPONENT_Y) ); |
---|
| 187 | assert( m_iPicHeight == pcPicYuvDst->getHeight(COMPONENT_Y) ); |
---|
| 188 | assert( m_chromaFormatIDC == pcPicYuvDst->getChromaFormat() ); |
---|
[2] | 189 | |
---|
[1313] | 190 | for(Int chan=0; chan<getNumberValidComponents(); chan++) |
---|
| 191 | { |
---|
| 192 | const ComponentID ch=ComponentID(chan); |
---|
| 193 | ::memcpy ( pcPicYuvDst->getBuf(ch), m_apiPicBuf[ch], sizeof (Pel) * getStride(ch) * getTotalHeight(ch)); |
---|
| 194 | } |
---|
[2] | 195 | return; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | Void TComPicYuv::extendPicBorder () |
---|
| 200 | { |
---|
[1313] | 201 | if ( m_bIsBorderExtended ) |
---|
| 202 | { |
---|
| 203 | return; |
---|
| 204 | } |
---|
[2] | 205 | |
---|
[1313] | 206 | for(Int chan=0; chan<getNumberValidComponents(); chan++) |
---|
[2] | 207 | { |
---|
[1313] | 208 | const ComponentID ch=ComponentID(chan); |
---|
| 209 | Pel *piTxt=getAddr(ch); // piTxt = point to (0,0) of image within bigger picture. |
---|
| 210 | const Int iStride=getStride(ch); |
---|
| 211 | const Int iWidth=getWidth(ch); |
---|
| 212 | const Int iHeight=getHeight(ch); |
---|
| 213 | const Int iMarginX=getMarginX(ch); |
---|
| 214 | const Int iMarginY=getMarginY(ch); |
---|
| 215 | |
---|
| 216 | Pel* pi = piTxt; |
---|
| 217 | // do left and right margins |
---|
| 218 | for (Int y = 0; y < iHeight; y++) |
---|
[2] | 219 | { |
---|
[1313] | 220 | for (Int x = 0; x < iMarginX; x++ ) |
---|
| 221 | { |
---|
| 222 | pi[ -iMarginX + x ] = pi[0]; |
---|
| 223 | pi[ iWidth + x ] = pi[iWidth-1]; |
---|
| 224 | } |
---|
| 225 | pi += iStride; |
---|
[2] | 226 | } |
---|
[1313] | 227 | |
---|
| 228 | // pi is now the (0,height) (bottom left of image within bigger picture |
---|
| 229 | pi -= (iStride + iMarginX); |
---|
| 230 | // pi is now the (-marginX, height-1) |
---|
| 231 | for (Int y = 0; y < iMarginY; y++ ) |
---|
| 232 | { |
---|
| 233 | ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | // pi is still (-marginX, height-1) |
---|
| 237 | pi -= ((iHeight-1) * iStride); |
---|
| 238 | // pi is now (-marginX, 0) |
---|
| 239 | for (Int y = 0; y < iMarginY; y++ ) |
---|
| 240 | { |
---|
| 241 | ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
---|
| 242 | } |
---|
[2] | 243 | } |
---|
[1313] | 244 | |
---|
| 245 | m_bIsBorderExtended = true; |
---|
[2] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[1313] | 249 | |
---|
| 250 | // NOTE: This function is never called, but may be useful for developers. |
---|
| 251 | Void TComPicYuv::dump (const Char* pFileName, const BitDepths &bitDepths, Bool bAdd) const |
---|
[2] | 252 | { |
---|
| 253 | FILE* pFile; |
---|
| 254 | if (!bAdd) |
---|
| 255 | { |
---|
| 256 | pFile = fopen (pFileName, "wb"); |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | pFile = fopen (pFileName, "ab"); |
---|
| 261 | } |
---|
[1313] | 262 | |
---|
| 263 | |
---|
| 264 | for(Int chan = 0; chan < getNumberValidComponents(); chan++) |
---|
[2] | 265 | { |
---|
[1313] | 266 | const ComponentID ch = ComponentID(chan); |
---|
| 267 | const Int shift = bitDepths.recon[toChannelType(ch)] - 8; |
---|
| 268 | const Int offset = (shift>0)?(1<<(shift-1)):0; |
---|
| 269 | const Pel *pi = getAddr(ch); |
---|
| 270 | const Int stride = getStride(ch); |
---|
| 271 | const Int height = getHeight(ch); |
---|
| 272 | const Int width = getWidth(ch); |
---|
| 273 | |
---|
| 274 | for (Int y = 0; y < height; y++ ) |
---|
[2] | 275 | { |
---|
[1313] | 276 | for (Int x = 0; x < width; x++ ) |
---|
| 277 | { |
---|
| 278 | UChar uc = (UChar)Clip3<Pel>(0, 255, (pi[x]+offset)>>shift); |
---|
| 279 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
| 280 | } |
---|
| 281 | pi += stride; |
---|
[2] | 282 | } |
---|
| 283 | } |
---|
[608] | 284 | |
---|
[2] | 285 | fclose(pFile); |
---|
| 286 | } |
---|
| 287 | |
---|
[56] | 288 | //! \} |
---|