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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * a tiny jpeg decoder.
23 * written in August 2001 by Michael Schroeder <mls@suse.de>
27 #define __LITTLE_ENDIAN
32 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
33 #define IMULT(a, b) (((a) * (b)) >> ISHIFT)
34 #define ITOINT(a) ((a) >> ISHIFT)
50 int (*func) __P((void *));
54 /*********************************/
59 struct dec_hufftbl *dhuff;
60 struct enc_hufftbl *ehuff;
64 int dc; /* old dc value */
68 int next; /* when to switch to next scan */
70 int cid; /* component id */
71 int hv; /* horiz/vert, copied from comp */
72 int tq; /* quant tbl, copied from comp */
75 /*********************************/
77 #define DECBITS 10 /* seems to be the optimum */
82 unsigned char vals[256];
83 unsigned int llvals[1 << DECBITS];
86 static void decode_mcus __P((struct in *, int *, int, struct scan *, int *));
87 static int dec_readmarker __P((struct in *));
88 static void dec_makehuff __P((struct dec_hufftbl *, int *, unsigned char *));
90 static void setinput __P((struct in *, unsigned char *));
91 /*********************************/
96 static void idctqtab __P((unsigned char *, PREC *));
97 static void idct __P((int *, int *, PREC *, PREC, int));
98 static void scaleidctqtab __P((PREC *, PREC));
100 /*********************************/
102 static void initcol __P((PREC[][64]));
104 static void col221111 __P((int *, unsigned char *, int));
105 static void col221111_16 __P((int *, unsigned char *, int));
106 static void col221111_32 __P((int *, unsigned char *, int));
108 /*********************************/
121 static unsigned char *datap;
123 static int getbyte(void)
128 static int getword(void)
144 int nc; /* number of components */
145 int ns; /* number of scans */
146 int dri; /* restart interval */
147 int nm; /* mcus til next marker */
148 int rm; /* next restart marker */
151 static struct jpginfo info;
152 static struct comp comps[MAXCOMP];
154 static struct scan dscans[MAXCOMP];
156 static unsigned char quant[4][64];
158 static struct dec_hufftbl dhuff[4];
160 #define dec_huffdc (dhuff + 0)
161 #define dec_huffac (dhuff + 2)
163 static struct in glob_in;
165 static int readtables(int till)
167 int m, l, i, j, lq, pq, tq;
171 if (getbyte() != 0xff)
173 if ((m = getbyte()) == till)
190 for (i = 0; i < 64; i++)
191 quant[tq][i] = getbyte();
200 unsigned char huffvals[256];
206 if (tc > 1 || th > 1)
208 for (i = 0; i < 16; i++)
209 hufflen[i] = getbyte();
212 for (i = 0; i < 16; i++) {
213 for (j = 0; j < hufflen[i]; j++)
214 huffvals[k++] = getbyte();
217 dec_makehuff(dhuff + tt, hufflen,
224 info.dri = getword();
237 static void dec_initscans(void)
241 info.nm = info.dri + 1;
243 for (i = 0; i < info.ns; i++)
247 static int dec_checkmarker(void)
251 if (dec_readmarker(&glob_in) != info.rm)
254 info.rm = (info.rm + 1) & ~0x08;
255 for (i = 0; i < info.ns; i++)
260 int jpeg_check_size(unsigned char *buf, int width, int height)
268 if (height != getword() || width != getword())
273 int jpeg_decode(unsigned char *buf, unsigned char *pic,
274 int width, int height, int depth, struct jpeg_decdata *decdata)
276 int i, j, m, tac, tdc;
277 int mcusx, mcusy, mx, my;
280 if (!decdata || !buf || !pic)
283 if (getbyte() != 0xff)
285 if (getbyte() != M_SOI)
287 if (readtables(M_SOF0))
288 return ERR_BAD_TABLES;
293 if (((getword() + 15) & ~15) != height)
294 return ERR_HEIGHT_MISMATCH;
295 if (((getword() + 15) & ~15) != width)
296 return ERR_WIDTH_MISMATCH;
297 if ((height & 15) || (width & 15))
298 return ERR_BAD_WIDTH_OR_HEIGHT;
300 if (info.nc > MAXCOMP)
301 return ERR_TOO_MANY_COMPPS;
302 for (i = 0; i < info.nc; i++) {
304 comps[i].cid = getbyte();
305 comps[i].hv = getbyte();
306 v = comps[i].hv & 15;
307 h = comps[i].hv >> 4;
308 comps[i].tq = getbyte();
310 return ERR_ILLEGAL_HV;
312 return ERR_QUANT_TABLE_SELECTOR;
314 if (readtables(M_SOS))
315 return ERR_BAD_TABLES;
319 return ERR_NOT_YCBCR_221111;
320 for (i = 0; i < 3; i++) {
321 dscans[i].cid = getbyte();
325 if (tdc > 1 || tac > 1)
326 return ERR_QUANT_TABLE_SELECTOR;
327 for (j = 0; j < info.nc; j++)
328 if (comps[j].cid == dscans[i].cid)
331 return ERR_UNKNOWN_CID_IN_SCAN;
332 dscans[i].hv = comps[j].hv;
333 dscans[i].tq = comps[j].tq;
334 dscans[i].hudc.dhuff = dec_huffdc + tdc;
335 dscans[i].huac.dhuff = dec_huffac + tac;
342 if (i != 0 || j != 63 || m != 0)
343 return ERR_NOT_SEQUENTIAL_DCT;
345 if (dscans[0].cid != 1 || dscans[1].cid != 2 || dscans[2].cid != 3)
346 return ERR_NOT_YCBCR_221111;
348 if (dscans[0].hv != 0x22 || dscans[1].hv != 0x11 || dscans[2].hv != 0x11)
349 return ERR_NOT_YCBCR_221111;
355 idctqtab(quant[dscans[0].tq], decdata->dquant[0]);
356 idctqtab(quant[dscans[1].tq], decdata->dquant[1]);
357 idctqtab(quant[dscans[2].tq], decdata->dquant[2]);
358 initcol(decdata->dquant);
359 setinput(&glob_in, datap);
365 img[len + 2] = M_EOF;
370 dscans[0].next = 6 - 4;
371 dscans[1].next = 6 - 4 - 1;
372 dscans[2].next = 6 - 4 - 1 - 1; /* 411 encoding */
373 for (my = 0; my < mcusy; my++) {
374 for (mx = 0; mx < mcusx; mx++) {
375 if (info.dri && !--info.nm)
376 if (dec_checkmarker())
377 return ERR_WRONG_MARKER;
379 decode_mcus(&glob_in, decdata->dcts, 6, dscans, max);
380 idct(decdata->dcts, decdata->out, decdata->dquant[0], IFIX(128.5), max[0]);
381 idct(decdata->dcts + 64, decdata->out + 64, decdata->dquant[0], IFIX(128.5), max[1]);
382 idct(decdata->dcts + 128, decdata->out + 128, decdata->dquant[0], IFIX(128.5), max[2]);
383 idct(decdata->dcts + 192, decdata->out + 192, decdata->dquant[0], IFIX(128.5), max[3]);
384 idct(decdata->dcts + 256, decdata->out + 256, decdata->dquant[1], IFIX(0.5), max[4]);
385 idct(decdata->dcts + 320, decdata->out + 320, decdata->dquant[2], IFIX(0.5), max[5]);
389 col221111_32(decdata->out, pic + (my * 16 * mcusx + mx) * 16 * 4, mcusx * 16 * 4);
392 col221111(decdata->out, pic + (my * 16 * mcusx + mx) * 16 * 3, mcusx * 16 * 3);
395 col221111_16(decdata->out, pic + (my * 16 * mcusx + mx) * (16 * 2), mcusx * (16 * 2));
398 return ERR_DEPTH_MISMATCH;
404 m = dec_readmarker(&glob_in);
411 /****************************************************************/
412 /************** huffman decoder ***************/
413 /****************************************************************/
415 static int fillbits __P((struct in *, int, unsigned int));
417 __P((struct in *, struct dec_hufftbl *, int *, int, int));
419 static void setinput(struct in *in, unsigned char *p)
427 static int fillbits(struct in *in, int le, unsigned int bi)
433 in->bits = bi << 16, le += 16;
438 if (b == 0xff && (m = *in->p++) != 0) {
440 if (in->func && (m = in->func(in->data)) == 0)
445 bi = bi << 16, le += 16;
451 in->bits = bi; /* tmp... 2 return values needed */
455 static int dec_readmarker(struct in *in)
459 in->left = fillbits(in, in->left, in->bits);
460 if ((m = in->marker) == 0)
467 #define LEBI_DCL int le, bi
468 #define LEBI_GET(in) (le = in->left, bi = in->bits)
469 #define LEBI_PUT(in) (in->left = le, in->bits = bi)
471 #define GETBITS(in, n) ( \
472 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
474 bi >> le & ((1 << (n)) - 1) \
477 #define UNGETBITS(in, n) ( \
482 static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp, int c,
489 UNGETBITS(in, i & 127);
493 for (i = DECBITS; (c = ((c << 1) | GETBITS(in, 1))) >= (hu->maxcode[i]); i++);
495 in->marker = M_BADHUFF;
498 i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
502 if (i == 0) { /* sigh, 0xf0 is 11 bit */
508 if (c < (1 << (i - 1)))
514 #define DEC_REC(in, hu, r, i) ( \
515 r = GETBITS(in, DECBITS), \
519 UNGETBITS(in, i & 127), \
526 i = dec_rec2(in, hu, &r, r, i), \
532 static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc, int *maxp)
534 struct dec_hufftbl *hu;
538 memset(dct, 0, n * 64 * sizeof(*dct));
542 *dct++ = (sc->dc += DEC_REC(in, hu, r, t));
547 t = DEC_REC(in, hu, r, t);
548 if (t == 0 && r == 0) {
563 static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen, unsigned char *huffvals)
565 int code, k, i, j, d, x, c, v;
566 for (i = 0; i < (1 << DECBITS); i++)
572 * value v already known, run r, backup u bits:
573 * vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
574 * value unknown, size b bits, run r, backup u bits:
575 * 000000000000bbbb 0000 rrrr 0 uuuuuuu
576 * value and size unknown:
577 * 0000000000000000 0000 0000 0 0000000
581 for (i = 0; i < 16; i++, code <<= 1) { /* sizes */
583 for (j = 0; j < hufflen[i]; j++) {
584 hu->vals[k] = *huffvals++;
586 c = code << (DECBITS - 1 - i);
587 v = hu->vals[k] & 0x0f; /* size */
588 for (d = 1 << (DECBITS - 1 - i); --d >= 0;) {
589 if (v + i < DECBITS) { /* both fit in table */
590 x = d >> (DECBITS - 1 - v -
592 if (v && x < (1 << (v - 1)))
594 x = x << 16 | (hu-> vals[k] & 0xf0) << 4 |
595 (DECBITS - (i + 1 + v)) | 128;
597 x = v << 16 | (hu-> vals[k] & 0xf0) << 4 |
599 hu->llvals[c | d] = x;
605 hu->maxcode[i] = code;
607 hu->maxcode[16] = 0x20000; /* always terminate decode */
610 /****************************************************************/
611 /************** idct ***************/
612 /****************************************************************/
614 #define ONE ((PREC)IFIX(1.))
615 #define S2 ((PREC)IFIX(0.382683432))
616 #define C2 ((PREC)IFIX(0.923879532))
617 #define C4 ((PREC)IFIX(0.707106781))
619 #define S22 ((PREC)IFIX(2 * 0.382683432))
620 #define C22 ((PREC)IFIX(2 * 0.923879532))
621 #define IC4 ((PREC)IFIX(1 / 0.707106781))
623 #define C3IC1 ((PREC)IFIX(0.847759065)) /* c3/c1 */
624 #define C5IC1 ((PREC)IFIX(0.566454497)) /* c5/c1 */
625 #define C7IC1 ((PREC)IFIX(0.198912367)) /* c7/c1 */
627 #define XPP(a,b) (t = a + b, b = a - b, a = t)
628 #define XMP(a,b) (t = a - b, b = a + b, a = t)
629 #define XPM(a,b) (t = a + b, b = b - a, a = t)
631 #define ROT(a,b,s,c) ( t = IMULT(a + b, s), \
632 a = IMULT(a, c - s) + t, \
633 b = IMULT(b, c + s) - t)
639 t2 = IMULT(t2, IC4) - t3, \
645 t5 = IMULT(t5, IC4), \
646 ROT(t4, t6, S22, C22),\
656 static unsigned char zig2[64] = {
657 0, 2, 3, 9, 10, 20, 21, 35,
658 14, 16, 25, 31, 39, 46, 50, 57,
659 5, 7, 12, 18, 23, 33, 37, 48,
660 27, 29, 41, 44, 52, 55, 59, 62,
661 15, 26, 30, 40, 45, 51, 56, 58,
662 1, 4, 8, 11, 19, 22, 34, 36,
663 28, 42, 43, 53, 54, 60, 61, 63,
664 6, 13, 17, 24, 32, 38, 47, 49
667 void idct(int *in, int *out, PREC *lquant, PREC off, int max)
669 PREC t0, t1, t2, t3, t4, t5, t6, t7, t;
672 unsigned char *zig2p;
676 t0 += in[0] * lquant[0];
677 for (i = 0; i < 64; i++)
683 for (i = 0; i < 8; i++) {
685 t0 += in[j] * lquant[j];
687 t5 = in[j] * lquant[j];
689 t2 = in[j] * lquant[j];
691 t7 = in[j] * lquant[j];
693 t1 = in[j] * lquant[j];
695 t4 = in[j] * lquant[j];
697 t3 = in[j] * lquant[j];
699 t6 = in[j] * lquant[j];
712 for (i = 0; i < 8; i++) {
722 out[8 * i + 0] = ITOINT(t0);
723 out[8 * i + 1] = ITOINT(t1);
724 out[8 * i + 2] = ITOINT(t2);
725 out[8 * i + 3] = ITOINT(t3);
726 out[8 * i + 4] = ITOINT(t4);
727 out[8 * i + 5] = ITOINT(t5);
728 out[8 * i + 6] = ITOINT(t6);
729 out[8 * i + 7] = ITOINT(t7);
733 static unsigned char zig[64] = {
734 0, 1, 5, 6, 14, 15, 27, 28,
735 2, 4, 7, 13, 16, 26, 29, 42,
736 3, 8, 12, 17, 25, 30, 41, 43,
737 9, 11, 18, 24, 31, 40, 44, 53,
738 10, 19, 23, 32, 39, 45, 52, 54,
739 20, 22, 33, 38, 46, 51, 55, 60,
740 21, 34, 37, 47, 50, 56, 59, 61,
741 35, 36, 48, 49, 57, 58, 62, 63
744 static PREC aaidct[8] = {
745 IFIX(0.3535533906), IFIX(0.4903926402),
746 IFIX(0.4619397663), IFIX(0.4157348062),
747 IFIX(0.3535533906), IFIX(0.2777851165),
748 IFIX(0.1913417162), IFIX(0.0975451610)
752 static void idctqtab(unsigned char *qin, PREC *qout)
756 for (i = 0; i < 8; i++)
757 for (j = 0; j < 8; j++)
758 qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] *
759 IMULT(aaidct[i], aaidct[j]);
762 static void scaleidctqtab(PREC *q, PREC sc)
766 for (i = 0; i < 64; i++)
767 q[i] = IMULT(q[i], sc);
770 /****************************************************************/
771 /************** color decoder ***************/
772 /****************************************************************/
777 * YCbCr Color transformation:
779 * y:0..255 Cb:-128..127 Cr:-128..127
781 * R = Y + 1.40200 * Cr
782 * G = Y - 0.34414 * Cb - 0.71414 * Cr
783 * B = Y + 1.77200 * Cb
788 * Cg = 0.19421 * Cb + .50937 * Cr;
794 * Cg = (50 * Cb + 130 * Cr + 128) >> 8;
797 static void initcol(PREC q[][64])
799 scaleidctqtab(q[1], IFIX(1.77200));
800 scaleidctqtab(q[2], IFIX(1.40200));
803 /* This is optimized for the stupid sun SUNWspro compiler. */
804 #define STORECLAMP(a,x) \
807 (unsigned int)(x) >= 256 ? \
808 ((a) = (x) < 0 ? 0 : 255) \
813 #define CLAMP(x) ((unsigned int)(x) >= 256 ? ((x) < 0 ? 0 : 255) : (x))
817 #define CBCRCG(yin, xin) \
819 cb = outc[0 +yin*8+xin], \
820 cr = outc[64+yin*8+xin], \
821 cg = (50 * cb + 130 * cr + 128) >> 8 \
826 #define CBCRCG(yin, xin) \
828 cb = outc[0 +yin*8+xin], \
829 cr = outc[64+yin*8+xin], \
830 cg = (3 * cb + 8 * cr) >> 4 \
835 #define PIC(yin, xin, p, xout) \
837 y = outy[(yin) * 8 + xin], \
838 STORECLAMP(p[(xout) * 3 + 0], y + cr), \
839 STORECLAMP(p[(xout) * 3 + 1], y - cg), \
840 STORECLAMP(p[(xout) * 3 + 2], y + cb) \
843 #ifdef __LITTLE_ENDIAN
844 #define PIC_16(yin, xin, p, xout, add) \
846 y = outy[(yin) * 8 + xin], \
847 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
848 ((CLAMP(y - cg + add ) & 0xfc) << 3) | \
849 ((CLAMP(y + cb + add*2+1) ) >> 3), \
850 p[(xout) * 2 + 0] = y & 0xff, \
851 p[(xout) * 2 + 1] = y >> 8 \
855 #define PIC_16(yin, xin, p, xout, add) \
857 y = outy[(yin) * 8 + xin], \
858 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 7) | \
859 ((CLAMP(y - cg + add*2+1) & 0xf8) << 2) | \
860 ((CLAMP(y + cb + add*2+1) ) >> 3), \
861 p[(xout) * 2 + 0] = y >> 8, \
862 p[(xout) * 2 + 1] = y & 0xff \
865 #define PIC_16(yin, xin, p, xout, add) \
867 y = outy[(yin) * 8 + xin], \
868 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
869 ((CLAMP(y - cg + add ) & 0xfc) << 3) | \
870 ((CLAMP(y + cb + add*2+1) ) >> 3), \
871 p[(xout) * 2 + 0] = y >> 8, \
872 p[(xout) * 2 + 1] = y & 0xff \
877 #define PIC_32(yin, xin, p, xout) \
879 y = outy[(yin) * 8 + xin], \
880 STORECLAMP(p[(xout) * 4 + 0], y + cr), \
881 STORECLAMP(p[(xout) * 4 + 1], y - cg), \
882 STORECLAMP(p[(xout) * 4 + 2], y + cb), \
883 p[(xout) * 4 + 3] = 0 \
886 #define PIC221111(xin) \
889 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
890 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
891 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
892 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
895 #define PIC221111_16(xin) \
898 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0, 3), \
899 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1, 0), \
900 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0, 1), \
901 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1, 2) \
904 #define PIC221111_32(xin) \
907 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0),\
908 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1),\
909 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0),\
910 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
913 static void col221111(int *out, unsigned char *pic, int width)
916 unsigned char *pic0, *pic1;
924 for (i = 2; i > 0; i--) {
925 for (j = 4; j > 0; j--) {
926 for (k = 0; k < 8; k++) {
934 outy += 64 * 2 - 16 * 4;
938 static void col221111_16(int *out, unsigned char *pic, int width)
941 unsigned char *pic0, *pic1;
949 for (i = 2; i > 0; i--) {
950 for (j = 4; j > 0; j--) {
951 for (k = 0; k < 8; k++) {
959 outy += 64 * 2 - 16 * 4;
963 static void col221111_32(int *out, unsigned char *pic, int width)
966 unsigned char *pic0, *pic1;
974 for (i = 2; i > 0; i--) {
975 for (j = 4; j > 0; j--) {
976 for (k = 0; k < 8; k++) {
984 outy += 64 * 2 - 16 * 4;