[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 | * |
---|
[595] | 6 | * Copyright (c) 2010-2014, 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 | |
---|
| 78 | #if SVC_EXTENSION |
---|
[1029] | 79 | Void TComPicYuv::create( const Int iPicWidth, const Int iPicHeight, const ChromaFormat chromaFormatIDC, const UInt uiMaxCUWidth, const UInt uiMaxCUHeight, const UInt uiMaxCUDepth, Window* conformanceWindow ) |
---|
[869] | 80 | #else |
---|
[1029] | 81 | Void TComPicYuv::create( const Int iPicWidth, const Int iPicHeight, const ChromaFormat chromaFormatIDC, |
---|
| 82 | const UInt uiMaxCUWidth, const UInt uiMaxCUHeight, const UInt uiMaxCUDepth ) |
---|
[494] | 83 | #endif |
---|
[313] | 84 | { |
---|
[1029] | 85 | m_iPicWidth = iPicWidth; |
---|
| 86 | m_iPicHeight = iPicHeight; |
---|
| 87 | m_chromaFormatIDC = chromaFormatIDC; |
---|
[313] | 88 | |
---|
[815] | 89 | #if SVC_EXTENSION |
---|
[869] | 90 | if(conformanceWindow != NULL) |
---|
| 91 | { |
---|
| 92 | m_conformanceWindow = *conformanceWindow; |
---|
| 93 | } |
---|
[313] | 94 | #endif |
---|
| 95 | |
---|
[494] | 96 | #if LAYER_CTB |
---|
[1029] | 97 | m_iMarginX = uiMaxCUWidth + 16; // for 16-byte alignment |
---|
| 98 | m_iMarginY = uiMaxCUHeight + 16; // margin for 8-tap filter and infinite padding |
---|
[494] | 99 | #else |
---|
[1029] | 100 | m_iMarginX = g_uiMaxCUWidth + 16; // for 16-byte alignment |
---|
| 101 | m_iMarginY = g_uiMaxCUHeight + 16; // margin for 8-tap filter and infinite padding |
---|
[494] | 102 | #endif |
---|
[313] | 103 | |
---|
| 104 | m_bIsBorderExtended = false; |
---|
[1029] | 105 | |
---|
| 106 | // assign the picture arrays and set up the ptr to the top left of the original picture |
---|
[313] | 107 | { |
---|
[1029] | 108 | Int chan=0; |
---|
| 109 | for(; chan<getNumberValidComponents(); chan++) |
---|
[313] | 110 | { |
---|
[1029] | 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)); |
---|
[313] | 114 | } |
---|
[1029] | 115 | for(;chan<MAX_NUM_COMPONENT; chan++) |
---|
[313] | 116 | { |
---|
[1029] | 117 | m_apiPicBuf[chan] = NULL; |
---|
| 118 | m_piPicOrg[chan] = NULL; |
---|
[313] | 119 | } |
---|
| 120 | } |
---|
[1029] | 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++) |
---|
| 126 | { |
---|
| 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++) |
---|
| 135 | for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++) |
---|
| 136 | m_ctuOffsetInBuffer[chan][cuRow * numCuInWidth + cuCol] = stride * cuRow * ctuHeight + cuCol * ctuWidth; |
---|
| 137 | |
---|
| 138 | m_subCuOffsetInBuffer[chan] = new Int[(size_t)1 << (2 * uiMaxCUDepth)]; |
---|
| 139 | |
---|
| 140 | const Int numSubBlockPartitions=(1<<uiMaxCUDepth); |
---|
| 141 | const Int minSubBlockHeight =(ctuHeight >> uiMaxCUDepth); |
---|
| 142 | const Int minSubBlockWidth =(ctuWidth >> uiMaxCUDepth); |
---|
| 143 | |
---|
| 144 | for (Int buRow = 0; buRow < numSubBlockPartitions; buRow++) |
---|
| 145 | for (Int buCol = 0; buCol < numSubBlockPartitions; buCol++) |
---|
| 146 | m_subCuOffsetInBuffer[chan][(buRow << uiMaxCUDepth) + buCol] = stride * buRow * minSubBlockHeight + buCol * minSubBlockWidth; |
---|
| 147 | } |
---|
[313] | 148 | return; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | |
---|
[1029] | 153 | Void TComPicYuv::destroy() |
---|
[313] | 154 | { |
---|
[1029] | 155 | for(Int chan=0; chan<MAX_NUM_COMPONENT; chan++) |
---|
[313] | 156 | { |
---|
[1029] | 157 | m_piPicOrg[chan] = NULL; |
---|
| 158 | |
---|
| 159 | if( m_apiPicBuf[chan] ){ xFree( m_apiPicBuf[chan] ); m_apiPicBuf[chan] = NULL; } |
---|
[313] | 160 | } |
---|
[1029] | 161 | |
---|
| 162 | for(UInt chan=0; chan<MAX_NUM_CHANNEL_TYPE; chan++) |
---|
[313] | 163 | { |
---|
[1029] | 164 | if (m_ctuOffsetInBuffer[chan]) delete[] m_ctuOffsetInBuffer[chan]; m_ctuOffsetInBuffer[chan] = NULL; |
---|
| 165 | if (m_subCuOffsetInBuffer[chan]) delete[] m_subCuOffsetInBuffer[chan]; m_subCuOffsetInBuffer[chan] = NULL; |
---|
[313] | 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | |
---|
[1029] | 171 | Void TComPicYuv::copyToPic (TComPicYuv* pcPicYuvDst) const |
---|
[313] | 172 | { |
---|
[1029] | 173 | assert( m_iPicWidth == pcPicYuvDst->getWidth(COMPONENT_Y) ); |
---|
| 174 | assert( m_iPicHeight == pcPicYuvDst->getHeight(COMPONENT_Y) ); |
---|
| 175 | assert( m_chromaFormatIDC == pcPicYuvDst->getChromaFormat() ); |
---|
[313] | 176 | |
---|
[1029] | 177 | for(Int chan=0; chan<getNumberValidComponents(); chan++) |
---|
| 178 | { |
---|
| 179 | const ComponentID ch=ComponentID(chan); |
---|
| 180 | ::memcpy ( pcPicYuvDst->getBuf(ch), m_apiPicBuf[ch], sizeof (Pel) * getStride(ch) * getTotalHeight(ch)); |
---|
| 181 | } |
---|
[313] | 182 | return; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | Void TComPicYuv::extendPicBorder () |
---|
| 187 | { |
---|
| 188 | if ( m_bIsBorderExtended ) return; |
---|
| 189 | |
---|
[1029] | 190 | for(Int chan=0; chan<getNumberValidComponents(); chan++) |
---|
[313] | 191 | { |
---|
[1029] | 192 | const ComponentID ch=ComponentID(chan); |
---|
| 193 | Pel *piTxt=getAddr(ch); // piTxt = point to (0,0) of image within bigger picture. |
---|
| 194 | const Int iStride=getStride(ch); |
---|
| 195 | const Int iWidth=getWidth(ch); |
---|
| 196 | const Int iHeight=getHeight(ch); |
---|
| 197 | const Int iMarginX=getMarginX(ch); |
---|
| 198 | const Int iMarginY=getMarginY(ch); |
---|
| 199 | |
---|
| 200 | Pel* pi = piTxt; |
---|
| 201 | // do left and right margins |
---|
| 202 | for (Int y = 0; y < iHeight; y++) |
---|
[313] | 203 | { |
---|
[1029] | 204 | for (Int x = 0; x < iMarginX; x++ ) |
---|
| 205 | { |
---|
| 206 | pi[ -iMarginX + x ] = pi[0]; |
---|
| 207 | pi[ iWidth + x ] = pi[iWidth-1]; |
---|
| 208 | } |
---|
| 209 | pi += iStride; |
---|
[313] | 210 | } |
---|
[1029] | 211 | |
---|
| 212 | // pi is now the (0,height) (bottom left of image within bigger picture |
---|
| 213 | pi -= (iStride + iMarginX); |
---|
| 214 | // pi is now the (-marginX, height-1) |
---|
| 215 | for (Int y = 0; y < iMarginY; y++ ) |
---|
| 216 | { |
---|
| 217 | ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | // pi is still (-marginX, height-1) |
---|
| 221 | pi -= ((iHeight-1) * iStride); |
---|
| 222 | // pi is now (-marginX, 0) |
---|
| 223 | for (Int y = 0; y < iMarginY; y++ ) |
---|
| 224 | { |
---|
| 225 | ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
---|
| 226 | } |
---|
[313] | 227 | } |
---|
[1029] | 228 | |
---|
| 229 | m_bIsBorderExtended = true; |
---|
[313] | 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
[1029] | 233 | |
---|
| 234 | // NOTE: This function is never called, but may be useful for developers. |
---|
| 235 | Void TComPicYuv::dump (const Char* pFileName, Bool bAdd) const |
---|
[313] | 236 | { |
---|
| 237 | FILE* pFile; |
---|
| 238 | if (!bAdd) |
---|
| 239 | { |
---|
| 240 | pFile = fopen (pFileName, "wb"); |
---|
| 241 | } |
---|
| 242 | else |
---|
| 243 | { |
---|
| 244 | pFile = fopen (pFileName, "ab"); |
---|
| 245 | } |
---|
[1029] | 246 | |
---|
| 247 | |
---|
| 248 | for(Int chan = 0; chan < getNumberValidComponents(); chan++) |
---|
[313] | 249 | { |
---|
[1029] | 250 | const ComponentID ch = ComponentID(chan); |
---|
| 251 | const Int shift = g_bitDepth[toChannelType(ch)] - 8; |
---|
| 252 | const Int offset = (shift>0)?(1<<(shift-1)):0; |
---|
| 253 | const Pel *pi = getAddr(ch); |
---|
| 254 | const Int stride = getStride(ch); |
---|
| 255 | const Int height = getHeight(ch); |
---|
| 256 | const Int width = getWidth(ch); |
---|
| 257 | |
---|
| 258 | for (Int y = 0; y < height; y++ ) |
---|
[313] | 259 | { |
---|
[1029] | 260 | for (Int x = 0; x < width; x++ ) |
---|
| 261 | { |
---|
| 262 | UChar uc = (UChar)Clip3<Pel>(0, 255, (pi[x]+offset)>>shift); |
---|
| 263 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
| 264 | } |
---|
| 265 | pi += stride; |
---|
[313] | 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | fclose(pFile); |
---|
| 270 | } |
---|
| 271 | |
---|
[815] | 272 | Void TComPicYuv::dump( Char* pFileName, Bool bAdd, Int bitDepth ) |
---|
| 273 | { |
---|
| 274 | FILE* pFile; |
---|
| 275 | if (!bAdd) |
---|
| 276 | { |
---|
| 277 | pFile = fopen (pFileName, "wb"); |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | pFile = fopen (pFileName, "ab"); |
---|
| 282 | } |
---|
[313] | 283 | |
---|
[815] | 284 | if( bitDepth == 8 ) |
---|
| 285 | { |
---|
| 286 | dump( pFileName, bAdd ); |
---|
| 287 | return; |
---|
| 288 | } |
---|
[1029] | 289 | |
---|
| 290 | for(Int chan = 0; chan < getNumberValidComponents(); chan++) |
---|
[815] | 291 | { |
---|
[1029] | 292 | const ComponentID ch = ComponentID(chan); |
---|
| 293 | const Pel *pi = getAddr(ch); |
---|
| 294 | const Int stride = getStride(ch); |
---|
| 295 | const Int height = getHeight(ch); |
---|
| 296 | const Int width = getWidth(ch); |
---|
| 297 | |
---|
| 298 | for (Int y = 0; y < height; y++ ) |
---|
[815] | 299 | { |
---|
[1029] | 300 | for (Int x = 0; x < width; x++ ) |
---|
| 301 | { |
---|
[1089] | 302 | Pel pix = pi[x]; |
---|
[1029] | 303 | |
---|
| 304 | UChar uc = pix & 0xff; |
---|
| 305 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
| 306 | uc = (pix >> 8) & 0xff; |
---|
| 307 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
---|
| 308 | } |
---|
| 309 | pi += stride; |
---|
[815] | 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
[1029] | 313 | fclose(pFile); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | #if AUXILIARY_PICTURES |
---|
| 317 | Void TComPicYuv::convertToMonochrome() |
---|
| 318 | { |
---|
| 319 | Pel grayVal = (1 << (g_bitDepth[CHANNEL_TYPE_CHROMA] - 1)); |
---|
| 320 | |
---|
[1031] | 321 | for( UInt comp = 1; comp < getNumberValidComponents(); comp++ ) |
---|
[815] | 322 | { |
---|
[1031] | 323 | const ComponentID ch=ComponentID(comp); |
---|
| 324 | for( UInt i = 0; i < getStride(ch) * getTotalHeight(ch); i++ ) |
---|
| 325 | { |
---|
| 326 | m_apiPicBuf[ch][i] = grayVal; |
---|
| 327 | } |
---|
[815] | 328 | } |
---|
| 329 | } |
---|
[1029] | 330 | #endif |
---|
[815] | 331 | |
---|
[313] | 332 | //! \} |
---|