[677] | 1 | #include <cstdio> |
---|
| 2 | #include <cstdlib> |
---|
| 3 | #include <cstring> |
---|
| 4 | #include <cmath> |
---|
| 5 | #include <algorithm> |
---|
| 6 | |
---|
| 7 | #include "TypeDef.h" |
---|
| 8 | #include "TCom3DAsymLUT.h" |
---|
| 9 | #include "TComPicYuv.h" |
---|
| 10 | |
---|
| 11 | #if Q0048_CGS_3D_ASYMLUT |
---|
| 12 | |
---|
| 13 | const Int TCom3DAsymLUT::m_nVertexIdxOffset[4][3] = { { 0 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 1 , 1 } , { 1 , 1 , 1 } }; |
---|
| 14 | |
---|
| 15 | TCom3DAsymLUT::TCom3DAsymLUT() |
---|
| 16 | { |
---|
| 17 | m_pCuboid = NULL; |
---|
| 18 | m_nResQuanBit = 0; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | TCom3DAsymLUT::~TCom3DAsymLUT() |
---|
| 22 | { |
---|
| 23 | destroy(); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | Void TCom3DAsymLUT::create( Int nMaxOctantDepth , Int nInputBitDepth , Int nInputBitDepthC , Int nOutputBitDepth , Int nOutputBitDepthC , Int nMaxYPartNumLog2 ) |
---|
| 27 | { |
---|
| 28 | m_nMaxOctantDepth = nMaxOctantDepth; |
---|
| 29 | m_nInputBitDepthY = nInputBitDepth; |
---|
| 30 | m_nOutputBitDepthY = nOutputBitDepth; |
---|
| 31 | m_nInputBitDepthC = nInputBitDepthC; |
---|
| 32 | m_nOutputBitDepthC = nOutputBitDepthC; |
---|
| 33 | m_nDeltaBitDepthC = m_nOutputBitDepthC - m_nInputBitDepthC; |
---|
| 34 | m_nDeltaBitDepth = m_nOutputBitDepthY - m_nInputBitDepthY; |
---|
| 35 | m_nMaxYPartNumLog2 = nMaxYPartNumLog2; |
---|
| 36 | m_nMaxPartNumLog2 = 3 * m_nMaxOctantDepth + m_nMaxYPartNumLog2; |
---|
| 37 | |
---|
| 38 | xUpdatePartitioning( nMaxOctantDepth , nMaxYPartNumLog2 ); |
---|
| 39 | |
---|
| 40 | m_nYSize = 1 << ( m_nMaxOctantDepth + m_nMaxYPartNumLog2 ); |
---|
| 41 | m_nUSize = 1 << m_nMaxOctantDepth; |
---|
| 42 | m_nVSize = 1 << m_nMaxOctantDepth; |
---|
| 43 | assert( m_nYSize > 0 && m_nUSize > 0 && m_nVSize > 0 ); |
---|
| 44 | |
---|
| 45 | if( m_pCuboid != NULL ) |
---|
| 46 | destroy(); |
---|
| 47 | xAllocate3DArray( m_pCuboid , m_nYSize , m_nUSize , m_nVSize ); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | Void TCom3DAsymLUT::destroy() |
---|
| 51 | { |
---|
| 52 | xFree3DArray( m_pCuboid ); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | Void TCom3DAsymLUT::xUpdatePartitioning( Int nCurOctantDepth , Int nCurYPartNumLog2 ) |
---|
| 56 | { |
---|
| 57 | assert( nCurOctantDepth <= m_nMaxOctantDepth ); |
---|
| 58 | assert( nCurYPartNumLog2 <= m_nMaxYPartNumLog2 ); |
---|
| 59 | |
---|
| 60 | m_nCurOctantDepth = nCurOctantDepth; |
---|
| 61 | m_nCurYPartNumLog2 = nCurYPartNumLog2; |
---|
| 62 | m_nYShift2Idx = m_nInputBitDepthY - m_nCurOctantDepth - m_nCurYPartNumLog2; |
---|
| 63 | m_nUShift2Idx = m_nVShift2Idx = m_nInputBitDepthC - m_nCurOctantDepth; |
---|
| 64 | m_nMappingShift = m_nYShift2Idx + m_nUShift2Idx; |
---|
| 65 | m_nMappingOffset = 1 << ( m_nMappingShift - 1 ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | Void TCom3DAsymLUT::colorMapping( TComPicYuv * pcPic, TComPicYuv * pcPicDst ) |
---|
| 69 | { |
---|
| 70 | Int nWidth = pcPic->getWidth(); |
---|
| 71 | Int nHeight = pcPic->getHeight(); |
---|
| 72 | Int nStrideY = pcPic->getStride(); |
---|
| 73 | Int nStrideC = pcPic->getCStride(); |
---|
| 74 | Pel * pY = pcPic->getLumaAddr(); |
---|
| 75 | Pel * pU = pcPic->getCbAddr(); |
---|
| 76 | Pel * pV = pcPic->getCrAddr(); |
---|
| 77 | |
---|
| 78 | Int nDstStrideY = pcPicDst->getStride(); |
---|
| 79 | Int nDstStrideC = pcPicDst->getCStride(); |
---|
| 80 | Pel * pYDst = pcPicDst->getLumaAddr(); |
---|
| 81 | Pel * pUDst = pcPicDst->getCbAddr(); |
---|
| 82 | Pel * pVDst = pcPicDst->getCrAddr(); |
---|
| 83 | |
---|
| 84 | Pel *pUPrev = pU; |
---|
| 85 | Pel *pVPrev = pV; |
---|
| 86 | Pel *pUNext = pU+nStrideC; |
---|
| 87 | Pel *pVNext = pV+nStrideC; |
---|
| 88 | |
---|
| 89 | // alignment padding |
---|
| 90 | pU += (nWidth>>1); |
---|
| 91 | pV += (nWidth>>1); |
---|
| 92 | for( Int y = 0 ; y < (nHeight>>1) ; y ++ ) |
---|
| 93 | { |
---|
| 94 | *pU = pU[-1]; |
---|
| 95 | *pV = pV[-1]; |
---|
| 96 | pU += nStrideC; |
---|
| 97 | pV += nStrideC; |
---|
| 98 | } |
---|
| 99 | memcpy(pU-(nWidth>>1), pU-(nWidth>>1)-nStrideC, ((nWidth>>1)+1)*sizeof(Pel)); |
---|
| 100 | memcpy(pV-(nWidth>>1), pV-(nWidth>>1)-nStrideC, ((nWidth>>1)+1)*sizeof(Pel)); |
---|
| 101 | pU = pcPic->getCbAddr(); |
---|
| 102 | pV = pcPic->getCrAddr(); |
---|
| 103 | |
---|
| 104 | Pel iMaxValY = (1<<getOutputBitDepthY())-1; |
---|
| 105 | Pel iMaxValC = (1<<getOutputBitDepthC())-1; |
---|
| 106 | for( Int y = 0 ; y < nHeight ; y += 2 ) |
---|
| 107 | { |
---|
| 108 | for( Int xY = 0 , xC = 0 ; xY < nWidth ; xY += 2 , xC++ ) |
---|
| 109 | { |
---|
| 110 | Pel srcY00 = pY[xY]; |
---|
| 111 | Pel srcY01 = pY[xY+1]; |
---|
| 112 | Pel srcY10 = pY[xY+nStrideY]; |
---|
| 113 | Pel srcY11 = pY[xY+nStrideY+1]; |
---|
| 114 | Pel srcYaver; |
---|
| 115 | Pel srcU = pU[xC]; |
---|
| 116 | Pel srcV = pV[xC]; |
---|
| 117 | Pel dstY00, dstY01, dstY10, dstY11; |
---|
| 118 | |
---|
| 119 | // alignment |
---|
| 120 | srcYaver = (srcY00 + srcY10 + 1 ) >> 1; |
---|
| 121 | Pel srcUP0 = pUPrev[xC]; |
---|
| 122 | Pel srcVP0 = pVPrev[xC]; |
---|
| 123 | Pel tmpU = (srcUP0 + srcU + (srcU<<1) + 2 ) >> 2; |
---|
| 124 | Pel tmpV = (srcVP0 + srcV + (srcV<<1) + 2 ) >> 2; |
---|
| 125 | dstY00 = xMapY( srcY00 , tmpU , tmpV ); |
---|
| 126 | Pel a = pU[xC+1] + srcU; |
---|
| 127 | tmpU = ((a<<1) + a + srcUP0 + pUPrev[xC+1] + 4 ) >> 3; |
---|
| 128 | Pel b = pV[xC+1] + srcV; |
---|
| 129 | tmpV = ((b<<1) + b + srcVP0 + pVPrev[xC+1] + 4 ) >> 3; |
---|
| 130 | dstY01 = xMapY( srcY01 , tmpU , tmpV ); |
---|
| 131 | |
---|
| 132 | srcUP0 = pUNext[xC]; |
---|
| 133 | srcVP0 = pVNext[xC]; |
---|
| 134 | tmpU = (srcUP0 + srcU + (srcU<<1) + 2 ) >> 2; |
---|
| 135 | tmpV = (srcVP0 + srcV + (srcV<<1) + 2 ) >> 2; |
---|
| 136 | dstY10 = xMapY( srcY10 , tmpU , tmpV ); |
---|
| 137 | tmpU = ((a<<1) + a + srcUP0 + pUNext[xC+1] + 4 ) >> 3; |
---|
| 138 | tmpV = ((b<<1) + b + srcVP0 + pVNext[xC+1] + 4 ) >> 3; |
---|
| 139 | dstY11 = xMapY( srcY11 , tmpU , tmpV ); |
---|
| 140 | |
---|
| 141 | SYUVP dstUV = xMapUV( srcYaver , srcU , srcV ); |
---|
| 142 | pYDst[xY] = Clip3((Pel)0, iMaxValY, dstY00 ); |
---|
| 143 | pYDst[xY+1] = Clip3((Pel)0, iMaxValY, dstY01 ); |
---|
| 144 | pYDst[xY+nDstStrideY] = Clip3((Pel)0, iMaxValY, dstY10 ); |
---|
| 145 | pYDst[xY+nDstStrideY+1] = Clip3((Pel)0, iMaxValY, dstY11 ); |
---|
| 146 | pUDst[xC] = Clip3((Pel)0, iMaxValC, dstUV.U ); |
---|
| 147 | pVDst[xC] = Clip3((Pel)0, iMaxValC, dstUV.V ); |
---|
| 148 | } |
---|
| 149 | pY += nStrideY + nStrideY; |
---|
| 150 | |
---|
| 151 | // alignment |
---|
| 152 | pUPrev = pU; |
---|
| 153 | pVPrev = pV; |
---|
| 154 | pU = pUNext; |
---|
| 155 | pV = pVNext; |
---|
| 156 | pUNext += nStrideC; |
---|
| 157 | pVNext += nStrideC; |
---|
| 158 | |
---|
| 159 | pYDst += nDstStrideY + nDstStrideY; |
---|
| 160 | pUDst += nDstStrideC; |
---|
| 161 | pVDst += nDstStrideC; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | SYUVP TCom3DAsymLUT::xGetCuboidVertexPredA( Int yIdx , Int uIdx , Int vIdx , Int nVertexIdx ) |
---|
| 166 | { |
---|
| 167 | assert( nVertexIdx < 4 ); |
---|
| 168 | |
---|
| 169 | SYUVP sPred; |
---|
| 170 | sPred.Y = ( yIdx + m_nVertexIdxOffset[nVertexIdx][0] ) << ( m_nYShift2Idx + m_nDeltaBitDepth ); |
---|
| 171 | sPred.U = ( uIdx + m_nVertexIdxOffset[nVertexIdx][1] ) << ( m_nUShift2Idx + m_nDeltaBitDepthC ); |
---|
| 172 | sPred.V = ( vIdx + m_nVertexIdxOffset[nVertexIdx][2] ) << ( m_nVShift2Idx + m_nDeltaBitDepthC ); |
---|
| 173 | return( sPred ); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | SYUVP TCom3DAsymLUT::xGetCuboidVertexPredAll( Int yIdx , Int uIdx , Int vIdx , Int nVertexIdx , SCuboid *** pCurCuboid ) |
---|
| 177 | { |
---|
| 178 | SCuboid*** pCuboid = pCurCuboid ? pCurCuboid : m_pCuboid ; |
---|
| 179 | |
---|
| 180 | // PredA |
---|
| 181 | SYUVP sPredA = xGetCuboidVertexPredA( yIdx , uIdx , vIdx , nVertexIdx ); |
---|
| 182 | |
---|
| 183 | // PredB |
---|
| 184 | SYUVP sPredB; |
---|
| 185 | memset( &sPredB , 0 , sizeof( sPredB ) ); |
---|
| 186 | if( yIdx > 0 ) |
---|
| 187 | { |
---|
| 188 | SYUVP & recNeighborP = pCuboid[yIdx-1][uIdx][vIdx].P[nVertexIdx]; |
---|
| 189 | SYUVP sPredNeighbor = xGetCuboidVertexPredA( yIdx - 1 , uIdx , vIdx , nVertexIdx ); |
---|
| 190 | sPredB.Y += recNeighborP.Y - sPredNeighbor.Y ; |
---|
| 191 | sPredB.U += recNeighborP.U - sPredNeighbor.U ; |
---|
| 192 | sPredB.V += recNeighborP.V - sPredNeighbor.V ; |
---|
| 193 | |
---|
| 194 | Pel min = - ( 1 << ( getOutputBitDepthY() - 2 ) ); |
---|
[678] | 195 | Pel max = - min; |
---|
[677] | 196 | sPredB.Y = Clip3( min , max , sPredB.Y ); |
---|
| 197 | min = - ( 1 << ( getOutputBitDepthC() - 2 ) ); |
---|
[678] | 198 | max = - min; |
---|
[677] | 199 | sPredB.U = Clip3( min , max , sPredB.U ); |
---|
| 200 | sPredB.V = Clip3( min , max , sPredB.V ); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | SYUVP sPred; |
---|
| 204 | sPred.Y = sPredA.Y + sPredB.Y; |
---|
| 205 | sPred.U = sPredA.U + sPredB.U; |
---|
| 206 | sPred.V = sPredA.V + sPredB.V; |
---|
| 207 | |
---|
| 208 | return sPred ; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | SYUVP TCom3DAsymLUT::getCuboidVertexResTree( Int yIdx , Int uIdx , Int vIdx , Int nVertexIdx ) |
---|
| 212 | { |
---|
| 213 | const SYUVP & rYUVP = m_pCuboid[yIdx][uIdx][vIdx].P[nVertexIdx]; |
---|
| 214 | SYUVP sPred = xGetCuboidVertexPredAll( yIdx , uIdx , vIdx , nVertexIdx ); |
---|
| 215 | |
---|
| 216 | SYUVP sResidue; |
---|
| 217 | sResidue.Y = ( rYUVP.Y - sPred.Y ) >> m_nResQuanBit; |
---|
| 218 | sResidue.U = ( rYUVP.U - sPred.U ) >> m_nResQuanBit; |
---|
| 219 | sResidue.V = ( rYUVP.V - sPred.V ) >> m_nResQuanBit; |
---|
| 220 | return( sResidue ); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | Void TCom3DAsymLUT::setCuboidVertexResTree( Int yIdx , Int uIdx , Int vIdx , Int nVertexIdx , Int deltaY , Int deltaU , Int deltaV ) |
---|
| 224 | { |
---|
| 225 | SYUVP & rYUVP = m_pCuboid[yIdx][uIdx][vIdx].P[nVertexIdx]; |
---|
| 226 | SYUVP sPred = xGetCuboidVertexPredAll( yIdx , uIdx , vIdx , nVertexIdx ); |
---|
| 227 | |
---|
| 228 | rYUVP.Y = sPred.Y + ( deltaY << m_nResQuanBit ); |
---|
| 229 | rYUVP.U = sPred.U + ( deltaU << m_nResQuanBit ); |
---|
| 230 | rYUVP.V = sPred.V + ( deltaV << m_nResQuanBit ); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | Pel TCom3DAsymLUT::xMapY( Pel y , Pel u , Pel v ) |
---|
| 234 | { |
---|
| 235 | const SCuboid & rCuboid = m_pCuboid[y>>m_nYShift2Idx][u>>m_nUShift2Idx][v>>m_nVShift2Idx]; |
---|
| 236 | Pel dstY = rCuboid.P[0].Y; |
---|
| 237 | Int deltaY = y - ( y >> m_nYShift2Idx << m_nYShift2Idx ); |
---|
| 238 | Int deltaU = u - ( u >> m_nUShift2Idx << m_nUShift2Idx ); |
---|
| 239 | Int deltaV = v - ( v >> m_nVShift2Idx << m_nVShift2Idx ); |
---|
| 240 | dstY += ( Pel )( ( ( ( deltaY * ( rCuboid.P[3].Y - rCuboid.P[2].Y ) ) << m_nUShift2Idx ) |
---|
| 241 | + ( ( deltaU * ( rCuboid.P[1].Y - rCuboid.P[0].Y ) ) << m_nYShift2Idx ) |
---|
| 242 | + ( ( deltaV * ( rCuboid.P[2].Y - rCuboid.P[1].Y ) ) << m_nYShift2Idx ) |
---|
| 243 | + m_nMappingOffset ) >> m_nMappingShift ); |
---|
| 244 | return( dstY ); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | SYUVP TCom3DAsymLUT::xMapUV( Pel y , Pel u , Pel v ) |
---|
| 248 | { |
---|
| 249 | const SCuboid & rCuboid = m_pCuboid[y>>m_nYShift2Idx][u>>m_nUShift2Idx][v>>m_nVShift2Idx]; |
---|
| 250 | SYUVP dst = rCuboid.P[0]; |
---|
| 251 | Int deltaY = y - ( y >> m_nYShift2Idx << m_nYShift2Idx ); |
---|
| 252 | Int deltaU = u - ( u >> m_nUShift2Idx << m_nUShift2Idx ); |
---|
| 253 | Int deltaV = v - ( v >> m_nVShift2Idx << m_nVShift2Idx ); |
---|
| 254 | dst.U += ( Pel )( ( ( ( deltaY * ( rCuboid.P[3].U - rCuboid.P[2].U ) ) << m_nUShift2Idx ) |
---|
| 255 | + ( ( deltaU * ( rCuboid.P[1].U - rCuboid.P[0].U ) ) << m_nYShift2Idx ) |
---|
| 256 | + ( ( deltaV * ( rCuboid.P[2].U - rCuboid.P[1].U ) ) << m_nYShift2Idx ) |
---|
| 257 | + m_nMappingOffset ) >> m_nMappingShift ); |
---|
| 258 | dst.V += ( Pel )( ( ( ( deltaY * ( rCuboid.P[3].V - rCuboid.P[2].V ) ) << m_nUShift2Idx ) |
---|
| 259 | + ( ( deltaU * ( rCuboid.P[1].V - rCuboid.P[0].V ) ) << m_nYShift2Idx ) |
---|
| 260 | + ( ( deltaV * ( rCuboid.P[2].V - rCuboid.P[1].V ) ) << m_nYShift2Idx ) |
---|
| 261 | + m_nMappingOffset ) >> m_nMappingShift ); |
---|
| 262 | return( dst ); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | Void TCom3DAsymLUT::xSaveCuboids( SCuboid *** pSrcCuboid ) |
---|
| 266 | { |
---|
| 267 | memcpy( m_pCuboid[0][0] , pSrcCuboid[0][0] , sizeof( SCuboid ) * m_nYSize * m_nUSize * m_nVSize ); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | Void TCom3DAsymLUT::copy3DAsymLUT( TCom3DAsymLUT * pSrc ) |
---|
| 271 | { |
---|
| 272 | assert( pSrc->getMaxOctantDepth() == getMaxOctantDepth() && pSrc->getMaxYPartNumLog2() == getMaxYPartNumLog2() ); |
---|
| 273 | xUpdatePartitioning( pSrc->getCurOctantDepth() , pSrc->getCurYPartNumLog2() ); |
---|
| 274 | setResQuantBit( pSrc->getResQuantBit() ); |
---|
| 275 | xSaveCuboids( pSrc->m_pCuboid ); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | #endif |
---|
| 279 | |
---|