blob: 511f63e8d084d94bb4f6b9c0e699ae8c20757568 [file] [log] [blame]
Stefan Reinauerd650e992010-02-22 04:33:13 +00001/*
2 * This file is part of the coreboot project.
Uwe Hermann548dbe72010-02-22 16:41:49 +00003 *
4 * Copyright (C) 2001 Michael Schroeder
Stefan Reinauerd650e992010-02-22 04:33:13 +00005 *
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
Uwe Hermann548dbe72010-02-22 16:41:49 +00008 * published by the Free Software Foundation; version 2 of the License.
Stefan Reinauerd650e992010-02-22 04:33:13 +00009 *
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.
Stefan Reinauerd650e992010-02-22 04:33:13 +000014 */
15
16/*
17 * a tiny jpeg decoder.
18 *
19 * written in August 2001 by Michael Schroeder <mls@suse.de>
20 *
21 */
22
23#define __LITTLE_ENDIAN
24#include <string.h>
25#include "jpeg.h"
26#define ISHIFT 11
27
28#define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
29#define IMULT(a, b) (((a) * (b)) >> ISHIFT)
30#define ITOINT(a) ((a) >> ISHIFT)
31
32#ifndef __P
33# define __P(x) x
34#endif
35
36/* special markers */
37#define M_BADHUFF -1
38#define M_EOF 0x80
39
40struct in {
41 unsigned char *p;
42 unsigned int bits;
43 int left;
44 int marker;
45
46 int (*func) __P((void *));
47 void *data;
48};
49
50/*********************************/
51struct dec_hufftbl;
52struct enc_hufftbl;
53
54union hufftblp {
55 struct dec_hufftbl *dhuff;
56 struct enc_hufftbl *ehuff;
57};
58
59struct scan {
60 int dc; /* old dc value */
61
62 union hufftblp hudc;
63 union hufftblp huac;
64 int next; /* when to switch to next scan */
65
66 int cid; /* component id */
67 int hv; /* horiz/vert, copied from comp */
68 int tq; /* quant tbl, copied from comp */
69};
70
71/*********************************/
72
73#define DECBITS 10 /* seems to be the optimum */
74
75struct dec_hufftbl {
76 int maxcode[17];
77 int valptr[16];
78 unsigned char vals[256];
79 unsigned int llvals[1 << DECBITS];
80};
81
82static void decode_mcus __P((struct in *, int *, int, struct scan *, int *));
83static int dec_readmarker __P((struct in *));
84static void dec_makehuff __P((struct dec_hufftbl *, int *, unsigned char *));
85
86static void setinput __P((struct in *, unsigned char *));
87/*********************************/
88
89#undef PREC
90#define PREC int
91
92static void idctqtab __P((unsigned char *, PREC *));
93static void idct __P((int *, int *, PREC *, PREC, int));
94static void scaleidctqtab __P((PREC *, PREC));
95
96/*********************************/
97
98static void initcol __P((PREC[][64]));
99
100static void col221111 __P((int *, unsigned char *, int));
101static void col221111_16 __P((int *, unsigned char *, int));
102static void col221111_32 __P((int *, unsigned char *, int));
103
104/*********************************/
105
106#define M_SOI 0xd8
107#define M_APP0 0xe0
108#define M_DQT 0xdb
109#define M_SOF0 0xc0
110#define M_DHT 0xc4
111#define M_DRI 0xdd
112#define M_SOS 0xda
113#define M_RST0 0xd0
114#define M_EOI 0xd9
115#define M_COM 0xfe
116
117static unsigned char *datap;
118
119static int getbyte(void)
120{
121 return *datap++;
122}
123
124static int getword(void)
125{
126 int c1, c2;
127 c1 = *datap++;
128 c2 = *datap++;
129 return c1 << 8 | c2;
130}
131
132struct comp {
133 int cid;
134 int hv;
135 int tq;
136};
137
138#define MAXCOMP 4
139struct jpginfo {
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 */
145};
146
147static struct jpginfo info;
148static struct comp comps[MAXCOMP];
149
150static struct scan dscans[MAXCOMP];
151
152static unsigned char quant[4][64];
153
154static struct dec_hufftbl dhuff[4];
155
156#define dec_huffdc (dhuff + 0)
157#define dec_huffac (dhuff + 2)
158
Stefan Reinauer87489e12010-03-17 04:03:22 +0000159static struct in glob_in;
Stefan Reinauerd650e992010-02-22 04:33:13 +0000160
161static int readtables(int till)
162{
163 int m, l, i, j, lq, pq, tq;
164 int tc, th, tt;
165
166 for (;;) {
167 if (getbyte() != 0xff)
168 return -1;
169 if ((m = getbyte()) == till)
170 break;
171
172 switch (m) {
173 case 0xc2:
174 return 0;
175
176 case M_DQT:
177 lq = getword();
178 while (lq > 2) {
179 pq = getbyte();
180 tq = pq & 15;
181 if (tq > 3)
182 return -1;
183 pq >>= 4;
184 if (pq != 0)
185 return -1;
186 for (i = 0; i < 64; i++)
187 quant[tq][i] = getbyte();
188 lq -= 64 + 1;
189 }
190 break;
191
192 case M_DHT:
193 l = getword();
194 while (l > 2) {
195 int hufflen[16], k;
196 unsigned char huffvals[256];
197
198 tc = getbyte();
199 th = tc & 15;
200 tc >>= 4;
201 tt = tc * 2 + th;
202 if (tc > 1 || th > 1)
203 return -1;
204 for (i = 0; i < 16; i++)
205 hufflen[i] = getbyte();
206 l -= 1 + 16;
207 k = 0;
208 for (i = 0; i < 16; i++) {
209 for (j = 0; j < hufflen[i]; j++)
210 huffvals[k++] = getbyte();
211 l -= hufflen[i];
212 }
213 dec_makehuff(dhuff + tt, hufflen,
214 huffvals);
215 }
216 break;
217
218 case M_DRI:
219 l = getword();
220 info.dri = getword();
221 break;
222
223 default:
224 l = getword();
225 while (l-- > 2)
226 getbyte();
227 break;
228 }
229 }
230 return 0;
231}
232
233static void dec_initscans(void)
234{
235 int i;
236
237 info.nm = info.dri + 1;
238 info.rm = M_RST0;
239 for (i = 0; i < info.ns; i++)
240 dscans[i].dc = 0;
241}
242
243static int dec_checkmarker(void)
244{
245 int i;
246
Stefan Reinauer87489e12010-03-17 04:03:22 +0000247 if (dec_readmarker(&glob_in) != info.rm)
Stefan Reinauerd650e992010-02-22 04:33:13 +0000248 return -1;
249 info.nm = info.dri;
250 info.rm = (info.rm + 1) & ~0x08;
251 for (i = 0; i < info.ns; i++)
252 dscans[i].dc = 0;
253 return 0;
254}
255
Patrick Georgi246179a2015-08-09 18:23:10 +0200256void jpeg_fetch_size(unsigned char *buf, int *width, int *height)
257{
258 datap = buf;
259 getbyte();
260 getbyte();
261 readtables(M_SOF0);
262 getword();
263 getbyte();
264 *height = getword();
265 *width = getword();
266}
267
Stefan Reinauerd650e992010-02-22 04:33:13 +0000268int jpeg_check_size(unsigned char *buf, int width, int height)
269{
270 datap = buf;
271 getbyte();
272 getbyte();
273 readtables(M_SOF0);
274 getword();
275 getbyte();
276 if (height != getword() || width != getword())
277 return 0;
278 return 1;
279}
280
Stefan Reinauer14e22772010-04-27 06:56:47 +0000281int jpeg_decode(unsigned char *buf, unsigned char *pic,
Stefan Reinauerd650e992010-02-22 04:33:13 +0000282 int width, int height, int depth, struct jpeg_decdata *decdata)
283{
284 int i, j, m, tac, tdc;
285 int mcusx, mcusy, mx, my;
286 int max[6];
287
288 if (!decdata || !buf || !pic)
289 return -1;
290 datap = buf;
291 if (getbyte() != 0xff)
292 return ERR_NO_SOI;
293 if (getbyte() != M_SOI)
294 return ERR_NO_SOI;
295 if (readtables(M_SOF0))
296 return ERR_BAD_TABLES;
297 getword();
298 i = getbyte();
299 if (i != 8)
300 return ERR_NOT_8BIT;
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;
307 info.nc = getbyte();
308 if (info.nc > MAXCOMP)
309 return ERR_TOO_MANY_COMPPS;
310 for (i = 0; i < info.nc; i++) {
311 int h, v;
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();
317 if (h > 3 || v > 3)
318 return ERR_ILLEGAL_HV;
319 if (comps[i].tq > 3)
320 return ERR_QUANT_TABLE_SELECTOR;
321 }
322 if (readtables(M_SOS))
323 return ERR_BAD_TABLES;
324 getword();
325 info.ns = getbyte();
326 if (info.ns != 3)
327 return ERR_NOT_YCBCR_221111;
328 for (i = 0; i < 3; i++) {
329 dscans[i].cid = getbyte();
330 tdc = getbyte();
331 tac = tdc & 15;
332 tdc >>= 4;
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)
337 break;
338 if (j == info.nc)
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;
344 }
345
346 i = getbyte();
347 j = getbyte();
348 m = getbyte();
349
350 if (i != 0 || j != 63 || m != 0)
351 return ERR_NOT_SEQUENTIAL_DCT;
352
353 if (dscans[0].cid != 1 || dscans[1].cid != 2 || dscans[2].cid != 3)
354 return ERR_NOT_YCBCR_221111;
355
356 if (dscans[0].hv != 0x22 || dscans[1].hv != 0x11 || dscans[2].hv != 0x11)
357 return ERR_NOT_YCBCR_221111;
358
359 mcusx = width >> 4;
360 mcusy = height >> 4;
361
362
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);
Stefan Reinauer87489e12010-03-17 04:03:22 +0000367 setinput(&glob_in, datap);
Stefan Reinauerd650e992010-02-22 04:33:13 +0000368
369#if 0
370 /* landing zone */
371 img[len] = 0;
372 img[len + 1] = 0xff;
373 img[len + 2] = M_EOF;
374#endif
375
376 dec_initscans();
377
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;
386
Stefan Reinauer87489e12010-03-17 04:03:22 +0000387 decode_mcus(&glob_in, decdata->dcts, 6, dscans, max);
Stefan Reinauerd650e992010-02-22 04:33:13 +0000388 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]);
394
395 switch (depth) {
396 case 32:
397 col221111_32(decdata->out, pic + (my * 16 * mcusx + mx) * 16 * 4, mcusx * 16 * 4);
398 break;
399 case 24:
400 col221111(decdata->out, pic + (my * 16 * mcusx + mx) * 16 * 3, mcusx * 16 * 3);
401 break;
402 case 16:
403 col221111_16(decdata->out, pic + (my * 16 * mcusx + mx) * (16 * 2), mcusx * (16 * 2));
404 break;
405 default:
406 return ERR_DEPTH_MISMATCH;
407 break;
408 }
409 }
410 }
411
Stefan Reinauer87489e12010-03-17 04:03:22 +0000412 m = dec_readmarker(&glob_in);
Stefan Reinauerd650e992010-02-22 04:33:13 +0000413 if (m != M_EOI)
414 return ERR_NO_EOI;
415
416 return 0;
417}
418
419/****************************************************************/
420/************** huffman decoder ***************/
421/****************************************************************/
422
423static int fillbits __P((struct in *, int, unsigned int));
424static int dec_rec2
425__P((struct in *, struct dec_hufftbl *, int *, int, int));
426
427static void setinput(struct in *in, unsigned char *p)
428{
429 in->p = p;
430 in->left = 0;
431 in->bits = 0;
432 in->marker = 0;
433}
434
435static int fillbits(struct in *in, int le, unsigned int bi)
436{
437 int b, m;
438
439 if (in->marker) {
440 if (le <= 16)
441 in->bits = bi << 16, le += 16;
442 return le;
443 }
444 while (le <= 24) {
445 b = *in->p++;
446 if (b == 0xff && (m = *in->p++) != 0) {
447 if (m == M_EOF) {
448 if (in->func && (m = in->func(in->data)) == 0)
449 continue;
450 }
451 in->marker = m;
452 if (le <= 16)
453 bi = bi << 16, le += 16;
454 break;
455 }
456 bi = bi << 8 | b;
457 le += 8;
458 }
459 in->bits = bi; /* tmp... 2 return values needed */
460 return le;
461}
462
463static int dec_readmarker(struct in *in)
464{
465 int m;
466
467 in->left = fillbits(in, in->left, in->bits);
468 if ((m = in->marker) == 0)
469 return 0;
470 in->left = 0;
471 in->marker = 0;
472 return m;
473}
474
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)
478
479#define GETBITS(in, n) ( \
480 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
481 (le -= (n)), \
482 bi >> le & ((1 << (n)) - 1) \
483)
484
485#define UNGETBITS(in, n) ( \
486 le += (n) \
487)
488
489
490static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp, int c,
491 int i)
492{
493 LEBI_DCL;
494
495 LEBI_GET(in);
496 if (i) {
497 UNGETBITS(in, i & 127);
498 *runp = i >> 8 & 15;
499 i >>= 16;
500 } else {
501 for (i = DECBITS; (c = ((c << 1) | GETBITS(in, 1))) >= (hu->maxcode[i]); i++);
502 if (i >= 16) {
503 in->marker = M_BADHUFF;
504 return 0;
505 }
506 i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
507 *runp = i >> 4;
508 i &= 15;
509 }
510 if (i == 0) { /* sigh, 0xf0 is 11 bit */
511 LEBI_PUT(in);
512 return 0;
513 }
514 /* receive part */
515 c = GETBITS(in, i);
516 if (c < (1 << (i - 1)))
517 c += (-1 << i) + 1;
518 LEBI_PUT(in);
519 return c;
520}
521
522#define DEC_REC(in, hu, r, i) ( \
523 r = GETBITS(in, DECBITS), \
524 i = hu->llvals[r], \
525 i & 128 ? \
526 ( \
527 UNGETBITS(in, i & 127), \
528 r = i >> 8 & 15, \
529 i >> 16 \
530 ) \
531 : \
532 ( \
533 LEBI_PUT(in), \
534 i = dec_rec2(in, hu, &r, r, i), \
535 LEBI_GET(in), \
536 i \
537 ) \
538)
539
540static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc, int *maxp)
541{
542 struct dec_hufftbl *hu;
543 int i, r, t;
544 LEBI_DCL;
545
546 memset(dct, 0, n * 64 * sizeof(*dct));
547 LEBI_GET(in);
548 while (n-- > 0) {
549 hu = sc->hudc.dhuff;
550 *dct++ = (sc->dc += DEC_REC(in, hu, r, t));
551
552 hu = sc->huac.dhuff;
553 i = 63;
554 while (i > 0) {
555 t = DEC_REC(in, hu, r, t);
556 if (t == 0 && r == 0) {
557 dct += i;
558 break;
559 }
560 dct += r;
561 *dct++ = t;
562 i -= r + 1;
563 }
564 *maxp++ = 64 - i;
565 if (n == sc->next)
566 sc++;
567 }
568 LEBI_PUT(in);
569}
570
571static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen, unsigned char *huffvals)
572{
573 int code, k, i, j, d, x, c, v;
574 for (i = 0; i < (1 << DECBITS); i++)
575 hu->llvals[i] = 0;
576
577/*
578 * llvals layout:
579 *
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
586 */
587 code = 0;
588 k = 0;
589 for (i = 0; i < 16; i++, code <<= 1) { /* sizes */
590 hu->valptr[i] = k;
591 for (j = 0; j < hufflen[i]; j++) {
592 hu->vals[k] = *huffvals++;
593 if (i < DECBITS) {
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 -
599 i);
600 if (v && x < (1 << (v - 1)))
601 x += (-1 << v) + 1;
602 x = x << 16 | (hu-> vals[k] & 0xf0) << 4 |
603 (DECBITS - (i + 1 + v)) | 128;
604 } else
605 x = v << 16 | (hu-> vals[k] & 0xf0) << 4 |
606 (DECBITS - (i + 1));
607 hu->llvals[c | d] = x;
608 }
609 }
610 code++;
611 k++;
612 }
613 hu->maxcode[i] = code;
614 }
615 hu->maxcode[16] = 0x20000; /* always terminate decode */
616}
617
618/****************************************************************/
619/************** idct ***************/
620/****************************************************************/
621
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))
626
627#define S22 ((PREC)IFIX(2 * 0.382683432))
628#define C22 ((PREC)IFIX(2 * 0.923879532))
629#define IC4 ((PREC)IFIX(1 / 0.707106781))
630
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 */
634
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)
638
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)
642
643#define IDCT \
644( \
645 XPP(t0, t1), \
646 XMP(t2, t3), \
647 t2 = IMULT(t2, IC4) - t3, \
648 XPP(t0, t3), \
649 XPP(t1, t2), \
650 XMP(t4, t7), \
651 XPP(t5, t6), \
652 XMP(t5, t7), \
653 t5 = IMULT(t5, IC4), \
654 ROT(t4, t6, S22, C22),\
655 t6 -= t7, \
656 t5 -= t6, \
657 t4 -= t5, \
658 XPP(t0, t7), \
659 XPP(t1, t6), \
660 XPP(t2, t5), \
661 XPP(t3, t4) \
662)
663
664static 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
673};
674
Stefan Reinauer87489e12010-03-17 04:03:22 +0000675void idct(int *in, int *out, PREC *lquant, PREC off, int max)
Stefan Reinauerd650e992010-02-22 04:33:13 +0000676{
677 PREC t0, t1, t2, t3, t4, t5, t6, t7, t;
678 PREC tmp[64], *tmpp;
679 int i, j;
680 unsigned char *zig2p;
681
682 t0 = off;
683 if (max == 1) {
Stefan Reinauer87489e12010-03-17 04:03:22 +0000684 t0 += in[0] * lquant[0];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000685 for (i = 0; i < 64; i++)
686 out[i] = ITOINT(t0);
687 return;
688 }
689 zig2p = zig2;
690 tmpp = tmp;
691 for (i = 0; i < 8; i++) {
692 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000693 t0 += in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000694 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000695 t5 = in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000696 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000697 t2 = in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000698 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000699 t7 = in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000700 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000701 t1 = in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000702 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000703 t4 = in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000704 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000705 t3 = in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000706 j = *zig2p++;
Stefan Reinauer87489e12010-03-17 04:03:22 +0000707 t6 = in[j] * lquant[j];
Stefan Reinauerd650e992010-02-22 04:33:13 +0000708 IDCT;
709 tmpp[0 * 8] = t0;
710 tmpp[1 * 8] = t1;
711 tmpp[2 * 8] = t2;
712 tmpp[3 * 8] = t3;
713 tmpp[4 * 8] = t4;
714 tmpp[5 * 8] = t5;
715 tmpp[6 * 8] = t6;
716 tmpp[7 * 8] = t7;
717 tmpp++;
718 t0 = 0;
719 }
720 for (i = 0; i < 8; i++) {
721 t0 = tmp[8 * i + 0];
722 t1 = tmp[8 * i + 1];
723 t2 = tmp[8 * i + 2];
724 t3 = tmp[8 * i + 3];
725 t4 = tmp[8 * i + 4];
726 t5 = tmp[8 * i + 5];
727 t6 = tmp[8 * i + 6];
728 t7 = tmp[8 * i + 7];
729 IDCT;
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);
738 }
739}
740
741static 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
750};
751
752static 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)
757};
758
759
760static void idctqtab(unsigned char *qin, PREC *qout)
761{
762 int i, j;
763
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]);
768}
769
770static void scaleidctqtab(PREC *q, PREC sc)
771{
772 int i;
773
774 for (i = 0; i < 64; i++)
775 q[i] = IMULT(q[i], sc);
776}
777
778/****************************************************************/
779/************** color decoder ***************/
780/****************************************************************/
781
782#define ROUND
783
784/*
785 * YCbCr Color transformation:
786 *
787 * y:0..255 Cb:-128..127 Cr:-128..127
788 *
789 * R = Y + 1.40200 * Cr
790 * G = Y - 0.34414 * Cb - 0.71414 * Cr
791 * B = Y + 1.77200 * Cb
792 *
793 * =>
794 * Cr *= 1.40200;
795 * Cb *= 1.77200;
796 * Cg = 0.19421 * Cb + .50937 * Cr;
797 * R = Y + Cr;
798 * G = Y - Cg;
799 * B = Y + Cb;
800 *
801 * =>
802 * Cg = (50 * Cb + 130 * Cr + 128) >> 8;
803 */
804
805static void initcol(PREC q[][64])
806{
807 scaleidctqtab(q[1], IFIX(1.77200));
808 scaleidctqtab(q[2], IFIX(1.40200));
809}
810
811/* This is optimized for the stupid sun SUNWspro compiler. */
812#define STORECLAMP(a,x) \
813( \
814 (a) = (x), \
815 (unsigned int)(x) >= 256 ? \
816 ((a) = (x) < 0 ? 0 : 255) \
817 : \
818 0 \
819)
820
821#define CLAMP(x) ((unsigned int)(x) >= 256 ? ((x) < 0 ? 0 : 255) : (x))
822
823#ifdef ROUND
824
825#define CBCRCG(yin, xin) \
826( \
827 cb = outc[0 +yin*8+xin], \
828 cr = outc[64+yin*8+xin], \
829 cg = (50 * cb + 130 * cr + 128) >> 8 \
830)
831
832#else
833
834#define CBCRCG(yin, xin) \
835( \
836 cb = outc[0 +yin*8+xin], \
837 cr = outc[64+yin*8+xin], \
838 cg = (3 * cb + 8 * cr) >> 4 \
839)
840
841#endif
842
843#define PIC(yin, xin, p, xout) \
844( \
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) \
849)
850
851#ifdef __LITTLE_ENDIAN
852#define PIC_16(yin, xin, p, xout, add) \
853( \
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 \
860)
861#else
862#ifdef CONFIG_PPC
863#define PIC_16(yin, xin, p, xout, add) \
864( \
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 \
871)
872#else
873#define PIC_16(yin, xin, p, xout, add) \
874( \
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 \
881)
882#endif
883#endif
884
885#define PIC_32(yin, xin, p, xout) \
886( \
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 \
892)
893
894#define PIC221111(xin) \
895( \
896 CBCRCG(0, 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) \
901)
902
903#define PIC221111_16(xin) \
904( \
905 CBCRCG(0, 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) \
910)
911
912#define PIC221111_32(xin) \
913( \
914 CBCRCG(0, 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) \
919)
920
921static void col221111(int *out, unsigned char *pic, int width)
922{
923 int i, j, k;
924 unsigned char *pic0, *pic1;
925 int *outy, *outc;
926 int cr, cg, cb, y;
927
928 pic0 = pic;
929 pic1 = pic + width;
930 outy = out;
931 outc = out + 64 * 4;
932 for (i = 2; i > 0; i--) {
933 for (j = 4; j > 0; j--) {
934 for (k = 0; k < 8; k++) {
935 PIC221111(k);
936 }
937 outc += 8;
938 outy += 16;
939 pic0 += 2 * width;
940 pic1 += 2 * width;
941 }
942 outy += 64 * 2 - 16 * 4;
943 }
944}
945
946static void col221111_16(int *out, unsigned char *pic, int width)
947{
948 int i, j, k;
949 unsigned char *pic0, *pic1;
950 int *outy, *outc;
951 int cr, cg, cb, y;
952
953 pic0 = pic;
954 pic1 = pic + width;
955 outy = out;
956 outc = out + 64 * 4;
957 for (i = 2; i > 0; i--) {
958 for (j = 4; j > 0; j--) {
959 for (k = 0; k < 8; k++) {
960 PIC221111_16(k);
961 }
962 outc += 8;
963 outy += 16;
964 pic0 += 2 * width;
965 pic1 += 2 * width;
966 }
967 outy += 64 * 2 - 16 * 4;
968 }
969}
970
971static void col221111_32(int *out, unsigned char *pic, int width)
972{
973 int i, j, k;
974 unsigned char *pic0, *pic1;
975 int *outy, *outc;
976 int cr, cg, cb, y;
977
978 pic0 = pic;
979 pic1 = pic + width;
980 outy = out;
981 outc = out + 64 * 4;
982 for (i = 2; i > 0; i--) {
983 for (j = 4; j > 0; j--) {
984 for (k = 0; k < 8; k++) {
985 PIC221111_32(k);
986 }
987 outc += 8;
988 outy += 16;
989 pic0 += 2 * width;
990 pic1 += 2 * width;
991 }
992 outy += 64 * 2 - 16 * 4;
993 }
994}