Initial bootsplash support.
[seabios.git] / src / jpeg.c
1 /*
2  * Copyright (C) 2001, Novell Inc.
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions 
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of Novell nor the names of the contributors may 
17  *    be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  * 
32  */
33
34 /*
35  * a tiny jpeg decoder.
36  *
37  * written in August 2001 by Michael Schroeder <mls@suse.de>
38  *
39  */
40
41 #define __LITTLE_ENDIAN
42 #include "util.h"
43 #include "jpeg.h"
44 #define ISHIFT 11
45
46 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
47 #define IMULT(a, b) (((a) * (b)) >> ISHIFT)
48 #define ITOINT(a) ((a) >> ISHIFT)
49
50 #ifndef __P
51 # define __P(x) x
52 #endif
53
54 /* special markers */
55 #define M_BADHUFF        -1
56 #define M_EOF                0x80
57
58 struct in {
59     unsigned char *p;
60     unsigned int bits;
61     int left;
62     int marker;
63
64     int (*func) __P((void *));
65     void *data;
66 };
67
68 /*********************************/
69 struct dec_hufftbl;
70 struct enc_hufftbl;
71
72 union hufftblp {
73     struct dec_hufftbl *dhuff;
74     struct enc_hufftbl *ehuff;
75 };
76
77 struct scan {
78     int dc;               /* old dc value */
79
80     union hufftblp hudc;
81     union hufftblp huac;
82     int next;             /* when to switch to next scan */
83
84     int cid;              /* component id */
85     int hv;               /* horiz/vert, copied from comp */
86     int tq;               /* quant tbl, copied from comp */
87 };
88
89 /*********************************/
90
91 #define DECBITS 10        /* seems to be the optimum */
92
93 struct dec_hufftbl {
94     int maxcode[17];
95     int valptr[16];
96     unsigned char vals[256];
97     unsigned int llvals[1 << DECBITS];
98 };
99
100 static void decode_mcus
101 __P((struct in *, int *, int, struct scan *, int *));
102 static int dec_readmarker __P((struct in *));
103 static void dec_makehuff
104 __P((struct dec_hufftbl *, int *, unsigned char *));
105
106 static void setinput __P((struct in *, unsigned char *));
107 /*********************************/
108
109 #undef PREC
110 #define PREC int
111
112 static void idctqtab __P((unsigned char *, PREC *));
113 static void idct __P((int *, int *, PREC *, PREC, int));
114 static void scaleidctqtab __P((PREC *, PREC));
115
116 /*********************************/
117
118 static void initcol __P((PREC[][64]));
119
120 static void col221111 __P((int *, unsigned char *, int));
121 static void col221111_16 __P((int *, unsigned char *, int));
122 static void col221111_32 __P((int *, unsigned char *, int));
123
124 /*********************************/
125
126 #define M_SOI   0xd8
127 #define M_APP0  0xe0
128 #define M_DQT   0xdb
129 #define M_SOF0  0xc0
130 #define M_DHT   0xc4
131 #define M_DRI   0xdd
132 #define M_SOS   0xda
133 #define M_RST0  0xd0
134 #define M_EOI   0xd9
135 #define M_COM   0xfe
136
137 static unsigned char *datap;
138
139 static int getbyte(void)
140 {
141     return *datap++;
142 }
143
144 static int getword(void)
145 {
146     int c1, c2;
147     c1 = *datap++;
148     c2 = *datap++;
149     return c1 << 8 | c2;
150 }
151
152 struct comp {
153     int cid;
154     int hv;
155     int tq;
156 };
157
158 #define MAXCOMP 4
159 struct jpginfo {
160     int nc;   /* number of components */
161     int ns;   /* number of scans */
162     int dri;  /* restart interval */
163     int nm;   /* mcus til next marker */
164     int rm;   /* next restart marker */
165 };
166
167 static struct jpginfo info;
168 static struct comp comps[MAXCOMP];
169
170 static struct scan dscans[MAXCOMP];
171
172 static unsigned char quant[4][64];
173
174 static struct dec_hufftbl dhuff[4];
175
176 #define dec_huffdc (dhuff + 0)
177 #define dec_huffac (dhuff + 2)
178
179 static struct in in;
180
181 static int readtables(int till)
182 {
183     int m, l, i, j, lq, pq, tq;
184     int tc, th, tt;
185
186     for (;;) {
187         if (getbyte() != 0xff)
188             return -1;
189         if ((m = getbyte()) == till)
190             break;
191
192         switch (m) {
193         case 0xc2:
194             return 0;
195
196         case M_DQT:
197             lq = getword();
198             while (lq > 2) {
199                 pq = getbyte();
200                 tq = pq & 15;
201                 if (tq > 3)
202                     return -1;
203                 pq >>= 4;
204                 if (pq != 0)
205                     return -1;
206                 for (i = 0; i < 64; i++)
207                     quant[tq][i] = getbyte();
208                 lq -= 64 + 1;
209             }
210             break;
211
212         case M_DHT:
213             l = getword();
214             while (l > 2) {
215                 int hufflen[16], k;
216                 unsigned char huffvals[256];
217
218                 tc = getbyte();
219                 th = tc & 15;
220                 tc >>= 4;
221                 tt = tc * 2 + th;
222                 if (tc > 1 || th > 1)
223                     return -1;
224                 for (i = 0; i < 16; i++)
225                     hufflen[i] = getbyte();
226                 l -= 1 + 16;
227                 k = 0;
228                 for (i = 0; i < 16; i++) {
229                     for (j = 0; j < hufflen[i]; j++)
230                         huffvals[k++] = getbyte();
231                     l -= hufflen[i];
232                 }
233                 dec_makehuff(dhuff + tt, hufflen, huffvals);
234             }
235             break;
236
237         case M_DRI:
238             l = getword();
239             info.dri = getword();
240             break;
241
242         default:
243             l = getword();
244             while (l-- > 2)
245                 getbyte();
246             break;
247         }
248     }
249     return 0;
250 }
251
252 static void dec_initscans(void)
253 {
254     int i;
255
256     info.nm = info.dri + 1;
257     info.rm = M_RST0;
258     for (i = 0; i < info.ns; i++)
259         dscans[i].dc = 0;
260 }
261
262 static int dec_checkmarker(void)
263 {
264     int i;
265
266     if (dec_readmarker(&in) != info.rm)
267         return -1;
268     info.nm = info.dri;
269     info.rm = (info.rm + 1) & ~0x08;
270     for (i = 0; i < info.ns; i++)
271         dscans[i].dc = 0;
272     return 0;
273 }
274
275 int jpeg_check_size(unsigned char *buf, int width, int height)
276 {
277     datap = buf;
278     getbyte();
279     getbyte();
280     readtables(M_SOF0);
281     getword();
282     getbyte();
283     if (height != getword() || width != getword())
284         return 0;
285     return 1;
286 }
287
288 int jpeg_decode(unsigned char *buf, unsigned char *pic,
289                 int width, int height, int depth,
290                 struct jpeg_decdata *decdata)
291 {
292     int i, j, m, tac, tdc;
293     int mcusx, mcusy, mx, my;
294     int max[6];
295
296     if (!decdata || !buf || !pic)
297         return -1;
298     datap = buf;
299     if (getbyte() != 0xff)
300         return ERR_NO_SOI;
301     if (getbyte() != M_SOI)
302         return ERR_NO_SOI;
303     if (readtables(M_SOF0))
304         return ERR_BAD_TABLES;
305     getword();
306     i = getbyte();
307     if (i != 8)
308         return ERR_NOT_8BIT;
309     if (((getword() + 15) & ~15) != height)
310         return ERR_HEIGHT_MISMATCH;
311     if (((getword() + 15) & ~15) != width)
312         return ERR_WIDTH_MISMATCH;
313     if ((height & 15) || (width & 15))
314         return ERR_BAD_WIDTH_OR_HEIGHT;
315     info.nc = getbyte();
316     if (info.nc > MAXCOMP)
317         return ERR_TOO_MANY_COMPPS;
318     for (i = 0; i < info.nc; i++) {
319         int h, v;
320         comps[i].cid = getbyte();
321         comps[i].hv = getbyte();
322         v = comps[i].hv & 15;
323         h = comps[i].hv >> 4;
324         comps[i].tq = getbyte();
325         if (h > 3 || v > 3)
326             return ERR_ILLEGAL_HV;
327         if (comps[i].tq > 3)
328             return ERR_QUANT_TABLE_SELECTOR;
329     }
330     if (readtables(M_SOS))
331         return ERR_BAD_TABLES;
332     getword();
333     info.ns = getbyte();
334     if (info.ns != 3)
335         return ERR_NOT_YCBCR_221111;
336     for (i = 0; i < 3; i++) {
337         dscans[i].cid = getbyte();
338         tdc = getbyte();
339         tac = tdc & 15;
340         tdc >>= 4;
341         if (tdc > 1 || tac > 1)
342             return ERR_QUANT_TABLE_SELECTOR;
343         for (j = 0; j < info.nc; j++)
344             if (comps[j].cid == dscans[i].cid)
345                 break;
346         if (j == info.nc)
347             return ERR_UNKNOWN_CID_IN_SCAN;
348         dscans[i].hv = comps[j].hv;
349         dscans[i].tq = comps[j].tq;
350         dscans[i].hudc.dhuff = dec_huffdc + tdc;
351         dscans[i].huac.dhuff = dec_huffac + tac;
352     }
353
354     i = getbyte();
355     j = getbyte();
356     m = getbyte();
357
358     if (i != 0 || j != 63 || m != 0)
359         return ERR_NOT_SEQUENTIAL_DCT;
360
361     if (dscans[0].cid != 1 || dscans[1].cid != 2 || dscans[2].cid != 3)
362         return ERR_NOT_YCBCR_221111;
363
364     if (dscans[0].hv != 0x22 || dscans[1].hv != 0x11
365         || dscans[2].hv != 0x11)
366         return ERR_NOT_YCBCR_221111;
367
368     mcusx = width >> 4;
369     mcusy = height >> 4;
370
371
372     idctqtab(quant[dscans[0].tq], decdata->dquant[0]);
373     idctqtab(quant[dscans[1].tq], decdata->dquant[1]);
374     idctqtab(quant[dscans[2].tq], decdata->dquant[2]);
375     initcol(decdata->dquant);
376     setinput(&in, datap);
377
378 #if 0
379     /* landing zone */
380     img[len] = 0;
381     img[len + 1] = 0xff;
382     img[len + 2] = M_EOF;
383 #endif
384
385     dec_initscans();
386
387     dscans[0].next = 6 - 4;
388     dscans[1].next = 6 - 4 - 1;
389     dscans[2].next = 6 - 4 - 1 - 1;        /* 411 encoding */
390     for (my = 0; my < mcusy; my++) {
391         for (mx = 0; mx < mcusx; mx++) {
392             if (info.dri && !--info.nm)
393                 if (dec_checkmarker())
394                     return ERR_WRONG_MARKER;
395
396             decode_mcus(&in, decdata->dcts, 6, dscans, max);
397             idct(decdata->dcts, decdata->out, decdata->dquant[0],
398                  IFIX(128.5), max[0]);
399             idct(decdata->dcts + 64, decdata->out + 64, decdata->dquant[0],
400                  IFIX(128.5), max[1]);
401             idct(decdata->dcts + 128, decdata->out + 128,
402                  decdata->dquant[0], IFIX(128.5), max[2]);
403             idct(decdata->dcts + 192, decdata->out + 192,
404                  decdata->dquant[0], IFIX(128.5), max[3]);
405             idct(decdata->dcts + 256, decdata->out + 256,
406                  decdata->dquant[1], IFIX(0.5), max[4]);
407             idct(decdata->dcts + 320, decdata->out + 320,
408                  decdata->dquant[2], IFIX(0.5), max[5]);
409
410             switch (depth) {
411             case 32:
412                 col221111_32(decdata->out,
413                              pic + (my * 16 * mcusx + mx) * 16 * 4,
414                              mcusx * 16 * 4);
415                 break;
416             case 24:
417                 col221111(decdata->out,
418                           pic + (my * 16 * mcusx + mx) * 16 * 3,
419                           mcusx * 16 * 3);
420                 break;
421             case 16:
422                 col221111_16(decdata->out,
423                              pic + (my * 16 * mcusx + mx) * (16 * 2),
424                              mcusx * (16 * 2));
425                 break;
426             default:
427                 return ERR_DEPTH_MISMATCH;
428                 break;
429             }
430         }
431     }
432
433     m = dec_readmarker(&in);
434     if (m != M_EOI)
435         return ERR_NO_EOI;
436
437     return 0;
438 }
439
440 /****************************************************************/
441 /**************       huffman decoder             ***************/
442 /****************************************************************/
443
444 static int fillbits __P((struct in *, int, unsigned int));
445 static int dec_rec2
446 __P((struct in *, struct dec_hufftbl *, int *, int, int));
447
448 static void setinput(struct in *in, unsigned char *p)
449 {
450     in->p = p;
451     in->left = 0;
452     in->bits = 0;
453     in->marker = 0;
454 }
455
456 static int fillbits(struct in *in, int le, unsigned int bi)
457 {
458     int b, m;
459
460     if (in->marker) {
461         if (le <= 16)
462             in->bits = bi << 16, le += 16;
463         return le;
464     }
465     while (le <= 24) {
466         b = *in->p++;
467         if (b == 0xff && (m = *in->p++) != 0) {
468             if (m == M_EOF) {
469                 if (in->func && (m = in->func(in->data)) == 0)
470                     continue;
471             }
472             in->marker = m;
473             if (le <= 16)
474                 bi = bi << 16, le += 16;
475             break;
476         }
477         bi = bi << 8 | b;
478         le += 8;
479     }
480     in->bits = bi;                /* tmp... 2 return values needed */
481     return le;
482 }
483
484 static int dec_readmarker(struct in *in)
485 {
486     int m;
487
488     in->left = fillbits(in, in->left, in->bits);
489     if ((m = in->marker) == 0)
490         return 0;
491     in->left = 0;
492     in->marker = 0;
493     return m;
494 }
495
496 #define LEBI_DCL       int le, bi
497 #define LEBI_GET(in)   (le = in->left, bi = in->bits)
498 #define LEBI_PUT(in)   (in->left = le, in->bits = bi)
499
500 #define GETBITS(in, n) (                                     \
501   (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
502   (le -= (n)),                                               \
503   bi >> le & ((1 << (n)) - 1)                                \
504 )
505
506 #define UNGETBITS(in, n) ( \
507   le += (n)                \
508 )
509
510
511 static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp,
512                     int c, int i)
513 {
514     LEBI_DCL;
515
516     LEBI_GET(in);
517     if (i) {
518         UNGETBITS(in, i & 127);
519         *runp = i >> 8 & 15;
520         i >>= 16;
521     } else {
522         for (i = DECBITS;
523              (c = ((c << 1) | GETBITS(in, 1))) >= (hu->maxcode[i]); i++);
524         if (i >= 16) {
525             in->marker = M_BADHUFF;
526             return 0;
527         }
528         i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
529         *runp = i >> 4;
530         i &= 15;
531     }
532     if (i == 0) {                /* sigh, 0xf0 is 11 bit */
533         LEBI_PUT(in);
534         return 0;
535     }
536     /* receive part */
537     c = GETBITS(in, i);
538     if (c < (1 << (i - 1)))
539         c += (-1 << i) + 1;
540     LEBI_PUT(in);
541     return c;
542 }
543
544 #define DEC_REC(in, hu, r, i)         (  \
545   r = GETBITS(in, DECBITS),              \
546   i = hu->llvals[r],                     \
547   i & 128 ?                              \
548     (                                    \
549       UNGETBITS(in, i & 127),            \
550       r = i >> 8 & 15,                   \
551       i >> 16                            \
552     )                                    \
553   :                                      \
554     (                                    \
555       LEBI_PUT(in),                      \
556       i = dec_rec2(in, hu, &r, r, i),    \
557       LEBI_GET(in),                      \
558       i                                  \
559     )                                    \
560 )
561
562 static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc,
563                         int *maxp)
564 {
565     struct dec_hufftbl *hu;
566     int i, r, t;
567     LEBI_DCL;
568
569     memset(dct, 0, n * 64 * sizeof(*dct));
570     LEBI_GET(in);
571     while (n-- > 0) {
572         hu = sc->hudc.dhuff;
573         *dct++ = (sc->dc += DEC_REC(in, hu, r, t));
574
575         hu = sc->huac.dhuff;
576         i = 63;
577         while (i > 0) {
578             t = DEC_REC(in, hu, r, t);
579             if (t == 0 && r == 0) {
580                 dct += i;
581                 break;
582             }
583             dct += r;
584             *dct++ = t;
585             i -= r + 1;
586         }
587         *maxp++ = 64 - i;
588         if (n == sc->next)
589             sc++;
590     }
591     LEBI_PUT(in);
592 }
593
594 static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen,
595                          unsigned char *huffvals)
596 {
597     int code, k, i, j, d, x, c, v;
598     for (i = 0; i < (1 << DECBITS); i++)
599         hu->llvals[i] = 0;
600
601     /*
602      * llvals layout:
603      *
604      * value v already known, run r, backup u bits:
605      *  vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
606      * value unknown, size b bits, run r, backup u bits:
607      *  000000000000bbbb 0000 rrrr 0 uuuuuuu
608      * value and size unknown:
609      *  0000000000000000 0000 0000 0 0000000
610      */
611
612     code = 0;
613     k = 0;
614     for (i = 0; i < 16; i++, code <<= 1) {        /* sizes */
615         hu->valptr[i] = k;
616         for (j = 0; j < hufflen[i]; j++) {
617             hu->vals[k] = *huffvals++;
618             if (i < DECBITS) {
619                 c = code << (DECBITS - 1 - i);
620                 v = hu->vals[k] & 0x0f;        /* size */
621                 for (d = 1 << (DECBITS - 1 - i); --d >= 0;) {
622                     if (v + i < DECBITS) {        /* both fit in table */
623                         x = d >> (DECBITS - 1 - v - i);
624                         if (v && x < (1 << (v - 1)))
625                             x += (-1 << v) + 1;
626                         x = x << 16 | (hu->vals[k] & 0xf0) << 4 |
627                             (DECBITS - (i + 1 + v)) | 128;
628                     } else
629                         x = v << 16 | (hu->vals[k] & 0xf0) << 4 |
630                             (DECBITS - (i + 1));
631                     hu->llvals[c | d] = x;
632                 }
633             }
634             code++;
635             k++;
636         }
637         hu->maxcode[i] = code;
638     }
639     hu->maxcode[16] = 0x20000;        /* always terminate decode */
640 }
641
642 /****************************************************************/
643 /**************             idct                  ***************/
644 /****************************************************************/
645
646 #define ONE ((PREC)IFIX(1.))
647 #define S2  ((PREC)IFIX(0.382683432))
648 #define C2  ((PREC)IFIX(0.923879532))
649 #define C4  ((PREC)IFIX(0.707106781))
650
651 #define S22 ((PREC)IFIX(2 * 0.382683432))
652 #define C22 ((PREC)IFIX(2 * 0.923879532))
653 #define IC4 ((PREC)IFIX(1 / 0.707106781))
654
655 #define C3IC1 ((PREC)IFIX(0.847759065))        /* c3/c1 */
656 #define C5IC1 ((PREC)IFIX(0.566454497))        /* c5/c1 */
657 #define C7IC1 ((PREC)IFIX(0.198912367))        /* c7/c1 */
658
659 #define XPP(a,b) (t = a + b, b = a - b, a = t)
660 #define XMP(a,b) (t = a - b, b = a + b, a = t)
661 #define XPM(a,b) (t = a + b, b = b - a, a = t)
662
663 #define ROT(a,b,s,c) (  t = IMULT(a + b, s),      \
664                         a = IMULT(a, c - s) + t,  \
665                         b = IMULT(b, c + s) - t)
666
667 #define IDCT                \
668 (                           \
669   XPP(t0, t1),              \
670   XMP(t2, t3),              \
671   t2 = IMULT(t2, IC4) - t3, \
672   XPP(t0, t3),              \
673   XPP(t1, t2),              \
674   XMP(t4, t7),              \
675   XPP(t5, t6),              \
676   XMP(t5, t7),              \
677   t5 = IMULT(t5, IC4),      \
678   ROT(t4, t6, S22, C22),    \
679   t6 -= t7,                 \
680   t5 -= t6,                 \
681   t4 -= t5,                 \
682   XPP(t0, t7),              \
683   XPP(t1, t6),              \
684   XPP(t2, t5),              \
685   XPP(t3, t4)               \
686 )
687
688 static unsigned char zig2[64] = {
689      0,  2,  3,  9, 10, 20, 21, 35,
690     14, 16, 25, 31, 39, 46, 50, 57,
691      5,  7, 12, 18, 23, 33, 37, 48,
692     27, 29, 41, 44, 52, 55, 59, 62,
693     15, 26, 30, 40, 45, 51, 56, 58,
694      1,  4,  8, 11, 19, 22, 34, 36,
695     28, 42, 43, 53, 54, 60, 61, 63,
696      6, 13, 17, 24, 32, 38, 47, 49
697 };
698
699 void idct(int *in, int *out, PREC * quant, PREC off, int max)
700 {
701     PREC t0, t1, t2, t3, t4, t5, t6, t7, t;
702     PREC tmp[64], *tmpp;
703     int i, j;
704     unsigned char *zig2p;
705
706     t0 = off;
707     if (max == 1) {
708         t0 += in[0] * quant[0];
709         for (i = 0; i < 64; i++)
710             out[i] = ITOINT(t0);
711         return;
712     }
713     zig2p = zig2;
714     tmpp = tmp;
715     for (i = 0; i < 8; i++) {
716         j = *zig2p++;
717         t0 += in[j] * quant[j];
718         j = *zig2p++;
719         t5 = in[j] * quant[j];
720         j = *zig2p++;
721         t2 = in[j] * quant[j];
722         j = *zig2p++;
723         t7 = in[j] * quant[j];
724         j = *zig2p++;
725         t1 = in[j] * quant[j];
726         j = *zig2p++;
727         t4 = in[j] * quant[j];
728         j = *zig2p++;
729         t3 = in[j] * quant[j];
730         j = *zig2p++;
731         t6 = in[j] * quant[j];
732         IDCT;
733         tmpp[0 * 8] = t0;
734         tmpp[1 * 8] = t1;
735         tmpp[2 * 8] = t2;
736         tmpp[3 * 8] = t3;
737         tmpp[4 * 8] = t4;
738         tmpp[5 * 8] = t5;
739         tmpp[6 * 8] = t6;
740         tmpp[7 * 8] = t7;
741         tmpp++;
742         t0 = 0;
743     }
744     for (i = 0; i < 8; i++) {
745         t0 = tmp[8 * i + 0];
746         t1 = tmp[8 * i + 1];
747         t2 = tmp[8 * i + 2];
748         t3 = tmp[8 * i + 3];
749         t4 = tmp[8 * i + 4];
750         t5 = tmp[8 * i + 5];
751         t6 = tmp[8 * i + 6];
752         t7 = tmp[8 * i + 7];
753         IDCT;
754         out[8 * i + 0] = ITOINT(t0);
755         out[8 * i + 1] = ITOINT(t1);
756         out[8 * i + 2] = ITOINT(t2);
757         out[8 * i + 3] = ITOINT(t3);
758         out[8 * i + 4] = ITOINT(t4);
759         out[8 * i + 5] = ITOINT(t5);
760         out[8 * i + 6] = ITOINT(t6);
761         out[8 * i + 7] = ITOINT(t7);
762     }
763 }
764
765 static unsigned char zig[64] = {
766      0,  1,  5,  6, 14, 15, 27, 28,
767      2,  4,  7, 13, 16, 26, 29, 42,
768      3,  8, 12, 17, 25, 30, 41, 43,
769      9, 11, 18, 24, 31, 40, 44, 53,
770     10, 19, 23, 32, 39, 45, 52, 54,
771     20, 22, 33, 38, 46, 51, 55, 60,
772     21, 34, 37, 47, 50, 56, 59, 61,
773     35, 36, 48, 49, 57, 58, 62, 63
774 };
775
776 static PREC aaidct[8] = {
777     IFIX(0.3535533906), IFIX(0.4903926402),
778     IFIX(0.4619397663), IFIX(0.4157348062),
779     IFIX(0.3535533906), IFIX(0.2777851165),
780     IFIX(0.1913417162), IFIX(0.0975451610)
781 };
782
783
784 static void idctqtab(unsigned char *qin, PREC * qout)
785 {
786     int i, j;
787
788     for (i = 0; i < 8; i++)
789         for (j = 0; j < 8; j++)
790             qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] *
791                 IMULT(aaidct[i], aaidct[j]);
792 }
793
794 static void scaleidctqtab(PREC * q, PREC sc)
795 {
796     int i;
797
798     for (i = 0; i < 64; i++)
799         q[i] = IMULT(q[i], sc);
800 }
801
802 /****************************************************************/
803 /**************          color decoder            ***************/
804 /****************************************************************/
805
806 #define ROUND
807
808 /*
809  * YCbCr Color transformation:
810  *
811  * y:0..255   Cb:-128..127   Cr:-128..127
812  *
813  *      R = Y                + 1.40200 * Cr
814  *      G = Y - 0.34414 * Cb - 0.71414 * Cr
815  *      B = Y + 1.77200 * Cb
816  *
817  * =>
818  *      Cr *= 1.40200;
819  *      Cb *= 1.77200;
820  *      Cg = 0.19421 * Cb + .50937 * Cr;
821  *      R = Y + Cr;
822  *      G = Y - Cg;
823  *      B = Y + Cb;
824  *
825  * =>
826  *      Cg = (50 * Cb + 130 * Cr + 128) >> 8;
827  */
828
829 static void initcol(PREC q[][64])
830 {
831     scaleidctqtab(q[1], IFIX(1.77200));
832     scaleidctqtab(q[2], IFIX(1.40200));
833 }
834
835 /* This is optimized for the stupid sun SUNWspro compiler. */
836 #define STORECLAMP(a,x)                          \
837 (                                                \
838   (a) = (x),                                     \
839   (unsigned int)(x) >= 256 ?                     \
840     ((a) = (x) < 0 ? 0 : 255)                    \
841   :                                              \
842     0                                            \
843 )
844
845 #define CLAMP(x) ((unsigned int)(x) >= 256 ? ((x) < 0 ? 0 : 255) : (x))
846
847 #ifdef ROUND
848
849 #define CBCRCG(yin, xin)                         \
850 (                                                \
851   cb = outc[0 +yin*8+xin],                       \
852   cr = outc[64+yin*8+xin],                       \
853   cg = (50 * cb + 130 * cr + 128) >> 8           \
854 )
855
856 #else
857
858 #define CBCRCG(yin, xin)                         \
859 (                                                \
860   cb = outc[0 +yin*8+xin],                       \
861   cr = outc[64+yin*8+xin],                       \
862   cg = (3 * cb + 8 * cr) >> 4                    \
863 )
864
865 #endif
866
867 #define PIC(yin, xin, p, xout)                   \
868 (                                                \
869   y = outy[(yin) * 8 + xin],                     \
870   STORECLAMP(p[(xout) * 3 + 0], y + cr),         \
871   STORECLAMP(p[(xout) * 3 + 1], y - cg),         \
872   STORECLAMP(p[(xout) * 3 + 2], y + cb)          \
873 )
874
875 #ifdef __LITTLE_ENDIAN
876 #define PIC_16(yin, xin, p, xout, add)           \
877 (                                                \
878   y = outy[(yin) * 8 + xin],                     \
879   y = ((CLAMP(y + cr + add*2+1) & 0xf8) <<  8) | \
880       ((CLAMP(y - cg + add    ) & 0xfc) <<  3) | \
881       ((CLAMP(y + cb + add*2+1)       ) >>  3),  \
882   p[(xout) * 2 + 0] = y & 0xff,                  \
883   p[(xout) * 2 + 1] = y >> 8                     \
884 )
885 #else
886 #ifdef CONFIG_PPC
887 #define PIC_16(yin, xin, p, xout, add)           \
888 (                                                \
889   y = outy[(yin) * 8 + xin],                     \
890   y = ((CLAMP(y + cr + add*2+1) & 0xf8) <<  7) | \
891       ((CLAMP(y - cg + add*2+1) & 0xf8) <<  2) | \
892       ((CLAMP(y + cb + add*2+1)       ) >>  3),  \
893   p[(xout) * 2 + 0] = y >> 8,                    \
894   p[(xout) * 2 + 1] = y & 0xff                   \
895 )
896 #else
897 #define PIC_16(yin, xin, p, xout, add)           \
898 (                                                \
899   y = outy[(yin) * 8 + xin],                     \
900   y = ((CLAMP(y + cr + add*2+1) & 0xf8) <<  8) | \
901       ((CLAMP(y - cg + add    ) & 0xfc) <<  3) | \
902       ((CLAMP(y + cb + add*2+1)       ) >>  3),  \
903   p[(xout) * 2 + 0] = y >> 8,                    \
904   p[(xout) * 2 + 1] = y & 0xff                   \
905 )
906 #endif
907 #endif
908
909 #define PIC_32(yin, xin, p, xout)               \
910 (                                               \
911   y = outy[(yin) * 8 + xin],                    \
912   STORECLAMP(p[(xout) * 4 + 0], y + cr),        \
913   STORECLAMP(p[(xout) * 4 + 1], y - cg),        \
914   STORECLAMP(p[(xout) * 4 + 2], y + cb),        \
915   p[(xout) * 4 + 3] = 0                         \
916 )
917
918 #define PIC221111(xin)                                              \
919 (                                                                   \
920   CBCRCG(0, xin),                                                   \
921   PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0),       \
922   PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1),       \
923   PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0),       \
924   PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1)        \
925 )
926
927 #define PIC221111_16(xin)                                           \
928 (                                                                   \
929   CBCRCG(0, xin),                                                   \
930   PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0, 3), \
931   PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1, 0), \
932   PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0, 1), \
933   PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1, 2)  \
934 )
935
936 #define PIC221111_32(xin)                                           \
937 (                                                                   \
938   CBCRCG(0, xin),                                                   \
939   PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0),    \
940   PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1),    \
941   PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0),    \
942   PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1)     \
943 )
944
945 static void col221111(int *out, unsigned char *pic, int width)
946 {
947     int i, j, k;
948     unsigned char *pic0, *pic1;
949     int *outy, *outc;
950     int cr, cg, cb, y;
951
952     pic0 = pic;
953     pic1 = pic + width;
954     outy = out;
955     outc = out + 64 * 4;
956     for (i = 2; i > 0; i--) {
957         for (j = 4; j > 0; j--) {
958             for (k = 0; k < 8; k++) {
959                 PIC221111(k);
960             }
961             outc += 8;
962             outy += 16;
963             pic0 += 2 * width;
964             pic1 += 2 * width;
965         }
966         outy += 64 * 2 - 16 * 4;
967     }
968 }
969
970 static void col221111_16(int *out, unsigned char *pic, int width)
971 {
972     int i, j, k;
973     unsigned char *pic0, *pic1;
974     int *outy, *outc;
975     int cr, cg, cb, y;
976
977     pic0 = pic;
978     pic1 = pic + width;
979     outy = out;
980     outc = out + 64 * 4;
981     for (i = 2; i > 0; i--) {
982         for (j = 4; j > 0; j--) {
983             for (k = 0; k < 8; k++) {
984                 PIC221111_16(k);
985             }
986             outc += 8;
987             outy += 16;
988             pic0 += 2 * width;
989             pic1 += 2 * width;
990         }
991         outy += 64 * 2 - 16 * 4;
992     }
993 }
994
995 static void col221111_32(int *out, unsigned char *pic, int width)
996 {
997     int i, j, k;
998     unsigned char *pic0, *pic1;
999     int *outy, *outc;
1000     int cr, cg, cb, y;
1001
1002     pic0 = pic;
1003     pic1 = pic + width;
1004     outy = out;
1005     outc = out + 64 * 4;
1006     for (i = 2; i > 0; i--) {
1007         for (j = 4; j > 0; j--) {
1008             for (k = 0; k < 8; k++) {
1009                 PIC221111_32(k);
1010             }
1011             outc += 8;
1012             outy += 16;
1013             pic0 += 2 * width;
1014             pic1 += 2 * width;
1015         }
1016         outy += 64 * 2 - 16 * 4;
1017     }
1018 }