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