50
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
//
|
|
15 |
|
|
16 |
#include <e32base.h>
|
|
17 |
|
|
18 |
#include "JpegTypes.h"
|
|
19 |
|
|
20 |
const TInt KShift = 14;
|
|
21 |
|
|
22 |
// TQTable
|
|
23 |
TInt TQTable::Set(const TUint8* aData,const TBool aSixteenBitPrecision)
|
|
24 |
{
|
|
25 |
iSixteenBitPrecision = aSixteenBitPrecision;
|
|
26 |
|
|
27 |
if (iSixteenBitPrecision)
|
|
28 |
{
|
|
29 |
TUint16* sixteenBitPtr = reinterpret_cast<TUint16*>(iValues);
|
|
30 |
const TUint16* sixteenBitPtrLimit = sixteenBitPtr + KJpgQTableEntries;
|
|
31 |
while (sixteenBitPtr < sixteenBitPtrLimit)
|
|
32 |
{
|
|
33 |
*sixteenBitPtr++ = ReadBigEndianUint16(aData);
|
|
34 |
}
|
|
35 |
|
|
36 |
return KJpgQTableEntries * 2;
|
|
37 |
}
|
|
38 |
|
|
39 |
Mem::Copy(iValues, aData, KJpgQTableEntries);
|
|
40 |
Mem::FillZ(iValues + KJpgQTableEntries,KJpgQTableEntries);
|
|
41 |
CalcQualityFactor();
|
|
42 |
|
|
43 |
return KJpgQTableEntries;
|
|
44 |
}
|
|
45 |
|
|
46 |
TInt TQTable::Get(TUint8* aData) const
|
|
47 |
{
|
|
48 |
const TInt valuesSize = KJpgQTableEntries * (iSixteenBitPrecision ? sizeof(TUint16) : sizeof(TUint8));
|
|
49 |
|
|
50 |
if (iSixteenBitPrecision)
|
|
51 |
{
|
|
52 |
TUint16* sixteenBitPtr = (TUint16*)iValues;
|
|
53 |
const TUint16* sixteenBitPtrLimit = sixteenBitPtr + KJpgQTableEntries;
|
|
54 |
while (sixteenBitPtr < sixteenBitPtrLimit)
|
|
55 |
{
|
|
56 |
WriteBigEndianUint16(aData,*sixteenBitPtr++);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
else
|
|
60 |
{
|
|
61 |
Mem::Copy(aData,iValues,valuesSize);
|
|
62 |
}
|
|
63 |
|
|
64 |
return valuesSize;
|
|
65 |
}
|
|
66 |
|
|
67 |
void TQTable::SetQualityFactor(TInt aQualityFactor)
|
|
68 |
{
|
|
69 |
TInt scaleFactor = 0;
|
|
70 |
if (aQualityFactor < 50)
|
|
71 |
{
|
|
72 |
aQualityFactor = Max(aQualityFactor,1);
|
|
73 |
scaleFactor = 5000 / aQualityFactor;
|
|
74 |
}
|
|
75 |
else
|
|
76 |
{
|
|
77 |
aQualityFactor = Min(aQualityFactor,100);
|
|
78 |
scaleFactor = 200 - (aQualityFactor * 2);
|
|
79 |
}
|
|
80 |
|
|
81 |
if (iSixteenBitPrecision)
|
|
82 |
{
|
|
83 |
TUint16* qPtr = (TUint16*)iValues;
|
|
84 |
const TUint16* qPtrLimit = qPtr + KJpgDCTBlockSize;
|
|
85 |
while (qPtr < qPtrLimit)
|
|
86 |
{
|
|
87 |
TInt qValue = (qPtr[0] * scaleFactor + 50) / 100;
|
|
88 |
if (qValue <= 0)
|
|
89 |
qValue = 1;
|
|
90 |
if (qValue > KMaxTInt16)
|
|
91 |
qValue = KMaxTInt16;
|
|
92 |
*qPtr++ = TUint16(qValue);
|
|
93 |
}
|
|
94 |
}
|
|
95 |
else
|
|
96 |
{
|
|
97 |
TUint8* qPtr = iValues;
|
|
98 |
const TUint8* qPtrLimit = qPtr + KJpgDCTBlockSize;
|
|
99 |
|
|
100 |
TInt* qPtrM = iValues2;
|
|
101 |
|
|
102 |
while (qPtr < qPtrLimit)
|
|
103 |
{
|
|
104 |
TInt qValue = (qPtr[0] * scaleFactor + 50) / 100;
|
|
105 |
if (qValue <= 0)
|
|
106 |
{
|
|
107 |
qValue = 1;
|
|
108 |
}
|
|
109 |
if (qValue > TInt(KMaxTUint8) )
|
|
110 |
{
|
|
111 |
qValue = KMaxTUint8;
|
|
112 |
}
|
|
113 |
*qPtrM++ = TUint16( (1 << KShift) / qValue );
|
|
114 |
*qPtr++ = TUint8(qValue);
|
|
115 |
}
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
void TQTable::CalcQualityFactor() // Assumes the default tables have been used
|
|
120 |
{
|
|
121 |
TInt scaleFactor = 0;
|
|
122 |
|
|
123 |
if (iSixteenBitPrecision)
|
|
124 |
{
|
|
125 |
TUint16* qPtr = (TUint16*)iValues;
|
|
126 |
scaleFactor = qPtr[KJpgQTableEntries - 1];
|
|
127 |
}
|
|
128 |
else
|
|
129 |
{
|
|
130 |
scaleFactor = iValues[KJpgQTableEntries - 1];
|
|
131 |
}
|
|
132 |
|
|
133 |
if (scaleFactor > 100)
|
|
134 |
{
|
|
135 |
iQualityFactor = 5000 / scaleFactor;
|
|
136 |
}
|
|
137 |
else
|
|
138 |
{
|
|
139 |
iQualityFactor = 100 - (scaleFactor / 2);
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
//
|
|
144 |
// this section contains many performance-critical code, so use ARM instruction set for it
|
|
145 |
//
|
|
146 |
#ifdef __ARMCC__
|
|
147 |
#pragma push
|
|
148 |
#pragma arm
|
|
149 |
#pragma O3
|
|
150 |
#pragma Otime
|
|
151 |
#endif
|
|
152 |
|
|
153 |
template <class T>
|
|
154 |
inline
|
|
155 |
void TQTable::DoQuantize(TDataUnit& aDestination, const TDataUnit& aSource) const
|
|
156 |
{
|
|
157 |
const TInt16* srcPtr = aSource.iCoeff;
|
|
158 |
TInt16* dstPtr = aDestination.iCoeff;
|
|
159 |
|
|
160 |
const TUint8* zigZagPtr = KZigZagSequence.iZigZag;
|
|
161 |
const TUint8* zigZagPtrLimit= zigZagPtr + KJpgDCTBlockSize;
|
|
162 |
|
|
163 |
register const T* qPtr = reinterpret_cast<const T*>(iValues);
|
|
164 |
|
|
165 |
do
|
|
166 |
{
|
|
167 |
register TInt qValue = *qPtr++;
|
|
168 |
register TInt dstValue = srcPtr[*zigZagPtr++];
|
|
169 |
if (dstValue > 0)
|
|
170 |
{
|
|
171 |
dstValue += (qValue >> 1);
|
|
172 |
if (dstValue >= qValue)
|
|
173 |
{
|
|
174 |
dstValue /= qValue;
|
|
175 |
}
|
|
176 |
else
|
|
177 |
{
|
|
178 |
dstValue = 0;
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
else
|
|
183 |
{
|
|
184 |
dstValue -= (qValue >> 1);
|
|
185 |
dstValue = -dstValue;
|
|
186 |
if (dstValue >= qValue)
|
|
187 |
{
|
|
188 |
dstValue /= qValue;
|
|
189 |
dstValue = -dstValue;
|
|
190 |
}
|
|
191 |
else
|
|
192 |
{
|
|
193 |
dstValue = 0;
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
*dstPtr++ = TInt16(dstValue);
|
|
198 |
|
|
199 |
} while (zigZagPtr < zigZagPtrLimit);
|
|
200 |
}
|
|
201 |
|
|
202 |
void TQTable::Quantize(TDataUnit& aDestination,const TDataUnit& aSource, TBool aHighSpeedMode) const
|
|
203 |
{
|
|
204 |
if (iSixteenBitPrecision)
|
|
205 |
{
|
|
206 |
DoQuantize<TUint16>(aDestination, aSource);
|
|
207 |
}
|
|
208 |
else
|
|
209 |
{
|
|
210 |
if (!aHighSpeedMode)
|
|
211 |
{
|
|
212 |
DoQuantize<TUint8>(aDestination, aSource);
|
|
213 |
}
|
|
214 |
else
|
|
215 |
{
|
|
216 |
const TInt16* srcPtr = aSource.iCoeff;
|
|
217 |
TUint64* dstPtr = reinterpret_cast<TUint64*>(aDestination.iCoeff);
|
|
218 |
|
|
219 |
const TUint8* zigZagPtr = KZigZagSequence.iZigZag;
|
|
220 |
const TUint8* zigZagPtrLimit= zigZagPtr + KJpgDCTBlockSize;
|
|
221 |
|
|
222 |
const TUint64* qPtr = &iValues2Aligment;
|
|
223 |
// we do 2 values per loop iteration
|
|
224 |
do
|
|
225 |
{
|
|
226 |
register TAligned64Value dstValue;
|
|
227 |
register TAligned64Value qValue;
|
|
228 |
register TAligned64Value zValue;
|
|
229 |
dstValue.iAligment = 0; //to remove possible RVCT warning
|
|
230 |
zValue.iAligment = *reinterpret_cast<const TUint64*>(zigZagPtr);
|
|
231 |
|
|
232 |
qValue.iAligment = *qPtr++;
|
|
233 |
|
|
234 |
dstValue.iWord[0] = TUint16(DESCALE(srcPtr[ zValue.iByte[0] ] * qValue.iInt[0], KShift));
|
|
235 |
dstValue.iWord[1] = TUint16(DESCALE(srcPtr[ zValue.iByte[1] ] * qValue.iInt[1], KShift));
|
|
236 |
|
|
237 |
qValue.iAligment = *qPtr++;
|
|
238 |
|
|
239 |
dstValue.iWord[2] = TUint16(DESCALE(srcPtr[ zValue.iByte[2] ] * qValue.iInt[0], KShift));
|
|
240 |
dstValue.iWord[3] = TUint16(DESCALE(srcPtr[ zValue.iByte[3] ] * qValue.iInt[1], KShift));
|
|
241 |
|
|
242 |
*dstPtr++ = dstValue.iAligment;
|
|
243 |
|
|
244 |
qValue.iAligment = *qPtr++;
|
|
245 |
|
|
246 |
dstValue.iWord[0] = TUint16(DESCALE(srcPtr[ zValue.iByte[4] ] * qValue.iInt[0], KShift));
|
|
247 |
dstValue.iWord[1] = TUint16(DESCALE(srcPtr[ zValue.iByte[5] ] * qValue.iInt[1], KShift));
|
|
248 |
|
|
249 |
qValue.iAligment = *qPtr++;
|
|
250 |
|
|
251 |
dstValue.iWord[2] = TUint16(DESCALE(srcPtr[ zValue.iByte[6] ] * qValue.iInt[0], KShift));
|
|
252 |
dstValue.iWord[3] = TUint16(DESCALE(srcPtr[ zValue.iByte[7] ] * qValue.iInt[1], KShift));
|
|
253 |
|
|
254 |
*dstPtr++ = dstValue.iAligment;
|
|
255 |
|
|
256 |
zigZagPtr += 8;
|
|
257 |
} while (zigZagPtr < zigZagPtrLimit);
|
|
258 |
}
|
|
259 |
}
|
|
260 |
}
|
|
261 |
|
|
262 |
void TQTable::FastHalfDeQuantize(TDataUnit& aDestination,const TDataUnit& aSource,TInt aNumNonZeroValues) const
|
|
263 |
{
|
|
264 |
DoDeQuantize(aDestination,aSource,Min(KJpgDCTBlockSize/2, aNumNonZeroValues), KJpgDCTBlockSize/2);
|
|
265 |
}
|
|
266 |
|
|
267 |
|
|
268 |
void TQTable::DeQuantize(TDataUnit& aDestination,const TDataUnit& aSource,TInt aNumNonZeroValues) const
|
|
269 |
{
|
|
270 |
DoDeQuantize(aDestination,aSource, aNumNonZeroValues, KJpgDCTBlockSize);
|
|
271 |
}
|
|
272 |
|
|
273 |
void TQTable::DoDeQuantize(TDataUnit& aDestination,const TDataUnit& aSource,
|
|
274 |
TInt aNumNonZeroValues, TInt aBlockLen) const
|
|
275 |
{
|
|
276 |
FillCompZ(aDestination, aBlockLen );
|
|
277 |
|
|
278 |
const TInt16* srcPtr = aSource.iCoeff;
|
|
279 |
const TInt16* const srcPtrLimit = srcPtr + aNumNonZeroValues;
|
|
280 |
TInt16* dstPtr = aDestination.iCoeff;
|
|
281 |
const TUint8* zigZagPtr = KZigZagSequence.iZigZag;
|
|
282 |
|
|
283 |
if (iSixteenBitPrecision)
|
|
284 |
{
|
|
285 |
const TUint16* qPtr = (const TUint16*)iValues;
|
|
286 |
while (srcPtr < srcPtrLimit)
|
|
287 |
{
|
|
288 |
TInt16 v=(*srcPtr++);
|
|
289 |
if (v)
|
|
290 |
{
|
|
291 |
dstPtr[*zigZagPtr] = TInt16(v * (*qPtr));
|
|
292 |
}
|
|
293 |
++qPtr;
|
|
294 |
++zigZagPtr;
|
|
295 |
}
|
|
296 |
}
|
|
297 |
else
|
|
298 |
{
|
|
299 |
const TUint8* qPtr = iValues;
|
|
300 |
while (srcPtr < srcPtrLimit)
|
|
301 |
{
|
|
302 |
TInt16 v=*srcPtr++;
|
|
303 |
if (v)
|
|
304 |
{
|
|
305 |
dstPtr[*zigZagPtr] = TInt16(v* (*qPtr));
|
|
306 |
}
|
|
307 |
++zigZagPtr;
|
|
308 |
++qPtr;
|
|
309 |
}
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
void TQTable::FastQuarterDeQuantize(TDataUnit& aDestination,const TDataUnit& aSource,TInt /*aNumNonZeroValues*/) const
|
|
314 |
{
|
|
315 |
// we're interested only in result at indeces 0,1 and 8,9 for fast 1/4 iDCT,
|
|
316 |
// so use excerpt of the ZigZag table:
|
|
317 |
// 0, 1, 8, 16, 9
|
|
318 |
//
|
|
319 |
const TInt16* srcPtr = aSource.iCoeff;
|
|
320 |
|
|
321 |
TInt16* dstPtr = aDestination.iCoeff;
|
|
322 |
|
|
323 |
if (iSixteenBitPrecision)
|
|
324 |
{
|
|
325 |
const TUint16* qPtr = (const TUint16*)iValues;
|
|
326 |
|
|
327 |
dstPtr[0] = TInt16(srcPtr[0] * qPtr[0]);
|
|
328 |
dstPtr[1] = TInt16(srcPtr[1] * qPtr[1]);
|
|
329 |
dstPtr[8] = TInt16(srcPtr[2] * qPtr[2]);
|
|
330 |
dstPtr[9] = TInt16(srcPtr[4] * qPtr[4]);
|
|
331 |
|
|
332 |
}
|
|
333 |
else
|
|
334 |
{
|
|
335 |
const TUint8* qPtr = iValues;
|
|
336 |
dstPtr[0] = TInt16(srcPtr[0] * qPtr[0]);
|
|
337 |
dstPtr[1] = TInt16(srcPtr[1] * qPtr[1]);
|
|
338 |
dstPtr[8] = TInt16(srcPtr[2] * qPtr[2]);
|
|
339 |
dstPtr[9] = TInt16(srcPtr[4] * qPtr[4]);
|
|
340 |
}
|
|
341 |
}
|
|
342 |
|
|
343 |
#ifdef __ARMCC__
|
|
344 |
#pragma pop
|
|
345 |
#endif
|
|
346 |
|