1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
32 #include "jpeg_load.h"
33 /*#define JPEG_BS_DEBUG*/
34 /* for portability of below JPEG code */
35 #define MEMSET(p,v,c) memset(p,v,c)
36 #define MEMCPY(d,s,c) memcpy(d,s,c)
37 #define INLINE static inline
38 #define ENDIAN_SWAP16(n) n /* only for poor little endian machines */
39 #ifdef ROCKBOX_DEBUG_JPEG
40 #define JDEBUGF DEBUGF
45 /**************** begin JPEG code ********************/
48 typedef struct uint8_rgb jpeg_pix_t
;
50 typedef uint8_t jpeg_pix_t
;
52 #define JPEG_PIX_SZ (sizeof(jpeg_pix_t))
54 /* This can't be in jpeg_load.h because plugin.h includes it, and it conflicts
55 * with the definition in jpeg_decoder.h
67 unsigned long int bitbuf
;
72 int x_size
, y_size
; /* size of image (can be less than block boundary) */
73 int x_phys
, y_phys
; /* physical size, block aligned */
74 int x_mbl
; /* x dimension of MBL */
75 int y_mbl
; /* y dimension of MBL */
76 int blocks
; /* blocks per MB */
77 int restart_interval
; /* number of MCUs between RSTm markers */
78 int restart
; /* blocks until next restart marker */
79 int mcu_row
; /* current row relative to first row of this row of MCUs */
80 unsigned char *out_ptr
; /* pointer to current row to output */
81 int cur_row
; /* current row relative to top of image */
83 int store_pos
[4]; /* for Y block ordering */
86 int h_scale
[2]; /* horizontal scalefactor = (2**N) / 8 */
87 int v_scale
[2]; /* same as above, for vertical direction */
88 int k_need
[2]; /* per component zig-zag index of last needed coefficient */
89 int zero_need
[2]; /* per compenent number of coefficients to zero */
92 int h_scale
[1]; /* horizontal scalefactor = (2**N) / 8 */
93 int v_scale
[1]; /* same as above, for vertical direction */
94 int k_need
[1]; /* per component zig-zag index of last needed coefficient */
95 int zero_need
[1]; /* per compenent number of coefficients to zero */
99 int quanttable
[4][QUANT_TABLE_LENGTH
]; /* raw quantization tables 0-3 */
101 struct huffman_table hufftable
[2]; /* Huffman tables */
102 struct derived_tbl dc_derived_tbls
[2]; /* Huffman-LUTs */
103 struct derived_tbl ac_derived_tbls
[2];
105 struct frame_component frameheader
[3]; /* Component descriptor */
106 struct scan_component scanheader
[3]; /* currently not used */
108 int mcu_membership
[6]; /* info per block */
109 int tab_membership
[6];
110 int subsample_x
[3]; /* info per component */
113 unsigned char buf
[JPEG_READ_BUF_SIZE
];
114 struct img_part part
;
118 static struct jpeg jpeg
;
121 INLINE
unsigned range_limit(int value
)
123 #if CONFIG_CPU == SH7034
125 asm ( /* Note: Uses knowledge that only low byte of result is used */
127 "sub %[t],%[v] \n" /* value -= -128; equals value += 128; */
128 "extu.b %[v],%[t] \n"
129 "cmp/eq %[v],%[t] \n" /* low byte == whole number ? */
130 "bt 1f \n" /* yes: no overflow */
131 "cmp/pz %[v] \n" /* overflow: positive? */
132 "subc %[v],%[v] \n" /* %[r] now either 0 or 0xffffffff */
139 #elif defined(CPU_COLDFIRE)
140 /* Note: Uses knowledge that only the low byte of the result is used */
142 "add.l #128,%[v] \n" /* value += 128; */
143 "cmp.l #255,%[v] \n" /* overflow? */
144 "bls.b 1f \n" /* no: return value */
145 /* yes: set low byte to appropriate boundary */
152 #elif defined(CPU_ARM)
153 /* Note: Uses knowledge that only the low byte of the result is used */
155 "add %[v], %[v], #128 \n" /* value += 128 */
156 "cmp %[v], #255 \n" /* out of range 0..255? */
157 "mvnhi %[v], %[v], asr #31 \n" /* yes: set all bits to ~(sign_bit) */
165 if ((unsigned)value
<= 255)
175 /* IDCT implementation */
178 #define CONST_BITS 13
182 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
183 * causing a lot of useless floating-point operations at run time.
184 * To get around this we use the following pre-calculated constants.
185 * If you change CONST_BITS you may want to add appropriate values.
186 * (With a reasonable C compiler, you can just rely on the FIX() macro...)
188 #define FIX_0_298631336 2446 /* FIX(0.298631336) */
189 #define FIX_0_390180644 3196 /* FIX(0.390180644) */
190 #define FIX_0_541196100 4433 /* FIX(0.541196100) */
191 #define FIX_0_765366865 6270 /* FIX(0.765366865) */
192 #define FIX_0_899976223 7373 /* FIX(0.899976223) */
193 #define FIX_1_175875602 9633 /* FIX(1.175875602) */
194 #define FIX_1_501321110 12299 /* FIX(1.501321110) */
195 #define FIX_1_847759065 15137 /* FIX(1.847759065) */
196 #define FIX_1_961570560 16069 /* FIX(1.961570560) */
197 #define FIX_2_053119869 16819 /* FIX(2.053119869) */
198 #define FIX_2_562915447 20995 /* FIX(2.562915447) */
199 #define FIX_3_072711026 25172 /* FIX(3.072711026) */
203 /* Multiply an long variable by an long constant to yield an long result.
204 * For 8-bit samples with the recommended scaling, all the variable
205 * and constant values involved are no more than 16 bits wide, so a
206 * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
207 * For 12-bit samples, a full 32-bit multiplication will be needed.
209 #define MULTIPLY16(var,const) (((short) (var)) * ((short) (const)))
211 #define MULTIPLY(var1, var2) ((var1) * (var2))
214 * Macros for handling fixed-point arithmetic; these are used by many
215 * but not all of the DCT/IDCT modules.
217 * All values are expected to be of type INT32.
218 * Fractional constants are scaled left by CONST_BITS bits.
219 * CONST_BITS is defined within each module using these macros,
220 * and may differ from one module to the next.
222 #define ONE ((long)1)
223 #define CONST_SCALE (ONE << CONST_BITS)
225 /* Convert a positive real constant to an integer scaled by CONST_SCALE.
226 * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
227 * thus causing a lot of useless floating-point operations at run time.
229 #define FIX(x) ((long) ((x) * CONST_SCALE + 0.5))
230 #define RIGHT_SHIFT(x,shft) ((x) >> (shft))
232 /* Descale and correctly round an int value that's scaled by N bits.
233 * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
234 * the fudge factor is correct for either sign of X.
236 #define DESCALE(x,n) (((x) + (1l << ((n)-1))) >> (n))
238 #define DS_OUT ((CONST_BITS)+(PASS1_BITS)+3)
241 * Conversion of full 0-255 range YCrCb to RGB:
242 * |R| |1.000000 -0.000001 1.402000| |Y'|
243 * |G| = |1.000000 -0.334136 -0.714136| |Pb|
244 * |B| |1.000000 1.772000 0.000000| |Pr|
245 * Scaled (yields s15-bit output):
246 * |R| |128 0 179| |Y |
247 * |G| = |128 -43 -91| |Cb - 128|
248 * |B| |128 227 0| |Cr - 128|
255 #define COMPONENT_SHIFT 15
257 /* horizontal-pass 1-point IDCT */
258 static void idct1h(int *ws
, unsigned char *out
, int rows
, int rowstep
)
261 for (row
= 0; row
< rows
; row
++)
263 *out
= range_limit((int) DESCALE(*ws
, DS_OUT
));
269 /* vertical-pass 2-point IDCT */
270 static void idct2v(int *ws
, int cols
)
273 for (col
= 0; col
< cols
; col
++)
283 /* horizontal-pass 2-point IDCT */
284 static void idct2h(int *ws
, unsigned char *out
, int rows
, int rowstep
)
287 for (row
= 0; row
< rows
; row
++)
289 int tmp1
= ws
[0] + (ONE
<< (DS_OUT
- 1));
291 out
[JPEG_PIX_SZ
*0] = range_limit((int) RIGHT_SHIFT(tmp1
+ tmp2
,
293 out
[JPEG_PIX_SZ
*1] = range_limit((int) RIGHT_SHIFT(tmp1
- tmp2
,
300 /* vertical-pass 4-point IDCT */
301 static void idct4v(int *ws
, int cols
)
303 int tmp0
, tmp2
, tmp10
, tmp12
;
306 for (col
= 0; col
< cols
; col
++, ws
++)
313 tmp10
= (tmp0
+ tmp2
) << PASS1_BITS
;
314 tmp12
= (tmp0
- tmp2
) << PASS1_BITS
;
317 /* Same rotation as in the even part of the 8x8 LL&M IDCT */
322 z1
= MULTIPLY16(z2
+ z3
, FIX_0_541196100
) +
323 (ONE
<< (CONST_BITS
- PASS1_BITS
- 1));
324 tmp0
= RIGHT_SHIFT(z1
+ MULTIPLY16(z3
, - FIX_1_847759065
),
325 CONST_BITS
-PASS1_BITS
);
326 tmp2
= RIGHT_SHIFT(z1
+ MULTIPLY16(z2
, FIX_0_765366865
),
327 CONST_BITS
-PASS1_BITS
);
329 /* Final output stage */
331 ws
[8*0] = (int) (tmp10
+ tmp2
);
332 ws
[8*3] = (int) (tmp10
- tmp2
);
333 ws
[8*1] = (int) (tmp12
+ tmp0
);
334 ws
[8*2] = (int) (tmp12
- tmp0
);
338 /* horizontal-pass 4-point IDCT */
339 static void idct4h(int *ws
, unsigned char *out
, int rows
, int rowstep
)
341 int tmp0
, tmp2
, tmp10
, tmp12
;
344 for (row
= 0; row
< rows
; row
++, out
+= rowstep
, ws
+= 8)
348 tmp0
= (int) ws
[0] + (ONE
<< (PASS1_BITS
+ 2));
351 tmp10
= (tmp0
+ tmp2
) << CONST_BITS
;
352 tmp12
= (tmp0
- tmp2
) << CONST_BITS
;
355 /* Same rotation as in the even part of the 8x8 LL&M IDCT */
360 z1
= MULTIPLY16(z2
+ z3
, FIX_0_541196100
);
361 tmp0
= z1
+ MULTIPLY16(z3
, - FIX_1_847759065
);
362 tmp2
= z1
+ MULTIPLY16(z2
, FIX_0_765366865
);
364 /* Final output stage */
366 out
[JPEG_PIX_SZ
*0] = range_limit((int) RIGHT_SHIFT(tmp10
+ tmp2
,
368 out
[JPEG_PIX_SZ
*3] = range_limit((int) RIGHT_SHIFT(tmp10
- tmp2
,
370 out
[JPEG_PIX_SZ
*1] = range_limit((int) RIGHT_SHIFT(tmp12
+ tmp0
,
372 out
[JPEG_PIX_SZ
*2] = range_limit((int) RIGHT_SHIFT(tmp12
- tmp0
,
377 /* vertical-pass 8-point IDCT */
378 static void idct8v(int *ws
, int cols
)
380 long tmp0
, tmp1
, tmp2
, tmp3
;
381 long tmp10
, tmp11
, tmp12
, tmp13
;
382 long z1
, z2
, z3
, z4
, z5
;
384 for (col
= 0; col
< cols
; col
++, ws
++)
386 /* Due to quantization, we will usually find that many of the input
387 * coefficients are zero, especially the AC terms. We can exploit this
388 * by short-circuiting the IDCT calculation for any column in which all
389 * the AC terms are zero. In that case each output is equal to the
390 * DC coefficient (with scale factor as needed).
391 * With typical images and quantization tables, half or more of the
392 * column DCT calculations can be simplified this way.
394 if ((ws
[8*1] | ws
[8*2] | ws
[8*3]
395 | ws
[8*4] | ws
[8*5] | ws
[8*6] | ws
[8*7]) == 0)
397 /* AC terms all zero */
398 int dcval
= ws
[8*0] << PASS1_BITS
;
400 ws
[8*0] = ws
[8*1] = ws
[8*2] = ws
[8*3] = ws
[8*4]
401 = ws
[8*5] = ws
[8*6] = ws
[8*7] = dcval
;
405 /* Even part: reverse the even part of the forward DCT. */
406 /* The rotator is sqrt(2)*c(-6). */
411 z1
= MULTIPLY16(z2
+ z3
, FIX_0_541196100
);
412 tmp2
= z1
+ MULTIPLY16(z3
, - FIX_1_847759065
);
413 tmp3
= z1
+ MULTIPLY16(z2
, FIX_0_765366865
);
415 z2
= ws
[8*0] << CONST_BITS
;
416 z2
+= ONE
<< (CONST_BITS
- PASS1_BITS
- 1);
417 z3
= ws
[8*4] << CONST_BITS
;
427 /* Odd part per figure 8; the matrix is unitary and hence its
428 transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */
439 z5
= MULTIPLY16(z3
+ z4
, FIX_1_175875602
); /* sqrt(2) * c3 */
441 tmp0
= MULTIPLY16(tmp0
, FIX_0_298631336
); /* sqrt(2) * (-c1+c3+c5-c7) */
442 tmp1
= MULTIPLY16(tmp1
, FIX_2_053119869
); /* sqrt(2) * ( c1+c3-c5+c7) */
443 tmp2
= MULTIPLY16(tmp2
, FIX_3_072711026
); /* sqrt(2) * ( c1+c3+c5-c7) */
444 tmp3
= MULTIPLY16(tmp3
, FIX_1_501321110
); /* sqrt(2) * ( c1+c3-c5-c7) */
445 z1
= MULTIPLY16(z1
, - FIX_0_899976223
); /* sqrt(2) * (c7-c3) */
446 z2
= MULTIPLY16(z2
, - FIX_2_562915447
); /* sqrt(2) * (-c1-c3) */
447 z3
= MULTIPLY16(z3
, - FIX_1_961570560
); /* sqrt(2) * (-c3-c5) */
448 z4
= MULTIPLY16(z4
, - FIX_0_390180644
); /* sqrt(2) * (c5-c3) */
458 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
460 ws
[8*0] = (int) RIGHT_SHIFT(tmp10
+ tmp3
, CONST_BITS
-PASS1_BITS
);
461 ws
[8*7] = (int) RIGHT_SHIFT(tmp10
- tmp3
, CONST_BITS
-PASS1_BITS
);
462 ws
[8*1] = (int) RIGHT_SHIFT(tmp11
+ tmp2
, CONST_BITS
-PASS1_BITS
);
463 ws
[8*6] = (int) RIGHT_SHIFT(tmp11
- tmp2
, CONST_BITS
-PASS1_BITS
);
464 ws
[8*2] = (int) RIGHT_SHIFT(tmp12
+ tmp1
, CONST_BITS
-PASS1_BITS
);
465 ws
[8*5] = (int) RIGHT_SHIFT(tmp12
- tmp1
, CONST_BITS
-PASS1_BITS
);
466 ws
[8*3] = (int) RIGHT_SHIFT(tmp13
+ tmp0
, CONST_BITS
-PASS1_BITS
);
467 ws
[8*4] = (int) RIGHT_SHIFT(tmp13
- tmp0
, CONST_BITS
-PASS1_BITS
);
471 /* horizontal-pass 8-point IDCT */
472 static void idct8h(int *ws
, unsigned char *out
, int rows
, int rowstep
)
474 long tmp0
, tmp1
, tmp2
, tmp3
;
475 long tmp10
, tmp11
, tmp12
, tmp13
;
476 long z1
, z2
, z3
, z4
, z5
;
478 for (row
= 0; row
< rows
; row
++, out
+= rowstep
, ws
+= 8)
480 /* Rows of zeroes can be exploited in the same way as we did with
481 * columns. However, the column calculation has created many nonzero AC
482 * terms, so the simplification applies less often (typically 5% to 10%
483 * of the time). On machines with very fast multiplication, it's
484 * possible that the test takes more time than it's worth. In that
485 * case this section may be commented out.
488 #ifndef NO_ZERO_ROW_TEST
489 if ((ws
[1] | ws
[2] | ws
[3]
490 | ws
[4] | ws
[5] | ws
[6] | ws
[7]) == 0)
492 /* AC terms all zero */
493 unsigned char dcval
= range_limit((int) DESCALE((long) ws
[0],
496 out
[JPEG_PIX_SZ
*0] = dcval
;
497 out
[JPEG_PIX_SZ
*1] = dcval
;
498 out
[JPEG_PIX_SZ
*2] = dcval
;
499 out
[JPEG_PIX_SZ
*3] = dcval
;
500 out
[JPEG_PIX_SZ
*4] = dcval
;
501 out
[JPEG_PIX_SZ
*5] = dcval
;
502 out
[JPEG_PIX_SZ
*6] = dcval
;
503 out
[JPEG_PIX_SZ
*7] = dcval
;
508 /* Even part: reverse the even part of the forward DCT. */
509 /* The rotator is sqrt(2)*c(-6). */
514 z1
= MULTIPLY16(z2
+ z3
, FIX_0_541196100
);
515 tmp2
= z1
+ MULTIPLY16(z3
, - FIX_1_847759065
);
516 tmp3
= z1
+ MULTIPLY16(z2
, FIX_0_765366865
);
518 z4
= (long) ws
[0] + (ONE
<< (PASS1_BITS
+ 2));
520 z5
= (long) ws
[4] << CONST_BITS
;
529 /* Odd part per figure 8; the matrix is unitary and hence its
530 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */
541 z5
= MULTIPLY16(z3
+ z4
, FIX_1_175875602
); /* sqrt(2) * c3 */
543 tmp0
= MULTIPLY16(tmp0
, FIX_0_298631336
); /* sqrt(2) * (-c1+c3+c5-c7) */
544 tmp1
= MULTIPLY16(tmp1
, FIX_2_053119869
); /* sqrt(2) * ( c1+c3-c5+c7) */
545 tmp2
= MULTIPLY16(tmp2
, FIX_3_072711026
); /* sqrt(2) * ( c1+c3+c5-c7) */
546 tmp3
= MULTIPLY16(tmp3
, FIX_1_501321110
); /* sqrt(2) * ( c1+c3-c5-c7) */
547 z1
= MULTIPLY16(z1
, - FIX_0_899976223
); /* sqrt(2) * (c7-c3) */
548 z2
= MULTIPLY16(z2
, - FIX_2_562915447
); /* sqrt(2) * (-c1-c3) */
549 z3
= MULTIPLY16(z3
, - FIX_1_961570560
); /* sqrt(2) * (-c3-c5) */
550 z4
= MULTIPLY16(z4
, - FIX_0_390180644
); /* sqrt(2) * (c5-c3) */
560 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
562 out
[JPEG_PIX_SZ
*0] = range_limit((int) RIGHT_SHIFT(tmp10
+ tmp3
,
564 out
[JPEG_PIX_SZ
*7] = range_limit((int) RIGHT_SHIFT(tmp10
- tmp3
,
566 out
[JPEG_PIX_SZ
*1] = range_limit((int) RIGHT_SHIFT(tmp11
+ tmp2
,
568 out
[JPEG_PIX_SZ
*6] = range_limit((int) RIGHT_SHIFT(tmp11
- tmp2
,
570 out
[JPEG_PIX_SZ
*2] = range_limit((int) RIGHT_SHIFT(tmp12
+ tmp1
,
572 out
[JPEG_PIX_SZ
*5] = range_limit((int) RIGHT_SHIFT(tmp12
- tmp1
,
574 out
[JPEG_PIX_SZ
*3] = range_limit((int) RIGHT_SHIFT(tmp13
+ tmp0
,
576 out
[JPEG_PIX_SZ
*4] = range_limit((int) RIGHT_SHIFT(tmp13
- tmp0
,
581 #ifdef HAVE_LCD_COLOR
582 /* vertical-pass 16-point IDCT */
583 static void idct16v(int *ws
, int cols
)
585 long tmp0
, tmp1
, tmp2
, tmp3
, tmp10
, tmp11
, tmp12
, tmp13
;
586 long tmp20
, tmp21
, tmp22
, tmp23
, tmp24
, tmp25
, tmp26
, tmp27
;
589 for (col
= 0; col
< cols
; col
++, ws
++)
593 tmp0
= ws
[8*0] << CONST_BITS
;
594 /* Add fudge factor here for final descale. */
595 tmp0
+= 1 << (CONST_BITS
-PASS1_BITS
-1);
598 tmp1
= MULTIPLY(z1
, FIX(1.306562965)); /* c4[16] = c2[8] */
599 tmp2
= MULTIPLY(z1
, FIX_0_541196100
); /* c12[16] = c6[8] */
609 z4
= MULTIPLY(z3
, FIX(0.275899379)); /* c14[16] = c7[8] */
610 z3
= MULTIPLY(z3
, FIX(1.387039845)); /* c2[16] = c1[8] */
612 /* (c6+c2)[16] = (c3+c1)[8] */
613 tmp0
= z3
+ MULTIPLY(z2
, FIX_2_562915447
);
614 /* (c6-c14)[16] = (c3-c7)[8] */
615 tmp1
= z4
+ MULTIPLY(z1
, FIX_0_899976223
);
616 /* (c2-c10)[16] = (c1-c5)[8] */
617 tmp2
= z3
- MULTIPLY(z1
, FIX(0.601344887));
618 /* (c10-c14)[16] = (c5-c7)[8] */
619 tmp3
= z4
- MULTIPLY(z2
, FIX(0.509795579));
621 tmp20
= tmp10
+ tmp0
;
622 tmp27
= tmp10
- tmp0
;
623 tmp21
= tmp12
+ tmp1
;
624 tmp26
= tmp12
- tmp1
;
625 tmp22
= tmp13
+ tmp2
;
626 tmp25
= tmp13
- tmp2
;
627 tmp23
= tmp11
+ tmp3
;
628 tmp24
= tmp11
- tmp3
;
639 tmp1
= MULTIPLY(z1
+ z2
, FIX(1.353318001)); /* c3 */
640 tmp2
= MULTIPLY(tmp11
, FIX(1.247225013)); /* c5 */
641 tmp3
= MULTIPLY(z1
+ z4
, FIX(1.093201867)); /* c7 */
642 tmp10
= MULTIPLY(z1
- z4
, FIX(0.897167586)); /* c9 */
643 tmp11
= MULTIPLY(tmp11
, FIX(0.666655658)); /* c11 */
644 tmp12
= MULTIPLY(z1
- z2
, FIX(0.410524528)); /* c13 */
645 tmp0
= tmp1
+ tmp2
+ tmp3
-
646 MULTIPLY(z1
, FIX(2.286341144)); /* c7+c5+c3-c1 */
647 tmp13
= tmp10
+ tmp11
+ tmp12
-
648 MULTIPLY(z1
, FIX(1.835730603)); /* c9+c11+c13-c15 */
649 z1
= MULTIPLY(z2
+ z3
, FIX(0.138617169)); /* c15 */
650 tmp1
+= z1
+ MULTIPLY(z2
, FIX(0.071888074)); /* c9+c11-c3-c15 */
651 tmp2
+= z1
- MULTIPLY(z3
, FIX(1.125726048)); /* c5+c7+c15-c3 */
652 z1
= MULTIPLY(z3
- z2
, FIX(1.407403738)); /* c1 */
653 tmp11
+= z1
- MULTIPLY(z3
, FIX(0.766367282)); /* c1+c11-c9-c13 */
654 tmp12
+= z1
+ MULTIPLY(z2
, FIX(1.971951411)); /* c1+c5+c13-c7 */
656 z1
= MULTIPLY(z2
, - FIX(0.666655658)); /* -c11 */
658 tmp3
+= z1
+ MULTIPLY(z4
, FIX(1.065388962)); /* c3+c11+c15-c7 */
659 z2
= MULTIPLY(z2
, - FIX(1.247225013)); /* -c5 */
660 tmp10
+= z2
+ MULTIPLY(z4
, FIX(3.141271809)); /* c1+c5+c9-c13 */
662 z2
= MULTIPLY(z3
+ z4
, - FIX(1.353318001)); /* -c3 */
665 z2
= MULTIPLY(z4
- z3
, FIX(0.410524528)); /* c13 */
669 /* Final output stage */
670 ws
[8*0] = (int) RIGHT_SHIFT(tmp20
+ tmp0
, CONST_BITS
-PASS1_BITS
);
671 ws
[8*15] = (int) RIGHT_SHIFT(tmp20
- tmp0
, CONST_BITS
-PASS1_BITS
);
672 ws
[8*1] = (int) RIGHT_SHIFT(tmp21
+ tmp1
, CONST_BITS
-PASS1_BITS
);
673 ws
[8*14] = (int) RIGHT_SHIFT(tmp21
- tmp1
, CONST_BITS
-PASS1_BITS
);
674 ws
[8*2] = (int) RIGHT_SHIFT(tmp22
+ tmp2
, CONST_BITS
-PASS1_BITS
);
675 ws
[8*13] = (int) RIGHT_SHIFT(tmp22
- tmp2
, CONST_BITS
-PASS1_BITS
);
676 ws
[8*3] = (int) RIGHT_SHIFT(tmp23
+ tmp3
, CONST_BITS
-PASS1_BITS
);
677 ws
[8*12] = (int) RIGHT_SHIFT(tmp23
- tmp3
, CONST_BITS
-PASS1_BITS
);
678 ws
[8*4] = (int) RIGHT_SHIFT(tmp24
+ tmp10
, CONST_BITS
-PASS1_BITS
);
679 ws
[8*11] = (int) RIGHT_SHIFT(tmp24
- tmp10
, CONST_BITS
-PASS1_BITS
);
680 ws
[8*5] = (int) RIGHT_SHIFT(tmp25
+ tmp11
, CONST_BITS
-PASS1_BITS
);
681 ws
[8*10] = (int) RIGHT_SHIFT(tmp25
- tmp11
, CONST_BITS
-PASS1_BITS
);
682 ws
[8*6] = (int) RIGHT_SHIFT(tmp26
+ tmp12
, CONST_BITS
-PASS1_BITS
);
683 ws
[8*9] = (int) RIGHT_SHIFT(tmp26
- tmp12
, CONST_BITS
-PASS1_BITS
);
684 ws
[8*7] = (int) RIGHT_SHIFT(tmp27
+ tmp13
, CONST_BITS
-PASS1_BITS
);
685 ws
[8*8] = (int) RIGHT_SHIFT(tmp27
- tmp13
, CONST_BITS
-PASS1_BITS
);
689 /* horizontal-pass 16-point IDCT */
690 static void idct16h(int *ws
, unsigned char *out
, int rows
, int rowstep
)
692 long tmp0
, tmp1
, tmp2
, tmp3
, tmp10
, tmp11
, tmp12
, tmp13
;
693 long tmp20
, tmp21
, tmp22
, tmp23
, tmp24
, tmp25
, tmp26
, tmp27
;
696 for (row
= 0; row
< rows
; row
++, out
+= rowstep
, ws
+= 8)
700 /* Add fudge factor here for final descale. */
701 tmp0
= (long) ws
[0] + (ONE
<< (PASS1_BITS
+2));
705 tmp1
= MULTIPLY(z1
, FIX(1.306562965)); /* c4[16] = c2[8] */
706 tmp2
= MULTIPLY(z1
, FIX_0_541196100
); /* c12[16] = c6[8] */
716 z4
= MULTIPLY(z3
, FIX(0.275899379)); /* c14[16] = c7[8] */
717 z3
= MULTIPLY(z3
, FIX(1.387039845)); /* c2[16] = c1[8] */
719 /* (c6+c2)[16] = (c3+c1)[8] */
720 tmp0
= z3
+ MULTIPLY(z2
, FIX_2_562915447
);
721 /* (c6-c14)[16] = (c3-c7)[8] */
722 tmp1
= z4
+ MULTIPLY(z1
, FIX_0_899976223
);
723 /* (c2-c10)[16] = (c1-c5)[8] */
724 tmp2
= z3
- MULTIPLY(z1
, FIX(0.601344887));
725 /* (c10-c14)[16] = (c5-c7)[8] */
726 tmp3
= z4
- MULTIPLY(z2
, FIX(0.509795579));
728 tmp20
= tmp10
+ tmp0
;
729 tmp27
= tmp10
- tmp0
;
730 tmp21
= tmp12
+ tmp1
;
731 tmp26
= tmp12
- tmp1
;
732 tmp22
= tmp13
+ tmp2
;
733 tmp25
= tmp13
- tmp2
;
734 tmp23
= tmp11
+ tmp3
;
735 tmp24
= tmp11
- tmp3
;
746 tmp1
= MULTIPLY(z1
+ z2
, FIX(1.353318001)); /* c3 */
747 tmp2
= MULTIPLY(tmp11
, FIX(1.247225013)); /* c5 */
748 tmp3
= MULTIPLY(z1
+ z4
, FIX(1.093201867)); /* c7 */
749 tmp10
= MULTIPLY(z1
- z4
, FIX(0.897167586)); /* c9 */
750 tmp11
= MULTIPLY(tmp11
, FIX(0.666655658)); /* c11 */
751 tmp12
= MULTIPLY(z1
- z2
, FIX(0.410524528)); /* c13 */
752 tmp0
= tmp1
+ tmp2
+ tmp3
-
753 MULTIPLY(z1
, FIX(2.286341144)); /* c7+c5+c3-c1 */
754 tmp13
= tmp10
+ tmp11
+ tmp12
-
755 MULTIPLY(z1
, FIX(1.835730603)); /* c9+c11+c13-c15 */
756 z1
= MULTIPLY(z2
+ z3
, FIX(0.138617169)); /* c15 */
757 tmp1
+= z1
+ MULTIPLY(z2
, FIX(0.071888074)); /* c9+c11-c3-c15 */
758 tmp2
+= z1
- MULTIPLY(z3
, FIX(1.125726048)); /* c5+c7+c15-c3 */
759 z1
= MULTIPLY(z3
- z2
, FIX(1.407403738)); /* c1 */
760 tmp11
+= z1
- MULTIPLY(z3
, FIX(0.766367282)); /* c1+c11-c9-c13 */
761 tmp12
+= z1
+ MULTIPLY(z2
, FIX(1.971951411)); /* c1+c5+c13-c7 */
763 z1
= MULTIPLY(z2
, - FIX(0.666655658)); /* -c11 */
765 tmp3
+= z1
+ MULTIPLY(z4
, FIX(1.065388962)); /* c3+c11+c15-c7 */
766 z2
= MULTIPLY(z2
, - FIX(1.247225013)); /* -c5 */
767 tmp10
+= z2
+ MULTIPLY(z4
, FIX(3.141271809)); /* c1+c5+c9-c13 */
769 z2
= MULTIPLY(z3
+ z4
, - FIX(1.353318001)); /* -c3 */
772 z2
= MULTIPLY(z4
- z3
, FIX(0.410524528)); /* c13 */
776 /* Final output stage */
778 out
[JPEG_PIX_SZ
*0] = range_limit((int) RIGHT_SHIFT(tmp20
+ tmp0
,
780 out
[JPEG_PIX_SZ
*15] = range_limit((int) RIGHT_SHIFT(tmp20
- tmp0
,
782 out
[JPEG_PIX_SZ
*1] = range_limit((int) RIGHT_SHIFT(tmp21
+ tmp1
,
784 out
[JPEG_PIX_SZ
*14] = range_limit((int) RIGHT_SHIFT(tmp21
- tmp1
,
786 out
[JPEG_PIX_SZ
*2] = range_limit((int) RIGHT_SHIFT(tmp22
+ tmp2
,
788 out
[JPEG_PIX_SZ
*13] = range_limit((int) RIGHT_SHIFT(tmp22
- tmp2
,
790 out
[JPEG_PIX_SZ
*3] = range_limit((int) RIGHT_SHIFT(tmp23
+ tmp3
,
792 out
[JPEG_PIX_SZ
*12] = range_limit((int) RIGHT_SHIFT(tmp23
- tmp3
,
794 out
[JPEG_PIX_SZ
*4] = range_limit((int) RIGHT_SHIFT(tmp24
+ tmp10
,
796 out
[JPEG_PIX_SZ
*11] = range_limit((int) RIGHT_SHIFT(tmp24
- tmp10
,
798 out
[JPEG_PIX_SZ
*5] = range_limit((int) RIGHT_SHIFT(tmp25
+ tmp11
,
800 out
[JPEG_PIX_SZ
*10] = range_limit((int) RIGHT_SHIFT(tmp25
- tmp11
,
802 out
[JPEG_PIX_SZ
*6] = range_limit((int) RIGHT_SHIFT(tmp26
+ tmp12
,
804 out
[JPEG_PIX_SZ
*9] = range_limit((int) RIGHT_SHIFT(tmp26
- tmp12
,
806 out
[JPEG_PIX_SZ
*7] = range_limit((int) RIGHT_SHIFT(tmp27
+ tmp13
,
808 out
[JPEG_PIX_SZ
*8] = range_limit((int) RIGHT_SHIFT(tmp27
- tmp13
,
817 void (*v_idct
)(int *ws
, int cols
);
818 void (*h_idct
)(int *ws
, unsigned char *out
, int rows
, int rowstep
);
821 struct idct_entry idct_tbl
[] = {
822 { PASS1_BITS
, CONST_BITS
, NULL
, idct1h
},
823 { PASS1_BITS
, CONST_BITS
, idct2v
, idct2h
},
824 { 0, 0, idct4v
, idct4h
},
825 { 0, 0, idct8v
, idct8h
},
826 #ifdef HAVE_LCD_COLOR
827 { 0, 0, idct16v
, idct16h
},
831 /* JPEG decoder implementation */
834 INLINE
unsigned char *getc(struct jpeg
* p_jpeg
)
836 if (LIKELY(p_jpeg
->len
))
839 return p_jpeg
->data
++;
844 INLINE
bool skip_bytes(struct jpeg
* p_jpeg
, int count
)
846 if (p_jpeg
->len
>= (unsigned)count
)
848 p_jpeg
->len
-= count
;
849 p_jpeg
->data
+= count
;
852 p_jpeg
->data
+= p_jpeg
->len
;
858 INLINE
void putc(struct jpeg
* p_jpeg
)
864 INLINE
void fill_buf(struct jpeg
* p_jpeg
)
866 p_jpeg
->buf_left
= read(p_jpeg
->fd
, p_jpeg
->buf
, JPEG_READ_BUF_SIZE
);
867 p_jpeg
->buf_index
= 0;
870 static unsigned char *getc(struct jpeg
* p_jpeg
)
872 if (UNLIKELY(p_jpeg
->buf_left
< 1))
874 if (UNLIKELY(p_jpeg
->buf_left
< 1))
877 return (p_jpeg
->buf_index
++) + p_jpeg
->buf
;
880 INLINE
bool skip_bytes_seek(struct jpeg
* p_jpeg
)
882 if (UNLIKELY(lseek(p_jpeg
->fd
, -p_jpeg
->buf_left
, SEEK_CUR
) < 0))
884 p_jpeg
->buf_left
= 0;
888 static bool skip_bytes(struct jpeg
* p_jpeg
, int count
)
890 p_jpeg
->buf_left
-= count
;
891 p_jpeg
->buf_index
+= count
;
892 return p_jpeg
->buf_left
>= 0 || skip_bytes_seek(p_jpeg
);
895 static void putc(struct jpeg
* p_jpeg
)
902 #define e_skip_bytes(jpeg, count) \
904 if (UNLIKELY(!skip_bytes((jpeg),(count)))) \
908 #define e_getc(jpeg, code) \
911 if (UNLIKELY(!(c = getc(jpeg)))) \
916 #define d_getc(jpeg, def) \
918 unsigned char *cp = getc(jpeg); \
919 unsigned char c = LIKELY(cp) ? *cp : (def); \
923 /* Preprocess the JPEG JFIF file */
924 static int process_markers(struct jpeg
* p_jpeg
)
927 int marker_size
; /* variable length of marker segment */
929 int ret
= 0; /* returned flags */
931 while ((c
= e_getc(p_jpeg
, -1)))
933 if (c
!= 0xFF) /* no marker? */
935 JDEBUGF("Non-marker data\n");
937 break; /* exit marker processing */
940 c
= e_getc(p_jpeg
, -1);
941 JDEBUGF("marker value %X\n",c
);
944 case 0xFF: /* Fill byte */
946 case 0x00: /* Zero stuffed byte - entropy data */
950 case 0xC0: /* SOF Huff - Baseline DCT */
952 JDEBUGF("SOF marker ");
954 marker_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
955 marker_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
956 JDEBUGF("len: %d\n", marker_size
);
957 n
= e_getc(p_jpeg
, -1); /* sample precision (= 8 or 12) */
960 return(-1); /* Unsupported sample precision */
962 p_jpeg
->y_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
963 p_jpeg
->y_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
964 p_jpeg
->x_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
965 p_jpeg
->x_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
966 JDEBUGF(" dimensions: %dx%d\n", p_jpeg
->x_size
,
969 n
= (marker_size
-2-6)/3;
970 if (e_getc(p_jpeg
, -1) != n
|| (n
!= 1 && n
!= 3))
972 return(-2); /* Unsupported SOF0 component specification */
977 p_jpeg
->frameheader
[i
].ID
= e_getc(p_jpeg
, -1);
978 p_jpeg
->frameheader
[i
].horizontal_sampling
=
979 (c
= e_getc(p_jpeg
, -1)) >> 4;
980 p_jpeg
->frameheader
[i
].vertical_sampling
= c
& 0x0F;
981 p_jpeg
->frameheader
[i
].quanttable_select
=
983 if (p_jpeg
->frameheader
[i
].horizontal_sampling
> 2
984 || p_jpeg
->frameheader
[i
].vertical_sampling
> 2)
985 return -3; /* Unsupported SOF0 subsampling */
991 case 0xC1: /* SOF Huff - Extended sequential DCT*/
992 case 0xC2: /* SOF Huff - Progressive DCT*/
993 case 0xC3: /* SOF Huff - Spatial (sequential) lossless*/
994 case 0xC5: /* SOF Huff - Differential sequential DCT*/
995 case 0xC6: /* SOF Huff - Differential progressive DCT*/
996 case 0xC7: /* SOF Huff - Differential spatial*/
997 case 0xC8: /* SOF Arith - Reserved for JPEG extensions*/
998 case 0xC9: /* SOF Arith - Extended sequential DCT*/
999 case 0xCA: /* SOF Arith - Progressive DCT*/
1000 case 0xCB: /* SOF Arith - Spatial (sequential) lossless*/
1001 case 0xCD: /* SOF Arith - Differential sequential DCT*/
1002 case 0xCE: /* SOF Arith - Differential progressive DCT*/
1003 case 0xCF: /* SOF Arith - Differential spatial*/
1005 return (-4); /* other DCT model than baseline not implemented */
1008 case 0xC4: /* Define Huffman Table(s) */
1011 marker_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
1012 marker_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
1015 while (marker_size
> 17) /* another table */
1017 c
= e_getc(p_jpeg
, -1);
1020 i
= c
& 0x0F; /* table index */
1023 return (-5); /* Huffman table index out of range */
1025 if (c
& 0xF0) /* AC table */
1027 for (j
=0; j
<16; j
++)
1029 p_jpeg
->hufftable
[i
].huffmancodes_ac
[j
] =
1030 (c
= e_getc(p_jpeg
, -1));
1034 if(16 + sum
> AC_LEN
)
1035 return -10; /* longer than allowed */
1037 for (; j
< 16 + sum
; j
++)
1039 p_jpeg
->hufftable
[i
].huffmancodes_ac
[j
] =
1046 for (j
=0; j
<16; j
++)
1048 p_jpeg
->hufftable
[i
].huffmancodes_dc
[j
] =
1049 (c
= e_getc(p_jpeg
, -1));
1053 if(16 + sum
> DC_LEN
)
1054 return -11; /* longer than allowed */
1056 for (; j
< 16 + sum
; j
++)
1058 p_jpeg
->hufftable
[i
].huffmancodes_dc
[j
] =
1065 e_skip_bytes(p_jpeg
, marker_size
);
1069 case 0xCC: /* Define Arithmetic coding conditioning(s) */
1070 return(-6); /* Arithmetic coding not supported */
1072 case 0xD8: /* Start of Image */
1075 case 0xD9: /* End of Image */
1078 case 0x01: /* for temp private use arith code */
1079 JDEBUGF("private\n");
1080 break; /* skip parameterless marker */
1083 case 0xDA: /* Start of Scan */
1086 marker_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
1087 marker_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
1090 n
= (marker_size
-1-3)/2;
1091 if (e_getc(p_jpeg
, -1) != n
|| (n
!= 1 && n
!= 3))
1093 return (-7); /* Unsupported SOS component specification */
1098 p_jpeg
->scanheader
[i
].ID
= e_getc(p_jpeg
, -1);
1099 p_jpeg
->scanheader
[i
].DC_select
= (c
= e_getc(p_jpeg
, -1))
1101 p_jpeg
->scanheader
[i
].AC_select
= c
& 0x0F;
1104 /* skip spectral information */
1105 e_skip_bytes(p_jpeg
, marker_size
);
1109 case 0xDB: /* Define quantization Table(s) */
1112 marker_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
1113 marker_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
1116 n
= (marker_size
)/(QUANT_TABLE_LENGTH
+1); /* # of tables */
1119 int id
= e_getc(p_jpeg
, -1); /* ID */
1123 return (-8); /* Unsupported quantization table */
1125 /* Read Quantisation table: */
1126 for (j
=0; j
<QUANT_TABLE_LENGTH
; j
++)
1128 p_jpeg
->quanttable
[id
][j
] = e_getc(p_jpeg
, -1);
1132 e_skip_bytes(p_jpeg
, marker_size
);
1136 case 0xDD: /* Define Restart Interval */
1138 marker_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
1139 marker_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
1142 p_jpeg
->restart_interval
= e_getc(p_jpeg
, -1) << 8;
1143 p_jpeg
->restart_interval
|= e_getc(p_jpeg
, -1); /* Lowbyte */
1144 e_skip_bytes(p_jpeg
, marker_size
); /* skip segment */
1148 case 0xDC: /* Define Number of Lines */
1149 case 0xDE: /* Define Hierarchical progression */
1150 case 0xDF: /* Expand Reference Component(s) */
1151 case 0xE0: /* Application Field 0*/
1152 case 0xE1: /* Application Field 1*/
1153 case 0xE2: /* Application Field 2*/
1154 case 0xE3: /* Application Field 3*/
1155 case 0xE4: /* Application Field 4*/
1156 case 0xE5: /* Application Field 5*/
1157 case 0xE6: /* Application Field 6*/
1158 case 0xE7: /* Application Field 7*/
1159 case 0xE8: /* Application Field 8*/
1160 case 0xE9: /* Application Field 9*/
1161 case 0xEA: /* Application Field 10*/
1162 case 0xEB: /* Application Field 11*/
1163 case 0xEC: /* Application Field 12*/
1164 case 0xED: /* Application Field 13*/
1165 case 0xEE: /* Application Field 14*/
1166 case 0xEF: /* Application Field 15*/
1167 case 0xFE: /* Comment */
1169 marker_size
= e_getc(p_jpeg
, -1) << 8; /* Highbyte */
1170 marker_size
|= e_getc(p_jpeg
, -1); /* Lowbyte */
1172 JDEBUGF("unhandled marker len %d\n", marker_size
);
1173 e_skip_bytes(p_jpeg
, marker_size
); /* skip segment */
1177 case 0xF0: /* Reserved for JPEG extensions */
1178 case 0xF1: /* Reserved for JPEG extensions */
1179 case 0xF2: /* Reserved for JPEG extensions */
1180 case 0xF3: /* Reserved for JPEG extensions */
1181 case 0xF4: /* Reserved for JPEG extensions */
1182 case 0xF5: /* Reserved for JPEG extensions */
1183 case 0xF6: /* Reserved for JPEG extensions */
1184 case 0xF7: /* Reserved for JPEG extensions */
1185 case 0xF8: /* Reserved for JPEG extensions */
1186 case 0xF9: /* Reserved for JPEG extensions */
1187 case 0xFA: /* Reserved for JPEG extensions */
1188 case 0xFB: /* Reserved for JPEG extensions */
1189 case 0xFC: /* Reserved for JPEG extensions */
1190 case 0xFD: /* Reserved for JPEG extensions */
1191 case 0x02: /* Reserved */
1193 return (-9); /* Unknown marker */
1197 return (ret
); /* return flags with seen markers */
1200 static const struct huffman_table luma_table
=
1203 0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
1204 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B
1207 0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,
1208 0x01,0x7D,0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,
1209 0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,
1210 0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,0x33,0x62,0x72,0x82,0x09,0x0A,0x16,
1211 0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,0x35,0x36,0x37,
1212 0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,
1213 0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,
1214 0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
1215 0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,0xA4,0xA5,
1216 0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,
1217 0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,
1218 0xD7,0xD8,0xD9,0xDA,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,
1219 0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA
1223 static const struct huffman_table chroma_table
=
1226 0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,
1227 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B
1230 0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01,
1231 0x02,0x77,0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,
1232 0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,
1233 0xC1,0x09,0x23,0x33,0x52,0xF0,0x15,0x62,0x72,0xD1,0x0A,0x16,0x24,0x34,
1234 0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,0x27,0x28,0x29,0x2A,0x35,0x36,
1235 0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,
1236 0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
1237 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87,
1238 0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,
1239 0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,
1240 0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,
1241 0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,
1242 0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA
1246 static void default_huff_tbl(struct jpeg
* p_jpeg
)
1249 MEMCPY(&p_jpeg
->hufftable
[0], &luma_table
, sizeof(luma_table
));
1250 MEMCPY(&p_jpeg
->hufftable
[1], &chroma_table
, sizeof(chroma_table
));
1255 /* Compute the derived values for a Huffman table */
1256 static void fix_huff_tbl(int* htbl
, struct derived_tbl
* dtbl
)
1261 unsigned int huffcode
[257];
1264 dtbl
->pub
= htbl
; /* fill in back link */
1266 /* Figure C.1: make table of Huffman code length for each symbol */
1267 /* Note that this is in code-length order. */
1270 for (l
= 1; l
<= 16; l
++)
1271 { /* all possible code length */
1272 for (i
= 1; i
<= (int) htbl
[l
-1]; i
++) /* all codes per length */
1273 huffsize
[p
++] = (char) l
;
1277 /* Figure C.2: generate the codes themselves */
1278 /* Note that this is in code-length order. */
1285 while (((int) huffsize
[p
]) == si
)
1287 huffcode
[p
++] = code
;
1294 /* Figure F.15: generate decoding tables for bit-sequential decoding */
1297 for (l
= 1; l
<= 16; l
++)
1301 /* huffval[] index of 1st symbol of code length l */
1302 dtbl
->valptr
[l
] = p
;
1303 dtbl
->mincode
[l
] = huffcode
[p
]; /* minimum code of length l */
1305 dtbl
->maxcode
[l
] = huffcode
[p
-1]; /* maximum code of length l */
1309 dtbl
->maxcode
[l
] = -1; /* -1 if no codes of this length */
1312 dtbl
->maxcode
[17] = 0xFFFFFL
; /* ensures huff_DECODE terminates */
1314 /* Compute lookahead tables to speed up decoding.
1315 * First we set all the table entries to 0, indicating "too long";
1316 * then we iterate through the Huffman codes that are short enough and
1317 * fill in all the entries that correspond to bit sequences starting
1321 MEMSET(dtbl
->look_nbits
, 0, sizeof(dtbl
->look_nbits
));
1324 for (l
= 1; l
<= HUFF_LOOKAHEAD
; l
++)
1326 for (i
= 1; i
<= (int) htbl
[l
-1]; i
++, p
++)
1328 /* l = current code's length, p = its index in huffcode[] &
1329 * huffval[]. Generate left-justified code followed by all possible
1332 lookbits
= huffcode
[p
] << (HUFF_LOOKAHEAD
-l
);
1333 for (ctr
= 1 << (HUFF_LOOKAHEAD
-l
); ctr
> 0; ctr
--)
1335 dtbl
->look_nbits
[lookbits
] = l
;
1336 dtbl
->look_sym
[lookbits
] = htbl
[16+p
];
1344 /* zag[i] is the natural-order position of the i'th element of zigzag order.
1345 * If the incoming data is corrupted, decode_mcu could attempt to
1346 * reference values beyond the end of the array. To avoid a wild store,
1347 * we put some extra zeroes after the real entries.
1349 static const unsigned char zag
[] =
1351 0, 1, 8, 16, 9, 2, 3, 10,
1352 17, 24, 32, 25, 18, 11, 4, 5,
1353 12, 19, 26, 33, 40, 48, 41, 34,
1354 27, 20, 13, 6, 7, 14, 21, 28,
1355 35, 42, 49, 56, 57, 50, 43, 36,
1356 29, 22, 15, 23, 30, 37, 44, 51,
1357 58, 59, 52, 45, 38, 31, 39, 46,
1358 53, 60, 61, 54, 47, 55, 62, 63,
1359 0, 0, 0, 0, 0, 0, 0, 0, /* extra entries in case k>63 below */
1360 0, 0, 0, 0, 0, 0, 0, 0
1363 /* zig[i] is the the zig-zag order position of the i'th element of natural
1364 * order, reading left-to-right then top-to-bottom.
1366 static const unsigned char zig
[] =
1368 0, 1, 5, 6, 14, 15, 27, 28,
1369 2, 4, 7, 13, 16, 26, 29, 42,
1370 3, 8, 12, 17, 25, 30, 41, 43,
1371 9, 11, 18, 24, 31, 40, 44, 53,
1372 10, 19, 23, 32, 39, 45, 52, 54,
1373 20, 22, 33, 38, 46, 51, 55, 60,
1374 21, 34, 37, 47, 50, 56, 59, 61,
1375 35, 36, 48, 49, 57, 58, 62, 63
1378 /* Reformat some image header data so that the decoder can use it properly. */
1379 INLINE
void fix_headers(struct jpeg
* p_jpeg
)
1384 p_jpeg
->store_pos
[i
] = i
; /* default ordering */
1386 /* assignments for the decoding of blocks */
1387 if (p_jpeg
->frameheader
[0].horizontal_sampling
== 2
1388 && p_jpeg
->frameheader
[0].vertical_sampling
== 1)
1391 p_jpeg
->x_mbl
= (p_jpeg
->x_size
+15) / 16;
1392 p_jpeg
->x_phys
= p_jpeg
->x_mbl
* 16;
1393 p_jpeg
->y_mbl
= (p_jpeg
->y_size
+7) / 8;
1394 p_jpeg
->y_phys
= p_jpeg
->y_mbl
* 8;
1395 p_jpeg
->mcu_membership
[0] = 0; /* Y1=Y2=0, U=1, V=2 */
1396 p_jpeg
->mcu_membership
[1] = 0;
1397 p_jpeg
->mcu_membership
[2] = 1;
1398 p_jpeg
->mcu_membership
[3] = 2;
1399 p_jpeg
->tab_membership
[0] = 0; /* DC, DC, AC, AC */
1400 p_jpeg
->tab_membership
[1] = 0;
1401 p_jpeg
->tab_membership
[2] = 1;
1402 p_jpeg
->tab_membership
[3] = 1;
1403 p_jpeg
->subsample_x
[0] = 1;
1404 p_jpeg
->subsample_x
[1] = 2;
1405 p_jpeg
->subsample_x
[2] = 2;
1406 p_jpeg
->subsample_y
[0] = 1;
1407 p_jpeg
->subsample_y
[1] = 1;
1408 p_jpeg
->subsample_y
[2] = 1;
1410 if (p_jpeg
->frameheader
[0].horizontal_sampling
== 1
1411 && p_jpeg
->frameheader
[0].vertical_sampling
== 2)
1412 { /* 4:2:2 vertically subsampled */
1413 p_jpeg
->store_pos
[1] = 2; /* block positions are mirrored */
1414 p_jpeg
->store_pos
[2] = 1;
1416 p_jpeg
->x_mbl
= (p_jpeg
->x_size
+7) / 8;
1417 p_jpeg
->x_phys
= p_jpeg
->x_mbl
* 8;
1418 p_jpeg
->y_mbl
= (p_jpeg
->y_size
+15) / 16;
1419 p_jpeg
->y_phys
= p_jpeg
->y_mbl
* 16;
1420 p_jpeg
->mcu_membership
[0] = 0; /* Y1=Y2=0, U=1, V=2 */
1421 p_jpeg
->mcu_membership
[1] = 0;
1422 p_jpeg
->mcu_membership
[2] = 1;
1423 p_jpeg
->mcu_membership
[3] = 2;
1424 p_jpeg
->tab_membership
[0] = 0; /* DC, DC, AC, AC */
1425 p_jpeg
->tab_membership
[1] = 0;
1426 p_jpeg
->tab_membership
[2] = 1;
1427 p_jpeg
->tab_membership
[3] = 1;
1428 p_jpeg
->subsample_x
[0] = 1;
1429 p_jpeg
->subsample_x
[1] = 1;
1430 p_jpeg
->subsample_x
[2] = 1;
1431 p_jpeg
->subsample_y
[0] = 1;
1432 p_jpeg
->subsample_y
[1] = 2;
1433 p_jpeg
->subsample_y
[2] = 2;
1435 else if (p_jpeg
->frameheader
[0].horizontal_sampling
== 2
1436 && p_jpeg
->frameheader
[0].vertical_sampling
== 2)
1439 p_jpeg
->x_mbl
= (p_jpeg
->x_size
+15) / 16;
1440 p_jpeg
->x_phys
= p_jpeg
->x_mbl
* 16;
1441 p_jpeg
->y_mbl
= (p_jpeg
->y_size
+15) / 16;
1442 p_jpeg
->y_phys
= p_jpeg
->y_mbl
* 16;
1443 p_jpeg
->mcu_membership
[0] = 0;
1444 p_jpeg
->mcu_membership
[1] = 0;
1445 p_jpeg
->mcu_membership
[2] = 0;
1446 p_jpeg
->mcu_membership
[3] = 0;
1447 p_jpeg
->mcu_membership
[4] = 1;
1448 p_jpeg
->mcu_membership
[5] = 2;
1449 p_jpeg
->tab_membership
[0] = 0;
1450 p_jpeg
->tab_membership
[1] = 0;
1451 p_jpeg
->tab_membership
[2] = 0;
1452 p_jpeg
->tab_membership
[3] = 0;
1453 p_jpeg
->tab_membership
[4] = 1;
1454 p_jpeg
->tab_membership
[5] = 1;
1455 p_jpeg
->subsample_x
[0] = 1;
1456 p_jpeg
->subsample_x
[1] = 2;
1457 p_jpeg
->subsample_x
[2] = 2;
1458 p_jpeg
->subsample_y
[0] = 1;
1459 p_jpeg
->subsample_y
[1] = 2;
1460 p_jpeg
->subsample_y
[2] = 2;
1462 else if (p_jpeg
->frameheader
[0].horizontal_sampling
== 1
1463 && p_jpeg
->frameheader
[0].vertical_sampling
== 1)
1465 /* don't overwrite p_jpeg->blocks */
1466 p_jpeg
->x_mbl
= (p_jpeg
->x_size
+7) / 8;
1467 p_jpeg
->x_phys
= p_jpeg
->x_mbl
* 8;
1468 p_jpeg
->y_mbl
= (p_jpeg
->y_size
+7) / 8;
1469 p_jpeg
->y_phys
= p_jpeg
->y_mbl
* 8;
1470 p_jpeg
->mcu_membership
[0] = 0;
1471 p_jpeg
->mcu_membership
[1] = 1;
1472 p_jpeg
->mcu_membership
[2] = 2;
1473 p_jpeg
->tab_membership
[0] = 0;
1474 p_jpeg
->tab_membership
[1] = 1;
1475 p_jpeg
->tab_membership
[2] = 1;
1476 p_jpeg
->subsample_x
[0] = 1;
1477 p_jpeg
->subsample_x
[1] = 1;
1478 p_jpeg
->subsample_x
[2] = 1;
1479 p_jpeg
->subsample_y
[0] = 1;
1480 p_jpeg
->subsample_y
[1] = 1;
1481 p_jpeg
->subsample_y
[2] = 1;
1490 INLINE
void fix_huff_tables(struct jpeg
*p_jpeg
)
1492 fix_huff_tbl(p_jpeg
->hufftable
[0].huffmancodes_dc
,
1493 &p_jpeg
->dc_derived_tbls
[0]);
1494 fix_huff_tbl(p_jpeg
->hufftable
[0].huffmancodes_ac
,
1495 &p_jpeg
->ac_derived_tbls
[0]);
1496 fix_huff_tbl(p_jpeg
->hufftable
[1].huffmancodes_dc
,
1497 &p_jpeg
->dc_derived_tbls
[1]);
1498 fix_huff_tbl(p_jpeg
->hufftable
[1].huffmancodes_ac
,
1499 &p_jpeg
->ac_derived_tbls
[1]);
1502 /* Because some of the IDCT routines never multiply by any constants, and
1503 * therefore do not produce shifted output, we add the shift into the
1504 * quantization table when one of these IDCT routines is used, rather than
1505 * have the IDCT shift each value it processes.
1507 INLINE
void fix_quant_tables(struct jpeg
*p_jpeg
)
1509 int shift
, i
, x
, y
, a
;
1510 for (i
= 0; i
< 2; i
++)
1512 shift
= idct_tbl
[p_jpeg
->v_scale
[i
]].v_scale
+
1513 idct_tbl
[p_jpeg
->h_scale
[i
]].h_scale
;
1517 for (y
= 0; y
< 1 << p_jpeg
->h_scale
[i
]; y
++)
1519 for (x
= 0; x
< 1 << p_jpeg
->v_scale
[i
]; x
++)
1520 p_jpeg
->quanttable
[i
][zig
[a
+x
]] <<= shift
;
1528 * These functions/macros provide the in-line portion of bit fetching.
1529 * Use check_bit_buffer to ensure there are N bits in get_buffer
1530 * before using get_bits, peek_bits, or drop_bits.
1531 * check_bit_buffer(state,n,action);
1532 * Ensure there are N bits in get_buffer; if suspend, take action.
1533 * val = get_bits(n);
1534 * Fetch next N bits.
1535 * val = peek_bits(n);
1536 * Fetch next N bits without removing them from the buffer.
1538 * Discard next N bits.
1539 * The value N should be a simple variable, not an expression, because it
1540 * is evaluated multiple times.
1543 static void fill_bit_buffer(struct jpeg
* p_jpeg
)
1545 unsigned char byte
, marker
;
1547 if (p_jpeg
->marker_val
)
1548 p_jpeg
->marker_ind
+= 16;
1549 byte
= d_getc(p_jpeg
, 0);
1550 if (UNLIKELY(byte
== 0xFF)) /* legal marker can be byte stuffing or RSTm */
1551 { /* simplification: just skip the (one-byte) marker code */
1552 marker
= d_getc(p_jpeg
, 0);
1553 if ((marker
& ~7) == 0xD0)
1555 p_jpeg
->marker_val
= marker
;
1556 p_jpeg
->marker_ind
= 8;
1559 p_jpeg
->bitbuf
= (p_jpeg
->bitbuf
<< 8) | byte
;
1561 byte
= d_getc(p_jpeg
, 0);
1562 if (UNLIKELY(byte
== 0xFF)) /* legal marker can be byte stuffing or RSTm */
1563 { /* simplification: just skip the (one-byte) marker code */
1564 marker
= d_getc(p_jpeg
, 0);
1565 if ((marker
& ~7) == 0xD0)
1567 p_jpeg
->marker_val
= marker
;
1568 p_jpeg
->marker_ind
= 0;
1571 p_jpeg
->bitbuf
= (p_jpeg
->bitbuf
<< 8) | byte
;
1572 p_jpeg
->bitbuf_bits
+= 16;
1573 #ifdef JPEG_BS_DEBUG
1574 DEBUGF("read in: %04X\n", p_jpeg
->bitbuf
& 0xFFFF);
1578 INLINE
void check_bit_buffer(struct jpeg
*p_jpeg
, int nbits
)
1580 if (nbits
> p_jpeg
->bitbuf_bits
)
1581 fill_bit_buffer(p_jpeg
);
1584 INLINE
int get_bits(struct jpeg
*p_jpeg
, int nbits
)
1586 #ifdef JPEG_BS_DEBUG
1587 if (nbits
> p_jpeg
->bitbuf_bits
)
1588 DEBUGF("bitbuffer underrun\n");
1589 int mask
= 1 << (p_jpeg
->bitbuf_bits
- 1);
1591 DEBUGF("get %d bits: ", nbits
);
1592 for (i
= 0; i
< nbits
; i
++)
1593 DEBUGF("%d",!!(p_jpeg
->bitbuf
& (mask
>>= 1)));
1596 return ((int) (p_jpeg
->bitbuf
>> (p_jpeg
->bitbuf_bits
-= nbits
))) &
1600 INLINE
int peek_bits(struct jpeg
*p_jpeg
, int nbits
)
1602 #ifdef JPEG_BS_DEBUG
1603 int mask
= 1 << (p_jpeg
->bitbuf_bits
- 1);
1605 DEBUGF("peek %d bits: ", nbits
);
1606 for (i
= 0; i
< nbits
; i
++)
1607 DEBUGF("%d",!!(p_jpeg
->bitbuf
& (mask
>>= 1)));
1610 return ((int) (p_jpeg
->bitbuf
>> (p_jpeg
->bitbuf_bits
- nbits
))) &
1614 INLINE
void drop_bits(struct jpeg
*p_jpeg
, int nbits
)
1616 #ifdef JPEG_BS_DEBUG
1617 int mask
= 1 << (p_jpeg
->bitbuf_bits
- 1);
1619 DEBUGF("drop %d bits: ", nbits
);
1620 for (i
= 0; i
< nbits
; i
++)
1621 DEBUGF("%d",!!(p_jpeg
->bitbuf
& (mask
>>= 1)));
1624 p_jpeg
->bitbuf_bits
-= nbits
;
1627 /* re-synchronize to entropy data (skip restart marker) */
1628 static void search_restart(struct jpeg
*p_jpeg
)
1630 if (p_jpeg
->marker_val
)
1632 p_jpeg
->marker_val
= 0;
1633 p_jpeg
->bitbuf_bits
= p_jpeg
->marker_ind
;
1634 p_jpeg
->marker_ind
= 0;
1638 p_jpeg
->bitbuf_bits
= 0;
1639 while ((byte
= d_getc(p_jpeg
, 0xFF)))
1643 byte
= d_getc(p_jpeg
, 0xD0);
1644 if ((byte
& ~7) == 0xD0)
1654 /* Figure F.12: extend sign bit. */
1655 #if CONFIG_CPU == SH7034
1656 /* SH1 lacks a variable-shift instruction */
1657 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
1659 static const int extend_test
[16] = /* entry n is 2**(n-1) */
1661 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
1662 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
1665 static const int extend_offset
[16] = /* entry n is (-1 << n) + 1 */
1667 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
1668 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
1669 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
1670 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1
1673 /* This saves some code and data size, benchmarks about the same on RAM */
1674 #define HUFF_EXTEND(x,s) \
1678 x__ & (1 << (s__- 1)) ? x__ : x__ + (-1 << s__) + 1; \
1682 /* Decode a single value */
1683 #define huff_decode_dc(p_jpeg, tbl, s, r) \
1687 check_bit_buffer((p_jpeg), HUFF_LOOKAHEAD); \
1688 look = peek_bits((p_jpeg), HUFF_LOOKAHEAD); \
1689 if ((nb = (tbl)->look_nbits[look]) != 0) \
1691 drop_bits((p_jpeg), nb); \
1692 s = (tbl)->look_sym[look]; \
1693 check_bit_buffer((p_jpeg), s); \
1694 r = get_bits((p_jpeg), s); \
1696 /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */ \
1698 nb=HUFF_LOOKAHEAD+1; \
1699 check_bit_buffer((p_jpeg), nb); \
1700 code = get_bits((p_jpeg), nb); \
1701 while (code > (tbl)->maxcode[nb]) \
1704 check_bit_buffer((p_jpeg), 1); \
1705 code |= get_bits((p_jpeg), 1); \
1708 if (nb > 16) /* error in Huffman */ \
1710 r = 0; s = 0; /* fake a zero, this is most safe */ \
1712 s = (tbl)->pub[16 + (tbl)->valptr[nb] + \
1713 ((int) (code - (tbl)->mincode[nb]))]; \
1714 check_bit_buffer((p_jpeg), s); \
1715 r = get_bits((p_jpeg), s); \
1717 } /* end slow decode */ \
1720 #define huff_decode_ac(p_jpeg, tbl, s) \
1724 check_bit_buffer((p_jpeg), HUFF_LOOKAHEAD); \
1725 look = peek_bits((p_jpeg), HUFF_LOOKAHEAD); \
1726 if ((nb = (tbl)->look_nbits[look]) != 0) \
1728 drop_bits((p_jpeg), nb); \
1729 s = (tbl)->look_sym[look]; \
1731 /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */ \
1733 nb=HUFF_LOOKAHEAD+1; \
1734 check_bit_buffer((p_jpeg), nb); \
1735 code = get_bits((p_jpeg), nb); \
1736 while (code > (tbl)->maxcode[nb]) \
1739 check_bit_buffer((p_jpeg), 1); \
1740 code |= get_bits((p_jpeg), 1); \
1743 if (nb > 16) /* error in Huffman */ \
1745 s = 0; /* fake a zero, this is most safe */ \
1747 s = (tbl)->pub[16 + (tbl)->valptr[nb] + \
1748 ((int) (code - (tbl)->mincode[nb]))]; \
1750 } /* end slow decode */ \
1753 static struct img_part
*store_row_jpeg(void *jpeg_args
)
1755 struct jpeg
*p_jpeg
= (struct jpeg
*) jpeg_args
;
1756 #ifdef HAVE_LCD_COLOR
1757 int mcu_hscale
= p_jpeg
->h_scale
[1];
1758 int mcu_vscale
= p_jpeg
->v_scale
[1];
1760 int mcu_hscale
= (p_jpeg
->h_scale
[0] +
1761 p_jpeg
->frameheader
[0].horizontal_sampling
- 1);
1762 int mcu_vscale
= (p_jpeg
->v_scale
[0] +
1763 p_jpeg
->frameheader
[0].vertical_sampling
- 1);
1765 unsigned int width
= p_jpeg
->x_mbl
<< mcu_hscale
;
1766 unsigned int b_width
= width
* JPEG_PIX_SZ
;
1767 int height
= 1U << mcu_vscale
;
1769 if (!p_jpeg
->mcu_row
) /* Need to decode a new row of MCUs */
1771 p_jpeg
->out_ptr
= (unsigned char *)p_jpeg
->img_buf
;
1773 #ifdef HAVE_LCD_COLOR
1774 unsigned mcu_width
= 1U << mcu_hscale
;
1776 int mcu_offset
= JPEG_PIX_SZ
<< mcu_hscale
;
1777 unsigned char *out
= p_jpeg
->out_ptr
;
1778 store_offs
[p_jpeg
->store_pos
[0]] = 0;
1779 store_offs
[p_jpeg
->store_pos
[1]] = JPEG_PIX_SZ
<< p_jpeg
->h_scale
[0];
1780 store_offs
[p_jpeg
->store_pos
[2]] = b_width
<< p_jpeg
->v_scale
[0];
1781 store_offs
[p_jpeg
->store_pos
[3]] = store_offs
[1] + store_offs
[2];
1783 int block
[128]; /* decoded DCT coefficients */
1784 for (x
= 0; x
< p_jpeg
->x_mbl
; x
++)
1787 for (blkn
= 0; blkn
< p_jpeg
->blocks
; blkn
++)
1789 int k
= 1; /* coefficient index */
1790 int s
, r
; /* huffman values */
1791 int ci
= p_jpeg
->mcu_membership
[blkn
]; /* component index */
1792 int ti
= p_jpeg
->tab_membership
[blkn
]; /* table index */
1793 struct derived_tbl
* dctbl
= &p_jpeg
->dc_derived_tbls
[ti
];
1794 struct derived_tbl
* actbl
= &p_jpeg
->ac_derived_tbls
[ti
];
1796 /* Section F.2.2.1: decode the DC coefficient difference */
1797 huff_decode_dc(p_jpeg
, dctbl
, s
, r
);
1799 #ifndef HAVE_LCD_COLOR
1803 s
= HUFF_EXTEND(r
, s
);
1804 #ifdef HAVE_LCD_COLOR
1805 p_jpeg
->last_dc_val
[ci
] += s
;
1806 /* output it (assumes zag[0] = 0) */
1807 block
[0] = p_jpeg
->last_dc_val
[ci
] *
1808 p_jpeg
->quanttable
[!!ci
][0];
1810 p_jpeg
->last_dc_val
+= s
;
1811 /* output it (assumes zag[0] = 0) */
1812 block
[0] = p_jpeg
->last_dc_val
*
1813 p_jpeg
->quanttable
[0][0];
1815 /* coefficient buffer must be cleared */
1816 MEMSET(block
+1, 0, p_jpeg
->zero_need
[!!ci
] * sizeof(int));
1817 /* Section F.2.2.2: decode the AC coefficients */
1818 for (; k
< p_jpeg
->k_need
[!!ci
]; k
++)
1820 huff_decode_ac(p_jpeg
, actbl
, s
);
1826 check_bit_buffer(p_jpeg
, s
);
1827 r
= get_bits(p_jpeg
, s
);
1828 r
= HUFF_EXTEND(r
, s
);
1830 if (a
<= zag
[p_jpeg
->k_need
[!!ci
]] && (a
& 7) <=
1831 (zag
[p_jpeg
->k_need
[!!ci
]] & 7))
1833 r
*= p_jpeg
->quanttable
[!!ci
][k
];
1850 huff_decode_ac(p_jpeg
, actbl
, s
);
1857 check_bit_buffer(p_jpeg
, s
);
1858 drop_bits(p_jpeg
, s
);
1867 #ifndef HAVE_LCD_COLOR
1871 int idct_cols
= 1 << MIN(p_jpeg
->h_scale
[!!ci
], 3);
1872 int idct_rows
= 1 << p_jpeg
->v_scale
[!!ci
];
1873 unsigned char *b_out
= out
+ (ci
? ci
: store_offs
[blkn
]);
1874 if (idct_tbl
[p_jpeg
->v_scale
[!!ci
]].v_idct
)
1875 idct_tbl
[p_jpeg
->v_scale
[!!ci
]].v_idct(block
,
1877 idct_tbl
[p_jpeg
->h_scale
[!!ci
]].h_idct(block
, b_out
,
1878 idct_rows
, b_width
);
1881 /* don't starve other threads while an MCU row decodes */
1883 #ifdef HAVE_LCD_COLOR
1886 unsigned char *row
= out
;
1887 if (p_jpeg
->blocks
== 1)
1889 for (yp
= 0; yp
< height
; yp
++, row
+= b_width
)
1891 unsigned char *px
= row
;
1892 for (xp
= 0; xp
< mcu_width
; xp
++, px
+= JPEG_PIX_SZ
)
1894 px
[1] = px
[2] = px
[0];
1900 if (p_jpeg
->restart_interval
&& --p_jpeg
->restart
== 0)
1901 { /* if a restart marker is due: */
1902 p_jpeg
->restart
= p_jpeg
->restart_interval
; /* count again */
1903 search_restart(p_jpeg
); /* align the bitstream */
1904 #ifdef HAVE_LCD_COLOR
1905 p_jpeg
->last_dc_val
[0] = p_jpeg
->last_dc_val
[1] =
1906 p_jpeg
->last_dc_val
[2] = 0; /* reset decoder */
1908 p_jpeg
->last_dc_val
= 0;
1912 } /* if !p_jpeg->mcu_row */
1913 p_jpeg
->mcu_row
= (p_jpeg
->mcu_row
+ 1) & (height
- 1);
1914 p_jpeg
->part
.len
= width
;
1915 p_jpeg
->part
.buf
= (jpeg_pix_t
*)p_jpeg
->out_ptr
;
1916 p_jpeg
->out_ptr
+= b_width
;
1917 return &(p_jpeg
->part
);
1920 /******************************************************************************
1923 * Reads a JPEG file and puts the data in rockbox format in *bitmap.
1925 *****************************************************************************/
1926 #ifndef JPEG_FROM_MEM
1927 int read_jpeg_file(const char* filename
,
1931 const struct custom_format
*cformat
)
1934 fd
= open(filename
, O_RDONLY
);
1936 JDEBUGF("read_jpeg_file: filename: %s buffer len: %d cformat: %p\n",
1937 filename
, maxsize
, cformat
);
1938 /* Exit if file opening failed */
1940 DEBUGF("read_jpeg_file: can't open '%s', rc: %d\n", filename
, fd
);
1944 ret
= read_jpeg_fd(fd
, bm
, maxsize
, format
, cformat
);
1950 static int calc_scale(int in_size
, int out_size
)
1954 for (scale
= 0; scale
< 3; scale
++)
1956 if (out_size
<= in_size
)
1964 #ifdef JPEG_FROM_MEM
1965 int get_jpeg_dim_mem(unsigned char *data
, unsigned long len
,
1968 struct jpeg
*p_jpeg
= &jpeg
;
1969 memset(p_jpeg
, 0, sizeof(struct jpeg
));
1970 p_jpeg
->data
= data
;
1972 int status
= process_markers(p_jpeg
);
1975 if ((status
& (DQT
| SOF0
)) != (DQT
| SOF0
))
1976 return -(status
* 16);
1977 size
->width
= p_jpeg
->x_size
;
1978 size
->height
= p_jpeg
->y_size
;
1982 int decode_jpeg_mem(unsigned char *data
, unsigned long len
,
1984 int read_jpeg_fd(int fd
,
1989 const struct custom_format
*cformat
)
1991 bool resize
= false, dither
= false;
1996 #ifdef JPEG_FROM_MEM
1997 struct jpeg
*p_jpeg
= &jpeg
;
1999 struct jpeg
*p_jpeg
= (struct jpeg
*)bm
->data
;
2000 int tmp_size
= maxsize
;
2001 ALIGN_BUFFER(p_jpeg
, tmp_size
, sizeof(int));
2002 /* not enough memory for our struct jpeg */
2003 if ((size_t)tmp_size
< sizeof(struct jpeg
))
2006 memset(p_jpeg
, 0, sizeof(struct jpeg
));
2007 #ifdef JPEG_FROM_MEM
2008 p_jpeg
->data
= data
;
2013 status
= process_markers(p_jpeg
);
2014 #ifndef JPEG_FROM_MEM
2015 JDEBUGF("position in file: %d buffer fill: %d\n",
2016 (int)lseek(p_jpeg
->fd
, 0, SEEK_CUR
), p_jpeg
->buf_left
);
2020 if ((status
& (DQT
| SOF0
)) != (DQT
| SOF0
))
2021 return -(status
* 16);
2022 if (!(status
& DHT
)) /* if no Huffman table present: */
2023 default_huff_tbl(p_jpeg
); /* use default */
2024 fix_headers(p_jpeg
); /* derive Huffman and other lookup-tables */
2025 src_dim
.width
= p_jpeg
->x_size
;
2026 src_dim
.height
= p_jpeg
->y_size
;
2027 if (format
& FORMAT_RESIZE
)
2029 if (format
& FORMAT_DITHER
)
2032 struct dim resize_dim
= {
2034 .height
= bm
->height
,
2036 if (format
& FORMAT_KEEP_ASPECT
)
2037 recalc_dimension(&resize_dim
, &src_dim
);
2038 bm
->width
= resize_dim
.width
;
2039 bm
->height
= resize_dim
.height
;
2041 bm
->width
= p_jpeg
->x_size
;
2042 bm
->height
= p_jpeg
->y_size
;
2044 p_jpeg
->h_scale
[0] = calc_scale(p_jpeg
->x_size
, bm
->width
);
2045 p_jpeg
->v_scale
[0] = calc_scale(p_jpeg
->y_size
, bm
->height
);
2046 JDEBUGF("luma IDCT size: %dx%d\n", 1 << p_jpeg
->h_scale
[0],
2047 1 << p_jpeg
->v_scale
[0]);
2048 if ((p_jpeg
->x_size
<< p_jpeg
->h_scale
[0]) >> 3 == bm
->width
&&
2049 (p_jpeg
->y_size
<< p_jpeg
->v_scale
[0]) >> 3 == bm
->height
)
2051 #ifdef HAVE_LCD_COLOR
2052 p_jpeg
->h_scale
[1] = p_jpeg
->h_scale
[0] +
2053 p_jpeg
->frameheader
[0].horizontal_sampling
- 1;
2054 p_jpeg
->v_scale
[1] = p_jpeg
->v_scale
[0] +
2055 p_jpeg
->frameheader
[0].vertical_sampling
- 1;
2056 JDEBUGF("chroma IDCT size: %dx%d\n", 1 << p_jpeg
->h_scale
[1],
2057 1 << p_jpeg
->v_scale
[1]);
2059 JDEBUGF("scaling from %dx%d -> %dx%d\n",
2060 (p_jpeg
->x_size
<< p_jpeg
->h_scale
[0]) >> 3,
2061 (p_jpeg
->y_size
<< p_jpeg
->v_scale
[0]) >> 3,
2062 bm
->width
, bm
->height
);
2063 fix_quant_tables(p_jpeg
);
2064 int decode_w
= (1 << p_jpeg
->h_scale
[0]) - 1;
2065 int decode_h
= (1 << p_jpeg
->v_scale
[0]) - 1;
2066 src_dim
.width
= (p_jpeg
->x_size
<< p_jpeg
->h_scale
[0]) >> 3;
2067 src_dim
.height
= (p_jpeg
->y_size
<< p_jpeg
->v_scale
[0]) >> 3;
2068 p_jpeg
->zero_need
[0] = (decode_h
<< 3) + decode_w
;
2069 p_jpeg
->k_need
[0] = zig
[p_jpeg
->zero_need
[0]];
2070 JDEBUGF("need luma components to %d\n", p_jpeg
->k_need
[0]);
2071 #ifdef HAVE_LCD_COLOR
2072 decode_w
= (1 << MIN(p_jpeg
->h_scale
[1],3)) - 1;
2073 decode_h
= (1 << MIN(p_jpeg
->v_scale
[1],3)) - 1;
2074 p_jpeg
->zero_need
[1] = (decode_h
<< 3) + decode_w
;
2075 p_jpeg
->k_need
[1] = zig
[p_jpeg
->zero_need
[1]];
2076 JDEBUGF("need chroma components to %d\n", p_jpeg
->k_need
[1]);
2079 bm_size
= cformat
->get_size(bm
);
2081 bm_size
= BM_SIZE(bm
->width
,bm
->height
,FORMAT_NATIVE
,false);
2082 if (bm_size
> maxsize
)
2084 char *buf_start
= (char *)bm
->data
+ bm_size
;
2085 char *buf_end
= (char *)bm
->data
+ maxsize
;
2086 maxsize
= buf_end
- buf_start
;
2087 #ifndef JPEG_FROM_MEM
2088 ALIGN_BUFFER(buf_start
, maxsize
, sizeof(uint32_t));
2089 if (maxsize
< (int)sizeof(struct jpeg
))
2091 memmove(buf_start
, p_jpeg
, sizeof(struct jpeg
));
2092 p_jpeg
= (struct jpeg
*)buf_start
;
2093 buf_start
+= sizeof(struct jpeg
);
2094 maxsize
= buf_end
- buf_start
;
2096 fix_huff_tables(p_jpeg
);
2097 #ifdef HAVE_LCD_COLOR
2098 int decode_buf_size
= (p_jpeg
->x_mbl
<< p_jpeg
->h_scale
[1])
2099 << p_jpeg
->v_scale
[1];
2101 int decode_buf_size
= (p_jpeg
->x_mbl
<< p_jpeg
->h_scale
[0])
2102 << p_jpeg
->v_scale
[0];
2103 decode_buf_size
<<= p_jpeg
->frameheader
[0].horizontal_sampling
+
2104 p_jpeg
->frameheader
[0].vertical_sampling
- 2;
2106 decode_buf_size
*= JPEG_PIX_SZ
;
2107 JDEBUGF("decode buffer size: %d\n", decode_buf_size
);
2108 p_jpeg
->img_buf
= (jpeg_pix_t
*)buf_start
;
2109 if (buf_end
- buf_start
< decode_buf_size
)
2111 buf_start
+= decode_buf_size
;
2112 maxsize
= buf_end
- buf_start
;
2113 memset(p_jpeg
->img_buf
, 0, decode_buf_size
);
2114 p_jpeg
->mcu_row
= 0;
2115 p_jpeg
->restart
= p_jpeg
->restart_interval
;
2117 rset
.rowstop
= bm
->height
;
2119 p_jpeg
->resize
= resize
;
2122 if (resize_on_load(bm
, dither
, &src_dim
, &rset
, buf_start
, maxsize
,
2123 cformat
, IF_PIX_FMT(p_jpeg
->blocks
== 1 ? 0 : 1,) store_row_jpeg
,
2128 struct scaler_context ctx
= {
2133 void (*output_row_8
)(uint32_t, void*, struct scaler_context
*) =
2134 output_row_8_native
;
2135 #elif defined(PLUGIN)
2136 void (*output_row_8
)(uint32_t, void*, struct scaler_context
*) = NULL
;
2138 #if LCD_DEPTH > 1 || defined(PLUGIN)
2140 output_row_8
= cformat
->output_row_8
;
2142 struct img_part
*part
;
2143 for (row
= 0; row
< bm
->height
; row
++)
2145 part
= store_row_jpeg(p_jpeg
);
2146 #ifdef HAVE_LCD_COLOR
2147 struct uint8_rgb
*qp
= part
->buf
;
2148 struct uint8_rgb
*end
= qp
+ bm
->width
;
2151 for (; qp
< end
; qp
++)
2156 yuv_to_rgb(y
, u
, v
, &r
, &g
, &b
);
2162 output_row_8(row
, part
->buf
, &ctx
);
2169 /**************** end JPEG code ********************/