2 * This file is part of the coreboot project.
4 * Copyright (C) 2001 Michael Schroeder
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 * a tiny jpeg decoder.
19 * written in August 2001 by Michael Schroeder <mls@suse.de>
23 #define __LITTLE_ENDIAN
28 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
29 #define IMULT(a, b) (((a) * (b)) >> ISHIFT)
30 #define ITOINT(a) ((a) >> ISHIFT)
46 int (*func
) __P((void *));
50 /*********************************/
55 struct dec_hufftbl
*dhuff
;
56 struct enc_hufftbl
*ehuff
;
60 int dc
; /* old dc value */
64 int next
; /* when to switch to next scan */
66 int cid
; /* component id */
67 int hv
; /* horiz/vert, copied from comp */
68 int tq
; /* quant tbl, copied from comp */
71 /*********************************/
73 #define DECBITS 10 /* seems to be the optimum */
78 unsigned char vals
[256];
79 unsigned int llvals
[1 << DECBITS
];
82 static void decode_mcus
__P((struct in
*, int *, int, struct scan
*, int *));
83 static int dec_readmarker
__P((struct in
*));
84 static void dec_makehuff
__P((struct dec_hufftbl
*, int *, unsigned char *));
86 static void setinput
__P((struct in
*, unsigned char *));
87 /*********************************/
92 static void idctqtab
__P((unsigned char *, PREC
*));
93 static void idct
__P((int *, int *, PREC
*, PREC
, int));
94 static void scaleidctqtab
__P((PREC
*, PREC
));
96 /*********************************/
98 static void initcol
__P((PREC
[][64]));
100 static void col221111
__P((int *, unsigned char *, int));
101 static void col221111_16
__P((int *, unsigned char *, int));
102 static void col221111_32
__P((int *, unsigned char *, int));
104 /*********************************/
117 static unsigned char *datap
;
119 static int getbyte(void)
124 static int getword(void)
140 int nc
; /* number of components */
141 int ns
; /* number of scans */
142 int dri
; /* restart interval */
143 int nm
; /* mcus til next marker */
144 int rm
; /* next restart marker */
147 static struct jpginfo info
;
148 static struct comp comps
[MAXCOMP
];
150 static struct scan dscans
[MAXCOMP
];
152 static unsigned char quant
[4][64];
154 static struct dec_hufftbl dhuff
[4];
156 #define dec_huffdc (dhuff + 0)
157 #define dec_huffac (dhuff + 2)
159 static struct in glob_in
;
161 static int readtables(int till
)
163 int m
, l
, i
, j
, lq
, pq
, tq
;
167 if (getbyte() != 0xff)
169 if ((m
= getbyte()) == till
)
186 for (i
= 0; i
< 64; i
++)
187 quant
[tq
][i
] = getbyte();
196 unsigned char huffvals
[256];
202 if (tc
> 1 || th
> 1)
204 for (i
= 0; i
< 16; i
++)
205 hufflen
[i
] = getbyte();
208 for (i
= 0; i
< 16; i
++) {
209 for (j
= 0; j
< hufflen
[i
]; j
++)
210 huffvals
[k
++] = getbyte();
213 dec_makehuff(dhuff
+ tt
, hufflen
,
220 info
.dri
= getword();
233 static void dec_initscans(void)
237 info
.nm
= info
.dri
+ 1;
239 for (i
= 0; i
< info
.ns
; i
++)
243 static int dec_checkmarker(void)
247 if (dec_readmarker(&glob_in
) != info
.rm
)
250 info
.rm
= (info
.rm
+ 1) & ~0x08;
251 for (i
= 0; i
< info
.ns
; i
++)
256 void jpeg_fetch_size(unsigned char *buf
, int *width
, int *height
)
268 int jpeg_check_size(unsigned char *buf
, int width
, int height
)
276 if (height
!= getword() || width
!= getword())
281 int jpeg_decode(unsigned char *buf
, unsigned char *pic
,
282 int width
, int height
, int depth
, struct jpeg_decdata
*decdata
)
284 int i
, j
, m
, tac
, tdc
;
285 int mcusx
, mcusy
, mx
, my
;
288 if (!decdata
|| !buf
|| !pic
)
291 if (getbyte() != 0xff)
293 if (getbyte() != M_SOI
)
295 if (readtables(M_SOF0
))
296 return ERR_BAD_TABLES
;
301 if (((getword() + 15) & ~15) != height
)
302 return ERR_HEIGHT_MISMATCH
;
303 if (((getword() + 15) & ~15) != width
)
304 return ERR_WIDTH_MISMATCH
;
305 if ((height
& 15) || (width
& 15))
306 return ERR_BAD_WIDTH_OR_HEIGHT
;
308 if (info
.nc
> MAXCOMP
)
309 return ERR_TOO_MANY_COMPPS
;
310 for (i
= 0; i
< info
.nc
; i
++) {
312 comps
[i
].cid
= getbyte();
313 comps
[i
].hv
= getbyte();
314 v
= comps
[i
].hv
& 15;
315 h
= comps
[i
].hv
>> 4;
316 comps
[i
].tq
= getbyte();
318 return ERR_ILLEGAL_HV
;
320 return ERR_QUANT_TABLE_SELECTOR
;
322 if (readtables(M_SOS
))
323 return ERR_BAD_TABLES
;
327 return ERR_NOT_YCBCR_221111
;
328 for (i
= 0; i
< 3; i
++) {
329 dscans
[i
].cid
= getbyte();
333 if (tdc
> 1 || tac
> 1)
334 return ERR_QUANT_TABLE_SELECTOR
;
335 for (j
= 0; j
< info
.nc
; j
++)
336 if (comps
[j
].cid
== dscans
[i
].cid
)
339 return ERR_UNKNOWN_CID_IN_SCAN
;
340 dscans
[i
].hv
= comps
[j
].hv
;
341 dscans
[i
].tq
= comps
[j
].tq
;
342 dscans
[i
].hudc
.dhuff
= dec_huffdc
+ tdc
;
343 dscans
[i
].huac
.dhuff
= dec_huffac
+ tac
;
350 if (i
!= 0 || j
!= 63 || m
!= 0)
351 return ERR_NOT_SEQUENTIAL_DCT
;
353 if (dscans
[0].cid
!= 1 || dscans
[1].cid
!= 2 || dscans
[2].cid
!= 3)
354 return ERR_NOT_YCBCR_221111
;
356 if (dscans
[0].hv
!= 0x22 || dscans
[1].hv
!= 0x11 || dscans
[2].hv
!= 0x11)
357 return ERR_NOT_YCBCR_221111
;
363 idctqtab(quant
[dscans
[0].tq
], decdata
->dquant
[0]);
364 idctqtab(quant
[dscans
[1].tq
], decdata
->dquant
[1]);
365 idctqtab(quant
[dscans
[2].tq
], decdata
->dquant
[2]);
366 initcol(decdata
->dquant
);
367 setinput(&glob_in
, datap
);
373 img
[len
+ 2] = M_EOF
;
378 dscans
[0].next
= 6 - 4;
379 dscans
[1].next
= 6 - 4 - 1;
380 dscans
[2].next
= 6 - 4 - 1 - 1; /* 411 encoding */
381 for (my
= 0; my
< mcusy
; my
++) {
382 for (mx
= 0; mx
< mcusx
; mx
++) {
383 if (info
.dri
&& !--info
.nm
)
384 if (dec_checkmarker())
385 return ERR_WRONG_MARKER
;
387 decode_mcus(&glob_in
, decdata
->dcts
, 6, dscans
, max
);
388 idct(decdata
->dcts
, decdata
->out
, decdata
->dquant
[0], IFIX(128.5), max
[0]);
389 idct(decdata
->dcts
+ 64, decdata
->out
+ 64, decdata
->dquant
[0], IFIX(128.5), max
[1]);
390 idct(decdata
->dcts
+ 128, decdata
->out
+ 128, decdata
->dquant
[0], IFIX(128.5), max
[2]);
391 idct(decdata
->dcts
+ 192, decdata
->out
+ 192, decdata
->dquant
[0], IFIX(128.5), max
[3]);
392 idct(decdata
->dcts
+ 256, decdata
->out
+ 256, decdata
->dquant
[1], IFIX(0.5), max
[4]);
393 idct(decdata
->dcts
+ 320, decdata
->out
+ 320, decdata
->dquant
[2], IFIX(0.5), max
[5]);
397 col221111_32(decdata
->out
, pic
+ (my
* 16 * mcusx
+ mx
) * 16 * 4, mcusx
* 16 * 4);
400 col221111(decdata
->out
, pic
+ (my
* 16 * mcusx
+ mx
) * 16 * 3, mcusx
* 16 * 3);
403 col221111_16(decdata
->out
, pic
+ (my
* 16 * mcusx
+ mx
) * (16 * 2), mcusx
* (16 * 2));
406 return ERR_DEPTH_MISMATCH
;
412 m
= dec_readmarker(&glob_in
);
419 /****************************************************************/
420 /************** huffman decoder ***************/
421 /****************************************************************/
423 static int fillbits
__P((struct in
*, int, unsigned int));
425 __P((struct in
*, struct dec_hufftbl
*, int *, int, int));
427 static void setinput(struct in
*in
, unsigned char *p
)
435 static int fillbits(struct in
*in
, int le
, unsigned int bi
)
441 in
->bits
= bi
<< 16, le
+= 16;
446 if (b
== 0xff && (m
= *in
->p
++) != 0) {
448 if (in
->func
&& (m
= in
->func(in
->data
)) == 0)
453 bi
= bi
<< 16, le
+= 16;
459 in
->bits
= bi
; /* tmp... 2 return values needed */
463 static int dec_readmarker(struct in
*in
)
467 in
->left
= fillbits(in
, in
->left
, in
->bits
);
468 if ((m
= in
->marker
) == 0)
475 #define LEBI_DCL int le, bi
476 #define LEBI_GET(in) (le = in->left, bi = in->bits)
477 #define LEBI_PUT(in) (in->left = le, in->bits = bi)
479 #define GETBITS(in, n) ( \
480 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
482 bi >> le & ((1 << (n)) - 1) \
485 #define UNGETBITS(in, n) ( \
490 static int dec_rec2(struct in
*in
, struct dec_hufftbl
*hu
, int *runp
, int c
,
497 UNGETBITS(in
, i
& 127);
501 for (i
= DECBITS
; (c
= ((c
<< 1) | GETBITS(in
, 1))) >= (hu
->maxcode
[i
]); i
++);
503 in
->marker
= M_BADHUFF
;
506 i
= hu
->vals
[hu
->valptr
[i
] + c
- hu
->maxcode
[i
- 1] * 2];
510 if (i
== 0) { /* sigh, 0xf0 is 11 bit */
516 if (c
< (1 << (i
- 1)))
522 #define DEC_REC(in, hu, r, i) ( \
523 r = GETBITS(in, DECBITS), \
527 UNGETBITS(in, i & 127), \
534 i = dec_rec2(in, hu, &r, r, i), \
540 static void decode_mcus(struct in
*in
, int *dct
, int n
, struct scan
*sc
, int *maxp
)
542 struct dec_hufftbl
*hu
;
546 memset(dct
, 0, n
* 64 * sizeof(*dct
));
550 *dct
++ = (sc
->dc
+= DEC_REC(in
, hu
, r
, t
));
555 t
= DEC_REC(in
, hu
, r
, t
);
556 if (t
== 0 && r
== 0) {
571 static void dec_makehuff(struct dec_hufftbl
*hu
, int *hufflen
, unsigned char *huffvals
)
573 int code
, k
, i
, j
, d
, x
, c
, v
;
574 for (i
= 0; i
< (1 << DECBITS
); i
++)
580 * value v already known, run r, backup u bits:
581 * vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
582 * value unknown, size b bits, run r, backup u bits:
583 * 000000000000bbbb 0000 rrrr 0 uuuuuuu
584 * value and size unknown:
585 * 0000000000000000 0000 0000 0 0000000
589 for (i
= 0; i
< 16; i
++, code
<<= 1) { /* sizes */
591 for (j
= 0; j
< hufflen
[i
]; j
++) {
592 hu
->vals
[k
] = *huffvals
++;
594 c
= code
<< (DECBITS
- 1 - i
);
595 v
= hu
->vals
[k
] & 0x0f; /* size */
596 for (d
= 1 << (DECBITS
- 1 - i
); --d
>= 0;) {
597 if (v
+ i
< DECBITS
) { /* both fit in table */
598 x
= d
>> (DECBITS
- 1 - v
-
600 if (v
&& x
< (1 << (v
- 1)))
602 x
= x
<< 16 | (hu
-> vals
[k
] & 0xf0) << 4 |
603 (DECBITS
- (i
+ 1 + v
)) | 128;
605 x
= v
<< 16 | (hu
-> vals
[k
] & 0xf0) << 4 |
607 hu
->llvals
[c
| d
] = x
;
613 hu
->maxcode
[i
] = code
;
615 hu
->maxcode
[16] = 0x20000; /* always terminate decode */
618 /****************************************************************/
619 /************** idct ***************/
620 /****************************************************************/
622 #define ONE ((PREC)IFIX(1.))
623 #define S2 ((PREC)IFIX(0.382683432))
624 #define C2 ((PREC)IFIX(0.923879532))
625 #define C4 ((PREC)IFIX(0.707106781))
627 #define S22 ((PREC)IFIX(2 * 0.382683432))
628 #define C22 ((PREC)IFIX(2 * 0.923879532))
629 #define IC4 ((PREC)IFIX(1 / 0.707106781))
631 #define C3IC1 ((PREC)IFIX(0.847759065)) /* c3/c1 */
632 #define C5IC1 ((PREC)IFIX(0.566454497)) /* c5/c1 */
633 #define C7IC1 ((PREC)IFIX(0.198912367)) /* c7/c1 */
635 #define XPP(a,b) (t = a + b, b = a - b, a = t)
636 #define XMP(a,b) (t = a - b, b = a + b, a = t)
637 #define XPM(a,b) (t = a + b, b = b - a, a = t)
639 #define ROT(a,b,s,c) ( t = IMULT(a + b, s), \
640 a = IMULT(a, c - s) + t, \
641 b = IMULT(b, c + s) - t)
647 t2 = IMULT(t2, IC4) - t3, \
653 t5 = IMULT(t5, IC4), \
654 ROT(t4, t6, S22, C22),\
664 static unsigned char zig2
[64] = {
665 0, 2, 3, 9, 10, 20, 21, 35,
666 14, 16, 25, 31, 39, 46, 50, 57,
667 5, 7, 12, 18, 23, 33, 37, 48,
668 27, 29, 41, 44, 52, 55, 59, 62,
669 15, 26, 30, 40, 45, 51, 56, 58,
670 1, 4, 8, 11, 19, 22, 34, 36,
671 28, 42, 43, 53, 54, 60, 61, 63,
672 6, 13, 17, 24, 32, 38, 47, 49
675 void idct(int *in
, int *out
, PREC
*lquant
, PREC off
, int max
)
677 PREC t0
, t1
, t2
, t3
, t4
, t5
, t6
, t7
, t
;
680 unsigned char *zig2p
;
684 t0
+= in
[0] * lquant
[0];
685 for (i
= 0; i
< 64; i
++)
691 for (i
= 0; i
< 8; i
++) {
693 t0
+= in
[j
] * lquant
[j
];
695 t5
= in
[j
] * lquant
[j
];
697 t2
= in
[j
] * lquant
[j
];
699 t7
= in
[j
] * lquant
[j
];
701 t1
= in
[j
] * lquant
[j
];
703 t4
= in
[j
] * lquant
[j
];
705 t3
= in
[j
] * lquant
[j
];
707 t6
= in
[j
] * lquant
[j
];
720 for (i
= 0; i
< 8; i
++) {
730 out
[8 * i
+ 0] = ITOINT(t0
);
731 out
[8 * i
+ 1] = ITOINT(t1
);
732 out
[8 * i
+ 2] = ITOINT(t2
);
733 out
[8 * i
+ 3] = ITOINT(t3
);
734 out
[8 * i
+ 4] = ITOINT(t4
);
735 out
[8 * i
+ 5] = ITOINT(t5
);
736 out
[8 * i
+ 6] = ITOINT(t6
);
737 out
[8 * i
+ 7] = ITOINT(t7
);
741 static unsigned char zig
[64] = {
742 0, 1, 5, 6, 14, 15, 27, 28,
743 2, 4, 7, 13, 16, 26, 29, 42,
744 3, 8, 12, 17, 25, 30, 41, 43,
745 9, 11, 18, 24, 31, 40, 44, 53,
746 10, 19, 23, 32, 39, 45, 52, 54,
747 20, 22, 33, 38, 46, 51, 55, 60,
748 21, 34, 37, 47, 50, 56, 59, 61,
749 35, 36, 48, 49, 57, 58, 62, 63
752 static PREC aaidct
[8] = {
753 IFIX(0.3535533906), IFIX(0.4903926402),
754 IFIX(0.4619397663), IFIX(0.4157348062),
755 IFIX(0.3535533906), IFIX(0.2777851165),
756 IFIX(0.1913417162), IFIX(0.0975451610)
760 static void idctqtab(unsigned char *qin
, PREC
*qout
)
764 for (i
= 0; i
< 8; i
++)
765 for (j
= 0; j
< 8; j
++)
766 qout
[zig
[i
* 8 + j
]] = qin
[zig
[i
* 8 + j
]] *
767 IMULT(aaidct
[i
], aaidct
[j
]);
770 static void scaleidctqtab(PREC
*q
, PREC sc
)
774 for (i
= 0; i
< 64; i
++)
775 q
[i
] = IMULT(q
[i
], sc
);
778 /****************************************************************/
779 /************** color decoder ***************/
780 /****************************************************************/
785 * YCbCr Color transformation:
787 * y:0..255 Cb:-128..127 Cr:-128..127
789 * R = Y + 1.40200 * Cr
790 * G = Y - 0.34414 * Cb - 0.71414 * Cr
791 * B = Y + 1.77200 * Cb
796 * Cg = 0.19421 * Cb + .50937 * Cr;
802 * Cg = (50 * Cb + 130 * Cr + 128) >> 8;
805 static void initcol(PREC q
[][64])
807 scaleidctqtab(q
[1], IFIX(1.77200));
808 scaleidctqtab(q
[2], IFIX(1.40200));
811 /* This is optimized for the stupid sun SUNWspro compiler. */
812 #define STORECLAMP(a,x) \
815 (unsigned int)(x) >= 256 ? \
816 ((a) = (x) < 0 ? 0 : 255) \
821 #define CLAMP(x) ((unsigned int)(x) >= 256 ? ((x) < 0 ? 0 : 255) : (x))
825 #define CBCRCG(yin, xin) \
827 cb = outc[0 +yin*8+xin], \
828 cr = outc[64+yin*8+xin], \
829 cg = (50 * cb + 130 * cr + 128) >> 8 \
834 #define CBCRCG(yin, xin) \
836 cb = outc[0 +yin*8+xin], \
837 cr = outc[64+yin*8+xin], \
838 cg = (3 * cb + 8 * cr) >> 4 \
843 #define PIC(yin, xin, p, xout) \
845 y = outy[(yin) * 8 + xin], \
846 STORECLAMP(p[(xout) * 3 + 0], y + cr), \
847 STORECLAMP(p[(xout) * 3 + 1], y - cg), \
848 STORECLAMP(p[(xout) * 3 + 2], y + cb) \
851 #ifdef __LITTLE_ENDIAN
852 #define PIC_16(yin, xin, p, xout, add) \
854 y = outy[(yin) * 8 + xin], \
855 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
856 ((CLAMP(y - cg + add ) & 0xfc) << 3) | \
857 ((CLAMP(y + cb + add*2+1) ) >> 3), \
858 p[(xout) * 2 + 0] = y & 0xff, \
859 p[(xout) * 2 + 1] = y >> 8 \
863 #define PIC_16(yin, xin, p, xout, add) \
865 y = outy[(yin) * 8 + xin], \
866 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 7) | \
867 ((CLAMP(y - cg + add*2+1) & 0xf8) << 2) | \
868 ((CLAMP(y + cb + add*2+1) ) >> 3), \
869 p[(xout) * 2 + 0] = y >> 8, \
870 p[(xout) * 2 + 1] = y & 0xff \
873 #define PIC_16(yin, xin, p, xout, add) \
875 y = outy[(yin) * 8 + xin], \
876 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
877 ((CLAMP(y - cg + add ) & 0xfc) << 3) | \
878 ((CLAMP(y + cb + add*2+1) ) >> 3), \
879 p[(xout) * 2 + 0] = y >> 8, \
880 p[(xout) * 2 + 1] = y & 0xff \
885 #define PIC_32(yin, xin, p, xout) \
887 y = outy[(yin) * 8 + xin], \
888 STORECLAMP(p[(xout) * 4 + 0], y + cr), \
889 STORECLAMP(p[(xout) * 4 + 1], y - cg), \
890 STORECLAMP(p[(xout) * 4 + 2], y + cb), \
891 p[(xout) * 4 + 3] = 0 \
894 #define PIC221111(xin) \
897 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
898 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
899 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
900 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
903 #define PIC221111_16(xin) \
906 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0, 3), \
907 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1, 0), \
908 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0, 1), \
909 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1, 2) \
912 #define PIC221111_32(xin) \
915 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0),\
916 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1),\
917 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0),\
918 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
921 static void col221111(int *out
, unsigned char *pic
, int width
)
924 unsigned char *pic0
, *pic1
;
932 for (i
= 2; i
> 0; i
--) {
933 for (j
= 4; j
> 0; j
--) {
934 for (k
= 0; k
< 8; k
++) {
942 outy
+= 64 * 2 - 16 * 4;
946 static void col221111_16(int *out
, unsigned char *pic
, int width
)
949 unsigned char *pic0
, *pic1
;
957 for (i
= 2; i
> 0; i
--) {
958 for (j
= 4; j
> 0; j
--) {
959 for (k
= 0; k
< 8; k
++) {
967 outy
+= 64 * 2 - 16 * 4;
971 static void col221111_32(int *out
, unsigned char *pic
, int width
)
974 unsigned char *pic0
, *pic1
;
982 for (i
= 2; i
> 0; i
--) {
983 for (j
= 4; j
> 0; j
--) {
984 for (k
= 0; k
< 8; k
++) {
992 outy
+= 64 * 2 - 16 * 4;