Fix typo in german translation.
[maemo-rb.git] / apps / recorder / jpeg_load.c
blob46b7cc9bc53db2e0ba3159550813f144751fa2bd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * JPEG image viewer
11 * (This is a real mess if it has to be coded in one single C file)
13 * Copyright (C) 2009 Andrew Mahone fractional decode, split IDCT - 16-point
14 * IDCT based on IJG jpeg-7 pre-release
15 * File scrolling addition (C) 2005 Alexander Spyridakis
16 * Copyright (C) 2004 Jörg Hohensohn aka [IDC]Dragon
17 * Heavily borrowed from the IJG implementation (C) Thomas G. Lane
18 * Small & fast downscaling IDCT (C) 2002 by Guido Vollbeding JPEGclub.org
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version 2
23 * of the License, or (at your option) any later version.
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
26 * KIND, either express or implied.
28 ****************************************************************************/
30 #include "plugin.h"
31 #include "debug.h"
32 #include "jpeg_load.h"
33 /*#define JPEG_BS_DEBUG*/
34 //#define ROCKBOX_DEBUG_JPEG
35 /* for portability of below JPEG code */
36 #define MEMSET(p,v,c) memset(p,v,c)
37 #define MEMCPY(d,s,c) memcpy(d,s,c)
38 #define INLINE static inline
39 #define ENDIAN_SWAP16(n) n /* only for poor little endian machines */
40 #ifdef ROCKBOX_DEBUG_JPEG
41 #define JDEBUGF DEBUGF
42 #else
43 #define JDEBUGF(...)
44 #endif
46 /**************** begin JPEG code ********************/
48 #ifdef HAVE_LCD_COLOR
49 typedef struct uint8_rgb jpeg_pix_t;
50 #else
51 typedef uint8_t jpeg_pix_t;
52 #endif
53 #define JPEG_IDCT_TRANSPOSE
54 #define JPEG_PIX_SZ (sizeof(jpeg_pix_t))
55 #ifdef HAVE_LCD_COLOR
56 #define COLOR_EXTRA_IDCT_WS 64
57 #else
58 #define COLOR_EXTRA_IDCT_WS 0
59 #endif
60 #ifdef JPEG_IDCT_TRANSPOSE
61 #define V_OUT(n) ws2[8*n]
62 #define V_IN_ST 1
63 #define TRANSPOSE_EXTRA_IDCT_WS 64
64 #else
65 #define V_OUT(n) ws[8*n]
66 #define V_IN_ST 8
67 #define TRANSPOSE_EXTRA_IDCT_WS 0
68 #endif
69 #define IDCT_WS_SIZE (64 + TRANSPOSE_EXTRA_IDCT_WS + COLOR_EXTRA_IDCT_WS)
71 /* This can't be in jpeg_load.h because plugin.h includes it, and it conflicts
72 * with the definition in jpeg_decoder.h
74 struct jpeg
76 #ifdef JPEG_FROM_MEM
77 unsigned char *data;
78 #else
79 int fd;
80 int buf_left;
81 int buf_index;
82 #endif
83 unsigned long len;
84 unsigned long int bitbuf;
85 int bitbuf_bits;
86 int marker_ind;
87 int marker_val;
88 unsigned char marker;
89 int x_size, y_size; /* size of image (can be less than block boundary) */
90 int x_phys, y_phys; /* physical size, block aligned */
91 int x_mbl; /* x dimension of MBL */
92 int y_mbl; /* y dimension of MBL */
93 int blocks; /* blocks per MB */
94 int restart_interval; /* number of MCUs between RSTm markers */
95 int restart; /* blocks until next restart marker */
96 int mcu_row; /* current row relative to first row of this row of MCUs */
97 unsigned char *out_ptr; /* pointer to current row to output */
98 int cur_row; /* current row relative to top of image */
99 int set_rows;
100 int store_pos[4]; /* for Y block ordering */
101 #ifdef HAVE_LCD_COLOR
102 int last_dc_val[3];
103 int h_scale[2]; /* horizontal scalefactor = (2**N) / 8 */
104 int v_scale[2]; /* same as above, for vertical direction */
105 int k_need[2]; /* per component zig-zag index of last needed coefficient */
106 int zero_need[2]; /* per compenent number of coefficients to zero */
107 #else
108 int last_dc_val;
109 int h_scale[1]; /* horizontal scalefactor = (2**N) / 8 */
110 int v_scale[1]; /* same as above, for vertical direction */
111 int k_need[1]; /* per component zig-zag index of last needed coefficient */
112 int zero_need[1]; /* per compenent number of coefficients to zero */
113 #endif
114 jpeg_pix_t *img_buf;
116 int16_t quanttable[4][QUANT_TABLE_LENGTH];/* raw quantization tables 0-3 */
118 struct huffman_table hufftable[2]; /* Huffman tables */
119 struct derived_tbl dc_derived_tbls[2]; /* Huffman-LUTs */
120 struct derived_tbl ac_derived_tbls[2];
122 struct frame_component frameheader[3]; /* Component descriptor */
123 struct scan_component scanheader[3]; /* currently not used */
125 int mcu_membership[6]; /* info per block */
126 int tab_membership[6];
127 int subsample_x[3]; /* info per component */
128 int subsample_y[3];
129 bool resize;
130 unsigned char buf[JPEG_READ_BUF_SIZE];
131 struct img_part part;
134 #ifdef JPEG_FROM_MEM
135 static struct jpeg jpeg;
136 #endif
138 INLINE unsigned range_limit(int value)
140 #if CONFIG_CPU == SH7034
141 unsigned tmp;
142 asm ( /* Note: Uses knowledge that only low byte of result is used */
143 "extu.b %[v],%[t] \n"
144 "cmp/eq %[v],%[t] \n" /* low byte == whole number ? */
145 "bt 1f \n" /* yes: no overflow */
146 "cmp/pz %[v] \n" /* overflow: positive? */
147 "subc %[v],%[v] \n" /* %[r] now either 0 or 0xffffffff */
148 "1: \n"
149 : /* outputs */
150 [v]"+r"(value),
151 [t]"=&r"(tmp)
153 return value;
154 #elif defined(CPU_COLDFIRE)
155 /* Note: Uses knowledge that only the low byte of the result is used */
156 asm (
157 "cmp.l #255,%[v] \n" /* overflow? */
158 "bls.b 1f \n" /* no: return value */
159 /* yes: set low byte to appropriate boundary */
160 "spl.b %[v] \n"
161 "1: \n"
162 : /* outputs */
163 [v]"+d"(value)
165 return value;
166 #elif defined(CPU_ARM)
167 /* Note: Uses knowledge that only the low byte of the result is used */
168 asm (
169 "cmp %[v], #255 \n" /* out of range 0..255? */
170 "mvnhi %[v], %[v], asr #31 \n" /* yes: set all bits to ~(sign_bit) */
171 : /* outputs */
172 [v]"+r"(value)
174 return value;
175 #else
176 if ((unsigned)value <= 255)
177 return value;
179 if (value < 0)
180 return 0;
182 return 255;
183 #endif
186 INLINE unsigned scale_output(int value)
188 #if defined(CPU_ARM) && ARM_ARCH >= 6
189 asm (
190 "usat %[v], #8, %[v], asr #18\n"
191 : [v] "+r" (value)
193 return value;
194 #else
195 return range_limit(value >> 18);
196 #endif
199 /* IDCT implementation */
202 #define CONST_BITS 13
203 #define PASS1_BITS 2
206 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
207 * causing a lot of useless floating-point operations at run time.
208 * To get around this we use the following pre-calculated constants.
209 * If you change CONST_BITS you may want to add appropriate values.
210 * (With a reasonable C compiler, you can just rely on the FIX() macro...)
212 #define FIX_0_298631336 2446 /* FIX(0.298631336) */
213 #define FIX_0_390180644 3196 /* FIX(0.390180644) */
214 #define FIX_0_541196100 4433 /* FIX(0.541196100) */
215 #define FIX_0_765366865 6270 /* FIX(0.765366865) */
216 #define FIX_0_899976223 7373 /* FIX(0.899976223) */
217 #define FIX_1_175875602 9633 /* FIX(1.175875602) */
218 #define FIX_1_501321110 12299 /* FIX(1.501321110) */
219 #define FIX_1_847759065 15137 /* FIX(1.847759065) */
220 #define FIX_1_961570560 16069 /* FIX(1.961570560) */
221 #define FIX_2_053119869 16819 /* FIX(2.053119869) */
222 #define FIX_2_562915447 20995 /* FIX(2.562915447) */
223 #define FIX_3_072711026 25172 /* FIX(3.072711026) */
227 /* Multiply an long variable by an long constant to yield an long result.
228 * For 8-bit samples with the recommended scaling, all the variable
229 * and constant values involved are no more than 16 bits wide, so a
230 * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
231 * For 12-bit samples, a full 32-bit multiplication will be needed.
233 #define MULTIPLY(var1, var2) ((var1) * (var2))
235 #if defined(CPU_SH) || defined(CPU_COLDFIRE) || \
236 (defined(CPU_ARM) && ARM_ARCH > 4)
237 #define MULTIPLY16(var,const) (((short) (var)) * ((short) (const)))
238 #else
239 #define MULTIPLY16 MULTIPLY
240 #endif
243 * Macros for handling fixed-point arithmetic; these are used by many
244 * but not all of the DCT/IDCT modules.
246 * All values are expected to be of type INT32.
247 * Fractional constants are scaled left by CONST_BITS bits.
248 * CONST_BITS is defined within each module using these macros,
249 * and may differ from one module to the next.
251 #define ONE ((long)1)
252 #define CONST_SCALE (ONE << CONST_BITS)
254 /* Convert a positive real constant to an integer scaled by CONST_SCALE.
255 * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
256 * thus causing a lot of useless floating-point operations at run time.
258 #define FIX(x) ((long) ((x) * CONST_SCALE + 0.5))
259 #define RIGHT_SHIFT(x,shft) ((x) >> (shft))
261 /* Descale and correctly round an int value that's scaled by N bits.
262 * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
263 * the fudge factor is correct for either sign of X.
265 #define DESCALE(x,n) (((x) + (1l << ((n)-1))) >> (n))
267 #define DS_OUT ((CONST_BITS)+(PASS1_BITS)+3)
270 * Conversion of full 0-255 range YCrCb to RGB:
271 * |R| |1.000000 -0.000001 1.402000| |Y'|
272 * |G| = |1.000000 -0.334136 -0.714136| |Pb|
273 * |B| |1.000000 1.772000 0.000000| |Pr|
274 * Scaled (yields s15-bit output):
275 * |R| |128 0 179| |Y |
276 * |G| = |128 -43 -91| |Cb - 128|
277 * |B| |128 227 0| |Cr - 128|
279 #define YFAC 128
280 #define RVFAC 179
281 #define GUFAC (-43)
282 #define GVFAC (-91)
283 #define BUFAC 227
284 #define COMPONENT_SHIFT 15
286 #ifndef CPU_ARM
287 /* horizontal-pass 1-point IDCT */
288 static void jpeg_idct1h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep)
290 for (; ws < end; ws += 8)
292 *out = range_limit(128 + (int) DESCALE(*ws, 3 + PASS1_BITS));
293 out += rowstep;
297 /* vertical-pass 2-point IDCT */
298 static void jpeg_idct2v(int16_t *ws, int16_t *end)
300 for (; ws < end; ws++)
302 int tmp1 = ws[0*8];
303 int tmp2 = ws[1*8];
304 ws[0*8] = tmp1 + tmp2;
305 ws[1*8] = tmp1 - tmp2;
309 /* horizontal-pass 2-point IDCT */
310 static void jpeg_idct2h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep)
312 for (; ws < end; ws += 8, out += rowstep)
314 int tmp1 = ws[0] + (ONE << (PASS1_BITS + 2))
315 + (128 << (PASS1_BITS + 3));
316 int tmp2 = ws[1];
317 out[JPEG_PIX_SZ*0] = range_limit((int) RIGHT_SHIFT(tmp1 + tmp2,
318 PASS1_BITS + 3));
319 out[JPEG_PIX_SZ*1] = range_limit((int) RIGHT_SHIFT(tmp1 - tmp2,
320 PASS1_BITS + 3));
324 /* vertical-pass 4-point IDCT */
325 static void jpeg_idct4v(int16_t *ws, int16_t *end)
327 for (; ws < end; ws++)
329 int tmp0, tmp2, tmp10, tmp12;
330 int z1, z2, z3;
331 /* Even part */
333 tmp0 = ws[8*0];
334 tmp2 = ws[8*2];
336 tmp10 = (tmp0 + tmp2) << PASS1_BITS;
337 tmp12 = (tmp0 - tmp2) << PASS1_BITS;
339 /* Odd part */
340 /* Same rotation as in the even part of the 8x8 LL&M IDCT */
342 z2 = ws[8*1];
343 z3 = ws[8*3];
345 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100) +
346 (ONE << (CONST_BITS - PASS1_BITS - 1));
347 tmp0 = RIGHT_SHIFT(z1 + MULTIPLY16(z3, - FIX_1_847759065),
348 CONST_BITS-PASS1_BITS);
349 tmp2 = RIGHT_SHIFT(z1 + MULTIPLY16(z2, FIX_0_765366865),
350 CONST_BITS-PASS1_BITS);
352 /* Final output stage */
353 ws[8*0] = (int) (tmp10 + tmp2);
354 ws[8*3] = (int) (tmp10 - tmp2);
355 ws[8*1] = (int) (tmp12 + tmp0);
356 ws[8*2] = (int) (tmp12 - tmp0);
360 /* horizontal-pass 4-point IDCT */
361 static void jpeg_idct4h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep)
363 for (; ws < end; out += rowstep, ws += 8)
365 int tmp0, tmp2, tmp10, tmp12;
366 int z1, z2, z3;
367 /* Even part */
369 tmp0 = (int) ws[0] + (ONE << (PASS1_BITS + 2))
370 + (128 << (PASS1_BITS + 3));
371 tmp2 = (int) ws[2];
373 tmp10 = (tmp0 + tmp2) << CONST_BITS;
374 tmp12 = (tmp0 - tmp2) << CONST_BITS;
376 /* Odd part */
377 /* Same rotation as in the even part of the 8x8 LL&M IDCT */
379 z2 = (int) ws[1];
380 z3 = (int) ws[3];
382 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100);
383 tmp0 = z1 - MULTIPLY16(z3, FIX_1_847759065);
384 tmp2 = z1 + MULTIPLY16(z2, FIX_0_765366865);
386 /* Final output stage */
388 out[JPEG_PIX_SZ*0] = range_limit((int) RIGHT_SHIFT(tmp10 + tmp2,
389 DS_OUT));
390 out[JPEG_PIX_SZ*3] = range_limit((int) RIGHT_SHIFT(tmp10 - tmp2,
391 DS_OUT));
392 out[JPEG_PIX_SZ*1] = range_limit((int) RIGHT_SHIFT(tmp12 + tmp0,
393 DS_OUT));
394 out[JPEG_PIX_SZ*2] = range_limit((int) RIGHT_SHIFT(tmp12 - tmp0,
395 DS_OUT));
399 /* vertical-pass 8-point IDCT */
400 static void jpeg_idct8v(int16_t *ws, int16_t *end)
402 long tmp0, tmp1, tmp2, tmp3;
403 long tmp10, tmp11, tmp12, tmp13;
404 long z1, z2, z3, z4, z5;
405 #ifdef JPEG_IDCT_TRANSPOSE
406 int16_t *ws2 = ws + 64;
407 for (; ws < end; ws += 8, ws2++)
409 #else
410 for (; ws < end; ws++)
412 #endif
413 /* Due to quantization, we will usually find that many of the input
414 * coefficients are zero, especially the AC terms. We can exploit this
415 * by short-circuiting the IDCT calculation for any column in which all
416 * the AC terms are zero. In that case each output is equal to the
417 * DC coefficient (with scale factor as needed).
418 * With typical images and quantization tables, half or more of the
419 * column DCT calculations can be simplified this way.
421 if ((ws[V_IN_ST*1] | ws[V_IN_ST*2] | ws[V_IN_ST*3]
422 | ws[V_IN_ST*4] | ws[V_IN_ST*5] | ws[V_IN_ST*6] | ws[V_IN_ST*7]) == 0)
424 /* AC terms all zero */
425 int dcval = ws[V_IN_ST*0] << PASS1_BITS;
427 V_OUT(0) = V_OUT(1) = V_OUT(2) = V_OUT(3) = V_OUT(4) = V_OUT(5) =
428 V_OUT(6) = V_OUT(7) = dcval;
429 continue;
432 /* Even part: reverse the even part of the forward DCT. */
433 /* The rotator is sqrt(2)*c(-6). */
435 z2 = ws[V_IN_ST*2];
436 z3 = ws[V_IN_ST*6];
438 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100);
439 tmp2 = z1 + MULTIPLY16(z3, - FIX_1_847759065);
440 tmp3 = z1 + MULTIPLY16(z2, FIX_0_765366865);
442 z2 = ws[V_IN_ST*0] << CONST_BITS;
443 z2 += ONE << (CONST_BITS - PASS1_BITS - 1);
444 z3 = ws[V_IN_ST*4] << CONST_BITS;
446 tmp0 = (z2 + z3);
447 tmp1 = (z2 - z3);
449 tmp10 = tmp0 + tmp3;
450 tmp13 = tmp0 - tmp3;
451 tmp11 = tmp1 + tmp2;
452 tmp12 = tmp1 - tmp2;
454 /* Odd part per figure 8; the matrix is unitary and hence its
455 transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */
457 tmp0 = ws[V_IN_ST*7];
458 tmp1 = ws[V_IN_ST*5];
459 tmp2 = ws[V_IN_ST*3];
460 tmp3 = ws[V_IN_ST*1];
462 z1 = tmp0 + tmp3;
463 z2 = tmp1 + tmp2;
464 z3 = tmp0 + tmp2;
465 z4 = tmp1 + tmp3;
466 z5 = MULTIPLY16(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
468 tmp0 = MULTIPLY16(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
469 tmp1 = MULTIPLY16(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
470 tmp2 = MULTIPLY16(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
471 tmp3 = MULTIPLY16(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
472 z1 = MULTIPLY16(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
473 z2 = MULTIPLY16(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
474 z3 = MULTIPLY16(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
475 z4 = MULTIPLY16(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
477 z3 += z5;
478 z4 += z5;
480 tmp0 += z1 + z3;
481 tmp1 += z2 + z4;
482 tmp2 += z2 + z3;
483 tmp3 += z1 + z4;
485 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
487 V_OUT(0) = (int) RIGHT_SHIFT(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
488 V_OUT(7) = (int) RIGHT_SHIFT(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
489 V_OUT(1) = (int) RIGHT_SHIFT(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
490 V_OUT(6) = (int) RIGHT_SHIFT(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
491 V_OUT(2) = (int) RIGHT_SHIFT(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
492 V_OUT(5) = (int) RIGHT_SHIFT(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
493 V_OUT(3) = (int) RIGHT_SHIFT(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
494 V_OUT(4) = (int) RIGHT_SHIFT(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
498 /* horizontal-pass 8-point IDCT */
499 static void jpeg_idct8h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep)
501 long tmp0, tmp1, tmp2, tmp3;
502 long tmp10, tmp11, tmp12, tmp13;
503 long z1, z2, z3, z4, z5;
504 for (; ws < end; out += rowstep, ws += 8)
506 /* Rows of zeroes can be exploited in the same way as we did with
507 * columns. However, the column calculation has created many nonzero AC
508 * terms, so the simplification applies less often (typically 5% to 10%
509 * of the time). On machines with very fast multiplication, it's
510 * possible that the test takes more time than it's worth. In that
511 * case this section may be commented out.
514 #ifndef NO_ZERO_ROW_TEST
515 if ((ws[1] | ws[2] | ws[3]
516 | ws[4] | ws[5] | ws[6] | ws[7]) == 0)
518 /* AC terms all zero */
519 unsigned char dcval = range_limit(128 + (int) DESCALE((long) ws[0],
520 PASS1_BITS+3));
522 out[JPEG_PIX_SZ*0] = dcval;
523 out[JPEG_PIX_SZ*1] = dcval;
524 out[JPEG_PIX_SZ*2] = dcval;
525 out[JPEG_PIX_SZ*3] = dcval;
526 out[JPEG_PIX_SZ*4] = dcval;
527 out[JPEG_PIX_SZ*5] = dcval;
528 out[JPEG_PIX_SZ*6] = dcval;
529 out[JPEG_PIX_SZ*7] = dcval;
530 continue;
532 #endif
534 /* Even part: reverse the even part of the forward DCT. */
535 /* The rotator is sqrt(2)*c(-6). */
537 z2 = (long) ws[2];
538 z3 = (long) ws[6];
540 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100);
541 tmp2 = z1 + MULTIPLY16(z3, - FIX_1_847759065);
542 tmp3 = z1 + MULTIPLY16(z2, FIX_0_765366865);
544 z4 = (long) ws[0] + (ONE << (PASS1_BITS + 2))
545 + (128 << (PASS1_BITS + 3));
546 z4 <<= CONST_BITS;
547 z5 = (long) ws[4] << CONST_BITS;
548 tmp0 = z4 + z5;
549 tmp1 = z4 - z5;
551 tmp10 = tmp0 + tmp3;
552 tmp13 = tmp0 - tmp3;
553 tmp11 = tmp1 + tmp2;
554 tmp12 = tmp1 - tmp2;
556 /* Odd part per figure 8; the matrix is unitary and hence its
557 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */
559 tmp0 = (long) ws[7];
560 tmp1 = (long) ws[5];
561 tmp2 = (long) ws[3];
562 tmp3 = (long) ws[1];
564 z1 = tmp0 + tmp3;
565 z2 = tmp1 + tmp2;
566 z3 = tmp0 + tmp2;
567 z4 = tmp1 + tmp3;
568 z5 = MULTIPLY16(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
570 tmp0 = MULTIPLY16(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
571 tmp1 = MULTIPLY16(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
572 tmp2 = MULTIPLY16(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
573 tmp3 = MULTIPLY16(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
574 z1 = MULTIPLY16(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
575 z2 = MULTIPLY16(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
576 z3 = MULTIPLY16(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
577 z4 = MULTIPLY16(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
579 z3 += z5;
580 z4 += z5;
582 tmp0 += z1 + z3;
583 tmp1 += z2 + z4;
584 tmp2 += z2 + z3;
585 tmp3 += z1 + z4;
587 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
589 out[JPEG_PIX_SZ*0] = range_limit((int) RIGHT_SHIFT(tmp10 + tmp3,
590 DS_OUT));
591 out[JPEG_PIX_SZ*7] = range_limit((int) RIGHT_SHIFT(tmp10 - tmp3,
592 DS_OUT));
593 out[JPEG_PIX_SZ*1] = range_limit((int) RIGHT_SHIFT(tmp11 + tmp2,
594 DS_OUT));
595 out[JPEG_PIX_SZ*6] = range_limit((int) RIGHT_SHIFT(tmp11 - tmp2,
596 DS_OUT));
597 out[JPEG_PIX_SZ*2] = range_limit((int) RIGHT_SHIFT(tmp12 + tmp1,
598 DS_OUT));
599 out[JPEG_PIX_SZ*5] = range_limit((int) RIGHT_SHIFT(tmp12 - tmp1,
600 DS_OUT));
601 out[JPEG_PIX_SZ*3] = range_limit((int) RIGHT_SHIFT(tmp13 + tmp0,
602 DS_OUT));
603 out[JPEG_PIX_SZ*4] = range_limit((int) RIGHT_SHIFT(tmp13 - tmp0,
604 DS_OUT));
608 #else
609 extern void jpeg_idct1h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep);
610 extern void jpeg_idct2v(int16_t *ws, int16_t *end);
611 extern void jpeg_idct2h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep);
612 extern void jpeg_idct4v(int16_t *ws, int16_t *end);
613 extern void jpeg_idct4h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep);
614 extern void jpeg_idct8v(int16_t *ws, int16_t *end);
615 extern void jpeg_idct8h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep);
616 #endif
618 #ifdef HAVE_LCD_COLOR
619 /* vertical-pass 16-point IDCT */
620 static void jpeg_idct16v(int16_t *ws, int16_t *end)
622 long tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
623 long tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27;
624 long z1, z2, z3, z4;
625 #ifdef JPEG_IDCT_TRANSPOSE
626 int16_t *ws2 = ws + 64;
627 for (; ws < end; ws += 8, ws2++)
629 #else
630 for (; ws < end; ws++)
632 #endif
633 /* Even part */
635 tmp0 = ws[V_IN_ST*0] << CONST_BITS;
636 /* Add fudge factor here for final descale. */
637 tmp0 += 1 << (CONST_BITS-PASS1_BITS-1);
639 z1 = ws[V_IN_ST*4];
640 tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */
641 tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */
643 tmp10 = tmp0 + tmp1;
644 tmp11 = tmp0 - tmp1;
645 tmp12 = tmp0 + tmp2;
646 tmp13 = tmp0 - tmp2;
648 z1 = ws[V_IN_ST*2];
649 z2 = ws[V_IN_ST*6];
650 z3 = z1 - z2;
651 z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */
652 z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */
654 /* (c6+c2)[16] = (c3+c1)[8] */
655 tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447);
656 /* (c6-c14)[16] = (c3-c7)[8] */
657 tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223);
658 /* (c2-c10)[16] = (c1-c5)[8] */
659 tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887));
660 /* (c10-c14)[16] = (c5-c7)[8] */
661 tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579));
663 tmp20 = tmp10 + tmp0;
664 tmp27 = tmp10 - tmp0;
665 tmp21 = tmp12 + tmp1;
666 tmp26 = tmp12 - tmp1;
667 tmp22 = tmp13 + tmp2;
668 tmp25 = tmp13 - tmp2;
669 tmp23 = tmp11 + tmp3;
670 tmp24 = tmp11 - tmp3;
672 /* Odd part */
674 z1 = ws[V_IN_ST*1];
675 z2 = ws[V_IN_ST*3];
676 z3 = ws[V_IN_ST*5];
677 z4 = ws[V_IN_ST*7];
679 tmp11 = z1 + z3;
681 tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */
682 tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */
683 tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */
684 tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */
685 tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */
686 tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */
687 tmp0 = tmp1 + tmp2 + tmp3 -
688 MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */
689 tmp13 = tmp10 + tmp11 + tmp12 -
690 MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */
691 z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */
692 tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */
693 tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */
694 z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */
695 tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */
696 tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */
697 z2 += z4;
698 z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */
699 tmp1 += z1;
700 tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */
701 z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */
702 tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */
703 tmp12 += z2;
704 z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */
705 tmp2 += z2;
706 tmp3 += z2;
707 z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */
708 tmp10 += z2;
709 tmp11 += z2;
711 /* Final output stage */
712 V_OUT(0) = (int) RIGHT_SHIFT(tmp20 + tmp0, CONST_BITS-PASS1_BITS);
713 V_OUT(15) = (int) RIGHT_SHIFT(tmp20 - tmp0, CONST_BITS-PASS1_BITS);
714 V_OUT(1) = (int) RIGHT_SHIFT(tmp21 + tmp1, CONST_BITS-PASS1_BITS);
715 V_OUT(14) = (int) RIGHT_SHIFT(tmp21 - tmp1, CONST_BITS-PASS1_BITS);
716 V_OUT(2) = (int) RIGHT_SHIFT(tmp22 + tmp2, CONST_BITS-PASS1_BITS);
717 V_OUT(13) = (int) RIGHT_SHIFT(tmp22 - tmp2, CONST_BITS-PASS1_BITS);
718 V_OUT(3) = (int) RIGHT_SHIFT(tmp23 + tmp3, CONST_BITS-PASS1_BITS);
719 V_OUT(12) = (int) RIGHT_SHIFT(tmp23 - tmp3, CONST_BITS-PASS1_BITS);
720 V_OUT(4) = (int) RIGHT_SHIFT(tmp24 + tmp10, CONST_BITS-PASS1_BITS);
721 V_OUT(11) = (int) RIGHT_SHIFT(tmp24 - tmp10, CONST_BITS-PASS1_BITS);
722 V_OUT(5) = (int) RIGHT_SHIFT(tmp25 + tmp11, CONST_BITS-PASS1_BITS);
723 V_OUT(10) = (int) RIGHT_SHIFT(tmp25 - tmp11, CONST_BITS-PASS1_BITS);
724 V_OUT(6) = (int) RIGHT_SHIFT(tmp26 + tmp12, CONST_BITS-PASS1_BITS);
725 V_OUT(9) = (int) RIGHT_SHIFT(tmp26 - tmp12, CONST_BITS-PASS1_BITS);
726 V_OUT(7) = (int) RIGHT_SHIFT(tmp27 + tmp13, CONST_BITS-PASS1_BITS);
727 V_OUT(8) = (int) RIGHT_SHIFT(tmp27 - tmp13, CONST_BITS-PASS1_BITS);
731 /* horizontal-pass 16-point IDCT */
732 static void jpeg_idct16h(int16_t *ws, unsigned char *out, int16_t *end, int rowstep)
734 long tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
735 long tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27;
736 long z1, z2, z3, z4;
737 for (; ws < end; out += rowstep, ws += 8)
739 /* Even part */
741 /* Add fudge factor here for final descale. */
742 tmp0 = (long) ws[0] + (ONE << (PASS1_BITS+2))
743 + (128 << (PASS1_BITS + 3));
744 tmp0 <<= CONST_BITS;
746 z1 = (long) ws[4];
747 tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */
748 tmp2 = MULTIPLY(z1, FIX_0_541196100); /* c12[16] = c6[8] */
750 tmp10 = tmp0 + tmp1;
751 tmp11 = tmp0 - tmp1;
752 tmp12 = tmp0 + tmp2;
753 tmp13 = tmp0 - tmp2;
755 z1 = (long) ws[2];
756 z2 = (long) ws[6];
757 z3 = z1 - z2;
758 z4 = MULTIPLY(z3, FIX(0.275899379)); /* c14[16] = c7[8] */
759 z3 = MULTIPLY(z3, FIX(1.387039845)); /* c2[16] = c1[8] */
761 /* (c6+c2)[16] = (c3+c1)[8] */
762 tmp0 = z3 + MULTIPLY(z2, FIX_2_562915447);
763 /* (c6-c14)[16] = (c3-c7)[8] */
764 tmp1 = z4 + MULTIPLY(z1, FIX_0_899976223);
765 /* (c2-c10)[16] = (c1-c5)[8] */
766 tmp2 = z3 - MULTIPLY(z1, FIX(0.601344887));
767 /* (c10-c14)[16] = (c5-c7)[8] */
768 tmp3 = z4 - MULTIPLY(z2, FIX(0.509795579));
770 tmp20 = tmp10 + tmp0;
771 tmp27 = tmp10 - tmp0;
772 tmp21 = tmp12 + tmp1;
773 tmp26 = tmp12 - tmp1;
774 tmp22 = tmp13 + tmp2;
775 tmp25 = tmp13 - tmp2;
776 tmp23 = tmp11 + tmp3;
777 tmp24 = tmp11 - tmp3;
779 /* Odd part */
781 z1 = (long) ws[1];
782 z2 = (long) ws[3];
783 z3 = (long) ws[5];
784 z4 = (long) ws[7];
786 tmp11 = z1 + z3;
788 tmp1 = MULTIPLY(z1 + z2, FIX(1.353318001)); /* c3 */
789 tmp2 = MULTIPLY(tmp11, FIX(1.247225013)); /* c5 */
790 tmp3 = MULTIPLY(z1 + z4, FIX(1.093201867)); /* c7 */
791 tmp10 = MULTIPLY(z1 - z4, FIX(0.897167586)); /* c9 */
792 tmp11 = MULTIPLY(tmp11, FIX(0.666655658)); /* c11 */
793 tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528)); /* c13 */
794 tmp0 = tmp1 + tmp2 + tmp3 -
795 MULTIPLY(z1, FIX(2.286341144)); /* c7+c5+c3-c1 */
796 tmp13 = tmp10 + tmp11 + tmp12 -
797 MULTIPLY(z1, FIX(1.835730603)); /* c9+c11+c13-c15 */
798 z1 = MULTIPLY(z2 + z3, FIX(0.138617169)); /* c15 */
799 tmp1 += z1 + MULTIPLY(z2, FIX(0.071888074)); /* c9+c11-c3-c15 */
800 tmp2 += z1 - MULTIPLY(z3, FIX(1.125726048)); /* c5+c7+c15-c3 */
801 z1 = MULTIPLY(z3 - z2, FIX(1.407403738)); /* c1 */
802 tmp11 += z1 - MULTIPLY(z3, FIX(0.766367282)); /* c1+c11-c9-c13 */
803 tmp12 += z1 + MULTIPLY(z2, FIX(1.971951411)); /* c1+c5+c13-c7 */
804 z2 += z4;
805 z1 = MULTIPLY(z2, - FIX(0.666655658)); /* -c11 */
806 tmp1 += z1;
807 tmp3 += z1 + MULTIPLY(z4, FIX(1.065388962)); /* c3+c11+c15-c7 */
808 z2 = MULTIPLY(z2, - FIX(1.247225013)); /* -c5 */
809 tmp10 += z2 + MULTIPLY(z4, FIX(3.141271809)); /* c1+c5+c9-c13 */
810 tmp12 += z2;
811 z2 = MULTIPLY(z3 + z4, - FIX(1.353318001)); /* -c3 */
812 tmp2 += z2;
813 tmp3 += z2;
814 z2 = MULTIPLY(z4 - z3, FIX(0.410524528)); /* c13 */
815 tmp10 += z2;
816 tmp11 += z2;
818 /* Final output stage */
820 out[JPEG_PIX_SZ*0] = scale_output(tmp20 + tmp0);
821 out[JPEG_PIX_SZ*15] = scale_output(tmp20 - tmp0);
822 out[JPEG_PIX_SZ*1] = scale_output(tmp21 + tmp1);
823 out[JPEG_PIX_SZ*14] = scale_output(tmp21 - tmp1);
824 out[JPEG_PIX_SZ*2] = scale_output(tmp22 + tmp2);
825 out[JPEG_PIX_SZ*13] = scale_output(tmp22 - tmp2);
826 out[JPEG_PIX_SZ*3] = scale_output(tmp23 + tmp3);
827 out[JPEG_PIX_SZ*12] = scale_output(tmp23 - tmp3);
828 out[JPEG_PIX_SZ*4] = scale_output(tmp24 + tmp10);
829 out[JPEG_PIX_SZ*11] = scale_output(tmp24 - tmp10);
830 out[JPEG_PIX_SZ*5] = scale_output(tmp25 + tmp11);
831 out[JPEG_PIX_SZ*10] = scale_output(tmp25 - tmp11);
832 out[JPEG_PIX_SZ*6] = scale_output(tmp26 + tmp12);
833 out[JPEG_PIX_SZ*9] = scale_output(tmp26 - tmp12);
834 out[JPEG_PIX_SZ*7] = scale_output(tmp27 + tmp13);
835 out[JPEG_PIX_SZ*8] = scale_output(tmp27 - tmp13);
838 #endif
840 struct idct_entry {
841 int scale;
842 void (*v_idct)(int16_t *ws, int16_t *end);
843 void (*h_idct)(int16_t *ws, unsigned char *out, int16_t *end, int rowstep);
846 static const struct idct_entry idct_tbl[] = {
847 { PASS1_BITS, NULL, jpeg_idct1h },
848 { PASS1_BITS, jpeg_idct2v, jpeg_idct2h },
849 { 0, jpeg_idct4v, jpeg_idct4h },
850 { 0, jpeg_idct8v, jpeg_idct8h },
851 #ifdef HAVE_LCD_COLOR
852 { 0, jpeg_idct16v, jpeg_idct16h },
853 #endif
856 /* JPEG decoder implementation */
858 #ifdef JPEG_FROM_MEM
859 INLINE unsigned char *jpeg_getc(struct jpeg* p_jpeg)
861 if (LIKELY(p_jpeg->len))
863 p_jpeg->len--;
864 return p_jpeg->data++;
865 } else
866 return NULL;
869 INLINE bool skip_bytes(struct jpeg* p_jpeg, int count)
871 if (p_jpeg->len >= (unsigned)count)
873 p_jpeg->len -= count;
874 p_jpeg->data += count;
875 return true;
876 } else {
877 p_jpeg->data += p_jpeg->len;
878 p_jpeg->len = 0;
879 return false;
883 INLINE void jpeg_putc(struct jpeg* p_jpeg)
885 p_jpeg->len++;
886 p_jpeg->data--;
888 #else
889 INLINE void fill_buf(struct jpeg* p_jpeg)
891 p_jpeg->buf_left = read(p_jpeg->fd, p_jpeg->buf,
892 (p_jpeg->len >= JPEG_READ_BUF_SIZE)?
893 JPEG_READ_BUF_SIZE : p_jpeg->len);
894 p_jpeg->buf_index = 0;
895 if (p_jpeg->buf_left > 0)
896 p_jpeg->len -= p_jpeg->buf_left;
899 static unsigned char *jpeg_getc(struct jpeg* p_jpeg)
901 if (UNLIKELY(p_jpeg->buf_left < 1))
902 fill_buf(p_jpeg);
903 if (UNLIKELY(p_jpeg->buf_left < 1))
904 return NULL;
905 p_jpeg->buf_left--;
906 return (p_jpeg->buf_index++) + p_jpeg->buf;
909 INLINE bool skip_bytes_seek(struct jpeg* p_jpeg)
911 if (UNLIKELY(lseek(p_jpeg->fd, -p_jpeg->buf_left, SEEK_CUR) < 0))
912 return false;
913 p_jpeg->buf_left = 0;
914 return true;
917 static bool skip_bytes(struct jpeg* p_jpeg, int count)
919 p_jpeg->buf_left -= count;
920 p_jpeg->buf_index += count;
921 return p_jpeg->buf_left >= 0 || skip_bytes_seek(p_jpeg);
924 static void jpeg_putc(struct jpeg* p_jpeg)
926 p_jpeg->buf_left++;
927 p_jpeg->buf_index--;
929 #endif
931 #define e_skip_bytes(jpeg, count) \
932 do {\
933 if (UNLIKELY(!skip_bytes((jpeg),(count)))) \
934 return -1; \
935 } while (0)
937 #define e_getc(jpeg, code) \
938 ({ \
939 unsigned char *c; \
940 if (UNLIKELY(!(c = jpeg_getc(jpeg)))) \
941 return (code); \
942 *c; \
945 #define d_getc(jpeg, def) \
946 ({ \
947 unsigned char *cp = jpeg_getc(jpeg); \
948 unsigned char c = LIKELY(cp) ? *cp : (def); \
949 c; \
952 /* Preprocess the JPEG JFIF file */
953 static int process_markers(struct jpeg* p_jpeg)
955 unsigned char c;
956 int marker_size; /* variable length of marker segment */
957 int i, j, n;
958 int ret = 0; /* returned flags */
960 while ((c = e_getc(p_jpeg, -1)))
962 if (c != 0xFF) /* no marker? */
964 JDEBUGF("Non-marker data\n");
965 jpeg_putc(p_jpeg);
966 break; /* exit marker processing */
969 c = e_getc(p_jpeg, -1);
970 JDEBUGF("marker value %X\n",c);
971 switch (c)
973 case 0xFF: /* Fill byte */
974 ret |= FILL_FF;
975 case 0x00: /* Zero stuffed byte - entropy data */
976 jpeg_putc(p_jpeg);
977 continue;
979 case 0xC0: /* SOF Huff - Baseline DCT */
981 JDEBUGF("SOF marker ");
982 ret |= SOF0;
983 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
984 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */
985 JDEBUGF("len: %d\n", marker_size);
986 n = e_getc(p_jpeg, -1); /* sample precision (= 8 or 12) */
987 if (n != 8)
989 return(-1); /* Unsupported sample precision */
991 p_jpeg->y_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
992 p_jpeg->y_size |= e_getc(p_jpeg, -1); /* Lowbyte */
993 p_jpeg->x_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
994 p_jpeg->x_size |= e_getc(p_jpeg, -1); /* Lowbyte */
995 JDEBUGF(" dimensions: %dx%d\n", p_jpeg->x_size,
996 p_jpeg->y_size);
998 n = (marker_size-2-6)/3;
999 if (e_getc(p_jpeg, -1) != n || (n != 1 && n != 3))
1001 return(-2); /* Unsupported SOF0 component specification */
1003 for (i=0; i<n; i++)
1005 /* Component info */
1006 p_jpeg->frameheader[i].ID = e_getc(p_jpeg, -1);
1007 p_jpeg->frameheader[i].horizontal_sampling =
1008 (c = e_getc(p_jpeg, -1)) >> 4;
1009 p_jpeg->frameheader[i].vertical_sampling = c & 0x0F;
1010 p_jpeg->frameheader[i].quanttable_select =
1011 e_getc(p_jpeg, -1);
1012 if (p_jpeg->frameheader[i].horizontal_sampling > 2
1013 || p_jpeg->frameheader[i].vertical_sampling > 2)
1014 return -3; /* Unsupported SOF0 subsampling */
1016 p_jpeg->blocks = n;
1018 break;
1020 case 0xC1: /* SOF Huff - Extended sequential DCT*/
1021 case 0xC2: /* SOF Huff - Progressive DCT*/
1022 case 0xC3: /* SOF Huff - Spatial (sequential) lossless*/
1023 case 0xC5: /* SOF Huff - Differential sequential DCT*/
1024 case 0xC6: /* SOF Huff - Differential progressive DCT*/
1025 case 0xC7: /* SOF Huff - Differential spatial*/
1026 case 0xC8: /* SOF Arith - Reserved for JPEG extensions*/
1027 case 0xC9: /* SOF Arith - Extended sequential DCT*/
1028 case 0xCA: /* SOF Arith - Progressive DCT*/
1029 case 0xCB: /* SOF Arith - Spatial (sequential) lossless*/
1030 case 0xCD: /* SOF Arith - Differential sequential DCT*/
1031 case 0xCE: /* SOF Arith - Differential progressive DCT*/
1032 case 0xCF: /* SOF Arith - Differential spatial*/
1034 return (-4); /* other DCT model than baseline not implemented */
1037 case 0xC4: /* Define Huffman Table(s) */
1039 ret |= DHT;
1040 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
1041 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */
1042 marker_size -= 2;
1044 while (marker_size > 17) /* another table */
1046 c = e_getc(p_jpeg, -1);
1047 marker_size--;
1048 int sum = 0;
1049 i = c & 0x0F; /* table index */
1050 if (i > 1)
1052 return (-5); /* Huffman table index out of range */
1053 } else {
1054 if (c & 0xF0) /* AC table */
1056 for (j=0; j<16; j++)
1058 p_jpeg->hufftable[i].huffmancodes_ac[j] =
1059 (c = e_getc(p_jpeg, -1));
1060 sum += c;
1061 marker_size -= 1;
1063 if(16 + sum > AC_LEN)
1064 return -10; /* longer than allowed */
1066 for (; j < 16 + sum; j++)
1068 p_jpeg->hufftable[i].huffmancodes_ac[j] =
1069 e_getc(p_jpeg, -1);
1070 marker_size--;
1073 else /* DC table */
1075 for (j=0; j<16; j++)
1077 p_jpeg->hufftable[i].huffmancodes_dc[j] =
1078 (c = e_getc(p_jpeg, -1));
1079 sum += c;
1080 marker_size--;
1082 if(16 + sum > DC_LEN)
1083 return -11; /* longer than allowed */
1085 for (; j < 16 + sum; j++)
1087 p_jpeg->hufftable[i].huffmancodes_dc[j] =
1088 e_getc(p_jpeg, -1);
1089 marker_size--;
1093 } /* while */
1094 e_skip_bytes(p_jpeg, marker_size);
1096 break;
1098 case 0xCC: /* Define Arithmetic coding conditioning(s) */
1099 return(-6); /* Arithmetic coding not supported */
1101 case 0xD8: /* Start of Image */
1102 JDEBUGF("SOI\n");
1103 break;
1104 case 0xD9: /* End of Image */
1105 JDEBUGF("EOI\n");
1106 break;
1107 case 0x01: /* for temp private use arith code */
1108 JDEBUGF("private\n");
1109 break; /* skip parameterless marker */
1112 case 0xDA: /* Start of Scan */
1114 ret |= SOS;
1115 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
1116 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */
1117 marker_size -= 2;
1119 n = (marker_size-1-3)/2;
1120 if (e_getc(p_jpeg, -1) != n || (n != 1 && n != 3))
1122 return (-7); /* Unsupported SOS component specification */
1124 marker_size--;
1125 for (i=0; i<n; i++)
1127 p_jpeg->scanheader[i].ID = e_getc(p_jpeg, -1);
1128 p_jpeg->scanheader[i].DC_select = (c = e_getc(p_jpeg, -1))
1129 >> 4;
1130 p_jpeg->scanheader[i].AC_select = c & 0x0F;
1131 marker_size -= 2;
1133 /* skip spectral information */
1134 e_skip_bytes(p_jpeg, marker_size);
1136 break;
1138 case 0xDB: /* Define quantization Table(s) */
1140 ret |= DQT;
1141 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
1142 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */
1143 marker_size -= 2;
1145 n = (marker_size)/(QUANT_TABLE_LENGTH+1); /* # of tables */
1146 for (i=0; i<n; i++)
1148 int id = e_getc(p_jpeg, -1); /* ID */
1149 marker_size--;
1150 if (id >= 4)
1152 return (-8); /* Unsupported quantization table */
1154 /* Read Quantisation table: */
1155 for (j=0; j<QUANT_TABLE_LENGTH; j++)
1157 p_jpeg->quanttable[id][j] = e_getc(p_jpeg, -1);
1158 marker_size--;
1161 e_skip_bytes(p_jpeg, marker_size);
1163 break;
1165 case 0xDD: /* Define Restart Interval */
1167 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
1168 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */
1169 marker_size -= 4;
1170 /* Highbyte */
1171 p_jpeg->restart_interval = e_getc(p_jpeg, -1) << 8;
1172 p_jpeg->restart_interval |= e_getc(p_jpeg, -1); /* Lowbyte */
1173 e_skip_bytes(p_jpeg, marker_size); /* skip segment */
1175 break;
1177 case 0xDC: /* Define Number of Lines */
1178 case 0xDE: /* Define Hierarchical progression */
1179 case 0xDF: /* Expand Reference Component(s) */
1180 case 0xE0: /* Application Field 0*/
1181 case 0xE1: /* Application Field 1*/
1182 case 0xE2: /* Application Field 2*/
1183 case 0xE3: /* Application Field 3*/
1184 case 0xE4: /* Application Field 4*/
1185 case 0xE5: /* Application Field 5*/
1186 case 0xE6: /* Application Field 6*/
1187 case 0xE7: /* Application Field 7*/
1188 case 0xE8: /* Application Field 8*/
1189 case 0xE9: /* Application Field 9*/
1190 case 0xEA: /* Application Field 10*/
1191 case 0xEB: /* Application Field 11*/
1192 case 0xEC: /* Application Field 12*/
1193 case 0xED: /* Application Field 13*/
1194 case 0xEE: /* Application Field 14*/
1195 case 0xEF: /* Application Field 15*/
1196 case 0xFE: /* Comment */
1198 marker_size = e_getc(p_jpeg, -1) << 8; /* Highbyte */
1199 marker_size |= e_getc(p_jpeg, -1); /* Lowbyte */
1200 marker_size -= 2;
1201 JDEBUGF("unhandled marker len %d\n", marker_size);
1202 e_skip_bytes(p_jpeg, marker_size); /* skip segment */
1204 break;
1206 case 0xF0: /* Reserved for JPEG extensions */
1207 case 0xF1: /* Reserved for JPEG extensions */
1208 case 0xF2: /* Reserved for JPEG extensions */
1209 case 0xF3: /* Reserved for JPEG extensions */
1210 case 0xF4: /* Reserved for JPEG extensions */
1211 case 0xF5: /* Reserved for JPEG extensions */
1212 case 0xF6: /* Reserved for JPEG extensions */
1213 case 0xF7: /* Reserved for JPEG extensions */
1214 case 0xF8: /* Reserved for JPEG extensions */
1215 case 0xF9: /* Reserved for JPEG extensions */
1216 case 0xFA: /* Reserved for JPEG extensions */
1217 case 0xFB: /* Reserved for JPEG extensions */
1218 case 0xFC: /* Reserved for JPEG extensions */
1219 case 0xFD: /* Reserved for JPEG extensions */
1220 case 0x02: /* Reserved */
1221 default:
1222 return (-9); /* Unknown marker */
1223 } /* switch */
1224 } /* while */
1226 return (ret); /* return flags with seen markers */
1229 static const struct huffman_table luma_table =
1232 0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
1233 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B
1236 0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,
1237 0x01,0x7D,0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,
1238 0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,
1239 0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,0x33,0x62,0x72,0x82,0x09,0x0A,0x16,
1240 0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,0x35,0x36,0x37,
1241 0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,
1242 0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,
1243 0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
1244 0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,0xA4,0xA5,
1245 0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,
1246 0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,
1247 0xD7,0xD8,0xD9,0xDA,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,
1248 0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA
1252 static const struct huffman_table chroma_table =
1255 0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,
1256 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B
1259 0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01,
1260 0x02,0x77,0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,
1261 0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,
1262 0xC1,0x09,0x23,0x33,0x52,0xF0,0x15,0x62,0x72,0xD1,0x0A,0x16,0x24,0x34,
1263 0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,0x27,0x28,0x29,0x2A,0x35,0x36,
1264 0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,
1265 0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
1266 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87,
1267 0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,
1268 0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,
1269 0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,
1270 0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,
1271 0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA
1275 static void default_huff_tbl(struct jpeg* p_jpeg)
1278 MEMCPY(&p_jpeg->hufftable[0], &luma_table, sizeof(luma_table));
1279 MEMCPY(&p_jpeg->hufftable[1], &chroma_table, sizeof(chroma_table));
1281 return;
1284 /* Compute the derived values for a Huffman table */
1285 static void fix_huff_tbl(int* htbl, struct derived_tbl* dtbl)
1287 int p, i, l, si;
1288 int lookbits, ctr;
1289 char huffsize[257];
1290 unsigned int huffcode[257];
1291 unsigned int code;
1293 dtbl->pub = htbl; /* fill in back link */
1295 /* Figure C.1: make table of Huffman code length for each symbol */
1296 /* Note that this is in code-length order. */
1298 p = 0;
1299 for (l = 1; l <= 16; l++)
1300 { /* all possible code length */
1301 for (i = 1; i <= (int) htbl[l-1]; i++) /* all codes per length */
1302 huffsize[p++] = (char) l;
1304 huffsize[p] = 0;
1306 /* Figure C.2: generate the codes themselves */
1307 /* Note that this is in code-length order. */
1309 code = 0;
1310 si = huffsize[0];
1311 p = 0;
1312 while (huffsize[p])
1314 while (((int) huffsize[p]) == si)
1316 huffcode[p++] = code;
1317 code++;
1319 code <<= 1;
1320 si++;
1323 /* Figure F.15: generate decoding tables for bit-sequential decoding */
1325 p = 0;
1326 for (l = 1; l <= 16; l++)
1328 if (htbl[l-1])
1330 /* huffval[] index of 1st symbol of code length l */
1331 dtbl->valptr[l] = p;
1332 dtbl->mincode[l] = huffcode[p]; /* minimum code of length l */
1333 p += htbl[l-1];
1334 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
1336 else
1338 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
1341 dtbl->maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */
1343 /* Compute lookahead tables to speed up decoding.
1344 * First we set all the table entries to 0, indicating "too long";
1345 * then we iterate through the Huffman codes that are short enough and
1346 * fill in all the entries that correspond to bit sequences starting
1347 * with that code.
1350 MEMSET(dtbl->look_nbits, 0, sizeof(dtbl->look_nbits));
1352 p = 0;
1353 for (l = 1; l <= HUFF_LOOKAHEAD; l++)
1355 for (i = 1; i <= (int) htbl[l-1]; i++, p++)
1357 /* l = current code's length, p = its index in huffcode[] &
1358 * huffval[]. Generate left-justified code followed by all possible
1359 * bit sequences
1361 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
1362 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--)
1364 dtbl->look_nbits[lookbits] = l;
1365 dtbl->look_sym[lookbits] = htbl[16+p];
1366 lookbits++;
1373 /* zag[i] is the natural-order position of the i'th element of zigzag order. */
1374 static const unsigned char zag[] =
1376 #ifdef JPEG_IDCT_TRANSPOSE
1377 0, 8, 1, 2, 9, 16, 24, 17,
1378 10, 3, 4, 11, 18, 25, 32, 40,
1379 33, 26, 19, 12, 5, 6, 13, 20,
1380 27, 34, 41, 48, 56, 49, 42, 35,
1381 28, 21, 14, 7, 15, 22, 29, 36,
1382 43, 50, 57, 58, 51, 44, 37, 30,
1383 23, 31, 38, 45, 52, 59, 60, 53,
1384 46, 39, 47, 54, 61, 62, 55, 63,
1385 #endif
1386 0, 1, 8, 16, 9, 2, 3, 10,
1387 17, 24, 32, 25, 18, 11, 4, 5,
1388 12, 19, 26, 33, 40, 48, 41, 34,
1389 27, 20, 13, 6, 7, 14, 21, 28,
1390 35, 42, 49, 56, 57, 50, 43, 36,
1391 29, 22, 15, 23, 30, 37, 44, 51,
1392 58, 59, 52, 45, 38, 31, 39, 46,
1393 53, 60, 61, 54, 47, 55, 62, 63,
1396 /* zig[i] is the the zig-zag order position of the i'th element of natural
1397 * order, reading left-to-right then top-to-bottom.
1399 static const unsigned char zig[] =
1401 0, 1, 5, 6, 14, 15, 27, 28,
1402 2, 4, 7, 13, 16, 26, 29, 42,
1403 3, 8, 12, 17, 25, 30, 41, 43,
1404 9, 11, 18, 24, 31, 40, 44, 53,
1405 10, 19, 23, 32, 39, 45, 52, 54,
1406 20, 22, 33, 38, 46, 51, 55, 60,
1407 21, 34, 37, 47, 50, 56, 59, 61,
1408 35, 36, 48, 49, 57, 58, 62, 63
1411 /* Reformat some image header data so that the decoder can use it properly. */
1412 INLINE void fix_headers(struct jpeg* p_jpeg)
1414 int i;
1416 for (i=0; i<4; i++)
1417 p_jpeg->store_pos[i] = i; /* default ordering */
1419 /* assignments for the decoding of blocks */
1420 if (p_jpeg->frameheader[0].horizontal_sampling == 2
1421 && p_jpeg->frameheader[0].vertical_sampling == 1)
1422 { /* 4:2:2 */
1423 p_jpeg->blocks = 4;
1424 p_jpeg->x_mbl = (p_jpeg->x_size+15) / 16;
1425 p_jpeg->x_phys = p_jpeg->x_mbl * 16;
1426 p_jpeg->y_mbl = (p_jpeg->y_size+7) / 8;
1427 p_jpeg->y_phys = p_jpeg->y_mbl * 8;
1428 p_jpeg->mcu_membership[0] = 0; /* Y1=Y2=0, U=1, V=2 */
1429 p_jpeg->mcu_membership[1] = 0;
1430 p_jpeg->mcu_membership[2] = 1;
1431 p_jpeg->mcu_membership[3] = 2;
1432 p_jpeg->tab_membership[0] = 0; /* DC, DC, AC, AC */
1433 p_jpeg->tab_membership[1] = 0;
1434 p_jpeg->tab_membership[2] = 1;
1435 p_jpeg->tab_membership[3] = 1;
1436 p_jpeg->subsample_x[0] = 1;
1437 p_jpeg->subsample_x[1] = 2;
1438 p_jpeg->subsample_x[2] = 2;
1439 p_jpeg->subsample_y[0] = 1;
1440 p_jpeg->subsample_y[1] = 1;
1441 p_jpeg->subsample_y[2] = 1;
1443 if (p_jpeg->frameheader[0].horizontal_sampling == 1
1444 && p_jpeg->frameheader[0].vertical_sampling == 2)
1445 { /* 4:2:2 vertically subsampled */
1446 p_jpeg->store_pos[1] = 2; /* block positions are mirrored */
1447 p_jpeg->store_pos[2] = 1;
1448 p_jpeg->blocks = 4;
1449 p_jpeg->x_mbl = (p_jpeg->x_size+7) / 8;
1450 p_jpeg->x_phys = p_jpeg->x_mbl * 8;
1451 p_jpeg->y_mbl = (p_jpeg->y_size+15) / 16;
1452 p_jpeg->y_phys = p_jpeg->y_mbl * 16;
1453 p_jpeg->mcu_membership[0] = 0; /* Y1=Y2=0, U=1, V=2 */
1454 p_jpeg->mcu_membership[1] = 0;
1455 p_jpeg->mcu_membership[2] = 1;
1456 p_jpeg->mcu_membership[3] = 2;
1457 p_jpeg->tab_membership[0] = 0; /* DC, DC, AC, AC */
1458 p_jpeg->tab_membership[1] = 0;
1459 p_jpeg->tab_membership[2] = 1;
1460 p_jpeg->tab_membership[3] = 1;
1461 p_jpeg->subsample_x[0] = 1;
1462 p_jpeg->subsample_x[1] = 1;
1463 p_jpeg->subsample_x[2] = 1;
1464 p_jpeg->subsample_y[0] = 1;
1465 p_jpeg->subsample_y[1] = 2;
1466 p_jpeg->subsample_y[2] = 2;
1468 else if (p_jpeg->frameheader[0].horizontal_sampling == 2
1469 && p_jpeg->frameheader[0].vertical_sampling == 2)
1470 { /* 4:2:0 */
1471 p_jpeg->blocks = 6;
1472 p_jpeg->x_mbl = (p_jpeg->x_size+15) / 16;
1473 p_jpeg->x_phys = p_jpeg->x_mbl * 16;
1474 p_jpeg->y_mbl = (p_jpeg->y_size+15) / 16;
1475 p_jpeg->y_phys = p_jpeg->y_mbl * 16;
1476 p_jpeg->mcu_membership[0] = 0;
1477 p_jpeg->mcu_membership[1] = 0;
1478 p_jpeg->mcu_membership[2] = 0;
1479 p_jpeg->mcu_membership[3] = 0;
1480 p_jpeg->mcu_membership[4] = 1;
1481 p_jpeg->mcu_membership[5] = 2;
1482 p_jpeg->tab_membership[0] = 0;
1483 p_jpeg->tab_membership[1] = 0;
1484 p_jpeg->tab_membership[2] = 0;
1485 p_jpeg->tab_membership[3] = 0;
1486 p_jpeg->tab_membership[4] = 1;
1487 p_jpeg->tab_membership[5] = 1;
1488 p_jpeg->subsample_x[0] = 1;
1489 p_jpeg->subsample_x[1] = 2;
1490 p_jpeg->subsample_x[2] = 2;
1491 p_jpeg->subsample_y[0] = 1;
1492 p_jpeg->subsample_y[1] = 2;
1493 p_jpeg->subsample_y[2] = 2;
1495 else if (p_jpeg->frameheader[0].horizontal_sampling == 1
1496 && p_jpeg->frameheader[0].vertical_sampling == 1)
1497 { /* 4:4:4 */
1498 /* don't overwrite p_jpeg->blocks */
1499 p_jpeg->x_mbl = (p_jpeg->x_size+7) / 8;
1500 p_jpeg->x_phys = p_jpeg->x_mbl * 8;
1501 p_jpeg->y_mbl = (p_jpeg->y_size+7) / 8;
1502 p_jpeg->y_phys = p_jpeg->y_mbl * 8;
1503 p_jpeg->mcu_membership[0] = 0;
1504 p_jpeg->mcu_membership[1] = 1;
1505 p_jpeg->mcu_membership[2] = 2;
1506 p_jpeg->tab_membership[0] = 0;
1507 p_jpeg->tab_membership[1] = 1;
1508 p_jpeg->tab_membership[2] = 1;
1509 p_jpeg->subsample_x[0] = 1;
1510 p_jpeg->subsample_x[1] = 1;
1511 p_jpeg->subsample_x[2] = 1;
1512 p_jpeg->subsample_y[0] = 1;
1513 p_jpeg->subsample_y[1] = 1;
1514 p_jpeg->subsample_y[2] = 1;
1516 else
1518 /* error */
1523 INLINE void fix_huff_tables(struct jpeg *p_jpeg)
1525 fix_huff_tbl(p_jpeg->hufftable[0].huffmancodes_dc,
1526 &p_jpeg->dc_derived_tbls[0]);
1527 fix_huff_tbl(p_jpeg->hufftable[0].huffmancodes_ac,
1528 &p_jpeg->ac_derived_tbls[0]);
1529 fix_huff_tbl(p_jpeg->hufftable[1].huffmancodes_dc,
1530 &p_jpeg->dc_derived_tbls[1]);
1531 fix_huff_tbl(p_jpeg->hufftable[1].huffmancodes_ac,
1532 &p_jpeg->ac_derived_tbls[1]);
1535 /* Because some of the IDCT routines never multiply by any constants, and
1536 * therefore do not produce shifted output, we add the shift into the
1537 * quantization table when one of these IDCT routines is used, rather than
1538 * have the IDCT shift each value it processes.
1540 INLINE void fix_quant_tables(struct jpeg *p_jpeg)
1542 int shift, i, j;
1543 for (i = 0; i < 2; i++)
1545 shift = idct_tbl[p_jpeg->v_scale[i]].scale;
1546 if (shift)
1548 for (j = 0; j < 64; j++)
1549 p_jpeg->quanttable[i][j] <<= shift;
1555 * These functions/macros provide the in-line portion of bit fetching.
1556 * Use check_bit_buffer to ensure there are N bits in get_buffer
1557 * before using get_bits, peek_bits, or drop_bits.
1558 * check_bit_buffer(state,n,action);
1559 * Ensure there are N bits in get_buffer; if suspend, take action.
1560 * val = get_bits(n);
1561 * Fetch next N bits.
1562 * val = peek_bits(n);
1563 * Fetch next N bits without removing them from the buffer.
1564 * drop_bits(n);
1565 * Discard next N bits.
1566 * The value N should be a simple variable, not an expression, because it
1567 * is evaluated multiple times.
1570 static void fill_bit_buffer(struct jpeg* p_jpeg)
1572 unsigned char byte, marker;
1574 if (p_jpeg->marker_val)
1575 p_jpeg->marker_ind += 16;
1576 byte = d_getc(p_jpeg, 0);
1577 if (UNLIKELY(byte == 0xFF)) /* legal marker can be byte stuffing or RSTm */
1578 { /* simplification: just skip the (one-byte) marker code */
1579 marker = d_getc(p_jpeg, 0);
1580 if ((marker & ~7) == 0xD0)
1582 p_jpeg->marker_val = marker;
1583 p_jpeg->marker_ind = 8;
1586 p_jpeg->bitbuf = (p_jpeg->bitbuf << 8) | byte;
1588 byte = d_getc(p_jpeg, 0);
1589 if (UNLIKELY(byte == 0xFF)) /* legal marker can be byte stuffing or RSTm */
1590 { /* simplification: just skip the (one-byte) marker code */
1591 marker = d_getc(p_jpeg, 0);
1592 if ((marker & ~7) == 0xD0)
1594 p_jpeg->marker_val = marker;
1595 p_jpeg->marker_ind = 0;
1598 p_jpeg->bitbuf = (p_jpeg->bitbuf << 8) | byte;
1599 p_jpeg->bitbuf_bits += 16;
1600 #ifdef JPEG_BS_DEBUG
1601 DEBUGF("read in: %04X\n", p_jpeg->bitbuf & 0xFFFF);
1602 #endif
1605 INLINE void check_bit_buffer(struct jpeg *p_jpeg, int nbits)
1607 if (nbits > p_jpeg->bitbuf_bits)
1608 fill_bit_buffer(p_jpeg);
1611 INLINE int get_bits(struct jpeg *p_jpeg, int nbits)
1613 #ifdef JPEG_BS_DEBUG
1614 if (nbits > p_jpeg->bitbuf_bits)
1615 DEBUGF("bitbuffer underrun\n");
1616 int mask = BIT_N(p_jpeg->bitbuf_bits - 1);
1617 int i;
1618 DEBUGF("get %d bits: ", nbits);
1619 for (i = 0; i < nbits; i++)
1620 DEBUGF("%d",!!(p_jpeg->bitbuf & (mask >>= 1)));
1621 DEBUGF("\n");
1622 #endif
1623 return ((int) (p_jpeg->bitbuf >> (p_jpeg->bitbuf_bits -= nbits))) &
1624 (BIT_N(nbits)-1);
1627 INLINE int peek_bits(struct jpeg *p_jpeg, int nbits)
1629 #ifdef JPEG_BS_DEBUG
1630 int mask = BIT_N(p_jpeg->bitbuf_bits - 1);
1631 int i;
1632 DEBUGF("peek %d bits: ", nbits);
1633 for (i = 0; i < nbits; i++)
1634 DEBUGF("%d",!!(p_jpeg->bitbuf & (mask >>= 1)));
1635 DEBUGF("\n");
1636 #endif
1637 return ((int) (p_jpeg->bitbuf >> (p_jpeg->bitbuf_bits - nbits))) &
1638 (BIT_N(nbits)-1);
1641 INLINE void drop_bits(struct jpeg *p_jpeg, int nbits)
1643 #ifdef JPEG_BS_DEBUG
1644 int mask = BIT_N(p_jpeg->bitbuf_bits - 1);
1645 int i;
1646 DEBUGF("drop %d bits: ", nbits);
1647 for (i = 0; i < nbits; i++)
1648 DEBUGF("%d",!!(p_jpeg->bitbuf & (mask >>= 1)));
1649 DEBUGF("\n");
1650 #endif
1651 p_jpeg->bitbuf_bits -= nbits;
1654 /* re-synchronize to entropy data (skip restart marker) */
1655 static void search_restart(struct jpeg *p_jpeg)
1657 if (p_jpeg->marker_val)
1659 p_jpeg->marker_val = 0;
1660 p_jpeg->bitbuf_bits = p_jpeg->marker_ind;
1661 p_jpeg->marker_ind = 0;
1662 return;
1664 unsigned char byte;
1665 p_jpeg->bitbuf_bits = 0;
1666 while ((byte = d_getc(p_jpeg, 0xFF)))
1668 if (byte == 0xff)
1670 byte = d_getc(p_jpeg, 0xD0);
1671 if ((byte & ~7) == 0xD0)
1673 return;
1675 else
1676 jpeg_putc(p_jpeg);
1681 /* Figure F.12: extend sign bit. */
1682 #if CONFIG_CPU == SH7034
1683 /* SH1 lacks a variable-shift instruction */
1684 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
1686 static const int extend_test[16] = /* entry n is 2**(n-1) */
1688 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
1689 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
1692 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
1694 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
1695 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
1696 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
1697 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1
1699 #else
1700 /* This saves some code and data size, benchmarks about the same on RAM */
1701 #define HUFF_EXTEND(x,s) \
1702 ({ \
1703 int x__ = x; \
1704 int s__ = s; \
1705 x__ & BIT_N(s__- 1) ? x__ : x__ + (-1 << s__) + 1; \
1707 #endif
1709 /* Decode a single value */
1710 #define huff_decode_dc(p_jpeg, tbl, s, r) \
1712 int nb, look; \
1714 check_bit_buffer((p_jpeg), HUFF_LOOKAHEAD); \
1715 look = peek_bits((p_jpeg), HUFF_LOOKAHEAD); \
1716 if ((nb = (tbl)->look_nbits[look]) != 0) \
1718 drop_bits((p_jpeg), nb); \
1719 s = (tbl)->look_sym[look]; \
1720 check_bit_buffer((p_jpeg), s); \
1721 r = get_bits((p_jpeg), s); \
1722 } else { \
1723 /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */ \
1724 long code; \
1725 nb=HUFF_LOOKAHEAD+1; \
1726 check_bit_buffer((p_jpeg), nb); \
1727 code = get_bits((p_jpeg), nb); \
1728 while (code > (tbl)->maxcode[nb]) \
1730 code <<= 1; \
1731 check_bit_buffer((p_jpeg), 1); \
1732 code |= get_bits((p_jpeg), 1); \
1733 nb++; \
1735 if (nb > 16) /* error in Huffman */ \
1737 r = 0; s = 0; /* fake a zero, this is most safe */ \
1738 } else { \
1739 s = (tbl)->pub[16 + (tbl)->valptr[nb] + \
1740 ((int) (code - (tbl)->mincode[nb]))]; \
1741 check_bit_buffer((p_jpeg), s); \
1742 r = get_bits((p_jpeg), s); \
1744 } /* end slow decode */ \
1747 #define huff_decode_ac(p_jpeg, tbl, s) \
1749 int nb, look; \
1751 check_bit_buffer((p_jpeg), HUFF_LOOKAHEAD); \
1752 look = peek_bits((p_jpeg), HUFF_LOOKAHEAD); \
1753 if ((nb = (tbl)->look_nbits[look]) != 0) \
1755 drop_bits((p_jpeg), nb); \
1756 s = (tbl)->look_sym[look]; \
1757 } else { \
1758 /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */ \
1759 long code; \
1760 nb=HUFF_LOOKAHEAD+1; \
1761 check_bit_buffer((p_jpeg), nb); \
1762 code = get_bits((p_jpeg), nb); \
1763 while (code > (tbl)->maxcode[nb]) \
1765 code <<= 1; \
1766 check_bit_buffer((p_jpeg), 1); \
1767 code |= get_bits((p_jpeg), 1); \
1768 nb++; \
1770 if (nb > 16) /* error in Huffman */ \
1772 s = 0; /* fake a zero, this is most safe */ \
1773 } else { \
1774 s = (tbl)->pub[16 + (tbl)->valptr[nb] + \
1775 ((int) (code - (tbl)->mincode[nb]))]; \
1777 } /* end slow decode */ \
1780 static struct img_part *store_row_jpeg(void *jpeg_args)
1782 struct jpeg *p_jpeg = (struct jpeg*) jpeg_args;
1783 #ifdef HAVE_LCD_COLOR
1784 int mcu_hscale = p_jpeg->h_scale[1];
1785 int mcu_vscale = p_jpeg->v_scale[1];
1786 #else
1787 int mcu_hscale = (p_jpeg->h_scale[0] +
1788 p_jpeg->frameheader[0].horizontal_sampling - 1);
1789 int mcu_vscale = (p_jpeg->v_scale[0] +
1790 p_jpeg->frameheader[0].vertical_sampling - 1);
1791 #endif
1792 unsigned int width = p_jpeg->x_mbl << mcu_hscale;
1793 unsigned int b_width = width * JPEG_PIX_SZ;
1794 int height = BIT_N(mcu_vscale);
1795 int x;
1796 if (!p_jpeg->mcu_row) /* Need to decode a new row of MCUs */
1798 p_jpeg->out_ptr = (unsigned char *)p_jpeg->img_buf;
1799 int store_offs[4];
1800 #ifdef HAVE_LCD_COLOR
1801 unsigned mcu_width = BIT_N(mcu_hscale);
1802 #endif
1803 int mcu_offset = JPEG_PIX_SZ << mcu_hscale;
1804 unsigned char *out = p_jpeg->out_ptr;
1805 store_offs[p_jpeg->store_pos[0]] = 0;
1806 store_offs[p_jpeg->store_pos[1]] = JPEG_PIX_SZ << p_jpeg->h_scale[0];
1807 store_offs[p_jpeg->store_pos[2]] = b_width << p_jpeg->v_scale[0];
1808 store_offs[p_jpeg->store_pos[3]] = store_offs[1] + store_offs[2];
1809 /* decoded DCT coefficients */
1810 int16_t block[IDCT_WS_SIZE] __attribute__((aligned(8)));
1811 for (x = 0; x < p_jpeg->x_mbl; x++)
1813 int blkn;
1814 for (blkn = 0; blkn < p_jpeg->blocks; blkn++)
1816 int ci = p_jpeg->mcu_membership[blkn]; /* component index */
1817 int ti = p_jpeg->tab_membership[blkn]; /* table index */
1818 #ifdef JPEG_IDCT_TRANSPOSE
1819 bool transpose = p_jpeg->v_scale[!!ci] > 2;
1820 #endif
1821 int k = 1; /* coefficient index */
1822 int s, r; /* huffman values */
1823 struct derived_tbl* dctbl = &p_jpeg->dc_derived_tbls[ti];
1824 struct derived_tbl* actbl = &p_jpeg->ac_derived_tbls[ti];
1826 /* Section F.2.2.1: decode the DC coefficient difference */
1827 huff_decode_dc(p_jpeg, dctbl, s, r);
1829 #ifndef HAVE_LCD_COLOR
1830 if (!ci)
1831 #endif
1833 s = HUFF_EXTEND(r, s);
1834 #ifdef HAVE_LCD_COLOR
1835 p_jpeg->last_dc_val[ci] += s;
1836 /* output it (assumes zag[0] = 0) */
1837 block[0] = MULTIPLY16(p_jpeg->last_dc_val[ci],
1838 p_jpeg->quanttable[!!ci][0]);
1839 #else
1840 p_jpeg->last_dc_val += s;
1841 /* output it (assumes zag[0] = 0) */
1842 block[0] = MULTIPLY16(p_jpeg->last_dc_val,
1843 p_jpeg->quanttable[0][0]);
1844 #endif
1845 /* coefficient buffer must be cleared */
1846 MEMSET(block+1, 0, p_jpeg->zero_need[!!ci] * sizeof(int));
1847 /* Section F.2.2.2: decode the AC coefficients */
1848 while(true)
1850 huff_decode_ac(p_jpeg, actbl, s);
1851 r = s >> 4;
1852 s &= 15;
1853 k += r;
1854 if (s)
1856 check_bit_buffer(p_jpeg, s);
1857 if (k >= p_jpeg->k_need[!!ci])
1858 goto skip_rest;
1859 r = get_bits(p_jpeg, s);
1860 r = HUFF_EXTEND(r, s);
1861 r = MULTIPLY16(r, p_jpeg->quanttable[!!ci][k]);
1862 #ifdef JPEG_IDCT_TRANSPOSE
1863 block[zag[transpose ? k : k + 64]] = r ;
1864 #else
1865 block[zag[k]] = r ;
1866 #endif
1868 else
1870 if (r != 15)
1871 goto block_end;
1873 if ((++k) & 64)
1874 goto block_end;
1875 } /* for k */
1877 for (; k < 64; k++)
1879 huff_decode_ac(p_jpeg, actbl, s);
1880 r = s >> 4;
1881 s &= 15;
1883 if (s)
1885 k += r;
1886 check_bit_buffer(p_jpeg, s);
1887 skip_rest:
1888 drop_bits(p_jpeg, s);
1890 else
1892 if (r != 15)
1893 break;
1894 k += r;
1896 } /* for k */
1897 block_end:
1898 #ifndef HAVE_LCD_COLOR
1899 if (!ci)
1900 #endif
1902 int idct_cols = BIT_N(MIN(p_jpeg->h_scale[!!ci], 3));
1903 int idct_rows = BIT_N(p_jpeg->v_scale[!!ci]);
1904 unsigned char *b_out = out + (ci ? ci : store_offs[blkn]);
1905 if (idct_tbl[p_jpeg->v_scale[!!ci]].v_idct)
1906 #ifdef JPEG_IDCT_TRANSPOSE
1907 idct_tbl[p_jpeg->v_scale[!!ci]].v_idct(block,
1908 transpose ? block + 8 * idct_cols
1909 : block + idct_cols);
1910 uint16_t * h_block = transpose ? block + 64 : block;
1911 idct_tbl[p_jpeg->h_scale[!!ci]].h_idct(h_block, b_out,
1912 h_block + idct_rows * 8, b_width);
1913 #else
1914 idct_tbl[p_jpeg->v_scale[!!ci]].v_idct(block,
1915 block + idct_cols);
1916 idct_tbl[p_jpeg->h_scale[!!ci]].h_idct(block, b_out,
1917 block + idct_rows * 8, b_width);
1918 #endif
1920 } /* for blkn */
1921 /* don't starve other threads while an MCU row decodes */
1922 yield();
1923 #ifdef HAVE_LCD_COLOR
1924 unsigned int xp;
1925 int yp;
1926 unsigned char *row = out;
1927 if (p_jpeg->blocks == 1)
1929 for (yp = 0; yp < height; yp++, row += b_width)
1931 unsigned char *px = row;
1932 for (xp = 0; xp < mcu_width; xp++, px += JPEG_PIX_SZ)
1934 px[1] = px[2] = px[0];
1938 #endif
1939 out += mcu_offset;
1940 if (p_jpeg->restart_interval && --p_jpeg->restart == 0)
1941 { /* if a restart marker is due: */
1942 p_jpeg->restart = p_jpeg->restart_interval; /* count again */
1943 search_restart(p_jpeg); /* align the bitstream */
1944 #ifdef HAVE_LCD_COLOR
1945 p_jpeg->last_dc_val[0] = p_jpeg->last_dc_val[1] =
1946 p_jpeg->last_dc_val[2] = 0; /* reset decoder */
1947 #else
1948 p_jpeg->last_dc_val = 0;
1949 #endif
1952 } /* if !p_jpeg->mcu_row */
1953 p_jpeg->mcu_row = (p_jpeg->mcu_row + 1) & (height - 1);
1954 p_jpeg->part.len = width;
1955 p_jpeg->part.buf = (jpeg_pix_t *)p_jpeg->out_ptr;
1956 p_jpeg->out_ptr += b_width;
1957 return &(p_jpeg->part);
1960 /******************************************************************************
1961 * read_jpeg_file()
1963 * Reads a JPEG file and puts the data in rockbox format in *bitmap.
1965 *****************************************************************************/
1966 #ifndef JPEG_FROM_MEM
1967 int clip_jpeg_file(const char* filename,
1968 int offset,
1969 unsigned long jpeg_size,
1970 struct bitmap *bm,
1971 int maxsize,
1972 int format,
1973 const struct custom_format *cformat)
1975 int fd, ret;
1976 fd = open(filename, O_RDONLY);
1977 JDEBUGF("read_jpeg_file: filename: %s buffer len: %d cformat: %p\n",
1978 filename, maxsize, cformat);
1979 /* Exit if file opening failed */
1980 if (fd < 0) {
1981 DEBUGF("read_jpeg_file: can't open '%s', rc: %d\n", filename, fd);
1982 return fd * 10 - 1;
1984 lseek(fd, offset, SEEK_SET);
1985 ret = clip_jpeg_fd(fd, jpeg_size, bm, maxsize, format, cformat);
1986 close(fd);
1987 return ret;
1990 int read_jpeg_file(const char* filename,
1991 struct bitmap *bm,
1992 int maxsize,
1993 int format,
1994 const struct custom_format *cformat)
1996 return clip_jpeg_file(filename, 0, 0, bm, maxsize, format, cformat);
1998 #endif
2000 static int calc_scale(int in_size, int out_size)
2002 int scale = 0;
2003 out_size <<= 3;
2004 for (scale = 0; scale < 3; scale++)
2006 if (out_size <= in_size)
2007 break;
2008 else
2009 in_size <<= 1;
2011 return scale;
2014 #ifdef JPEG_FROM_MEM
2015 int get_jpeg_dim_mem(unsigned char *data, unsigned long len,
2016 struct dim *size)
2018 struct jpeg *p_jpeg = &jpeg;
2019 memset(p_jpeg, 0, sizeof(struct jpeg));
2020 p_jpeg->data = data;
2021 p_jpeg->len = len;
2022 int status = process_markers(p_jpeg);
2023 if (status < 0)
2024 return status;
2025 if ((status & (DQT | SOF0)) != (DQT | SOF0))
2026 return -(status * 16);
2027 size->width = p_jpeg->x_size;
2028 size->height = p_jpeg->y_size;
2029 return 0;
2032 int decode_jpeg_mem(unsigned char *data,
2033 #else
2034 int clip_jpeg_fd(int fd,
2035 #endif
2036 unsigned long len,
2037 struct bitmap *bm,
2038 int maxsize,
2039 int format,
2040 const struct custom_format *cformat)
2042 bool resize = false, dither = false;
2043 struct rowset rset;
2044 struct dim src_dim;
2045 int status;
2046 int bm_size;
2047 #ifdef JPEG_FROM_MEM
2048 struct jpeg *p_jpeg = &jpeg;
2049 #else
2050 struct jpeg *p_jpeg = (struct jpeg*)bm->data;
2051 int tmp_size = maxsize;
2052 ALIGN_BUFFER(p_jpeg, tmp_size, sizeof(int));
2053 /* not enough memory for our struct jpeg */
2054 if ((size_t)tmp_size < sizeof(struct jpeg))
2055 return -1;
2056 #endif
2057 memset(p_jpeg, 0, sizeof(struct jpeg));
2058 p_jpeg->len = len;
2059 #ifdef JPEG_FROM_MEM
2060 p_jpeg->data = data;
2061 #else
2062 p_jpeg->fd = fd;
2063 if (p_jpeg->len == 0)
2064 p_jpeg->len = filesize(p_jpeg->fd);
2065 #endif
2066 status = process_markers(p_jpeg);
2067 #ifndef JPEG_FROM_MEM
2068 JDEBUGF("position in file: %d buffer fill: %d\n",
2069 (int)lseek(p_jpeg->fd, 0, SEEK_CUR), p_jpeg->buf_left);
2070 #endif
2071 if (status < 0)
2072 return status;
2073 if ((status & (DQT | SOF0)) != (DQT | SOF0))
2074 return -(status * 16);
2075 if (!(status & DHT)) /* if no Huffman table present: */
2076 default_huff_tbl(p_jpeg); /* use default */
2077 fix_headers(p_jpeg); /* derive Huffman and other lookup-tables */
2078 src_dim.width = p_jpeg->x_size;
2079 src_dim.height = p_jpeg->y_size;
2080 if (format & FORMAT_RESIZE)
2081 resize = true;
2082 if (format & FORMAT_DITHER)
2083 dither = true;
2084 #ifdef HAVE_LCD_COLOR
2085 bm->alpha_offset = 0; /* no alpha channel */
2086 #endif
2087 if (resize) {
2088 struct dim resize_dim = {
2089 .width = bm->width,
2090 .height = bm->height,
2092 if (format & FORMAT_KEEP_ASPECT)
2093 recalc_dimension(&resize_dim, &src_dim);
2094 bm->width = resize_dim.width;
2095 bm->height = resize_dim.height;
2096 } else {
2097 bm->width = p_jpeg->x_size;
2098 bm->height = p_jpeg->y_size;
2100 p_jpeg->h_scale[0] = calc_scale(p_jpeg->x_size, bm->width);
2101 p_jpeg->v_scale[0] = calc_scale(p_jpeg->y_size, bm->height);
2102 JDEBUGF("luma IDCT size: %dx%d\n", BIT_N(p_jpeg->h_scale[0]),
2103 BIT_N(p_jpeg->v_scale[0]));
2104 if ((p_jpeg->x_size << p_jpeg->h_scale[0]) >> 3 == bm->width &&
2105 (p_jpeg->y_size << p_jpeg->v_scale[0]) >> 3 == bm->height)
2106 resize = false;
2107 #ifdef HAVE_LCD_COLOR
2108 p_jpeg->h_scale[1] = p_jpeg->h_scale[0] +
2109 p_jpeg->frameheader[0].horizontal_sampling - 1;
2110 p_jpeg->v_scale[1] = p_jpeg->v_scale[0] +
2111 p_jpeg->frameheader[0].vertical_sampling - 1;
2112 JDEBUGF("chroma IDCT size: %dx%d\n", BIT_N(p_jpeg->h_scale[1]),
2113 BIT_N(p_jpeg->v_scale[1]));
2114 #endif
2115 JDEBUGF("scaling from %dx%d -> %dx%d\n",
2116 (p_jpeg->x_size << p_jpeg->h_scale[0]) >> 3,
2117 (p_jpeg->y_size << p_jpeg->v_scale[0]) >> 3,
2118 bm->width, bm->height);
2119 fix_quant_tables(p_jpeg);
2120 int decode_w = BIT_N(p_jpeg->h_scale[0]) - 1;
2121 int decode_h = BIT_N(p_jpeg->v_scale[0]) - 1;
2122 src_dim.width = (p_jpeg->x_size << p_jpeg->h_scale[0]) >> 3;
2123 src_dim.height = (p_jpeg->y_size << p_jpeg->v_scale[0]) >> 3;
2124 #ifdef JPEG_IDCT_TRANSPOSE
2125 if (p_jpeg->v_scale[0] > 2)
2126 p_jpeg->zero_need[0] = (decode_w << 3) + decode_h;
2127 else
2128 #endif
2129 p_jpeg->zero_need[0] = (decode_h << 3) + decode_w;
2130 p_jpeg->k_need[0] = zig[(decode_h << 3) + decode_w];
2131 JDEBUGF("need luma components to %d\n", p_jpeg->k_need[0]);
2132 #ifdef HAVE_LCD_COLOR
2133 decode_w = BIT_N(MIN(p_jpeg->h_scale[1],3)) - 1;
2134 decode_h = BIT_N(MIN(p_jpeg->v_scale[1],3)) - 1;
2135 if (p_jpeg->v_scale[1] > 2)
2136 p_jpeg->zero_need[1] = (decode_w << 3) + decode_h;
2137 else
2138 p_jpeg->zero_need[1] = (decode_h << 3) + decode_w;
2139 p_jpeg->k_need[1] = zig[(decode_h << 3) + decode_w];
2140 JDEBUGF("need chroma components to %d\n", p_jpeg->k_need[1]);
2141 #endif
2142 if (cformat)
2143 bm_size = cformat->get_size(bm);
2144 else
2145 bm_size = BM_SIZE(bm->width,bm->height,FORMAT_NATIVE,false);
2146 if (bm_size > maxsize)
2147 return -1;
2148 char *buf_start = (char *)bm->data + bm_size;
2149 char *buf_end = (char *)bm->data + maxsize;
2150 maxsize = buf_end - buf_start;
2151 #ifndef JPEG_FROM_MEM
2152 ALIGN_BUFFER(buf_start, maxsize, sizeof(uint32_t));
2153 if (maxsize < (int)sizeof(struct jpeg))
2154 return -1;
2155 memmove(buf_start, p_jpeg, sizeof(struct jpeg));
2156 p_jpeg = (struct jpeg *)buf_start;
2157 buf_start += sizeof(struct jpeg);
2158 maxsize = buf_end - buf_start;
2159 #endif
2160 fix_huff_tables(p_jpeg);
2161 #ifdef HAVE_LCD_COLOR
2162 int decode_buf_size = (p_jpeg->x_mbl << p_jpeg->h_scale[1])
2163 << p_jpeg->v_scale[1];
2164 #else
2165 int decode_buf_size = (p_jpeg->x_mbl << p_jpeg->h_scale[0])
2166 << p_jpeg->v_scale[0];
2167 decode_buf_size <<= p_jpeg->frameheader[0].horizontal_sampling +
2168 p_jpeg->frameheader[0].vertical_sampling - 2;
2169 #endif
2170 decode_buf_size *= JPEG_PIX_SZ;
2171 JDEBUGF("decode buffer size: %d\n", decode_buf_size);
2172 p_jpeg->img_buf = (jpeg_pix_t *)buf_start;
2173 if (buf_end - buf_start < decode_buf_size)
2174 return -1;
2175 buf_start += decode_buf_size;
2176 maxsize = buf_end - buf_start;
2177 memset(p_jpeg->img_buf, 0, decode_buf_size);
2178 p_jpeg->mcu_row = 0;
2179 p_jpeg->restart = p_jpeg->restart_interval;
2180 rset.rowstart = 0;
2181 rset.rowstop = bm->height;
2182 rset.rowstep = 1;
2183 p_jpeg->resize = resize;
2184 if (resize)
2186 if (resize_on_load(bm, dither, &src_dim, &rset, buf_start, maxsize,
2187 cformat, IF_PIX_FMT(p_jpeg->blocks == 1 ? 0 : 1,) store_row_jpeg,
2188 p_jpeg))
2189 return bm_size;
2190 } else {
2191 int row;
2192 struct scaler_context ctx = {
2193 .bm = bm,
2194 .dither = dither,
2196 #if LCD_DEPTH > 1
2197 void (*output_row_8)(uint32_t, void*, struct scaler_context*) =
2198 output_row_8_native;
2199 #elif defined(PLUGIN)
2200 void (*output_row_8)(uint32_t, void*, struct scaler_context*) = NULL;
2201 #endif
2202 #if LCD_DEPTH > 1 || defined(PLUGIN)
2203 if (cformat)
2204 output_row_8 = cformat->output_row_8;
2205 #endif
2206 struct img_part *part;
2207 for (row = 0; row < bm->height; row++)
2209 part = store_row_jpeg(p_jpeg);
2210 #ifdef HAVE_LCD_COLOR
2211 if (p_jpeg->blocks > 1)
2213 struct uint8_rgb *qp = part->buf;
2214 struct uint8_rgb *end = qp + bm->width;
2215 uint8_t y, u, v;
2216 unsigned r, g, b;
2217 for (; qp < end; qp++)
2219 y = qp->blue;
2220 u = qp->green;
2221 v = qp->red;
2222 yuv_to_rgb(y, u, v, &r, &g, &b);
2223 qp->red = r;
2224 qp->blue = b;
2225 qp->green = g;
2228 #endif
2229 output_row_8(row, part->buf, &ctx);
2231 return bm_size;
2233 return 0;
2236 #ifndef JPEG_FROM_MEM
2237 int read_jpeg_fd(int fd,
2238 struct bitmap *bm,
2239 int maxsize,
2240 int format,
2241 const struct custom_format *cformat)
2243 return clip_jpeg_fd(fd, 0, bm, maxsize, format, cformat);
2245 #endif
2247 /**************** end JPEG code ********************/