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