| [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 |
|---|
| 4 | * granted under this license. |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) 2010-2013, 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 | |
|---|
| 50 | //! \ingroup TLibCommon |
|---|
| 51 | //! \{ |
|---|
| 52 | |
|---|
| 53 | TComPicYuv::TComPicYuv() |
|---|
| 54 | { |
|---|
| 55 | m_apiPicBufY = NULL; // Buffer (including margin) |
|---|
| 56 | m_apiPicBufU = NULL; |
|---|
| 57 | m_apiPicBufV = NULL; |
|---|
| 58 | |
|---|
| 59 | m_piPicOrgY = NULL; // m_apiPicBufY + m_iMarginLuma*getStride() + m_iMarginLuma |
|---|
| 60 | m_piPicOrgU = NULL; |
|---|
| 61 | m_piPicOrgV = NULL; |
|---|
| 62 | |
|---|
| 63 | m_bIsBorderExtended = false; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | TComPicYuv::~TComPicYuv() |
|---|
| 67 | { |
|---|
| 68 | } |
|---|
| 69 | #if SVC_UPSAMPLING |
|---|
| 70 | Void TComPicYuv::create( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth, TComSPS* pcSps ) |
|---|
| 71 | #else |
|---|
| 72 | Void TComPicYuv::create( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth ) |
|---|
| 73 | #endif |
|---|
| 74 | { |
|---|
| 75 | m_iPicWidth = iPicWidth; |
|---|
| 76 | m_iPicHeight = iPicHeight; |
|---|
| 77 | |
|---|
| 78 | #if SVC_UPSAMPLING |
|---|
| 79 | if(pcSps != NULL) |
|---|
| 80 | { |
|---|
| 81 | m_conformanceWindow = pcSps->getConformanceWindow(); |
|---|
| 82 | } |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | // --> After config finished! |
|---|
| 86 | m_iCuWidth = uiMaxCUWidth; |
|---|
| 87 | m_iCuHeight = uiMaxCUHeight; |
|---|
| 88 | |
|---|
| 89 | Int numCuInWidth = m_iPicWidth / m_iCuWidth + (m_iPicWidth % m_iCuWidth != 0); |
|---|
| 90 | Int numCuInHeight = m_iPicHeight / m_iCuHeight + (m_iPicHeight % m_iCuHeight != 0); |
|---|
| 91 | |
|---|
| 92 | m_iLumaMarginX = g_uiMaxCUWidth + 16; // for 16-byte alignment |
|---|
| 93 | m_iLumaMarginY = g_uiMaxCUHeight + 16; // margin for 8-tap filter and infinite padding |
|---|
| 94 | |
|---|
| 95 | m_iChromaMarginX = m_iLumaMarginX>>1; |
|---|
| 96 | m_iChromaMarginY = m_iLumaMarginY>>1; |
|---|
| 97 | |
|---|
| 98 | m_apiPicBufY = (Pel*)xMalloc( Pel, ( m_iPicWidth + (m_iLumaMarginX <<1)) * ( m_iPicHeight + (m_iLumaMarginY <<1))); |
|---|
| 99 | m_apiPicBufU = (Pel*)xMalloc( Pel, ((m_iPicWidth >> 1) + (m_iChromaMarginX<<1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY<<1))); |
|---|
| 100 | m_apiPicBufV = (Pel*)xMalloc( Pel, ((m_iPicWidth >> 1) + (m_iChromaMarginX<<1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY<<1))); |
|---|
| 101 | |
|---|
| 102 | m_piPicOrgY = m_apiPicBufY + m_iLumaMarginY * getStride() + m_iLumaMarginX; |
|---|
| 103 | m_piPicOrgU = m_apiPicBufU + m_iChromaMarginY * getCStride() + m_iChromaMarginX; |
|---|
| 104 | m_piPicOrgV = m_apiPicBufV + m_iChromaMarginY * getCStride() + m_iChromaMarginX; |
|---|
| 105 | |
|---|
| 106 | m_bIsBorderExtended = false; |
|---|
| 107 | |
|---|
| 108 | m_cuOffsetY = new Int[numCuInWidth * numCuInHeight]; |
|---|
| 109 | m_cuOffsetC = new Int[numCuInWidth * numCuInHeight]; |
|---|
| 110 | for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++) |
|---|
| 111 | { |
|---|
| 112 | for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++) |
|---|
| 113 | { |
|---|
| 114 | m_cuOffsetY[cuRow * numCuInWidth + cuCol] = getStride() * cuRow * m_iCuHeight + cuCol * m_iCuWidth; |
|---|
| 115 | m_cuOffsetC[cuRow * numCuInWidth + cuCol] = getCStride() * cuRow * (m_iCuHeight / 2) + cuCol * (m_iCuWidth / 2); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | m_buOffsetY = new Int[(size_t)1 << (2 * uiMaxCUDepth)]; |
|---|
| 120 | m_buOffsetC = new Int[(size_t)1 << (2 * uiMaxCUDepth)]; |
|---|
| 121 | for (Int buRow = 0; buRow < (1 << uiMaxCUDepth); buRow++) |
|---|
| 122 | { |
|---|
| 123 | for (Int buCol = 0; buCol < (1 << uiMaxCUDepth); buCol++) |
|---|
| 124 | { |
|---|
| 125 | m_buOffsetY[(buRow << uiMaxCUDepth) + buCol] = getStride() * buRow * (uiMaxCUHeight >> uiMaxCUDepth) + buCol * (uiMaxCUWidth >> uiMaxCUDepth); |
|---|
| 126 | m_buOffsetC[(buRow << uiMaxCUDepth) + buCol] = getCStride() * buRow * (uiMaxCUHeight / 2 >> uiMaxCUDepth) + buCol * (uiMaxCUWidth / 2 >> uiMaxCUDepth); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | return; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | Void TComPicYuv::destroy() |
|---|
| 133 | { |
|---|
| 134 | m_piPicOrgY = NULL; |
|---|
| 135 | m_piPicOrgU = NULL; |
|---|
| 136 | m_piPicOrgV = NULL; |
|---|
| 137 | |
|---|
| 138 | if( m_apiPicBufY ){ xFree( m_apiPicBufY ); m_apiPicBufY = NULL; } |
|---|
| 139 | if( m_apiPicBufU ){ xFree( m_apiPicBufU ); m_apiPicBufU = NULL; } |
|---|
| 140 | if( m_apiPicBufV ){ xFree( m_apiPicBufV ); m_apiPicBufV = NULL; } |
|---|
| 141 | |
|---|
| 142 | delete[] m_cuOffsetY; |
|---|
| 143 | delete[] m_cuOffsetC; |
|---|
| 144 | delete[] m_buOffsetY; |
|---|
| 145 | delete[] m_buOffsetC; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | Void TComPicYuv::createLuma( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth ) |
|---|
| 149 | { |
|---|
| 150 | m_iPicWidth = iPicWidth; |
|---|
| 151 | m_iPicHeight = iPicHeight; |
|---|
| 152 | |
|---|
| 153 | // --> After config finished! |
|---|
| 154 | m_iCuWidth = uiMaxCUWidth; |
|---|
| 155 | m_iCuHeight = uiMaxCUHeight; |
|---|
| 156 | |
|---|
| 157 | Int numCuInWidth = m_iPicWidth / m_iCuWidth + (m_iPicWidth % m_iCuWidth != 0); |
|---|
| 158 | Int numCuInHeight = m_iPicHeight / m_iCuHeight + (m_iPicHeight % m_iCuHeight != 0); |
|---|
| 159 | |
|---|
| 160 | m_iLumaMarginX = g_uiMaxCUWidth + 16; // for 16-byte alignment |
|---|
| 161 | m_iLumaMarginY = g_uiMaxCUHeight + 16; // margin for 8-tap filter and infinite padding |
|---|
| 162 | |
|---|
| 163 | m_apiPicBufY = (Pel*)xMalloc( Pel, ( m_iPicWidth + (m_iLumaMarginX <<1)) * ( m_iPicHeight + (m_iLumaMarginY <<1))); |
|---|
| 164 | m_piPicOrgY = m_apiPicBufY + m_iLumaMarginY * getStride() + m_iLumaMarginX; |
|---|
| 165 | |
|---|
| 166 | m_cuOffsetY = new Int[numCuInWidth * numCuInHeight]; |
|---|
| 167 | m_cuOffsetC = NULL; |
|---|
| 168 | for (Int cuRow = 0; cuRow < numCuInHeight; cuRow++) |
|---|
| 169 | { |
|---|
| 170 | for (Int cuCol = 0; cuCol < numCuInWidth; cuCol++) |
|---|
| 171 | { |
|---|
| 172 | m_cuOffsetY[cuRow * numCuInWidth + cuCol] = getStride() * cuRow * m_iCuHeight + cuCol * m_iCuWidth; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | m_buOffsetY = new Int[(size_t)1 << (2 * uiMaxCUDepth)]; |
|---|
| 177 | m_buOffsetC = NULL; |
|---|
| 178 | for (Int buRow = 0; buRow < (1 << uiMaxCUDepth); buRow++) |
|---|
| 179 | { |
|---|
| 180 | for (Int buCol = 0; buCol < (1 << uiMaxCUDepth); buCol++) |
|---|
| 181 | { |
|---|
| 182 | m_buOffsetY[(buRow << uiMaxCUDepth) + buCol] = getStride() * buRow * (uiMaxCUHeight >> uiMaxCUDepth) + buCol * (uiMaxCUWidth >> uiMaxCUDepth); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | return; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | Void TComPicYuv::destroyLuma() |
|---|
| 189 | { |
|---|
| 190 | m_piPicOrgY = NULL; |
|---|
| 191 | |
|---|
| 192 | if( m_apiPicBufY ){ xFree( m_apiPicBufY ); m_apiPicBufY = NULL; } |
|---|
| 193 | |
|---|
| 194 | delete[] m_cuOffsetY; |
|---|
| 195 | delete[] m_buOffsetY; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | Void TComPicYuv::copyToPic (TComPicYuv* pcPicYuvDst) |
|---|
| 199 | { |
|---|
| 200 | assert( m_iPicWidth == pcPicYuvDst->getWidth() ); |
|---|
| 201 | assert( m_iPicHeight == pcPicYuvDst->getHeight() ); |
|---|
| 202 | |
|---|
| 203 | ::memcpy ( pcPicYuvDst->getBufY(), m_apiPicBufY, sizeof (Pel) * ( m_iPicWidth + (m_iLumaMarginX << 1)) * ( m_iPicHeight + (m_iLumaMarginY << 1)) ); |
|---|
| 204 | ::memcpy ( pcPicYuvDst->getBufU(), m_apiPicBufU, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) ); |
|---|
| 205 | ::memcpy ( pcPicYuvDst->getBufV(), m_apiPicBufV, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) ); |
|---|
| 206 | return; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | Void TComPicYuv::copyToPicLuma (TComPicYuv* pcPicYuvDst) |
|---|
| 210 | { |
|---|
| 211 | assert( m_iPicWidth == pcPicYuvDst->getWidth() ); |
|---|
| 212 | assert( m_iPicHeight == pcPicYuvDst->getHeight() ); |
|---|
| 213 | |
|---|
| 214 | ::memcpy ( pcPicYuvDst->getBufY(), m_apiPicBufY, sizeof (Pel) * ( m_iPicWidth + (m_iLumaMarginX << 1)) * ( m_iPicHeight + (m_iLumaMarginY << 1)) ); |
|---|
| 215 | return; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | Void TComPicYuv::copyToPicCb (TComPicYuv* pcPicYuvDst) |
|---|
| 219 | { |
|---|
| 220 | assert( m_iPicWidth == pcPicYuvDst->getWidth() ); |
|---|
| 221 | assert( m_iPicHeight == pcPicYuvDst->getHeight() ); |
|---|
| 222 | |
|---|
| 223 | ::memcpy ( pcPicYuvDst->getBufU(), m_apiPicBufU, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) ); |
|---|
| 224 | return; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | Void TComPicYuv::copyToPicCr (TComPicYuv* pcPicYuvDst) |
|---|
| 228 | { |
|---|
| 229 | assert( m_iPicWidth == pcPicYuvDst->getWidth() ); |
|---|
| 230 | assert( m_iPicHeight == pcPicYuvDst->getHeight() ); |
|---|
| 231 | |
|---|
| 232 | ::memcpy ( pcPicYuvDst->getBufV(), m_apiPicBufV, sizeof (Pel) * ((m_iPicWidth >> 1) + (m_iChromaMarginX << 1)) * ((m_iPicHeight >> 1) + (m_iChromaMarginY << 1)) ); |
|---|
| 233 | return; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | Void TComPicYuv::extendPicBorder () |
|---|
| 237 | { |
|---|
| 238 | if ( m_bIsBorderExtended ) return; |
|---|
| 239 | |
|---|
| 240 | xExtendPicCompBorder( getLumaAddr(), getStride(), getWidth(), getHeight(), m_iLumaMarginX, m_iLumaMarginY ); |
|---|
| 241 | xExtendPicCompBorder( getCbAddr() , getCStride(), getWidth() >> 1, getHeight() >> 1, m_iChromaMarginX, m_iChromaMarginY ); |
|---|
| 242 | xExtendPicCompBorder( getCrAddr() , getCStride(), getWidth() >> 1, getHeight() >> 1, m_iChromaMarginX, m_iChromaMarginY ); |
|---|
| 243 | |
|---|
| 244 | m_bIsBorderExtended = true; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | Void TComPicYuv::xExtendPicCompBorder (Pel* piTxt, Int iStride, Int iWidth, Int iHeight, Int iMarginX, Int iMarginY) |
|---|
| 248 | { |
|---|
| 249 | Int x, y; |
|---|
| 250 | Pel* pi; |
|---|
| 251 | |
|---|
| 252 | pi = piTxt; |
|---|
| 253 | for ( y = 0; y < iHeight; y++) |
|---|
| 254 | { |
|---|
| 255 | for ( x = 0; x < iMarginX; x++ ) |
|---|
| 256 | { |
|---|
| 257 | pi[ -iMarginX + x ] = pi[0]; |
|---|
| 258 | pi[ iWidth + x ] = pi[iWidth-1]; |
|---|
| 259 | } |
|---|
| 260 | pi += iStride; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | pi -= (iStride + iMarginX); |
|---|
| 264 | for ( y = 0; y < iMarginY; y++ ) |
|---|
| 265 | { |
|---|
| 266 | ::memcpy( pi + (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | pi -= ((iHeight-1) * iStride); |
|---|
| 270 | for ( y = 0; y < iMarginY; y++ ) |
|---|
| 271 | { |
|---|
| 272 | ::memcpy( pi - (y+1)*iStride, pi, sizeof(Pel)*(iWidth + (iMarginX<<1)) ); |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | Void TComPicYuv::dump (Char* pFileName, Bool bAdd) |
|---|
| 278 | { |
|---|
| 279 | FILE* pFile; |
|---|
| 280 | if (!bAdd) |
|---|
| 281 | { |
|---|
| 282 | pFile = fopen (pFileName, "wb"); |
|---|
| 283 | } |
|---|
| 284 | else |
|---|
| 285 | { |
|---|
| 286 | pFile = fopen (pFileName, "ab"); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | Int shift = g_bitDepthY-8; |
|---|
| 290 | Int offset = (shift>0)?(1<<(shift-1)):0; |
|---|
| 291 | |
|---|
| 292 | Int x, y; |
|---|
| 293 | UChar uc; |
|---|
| 294 | |
|---|
| 295 | Pel* piY = getLumaAddr(); |
|---|
| 296 | Pel* piCb = getCbAddr(); |
|---|
| 297 | Pel* piCr = getCrAddr(); |
|---|
| 298 | |
|---|
| 299 | for ( y = 0; y < m_iPicHeight; y++ ) |
|---|
| 300 | { |
|---|
| 301 | for ( x = 0; x < m_iPicWidth; x++ ) |
|---|
| 302 | { |
|---|
| 303 | uc = (UChar)Clip3<Pel>(0, 255, (piY[x]+offset)>>shift); |
|---|
| 304 | |
|---|
| 305 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
|---|
| 306 | } |
|---|
| 307 | piY += getStride(); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | shift = g_bitDepthC-8; |
|---|
| 311 | offset = (shift>0)?(1<<(shift-1)):0; |
|---|
| 312 | |
|---|
| 313 | for ( y = 0; y < m_iPicHeight >> 1; y++ ) |
|---|
| 314 | { |
|---|
| 315 | for ( x = 0; x < m_iPicWidth >> 1; x++ ) |
|---|
| 316 | { |
|---|
| 317 | uc = (UChar)Clip3<Pel>(0, 255, (piCb[x]+offset)>>shift); |
|---|
| 318 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
|---|
| 319 | } |
|---|
| 320 | piCb += getCStride(); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | for ( y = 0; y < m_iPicHeight >> 1; y++ ) |
|---|
| 324 | { |
|---|
| 325 | for ( x = 0; x < m_iPicWidth >> 1; x++ ) |
|---|
| 326 | { |
|---|
| 327 | uc = (UChar)Clip3<Pel>(0, 255, (piCr[x]+offset)>>shift); |
|---|
| 328 | fwrite( &uc, sizeof(UChar), 1, pFile ); |
|---|
| 329 | } |
|---|
| 330 | piCr += getCStride(); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | fclose(pFile); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | //! \} |
|---|