1 | // jpgd.cpp - C++ class for JPEG decompression. Written by Richard Geldreich <richgel99@gmail.com> between 1994-2020. |
2 | // Supports progressive and baseline sequential JPEG image files, and the most common chroma subsampling factors: Y, H1V1, H2V1, H1V2, and H2V2. |
3 | // Supports box and linear chroma upsampling. |
4 | // |
5 | // Released under two licenses. You are free to choose which license you want: |
6 | // License 1: |
7 | // Public Domain |
8 | // |
9 | // License 2: |
10 | // Licensed under the Apache License, Version 2.0 (the "License"); |
11 | // you may not use this file except in compliance with the License. |
12 | // You may obtain a copy of the License at |
13 | // |
14 | // http://www.apache.org/licenses/LICENSE-2.0 |
15 | // |
16 | // Unless required by applicable law or agreed to in writing, software |
17 | // distributed under the License is distributed on an "AS IS" BASIS, |
18 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 | // See the License for the specific language governing permissions and |
20 | // limitations under the License. |
21 | // |
22 | // Alex Evans: Linear memory allocator (taken from jpge.h). |
23 | // v1.04, May. 19, 2012: Code tweaks to fix VS2008 static code analysis warnings |
24 | // v2.00, March 20, 2020: Fuzzed with zzuf and afl. Fixed several issues, converted most assert()'s to run-time checks. Added chroma upsampling. Removed freq. domain upsampling. gcc/clang warnings. |
25 | // |
26 | |
27 | #include "jpgd.h" |
28 | #include <string.h> |
29 | #include <algorithm> |
30 | #include <assert.h> |
31 | |
32 | #ifdef _MSC_VER |
33 | #pragma warning (disable : 4611) // warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable |
34 | #endif |
35 | |
36 | #define JPGD_TRUE (1) |
37 | #define JPGD_FALSE (0) |
38 | |
39 | #define JPGD_MAX(a,b) (((a)>(b)) ? (a) : (b)) |
40 | #define JPGD_MIN(a,b) (((a)<(b)) ? (a) : (b)) |
41 | |
42 | namespace jpgd { |
43 | |
44 | static inline void* jpgd_malloc(size_t nSize) { return malloc(nSize); } |
45 | static inline void jpgd_free(void* p) { free(p); } |
46 | |
47 | // DCT coefficients are stored in this sequence. |
48 | static int g_ZAG[64] = { 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63 }; |
49 | |
50 | enum JPEG_MARKER |
51 | { |
52 | M_SOF0 = 0xC0, M_SOF1 = 0xC1, M_SOF2 = 0xC2, M_SOF3 = 0xC3, M_SOF5 = 0xC5, M_SOF6 = 0xC6, M_SOF7 = 0xC7, M_JPG = 0xC8, |
53 | M_SOF9 = 0xC9, M_SOF10 = 0xCA, M_SOF11 = 0xCB, M_SOF13 = 0xCD, M_SOF14 = 0xCE, M_SOF15 = 0xCF, M_DHT = 0xC4, M_DAC = 0xCC, |
54 | M_RST0 = 0xD0, M_RST1 = 0xD1, M_RST2 = 0xD2, M_RST3 = 0xD3, M_RST4 = 0xD4, M_RST5 = 0xD5, M_RST6 = 0xD6, M_RST7 = 0xD7, |
55 | M_SOI = 0xD8, M_EOI = 0xD9, M_SOS = 0xDA, M_DQT = 0xDB, M_DNL = 0xDC, M_DRI = 0xDD, M_DHP = 0xDE, M_EXP = 0xDF, |
56 | M_APP0 = 0xE0, M_APP15 = 0xEF, M_JPG0 = 0xF0, M_JPG13 = 0xFD, M_COM = 0xFE, M_TEM = 0x01, M_ERROR = 0x100, RST0 = 0xD0 |
57 | }; |
58 | |
59 | enum JPEG_SUBSAMPLING { JPGD_GRAYSCALE = 0, JPGD_YH1V1, JPGD_YH2V1, JPGD_YH1V2, JPGD_YH2V2 }; |
60 | |
61 | #define CONST_BITS 13 |
62 | #define PASS1_BITS 2 |
63 | #define SCALEDONE ((int32)1) |
64 | |
65 | #define FIX_0_298631336 ((int32)2446) /* FIX(0.298631336) */ |
66 | #define FIX_0_390180644 ((int32)3196) /* FIX(0.390180644) */ |
67 | #define FIX_0_541196100 ((int32)4433) /* FIX(0.541196100) */ |
68 | #define FIX_0_765366865 ((int32)6270) /* FIX(0.765366865) */ |
69 | #define FIX_0_899976223 ((int32)7373) /* FIX(0.899976223) */ |
70 | #define FIX_1_175875602 ((int32)9633) /* FIX(1.175875602) */ |
71 | #define FIX_1_501321110 ((int32)12299) /* FIX(1.501321110) */ |
72 | #define FIX_1_847759065 ((int32)15137) /* FIX(1.847759065) */ |
73 | #define FIX_1_961570560 ((int32)16069) /* FIX(1.961570560) */ |
74 | #define FIX_2_053119869 ((int32)16819) /* FIX(2.053119869) */ |
75 | #define FIX_2_562915447 ((int32)20995) /* FIX(2.562915447) */ |
76 | #define FIX_3_072711026 ((int32)25172) /* FIX(3.072711026) */ |
77 | |
78 | #define DESCALE(x,n) (((x) + (SCALEDONE << ((n)-1))) >> (n)) |
79 | #define DESCALE_ZEROSHIFT(x,n) (((x) + (128 << (n)) + (SCALEDONE << ((n)-1))) >> (n)) |
80 | |
81 | #define MULTIPLY(var, cnst) ((var) * (cnst)) |
82 | |
83 | #define CLAMP(i) ((static_cast<uint>(i) > 255) ? (((~i) >> 31) & 0xFF) : (i)) |
84 | |
85 | static inline int left_shifti(int val, uint32_t bits) |
86 | { |
87 | return static_cast<int>(static_cast<uint32_t>(val) << bits); |
88 | } |
89 | |
90 | // Compiler creates a fast path 1D IDCT for X non-zero columns |
91 | template <int NONZERO_COLS> |
92 | struct Row |
93 | { |
94 | static void idct(int* pTemp, const jpgd_block_t* pSrc) |
95 | { |
96 | // ACCESS_COL() will be optimized at compile time to either an array access, or 0. Good compilers will then optimize out muls against 0. |
97 | #define ACCESS_COL(x) (((x) < NONZERO_COLS) ? (int)pSrc[x] : 0) |
98 | |
99 | const int z2 = ACCESS_COL(2), z3 = ACCESS_COL(6); |
100 | |
101 | const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100); |
102 | const int tmp2 = z1 + MULTIPLY(z3, -FIX_1_847759065); |
103 | const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865); |
104 | |
105 | const int tmp0 = left_shifti(ACCESS_COL(0) + ACCESS_COL(4), CONST_BITS); |
106 | const int tmp1 = left_shifti(ACCESS_COL(0) - ACCESS_COL(4), CONST_BITS); |
107 | |
108 | const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2; |
109 | |
110 | const int atmp0 = ACCESS_COL(7), atmp1 = ACCESS_COL(5), atmp2 = ACCESS_COL(3), atmp3 = ACCESS_COL(1); |
111 | |
112 | const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3; |
113 | const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602); |
114 | |
115 | const int az1 = MULTIPLY(bz1, -FIX_0_899976223); |
116 | const int az2 = MULTIPLY(bz2, -FIX_2_562915447); |
117 | const int az3 = MULTIPLY(bz3, -FIX_1_961570560) + bz5; |
118 | const int az4 = MULTIPLY(bz4, -FIX_0_390180644) + bz5; |
119 | |
120 | const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3; |
121 | const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4; |
122 | const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3; |
123 | const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4; |
124 | |
125 | pTemp[0] = DESCALE(tmp10 + btmp3, CONST_BITS - PASS1_BITS); |
126 | pTemp[7] = DESCALE(tmp10 - btmp3, CONST_BITS - PASS1_BITS); |
127 | pTemp[1] = DESCALE(tmp11 + btmp2, CONST_BITS - PASS1_BITS); |
128 | pTemp[6] = DESCALE(tmp11 - btmp2, CONST_BITS - PASS1_BITS); |
129 | pTemp[2] = DESCALE(tmp12 + btmp1, CONST_BITS - PASS1_BITS); |
130 | pTemp[5] = DESCALE(tmp12 - btmp1, CONST_BITS - PASS1_BITS); |
131 | pTemp[3] = DESCALE(tmp13 + btmp0, CONST_BITS - PASS1_BITS); |
132 | pTemp[4] = DESCALE(tmp13 - btmp0, CONST_BITS - PASS1_BITS); |
133 | } |
134 | }; |
135 | |
136 | template <> |
137 | struct Row<0> |
138 | { |
139 | static void idct(int* pTemp, const jpgd_block_t* pSrc) |
140 | { |
141 | (void)pTemp; |
142 | (void)pSrc; |
143 | } |
144 | }; |
145 | |
146 | template <> |
147 | struct Row<1> |
148 | { |
149 | static void idct(int* pTemp, const jpgd_block_t* pSrc) |
150 | { |
151 | const int dcval = left_shifti(pSrc[0], PASS1_BITS); |
152 | |
153 | pTemp[0] = dcval; |
154 | pTemp[1] = dcval; |
155 | pTemp[2] = dcval; |
156 | pTemp[3] = dcval; |
157 | pTemp[4] = dcval; |
158 | pTemp[5] = dcval; |
159 | pTemp[6] = dcval; |
160 | pTemp[7] = dcval; |
161 | } |
162 | }; |
163 | |
164 | // Compiler creates a fast path 1D IDCT for X non-zero rows |
165 | template <int NONZERO_ROWS> |
166 | struct Col |
167 | { |
168 | static void idct(uint8* pDst_ptr, const int* pTemp) |
169 | { |
170 | // ACCESS_ROW() will be optimized at compile time to either an array access, or 0. |
171 | #define ACCESS_ROW(x) (((x) < NONZERO_ROWS) ? pTemp[x * 8] : 0) |
172 | |
173 | const int z2 = ACCESS_ROW(2); |
174 | const int z3 = ACCESS_ROW(6); |
175 | |
176 | const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100); |
177 | const int tmp2 = z1 + MULTIPLY(z3, -FIX_1_847759065); |
178 | const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865); |
179 | |
180 | const int tmp0 = left_shifti(ACCESS_ROW(0) + ACCESS_ROW(4), CONST_BITS); |
181 | const int tmp1 = left_shifti(ACCESS_ROW(0) - ACCESS_ROW(4), CONST_BITS); |
182 | |
183 | const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2; |
184 | |
185 | const int atmp0 = ACCESS_ROW(7), atmp1 = ACCESS_ROW(5), atmp2 = ACCESS_ROW(3), atmp3 = ACCESS_ROW(1); |
186 | |
187 | const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3; |
188 | const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602); |
189 | |
190 | const int az1 = MULTIPLY(bz1, -FIX_0_899976223); |
191 | const int az2 = MULTIPLY(bz2, -FIX_2_562915447); |
192 | const int az3 = MULTIPLY(bz3, -FIX_1_961570560) + bz5; |
193 | const int az4 = MULTIPLY(bz4, -FIX_0_390180644) + bz5; |
194 | |
195 | const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3; |
196 | const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4; |
197 | const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3; |
198 | const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4; |
199 | |
200 | int i = DESCALE_ZEROSHIFT(tmp10 + btmp3, CONST_BITS + PASS1_BITS + 3); |
201 | pDst_ptr[8 * 0] = (uint8)CLAMP(i); |
202 | |
203 | i = DESCALE_ZEROSHIFT(tmp10 - btmp3, CONST_BITS + PASS1_BITS + 3); |
204 | pDst_ptr[8 * 7] = (uint8)CLAMP(i); |
205 | |
206 | i = DESCALE_ZEROSHIFT(tmp11 + btmp2, CONST_BITS + PASS1_BITS + 3); |
207 | pDst_ptr[8 * 1] = (uint8)CLAMP(i); |
208 | |
209 | i = DESCALE_ZEROSHIFT(tmp11 - btmp2, CONST_BITS + PASS1_BITS + 3); |
210 | pDst_ptr[8 * 6] = (uint8)CLAMP(i); |
211 | |
212 | i = DESCALE_ZEROSHIFT(tmp12 + btmp1, CONST_BITS + PASS1_BITS + 3); |
213 | pDst_ptr[8 * 2] = (uint8)CLAMP(i); |
214 | |
215 | i = DESCALE_ZEROSHIFT(tmp12 - btmp1, CONST_BITS + PASS1_BITS + 3); |
216 | pDst_ptr[8 * 5] = (uint8)CLAMP(i); |
217 | |
218 | i = DESCALE_ZEROSHIFT(tmp13 + btmp0, CONST_BITS + PASS1_BITS + 3); |
219 | pDst_ptr[8 * 3] = (uint8)CLAMP(i); |
220 | |
221 | i = DESCALE_ZEROSHIFT(tmp13 - btmp0, CONST_BITS + PASS1_BITS + 3); |
222 | pDst_ptr[8 * 4] = (uint8)CLAMP(i); |
223 | } |
224 | }; |
225 | |
226 | template <> |
227 | struct Col<1> |
228 | { |
229 | static void idct(uint8* pDst_ptr, const int* pTemp) |
230 | { |
231 | int dcval = DESCALE_ZEROSHIFT(pTemp[0], PASS1_BITS + 3); |
232 | const uint8 dcval_clamped = (uint8)CLAMP(dcval); |
233 | pDst_ptr[0 * 8] = dcval_clamped; |
234 | pDst_ptr[1 * 8] = dcval_clamped; |
235 | pDst_ptr[2 * 8] = dcval_clamped; |
236 | pDst_ptr[3 * 8] = dcval_clamped; |
237 | pDst_ptr[4 * 8] = dcval_clamped; |
238 | pDst_ptr[5 * 8] = dcval_clamped; |
239 | pDst_ptr[6 * 8] = dcval_clamped; |
240 | pDst_ptr[7 * 8] = dcval_clamped; |
241 | } |
242 | }; |
243 | |
244 | static const uint8 s_idct_row_table[] = |
245 | { |
246 | 1,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0, 2,1,0,0,0,0,0,0, 2,1,1,0,0,0,0,0, 2,2,1,0,0,0,0,0, 3,2,1,0,0,0,0,0, 4,2,1,0,0,0,0,0, 4,3,1,0,0,0,0,0, |
247 | 4,3,2,0,0,0,0,0, 4,3,2,1,0,0,0,0, 4,3,2,1,1,0,0,0, 4,3,2,2,1,0,0,0, 4,3,3,2,1,0,0,0, 4,4,3,2,1,0,0,0, 5,4,3,2,1,0,0,0, 6,4,3,2,1,0,0,0, |
248 | 6,5,3,2,1,0,0,0, 6,5,4,2,1,0,0,0, 6,5,4,3,1,0,0,0, 6,5,4,3,2,0,0,0, 6,5,4,3,2,1,0,0, 6,5,4,3,2,1,1,0, 6,5,4,3,2,2,1,0, 6,5,4,3,3,2,1,0, |
249 | 6,5,4,4,3,2,1,0, 6,5,5,4,3,2,1,0, 6,6,5,4,3,2,1,0, 7,6,5,4,3,2,1,0, 8,6,5,4,3,2,1,0, 8,7,5,4,3,2,1,0, 8,7,6,4,3,2,1,0, 8,7,6,5,3,2,1,0, |
250 | 8,7,6,5,4,2,1,0, 8,7,6,5,4,3,1,0, 8,7,6,5,4,3,2,0, 8,7,6,5,4,3,2,1, 8,7,6,5,4,3,2,2, 8,7,6,5,4,3,3,2, 8,7,6,5,4,4,3,2, 8,7,6,5,5,4,3,2, |
251 | 8,7,6,6,5,4,3,2, 8,7,7,6,5,4,3,2, 8,8,7,6,5,4,3,2, 8,8,8,6,5,4,3,2, 8,8,8,7,5,4,3,2, 8,8,8,7,6,4,3,2, 8,8,8,7,6,5,3,2, 8,8,8,7,6,5,4,2, |
252 | 8,8,8,7,6,5,4,3, 8,8,8,7,6,5,4,4, 8,8,8,7,6,5,5,4, 8,8,8,7,6,6,5,4, 8,8,8,7,7,6,5,4, 8,8,8,8,7,6,5,4, 8,8,8,8,8,6,5,4, 8,8,8,8,8,7,5,4, |
253 | 8,8,8,8,8,7,6,4, 8,8,8,8,8,7,6,5, 8,8,8,8,8,7,6,6, 8,8,8,8,8,7,7,6, 8,8,8,8,8,8,7,6, 8,8,8,8,8,8,8,6, 8,8,8,8,8,8,8,7, 8,8,8,8,8,8,8,8, |
254 | }; |
255 | |
256 | static const uint8 s_idct_col_table[] = |
257 | { |
258 | 1, 1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
259 | 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 |
260 | }; |
261 | |
262 | // Scalar "fast pathing" IDCT. |
263 | static void idct(const jpgd_block_t* pSrc_ptr, uint8* pDst_ptr, int block_max_zag) |
264 | { |
265 | assert(block_max_zag >= 1); |
266 | assert(block_max_zag <= 64); |
267 | |
268 | if (block_max_zag <= 1) |
269 | { |
270 | int k = ((pSrc_ptr[0] + 4) >> 3) + 128; |
271 | k = CLAMP(k); |
272 | k = k | (k << 8); |
273 | k = k | (k << 16); |
274 | |
275 | for (int i = 8; i > 0; i--) |
276 | { |
277 | *(int*)&pDst_ptr[0] = k; |
278 | *(int*)&pDst_ptr[4] = k; |
279 | pDst_ptr += 8; |
280 | } |
281 | return; |
282 | } |
283 | |
284 | int temp[64]; |
285 | |
286 | const jpgd_block_t* pSrc = pSrc_ptr; |
287 | int* pTemp = temp; |
288 | |
289 | const uint8* pRow_tab = &s_idct_row_table[(block_max_zag - 1) * 8]; |
290 | int i; |
291 | for (i = 8; i > 0; i--, pRow_tab++) |
292 | { |
293 | switch (*pRow_tab) |
294 | { |
295 | case 0: Row<0>::idct(pTemp, pSrc); break; |
296 | case 1: Row<1>::idct(pTemp, pSrc); break; |
297 | case 2: Row<2>::idct(pTemp, pSrc); break; |
298 | case 3: Row<3>::idct(pTemp, pSrc); break; |
299 | case 4: Row<4>::idct(pTemp, pSrc); break; |
300 | case 5: Row<5>::idct(pTemp, pSrc); break; |
301 | case 6: Row<6>::idct(pTemp, pSrc); break; |
302 | case 7: Row<7>::idct(pTemp, pSrc); break; |
303 | case 8: Row<8>::idct(pTemp, pSrc); break; |
304 | } |
305 | |
306 | pSrc += 8; |
307 | pTemp += 8; |
308 | } |
309 | |
310 | pTemp = temp; |
311 | |
312 | const int nonzero_rows = s_idct_col_table[block_max_zag - 1]; |
313 | for (i = 8; i > 0; i--) |
314 | { |
315 | switch (nonzero_rows) |
316 | { |
317 | case 1: Col<1>::idct(pDst_ptr, pTemp); break; |
318 | case 2: Col<2>::idct(pDst_ptr, pTemp); break; |
319 | case 3: Col<3>::idct(pDst_ptr, pTemp); break; |
320 | case 4: Col<4>::idct(pDst_ptr, pTemp); break; |
321 | case 5: Col<5>::idct(pDst_ptr, pTemp); break; |
322 | case 6: Col<6>::idct(pDst_ptr, pTemp); break; |
323 | case 7: Col<7>::idct(pDst_ptr, pTemp); break; |
324 | case 8: Col<8>::idct(pDst_ptr, pTemp); break; |
325 | } |
326 | |
327 | pTemp++; |
328 | pDst_ptr++; |
329 | } |
330 | } |
331 | |
332 | // Retrieve one character from the input stream. |
333 | inline uint jpeg_decoder::get_char() |
334 | { |
335 | // Any bytes remaining in buffer? |
336 | if (!m_in_buf_left) |
337 | { |
338 | // Try to get more bytes. |
339 | prep_in_buffer(); |
340 | // Still nothing to get? |
341 | if (!m_in_buf_left) |
342 | { |
343 | // Pad the end of the stream with 0xFF 0xD9 (EOI marker) |
344 | int t = m_tem_flag; |
345 | m_tem_flag ^= 1; |
346 | if (t) |
347 | return 0xD9; |
348 | else |
349 | return 0xFF; |
350 | } |
351 | } |
352 | |
353 | uint c = *m_pIn_buf_ofs++; |
354 | m_in_buf_left--; |
355 | |
356 | return c; |
357 | } |
358 | |
359 | // Same as previous method, except can indicate if the character is a pad character or not. |
360 | inline uint jpeg_decoder::get_char(bool* pPadding_flag) |
361 | { |
362 | if (!m_in_buf_left) |
363 | { |
364 | prep_in_buffer(); |
365 | if (!m_in_buf_left) |
366 | { |
367 | *pPadding_flag = true; |
368 | int t = m_tem_flag; |
369 | m_tem_flag ^= 1; |
370 | if (t) |
371 | return 0xD9; |
372 | else |
373 | return 0xFF; |
374 | } |
375 | } |
376 | |
377 | *pPadding_flag = false; |
378 | |
379 | uint c = *m_pIn_buf_ofs++; |
380 | m_in_buf_left--; |
381 | |
382 | return c; |
383 | } |
384 | |
385 | // Inserts a previously retrieved character back into the input buffer. |
386 | inline void jpeg_decoder::stuff_char(uint8 q) |
387 | { |
388 | // This could write before the input buffer, but we've placed another array there. |
389 | *(--m_pIn_buf_ofs) = q; |
390 | m_in_buf_left++; |
391 | } |
392 | |
393 | // Retrieves one character from the input stream, but does not read past markers. Will continue to return 0xFF when a marker is encountered. |
394 | inline uint8 jpeg_decoder::get_octet() |
395 | { |
396 | bool padding_flag; |
397 | int c = get_char(&padding_flag); |
398 | |
399 | if (c == 0xFF) |
400 | { |
401 | if (padding_flag) |
402 | return 0xFF; |
403 | |
404 | c = get_char(&padding_flag); |
405 | if (padding_flag) |
406 | { |
407 | stuff_char(0xFF); |
408 | return 0xFF; |
409 | } |
410 | |
411 | if (c == 0x00) |
412 | return 0xFF; |
413 | else |
414 | { |
415 | stuff_char(static_cast<uint8>(c)); |
416 | stuff_char(0xFF); |
417 | return 0xFF; |
418 | } |
419 | } |
420 | |
421 | return static_cast<uint8>(c); |
422 | } |
423 | |
424 | // Retrieves a variable number of bits from the input stream. Does not recognize markers. |
425 | inline uint jpeg_decoder::get_bits(int num_bits) |
426 | { |
427 | if (!num_bits) |
428 | return 0; |
429 | |
430 | uint i = m_bit_buf >> (32 - num_bits); |
431 | |
432 | if ((m_bits_left -= num_bits) <= 0) |
433 | { |
434 | m_bit_buf <<= (num_bits += m_bits_left); |
435 | |
436 | uint c1 = get_char(); |
437 | uint c2 = get_char(); |
438 | m_bit_buf = (m_bit_buf & 0xFFFF0000) | (c1 << 8) | c2; |
439 | |
440 | m_bit_buf <<= -m_bits_left; |
441 | |
442 | m_bits_left += 16; |
443 | |
444 | assert(m_bits_left >= 0); |
445 | } |
446 | else |
447 | m_bit_buf <<= num_bits; |
448 | |
449 | return i; |
450 | } |
451 | |
452 | // Retrieves a variable number of bits from the input stream. Markers will not be read into the input bit buffer. Instead, an infinite number of all 1's will be returned when a marker is encountered. |
453 | inline uint jpeg_decoder::get_bits_no_markers(int num_bits) |
454 | { |
455 | if (!num_bits) |
456 | return 0; |
457 | |
458 | assert(num_bits <= 16); |
459 | |
460 | uint i = m_bit_buf >> (32 - num_bits); |
461 | |
462 | if ((m_bits_left -= num_bits) <= 0) |
463 | { |
464 | m_bit_buf <<= (num_bits += m_bits_left); |
465 | |
466 | if ((m_in_buf_left < 2) || (m_pIn_buf_ofs[0] == 0xFF) || (m_pIn_buf_ofs[1] == 0xFF)) |
467 | { |
468 | uint c1 = get_octet(); |
469 | uint c2 = get_octet(); |
470 | m_bit_buf |= (c1 << 8) | c2; |
471 | } |
472 | else |
473 | { |
474 | m_bit_buf |= ((uint)m_pIn_buf_ofs[0] << 8) | m_pIn_buf_ofs[1]; |
475 | m_in_buf_left -= 2; |
476 | m_pIn_buf_ofs += 2; |
477 | } |
478 | |
479 | m_bit_buf <<= -m_bits_left; |
480 | |
481 | m_bits_left += 16; |
482 | |
483 | assert(m_bits_left >= 0); |
484 | } |
485 | else |
486 | m_bit_buf <<= num_bits; |
487 | |
488 | return i; |
489 | } |
490 | |
491 | // Decodes a Huffman encoded symbol. |
492 | inline int jpeg_decoder::huff_decode(huff_tables* pH) |
493 | { |
494 | if (!pH) |
495 | stop_decoding(JPGD_DECODE_ERROR); |
496 | |
497 | int symbol; |
498 | // Check first 8-bits: do we have a complete symbol? |
499 | if ((symbol = pH->look_up[m_bit_buf >> 24]) < 0) |
500 | { |
501 | // Decode more bits, use a tree traversal to find symbol. |
502 | int ofs = 23; |
503 | do |
504 | { |
505 | unsigned int idx = -(int)(symbol + ((m_bit_buf >> ofs) & 1)); |
506 | |
507 | // This should never happen, but to be safe I'm turning these asserts into a run-time check. |
508 | if ((idx >= JPGD_HUFF_TREE_MAX_LENGTH) || (ofs < 0)) |
509 | stop_decoding(JPGD_DECODE_ERROR); |
510 | |
511 | symbol = pH->tree[idx]; |
512 | ofs--; |
513 | } while (symbol < 0); |
514 | |
515 | get_bits_no_markers(8 + (23 - ofs)); |
516 | } |
517 | else |
518 | { |
519 | assert(symbol < JPGD_HUFF_CODE_SIZE_MAX_LENGTH); |
520 | get_bits_no_markers(pH->code_size[symbol]); |
521 | } |
522 | |
523 | return symbol; |
524 | } |
525 | |
526 | // Decodes a Huffman encoded symbol. |
527 | inline int jpeg_decoder::huff_decode(huff_tables* pH, int& ) |
528 | { |
529 | int symbol; |
530 | |
531 | if (!pH) |
532 | stop_decoding(JPGD_DECODE_ERROR); |
533 | |
534 | // Check first 8-bits: do we have a complete symbol? |
535 | if ((symbol = pH->look_up2[m_bit_buf >> 24]) < 0) |
536 | { |
537 | // Use a tree traversal to find symbol. |
538 | int ofs = 23; |
539 | do |
540 | { |
541 | unsigned int idx = -(int)(symbol + ((m_bit_buf >> ofs) & 1)); |
542 | |
543 | // This should never happen, but to be safe I'm turning these asserts into a run-time check. |
544 | if ((idx >= JPGD_HUFF_TREE_MAX_LENGTH) || (ofs < 0)) |
545 | stop_decoding(JPGD_DECODE_ERROR); |
546 | |
547 | symbol = pH->tree[idx]; |
548 | ofs--; |
549 | } while (symbol < 0); |
550 | |
551 | get_bits_no_markers(8 + (23 - ofs)); |
552 | |
553 | extra_bits = get_bits_no_markers(symbol & 0xF); |
554 | } |
555 | else |
556 | { |
557 | if (symbol & 0x8000) |
558 | { |
559 | //get_bits_no_markers((symbol >> 8) & 31); |
560 | assert(((symbol >> 8) & 31) <= 15); |
561 | get_bits_no_markers((symbol >> 8) & 15); |
562 | extra_bits = symbol >> 16; |
563 | } |
564 | else |
565 | { |
566 | int code_size = (symbol >> 8) & 31; |
567 | int = symbol & 0xF; |
568 | int bits = code_size + num_extra_bits; |
569 | |
570 | if (bits <= 16) |
571 | extra_bits = get_bits_no_markers(bits) & ((1 << num_extra_bits) - 1); |
572 | else |
573 | { |
574 | get_bits_no_markers(code_size); |
575 | extra_bits = get_bits_no_markers(num_extra_bits); |
576 | } |
577 | } |
578 | |
579 | symbol &= 0xFF; |
580 | } |
581 | |
582 | return symbol; |
583 | } |
584 | |
585 | // Tables and macro used to fully decode the DPCM differences. |
586 | static const int s_extend_test[16] = { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; |
587 | static const int s_extend_offset[16] = { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 }; |
588 | //static const int s_extend_mask[] = { 0, (1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4), (1 << 5), (1 << 6), (1 << 7), (1 << 8), (1 << 9), (1 << 10), (1 << 11), (1 << 12), (1 << 13), (1 << 14), (1 << 15), (1 << 16) }; |
589 | |
590 | #define JPGD_HUFF_EXTEND(x, s) (((x) < s_extend_test[s & 15]) ? ((x) + s_extend_offset[s & 15]) : (x)) |
591 | |
592 | // Unconditionally frees all allocated m_blocks. |
593 | void jpeg_decoder::free_all_blocks() |
594 | { |
595 | m_pStream = nullptr; |
596 | for (mem_block* b = m_pMem_blocks; b; ) |
597 | { |
598 | mem_block* n = b->m_pNext; |
599 | jpgd_free(b); |
600 | b = n; |
601 | } |
602 | m_pMem_blocks = nullptr; |
603 | } |
604 | |
605 | // This method handles all errors. It will never return. |
606 | // It could easily be changed to use C++ exceptions. |
607 | JPGD_NORETURN void jpeg_decoder::stop_decoding(jpgd_status status) |
608 | { |
609 | m_error_code = status; |
610 | free_all_blocks(); |
611 | longjmp(m_jmp_state, status); |
612 | } |
613 | |
614 | void* jpeg_decoder::alloc(size_t nSize, bool zero) |
615 | { |
616 | nSize = (JPGD_MAX(nSize, 1) + 3) & ~3; |
617 | char* rv = nullptr; |
618 | for (mem_block* b = m_pMem_blocks; b; b = b->m_pNext) |
619 | { |
620 | if ((b->m_used_count + nSize) <= b->m_size) |
621 | { |
622 | rv = b->m_data + b->m_used_count; |
623 | b->m_used_count += nSize; |
624 | break; |
625 | } |
626 | } |
627 | if (!rv) |
628 | { |
629 | int capacity = JPGD_MAX(32768 - 256, ((int)nSize + 2047) & ~2047); |
630 | mem_block* b = (mem_block*)jpgd_malloc(sizeof(mem_block) + capacity); |
631 | if (!b) |
632 | { |
633 | stop_decoding(JPGD_NOTENOUGHMEM); |
634 | } |
635 | |
636 | b->m_pNext = m_pMem_blocks; |
637 | m_pMem_blocks = b; |
638 | b->m_used_count = nSize; |
639 | b->m_size = capacity; |
640 | rv = b->m_data; |
641 | } |
642 | if (zero) memset(rv, 0, nSize); |
643 | return rv; |
644 | } |
645 | |
646 | void jpeg_decoder::word_clear(void* p, uint16 c, uint n) |
647 | { |
648 | uint8* pD = (uint8*)p; |
649 | const uint8 l = c & 0xFF, h = (c >> 8) & 0xFF; |
650 | while (n) |
651 | { |
652 | pD[0] = l; |
653 | pD[1] = h; |
654 | pD += 2; |
655 | n--; |
656 | } |
657 | } |
658 | |
659 | // Refill the input buffer. |
660 | // This method will sit in a loop until (A) the buffer is full or (B) |
661 | // the stream's read() method reports and end of file condition. |
662 | void jpeg_decoder::prep_in_buffer() |
663 | { |
664 | m_in_buf_left = 0; |
665 | m_pIn_buf_ofs = m_in_buf; |
666 | |
667 | if (m_eof_flag) |
668 | return; |
669 | |
670 | do |
671 | { |
672 | int bytes_read = m_pStream->read(m_in_buf + m_in_buf_left, JPGD_IN_BUF_SIZE - m_in_buf_left, &m_eof_flag); |
673 | if (bytes_read == -1) |
674 | stop_decoding(JPGD_STREAM_READ); |
675 | |
676 | m_in_buf_left += bytes_read; |
677 | } while ((m_in_buf_left < JPGD_IN_BUF_SIZE) && (!m_eof_flag)); |
678 | |
679 | m_total_bytes_read += m_in_buf_left; |
680 | |
681 | // Pad the end of the block with M_EOI (prevents the decompressor from going off the rails if the stream is invalid). |
682 | // (This dates way back to when this decompressor was written in C/asm, and the all-asm Huffman decoder did some fancy things to increase perf.) |
683 | word_clear(m_pIn_buf_ofs + m_in_buf_left, 0xD9FF, 64); |
684 | } |
685 | |
686 | // Read a Huffman code table. |
687 | void jpeg_decoder::read_dht_marker() |
688 | { |
689 | int i, index, count; |
690 | uint8 huff_num[17]; |
691 | uint8 huff_val[256]; |
692 | |
693 | uint num_left = get_bits(16); |
694 | |
695 | if (num_left < 2) |
696 | stop_decoding(JPGD_BAD_DHT_MARKER); |
697 | |
698 | num_left -= 2; |
699 | |
700 | while (num_left) |
701 | { |
702 | index = get_bits(8); |
703 | |
704 | huff_num[0] = 0; |
705 | |
706 | count = 0; |
707 | |
708 | for (i = 1; i <= 16; i++) |
709 | { |
710 | huff_num[i] = static_cast<uint8>(get_bits(8)); |
711 | count += huff_num[i]; |
712 | } |
713 | |
714 | if (count > 255) |
715 | stop_decoding(JPGD_BAD_DHT_COUNTS); |
716 | |
717 | bool symbol_present[256]; |
718 | memset(symbol_present, 0, sizeof(symbol_present)); |
719 | |
720 | for (i = 0; i < count; i++) |
721 | { |
722 | const int s = get_bits(8); |
723 | |
724 | // Check for obviously bogus tables. |
725 | if (symbol_present[s]) |
726 | stop_decoding(JPGD_BAD_DHT_COUNTS); |
727 | |
728 | huff_val[i] = static_cast<uint8_t>(s); |
729 | symbol_present[s] = true; |
730 | } |
731 | |
732 | i = 1 + 16 + count; |
733 | |
734 | if (num_left < (uint)i) |
735 | stop_decoding(JPGD_BAD_DHT_MARKER); |
736 | |
737 | num_left -= i; |
738 | |
739 | if ((index & 0x10) > 0x10) |
740 | stop_decoding(JPGD_BAD_DHT_INDEX); |
741 | |
742 | index = (index & 0x0F) + ((index & 0x10) >> 4) * (JPGD_MAX_HUFF_TABLES >> 1); |
743 | |
744 | if (index >= JPGD_MAX_HUFF_TABLES) |
745 | stop_decoding(JPGD_BAD_DHT_INDEX); |
746 | |
747 | if (!m_huff_num[index]) |
748 | m_huff_num[index] = (uint8*)alloc(17); |
749 | |
750 | if (!m_huff_val[index]) |
751 | m_huff_val[index] = (uint8*)alloc(256); |
752 | |
753 | m_huff_ac[index] = (index & 0x10) != 0; |
754 | memcpy(m_huff_num[index], huff_num, 17); |
755 | memcpy(m_huff_val[index], huff_val, 256); |
756 | } |
757 | } |
758 | |
759 | // Read a quantization table. |
760 | void jpeg_decoder::read_dqt_marker() |
761 | { |
762 | int n, i, prec; |
763 | uint num_left; |
764 | uint temp; |
765 | |
766 | num_left = get_bits(16); |
767 | |
768 | if (num_left < 2) |
769 | stop_decoding(JPGD_BAD_DQT_MARKER); |
770 | |
771 | num_left -= 2; |
772 | |
773 | while (num_left) |
774 | { |
775 | n = get_bits(8); |
776 | prec = n >> 4; |
777 | n &= 0x0F; |
778 | |
779 | if (n >= JPGD_MAX_QUANT_TABLES) |
780 | stop_decoding(JPGD_BAD_DQT_TABLE); |
781 | |
782 | if (!m_quant[n]) |
783 | m_quant[n] = (jpgd_quant_t*)alloc(64 * sizeof(jpgd_quant_t)); |
784 | |
785 | // read quantization entries, in zag order |
786 | for (i = 0; i < 64; i++) |
787 | { |
788 | temp = get_bits(8); |
789 | |
790 | if (prec) |
791 | temp = (temp << 8) + get_bits(8); |
792 | |
793 | m_quant[n][i] = static_cast<jpgd_quant_t>(temp); |
794 | } |
795 | |
796 | i = 64 + 1; |
797 | |
798 | if (prec) |
799 | i += 64; |
800 | |
801 | if (num_left < (uint)i) |
802 | stop_decoding(JPGD_BAD_DQT_LENGTH); |
803 | |
804 | num_left -= i; |
805 | } |
806 | } |
807 | |
808 | // Read the start of frame (SOF) marker. |
809 | void jpeg_decoder::read_sof_marker() |
810 | { |
811 | int i; |
812 | uint num_left; |
813 | |
814 | num_left = get_bits(16); |
815 | |
816 | /* precision: sorry, only 8-bit precision is supported */ |
817 | if (get_bits(8) != 8) |
818 | stop_decoding(JPGD_BAD_PRECISION); |
819 | |
820 | m_image_y_size = get_bits(16); |
821 | |
822 | if ((m_image_y_size < 1) || (m_image_y_size > JPGD_MAX_HEIGHT)) |
823 | stop_decoding(JPGD_BAD_HEIGHT); |
824 | |
825 | m_image_x_size = get_bits(16); |
826 | |
827 | if ((m_image_x_size < 1) || (m_image_x_size > JPGD_MAX_WIDTH)) |
828 | stop_decoding(JPGD_BAD_WIDTH); |
829 | |
830 | m_comps_in_frame = get_bits(8); |
831 | |
832 | if (m_comps_in_frame > JPGD_MAX_COMPONENTS) |
833 | stop_decoding(JPGD_TOO_MANY_COMPONENTS); |
834 | |
835 | if (num_left != (uint)(m_comps_in_frame * 3 + 8)) |
836 | stop_decoding(JPGD_BAD_SOF_LENGTH); |
837 | |
838 | for (i = 0; i < m_comps_in_frame; i++) |
839 | { |
840 | m_comp_ident[i] = get_bits(8); |
841 | m_comp_h_samp[i] = get_bits(4); |
842 | m_comp_v_samp[i] = get_bits(4); |
843 | |
844 | if (!m_comp_h_samp[i] || !m_comp_v_samp[i] || (m_comp_h_samp[i] > 2) || (m_comp_v_samp[i] > 2)) |
845 | stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS); |
846 | |
847 | m_comp_quant[i] = get_bits(8); |
848 | if (m_comp_quant[i] >= JPGD_MAX_QUANT_TABLES) |
849 | stop_decoding(JPGD_DECODE_ERROR); |
850 | } |
851 | } |
852 | |
853 | // Used to skip unrecognized markers. |
854 | void jpeg_decoder::skip_variable_marker() |
855 | { |
856 | uint num_left; |
857 | |
858 | num_left = get_bits(16); |
859 | |
860 | if (num_left < 2) |
861 | stop_decoding(JPGD_BAD_VARIABLE_MARKER); |
862 | |
863 | num_left -= 2; |
864 | |
865 | while (num_left) |
866 | { |
867 | get_bits(8); |
868 | num_left--; |
869 | } |
870 | } |
871 | |
872 | // Read a define restart interval (DRI) marker. |
873 | void jpeg_decoder::read_dri_marker() |
874 | { |
875 | if (get_bits(16) != 4) |
876 | stop_decoding(JPGD_BAD_DRI_LENGTH); |
877 | |
878 | m_restart_interval = get_bits(16); |
879 | } |
880 | |
881 | // Read a start of scan (SOS) marker. |
882 | void jpeg_decoder::read_sos_marker() |
883 | { |
884 | uint num_left; |
885 | int i, ci, n, c, cc; |
886 | |
887 | num_left = get_bits(16); |
888 | |
889 | n = get_bits(8); |
890 | |
891 | m_comps_in_scan = n; |
892 | |
893 | num_left -= 3; |
894 | |
895 | if ((num_left != (uint)(n * 2 + 3)) || (n < 1) || (n > JPGD_MAX_COMPS_IN_SCAN)) |
896 | stop_decoding(JPGD_BAD_SOS_LENGTH); |
897 | |
898 | for (i = 0; i < n; i++) |
899 | { |
900 | cc = get_bits(8); |
901 | c = get_bits(8); |
902 | num_left -= 2; |
903 | |
904 | for (ci = 0; ci < m_comps_in_frame; ci++) |
905 | if (cc == m_comp_ident[ci]) |
906 | break; |
907 | |
908 | if (ci >= m_comps_in_frame) |
909 | stop_decoding(JPGD_BAD_SOS_COMP_ID); |
910 | |
911 | if (ci >= JPGD_MAX_COMPONENTS) |
912 | stop_decoding(JPGD_DECODE_ERROR); |
913 | |
914 | m_comp_list[i] = ci; |
915 | |
916 | m_comp_dc_tab[ci] = (c >> 4) & 15; |
917 | m_comp_ac_tab[ci] = (c & 15) + (JPGD_MAX_HUFF_TABLES >> 1); |
918 | |
919 | if (m_comp_dc_tab[ci] >= JPGD_MAX_HUFF_TABLES) |
920 | stop_decoding(JPGD_DECODE_ERROR); |
921 | |
922 | if (m_comp_ac_tab[ci] >= JPGD_MAX_HUFF_TABLES) |
923 | stop_decoding(JPGD_DECODE_ERROR); |
924 | } |
925 | |
926 | m_spectral_start = get_bits(8); |
927 | m_spectral_end = get_bits(8); |
928 | m_successive_high = get_bits(4); |
929 | m_successive_low = get_bits(4); |
930 | |
931 | if (!m_progressive_flag) |
932 | { |
933 | m_spectral_start = 0; |
934 | m_spectral_end = 63; |
935 | } |
936 | |
937 | num_left -= 3; |
938 | |
939 | /* read past whatever is num_left */ |
940 | while (num_left) |
941 | { |
942 | get_bits(8); |
943 | num_left--; |
944 | } |
945 | } |
946 | |
947 | // Finds the next marker. |
948 | int jpeg_decoder::next_marker() |
949 | { |
950 | uint c, bytes; |
951 | |
952 | bytes = 0; |
953 | |
954 | do |
955 | { |
956 | do |
957 | { |
958 | bytes++; |
959 | c = get_bits(8); |
960 | } while (c != 0xFF); |
961 | |
962 | do |
963 | { |
964 | c = get_bits(8); |
965 | } while (c == 0xFF); |
966 | |
967 | } while (c == 0); |
968 | |
969 | // If bytes > 0 here, there where extra bytes before the marker (not good). |
970 | |
971 | return c; |
972 | } |
973 | |
974 | // Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is |
975 | // encountered. |
976 | int jpeg_decoder::process_markers() |
977 | { |
978 | int c; |
979 | |
980 | for (; ; ) |
981 | { |
982 | c = next_marker(); |
983 | |
984 | switch (c) |
985 | { |
986 | case M_SOF0: |
987 | case M_SOF1: |
988 | case M_SOF2: |
989 | case M_SOF3: |
990 | case M_SOF5: |
991 | case M_SOF6: |
992 | case M_SOF7: |
993 | // case M_JPG: |
994 | case M_SOF9: |
995 | case M_SOF10: |
996 | case M_SOF11: |
997 | case M_SOF13: |
998 | case M_SOF14: |
999 | case M_SOF15: |
1000 | case M_SOI: |
1001 | case M_EOI: |
1002 | case M_SOS: |
1003 | { |
1004 | return c; |
1005 | } |
1006 | case M_DHT: |
1007 | { |
1008 | read_dht_marker(); |
1009 | break; |
1010 | } |
1011 | // No arithmitic support - dumb patents! |
1012 | case M_DAC: |
1013 | { |
1014 | stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT); |
1015 | break; |
1016 | } |
1017 | case M_DQT: |
1018 | { |
1019 | read_dqt_marker(); |
1020 | break; |
1021 | } |
1022 | case M_DRI: |
1023 | { |
1024 | read_dri_marker(); |
1025 | break; |
1026 | } |
1027 | //case M_APP0: /* no need to read the JFIF marker */ |
1028 | case M_JPG: |
1029 | case M_RST0: /* no parameters */ |
1030 | case M_RST1: |
1031 | case M_RST2: |
1032 | case M_RST3: |
1033 | case M_RST4: |
1034 | case M_RST5: |
1035 | case M_RST6: |
1036 | case M_RST7: |
1037 | case M_TEM: |
1038 | { |
1039 | stop_decoding(JPGD_UNEXPECTED_MARKER); |
1040 | break; |
1041 | } |
1042 | default: /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */ |
1043 | { |
1044 | skip_variable_marker(); |
1045 | break; |
1046 | } |
1047 | } |
1048 | } |
1049 | } |
1050 | |
1051 | // Finds the start of image (SOI) marker. |
1052 | void jpeg_decoder::locate_soi_marker() |
1053 | { |
1054 | uint lastchar, thischar; |
1055 | uint bytesleft; |
1056 | |
1057 | lastchar = get_bits(8); |
1058 | |
1059 | thischar = get_bits(8); |
1060 | |
1061 | /* ok if it's a normal JPEG file without a special header */ |
1062 | |
1063 | if ((lastchar == 0xFF) && (thischar == M_SOI)) |
1064 | return; |
1065 | |
1066 | bytesleft = 4096; |
1067 | |
1068 | for (; ; ) |
1069 | { |
1070 | if (--bytesleft == 0) |
1071 | stop_decoding(JPGD_NOT_JPEG); |
1072 | |
1073 | lastchar = thischar; |
1074 | |
1075 | thischar = get_bits(8); |
1076 | |
1077 | if (lastchar == 0xFF) |
1078 | { |
1079 | if (thischar == M_SOI) |
1080 | break; |
1081 | else if (thischar == M_EOI) // get_bits will keep returning M_EOI if we read past the end |
1082 | stop_decoding(JPGD_NOT_JPEG); |
1083 | } |
1084 | } |
1085 | |
1086 | // Check the next character after marker: if it's not 0xFF, it can't be the start of the next marker, so the file is bad. |
1087 | thischar = (m_bit_buf >> 24) & 0xFF; |
1088 | |
1089 | if (thischar != 0xFF) |
1090 | stop_decoding(JPGD_NOT_JPEG); |
1091 | } |
1092 | |
1093 | // Find a start of frame (SOF) marker. |
1094 | void jpeg_decoder::locate_sof_marker() |
1095 | { |
1096 | locate_soi_marker(); |
1097 | |
1098 | int c = process_markers(); |
1099 | |
1100 | switch (c) |
1101 | { |
1102 | case M_SOF2: |
1103 | { |
1104 | m_progressive_flag = JPGD_TRUE; |
1105 | read_sof_marker(); |
1106 | break; |
1107 | } |
1108 | case M_SOF0: /* baseline DCT */ |
1109 | case M_SOF1: /* extended sequential DCT */ |
1110 | { |
1111 | read_sof_marker(); |
1112 | break; |
1113 | } |
1114 | case M_SOF9: /* Arithmitic coding */ |
1115 | { |
1116 | stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT); |
1117 | break; |
1118 | } |
1119 | default: |
1120 | { |
1121 | stop_decoding(JPGD_UNSUPPORTED_MARKER); |
1122 | break; |
1123 | } |
1124 | } |
1125 | } |
1126 | |
1127 | // Find a start of scan (SOS) marker. |
1128 | int jpeg_decoder::locate_sos_marker() |
1129 | { |
1130 | int c; |
1131 | |
1132 | c = process_markers(); |
1133 | |
1134 | if (c == M_EOI) |
1135 | return JPGD_FALSE; |
1136 | else if (c != M_SOS) |
1137 | stop_decoding(JPGD_UNEXPECTED_MARKER); |
1138 | |
1139 | read_sos_marker(); |
1140 | |
1141 | return JPGD_TRUE; |
1142 | } |
1143 | |
1144 | // Reset everything to default/uninitialized state. |
1145 | void jpeg_decoder::init(jpeg_decoder_stream* pStream, uint32_t flags) |
1146 | { |
1147 | m_flags = flags; |
1148 | m_pMem_blocks = nullptr; |
1149 | m_error_code = JPGD_SUCCESS; |
1150 | m_ready_flag = false; |
1151 | m_image_x_size = m_image_y_size = 0; |
1152 | m_pStream = pStream; |
1153 | m_progressive_flag = JPGD_FALSE; |
1154 | |
1155 | memset(m_huff_ac, 0, sizeof(m_huff_ac)); |
1156 | memset(m_huff_num, 0, sizeof(m_huff_num)); |
1157 | memset(m_huff_val, 0, sizeof(m_huff_val)); |
1158 | memset(m_quant, 0, sizeof(m_quant)); |
1159 | |
1160 | m_scan_type = 0; |
1161 | m_comps_in_frame = 0; |
1162 | |
1163 | memset(m_comp_h_samp, 0, sizeof(m_comp_h_samp)); |
1164 | memset(m_comp_v_samp, 0, sizeof(m_comp_v_samp)); |
1165 | memset(m_comp_quant, 0, sizeof(m_comp_quant)); |
1166 | memset(m_comp_ident, 0, sizeof(m_comp_ident)); |
1167 | memset(m_comp_h_blocks, 0, sizeof(m_comp_h_blocks)); |
1168 | memset(m_comp_v_blocks, 0, sizeof(m_comp_v_blocks)); |
1169 | |
1170 | m_comps_in_scan = 0; |
1171 | memset(m_comp_list, 0, sizeof(m_comp_list)); |
1172 | memset(m_comp_dc_tab, 0, sizeof(m_comp_dc_tab)); |
1173 | memset(m_comp_ac_tab, 0, sizeof(m_comp_ac_tab)); |
1174 | |
1175 | m_spectral_start = 0; |
1176 | m_spectral_end = 0; |
1177 | m_successive_low = 0; |
1178 | m_successive_high = 0; |
1179 | m_max_mcu_x_size = 0; |
1180 | m_max_mcu_y_size = 0; |
1181 | m_blocks_per_mcu = 0; |
1182 | m_max_blocks_per_row = 0; |
1183 | m_mcus_per_row = 0; |
1184 | m_mcus_per_col = 0; |
1185 | |
1186 | memset(m_mcu_org, 0, sizeof(m_mcu_org)); |
1187 | |
1188 | m_total_lines_left = 0; |
1189 | m_mcu_lines_left = 0; |
1190 | m_num_buffered_scanlines = 0; |
1191 | m_real_dest_bytes_per_scan_line = 0; |
1192 | m_dest_bytes_per_scan_line = 0; |
1193 | m_dest_bytes_per_pixel = 0; |
1194 | |
1195 | memset(m_pHuff_tabs, 0, sizeof(m_pHuff_tabs)); |
1196 | |
1197 | memset(m_dc_coeffs, 0, sizeof(m_dc_coeffs)); |
1198 | memset(m_ac_coeffs, 0, sizeof(m_ac_coeffs)); |
1199 | memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu)); |
1200 | |
1201 | m_eob_run = 0; |
1202 | |
1203 | m_pIn_buf_ofs = m_in_buf; |
1204 | m_in_buf_left = 0; |
1205 | m_eof_flag = false; |
1206 | m_tem_flag = 0; |
1207 | |
1208 | memset(m_in_buf_pad_start, 0, sizeof(m_in_buf_pad_start)); |
1209 | memset(m_in_buf, 0, sizeof(m_in_buf)); |
1210 | memset(m_in_buf_pad_end, 0, sizeof(m_in_buf_pad_end)); |
1211 | |
1212 | m_restart_interval = 0; |
1213 | m_restarts_left = 0; |
1214 | m_next_restart_num = 0; |
1215 | |
1216 | m_max_mcus_per_row = 0; |
1217 | m_max_blocks_per_mcu = 0; |
1218 | m_max_mcus_per_col = 0; |
1219 | |
1220 | memset(m_last_dc_val, 0, sizeof(m_last_dc_val)); |
1221 | m_pMCU_coefficients = nullptr; |
1222 | m_pSample_buf = nullptr; |
1223 | m_pSample_buf_prev = nullptr; |
1224 | m_sample_buf_prev_valid = false; |
1225 | |
1226 | m_total_bytes_read = 0; |
1227 | |
1228 | m_pScan_line_0 = nullptr; |
1229 | m_pScan_line_1 = nullptr; |
1230 | |
1231 | // Ready the input buffer. |
1232 | prep_in_buffer(); |
1233 | |
1234 | // Prime the bit buffer. |
1235 | m_bits_left = 16; |
1236 | m_bit_buf = 0; |
1237 | |
1238 | get_bits(16); |
1239 | get_bits(16); |
1240 | |
1241 | for (int i = 0; i < JPGD_MAX_BLOCKS_PER_MCU; i++) |
1242 | m_mcu_block_max_zag[i] = 64; |
1243 | } |
1244 | |
1245 | #define SCALEBITS 16 |
1246 | #define ONE_HALF ((int) 1 << (SCALEBITS-1)) |
1247 | #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5f)) |
1248 | |
1249 | // Create a few tables that allow us to quickly convert YCbCr to RGB. |
1250 | void jpeg_decoder::create_look_ups() |
1251 | { |
1252 | for (int i = 0; i <= 255; i++) |
1253 | { |
1254 | int k = i - 128; |
1255 | m_crr[i] = (FIX(1.40200f) * k + ONE_HALF) >> SCALEBITS; |
1256 | m_cbb[i] = (FIX(1.77200f) * k + ONE_HALF) >> SCALEBITS; |
1257 | m_crg[i] = (-FIX(0.71414f)) * k; |
1258 | m_cbg[i] = (-FIX(0.34414f)) * k + ONE_HALF; |
1259 | } |
1260 | } |
1261 | |
1262 | // This method throws back into the stream any bytes that where read |
1263 | // into the bit buffer during initial marker scanning. |
1264 | void jpeg_decoder::fix_in_buffer() |
1265 | { |
1266 | // In case any 0xFF's where pulled into the buffer during marker scanning. |
1267 | assert((m_bits_left & 7) == 0); |
1268 | |
1269 | if (m_bits_left == 16) |
1270 | stuff_char((uint8)(m_bit_buf & 0xFF)); |
1271 | |
1272 | if (m_bits_left >= 8) |
1273 | stuff_char((uint8)((m_bit_buf >> 8) & 0xFF)); |
1274 | |
1275 | stuff_char((uint8)((m_bit_buf >> 16) & 0xFF)); |
1276 | stuff_char((uint8)((m_bit_buf >> 24) & 0xFF)); |
1277 | |
1278 | m_bits_left = 16; |
1279 | get_bits_no_markers(16); |
1280 | get_bits_no_markers(16); |
1281 | } |
1282 | |
1283 | void jpeg_decoder::transform_mcu(int mcu_row) |
1284 | { |
1285 | jpgd_block_t* pSrc_ptr = m_pMCU_coefficients; |
1286 | if (mcu_row * m_blocks_per_mcu >= m_max_blocks_per_row) |
1287 | stop_decoding(JPGD_DECODE_ERROR); |
1288 | |
1289 | uint8* pDst_ptr = m_pSample_buf + mcu_row * m_blocks_per_mcu * 64; |
1290 | |
1291 | for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++) |
1292 | { |
1293 | idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]); |
1294 | pSrc_ptr += 64; |
1295 | pDst_ptr += 64; |
1296 | } |
1297 | } |
1298 | |
1299 | // Loads and dequantizes the next row of (already decoded) coefficients. |
1300 | // Progressive images only. |
1301 | void jpeg_decoder::load_next_row() |
1302 | { |
1303 | int i; |
1304 | jpgd_block_t* p; |
1305 | jpgd_quant_t* q; |
1306 | int mcu_row, mcu_block, row_block = 0; |
1307 | int component_num, component_id; |
1308 | int block_x_mcu[JPGD_MAX_COMPONENTS]; |
1309 | |
1310 | memset(block_x_mcu, 0, JPGD_MAX_COMPONENTS * sizeof(int)); |
1311 | |
1312 | for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++) |
1313 | { |
1314 | int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0; |
1315 | |
1316 | for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++) |
1317 | { |
1318 | component_id = m_mcu_org[mcu_block]; |
1319 | if (m_comp_quant[component_id] >= JPGD_MAX_QUANT_TABLES) |
1320 | stop_decoding(JPGD_DECODE_ERROR); |
1321 | |
1322 | q = m_quant[m_comp_quant[component_id]]; |
1323 | |
1324 | p = m_pMCU_coefficients + 64 * mcu_block; |
1325 | |
1326 | jpgd_block_t* pAC = coeff_buf_getp(m_ac_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs); |
1327 | jpgd_block_t* pDC = coeff_buf_getp(m_dc_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs); |
1328 | p[0] = pDC[0]; |
1329 | memcpy(&p[1], &pAC[1], 63 * sizeof(jpgd_block_t)); |
1330 | |
1331 | for (i = 63; i > 0; i--) |
1332 | if (p[g_ZAG[i]]) |
1333 | break; |
1334 | |
1335 | m_mcu_block_max_zag[mcu_block] = i + 1; |
1336 | |
1337 | for (; i >= 0; i--) |
1338 | if (p[g_ZAG[i]]) |
1339 | p[g_ZAG[i]] = static_cast<jpgd_block_t>(p[g_ZAG[i]] * q[i]); |
1340 | |
1341 | row_block++; |
1342 | |
1343 | if (m_comps_in_scan == 1) |
1344 | block_x_mcu[component_id]++; |
1345 | else |
1346 | { |
1347 | if (++block_x_mcu_ofs == m_comp_h_samp[component_id]) |
1348 | { |
1349 | block_x_mcu_ofs = 0; |
1350 | |
1351 | if (++block_y_mcu_ofs == m_comp_v_samp[component_id]) |
1352 | { |
1353 | block_y_mcu_ofs = 0; |
1354 | |
1355 | block_x_mcu[component_id] += m_comp_h_samp[component_id]; |
1356 | } |
1357 | } |
1358 | } |
1359 | } |
1360 | |
1361 | transform_mcu(mcu_row); |
1362 | } |
1363 | |
1364 | if (m_comps_in_scan == 1) |
1365 | m_block_y_mcu[m_comp_list[0]]++; |
1366 | else |
1367 | { |
1368 | for (component_num = 0; component_num < m_comps_in_scan; component_num++) |
1369 | { |
1370 | component_id = m_comp_list[component_num]; |
1371 | |
1372 | m_block_y_mcu[component_id] += m_comp_v_samp[component_id]; |
1373 | } |
1374 | } |
1375 | } |
1376 | |
1377 | // Restart interval processing. |
1378 | void jpeg_decoder::process_restart() |
1379 | { |
1380 | int i; |
1381 | int c = 0; |
1382 | |
1383 | // Align to a byte boundry |
1384 | // FIXME: Is this really necessary? get_bits_no_markers() never reads in markers! |
1385 | //get_bits_no_markers(m_bits_left & 7); |
1386 | |
1387 | // Let's scan a little bit to find the marker, but not _too_ far. |
1388 | // 1536 is a "fudge factor" that determines how much to scan. |
1389 | for (i = 1536; i > 0; i--) |
1390 | if (get_char() == 0xFF) |
1391 | break; |
1392 | |
1393 | if (i == 0) |
1394 | stop_decoding(JPGD_BAD_RESTART_MARKER); |
1395 | |
1396 | for (; i > 0; i--) |
1397 | if ((c = get_char()) != 0xFF) |
1398 | break; |
1399 | |
1400 | if (i == 0) |
1401 | stop_decoding(JPGD_BAD_RESTART_MARKER); |
1402 | |
1403 | // Is it the expected marker? If not, something bad happened. |
1404 | if (c != (m_next_restart_num + M_RST0)) |
1405 | stop_decoding(JPGD_BAD_RESTART_MARKER); |
1406 | |
1407 | // Reset each component's DC prediction values. |
1408 | memset(&m_last_dc_val, 0, m_comps_in_frame * sizeof(uint)); |
1409 | |
1410 | m_eob_run = 0; |
1411 | |
1412 | m_restarts_left = m_restart_interval; |
1413 | |
1414 | m_next_restart_num = (m_next_restart_num + 1) & 7; |
1415 | |
1416 | // Get the bit buffer going again... |
1417 | |
1418 | m_bits_left = 16; |
1419 | get_bits_no_markers(16); |
1420 | get_bits_no_markers(16); |
1421 | } |
1422 | |
1423 | static inline int dequantize_ac(int c, int q) { c *= q; return c; } |
1424 | |
1425 | // Decodes and dequantizes the next row of coefficients. |
1426 | void jpeg_decoder::decode_next_row() |
1427 | { |
1428 | int row_block = 0; |
1429 | |
1430 | for (int mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++) |
1431 | { |
1432 | if ((m_restart_interval) && (m_restarts_left == 0)) |
1433 | process_restart(); |
1434 | |
1435 | jpgd_block_t* p = m_pMCU_coefficients; |
1436 | for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++, p += 64) |
1437 | { |
1438 | int component_id = m_mcu_org[mcu_block]; |
1439 | if (m_comp_quant[component_id] >= JPGD_MAX_QUANT_TABLES) |
1440 | stop_decoding(JPGD_DECODE_ERROR); |
1441 | |
1442 | jpgd_quant_t* q = m_quant[m_comp_quant[component_id]]; |
1443 | |
1444 | int r, s; |
1445 | s = huff_decode(m_pHuff_tabs[m_comp_dc_tab[component_id]], r); |
1446 | if (s >= 16) |
1447 | stop_decoding(JPGD_DECODE_ERROR); |
1448 | |
1449 | s = JPGD_HUFF_EXTEND(r, s); |
1450 | |
1451 | m_last_dc_val[component_id] = (s += m_last_dc_val[component_id]); |
1452 | |
1453 | p[0] = static_cast<jpgd_block_t>(s * q[0]); |
1454 | |
1455 | int prev_num_set = m_mcu_block_max_zag[mcu_block]; |
1456 | |
1457 | huff_tables* pH = m_pHuff_tabs[m_comp_ac_tab[component_id]]; |
1458 | |
1459 | int k; |
1460 | for (k = 1; k < 64; k++) |
1461 | { |
1462 | int ; |
1463 | s = huff_decode(pH, extra_bits); |
1464 | |
1465 | r = s >> 4; |
1466 | s &= 15; |
1467 | |
1468 | if (s) |
1469 | { |
1470 | if (r) |
1471 | { |
1472 | if ((k + r) > 63) |
1473 | stop_decoding(JPGD_DECODE_ERROR); |
1474 | |
1475 | if (k < prev_num_set) |
1476 | { |
1477 | int n = JPGD_MIN(r, prev_num_set - k); |
1478 | int kt = k; |
1479 | while (n--) |
1480 | p[g_ZAG[kt++]] = 0; |
1481 | } |
1482 | |
1483 | k += r; |
1484 | } |
1485 | |
1486 | s = JPGD_HUFF_EXTEND(extra_bits, s); |
1487 | |
1488 | if (k >= 64) |
1489 | stop_decoding(JPGD_DECODE_ERROR); |
1490 | |
1491 | p[g_ZAG[k]] = static_cast<jpgd_block_t>(dequantize_ac(s, q[k])); //s * q[k]; |
1492 | } |
1493 | else |
1494 | { |
1495 | if (r == 15) |
1496 | { |
1497 | if ((k + 16) > 64) |
1498 | stop_decoding(JPGD_DECODE_ERROR); |
1499 | |
1500 | if (k < prev_num_set) |
1501 | { |
1502 | int n = JPGD_MIN(16, prev_num_set - k); |
1503 | int kt = k; |
1504 | while (n--) |
1505 | { |
1506 | if (kt > 63) |
1507 | stop_decoding(JPGD_DECODE_ERROR); |
1508 | p[g_ZAG[kt++]] = 0; |
1509 | } |
1510 | } |
1511 | |
1512 | k += 16 - 1; // - 1 because the loop counter is k |
1513 | |
1514 | if (p[g_ZAG[k & 63]] != 0) |
1515 | stop_decoding(JPGD_DECODE_ERROR); |
1516 | } |
1517 | else |
1518 | break; |
1519 | } |
1520 | } |
1521 | |
1522 | if (k < prev_num_set) |
1523 | { |
1524 | int kt = k; |
1525 | while (kt < prev_num_set) |
1526 | p[g_ZAG[kt++]] = 0; |
1527 | } |
1528 | |
1529 | m_mcu_block_max_zag[mcu_block] = k; |
1530 | |
1531 | row_block++; |
1532 | } |
1533 | |
1534 | transform_mcu(mcu_row); |
1535 | |
1536 | m_restarts_left--; |
1537 | } |
1538 | } |
1539 | |
1540 | // YCbCr H1V1 (1x1:1:1, 3 m_blocks per MCU) to RGB |
1541 | void jpeg_decoder::H1V1Convert() |
1542 | { |
1543 | int row = m_max_mcu_y_size - m_mcu_lines_left; |
1544 | uint8* d = m_pScan_line_0; |
1545 | uint8* s = m_pSample_buf + row * 8; |
1546 | |
1547 | for (int i = m_max_mcus_per_row; i > 0; i--) |
1548 | { |
1549 | for (int j = 0; j < 8; j++) |
1550 | { |
1551 | int y = s[j]; |
1552 | int cb = s[64 + j]; |
1553 | int cr = s[128 + j]; |
1554 | |
1555 | d[0] = clamp(y + m_crr[cr]); |
1556 | d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16)); |
1557 | d[2] = clamp(y + m_cbb[cb]); |
1558 | d[3] = 255; |
1559 | |
1560 | d += 4; |
1561 | } |
1562 | |
1563 | s += 64 * 3; |
1564 | } |
1565 | } |
1566 | |
1567 | // YCbCr H2V1 (2x1:1:1, 4 m_blocks per MCU) to RGB |
1568 | void jpeg_decoder::H2V1Convert() |
1569 | { |
1570 | int row = m_max_mcu_y_size - m_mcu_lines_left; |
1571 | uint8* d0 = m_pScan_line_0; |
1572 | uint8* y = m_pSample_buf + row * 8; |
1573 | uint8* c = m_pSample_buf + 2 * 64 + row * 8; |
1574 | |
1575 | for (int i = m_max_mcus_per_row; i > 0; i--) |
1576 | { |
1577 | for (int l = 0; l < 2; l++) |
1578 | { |
1579 | for (int j = 0; j < 4; j++) |
1580 | { |
1581 | int cb = c[0]; |
1582 | int cr = c[64]; |
1583 | |
1584 | int rc = m_crr[cr]; |
1585 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1586 | int bc = m_cbb[cb]; |
1587 | |
1588 | int yy = y[j << 1]; |
1589 | d0[0] = clamp(yy + rc); |
1590 | d0[1] = clamp(yy + gc); |
1591 | d0[2] = clamp(yy + bc); |
1592 | d0[3] = 255; |
1593 | |
1594 | yy = y[(j << 1) + 1]; |
1595 | d0[4] = clamp(yy + rc); |
1596 | d0[5] = clamp(yy + gc); |
1597 | d0[6] = clamp(yy + bc); |
1598 | d0[7] = 255; |
1599 | |
1600 | d0 += 8; |
1601 | |
1602 | c++; |
1603 | } |
1604 | y += 64; |
1605 | } |
1606 | |
1607 | y += 64 * 4 - 64 * 2; |
1608 | c += 64 * 4 - 8; |
1609 | } |
1610 | } |
1611 | |
1612 | // YCbCr H2V1 (2x1:1:1, 4 m_blocks per MCU) to RGB |
1613 | void jpeg_decoder::H2V1ConvertFiltered() |
1614 | { |
1615 | const uint BLOCKS_PER_MCU = 4; |
1616 | int row = m_max_mcu_y_size - m_mcu_lines_left; |
1617 | uint8* d0 = m_pScan_line_0; |
1618 | |
1619 | const int half_image_x_size = (m_image_x_size >> 1) - 1; |
1620 | const int row_x8 = row * 8; |
1621 | |
1622 | for (int x = 0; x < m_image_x_size; x++) |
1623 | { |
1624 | int y = m_pSample_buf[check_sample_buf_ofs((x >> 4) * BLOCKS_PER_MCU * 64 + ((x & 8) ? 64 : 0) + (x & 7) + row_x8)]; |
1625 | |
1626 | int c_x0 = (x - 1) >> 1; |
1627 | int c_x1 = JPGD_MIN(c_x0 + 1, half_image_x_size); |
1628 | c_x0 = JPGD_MAX(c_x0, 0); |
1629 | |
1630 | int a = (c_x0 >> 3) * BLOCKS_PER_MCU * 64 + (c_x0 & 7) + row_x8 + 128; |
1631 | int cb0 = m_pSample_buf[check_sample_buf_ofs(a)]; |
1632 | int cr0 = m_pSample_buf[check_sample_buf_ofs(a + 64)]; |
1633 | |
1634 | int b = (c_x1 >> 3) * BLOCKS_PER_MCU * 64 + (c_x1 & 7) + row_x8 + 128; |
1635 | int cb1 = m_pSample_buf[check_sample_buf_ofs(b)]; |
1636 | int cr1 = m_pSample_buf[check_sample_buf_ofs(b + 64)]; |
1637 | |
1638 | int w0 = (x & 1) ? 3 : 1; |
1639 | int w1 = (x & 1) ? 1 : 3; |
1640 | |
1641 | int cb = (cb0 * w0 + cb1 * w1 + 2) >> 2; |
1642 | int cr = (cr0 * w0 + cr1 * w1 + 2) >> 2; |
1643 | |
1644 | int rc = m_crr[cr]; |
1645 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1646 | int bc = m_cbb[cb]; |
1647 | |
1648 | d0[0] = clamp(y + rc); |
1649 | d0[1] = clamp(y + gc); |
1650 | d0[2] = clamp(y + bc); |
1651 | d0[3] = 255; |
1652 | |
1653 | d0 += 4; |
1654 | } |
1655 | } |
1656 | |
1657 | // YCbCr H2V1 (1x2:1:1, 4 m_blocks per MCU) to RGB |
1658 | void jpeg_decoder::H1V2Convert() |
1659 | { |
1660 | int row = m_max_mcu_y_size - m_mcu_lines_left; |
1661 | uint8* d0 = m_pScan_line_0; |
1662 | uint8* d1 = m_pScan_line_1; |
1663 | uint8* y; |
1664 | uint8* c; |
1665 | |
1666 | if (row < 8) |
1667 | y = m_pSample_buf + row * 8; |
1668 | else |
1669 | y = m_pSample_buf + 64 * 1 + (row & 7) * 8; |
1670 | |
1671 | c = m_pSample_buf + 64 * 2 + (row >> 1) * 8; |
1672 | |
1673 | for (int i = m_max_mcus_per_row; i > 0; i--) |
1674 | { |
1675 | for (int j = 0; j < 8; j++) |
1676 | { |
1677 | int cb = c[0 + j]; |
1678 | int cr = c[64 + j]; |
1679 | |
1680 | int rc = m_crr[cr]; |
1681 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1682 | int bc = m_cbb[cb]; |
1683 | |
1684 | int yy = y[j]; |
1685 | d0[0] = clamp(yy + rc); |
1686 | d0[1] = clamp(yy + gc); |
1687 | d0[2] = clamp(yy + bc); |
1688 | d0[3] = 255; |
1689 | |
1690 | yy = y[8 + j]; |
1691 | d1[0] = clamp(yy + rc); |
1692 | d1[1] = clamp(yy + gc); |
1693 | d1[2] = clamp(yy + bc); |
1694 | d1[3] = 255; |
1695 | |
1696 | d0 += 4; |
1697 | d1 += 4; |
1698 | } |
1699 | |
1700 | y += 64 * 4; |
1701 | c += 64 * 4; |
1702 | } |
1703 | } |
1704 | |
1705 | // YCbCr H2V1 (1x2:1:1, 4 m_blocks per MCU) to RGB |
1706 | void jpeg_decoder::H1V2ConvertFiltered() |
1707 | { |
1708 | const uint BLOCKS_PER_MCU = 4; |
1709 | int y = m_image_y_size - m_total_lines_left; |
1710 | int row = y & 15; |
1711 | |
1712 | const int half_image_y_size = (m_image_y_size >> 1) - 1; |
1713 | |
1714 | uint8* d0 = m_pScan_line_0; |
1715 | |
1716 | const int w0 = (row & 1) ? 3 : 1; |
1717 | const int w1 = (row & 1) ? 1 : 3; |
1718 | |
1719 | int c_y0 = (y - 1) >> 1; |
1720 | int c_y1 = JPGD_MIN(c_y0 + 1, half_image_y_size); |
1721 | |
1722 | const uint8_t* p_YSamples = m_pSample_buf; |
1723 | const uint8_t* p_C0Samples = m_pSample_buf; |
1724 | if ((c_y0 >= 0) && (((row & 15) == 0) || ((row & 15) == 15)) && (m_total_lines_left > 1)) |
1725 | { |
1726 | assert(y > 0); |
1727 | assert(m_sample_buf_prev_valid); |
1728 | |
1729 | if ((row & 15) == 15) |
1730 | p_YSamples = m_pSample_buf_prev; |
1731 | |
1732 | p_C0Samples = m_pSample_buf_prev; |
1733 | } |
1734 | |
1735 | const int y_sample_base_ofs = ((row & 8) ? 64 : 0) + (row & 7) * 8; |
1736 | const int y0_base = (c_y0 & 7) * 8 + 128; |
1737 | const int y1_base = (c_y1 & 7) * 8 + 128; |
1738 | |
1739 | for (int x = 0; x < m_image_x_size; x++) |
1740 | { |
1741 | const int base_ofs = (x >> 3) * BLOCKS_PER_MCU * 64 + (x & 7); |
1742 | |
1743 | int y_sample = p_YSamples[check_sample_buf_ofs(base_ofs + y_sample_base_ofs)]; |
1744 | |
1745 | int a = base_ofs + y0_base; |
1746 | int cb0_sample = p_C0Samples[check_sample_buf_ofs(a)]; |
1747 | int cr0_sample = p_C0Samples[check_sample_buf_ofs(a + 64)]; |
1748 | |
1749 | int b = base_ofs + y1_base; |
1750 | int cb1_sample = m_pSample_buf[check_sample_buf_ofs(b)]; |
1751 | int cr1_sample = m_pSample_buf[check_sample_buf_ofs(b + 64)]; |
1752 | |
1753 | int cb = (cb0_sample * w0 + cb1_sample * w1 + 2) >> 2; |
1754 | int cr = (cr0_sample * w0 + cr1_sample * w1 + 2) >> 2; |
1755 | |
1756 | int rc = m_crr[cr]; |
1757 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1758 | int bc = m_cbb[cb]; |
1759 | |
1760 | d0[0] = clamp(y_sample + rc); |
1761 | d0[1] = clamp(y_sample + gc); |
1762 | d0[2] = clamp(y_sample + bc); |
1763 | d0[3] = 255; |
1764 | |
1765 | d0 += 4; |
1766 | } |
1767 | } |
1768 | |
1769 | // YCbCr H2V2 (2x2:1:1, 6 m_blocks per MCU) to RGB |
1770 | void jpeg_decoder::H2V2Convert() |
1771 | { |
1772 | int row = m_max_mcu_y_size - m_mcu_lines_left; |
1773 | uint8* d0 = m_pScan_line_0; |
1774 | uint8* d1 = m_pScan_line_1; |
1775 | uint8* y; |
1776 | uint8* c; |
1777 | |
1778 | if (row < 8) |
1779 | y = m_pSample_buf + row * 8; |
1780 | else |
1781 | y = m_pSample_buf + 64 * 2 + (row & 7) * 8; |
1782 | |
1783 | c = m_pSample_buf + 64 * 4 + (row >> 1) * 8; |
1784 | |
1785 | for (int i = m_max_mcus_per_row; i > 0; i--) |
1786 | { |
1787 | for (int l = 0; l < 2; l++) |
1788 | { |
1789 | for (int j = 0; j < 8; j += 2) |
1790 | { |
1791 | int cb = c[0]; |
1792 | int cr = c[64]; |
1793 | |
1794 | int rc = m_crr[cr]; |
1795 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1796 | int bc = m_cbb[cb]; |
1797 | |
1798 | int yy = y[j]; |
1799 | d0[0] = clamp(yy + rc); |
1800 | d0[1] = clamp(yy + gc); |
1801 | d0[2] = clamp(yy + bc); |
1802 | d0[3] = 255; |
1803 | |
1804 | yy = y[j + 1]; |
1805 | d0[4] = clamp(yy + rc); |
1806 | d0[5] = clamp(yy + gc); |
1807 | d0[6] = clamp(yy + bc); |
1808 | d0[7] = 255; |
1809 | |
1810 | yy = y[j + 8]; |
1811 | d1[0] = clamp(yy + rc); |
1812 | d1[1] = clamp(yy + gc); |
1813 | d1[2] = clamp(yy + bc); |
1814 | d1[3] = 255; |
1815 | |
1816 | yy = y[j + 8 + 1]; |
1817 | d1[4] = clamp(yy + rc); |
1818 | d1[5] = clamp(yy + gc); |
1819 | d1[6] = clamp(yy + bc); |
1820 | d1[7] = 255; |
1821 | |
1822 | d0 += 8; |
1823 | d1 += 8; |
1824 | |
1825 | c++; |
1826 | } |
1827 | y += 64; |
1828 | } |
1829 | |
1830 | y += 64 * 6 - 64 * 2; |
1831 | c += 64 * 6 - 8; |
1832 | } |
1833 | } |
1834 | |
1835 | uint32_t jpeg_decoder::H2V2ConvertFiltered() |
1836 | { |
1837 | const uint BLOCKS_PER_MCU = 6; |
1838 | int y = m_image_y_size - m_total_lines_left; |
1839 | int row = y & 15; |
1840 | |
1841 | const int half_image_y_size = (m_image_y_size >> 1) - 1; |
1842 | |
1843 | uint8* d0 = m_pScan_line_0; |
1844 | |
1845 | int c_y0 = (y - 1) >> 1; |
1846 | int c_y1 = JPGD_MIN(c_y0 + 1, half_image_y_size); |
1847 | |
1848 | const uint8_t* p_YSamples = m_pSample_buf; |
1849 | const uint8_t* p_C0Samples = m_pSample_buf; |
1850 | if ((c_y0 >= 0) && (((row & 15) == 0) || ((row & 15) == 15)) && (m_total_lines_left > 1)) |
1851 | { |
1852 | assert(y > 0); |
1853 | assert(m_sample_buf_prev_valid); |
1854 | |
1855 | if ((row & 15) == 15) |
1856 | p_YSamples = m_pSample_buf_prev; |
1857 | |
1858 | p_C0Samples = m_pSample_buf_prev; |
1859 | } |
1860 | |
1861 | const int y_sample_base_ofs = ((row & 8) ? 128 : 0) + (row & 7) * 8; |
1862 | const int y0_base = (c_y0 & 7) * 8 + 256; |
1863 | const int y1_base = (c_y1 & 7) * 8 + 256; |
1864 | |
1865 | const int half_image_x_size = (m_image_x_size >> 1) - 1; |
1866 | |
1867 | static const uint8_t s_muls[2][2][4] = |
1868 | { |
1869 | { { 1, 3, 3, 9 }, { 3, 9, 1, 3 }, }, |
1870 | { { 3, 1, 9, 3 }, { 9, 3, 3, 1 } } |
1871 | }; |
1872 | |
1873 | if (((row & 15) >= 1) && ((row & 15) <= 14)) |
1874 | { |
1875 | assert((row & 1) == 1); |
1876 | assert(((y + 1 - 1) >> 1) == c_y0); |
1877 | |
1878 | assert(p_YSamples == m_pSample_buf); |
1879 | assert(p_C0Samples == m_pSample_buf); |
1880 | |
1881 | uint8* d1 = m_pScan_line_1; |
1882 | const int y_sample_base_ofs1 = (((row + 1) & 8) ? 128 : 0) + ((row + 1) & 7) * 8; |
1883 | |
1884 | for (int x = 0; x < m_image_x_size; x++) |
1885 | { |
1886 | int k = (x >> 4) * BLOCKS_PER_MCU * 64 + ((x & 8) ? 64 : 0) + (x & 7); |
1887 | int y_sample0 = p_YSamples[check_sample_buf_ofs(k + y_sample_base_ofs)]; |
1888 | int y_sample1 = p_YSamples[check_sample_buf_ofs(k + y_sample_base_ofs1)]; |
1889 | |
1890 | int c_x0 = (x - 1) >> 1; |
1891 | int c_x1 = JPGD_MIN(c_x0 + 1, half_image_x_size); |
1892 | c_x0 = JPGD_MAX(c_x0, 0); |
1893 | |
1894 | int a = (c_x0 >> 3) * BLOCKS_PER_MCU * 64 + (c_x0 & 7); |
1895 | int cb00_sample = p_C0Samples[check_sample_buf_ofs(a + y0_base)]; |
1896 | int cr00_sample = p_C0Samples[check_sample_buf_ofs(a + y0_base + 64)]; |
1897 | |
1898 | int cb01_sample = m_pSample_buf[check_sample_buf_ofs(a + y1_base)]; |
1899 | int cr01_sample = m_pSample_buf[check_sample_buf_ofs(a + y1_base + 64)]; |
1900 | |
1901 | int b = (c_x1 >> 3) * BLOCKS_PER_MCU * 64 + (c_x1 & 7); |
1902 | int cb10_sample = p_C0Samples[check_sample_buf_ofs(b + y0_base)]; |
1903 | int cr10_sample = p_C0Samples[check_sample_buf_ofs(b + y0_base + 64)]; |
1904 | |
1905 | int cb11_sample = m_pSample_buf[check_sample_buf_ofs(b + y1_base)]; |
1906 | int cr11_sample = m_pSample_buf[check_sample_buf_ofs(b + y1_base + 64)]; |
1907 | |
1908 | { |
1909 | const uint8_t* pMuls = &s_muls[row & 1][x & 1][0]; |
1910 | int cb = (cb00_sample * pMuls[0] + cb01_sample * pMuls[1] + cb10_sample * pMuls[2] + cb11_sample * pMuls[3] + 8) >> 4; |
1911 | int cr = (cr00_sample * pMuls[0] + cr01_sample * pMuls[1] + cr10_sample * pMuls[2] + cr11_sample * pMuls[3] + 8) >> 4; |
1912 | |
1913 | int rc = m_crr[cr]; |
1914 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1915 | int bc = m_cbb[cb]; |
1916 | |
1917 | d0[0] = clamp(y_sample0 + rc); |
1918 | d0[1] = clamp(y_sample0 + gc); |
1919 | d0[2] = clamp(y_sample0 + bc); |
1920 | d0[3] = 255; |
1921 | |
1922 | d0 += 4; |
1923 | } |
1924 | |
1925 | { |
1926 | const uint8_t* pMuls = &s_muls[(row + 1) & 1][x & 1][0]; |
1927 | int cb = (cb00_sample * pMuls[0] + cb01_sample * pMuls[1] + cb10_sample * pMuls[2] + cb11_sample * pMuls[3] + 8) >> 4; |
1928 | int cr = (cr00_sample * pMuls[0] + cr01_sample * pMuls[1] + cr10_sample * pMuls[2] + cr11_sample * pMuls[3] + 8) >> 4; |
1929 | |
1930 | int rc = m_crr[cr]; |
1931 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1932 | int bc = m_cbb[cb]; |
1933 | |
1934 | d1[0] = clamp(y_sample1 + rc); |
1935 | d1[1] = clamp(y_sample1 + gc); |
1936 | d1[2] = clamp(y_sample1 + bc); |
1937 | d1[3] = 255; |
1938 | |
1939 | d1 += 4; |
1940 | } |
1941 | |
1942 | if (((x & 1) == 1) && (x < m_image_x_size - 1)) |
1943 | { |
1944 | const int nx = x + 1; |
1945 | assert(c_x0 == (nx - 1) >> 1); |
1946 | |
1947 | k = (nx >> 4) * BLOCKS_PER_MCU * 64 + ((nx & 8) ? 64 : 0) + (nx & 7); |
1948 | y_sample0 = p_YSamples[check_sample_buf_ofs(k + y_sample_base_ofs)]; |
1949 | y_sample1 = p_YSamples[check_sample_buf_ofs(k + y_sample_base_ofs1)]; |
1950 | |
1951 | { |
1952 | const uint8_t* pMuls = &s_muls[row & 1][nx & 1][0]; |
1953 | int cb = (cb00_sample * pMuls[0] + cb01_sample * pMuls[1] + cb10_sample * pMuls[2] + cb11_sample * pMuls[3] + 8) >> 4; |
1954 | int cr = (cr00_sample * pMuls[0] + cr01_sample * pMuls[1] + cr10_sample * pMuls[2] + cr11_sample * pMuls[3] + 8) >> 4; |
1955 | |
1956 | int rc = m_crr[cr]; |
1957 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1958 | int bc = m_cbb[cb]; |
1959 | |
1960 | d0[0] = clamp(y_sample0 + rc); |
1961 | d0[1] = clamp(y_sample0 + gc); |
1962 | d0[2] = clamp(y_sample0 + bc); |
1963 | d0[3] = 255; |
1964 | |
1965 | d0 += 4; |
1966 | } |
1967 | |
1968 | { |
1969 | const uint8_t* pMuls = &s_muls[(row + 1) & 1][nx & 1][0]; |
1970 | int cb = (cb00_sample * pMuls[0] + cb01_sample * pMuls[1] + cb10_sample * pMuls[2] + cb11_sample * pMuls[3] + 8) >> 4; |
1971 | int cr = (cr00_sample * pMuls[0] + cr01_sample * pMuls[1] + cr10_sample * pMuls[2] + cr11_sample * pMuls[3] + 8) >> 4; |
1972 | |
1973 | int rc = m_crr[cr]; |
1974 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
1975 | int bc = m_cbb[cb]; |
1976 | |
1977 | d1[0] = clamp(y_sample1 + rc); |
1978 | d1[1] = clamp(y_sample1 + gc); |
1979 | d1[2] = clamp(y_sample1 + bc); |
1980 | d1[3] = 255; |
1981 | |
1982 | d1 += 4; |
1983 | } |
1984 | |
1985 | ++x; |
1986 | } |
1987 | } |
1988 | |
1989 | return 2; |
1990 | } |
1991 | else |
1992 | { |
1993 | for (int x = 0; x < m_image_x_size; x++) |
1994 | { |
1995 | int y_sample = p_YSamples[check_sample_buf_ofs((x >> 4) * BLOCKS_PER_MCU * 64 + ((x & 8) ? 64 : 0) + (x & 7) + y_sample_base_ofs)]; |
1996 | |
1997 | int c_x0 = (x - 1) >> 1; |
1998 | int c_x1 = JPGD_MIN(c_x0 + 1, half_image_x_size); |
1999 | c_x0 = JPGD_MAX(c_x0, 0); |
2000 | |
2001 | int a = (c_x0 >> 3) * BLOCKS_PER_MCU * 64 + (c_x0 & 7); |
2002 | int cb00_sample = p_C0Samples[check_sample_buf_ofs(a + y0_base)]; |
2003 | int cr00_sample = p_C0Samples[check_sample_buf_ofs(a + y0_base + 64)]; |
2004 | |
2005 | int cb01_sample = m_pSample_buf[check_sample_buf_ofs(a + y1_base)]; |
2006 | int cr01_sample = m_pSample_buf[check_sample_buf_ofs(a + y1_base + 64)]; |
2007 | |
2008 | int b = (c_x1 >> 3) * BLOCKS_PER_MCU * 64 + (c_x1 & 7); |
2009 | int cb10_sample = p_C0Samples[check_sample_buf_ofs(b + y0_base)]; |
2010 | int cr10_sample = p_C0Samples[check_sample_buf_ofs(b + y0_base + 64)]; |
2011 | |
2012 | int cb11_sample = m_pSample_buf[check_sample_buf_ofs(b + y1_base)]; |
2013 | int cr11_sample = m_pSample_buf[check_sample_buf_ofs(b + y1_base + 64)]; |
2014 | |
2015 | const uint8_t* pMuls = &s_muls[row & 1][x & 1][0]; |
2016 | int cb = (cb00_sample * pMuls[0] + cb01_sample * pMuls[1] + cb10_sample * pMuls[2] + cb11_sample * pMuls[3] + 8) >> 4; |
2017 | int cr = (cr00_sample * pMuls[0] + cr01_sample * pMuls[1] + cr10_sample * pMuls[2] + cr11_sample * pMuls[3] + 8) >> 4; |
2018 | |
2019 | int rc = m_crr[cr]; |
2020 | int gc = ((m_crg[cr] + m_cbg[cb]) >> 16); |
2021 | int bc = m_cbb[cb]; |
2022 | |
2023 | d0[0] = clamp(y_sample + rc); |
2024 | d0[1] = clamp(y_sample + gc); |
2025 | d0[2] = clamp(y_sample + bc); |
2026 | d0[3] = 255; |
2027 | |
2028 | d0 += 4; |
2029 | } |
2030 | |
2031 | return 1; |
2032 | } |
2033 | } |
2034 | |
2035 | // Y (1 block per MCU) to 8-bit grayscale |
2036 | void jpeg_decoder::gray_convert() |
2037 | { |
2038 | int row = m_max_mcu_y_size - m_mcu_lines_left; |
2039 | uint8* d = m_pScan_line_0; |
2040 | uint8* s = m_pSample_buf + row * 8; |
2041 | |
2042 | for (int i = m_max_mcus_per_row; i > 0; i--) |
2043 | { |
2044 | *(uint*)d = *(uint*)s; |
2045 | *(uint*)(&d[4]) = *(uint*)(&s[4]); |
2046 | |
2047 | s += 64; |
2048 | d += 8; |
2049 | } |
2050 | } |
2051 | |
2052 | // Find end of image (EOI) marker, so we can return to the user the exact size of the input stream. |
2053 | void jpeg_decoder::find_eoi() |
2054 | { |
2055 | if (!m_progressive_flag) |
2056 | { |
2057 | // Attempt to read the EOI marker. |
2058 | //get_bits_no_markers(m_bits_left & 7); |
2059 | |
2060 | // Prime the bit buffer |
2061 | m_bits_left = 16; |
2062 | get_bits(16); |
2063 | get_bits(16); |
2064 | |
2065 | // The next marker _should_ be EOI |
2066 | process_markers(); |
2067 | } |
2068 | |
2069 | m_total_bytes_read -= m_in_buf_left; |
2070 | } |
2071 | |
2072 | int jpeg_decoder::decode_next_mcu_row() |
2073 | { |
2074 | if (setjmp(m_jmp_state)) |
2075 | return JPGD_FAILED; |
2076 | |
2077 | const bool chroma_y_filtering = (m_flags & cFlagLinearChromaFiltering) && ((m_scan_type == JPGD_YH2V2) || (m_scan_type == JPGD_YH1V2)) && (m_image_x_size >= 2) && (m_image_y_size >= 2); |
2078 | if (chroma_y_filtering) |
2079 | { |
2080 | std::swap(m_pSample_buf, m_pSample_buf_prev); |
2081 | |
2082 | m_sample_buf_prev_valid = true; |
2083 | } |
2084 | |
2085 | if (m_progressive_flag) |
2086 | load_next_row(); |
2087 | else |
2088 | decode_next_row(); |
2089 | |
2090 | // Find the EOI marker if that was the last row. |
2091 | if (m_total_lines_left <= m_max_mcu_y_size) |
2092 | find_eoi(); |
2093 | |
2094 | m_mcu_lines_left = m_max_mcu_y_size; |
2095 | return 0; |
2096 | } |
2097 | |
2098 | int jpeg_decoder::decode(const void** pScan_line, uint* pScan_line_len) |
2099 | { |
2100 | if ((m_error_code) || (!m_ready_flag)) |
2101 | return JPGD_FAILED; |
2102 | |
2103 | if (m_total_lines_left == 0) |
2104 | return JPGD_DONE; |
2105 | |
2106 | const bool chroma_y_filtering = (m_flags & cFlagLinearChromaFiltering) && ((m_scan_type == JPGD_YH2V2) || (m_scan_type == JPGD_YH1V2)) && (m_image_x_size >= 2) && (m_image_y_size >= 2); |
2107 | |
2108 | bool get_another_mcu_row = false; |
2109 | bool got_mcu_early = false; |
2110 | if (chroma_y_filtering) |
2111 | { |
2112 | if (m_total_lines_left == m_image_y_size) |
2113 | get_another_mcu_row = true; |
2114 | else if ((m_mcu_lines_left == 1) && (m_total_lines_left > 1)) |
2115 | { |
2116 | get_another_mcu_row = true; |
2117 | got_mcu_early = true; |
2118 | } |
2119 | } |
2120 | else |
2121 | { |
2122 | get_another_mcu_row = (m_mcu_lines_left == 0); |
2123 | } |
2124 | |
2125 | if (get_another_mcu_row) |
2126 | { |
2127 | int status = decode_next_mcu_row(); |
2128 | if (status != 0) |
2129 | return status; |
2130 | } |
2131 | |
2132 | switch (m_scan_type) |
2133 | { |
2134 | case JPGD_YH2V2: |
2135 | { |
2136 | if ((m_flags & cFlagLinearChromaFiltering) && (m_image_x_size >= 2) && (m_image_y_size >= 2)) |
2137 | { |
2138 | if (m_num_buffered_scanlines == 1) |
2139 | { |
2140 | *pScan_line = m_pScan_line_1; |
2141 | } |
2142 | else if (m_num_buffered_scanlines == 0) |
2143 | { |
2144 | m_num_buffered_scanlines = H2V2ConvertFiltered(); |
2145 | *pScan_line = m_pScan_line_0; |
2146 | } |
2147 | |
2148 | m_num_buffered_scanlines--; |
2149 | } |
2150 | else |
2151 | { |
2152 | if ((m_mcu_lines_left & 1) == 0) |
2153 | { |
2154 | H2V2Convert(); |
2155 | *pScan_line = m_pScan_line_0; |
2156 | } |
2157 | else |
2158 | *pScan_line = m_pScan_line_1; |
2159 | } |
2160 | |
2161 | break; |
2162 | } |
2163 | case JPGD_YH2V1: |
2164 | { |
2165 | if ((m_flags & cFlagLinearChromaFiltering) && (m_image_x_size >= 2) && (m_image_y_size >= 2)) |
2166 | H2V1ConvertFiltered(); |
2167 | else |
2168 | H2V1Convert(); |
2169 | *pScan_line = m_pScan_line_0; |
2170 | break; |
2171 | } |
2172 | case JPGD_YH1V2: |
2173 | { |
2174 | if (chroma_y_filtering) |
2175 | { |
2176 | H1V2ConvertFiltered(); |
2177 | *pScan_line = m_pScan_line_0; |
2178 | } |
2179 | else |
2180 | { |
2181 | if ((m_mcu_lines_left & 1) == 0) |
2182 | { |
2183 | H1V2Convert(); |
2184 | *pScan_line = m_pScan_line_0; |
2185 | } |
2186 | else |
2187 | *pScan_line = m_pScan_line_1; |
2188 | } |
2189 | |
2190 | break; |
2191 | } |
2192 | case JPGD_YH1V1: |
2193 | { |
2194 | H1V1Convert(); |
2195 | *pScan_line = m_pScan_line_0; |
2196 | break; |
2197 | } |
2198 | case JPGD_GRAYSCALE: |
2199 | { |
2200 | gray_convert(); |
2201 | *pScan_line = m_pScan_line_0; |
2202 | |
2203 | break; |
2204 | } |
2205 | } |
2206 | |
2207 | *pScan_line_len = m_real_dest_bytes_per_scan_line; |
2208 | |
2209 | if (!got_mcu_early) |
2210 | { |
2211 | m_mcu_lines_left--; |
2212 | } |
2213 | |
2214 | m_total_lines_left--; |
2215 | |
2216 | return JPGD_SUCCESS; |
2217 | } |
2218 | |
2219 | // Creates the tables needed for efficient Huffman decoding. |
2220 | void jpeg_decoder::make_huff_table(int index, huff_tables* pH) |
2221 | { |
2222 | int p, i, l, si; |
2223 | uint8 huffsize[258]; |
2224 | uint huffcode[258]; |
2225 | uint code; |
2226 | uint subtree; |
2227 | int code_size; |
2228 | int lastp; |
2229 | int nextfreeentry; |
2230 | int currententry; |
2231 | |
2232 | pH->ac_table = m_huff_ac[index] != 0; |
2233 | |
2234 | p = 0; |
2235 | |
2236 | for (l = 1; l <= 16; l++) |
2237 | { |
2238 | for (i = 1; i <= m_huff_num[index][l]; i++) |
2239 | { |
2240 | if (p >= 257) |
2241 | stop_decoding(JPGD_DECODE_ERROR); |
2242 | huffsize[p++] = static_cast<uint8>(l); |
2243 | } |
2244 | } |
2245 | |
2246 | assert(p < 258); |
2247 | huffsize[p] = 0; |
2248 | |
2249 | lastp = p; |
2250 | |
2251 | code = 0; |
2252 | si = huffsize[0]; |
2253 | p = 0; |
2254 | |
2255 | while (huffsize[p]) |
2256 | { |
2257 | while (huffsize[p] == si) |
2258 | { |
2259 | if (p >= 257) |
2260 | stop_decoding(JPGD_DECODE_ERROR); |
2261 | huffcode[p++] = code; |
2262 | code++; |
2263 | } |
2264 | |
2265 | code <<= 1; |
2266 | si++; |
2267 | } |
2268 | |
2269 | memset(pH->look_up, 0, sizeof(pH->look_up)); |
2270 | memset(pH->look_up2, 0, sizeof(pH->look_up2)); |
2271 | memset(pH->tree, 0, sizeof(pH->tree)); |
2272 | memset(pH->code_size, 0, sizeof(pH->code_size)); |
2273 | |
2274 | nextfreeentry = -1; |
2275 | |
2276 | p = 0; |
2277 | |
2278 | while (p < lastp) |
2279 | { |
2280 | i = m_huff_val[index][p]; |
2281 | |
2282 | code = huffcode[p]; |
2283 | code_size = huffsize[p]; |
2284 | |
2285 | assert(i < JPGD_HUFF_CODE_SIZE_MAX_LENGTH); |
2286 | pH->code_size[i] = static_cast<uint8>(code_size); |
2287 | |
2288 | if (code_size <= 8) |
2289 | { |
2290 | code <<= (8 - code_size); |
2291 | |
2292 | for (l = 1 << (8 - code_size); l > 0; l--) |
2293 | { |
2294 | if (code >= 256) |
2295 | stop_decoding(JPGD_DECODE_ERROR); |
2296 | |
2297 | pH->look_up[code] = i; |
2298 | |
2299 | bool = false; |
2300 | int = 0; |
2301 | int = i & 15; |
2302 | |
2303 | int bits_to_fetch = code_size; |
2304 | if (num_extra_bits) |
2305 | { |
2306 | int total_codesize = code_size + num_extra_bits; |
2307 | if (total_codesize <= 8) |
2308 | { |
2309 | has_extrabits = true; |
2310 | extra_bits = ((1 << num_extra_bits) - 1) & (code >> (8 - total_codesize)); |
2311 | |
2312 | if (extra_bits > 0x7FFF) |
2313 | stop_decoding(JPGD_DECODE_ERROR); |
2314 | |
2315 | bits_to_fetch += num_extra_bits; |
2316 | } |
2317 | } |
2318 | |
2319 | if (!has_extrabits) |
2320 | pH->look_up2[code] = i | (bits_to_fetch << 8); |
2321 | else |
2322 | pH->look_up2[code] = i | 0x8000 | (extra_bits << 16) | (bits_to_fetch << 8); |
2323 | |
2324 | code++; |
2325 | } |
2326 | } |
2327 | else |
2328 | { |
2329 | subtree = (code >> (code_size - 8)) & 0xFF; |
2330 | |
2331 | currententry = pH->look_up[subtree]; |
2332 | |
2333 | if (currententry == 0) |
2334 | { |
2335 | pH->look_up[subtree] = currententry = nextfreeentry; |
2336 | pH->look_up2[subtree] = currententry = nextfreeentry; |
2337 | |
2338 | nextfreeentry -= 2; |
2339 | } |
2340 | |
2341 | code <<= (16 - (code_size - 8)); |
2342 | |
2343 | for (l = code_size; l > 9; l--) |
2344 | { |
2345 | if ((code & 0x8000) == 0) |
2346 | currententry--; |
2347 | |
2348 | unsigned int idx = -currententry - 1; |
2349 | |
2350 | if (idx >= JPGD_HUFF_TREE_MAX_LENGTH) |
2351 | stop_decoding(JPGD_DECODE_ERROR); |
2352 | |
2353 | if (pH->tree[idx] == 0) |
2354 | { |
2355 | pH->tree[idx] = nextfreeentry; |
2356 | |
2357 | currententry = nextfreeentry; |
2358 | |
2359 | nextfreeentry -= 2; |
2360 | } |
2361 | else |
2362 | { |
2363 | currententry = pH->tree[idx]; |
2364 | } |
2365 | |
2366 | code <<= 1; |
2367 | } |
2368 | |
2369 | if ((code & 0x8000) == 0) |
2370 | currententry--; |
2371 | |
2372 | if ((-currententry - 1) >= JPGD_HUFF_TREE_MAX_LENGTH) |
2373 | stop_decoding(JPGD_DECODE_ERROR); |
2374 | |
2375 | pH->tree[-currententry - 1] = i; |
2376 | } |
2377 | |
2378 | p++; |
2379 | } |
2380 | } |
2381 | |
2382 | // Verifies the quantization tables needed for this scan are available. |
2383 | void jpeg_decoder::check_quant_tables() |
2384 | { |
2385 | for (int i = 0; i < m_comps_in_scan; i++) |
2386 | if (m_quant[m_comp_quant[m_comp_list[i]]] == nullptr) |
2387 | stop_decoding(JPGD_UNDEFINED_QUANT_TABLE); |
2388 | } |
2389 | |
2390 | // Verifies that all the Huffman tables needed for this scan are available. |
2391 | void jpeg_decoder::check_huff_tables() |
2392 | { |
2393 | for (int i = 0; i < m_comps_in_scan; i++) |
2394 | { |
2395 | if ((m_spectral_start == 0) && (m_huff_num[m_comp_dc_tab[m_comp_list[i]]] == nullptr)) |
2396 | stop_decoding(JPGD_UNDEFINED_HUFF_TABLE); |
2397 | |
2398 | if ((m_spectral_end > 0) && (m_huff_num[m_comp_ac_tab[m_comp_list[i]]] == nullptr)) |
2399 | stop_decoding(JPGD_UNDEFINED_HUFF_TABLE); |
2400 | } |
2401 | |
2402 | for (int i = 0; i < JPGD_MAX_HUFF_TABLES; i++) |
2403 | if (m_huff_num[i]) |
2404 | { |
2405 | if (!m_pHuff_tabs[i]) |
2406 | m_pHuff_tabs[i] = (huff_tables*)alloc(sizeof(huff_tables)); |
2407 | |
2408 | make_huff_table(i, m_pHuff_tabs[i]); |
2409 | } |
2410 | } |
2411 | |
2412 | // Determines the component order inside each MCU. |
2413 | // Also calcs how many MCU's are on each row, etc. |
2414 | bool jpeg_decoder::calc_mcu_block_order() |
2415 | { |
2416 | int component_num, component_id; |
2417 | int max_h_samp = 0, max_v_samp = 0; |
2418 | |
2419 | for (component_id = 0; component_id < m_comps_in_frame; component_id++) |
2420 | { |
2421 | if (m_comp_h_samp[component_id] > max_h_samp) |
2422 | max_h_samp = m_comp_h_samp[component_id]; |
2423 | |
2424 | if (m_comp_v_samp[component_id] > max_v_samp) |
2425 | max_v_samp = m_comp_v_samp[component_id]; |
2426 | } |
2427 | |
2428 | for (component_id = 0; component_id < m_comps_in_frame; component_id++) |
2429 | { |
2430 | m_comp_h_blocks[component_id] = ((((m_image_x_size * m_comp_h_samp[component_id]) + (max_h_samp - 1)) / max_h_samp) + 7) / 8; |
2431 | m_comp_v_blocks[component_id] = ((((m_image_y_size * m_comp_v_samp[component_id]) + (max_v_samp - 1)) / max_v_samp) + 7) / 8; |
2432 | } |
2433 | |
2434 | if (m_comps_in_scan == 1) |
2435 | { |
2436 | m_mcus_per_row = m_comp_h_blocks[m_comp_list[0]]; |
2437 | m_mcus_per_col = m_comp_v_blocks[m_comp_list[0]]; |
2438 | } |
2439 | else |
2440 | { |
2441 | m_mcus_per_row = (((m_image_x_size + 7) / 8) + (max_h_samp - 1)) / max_h_samp; |
2442 | m_mcus_per_col = (((m_image_y_size + 7) / 8) + (max_v_samp - 1)) / max_v_samp; |
2443 | } |
2444 | |
2445 | if (m_comps_in_scan == 1) |
2446 | { |
2447 | m_mcu_org[0] = m_comp_list[0]; |
2448 | |
2449 | m_blocks_per_mcu = 1; |
2450 | } |
2451 | else |
2452 | { |
2453 | m_blocks_per_mcu = 0; |
2454 | |
2455 | for (component_num = 0; component_num < m_comps_in_scan; component_num++) |
2456 | { |
2457 | int num_blocks; |
2458 | |
2459 | component_id = m_comp_list[component_num]; |
2460 | |
2461 | num_blocks = m_comp_h_samp[component_id] * m_comp_v_samp[component_id]; |
2462 | |
2463 | while (num_blocks--) |
2464 | m_mcu_org[m_blocks_per_mcu++] = component_id; |
2465 | } |
2466 | } |
2467 | |
2468 | if (m_blocks_per_mcu > m_max_blocks_per_mcu) |
2469 | return false; |
2470 | |
2471 | for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++) |
2472 | { |
2473 | int comp_id = m_mcu_org[mcu_block]; |
2474 | if (comp_id >= JPGD_MAX_QUANT_TABLES) |
2475 | return false; |
2476 | } |
2477 | |
2478 | return true; |
2479 | } |
2480 | |
2481 | // Starts a new scan. |
2482 | int jpeg_decoder::init_scan() |
2483 | { |
2484 | if (!locate_sos_marker()) |
2485 | return JPGD_FALSE; |
2486 | |
2487 | if (!calc_mcu_block_order()) |
2488 | return JPGD_FALSE; |
2489 | |
2490 | check_huff_tables(); |
2491 | |
2492 | check_quant_tables(); |
2493 | |
2494 | memset(m_last_dc_val, 0, m_comps_in_frame * sizeof(uint)); |
2495 | |
2496 | m_eob_run = 0; |
2497 | |
2498 | if (m_restart_interval) |
2499 | { |
2500 | m_restarts_left = m_restart_interval; |
2501 | m_next_restart_num = 0; |
2502 | } |
2503 | |
2504 | fix_in_buffer(); |
2505 | |
2506 | return JPGD_TRUE; |
2507 | } |
2508 | |
2509 | // Starts a frame. Determines if the number of components or sampling factors |
2510 | // are supported. |
2511 | void jpeg_decoder::init_frame() |
2512 | { |
2513 | int i; |
2514 | |
2515 | if (m_comps_in_frame == 1) |
2516 | { |
2517 | if ((m_comp_h_samp[0] != 1) || (m_comp_v_samp[0] != 1)) |
2518 | stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS); |
2519 | |
2520 | m_scan_type = JPGD_GRAYSCALE; |
2521 | m_max_blocks_per_mcu = 1; |
2522 | m_max_mcu_x_size = 8; |
2523 | m_max_mcu_y_size = 8; |
2524 | } |
2525 | else if (m_comps_in_frame == 3) |
2526 | { |
2527 | if (((m_comp_h_samp[1] != 1) || (m_comp_v_samp[1] != 1)) || |
2528 | ((m_comp_h_samp[2] != 1) || (m_comp_v_samp[2] != 1))) |
2529 | stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS); |
2530 | |
2531 | if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 1)) |
2532 | { |
2533 | m_scan_type = JPGD_YH1V1; |
2534 | |
2535 | m_max_blocks_per_mcu = 3; |
2536 | m_max_mcu_x_size = 8; |
2537 | m_max_mcu_y_size = 8; |
2538 | } |
2539 | else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 1)) |
2540 | { |
2541 | m_scan_type = JPGD_YH2V1; |
2542 | m_max_blocks_per_mcu = 4; |
2543 | m_max_mcu_x_size = 16; |
2544 | m_max_mcu_y_size = 8; |
2545 | } |
2546 | else if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 2)) |
2547 | { |
2548 | m_scan_type = JPGD_YH1V2; |
2549 | m_max_blocks_per_mcu = 4; |
2550 | m_max_mcu_x_size = 8; |
2551 | m_max_mcu_y_size = 16; |
2552 | } |
2553 | else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 2)) |
2554 | { |
2555 | m_scan_type = JPGD_YH2V2; |
2556 | m_max_blocks_per_mcu = 6; |
2557 | m_max_mcu_x_size = 16; |
2558 | m_max_mcu_y_size = 16; |
2559 | } |
2560 | else |
2561 | stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS); |
2562 | } |
2563 | else |
2564 | stop_decoding(JPGD_UNSUPPORTED_COLORSPACE); |
2565 | |
2566 | m_max_mcus_per_row = (m_image_x_size + (m_max_mcu_x_size - 1)) / m_max_mcu_x_size; |
2567 | m_max_mcus_per_col = (m_image_y_size + (m_max_mcu_y_size - 1)) / m_max_mcu_y_size; |
2568 | |
2569 | // These values are for the *destination* pixels: after conversion. |
2570 | if (m_scan_type == JPGD_GRAYSCALE) |
2571 | m_dest_bytes_per_pixel = 1; |
2572 | else |
2573 | m_dest_bytes_per_pixel = 4; |
2574 | |
2575 | m_dest_bytes_per_scan_line = ((m_image_x_size + 15) & 0xFFF0) * m_dest_bytes_per_pixel; |
2576 | |
2577 | m_real_dest_bytes_per_scan_line = (m_image_x_size * m_dest_bytes_per_pixel); |
2578 | |
2579 | // Initialize two scan line buffers. |
2580 | m_pScan_line_0 = (uint8*)alloc(m_dest_bytes_per_scan_line, true); |
2581 | if ((m_scan_type == JPGD_YH1V2) || (m_scan_type == JPGD_YH2V2)) |
2582 | m_pScan_line_1 = (uint8*)alloc(m_dest_bytes_per_scan_line, true); |
2583 | |
2584 | m_max_blocks_per_row = m_max_mcus_per_row * m_max_blocks_per_mcu; |
2585 | |
2586 | // Should never happen |
2587 | if (m_max_blocks_per_row > JPGD_MAX_BLOCKS_PER_ROW) |
2588 | stop_decoding(JPGD_DECODE_ERROR); |
2589 | |
2590 | // Allocate the coefficient buffer, enough for one MCU |
2591 | m_pMCU_coefficients = (jpgd_block_t*)alloc(m_max_blocks_per_mcu * 64 * sizeof(jpgd_block_t)); |
2592 | |
2593 | for (i = 0; i < m_max_blocks_per_mcu; i++) |
2594 | m_mcu_block_max_zag[i] = 64; |
2595 | |
2596 | m_pSample_buf = (uint8*)alloc(m_max_blocks_per_row * 64); |
2597 | m_pSample_buf_prev = (uint8*)alloc(m_max_blocks_per_row * 64); |
2598 | |
2599 | m_total_lines_left = m_image_y_size; |
2600 | |
2601 | m_mcu_lines_left = 0; |
2602 | |
2603 | create_look_ups(); |
2604 | } |
2605 | |
2606 | // The coeff_buf series of methods originally stored the coefficients |
2607 | // into a "virtual" file which was located in EMS, XMS, or a disk file. A cache |
2608 | // was used to make this process more efficient. Now, we can store the entire |
2609 | // thing in RAM. |
2610 | jpeg_decoder::coeff_buf* jpeg_decoder::coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y) |
2611 | { |
2612 | coeff_buf* cb = (coeff_buf*)alloc(sizeof(coeff_buf)); |
2613 | |
2614 | cb->block_num_x = block_num_x; |
2615 | cb->block_num_y = block_num_y; |
2616 | cb->block_len_x = block_len_x; |
2617 | cb->block_len_y = block_len_y; |
2618 | cb->block_size = (block_len_x * block_len_y) * sizeof(jpgd_block_t); |
2619 | cb->pData = (uint8*)alloc(cb->block_size * block_num_x * block_num_y, true); |
2620 | return cb; |
2621 | } |
2622 | |
2623 | inline jpgd_block_t* jpeg_decoder::coeff_buf_getp(coeff_buf* cb, int block_x, int block_y) |
2624 | { |
2625 | if ((block_x >= cb->block_num_x) || (block_y >= cb->block_num_y)) |
2626 | stop_decoding(JPGD_DECODE_ERROR); |
2627 | |
2628 | return (jpgd_block_t*)(cb->pData + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x)); |
2629 | } |
2630 | |
2631 | // The following methods decode the various types of m_blocks encountered |
2632 | // in progressively encoded images. |
2633 | void jpeg_decoder::decode_block_dc_first(jpeg_decoder* pD, int component_id, int block_x, int block_y) |
2634 | { |
2635 | int s, r; |
2636 | jpgd_block_t* p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y); |
2637 | |
2638 | if ((s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_dc_tab[component_id]])) != 0) |
2639 | { |
2640 | if (s >= 16) |
2641 | pD->stop_decoding(JPGD_DECODE_ERROR); |
2642 | |
2643 | r = pD->get_bits_no_markers(s); |
2644 | s = JPGD_HUFF_EXTEND(r, s); |
2645 | } |
2646 | |
2647 | pD->m_last_dc_val[component_id] = (s += pD->m_last_dc_val[component_id]); |
2648 | |
2649 | p[0] = static_cast<jpgd_block_t>(s << pD->m_successive_low); |
2650 | } |
2651 | |
2652 | void jpeg_decoder::decode_block_dc_refine(jpeg_decoder* pD, int component_id, int block_x, int block_y) |
2653 | { |
2654 | if (pD->get_bits_no_markers(1)) |
2655 | { |
2656 | jpgd_block_t* p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y); |
2657 | |
2658 | p[0] |= (1 << pD->m_successive_low); |
2659 | } |
2660 | } |
2661 | |
2662 | void jpeg_decoder::decode_block_ac_first(jpeg_decoder* pD, int component_id, int block_x, int block_y) |
2663 | { |
2664 | int k, s, r; |
2665 | |
2666 | if (pD->m_eob_run) |
2667 | { |
2668 | pD->m_eob_run--; |
2669 | return; |
2670 | } |
2671 | |
2672 | jpgd_block_t* p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y); |
2673 | |
2674 | for (k = pD->m_spectral_start; k <= pD->m_spectral_end; k++) |
2675 | { |
2676 | unsigned int idx = pD->m_comp_ac_tab[component_id]; |
2677 | if (idx >= JPGD_MAX_HUFF_TABLES) |
2678 | pD->stop_decoding(JPGD_DECODE_ERROR); |
2679 | |
2680 | s = pD->huff_decode(pD->m_pHuff_tabs[idx]); |
2681 | |
2682 | r = s >> 4; |
2683 | s &= 15; |
2684 | |
2685 | if (s) |
2686 | { |
2687 | if ((k += r) > 63) |
2688 | pD->stop_decoding(JPGD_DECODE_ERROR); |
2689 | |
2690 | r = pD->get_bits_no_markers(s); |
2691 | s = JPGD_HUFF_EXTEND(r, s); |
2692 | |
2693 | p[g_ZAG[k]] = static_cast<jpgd_block_t>(s << pD->m_successive_low); |
2694 | } |
2695 | else |
2696 | { |
2697 | if (r == 15) |
2698 | { |
2699 | if ((k += 15) > 63) |
2700 | pD->stop_decoding(JPGD_DECODE_ERROR); |
2701 | } |
2702 | else |
2703 | { |
2704 | pD->m_eob_run = 1 << r; |
2705 | |
2706 | if (r) |
2707 | pD->m_eob_run += pD->get_bits_no_markers(r); |
2708 | |
2709 | pD->m_eob_run--; |
2710 | |
2711 | break; |
2712 | } |
2713 | } |
2714 | } |
2715 | } |
2716 | |
2717 | void jpeg_decoder::decode_block_ac_refine(jpeg_decoder* pD, int component_id, int block_x, int block_y) |
2718 | { |
2719 | int s, k, r; |
2720 | |
2721 | int p1 = 1 << pD->m_successive_low; |
2722 | |
2723 | //int m1 = (-1) << pD->m_successive_low; |
2724 | int m1 = static_cast<int>((UINT32_MAX << pD->m_successive_low)); |
2725 | |
2726 | jpgd_block_t* p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y); |
2727 | if (pD->m_spectral_end > 63) |
2728 | pD->stop_decoding(JPGD_DECODE_ERROR); |
2729 | |
2730 | k = pD->m_spectral_start; |
2731 | |
2732 | if (pD->m_eob_run == 0) |
2733 | { |
2734 | for (; k <= pD->m_spectral_end; k++) |
2735 | { |
2736 | unsigned int idx = pD->m_comp_ac_tab[component_id]; |
2737 | if (idx >= JPGD_MAX_HUFF_TABLES) |
2738 | pD->stop_decoding(JPGD_DECODE_ERROR); |
2739 | |
2740 | s = pD->huff_decode(pD->m_pHuff_tabs[idx]); |
2741 | |
2742 | r = s >> 4; |
2743 | s &= 15; |
2744 | |
2745 | if (s) |
2746 | { |
2747 | if (s != 1) |
2748 | pD->stop_decoding(JPGD_DECODE_ERROR); |
2749 | |
2750 | if (pD->get_bits_no_markers(1)) |
2751 | s = p1; |
2752 | else |
2753 | s = m1; |
2754 | } |
2755 | else |
2756 | { |
2757 | if (r != 15) |
2758 | { |
2759 | pD->m_eob_run = 1 << r; |
2760 | |
2761 | if (r) |
2762 | pD->m_eob_run += pD->get_bits_no_markers(r); |
2763 | |
2764 | break; |
2765 | } |
2766 | } |
2767 | |
2768 | do |
2769 | { |
2770 | jpgd_block_t* this_coef = p + g_ZAG[k & 63]; |
2771 | |
2772 | if (*this_coef != 0) |
2773 | { |
2774 | if (pD->get_bits_no_markers(1)) |
2775 | { |
2776 | if ((*this_coef & p1) == 0) |
2777 | { |
2778 | if (*this_coef >= 0) |
2779 | *this_coef = static_cast<jpgd_block_t>(*this_coef + p1); |
2780 | else |
2781 | *this_coef = static_cast<jpgd_block_t>(*this_coef + m1); |
2782 | } |
2783 | } |
2784 | } |
2785 | else |
2786 | { |
2787 | if (--r < 0) |
2788 | break; |
2789 | } |
2790 | |
2791 | k++; |
2792 | |
2793 | } while (k <= pD->m_spectral_end); |
2794 | |
2795 | if ((s) && (k < 64)) |
2796 | { |
2797 | p[g_ZAG[k]] = static_cast<jpgd_block_t>(s); |
2798 | } |
2799 | } |
2800 | } |
2801 | |
2802 | if (pD->m_eob_run > 0) |
2803 | { |
2804 | for (; k <= pD->m_spectral_end; k++) |
2805 | { |
2806 | jpgd_block_t* this_coef = p + g_ZAG[k & 63]; // logical AND to shut up static code analysis |
2807 | |
2808 | if (*this_coef != 0) |
2809 | { |
2810 | if (pD->get_bits_no_markers(1)) |
2811 | { |
2812 | if ((*this_coef & p1) == 0) |
2813 | { |
2814 | if (*this_coef >= 0) |
2815 | *this_coef = static_cast<jpgd_block_t>(*this_coef + p1); |
2816 | else |
2817 | *this_coef = static_cast<jpgd_block_t>(*this_coef + m1); |
2818 | } |
2819 | } |
2820 | } |
2821 | } |
2822 | |
2823 | pD->m_eob_run--; |
2824 | } |
2825 | } |
2826 | |
2827 | // Decode a scan in a progressively encoded image. |
2828 | void jpeg_decoder::decode_scan(pDecode_block_func decode_block_func) |
2829 | { |
2830 | int mcu_row, mcu_col, mcu_block; |
2831 | int block_x_mcu[JPGD_MAX_COMPONENTS], block_y_mcu[JPGD_MAX_COMPONENTS]; |
2832 | |
2833 | memset(block_y_mcu, 0, sizeof(block_y_mcu)); |
2834 | |
2835 | for (mcu_col = 0; mcu_col < m_mcus_per_col; mcu_col++) |
2836 | { |
2837 | int component_num, component_id; |
2838 | |
2839 | memset(block_x_mcu, 0, sizeof(block_x_mcu)); |
2840 | |
2841 | for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++) |
2842 | { |
2843 | int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0; |
2844 | |
2845 | if ((m_restart_interval) && (m_restarts_left == 0)) |
2846 | process_restart(); |
2847 | |
2848 | for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++) |
2849 | { |
2850 | component_id = m_mcu_org[mcu_block]; |
2851 | |
2852 | decode_block_func(this, component_id, block_x_mcu[component_id] + block_x_mcu_ofs, block_y_mcu[component_id] + block_y_mcu_ofs); |
2853 | |
2854 | if (m_comps_in_scan == 1) |
2855 | block_x_mcu[component_id]++; |
2856 | else |
2857 | { |
2858 | if (++block_x_mcu_ofs == m_comp_h_samp[component_id]) |
2859 | { |
2860 | block_x_mcu_ofs = 0; |
2861 | |
2862 | if (++block_y_mcu_ofs == m_comp_v_samp[component_id]) |
2863 | { |
2864 | block_y_mcu_ofs = 0; |
2865 | block_x_mcu[component_id] += m_comp_h_samp[component_id]; |
2866 | } |
2867 | } |
2868 | } |
2869 | } |
2870 | |
2871 | m_restarts_left--; |
2872 | } |
2873 | |
2874 | if (m_comps_in_scan == 1) |
2875 | block_y_mcu[m_comp_list[0]]++; |
2876 | else |
2877 | { |
2878 | for (component_num = 0; component_num < m_comps_in_scan; component_num++) |
2879 | { |
2880 | component_id = m_comp_list[component_num]; |
2881 | block_y_mcu[component_id] += m_comp_v_samp[component_id]; |
2882 | } |
2883 | } |
2884 | } |
2885 | } |
2886 | |
2887 | // Decode a progressively encoded image. |
2888 | void jpeg_decoder::init_progressive() |
2889 | { |
2890 | int i; |
2891 | |
2892 | if (m_comps_in_frame == 4) |
2893 | stop_decoding(JPGD_UNSUPPORTED_COLORSPACE); |
2894 | |
2895 | // Allocate the coefficient buffers. |
2896 | for (i = 0; i < m_comps_in_frame; i++) |
2897 | { |
2898 | m_dc_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 1, 1); |
2899 | m_ac_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 8, 8); |
2900 | } |
2901 | |
2902 | // See https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf |
2903 | uint32_t total_scans = 0; |
2904 | const uint32_t MAX_SCANS_TO_PROCESS = 1000; |
2905 | |
2906 | for (; ; ) |
2907 | { |
2908 | int dc_only_scan, refinement_scan; |
2909 | pDecode_block_func decode_block_func; |
2910 | |
2911 | if (!init_scan()) |
2912 | break; |
2913 | |
2914 | dc_only_scan = (m_spectral_start == 0); |
2915 | refinement_scan = (m_successive_high != 0); |
2916 | |
2917 | if ((m_spectral_start > m_spectral_end) || (m_spectral_end > 63)) |
2918 | stop_decoding(JPGD_BAD_SOS_SPECTRAL); |
2919 | |
2920 | if (dc_only_scan) |
2921 | { |
2922 | if (m_spectral_end) |
2923 | stop_decoding(JPGD_BAD_SOS_SPECTRAL); |
2924 | } |
2925 | else if (m_comps_in_scan != 1) /* AC scans can only contain one component */ |
2926 | stop_decoding(JPGD_BAD_SOS_SPECTRAL); |
2927 | |
2928 | if ((refinement_scan) && (m_successive_low != m_successive_high - 1)) |
2929 | stop_decoding(JPGD_BAD_SOS_SUCCESSIVE); |
2930 | |
2931 | if (dc_only_scan) |
2932 | { |
2933 | if (refinement_scan) |
2934 | decode_block_func = decode_block_dc_refine; |
2935 | else |
2936 | decode_block_func = decode_block_dc_first; |
2937 | } |
2938 | else |
2939 | { |
2940 | if (refinement_scan) |
2941 | decode_block_func = decode_block_ac_refine; |
2942 | else |
2943 | decode_block_func = decode_block_ac_first; |
2944 | } |
2945 | |
2946 | decode_scan(decode_block_func); |
2947 | |
2948 | m_bits_left = 16; |
2949 | get_bits(16); |
2950 | get_bits(16); |
2951 | |
2952 | total_scans++; |
2953 | if (total_scans > MAX_SCANS_TO_PROCESS) |
2954 | stop_decoding(JPGD_TOO_MANY_SCANS); |
2955 | } |
2956 | |
2957 | m_comps_in_scan = m_comps_in_frame; |
2958 | |
2959 | for (i = 0; i < m_comps_in_frame; i++) |
2960 | m_comp_list[i] = i; |
2961 | |
2962 | if (!calc_mcu_block_order()) |
2963 | stop_decoding(JPGD_DECODE_ERROR); |
2964 | } |
2965 | |
2966 | void jpeg_decoder::init_sequential() |
2967 | { |
2968 | if (!init_scan()) |
2969 | stop_decoding(JPGD_UNEXPECTED_MARKER); |
2970 | } |
2971 | |
2972 | void jpeg_decoder::decode_start() |
2973 | { |
2974 | init_frame(); |
2975 | |
2976 | if (m_progressive_flag) |
2977 | init_progressive(); |
2978 | else |
2979 | init_sequential(); |
2980 | } |
2981 | |
2982 | void jpeg_decoder::decode_init(jpeg_decoder_stream* pStream, uint32_t flags) |
2983 | { |
2984 | init(pStream, flags); |
2985 | locate_sof_marker(); |
2986 | } |
2987 | |
2988 | jpeg_decoder::jpeg_decoder(jpeg_decoder_stream* pStream, uint32_t flags) |
2989 | { |
2990 | if (setjmp(m_jmp_state)) |
2991 | return; |
2992 | decode_init(pStream, flags); |
2993 | } |
2994 | |
2995 | int jpeg_decoder::begin_decoding() |
2996 | { |
2997 | if (m_ready_flag) |
2998 | return JPGD_SUCCESS; |
2999 | |
3000 | if (m_error_code) |
3001 | return JPGD_FAILED; |
3002 | |
3003 | if (setjmp(m_jmp_state)) |
3004 | return JPGD_FAILED; |
3005 | |
3006 | decode_start(); |
3007 | |
3008 | m_ready_flag = true; |
3009 | |
3010 | return JPGD_SUCCESS; |
3011 | } |
3012 | |
3013 | jpeg_decoder::~jpeg_decoder() |
3014 | { |
3015 | free_all_blocks(); |
3016 | } |
3017 | |
3018 | jpeg_decoder_file_stream::jpeg_decoder_file_stream() |
3019 | { |
3020 | m_pFile = nullptr; |
3021 | m_eof_flag = false; |
3022 | m_error_flag = false; |
3023 | } |
3024 | |
3025 | void jpeg_decoder_file_stream::close() |
3026 | { |
3027 | if (m_pFile) |
3028 | { |
3029 | fclose(m_pFile); |
3030 | m_pFile = nullptr; |
3031 | } |
3032 | |
3033 | m_eof_flag = false; |
3034 | m_error_flag = false; |
3035 | } |
3036 | |
3037 | jpeg_decoder_file_stream::~jpeg_decoder_file_stream() |
3038 | { |
3039 | close(); |
3040 | } |
3041 | |
3042 | bool jpeg_decoder_file_stream::open(const char* Pfilename) |
3043 | { |
3044 | close(); |
3045 | |
3046 | m_eof_flag = false; |
3047 | m_error_flag = false; |
3048 | |
3049 | #if defined(_MSC_VER) |
3050 | m_pFile = nullptr; |
3051 | fopen_s(&m_pFile, Pfilename, "rb" ); |
3052 | #else |
3053 | m_pFile = fopen(Pfilename, "rb" ); |
3054 | #endif |
3055 | return m_pFile != nullptr; |
3056 | } |
3057 | |
3058 | int jpeg_decoder_file_stream::read(uint8* pBuf, int max_bytes_to_read, bool* pEOF_flag) |
3059 | { |
3060 | if (!m_pFile) |
3061 | return -1; |
3062 | |
3063 | if (m_eof_flag) |
3064 | { |
3065 | *pEOF_flag = true; |
3066 | return 0; |
3067 | } |
3068 | |
3069 | if (m_error_flag) |
3070 | return -1; |
3071 | |
3072 | int bytes_read = static_cast<int>(fread(pBuf, 1, max_bytes_to_read, m_pFile)); |
3073 | if (bytes_read < max_bytes_to_read) |
3074 | { |
3075 | if (ferror(m_pFile)) |
3076 | { |
3077 | m_error_flag = true; |
3078 | return -1; |
3079 | } |
3080 | |
3081 | m_eof_flag = true; |
3082 | *pEOF_flag = true; |
3083 | } |
3084 | |
3085 | return bytes_read; |
3086 | } |
3087 | |
3088 | bool jpeg_decoder_mem_stream::open(const uint8* pSrc_data, uint size) |
3089 | { |
3090 | close(); |
3091 | m_pSrc_data = pSrc_data; |
3092 | m_ofs = 0; |
3093 | m_size = size; |
3094 | return true; |
3095 | } |
3096 | |
3097 | int jpeg_decoder_mem_stream::read(uint8* pBuf, int max_bytes_to_read, bool* pEOF_flag) |
3098 | { |
3099 | *pEOF_flag = false; |
3100 | |
3101 | if (!m_pSrc_data) |
3102 | return -1; |
3103 | |
3104 | uint bytes_remaining = m_size - m_ofs; |
3105 | if ((uint)max_bytes_to_read > bytes_remaining) |
3106 | { |
3107 | max_bytes_to_read = bytes_remaining; |
3108 | *pEOF_flag = true; |
3109 | } |
3110 | |
3111 | memcpy(pBuf, m_pSrc_data + m_ofs, max_bytes_to_read); |
3112 | m_ofs += max_bytes_to_read; |
3113 | |
3114 | return max_bytes_to_read; |
3115 | } |
3116 | |
3117 | unsigned char* decompress_jpeg_image_from_stream(jpeg_decoder_stream* pStream, int* width, int* height, int* actual_comps, int req_comps, uint32_t flags) |
3118 | { |
3119 | if (!actual_comps) |
3120 | return nullptr; |
3121 | *actual_comps = 0; |
3122 | |
3123 | if ((!pStream) || (!width) || (!height) || (!req_comps)) |
3124 | return nullptr; |
3125 | |
3126 | if ((req_comps != 1) && (req_comps != 3) && (req_comps != 4)) |
3127 | return nullptr; |
3128 | |
3129 | jpeg_decoder decoder(pStream, flags); |
3130 | if (decoder.get_error_code() != JPGD_SUCCESS) |
3131 | return nullptr; |
3132 | |
3133 | const int image_width = decoder.get_width(), image_height = decoder.get_height(); |
3134 | *width = image_width; |
3135 | *height = image_height; |
3136 | *actual_comps = decoder.get_num_components(); |
3137 | |
3138 | if (decoder.begin_decoding() != JPGD_SUCCESS) |
3139 | return nullptr; |
3140 | |
3141 | const int dst_bpl = image_width * req_comps; |
3142 | |
3143 | uint8* pImage_data = (uint8*)jpgd_malloc(dst_bpl * image_height); |
3144 | if (!pImage_data) |
3145 | return nullptr; |
3146 | |
3147 | for (int y = 0; y < image_height; y++) |
3148 | { |
3149 | const uint8* pScan_line; |
3150 | uint scan_line_len; |
3151 | if (decoder.decode((const void**)&pScan_line, &scan_line_len) != JPGD_SUCCESS) |
3152 | { |
3153 | jpgd_free(pImage_data); |
3154 | return nullptr; |
3155 | } |
3156 | |
3157 | uint8* pDst = pImage_data + y * dst_bpl; |
3158 | |
3159 | if (((req_comps == 1) && (decoder.get_num_components() == 1)) || ((req_comps == 4) && (decoder.get_num_components() == 3))) |
3160 | memcpy(pDst, pScan_line, dst_bpl); |
3161 | else if (decoder.get_num_components() == 1) |
3162 | { |
3163 | if (req_comps == 3) |
3164 | { |
3165 | for (int x = 0; x < image_width; x++) |
3166 | { |
3167 | uint8 luma = pScan_line[x]; |
3168 | pDst[0] = luma; |
3169 | pDst[1] = luma; |
3170 | pDst[2] = luma; |
3171 | pDst += 3; |
3172 | } |
3173 | } |
3174 | else |
3175 | { |
3176 | for (int x = 0; x < image_width; x++) |
3177 | { |
3178 | uint8 luma = pScan_line[x]; |
3179 | pDst[0] = luma; |
3180 | pDst[1] = luma; |
3181 | pDst[2] = luma; |
3182 | pDst[3] = 255; |
3183 | pDst += 4; |
3184 | } |
3185 | } |
3186 | } |
3187 | else if (decoder.get_num_components() == 3) |
3188 | { |
3189 | if (req_comps == 1) |
3190 | { |
3191 | const int YR = 19595, YG = 38470, YB = 7471; |
3192 | for (int x = 0; x < image_width; x++) |
3193 | { |
3194 | int r = pScan_line[x * 4 + 0]; |
3195 | int g = pScan_line[x * 4 + 1]; |
3196 | int b = pScan_line[x * 4 + 2]; |
3197 | *pDst++ = static_cast<uint8>((r * YR + g * YG + b * YB + 32768) >> 16); |
3198 | } |
3199 | } |
3200 | else |
3201 | { |
3202 | for (int x = 0; x < image_width; x++) |
3203 | { |
3204 | pDst[0] = pScan_line[x * 4 + 0]; |
3205 | pDst[1] = pScan_line[x * 4 + 1]; |
3206 | pDst[2] = pScan_line[x * 4 + 2]; |
3207 | pDst += 3; |
3208 | } |
3209 | } |
3210 | } |
3211 | } |
3212 | |
3213 | return pImage_data; |
3214 | } |
3215 | |
3216 | unsigned char* decompress_jpeg_image_from_memory(const unsigned char* pSrc_data, int src_data_size, int* width, int* height, int* actual_comps, int req_comps, uint32_t flags) |
3217 | { |
3218 | jpgd::jpeg_decoder_mem_stream mem_stream(pSrc_data, src_data_size); |
3219 | return decompress_jpeg_image_from_stream(&mem_stream, width, height, actual_comps, req_comps, flags); |
3220 | } |
3221 | |
3222 | unsigned char* decompress_jpeg_image_from_file(const char* pSrc_filename, int* width, int* height, int* actual_comps, int req_comps, uint32_t flags) |
3223 | { |
3224 | jpgd::jpeg_decoder_file_stream file_stream; |
3225 | if (!file_stream.open(pSrc_filename)) |
3226 | return nullptr; |
3227 | return decompress_jpeg_image_from_stream(&file_stream, width, height, actual_comps, req_comps, flags); |
3228 | } |
3229 | |
3230 | } // namespace jpgd |
3231 | |