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